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/init.h>
16 #include <linux/file.h>
17 #include <linux/dcache.h>
18 #include <linux/module.h>
19 #include <linux/net.h>
20 #include <linux/kthread.h>
23 #include <linux/smb_fs.h>
24 #include <linux/smbno.h>
25 #include <linux/smb_mount.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
30 #include "smb_debug.h"
40 static enum smbiod_state smbiod_state
= SMBIOD_DEAD
;
41 static struct task_struct
*smbiod_thread
;
42 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait
);
43 static LIST_HEAD(smb_servers
);
44 static DEFINE_SPINLOCK(servers_lock
);
46 #define SMBIOD_DATA_READY (1<<0)
47 static unsigned long smbiod_flags
;
49 static int smbiod(void *);
50 static int smbiod_start(void);
53 * called when there's work for us to do
55 void smbiod_wake_up(void)
57 if (smbiod_state
== SMBIOD_DEAD
)
59 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
60 wake_up_interruptible(&smbiod_wait
);
64 * start smbiod if none is running
66 static int smbiod_start(void)
68 struct task_struct
*tsk
;
71 if (smbiod_state
!= SMBIOD_DEAD
)
73 smbiod_state
= SMBIOD_STARTING
;
74 __module_get(THIS_MODULE
);
75 spin_unlock(&servers_lock
);
76 tsk
= kthread_run(smbiod
, NULL
, "smbiod");
79 module_put(THIS_MODULE
);
82 spin_lock(&servers_lock
);
84 smbiod_state
= SMBIOD_DEAD
;
87 smbiod_state
= SMBIOD_RUNNING
;
94 * register a server & start smbiod if necessary
96 int smbiod_register_server(struct smb_sb_info
*server
)
99 spin_lock(&servers_lock
);
100 list_add(&server
->entry
, &smb_servers
);
101 VERBOSE("%p\n", server
);
102 ret
= smbiod_start();
103 spin_unlock(&servers_lock
);
108 * Unregister a server
109 * Must be called with the server lock held.
111 void smbiod_unregister_server(struct smb_sb_info
*server
)
113 spin_lock(&servers_lock
);
114 list_del_init(&server
->entry
);
115 VERBOSE("%p\n", server
);
116 spin_unlock(&servers_lock
);
119 smbiod_flush(server
);
122 void smbiod_flush(struct smb_sb_info
*server
)
124 struct list_head
*tmp
, *n
;
125 struct smb_request
*req
;
127 list_for_each_safe(tmp
, n
, &server
->xmitq
) {
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
);
134 list_for_each_safe(tmp
, n
, &server
->recvq
) {
135 req
= list_entry(tmp
, struct smb_request
, rq_queue
);
136 req
->rq_errno
= -EIO
;
137 list_del_init(&req
->rq_queue
);
139 wake_up_interruptible(&req
->rq_wait
);
143 int smbiod_retry(struct smb_sb_info
*server
)
145 struct list_head
*head
;
146 struct smb_request
*req
;
147 struct pid
*pid
= get_pid(server
->conn_pid
);
150 VERBOSE("state: %d\n", server
->state
);
151 if (server
->state
== CONN_VALID
|| server
->state
== CONN_RETRYING
)
154 smb_invalidate_inodes(server
);
157 * Some requests are meaningless after a retry, so we abort them.
158 * One example are all requests using 'fileid' since the files are
161 head
= server
->xmitq
.next
;
162 while (head
!= &server
->xmitq
) {
163 req
= list_entry(head
, struct smb_request
, rq_queue
);
166 req
->rq_bytes_sent
= 0;
167 if (req
->rq_flags
& SMB_REQ_NORETRY
) {
168 VERBOSE("aborting request %p on xmitq\n", req
);
169 req
->rq_errno
= -EIO
;
170 list_del_init(&req
->rq_queue
);
172 wake_up_interruptible(&req
->rq_wait
);
176 head
= server
->recvq
.next
;
177 while (head
!= &server
->recvq
) {
178 req
= list_entry(head
, struct smb_request
, rq_queue
);
181 VERBOSE("aborting request %p on recvq\n", req
);
182 /* req->rq_rcls = ???; */
183 req
->rq_errno
= -EIO
;
184 list_del_init(&req
->rq_queue
);
186 wake_up_interruptible(&req
->rq_wait
);
189 smb_close_socket(server
);
192 printk(KERN_ERR
"smb_retry: no connection process\n");
193 server
->state
= CONN_RETRIED
;
198 * Change state so that only one retry per server will be started.
200 server
->state
= CONN_RETRYING
;
203 * Note: use the "priv" flag, as a user process may need to reconnect.
205 result
= kill_pid(pid
, SIGUSR1
, 1);
207 printk(KERN_ERR
"smb_retry: signal failed [%d]\n", result
);
210 VERBOSE("signalled pid %d\n", pid_nr(pid
));
219 * Currently handles lockingX packets.
221 static void smbiod_handle_request(struct smb_sb_info
*server
)
223 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
224 server
->rstate
= SMB_RECV_DROP
;
228 * Do some IO for one server.
230 static void smbiod_doio(struct smb_sb_info
*server
)
235 if (server
->state
!= CONN_VALID
)
239 result
= smb_request_recv(server
);
241 server
->state
= CONN_INVALID
;
242 smbiod_retry(server
);
243 goto out
; /* reconnecting is slow */
244 } else if (server
->rstate
== SMB_RECV_REQUEST
)
245 smbiod_handle_request(server
);
246 } while (result
> 0 && maxwork
-- > 0);
249 * If there is more to read then we want to be sure to wake up again.
251 if (server
->state
!= CONN_VALID
)
253 if (smb_recv_available(server
) > 0)
254 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
257 result
= smb_request_send_server(server
);
259 server
->state
= CONN_INVALID
;
260 smbiod_retry(server
);
261 goto out
; /* reconnecting is slow */
263 } while (result
> 0);
266 * If the last request was not sent out we want to wake up again.
268 if (!list_empty(&server
->xmitq
))
269 set_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
276 * smbiod kernel thread
278 static int smbiod(void *unused
)
280 VERBOSE("SMB Kernel thread starting (%d) ...\n", current
->pid
);
283 struct smb_sb_info
*server
;
284 struct list_head
*pos
, *n
;
286 wait_event_interruptible(smbiod_wait
,
287 test_bit(SMBIOD_DATA_READY
, &smbiod_flags
));
288 if (signal_pending(current
)) {
289 spin_lock(&servers_lock
);
290 smbiod_state
= SMBIOD_DEAD
;
291 spin_unlock(&servers_lock
);
295 clear_bit(SMBIOD_DATA_READY
, &smbiod_flags
);
297 spin_lock(&servers_lock
);
298 if (list_empty(&smb_servers
)) {
299 smbiod_state
= SMBIOD_DEAD
;
300 spin_unlock(&servers_lock
);
304 list_for_each_safe(pos
, n
, &smb_servers
) {
305 server
= list_entry(pos
, struct smb_sb_info
, entry
);
306 VERBOSE("checking server %p\n", server
);
308 if (server
->state
== CONN_VALID
) {
309 spin_unlock(&servers_lock
);
311 smb_lock_server(server
);
313 smb_unlock_server(server
);
315 spin_lock(&servers_lock
);
318 spin_unlock(&servers_lock
);
321 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current
->pid
);
322 module_put_and_exit(0);