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>
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 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
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 void smbiod_start()
71 if (smbiod_state
!= SMBIOD_DEAD
)
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
;
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
);
91 spin_unlock(&servers_lock
);
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
);
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
);
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
);
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
;
143 VERBOSE("state: %d\n", server
->state
);
144 if (server
->state
== CONN_VALID
|| server
->state
== CONN_RETRYING
)
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
154 head
= server
->xmitq
.next
;
155 while (head
!= &server
->xmitq
) {
156 req
= list_entry(head
, struct smb_request
, rq_queue
);
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
);
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
);
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
);
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
);
189 wake_up_interruptible(&req
->rq_wait
);
192 smb_close_socket(server
);
195 /* FIXME: this is fatal, umount? */
196 printk(KERN_ERR
"smb_retry: no connection process\n");
197 server
->state
= CONN_RETRIED
;
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);
211 /* FIXME: this is most likely fatal, umount? */
212 printk(KERN_ERR
"smb_retry: signal failed [%d]\n", result
);
215 VERBOSE("signalled pid %d\n", pid
);
217 /* FIXME: The retried requests should perhaps get a "time boost". */
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
)
240 if (server
->state
!= CONN_VALID
)
244 result
= smb_request_recv(server
);
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
)
258 if (smb_recv_available(server
) > 0)
259 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
262 result
= smb_request_send_server(server
);
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
);
281 * smbiod kernel thread
283 static int smbiod(void *unused
)
288 allow_signal(SIGKILL
);
290 VERBOSE("SMB Kernel thread starting (%d) ...\n", current
->pid
);
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
);
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
);
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
);
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
);