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 <linux/kp30.h>
28 #include <linux/obd_support.h>
29 #include <linux/obd_class.h>
30 #include <linux/lustre_net.h>
31 #include <linux/lustre_log.h>
32 #include <portals/types.h>
33 #include "ptlrpc_internal.h"
36 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
39 static LIST_HEAD (ptlrpc_all_services);
40 static spinlock_t ptlrpc_all_services_lock = SPIN_LOCK_UNLOCKED;
43 ptlrpc_free_server_req (struct ptlrpc_request *req)
45 /* The last request to be received into a request buffer uses space
46 * in the request buffer descriptor, otherwise requests are
47 * allocated dynamically in the incoming reply event handler */
48 if (req == &req->rq_rqbd->rqbd_req)
51 OBD_FREE(req, sizeof(*req));
55 ptlrpc_alloc_request_buffer (int size)
59 if (size > SVC_BUF_VMALLOC_THRESHOLD)
60 OBD_VMALLOC(ptr, size);
68 ptlrpc_free_request_buffer (char *ptr, int size)
70 if (size > SVC_BUF_VMALLOC_THRESHOLD)
76 struct ptlrpc_request_buffer_desc *
77 ptlrpc_alloc_rqbd (struct ptlrpc_srv_ni *srv_ni)
79 struct ptlrpc_service *svc = srv_ni->sni_service;
81 struct ptlrpc_request_buffer_desc *rqbd;
83 OBD_ALLOC(rqbd, sizeof (*rqbd));
87 rqbd->rqbd_srv_ni = srv_ni;
88 rqbd->rqbd_refcount = 0;
89 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
90 rqbd->rqbd_cbid.cbid_arg = rqbd;
91 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
93 if (rqbd->rqbd_buffer == NULL) {
94 OBD_FREE(rqbd, sizeof (*rqbd));
98 spin_lock_irqsave (&svc->srv_lock, flags);
99 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
101 spin_unlock_irqrestore (&svc->srv_lock, flags);
107 ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
109 struct ptlrpc_srv_ni *sni = rqbd->rqbd_srv_ni;
110 struct ptlrpc_service *svc = sni->sni_service;
113 LASSERT (rqbd->rqbd_refcount == 0);
115 spin_lock_irqsave(&svc->srv_lock, flags);
116 list_del(&rqbd->rqbd_list);
118 spin_unlock_irqrestore(&svc->srv_lock, flags);
120 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
121 OBD_FREE (rqbd, sizeof (*rqbd));
125 ptlrpc_grow_req_bufs(struct ptlrpc_srv_ni *srv_ni)
127 struct ptlrpc_service *svc = srv_ni->sni_service;
128 struct ptlrpc_request_buffer_desc *rqbd;
131 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
132 rqbd = ptlrpc_alloc_rqbd(srv_ni);
135 CERROR ("%s/%s: Can't allocate request buffer\n",
136 svc->srv_name, srv_ni->sni_ni->pni_name);
140 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
148 ptlrpc_save_llog_lock(struct ptlrpc_request *req, struct llog_create_locks *lcl)
150 struct ptlrpc_reply_state *rs = req->rq_reply_state;
151 LASSERT (rs != NULL);
152 LASSERT (rs->rs_llog_locks == NULL);
154 rs->rs_llog_locks = lcl;
158 ptlrpc_require_repack(struct ptlrpc_request *req)
160 struct ptlrpc_reply_state *rs = req->rq_reply_state;
161 LASSERT (rs != NULL);
162 rs->rs_difficult = 1;
166 ptlrpc_save_lock (struct ptlrpc_request *req,
167 struct lustre_handle *lock, int mode)
169 struct ptlrpc_reply_state *rs = req->rq_reply_state;
175 LASSERT (rs != NULL);
176 LASSERT (rs->rs_nlocks < RS_MAX_LOCKS);
178 idx = rs->rs_nlocks++;
179 rs->rs_locks[idx] = *lock;
180 rs->rs_modes[idx] = mode;
181 rs->rs_difficult = 1;
185 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
187 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
190 LASSERT (spin_is_locked (&svc->srv_lock));
192 LASSERT (rs->rs_difficult);
193 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
195 if (rs->rs_scheduled) /* being set up or already notified */
198 rs->rs_scheduled = 1;
199 list_del (&rs->rs_list);
200 list_add (&rs->rs_list, &svc->srv_reply_queue);
201 wake_up (&svc->srv_waitq);
205 ptlrpc_commit_replies (struct obd_device *obd)
207 struct list_head *tmp;
208 struct list_head *nxt;
211 /* Find any replies that have been committed and get their service
212 * to attend to complete them. */
214 /* CAVEAT EMPTOR: spinlock ordering!!! */
215 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
217 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
218 struct ptlrpc_reply_state *rs =
219 list_entry (tmp, struct ptlrpc_reply_state, rs_obd_list);
220 struct llog_create_locks *lcl = rs->rs_llog_locks;
222 rs->rs_llog_locks = NULL;
223 LASSERT (rs->rs_difficult);
225 if (rs->rs_transno <= obd->obd_last_committed) {
226 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
228 spin_lock (&svc->srv_lock);
229 list_del_init (&rs->rs_obd_list);
230 ptlrpc_schedule_difficult_reply (rs);
231 spin_unlock (&svc->srv_lock);
234 llog_create_lock_free(lcl);
238 spin_unlock_irqrestore (&obd->obd_uncommitted_replies_lock, flags);
242 timeval_sub(struct timeval *large, struct timeval *small)
244 return (large->tv_sec - small->tv_sec) * 1000000 +
245 (large->tv_usec - small->tv_usec);
249 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
251 struct ptlrpc_srv_ni *srv_ni;
252 struct ptlrpc_request_buffer_desc *rqbd;
258 spin_lock_irqsave(&svc->srv_lock, flags);
259 if (list_empty (&svc->srv_idle_rqbds)) {
260 spin_unlock_irqrestore(&svc->srv_lock, flags);
263 rqbd = list_entry(svc->srv_idle_rqbds.next,
264 struct ptlrpc_request_buffer_desc,
266 list_del (&rqbd->rqbd_list);
268 /* assume we will post successfully */
269 srv_ni = rqbd->rqbd_srv_ni;
270 srv_ni->sni_nrqbd_receiving++;
271 list_add (&rqbd->rqbd_list, &srv_ni->sni_active_rqbds);
272 spin_unlock_irqrestore(&svc->srv_lock, flags);
274 rc = ptlrpc_register_rqbd(rqbd);
282 spin_lock_irqsave(&svc->srv_lock, flags);
284 srv_ni->sni_nrqbd_receiving--;
285 list_del(&rqbd->rqbd_list);
286 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
288 if (srv_ni->sni_nrqbd_receiving == 0) {
289 /* This service is off-air on this interface because all
290 * its request buffers are busy. Portals will have started
291 * dropping incoming requests until more buffers get
293 CERROR("All %s %s request buffers busy\n",
294 svc->srv_name, srv_ni->sni_ni->pni_name);
297 spin_unlock_irqrestore (&svc->srv_lock, flags);
302 struct ptlrpc_service *
303 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
304 int req_portal, int rep_portal,
305 svc_handler_t handler, char *name,
306 struct proc_dir_entry *proc_entry)
311 struct ptlrpc_service *service;
312 struct ptlrpc_srv_ni *srv_ni;
315 LASSERT (ptlrpc_ninterfaces > 0);
317 LASSERT (bufsize >= max_req_size);
319 ssize = offsetof (struct ptlrpc_service,
320 srv_interfaces[ptlrpc_ninterfaces]);
321 OBD_ALLOC(service, ssize);
325 service->srv_name = name;
326 spin_lock_init(&service->srv_lock);
327 INIT_LIST_HEAD(&service->srv_threads);
328 init_waitqueue_head(&service->srv_waitq);
330 service->srv_nbuf_per_group = nbufs;
331 service->srv_max_req_size = max_req_size;
332 service->srv_buf_size = bufsize;
333 service->srv_rep_portal = rep_portal;
334 service->srv_req_portal = req_portal;
335 service->srv_handler = handler;
337 INIT_LIST_HEAD(&service->srv_request_queue);
338 INIT_LIST_HEAD(&service->srv_idle_rqbds);
339 INIT_LIST_HEAD(&service->srv_reply_queue);
341 /* First initialise enough for early teardown */
342 for (i = 0; i < ptlrpc_ninterfaces; i++) {
343 srv_ni = &service->srv_interfaces[i];
345 srv_ni->sni_service = service;
346 srv_ni->sni_ni = &ptlrpc_interfaces[i];
347 INIT_LIST_HEAD(&srv_ni->sni_active_rqbds);
348 INIT_LIST_HEAD(&srv_ni->sni_active_replies);
351 spin_lock (&ptlrpc_all_services_lock);
352 list_add (&service->srv_list, &ptlrpc_all_services);
353 spin_unlock (&ptlrpc_all_services_lock);
355 /* Now allocate the request buffers, assuming all interfaces require
356 * the same number. */
357 for (i = 0; i < ptlrpc_ninterfaces; i++) {
358 srv_ni = &service->srv_interfaces[i];
359 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
360 srv_ni->sni_ni->pni_name);
362 rc = ptlrpc_grow_req_bufs(srv_ni);
363 /* We shouldn't be under memory pressure at startup, so
364 * fail if we can't post all our buffers at this time. */
369 if (proc_entry != NULL)
370 ptlrpc_lprocfs_register_service(proc_entry, service);
372 CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
373 service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
377 ptlrpc_unregister_service(service);
382 ptlrpc_server_free_request(struct ptlrpc_service *svc, struct ptlrpc_request *req)
387 spin_lock_irqsave(&svc->srv_lock, flags);
388 svc->srv_n_active_reqs--;
389 refcount = --(req->rq_rqbd->rqbd_refcount);
391 /* request buffer is now idle */
392 list_del(&req->rq_rqbd->rqbd_list);
393 list_add_tail(&req->rq_rqbd->rqbd_list,
394 &svc->srv_idle_rqbds);
396 spin_unlock_irqrestore(&svc->srv_lock, flags);
398 ptlrpc_free_server_req(req);
401 static char str[PTL_NALFMT_SIZE];
403 ptlrpc_server_handle_request (struct ptlrpc_service *svc)
405 struct obd_export *export = NULL;
406 struct ptlrpc_request *request;
408 struct timeval work_start;
409 struct timeval work_end;
414 spin_lock_irqsave (&svc->srv_lock, flags);
415 if (list_empty (&svc->srv_request_queue) ||
416 (svc->srv_n_difficult_replies != 0 &&
417 svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
418 /* If all the other threads are handling requests, I must
419 * remain free to handle any 'difficult' reply that might
421 spin_unlock_irqrestore (&svc->srv_lock, flags);
425 request = list_entry (svc->srv_request_queue.next,
426 struct ptlrpc_request, rq_list);
427 list_del_init (&request->rq_list);
428 svc->srv_n_queued_reqs--;
429 svc->srv_n_active_reqs++;
431 spin_unlock_irqrestore (&svc->srv_lock, flags);
433 do_gettimeofday(&work_start);
434 timediff = timeval_sub(&work_start, &request->rq_arrival_time);
435 if (svc->srv_stats != NULL) {
436 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
438 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
439 svc->srv_n_queued_reqs);
440 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
441 svc->srv_n_active_reqs);
445 /* Clear request swab mask; this is a new request */
446 request->rq_req_swab_mask = 0;
448 rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
450 CERROR ("error unpacking request: ptl %d from %s"
451 " xid "LPU64"\n", svc->srv_req_portal,
452 ptlrpc_peernid2str(&request->rq_peer, str),
458 if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
459 CERROR("wrong packet type received (type=%u) from %s\n",
460 request->rq_reqmsg->type,
461 ptlrpc_peernid2str(&request->rq_peer, str));
465 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
467 /* Discard requests queued for longer than my timeout. If the
468 * client's timeout is similar to mine, she'll be timing out this
469 * REQ anyway (bug 1502) */
470 if (timediff / 1000000 > (long)obd_timeout) {
471 CERROR("Dropping timed-out opc %d request from %s"
472 ": %ld seconds old\n", request->rq_reqmsg->opc,
473 ptlrpc_peernid2str(&request->rq_peer, str),
478 request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
480 if (request->rq_export) {
481 if (request->rq_reqmsg->conn_cnt <
482 request->rq_export->exp_conn_cnt) {
483 DEBUG_REQ(D_ERROR, request,
484 "DROPPING req from old connection %d < %d",
485 request->rq_reqmsg->conn_cnt,
486 request->rq_export->exp_conn_cnt);
490 export = class_export_rpc_get(request->rq_export);
491 request->rq_export->exp_last_request_time =
492 LTIME_S(CURRENT_TIME);
495 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
496 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
497 (request->rq_export ?
498 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
499 (request->rq_export ?
500 atomic_read(&request->rq_export->exp_refcount) : -99),
501 request->rq_reqmsg->status, request->rq_xid,
502 request->rq_peer.peer_ni->pni_name,
503 ptlrpc_peernid2str(&request->rq_peer, str),
504 request->rq_reqmsg->opc);
505 request->rq_svc = svc;
506 rc = svc->srv_handler(request);
507 request->rq_svc = NULL;
508 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
509 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
510 (request->rq_export ?
511 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
512 (request->rq_export ?
513 atomic_read(&request->rq_export->exp_refcount) : -99),
514 request->rq_reqmsg->status, request->rq_xid,
515 request->rq_peer.peer_ni->pni_name,
516 ptlrpc_peernid2str(&request->rq_peer, str),
517 request->rq_reqmsg->opc);
520 class_export_rpc_put(export);
523 if (request->rq_export != NULL)
524 class_export_put(request->rq_export);
527 do_gettimeofday(&work_end);
529 timediff = timeval_sub(&work_end, &work_start);
531 CDEBUG((timediff / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
532 "request "LPU64" opc %u from NID %s processed in %ldus "
533 "(%ldus total)\n", request->rq_xid, request->rq_reqmsg->opc,
534 ptlrpc_peernid2str(&request->rq_peer, str),
535 timediff, timeval_sub(&work_end, &request->rq_arrival_time));
537 if (svc->srv_stats != NULL) {
538 int opc = opcode_offset(request->rq_reqmsg->opc);
540 LASSERT(opc < LUSTRE_MAX_OPCODES);
541 lprocfs_counter_add(svc->srv_stats,
542 opc + PTLRPC_LAST_CNTR,
547 ptlrpc_server_free_request(svc, request);
553 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
555 struct ptlrpc_reply_state *rs;
557 struct obd_export *exp;
558 struct obd_device *obd;
559 struct llog_create_locks *lcl;
564 spin_lock_irqsave (&svc->srv_lock, flags);
565 if (list_empty (&svc->srv_reply_queue)) {
566 spin_unlock_irqrestore (&svc->srv_lock, flags);
570 rs = list_entry (svc->srv_reply_queue.next,
571 struct ptlrpc_reply_state, rs_list);
576 LASSERT (rs->rs_difficult);
577 LASSERT (rs->rs_scheduled);
579 list_del_init (&rs->rs_list);
581 /* Disengage from notifiers carefully (lock ordering!) */
582 spin_unlock(&svc->srv_lock);
584 spin_lock (&obd->obd_uncommitted_replies_lock);
585 /* Noop if removed already */
586 list_del_init (&rs->rs_obd_list);
587 spin_unlock (&obd->obd_uncommitted_replies_lock);
589 spin_lock (&exp->exp_lock);
590 /* Noop if removed already */
591 list_del_init (&rs->rs_exp_list);
592 spin_unlock (&exp->exp_lock);
594 spin_lock(&svc->srv_lock);
596 been_handled = rs->rs_handled;
599 nlocks = rs->rs_nlocks; /* atomic "steal", but */
600 rs->rs_nlocks = 0; /* locks still on rs_locks! */
602 lcl = rs->rs_llog_locks;
603 rs->rs_llog_locks = NULL;
605 if (nlocks == 0 && !been_handled) {
606 /* If we see this, we should already have seen the warning
607 * in mds_steal_ack_locks() */
609 char str[PTL_NALFMT_SIZE];
610 /* CMD may ask to save request with no DLM locks -bzzz */
611 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
614 rs->rs_xid, rs->rs_transno,
616 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
620 if ((!been_handled && rs->rs_on_net) ||
621 nlocks > 0 || lcl != NULL) {
622 spin_unlock_irqrestore(&svc->srv_lock, flags);
624 if (!been_handled && rs->rs_on_net) {
625 PtlMDUnlink(rs->rs_md_h);
626 /* Ignore return code; we're racing with
631 ldlm_lock_decref(&rs->rs_locks[nlocks],
632 rs->rs_modes[nlocks]);
635 llog_create_lock_free(lcl);
637 spin_lock_irqsave(&svc->srv_lock, flags);
640 rs->rs_scheduled = 0;
642 if (!rs->rs_on_net) {
644 svc->srv_n_difficult_replies--;
645 spin_unlock_irqrestore(&svc->srv_lock, flags);
647 class_export_put (exp);
648 rs->rs_export = NULL;
649 lustre_free_reply_state (rs);
650 atomic_dec (&svc->srv_outstanding_replies);
654 /* still on the net; callback will schedule */
655 spin_unlock_irqrestore (&svc->srv_lock, flags);
660 /* FIXME make use of timeout later */
662 liblustre_check_services (void *arg)
664 int did_something = 0;
666 struct list_head *tmp, *nxt;
669 /* I'm relying on being single threaded, not to have to lock
670 * ptlrpc_all_services etc */
671 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
672 struct ptlrpc_service *svc =
673 list_entry (tmp, struct ptlrpc_service, srv_list);
675 if (svc->srv_nthreads != 0) /* I've recursed */
678 /* service threads can block for bulk, so this limits us
679 * (arbitrarily) to recursing 1 stack frame per service.
680 * Note that the problem with recursion is that we have to
681 * unwind completely before our caller can resume. */
686 rc = ptlrpc_server_handle_reply(svc);
687 rc |= ptlrpc_server_handle_request(svc);
688 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
695 RETURN(did_something);
698 #else /* __KERNEL__ */
700 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
701 void ptlrpc_daemonize(void)
704 lustre_daemonize_helper();
710 ptlrpc_check_rqbd_pools(struct ptlrpc_service *svc)
712 struct ptlrpc_srv_ni *sni;
715 int low_water = svc->srv_nbuf_per_group/2;
717 for (i = 0; i < ptlrpc_ninterfaces; i++) {
718 sni = &svc->srv_interfaces[i];
720 avail += sni->sni_nrqbd_receiving;
721 /* NB I'm not locking; just looking. */
722 if (sni->sni_nrqbd_receiving <= low_water)
723 ptlrpc_grow_req_bufs(sni);
726 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR, avail);
730 ptlrpc_retry_rqbds(void *arg)
732 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
734 svc->srv_rqbd_timeout = 0;
738 static int ptlrpc_main(void *arg)
740 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
741 struct ptlrpc_service *svc = data->svc;
742 struct ptlrpc_thread *thread = data->thread;
749 SIGNAL_MASK_LOCK(current, flags);
750 sigfillset(¤t->blocked);
752 SIGNAL_MASK_UNLOCK(current, flags);
754 LASSERTF(strlen(data->name) < sizeof(current->comm),
755 "name %d > len %d\n",
756 (int)strlen(data->name), (int)sizeof(current->comm));
757 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
761 /* Record that the thread is running */
762 thread->t_flags = SVC_RUNNING;
763 wake_up(&thread->t_ctl_waitq);
765 spin_lock_irqsave(&svc->srv_lock, flags);
767 spin_unlock_irqrestore(&svc->srv_lock, flags);
769 /* XXX maintain a list of all managed devices: insert here */
771 while ((thread->t_flags & SVC_STOPPING) == 0 ||
772 svc->srv_n_difficult_replies != 0) {
773 /* Don't exit while there are replies to be handled */
774 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
775 ptlrpc_retry_rqbds, svc);
777 l_wait_event_exclusive (svc->srv_waitq,
778 ((thread->t_flags & SVC_STOPPING) != 0 &&
779 svc->srv_n_difficult_replies == 0) ||
780 (!list_empty(&svc->srv_idle_rqbds) &&
781 svc->srv_rqbd_timeout == 0) ||
782 !list_empty (&svc->srv_reply_queue) ||
783 (!list_empty (&svc->srv_request_queue) &&
784 (svc->srv_n_difficult_replies == 0 ||
785 svc->srv_n_active_reqs <
786 (svc->srv_nthreads - 1))),
789 ptlrpc_check_rqbd_pools(svc);
791 if (!list_empty (&svc->srv_reply_queue))
792 ptlrpc_server_handle_reply (svc);
794 /* only handle requests if there are no difficult replies
795 * outstanding, or I'm not the last thread handling
797 if (!list_empty (&svc->srv_request_queue) &&
798 (svc->srv_n_difficult_replies == 0 ||
799 svc->srv_n_active_reqs < (svc->srv_nthreads - 1)))
800 ptlrpc_server_handle_request (svc);
802 if (!list_empty(&svc->srv_idle_rqbds) &&
803 ptlrpc_server_post_idle_rqbds(svc) < 0) {
804 /* I just failed to repost request buffers. Wait
805 * for a timeout (unless something else happens)
806 * before I try again */
807 svc->srv_rqbd_timeout = HZ/10;
811 spin_lock_irqsave(&svc->srv_lock, flags);
813 svc->srv_nthreads--; /* must know immediately */
814 thread->t_flags = SVC_STOPPED;
815 wake_up(&thread->t_ctl_waitq);
817 spin_unlock_irqrestore(&svc->srv_lock, flags);
819 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
823 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
824 struct ptlrpc_thread *thread)
826 struct l_wait_info lwi = { 0 };
829 spin_lock_irqsave(&svc->srv_lock, flags);
830 thread->t_flags = SVC_STOPPING;
831 spin_unlock_irqrestore(&svc->srv_lock, flags);
833 wake_up_all(&svc->srv_waitq);
834 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
837 spin_lock_irqsave(&svc->srv_lock, flags);
838 list_del(&thread->t_link);
839 spin_unlock_irqrestore(&svc->srv_lock, flags);
841 OBD_FREE(thread, sizeof(*thread));
844 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
847 struct ptlrpc_thread *thread;
849 spin_lock_irqsave(&svc->srv_lock, flags);
850 while (!list_empty(&svc->srv_threads)) {
851 thread = list_entry(svc->srv_threads.next,
852 struct ptlrpc_thread, t_link);
854 spin_unlock_irqrestore(&svc->srv_lock, flags);
855 ptlrpc_stop_thread(svc, thread);
856 spin_lock_irqsave(&svc->srv_lock, flags);
859 spin_unlock_irqrestore(&svc->srv_lock, flags);
862 /* @base_name should be 12 characters or less - 3 will be added on */
863 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
864 int num_threads, char *base_name)
869 for (i = 0; i < num_threads; i++) {
871 sprintf(name, "%s_%02d", base_name, i);
872 rc = ptlrpc_start_thread(dev, svc, name);
874 CERROR("cannot start %s thread #%d: rc %d\n", base_name,
876 ptlrpc_stop_all_threads(svc);
882 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
885 struct l_wait_info lwi = { 0 };
886 struct ptlrpc_svc_data d;
887 struct ptlrpc_thread *thread;
892 OBD_ALLOC(thread, sizeof(*thread));
895 init_waitqueue_head(&thread->t_ctl_waitq);
902 spin_lock_irqsave(&svc->srv_lock, flags);
903 list_add(&thread->t_link, &svc->srv_threads);
904 spin_unlock_irqrestore(&svc->srv_lock, flags);
906 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
907 * just drop the VM and FILES in ptlrpc_daemonize() right away.
909 rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
911 CERROR("cannot start thread: %d\n", rc);
912 OBD_FREE(thread, sizeof(*thread));
915 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
921 int ptlrpc_unregister_service(struct ptlrpc_service *service)
926 struct ptlrpc_srv_ni *srv_ni;
927 struct l_wait_info lwi;
928 struct list_head *tmp;
930 LASSERT(list_empty(&service->srv_threads));
932 spin_lock (&ptlrpc_all_services_lock);
933 list_del_init (&service->srv_list);
934 spin_unlock (&ptlrpc_all_services_lock);
936 ptlrpc_lprocfs_unregister_service(service);
938 for (i = 0; i < ptlrpc_ninterfaces; i++) {
939 srv_ni = &service->srv_interfaces[i];
940 CDEBUG(D_NET, "%s: tearing down interface %s\n",
941 service->srv_name, srv_ni->sni_ni->pni_name);
943 /* Unlink all the request buffers. This forces a 'final'
944 * event with its 'unlink' flag set for each posted rqbd */
945 list_for_each(tmp, &srv_ni->sni_active_rqbds) {
946 struct ptlrpc_request_buffer_desc *rqbd =
947 list_entry(tmp, struct ptlrpc_request_buffer_desc,
950 rc = PtlMDUnlink(rqbd->rqbd_md_h);
951 LASSERT (rc == PTL_OK || rc == PTL_MD_INVALID);
954 /* Wait for the network to release any buffers it's
955 * currently filling */
957 spin_lock_irqsave(&service->srv_lock, flags);
958 rc = srv_ni->sni_nrqbd_receiving;
959 spin_unlock_irqrestore(&service->srv_lock, flags);
964 /* Network access will complete in finite time but
965 * the HUGE timeout lets us CWARN for visibility of
967 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
968 rc = l_wait_event(service->srv_waitq,
969 srv_ni->sni_nrqbd_receiving == 0,
971 if (rc == -ETIMEDOUT)
972 CWARN("Waiting for request buffers on "
973 "service %s on interface %s ",
974 service->srv_name, srv_ni->sni_ni->pni_name);
977 /* schedule all outstanding replies to terminate them */
978 spin_lock_irqsave(&service->srv_lock, flags);
979 while (!list_empty(&srv_ni->sni_active_replies)) {
980 struct ptlrpc_reply_state *rs =
981 list_entry(srv_ni->sni_active_replies.next,
982 struct ptlrpc_reply_state,
984 ptlrpc_schedule_difficult_reply(rs);
986 spin_unlock_irqrestore(&service->srv_lock, flags);
989 /* purge the request queue. NB No new replies (rqbds all unlinked)
990 * and no service threads, so I'm the only thread noodling the
991 * request queue now */
992 while (!list_empty(&service->srv_request_queue)) {
993 struct ptlrpc_request *req =
994 list_entry(service->srv_request_queue.next,
995 struct ptlrpc_request,
998 list_del(&req->rq_list);
999 service->srv_n_queued_reqs--;
1000 service->srv_n_active_reqs++;
1002 ptlrpc_server_free_request(service, req);
1004 LASSERT(service->srv_n_queued_reqs == 0);
1005 LASSERT(service->srv_n_active_reqs == 0);
1007 for (i = 0; i < ptlrpc_ninterfaces; i++) {
1008 srv_ni = &service->srv_interfaces[i];
1009 LASSERT(list_empty(&srv_ni->sni_active_rqbds));
1012 /* Now free all the request buffers since nothing references them
1014 while (!list_empty(&service->srv_idle_rqbds)) {
1015 struct ptlrpc_request_buffer_desc *rqbd =
1016 list_entry(service->srv_idle_rqbds.next,
1017 struct ptlrpc_request_buffer_desc,
1020 ptlrpc_free_rqbd(rqbd);
1023 /* wait for all outstanding replies to complete (they were
1024 * scheduled having been flagged to abort above) */
1025 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1026 struct l_wait_info lwi = LWI_TIMEOUT(10 * HZ, NULL, NULL);
1028 rc = l_wait_event(service->srv_waitq,
1029 !list_empty(&service->srv_reply_queue), &lwi);
1030 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1033 ptlrpc_server_handle_reply(service);
1036 CWARN("Unexpectedly long timeout %p\n", service);
1040 offsetof(struct ptlrpc_service,
1041 srv_interfaces[ptlrpc_ninterfaces]));