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, struct llog_create_locks *lcl)
123 struct ptlrpc_reply_state *rs = req->rq_reply_state;
124 LASSERT (rs != NULL);
125 LASSERT (rs->rs_llog_locks == NULL);
127 rs->rs_llog_locks = lcl;
131 ptlrpc_require_repack(struct ptlrpc_request *req)
133 struct ptlrpc_reply_state *rs = req->rq_reply_state;
134 LASSERT (rs != NULL);
135 rs->rs_difficult = 1;
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;
148 LASSERT (rs != NULL);
149 LASSERT (rs->rs_nlocks < RS_MAX_LOCKS);
151 idx = rs->rs_nlocks++;
152 rs->rs_locks[idx] = *lock;
153 rs->rs_modes[idx] = mode;
154 rs->rs_difficult = 1;
158 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
160 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
163 LASSERT (spin_is_locked (&svc->srv_lock));
165 LASSERT (rs->rs_difficult);
166 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
168 if (rs->rs_scheduled) /* being set up or already notified */
171 rs->rs_scheduled = 1;
172 list_del (&rs->rs_list);
173 list_add (&rs->rs_list, &svc->srv_reply_queue);
174 wake_up (&svc->srv_waitq);
178 ptlrpc_commit_replies (struct obd_device *obd)
180 struct list_head *tmp;
181 struct list_head *nxt;
184 /* Find any replies that have been committed and get their service
185 * to attend to complete them. */
187 /* CAVEAT EMPTOR: spinlock ordering!!! */
188 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
190 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
191 struct ptlrpc_reply_state *rs =
192 list_entry (tmp, struct ptlrpc_reply_state, rs_obd_list);
193 struct llog_create_locks *lcl = rs->rs_llog_locks;
195 rs->rs_llog_locks = NULL;
196 LASSERT (rs->rs_difficult);
198 if (rs->rs_transno <= obd->obd_last_committed) {
199 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
201 spin_lock (&svc->srv_lock);
202 list_del_init (&rs->rs_obd_list);
203 ptlrpc_schedule_difficult_reply (rs);
204 spin_unlock (&svc->srv_lock);
207 llog_create_lock_free(lcl);
211 spin_unlock_irqrestore (&obd->obd_uncommitted_replies_lock, flags);
215 timeval_sub(struct timeval *large, struct timeval *small)
217 return (large->tv_sec - small->tv_sec) * 1000000 +
218 (large->tv_usec - small->tv_usec);
222 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
224 struct ptlrpc_srv_ni *srv_ni;
225 struct ptlrpc_request_buffer_desc *rqbd;
229 spin_lock_irqsave(&svc->srv_lock, flags);
230 if (list_empty (&svc->srv_idle_rqbds)) {
231 spin_unlock_irqrestore(&svc->srv_lock, flags);
235 rqbd = list_entry(svc->srv_idle_rqbds.next,
236 struct ptlrpc_request_buffer_desc,
238 list_del (&rqbd->rqbd_list);
240 /* assume we will post successfully */
241 srv_ni = rqbd->rqbd_srv_ni;
242 srv_ni->sni_nrqbd_receiving++;
243 list_add (&rqbd->rqbd_list, &srv_ni->sni_active_rqbds);
245 spin_unlock_irqrestore(&svc->srv_lock, flags);
247 rc = ptlrpc_register_rqbd(rqbd);
251 spin_lock_irqsave(&svc->srv_lock, flags);
253 srv_ni->sni_nrqbd_receiving--;
254 list_del(&rqbd->rqbd_list);
255 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
257 if (srv_ni->sni_nrqbd_receiving == 0) {
258 /* This service is off-air on this interface because all
259 * its request buffers are busy. Portals will have started
260 * dropping incoming requests until more buffers get
262 CERROR("All %s %s request buffers busy\n",
263 svc->srv_name, srv_ni->sni_ni->pni_name);
266 spin_unlock_irqrestore (&svc->srv_lock, flags);
271 struct ptlrpc_service *
272 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
273 int req_portal, int rep_portal,
274 svc_handler_t handler, char *name,
275 struct proc_dir_entry *proc_entry)
280 struct ptlrpc_service *service;
281 struct ptlrpc_srv_ni *srv_ni;
282 struct ptlrpc_request_buffer_desc *rqbd;
285 LASSERT (ptlrpc_ninterfaces > 0);
287 LASSERT (bufsize >= max_req_size);
289 ssize = offsetof (struct ptlrpc_service,
290 srv_interfaces[ptlrpc_ninterfaces]);
291 OBD_ALLOC(service, ssize);
295 service->srv_name = name;
296 spin_lock_init(&service->srv_lock);
297 INIT_LIST_HEAD(&service->srv_threads);
298 init_waitqueue_head(&service->srv_waitq);
300 service->srv_max_req_size = max_req_size;
301 service->srv_buf_size = bufsize;
302 service->srv_rep_portal = rep_portal;
303 service->srv_req_portal = req_portal;
304 service->srv_handler = handler;
306 INIT_LIST_HEAD(&service->srv_request_queue);
307 INIT_LIST_HEAD(&service->srv_idle_rqbds);
308 INIT_LIST_HEAD(&service->srv_reply_queue);
310 /* First initialise enough for early teardown */
311 for (i = 0; i < ptlrpc_ninterfaces; i++) {
312 srv_ni = &service->srv_interfaces[i];
314 srv_ni->sni_service = service;
315 srv_ni->sni_ni = &ptlrpc_interfaces[i];
316 INIT_LIST_HEAD(&srv_ni->sni_active_rqbds);
317 INIT_LIST_HEAD(&srv_ni->sni_active_replies);
320 spin_lock (&ptlrpc_all_services_lock);
321 list_add (&service->srv_list, &ptlrpc_all_services);
322 spin_unlock (&ptlrpc_all_services_lock);
324 /* Now allocate the request buffers, assuming all interfaces require
325 * the same number. */
326 for (i = 0; i < ptlrpc_ninterfaces; i++) {
327 srv_ni = &service->srv_interfaces[i];
328 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
329 srv_ni->sni_ni->pni_name);
331 for (j = 0; j < nbufs; j++) {
332 rqbd = ptlrpc_alloc_rqbd (srv_ni);
335 CERROR ("%s.%d: Can't allocate request %d "
336 "on %s\n", name, i, j,
337 srv_ni->sni_ni->pni_name);
341 /* We shouldn't be under memory pressure at
342 * startup, so fail if we can't post all our
343 * buffers at this time. */
344 if (ptlrpc_server_post_idle_rqbds(service) <= 0)
349 if (proc_entry != NULL)
350 ptlrpc_lprocfs_register_service(proc_entry, service);
352 CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
353 service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
357 ptlrpc_unregister_service(service);
362 ptlrpc_server_free_request(struct ptlrpc_service *svc, struct ptlrpc_request *req)
367 spin_lock_irqsave(&svc->srv_lock, flags);
368 svc->srv_n_active_reqs--;
369 refcount = --(req->rq_rqbd->rqbd_refcount);
371 /* request buffer is now idle */
372 list_del(&req->rq_rqbd->rqbd_list);
373 list_add_tail(&req->rq_rqbd->rqbd_list,
374 &svc->srv_idle_rqbds);
376 spin_unlock_irqrestore(&svc->srv_lock, flags);
378 ptlrpc_free_server_req(req);
382 ptlrpc_server_handle_request (struct ptlrpc_service *svc)
384 struct obd_export *export = NULL;
385 struct ptlrpc_request *request;
387 struct timeval work_start;
388 struct timeval work_end;
391 char str[PTL_NALFMT_SIZE];
394 spin_lock_irqsave (&svc->srv_lock, flags);
395 if (list_empty (&svc->srv_request_queue) ||
396 (svc->srv_n_difficult_replies != 0 &&
397 svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
398 /* If all the other threads are handling requests, I must
399 * remain free to handle any 'difficult' reply that might
401 spin_unlock_irqrestore (&svc->srv_lock, flags);
405 request = list_entry (svc->srv_request_queue.next,
406 struct ptlrpc_request, rq_list);
407 list_del_init (&request->rq_list);
408 svc->srv_n_queued_reqs--;
409 svc->srv_n_active_reqs++;
411 spin_unlock_irqrestore (&svc->srv_lock, flags);
413 do_gettimeofday(&work_start);
414 timediff = timeval_sub(&work_start, &request->rq_arrival_time);
415 if (svc->srv_stats != NULL) {
416 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
418 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
419 svc->srv_n_queued_reqs);
420 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
421 svc->srv_n_active_reqs);
425 /* Clear request swab mask; this is a new request */
426 request->rq_req_swab_mask = 0;
428 rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
430 CERROR ("error unpacking request: ptl %d from %s"
431 " xid "LPU64"\n", svc->srv_req_portal,
432 ptlrpc_peernid2str(&request->rq_peer, str),
438 if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
439 CERROR("wrong packet type received (type=%u) from %s\n",
440 request->rq_reqmsg->type,
441 ptlrpc_peernid2str(&request->rq_peer, str));
445 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
447 /* Discard requests queued for longer than my timeout. If the
448 * client's timeout is similar to mine, she'll be timing out this
449 * REQ anyway (bug 1502) */
450 if (timediff / 1000000 > (long)obd_timeout) {
451 CERROR("Dropping timed-out request from %s: %ld seconds old\n",
452 ptlrpc_peernid2str(&request->rq_peer, str),
457 request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
459 if (request->rq_export) {
460 if (request->rq_reqmsg->conn_cnt <
461 request->rq_export->exp_conn_cnt) {
462 DEBUG_REQ(D_ERROR, request,
463 "DROPPING req from old connection %d < %d",
464 request->rq_reqmsg->conn_cnt,
465 request->rq_export->exp_conn_cnt);
469 export = class_export_rpc_get(request->rq_export);
470 request->rq_export->exp_last_request_time =
471 LTIME_S(CURRENT_TIME);
474 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
475 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
476 (request->rq_export ?
477 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
478 (request->rq_export ?
479 atomic_read(&request->rq_export->exp_refcount) : -99),
480 request->rq_reqmsg->status, request->rq_xid,
481 request->rq_peer.peer_ni->pni_name,
482 ptlrpc_peernid2str(&request->rq_peer, str),
483 request->rq_reqmsg->opc);
485 rc = svc->srv_handler(request);
486 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
487 "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
488 (request->rq_export ?
489 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
490 (request->rq_export ?
491 atomic_read(&request->rq_export->exp_refcount) : -99),
492 request->rq_reqmsg->status, request->rq_xid,
493 request->rq_peer.peer_ni->pni_name,
494 ptlrpc_peernid2str(&request->rq_peer, str),
495 request->rq_reqmsg->opc);
498 class_export_rpc_put(export);
501 if (request->rq_export != NULL)
502 class_export_put(request->rq_export);
505 do_gettimeofday(&work_end);
507 timediff = timeval_sub(&work_end, &work_start);
509 CDEBUG((timediff / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
510 "request "LPU64" opc %u from NID %s processed in %ldus "
511 "(%ldus total)\n", request->rq_xid, request->rq_reqmsg->opc,
512 ptlrpc_peernid2str(&request->rq_peer, str),
513 timediff, timeval_sub(&work_end, &request->rq_arrival_time));
515 if (svc->srv_stats != NULL) {
516 int opc = opcode_offset(request->rq_reqmsg->opc);
518 LASSERT(opc < LUSTRE_MAX_OPCODES);
519 lprocfs_counter_add(svc->srv_stats,
520 opc + PTLRPC_LAST_CNTR,
525 ptlrpc_server_free_request(svc, request);
531 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
533 struct ptlrpc_reply_state *rs;
535 struct obd_export *exp;
536 struct obd_device *obd;
537 struct llog_create_locks *lcl;
542 spin_lock_irqsave (&svc->srv_lock, flags);
543 if (list_empty (&svc->srv_reply_queue)) {
544 spin_unlock_irqrestore (&svc->srv_lock, flags);
548 rs = list_entry (svc->srv_reply_queue.next,
549 struct ptlrpc_reply_state, rs_list);
554 LASSERT (rs->rs_difficult);
555 LASSERT (rs->rs_scheduled);
557 list_del_init (&rs->rs_list);
559 /* Disengage from notifiers carefully (lock ordering!) */
560 spin_unlock(&svc->srv_lock);
562 spin_lock (&obd->obd_uncommitted_replies_lock);
563 /* Noop if removed already */
564 list_del_init (&rs->rs_obd_list);
565 spin_unlock (&obd->obd_uncommitted_replies_lock);
567 spin_lock (&exp->exp_lock);
568 /* Noop if removed already */
569 list_del_init (&rs->rs_exp_list);
570 spin_unlock (&exp->exp_lock);
572 spin_lock(&svc->srv_lock);
574 been_handled = rs->rs_handled;
577 nlocks = rs->rs_nlocks; /* atomic "steal", but */
578 rs->rs_nlocks = 0; /* locks still on rs_locks! */
580 lcl = rs->rs_llog_locks;
581 rs->rs_llog_locks = NULL;
583 if (nlocks == 0 && !been_handled) {
584 /* If we see this, we should already have seen the warning
585 * in mds_steal_ack_locks() */
587 char str[PTL_NALFMT_SIZE];
588 /* CMD may ask to save request with no DLM locks -bzzz */
589 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
592 rs->rs_xid, rs->rs_transno,
594 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
598 if ((!been_handled && rs->rs_on_net) ||
599 nlocks > 0 || lcl != NULL) {
600 spin_unlock_irqrestore(&svc->srv_lock, flags);
602 if (!been_handled && rs->rs_on_net) {
603 PtlMDUnlink(rs->rs_md_h);
604 /* Ignore return code; we're racing with
609 ldlm_lock_decref(&rs->rs_locks[nlocks],
610 rs->rs_modes[nlocks]);
613 llog_create_lock_free(lcl);
615 spin_lock_irqsave(&svc->srv_lock, flags);
618 rs->rs_scheduled = 0;
620 if (!rs->rs_on_net) {
622 svc->srv_n_difficult_replies--;
623 spin_unlock_irqrestore(&svc->srv_lock, flags);
625 class_export_put (exp);
626 rs->rs_export = NULL;
627 lustre_free_reply_state (rs);
628 atomic_dec (&svc->srv_outstanding_replies);
632 /* still on the net; callback will schedule */
633 spin_unlock_irqrestore (&svc->srv_lock, flags);
638 /* FIXME make use of timeout later */
640 liblustre_check_services (void *arg)
642 int did_something = 0;
644 struct list_head *tmp, *nxt;
647 /* I'm relying on being single threaded, not to have to lock
648 * ptlrpc_all_services etc */
649 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
650 struct ptlrpc_service *svc =
651 list_entry (tmp, struct ptlrpc_service, srv_list);
653 if (svc->srv_nthreads != 0) /* I've recursed */
656 /* service threads can block for bulk, so this limits us
657 * (arbitrarily) to recursing 1 stack frame per service.
658 * Note that the problem with recursion is that we have to
659 * unwind completely before our caller can resume. */
664 rc = ptlrpc_server_handle_reply(svc);
665 rc |= ptlrpc_server_handle_request(svc);
666 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
673 RETURN(did_something);
676 #else /* __KERNEL__ */
678 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
679 void ptlrpc_daemonize(void)
682 lustre_daemonize_helper();
688 ptlrpc_retry_rqbds(void *arg)
690 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
692 svc->srv_rqbd_timeout = 0;
696 static int ptlrpc_main(void *arg)
698 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
699 struct ptlrpc_service *svc = data->svc;
700 struct ptlrpc_thread *thread = data->thread;
707 SIGNAL_MASK_LOCK(current, flags);
708 sigfillset(¤t->blocked);
710 SIGNAL_MASK_UNLOCK(current, flags);
712 LASSERTF(strlen(data->name) < sizeof(current->comm),
713 "name %d > len %d\n",
714 (int)strlen(data->name), (int)sizeof(current->comm));
715 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
719 /* Record that the thread is running */
720 thread->t_flags = SVC_RUNNING;
721 wake_up(&thread->t_ctl_waitq);
723 spin_lock_irqsave(&svc->srv_lock, flags);
725 spin_unlock_irqrestore(&svc->srv_lock, flags);
727 /* XXX maintain a list of all managed devices: insert here */
729 while ((thread->t_flags & SVC_STOPPING) == 0 ||
730 svc->srv_n_difficult_replies != 0) {
731 /* Don't exit while there are replies to be handled */
732 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
733 ptlrpc_retry_rqbds, svc);
735 l_wait_event_exclusive (svc->srv_waitq,
736 ((thread->t_flags & SVC_STOPPING) != 0 &&
737 svc->srv_n_difficult_replies == 0) ||
738 (!list_empty(&svc->srv_idle_rqbds) &&
739 svc->srv_rqbd_timeout == 0) ||
740 !list_empty (&svc->srv_reply_queue) ||
741 (!list_empty (&svc->srv_request_queue) &&
742 (svc->srv_n_difficult_replies == 0 ||
743 svc->srv_n_active_reqs <
744 (svc->srv_nthreads - 1))),
747 if (!list_empty (&svc->srv_reply_queue))
748 ptlrpc_server_handle_reply (svc);
750 /* only handle requests if there are no difficult replies
751 * outstanding, or I'm not the last thread handling
753 if (!list_empty (&svc->srv_request_queue) &&
754 (svc->srv_n_difficult_replies == 0 ||
755 svc->srv_n_active_reqs < (svc->srv_nthreads - 1)))
756 ptlrpc_server_handle_request (svc);
758 if (!list_empty(&svc->srv_idle_rqbds) &&
759 ptlrpc_server_post_idle_rqbds(svc) < 0) {
760 /* I just failed to repost request buffers. Wait
761 * for a timeout (unless something else happens)
762 * before I try again */
763 svc->srv_rqbd_timeout = HZ/10;
767 spin_lock_irqsave(&svc->srv_lock, flags);
769 svc->srv_nthreads--; /* must know immediately */
770 thread->t_flags = SVC_STOPPED;
771 wake_up(&thread->t_ctl_waitq);
773 spin_unlock_irqrestore(&svc->srv_lock, flags);
775 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
779 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
780 struct ptlrpc_thread *thread)
782 struct l_wait_info lwi = { 0 };
785 spin_lock_irqsave(&svc->srv_lock, flags);
786 thread->t_flags = SVC_STOPPING;
787 spin_unlock_irqrestore(&svc->srv_lock, flags);
789 wake_up_all(&svc->srv_waitq);
790 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
793 spin_lock_irqsave(&svc->srv_lock, flags);
794 list_del(&thread->t_link);
795 spin_unlock_irqrestore(&svc->srv_lock, flags);
797 OBD_FREE(thread, sizeof(*thread));
800 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
803 struct ptlrpc_thread *thread;
805 spin_lock_irqsave(&svc->srv_lock, flags);
806 while (!list_empty(&svc->srv_threads)) {
807 thread = list_entry(svc->srv_threads.next,
808 struct ptlrpc_thread, t_link);
810 spin_unlock_irqrestore(&svc->srv_lock, flags);
811 ptlrpc_stop_thread(svc, thread);
812 spin_lock_irqsave(&svc->srv_lock, flags);
815 spin_unlock_irqrestore(&svc->srv_lock, flags);
818 /* @base_name should be 12 characters or less - 3 will be added on */
819 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
820 int num_threads, char *base_name)
825 for (i = 0; i < num_threads; i++) {
827 sprintf(name, "%s_%02d", base_name, i);
828 rc = ptlrpc_start_thread(dev, svc, name);
830 CERROR("cannot start %s thread #%d: rc %d\n", base_name,
832 ptlrpc_stop_all_threads(svc);
838 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
841 struct l_wait_info lwi = { 0 };
842 struct ptlrpc_svc_data d;
843 struct ptlrpc_thread *thread;
848 OBD_ALLOC(thread, sizeof(*thread));
851 init_waitqueue_head(&thread->t_ctl_waitq);
858 spin_lock_irqsave(&svc->srv_lock, flags);
859 list_add(&thread->t_link, &svc->srv_threads);
860 spin_unlock_irqrestore(&svc->srv_lock, flags);
862 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
863 * just drop the VM and FILES in ptlrpc_daemonize() right away.
865 rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
867 CERROR("cannot start thread: %d\n", rc);
868 OBD_FREE(thread, sizeof(*thread));
871 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
877 int ptlrpc_unregister_service(struct ptlrpc_service *service)
882 struct ptlrpc_srv_ni *srv_ni;
883 struct l_wait_info lwi;
884 struct list_head *tmp;
886 LASSERT(list_empty(&service->srv_threads));
888 spin_lock (&ptlrpc_all_services_lock);
889 list_del_init (&service->srv_list);
890 spin_unlock (&ptlrpc_all_services_lock);
892 ptlrpc_lprocfs_unregister_service(service);
894 for (i = 0; i < ptlrpc_ninterfaces; i++) {
895 srv_ni = &service->srv_interfaces[i];
896 CDEBUG(D_NET, "%s: tearing down interface %s\n",
897 service->srv_name, srv_ni->sni_ni->pni_name);
899 /* Unlink all the request buffers. This forces a 'final'
900 * event with its 'unlink' flag set for each posted rqbd */
901 list_for_each(tmp, &srv_ni->sni_active_rqbds) {
902 struct ptlrpc_request_buffer_desc *rqbd =
903 list_entry(tmp, struct ptlrpc_request_buffer_desc,
906 rc = PtlMDUnlink(rqbd->rqbd_md_h);
907 LASSERT (rc == PTL_OK || rc == PTL_MD_INVALID);
910 /* Wait for the network to release any buffers it's
911 * currently filling */
913 spin_lock_irqsave(&service->srv_lock, flags);
914 rc = srv_ni->sni_nrqbd_receiving;
915 spin_unlock_irqrestore(&service->srv_lock, flags);
920 /* Network access will complete in finite time but
921 * the HUGE timeout lets us CWARN for visibility of
923 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
924 rc = l_wait_event(service->srv_waitq,
925 srv_ni->sni_nrqbd_receiving == 0,
927 if (rc == -ETIMEDOUT)
928 CWARN("Waiting for request buffers on "
929 "service %s on interface %s ",
930 service->srv_name, srv_ni->sni_ni->pni_name);
933 /* schedule all outstanding replies to terminate them */
934 spin_lock_irqsave(&service->srv_lock, flags);
935 while (!list_empty(&srv_ni->sni_active_replies)) {
936 struct ptlrpc_reply_state *rs =
937 list_entry(srv_ni->sni_active_replies.next,
938 struct ptlrpc_reply_state,
940 ptlrpc_schedule_difficult_reply(rs);
942 spin_unlock_irqrestore(&service->srv_lock, flags);
945 /* purge the request queue. NB No new replies (rqbds all unlinked)
946 * and no service threads, so I'm the only thread noodling the
947 * request queue now */
948 while (!list_empty(&service->srv_request_queue)) {
949 struct ptlrpc_request *req =
950 list_entry(service->srv_request_queue.next,
951 struct ptlrpc_request,
954 list_del(&req->rq_list);
955 service->srv_n_queued_reqs--;
956 service->srv_n_active_reqs++;
958 ptlrpc_server_free_request(service, req);
960 LASSERT(service->srv_n_queued_reqs == 0);
961 LASSERT(service->srv_n_active_reqs == 0);
963 for (i = 0; i < ptlrpc_ninterfaces; i++) {
964 srv_ni = &service->srv_interfaces[i];
965 LASSERT(list_empty(&srv_ni->sni_active_rqbds));
968 /* Now free all the request buffers since nothing references them
970 while (!list_empty(&service->srv_idle_rqbds)) {
971 struct ptlrpc_request_buffer_desc *rqbd =
972 list_entry(service->srv_idle_rqbds.next,
973 struct ptlrpc_request_buffer_desc,
976 ptlrpc_free_rqbd(rqbd);
979 /* wait for all outstanding replies to complete (they were
980 * scheduled having been flagged to abort above) */
981 while (atomic_read(&service->srv_outstanding_replies) != 0) {
982 struct l_wait_info lwi = LWI_TIMEOUT(10 * HZ, NULL, NULL);
984 rc = l_wait_event(service->srv_waitq,
985 !list_empty(&service->srv_reply_queue), &lwi);
986 LASSERT(rc == 0 || rc == -ETIMEDOUT);
989 ptlrpc_server_handle_reply(service);
992 CWARN("Unexpectedly long timeout %p\n", service);
996 offsetof(struct ptlrpc_service,
997 srv_interfaces[ptlrpc_ninterfaces]));