RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / smbfs / smbiod.c
blob9d1f52dbfd6492abdd810a2c7bac1910285f25af
1 /*
2 * smbiod.c
4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
6 */
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.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>
21 #include <net/ip.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"
31 #include "request.h"
32 #include "proto.h"
34 enum smbiod_state {
35 SMBIOD_DEAD,
36 SMBIOD_STARTING,
37 SMBIOD_RUNNING,
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)
58 return;
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;
69 int err = 0;
71 if (smbiod_state != SMBIOD_DEAD)
72 return 0;
73 smbiod_state = SMBIOD_STARTING;
74 __module_get(THIS_MODULE);
75 spin_unlock(&servers_lock);
76 tsk = kthread_run(smbiod, NULL, "smbiod");
77 if (IS_ERR(tsk)) {
78 err = PTR_ERR(tsk);
79 module_put(THIS_MODULE);
82 spin_lock(&servers_lock);
83 if (err < 0) {
84 smbiod_state = SMBIOD_DEAD;
85 smbiod_thread = NULL;
86 } else {
87 smbiod_state = SMBIOD_RUNNING;
88 smbiod_thread = tsk;
90 return err;
94 * register a server & start smbiod if necessary
96 int smbiod_register_server(struct smb_sb_info *server)
98 int ret;
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);
104 return ret;
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);
118 smbiod_wake_up();
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);
131 smb_rput(req);
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);
138 smb_rput(req);
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);
148 int result = 0;
150 VERBOSE("state: %d\n", server->state);
151 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
152 goto out;
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
159 * closed on retry.
161 head = server->xmitq.next;
162 while (head != &server->xmitq) {
163 req = list_entry(head, struct smb_request, rq_queue);
164 head = head->next;
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);
171 smb_rput(req);
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);
179 head = head->next;
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);
185 smb_rput(req);
186 wake_up_interruptible(&req->rq_wait);
189 smb_close_socket(server);
191 if (!pid) {
192 printk(KERN_ERR "smb_retry: no connection process\n");
193 server->state = CONN_RETRIED;
194 goto out;
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);
206 if (result) {
207 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
208 goto out;
210 VERBOSE("signalled pid %d\n", pid_nr(pid));
213 out:
214 put_pid(pid);
215 return result;
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)
232 int result;
233 int maxwork = 7;
235 if (server->state != CONN_VALID)
236 goto out;
238 do {
239 result = smb_request_recv(server);
240 if (result < 0) {
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)
252 goto out;
253 if (smb_recv_available(server) > 0)
254 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
256 do {
257 result = smb_request_send_server(server);
258 if (result < 0) {
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);
271 out:
272 return;
276 * smbiod kernel thread
278 static int smbiod(void *unused)
280 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
282 for (;;) {
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);
292 break;
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);
301 break;
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);
312 smbiod_doio(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);