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 <portals/types.h>
32 #include "ptlrpc_internal.h"
34 static LIST_HEAD (ptlrpc_all_services);
35 static spinlock_t ptlrpc_all_services_lock = SPIN_LOCK_UNLOCKED;
38 ptlrpc_free_server_req (struct ptlrpc_request *req)
40 /* The last request to be received into a request buffer uses space
41 * in the request buffer descriptor, otherwise requests are
42 * allocated dynamically in the incoming reply event handler */
43 if (req == &req->rq_rqbd->rqbd_req)
46 OBD_FREE(req, sizeof(*req));
50 ptlrpc_alloc_request_buffer (int size)
54 if (size > SVC_BUF_VMALLOC_THRESHOLD)
55 OBD_VMALLOC(ptr, size);
63 ptlrpc_free_request_buffer (char *ptr, int size)
65 if (size > SVC_BUF_VMALLOC_THRESHOLD)
71 struct ptlrpc_request_buffer_desc *
72 ptlrpc_alloc_rqbd (struct ptlrpc_srv_ni *srv_ni)
74 struct ptlrpc_service *svc = srv_ni->sni_service;
76 struct ptlrpc_request_buffer_desc *rqbd;
78 OBD_ALLOC(rqbd, sizeof (*rqbd));
82 rqbd->rqbd_srv_ni = srv_ni;
83 rqbd->rqbd_refcount = 0;
84 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
85 rqbd->rqbd_cbid.cbid_arg = rqbd;
86 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
88 if (rqbd->rqbd_buffer == NULL) {
89 OBD_FREE(rqbd, sizeof (*rqbd));
93 spin_lock_irqsave (&svc->srv_lock, flags);
94 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
96 spin_unlock_irqrestore (&svc->srv_lock, flags);
102 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
104 struct ptlrpc_srv_ni *sni = rqbd->rqbd_srv_ni;
105 struct ptlrpc_service *svc = sni->sni_service;
108 LASSERT (rqbd->rqbd_refcount == 0);
110 spin_lock_irqsave(&svc->srv_lock, flags);
111 list_del(&rqbd->rqbd_list);
113 spin_unlock_irqrestore(&svc->srv_lock, flags);
115 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
116 OBD_FREE (rqbd, sizeof (*rqbd));
120 ptlrpc_save_lock (struct ptlrpc_request *req,
121 struct lustre_handle *lock, int mode)
123 struct ptlrpc_reply_state *rs = req->rq_reply_state;
129 LASSERT (rs != NULL);
130 LASSERT (rs->rs_nlocks < RS_MAX_LOCKS);
132 idx = rs->rs_nlocks++;
133 rs->rs_locks[idx] = *lock;
134 rs->rs_modes[idx] = mode;
135 rs->rs_difficult = 1;
139 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
141 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
144 LASSERT (spin_is_locked (&svc->srv_lock));
146 LASSERT (rs->rs_difficult);
147 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
149 if (rs->rs_scheduled) /* being set up or already notified */
152 rs->rs_scheduled = 1;
153 list_del (&rs->rs_list);
154 list_add (&rs->rs_list, &svc->srv_reply_queue);
155 wake_up (&svc->srv_waitq);
159 ptlrpc_commit_replies (struct obd_device *obd)
161 struct list_head *tmp;
162 struct list_head *nxt;
165 /* Find any replies that have been committed and get their service
166 * to attend to complete them. */
168 /* CAVEAT EMPTOR: spinlock ordering!!! */
169 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
171 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
172 struct ptlrpc_reply_state *rs =
173 list_entry (tmp, struct ptlrpc_reply_state, rs_obd_list);
175 LASSERT (rs->rs_difficult);
177 if (rs->rs_transno <= obd->obd_last_committed) {
178 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
180 spin_lock (&svc->srv_lock);
181 list_del_init (&rs->rs_obd_list);
182 ptlrpc_schedule_difficult_reply (rs);
183 spin_unlock (&svc->srv_lock);
187 spin_unlock_irqrestore (&obd->obd_uncommitted_replies_lock, flags);
191 timeval_sub(struct timeval *large, struct timeval *small)
193 return (large->tv_sec - small->tv_sec) * 1000000 +
194 (large->tv_usec - small->tv_usec);
198 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
200 struct ptlrpc_srv_ni *srv_ni;
201 struct ptlrpc_request_buffer_desc *rqbd;
205 spin_lock_irqsave(&svc->srv_lock, flags);
206 if (list_empty (&svc->srv_idle_rqbds)) {
207 spin_unlock_irqrestore(&svc->srv_lock, flags);
211 rqbd = list_entry(svc->srv_idle_rqbds.next,
212 struct ptlrpc_request_buffer_desc,
214 list_del (&rqbd->rqbd_list);
216 /* assume we will post successfully */
217 srv_ni = rqbd->rqbd_srv_ni;
218 srv_ni->sni_nrqbd_receiving++;
219 list_add (&rqbd->rqbd_list, &srv_ni->sni_active_rqbds);
221 spin_unlock_irqrestore(&svc->srv_lock, flags);
223 rc = ptlrpc_register_rqbd(rqbd);
227 spin_lock_irqsave(&svc->srv_lock, flags);
229 srv_ni->sni_nrqbd_receiving--;
230 list_del(&rqbd->rqbd_list);
231 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
233 if (srv_ni->sni_nrqbd_receiving == 0) {
234 /* This service is off-air on this interface because all
235 * its request buffers are busy. Portals will have started
236 * dropping incoming requests until more buffers get
238 CERROR("All %s %s request buffers busy\n",
239 svc->srv_name, srv_ni->sni_ni->pni_name);
242 spin_unlock_irqrestore (&svc->srv_lock, flags);
247 struct ptlrpc_service *
248 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
249 int req_portal, int rep_portal,
250 svc_handler_t handler, char *name,
251 struct proc_dir_entry *proc_entry)
256 struct ptlrpc_service *service;
257 struct ptlrpc_srv_ni *srv_ni;
258 struct ptlrpc_request_buffer_desc *rqbd;
261 LASSERT (ptlrpc_ninterfaces > 0);
263 LASSERT (bufsize >= max_req_size);
265 ssize = offsetof (struct ptlrpc_service,
266 srv_interfaces[ptlrpc_ninterfaces]);
267 OBD_ALLOC(service, ssize);
271 service->srv_name = name;
272 spin_lock_init(&service->srv_lock);
273 INIT_LIST_HEAD(&service->srv_threads);
274 init_waitqueue_head(&service->srv_waitq);
276 service->srv_max_req_size = max_req_size;
277 service->srv_buf_size = bufsize;
278 service->srv_rep_portal = rep_portal;
279 service->srv_req_portal = req_portal;
280 service->srv_handler = handler;
282 INIT_LIST_HEAD(&service->srv_request_queue);
283 INIT_LIST_HEAD(&service->srv_idle_rqbds);
284 INIT_LIST_HEAD(&service->srv_reply_queue);
286 /* First initialise enough for early teardown */
287 for (i = 0; i < ptlrpc_ninterfaces; i++) {
288 srv_ni = &service->srv_interfaces[i];
290 srv_ni->sni_service = service;
291 srv_ni->sni_ni = &ptlrpc_interfaces[i];
292 INIT_LIST_HEAD(&srv_ni->sni_active_rqbds);
293 INIT_LIST_HEAD(&srv_ni->sni_active_replies);
296 spin_lock (&ptlrpc_all_services_lock);
297 list_add (&service->srv_list, &ptlrpc_all_services);
298 spin_unlock (&ptlrpc_all_services_lock);
300 /* Now allocate the request buffers, assuming all interfaces require
301 * the same number. */
302 for (i = 0; i < ptlrpc_ninterfaces; i++) {
303 srv_ni = &service->srv_interfaces[i];
304 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
305 srv_ni->sni_ni->pni_name);
307 for (j = 0; j < nbufs; j++) {
308 rqbd = ptlrpc_alloc_rqbd (srv_ni);
311 CERROR ("%s.%d: Can't allocate request %d "
312 "on %s\n", name, i, j,
313 srv_ni->sni_ni->pni_name);
317 /* We shouldn't be under memory pressure at
318 * startup, so fail if we can't post all our
319 * buffers at this time. */
320 if (ptlrpc_server_post_idle_rqbds(service) <= 0)
325 if (proc_entry != NULL)
326 ptlrpc_lprocfs_register_service(proc_entry, service);
328 CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
329 service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
333 ptlrpc_unregister_service(service);
338 ptlrpc_server_free_request(struct ptlrpc_service *svc, struct ptlrpc_request *req)
343 spin_lock_irqsave(&svc->srv_lock, flags);
344 svc->srv_n_active_reqs--;
345 refcount = --(req->rq_rqbd->rqbd_refcount);
347 /* request buffer is now idle */
348 list_del(&req->rq_rqbd->rqbd_list);
349 list_add_tail(&req->rq_rqbd->rqbd_list,
350 &svc->srv_idle_rqbds);
352 spin_unlock_irqrestore(&svc->srv_lock, flags);
354 ptlrpc_free_server_req(req);
358 ptlrpc_server_handle_request (struct ptlrpc_service *svc)
360 struct obd_export *export = NULL;
361 struct ptlrpc_request *request;
363 struct timeval work_start;
364 struct timeval work_end;
367 char str[PTL_NALFMT_SIZE];
370 spin_lock_irqsave (&svc->srv_lock, flags);
371 if (list_empty (&svc->srv_request_queue) ||
372 (svc->srv_n_difficult_replies != 0 &&
373 svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
374 /* If all the other threads are handling requests, I must
375 * remain free to handle any 'difficult' reply that might
377 spin_unlock_irqrestore (&svc->srv_lock, flags);
381 request = list_entry (svc->srv_request_queue.next,
382 struct ptlrpc_request, rq_list);
383 list_del_init (&request->rq_list);
384 svc->srv_n_queued_reqs--;
385 svc->srv_n_active_reqs++;
387 spin_unlock_irqrestore (&svc->srv_lock, flags);
389 do_gettimeofday(&work_start);
390 timediff = timeval_sub(&work_start, &request->rq_arrival_time);
391 if (svc->srv_stats != NULL) {
392 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
394 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
395 svc->srv_n_queued_reqs);
396 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
397 svc->srv_n_active_reqs);
401 /* Clear request swab mask; this is a new request */
402 request->rq_req_swab_mask = 0;
404 rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
406 CERROR ("error unpacking request: ptl %d from %s"
407 " xid "LPU64"\n", svc->srv_req_portal,
408 ptlrpc_peernid2str(&request->rq_peer, str),
414 if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
415 CERROR("wrong packet type received (type=%u) from %s\n",
416 request->rq_reqmsg->type,
417 ptlrpc_peernid2str(&request->rq_peer, str));
421 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
423 /* Discard requests queued for longer than my timeout. If the
424 * client's timeout is similar to mine, she'll be timing out this
425 * REQ anyway (bug 1502) */
426 if (timediff / 1000000 > (long)obd_timeout) {
427 CERROR("Dropping timed-out request from %s: %ld seconds old\n",
428 ptlrpc_peernid2str(&request->rq_peer, str),
433 request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
435 if (request->rq_export) {
436 if (request->rq_reqmsg->conn_cnt <
437 request->rq_export->exp_conn_cnt) {
438 DEBUG_REQ(D_ERROR, request,
439 "DROPPING req from old connection %d < %d",
440 request->rq_reqmsg->conn_cnt,
441 request->rq_export->exp_conn_cnt);
445 export = class_export_rpc_get(request->rq_export);
446 request->rq_export->exp_last_request_time =
447 LTIME_S(CURRENT_TIME);
450 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
451 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
452 (request->rq_export ?
453 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
454 (request->rq_export ?
455 atomic_read(&request->rq_export->exp_refcount) : -99),
456 request->rq_reqmsg->status, request->rq_xid,
457 request->rq_peer.peer_ni->pni_name,
458 ptlrpc_peernid2str(&request->rq_peer, str),
459 request->rq_reqmsg->opc);
461 rc = svc->srv_handler(request);
462 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
463 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
464 (request->rq_export ?
465 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
466 (request->rq_export ?
467 atomic_read(&request->rq_export->exp_refcount) : -99),
468 request->rq_reqmsg->status, request->rq_xid,
469 request->rq_peer.peer_ni->pni_name,
470 ptlrpc_peernid2str(&request->rq_peer, str),
471 request->rq_reqmsg->opc);
474 class_export_rpc_put(export);
477 if (request->rq_export != NULL)
478 class_export_put(request->rq_export);
481 do_gettimeofday(&work_end);
483 timediff = timeval_sub(&work_end, &work_start);
485 CDEBUG((timediff / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
486 "request "LPU64" opc %u from NID %s processed in %ldus "
487 "(%ldus total)\n", request->rq_xid, request->rq_reqmsg->opc,
488 ptlrpc_peernid2str(&request->rq_peer, str),
489 timediff, timeval_sub(&work_end, &request->rq_arrival_time));
491 if (svc->srv_stats != NULL) {
492 int opc = opcode_offset(request->rq_reqmsg->opc);
494 LASSERT(opc < LUSTRE_MAX_OPCODES);
495 lprocfs_counter_add(svc->srv_stats,
496 opc + PTLRPC_LAST_CNTR,
501 ptlrpc_server_free_request(svc, request);
507 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
509 struct ptlrpc_reply_state *rs;
511 struct obd_export *exp;
512 struct obd_device *obd;
515 char str[PTL_NALFMT_SIZE];
518 spin_lock_irqsave (&svc->srv_lock, flags);
519 if (list_empty (&svc->srv_reply_queue)) {
520 spin_unlock_irqrestore (&svc->srv_lock, flags);
524 rs = list_entry (svc->srv_reply_queue.next,
525 struct ptlrpc_reply_state, rs_list);
530 LASSERT (rs->rs_difficult);
531 LASSERT (rs->rs_scheduled);
533 list_del_init (&rs->rs_list);
535 /* Disengage from notifiers carefully (lock ordering!) */
536 spin_unlock(&svc->srv_lock);
538 spin_lock (&obd->obd_uncommitted_replies_lock);
539 /* Noop if removed already */
540 list_del_init (&rs->rs_obd_list);
541 spin_unlock (&obd->obd_uncommitted_replies_lock);
543 spin_lock (&exp->exp_lock);
544 /* Noop if removed already */
545 list_del_init (&rs->rs_exp_list);
546 spin_unlock (&exp->exp_lock);
548 spin_lock(&svc->srv_lock);
550 been_handled = rs->rs_handled;
553 nlocks = rs->rs_nlocks; /* atomic "steal", but */
554 rs->rs_nlocks = 0; /* locks still on rs_locks! */
556 if (nlocks == 0 && !been_handled) {
557 /* If we see this, we should already have seen the warning
558 * in mds_steal_ack_locks() */
559 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
562 rs->rs_xid, rs->rs_transno,
564 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
567 if ((!been_handled && rs->rs_on_net) ||
569 spin_unlock_irqrestore(&svc->srv_lock, flags);
571 if (!been_handled && rs->rs_on_net) {
572 PtlMDUnlink(rs->rs_md_h);
573 /* Ignore return code; we're racing with
578 ldlm_lock_decref(&rs->rs_locks[nlocks],
579 rs->rs_modes[nlocks]);
581 spin_lock_irqsave(&svc->srv_lock, flags);
584 rs->rs_scheduled = 0;
586 if (!rs->rs_on_net) {
588 svc->srv_n_difficult_replies--;
589 spin_unlock_irqrestore(&svc->srv_lock, flags);
591 class_export_put (exp);
592 rs->rs_export = NULL;
593 lustre_free_reply_state (rs);
594 atomic_dec (&svc->srv_outstanding_replies);
598 /* still on the net; callback will schedule */
599 spin_unlock_irqrestore (&svc->srv_lock, flags);
604 /* FIXME make use of timeout later */
606 liblustre_check_services (void *arg)
608 int did_something = 0;
610 struct list_head *tmp, *nxt;
613 /* I'm relying on being single threaded, not to have to lock
614 * ptlrpc_all_services etc */
615 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
616 struct ptlrpc_service *svc =
617 list_entry (tmp, struct ptlrpc_service, srv_list);
619 if (svc->srv_nthreads != 0) /* I've recursed */
622 /* service threads can block for bulk, so this limits us
623 * (arbitrarily) to recursing 1 stack frame per service.
624 * Note that the problem with recursion is that we have to
625 * unwind completely before our caller can resume. */
630 rc = ptlrpc_server_handle_reply(svc);
631 rc |= ptlrpc_server_handle_request(svc);
632 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
639 RETURN(did_something);
642 #else /* __KERNEL__ */
644 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
645 void ptlrpc_daemonize(void)
648 lustre_daemonize_helper();
654 ptlrpc_retry_rqbds(void *arg)
656 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
658 svc->srv_rqbd_timeout = 0;
662 static int ptlrpc_main(void *arg)
664 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
665 struct ptlrpc_service *svc = data->svc;
666 struct ptlrpc_thread *thread = data->thread;
673 SIGNAL_MASK_LOCK(current, flags);
674 sigfillset(¤t->blocked);
676 SIGNAL_MASK_UNLOCK(current, flags);
678 LASSERTF(strlen(data->name) < sizeof(current->comm),
679 "name %d > len %d\n",
680 (int)strlen(data->name), (int)sizeof(current->comm));
681 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
685 /* Record that the thread is running */
686 thread->t_flags = SVC_RUNNING;
687 wake_up(&thread->t_ctl_waitq);
689 spin_lock_irqsave(&svc->srv_lock, flags);
691 spin_unlock_irqrestore(&svc->srv_lock, flags);
693 /* XXX maintain a list of all managed devices: insert here */
695 while ((thread->t_flags & SVC_STOPPING) == 0 ||
696 svc->srv_n_difficult_replies != 0) {
697 /* Don't exit while there are replies to be handled */
698 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
699 ptlrpc_retry_rqbds, svc);
701 l_wait_event_exclusive (svc->srv_waitq,
702 ((thread->t_flags & SVC_STOPPING) != 0 &&
703 svc->srv_n_difficult_replies == 0) ||
704 (!list_empty(&svc->srv_idle_rqbds) &&
705 svc->srv_rqbd_timeout == 0) ||
706 !list_empty (&svc->srv_reply_queue) ||
707 (!list_empty (&svc->srv_request_queue) &&
708 (svc->srv_n_difficult_replies == 0 ||
709 svc->srv_n_active_reqs <
710 (svc->srv_nthreads - 1))),
713 if (!list_empty (&svc->srv_reply_queue))
714 ptlrpc_server_handle_reply (svc);
716 /* only handle requests if there are no difficult replies
717 * outstanding, or I'm not the last thread handling
719 if (!list_empty (&svc->srv_request_queue) &&
720 (svc->srv_n_difficult_replies == 0 ||
721 svc->srv_n_active_reqs < (svc->srv_nthreads - 1)))
722 ptlrpc_server_handle_request (svc);
724 if (!list_empty(&svc->srv_idle_rqbds) &&
725 ptlrpc_server_post_idle_rqbds(svc) < 0) {
726 /* I just failed to repost request buffers. Wait
727 * for a timeout (unless something else happens)
728 * before I try again */
729 svc->srv_rqbd_timeout = HZ/10;
733 spin_lock_irqsave(&svc->srv_lock, flags);
735 svc->srv_nthreads--; /* must know immediately */
736 thread->t_flags = SVC_STOPPED;
737 wake_up(&thread->t_ctl_waitq);
739 spin_unlock_irqrestore(&svc->srv_lock, flags);
741 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
745 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
746 struct ptlrpc_thread *thread)
748 struct l_wait_info lwi = { 0 };
751 spin_lock_irqsave(&svc->srv_lock, flags);
752 thread->t_flags = SVC_STOPPING;
753 spin_unlock_irqrestore(&svc->srv_lock, flags);
755 wake_up_all(&svc->srv_waitq);
756 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
759 spin_lock_irqsave(&svc->srv_lock, flags);
760 list_del(&thread->t_link);
761 spin_unlock_irqrestore(&svc->srv_lock, flags);
763 OBD_FREE(thread, sizeof(*thread));
766 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
769 struct ptlrpc_thread *thread;
771 spin_lock_irqsave(&svc->srv_lock, flags);
772 while (!list_empty(&svc->srv_threads)) {
773 thread = list_entry(svc->srv_threads.next,
774 struct ptlrpc_thread, t_link);
776 spin_unlock_irqrestore(&svc->srv_lock, flags);
777 ptlrpc_stop_thread(svc, thread);
778 spin_lock_irqsave(&svc->srv_lock, flags);
781 spin_unlock_irqrestore(&svc->srv_lock, flags);
784 /* @base_name should be 12 characters or less - 3 will be added on */
785 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
786 int num_threads, char *base_name)
791 for (i = 0; i < num_threads; i++) {
793 sprintf(name, "%s_%02d", base_name, i);
794 rc = ptlrpc_start_thread(dev, svc, name);
796 CERROR("cannot start %s thread #%d: rc %d\n", base_name,
798 ptlrpc_stop_all_threads(svc);
804 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
807 struct l_wait_info lwi = { 0 };
808 struct ptlrpc_svc_data d;
809 struct ptlrpc_thread *thread;
814 OBD_ALLOC(thread, sizeof(*thread));
817 init_waitqueue_head(&thread->t_ctl_waitq);
824 spin_lock_irqsave(&svc->srv_lock, flags);
825 list_add(&thread->t_link, &svc->srv_threads);
826 spin_unlock_irqrestore(&svc->srv_lock, flags);
828 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
829 * just drop the VM and FILES in ptlrpc_daemonize() right away.
831 rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
833 CERROR("cannot start thread: %d\n", rc);
834 OBD_FREE(thread, sizeof(*thread));
837 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
843 int ptlrpc_unregister_service(struct ptlrpc_service *service)
848 struct ptlrpc_srv_ni *srv_ni;
849 struct l_wait_info lwi;
850 struct list_head *tmp;
852 LASSERT(list_empty(&service->srv_threads));
854 spin_lock (&ptlrpc_all_services_lock);
855 list_del_init (&service->srv_list);
856 spin_unlock (&ptlrpc_all_services_lock);
858 ptlrpc_lprocfs_unregister_service(service);
860 for (i = 0; i < ptlrpc_ninterfaces; i++) {
861 srv_ni = &service->srv_interfaces[i];
862 CDEBUG(D_NET, "%s: tearing down interface %s\n",
863 service->srv_name, srv_ni->sni_ni->pni_name);
865 /* Unlink all the request buffers. This forces a 'final'
866 * event with its 'unlink' flag set for each posted rqbd */
867 list_for_each(tmp, &srv_ni->sni_active_rqbds) {
868 struct ptlrpc_request_buffer_desc *rqbd =
869 list_entry(tmp, struct ptlrpc_request_buffer_desc,
872 rc = PtlMDUnlink(rqbd->rqbd_md_h);
873 LASSERT (rc == PTL_OK || rc == PTL_MD_INVALID);
876 /* Wait for the network to release any buffers it's
877 * currently filling */
879 spin_lock_irqsave(&service->srv_lock, flags);
880 rc = srv_ni->sni_nrqbd_receiving;
881 spin_unlock_irqrestore(&service->srv_lock, flags);
886 /* Network access will complete in finite time but
887 * the HUGE timeout lets us CWARN for visibility of
889 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
890 rc = l_wait_event(service->srv_waitq,
891 srv_ni->sni_nrqbd_receiving == 0,
893 if (rc == -ETIMEDOUT)
894 CWARN("Waiting for request buffers on "
895 "service %s on interface %s ",
896 service->srv_name, srv_ni->sni_ni->pni_name);
899 /* schedule all outstanding replies to terminate them */
900 spin_lock_irqsave(&service->srv_lock, flags);
901 while (!list_empty(&srv_ni->sni_active_replies)) {
902 struct ptlrpc_reply_state *rs =
903 list_entry(srv_ni->sni_active_replies.next,
904 struct ptlrpc_reply_state,
906 ptlrpc_schedule_difficult_reply(rs);
908 spin_unlock_irqrestore(&service->srv_lock, flags);
911 /* purge the request queue. NB No new replies (rqbds all unlinked)
912 * and no service threads, so I'm the only thread noodling the
913 * request queue now */
914 while (!list_empty(&service->srv_request_queue)) {
915 struct ptlrpc_request *req =
916 list_entry(service->srv_request_queue.next,
917 struct ptlrpc_request,
920 list_del(&req->rq_list);
921 service->srv_n_queued_reqs--;
922 service->srv_n_active_reqs++;
924 ptlrpc_server_free_request(service, req);
926 LASSERT(service->srv_n_queued_reqs == 0);
927 LASSERT(service->srv_n_active_reqs == 0);
929 for (i = 0; i < ptlrpc_ninterfaces; i++) {
930 srv_ni = &service->srv_interfaces[i];
931 LASSERT(list_empty(&srv_ni->sni_active_rqbds));
934 /* Now free all the request buffers since nothing references them
936 while (!list_empty(&service->srv_idle_rqbds)) {
937 struct ptlrpc_request_buffer_desc *rqbd =
938 list_entry(service->srv_idle_rqbds.next,
939 struct ptlrpc_request_buffer_desc,
942 ptlrpc_free_rqbd(rqbd);
945 /* wait for all outstanding replies to complete (they were
946 * scheduled having been flagged to abort above) */
947 while (atomic_read(&service->srv_outstanding_replies) != 0) {
948 struct l_wait_info lwi = LWI_TIMEOUT(10 * HZ, NULL, NULL);
950 rc = l_wait_event(service->srv_waitq,
951 !list_empty(&service->srv_reply_queue), &lwi);
952 LASSERT(rc == 0 || rc == -ETIMEDOUT);
955 ptlrpc_server_handle_reply(service);
958 CWARN("Unexpectedly long timeout %p\n", service);
962 offsetof(struct ptlrpc_service,
963 srv_interfaces[ptlrpc_ninterfaces]));