4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
9 #include <linux/sched.h>
10 #include <linux/kernel.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>
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"
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
)
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
;
73 if (smbiod_state
!= SMBIOD_DEAD
)
75 smbiod_state
= SMBIOD_STARTING
;
76 __module_get(THIS_MODULE
);
77 spin_unlock(&servers_lock
);
78 tsk
= kthread_run(smbiod
, NULL
, "smbiod");
81 module_put(THIS_MODULE
);
84 spin_lock(&servers_lock
);
86 smbiod_state
= SMBIOD_DEAD
;
89 smbiod_state
= SMBIOD_RUNNING
;
96 * register a server & start smbiod if necessary
98 int smbiod_register_server(struct smb_sb_info
*server
)
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
);
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
);
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
);
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
);
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 pid_t pid
= server
->conn_pid
;
158 VERBOSE("state: %d\n", server
->state
);
159 if (server
->state
== CONN_VALID
|| server
->state
== CONN_RETRYING
)
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
169 head
= server
->xmitq
.next
;
170 while (head
!= &server
->xmitq
) {
171 req
= list_entry(head
, struct smb_request
, rq_queue
);
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
);
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
);
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
);
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
);
205 wake_up_interruptible(&req
->rq_wait
);
208 smb_close_socket(server
);
211 /* FIXME: this is fatal, umount? */
212 printk(KERN_ERR
"smb_retry: no connection process\n");
213 server
->state
= CONN_RETRIED
;
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_proc(pid
, SIGUSR1
, 1);
227 /* FIXME: this is most likely fatal, umount? */
228 printk(KERN_ERR
"smb_retry: signal failed [%d]\n", result
);
231 VERBOSE("signalled pid %d\n", pid
);
233 /* FIXME: The retried requests should perhaps get a "time boost". */
240 * Currently handles lockingX packets.
242 static void smbiod_handle_request(struct smb_sb_info
*server
)
244 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
245 server
->rstate
= SMB_RECV_DROP
;
249 * Do some IO for one server.
251 static void smbiod_doio(struct smb_sb_info
*server
)
256 if (server
->state
!= CONN_VALID
)
260 result
= smb_request_recv(server
);
262 server
->state
= CONN_INVALID
;
263 smbiod_retry(server
);
264 goto out
; /* reconnecting is slow */
265 } else if (server
->rstate
== SMB_RECV_REQUEST
)
266 smbiod_handle_request(server
);
267 } while (result
> 0 && maxwork
-- > 0);
270 * If there is more to read then we want to be sure to wake up again.
272 if (server
->state
!= CONN_VALID
)
274 if (smb_recv_available(server
) > 0)
275 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
278 result
= smb_request_send_server(server
);
280 server
->state
= CONN_INVALID
;
281 smbiod_retry(server
);
282 goto out
; /* reconnecting is slow */
284 } while (result
> 0);
287 * If the last request was not sent out we want to wake up again.
289 if (!list_empty(&server
->xmitq
))
290 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
297 * smbiod kernel thread
299 static int smbiod(void *unused
)
301 allow_signal(SIGKILL
);
303 VERBOSE("SMB Kernel thread starting (%d) ...\n", current
->pid
);
306 struct smb_sb_info
*server
;
307 struct list_head
*pos
, *n
;
309 /* FIXME: Use poll? */
310 wait_event_interruptible(smbiod_wait
,
311 test_bit(SMBIOD_DATA_READY
, &smbiod_flags
));
312 if (signal_pending(current
)) {
313 spin_lock(&servers_lock
);
314 smbiod_state
= SMBIOD_DEAD
;
315 spin_unlock(&servers_lock
);
319 clear_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
321 spin_lock(&servers_lock
);
322 if (list_empty(&smb_servers
)) {
323 smbiod_state
= SMBIOD_DEAD
;
324 spin_unlock(&servers_lock
);
328 list_for_each_safe(pos
, n
, &smb_servers
) {
329 server
= list_entry(pos
, struct smb_sb_info
, entry
);
330 VERBOSE("checking server %p\n", server
);
332 if (server
->state
== CONN_VALID
) {
333 spin_unlock(&servers_lock
);
335 smb_lock_server(server
);
337 smb_unlock_server(server
);
339 spin_lock(&servers_lock
);
342 spin_unlock(&servers_lock
);
345 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current
->pid
);
346 module_put_and_exit(0);