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 <lnet/types.h>
35 #include "ptlrpc_internal.h"
37 int test_req_buffer_pressure = 0;
38 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
39 "set non-zero to put pressure on request buffer pools");
42 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
44 static CFS_LIST_HEAD (ptlrpc_all_services);
45 spinlock_t ptlrpc_all_services_lock;
48 ptlrpc_alloc_request_buffer (int size)
52 if (size > SVC_BUF_VMALLOC_THRESHOLD)
53 OBD_VMALLOC(ptr, size);
61 ptlrpc_free_request_buffer (char *ptr, int size)
63 if (size > SVC_BUF_VMALLOC_THRESHOLD)
69 struct ptlrpc_request_buffer_desc *
70 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
72 struct ptlrpc_request_buffer_desc *rqbd;
74 OBD_ALLOC(rqbd, sizeof (*rqbd));
78 rqbd->rqbd_service = svc;
79 rqbd->rqbd_refcount = 0;
80 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
81 rqbd->rqbd_cbid.cbid_arg = rqbd;
82 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
83 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
85 if (rqbd->rqbd_buffer == NULL) {
86 OBD_FREE(rqbd, sizeof (*rqbd));
90 spin_lock(&svc->srv_lock);
91 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
93 spin_unlock(&svc->srv_lock);
99 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
101 struct ptlrpc_service *svc = rqbd->rqbd_service;
103 LASSERT (rqbd->rqbd_refcount == 0);
104 LASSERT (list_empty(&rqbd->rqbd_reqs));
106 spin_lock(&svc->srv_lock);
107 list_del(&rqbd->rqbd_list);
109 spin_unlock(&svc->srv_lock);
111 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
112 OBD_FREE (rqbd, sizeof (*rqbd));
116 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
118 struct ptlrpc_request_buffer_desc *rqbd;
121 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
122 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
123 svc->srv_nrqbd_receiving, svc->srv_nbufs);
124 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
125 rqbd = ptlrpc_alloc_rqbd(svc);
128 CERROR ("%s: Can't allocate request buffer\n",
133 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
141 ptlrpc_save_lock (struct ptlrpc_request *req,
142 struct lustre_handle *lock, int mode)
144 struct ptlrpc_reply_state *rs = req->rq_reply_state;
148 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
150 idx = rs->rs_nlocks++;
151 rs->rs_locks[idx] = *lock;
152 rs->rs_modes[idx] = mode;
153 rs->rs_difficult = 1;
157 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
159 struct ptlrpc_service *svc = rs->rs_service;
162 LASSERT (spin_is_locked (&svc->srv_lock));
164 LASSERT (rs->rs_difficult);
165 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
167 if (rs->rs_scheduled) /* being set up or already notified */
170 rs->rs_scheduled = 1;
171 list_del (&rs->rs_list);
172 list_add (&rs->rs_list, &svc->srv_reply_queue);
173 cfs_waitq_signal (&svc->srv_waitq);
177 ptlrpc_commit_replies (struct obd_device *obd)
179 struct list_head *tmp;
180 struct list_head *nxt;
182 /* Find any replies that have been committed and get their service
183 * to attend to complete them. */
185 /* CAVEAT EMPTOR: spinlock ordering!!! */
186 spin_lock(&obd->obd_uncommitted_replies_lock);
188 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
189 struct ptlrpc_reply_state *rs =
190 list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
192 LASSERT (rs->rs_difficult);
194 if (rs->rs_transno <= obd->obd_last_committed) {
195 struct ptlrpc_service *svc = rs->rs_service;
197 spin_lock (&svc->srv_lock);
198 list_del_init (&rs->rs_obd_list);
199 ptlrpc_schedule_difficult_reply (rs);
200 spin_unlock (&svc->srv_lock);
204 spin_unlock(&obd->obd_uncommitted_replies_lock);
208 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
210 struct ptlrpc_request_buffer_desc *rqbd;
215 spin_lock(&svc->srv_lock);
217 if (list_empty (&svc->srv_idle_rqbds)) {
218 spin_unlock(&svc->srv_lock);
222 rqbd = list_entry(svc->srv_idle_rqbds.next,
223 struct ptlrpc_request_buffer_desc,
225 list_del (&rqbd->rqbd_list);
227 /* assume we will post successfully */
228 svc->srv_nrqbd_receiving++;
229 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
231 spin_unlock(&svc->srv_lock);
233 rc = ptlrpc_register_rqbd(rqbd);
240 spin_lock(&svc->srv_lock);
242 svc->srv_nrqbd_receiving--;
243 list_del(&rqbd->rqbd_list);
244 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
246 /* Don't complain if no request buffers are posted right now; LNET
247 * won't drop requests because we set the portal lazy! */
249 spin_unlock(&svc->srv_lock);
254 /* @threadname should be 11 characters or less - 3 will be added on */
255 struct ptlrpc_service *
256 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
257 int req_portal, int rep_portal, int watchdog_timeout,
258 svc_handler_t handler, char *name,
259 cfs_proc_dir_entry_t *proc_entry,
260 svcreq_printfn_t svcreq_printfn,
261 int min_threads, int max_threads, char *threadname)
264 struct ptlrpc_service *service;
268 LASSERT (bufsize >= max_req_size);
270 OBD_ALLOC(service, sizeof(*service));
274 /* First initialise enough for early teardown */
276 service->srv_name = name;
277 spin_lock_init(&service->srv_lock);
278 CFS_INIT_LIST_HEAD(&service->srv_threads);
279 cfs_waitq_init(&service->srv_waitq);
281 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
282 service->srv_max_req_size = max_req_size;
283 service->srv_buf_size = bufsize;
284 service->srv_rep_portal = rep_portal;
285 service->srv_req_portal = req_portal;
286 service->srv_watchdog_timeout = watchdog_timeout;
287 service->srv_handler = handler;
288 service->srv_request_history_print_fn = svcreq_printfn;
289 service->srv_request_seq = 1; /* valid seq #s start at 1 */
290 service->srv_request_max_cull_seq = 0;
291 service->srv_threads_min = min_threads;
292 service->srv_threads_max = max_threads;
293 service->srv_thread_name = threadname;
295 rc = LNetSetLazyPortal(service->srv_req_portal);
298 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
299 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
300 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
301 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
302 CFS_INIT_LIST_HEAD(&service->srv_request_history);
303 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
304 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
305 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
306 cfs_waitq_init(&service->srv_free_rs_waitq);
308 spin_lock (&ptlrpc_all_services_lock);
309 list_add (&service->srv_list, &ptlrpc_all_services);
310 spin_unlock (&ptlrpc_all_services_lock);
312 /* Now allocate the request buffers */
313 rc = ptlrpc_grow_req_bufs(service);
314 /* We shouldn't be under memory pressure at startup, so
315 * fail if we can't post all our buffers at this time. */
319 /* Now allocate pool of reply buffers */
320 /* Increase max reply size to next power of two */
321 service->srv_max_reply_size = 1;
322 while (service->srv_max_reply_size < max_reply_size)
323 service->srv_max_reply_size <<= 1;
325 if (proc_entry != NULL)
326 ptlrpc_lprocfs_register_service(proc_entry, service);
328 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
329 service->srv_name, service->srv_req_portal);
333 ptlrpc_unregister_service(service);
337 static void __ptlrpc_server_free_request(struct ptlrpc_request *req)
339 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
341 list_del(&req->rq_list);
343 if (req->rq_reply_state != NULL) {
344 ptlrpc_rs_decref(req->rq_reply_state);
345 req->rq_reply_state = NULL;
348 if (req != &rqbd->rqbd_req) {
349 /* NB request buffers use an embedded
350 * req if the incoming req unlinked the
351 * MD; this isn't one of them! */
352 OBD_FREE(req, sizeof(*req));
357 ptlrpc_server_free_request(struct ptlrpc_request *req)
359 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
360 struct ptlrpc_service *svc = rqbd->rqbd_service;
362 struct list_head *tmp;
363 struct list_head *nxt;
365 spin_lock(&svc->srv_lock);
367 svc->srv_n_active_reqs--;
368 list_add(&req->rq_list, &rqbd->rqbd_reqs);
370 refcount = --(rqbd->rqbd_refcount);
372 /* request buffer is now idle: add to history */
373 list_del(&rqbd->rqbd_list);
374 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
375 svc->srv_n_history_rqbds++;
377 /* cull some history?
378 * I expect only about 1 or 2 rqbds need to be recycled here */
379 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
380 rqbd = list_entry(svc->srv_history_rqbds.next,
381 struct ptlrpc_request_buffer_desc,
384 list_del(&rqbd->rqbd_list);
385 svc->srv_n_history_rqbds--;
387 /* remove rqbd's reqs from svc's req history while
388 * I've got the service lock */
389 list_for_each(tmp, &rqbd->rqbd_reqs) {
390 req = list_entry(tmp, struct ptlrpc_request,
392 /* Track the highest culled req seq */
393 if (req->rq_history_seq >
394 svc->srv_request_max_cull_seq)
395 svc->srv_request_max_cull_seq =
397 list_del(&req->rq_history_list);
400 spin_unlock(&svc->srv_lock);
402 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
403 req = list_entry(rqbd->rqbd_reqs.next,
404 struct ptlrpc_request,
406 __ptlrpc_server_free_request(req);
409 spin_lock(&svc->srv_lock);
411 /* schedule request buffer for re-use.
412 * NB I can only do this after I've disposed of their
413 * reqs; particularly the embedded req */
414 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
416 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
417 /* If we are low on memory, we are not interested in
419 list_del(&req->rq_history_list);
420 __ptlrpc_server_free_request(req);
423 spin_unlock(&svc->srv_lock);
427 /* This function makes sure dead exports are evicted in a timely manner.
428 This function is only called when some export receives a message (i.e.,
429 the network is up.) */
430 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
432 struct obd_export *oldest_exp;
439 /* Compensate for slow machines, etc, by faking our request time
440 into the future. Although this can break the strict time-ordering
441 of the list, we can be really lazy here - we don't have to evict
442 at the exact right moment. Eventually, all silent exports
443 will make it to the top of the list. */
444 exp->exp_last_request_time = max(exp->exp_last_request_time,
445 (time_t)CURRENT_SECONDS + extra_delay);
447 CDEBUG(D_INFO, "updating export %s at %ld\n",
448 exp->exp_client_uuid.uuid,
449 exp->exp_last_request_time);
451 /* exports may get disconnected from the chain even though the
452 export has references, so we must keep the spin lock while
453 manipulating the lists */
454 spin_lock(&exp->exp_obd->obd_dev_lock);
456 if (list_empty(&exp->exp_obd_chain_timed)) {
457 /* this one is not timed */
458 spin_unlock(&exp->exp_obd->obd_dev_lock);
463 list_move_tail(&exp->exp_obd_chain_timed,
464 &exp->exp_obd->obd_exports_timed);
466 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
467 struct obd_export, exp_obd_chain_timed);
468 oldest_time = oldest_exp->exp_last_request_time;
469 spin_unlock(&exp->exp_obd->obd_dev_lock);
471 if (exp->exp_obd->obd_recovering) {
472 /* be nice to everyone during recovery */
477 /* Note - racing to start/reset the obd_eviction timer is safe */
478 if (exp->exp_obd->obd_eviction_timer == 0) {
479 /* Check if the oldest entry is expired. */
480 if (CURRENT_SECONDS > (oldest_time +
481 (3 * obd_timeout / 2) + extra_delay)) {
482 /* We need a second timer, in case the net was down and
483 * it just came back. Since the pinger may skip every
484 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
485 * we better wait for 3. */
486 exp->exp_obd->obd_eviction_timer = CURRENT_SECONDS +
488 CDEBUG(D_HA, "%s: Think about evicting %s from %ld\n",
489 exp->exp_obd->obd_name, obd_export_nid2str(exp),
493 if (CURRENT_SECONDS > (exp->exp_obd->obd_eviction_timer +
495 /* The evictor won't evict anyone who we've heard from
496 * recently, so we don't have to check before we start
498 if (!ping_evictor_wake(exp))
499 exp->exp_obd->obd_eviction_timer = 0;
507 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
508 struct ptlrpc_thread *thread)
510 struct obd_export *export = NULL;
511 struct ptlrpc_request *request;
512 struct timeval work_start;
513 struct timeval work_end;
520 spin_lock(&svc->srv_lock);
521 if (list_empty (&svc->srv_request_queue) ||
522 (svc->srv_n_difficult_replies != 0 &&
523 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1))) {
524 /* If all the other threads are handling requests, I must
525 * remain free to handle any 'difficult' reply that might
527 spin_unlock(&svc->srv_lock);
531 request = list_entry (svc->srv_request_queue.next,
532 struct ptlrpc_request, rq_list);
533 list_del_init (&request->rq_list);
534 svc->srv_n_queued_reqs--;
535 svc->srv_n_active_reqs++;
537 spin_unlock(&svc->srv_lock);
539 do_gettimeofday(&work_start);
540 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
541 if (svc->srv_stats != NULL) {
542 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
544 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
545 svc->srv_n_queued_reqs);
546 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
547 svc->srv_n_active_reqs);
551 /* Clear request swab mask; this is a new request */
552 request->rq_req_swab_mask = 0;
554 rc = lustre_unpack_msg(request->rq_reqmsg, request->rq_reqlen);
556 CERROR ("error unpacking request: ptl %d from %s"
557 " xid "LPU64"\n", svc->srv_req_portal,
558 libcfs_id2str(request->rq_peer), request->rq_xid);
562 rc = lustre_unpack_req_ptlrpc_body(request, MSG_PTLRPC_BODY_OFF);
564 CERROR ("error unpacking ptlrpc body: ptl %d from %s"
565 " xid "LPU64"\n", svc->srv_req_portal,
566 libcfs_id2str(request->rq_peer), request->rq_xid);
571 if (lustre_msg_get_type(request->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
572 CERROR("wrong packet type received (type=%u) from %s\n",
573 lustre_msg_get_type(request->rq_reqmsg),
574 libcfs_id2str(request->rq_peer));
578 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
580 request->rq_svc_thread = thread;
581 request->rq_export = class_conn2export(
582 lustre_msg_get_handle(request->rq_reqmsg));
584 if (request->rq_export) {
585 if (lustre_msg_get_conn_cnt(request->rq_reqmsg) <
586 request->rq_export->exp_conn_cnt) {
587 DEBUG_REQ(D_ERROR, request,
588 "DROPPING req from old connection %d < %d",
589 lustre_msg_get_conn_cnt(request->rq_reqmsg),
590 request->rq_export->exp_conn_cnt);
593 if (request->rq_export->exp_obd &&
594 request->rq_export->exp_obd->obd_fail) {
595 /* Failing over, don't handle any more reqs, send
596 error response instead. */
597 CDEBUG(D_HA, "Dropping req %p for failed obd %s\n",
598 request, request->rq_export->exp_obd->obd_name);
599 request->rq_status = -ENODEV;
600 ptlrpc_error(request);
604 ptlrpc_update_export_timer(request->rq_export, timediff/500000);
605 export = class_export_rpc_get(request->rq_export);
608 /* Discard requests queued for longer than my timeout. If the
609 * client's timeout is similar to mine, she'll be timing out this
610 * REQ anyway (bug 1502) */
611 if (timediff / 1000000 > (long)obd_timeout) {
612 CERROR("Dropping timed-out opc %d request from %s"
613 ": %ld seconds old\n",
614 lustre_msg_get_opc(request->rq_reqmsg),
615 libcfs_id2str(request->rq_peer),
620 request->rq_phase = RQ_PHASE_INTERPRET;
622 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
623 "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
624 (request->rq_export ?
625 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
626 (request->rq_export ?
627 atomic_read(&request->rq_export->exp_refcount) : -99),
628 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
629 libcfs_id2str(request->rq_peer),
630 lustre_msg_get_opc(request->rq_reqmsg));
632 rc = svc->srv_handler(request);
634 request->rq_phase = RQ_PHASE_COMPLETE;
636 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
637 "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
638 (request->rq_export ?
639 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
640 (request->rq_export ?
641 atomic_read(&request->rq_export->exp_refcount) : -99),
642 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
643 libcfs_id2str(request->rq_peer),
644 lustre_msg_get_opc(request->rq_reqmsg));
648 class_export_rpc_put(export);
651 if (request->rq_export != NULL)
652 class_export_put(request->rq_export);
655 reply = request->rq_reply_state && request->rq_repmsg; /* bug 11169 */
657 do_gettimeofday(&work_end);
658 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
659 if (timediff / 1000000 > (long)obd_timeout)
660 CERROR("request "LPU64" opc %u from %s processed in %lds "
661 "trans "LPU64" rc %d/%d\n",
662 request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
663 libcfs_id2str(request->rq_peer),
664 cfs_timeval_sub(&work_end, &request->rq_arrival_time,
666 reply ? lustre_msg_get_transno(request->rq_repmsg) :
669 reply ? lustre_msg_get_status(request->rq_repmsg): -999);
671 CDEBUG(D_HA, "request "LPU64" opc %u from %s processed in "
672 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
673 request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
674 libcfs_id2str(request->rq_peer), timediff,
675 cfs_timeval_sub(&work_end, &request->rq_arrival_time,
677 request->rq_transno, request->rq_status,
678 reply ? lustre_msg_get_status(request->rq_repmsg): -999);
680 if (svc->srv_stats != NULL) {
681 int opc = opcode_offset(lustre_msg_get_opc(request->rq_reqmsg));
683 LASSERT(opc < LUSTRE_MAX_OPCODES);
684 lprocfs_counter_add(svc->srv_stats,
685 opc + PTLRPC_LAST_CNTR,
690 ptlrpc_server_free_request(request);
696 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
698 struct ptlrpc_reply_state *rs;
699 struct obd_export *exp;
700 struct obd_device *obd;
705 spin_lock(&svc->srv_lock);
706 if (list_empty (&svc->srv_reply_queue)) {
707 spin_unlock(&svc->srv_lock);
711 rs = list_entry (svc->srv_reply_queue.next,
712 struct ptlrpc_reply_state, rs_list);
717 LASSERT (rs->rs_difficult);
718 LASSERT (rs->rs_scheduled);
720 list_del_init (&rs->rs_list);
722 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
723 spin_unlock(&svc->srv_lock);
725 spin_lock (&obd->obd_uncommitted_replies_lock);
726 /* Noop if removed already */
727 list_del_init (&rs->rs_obd_list);
728 spin_unlock (&obd->obd_uncommitted_replies_lock);
730 spin_lock (&exp->exp_lock);
731 /* Noop if removed already */
732 list_del_init (&rs->rs_exp_list);
733 spin_unlock (&exp->exp_lock);
735 spin_lock(&svc->srv_lock);
737 been_handled = rs->rs_handled;
740 nlocks = rs->rs_nlocks; /* atomic "steal", but */
741 rs->rs_nlocks = 0; /* locks still on rs_locks! */
743 if (nlocks == 0 && !been_handled) {
744 /* If we see this, we should already have seen the warning
745 * in mds_steal_ack_locks() */
746 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
749 rs->rs_xid, rs->rs_transno,
750 lustre_msg_get_opc(rs->rs_msg),
751 libcfs_nid2str(exp->exp_connection->c_peer.nid));
754 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
755 spin_unlock(&svc->srv_lock);
757 if (!been_handled && rs->rs_on_net) {
758 LNetMDUnlink(rs->rs_md_h);
759 /* Ignore return code; we're racing with
764 ldlm_lock_decref(&rs->rs_locks[nlocks],
765 rs->rs_modes[nlocks]);
767 spin_lock(&svc->srv_lock);
770 rs->rs_scheduled = 0;
772 if (!rs->rs_on_net) {
774 svc->srv_n_difficult_replies--;
775 spin_unlock(&svc->srv_lock);
777 class_export_put (exp);
778 rs->rs_export = NULL;
779 ptlrpc_rs_decref (rs);
780 atomic_dec (&svc->srv_outstanding_replies);
784 /* still on the net; callback will schedule */
785 spin_unlock(&svc->srv_lock);
790 /* FIXME make use of timeout later */
792 liblustre_check_services (void *arg)
794 int did_something = 0;
796 struct list_head *tmp, *nxt;
799 /* I'm relying on being single threaded, not to have to lock
800 * ptlrpc_all_services etc */
801 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
802 struct ptlrpc_service *svc =
803 list_entry (tmp, struct ptlrpc_service, srv_list);
805 if (svc->srv_threads_running != 0) /* I've recursed */
808 /* service threads can block for bulk, so this limits us
809 * (arbitrarily) to recursing 1 stack frame per service.
810 * Note that the problem with recursion is that we have to
811 * unwind completely before our caller can resume. */
813 svc->srv_threads_running++;
816 rc = ptlrpc_server_handle_reply(svc);
817 rc |= ptlrpc_server_handle_request(svc, NULL);
818 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
822 svc->srv_threads_running--;
825 RETURN(did_something);
827 #define ptlrpc_stop_all_threads(s) do {} while (0)
829 #else /* __KERNEL__ */
831 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
832 void ptlrpc_daemonize(char *name)
834 struct fs_struct *fs = current->fs;
836 atomic_inc(&fs->count);
838 exit_fs(cfs_current());
840 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
844 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
846 int avail = svc->srv_nrqbd_receiving;
847 int low_water = test_req_buffer_pressure ? 0 :
848 svc->srv_nbuf_per_group/2;
850 /* NB I'm not locking; just looking. */
852 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
853 * allowed the request history to grow out of control. We could put a
854 * sanity check on that here and cull some history if we need the
857 if (avail <= low_water)
858 ptlrpc_grow_req_bufs(svc);
861 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
866 ptlrpc_retry_rqbds(void *arg)
868 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
870 svc->srv_rqbd_timeout = 0;
874 static int ptlrpc_main(void *arg)
876 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
877 struct ptlrpc_service *svc = data->svc;
878 struct ptlrpc_thread *thread = data->thread;
879 struct obd_device *dev = data->dev;
880 struct ptlrpc_reply_state *rs;
881 struct lc_watchdog *watchdog;
882 #ifdef WITH_GROUP_INFO
883 struct group_info *ginfo = NULL;
888 ptlrpc_daemonize(data->name);
890 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9) && defined(CONFIG_NUMA)
891 /* we need to do this before any per-thread allocation is done so that
892 * we get the per-thread allocations on local node. bug 7342 */
893 if (svc->srv_cpu_affinity) {
896 for (cpu = 0, num_cpu = 0; cpu < NR_CPUS; cpu++) {
897 if (!cpu_online(cpu))
899 if (num_cpu == thread->t_id % num_online_cpus())
903 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
907 #ifdef WITH_GROUP_INFO
908 ginfo = groups_alloc(0);
914 set_current_groups(ginfo);
915 put_group_info(ginfo);
918 if (svc->srv_init != NULL) {
919 rc = svc->srv_init(thread);
924 /* Alloc reply state structure for this one */
925 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
931 /* Record that the thread is running */
932 thread->t_flags = SVC_RUNNING;
934 * wake up our creator. Note: @data is invalid after this point,
935 * because it's allocated on ptlrpc_start_thread() stack.
937 cfs_waitq_signal(&thread->t_ctl_waitq);
939 watchdog = lc_watchdog_add(svc->srv_watchdog_timeout, NULL, NULL);
941 spin_lock(&svc->srv_lock);
942 svc->srv_threads_running++;
943 list_add(&rs->rs_list, &svc->srv_free_rs_list);
944 spin_unlock(&svc->srv_lock);
945 cfs_waitq_signal(&svc->srv_free_rs_waitq);
947 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
948 svc->srv_threads_running);
950 /* XXX maintain a list of all managed devices: insert here */
952 while ((thread->t_flags & SVC_STOPPING) == 0 ||
953 svc->srv_n_difficult_replies != 0) {
954 /* Don't exit while there are replies to be handled */
955 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
956 ptlrpc_retry_rqbds, svc);
958 lc_watchdog_disable(watchdog);
960 l_wait_event_exclusive (svc->srv_waitq,
961 ((thread->t_flags & SVC_STOPPING) != 0 &&
962 svc->srv_n_difficult_replies == 0) ||
963 (!list_empty(&svc->srv_idle_rqbds) &&
964 svc->srv_rqbd_timeout == 0) ||
965 !list_empty (&svc->srv_reply_queue) ||
966 (!list_empty (&svc->srv_request_queue) &&
967 (svc->srv_n_difficult_replies == 0 ||
968 svc->srv_n_active_reqs <
969 (svc->srv_threads_running - 1))),
972 lc_watchdog_touch(watchdog);
974 ptlrpc_check_rqbd_pool(svc);
976 if ((svc->srv_threads_started < svc->srv_threads_max) &&
977 (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
978 /* Ignore return code - we tried... */
979 ptlrpc_start_thread(dev, svc);
982 if (!list_empty (&svc->srv_reply_queue))
983 ptlrpc_server_handle_reply (svc);
985 /* only handle requests if there are no difficult replies
986 * outstanding, or I'm not the last thread handling
988 if (!list_empty (&svc->srv_request_queue) &&
989 (svc->srv_n_difficult_replies == 0 ||
990 svc->srv_n_active_reqs < (svc->srv_threads_running - 1)))
991 ptlrpc_server_handle_request(svc, thread);
993 if (!list_empty(&svc->srv_idle_rqbds) &&
994 ptlrpc_server_post_idle_rqbds(svc) < 0) {
995 /* I just failed to repost request buffers. Wait
996 * for a timeout (unless something else happens)
997 * before I try again */
998 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
999 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1000 svc->srv_nrqbd_receiving);
1004 lc_watchdog_delete(watchdog);
1008 * deconstruct service specific state created by ptlrpc_start_thread()
1010 if (svc->srv_done != NULL)
1011 svc->srv_done(thread);
1014 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1016 spin_lock(&svc->srv_lock);
1017 svc->srv_threads_running--; /* must know immediately */
1019 thread->t_flags = SVC_STOPPED;
1021 cfs_waitq_signal(&thread->t_ctl_waitq);
1022 spin_unlock(&svc->srv_lock);
1027 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1028 struct ptlrpc_thread *thread)
1030 struct l_wait_info lwi = { 0 };
1032 spin_lock(&svc->srv_lock);
1033 thread->t_flags = SVC_STOPPING;
1034 spin_unlock(&svc->srv_lock);
1036 cfs_waitq_broadcast(&svc->srv_waitq);
1037 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1040 spin_lock(&svc->srv_lock);
1041 list_del(&thread->t_link);
1042 spin_unlock(&svc->srv_lock);
1044 OBD_FREE(thread, sizeof(*thread));
1047 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1049 struct ptlrpc_thread *thread;
1051 spin_lock(&svc->srv_lock);
1052 while (!list_empty(&svc->srv_threads)) {
1053 thread = list_entry(svc->srv_threads.next,
1054 struct ptlrpc_thread, t_link);
1056 spin_unlock(&svc->srv_lock);
1057 ptlrpc_stop_thread(svc, thread);
1058 spin_lock(&svc->srv_lock);
1061 spin_unlock(&svc->srv_lock);
1064 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1069 LASSERT(svc->srv_threads_min > 0);
1070 for (i = 0; i < svc->srv_threads_min; i++) {
1071 rc = ptlrpc_start_thread(dev, svc);
1073 CERROR("cannot start %s thread #%d: rc %d\n",
1074 svc->srv_thread_name, i, rc);
1075 ptlrpc_stop_all_threads(svc);
1081 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1083 struct l_wait_info lwi = { 0 };
1084 struct ptlrpc_svc_data d;
1085 struct ptlrpc_thread *thread;
1090 /* Not worrying about the svc_lock at this check - no big deal if
1091 we start an extra thread or 2. */
1092 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1093 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1094 svc->srv_threads_max, svc->srv_threads_running);
1095 if (svc->srv_threads_started >= svc->srv_threads_max)
1098 OBD_ALLOC(thread, sizeof(*thread));
1101 cfs_waitq_init(&thread->t_ctl_waitq);
1103 spin_lock(&svc->srv_lock);
1104 list_add(&thread->t_link, &svc->srv_threads);
1105 id = ++svc->srv_threads_started;
1106 spin_unlock(&svc->srv_lock);
1109 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1115 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1117 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1118 * just drop the VM and FILES in ptlrpc_daemonize() right away.
1120 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1122 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1124 spin_lock(&svc->srv_lock);
1125 list_del(&thread->t_link);
1126 --svc->srv_threads_started;
1127 spin_unlock(&svc->srv_lock);
1129 OBD_FREE(thread, sizeof(*thread));
1132 l_wait_event(thread->t_ctl_waitq,
1133 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1135 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1140 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1143 struct l_wait_info lwi;
1144 struct list_head *tmp;
1145 struct ptlrpc_reply_state *rs, *t;
1147 ptlrpc_stop_all_threads(service);
1148 LASSERT(list_empty(&service->srv_threads));
1150 spin_lock (&ptlrpc_all_services_lock);
1151 list_del_init (&service->srv_list);
1152 spin_unlock (&ptlrpc_all_services_lock);
1154 ptlrpc_lprocfs_unregister_service(service);
1156 /* All history will be culled when the next request buffer is
1158 service->srv_max_history_rqbds = 0;
1160 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1162 rc = LNetClearLazyPortal(service->srv_req_portal);
1165 /* Unlink all the request buffers. This forces a 'final' event with
1166 * its 'unlink' flag set for each posted rqbd */
1167 list_for_each(tmp, &service->srv_active_rqbds) {
1168 struct ptlrpc_request_buffer_desc *rqbd =
1169 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1172 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1173 LASSERT (rc == 0 || rc == -ENOENT);
1176 /* Wait for the network to release any buffers it's currently
1179 spin_lock(&service->srv_lock);
1180 rc = service->srv_nrqbd_receiving;
1181 spin_unlock(&service->srv_lock);
1186 /* Network access will complete in finite time but the HUGE
1187 * timeout lets us CWARN for visibility of sluggish NALs */
1188 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1189 rc = l_wait_event(service->srv_waitq,
1190 service->srv_nrqbd_receiving == 0,
1192 if (rc == -ETIMEDOUT)
1193 CWARN("Service %s waiting for request buffers\n",
1197 /* schedule all outstanding replies to terminate them */
1198 spin_lock(&service->srv_lock);
1199 while (!list_empty(&service->srv_active_replies)) {
1200 struct ptlrpc_reply_state *rs =
1201 list_entry(service->srv_active_replies.next,
1202 struct ptlrpc_reply_state, rs_list);
1203 ptlrpc_schedule_difficult_reply(rs);
1205 spin_unlock(&service->srv_lock);
1207 /* purge the request queue. NB No new replies (rqbds all unlinked)
1208 * and no service threads, so I'm the only thread noodling the
1209 * request queue now */
1210 while (!list_empty(&service->srv_request_queue)) {
1211 struct ptlrpc_request *req =
1212 list_entry(service->srv_request_queue.next,
1213 struct ptlrpc_request,
1216 list_del(&req->rq_list);
1217 service->srv_n_queued_reqs--;
1218 service->srv_n_active_reqs++;
1220 ptlrpc_server_free_request(req);
1222 LASSERT(service->srv_n_queued_reqs == 0);
1223 LASSERT(service->srv_n_active_reqs == 0);
1224 LASSERT(service->srv_n_history_rqbds == 0);
1225 LASSERT(list_empty(&service->srv_active_rqbds));
1227 /* Now free all the request buffers since nothing references them
1229 while (!list_empty(&service->srv_idle_rqbds)) {
1230 struct ptlrpc_request_buffer_desc *rqbd =
1231 list_entry(service->srv_idle_rqbds.next,
1232 struct ptlrpc_request_buffer_desc,
1235 ptlrpc_free_rqbd(rqbd);
1238 /* wait for all outstanding replies to complete (they were
1239 * scheduled having been flagged to abort above) */
1240 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1241 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1243 rc = l_wait_event(service->srv_waitq,
1244 !list_empty(&service->srv_reply_queue), &lwi);
1245 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1248 ptlrpc_server_handle_reply(service);
1251 CWARN("Unexpectedly long timeout %p\n", service);
1254 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1255 list_del(&rs->rs_list);
1256 OBD_FREE(rs, service->srv_max_reply_size);
1259 OBD_FREE(service, sizeof(*service));
1263 /* Returns 0 if the service is healthy.
1265 * Right now, it just checks to make sure that requests aren't languishing
1266 * in the queue. We'll use this health check to govern whether a node needs
1267 * to be shot, so it's intentionally non-aggressive. */
1268 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1270 struct ptlrpc_request *request;
1271 struct timeval right_now;
1272 long timediff, cutoff;
1278 spin_lock(&svc->srv_lock);
1280 if (list_empty(&svc->srv_request_queue))
1283 request = list_entry(svc->srv_request_queue.next,
1284 struct ptlrpc_request, rq_list);
1286 do_gettimeofday(&right_now);
1287 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1289 cutoff = obd_health_check_timeout;
1291 if (timediff / 1000000 > cutoff) {
1297 spin_unlock(&svc->srv_lock);