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_move(&req
->rq_queue
, &server
->xmitq
);
201 VERBOSE("aborting request %p on recvq\n", req
);
202 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
203 req
->rq_errno
= -EIO
;
204 list_del_init(&req
->rq_queue
);
206 wake_up_interruptible(&req
->rq_wait
);
209 smb_close_socket(server
);
212 /* FIXME: this is fatal, umount? */
213 printk(KERN_ERR
"smb_retry: no connection process\n");
214 server
->state
= CONN_RETRIED
;
219 * Change state so that only one retry per server will be started.
221 server
->state
= CONN_RETRYING
;
224 * Note: use the "priv" flag, as a user process may need to reconnect.
226 result
= kill_proc(pid
, SIGUSR1
, 1);
228 /* FIXME: this is most likely fatal, umount? */
229 printk(KERN_ERR
"smb_retry: signal failed [%d]\n", result
);
232 VERBOSE("signalled pid %d\n", pid
);
234 /* FIXME: The retried requests should perhaps get a "time boost". */
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
)
257 if (server
->state
!= CONN_VALID
)
261 result
= smb_request_recv(server
);
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
)
275 if (smb_recv_available(server
) > 0)
276 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
279 result
= smb_request_send_server(server
);
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
);
298 * smbiod kernel thread
300 static int smbiod(void *unused
)
302 allow_signal(SIGKILL
);
304 VERBOSE("SMB Kernel thread starting (%d) ...\n", current
->pid
);
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
);
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
);
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
);
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);