Whamcloud - gitweb
42e3716bf8b4d17c0d3a76ac061d7070030d3975
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpcd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/ptlrpcd.c
37  */
38
39 #define DEBUG_SUBSYSTEM S_RPC
40
41 #ifdef __KERNEL__
42 # include <libcfs/libcfs.h>
43 #else /* __KERNEL__ */
44 # include <liblustre.h>
45 # include <ctype.h>
46 #endif
47
48 #include <lustre_net.h>
49 # include <lustre_lib.h>
50
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>
56
57 enum pscope_thread {
58         PT_NORMAL,
59         PT_RECOVERY,
60         PT_NR
61 };
62
63 struct ptlrpcd_scope_ctl {
64         struct ptlrpcd_thread {
65                 const char        *pt_name;
66                 struct ptlrpcd_ctl pt_ctl;
67         } pscope_thread[PT_NR];
68 };
69
70 static struct ptlrpcd_scope_ctl ptlrpcd_scopes[PSCOPE_NR] = {
71         [PSCOPE_BRW] = {
72                 .pscope_thread = {
73                         [PT_NORMAL] = {
74                                 .pt_name = "ptlrpcd-brw"
75                         },
76                         [PT_RECOVERY] = {
77                                 .pt_name = "ptlrpcd-brw-rcv"
78                         }
79                 }
80         },
81         [PSCOPE_OTHER] = {
82                 .pscope_thread = {
83                         [PT_NORMAL] = {
84                                 .pt_name = "ptlrpcd"
85                         },
86                         [PT_RECOVERY] = {
87                                 .pt_name = "ptlrpcd-rcv"
88                         }
89                 }
90         }
91 };
92
93 cfs_semaphore_t ptlrpcd_sem;
94 static int ptlrpcd_users = 0;
95
96 void ptlrpcd_wake(struct ptlrpc_request *req)
97 {
98         struct ptlrpc_request_set *rq_set = req->rq_set;
99
100         LASSERT(rq_set != NULL);
101
102         cfs_waitq_signal(&rq_set->set_waitq);
103 }
104
105 /*
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.
108  */
109 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set)
110 {
111         cfs_list_t *tmp, *pos;
112
113         cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
114                 struct ptlrpc_request *req =
115                         cfs_list_entry(pos, struct ptlrpc_request,
116                                        rq_set_chain);
117
118                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
119                 cfs_list_del_init(&req->rq_set_chain);
120                 req->rq_set = NULL;
121                 ptlrpcd_add_req(req, PSCOPE_OTHER);
122                 set->set_remaining--;
123         }
124         LASSERT(set->set_remaining == 0);
125 }
126 EXPORT_SYMBOL(ptlrpcd_add_rqset);
127
128 /*
129  * Requests that are added to the ptlrpcd queue are sent via
130  * ptlrpcd_check->ptlrpc_check_set().
131  */
132 int ptlrpcd_add_req(struct ptlrpc_request *req, enum ptlrpcd_scope scope)
133 {
134         struct ptlrpcd_ctl *pc;
135         enum pscope_thread  pt;
136         int rc;
137
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);
142         /*
143          * XXX disable this for CLIO: environment is needed for interpreter.
144          */
145         if (rc && 0) {
146                 /*
147                  * Thread is probably in stop now so we need to
148                  * kill this rpc as it was not added. Let's call
149                  * interpret for it to let know we're killing it
150                  * so that higher levels might free associated
151                  * resources.
152                  */
153                 ptlrpc_req_interpret(NULL, req, -EBADR);
154                 req->rq_set = NULL;
155                 ptlrpc_req_finished(req);
156         }
157
158         return rc;
159 }
160
161 static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
162 {
163         cfs_list_t *tmp, *pos;
164         struct ptlrpc_request *req;
165         int rc = 0;
166         ENTRY;
167
168         cfs_spin_lock(&pc->pc_set->set_new_req_lock);
169         cfs_list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
170                 req = cfs_list_entry(pos, struct ptlrpc_request, rq_set_chain);
171                 cfs_list_del_init(&req->rq_set_chain);
172                 ptlrpc_set_add_req(pc->pc_set, req);
173                 /*
174                  * Need to calculate its timeout.
175                  */
176                 rc = 1;
177         }
178         cfs_spin_unlock(&pc->pc_set->set_new_req_lock);
179
180         if (pc->pc_set->set_remaining) {
181                 rc = rc | ptlrpc_check_set(env, pc->pc_set);
182
183                 /*
184                  * XXX: our set never completes, so we prune the completed
185                  * reqs after each iteration. boy could this be smarter.
186                  */
187                 cfs_list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
188                         req = cfs_list_entry(pos, struct ptlrpc_request,
189                                          rq_set_chain);
190                         if (req->rq_phase != RQ_PHASE_COMPLETE)
191                                 continue;
192
193                         cfs_list_del_init(&req->rq_set_chain);
194                         req->rq_set = NULL;
195                         ptlrpc_req_finished (req);
196                 }
197         }
198
199         if (rc == 0) {
200                 /*
201                  * If new requests have been added, make sure to wake up.
202                  */
203                 cfs_spin_lock(&pc->pc_set->set_new_req_lock);
204                 rc = !cfs_list_empty(&pc->pc_set->set_new_requests);
205                 cfs_spin_unlock(&pc->pc_set->set_new_req_lock);
206         }
207
208         RETURN(rc);
209 }
210
211 #ifdef __KERNEL__
212 /*
213  * ptlrpc's code paths like to execute in process context, so we have this
214  * thread which spins on a set which contains the io rpcs. llite specifies
215  * ptlrpcd's set when it pushes pages down into the oscs.
216  */
217 static int ptlrpcd(void *arg)
218 {
219         struct ptlrpcd_ctl *pc = arg;
220         struct lu_env env = { .le_ses = NULL };
221         int rc, exit = 0;
222         ENTRY;
223
224         rc = cfs_daemonize_ctxt(pc->pc_name);
225         if (rc == 0) {
226                 /*
227                  * XXX So far only "client" ptlrpcd uses an environment. In
228                  * the future, ptlrpcd thread (or a thread-set) has to given
229                  * an argument, describing its "scope".
230                  */
231                 rc = lu_context_init(&env.le_ctx,
232                                      LCT_CL_THREAD|LCT_REMEMBER|LCT_NOREF);
233         }
234
235         cfs_complete(&pc->pc_starting);
236
237         if (rc != 0)
238                 RETURN(rc);
239         env.le_ctx.lc_cookie = 0x7;
240
241         /*
242          * This mainloop strongly resembles ptlrpc_set_wait() except that our
243          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
244          * there are requests in the set. New requests come in on the set's
245          * new_req_list and ptlrpcd_check() moves them into the set.
246          */
247         do {
248                 struct l_wait_info lwi;
249                 int timeout;
250
251                 rc = lu_env_refill(&env);
252                 if (rc != 0) {
253                         /*
254                          * XXX This is very awkward situation, because
255                          * execution can neither continue (request
256                          * interpreters assume that env is set up), nor repeat
257                          * the loop (as this potentially results in a tight
258                          * loop of -ENOMEM's).
259                          *
260                          * Fortunately, refill only ever does something when
261                          * new modules are loaded, i.e., early during boot up.
262                          */
263                         CERROR("Failure to refill session: %d\n", rc);
264                         continue;
265                 }
266
267                 timeout = ptlrpc_set_next_timeout(pc->pc_set);
268                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
269                                   ptlrpc_expired_set, pc->pc_set);
270
271                 lu_context_enter(&env.le_ctx);
272                 l_wait_event(pc->pc_set->set_waitq,
273                              ptlrpcd_check(&env, pc), &lwi);
274                 lu_context_exit(&env.le_ctx);
275
276                 /*
277                  * Abort inflight rpcs for forced stop case.
278                  */
279                 if (cfs_test_bit(LIOD_STOP, &pc->pc_flags)) {
280                         if (cfs_test_bit(LIOD_FORCE, &pc->pc_flags))
281                                 ptlrpc_abort_set(pc->pc_set);
282                         exit++;
283                 }
284
285                 /*
286                  * Let's make one more loop to make sure that ptlrpcd_check()
287                  * copied all raced new rpcs into the set so we can kill them.
288                  */
289         } while (exit < 2);
290
291         /*
292          * Wait for inflight requests to drain.
293          */
294         if (!cfs_list_empty(&pc->pc_set->set_requests))
295                 ptlrpc_set_wait(pc->pc_set);
296         lu_context_fini(&env.le_ctx);
297         cfs_complete(&pc->pc_finishing);
298
299         cfs_clear_bit(LIOD_START, &pc->pc_flags);
300         cfs_clear_bit(LIOD_STOP, &pc->pc_flags);
301         cfs_clear_bit(LIOD_FORCE, &pc->pc_flags);
302         return 0;
303 }
304
305 #else /* !__KERNEL__ */
306
307 int ptlrpcd_check_async_rpcs(void *arg)
308 {
309         struct ptlrpcd_ctl *pc = arg;
310         int                 rc = 0;
311
312         /*
313          * Single threaded!!
314          */
315         pc->pc_recurred++;
316
317         if (pc->pc_recurred == 1) {
318                 rc = lu_env_refill(&pc->pc_env);
319                 if (rc == 0) {
320                         lu_context_enter(&pc->pc_env.le_ctx);
321                         rc = ptlrpcd_check(&pc->pc_env, pc);
322                         lu_context_exit(&pc->pc_env.le_ctx);
323                         if (!rc)
324                                 ptlrpc_expired_set(pc->pc_set);
325                         /*
326                          * XXX: send replay requests.
327                          */
328                         if (cfs_test_bit(LIOD_RECOVERY, &pc->pc_flags))
329                                 rc = ptlrpcd_check(&pc->pc_env, pc);
330                 }
331         }
332
333         pc->pc_recurred--;
334         return rc;
335 }
336
337 int ptlrpcd_idle(void *arg)
338 {
339         struct ptlrpcd_ctl *pc = arg;
340
341         return (cfs_list_empty(&pc->pc_set->set_new_requests) &&
342                 pc->pc_set->set_remaining == 0);
343 }
344
345 #endif
346
347 int ptlrpcd_start(const char *name, struct ptlrpcd_ctl *pc)
348 {
349         int rc;
350         ENTRY;
351
352         /*
353          * Do not allow start second thread for one pc.
354          */
355         if (cfs_test_and_set_bit(LIOD_START, &pc->pc_flags)) {
356                 CERROR("Starting second thread (%s) for same pc %p\n",
357                        name, pc);
358                 RETURN(-EALREADY);
359         }
360
361         cfs_init_completion(&pc->pc_starting);
362         cfs_init_completion(&pc->pc_finishing);
363         cfs_spin_lock_init(&pc->pc_lock);
364         strncpy(pc->pc_name, name, sizeof(pc->pc_name) - 1);
365         pc->pc_set = ptlrpc_prep_set();
366         if (pc->pc_set == NULL)
367                 GOTO(out, rc = -ENOMEM);
368         /*
369          * So far only "client" ptlrpcd uses an environment. In the future,
370          * ptlrpcd thread (or a thread-set) has to be given an argument,
371          * describing its "scope".
372          */
373         rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
374         if (rc != 0) {
375                 ptlrpc_set_destroy(pc->pc_set);
376                 GOTO(out, rc);
377         }
378
379 #ifdef __KERNEL__
380         rc = cfs_kernel_thread(ptlrpcd, pc, 0);
381         if (rc < 0)  {
382                 lu_context_fini(&pc->pc_env.le_ctx);
383                 ptlrpc_set_destroy(pc->pc_set);
384                 GOTO(out, rc);
385         }
386         rc = 0;
387         cfs_wait_for_completion(&pc->pc_starting);
388 #else
389         pc->pc_wait_callback =
390                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
391                                                  &ptlrpcd_check_async_rpcs, pc);
392         pc->pc_idle_callback =
393                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
394                                                  &ptlrpcd_idle, pc);
395 #endif
396 out:
397         if (rc)
398                 cfs_clear_bit(LIOD_START, &pc->pc_flags);
399         RETURN(rc);
400 }
401
402 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
403 {
404         if (!cfs_test_bit(LIOD_START, &pc->pc_flags)) {
405                 CERROR("Thread for pc %p was not started\n", pc);
406                 return;
407         }
408
409         cfs_set_bit(LIOD_STOP, &pc->pc_flags);
410         if (force)
411                 cfs_set_bit(LIOD_FORCE, &pc->pc_flags);
412         cfs_waitq_signal(&pc->pc_set->set_waitq);
413 #ifdef __KERNEL__
414         cfs_wait_for_completion(&pc->pc_finishing);
415 #else
416         liblustre_deregister_wait_callback(pc->pc_wait_callback);
417         liblustre_deregister_idle_callback(pc->pc_idle_callback);
418 #endif
419         lu_context_fini(&pc->pc_env.le_ctx);
420         ptlrpc_set_destroy(pc->pc_set);
421 }
422
423 void ptlrpcd_fini(void)
424 {
425         int i;
426         int j;
427
428         ENTRY;
429
430         for (i = 0; i < PSCOPE_NR; ++i) {
431                 for (j = 0; j < PT_NR; ++j) {
432                         struct ptlrpcd_ctl *pc;
433
434                         pc = &ptlrpcd_scopes[i].pscope_thread[j].pt_ctl;
435
436                         if (cfs_test_bit(LIOD_START, &pc->pc_flags))
437                                 ptlrpcd_stop(pc, 0);
438                 }
439         }
440         EXIT;
441 }
442
443 int ptlrpcd_addref(void)
444 {
445         int rc = 0;
446         int i;
447         int j;
448         ENTRY;
449
450         cfs_mutex_down(&ptlrpcd_sem);
451         if (++ptlrpcd_users == 1) {
452                 for (i = 0; rc == 0 && i < PSCOPE_NR; ++i) {
453                         for (j = 0; rc == 0 && j < PT_NR; ++j) {
454                                 struct ptlrpcd_thread *pt;
455                                 struct ptlrpcd_ctl    *pc;
456
457                                 pt = &ptlrpcd_scopes[i].pscope_thread[j];
458                                 pc = &pt->pt_ctl;
459                                 if (j == PT_RECOVERY)
460                                         cfs_set_bit(LIOD_RECOVERY, &pc->pc_flags);
461                                 rc = ptlrpcd_start(pt->pt_name, pc);
462                         }
463                 }
464                 if (rc != 0) {
465                         --ptlrpcd_users;
466                         ptlrpcd_fini();
467                 }
468         }
469         cfs_mutex_up(&ptlrpcd_sem);
470         RETURN(rc);
471 }
472
473 void ptlrpcd_decref(void)
474 {
475         cfs_mutex_down(&ptlrpcd_sem);
476         if (--ptlrpcd_users == 0)
477                 ptlrpcd_fini();
478         cfs_mutex_up(&ptlrpcd_sem);
479 }