[PATCH] smbfs: Make conn_pid a struct pid
[linux-2.6.git] / fs / 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/slab.h>
16 #include <linux/init.h>
17 #include <linux/file.h>
18 #include <linux/dcache.h>
19 #include <linux/smp_lock.h>
20 #include <linux/module.h>
21 #include <linux/net.h>
22 #include <linux/kthread.h>
23 #include <net/ip.h>
24
25 #include <linux/smb_fs.h>
26 #include <linux/smbno.h>
27 #include <linux/smb_mount.h>
28
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31
32 #include "smb_debug.h"
33 #include "request.h"
34 #include "proto.h"
35
36 enum smbiod_state {
37         SMBIOD_DEAD,
38         SMBIOD_STARTING,
39         SMBIOD_RUNNING,
40 };
41
42 static enum smbiod_state smbiod_state = SMBIOD_DEAD;
43 static struct task_struct *smbiod_thread;
44 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
45 static LIST_HEAD(smb_servers);
46 static DEFINE_SPINLOCK(servers_lock);
47
48 #define SMBIOD_DATA_READY       (1<<0)
49 static long smbiod_flags;
50
51 static int smbiod(void *);
52 static int smbiod_start(void);
53
54 /*
55  * called when there's work for us to do
56  */
57 void smbiod_wake_up(void)
58 {
59         if (smbiod_state == SMBIOD_DEAD)
60                 return;
61         set_bit(SMBIOD_DATA_READY, &smbiod_flags);
62         wake_up_interruptible(&smbiod_wait);
63 }
64
65 /*
66  * start smbiod if none is running
67  */
68 static int smbiod_start(void)
69 {
70         struct task_struct *tsk;
71         int err = 0;
72
73         if (smbiod_state != SMBIOD_DEAD)
74                 return 0;
75         smbiod_state = SMBIOD_STARTING;
76         __module_get(THIS_MODULE);
77         spin_unlock(&servers_lock);
78         tsk = kthread_run(smbiod, NULL, "smbiod");
79         if (IS_ERR(tsk)) {
80                 err = PTR_ERR(tsk);
81                 module_put(THIS_MODULE);
82         }
83
84         spin_lock(&servers_lock);
85         if (err < 0) {
86                 smbiod_state = SMBIOD_DEAD;
87                 smbiod_thread = NULL;
88         } else {
89                 smbiod_state = SMBIOD_RUNNING;
90                 smbiod_thread = tsk;
91         }
92         return err;
93 }
94
95 /*
96  * register a server & start smbiod if necessary
97  */
98 int smbiod_register_server(struct smb_sb_info *server)
99 {
100         int ret;
101         spin_lock(&servers_lock);
102         list_add(&server->entry, &smb_servers);
103         VERBOSE("%p\n", server);
104         ret = smbiod_start();
105         spin_unlock(&servers_lock);
106         return ret;
107 }
108
109 /*
110  * Unregister a server
111  * Must be called with the server lock held.
112  */
113 void smbiod_unregister_server(struct smb_sb_info *server)
114 {
115         spin_lock(&servers_lock);
116         list_del_init(&server->entry);
117         VERBOSE("%p\n", server);
118         spin_unlock(&servers_lock);
119
120         smbiod_wake_up();
121         smbiod_flush(server);
122 }
123
124 void smbiod_flush(struct smb_sb_info *server)
125 {
126         struct list_head *tmp, *n;
127         struct smb_request *req;
128
129         list_for_each_safe(tmp, n, &server->xmitq) {
130                 req = list_entry(tmp, struct smb_request, rq_queue);
131                 req->rq_errno = -EIO;
132                 list_del_init(&req->rq_queue);
133                 smb_rput(req);
134                 wake_up_interruptible(&req->rq_wait);
135         }
136         list_for_each_safe(tmp, n, &server->recvq) {
137                 req = list_entry(tmp, struct smb_request, rq_queue);
138                 req->rq_errno = -EIO;
139                 list_del_init(&req->rq_queue);
140                 smb_rput(req);
141                 wake_up_interruptible(&req->rq_wait);
142         }
143 }
144
145 /*
146  * Wake up smbmount and make it reconnect to the server.
147  * This must be called with the server locked.
148  *
149  * FIXME: add smbconnect version to this
150  */
151 int smbiod_retry(struct smb_sb_info *server)
152 {
153         struct list_head *head;
154         struct smb_request *req;
155         struct pid *pid = get_pid(server->conn_pid);
156         int result = 0;
157
158         VERBOSE("state: %d\n", server->state);
159         if (server->state == CONN_VALID || server->state == CONN_RETRYING)
160                 goto out;
161
162         smb_invalidate_inodes(server);
163
164         /*
165          * Some requests are meaningless after a retry, so we abort them.
166          * One example are all requests using 'fileid' since the files are
167          * closed on retry.
168          */
169         head = server->xmitq.next;
170         while (head != &server->xmitq) {
171                 req = list_entry(head, struct smb_request, rq_queue);
172                 head = head->next;
173
174                 req->rq_bytes_sent = 0;
175                 if (req->rq_flags & SMB_REQ_NORETRY) {
176                         VERBOSE("aborting request %p on xmitq\n", req);
177                         req->rq_errno = -EIO;
178                         list_del_init(&req->rq_queue);
179                         smb_rput(req);
180                         wake_up_interruptible(&req->rq_wait);
181                 }
182         }
183
184         /*
185          * FIXME: test the code for retrying request we already sent
186          */
187         head = server->recvq.next;
188         while (head != &server->recvq) {
189                 req = list_entry(head, struct smb_request, rq_queue);
190                 head = head->next;
191 #if 0
192                 if (req->rq_flags & SMB_REQ_RETRY) {
193                         /* must move the request to the xmitq */
194                         VERBOSE("retrying request %p on recvq\n", req);
195                         list_move(&req->rq_queue, &server->xmitq);
196                         continue;
197                 }
198 #endif
199
200                 VERBOSE("aborting request %p on recvq\n", req);
201                 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
202                 req->rq_errno = -EIO;
203                 list_del_init(&req->rq_queue);
204                 smb_rput(req);
205                 wake_up_interruptible(&req->rq_wait);
206         }
207
208         smb_close_socket(server);
209
210         if (pid == 0) {
211                 /* FIXME: this is fatal, umount? */
212                 printk(KERN_ERR "smb_retry: no connection process\n");
213                 server->state = CONN_RETRIED;
214                 goto out;
215         }
216
217         /*
218          * Change state so that only one retry per server will be started.
219          */
220         server->state = CONN_RETRYING;
221
222         /*
223          * Note: use the "priv" flag, as a user process may need to reconnect.
224          */
225         result = kill_pid(pid, SIGUSR1, 1);
226         if (result) {
227                 /* FIXME: this is most likely fatal, umount? */
228                 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
229                 goto out;
230         }
231         VERBOSE("signalled pid %d\n", pid);
232
233         /* FIXME: The retried requests should perhaps get a "time boost". */
234
235 out:
236         put_pid(pid);
237         return result;
238 }
239
240 /*
241  * Currently handles lockingX packets.
242  */
243 static 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  */
252 static 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
293 out:
294         return;
295 }
296
297 /*
298  * smbiod kernel thread
299  */
300 static int smbiod(void *unused)
301 {
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 }