[TCP]: Restore SKB socket owner setting in tcp_transmit_skb().
[linux-2.6/x86.git] / fs / smbfs / smbiod.c
blob89eaf31f1d46c18a42870c604d4b58ced724bfa5
1 /*
2 * smbiod.c
4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
6 */
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>
25 #include <linux/smb_fs.h>
26 #include <linux/smbno.h>
27 #include <linux/smb_mount.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
32 #include "smb_debug.h"
33 #include "request.h"
34 #include "proto.h"
36 enum smbiod_state {
37 SMBIOD_DEAD,
38 SMBIOD_STARTING,
39 SMBIOD_RUNNING,
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);
48 #define SMBIOD_DATA_READY (1<<0)
49 static long smbiod_flags;
51 static int smbiod(void *);
52 static int smbiod_start(void);
55 * called when there's work for us to do
57 void smbiod_wake_up(void)
59 if (smbiod_state == SMBIOD_DEAD)
60 return;
61 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
62 wake_up_interruptible(&smbiod_wait);
66 * start smbiod if none is running
68 static int smbiod_start(void)
70 struct task_struct *tsk;
71 int err = 0;
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);
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;
92 return err;
96 * register a server & start smbiod if necessary
98 int smbiod_register_server(struct smb_sb_info *server)
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;
110 * Unregister a server
111 * Must be called with the server lock held.
113 void smbiod_unregister_server(struct smb_sb_info *server)
115 spin_lock(&servers_lock);
116 list_del_init(&server->entry);
117 VERBOSE("%p\n", server);
118 spin_unlock(&servers_lock);
120 smbiod_wake_up();
121 smbiod_flush(server);
124 void smbiod_flush(struct smb_sb_info *server)
126 struct list_head *tmp, *n;
127 struct smb_request *req;
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);
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);
146 * Wake up smbmount and make it reconnect to the server.
147 * This must be called with the server locked.
149 * FIXME: add smbconnect version to this
151 int smbiod_retry(struct smb_sb_info *server)
153 struct list_head *head;
154 struct smb_request *req;
155 struct pid *pid = get_pid(server->conn_pid);
156 int result = 0;
158 VERBOSE("state: %d\n", server->state);
159 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
160 goto out;
162 smb_invalidate_inodes(server);
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.
169 head = server->xmitq.next;
170 while (head != &server->xmitq) {
171 req = list_entry(head, struct smb_request, rq_queue);
172 head = head->next;
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);
185 * FIXME: test the code for retrying request we already sent
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;
198 #endif
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);
208 smb_close_socket(server);
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;
218 * Change state so that only one retry per server will be started.
220 server->state = CONN_RETRYING;
223 * Note: use the "priv" flag, as a user process may need to reconnect.
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;
231 VERBOSE("signalled pid %d\n", pid);
233 /* FIXME: The retried requests should perhaps get a "time boost". */
235 out:
236 put_pid(pid);
237 return result;
241 * Currently handles lockingX packets.
243 static void smbiod_handle_request(struct smb_sb_info *server)
245 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
246 server->rstate = SMB_RECV_DROP;
250 * Do some IO for one server.
252 static void smbiod_doio(struct smb_sb_info *server)
254 int result;
255 int maxwork = 7;
257 if (server->state != CONN_VALID)
258 goto out;
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);
271 * If there is more to read then we want to be sure to wake up again.
273 if (server->state != CONN_VALID)
274 goto out;
275 if (smb_recv_available(server) > 0)
276 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
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 */
285 } while (result > 0);
288 * If the last request was not sent out we want to wake up again.
290 if (!list_empty(&server->xmitq))
291 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
293 out:
294 return;
298 * smbiod kernel thread
300 static int smbiod(void *unused)
302 allow_signal(SIGKILL);
304 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
306 for (;;) {
307 struct smb_sb_info *server;
308 struct list_head *pos, *n;
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;
320 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
322 spin_lock(&servers_lock);
323 if (list_empty(&smb_servers)) {
324 smbiod_state = SMBIOD_DEAD;
325 spin_unlock(&servers_lock);
326 break;
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);
333 if (server->state == CONN_VALID) {
334 spin_unlock(&servers_lock);
336 smb_lock_server(server);
337 smbiod_doio(server);
338 smb_unlock_server(server);
340 spin_lock(&servers_lock);
343 spin_unlock(&servers_lock);
346 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
347 module_put_and_exit(0);