[PATCH] Add a driver for the Technisat Skystar2 DVB card
[linux-2.6/history.git] / fs / smbfs / smbiod.c
blobf09ce74b8763404439887e2d630d88ae7063d5ef
1 /*
2 * smbiod.c
4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
6 */
8 #include <linux/config.h>
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>
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 pid_t smbiod_pid;
44 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
45 static LIST_HEAD(smb_servers);
46 static spinlock_t servers_lock = SPIN_LOCK_UNLOCKED;
48 #define SMBIOD_DATA_READY (1<<0)
49 static long smbiod_flags;
51 static int smbiod(void *);
52 static void smbiod_start(void);
55 * called when there's work for us to do
57 void smbiod_wake_up()
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 void smbiod_start()
70 pid_t pid;
71 if (smbiod_state != SMBIOD_DEAD)
72 return;
73 smbiod_state = SMBIOD_STARTING;
74 spin_unlock(&servers_lock);
75 pid = kernel_thread(smbiod, NULL, 0);
77 spin_lock(&servers_lock);
78 smbiod_state = SMBIOD_RUNNING;
79 smbiod_pid = pid;
83 * register a server & start smbiod if necessary
85 void smbiod_register_server(struct smb_sb_info *server)
87 spin_lock(&servers_lock);
88 list_add(&server->entry, &smb_servers);
89 VERBOSE("%p\n", server);
90 smbiod_start();
91 spin_unlock(&servers_lock);
95 * Unregister a server
96 * Must be called with the server lock held.
98 void smbiod_unregister_server(struct smb_sb_info *server)
100 spin_lock(&servers_lock);
101 list_del_init(&server->entry);
102 VERBOSE("%p\n", server);
103 spin_unlock(&servers_lock);
105 smbiod_wake_up();
106 smbiod_flush(server);
109 void smbiod_flush(struct smb_sb_info *server)
111 struct list_head *tmp, *n;
112 struct smb_request *req;
114 list_for_each_safe(tmp, n, &server->xmitq) {
115 req = list_entry(tmp, struct smb_request, rq_queue);
116 req->rq_errno = -EIO;
117 list_del_init(&req->rq_queue);
118 smb_rput(req);
119 wake_up_interruptible(&req->rq_wait);
121 list_for_each_safe(tmp, n, &server->recvq) {
122 req = list_entry(tmp, struct smb_request, rq_queue);
123 req->rq_errno = -EIO;
124 list_del_init(&req->rq_queue);
125 smb_rput(req);
126 wake_up_interruptible(&req->rq_wait);
131 * Wake up smbmount and make it reconnect to the server.
132 * This must be called with the server locked.
134 * FIXME: add smbconnect version to this
136 int smbiod_retry(struct smb_sb_info *server)
138 struct list_head *head;
139 struct smb_request *req;
140 pid_t pid = server->conn_pid;
141 int result = 0;
143 VERBOSE("state: %d\n", server->state);
144 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
145 goto out;
147 smb_invalidate_inodes(server);
150 * Some requests are meaningless after a retry, so we abort them.
151 * One example are all requests using 'fileid' since the files are
152 * closed on retry.
154 head = server->xmitq.next;
155 while (head != &server->xmitq) {
156 req = list_entry(head, struct smb_request, rq_queue);
157 head = head->next;
158 if (req->rq_flags & SMB_REQ_NORETRY) {
159 VERBOSE("aborting request %p on xmitq\n", req);
160 req->rq_errno = -EIO;
161 list_del_init(&req->rq_queue);
162 smb_rput(req);
163 wake_up_interruptible(&req->rq_wait);
168 * FIXME: test the code for retrying request we already sent
170 head = server->recvq.next;
171 while (head != &server->recvq) {
172 req = list_entry(head, struct smb_request, rq_queue);
173 head = head->next;
174 #if 0
175 if (req->rq_flags & SMB_REQ_RETRY) {
176 /* must move the request to the xmitq */
177 VERBOSE("retrying request %p on recvq\n", req);
178 list_del(&req->rq_queue);
179 list_add(&req->rq_queue, &server->xmitq);
180 continue;
182 #endif
184 VERBOSE("aborting request %p on recvq\n", req);
185 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
186 req->rq_errno = -EIO;
187 list_del_init(&req->rq_queue);
188 smb_rput(req);
189 wake_up_interruptible(&req->rq_wait);
192 smb_close_socket(server);
194 if (pid == 0) {
195 /* FIXME: this is fatal, umount? */
196 printk(KERN_ERR "smb_retry: no connection process\n");
197 server->state = CONN_RETRIED;
198 goto out;
202 * Change state so that only one retry per server will be started.
204 server->state = CONN_RETRYING;
207 * Note: use the "priv" flag, as a user process may need to reconnect.
209 result = kill_proc(pid, SIGUSR1, 1);
210 if (result) {
211 /* FIXME: this is most likely fatal, umount? */
212 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
213 goto out;
215 VERBOSE("signalled pid %d\n", pid);
217 /* FIXME: The retried requests should perhaps get a "time boost". */
219 out:
220 return result;
224 * Currently handles lockingX packets.
226 static void smbiod_handle_request(struct smb_sb_info *server)
228 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
229 server->rstate = SMB_RECV_DROP;
233 * Do some IO for one server.
235 static void smbiod_doio(struct smb_sb_info *server)
237 int result;
238 int maxwork = 7;
240 if (server->state != CONN_VALID)
241 goto out;
243 do {
244 result = smb_request_recv(server);
245 if (result < 0) {
246 server->state = CONN_INVALID;
247 smbiod_retry(server);
248 goto out; /* reconnecting is slow */
249 } else if (server->rstate == SMB_RECV_REQUEST)
250 smbiod_handle_request(server);
251 } while (result > 0 && maxwork-- > 0);
254 * If there is more to read then we want to be sure to wake up again.
256 if (server->state != CONN_VALID)
257 goto out;
258 if (smb_recv_available(server) > 0)
259 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
261 do {
262 result = smb_request_send_server(server);
263 if (result < 0) {
264 server->state = CONN_INVALID;
265 smbiod_retry(server);
266 goto out; /* reconnecting is slow */
268 } while (result > 0);
271 * If the last request was not sent out we want to wake up again.
273 if (!list_empty(&server->xmitq))
274 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
276 out:
277 return;
281 * smbiod kernel thread
283 static int smbiod(void *unused)
285 MOD_INC_USE_COUNT;
286 daemonize("smbiod");
288 allow_signal(SIGKILL);
290 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
292 for (;;) {
293 struct smb_sb_info *server;
294 struct list_head *pos, *n;
296 /* FIXME: Use poll? */
297 wait_event_interruptible(smbiod_wait,
298 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
299 if (signal_pending(current)) {
300 spin_lock(&servers_lock);
301 smbiod_state = SMBIOD_DEAD;
302 spin_unlock(&servers_lock);
303 break;
306 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
308 spin_lock(&servers_lock);
309 if (list_empty(&smb_servers)) {
310 smbiod_state = SMBIOD_DEAD;
311 spin_unlock(&servers_lock);
312 break;
315 list_for_each_safe(pos, n, &smb_servers) {
316 server = list_entry(pos, struct smb_sb_info, entry);
317 VERBOSE("checking server %p\n", server);
319 if (server->state == CONN_VALID) {
320 spin_unlock(&servers_lock);
322 smb_lock_server(server);
323 smbiod_doio(server);
324 smb_unlock_server(server);
326 spin_lock(&servers_lock);
329 spin_unlock(&servers_lock);
332 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
333 MOD_DEC_USE_COUNT;
334 return 0;