x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / fs / smbfs / smbiod.c
blob6bd9b691a463f783058b03a84d6de28b49813ee7
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/slab.h>
16 #include <linux/init.h>
17 #include <linux/file.h>
18 #include <linux/dcache.h>
19 #include <linux/module.h>
20 #include <linux/net.h>
21 #include <linux/kthread.h>
22 #include <net/ip.h>
24 #include <linux/smb_fs.h>
25 #include <linux/smbno.h>
26 #include <linux/smb_mount.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
31 #include "smb_debug.h"
32 #include "request.h"
33 #include "proto.h"
35 enum smbiod_state {
36 SMBIOD_DEAD,
37 SMBIOD_STARTING,
38 SMBIOD_RUNNING,
41 static enum smbiod_state smbiod_state = SMBIOD_DEAD;
42 static struct task_struct *smbiod_thread;
43 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
44 static LIST_HEAD(smb_servers);
45 static DEFINE_SPINLOCK(servers_lock);
47 #define SMBIOD_DATA_READY (1<<0)
48 static unsigned long smbiod_flags;
50 static int smbiod(void *);
51 static int smbiod_start(void);
54 * called when there's work for us to do
56 void smbiod_wake_up(void)
58 if (smbiod_state == SMBIOD_DEAD)
59 return;
60 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
61 wake_up_interruptible(&smbiod_wait);
65 * start smbiod if none is running
67 static int smbiod_start(void)
69 struct task_struct *tsk;
70 int err = 0;
72 if (smbiod_state != SMBIOD_DEAD)
73 return 0;
74 smbiod_state = SMBIOD_STARTING;
75 __module_get(THIS_MODULE);
76 spin_unlock(&servers_lock);
77 tsk = kthread_run(smbiod, NULL, "smbiod");
78 if (IS_ERR(tsk)) {
79 err = PTR_ERR(tsk);
80 module_put(THIS_MODULE);
83 spin_lock(&servers_lock);
84 if (err < 0) {
85 smbiod_state = SMBIOD_DEAD;
86 smbiod_thread = NULL;
87 } else {
88 smbiod_state = SMBIOD_RUNNING;
89 smbiod_thread = tsk;
91 return err;
95 * register a server & start smbiod if necessary
97 int smbiod_register_server(struct smb_sb_info *server)
99 int ret;
100 spin_lock(&servers_lock);
101 list_add(&server->entry, &smb_servers);
102 VERBOSE("%p\n", server);
103 ret = smbiod_start();
104 spin_unlock(&servers_lock);
105 return ret;
109 * Unregister a server
110 * Must be called with the server lock held.
112 void smbiod_unregister_server(struct smb_sb_info *server)
114 spin_lock(&servers_lock);
115 list_del_init(&server->entry);
116 VERBOSE("%p\n", server);
117 spin_unlock(&servers_lock);
119 smbiod_wake_up();
120 smbiod_flush(server);
123 void smbiod_flush(struct smb_sb_info *server)
125 struct list_head *tmp, *n;
126 struct smb_request *req;
128 list_for_each_safe(tmp, n, &server->xmitq) {
129 req = list_entry(tmp, struct smb_request, rq_queue);
130 req->rq_errno = -EIO;
131 list_del_init(&req->rq_queue);
132 smb_rput(req);
133 wake_up_interruptible(&req->rq_wait);
135 list_for_each_safe(tmp, n, &server->recvq) {
136 req = list_entry(tmp, struct smb_request, rq_queue);
137 req->rq_errno = -EIO;
138 list_del_init(&req->rq_queue);
139 smb_rput(req);
140 wake_up_interruptible(&req->rq_wait);
145 * Wake up smbmount and make it reconnect to the server.
146 * This must be called with the server locked.
148 * FIXME: add smbconnect version to this
150 int smbiod_retry(struct smb_sb_info *server)
152 struct list_head *head;
153 struct smb_request *req;
154 struct pid *pid = get_pid(server->conn_pid);
155 int result = 0;
157 VERBOSE("state: %d\n", server->state);
158 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
159 goto out;
161 smb_invalidate_inodes(server);
164 * Some requests are meaningless after a retry, so we abort them.
165 * One example are all requests using 'fileid' since the files are
166 * closed on retry.
168 head = server->xmitq.next;
169 while (head != &server->xmitq) {
170 req = list_entry(head, struct smb_request, rq_queue);
171 head = head->next;
173 req->rq_bytes_sent = 0;
174 if (req->rq_flags & SMB_REQ_NORETRY) {
175 VERBOSE("aborting request %p on xmitq\n", req);
176 req->rq_errno = -EIO;
177 list_del_init(&req->rq_queue);
178 smb_rput(req);
179 wake_up_interruptible(&req->rq_wait);
184 * FIXME: test the code for retrying request we already sent
186 head = server->recvq.next;
187 while (head != &server->recvq) {
188 req = list_entry(head, struct smb_request, rq_queue);
189 head = head->next;
190 #if 0
191 if (req->rq_flags & SMB_REQ_RETRY) {
192 /* must move the request to the xmitq */
193 VERBOSE("retrying request %p on recvq\n", req);
194 list_move(&req->rq_queue, &server->xmitq);
195 continue;
197 #endif
199 VERBOSE("aborting request %p on recvq\n", req);
200 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
201 req->rq_errno = -EIO;
202 list_del_init(&req->rq_queue);
203 smb_rput(req);
204 wake_up_interruptible(&req->rq_wait);
207 smb_close_socket(server);
209 if (!pid) {
210 /* FIXME: this is fatal, umount? */
211 printk(KERN_ERR "smb_retry: no connection process\n");
212 server->state = CONN_RETRIED;
213 goto out;
217 * Change state so that only one retry per server will be started.
219 server->state = CONN_RETRYING;
222 * Note: use the "priv" flag, as a user process may need to reconnect.
224 result = kill_pid(pid, SIGUSR1, 1);
225 if (result) {
226 /* FIXME: this is most likely fatal, umount? */
227 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
228 goto out;
230 VERBOSE("signalled pid %d\n", pid_nr(pid));
232 /* FIXME: The retried requests should perhaps get a "time boost". */
234 out:
235 put_pid(pid);
236 return result;
240 * Currently handles lockingX packets.
242 static void smbiod_handle_request(struct smb_sb_info *server)
244 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
245 server->rstate = SMB_RECV_DROP;
249 * Do some IO for one server.
251 static void smbiod_doio(struct smb_sb_info *server)
253 int result;
254 int maxwork = 7;
256 if (server->state != CONN_VALID)
257 goto out;
259 do {
260 result = smb_request_recv(server);
261 if (result < 0) {
262 server->state = CONN_INVALID;
263 smbiod_retry(server);
264 goto out; /* reconnecting is slow */
265 } else if (server->rstate == SMB_RECV_REQUEST)
266 smbiod_handle_request(server);
267 } while (result > 0 && maxwork-- > 0);
270 * If there is more to read then we want to be sure to wake up again.
272 if (server->state != CONN_VALID)
273 goto out;
274 if (smb_recv_available(server) > 0)
275 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
277 do {
278 result = smb_request_send_server(server);
279 if (result < 0) {
280 server->state = CONN_INVALID;
281 smbiod_retry(server);
282 goto out; /* reconnecting is slow */
284 } while (result > 0);
287 * If the last request was not sent out we want to wake up again.
289 if (!list_empty(&server->xmitq))
290 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
292 out:
293 return;
297 * smbiod kernel thread
299 static int smbiod(void *unused)
301 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
303 for (;;) {
304 struct smb_sb_info *server;
305 struct list_head *pos, *n;
307 /* FIXME: Use poll? */
308 wait_event_interruptible(smbiod_wait,
309 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
310 if (signal_pending(current)) {
311 spin_lock(&servers_lock);
312 smbiod_state = SMBIOD_DEAD;
313 spin_unlock(&servers_lock);
314 break;
317 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
319 spin_lock(&servers_lock);
320 if (list_empty(&smb_servers)) {
321 smbiod_state = SMBIOD_DEAD;
322 spin_unlock(&servers_lock);
323 break;
326 list_for_each_safe(pos, n, &smb_servers) {
327 server = list_entry(pos, struct smb_sb_info, entry);
328 VERBOSE("checking server %p\n", server);
330 if (server->state == CONN_VALID) {
331 spin_unlock(&servers_lock);
333 smb_lock_server(server);
334 smbiod_doio(server);
335 smb_unlock_server(server);
337 spin_lock(&servers_lock);
340 spin_unlock(&servers_lock);
343 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
344 module_put_and_exit(0);