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 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)
71 if (smbiod_state
!= SMBIOD_DEAD
)
73 smbiod_state
= SMBIOD_STARTING
;
74 __module_get(THIS_MODULE
);
75 spin_unlock(&servers_lock
);
76 pid
= kernel_thread(smbiod
, NULL
, 0);
78 module_put(THIS_MODULE
);
80 spin_lock(&servers_lock
);
81 smbiod_state
= pid
< 0 ? SMBIOD_DEAD
: SMBIOD_RUNNING
;
87 * register a server & start smbiod if necessary
89 int smbiod_register_server(struct smb_sb_info
*server
)
92 spin_lock(&servers_lock
);
93 list_add(&server
->entry
, &smb_servers
);
94 VERBOSE("%p\n", server
);
96 spin_unlock(&servers_lock
);
101 * Unregister a server
102 * Must be called with the server lock held.
104 void smbiod_unregister_server(struct smb_sb_info
*server
)
106 spin_lock(&servers_lock
);
107 list_del_init(&server
->entry
);
108 VERBOSE("%p\n", server
);
109 spin_unlock(&servers_lock
);
112 smbiod_flush(server
);
115 void smbiod_flush(struct smb_sb_info
*server
)
117 struct list_head
*tmp
, *n
;
118 struct smb_request
*req
;
120 list_for_each_safe(tmp
, n
, &server
->xmitq
) {
121 req
= list_entry(tmp
, struct smb_request
, rq_queue
);
122 req
->rq_errno
= -EIO
;
123 list_del_init(&req
->rq_queue
);
125 wake_up_interruptible(&req
->rq_wait
);
127 list_for_each_safe(tmp
, n
, &server
->recvq
) {
128 req
= list_entry(tmp
, struct smb_request
, rq_queue
);
129 req
->rq_errno
= -EIO
;
130 list_del_init(&req
->rq_queue
);
132 wake_up_interruptible(&req
->rq_wait
);
137 * Wake up smbmount and make it reconnect to the server.
138 * This must be called with the server locked.
140 * FIXME: add smbconnect version to this
142 int smbiod_retry(struct smb_sb_info
*server
)
144 struct list_head
*head
;
145 struct smb_request
*req
;
146 pid_t pid
= server
->conn_pid
;
149 VERBOSE("state: %d\n", server
->state
);
150 if (server
->state
== CONN_VALID
|| server
->state
== CONN_RETRYING
)
153 smb_invalidate_inodes(server
);
156 * Some requests are meaningless after a retry, so we abort them.
157 * One example are all requests using 'fileid' since the files are
160 head
= server
->xmitq
.next
;
161 while (head
!= &server
->xmitq
) {
162 req
= list_entry(head
, struct smb_request
, rq_queue
);
165 req
->rq_bytes_sent
= 0;
166 if (req
->rq_flags
& SMB_REQ_NORETRY
) {
167 VERBOSE("aborting request %p on xmitq\n", req
);
168 req
->rq_errno
= -EIO
;
169 list_del_init(&req
->rq_queue
);
171 wake_up_interruptible(&req
->rq_wait
);
176 * FIXME: test the code for retrying request we already sent
178 head
= server
->recvq
.next
;
179 while (head
!= &server
->recvq
) {
180 req
= list_entry(head
, struct smb_request
, rq_queue
);
183 if (req
->rq_flags
& SMB_REQ_RETRY
) {
184 /* must move the request to the xmitq */
185 VERBOSE("retrying request %p on recvq\n", req
);
186 list_del(&req
->rq_queue
);
187 list_add(&req
->rq_queue
, &server
->xmitq
);
192 VERBOSE("aborting request %p on recvq\n", req
);
193 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
194 req
->rq_errno
= -EIO
;
195 list_del_init(&req
->rq_queue
);
197 wake_up_interruptible(&req
->rq_wait
);
200 smb_close_socket(server
);
203 /* FIXME: this is fatal, umount? */
204 printk(KERN_ERR
"smb_retry: no connection process\n");
205 server
->state
= CONN_RETRIED
;
210 * Change state so that only one retry per server will be started.
212 server
->state
= CONN_RETRYING
;
215 * Note: use the "priv" flag, as a user process may need to reconnect.
217 result
= kill_proc(pid
, SIGUSR1
, 1);
219 /* FIXME: this is most likely fatal, umount? */
220 printk(KERN_ERR
"smb_retry: signal failed [%d]\n", result
);
223 VERBOSE("signalled pid %d\n", pid
);
225 /* FIXME: The retried requests should perhaps get a "time boost". */
232 * Currently handles lockingX packets.
234 static void smbiod_handle_request(struct smb_sb_info
*server
)
236 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
237 server
->rstate
= SMB_RECV_DROP
;
241 * Do some IO for one server.
243 static void smbiod_doio(struct smb_sb_info
*server
)
248 if (server
->state
!= CONN_VALID
)
252 result
= smb_request_recv(server
);
254 server
->state
= CONN_INVALID
;
255 smbiod_retry(server
);
256 goto out
; /* reconnecting is slow */
257 } else if (server
->rstate
== SMB_RECV_REQUEST
)
258 smbiod_handle_request(server
);
259 } while (result
> 0 && maxwork
-- > 0);
262 * If there is more to read then we want to be sure to wake up again.
264 if (server
->state
!= CONN_VALID
)
266 if (smb_recv_available(server
) > 0)
267 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
270 result
= smb_request_send_server(server
);
272 server
->state
= CONN_INVALID
;
273 smbiod_retry(server
);
274 goto out
; /* reconnecting is slow */
276 } while (result
> 0);
279 * If the last request was not sent out we want to wake up again.
281 if (!list_empty(&server
->xmitq
))
282 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
289 * smbiod kernel thread
291 static int smbiod(void *unused
)
295 allow_signal(SIGKILL
);
297 VERBOSE("SMB Kernel thread starting (%d) ...\n", current
->pid
);
300 struct smb_sb_info
*server
;
301 struct list_head
*pos
, *n
;
303 /* FIXME: Use poll? */
304 wait_event_interruptible(smbiod_wait
,
305 test_bit(SMBIOD_DATA_READY
, &smbiod_flags
));
306 if (signal_pending(current
)) {
307 spin_lock(&servers_lock
);
308 smbiod_state
= SMBIOD_DEAD
;
309 spin_unlock(&servers_lock
);
313 clear_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
315 spin_lock(&servers_lock
);
316 if (list_empty(&smb_servers
)) {
317 smbiod_state
= SMBIOD_DEAD
;
318 spin_unlock(&servers_lock
);
322 list_for_each_safe(pos
, n
, &smb_servers
) {
323 server
= list_entry(pos
, struct smb_sb_info
, entry
);
324 VERBOSE("checking server %p\n", server
);
326 if (server
->state
== CONN_VALID
) {
327 spin_unlock(&servers_lock
);
329 smb_lock_server(server
);
331 smb_unlock_server(server
);
333 spin_lock(&servers_lock
);
336 spin_unlock(&servers_lock
);
339 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current
->pid
);
340 module_put_and_exit(0);