Whamcloud - gitweb
Don't flag the ptlrpcd thread with PF_MEMALLOC (from 1.0.4).
[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  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author Peter Braam <braam@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #define DEBUG_SUBSYSTEM S_RPC
25
26 #ifdef __KERNEL__
27 # include <linux/version.h>
28 # include <linux/module.h>
29 # include <linux/mm.h>
30 # include <linux/highmem.h>
31 # include <linux/lustre_dlm.h>
32 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
33 #  include <linux/workqueue.h>
34 #  include <linux/smp_lock.h>
35 # else
36 #  include <linux/locks.h>
37 # endif
38 #else /* __KERNEL__ */
39 # include <liblustre.h>
40 #endif
41
42 #include <linux/kp30.h>
43 #include <linux/lustre_net.h>
44
45 #ifndef  __CYGWIN__
46 # include <linux/ctype.h>
47 # include <linux/init.h>
48 #else
49 # include <ctype.h>
50 #endif
51
52 #include <linux/lustre_ha.h>
53 #include <linux/obd_support.h> /* for OBD_FAIL_CHECK */
54 #include <linux/lprocfs_status.h>
55
56 #define LIOD_STOP 0
57 static struct ptlrpcd_ctl {
58         unsigned long             pc_flags;
59         spinlock_t                pc_lock;
60         struct completion         pc_starting;
61         struct completion         pc_finishing;
62         struct list_head          pc_req_list;
63         wait_queue_head_t         pc_waitq;
64         struct ptlrpc_request_set *pc_set;
65 } ptlrpcd_pc;
66
67 static DECLARE_MUTEX(ptlrpcd_sem);
68 static int ptlrpcd_users = 0;
69
70 void ptlrpcd_wake(void)
71 {
72         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
73         wake_up(&pc->pc_waitq);
74 }
75
76 void ptlrpcd_add_req(struct ptlrpc_request *req)
77 {
78         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
79
80         ptlrpc_set_add_new_req(pc->pc_set, req);
81         ptlrpcd_wake();
82 }
83
84 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
85 {
86         struct list_head *tmp, *pos;
87         struct ptlrpc_request *req;
88         unsigned long flags;
89         int rc = 0;
90         ENTRY;
91
92         if (test_bit(LIOD_STOP, &pc->pc_flags))
93                 RETURN(1);
94
95         spin_lock_irqsave(&pc->pc_set->set_new_req_lock, flags);
96         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
97                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
98                 list_del_init(&req->rq_set_chain);
99                 ptlrpc_set_add_req(pc->pc_set, req);
100                 rc = 1; /* need to calculate its timeout */
101         }
102         spin_unlock_irqrestore(&pc->pc_set->set_new_req_lock, flags);
103
104         if (pc->pc_set->set_remaining) {
105                 rc = rc | ptlrpc_check_set(pc->pc_set);
106
107                 /* XXX our set never completes, so we prune the completed
108                  * reqs after each iteration. boy could this be smarter. */
109                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
110                         req = list_entry(pos, struct ptlrpc_request,
111                                          rq_set_chain);
112                         if (req->rq_phase != RQ_PHASE_COMPLETE)
113                                 continue;
114
115                         list_del_init(&req->rq_set_chain);
116                         req->rq_set = NULL;
117                         ptlrpc_req_finished (req);
118                 }
119         }
120
121         RETURN(rc);
122 }
123
124 #ifdef __KERNEL__
125 /* ptlrpc's code paths like to execute in process context, so we have this
126  * thread which spins on a set which contains the io rpcs.  llite specifies
127  * ptlrpcd's set when it pushes pages down into the oscs */
128 static int ptlrpcd(void *arg)
129 {
130         struct ptlrpcd_ctl *pc = arg;
131         unsigned long flags;
132         ENTRY;
133
134         kportal_daemonize("ptlrpcd");
135
136         SIGNAL_MASK_LOCK(current, flags);
137         sigfillset(&current->blocked);
138         RECALC_SIGPENDING;
139         SIGNAL_MASK_UNLOCK(current, flags);
140
141         complete(&pc->pc_starting);
142
143         /* this mainloop strongly resembles ptlrpc_set_wait except
144          * that our set never completes.  ptlrpcd_check calls ptlrpc_check_set
145          * when there are requests in the set.  new requests come in
146          * on the set's new_req_list and ptlrpcd_check moves them into
147          * the set. */
148         while (1) {
149                 wait_queue_t set_wait;
150                 struct l_wait_info lwi;
151                 int timeout;
152
153                 timeout = ptlrpc_set_next_timeout(pc->pc_set) * HZ;
154                 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
155
156                 /* ala the pinger, wait on pc's waitqueue and the set's */
157                 init_waitqueue_entry(&set_wait, current);
158                 add_wait_queue(&pc->pc_set->set_waitq, &set_wait);
159                 l_wait_event(pc->pc_waitq, ptlrpcd_check(pc), &lwi);
160                 remove_wait_queue(&pc->pc_set->set_waitq, &set_wait);
161
162                 if (test_bit(LIOD_STOP, &pc->pc_flags))
163                         break;
164         }
165         /* XXX should be making sure we don't have anything in flight */
166         complete(&pc->pc_finishing);
167         return 0;
168 }
169 #else
170 static int ptlrpcd_recurred = 0;
171 static void *ptlrpcd_callback;
172
173 int ptlrpcd_check_async_rpcs(void *arg)
174 {
175         struct ptlrpcd_ctl *pc = arg;
176         int                  rc = 0;
177
178         /* single threaded!! */
179         ptlrpcd_recurred++;
180
181         if (ptlrpcd_recurred == 1)
182                 rc = ptlrpcd_check(pc);
183
184         ptlrpcd_recurred--;
185         return rc;
186 }
187 #endif
188
189 int ptlrpcd_addref(void)
190 {
191         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
192         int rc = 0;
193         ENTRY;
194
195         down(&ptlrpcd_sem);
196         if (++ptlrpcd_users != 1)
197                 GOTO(out, rc);
198
199         memset(pc, 0, sizeof(*pc));
200         init_completion(&pc->pc_starting);
201         init_completion(&pc->pc_finishing);
202         init_waitqueue_head(&pc->pc_waitq);
203         pc->pc_flags = 0;
204         spin_lock_init(&pc->pc_lock);
205         INIT_LIST_HEAD(&pc->pc_req_list);
206
207         pc->pc_set = ptlrpc_prep_set();
208         if (pc->pc_set == NULL)
209                 GOTO(out, rc = -ENOMEM);
210
211 #ifdef __KERNEL__
212         if (kernel_thread(ptlrpcd, pc, 0) < 0)  {
213                 ptlrpc_set_destroy(pc->pc_set);
214                 GOTO(out, rc = -ECHILD);
215         }
216
217         wait_for_completion(&pc->pc_starting);
218 #else
219         ptlrpcd_callback =
220                 liblustre_register_wait_callback(&ptlrpcd_check_async_rpcs, pc);
221 #endif
222 out:
223         up(&ptlrpcd_sem);
224         RETURN(rc);
225 }
226
227 void ptlrpcd_decref(void)
228 {
229         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
230
231         down(&ptlrpcd_sem);
232         if (--ptlrpcd_users == 0) {
233                 set_bit(LIOD_STOP, &pc->pc_flags);
234                 wake_up(&pc->pc_waitq);
235 #ifdef __KERNEL__
236                 wait_for_completion(&pc->pc_finishing);
237 #else
238                 liblustre_deregister_wait_callback(ptlrpcd_callback);
239 #endif
240                 ptlrpc_set_destroy(pc->pc_set);
241         }
242         up(&ptlrpcd_sem);
243 }