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>
40 #include <libcfs/kp30.h>
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <lustre_net.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");
53 CFS_MODULE_PARM(at_min, "i", int, 0644,
54 "Adaptive timeout minimum (sec)");
55 CFS_MODULE_PARM(at_max, "i", int, 0644,
56 "Adaptive timeout maximum (sec)");
57 CFS_MODULE_PARM(at_history, "i", int, 0644,
58 "Adaptive timeouts remember the slowest event that took place "
59 "within this period (sec)");
60 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
61 "How soon before an RPC deadline to send an early reply");
62 CFS_MODULE_PARM(at_extra, "i", int, 0644,
63 "How much extra time to give with each early reply");
66 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
68 static CFS_LIST_HEAD (ptlrpc_all_services);
69 spinlock_t ptlrpc_all_services_lock;
72 ptlrpc_alloc_request_buffer (int size)
76 if (size > SVC_BUF_VMALLOC_THRESHOLD)
77 OBD_VMALLOC(ptr, size);
85 ptlrpc_free_request_buffer (char *ptr, int size)
87 if (size > SVC_BUF_VMALLOC_THRESHOLD)
93 struct ptlrpc_request_buffer_desc *
94 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
96 struct ptlrpc_request_buffer_desc *rqbd;
98 OBD_ALLOC(rqbd, sizeof (*rqbd));
102 rqbd->rqbd_service = svc;
103 rqbd->rqbd_refcount = 0;
104 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
105 rqbd->rqbd_cbid.cbid_arg = rqbd;
106 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
107 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
109 if (rqbd->rqbd_buffer == NULL) {
110 OBD_FREE(rqbd, sizeof (*rqbd));
114 spin_lock(&svc->srv_lock);
115 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
117 spin_unlock(&svc->srv_lock);
123 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
125 struct ptlrpc_service *svc = rqbd->rqbd_service;
127 LASSERT (rqbd->rqbd_refcount == 0);
128 LASSERT (list_empty(&rqbd->rqbd_reqs));
130 spin_lock(&svc->srv_lock);
131 list_del(&rqbd->rqbd_list);
133 spin_unlock(&svc->srv_lock);
135 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
136 OBD_FREE (rqbd, sizeof (*rqbd));
140 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
142 struct ptlrpc_request_buffer_desc *rqbd;
145 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
146 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
147 svc->srv_nrqbd_receiving, svc->srv_nbufs);
148 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
149 rqbd = ptlrpc_alloc_rqbd(svc);
152 CERROR ("%s: Can't allocate request buffer\n",
157 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
165 ptlrpc_save_lock(struct ptlrpc_request *req,
166 struct lustre_handle *lock, int mode)
168 struct ptlrpc_reply_state *rs = req->rq_reply_state;
172 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
174 if (req->rq_export->exp_disconnected) {
175 ldlm_lock_decref(lock, mode);
177 idx = rs->rs_nlocks++;
178 rs->rs_locks[idx] = *lock;
179 rs->rs_modes[idx] = mode;
180 rs->rs_difficult = 1;
185 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
187 struct ptlrpc_service *svc = rs->rs_service;
189 LASSERT_SPIN_LOCKED (&svc->srv_lock);
190 LASSERT (rs->rs_difficult);
191 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
193 if (rs->rs_scheduled) /* being set up or already notified */
196 rs->rs_scheduled = 1;
197 list_del (&rs->rs_list);
198 list_add (&rs->rs_list, &svc->srv_reply_queue);
199 cfs_waitq_signal (&svc->srv_waitq);
203 ptlrpc_commit_replies (struct obd_export *exp)
205 struct list_head *tmp;
206 struct list_head *nxt;
208 /* Find any replies that have been committed and get their service
209 * to attend to complete them. */
211 /* CAVEAT EMPTOR: spinlock ordering!!! */
212 spin_lock(&exp->exp_uncommitted_replies_lock);
214 list_for_each_safe(tmp, nxt, &exp->exp_uncommitted_replies) {
215 struct ptlrpc_reply_state *rs =
216 list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
218 LASSERT(rs->rs_difficult);
219 /* VBR: per-export last_committed */
220 LASSERT(rs->rs_export);
221 if (rs->rs_transno <= rs->rs_export->exp_last_committed) {
222 struct ptlrpc_service *svc = rs->rs_service;
224 spin_lock (&svc->srv_lock);
225 list_del_init (&rs->rs_obd_list);
226 ptlrpc_schedule_difficult_reply (rs);
227 spin_unlock (&svc->srv_lock);
231 spin_unlock(&exp->exp_uncommitted_replies_lock);
235 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
237 struct ptlrpc_request_buffer_desc *rqbd;
242 spin_lock(&svc->srv_lock);
244 if (list_empty (&svc->srv_idle_rqbds)) {
245 spin_unlock(&svc->srv_lock);
249 rqbd = list_entry(svc->srv_idle_rqbds.next,
250 struct ptlrpc_request_buffer_desc,
252 list_del (&rqbd->rqbd_list);
254 /* assume we will post successfully */
255 svc->srv_nrqbd_receiving++;
256 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
258 spin_unlock(&svc->srv_lock);
260 rc = ptlrpc_register_rqbd(rqbd);
267 spin_lock(&svc->srv_lock);
269 svc->srv_nrqbd_receiving--;
270 list_del(&rqbd->rqbd_list);
271 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
273 /* Don't complain if no request buffers are posted right now; LNET
274 * won't drop requests because we set the portal lazy! */
276 spin_unlock(&svc->srv_lock);
281 static void ptlrpc_at_timer(unsigned long castmeharder)
283 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
284 svc->srv_at_check = 1;
285 svc->srv_at_checktime = cfs_time_current();
286 cfs_waitq_signal(&svc->srv_waitq);
289 /* @threadname should be 11 characters or less - 3 will be added on */
290 struct ptlrpc_service *
291 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
292 int req_portal, int rep_portal, int watchdog_factor,
293 svc_handler_t handler, char *name,
294 cfs_proc_dir_entry_t *proc_entry,
295 svcreq_printfn_t svcreq_printfn,
296 int min_threads, int max_threads, char *threadname,
297 svc_hpreq_handler_t hp_handler)
300 struct ptlrpc_at_array *array;
301 struct ptlrpc_service *service;
302 unsigned int size, index;
306 LASSERT (bufsize >= max_req_size);
308 OBD_ALLOC(service, sizeof(*service));
312 /* First initialise enough for early teardown */
314 service->srv_name = name;
315 spin_lock_init(&service->srv_lock);
316 CFS_INIT_LIST_HEAD(&service->srv_threads);
317 cfs_waitq_init(&service->srv_waitq);
319 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
320 service->srv_max_req_size = max_req_size;
321 service->srv_buf_size = bufsize;
322 service->srv_rep_portal = rep_portal;
323 service->srv_req_portal = req_portal;
324 service->srv_watchdog_factor = watchdog_factor;
325 service->srv_handler = handler;
326 service->srv_request_history_print_fn = svcreq_printfn;
327 service->srv_request_seq = 1; /* valid seq #s start at 1 */
328 service->srv_request_max_cull_seq = 0;
329 service->srv_threads_min = min_threads;
330 service->srv_threads_max = max_threads;
331 service->srv_thread_name = threadname;
332 service->srv_hpreq_handler = hp_handler;
333 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
334 service->srv_hpreq_count = 0;
335 service->srv_n_hpreq = 0;
337 rc = LNetSetLazyPortal(service->srv_req_portal);
340 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
341 CFS_INIT_LIST_HEAD(&service->srv_request_hpq);
342 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
343 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
344 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
345 CFS_INIT_LIST_HEAD(&service->srv_request_history);
346 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
347 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
348 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
349 cfs_waitq_init(&service->srv_free_rs_waitq);
351 spin_lock_init(&service->srv_at_lock);
352 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
354 array = &service->srv_at_array;
355 size = at_est2timeout(at_max);
356 array->paa_size = size;
357 array->paa_count = 0;
358 array->paa_deadline = -1;
360 /* allocate memory for srv_at_array (ptlrpc_at_array) */
361 OBD_ALLOC(array->paa_reqs_array, sizeof(struct list_head) * size);
362 if (array->paa_reqs_array == NULL)
365 for (index = 0; index < size; index++)
366 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
368 OBD_ALLOC(array->paa_reqs_count, sizeof(__u32) * size);
369 if (array->paa_reqs_count == NULL)
372 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
373 /* At SOW, service time should be quick; 10s seems generous. If client
374 timeout is less than this, we'll be sending an early reply. */
375 at_init(&service->srv_at_estimate, 10, 0);
377 spin_lock (&ptlrpc_all_services_lock);
378 list_add (&service->srv_list, &ptlrpc_all_services);
379 spin_unlock (&ptlrpc_all_services_lock);
381 /* Now allocate the request buffers */
382 rc = ptlrpc_grow_req_bufs(service);
383 /* We shouldn't be under memory pressure at startup, so
384 * fail if we can't post all our buffers at this time. */
388 /* Now allocate pool of reply buffers */
389 /* Increase max reply size to next power of two */
390 service->srv_max_reply_size = 1;
391 while (service->srv_max_reply_size < max_reply_size)
392 service->srv_max_reply_size <<= 1;
394 if (proc_entry != NULL)
395 ptlrpc_lprocfs_register_service(proc_entry, service);
397 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
398 service->srv_name, service->srv_req_portal);
402 ptlrpc_unregister_service(service);
407 * to actually free the request, must be called without holding svc_lock.
408 * note it's caller's responsibility to unlink req->rq_list.
410 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
412 LASSERT(atomic_read(&req->rq_refcount) == 0);
413 LASSERT(list_empty(&req->rq_timed_list));
415 /* DEBUG_REQ() assumes the reply state of a request with a valid
416 * ref will not be destroyed until that reference is dropped. */
417 ptlrpc_req_drop_rs(req);
419 if (req != &req->rq_rqbd->rqbd_req) {
420 /* NB request buffers use an embedded
421 * req if the incoming req unlinked the
422 * MD; this isn't one of them! */
423 OBD_FREE(req, sizeof(*req));
428 * drop a reference count of the request. if it reaches 0, we either
429 * put it into history list, or free it immediately.
431 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
433 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
434 struct ptlrpc_service *svc = rqbd->rqbd_service;
436 struct list_head *tmp;
437 struct list_head *nxt;
439 if (!atomic_dec_and_test(&req->rq_refcount))
442 spin_lock(&svc->srv_at_lock);
443 list_del_init(&req->rq_timed_list);
444 if (req->rq_at_linked) {
445 struct ptlrpc_at_array *array = &svc->srv_at_array;
446 __u32 index = req->rq_at_index;
448 req->rq_at_linked = 0;
449 array->paa_reqs_count[index]--;
452 spin_unlock(&svc->srv_at_lock);
454 /* finalize request */
455 if (req->rq_export) {
456 class_export_put(req->rq_export);
457 req->rq_export = NULL;
460 spin_lock(&svc->srv_lock);
462 svc->srv_n_active_reqs--;
463 list_add(&req->rq_list, &rqbd->rqbd_reqs);
465 refcount = --(rqbd->rqbd_refcount);
467 /* request buffer is now idle: add to history */
468 list_del(&rqbd->rqbd_list);
469 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
470 svc->srv_n_history_rqbds++;
472 /* cull some history?
473 * I expect only about 1 or 2 rqbds need to be recycled here */
474 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
475 rqbd = list_entry(svc->srv_history_rqbds.next,
476 struct ptlrpc_request_buffer_desc,
479 list_del(&rqbd->rqbd_list);
480 svc->srv_n_history_rqbds--;
482 /* remove rqbd's reqs from svc's req history while
483 * I've got the service lock */
484 list_for_each(tmp, &rqbd->rqbd_reqs) {
485 req = list_entry(tmp, struct ptlrpc_request,
487 /* Track the highest culled req seq */
488 if (req->rq_history_seq >
489 svc->srv_request_max_cull_seq)
490 svc->srv_request_max_cull_seq =
492 list_del(&req->rq_history_list);
495 spin_unlock(&svc->srv_lock);
497 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
498 req = list_entry(rqbd->rqbd_reqs.next,
499 struct ptlrpc_request,
501 list_del(&req->rq_list);
502 ptlrpc_server_free_request(req);
505 spin_lock(&svc->srv_lock);
507 * now all reqs including the embedded req has been
508 * disposed, schedule request buffer for re-use.
510 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
511 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
514 spin_unlock(&svc->srv_lock);
515 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
516 /* If we are low on memory, we are not interested in history */
517 list_del(&req->rq_list);
518 list_del_init(&req->rq_history_list);
519 spin_unlock(&svc->srv_lock);
521 ptlrpc_server_free_request(req);
523 spin_unlock(&svc->srv_lock);
528 * to finish a request: stop sending more early replies, and release
529 * the request. should be called after we finished handling the request.
531 static void ptlrpc_server_finish_request(struct ptlrpc_request *req)
533 ptlrpc_server_drop_request(req);
536 /* This function makes sure dead exports are evicted in a timely manner.
537 This function is only called when some export receives a message (i.e.,
538 the network is up.) */
539 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
541 struct obd_export *oldest_exp;
542 time_t oldest_time, new_time;
548 /* Compensate for slow machines, etc, by faking our request time
549 into the future. Although this can break the strict time-ordering
550 of the list, we can be really lazy here - we don't have to evict
551 at the exact right moment. Eventually, all silent exports
552 will make it to the top of the list. */
554 /* Do not pay attention on 1sec or smaller renewals. */
555 new_time = cfs_time_current_sec() + extra_delay;
556 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
559 exp->exp_last_request_time = new_time;
560 CDEBUG(D_INFO, "updating export %s at %ld\n",
561 exp->exp_client_uuid.uuid,
562 exp->exp_last_request_time);
564 /* exports may get disconnected from the chain even though the
565 export has references, so we must keep the spin lock while
566 manipulating the lists */
567 spin_lock(&exp->exp_obd->obd_dev_lock);
569 if (list_empty(&exp->exp_obd_chain_timed)) {
570 /* this one is not timed */
571 spin_unlock(&exp->exp_obd->obd_dev_lock);
575 list_move_tail(&exp->exp_obd_chain_timed,
576 &exp->exp_obd->obd_exports_timed);
578 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
579 struct obd_export, exp_obd_chain_timed);
580 oldest_time = oldest_exp->exp_last_request_time;
581 spin_unlock(&exp->exp_obd->obd_dev_lock);
583 if (exp->exp_obd->obd_recovering) {
584 /* be nice to everyone during recovery */
589 /* Note - racing to start/reset the obd_eviction timer is safe */
590 if (exp->exp_obd->obd_eviction_timer == 0) {
591 /* Check if the oldest entry is expired. */
592 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
594 /* We need a second timer, in case the net was down and
595 * it just came back. Since the pinger may skip every
596 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
597 * we better wait for 3. */
598 exp->exp_obd->obd_eviction_timer =
599 cfs_time_current_sec() + 3 * PING_INTERVAL;
600 CDEBUG(D_HA, "%s: Think about evicting %s from %ld\n",
601 exp->exp_obd->obd_name,
602 obd_export_nid2str(oldest_exp), oldest_time);
605 if (cfs_time_current_sec() >
606 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
607 /* The evictor won't evict anyone who we've heard from
608 * recently, so we don't have to check before we start
610 if (!ping_evictor_wake(exp))
611 exp->exp_obd->obd_eviction_timer = 0;
618 static int ptlrpc_check_req(struct ptlrpc_request *req)
620 if (lustre_msg_get_conn_cnt(req->rq_reqmsg) <
621 req->rq_export->exp_conn_cnt) {
622 DEBUG_REQ(D_ERROR, req,
623 "DROPPING req from old connection %d < %d",
624 lustre_msg_get_conn_cnt(req->rq_reqmsg),
625 req->rq_export->exp_conn_cnt);
628 if (req->rq_export->exp_obd && req->rq_export->exp_obd->obd_fail) {
629 /* Failing over, don't handle any more reqs, send
630 error response instead. */
631 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
632 req, req->rq_export->exp_obd->obd_name);
633 req->rq_status = -ENODEV;
640 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
642 struct ptlrpc_at_array *array = &svc->srv_at_array;
645 spin_lock(&svc->srv_at_lock);
646 if (array->paa_count == 0) {
647 cfs_timer_disarm(&svc->srv_at_timer);
648 spin_unlock(&svc->srv_at_lock);
652 /* Set timer for closest deadline */
653 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
656 ptlrpc_at_timer((unsigned long)svc);
658 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
659 spin_unlock(&svc->srv_at_lock);
660 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
663 /* Add rpc to early reply check list */
664 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
666 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
667 struct ptlrpc_request *rq = NULL;
668 struct ptlrpc_at_array *array = &svc->srv_at_array;
675 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
678 spin_lock(&svc->srv_at_lock);
679 LASSERT(list_empty(&req->rq_timed_list));
681 index = (unsigned long)req->rq_deadline % array->paa_size;
682 if (array->paa_reqs_count[index] > 0) {
683 /* latest rpcs will have the latest deadlines in the list,
684 * so search backward. */
685 list_for_each_entry_reverse(rq, &array->paa_reqs_array[index],
687 if (req->rq_deadline >= rq->rq_deadline) {
688 list_add(&req->rq_timed_list,
695 /* Add the request at the head of the list */
696 if (list_empty(&req->rq_timed_list))
697 list_add(&req->rq_timed_list, &array->paa_reqs_array[index]);
699 req->rq_at_linked = 1;
700 req->rq_at_index = index;
701 array->paa_reqs_count[index]++;
703 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
704 array->paa_deadline = req->rq_deadline;
707 spin_unlock(&svc->srv_at_lock);
710 ptlrpc_at_set_timer(svc);
715 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
717 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
718 struct ptlrpc_request *reqcopy;
719 struct lustre_msg *reqmsg;
720 long olddl = req->rq_deadline - cfs_time_current_sec();
725 /* deadline is when the client expects us to reply, margin is the
726 difference between clients' and servers' expectations */
727 DEBUG_REQ(D_ADAPTTO, req,
728 "%ssending early reply (deadline %+lds, margin %+lds) for "
729 "%d+%d", AT_OFF ? "AT off - not " : "",
730 olddl, olddl - at_get(&svc->srv_at_estimate),
731 at_get(&svc->srv_at_estimate), at_extra);
737 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
738 "not sending early reply. Consider increasing "
739 "at_early_margin (%d)?", olddl, at_early_margin);
741 /* Return an error so we're not re-added to the timed list. */
745 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
746 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
747 "but no AT support");
751 if (req->rq_export &&
752 lustre_msg_get_flags(req->rq_reqmsg) &
753 (MSG_REPLAY | MSG_LAST_REPLAY)) {
754 /* During recovery, we don't want to send too many early
755 * replies, but on the other hand we want to make sure the
756 * client has enough time to resend if the rpc is lost. So
757 * during the recovery period send at least 4 early replies,
758 * spacing them every at_extra if we can. at_estimate should
759 * always equal this fixed value during recovery. */
760 at_measured(&svc->srv_at_estimate, min(at_extra,
761 req->rq_export->exp_obd->obd_recovery_timeout / 4));
763 /* Fake our processing time into the future to ask the
764 * clients for some extra amount of time */
765 at_measured(&svc->srv_at_estimate, at_extra +
766 cfs_time_current_sec() -
767 req->rq_arrival_time.tv_sec);
769 /* Check to see if we've actually increased the deadline -
770 * we may be past adaptive_max */
771 if (req->rq_deadline >= req->rq_arrival_time.tv_sec +
772 at_get(&svc->srv_at_estimate)) {
773 DEBUG_REQ(D_WARNING, req, "Couldn't add any time "
774 "(%ld/%ld), not sending early reply\n",
775 olddl, req->rq_arrival_time.tv_sec +
776 at_get(&svc->srv_at_estimate) -
777 cfs_time_current_sec());
781 newdl = cfs_time_current_sec() + at_get(&svc->srv_at_estimate);
783 OBD_ALLOC(reqcopy, sizeof *reqcopy);
786 OBD_ALLOC(reqmsg, req->rq_reqlen);
788 OBD_FREE(reqcopy, sizeof *reqcopy);
793 reqcopy->rq_reply_state = NULL;
794 reqcopy->rq_rep_swab_mask = 0;
795 /* We only need the reqmsg for the magic */
796 reqcopy->rq_reqmsg = reqmsg;
797 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
799 LASSERT(atomic_read(&req->rq_refcount));
800 /** if it is last refcount then early reply isn't needed */
801 if (atomic_read(&req->rq_refcount) == 1) {
802 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
803 "abort sending early reply\n");
804 GOTO(out, rc = -EINVAL);
808 reqcopy->rq_export = class_conn2export(
809 lustre_msg_get_handle(reqcopy->rq_reqmsg));
810 if (reqcopy->rq_export == NULL)
811 GOTO(out, rc = -ENODEV);
814 class_export_rpc_get(reqcopy->rq_export);
815 if (reqcopy->rq_export->exp_obd &&
816 reqcopy->rq_export->exp_obd->obd_fail)
817 GOTO(out_put, rc = -ENODEV);
819 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
823 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
826 /* Adjust our own deadline to what we told the client */
827 req->rq_deadline = newdl;
828 req->rq_early_count++; /* number sent, server side */
830 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
833 /* Free the (early) reply state from lustre_pack_reply.
834 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
835 ptlrpc_req_drop_rs(reqcopy);
838 class_export_rpc_put(reqcopy->rq_export);
839 class_export_put(reqcopy->rq_export);
841 OBD_FREE(reqmsg, req->rq_reqlen);
842 OBD_FREE(reqcopy, sizeof *reqcopy);
846 /* Send early replies to everybody expiring within at_early_margin
847 asking for at_extra time */
848 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
850 struct ptlrpc_request *rq, *n;
851 struct list_head work_list;
852 struct ptlrpc_at_array *array = &svc->srv_at_array;
855 time_t now = cfs_time_current_sec();
856 cfs_duration_t delay;
857 int first, counter = 0;
860 spin_lock(&svc->srv_at_lock);
861 if (svc->srv_at_check == 0) {
862 spin_unlock(&svc->srv_at_lock);
865 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
866 svc->srv_at_check = 0;
868 if (array->paa_count == 0) {
869 spin_unlock(&svc->srv_at_lock);
873 /* The timer went off, but maybe the nearest rpc already completed. */
874 first = array->paa_deadline - now;
875 if (first > at_early_margin) {
876 /* We've still got plenty of time. Reset the timer. */
877 spin_unlock(&svc->srv_at_lock);
878 ptlrpc_at_set_timer(svc);
882 /* We're close to a timeout, and we don't know how much longer the
883 server will take. Send early replies to everyone expiring soon. */
884 CFS_INIT_LIST_HEAD(&work_list);
886 index = (unsigned long)array->paa_deadline % array->paa_size;
887 count = array->paa_count;
889 count -= array->paa_reqs_count[index];
890 list_for_each_entry_safe(rq, n, &array->paa_reqs_array[index],
892 if (rq->rq_deadline <= now + at_early_margin) {
893 list_del_init(&rq->rq_timed_list);
895 * ptlrpc_server_drop_request() may drop
896 * refcount to 0 already. Let's check this and
897 * don't add entry to work_list
899 if (likely(atomic_inc_not_zero(&rq->rq_refcount)))
900 list_add(&rq->rq_timed_list, &work_list);
902 array->paa_reqs_count[index]--;
904 rq->rq_at_linked = 0;
908 /* update the earliest deadline */
909 if (deadline == -1 || rq->rq_deadline < deadline)
910 deadline = rq->rq_deadline;
915 if (++index >= array->paa_size)
918 array->paa_deadline = deadline;
919 spin_unlock(&svc->srv_at_lock);
921 /* we have a new earliest deadline, restart the timer */
922 ptlrpc_at_set_timer(svc);
924 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
925 "replies\n", first, at_extra, counter);
928 /* We're already past request deadlines before we even get a
929 chance to send early replies */
930 LCONSOLE_WARN("%s: This server is not able to keep up with "
931 "request traffic (cpu-bound).\n", svc->srv_name);
932 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
933 "delay="CFS_DURATION_T"(jiff)\n",
934 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
935 at_get(&svc->srv_at_estimate), delay);
938 /* we took additional refcount so entries can't be deleted from list, no
939 * locking is needed */
940 while (!list_empty(&work_list)) {
941 rq = list_entry(work_list.next, struct ptlrpc_request,
943 list_del_init(&rq->rq_timed_list);
945 if (ptlrpc_at_send_early_reply(rq) == 0)
946 ptlrpc_at_add_timed(rq);
948 ptlrpc_server_drop_request(rq);
955 * Put the request to the export list if the request may become
956 * a high priority one.
958 static int ptlrpc_hpreq_init(struct ptlrpc_service *svc,
959 struct ptlrpc_request *req)
964 if (svc->srv_hpreq_handler) {
965 rc = svc->srv_hpreq_handler(req);
969 if (req->rq_export && req->rq_ops) {
970 spin_lock(&req->rq_export->exp_lock);
971 list_add(&req->rq_exp_list, &req->rq_export->exp_queued_rpc);
972 spin_unlock(&req->rq_export->exp_lock);
978 /** Remove the request from the export list. */
979 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req)
982 if (req->rq_export && req->rq_ops) {
983 spin_lock(&req->rq_export->exp_lock);
984 list_del_init(&req->rq_exp_list);
985 spin_unlock(&req->rq_export->exp_lock);
991 * Make the request a high priority one.
993 * All the high priority requests are queued in a separate FIFO
994 * ptlrpc_service::srv_request_hpq list which is parallel to
995 * ptlrpc_service::srv_request_queue list but has a higher priority
998 * \see ptlrpc_server_handle_request().
1000 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service *svc,
1001 struct ptlrpc_request *req)
1004 LASSERT(svc != NULL);
1005 spin_lock(&req->rq_lock);
1006 if (req->rq_hp == 0) {
1007 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1009 /* Add to the high priority queue. */
1010 list_move_tail(&req->rq_list, &svc->srv_request_hpq);
1012 if (opc != OBD_PING)
1013 DEBUG_REQ(D_NET, req, "high priority req");
1015 spin_unlock(&req->rq_lock);
1019 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1021 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1024 spin_lock(&svc->srv_lock);
1025 /* It may happen that the request is already taken for the processing
1026 * but still in the export list, do not re-add it into the HP list. */
1027 if (req->rq_phase == RQ_PHASE_NEW)
1028 ptlrpc_hpreq_reorder_nolock(svc, req);
1029 spin_unlock(&svc->srv_lock);
1033 /** Check if the request if a high priority one. */
1034 static int ptlrpc_server_hpreq_check(struct ptlrpc_request *req)
1039 /* Check by request opc. */
1040 opc = lustre_msg_get_opc(req->rq_reqmsg);
1041 if (opc == OBD_PING)
1044 /* Perform request specific check. */
1045 if (req->rq_ops && req->rq_ops->hpreq_check)
1046 rc = req->rq_ops->hpreq_check(req);
1050 /** Check if a request is a high priority one. */
1051 static int ptlrpc_server_request_add(struct ptlrpc_service *svc,
1052 struct ptlrpc_request *req)
1057 rc = ptlrpc_server_hpreq_check(req);
1061 spin_lock(&svc->srv_lock);
1062 /* Before inserting the request into the queue, check if it is not
1063 * inserted yet, or even already handled -- it may happen due to
1064 * a racing ldlm_server_blocking_ast(). */
1065 if (req->rq_phase == RQ_PHASE_NEW && list_empty(&req->rq_list)) {
1067 ptlrpc_hpreq_reorder_nolock(svc, req);
1069 list_add_tail(&req->rq_list, &svc->srv_request_queue);
1071 spin_unlock(&svc->srv_lock);
1076 /* Only allow normal priority requests on a service that has a high-priority
1077 * queue if forced (i.e. cleanup), if there are other high priority requests
1078 * already being processed (i.e. those threads can service more high-priority
1079 * requests), or if there are enough idle threads that a later thread can do
1080 * a high priority request. */
1081 static int ptlrpc_server_allow_normal(struct ptlrpc_service *svc, int force)
1083 return force || !svc->srv_hpreq_handler || svc->srv_n_hpreq > 0 ||
1084 svc->srv_n_active_reqs < svc->srv_threads_running - 2;
1087 static struct ptlrpc_request *
1088 ptlrpc_server_request_get(struct ptlrpc_service *svc, int force)
1090 struct ptlrpc_request *req = NULL;
1093 if (ptlrpc_server_allow_normal(svc, force) &&
1094 !list_empty(&svc->srv_request_queue) &&
1095 (list_empty(&svc->srv_request_hpq) ||
1096 svc->srv_hpreq_count >= svc->srv_hpreq_ratio)) {
1097 req = list_entry(svc->srv_request_queue.next,
1098 struct ptlrpc_request, rq_list);
1099 svc->srv_hpreq_count = 0;
1100 } else if (!list_empty(&svc->srv_request_hpq)) {
1101 req = list_entry(svc->srv_request_hpq.next,
1102 struct ptlrpc_request, rq_list);
1103 svc->srv_hpreq_count++;
1108 static int ptlrpc_server_request_pending(struct ptlrpc_service *svc, int force)
1110 return ((ptlrpc_server_allow_normal(svc, force) &&
1111 !list_empty(&svc->srv_request_queue)) ||
1112 !list_empty(&svc->srv_request_hpq));
1115 /* Handle freshly incoming reqs, add to timed early reply list,
1116 pass on to regular request queue */
1118 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
1120 struct ptlrpc_request *req;
1127 spin_lock(&svc->srv_lock);
1128 if (list_empty(&svc->srv_req_in_queue)) {
1129 spin_unlock(&svc->srv_lock);
1133 req = list_entry(svc->srv_req_in_queue.next,
1134 struct ptlrpc_request, rq_list);
1135 list_del_init (&req->rq_list);
1136 /* Consider this still a "queued" request as far as stats are
1138 spin_unlock(&svc->srv_lock);
1140 /* Clear request swab mask; this is a new request */
1141 req->rq_req_swab_mask = 0;
1143 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
1145 CERROR ("error unpacking request: ptl %d from %s"
1146 " xid "LPU64"\n", svc->srv_req_portal,
1147 libcfs_id2str(req->rq_peer), req->rq_xid);
1152 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1154 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1156 CERROR ("error unpacking ptlrpc body: ptl %d from %s"
1157 " xid "LPU64"\n", svc->srv_req_portal,
1158 libcfs_id2str(req->rq_peer), req->rq_xid);
1163 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1164 CERROR("wrong packet type received (type=%u) from %s\n",
1165 lustre_msg_get_type(req->rq_reqmsg),
1166 libcfs_id2str(req->rq_peer));
1170 CDEBUG(D_NET, "got req "LPU64"\n", req->rq_xid);
1172 req->rq_export = class_conn2export(
1173 lustre_msg_get_handle(req->rq_reqmsg));
1174 if (req->rq_export) {
1175 rc = ptlrpc_check_req(req);
1178 ptlrpc_update_export_timer(req->rq_export, 0);
1181 /* req_in handling should/must be fast */
1182 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1183 DEBUG_REQ(D_WARNING, req, "Slow req_in handling %lus",
1184 cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
1186 /* Set rpc server deadline and add it to the timed list */
1187 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1188 MSGHDR_AT_SUPPORT) ?
1189 /* The max time the client expects us to take */
1190 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1191 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1192 if (unlikely(deadline == 0)) {
1193 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1197 ptlrpc_at_add_timed(req);
1198 rc = ptlrpc_hpreq_init(svc, req);
1202 /* Move it over to the request processing queue */
1203 rc = ptlrpc_server_request_add(svc, req);
1206 cfs_waitq_signal(&svc->srv_waitq);
1210 spin_lock(&svc->srv_lock);
1211 svc->srv_n_queued_reqs--;
1212 svc->srv_n_active_reqs++;
1213 spin_unlock(&svc->srv_lock);
1214 ptlrpc_server_finish_request(req);
1220 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1221 struct ptlrpc_thread *thread)
1223 struct obd_export *export = NULL;
1224 struct ptlrpc_request *request;
1225 struct timeval work_start;
1226 struct timeval work_end;
1234 spin_lock(&svc->srv_lock);
1235 if (!ptlrpc_server_request_pending(svc, 0) ||
1238 /* !@%$# liblustre only has 1 thread */
1239 svc->srv_n_difficult_replies != 0 &&
1241 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1))) {
1242 /* Don't handle regular requests in the last thread, in order
1243 * to handle difficult replies (which might block other threads)
1244 * as well as handle any incoming reqs, early replies, etc.
1245 * That means we always need at least 2 service threads. */
1246 spin_unlock(&svc->srv_lock);
1250 request = ptlrpc_server_request_get(svc, 0);
1251 if (request == NULL) {
1252 spin_unlock(&svc->srv_lock);
1256 opc = lustre_msg_get_opc(request->rq_reqmsg);
1257 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1258 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1259 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1260 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1262 if (unlikely(fail_opc)) {
1263 if (request->rq_export && request->rq_ops) {
1264 spin_unlock(&svc->srv_lock);
1265 OBD_FAIL_TIMEOUT(fail_opc, 4);
1266 spin_lock(&svc->srv_lock);
1267 request = ptlrpc_server_request_get(svc, 0);
1268 if (request == NULL) {
1269 spin_unlock(&svc->srv_lock);
1272 LASSERT(ptlrpc_server_request_pending(svc, 0));
1276 list_del_init(&request->rq_list);
1277 svc->srv_n_queued_reqs--;
1278 svc->srv_n_active_reqs++;
1283 /* The phase is changed under the lock here because we need to know
1284 * the request is under processing (see ptlrpc_hpreq_reorder()). */
1285 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1286 spin_unlock(&svc->srv_lock);
1288 ptlrpc_hpreq_fini(request);
1290 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1291 libcfs_debug_dumplog();
1293 do_gettimeofday(&work_start);
1294 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1295 if (svc->srv_stats != NULL) {
1296 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1298 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1299 svc->srv_n_queued_reqs);
1300 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1301 svc->srv_n_active_reqs);
1302 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1303 at_get(&svc->srv_at_estimate));
1306 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1308 request->rq_svc_thread = thread;
1309 if (request->rq_export) {
1310 if (ptlrpc_check_req(request))
1312 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1313 export = class_export_rpc_get(request->rq_export);
1316 /* Discard requests queued for longer than the deadline.
1317 The deadline is increased if we send an early reply. */
1318 if (cfs_time_current_sec() > request->rq_deadline) {
1319 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1320 ": deadline %ld%+lds ago\n",
1321 libcfs_id2str(request->rq_peer),
1322 request->rq_deadline -
1323 request->rq_arrival_time.tv_sec,
1324 cfs_time_current_sec() - request->rq_deadline);
1325 goto put_rpc_export;
1328 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1329 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1330 (request->rq_export ?
1331 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1332 (request->rq_export ?
1333 atomic_read(&request->rq_export->exp_refcount) : -99),
1334 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1335 libcfs_id2str(request->rq_peer),
1336 lustre_msg_get_opc(request->rq_reqmsg));
1338 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1340 rc = svc->srv_handler(request);
1342 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1345 if (export != NULL && !request->rq_copy_queued)
1346 class_export_rpc_put(export);
1349 if (cfs_time_current_sec() > request->rq_deadline) {
1350 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1351 "than estimated (%ld%+lds); client may timeout.",
1352 request->rq_xid, request->rq_deadline -
1353 request->rq_arrival_time.tv_sec,
1354 cfs_time_current_sec() - request->rq_deadline);
1357 do_gettimeofday(&work_end);
1358 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1360 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1361 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
1362 "%lds (%lds total) trans "LPU64" rc %d/%d\n",
1364 (request->rq_export ?
1365 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1366 (request->rq_export ?
1367 atomic_read(&request->rq_export->exp_refcount) : -99),
1368 lustre_msg_get_status(request->rq_reqmsg),
1370 libcfs_id2str(request->rq_peer),
1371 lustre_msg_get_opc(request->rq_reqmsg),
1373 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1374 (request->rq_repmsg ?
1375 lustre_msg_get_transno(request->rq_repmsg) :
1376 request->rq_transno),
1378 (request->rq_repmsg ?
1379 lustre_msg_get_status(request->rq_repmsg) : -999));
1381 if (svc->srv_stats != NULL) {
1382 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1383 int opc = opcode_offset(op);
1384 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1385 LASSERT(opc < LUSTRE_MAX_OPCODES);
1386 lprocfs_counter_add(svc->srv_stats,
1387 opc + EXTRA_MAX_OPCODES,
1391 if (request->rq_early_count) {
1392 DEBUG_REQ(D_ADAPTTO, request,
1393 "sent %d early replies before finishing in %lds",
1394 request->rq_early_count,
1395 work_end.tv_sec - request->rq_arrival_time.tv_sec);
1398 spin_lock(&svc->srv_lock);
1401 spin_unlock(&svc->srv_lock);
1402 ptlrpc_server_finish_request(request);
1408 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
1410 struct ptlrpc_reply_state *rs;
1411 struct obd_export *exp;
1412 struct obd_device *obd;
1417 spin_lock(&svc->srv_lock);
1418 if (list_empty (&svc->srv_reply_queue)) {
1419 spin_unlock(&svc->srv_lock);
1423 rs = list_entry (svc->srv_reply_queue.next,
1424 struct ptlrpc_reply_state, rs_list);
1426 exp = rs->rs_export;
1429 LASSERT (rs->rs_difficult);
1430 LASSERT (rs->rs_scheduled);
1432 list_del_init (&rs->rs_list);
1434 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
1435 spin_unlock(&svc->srv_lock);
1437 spin_lock (&exp->exp_uncommitted_replies_lock);
1438 /* Noop if removed already */
1439 list_del_init (&rs->rs_obd_list);
1440 spin_unlock (&exp->exp_uncommitted_replies_lock);
1442 spin_lock (&exp->exp_lock);
1443 /* Noop if removed already */
1444 list_del_init (&rs->rs_exp_list);
1445 spin_unlock (&exp->exp_lock);
1447 spin_lock(&svc->srv_lock);
1449 been_handled = rs->rs_handled;
1452 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1453 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1455 if (nlocks == 0 && !been_handled) {
1456 /* If we see this, we should already have seen the warning
1457 * in mds_steal_ack_locks() */
1458 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1459 " o%d NID %s\n", rs, rs->rs_xid, rs->rs_transno,
1460 lustre_msg_get_opc(rs->rs_msg),
1461 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1464 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1465 spin_unlock(&svc->srv_lock);
1467 if (!been_handled && rs->rs_on_net) {
1468 LNetMDUnlink(rs->rs_md_h);
1469 /* Ignore return code; we're racing with
1473 while (nlocks-- > 0)
1474 ldlm_lock_decref(&rs->rs_locks[nlocks],
1475 rs->rs_modes[nlocks]);
1477 spin_lock(&svc->srv_lock);
1480 rs->rs_scheduled = 0;
1482 if (!rs->rs_on_net) {
1484 svc->srv_n_difficult_replies--;
1485 spin_unlock(&svc->srv_lock);
1487 class_export_put (exp);
1488 rs->rs_export = NULL;
1489 ptlrpc_rs_decref (rs);
1490 atomic_dec (&svc->srv_outstanding_replies);
1494 /* still on the net; callback will schedule */
1495 spin_unlock(&svc->srv_lock);
1500 /* FIXME make use of timeout later */
1502 liblustre_check_services (void *arg)
1504 int did_something = 0;
1506 struct list_head *tmp, *nxt;
1509 /* I'm relying on being single threaded, not to have to lock
1510 * ptlrpc_all_services etc */
1511 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1512 struct ptlrpc_service *svc =
1513 list_entry (tmp, struct ptlrpc_service, srv_list);
1515 if (svc->srv_threads_running != 0) /* I've recursed */
1518 /* service threads can block for bulk, so this limits us
1519 * (arbitrarily) to recursing 1 stack frame per service.
1520 * Note that the problem with recursion is that we have to
1521 * unwind completely before our caller can resume. */
1523 svc->srv_threads_running++;
1526 rc = ptlrpc_server_handle_req_in(svc);
1527 rc |= ptlrpc_server_handle_reply(svc);
1528 rc |= ptlrpc_at_check_timed(svc);
1529 rc |= ptlrpc_server_handle_request(svc, NULL);
1530 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1531 did_something |= rc;
1534 svc->srv_threads_running--;
1537 RETURN(did_something);
1539 #define ptlrpc_stop_all_threads(s) do {} while (0)
1541 #else /* __KERNEL__ */
1544 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1546 int avail = svc->srv_nrqbd_receiving;
1547 int low_water = test_req_buffer_pressure ? 0 :
1548 svc->srv_nbuf_per_group/2;
1550 /* NB I'm not locking; just looking. */
1552 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1553 * allowed the request history to grow out of control. We could put a
1554 * sanity check on that here and cull some history if we need the
1557 if (avail <= low_water)
1558 ptlrpc_grow_req_bufs(svc);
1561 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1566 ptlrpc_retry_rqbds(void *arg)
1568 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1570 svc->srv_rqbd_timeout = 0;
1571 return (-ETIMEDOUT);
1574 static int ptlrpc_main(void *arg)
1576 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1577 struct ptlrpc_service *svc = data->svc;
1578 struct ptlrpc_thread *thread = data->thread;
1579 struct obd_device *dev = data->dev;
1580 struct ptlrpc_reply_state *rs;
1581 #ifdef WITH_GROUP_INFO
1582 struct group_info *ginfo = NULL;
1584 int counter = 0, rc = 0;
1587 cfs_daemonize_ctxt(data->name);
1589 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1590 /* we need to do this before any per-thread allocation is done so that
1591 * we get the per-thread allocations on local node. bug 7342 */
1592 if (svc->srv_cpu_affinity) {
1595 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1596 if (!cpu_online(cpu))
1598 if (num_cpu == thread->t_id % num_online_cpus())
1602 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1606 #ifdef WITH_GROUP_INFO
1607 ginfo = groups_alloc(0);
1613 set_current_groups(ginfo);
1614 put_group_info(ginfo);
1617 if (svc->srv_init != NULL) {
1618 rc = svc->srv_init(thread);
1623 /* Alloc reply state structure for this one */
1624 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1630 spin_lock(&svc->srv_lock);
1631 /* SVC_STOPPING may already be set here if someone else is trying
1632 * to stop the service while this new thread has been dynamically
1633 * forked. We still set SVC_RUNNING to let our creator know that
1634 * we are now running, however we will exit as soon as possible */
1635 thread->t_flags |= SVC_RUNNING;
1636 spin_unlock(&svc->srv_lock);
1639 * wake up our creator. Note: @data is invalid after this point,
1640 * because it's allocated on ptlrpc_start_thread() stack.
1642 cfs_waitq_signal(&thread->t_ctl_waitq);
1644 thread->t_watchdog = lc_watchdog_add(GET_TIMEOUT(svc), NULL, NULL);
1646 spin_lock(&svc->srv_lock);
1647 svc->srv_threads_running++;
1648 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1649 spin_unlock(&svc->srv_lock);
1650 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1652 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
1653 svc->srv_threads_running);
1655 /* XXX maintain a list of all managed devices: insert here */
1657 while ((thread->t_flags & SVC_STOPPING) == 0 ||
1658 svc->srv_n_difficult_replies != 0) {
1659 /* Don't exit while there are replies to be handled */
1660 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1661 ptlrpc_retry_rqbds, svc);
1663 lc_watchdog_disable(thread->t_watchdog);
1667 l_wait_event_exclusive (svc->srv_waitq,
1668 ((thread->t_flags & SVC_STOPPING) != 0 &&
1669 svc->srv_n_difficult_replies == 0) ||
1670 (!list_empty(&svc->srv_idle_rqbds) &&
1671 svc->srv_rqbd_timeout == 0) ||
1672 !list_empty(&svc->srv_req_in_queue) ||
1673 !list_empty(&svc->srv_reply_queue) ||
1674 (ptlrpc_server_request_pending(svc, 0) &&
1675 (svc->srv_n_active_reqs <
1676 (svc->srv_threads_running - 1))) ||
1680 lc_watchdog_touch(thread->t_watchdog, GET_TIMEOUT(svc));
1682 ptlrpc_check_rqbd_pool(svc);
1684 if ((svc->srv_threads_started < svc->srv_threads_max) &&
1685 (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
1686 /* Ignore return code - we tried... */
1687 ptlrpc_start_thread(dev, svc);
1690 if (!list_empty(&svc->srv_reply_queue))
1691 ptlrpc_server_handle_reply (svc);
1693 if (!list_empty(&svc->srv_req_in_queue)) {
1694 /* Process all incoming reqs before handling any */
1695 ptlrpc_server_handle_req_in(svc);
1696 /* but limit ourselves in case of flood */
1697 if (counter++ < 1000)
1702 if (svc->srv_at_check)
1703 ptlrpc_at_check_timed(svc);
1705 /* don't handle requests in the last thread */
1706 if (ptlrpc_server_request_pending(svc, 0) &&
1707 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1)))
1708 ptlrpc_server_handle_request(svc, thread);
1710 if (!list_empty(&svc->srv_idle_rqbds) &&
1711 ptlrpc_server_post_idle_rqbds(svc) < 0) {
1712 /* I just failed to repost request buffers. Wait
1713 * for a timeout (unless something else happens)
1714 * before I try again */
1715 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1716 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1717 svc->srv_nrqbd_receiving);
1721 lc_watchdog_delete(thread->t_watchdog);
1722 thread->t_watchdog = NULL;
1726 * deconstruct service specific state created by ptlrpc_start_thread()
1728 if (svc->srv_done != NULL)
1729 svc->srv_done(thread);
1732 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1734 spin_lock(&svc->srv_lock);
1735 svc->srv_threads_running--; /* must know immediately */
1737 thread->t_flags = SVC_STOPPED;
1739 cfs_waitq_signal(&thread->t_ctl_waitq);
1740 spin_unlock(&svc->srv_lock);
1745 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1746 struct ptlrpc_thread *thread)
1748 struct l_wait_info lwi = { 0 };
1750 spin_lock(&svc->srv_lock);
1751 /* let the thread know that we would like it to stop asap */
1752 thread->t_flags |= SVC_STOPPING;
1753 spin_unlock(&svc->srv_lock);
1755 cfs_waitq_broadcast(&svc->srv_waitq);
1756 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1759 spin_lock(&svc->srv_lock);
1760 list_del(&thread->t_link);
1761 spin_unlock(&svc->srv_lock);
1763 OBD_FREE(thread, sizeof(*thread));
1766 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1768 struct ptlrpc_thread *thread;
1770 spin_lock(&svc->srv_lock);
1771 while (!list_empty(&svc->srv_threads)) {
1772 thread = list_entry(svc->srv_threads.next,
1773 struct ptlrpc_thread, t_link);
1775 spin_unlock(&svc->srv_lock);
1776 ptlrpc_stop_thread(svc, thread);
1777 spin_lock(&svc->srv_lock);
1780 spin_unlock(&svc->srv_lock);
1783 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1788 /* We require 2 threads min - see note in
1789 * ptlrpc_server_handle_request() */
1791 LASSERT(svc->srv_threads_min >= 2);
1792 for (i = 0; i < svc->srv_threads_min; i++) {
1793 rc = ptlrpc_start_thread(dev, svc);
1794 /* We have enough threads, don't start more. b=15759 */
1798 CERROR("cannot start %s thread #%d: rc %d\n",
1799 svc->srv_thread_name, i, rc);
1800 ptlrpc_stop_all_threads(svc);
1806 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1808 struct l_wait_info lwi = { 0 };
1809 struct ptlrpc_svc_data d;
1810 struct ptlrpc_thread *thread;
1815 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1816 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1817 svc->srv_threads_max, svc->srv_threads_running);
1818 if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
1819 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
1820 svc->srv_threads_started == svc->srv_threads_min - 1))
1823 OBD_ALLOC(thread, sizeof(*thread));
1826 cfs_waitq_init(&thread->t_ctl_waitq);
1828 spin_lock(&svc->srv_lock);
1829 if (svc->srv_threads_started >= svc->srv_threads_max) {
1830 spin_unlock(&svc->srv_lock);
1831 OBD_FREE(thread, sizeof(*thread));
1834 list_add(&thread->t_link, &svc->srv_threads);
1835 id = svc->srv_threads_started++;
1836 spin_unlock(&svc->srv_lock);
1838 thread->t_svc = svc;
1840 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1846 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1848 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1849 * just drop the VM and FILES in cfs_daemonize_ctxt() right away.
1851 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1853 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1855 spin_lock(&svc->srv_lock);
1856 list_del(&thread->t_link);
1857 --svc->srv_threads_started;
1858 spin_unlock(&svc->srv_lock);
1860 OBD_FREE(thread, sizeof(*thread));
1863 l_wait_event(thread->t_ctl_waitq,
1864 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1866 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1871 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1874 struct l_wait_info lwi;
1875 struct list_head *tmp;
1876 struct ptlrpc_reply_state *rs, *t;
1877 struct ptlrpc_at_array *array = &service->srv_at_array;
1879 cfs_timer_disarm(&service->srv_at_timer);
1881 ptlrpc_stop_all_threads(service);
1882 LASSERT(list_empty(&service->srv_threads));
1884 spin_lock (&ptlrpc_all_services_lock);
1885 list_del_init (&service->srv_list);
1886 spin_unlock (&ptlrpc_all_services_lock);
1888 ptlrpc_lprocfs_unregister_service(service);
1890 /* All history will be culled when the next request buffer is
1892 service->srv_max_history_rqbds = 0;
1894 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1896 rc = LNetClearLazyPortal(service->srv_req_portal);
1899 /* Unlink all the request buffers. This forces a 'final' event with
1900 * its 'unlink' flag set for each posted rqbd */
1901 list_for_each(tmp, &service->srv_active_rqbds) {
1902 struct ptlrpc_request_buffer_desc *rqbd =
1903 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1906 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1907 LASSERT (rc == 0 || rc == -ENOENT);
1910 /* Wait for the network to release any buffers it's currently
1913 spin_lock(&service->srv_lock);
1914 rc = service->srv_nrqbd_receiving;
1915 spin_unlock(&service->srv_lock);
1920 /* Network access will complete in finite time but the HUGE
1921 * timeout lets us CWARN for visibility of sluggish NALs */
1922 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
1923 cfs_time_seconds(1), NULL, NULL);
1924 rc = l_wait_event(service->srv_waitq,
1925 service->srv_nrqbd_receiving == 0,
1927 if (rc == -ETIMEDOUT)
1928 CWARN("Service %s waiting for request buffers\n",
1932 /* schedule all outstanding replies to terminate them */
1933 spin_lock(&service->srv_lock);
1934 while (!list_empty(&service->srv_active_replies)) {
1935 struct ptlrpc_reply_state *rs =
1936 list_entry(service->srv_active_replies.next,
1937 struct ptlrpc_reply_state, rs_list);
1938 ptlrpc_schedule_difficult_reply(rs);
1940 spin_unlock(&service->srv_lock);
1942 /* purge the request queue. NB No new replies (rqbds all unlinked)
1943 * and no service threads, so I'm the only thread noodling the
1944 * request queue now */
1945 while (!list_empty(&service->srv_req_in_queue)) {
1946 struct ptlrpc_request *req =
1947 list_entry(service->srv_req_in_queue.next,
1948 struct ptlrpc_request,
1951 list_del(&req->rq_list);
1952 service->srv_n_queued_reqs--;
1953 service->srv_n_active_reqs++;
1954 ptlrpc_server_finish_request(req);
1956 while (ptlrpc_server_request_pending(service, 1)) {
1957 struct ptlrpc_request *req;
1959 req = ptlrpc_server_request_get(service, 1);
1960 list_del(&req->rq_list);
1961 service->srv_n_queued_reqs--;
1962 service->srv_n_active_reqs++;
1963 ptlrpc_hpreq_fini(req);
1964 ptlrpc_server_finish_request(req);
1966 LASSERT(service->srv_n_queued_reqs == 0);
1967 LASSERT(service->srv_n_active_reqs == 0);
1968 LASSERT(service->srv_n_history_rqbds == 0);
1969 LASSERT(list_empty(&service->srv_active_rqbds));
1971 /* Now free all the request buffers since nothing references them
1973 while (!list_empty(&service->srv_idle_rqbds)) {
1974 struct ptlrpc_request_buffer_desc *rqbd =
1975 list_entry(service->srv_idle_rqbds.next,
1976 struct ptlrpc_request_buffer_desc,
1979 ptlrpc_free_rqbd(rqbd);
1982 /* wait for all outstanding replies to complete (they were
1983 * scheduled having been flagged to abort above) */
1984 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1985 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1987 rc = l_wait_event(service->srv_waitq,
1988 !list_empty(&service->srv_reply_queue), &lwi);
1989 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1992 ptlrpc_server_handle_reply(service);
1995 CWARN("Unexpectedly long timeout %p\n", service);
1998 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1999 list_del(&rs->rs_list);
2000 OBD_FREE(rs, service->srv_max_reply_size);
2003 /* In case somebody rearmed this in the meantime */
2004 cfs_timer_disarm(&service->srv_at_timer);
2006 if (array->paa_reqs_array != NULL) {
2007 OBD_FREE(array->paa_reqs_array,
2008 sizeof(struct list_head) * array->paa_size);
2009 array->paa_reqs_array = NULL;
2012 if (array->paa_reqs_count != NULL) {
2013 OBD_FREE(array->paa_reqs_count,
2014 sizeof(__u32) * array->paa_size);
2015 array->paa_reqs_count= NULL;
2018 OBD_FREE(service, sizeof(*service));
2022 /* Returns 0 if the service is healthy.
2024 * Right now, it just checks to make sure that requests aren't languishing
2025 * in the queue. We'll use this health check to govern whether a node needs
2026 * to be shot, so it's intentionally non-aggressive. */
2027 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
2029 struct ptlrpc_request *request;
2030 struct timeval right_now;
2036 do_gettimeofday(&right_now);
2038 spin_lock(&svc->srv_lock);
2039 if (!ptlrpc_server_request_pending(svc, 1)) {
2040 spin_unlock(&svc->srv_lock);
2044 /* How long has the next entry been waiting? */
2045 if (list_empty(&svc->srv_request_queue))
2046 request = list_entry(svc->srv_request_hpq.next,
2047 struct ptlrpc_request, rq_list);
2049 request = list_entry(svc->srv_request_queue.next,
2050 struct ptlrpc_request, rq_list);
2051 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
2052 spin_unlock(&svc->srv_lock);
2054 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
2056 CERROR("%s: unhealthy - request has been waiting %lds\n",
2057 svc->srv_name, timediff / ONE_MILLION);