[PATCH] fs: use list_move()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / smbfs / smbiod.c
CommitLineData
1da177e4
LT
1/*
2 * smbiod.c
3 *
4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
6 */
7
8#include <linux/config.h>
9
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/mm.h>
13#include <linux/string.h>
14#include <linux/stat.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/file.h>
19#include <linux/dcache.h>
20#include <linux/smp_lock.h>
21#include <linux/module.h>
22#include <linux/net.h>
fa366ad5 23#include <linux/kthread.h>
1da177e4
LT
24#include <net/ip.h>
25
26#include <linux/smb_fs.h>
27#include <linux/smbno.h>
28#include <linux/smb_mount.h>
29
30#include <asm/system.h>
31#include <asm/uaccess.h>
32
33#include "smb_debug.h"
34#include "request.h"
35#include "proto.h"
36
37enum smbiod_state {
38 SMBIOD_DEAD,
39 SMBIOD_STARTING,
40 SMBIOD_RUNNING,
41};
42
43static enum smbiod_state smbiod_state = SMBIOD_DEAD;
fa366ad5 44static struct task_struct *smbiod_thread;
1da177e4
LT
45static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
46static LIST_HEAD(smb_servers);
47static DEFINE_SPINLOCK(servers_lock);
48
49#define SMBIOD_DATA_READY (1<<0)
50static long smbiod_flags;
51
52static int smbiod(void *);
53static int smbiod_start(void);
54
55/*
56 * called when there's work for us to do
57 */
58void smbiod_wake_up(void)
59{
60 if (smbiod_state == SMBIOD_DEAD)
61 return;
62 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
63 wake_up_interruptible(&smbiod_wait);
64}
65
66/*
67 * start smbiod if none is running
68 */
69static int smbiod_start(void)
70{
fa366ad5
SH
71 struct task_struct *tsk;
72 int err = 0;
73
1da177e4
LT
74 if (smbiod_state != SMBIOD_DEAD)
75 return 0;
76 smbiod_state = SMBIOD_STARTING;
77 __module_get(THIS_MODULE);
78 spin_unlock(&servers_lock);
fa366ad5
SH
79 tsk = kthread_run(smbiod, NULL, "smbiod");
80 if (IS_ERR(tsk)) {
81 err = PTR_ERR(tsk);
1da177e4 82 module_put(THIS_MODULE);
fa366ad5 83 }
1da177e4
LT
84
85 spin_lock(&servers_lock);
fa366ad5
SH
86 if (err < 0) {
87 smbiod_state = SMBIOD_DEAD;
88 smbiod_thread = NULL;
89 } else {
90 smbiod_state = SMBIOD_RUNNING;
91 smbiod_thread = tsk;
92 }
93 return err;
1da177e4
LT
94}
95
96/*
97 * register a server & start smbiod if necessary
98 */
99int smbiod_register_server(struct smb_sb_info *server)
100{
101 int ret;
102 spin_lock(&servers_lock);
103 list_add(&server->entry, &smb_servers);
104 VERBOSE("%p\n", server);
105 ret = smbiod_start();
106 spin_unlock(&servers_lock);
107 return ret;
108}
109
110/*
111 * Unregister a server
112 * Must be called with the server lock held.
113 */
114void smbiod_unregister_server(struct smb_sb_info *server)
115{
116 spin_lock(&servers_lock);
117 list_del_init(&server->entry);
118 VERBOSE("%p\n", server);
119 spin_unlock(&servers_lock);
120
121 smbiod_wake_up();
122 smbiod_flush(server);
123}
124
125void smbiod_flush(struct smb_sb_info *server)
126{
127 struct list_head *tmp, *n;
128 struct smb_request *req;
129
130 list_for_each_safe(tmp, n, &server->xmitq) {
131 req = list_entry(tmp, struct smb_request, rq_queue);
132 req->rq_errno = -EIO;
133 list_del_init(&req->rq_queue);
134 smb_rput(req);
135 wake_up_interruptible(&req->rq_wait);
136 }
137 list_for_each_safe(tmp, n, &server->recvq) {
138 req = list_entry(tmp, struct smb_request, rq_queue);
139 req->rq_errno = -EIO;
140 list_del_init(&req->rq_queue);
141 smb_rput(req);
142 wake_up_interruptible(&req->rq_wait);
143 }
144}
145
146/*
147 * Wake up smbmount and make it reconnect to the server.
148 * This must be called with the server locked.
149 *
150 * FIXME: add smbconnect version to this
151 */
152int smbiod_retry(struct smb_sb_info *server)
153{
154 struct list_head *head;
155 struct smb_request *req;
156 pid_t pid = server->conn_pid;
157 int result = 0;
158
159 VERBOSE("state: %d\n", server->state);
160 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
161 goto out;
162
163 smb_invalidate_inodes(server);
164
165 /*
166 * Some requests are meaningless after a retry, so we abort them.
167 * One example are all requests using 'fileid' since the files are
168 * closed on retry.
169 */
170 head = server->xmitq.next;
171 while (head != &server->xmitq) {
172 req = list_entry(head, struct smb_request, rq_queue);
173 head = head->next;
174
175 req->rq_bytes_sent = 0;
176 if (req->rq_flags & SMB_REQ_NORETRY) {
177 VERBOSE("aborting request %p on xmitq\n", req);
178 req->rq_errno = -EIO;
179 list_del_init(&req->rq_queue);
180 smb_rput(req);
181 wake_up_interruptible(&req->rq_wait);
182 }
183 }
184
185 /*
186 * FIXME: test the code for retrying request we already sent
187 */
188 head = server->recvq.next;
189 while (head != &server->recvq) {
190 req = list_entry(head, struct smb_request, rq_queue);
191 head = head->next;
192#if 0
193 if (req->rq_flags & SMB_REQ_RETRY) {
194 /* must move the request to the xmitq */
195 VERBOSE("retrying request %p on recvq\n", req);
f116629d 196 list_move(&req->rq_queue, &server->xmitq);
1da177e4
LT
197 continue;
198 }
199#endif
200
201 VERBOSE("aborting request %p on recvq\n", req);
202 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
203 req->rq_errno = -EIO;
204 list_del_init(&req->rq_queue);
205 smb_rput(req);
206 wake_up_interruptible(&req->rq_wait);
207 }
208
209 smb_close_socket(server);
210
211 if (pid == 0) {
212 /* FIXME: this is fatal, umount? */
213 printk(KERN_ERR "smb_retry: no connection process\n");
214 server->state = CONN_RETRIED;
215 goto out;
216 }
217
218 /*
219 * Change state so that only one retry per server will be started.
220 */
221 server->state = CONN_RETRYING;
222
223 /*
224 * Note: use the "priv" flag, as a user process may need to reconnect.
225 */
226 result = kill_proc(pid, SIGUSR1, 1);
227 if (result) {
228 /* FIXME: this is most likely fatal, umount? */
229 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
230 goto out;
231 }
232 VERBOSE("signalled pid %d\n", pid);
233
234 /* FIXME: The retried requests should perhaps get a "time boost". */
235
236out:
237 return result;
238}
239
240/*
241 * Currently handles lockingX packets.
242 */
243static void smbiod_handle_request(struct smb_sb_info *server)
244{
245 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
246 server->rstate = SMB_RECV_DROP;
247}
248
249/*
250 * Do some IO for one server.
251 */
252static void smbiod_doio(struct smb_sb_info *server)
253{
254 int result;
255 int maxwork = 7;
256
257 if (server->state != CONN_VALID)
258 goto out;
259
260 do {
261 result = smb_request_recv(server);
262 if (result < 0) {
263 server->state = CONN_INVALID;
264 smbiod_retry(server);
265 goto out; /* reconnecting is slow */
266 } else if (server->rstate == SMB_RECV_REQUEST)
267 smbiod_handle_request(server);
268 } while (result > 0 && maxwork-- > 0);
269
270 /*
271 * If there is more to read then we want to be sure to wake up again.
272 */
273 if (server->state != CONN_VALID)
274 goto out;
275 if (smb_recv_available(server) > 0)
276 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
277
278 do {
279 result = smb_request_send_server(server);
280 if (result < 0) {
281 server->state = CONN_INVALID;
282 smbiod_retry(server);
283 goto out; /* reconnecting is slow */
284 }
285 } while (result > 0);
286
287 /*
288 * If the last request was not sent out we want to wake up again.
289 */
290 if (!list_empty(&server->xmitq))
291 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
292
293out:
294 return;
295}
296
297/*
298 * smbiod kernel thread
299 */
300static int smbiod(void *unused)
301{
1da177e4
LT
302 allow_signal(SIGKILL);
303
304 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
305
306 for (;;) {
307 struct smb_sb_info *server;
308 struct list_head *pos, *n;
309
310 /* FIXME: Use poll? */
311 wait_event_interruptible(smbiod_wait,
312 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
313 if (signal_pending(current)) {
314 spin_lock(&servers_lock);
315 smbiod_state = SMBIOD_DEAD;
316 spin_unlock(&servers_lock);
317 break;
318 }
319
320 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
321
322 spin_lock(&servers_lock);
323 if (list_empty(&smb_servers)) {
324 smbiod_state = SMBIOD_DEAD;
325 spin_unlock(&servers_lock);
326 break;
327 }
328
329 list_for_each_safe(pos, n, &smb_servers) {
330 server = list_entry(pos, struct smb_sb_info, entry);
331 VERBOSE("checking server %p\n", server);
332
333 if (server->state == CONN_VALID) {
334 spin_unlock(&servers_lock);
335
336 smb_lock_server(server);
337 smbiod_doio(server);
338 smb_unlock_server(server);
339
340 spin_lock(&servers_lock);
341 }
342 }
343 spin_unlock(&servers_lock);
344 }
345
346 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
347 module_put_and_exit(0);
348}