Whamcloud - gitweb
a52e62bee6c8f39da99bca85504a00e786cf1a57
[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 struct semaphore 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  * Requests that are added to the ptlrpcd queue are sent via
107  * ptlrpcd_check->ptlrpc_check_set().
108  */
109 void ptlrpcd_add_req(struct ptlrpc_request *req, enum ptlrpcd_scope scope)
110 {
111         struct ptlrpcd_ctl *pc;
112         enum pscope_thread  pt;
113         int rc;
114
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);
119         /*
120          * XXX disable this for CLIO: environment is needed for interpreter.
121          */
122         if (rc && 0) {
123                 ptlrpc_interpterer_t interpreter;
124
125                 interpreter = req->rq_interpret_reply;
126
127                 /*
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
132                  * resources.
133                  */
134                 req->rq_status = -EBADR;
135                 interpreter(NULL, req, &req->rq_async_args,
136                             req->rq_status);
137                 req->rq_set = NULL;
138                 ptlrpc_req_finished(req);
139         }
140 }
141
142 static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
143 {
144         struct list_head *tmp, *pos;
145         struct ptlrpc_request *req;
146         int rc = 0;
147         ENTRY;
148
149         spin_lock(&pc->pc_set->set_new_req_lock);
150         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
151                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
152                 list_del_init(&req->rq_set_chain);
153                 ptlrpc_set_add_req(pc->pc_set, req);
154                 /*
155                  * Need to calculate its timeout.
156                  */
157                 rc = 1;
158         }
159         spin_unlock(&pc->pc_set->set_new_req_lock);
160
161         if (pc->pc_set->set_remaining) {
162                 rc = rc | ptlrpc_check_set(env, pc->pc_set);
163
164                 /*
165                  * XXX: our set never completes, so we prune the completed
166                  * reqs after each iteration. boy could this be smarter.
167                  */
168                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
169                         req = list_entry(pos, struct ptlrpc_request,
170                                          rq_set_chain);
171                         if (req->rq_phase != RQ_PHASE_COMPLETE)
172                                 continue;
173
174                         list_del_init(&req->rq_set_chain);
175                         req->rq_set = NULL;
176                         ptlrpc_req_finished (req);
177                 }
178         }
179
180         if (rc == 0) {
181                 /*
182                  * If new requests have been added, make sure to wake up.
183                  */
184                 spin_lock(&pc->pc_set->set_new_req_lock);
185                 rc = !list_empty(&pc->pc_set->set_new_requests);
186                 spin_unlock(&pc->pc_set->set_new_req_lock);
187         }
188
189         RETURN(rc);
190 }
191
192 #ifdef __KERNEL__
193 /*
194  * ptlrpc's code paths like to execute in process context, so we have this
195  * thread which spins on a set which contains the io rpcs. llite specifies
196  * ptlrpcd's set when it pushes pages down into the oscs.
197  */
198 static int ptlrpcd(void *arg)
199 {
200         struct ptlrpcd_ctl *pc = arg;
201         struct lu_env env = { .le_ses = NULL };
202         int rc, exit = 0;
203         ENTRY;
204
205         rc = cfs_daemonize_ctxt(pc->pc_name);
206         if (rc == 0) {
207                 /*
208                  * XXX So far only "client" ptlrpcd uses an environment. In
209                  * the future, ptlrpcd thread (or a thread-set) has to given
210                  * an argument, describing its "scope".
211                  */
212                 rc = lu_context_init(&env.le_ctx,
213                                      LCT_CL_THREAD|LCT_REMEMBER|LCT_NOREF);
214         }
215
216         complete(&pc->pc_starting);
217
218         if (rc != 0)
219                 RETURN(rc);
220         env.le_ctx.lc_cookie = 0x7;
221
222         /*
223          * This mainloop strongly resembles ptlrpc_set_wait() except that our
224          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
225          * there are requests in the set. New requests come in on the set's
226          * new_req_list and ptlrpcd_check() moves them into the set.
227          */
228         do {
229                 struct l_wait_info lwi;
230                 int timeout;
231
232                 rc = lu_env_refill(&env);
233                 if (rc != 0) {
234                         /*
235                          * XXX This is very awkward situation, because
236                          * execution can neither continue (request
237                          * interpreters assume that env is set up), nor repeat
238                          * the loop (as this potentially results in a tight
239                          * loop of -ENOMEM's).
240                          *
241                          * Fortunately, refill only ever does something when
242                          * new modules are loaded, i.e., early during boot up.
243                          */
244                         CERROR("Failure to refill session: %d\n", rc);
245                         continue;
246                 }
247
248                 timeout = ptlrpc_set_next_timeout(pc->pc_set);
249                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
250                                   ptlrpc_expired_set, pc->pc_set);
251
252                 lu_context_enter(&env.le_ctx);
253                 l_wait_event(pc->pc_set->set_waitq,
254                              ptlrpcd_check(&env, pc), &lwi);
255                 lu_context_exit(&env.le_ctx);
256
257                 /*
258                  * Abort inflight rpcs for forced stop case.
259                  */
260                 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
261                         if (test_bit(LIOD_FORCE, &pc->pc_flags))
262                                 ptlrpc_abort_set(pc->pc_set);
263                         exit++;
264                 }
265
266                 /* 
267                  * Let's make one more loop to make sure that ptlrpcd_check()
268                  * copied all raced new rpcs into the set so we can kill them.
269                  */
270         } while (exit < 2);
271
272         /*
273          * Wait for inflight requests to drain.
274          */
275         if (!list_empty(&pc->pc_set->set_requests))
276                 ptlrpc_set_wait(pc->pc_set);
277         lu_context_fini(&env.le_ctx);
278         complete(&pc->pc_finishing);
279
280         clear_bit(LIOD_START, &pc->pc_flags);
281         clear_bit(LIOD_STOP, &pc->pc_flags);
282         clear_bit(LIOD_FORCE, &pc->pc_flags);
283         return 0;
284 }
285
286 #else /* !__KERNEL__ */
287
288 int ptlrpcd_check_async_rpcs(void *arg)
289 {
290         struct ptlrpcd_ctl *pc = arg;
291         int                 rc = 0;
292
293         /*
294          * Single threaded!!
295          */
296         pc->pc_recurred++;
297
298         if (pc->pc_recurred == 1) {
299                 lu_context_enter(&pc->pc_env.le_ctx);
300                 rc = ptlrpcd_check(&pc->pc_env, pc);
301                 lu_context_exit(&pc->pc_env.le_ctx);
302                 if (!rc)
303                         ptlrpc_expired_set(pc->pc_set);
304                 /*
305                  * XXX: send replay requests.
306                  */
307                 if (test_bit(LIOD_RECOVERY, &pc->pc_flags))
308                         rc = ptlrpcd_check(&pc->pc_env, pc);
309         }
310
311         pc->pc_recurred--;
312         return rc;
313 }
314
315 int ptlrpcd_idle(void *arg)
316 {
317         struct ptlrpcd_ctl *pc = arg;
318
319         return (list_empty(&pc->pc_set->set_new_requests) &&
320                 pc->pc_set->set_remaining == 0);
321 }
322
323 #endif
324
325 int ptlrpcd_start(const char *name, struct ptlrpcd_ctl *pc)
326 {
327         int rc;
328         ENTRY;
329
330         /*
331          * Do not allow start second thread for one pc.
332          */
333         if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
334                 CERROR("Starting second thread (%s) for same pc %p\n",
335                        name, pc);
336                 RETURN(-EALREADY);
337         }
338
339         init_completion(&pc->pc_starting);
340         init_completion(&pc->pc_finishing);
341         spin_lock_init(&pc->pc_lock);
342         snprintf (pc->pc_name, sizeof (pc->pc_name), name);
343         pc->pc_set = ptlrpc_prep_set();
344         if (pc->pc_set == NULL)
345                 GOTO(out, rc = -ENOMEM);
346         /*
347          * So far only "client" ptlrpcd uses an environment. In the future,
348          * ptlrpcd thread (or a thread-set) has to be given an argument,
349          * describing its "scope".
350          */
351         rc = lu_context_init(&pc->pc_env.le_ctx, LCT_CL_THREAD|LCT_REMEMBER);
352         if (rc != 0) {
353                 ptlrpc_set_destroy(pc->pc_set);
354                 GOTO(out, rc);
355         }
356
357 #ifdef __KERNEL__
358         rc = cfs_kernel_thread(ptlrpcd, pc, 0);
359         if (rc < 0)  {
360                 lu_context_fini(&pc->pc_env.le_ctx);
361                 ptlrpc_set_destroy(pc->pc_set);
362                 GOTO(out, rc);
363         }
364         rc = 0;
365         wait_for_completion(&pc->pc_starting);
366 #else
367         pc->pc_wait_callback =
368                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
369                                                  &ptlrpcd_check_async_rpcs, pc);
370         pc->pc_idle_callback =
371                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
372                                                  &ptlrpcd_idle, pc);
373 #endif
374 out:
375         if (rc)
376                 clear_bit(LIOD_START, &pc->pc_flags);
377         RETURN(rc);
378 }
379
380 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
381 {
382         if (!test_bit(LIOD_START, &pc->pc_flags)) {
383                 CERROR("Thread for pc %p was not started\n", pc);
384                 return;
385         }
386
387         set_bit(LIOD_STOP, &pc->pc_flags);
388         if (force)
389                 set_bit(LIOD_FORCE, &pc->pc_flags);
390         cfs_waitq_signal(&pc->pc_set->set_waitq);
391 #ifdef __KERNEL__
392         wait_for_completion(&pc->pc_finishing);
393 #else
394         liblustre_deregister_wait_callback(pc->pc_wait_callback);
395         liblustre_deregister_idle_callback(pc->pc_idle_callback);
396 #endif
397         lu_context_fini(&pc->pc_env.le_ctx);
398         ptlrpc_set_destroy(pc->pc_set);
399 }
400
401 void ptlrpcd_fini(void)
402 {
403         int i;
404         int j;
405
406         ENTRY;
407
408         for (i = 0; i < PSCOPE_NR; ++i) {
409                 for (j = 0; j < PT_NR; ++j) {
410                         struct ptlrpcd_ctl *pc;
411
412                         pc = &ptlrpcd_scopes[i].pscope_thread[j].pt_ctl;
413
414                         if (test_bit(LIOD_START, &pc->pc_flags))
415                                 ptlrpcd_stop(pc, 0);
416                 }
417         }
418         EXIT;
419 }
420
421 int ptlrpcd_addref(void)
422 {
423         int rc = 0;
424         int i;
425         int j;
426         ENTRY;
427
428         mutex_down(&ptlrpcd_sem);
429         if (++ptlrpcd_users == 1) {
430                 for (i = 0; rc == 0 && i < PSCOPE_NR; ++i) {
431                         for (j = 0; rc == 0 && j < PT_NR; ++j) {
432                                 struct ptlrpcd_thread *pt;
433                                 struct ptlrpcd_ctl    *pc;
434
435                                 pt = &ptlrpcd_scopes[i].pscope_thread[j];
436                                 pc = &pt->pt_ctl;
437                                 if (j == PT_RECOVERY)
438                                         set_bit(LIOD_RECOVERY, &pc->pc_flags);
439                                 rc = ptlrpcd_start(pt->pt_name, pc);
440                         }
441                 }
442                 if (rc != 0) {
443                         --ptlrpcd_users;
444                         ptlrpcd_fini();
445                 }
446         }
447         mutex_up(&ptlrpcd_sem);
448         RETURN(rc);
449 }
450
451 void ptlrpcd_decref(void)
452 {
453         mutex_down(&ptlrpcd_sem);
454         if (--ptlrpcd_users == 0)
455                 ptlrpcd_fini();
456         mutex_up(&ptlrpcd_sem);
457 }