1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author Peter Braam <braam@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_RPC
30 # include <libcfs/libcfs.h>
31 #else /* __KERNEL__ */
32 # include <liblustre.h>
36 #include <libcfs/kp30.h>
37 #include <lustre_net.h>
38 # include <lustre_lib.h>
40 #include <lustre_ha.h>
41 #include <obd_class.h> /* for obd_zombie */
42 #include <obd_support.h> /* for OBD_FAIL_CHECK */
43 #include <lprocfs_status.h>
47 unsigned long pc_flags;
49 struct completion pc_starting;
50 struct completion pc_finishing;
51 struct ptlrpc_request_set *pc_set;
56 void *pc_wait_callback;
57 void *pc_idle_callback;
61 static struct ptlrpcd_ctl ptlrpcd_pc;
62 static struct ptlrpcd_ctl ptlrpcd_recovery_pc;
64 struct semaphore ptlrpcd_sem;
65 static int ptlrpcd_users = 0;
67 void ptlrpcd_wake(struct ptlrpc_request *req)
69 struct ptlrpc_request_set *rq_set = req->rq_set;
71 LASSERT(rq_set != NULL);
73 cfs_waitq_signal(&rq_set->set_waitq);
76 /* requests that are added to the ptlrpcd queue are sent via
77 * ptlrpcd_check->ptlrpc_check_set() */
78 void ptlrpcd_add_req(struct ptlrpc_request *req)
80 struct ptlrpcd_ctl *pc;
82 if (req->rq_send_state == LUSTRE_IMP_FULL)
85 pc = &ptlrpcd_recovery_pc;
87 ptlrpc_set_add_new_req(pc->pc_set, req);
88 cfs_waitq_signal(&pc->pc_set->set_waitq);
91 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
93 struct list_head *tmp, *pos;
94 struct ptlrpc_request *req;
98 if (test_bit(LIOD_STOP, &pc->pc_flags))
101 spin_lock(&pc->pc_set->set_new_req_lock);
102 list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
103 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
104 list_del_init(&req->rq_set_chain);
105 ptlrpc_set_add_req(pc->pc_set, req);
106 rc = 1; /* need to calculate its timeout */
108 spin_unlock(&pc->pc_set->set_new_req_lock);
110 if (pc->pc_set->set_remaining) {
111 rc = rc | ptlrpc_check_set(pc->pc_set);
113 /* XXX our set never completes, so we prune the completed
114 * reqs after each iteration. boy could this be smarter. */
115 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
116 req = list_entry(pos, struct ptlrpc_request,
118 if (req->rq_phase != RQ_PHASE_COMPLETE)
121 list_del_init(&req->rq_set_chain);
123 ptlrpc_req_finished (req);
128 /* If new requests have been added, make sure to wake up */
129 spin_lock(&pc->pc_set->set_new_req_lock);
130 rc = !list_empty(&pc->pc_set->set_new_requests);
131 spin_unlock(&pc->pc_set->set_new_req_lock);
138 /* ptlrpc's code paths like to execute in process context, so we have this
139 * thread which spins on a set which contains the io rpcs. llite specifies
140 * ptlrpcd's set when it pushes pages down into the oscs */
141 static int ptlrpcd(void *arg)
143 struct ptlrpcd_ctl *pc = arg;
147 if ((rc = cfs_daemonize_ctxt(pc->pc_name))) {
148 complete(&pc->pc_starting);
152 complete(&pc->pc_starting);
154 /* this mainloop strongly resembles ptlrpc_set_wait except
155 * that our set never completes. ptlrpcd_check calls ptlrpc_check_set
156 * when there are requests in the set. new requests come in
157 * on the set's new_req_list and ptlrpcd_check moves them into
160 struct l_wait_info lwi;
161 cfs_duration_t timeout;
163 timeout = cfs_time_seconds(ptlrpc_set_next_timeout(pc->pc_set));
164 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
166 l_wait_event(pc->pc_set->set_waitq, ptlrpcd_check(pc), &lwi);
168 if (test_bit(LIOD_STOP, &pc->pc_flags))
171 /* wait for inflight requests to drain */
172 if (!list_empty(&pc->pc_set->set_requests))
173 ptlrpc_set_wait(pc->pc_set);
174 complete(&pc->pc_finishing);
180 int ptlrpcd_check_async_rpcs(void *arg)
182 struct ptlrpcd_ctl *pc = arg;
185 /* single threaded!! */
188 if (pc->pc_recurred == 1) {
189 rc = ptlrpcd_check(pc);
191 ptlrpc_expired_set(pc->pc_set);
192 /*XXX send replay requests */
193 if (pc == &ptlrpcd_recovery_pc)
194 rc = ptlrpcd_check(pc);
201 int ptlrpcd_idle(void *arg)
203 struct ptlrpcd_ctl *pc = arg;
205 return (list_empty(&pc->pc_set->set_new_requests) &&
206 pc->pc_set->set_remaining == 0);
211 static int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
216 memset(pc, 0, sizeof(*pc));
217 init_completion(&pc->pc_starting);
218 init_completion(&pc->pc_finishing);
220 spin_lock_init(&pc->pc_lock);
221 snprintf (pc->pc_name, sizeof (pc->pc_name), name);
223 pc->pc_set = ptlrpc_prep_set();
224 if (pc->pc_set == NULL)
228 rc = cfs_kernel_thread(ptlrpcd, pc, 0);
230 ptlrpc_set_destroy(pc->pc_set);
234 wait_for_completion(&pc->pc_starting);
236 pc->pc_wait_callback =
237 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
238 &ptlrpcd_check_async_rpcs, pc);
239 pc->pc_idle_callback =
240 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
247 static void ptlrpcd_stop(struct ptlrpcd_ctl *pc)
249 set_bit(LIOD_STOP, &pc->pc_flags);
250 cfs_waitq_signal(&pc->pc_set->set_waitq);
252 wait_for_completion(&pc->pc_finishing);
254 liblustre_deregister_wait_callback(pc->pc_wait_callback);
255 liblustre_deregister_idle_callback(pc->pc_idle_callback);
257 ptlrpc_set_destroy(pc->pc_set);
260 int ptlrpcd_addref(void)
265 mutex_down(&ptlrpcd_sem);
266 if (++ptlrpcd_users != 1)
269 rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
275 rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
277 ptlrpcd_stop(&ptlrpcd_pc);
282 mutex_up(&ptlrpcd_sem);
286 void ptlrpcd_decref(void)
288 mutex_down(&ptlrpcd_sem);
289 if (--ptlrpcd_users == 0) {
290 ptlrpcd_stop(&ptlrpcd_pc);
291 ptlrpcd_stop(&ptlrpcd_recovery_pc);
293 mutex_up(&ptlrpcd_sem);