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 Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <liblustre.h>
26 #include <libcfs/kp30.h>
28 #include <linux/obd_support.h>
29 #include <linux/obd_class.h>
30 #include <linux/lustre_net.h>
31 #include <portals/types.h>
32 #include "ptlrpc_internal.h"
35 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
37 static LIST_HEAD (ptlrpc_all_services);
38 static spinlock_t ptlrpc_all_services_lock = SPIN_LOCK_UNLOCKED;
41 ptlrpc_alloc_request_buffer (int size)
45 if (size > SVC_BUF_VMALLOC_THRESHOLD)
46 OBD_VMALLOC(ptr, size);
54 ptlrpc_free_request_buffer (char *ptr, int size)
56 if (size > SVC_BUF_VMALLOC_THRESHOLD)
62 struct ptlrpc_request_buffer_desc *
63 ptlrpc_alloc_rqbd (struct ptlrpc_srv_ni *srv_ni)
65 struct ptlrpc_service *svc = srv_ni->sni_service;
67 struct ptlrpc_request_buffer_desc *rqbd;
69 OBD_ALLOC(rqbd, sizeof (*rqbd));
73 rqbd->rqbd_srv_ni = srv_ni;
74 rqbd->rqbd_refcount = 0;
75 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
76 rqbd->rqbd_cbid.cbid_arg = rqbd;
77 INIT_LIST_HEAD(&rqbd->rqbd_reqs);
78 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
80 if (rqbd->rqbd_buffer == NULL) {
81 OBD_FREE(rqbd, sizeof (*rqbd));
85 spin_lock_irqsave (&svc->srv_lock, flags);
86 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
88 spin_unlock_irqrestore (&svc->srv_lock, flags);
94 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
96 struct ptlrpc_srv_ni *sni = rqbd->rqbd_srv_ni;
97 struct ptlrpc_service *svc = sni->sni_service;
100 LASSERT (rqbd->rqbd_refcount == 0);
101 LASSERT (list_empty(&rqbd->rqbd_reqs));
103 spin_lock_irqsave(&svc->srv_lock, flags);
104 list_del(&rqbd->rqbd_list);
106 spin_unlock_irqrestore(&svc->srv_lock, flags);
108 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
109 OBD_FREE (rqbd, sizeof (*rqbd));
113 ptlrpc_grow_req_bufs(struct ptlrpc_srv_ni *srv_ni)
115 struct ptlrpc_service *svc = srv_ni->sni_service;
116 struct ptlrpc_request_buffer_desc *rqbd;
119 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
120 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
121 srv_ni->sni_nrqbd_receiving, svc->srv_nbufs);
122 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
123 rqbd = ptlrpc_alloc_rqbd(srv_ni);
126 CERROR ("%s/%s: Can't allocate request buffer\n",
127 svc->srv_name, srv_ni->sni_ni->pni_name);
131 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
139 ptlrpc_save_lock (struct ptlrpc_request *req,
140 struct lustre_handle *lock, int mode)
142 struct ptlrpc_reply_state *rs = req->rq_reply_state;
145 LASSERT (rs != NULL);
146 LASSERT (rs->rs_nlocks < RS_MAX_LOCKS);
148 idx = rs->rs_nlocks++;
149 rs->rs_locks[idx] = *lock;
150 rs->rs_modes[idx] = mode;
151 rs->rs_difficult = 1;
155 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
157 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
160 LASSERT (spin_is_locked (&svc->srv_lock));
162 LASSERT (rs->rs_difficult);
163 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
165 if (rs->rs_scheduled) /* being set up or already notified */
168 rs->rs_scheduled = 1;
169 list_del (&rs->rs_list);
170 list_add (&rs->rs_list, &svc->srv_reply_queue);
171 wake_up (&svc->srv_waitq);
175 ptlrpc_commit_replies (struct obd_device *obd)
177 struct list_head *tmp;
178 struct list_head *nxt;
181 /* Find any replies that have been committed and get their service
182 * to attend to complete them. */
184 /* CAVEAT EMPTOR: spinlock ordering!!! */
185 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
187 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
188 struct ptlrpc_reply_state *rs =
189 list_entry (tmp, struct ptlrpc_reply_state, rs_obd_list);
191 LASSERT (rs->rs_difficult);
193 if (rs->rs_transno <= obd->obd_last_committed) {
194 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
196 spin_lock (&svc->srv_lock);
197 list_del_init (&rs->rs_obd_list);
198 ptlrpc_schedule_difficult_reply (rs);
199 spin_unlock (&svc->srv_lock);
203 spin_unlock_irqrestore (&obd->obd_uncommitted_replies_lock, flags);
207 timeval_sub(struct timeval *large, struct timeval *small)
209 return (large->tv_sec - small->tv_sec) * 1000000 +
210 (large->tv_usec - small->tv_usec);
214 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
216 struct ptlrpc_srv_ni *srv_ni;
217 struct ptlrpc_request_buffer_desc *rqbd;
223 spin_lock_irqsave(&svc->srv_lock, flags);
225 if (list_empty (&svc->srv_idle_rqbds)) {
226 spin_unlock_irqrestore(&svc->srv_lock, flags);
230 rqbd = list_entry(svc->srv_idle_rqbds.next,
231 struct ptlrpc_request_buffer_desc,
233 list_del (&rqbd->rqbd_list);
235 /* assume we will post successfully */
236 srv_ni = rqbd->rqbd_srv_ni;
237 srv_ni->sni_nrqbd_receiving++;
238 list_add (&rqbd->rqbd_list, &srv_ni->sni_active_rqbds);
240 spin_unlock_irqrestore(&svc->srv_lock, flags);
242 rc = ptlrpc_register_rqbd(rqbd);
249 spin_lock_irqsave(&svc->srv_lock, flags);
251 srv_ni->sni_nrqbd_receiving--;
252 list_del(&rqbd->rqbd_list);
253 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
255 if (srv_ni->sni_nrqbd_receiving == 0) {
256 /* This service is off-air on this interface because all
257 * its request buffers are busy. Portals will have started
258 * dropping incoming requests until more buffers get
260 CERROR("All %s %s request buffers busy\n",
261 svc->srv_name, srv_ni->sni_ni->pni_name);
264 spin_unlock_irqrestore (&svc->srv_lock, flags);
269 struct ptlrpc_service *
270 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
271 int req_portal, int rep_portal, int watchdog_timeout,
272 svc_handler_t handler, char *name,
273 struct proc_dir_entry *proc_entry,
274 svcreq_printfn_t svcreq_printfn)
279 struct ptlrpc_service *service;
280 struct ptlrpc_srv_ni *srv_ni;
283 LASSERT (ptlrpc_ninterfaces > 0);
285 LASSERT (bufsize >= max_req_size);
287 ssize = offsetof (struct ptlrpc_service,
288 srv_interfaces[ptlrpc_ninterfaces]);
289 OBD_ALLOC(service, ssize);
293 service->srv_name = name;
294 spin_lock_init(&service->srv_lock);
295 INIT_LIST_HEAD(&service->srv_threads);
296 init_waitqueue_head(&service->srv_waitq);
298 service->srv_nbuf_per_group = nbufs;
299 service->srv_max_req_size = max_req_size;
300 service->srv_buf_size = bufsize;
301 service->srv_rep_portal = rep_portal;
302 service->srv_req_portal = req_portal;
303 service->srv_watchdog_timeout = watchdog_timeout;
304 service->srv_handler = handler;
305 service->srv_request_history_print_fn = svcreq_printfn;
306 service->srv_request_seq = 1; /* valid seq #s start at 1 */
307 service->srv_request_max_cull_seq = 0;
309 INIT_LIST_HEAD(&service->srv_request_queue);
310 INIT_LIST_HEAD(&service->srv_idle_rqbds);
311 INIT_LIST_HEAD(&service->srv_history_rqbds);
312 INIT_LIST_HEAD(&service->srv_request_history);
313 INIT_LIST_HEAD(&service->srv_reply_queue);
315 /* First initialise enough for early teardown */
316 for (i = 0; i < ptlrpc_ninterfaces; i++) {
317 srv_ni = &service->srv_interfaces[i];
319 srv_ni->sni_service = service;
320 srv_ni->sni_ni = &ptlrpc_interfaces[i];
321 INIT_LIST_HEAD(&srv_ni->sni_active_rqbds);
322 INIT_LIST_HEAD(&srv_ni->sni_active_replies);
325 spin_lock (&ptlrpc_all_services_lock);
326 list_add (&service->srv_list, &ptlrpc_all_services);
327 spin_unlock (&ptlrpc_all_services_lock);
329 /* Now allocate the request buffers, assuming all interfaces require
330 * the same number. */
331 for (i = 0; i < ptlrpc_ninterfaces; i++) {
332 srv_ni = &service->srv_interfaces[i];
333 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
334 srv_ni->sni_ni->pni_name);
336 rc = ptlrpc_grow_req_bufs(srv_ni);
337 /* We shouldn't be under memory pressure at startup, so
338 * fail if we can't post all our buffers at this time. */
343 if (proc_entry != NULL)
344 ptlrpc_lprocfs_register_service(proc_entry, service);
346 CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
347 service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
351 ptlrpc_unregister_service(service);
356 ptlrpc_server_free_request(struct ptlrpc_request *req)
358 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
359 struct ptlrpc_srv_ni *srv_ni = rqbd->rqbd_srv_ni;
360 struct ptlrpc_service *svc = srv_ni->sni_service;
363 struct list_head *tmp;
364 struct list_head *nxt;
366 spin_lock_irqsave(&svc->srv_lock, flags);
368 svc->srv_n_active_reqs--;
369 list_add (&req->rq_list, &rqbd->rqbd_reqs);
371 refcount = --(rqbd->rqbd_refcount);
373 /* request buffer is now idle: add to history */
374 list_del(&rqbd->rqbd_list);
375 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
376 svc->srv_n_history_rqbds++;
378 /* cull some history?
379 * I expect only about 1 or 2 rqbds need to be recycled here */
380 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
381 rqbd = list_entry(svc->srv_history_rqbds.next,
382 struct ptlrpc_request_buffer_desc,
385 list_del(&rqbd->rqbd_list);
386 svc->srv_n_history_rqbds--;
388 /* remove rqbd's reqs from svc's req history while
389 * I've got the service lock */
390 list_for_each(tmp, &rqbd->rqbd_reqs) {
391 req = list_entry(tmp, struct ptlrpc_request,
393 /* Track the highest culled req seq */
394 if (req->rq_history_seq >
395 svc->srv_request_max_cull_seq)
396 svc->srv_request_max_cull_seq =
398 list_del(&req->rq_history_list);
401 spin_unlock_irqrestore(&svc->srv_lock, flags);
403 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
404 req = list_entry(rqbd->rqbd_reqs.next,
405 struct ptlrpc_request,
408 list_del(&req->rq_list);
410 if (req->rq_reply_state != NULL) {
411 ptlrpc_rs_decref(req->rq_reply_state);
412 req->rq_reply_state = NULL;
415 if (req != &rqbd->rqbd_req) {
416 /* NB request buffers use an embedded
417 * req if the incoming req unlinked the
418 * MD; this isn't one of them! */
419 OBD_FREE(req, sizeof(*req));
423 spin_lock_irqsave(&svc->srv_lock, flags);
425 /* schedule request buffer for re-use.
426 * NB I can only do this after I've disposed of their
427 * reqs; particularly the embedded req */
428 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
432 spin_unlock_irqrestore(&svc->srv_lock, flags);
436 ptlrpc_server_handle_request (struct ptlrpc_service *svc)
438 struct ptlrpc_request *request;
440 struct timeval work_start;
441 struct timeval work_end;
448 spin_lock_irqsave (&svc->srv_lock, flags);
449 if (list_empty (&svc->srv_request_queue) ||
450 (svc->srv_n_difficult_replies != 0 &&
451 svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
452 /* If all the other threads are handling requests, I must
453 * remain free to handle any 'difficult' reply that might
455 spin_unlock_irqrestore (&svc->srv_lock, flags);
459 request = list_entry (svc->srv_request_queue.next,
460 struct ptlrpc_request, rq_list);
461 list_del_init (&request->rq_list);
462 svc->srv_n_queued_reqs--;
463 svc->srv_n_active_reqs++;
465 spin_unlock_irqrestore (&svc->srv_lock, flags);
467 do_gettimeofday(&work_start);
468 timediff = timeval_sub(&work_start, &request->rq_arrival_time);
469 if (svc->srv_stats != NULL) {
470 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
472 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
473 svc->srv_n_queued_reqs);
474 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
475 svc->srv_n_active_reqs);
479 /* Clear request swab mask; this is a new request */
480 request->rq_req_swab_mask = 0;
482 rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
484 CERROR ("error unpacking request: ptl %d from %s"
485 " xid "LPU64"\n", svc->srv_req_portal,
486 request->rq_peerstr, request->rq_xid);
491 if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
492 CERROR("wrong packet type received (type=%u) from %s\n",
493 request->rq_reqmsg->type, request->rq_peerstr);
497 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
499 request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
501 if (request->rq_export) {
502 if (request->rq_reqmsg->conn_cnt <
503 request->rq_export->exp_conn_cnt) {
504 DEBUG_REQ(D_ERROR, request,
505 "DROPPING req from old connection %d < %d",
506 request->rq_reqmsg->conn_cnt,
507 request->rq_export->exp_conn_cnt);
510 if (request->rq_export->exp_obd
511 && request->rq_export->exp_obd->obd_fail) {
512 /* Failing over, don't handle any more reqs, send
513 error response instead. */
514 CDEBUG(D_HA, "Dropping req %p for failed obd %s\n",
515 request, request->rq_export->exp_obd->obd_name);
516 request->rq_status = -ENODEV;
517 ptlrpc_error(request);
521 class_update_export_timer(request->rq_export,
522 (time_t)(timediff / 1000000));
525 /* Discard requests queued for longer than my timeout. If the
526 * client's timeout is similar to mine, she'll be timing out this
527 * REQ anyway (bug 1502) */
528 if (timediff / 1000000 > (long)obd_timeout) {
529 CERROR("Dropping timed-out opc %d request from %s"
530 ": %ld seconds old\n", request->rq_reqmsg->opc,
536 request->rq_phase = RQ_PHASE_INTERPRET;
538 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
539 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
540 (request->rq_export ?
541 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
542 (request->rq_export ?
543 atomic_read(&request->rq_export->exp_refcount) : -99),
544 request->rq_reqmsg->status, request->rq_xid,
545 request->rq_peer.peer_ni->pni_name,
547 request->rq_reqmsg->opc);
549 rc = svc->srv_handler(request);
551 request->rq_phase = RQ_PHASE_COMPLETE;
553 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
554 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
555 (request->rq_export ?
556 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
557 (request->rq_export ?
558 atomic_read(&request->rq_export->exp_refcount) : -99),
559 request->rq_reqmsg->status, request->rq_xid,
560 request->rq_peer.peer_ni->pni_name,
562 request->rq_reqmsg->opc);
565 if (request->rq_export != NULL)
566 class_export_put(request->rq_export);
569 do_gettimeofday(&work_end);
571 timediff = timeval_sub(&work_end, &work_start);
573 CDEBUG((timediff / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
574 "request "LPU64" opc %u from %s processed in %ldus "
575 "(%ldus total)\n", request->rq_xid, request->rq_reqmsg->opc,
577 timediff, timeval_sub(&work_end, &request->rq_arrival_time));
579 if (svc->srv_stats != NULL) {
580 int opc = opcode_offset(request->rq_reqmsg->opc);
582 LASSERT(opc < LUSTRE_MAX_OPCODES);
583 lprocfs_counter_add(svc->srv_stats,
584 opc + PTLRPC_LAST_CNTR,
589 ptlrpc_server_free_request(request);
595 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
597 struct ptlrpc_reply_state *rs;
599 struct obd_export *exp;
600 struct obd_device *obd;
603 char str[PTL_NALFMT_SIZE];
606 spin_lock_irqsave (&svc->srv_lock, flags);
607 if (list_empty (&svc->srv_reply_queue)) {
608 spin_unlock_irqrestore (&svc->srv_lock, flags);
612 rs = list_entry (svc->srv_reply_queue.next,
613 struct ptlrpc_reply_state, rs_list);
618 LASSERT (rs->rs_difficult);
619 LASSERT (rs->rs_scheduled);
621 list_del_init (&rs->rs_list);
623 /* Disengage from notifiers carefully (lock ordering!) */
624 spin_unlock(&svc->srv_lock);
626 spin_lock (&obd->obd_uncommitted_replies_lock);
627 /* Noop if removed already */
628 list_del_init (&rs->rs_obd_list);
629 spin_unlock (&obd->obd_uncommitted_replies_lock);
631 spin_lock (&exp->exp_lock);
632 /* Noop if removed already */
633 list_del_init (&rs->rs_exp_list);
634 spin_unlock (&exp->exp_lock);
636 spin_lock(&svc->srv_lock);
638 been_handled = rs->rs_handled;
641 nlocks = rs->rs_nlocks; /* atomic "steal", but */
642 rs->rs_nlocks = 0; /* locks still on rs_locks! */
644 if (nlocks == 0 && !been_handled) {
645 /* If we see this, we should already have seen the warning
646 * in mds_steal_ack_locks() */
647 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
650 rs->rs_xid, rs->rs_transno,
652 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
655 if ((!been_handled && rs->rs_on_net) ||
657 spin_unlock_irqrestore(&svc->srv_lock, flags);
659 if (!been_handled && rs->rs_on_net) {
660 PtlMDUnlink(rs->rs_md_h);
661 /* Ignore return code; we're racing with
666 ldlm_lock_decref(&rs->rs_locks[nlocks],
667 rs->rs_modes[nlocks]);
669 spin_lock_irqsave(&svc->srv_lock, flags);
672 rs->rs_scheduled = 0;
674 if (!rs->rs_on_net) {
676 svc->srv_n_difficult_replies--;
677 spin_unlock_irqrestore(&svc->srv_lock, flags);
679 class_export_put (exp);
680 rs->rs_export = NULL;
681 ptlrpc_rs_decref (rs);
682 atomic_dec (&svc->srv_outstanding_replies);
686 /* still on the net; callback will schedule */
687 spin_unlock_irqrestore (&svc->srv_lock, flags);
692 /* FIXME make use of timeout later */
694 liblustre_check_services (void *arg)
696 int did_something = 0;
698 struct list_head *tmp, *nxt;
701 /* I'm relying on being single threaded, not to have to lock
702 * ptlrpc_all_services etc */
703 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
704 struct ptlrpc_service *svc =
705 list_entry (tmp, struct ptlrpc_service, srv_list);
707 if (svc->srv_nthreads != 0) /* I've recursed */
710 /* service threads can block for bulk, so this limits us
711 * (arbitrarily) to recursing 1 stack frame per service.
712 * Note that the problem with recursion is that we have to
713 * unwind completely before our caller can resume. */
718 rc = ptlrpc_server_handle_reply(svc);
719 rc |= ptlrpc_server_handle_request(svc);
720 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
727 RETURN(did_something);
729 #define ptlrpc_stop_all_threads(s) do {} while (0)
731 #else /* __KERNEL__ */
733 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
734 void ptlrpc_daemonize(void)
737 lustre_daemonize_helper();
743 ptlrpc_check_rqbd_pools(struct ptlrpc_service *svc)
745 struct ptlrpc_srv_ni *sni;
748 int low_water = svc->srv_nbuf_per_group/2;
750 for (i = 0; i < ptlrpc_ninterfaces; i++) {
751 sni = &svc->srv_interfaces[i];
753 avail += sni->sni_nrqbd_receiving;
754 /* NB I'm not locking; just looking. */
756 /* CAVEAT EMPTOR: We might be allocating buffers here
757 * because we've allowed the request history to grow out of
758 * control. We could put a sanity check on that here and
759 * cull some history if we need the space. */
761 if (sni->sni_nrqbd_receiving <= low_water)
762 ptlrpc_grow_req_bufs(sni);
765 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR, avail);
769 ptlrpc_retry_rqbds(void *arg)
771 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
773 svc->srv_rqbd_timeout = 0;
777 static int ptlrpc_main(void *arg)
779 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
780 struct ptlrpc_service *svc = data->svc;
781 struct ptlrpc_thread *thread = data->thread;
782 struct lc_watchdog *watchdog;
784 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
785 struct group_info *ginfo = NULL;
792 SIGNAL_MASK_LOCK(current, flags);
793 sigfillset(¤t->blocked);
795 SIGNAL_MASK_UNLOCK(current, flags);
797 LASSERTF(strlen(data->name) < sizeof(current->comm),
798 "name %d > len %d\n",
799 (int)strlen(data->name), (int)sizeof(current->comm));
800 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
803 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
804 ginfo = groups_alloc(0);
806 thread->t_flags = SVC_RUNNING;
807 wake_up(&thread->t_ctl_waitq);
810 set_current_groups(ginfo);
811 put_group_info(ginfo);
814 /* Record that the thread is running */
815 thread->t_flags = SVC_RUNNING;
816 wake_up(&thread->t_ctl_waitq);
818 watchdog = lc_watchdog_add(svc->srv_watchdog_timeout,
819 LC_WATCHDOG_DEFAULT_CB, NULL);
821 spin_lock_irqsave(&svc->srv_lock, flags);
823 spin_unlock_irqrestore(&svc->srv_lock, flags);
825 /* XXX maintain a list of all managed devices: insert here */
827 while ((thread->t_flags & SVC_STOPPING) == 0 ||
828 svc->srv_n_difficult_replies != 0) {
829 /* Don't exit while there are replies to be handled */
830 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
831 ptlrpc_retry_rqbds, svc);
833 lc_watchdog_disable(watchdog);
835 l_wait_event_exclusive (svc->srv_waitq,
836 ((thread->t_flags & SVC_STOPPING) != 0 &&
837 svc->srv_n_difficult_replies == 0) ||
838 (!list_empty(&svc->srv_idle_rqbds) &&
839 svc->srv_rqbd_timeout == 0) ||
840 !list_empty (&svc->srv_reply_queue) ||
841 (!list_empty (&svc->srv_request_queue) &&
842 (svc->srv_n_difficult_replies == 0 ||
843 svc->srv_n_active_reqs <
844 (svc->srv_nthreads - 1))),
847 lc_watchdog_touch(watchdog);
849 ptlrpc_check_rqbd_pools(svc);
851 if (!list_empty (&svc->srv_reply_queue))
852 ptlrpc_server_handle_reply (svc);
854 /* only handle requests if there are no difficult replies
855 * outstanding, or I'm not the last thread handling
857 if (!list_empty (&svc->srv_request_queue) &&
858 (svc->srv_n_difficult_replies == 0 ||
859 svc->srv_n_active_reqs < (svc->srv_nthreads - 1)))
860 ptlrpc_server_handle_request (svc);
862 if (!list_empty(&svc->srv_idle_rqbds) &&
863 ptlrpc_server_post_idle_rqbds(svc) < 0) {
864 /* I just failed to repost request buffers. Wait
865 * for a timeout (unless something else happens)
866 * before I try again */
867 svc->srv_rqbd_timeout = HZ/10;
871 spin_lock_irqsave(&svc->srv_lock, flags);
873 svc->srv_nthreads--; /* must know immediately */
874 thread->t_flags = SVC_STOPPED;
875 wake_up(&thread->t_ctl_waitq);
877 spin_unlock_irqrestore(&svc->srv_lock, flags);
879 lc_watchdog_delete(watchdog);
881 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
885 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
886 struct ptlrpc_thread *thread)
888 struct l_wait_info lwi = { 0 };
891 spin_lock_irqsave(&svc->srv_lock, flags);
892 thread->t_flags = SVC_STOPPING;
893 spin_unlock_irqrestore(&svc->srv_lock, flags);
895 wake_up_all(&svc->srv_waitq);
896 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
899 spin_lock_irqsave(&svc->srv_lock, flags);
900 list_del(&thread->t_link);
901 spin_unlock_irqrestore(&svc->srv_lock, flags);
903 OBD_FREE(thread, sizeof(*thread));
906 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
909 struct ptlrpc_thread *thread;
911 spin_lock_irqsave(&svc->srv_lock, flags);
912 while (!list_empty(&svc->srv_threads)) {
913 thread = list_entry(svc->srv_threads.next,
914 struct ptlrpc_thread, t_link);
916 spin_unlock_irqrestore(&svc->srv_lock, flags);
917 ptlrpc_stop_thread(svc, thread);
918 spin_lock_irqsave(&svc->srv_lock, flags);
921 spin_unlock_irqrestore(&svc->srv_lock, flags);
924 /* @base_name should be 12 characters or less - 3 will be added on */
925 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
926 int num_threads, char *base_name)
931 for (i = 0; i < num_threads; i++) {
933 sprintf(name, "%s_%02d", base_name, i);
934 rc = ptlrpc_start_thread(dev, svc, name);
936 CERROR("cannot start %s thread #%d: rc %d\n", base_name,
938 ptlrpc_stop_all_threads(svc);
944 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
947 struct l_wait_info lwi = { 0 };
948 struct ptlrpc_svc_data d;
949 struct ptlrpc_thread *thread;
954 OBD_ALLOC(thread, sizeof(*thread));
957 init_waitqueue_head(&thread->t_ctl_waitq);
964 spin_lock_irqsave(&svc->srv_lock, flags);
965 list_add(&thread->t_link, &svc->srv_threads);
966 spin_unlock_irqrestore(&svc->srv_lock, flags);
968 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
969 * just drop the VM and FILES in ptlrpc_daemonize() right away.
971 rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
973 CERROR("cannot start thread '%s': rc %d\n", name, rc);
975 spin_lock_irqsave(&svc->srv_lock, flags);
976 list_del(&thread->t_link);
977 spin_unlock_irqrestore(&svc->srv_lock, flags);
978 OBD_FREE(thread, sizeof(*thread));
981 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
987 int ptlrpc_unregister_service(struct ptlrpc_service *service)
992 struct ptlrpc_srv_ni *srv_ni;
993 struct l_wait_info lwi;
994 struct list_head *tmp;
996 ptlrpc_stop_all_threads(service);
997 LASSERT(list_empty(&service->srv_threads));
999 spin_lock (&ptlrpc_all_services_lock);
1000 list_del_init (&service->srv_list);
1001 spin_unlock (&ptlrpc_all_services_lock);
1003 ptlrpc_lprocfs_unregister_service(service);
1005 /* All history will be culled when the next request buffer is
1007 service->srv_max_history_rqbds = 0;
1009 for (i = 0; i < ptlrpc_ninterfaces; i++) {
1010 srv_ni = &service->srv_interfaces[i];
1011 CDEBUG(D_NET, "%s: tearing down interface %s\n",
1012 service->srv_name, srv_ni->sni_ni->pni_name);
1014 /* Unlink all the request buffers. This forces a 'final'
1015 * event with its 'unlink' flag set for each posted rqbd */
1016 list_for_each(tmp, &srv_ni->sni_active_rqbds) {
1017 struct ptlrpc_request_buffer_desc *rqbd =
1018 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1021 rc = PtlMDUnlink(rqbd->rqbd_md_h);
1022 LASSERT (rc == PTL_OK || rc == PTL_MD_INVALID);
1025 /* Wait for the network to release any buffers it's
1026 * currently filling */
1028 spin_lock_irqsave(&service->srv_lock, flags);
1029 rc = srv_ni->sni_nrqbd_receiving;
1030 spin_unlock_irqrestore(&service->srv_lock, flags);
1035 /* Network access will complete in finite time but
1036 * the HUGE timeout lets us CWARN for visibility of
1038 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
1039 rc = l_wait_event(service->srv_waitq,
1040 srv_ni->sni_nrqbd_receiving == 0,
1042 if (rc == -ETIMEDOUT)
1043 CWARN("Waiting for request buffers on "
1044 "service %s on interface %s ",
1045 service->srv_name, srv_ni->sni_ni->pni_name);
1048 /* schedule all outstanding replies to terminate them */
1049 spin_lock_irqsave(&service->srv_lock, flags);
1050 while (!list_empty(&srv_ni->sni_active_replies)) {
1051 struct ptlrpc_reply_state *rs =
1052 list_entry(srv_ni->sni_active_replies.next,
1053 struct ptlrpc_reply_state,
1055 ptlrpc_schedule_difficult_reply(rs);
1057 spin_unlock_irqrestore(&service->srv_lock, flags);
1060 /* purge the request queue. NB No new replies (rqbds all unlinked)
1061 * and no service threads, so I'm the only thread noodling the
1062 * request queue now */
1063 while (!list_empty(&service->srv_request_queue)) {
1064 struct ptlrpc_request *req =
1065 list_entry(service->srv_request_queue.next,
1066 struct ptlrpc_request,
1069 list_del(&req->rq_list);
1070 service->srv_n_queued_reqs--;
1071 service->srv_n_active_reqs++;
1073 ptlrpc_server_free_request(req);
1075 LASSERT(service->srv_n_queued_reqs == 0);
1076 LASSERT(service->srv_n_active_reqs == 0);
1077 LASSERT(service->srv_n_history_rqbds == 0);
1079 for (i = 0; i < ptlrpc_ninterfaces; i++) {
1080 srv_ni = &service->srv_interfaces[i];
1081 LASSERT(list_empty(&srv_ni->sni_active_rqbds));
1084 /* Now free all the request buffers since nothing references them
1086 while (!list_empty(&service->srv_idle_rqbds)) {
1087 struct ptlrpc_request_buffer_desc *rqbd =
1088 list_entry(service->srv_idle_rqbds.next,
1089 struct ptlrpc_request_buffer_desc,
1092 ptlrpc_free_rqbd(rqbd);
1095 /* wait for all outstanding replies to complete (they were
1096 * scheduled having been flagged to abort above) */
1097 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1098 struct l_wait_info lwi = LWI_TIMEOUT(10 * HZ, NULL, NULL);
1100 rc = l_wait_event(service->srv_waitq,
1101 !list_empty(&service->srv_reply_queue), &lwi);
1102 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1105 ptlrpc_server_handle_reply(service);
1108 CWARN("Unexpectedly long timeout %p\n", service);
1112 offsetof(struct ptlrpc_service,
1113 srv_interfaces[ptlrpc_ninterfaces]));