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"
35 static LIST_HEAD (ptlrpc_all_services);
36 static spinlock_t ptlrpc_all_services_lock = SPIN_LOCK_UNLOCKED;
39 ptlrpc_free_server_req (struct ptlrpc_request *req)
41 /* The last request to be received into a request buffer uses space
42 * in the request buffer descriptor, otherwise requests are
43 * allocated dynamically in the incoming reply event handler */
44 if (req == &req->rq_rqbd->rqbd_req)
47 OBD_FREE(req, sizeof(*req));
51 ptlrpc_alloc_request_buffer (int size)
55 if (size > SVC_BUF_VMALLOC_THRESHOLD)
56 OBD_VMALLOC(ptr, size);
64 ptlrpc_free_request_buffer (char *ptr, int size)
66 if (size > SVC_BUF_VMALLOC_THRESHOLD)
72 struct ptlrpc_request_buffer_desc *
73 ptlrpc_alloc_rqbd (struct ptlrpc_srv_ni *srv_ni)
75 struct ptlrpc_service *svc = srv_ni->sni_service;
77 struct ptlrpc_request_buffer_desc *rqbd;
79 OBD_ALLOC(rqbd, sizeof (*rqbd));
83 rqbd->rqbd_srv_ni = srv_ni;
84 rqbd->rqbd_refcount = 0;
85 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
86 rqbd->rqbd_cbid.cbid_arg = rqbd;
87 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
89 if (rqbd->rqbd_buffer == NULL) {
90 OBD_FREE(rqbd, sizeof (*rqbd));
94 spin_lock_irqsave (&svc->srv_lock, flags);
95 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
97 spin_unlock_irqrestore (&svc->srv_lock, flags);
103 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
105 struct ptlrpc_srv_ni *sni = rqbd->rqbd_srv_ni;
106 struct ptlrpc_service *svc = sni->sni_service;
109 LASSERT (rqbd->rqbd_refcount == 0);
111 spin_lock_irqsave(&svc->srv_lock, flags);
112 list_del(&rqbd->rqbd_list);
114 spin_unlock_irqrestore(&svc->srv_lock, flags);
116 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
117 OBD_FREE (rqbd, sizeof (*rqbd));
121 ptlrpc_save_llog_lock (struct ptlrpc_request *req,
122 struct llog_create_locks *lcl)
124 struct ptlrpc_reply_state *rs = req->rq_reply_state;
125 LASSERT (rs != NULL);
126 LASSERT (rs->rs_llog_locks == NULL);
128 rs->rs_llog_locks = lcl;
132 ptlrpc_save_lock (struct ptlrpc_request *req,
133 struct lustre_handle *lock, int mode)
135 struct ptlrpc_reply_state *rs = req->rq_reply_state;
141 LASSERT (rs != NULL);
142 LASSERT (rs->rs_nlocks < RS_MAX_LOCKS);
144 idx = rs->rs_nlocks++;
145 rs->rs_locks[idx] = *lock;
146 rs->rs_modes[idx] = mode;
147 rs->rs_difficult = 1;
151 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
153 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
156 LASSERT (spin_is_locked (&svc->srv_lock));
158 LASSERT (rs->rs_difficult);
159 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
161 if (rs->rs_scheduled) /* being set up or already notified */
164 rs->rs_scheduled = 1;
165 list_del (&rs->rs_list);
166 list_add (&rs->rs_list, &svc->srv_reply_queue);
167 wake_up (&svc->srv_waitq);
171 ptlrpc_commit_replies (struct obd_device *obd)
173 struct list_head *tmp;
174 struct list_head *nxt;
177 /* Find any replies that have been committed and get their service
178 * to attend to complete them. */
180 /* CAVEAT EMPTOR: spinlock ordering!!! */
181 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
183 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
184 struct ptlrpc_reply_state *rs =
185 list_entry (tmp, struct ptlrpc_reply_state, rs_obd_list);
186 struct llog_create_locks *lcl = rs->rs_llog_locks;
188 rs->rs_llog_locks = NULL;
189 LASSERT (rs->rs_difficult);
191 if (rs->rs_transno <= obd->obd_last_committed) {
192 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
194 spin_lock (&svc->srv_lock);
195 list_del_init (&rs->rs_obd_list);
196 ptlrpc_schedule_difficult_reply (rs);
197 spin_unlock (&svc->srv_lock);
200 llog_create_lock_free(lcl);
204 spin_unlock_irqrestore (&obd->obd_uncommitted_replies_lock, flags);
208 timeval_sub(struct timeval *large, struct timeval *small)
210 return (large->tv_sec - small->tv_sec) * 1000000 +
211 (large->tv_usec - small->tv_usec);
215 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
217 struct ptlrpc_srv_ni *srv_ni;
218 struct ptlrpc_request_buffer_desc *rqbd;
222 spin_lock_irqsave(&svc->srv_lock, flags);
223 if (list_empty (&svc->srv_idle_rqbds)) {
224 spin_unlock_irqrestore(&svc->srv_lock, flags);
228 rqbd = list_entry(svc->srv_idle_rqbds.next,
229 struct ptlrpc_request_buffer_desc,
231 list_del (&rqbd->rqbd_list);
233 /* assume we will post successfully */
234 srv_ni = rqbd->rqbd_srv_ni;
235 srv_ni->sni_nrqbd_receiving++;
236 list_add (&rqbd->rqbd_list, &srv_ni->sni_active_rqbds);
238 spin_unlock_irqrestore(&svc->srv_lock, flags);
240 rc = ptlrpc_register_rqbd(rqbd);
244 spin_lock_irqsave(&svc->srv_lock, flags);
246 srv_ni->sni_nrqbd_receiving--;
247 list_del(&rqbd->rqbd_list);
248 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
250 if (srv_ni->sni_nrqbd_receiving == 0) {
251 /* This service is off-air on this interface because all
252 * its request buffers are busy. Portals will have started
253 * dropping incoming requests until more buffers get
255 CERROR("All %s %s request buffers busy\n",
256 svc->srv_name, srv_ni->sni_ni->pni_name);
259 spin_unlock_irqrestore (&svc->srv_lock, flags);
264 struct ptlrpc_service *
265 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
266 int req_portal, int rep_portal,
267 svc_handler_t handler, char *name,
268 struct proc_dir_entry *proc_entry)
273 struct ptlrpc_service *service;
274 struct ptlrpc_srv_ni *srv_ni;
275 struct ptlrpc_request_buffer_desc *rqbd;
278 LASSERT (ptlrpc_ninterfaces > 0);
280 LASSERT (bufsize >= max_req_size);
282 ssize = offsetof (struct ptlrpc_service,
283 srv_interfaces[ptlrpc_ninterfaces]);
284 OBD_ALLOC(service, ssize);
288 service->srv_name = name;
289 spin_lock_init(&service->srv_lock);
290 INIT_LIST_HEAD(&service->srv_threads);
291 init_waitqueue_head(&service->srv_waitq);
293 service->srv_max_req_size = max_req_size;
294 service->srv_buf_size = bufsize;
295 service->srv_rep_portal = rep_portal;
296 service->srv_req_portal = req_portal;
297 service->srv_handler = handler;
299 INIT_LIST_HEAD(&service->srv_request_queue);
300 INIT_LIST_HEAD(&service->srv_idle_rqbds);
301 INIT_LIST_HEAD(&service->srv_reply_queue);
303 /* First initialise enough for early teardown */
304 for (i = 0; i < ptlrpc_ninterfaces; i++) {
305 srv_ni = &service->srv_interfaces[i];
307 srv_ni->sni_service = service;
308 srv_ni->sni_ni = &ptlrpc_interfaces[i];
309 INIT_LIST_HEAD(&srv_ni->sni_active_rqbds);
310 INIT_LIST_HEAD(&srv_ni->sni_active_replies);
313 spin_lock (&ptlrpc_all_services_lock);
314 list_add (&service->srv_list, &ptlrpc_all_services);
315 spin_unlock (&ptlrpc_all_services_lock);
317 /* Now allocate the request buffers, assuming all interfaces require
318 * the same number. */
319 for (i = 0; i < ptlrpc_ninterfaces; i++) {
320 srv_ni = &service->srv_interfaces[i];
321 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
322 srv_ni->sni_ni->pni_name);
324 for (j = 0; j < nbufs; j++) {
325 rqbd = ptlrpc_alloc_rqbd (srv_ni);
328 CERROR ("%s.%d: Can't allocate request %d "
329 "on %s\n", name, i, j,
330 srv_ni->sni_ni->pni_name);
334 /* We shouldn't be under memory pressure at
335 * startup, so fail if we can't post all our
336 * buffers at this time. */
337 if (ptlrpc_server_post_idle_rqbds(service) <= 0)
342 if (proc_entry != NULL)
343 ptlrpc_lprocfs_register_service(proc_entry, service);
345 CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
346 service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
350 ptlrpc_unregister_service(service);
355 ptlrpc_server_free_request(struct ptlrpc_service *svc, struct ptlrpc_request *req)
360 spin_lock_irqsave(&svc->srv_lock, flags);
361 svc->srv_n_active_reqs--;
362 refcount = --(req->rq_rqbd->rqbd_refcount);
364 /* request buffer is now idle */
365 list_del(&req->rq_rqbd->rqbd_list);
366 list_add_tail(&req->rq_rqbd->rqbd_list,
367 &svc->srv_idle_rqbds);
369 spin_unlock_irqrestore(&svc->srv_lock, flags);
371 ptlrpc_free_server_req(req);
375 ptlrpc_server_handle_request (struct ptlrpc_service *svc)
377 struct obd_export *export = NULL;
378 struct ptlrpc_request *request;
380 struct timeval work_start;
381 struct timeval work_end;
384 char str[PTL_NALFMT_SIZE];
387 spin_lock_irqsave (&svc->srv_lock, flags);
388 if (list_empty (&svc->srv_request_queue) ||
389 (svc->srv_n_difficult_replies != 0 &&
390 svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
391 /* If all the other threads are handling requests, I must
392 * remain free to handle any 'difficult' reply that might
394 spin_unlock_irqrestore (&svc->srv_lock, flags);
398 request = list_entry (svc->srv_request_queue.next,
399 struct ptlrpc_request, rq_list);
400 list_del_init (&request->rq_list);
401 svc->srv_n_queued_reqs--;
402 svc->srv_n_active_reqs++;
404 spin_unlock_irqrestore (&svc->srv_lock, flags);
406 do_gettimeofday(&work_start);
407 timediff = timeval_sub(&work_start, &request->rq_arrival_time);
408 if (svc->srv_stats != NULL) {
409 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
411 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
412 svc->srv_n_queued_reqs);
413 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
414 svc->srv_n_active_reqs);
418 /* Clear request swab mask; this is a new request */
419 request->rq_req_swab_mask = 0;
421 rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
423 CERROR ("error unpacking request: ptl %d from %s"
424 " xid "LPU64"\n", svc->srv_req_portal,
425 ptlrpc_peernid2str(&request->rq_peer, str),
431 if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
432 CERROR("wrong packet type received (type=%u) from %s\n",
433 request->rq_reqmsg->type,
434 ptlrpc_peernid2str(&request->rq_peer, str));
438 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
440 /* Discard requests queued for longer than my timeout. If the
441 * client's timeout is similar to mine, she'll be timing out this
442 * REQ anyway (bug 1502) */
443 if (timediff / 1000000 > (long)obd_timeout) {
444 CERROR("Dropping timed-out request from %s: %ld seconds old\n",
445 ptlrpc_peernid2str(&request->rq_peer, str),
450 request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
452 if (request->rq_export) {
453 if (request->rq_reqmsg->conn_cnt <
454 request->rq_export->exp_conn_cnt) {
455 DEBUG_REQ(D_ERROR, request,
456 "DROPPING req from old connection %d < %d",
457 request->rq_reqmsg->conn_cnt,
458 request->rq_export->exp_conn_cnt);
462 export = class_export_rpc_get(request->rq_export);
463 request->rq_export->exp_last_request_time =
464 LTIME_S(CURRENT_TIME);
467 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
468 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
469 (request->rq_export ?
470 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
471 (request->rq_export ?
472 atomic_read(&request->rq_export->exp_refcount) : -99),
473 request->rq_reqmsg->status, request->rq_xid,
474 request->rq_peer.peer_ni->pni_name,
475 ptlrpc_peernid2str(&request->rq_peer, str),
476 request->rq_reqmsg->opc);
478 rc = svc->srv_handler(request);
479 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
480 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
481 (request->rq_export ?
482 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
483 (request->rq_export ?
484 atomic_read(&request->rq_export->exp_refcount) : -99),
485 request->rq_reqmsg->status, request->rq_xid,
486 request->rq_peer.peer_ni->pni_name,
487 ptlrpc_peernid2str(&request->rq_peer, str),
488 request->rq_reqmsg->opc);
491 class_export_rpc_put(export);
494 if (request->rq_export != NULL)
495 class_export_put(request->rq_export);
498 do_gettimeofday(&work_end);
500 timediff = timeval_sub(&work_end, &work_start);
502 CDEBUG((timediff / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
503 "request "LPU64" opc %u from NID %s processed in %ldus "
504 "(%ldus total)\n", request->rq_xid, request->rq_reqmsg->opc,
505 ptlrpc_peernid2str(&request->rq_peer, str),
506 timediff, timeval_sub(&work_end, &request->rq_arrival_time));
508 if (svc->srv_stats != NULL) {
509 int opc = opcode_offset(request->rq_reqmsg->opc);
511 LASSERT(opc < LUSTRE_MAX_OPCODES);
512 lprocfs_counter_add(svc->srv_stats,
513 opc + PTLRPC_LAST_CNTR,
518 ptlrpc_server_free_request(svc, request);
524 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
526 struct ptlrpc_reply_state *rs;
528 struct obd_export *exp;
529 struct obd_device *obd;
530 struct llog_create_locks *lcl;
533 char str[PTL_NALFMT_SIZE];
536 spin_lock_irqsave (&svc->srv_lock, flags);
537 if (list_empty (&svc->srv_reply_queue)) {
538 spin_unlock_irqrestore (&svc->srv_lock, flags);
542 rs = list_entry (svc->srv_reply_queue.next,
543 struct ptlrpc_reply_state, rs_list);
548 LASSERT (rs->rs_difficult);
549 LASSERT (rs->rs_scheduled);
551 list_del_init (&rs->rs_list);
553 /* Disengage from notifiers carefully (lock ordering!) */
554 spin_unlock(&svc->srv_lock);
556 spin_lock (&obd->obd_uncommitted_replies_lock);
557 /* Noop if removed already */
558 list_del_init (&rs->rs_obd_list);
559 spin_unlock (&obd->obd_uncommitted_replies_lock);
561 spin_lock (&exp->exp_lock);
562 /* Noop if removed already */
563 list_del_init (&rs->rs_exp_list);
564 spin_unlock (&exp->exp_lock);
566 spin_lock(&svc->srv_lock);
568 been_handled = rs->rs_handled;
571 nlocks = rs->rs_nlocks; /* atomic "steal", but */
572 rs->rs_nlocks = 0; /* locks still on rs_locks! */
574 lcl = rs->rs_llog_locks;
575 rs->rs_llog_locks = NULL;
577 if (nlocks == 0 && !been_handled) {
578 /* If we see this, we should already have seen the warning
579 * in mds_steal_ack_locks() */
580 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
583 rs->rs_xid, rs->rs_transno,
585 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
588 if ((!been_handled && rs->rs_on_net) ||
589 nlocks > 0 || lcl != NULL) {
590 spin_unlock_irqrestore(&svc->srv_lock, flags);
592 if (!been_handled && rs->rs_on_net) {
593 PtlMDUnlink(rs->rs_md_h);
594 /* Ignore return code; we're racing with
599 ldlm_lock_decref(&rs->rs_locks[nlocks],
600 rs->rs_modes[nlocks]);
603 llog_create_lock_free(lcl);
605 spin_lock_irqsave(&svc->srv_lock, flags);
608 rs->rs_scheduled = 0;
610 if (!rs->rs_on_net) {
612 svc->srv_n_difficult_replies--;
613 spin_unlock_irqrestore(&svc->srv_lock, flags);
615 class_export_put (exp);
616 rs->rs_export = NULL;
617 lustre_free_reply_state (rs);
618 atomic_dec (&svc->srv_outstanding_replies);
622 /* still on the net; callback will schedule */
623 spin_unlock_irqrestore (&svc->srv_lock, flags);
628 /* FIXME make use of timeout later */
630 liblustre_check_services (void *arg)
632 int did_something = 0;
634 struct list_head *tmp, *nxt;
637 /* I'm relying on being single threaded, not to have to lock
638 * ptlrpc_all_services etc */
639 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
640 struct ptlrpc_service *svc =
641 list_entry (tmp, struct ptlrpc_service, srv_list);
643 if (svc->srv_nthreads != 0) /* I've recursed */
646 /* service threads can block for bulk, so this limits us
647 * (arbitrarily) to recursing 1 stack frame per service.
648 * Note that the problem with recursion is that we have to
649 * unwind completely before our caller can resume. */
654 rc = ptlrpc_server_handle_reply(svc);
655 rc |= ptlrpc_server_handle_request(svc);
656 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
663 RETURN(did_something);
666 #else /* __KERNEL__ */
668 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
669 void ptlrpc_daemonize(void)
672 lustre_daemonize_helper();
678 ptlrpc_retry_rqbds(void *arg)
680 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
682 svc->srv_rqbd_timeout = 0;
686 static int ptlrpc_main(void *arg)
688 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
689 struct ptlrpc_service *svc = data->svc;
690 struct ptlrpc_thread *thread = data->thread;
697 SIGNAL_MASK_LOCK(current, flags);
698 sigfillset(¤t->blocked);
700 SIGNAL_MASK_UNLOCK(current, flags);
702 LASSERTF(strlen(data->name) < sizeof(current->comm),
703 "name %d > len %d\n",
704 (int)strlen(data->name), (int)sizeof(current->comm));
705 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
709 /* Record that the thread is running */
710 thread->t_flags = SVC_RUNNING;
711 wake_up(&thread->t_ctl_waitq);
713 spin_lock_irqsave(&svc->srv_lock, flags);
715 spin_unlock_irqrestore(&svc->srv_lock, flags);
717 /* XXX maintain a list of all managed devices: insert here */
719 while ((thread->t_flags & SVC_STOPPING) == 0 ||
720 svc->srv_n_difficult_replies != 0) {
721 /* Don't exit while there are replies to be handled */
722 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
723 ptlrpc_retry_rqbds, svc);
725 l_wait_event_exclusive (svc->srv_waitq,
726 ((thread->t_flags & SVC_STOPPING) != 0 &&
727 svc->srv_n_difficult_replies == 0) ||
728 (!list_empty(&svc->srv_idle_rqbds) &&
729 svc->srv_rqbd_timeout == 0) ||
730 !list_empty (&svc->srv_reply_queue) ||
731 (!list_empty (&svc->srv_request_queue) &&
732 (svc->srv_n_difficult_replies == 0 ||
733 svc->srv_n_active_reqs <
734 (svc->srv_nthreads - 1))),
737 if (!list_empty (&svc->srv_reply_queue))
738 ptlrpc_server_handle_reply (svc);
740 /* only handle requests if there are no difficult replies
741 * outstanding, or I'm not the last thread handling
743 if (!list_empty (&svc->srv_request_queue) &&
744 (svc->srv_n_difficult_replies == 0 ||
745 svc->srv_n_active_reqs < (svc->srv_nthreads - 1)))
746 ptlrpc_server_handle_request (svc);
748 if (!list_empty(&svc->srv_idle_rqbds) &&
749 ptlrpc_server_post_idle_rqbds(svc) < 0) {
750 /* I just failed to repost request buffers. Wait
751 * for a timeout (unless something else happens)
752 * before I try again */
753 svc->srv_rqbd_timeout = HZ/10;
757 spin_lock_irqsave(&svc->srv_lock, flags);
759 svc->srv_nthreads--; /* must know immediately */
760 thread->t_flags = SVC_STOPPED;
761 wake_up(&thread->t_ctl_waitq);
763 spin_unlock_irqrestore(&svc->srv_lock, flags);
765 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
769 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
770 struct ptlrpc_thread *thread)
772 struct l_wait_info lwi = { 0 };
775 spin_lock_irqsave(&svc->srv_lock, flags);
776 thread->t_flags = SVC_STOPPING;
777 spin_unlock_irqrestore(&svc->srv_lock, flags);
779 wake_up_all(&svc->srv_waitq);
780 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
783 spin_lock_irqsave(&svc->srv_lock, flags);
784 list_del(&thread->t_link);
785 spin_unlock_irqrestore(&svc->srv_lock, flags);
787 OBD_FREE(thread, sizeof(*thread));
790 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
793 struct ptlrpc_thread *thread;
795 spin_lock_irqsave(&svc->srv_lock, flags);
796 while (!list_empty(&svc->srv_threads)) {
797 thread = list_entry(svc->srv_threads.next,
798 struct ptlrpc_thread, t_link);
800 spin_unlock_irqrestore(&svc->srv_lock, flags);
801 ptlrpc_stop_thread(svc, thread);
802 spin_lock_irqsave(&svc->srv_lock, flags);
805 spin_unlock_irqrestore(&svc->srv_lock, flags);
808 /* @base_name should be 12 characters or less - 3 will be added on */
809 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
810 int num_threads, char *base_name)
815 for (i = 0; i < num_threads; i++) {
817 sprintf(name, "%s_%02d", base_name, i);
818 rc = ptlrpc_start_thread(dev, svc, name);
820 CERROR("cannot start %s thread #%d: rc %d\n", base_name,
822 ptlrpc_stop_all_threads(svc);
828 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
831 struct l_wait_info lwi = { 0 };
832 struct ptlrpc_svc_data d;
833 struct ptlrpc_thread *thread;
838 OBD_ALLOC(thread, sizeof(*thread));
841 init_waitqueue_head(&thread->t_ctl_waitq);
848 spin_lock_irqsave(&svc->srv_lock, flags);
849 list_add(&thread->t_link, &svc->srv_threads);
850 spin_unlock_irqrestore(&svc->srv_lock, flags);
852 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
853 * just drop the VM and FILES in ptlrpc_daemonize() right away.
855 rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
857 CERROR("cannot start thread: %d\n", rc);
858 OBD_FREE(thread, sizeof(*thread));
861 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
867 int ptlrpc_unregister_service(struct ptlrpc_service *service)
872 struct ptlrpc_srv_ni *srv_ni;
873 struct l_wait_info lwi;
874 struct list_head *tmp;
876 LASSERT(list_empty(&service->srv_threads));
878 spin_lock (&ptlrpc_all_services_lock);
879 list_del_init (&service->srv_list);
880 spin_unlock (&ptlrpc_all_services_lock);
882 ptlrpc_lprocfs_unregister_service(service);
884 for (i = 0; i < ptlrpc_ninterfaces; i++) {
885 srv_ni = &service->srv_interfaces[i];
886 CDEBUG(D_NET, "%s: tearing down interface %s\n",
887 service->srv_name, srv_ni->sni_ni->pni_name);
889 /* Unlink all the request buffers. This forces a 'final'
890 * event with its 'unlink' flag set for each posted rqbd */
891 list_for_each(tmp, &srv_ni->sni_active_rqbds) {
892 struct ptlrpc_request_buffer_desc *rqbd =
893 list_entry(tmp, struct ptlrpc_request_buffer_desc,
896 rc = PtlMDUnlink(rqbd->rqbd_md_h);
897 LASSERT (rc == PTL_OK || rc == PTL_MD_INVALID);
900 /* Wait for the network to release any buffers it's
901 * currently filling */
903 spin_lock_irqsave(&service->srv_lock, flags);
904 rc = srv_ni->sni_nrqbd_receiving;
905 spin_unlock_irqrestore(&service->srv_lock, flags);
910 /* Network access will complete in finite time but
911 * the HUGE timeout lets us CWARN for visibility of
913 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
914 rc = l_wait_event(service->srv_waitq,
915 srv_ni->sni_nrqbd_receiving == 0,
917 if (rc == -ETIMEDOUT)
918 CWARN("Waiting for request buffers on "
919 "service %s on interface %s ",
920 service->srv_name, srv_ni->sni_ni->pni_name);
923 /* schedule all outstanding replies to terminate them */
924 spin_lock_irqsave(&service->srv_lock, flags);
925 while (!list_empty(&srv_ni->sni_active_replies)) {
926 struct ptlrpc_reply_state *rs =
927 list_entry(srv_ni->sni_active_replies.next,
928 struct ptlrpc_reply_state,
930 ptlrpc_schedule_difficult_reply(rs);
932 spin_unlock_irqrestore(&service->srv_lock, flags);
935 /* purge the request queue. NB No new replies (rqbds all unlinked)
936 * and no service threads, so I'm the only thread noodling the
937 * request queue now */
938 while (!list_empty(&service->srv_request_queue)) {
939 struct ptlrpc_request *req =
940 list_entry(service->srv_request_queue.next,
941 struct ptlrpc_request,
944 list_del(&req->rq_list);
945 service->srv_n_queued_reqs--;
946 service->srv_n_active_reqs++;
948 ptlrpc_server_free_request(service, req);
950 LASSERT(service->srv_n_queued_reqs == 0);
951 LASSERT(service->srv_n_active_reqs == 0);
953 for (i = 0; i < ptlrpc_ninterfaces; i++) {
954 srv_ni = &service->srv_interfaces[i];
955 LASSERT(list_empty(&srv_ni->sni_active_rqbds));
958 /* Now free all the request buffers since nothing references them
960 while (!list_empty(&service->srv_idle_rqbds)) {
961 struct ptlrpc_request_buffer_desc *rqbd =
962 list_entry(service->srv_idle_rqbds.next,
963 struct ptlrpc_request_buffer_desc,
966 ptlrpc_free_rqbd(rqbd);
969 /* wait for all outstanding replies to complete (they were
970 * scheduled having been flagged to abort above) */
971 while (atomic_read(&service->srv_outstanding_replies) != 0) {
972 struct l_wait_info lwi = LWI_TIMEOUT(10 * HZ, NULL, NULL);
974 rc = l_wait_event(service->srv_waitq,
975 !list_empty(&service->srv_reply_queue), &lwi);
976 LASSERT(rc == 0 || rc == -ETIMEDOUT);
979 ptlrpc_server_handle_reply(service);
982 CWARN("Unexpectedly long timeout %p\n", service);
986 offsetof(struct ptlrpc_service,
987 srv_interfaces[ptlrpc_ninterfaces]));