1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
28 #include <liblustre.h>
29 #include <libcfs/kp30.h>
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lu_object.h>
35 #include <lnet/types.h>
36 #include "ptlrpc_internal.h"
38 int test_req_buffer_pressure = 0;
39 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
40 "set non-zero to put pressure on request buffer pools");
43 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
45 static CFS_LIST_HEAD (ptlrpc_all_services);
46 spinlock_t ptlrpc_all_services_lock;
49 ptlrpc_alloc_request_buffer (int size)
53 if (size > SVC_BUF_VMALLOC_THRESHOLD)
54 OBD_VMALLOC(ptr, size);
62 ptlrpc_free_request_buffer (char *ptr, int size)
64 if (size > SVC_BUF_VMALLOC_THRESHOLD)
70 struct ptlrpc_request_buffer_desc *
71 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
73 struct ptlrpc_request_buffer_desc *rqbd;
79 rqbd->rqbd_service = svc;
80 rqbd->rqbd_refcount = 0;
81 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
82 rqbd->rqbd_cbid.cbid_arg = rqbd;
83 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
84 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
86 if (rqbd->rqbd_buffer == NULL) {
91 spin_lock(&svc->srv_lock);
92 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
94 spin_unlock(&svc->srv_lock);
100 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
102 struct ptlrpc_service *svc = rqbd->rqbd_service;
104 LASSERT (rqbd->rqbd_refcount == 0);
105 LASSERT (list_empty(&rqbd->rqbd_reqs));
107 spin_lock(&svc->srv_lock);
108 list_del(&rqbd->rqbd_list);
110 spin_unlock(&svc->srv_lock);
112 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
117 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
119 struct ptlrpc_request_buffer_desc *rqbd;
122 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
123 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
124 svc->srv_nrqbd_receiving, svc->srv_nbufs);
125 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
126 rqbd = ptlrpc_alloc_rqbd(svc);
129 CERROR ("%s: Can't allocate request buffer\n",
134 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
142 ptlrpc_save_lock (struct ptlrpc_request *req,
143 struct lustre_handle *lock, int mode)
145 struct ptlrpc_reply_state *rs = req->rq_reply_state;
149 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
151 idx = rs->rs_nlocks++;
152 rs->rs_locks[idx] = *lock;
153 rs->rs_modes[idx] = mode;
154 rs->rs_difficult = 1;
158 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
160 struct ptlrpc_service *svc = rs->rs_service;
163 LASSERT (spin_is_locked (&svc->srv_lock));
165 LASSERT (rs->rs_difficult);
166 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
168 if (rs->rs_scheduled) /* being set up or already notified */
171 rs->rs_scheduled = 1;
172 list_del (&rs->rs_list);
173 list_add (&rs->rs_list, &svc->srv_reply_queue);
174 cfs_waitq_signal (&svc->srv_waitq);
178 ptlrpc_commit_replies (struct obd_device *obd)
180 struct list_head *tmp;
181 struct list_head *nxt;
183 /* Find any replies that have been committed and get their service
184 * to attend to complete them. */
186 /* CAVEAT EMPTOR: spinlock ordering!!! */
187 spin_lock(&obd->obd_uncommitted_replies_lock);
189 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
190 struct ptlrpc_reply_state *rs =
191 list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
193 LASSERT (rs->rs_difficult);
195 if (rs->rs_transno <= obd->obd_last_committed) {
196 struct ptlrpc_service *svc = rs->rs_service;
198 spin_lock (&svc->srv_lock);
199 list_del_init (&rs->rs_obd_list);
200 ptlrpc_schedule_difficult_reply (rs);
201 spin_unlock (&svc->srv_lock);
205 spin_unlock(&obd->obd_uncommitted_replies_lock);
209 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
211 struct ptlrpc_request_buffer_desc *rqbd;
216 spin_lock(&svc->srv_lock);
218 if (list_empty (&svc->srv_idle_rqbds)) {
219 spin_unlock(&svc->srv_lock);
223 rqbd = list_entry(svc->srv_idle_rqbds.next,
224 struct ptlrpc_request_buffer_desc,
226 list_del (&rqbd->rqbd_list);
228 /* assume we will post successfully */
229 svc->srv_nrqbd_receiving++;
230 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
232 spin_unlock(&svc->srv_lock);
234 rc = ptlrpc_register_rqbd(rqbd);
241 spin_lock(&svc->srv_lock);
243 svc->srv_nrqbd_receiving--;
244 list_del(&rqbd->rqbd_list);
245 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
247 /* Don't complain if no request buffers are posted right now; LNET
248 * won't drop requests because we set the portal lazy! */
250 spin_unlock(&svc->srv_lock);
255 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
256 svc_handler_t h, char *name,
257 struct proc_dir_entry *proc_entry,
258 svcreq_printfn_t prntfn)
260 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
261 c->psc_max_req_size, c->psc_max_reply_size,
262 c->psc_req_portal, c->psc_rep_portal,
263 c->psc_watchdog_timeout,
265 prntfn, c->psc_num_threads, c->psc_ctx_tags);
267 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
269 struct ptlrpc_service *
270 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
271 int req_portal, int rep_portal, int watchdog_timeout,
272 svc_handler_t handler, char *name,
273 cfs_proc_dir_entry_t *proc_entry,
274 svcreq_printfn_t svcreq_printfn, int num_threads,
278 struct ptlrpc_service *service;
282 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
283 LASSERT (ctx_tags != 0);
285 OBD_ALLOC_PTR(service);
289 /* First initialise enough for early teardown */
291 service->srv_name = name;
292 spin_lock_init(&service->srv_lock);
293 CFS_INIT_LIST_HEAD(&service->srv_threads);
294 cfs_waitq_init(&service->srv_waitq);
296 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
297 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
298 service->srv_buf_size = bufsize;
299 service->srv_rep_portal = rep_portal;
300 service->srv_req_portal = req_portal;
301 service->srv_watchdog_timeout = watchdog_timeout;
302 service->srv_handler = handler;
303 service->srv_request_history_print_fn = svcreq_printfn;
304 service->srv_request_seq = 1; /* valid seq #s start at 1 */
305 service->srv_request_max_cull_seq = 0;
306 service->srv_num_threads = num_threads;
307 service->srv_ctx_tags = ctx_tags;
309 rc = LNetSetLazyPortal(service->srv_req_portal);
312 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
313 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
314 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
315 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
316 CFS_INIT_LIST_HEAD(&service->srv_request_history);
317 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
318 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
319 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
320 cfs_waitq_init(&service->srv_free_rs_waitq);
322 spin_lock (&ptlrpc_all_services_lock);
323 list_add (&service->srv_list, &ptlrpc_all_services);
324 spin_unlock (&ptlrpc_all_services_lock);
326 /* Now allocate the request buffers */
327 rc = ptlrpc_grow_req_bufs(service);
328 /* We shouldn't be under memory pressure at startup, so
329 * fail if we can't post all our buffers at this time. */
333 /* Now allocate pool of reply buffers */
334 /* Increase max reply size to next power of two */
335 service->srv_max_reply_size = 1;
336 while (service->srv_max_reply_size <
337 max_reply_size + SPTLRPC_MAX_PAYLOAD)
338 service->srv_max_reply_size <<= 1;
340 if (proc_entry != NULL)
341 ptlrpc_lprocfs_register_service(proc_entry, service);
343 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
344 service->srv_name, service->srv_req_portal);
348 ptlrpc_unregister_service(service);
352 static void __ptlrpc_server_free_request(struct ptlrpc_request *req)
354 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
356 list_del(&req->rq_list);
358 if (req->rq_reply_state != NULL) {
359 ptlrpc_rs_decref(req->rq_reply_state);
360 req->rq_reply_state = NULL;
363 sptlrpc_svc_ctx_decref(req);
365 if (req != &rqbd->rqbd_req) {
366 /* NB request buffers use an embedded
367 * req if the incoming req unlinked the
368 * MD; this isn't one of them! */
369 OBD_FREE(req, sizeof(*req));
374 ptlrpc_server_free_request(struct ptlrpc_request *req)
376 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
377 struct ptlrpc_service *svc = rqbd->rqbd_service;
379 struct list_head *tmp;
380 struct list_head *nxt;
382 spin_lock(&svc->srv_lock);
384 svc->srv_n_active_reqs--;
385 list_add(&req->rq_list, &rqbd->rqbd_reqs);
387 refcount = --(rqbd->rqbd_refcount);
389 /* request buffer is now idle: add to history */
390 list_del(&rqbd->rqbd_list);
391 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
392 svc->srv_n_history_rqbds++;
394 /* cull some history?
395 * I expect only about 1 or 2 rqbds need to be recycled here */
396 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
397 rqbd = list_entry(svc->srv_history_rqbds.next,
398 struct ptlrpc_request_buffer_desc,
401 list_del(&rqbd->rqbd_list);
402 svc->srv_n_history_rqbds--;
404 /* remove rqbd's reqs from svc's req history while
405 * I've got the service lock */
406 list_for_each(tmp, &rqbd->rqbd_reqs) {
407 req = list_entry(tmp, struct ptlrpc_request,
409 /* Track the highest culled req seq */
410 if (req->rq_history_seq >
411 svc->srv_request_max_cull_seq)
412 svc->srv_request_max_cull_seq =
414 list_del(&req->rq_history_list);
417 spin_unlock(&svc->srv_lock);
419 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
420 req = list_entry(rqbd->rqbd_reqs.next,
421 struct ptlrpc_request,
423 __ptlrpc_server_free_request(req);
426 spin_lock(&svc->srv_lock);
428 /* schedule request buffer for re-use.
429 * NB I can only do this after I've disposed of their
430 * reqs; particularly the embedded req */
431 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
433 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
434 /* If we are low on memory, we are not interested in
436 list_del(&req->rq_history_list);
437 __ptlrpc_server_free_request(req);
440 spin_unlock(&svc->srv_lock);
444 /* This function makes sure dead exports are evicted in a timely manner.
445 This function is only called when some export receives a message (i.e.,
446 the network is up.) */
447 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
449 struct obd_export *oldest_exp;
456 /* Compensate for slow machines, etc, by faking our request time
457 into the future. Although this can break the strict time-ordering
458 of the list, we can be really lazy here - we don't have to evict
459 at the exact right moment. Eventually, all silent exports
460 will make it to the top of the list. */
461 exp->exp_last_request_time = max(exp->exp_last_request_time,
462 (time_t)CURRENT_SECONDS + extra_delay);
464 CDEBUG(D_INFO, "updating export %s at %ld\n",
465 exp->exp_client_uuid.uuid,
466 exp->exp_last_request_time);
468 /* exports may get disconnected from the chain even though the
469 export has references, so we must keep the spin lock while
470 manipulating the lists */
471 spin_lock(&exp->exp_obd->obd_dev_lock);
473 if (list_empty(&exp->exp_obd_chain_timed)) {
474 /* this one is not timed */
475 spin_unlock(&exp->exp_obd->obd_dev_lock);
480 list_move_tail(&exp->exp_obd_chain_timed,
481 &exp->exp_obd->obd_exports_timed);
483 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
484 struct obd_export, exp_obd_chain_timed);
485 oldest_time = oldest_exp->exp_last_request_time;
486 spin_unlock(&exp->exp_obd->obd_dev_lock);
488 if (exp->exp_obd->obd_recovering) {
489 /* be nice to everyone during recovery */
494 /* Note - racing to start/reset the obd_eviction timer is safe */
495 if (exp->exp_obd->obd_eviction_timer == 0) {
496 /* Check if the oldest entry is expired. */
497 if (CURRENT_SECONDS > (oldest_time +
498 (3 * obd_timeout / 2) + extra_delay)) {
499 /* We need a second timer, in case the net was down and
500 * it just came back. Since the pinger may skip every
501 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
502 * we better wait for 3. */
503 exp->exp_obd->obd_eviction_timer = CURRENT_SECONDS +
505 CDEBUG(D_HA, "%s: Think about evicting %s from %ld\n",
506 exp->exp_obd->obd_name, obd_export_nid2str(exp),
510 if (CURRENT_SECONDS > (exp->exp_obd->obd_eviction_timer +
512 /* The evictor won't evict anyone who we've heard from
513 * recently, so we don't have to check before we start
515 if (!ping_evictor_wake(exp))
516 exp->exp_obd->obd_eviction_timer = 0;
524 int lu_context_init(struct lu_context *ctx, __u32 tags)
529 void lu_context_fini(struct lu_context *ctx)
533 void lu_context_enter(struct lu_context *ctx)
537 void lu_context_exit(struct lu_context *ctx)
544 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
545 struct ptlrpc_thread *thread)
547 struct ptlrpc_request *request;
548 struct timeval work_start;
549 struct timeval work_end;
556 spin_lock(&svc->srv_lock);
557 if (list_empty (&svc->srv_request_queue) ||
558 (svc->srv_n_difficult_replies != 0 &&
559 svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
560 /* If all the other threads are handling requests, I must
561 * remain free to handle any 'difficult' reply that might
563 spin_unlock(&svc->srv_lock);
567 request = list_entry (svc->srv_request_queue.next,
568 struct ptlrpc_request, rq_list);
569 list_del_init (&request->rq_list);
570 svc->srv_n_queued_reqs--;
571 svc->srv_n_active_reqs++;
573 spin_unlock(&svc->srv_lock);
575 do_gettimeofday(&work_start);
576 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
577 if (svc->srv_stats != NULL) {
578 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
580 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
581 svc->srv_n_queued_reqs);
582 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
583 svc->srv_n_active_reqs);
586 /* go through security check/transform */
587 request->rq_auth_uid = INVALID_UID;
588 request->rq_auth_mapped_uid = INVALID_UID;
590 rc = sptlrpc_svc_unwrap_request(request);
594 case SECSVC_COMPLETE:
595 target_send_reply(request, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
604 /* Clear request swab mask; this is a new request */
605 request->rq_req_swab_mask = 0;
607 rc = lustre_unpack_msg(request->rq_reqmsg, request->rq_reqlen);
609 CERROR ("error unpacking request: ptl %d from %s"
610 " xid "LPU64"\n", svc->srv_req_portal,
611 libcfs_id2str(request->rq_peer), request->rq_xid);
615 rc = lustre_unpack_ptlrpc_body(request->rq_reqmsg);
617 CERROR ("error unpacking ptlrpc body: ptl %d from %s"
618 " xid "LPU64"\n", svc->srv_req_portal,
619 libcfs_id2str(request->rq_peer), request->rq_xid);
624 if (lustre_msg_get_type(request->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
625 CERROR("wrong packet type received (type=%u) from %s\n",
626 lustre_msg_get_type(request->rq_reqmsg),
627 libcfs_id2str(request->rq_peer));
631 rc = lu_context_init(&request->rq_session, LCT_SESSION);
633 CERROR("Failure to initialize session: %d\n", rc);
636 request->rq_session.lc_thread = thread;
637 lu_context_enter(&request->rq_session);
639 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
641 request->rq_svc_thread = thread;
642 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
644 request->rq_export = class_conn2export(
645 lustre_msg_get_handle(request->rq_reqmsg));
647 if (request->rq_export) {
648 if (lustre_msg_get_conn_cnt(request->rq_reqmsg) <
649 request->rq_export->exp_conn_cnt) {
650 DEBUG_REQ(D_ERROR, request,
651 "DROPPING req from old connection %d < %d",
652 lustre_msg_get_conn_cnt(request->rq_reqmsg),
653 request->rq_export->exp_conn_cnt);
656 if (request->rq_export->exp_obd &&
657 request->rq_export->exp_obd->obd_fail) {
658 /* Failing over, don't handle any more reqs, send
659 error response instead. */
660 CDEBUG(D_HA, "Dropping req %p for failed obd %s\n",
661 request, request->rq_export->exp_obd->obd_name);
662 request->rq_status = -ENODEV;
663 ptlrpc_error(request);
667 ptlrpc_update_export_timer(request->rq_export, timediff/500000);
670 /* Discard requests queued for longer than my timeout. If the
671 * client's timeout is similar to mine, she'll be timing out this
672 * REQ anyway (bug 1502) */
673 if (timediff / 1000000 > (long)obd_timeout) {
674 CERROR("Dropping timed-out opc %d request from %s"
675 ": %ld seconds old\n",
676 lustre_msg_get_opc(request->rq_reqmsg),
677 libcfs_id2str(request->rq_peer),
682 request->rq_phase = RQ_PHASE_INTERPRET;
684 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
685 "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
686 (request->rq_export ?
687 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
688 (request->rq_export ?
689 atomic_read(&request->rq_export->exp_refcount) : -99),
690 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
691 libcfs_id2str(request->rq_peer),
692 lustre_msg_get_opc(request->rq_reqmsg));
694 rc = svc->srv_handler(request);
696 request->rq_phase = RQ_PHASE_COMPLETE;
698 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
699 "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
700 (request->rq_export ?
701 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
702 (request->rq_export ?
703 atomic_read(&request->rq_export->exp_refcount) : -99),
704 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
705 libcfs_id2str(request->rq_peer),
706 lustre_msg_get_opc(request->rq_reqmsg));
709 if (request->rq_export != NULL)
710 class_export_put(request->rq_export);
713 lu_context_exit(&request->rq_session);
714 lu_context_fini(&request->rq_session);
716 do_gettimeofday(&work_end);
718 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
720 if (timediff / 1000000 > (long)obd_timeout)
721 CERROR("request "LPU64" opc %u from %s processed in %lds "
722 "trans "LPU64" rc %d/%d\n",
725 lustre_msg_get_opc(request->rq_reqmsg) : 0,
726 libcfs_id2str(request->rq_peer),
727 cfs_timeval_sub(&work_end, &request->rq_arrival_time,
730 lustre_msg_get_transno(request->rq_repmsg) :
731 request->rq_transno, request->rq_status,
733 lustre_msg_get_status(request->rq_repmsg) :
736 CDEBUG(D_HA, "request "LPU64" opc %u from %s processed in "
737 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
740 lustre_msg_get_opc(request->rq_reqmsg) : 0,
741 libcfs_id2str(request->rq_peer), timediff,
742 cfs_timeval_sub(&work_end, &request->rq_arrival_time,
744 request->rq_transno, request->rq_status,
746 lustre_msg_get_status(request->rq_repmsg) :
749 if (svc->srv_stats != NULL && request->rq_reqmsg != NULL) {
750 int opc = opcode_offset(lustre_msg_get_opc(request->rq_reqmsg));
752 LASSERT(opc < LUSTRE_MAX_OPCODES);
753 lprocfs_counter_add(svc->srv_stats,
754 opc + PTLRPC_LAST_CNTR,
759 ptlrpc_server_free_request(request);
765 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
767 struct ptlrpc_reply_state *rs;
768 struct obd_export *exp;
769 struct obd_device *obd;
774 spin_lock(&svc->srv_lock);
775 if (list_empty (&svc->srv_reply_queue)) {
776 spin_unlock(&svc->srv_lock);
780 rs = list_entry (svc->srv_reply_queue.next,
781 struct ptlrpc_reply_state, rs_list);
786 LASSERT (rs->rs_difficult);
787 LASSERT (rs->rs_scheduled);
789 list_del_init (&rs->rs_list);
791 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
792 spin_unlock(&svc->srv_lock);
794 spin_lock (&obd->obd_uncommitted_replies_lock);
795 /* Noop if removed already */
796 list_del_init (&rs->rs_obd_list);
797 spin_unlock (&obd->obd_uncommitted_replies_lock);
799 spin_lock (&exp->exp_lock);
800 /* Noop if removed already */
801 list_del_init (&rs->rs_exp_list);
802 spin_unlock (&exp->exp_lock);
804 spin_lock(&svc->srv_lock);
806 been_handled = rs->rs_handled;
809 nlocks = rs->rs_nlocks; /* atomic "steal", but */
810 rs->rs_nlocks = 0; /* locks still on rs_locks! */
812 if (nlocks == 0 && !been_handled) {
813 /* If we see this, we should already have seen the warning
814 * in mds_steal_ack_locks() */
815 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
818 rs->rs_xid, rs->rs_transno,
819 lustre_msg_get_opc(rs->rs_msg),
820 libcfs_nid2str(exp->exp_connection->c_peer.nid));
823 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
824 spin_unlock(&svc->srv_lock);
826 if (!been_handled && rs->rs_on_net) {
827 LNetMDUnlink(rs->rs_md_h);
828 /* Ignore return code; we're racing with
833 ldlm_lock_decref(&rs->rs_locks[nlocks],
834 rs->rs_modes[nlocks]);
836 spin_lock(&svc->srv_lock);
839 rs->rs_scheduled = 0;
841 if (!rs->rs_on_net) {
843 svc->srv_n_difficult_replies--;
844 spin_unlock(&svc->srv_lock);
846 class_export_put (exp);
847 rs->rs_export = NULL;
848 ptlrpc_rs_decref (rs);
849 atomic_dec (&svc->srv_outstanding_replies);
853 /* still on the net; callback will schedule */
854 spin_unlock(&svc->srv_lock);
859 /* FIXME make use of timeout later */
861 liblustre_check_services (void *arg)
863 int did_something = 0;
865 struct list_head *tmp, *nxt;
868 /* I'm relying on being single threaded, not to have to lock
869 * ptlrpc_all_services etc */
870 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
871 struct ptlrpc_service *svc =
872 list_entry (tmp, struct ptlrpc_service, srv_list);
874 if (svc->srv_nthreads != 0) /* I've recursed */
877 /* service threads can block for bulk, so this limits us
878 * (arbitrarily) to recursing 1 stack frame per service.
879 * Note that the problem with recursion is that we have to
880 * unwind completely before our caller can resume. */
885 rc = ptlrpc_server_handle_reply(svc);
886 rc |= ptlrpc_server_handle_request(svc, NULL);
887 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
894 RETURN(did_something);
896 #define ptlrpc_stop_all_threads(s) do {} while (0)
898 #else /* __KERNEL__ */
900 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
901 void ptlrpc_daemonize(char *name)
903 struct fs_struct *fs = current->fs;
905 atomic_inc(&fs->count);
907 exit_fs(cfs_current());
909 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
913 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
915 int avail = svc->srv_nrqbd_receiving;
916 int low_water = test_req_buffer_pressure ? 0 :
917 svc->srv_nbuf_per_group/2;
919 /* NB I'm not locking; just looking. */
921 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
922 * allowed the request history to grow out of control. We could put a
923 * sanity check on that here and cull some history if we need the
926 if (avail <= low_water)
927 ptlrpc_grow_req_bufs(svc);
930 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
935 ptlrpc_retry_rqbds(void *arg)
937 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
939 svc->srv_rqbd_timeout = 0;
943 static int ptlrpc_main(void *arg)
945 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
946 struct ptlrpc_service *svc = data->svc;
947 struct ptlrpc_thread *thread = data->thread;
948 struct ptlrpc_reply_state *rs;
949 struct lc_watchdog *watchdog;
950 #ifdef WITH_GROUP_INFO
951 struct group_info *ginfo = NULL;
957 ptlrpc_daemonize(data->name);
959 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9) && CONFIG_NUMA
960 /* we need to do this before any per-thread allocation is done so that
961 * we get the per-thread allocations on local node. bug 7342 */
962 if (svc->srv_cpu_affinity) {
965 for (cpu = 0, num_cpu = 0; cpu < NR_CPUS; cpu++) {
966 if (!cpu_online(cpu))
968 if (num_cpu == thread->t_id % num_online_cpus())
972 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
976 #ifdef WITH_GROUP_INFO
977 ginfo = groups_alloc(0);
983 set_current_groups(ginfo);
984 put_group_info(ginfo);
987 if (svc->srv_init != NULL) {
988 rc = svc->srv_init(thread);
993 rc = lu_context_init(&env.le_ctx, svc->srv_ctx_tags);
997 thread->t_env = &env;
998 env.le_ctx.lc_thread = thread;
1000 /* Alloc reply state structure for this one */
1001 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1007 /* Record that the thread is running */
1008 thread->t_flags = SVC_RUNNING;
1010 * wake up our creator. Note: @data is invalid after this point,
1011 * because it's allocated on ptlrpc_start_thread() stack.
1013 cfs_waitq_signal(&thread->t_ctl_waitq);
1015 watchdog = lc_watchdog_add(svc->srv_watchdog_timeout, NULL, NULL);
1017 spin_lock(&svc->srv_lock);
1018 svc->srv_nthreads++;
1019 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1020 spin_unlock(&svc->srv_lock);
1021 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1023 CDEBUG(D_NET, "service thread %d started\n", thread->t_id);
1025 /* XXX maintain a list of all managed devices: insert here */
1027 while ((thread->t_flags & SVC_STOPPING) == 0 ||
1028 svc->srv_n_difficult_replies != 0) {
1029 /* Don't exit while there are replies to be handled */
1030 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1031 ptlrpc_retry_rqbds, svc);
1033 lc_watchdog_disable(watchdog);
1035 l_wait_event_exclusive (svc->srv_waitq,
1036 ((thread->t_flags & SVC_STOPPING) != 0 &&
1037 svc->srv_n_difficult_replies == 0) ||
1038 (!list_empty(&svc->srv_idle_rqbds) &&
1039 svc->srv_rqbd_timeout == 0) ||
1040 !list_empty (&svc->srv_reply_queue) ||
1041 (!list_empty (&svc->srv_request_queue) &&
1042 (svc->srv_n_difficult_replies == 0 ||
1043 svc->srv_n_active_reqs <
1044 (svc->srv_nthreads - 1))),
1047 lc_watchdog_touch(watchdog);
1049 ptlrpc_check_rqbd_pool(svc);
1051 if (!list_empty (&svc->srv_reply_queue))
1052 ptlrpc_server_handle_reply (svc);
1054 /* only handle requests if there are no difficult replies
1055 * outstanding, or I'm not the last thread handling
1057 if (!list_empty (&svc->srv_request_queue) &&
1058 (svc->srv_n_difficult_replies == 0 ||
1059 svc->srv_n_active_reqs < (svc->srv_nthreads - 1))) {
1060 lu_context_enter(&env.le_ctx);
1061 ptlrpc_server_handle_request(svc, thread);
1062 lu_context_exit(&env.le_ctx);
1065 if (!list_empty(&svc->srv_idle_rqbds) &&
1066 ptlrpc_server_post_idle_rqbds(svc) < 0) {
1067 /* I just failed to repost request buffers. Wait
1068 * for a timeout (unless something else happens)
1069 * before I try again */
1070 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1071 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1072 svc->srv_nrqbd_receiving);
1076 lc_watchdog_delete(watchdog);
1080 * deconstruct service specific state created by ptlrpc_start_thread()
1082 if (svc->srv_done != NULL)
1083 svc->srv_done(thread);
1088 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1090 spin_lock(&svc->srv_lock);
1091 svc->srv_nthreads--; /* must know immediately */
1093 thread->t_flags = SVC_STOPPED;
1095 cfs_waitq_signal(&thread->t_ctl_waitq);
1096 spin_unlock(&svc->srv_lock);
1101 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1102 struct ptlrpc_thread *thread)
1104 struct l_wait_info lwi = { 0 };
1106 spin_lock(&svc->srv_lock);
1107 thread->t_flags = SVC_STOPPING;
1108 spin_unlock(&svc->srv_lock);
1110 cfs_waitq_broadcast(&svc->srv_waitq);
1111 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1114 spin_lock(&svc->srv_lock);
1115 list_del(&thread->t_link);
1116 spin_unlock(&svc->srv_lock);
1118 OBD_FREE_PTR(thread);
1121 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1123 struct ptlrpc_thread *thread;
1125 spin_lock(&svc->srv_lock);
1126 while (!list_empty(&svc->srv_threads)) {
1127 thread = list_entry(svc->srv_threads.next,
1128 struct ptlrpc_thread, t_link);
1130 spin_unlock(&svc->srv_lock);
1131 ptlrpc_stop_thread(svc, thread);
1132 spin_lock(&svc->srv_lock);
1135 spin_unlock(&svc->srv_lock);
1138 /* @base_name should be 11 characters or less - 3 will be added on */
1139 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc,
1145 LASSERT(svc->srv_num_threads > 0);
1146 for (i = 0; i < svc->srv_num_threads; i++) {
1148 sprintf(name, "%s_%02d", base_name, i);
1149 rc = ptlrpc_start_thread(dev, svc, name, i);
1151 CERROR("cannot start %s thread #%d: rc %d\n", base_name,
1153 ptlrpc_stop_all_threads(svc);
1159 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
1162 struct l_wait_info lwi = { 0 };
1163 struct ptlrpc_svc_data d;
1164 struct ptlrpc_thread *thread;
1168 OBD_ALLOC_PTR(thread);
1171 cfs_waitq_init(&thread->t_ctl_waitq);
1174 spin_lock(&svc->srv_lock);
1175 list_add(&thread->t_link, &svc->srv_threads);
1176 spin_unlock(&svc->srv_lock);
1183 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1184 * just drop the VM and FILES in ptlrpc_daemonize() right away.
1186 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1188 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1190 spin_lock(&svc->srv_lock);
1191 list_del(&thread->t_link);
1192 spin_unlock(&svc->srv_lock);
1194 OBD_FREE(thread, sizeof(*thread));
1197 l_wait_event(thread->t_ctl_waitq,
1198 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1200 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1205 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1208 struct l_wait_info lwi;
1209 struct list_head *tmp;
1210 struct ptlrpc_reply_state *rs, *t;
1212 ptlrpc_stop_all_threads(service);
1213 LASSERT(list_empty(&service->srv_threads));
1215 spin_lock (&ptlrpc_all_services_lock);
1216 list_del_init (&service->srv_list);
1217 spin_unlock (&ptlrpc_all_services_lock);
1219 ptlrpc_lprocfs_unregister_service(service);
1221 /* All history will be culled when the next request buffer is
1223 service->srv_max_history_rqbds = 0;
1225 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1227 rc = LNetClearLazyPortal(service->srv_req_portal);
1230 /* Unlink all the request buffers. This forces a 'final' event with
1231 * its 'unlink' flag set for each posted rqbd */
1232 list_for_each(tmp, &service->srv_active_rqbds) {
1233 struct ptlrpc_request_buffer_desc *rqbd =
1234 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1237 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1238 LASSERT (rc == 0 || rc == -ENOENT);
1241 /* Wait for the network to release any buffers it's currently
1244 spin_lock(&service->srv_lock);
1245 rc = service->srv_nrqbd_receiving;
1246 spin_unlock(&service->srv_lock);
1251 /* Network access will complete in finite time but the HUGE
1252 * timeout lets us CWARN for visibility of sluggish NALs */
1253 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1254 rc = l_wait_event(service->srv_waitq,
1255 service->srv_nrqbd_receiving == 0,
1257 if (rc == -ETIMEDOUT)
1258 CWARN("Service %s waiting for request buffers\n",
1262 /* schedule all outstanding replies to terminate them */
1263 spin_lock(&service->srv_lock);
1264 while (!list_empty(&service->srv_active_replies)) {
1265 struct ptlrpc_reply_state *rs =
1266 list_entry(service->srv_active_replies.next,
1267 struct ptlrpc_reply_state, rs_list);
1268 ptlrpc_schedule_difficult_reply(rs);
1270 spin_unlock(&service->srv_lock);
1272 /* purge the request queue. NB No new replies (rqbds all unlinked)
1273 * and no service threads, so I'm the only thread noodling the
1274 * request queue now */
1275 while (!list_empty(&service->srv_request_queue)) {
1276 struct ptlrpc_request *req =
1277 list_entry(service->srv_request_queue.next,
1278 struct ptlrpc_request,
1281 list_del(&req->rq_list);
1282 service->srv_n_queued_reqs--;
1283 service->srv_n_active_reqs++;
1285 ptlrpc_server_free_request(req);
1287 LASSERT(service->srv_n_queued_reqs == 0);
1288 LASSERT(service->srv_n_active_reqs == 0);
1289 LASSERT(service->srv_n_history_rqbds == 0);
1290 LASSERT(list_empty(&service->srv_active_rqbds));
1292 /* Now free all the request buffers since nothing references them
1294 while (!list_empty(&service->srv_idle_rqbds)) {
1295 struct ptlrpc_request_buffer_desc *rqbd =
1296 list_entry(service->srv_idle_rqbds.next,
1297 struct ptlrpc_request_buffer_desc,
1300 ptlrpc_free_rqbd(rqbd);
1303 /* wait for all outstanding replies to complete (they were
1304 * scheduled having been flagged to abort above) */
1305 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1306 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1308 rc = l_wait_event(service->srv_waitq,
1309 !list_empty(&service->srv_reply_queue), &lwi);
1310 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1313 ptlrpc_server_handle_reply(service);
1316 CWARN("Unexpectedly long timeout %p\n", service);
1319 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1320 list_del(&rs->rs_list);
1321 OBD_FREE(rs, service->srv_max_reply_size);
1324 OBD_FREE_PTR(service);
1328 /* Returns 0 if the service is healthy.
1330 * Right now, it just checks to make sure that requests aren't languishing
1331 * in the queue. We'll use this health check to govern whether a node needs
1332 * to be shot, so it's intentionally non-aggressive. */
1333 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1335 struct ptlrpc_request *request;
1336 struct timeval right_now;
1337 long timediff, cutoff;
1343 spin_lock(&svc->srv_lock);
1345 if (list_empty(&svc->srv_request_queue))
1348 request = list_entry(svc->srv_request_queue.next,
1349 struct ptlrpc_request, rq_list);
1351 do_gettimeofday(&right_now);
1352 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1354 cutoff = obd_health_check_timeout;
1356 if (timediff / 1000000 > cutoff) {
1362 spin_unlock(&svc->srv_lock);