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