4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
8 #include <linux/config.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.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 <linux/kthread.h>
26 #include <linux/smb_fs.h>
27 #include <linux/smbno.h>
28 #include <linux/smb_mount.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 #include "smb_debug.h"
43 static enum smbiod_state smbiod_state = SMBIOD_DEAD;
44 static struct task_struct *smbiod_thread;
45 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
46 static LIST_HEAD(smb_servers);
47 static DEFINE_SPINLOCK(servers_lock);
49 #define SMBIOD_DATA_READY (1<<0)
50 static long smbiod_flags;
52 static int smbiod(void *);
53 static int smbiod_start(void);
56 * called when there's work for us to do
58 void smbiod_wake_up(void)
60 if (smbiod_state == SMBIOD_DEAD)
62 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
63 wake_up_interruptible(&smbiod_wait);
67 * start smbiod if none is running
69 static int smbiod_start(void)
71 struct task_struct *tsk;
74 if (smbiod_state != SMBIOD_DEAD)
76 smbiod_state = SMBIOD_STARTING;
77 __module_get(THIS_MODULE);
78 spin_unlock(&servers_lock);
79 tsk = kthread_run(smbiod, NULL, "smbiod");
82 module_put(THIS_MODULE);
85 spin_lock(&servers_lock);
87 smbiod_state = SMBIOD_DEAD;
90 smbiod_state = SMBIOD_RUNNING;
97 * register a server & start smbiod if necessary
99 int smbiod_register_server(struct smb_sb_info *server)
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);
111 * Unregister a server
112 * Must be called with the server lock held.
114 void smbiod_unregister_server(struct smb_sb_info *server)
116 spin_lock(&servers_lock);
117 list_del_init(&server->entry);
118 VERBOSE("%p\n", server);
119 spin_unlock(&servers_lock);
122 smbiod_flush(server);
125 void smbiod_flush(struct smb_sb_info *server)
127 struct list_head *tmp, *n;
128 struct smb_request *req;
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);
135 wake_up_interruptible(&req->rq_wait);
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);
142 wake_up_interruptible(&req->rq_wait);
147 * Wake up smbmount and make it reconnect to the server.
148 * This must be called with the server locked.
150 * FIXME: add smbconnect version to this
152 int smbiod_retry(struct smb_sb_info *server)
154 struct list_head *head;
155 struct smb_request *req;
156 pid_t pid = server->conn_pid;
159 VERBOSE("state: %d\n", server->state);
160 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
163 smb_invalidate_inodes(server);
166 * Some requests are meaningless after a retry, so we abort them.
167 * One example are all requests using 'fileid' since the files are
170 head = server->xmitq.next;
171 while (head != &server->xmitq) {
172 req = list_entry(head, struct smb_request, rq_queue);
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);
181 wake_up_interruptible(&req->rq_wait);
186 * FIXME: test the code for retrying request we already sent
188 head = server->recvq.next;
189 while (head != &server->recvq) {
190 req = list_entry(head, struct smb_request, rq_queue);
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);
196 list_del(&req->rq_queue);
197 list_add(&req->rq_queue, &server->xmitq);
202 VERBOSE("aborting request %p on recvq\n", req);
203 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
204 req->rq_errno = -EIO;
205 list_del_init(&req->rq_queue);
207 wake_up_interruptible(&req->rq_wait);
210 smb_close_socket(server);
213 /* FIXME: this is fatal, umount? */
214 printk(KERN_ERR "smb_retry: no connection process\n");
215 server->state = CONN_RETRIED;
220 * Change state so that only one retry per server will be started.
222 server->state = CONN_RETRYING;
225 * Note: use the "priv" flag, as a user process may need to reconnect.
227 result = kill_proc(pid, SIGUSR1, 1);
229 /* FIXME: this is most likely fatal, umount? */
230 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
233 VERBOSE("signalled pid %d\n", pid);
235 /* FIXME: The retried requests should perhaps get a "time boost". */
242 * Currently handles lockingX packets.
244 static void smbiod_handle_request(struct smb_sb_info *server)
246 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
247 server->rstate = SMB_RECV_DROP;
251 * Do some IO for one server.
253 static void smbiod_doio(struct smb_sb_info *server)
258 if (server->state != CONN_VALID)
262 result = smb_request_recv(server);
264 server->state = CONN_INVALID;
265 smbiod_retry(server);
266 goto out; /* reconnecting is slow */
267 } else if (server->rstate == SMB_RECV_REQUEST)
268 smbiod_handle_request(server);
269 } while (result > 0 && maxwork-- > 0);
272 * If there is more to read then we want to be sure to wake up again.
274 if (server->state != CONN_VALID)
276 if (smb_recv_available(server) > 0)
277 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
280 result = smb_request_send_server(server);
282 server->state = CONN_INVALID;
283 smbiod_retry(server);
284 goto out; /* reconnecting is slow */
286 } while (result > 0);
289 * If the last request was not sent out we want to wake up again.
291 if (!list_empty(&server->xmitq))
292 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
299 * smbiod kernel thread
301 static int smbiod(void *unused)
303 allow_signal(SIGKILL);
305 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
308 struct smb_sb_info *server;
309 struct list_head *pos, *n;
311 /* FIXME: Use poll? */
312 wait_event_interruptible(smbiod_wait,
313 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
314 if (signal_pending(current)) {
315 spin_lock(&servers_lock);
316 smbiod_state = SMBIOD_DEAD;
317 spin_unlock(&servers_lock);
321 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
323 spin_lock(&servers_lock);
324 if (list_empty(&smb_servers)) {
325 smbiod_state = SMBIOD_DEAD;
326 spin_unlock(&servers_lock);
330 list_for_each_safe(pos, n, &smb_servers) {
331 server = list_entry(pos, struct smb_sb_info, entry);
332 VERBOSE("checking server %p\n", server);
334 if (server->state == CONN_VALID) {
335 spin_unlock(&servers_lock);
337 smb_lock_server(server);
339 smb_unlock_server(server);
341 spin_lock(&servers_lock);
344 spin_unlock(&servers_lock);
347 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
348 module_put_and_exit(0);