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 cfs_semaphore_t 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 cfs_list_t *tmp, *pos;
113 cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
114 struct ptlrpc_request *req =
115 cfs_list_entry(pos, struct ptlrpc_request,
118 LASSERT(req->rq_phase == RQ_PHASE_NEW);
119 cfs_list_del_init(&req->rq_set_chain);
121 ptlrpcd_add_req(req, PSCOPE_OTHER);
122 set->set_remaining--;
124 LASSERT(set->set_remaining == 0);
126 EXPORT_SYMBOL(ptlrpcd_add_rqset);
129 * Requests that are added to the ptlrpcd queue are sent via
130 * ptlrpcd_check->ptlrpc_check_set().
132 int ptlrpcd_add_req(struct ptlrpc_request *req, enum ptlrpcd_scope scope)
134 struct ptlrpcd_ctl *pc;
135 enum pscope_thread pt;
138 LASSERT(scope < PSCOPE_NR);
139 pt = req->rq_send_state == LUSTRE_IMP_FULL ? PT_NORMAL : PT_RECOVERY;
140 pc = &ptlrpcd_scopes[scope].pscope_thread[pt].pt_ctl;
141 rc = ptlrpc_set_add_new_req(pc, req);
143 * XXX disable this for CLIO: environment is needed for interpreter.
144 * add debug temporary to check rc.
146 LASSERTF(rc == 0, "ptlrpcd_add_req failed (rc = %d)\n", rc);
149 * Thread is probably in stop now so we need to
150 * kill this rpc as it was not added. Let's call
151 * interpret for it to let know we're killing it
152 * so that higher levels might free associated
155 ptlrpc_req_interpret(NULL, req, -EBADR);
157 ptlrpc_req_finished(req);
158 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING) {
160 * The request is for recovery, should be sent ASAP.
162 cfs_waitq_signal(&pc->pc_set->set_waitq);
168 static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
170 cfs_list_t *tmp, *pos;
171 struct ptlrpc_request *req;
175 cfs_spin_lock(&pc->pc_set->set_new_req_lock);
176 cfs_list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
177 req = cfs_list_entry(pos, struct ptlrpc_request, rq_set_chain);
178 cfs_list_del_init(&req->rq_set_chain);
179 ptlrpc_set_add_req(pc->pc_set, req);
181 * Need to calculate its timeout.
185 cfs_spin_unlock(&pc->pc_set->set_new_req_lock);
187 if (pc->pc_set->set_remaining) {
188 rc = rc | ptlrpc_check_set(env, pc->pc_set);
191 * XXX: our set never completes, so we prune the completed
192 * reqs after each iteration. boy could this be smarter.
194 cfs_list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
195 req = cfs_list_entry(pos, struct ptlrpc_request,
197 if (req->rq_phase != RQ_PHASE_COMPLETE)
200 cfs_list_del_init(&req->rq_set_chain);
202 ptlrpc_req_finished (req);
208 * If new requests have been added, make sure to wake up.
210 cfs_spin_lock(&pc->pc_set->set_new_req_lock);
211 rc = !cfs_list_empty(&pc->pc_set->set_new_requests);
212 cfs_spin_unlock(&pc->pc_set->set_new_req_lock);
220 * ptlrpc's code paths like to execute in process context, so we have this
221 * thread which spins on a set which contains the io rpcs. llite specifies
222 * ptlrpcd's set when it pushes pages down into the oscs.
224 static int ptlrpcd(void *arg)
226 struct ptlrpcd_ctl *pc = arg;
227 struct lu_env env = { .le_ses = NULL };
231 rc = cfs_daemonize_ctxt(pc->pc_name);
234 * XXX So far only "client" ptlrpcd uses an environment. In
235 * the future, ptlrpcd thread (or a thread-set) has to given
236 * an argument, describing its "scope".
238 rc = lu_context_init(&env.le_ctx,
239 LCT_CL_THREAD|LCT_REMEMBER|LCT_NOREF);
242 cfs_complete(&pc->pc_starting);
246 env.le_ctx.lc_cookie = 0x7;
249 * This mainloop strongly resembles ptlrpc_set_wait() except that our
250 * set never completes. ptlrpcd_check() calls ptlrpc_check_set() when
251 * there are requests in the set. New requests come in on the set's
252 * new_req_list and ptlrpcd_check() moves them into the set.
255 struct l_wait_info lwi;
258 rc = lu_env_refill(&env);
261 * XXX This is very awkward situation, because
262 * execution can neither continue (request
263 * interpreters assume that env is set up), nor repeat
264 * the loop (as this potentially results in a tight
265 * loop of -ENOMEM's).
267 * Fortunately, refill only ever does something when
268 * new modules are loaded, i.e., early during boot up.
270 CERROR("Failure to refill session: %d\n", rc);
274 timeout = ptlrpc_set_next_timeout(pc->pc_set);
275 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
276 ptlrpc_expired_set, pc->pc_set);
278 lu_context_enter(&env.le_ctx);
279 l_wait_event(pc->pc_set->set_waitq,
280 ptlrpcd_check(&env, pc), &lwi);
281 lu_context_exit(&env.le_ctx);
284 * Abort inflight rpcs for forced stop case.
286 if (cfs_test_bit(LIOD_STOP, &pc->pc_flags)) {
287 if (cfs_test_bit(LIOD_FORCE, &pc->pc_flags))
288 ptlrpc_abort_set(pc->pc_set);
293 * Let's make one more loop to make sure that ptlrpcd_check()
294 * copied all raced new rpcs into the set so we can kill them.
299 * Wait for inflight requests to drain.
301 if (!cfs_list_empty(&pc->pc_set->set_requests))
302 ptlrpc_set_wait(pc->pc_set);
303 lu_context_fini(&env.le_ctx);
304 cfs_complete(&pc->pc_finishing);
306 cfs_clear_bit(LIOD_START, &pc->pc_flags);
307 cfs_clear_bit(LIOD_STOP, &pc->pc_flags);
308 cfs_clear_bit(LIOD_FORCE, &pc->pc_flags);
312 #else /* !__KERNEL__ */
314 int ptlrpcd_check_async_rpcs(void *arg)
316 struct ptlrpcd_ctl *pc = arg;
324 if (pc->pc_recurred == 1) {
325 rc = lu_env_refill(&pc->pc_env);
327 lu_context_enter(&pc->pc_env.le_ctx);
328 rc = ptlrpcd_check(&pc->pc_env, pc);
329 lu_context_exit(&pc->pc_env.le_ctx);
331 ptlrpc_expired_set(pc->pc_set);
333 * XXX: send replay requests.
335 if (cfs_test_bit(LIOD_RECOVERY, &pc->pc_flags))
336 rc = ptlrpcd_check(&pc->pc_env, pc);
344 int ptlrpcd_idle(void *arg)
346 struct ptlrpcd_ctl *pc = arg;
348 return (cfs_list_empty(&pc->pc_set->set_new_requests) &&
349 pc->pc_set->set_remaining == 0);
354 int ptlrpcd_start(const char *name, struct ptlrpcd_ctl *pc)
360 * Do not allow start second thread for one pc.
362 if (cfs_test_and_set_bit(LIOD_START, &pc->pc_flags)) {
363 CERROR("Starting second thread (%s) for same pc %p\n",
368 cfs_init_completion(&pc->pc_starting);
369 cfs_init_completion(&pc->pc_finishing);
370 cfs_spin_lock_init(&pc->pc_lock);
371 strncpy(pc->pc_name, name, sizeof(pc->pc_name) - 1);
372 pc->pc_set = ptlrpc_prep_set();
373 if (pc->pc_set == NULL)
374 GOTO(out, rc = -ENOMEM);
376 * So far only "client" ptlrpcd uses an environment. In the future,
377 * ptlrpcd thread (or a thread-set) has to be given an argument,
378 * describing its "scope".
380 rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
382 ptlrpc_set_destroy(pc->pc_set);
387 rc = cfs_kernel_thread(ptlrpcd, pc, 0);
389 lu_context_fini(&pc->pc_env.le_ctx);
390 ptlrpc_set_destroy(pc->pc_set);
394 cfs_wait_for_completion(&pc->pc_starting);
396 pc->pc_wait_callback =
397 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
398 &ptlrpcd_check_async_rpcs, pc);
399 pc->pc_idle_callback =
400 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
405 cfs_clear_bit(LIOD_START, &pc->pc_flags);
409 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
411 if (!cfs_test_bit(LIOD_START, &pc->pc_flags)) {
412 CERROR("Thread for pc %p was not started\n", pc);
416 cfs_set_bit(LIOD_STOP, &pc->pc_flags);
418 cfs_set_bit(LIOD_FORCE, &pc->pc_flags);
419 cfs_waitq_signal(&pc->pc_set->set_waitq);
421 cfs_wait_for_completion(&pc->pc_finishing);
423 liblustre_deregister_wait_callback(pc->pc_wait_callback);
424 liblustre_deregister_idle_callback(pc->pc_idle_callback);
426 lu_context_fini(&pc->pc_env.le_ctx);
427 ptlrpc_set_destroy(pc->pc_set);
430 void ptlrpcd_fini(void)
437 for (i = 0; i < PSCOPE_NR; ++i) {
438 for (j = 0; j < PT_NR; ++j) {
439 struct ptlrpcd_ctl *pc;
441 pc = &ptlrpcd_scopes[i].pscope_thread[j].pt_ctl;
443 if (cfs_test_bit(LIOD_START, &pc->pc_flags))
450 int ptlrpcd_addref(void)
457 cfs_mutex_down(&ptlrpcd_sem);
458 if (++ptlrpcd_users == 1) {
459 for (i = 0; rc == 0 && i < PSCOPE_NR; ++i) {
460 for (j = 0; rc == 0 && j < PT_NR; ++j) {
461 struct ptlrpcd_thread *pt;
462 struct ptlrpcd_ctl *pc;
464 pt = &ptlrpcd_scopes[i].pscope_thread[j];
466 if (j == PT_RECOVERY)
467 cfs_set_bit(LIOD_RECOVERY, &pc->pc_flags);
468 rc = ptlrpcd_start(pt->pt_name, pc);
476 cfs_mutex_up(&ptlrpcd_sem);
480 void ptlrpcd_decref(void)
482 cfs_mutex_down(&ptlrpcd_sem);
483 if (--ptlrpcd_users == 0)
485 cfs_mutex_up(&ptlrpcd_sem);