1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/ptlrpcd.c
39 #define DEBUG_SUBSYSTEM S_RPC
42 # include <libcfs/libcfs.h>
43 #else /* __KERNEL__ */
44 # include <liblustre.h>
48 #include <lustre_net.h>
49 # include <lustre_lib.h>
51 #include <lustre_ha.h>
52 #include <obd_class.h> /* for obd_zombie */
53 #include <obd_support.h> /* for OBD_FAIL_CHECK */
54 #include <cl_object.h> /* cl_env_{get,put}() */
55 #include <lprocfs_status.h>
63 struct ptlrpcd_scope_ctl {
64 struct ptlrpcd_thread {
66 struct ptlrpcd_ctl pt_ctl;
67 } pscope_thread[PT_NR];
70 static struct ptlrpcd_scope_ctl ptlrpcd_scopes[PSCOPE_NR] = {
74 .pt_name = "ptlrpcd-brw"
77 .pt_name = "ptlrpcd-brw-rcv"
87 .pt_name = "ptlrpcd-rcv"
93 struct semaphore ptlrpcd_sem;
94 static int ptlrpcd_users = 0;
96 void ptlrpcd_wake(struct ptlrpc_request *req)
98 struct ptlrpc_request_set *rq_set = req->rq_set;
100 LASSERT(rq_set != NULL);
102 cfs_waitq_signal(&rq_set->set_waitq);
106 * Requests that are added to the ptlrpcd queue are sent via
107 * ptlrpcd_check->ptlrpc_check_set().
109 void ptlrpcd_add_req(struct ptlrpc_request *req, enum ptlrpcd_scope scope)
111 struct ptlrpcd_ctl *pc;
112 enum pscope_thread pt;
115 LASSERT(scope < PSCOPE_NR);
116 pt = req->rq_send_state == LUSTRE_IMP_FULL ? PT_NORMAL : PT_RECOVERY;
117 pc = &ptlrpcd_scopes[scope].pscope_thread[pt].pt_ctl;
118 rc = ptlrpc_set_add_new_req(pc, req);
120 * XXX disable this for CLIO: environment is needed for interpreter.
123 ptlrpc_interpterer_t interpreter;
125 interpreter = req->rq_interpret_reply;
128 * Thread is probably in stop now so we need to
129 * kill this rpc as it was not added. Let's call
130 * interpret for it to let know we're killing it
131 * so that higher levels might free assosiated
134 req->rq_status = -EBADR;
135 interpreter(NULL, req, &req->rq_async_args,
138 ptlrpc_req_finished(req);
142 static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
144 struct list_head *tmp, *pos;
145 struct ptlrpc_request *req;
149 if (test_bit(LIOD_STOP, &pc->pc_flags))
152 spin_lock(&pc->pc_set->set_new_req_lock);
153 list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
154 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
155 list_del_init(&req->rq_set_chain);
156 ptlrpc_set_add_req(pc->pc_set, req);
158 * Need to calculate its timeout.
162 spin_unlock(&pc->pc_set->set_new_req_lock);
164 if (pc->pc_set->set_remaining) {
165 rc = rc | ptlrpc_check_set(env, pc->pc_set);
168 * XXX: our set never completes, so we prune the completed
169 * reqs after each iteration. boy could this be smarter.
171 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
172 req = list_entry(pos, struct ptlrpc_request,
174 if (req->rq_phase != RQ_PHASE_COMPLETE)
177 list_del_init(&req->rq_set_chain);
179 ptlrpc_req_finished (req);
185 * If new requests have been added, make sure to wake up.
187 spin_lock(&pc->pc_set->set_new_req_lock);
188 rc = !list_empty(&pc->pc_set->set_new_requests);
189 spin_unlock(&pc->pc_set->set_new_req_lock);
197 * ptlrpc's code paths like to execute in process context, so we have this
198 * thread which spins on a set which contains the io rpcs. llite specifies
199 * ptlrpcd's set when it pushes pages down into the oscs.
201 static int ptlrpcd(void *arg)
203 struct ptlrpcd_ctl *pc = arg;
204 struct lu_env env = { .le_ses = NULL };
208 rc = cfs_daemonize_ctxt(pc->pc_name);
211 * XXX So far only "client" ptlrpcd uses an environment. In
212 * the future, ptlrpcd thread (or a thread-set) has to given
213 * an argument, describing its "scope".
215 rc = lu_context_init(&env.le_ctx,
216 LCT_CL_THREAD|LCT_REMEMBER|LCT_NOREF);
219 complete(&pc->pc_starting);
223 env.le_ctx.lc_cookie = 0x7;
225 * This mainloop strongly resembles ptlrpc_set_wait() except that our
226 * set never completes. ptlrpcd_check() calls ptlrpc_check_set() when
227 * there are requests in the set. New requests come in on the set's
228 * new_req_list and ptlrpcd_check() moves them into the set.
231 struct l_wait_info lwi;
234 rc = lu_env_refill(&env);
237 * XXX This is very awkward situation, because
238 * execution can neither continue (request
239 * interpreters assume that env is set up), nor repeat
240 * the loop (as this potentially results in a tight
241 * loop of -ENOMEM's).
243 * Fortunately, refill only ever does something when
244 * new modules are loaded, i.e., early during boot up.
246 CERROR("Failure to refill session: %d\n", rc);
250 timeout = ptlrpc_set_next_timeout(pc->pc_set);
251 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
252 ptlrpc_expired_set, pc->pc_set);
254 lu_context_enter(&env.le_ctx);
255 l_wait_event(pc->pc_set->set_waitq,
256 ptlrpcd_check(&env, pc), &lwi);
257 lu_context_exit(&env.le_ctx);
260 * Abort inflight rpcs for forced stop case.
262 if (test_bit(LIOD_STOP_FORCE, &pc->pc_flags))
263 ptlrpc_abort_set(pc->pc_set);
265 if (test_bit(LIOD_STOP, &pc->pc_flags))
270 * Wait for inflight requests to drain.
272 if (!list_empty(&pc->pc_set->set_requests))
273 ptlrpc_set_wait(pc->pc_set);
274 lu_context_fini(&env.le_ctx);
275 complete(&pc->pc_finishing);
277 clear_bit(LIOD_START, &pc->pc_flags);
278 clear_bit(LIOD_STOP, &pc->pc_flags);
282 #else /* !__KERNEL__ */
284 int ptlrpcd_check_async_rpcs(void *arg)
286 struct ptlrpcd_ctl *pc = arg;
294 if (pc->pc_recurred == 1) {
295 lu_context_enter(&pc->pc_env.le_ctx);
296 rc = ptlrpcd_check(&pc->pc_env, pc);
297 lu_context_exit(&pc->pc_env.le_ctx);
299 ptlrpc_expired_set(pc->pc_set);
301 * XXX: send replay requests.
303 if (test_bit(LIOD_RECOVERY, &pc->pc_flags))
304 rc = ptlrpcd_check(&pc->pc_env, pc);
311 int ptlrpcd_idle(void *arg)
313 struct ptlrpcd_ctl *pc = arg;
315 return (list_empty(&pc->pc_set->set_new_requests) &&
316 pc->pc_set->set_remaining == 0);
321 int ptlrpcd_start(const char *name, struct ptlrpcd_ctl *pc)
327 * Do not allow start second thread for one pc.
329 if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
330 CERROR("Starting second thread (%s) for same pc %p\n",
335 init_completion(&pc->pc_starting);
336 init_completion(&pc->pc_finishing);
337 spin_lock_init(&pc->pc_lock);
338 snprintf (pc->pc_name, sizeof (pc->pc_name), name);
339 pc->pc_set = ptlrpc_prep_set();
340 if (pc->pc_set == NULL)
341 GOTO(out, rc = -ENOMEM);
343 * So far only "client" ptlrpcd uses an environment. In the future,
344 * ptlrpcd thread (or a thread-set) has to be given an argument,
345 * describing its "scope".
347 rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
349 ptlrpc_set_destroy(pc->pc_set);
354 rc = cfs_kernel_thread(ptlrpcd, pc, 0);
356 lu_context_fini(&pc->pc_env.le_ctx);
357 ptlrpc_set_destroy(pc->pc_set);
361 wait_for_completion(&pc->pc_starting);
363 pc->pc_wait_callback =
364 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
365 &ptlrpcd_check_async_rpcs, pc);
366 pc->pc_idle_callback =
367 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
372 clear_bit(LIOD_START, &pc->pc_flags);
376 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
378 if (!test_bit(LIOD_START, &pc->pc_flags)) {
379 CERROR("Thread for pc %p was not started\n", pc);
383 set_bit(LIOD_STOP, &pc->pc_flags);
385 set_bit(LIOD_STOP_FORCE, &pc->pc_flags);
386 cfs_waitq_signal(&pc->pc_set->set_waitq);
388 wait_for_completion(&pc->pc_finishing);
390 liblustre_deregister_wait_callback(pc->pc_wait_callback);
391 liblustre_deregister_idle_callback(pc->pc_idle_callback);
393 lu_context_fini(&pc->pc_env.le_ctx);
394 ptlrpc_set_destroy(pc->pc_set);
397 void ptlrpcd_fini(void)
404 for (i = 0; i < PSCOPE_NR; ++i) {
405 for (j = 0; j < PT_NR; ++j) {
406 struct ptlrpcd_ctl *pc;
408 pc = &ptlrpcd_scopes[i].pscope_thread[j].pt_ctl;
410 if (test_bit(LIOD_START, &pc->pc_flags))
417 int ptlrpcd_addref(void)
424 mutex_down(&ptlrpcd_sem);
425 if (++ptlrpcd_users == 1) {
426 for (i = 0; rc == 0 && i < PSCOPE_NR; ++i) {
427 for (j = 0; rc == 0 && j < PT_NR; ++j) {
428 struct ptlrpcd_thread *pt;
429 struct ptlrpcd_ctl *pc;
431 pt = &ptlrpcd_scopes[i].pscope_thread[j];
433 if (j == PT_RECOVERY)
434 set_bit(LIOD_RECOVERY, &pc->pc_flags);
435 rc = ptlrpcd_start(pt->pt_name, pc);
443 mutex_up(&ptlrpcd_sem);
447 void ptlrpcd_decref(void)
449 mutex_down(&ptlrpcd_sem);
450 if (--ptlrpcd_users == 0)
452 mutex_up(&ptlrpcd_sem);