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 * Move all request from an existing request set to the ptlrpcd queue.
107 * All requests from the set must be in phase RQ_PHASE_NEW.
109 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set)
111 struct list_head *tmp, *pos;
113 list_for_each_safe(pos, tmp, &set->set_requests) {
114 struct ptlrpc_request *req =
115 list_entry(pos, struct ptlrpc_request, rq_set_chain);
117 LASSERT(req->rq_phase == RQ_PHASE_NEW);
118 list_del_init(&req->rq_set_chain);
120 ptlrpcd_add_req(req, PSCOPE_OTHER);
121 set->set_remaining--;
123 LASSERT(set->set_remaining == 0);
125 EXPORT_SYMBOL(ptlrpcd_add_rqset);
128 * Requests that are added to the ptlrpcd queue are sent via
129 * ptlrpcd_check->ptlrpc_check_set().
131 int ptlrpcd_add_req(struct ptlrpc_request *req, enum ptlrpcd_scope scope)
133 struct ptlrpcd_ctl *pc;
134 enum pscope_thread pt;
137 LASSERT(scope < PSCOPE_NR);
138 pt = req->rq_send_state == LUSTRE_IMP_FULL ? PT_NORMAL : PT_RECOVERY;
139 pc = &ptlrpcd_scopes[scope].pscope_thread[pt].pt_ctl;
140 rc = ptlrpc_set_add_new_req(pc, req);
142 * XXX disable this for CLIO: environment is needed for interpreter.
146 * Thread is probably in stop now so we need to
147 * kill this rpc as it was not added. Let's call
148 * interpret for it to let know we're killing it
149 * so that higher levels might free associated
152 ptlrpc_req_interpret(NULL, req, -EBADR);
154 ptlrpc_req_finished(req);
160 static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
162 struct list_head *tmp, *pos;
163 struct ptlrpc_request *req;
167 spin_lock(&pc->pc_set->set_new_req_lock);
168 list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
169 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
170 list_del_init(&req->rq_set_chain);
171 ptlrpc_set_add_req(pc->pc_set, req);
173 * Need to calculate its timeout.
177 spin_unlock(&pc->pc_set->set_new_req_lock);
179 if (pc->pc_set->set_remaining) {
180 rc = rc | ptlrpc_check_set(env, pc->pc_set);
183 * XXX: our set never completes, so we prune the completed
184 * reqs after each iteration. boy could this be smarter.
186 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
187 req = list_entry(pos, struct ptlrpc_request,
189 if (req->rq_phase != RQ_PHASE_COMPLETE)
192 list_del_init(&req->rq_set_chain);
194 ptlrpc_req_finished (req);
200 * If new requests have been added, make sure to wake up.
202 spin_lock(&pc->pc_set->set_new_req_lock);
203 rc = !list_empty(&pc->pc_set->set_new_requests);
204 spin_unlock(&pc->pc_set->set_new_req_lock);
212 * ptlrpc's code paths like to execute in process context, so we have this
213 * thread which spins on a set which contains the io rpcs. llite specifies
214 * ptlrpcd's set when it pushes pages down into the oscs.
216 static int ptlrpcd(void *arg)
218 struct ptlrpcd_ctl *pc = arg;
219 struct lu_env env = { .le_ses = NULL };
223 rc = cfs_daemonize_ctxt(pc->pc_name);
226 * XXX So far only "client" ptlrpcd uses an environment. In
227 * the future, ptlrpcd thread (or a thread-set) has to given
228 * an argument, describing its "scope".
230 rc = lu_context_init(&env.le_ctx,
231 LCT_CL_THREAD|LCT_REMEMBER|LCT_NOREF);
234 complete(&pc->pc_starting);
238 env.le_ctx.lc_cookie = 0x7;
241 * This mainloop strongly resembles ptlrpc_set_wait() except that our
242 * set never completes. ptlrpcd_check() calls ptlrpc_check_set() when
243 * there are requests in the set. New requests come in on the set's
244 * new_req_list and ptlrpcd_check() moves them into the set.
247 struct l_wait_info lwi;
250 rc = lu_env_refill(&env);
253 * XXX This is very awkward situation, because
254 * execution can neither continue (request
255 * interpreters assume that env is set up), nor repeat
256 * the loop (as this potentially results in a tight
257 * loop of -ENOMEM's).
259 * Fortunately, refill only ever does something when
260 * new modules are loaded, i.e., early during boot up.
262 CERROR("Failure to refill session: %d\n", rc);
266 timeout = ptlrpc_set_next_timeout(pc->pc_set);
267 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
268 ptlrpc_expired_set, pc->pc_set);
270 lu_context_enter(&env.le_ctx);
271 l_wait_event(pc->pc_set->set_waitq,
272 ptlrpcd_check(&env, pc), &lwi);
273 lu_context_exit(&env.le_ctx);
276 * Abort inflight rpcs for forced stop case.
278 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
279 if (test_bit(LIOD_FORCE, &pc->pc_flags))
280 ptlrpc_abort_set(pc->pc_set);
285 * Let's make one more loop to make sure that ptlrpcd_check()
286 * copied all raced new rpcs into the set so we can kill them.
291 * Wait for inflight requests to drain.
293 if (!list_empty(&pc->pc_set->set_requests))
294 ptlrpc_set_wait(pc->pc_set);
295 lu_context_fini(&env.le_ctx);
296 complete(&pc->pc_finishing);
298 clear_bit(LIOD_START, &pc->pc_flags);
299 clear_bit(LIOD_STOP, &pc->pc_flags);
300 clear_bit(LIOD_FORCE, &pc->pc_flags);
304 #else /* !__KERNEL__ */
306 int ptlrpcd_check_async_rpcs(void *arg)
308 struct ptlrpcd_ctl *pc = arg;
316 if (pc->pc_recurred == 1) {
317 rc = lu_env_refill(&pc->pc_env);
319 lu_context_enter(&pc->pc_env.le_ctx);
320 rc = ptlrpcd_check(&pc->pc_env, pc);
321 lu_context_exit(&pc->pc_env.le_ctx);
323 ptlrpc_expired_set(pc->pc_set);
325 * XXX: send replay requests.
327 if (test_bit(LIOD_RECOVERY, &pc->pc_flags))
328 rc = ptlrpcd_check(&pc->pc_env, pc);
336 int ptlrpcd_idle(void *arg)
338 struct ptlrpcd_ctl *pc = arg;
340 return (list_empty(&pc->pc_set->set_new_requests) &&
341 pc->pc_set->set_remaining == 0);
346 int ptlrpcd_start(const char *name, struct ptlrpcd_ctl *pc)
352 * Do not allow start second thread for one pc.
354 if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
355 CERROR("Starting second thread (%s) for same pc %p\n",
360 init_completion(&pc->pc_starting);
361 init_completion(&pc->pc_finishing);
362 spin_lock_init(&pc->pc_lock);
363 snprintf (pc->pc_name, sizeof (pc->pc_name), name);
364 pc->pc_set = ptlrpc_prep_set();
365 if (pc->pc_set == NULL)
366 GOTO(out, rc = -ENOMEM);
368 * So far only "client" ptlrpcd uses an environment. In the future,
369 * ptlrpcd thread (or a thread-set) has to be given an argument,
370 * describing its "scope".
372 rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
374 ptlrpc_set_destroy(pc->pc_set);
379 rc = cfs_kernel_thread(ptlrpcd, pc, 0);
381 lu_context_fini(&pc->pc_env.le_ctx);
382 ptlrpc_set_destroy(pc->pc_set);
386 wait_for_completion(&pc->pc_starting);
388 pc->pc_wait_callback =
389 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
390 &ptlrpcd_check_async_rpcs, pc);
391 pc->pc_idle_callback =
392 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
397 clear_bit(LIOD_START, &pc->pc_flags);
401 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
403 if (!test_bit(LIOD_START, &pc->pc_flags)) {
404 CERROR("Thread for pc %p was not started\n", pc);
408 set_bit(LIOD_STOP, &pc->pc_flags);
410 set_bit(LIOD_FORCE, &pc->pc_flags);
411 cfs_waitq_signal(&pc->pc_set->set_waitq);
413 wait_for_completion(&pc->pc_finishing);
415 liblustre_deregister_wait_callback(pc->pc_wait_callback);
416 liblustre_deregister_idle_callback(pc->pc_idle_callback);
418 lu_context_fini(&pc->pc_env.le_ctx);
419 ptlrpc_set_destroy(pc->pc_set);
422 void ptlrpcd_fini(void)
429 for (i = 0; i < PSCOPE_NR; ++i) {
430 for (j = 0; j < PT_NR; ++j) {
431 struct ptlrpcd_ctl *pc;
433 pc = &ptlrpcd_scopes[i].pscope_thread[j].pt_ctl;
435 if (test_bit(LIOD_START, &pc->pc_flags))
442 int ptlrpcd_addref(void)
449 mutex_down(&ptlrpcd_sem);
450 if (++ptlrpcd_users == 1) {
451 for (i = 0; rc == 0 && i < PSCOPE_NR; ++i) {
452 for (j = 0; rc == 0 && j < PT_NR; ++j) {
453 struct ptlrpcd_thread *pt;
454 struct ptlrpcd_ctl *pc;
456 pt = &ptlrpcd_scopes[i].pscope_thread[j];
458 if (j == PT_RECOVERY)
459 set_bit(LIOD_RECOVERY, &pc->pc_flags);
460 rc = ptlrpcd_start(pt->pt_name, pc);
468 mutex_up(&ptlrpcd_sem);
472 void ptlrpcd_decref(void)
474 mutex_down(&ptlrpcd_sem);
475 if (--ptlrpcd_users == 0)
477 mutex_up(&ptlrpcd_sem);