blob: ec998920f8d9e83b8cc3470a503acfaad958f4ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * smbiod.c
3 *
4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/file.h>
17#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/net.h>
Serge E. Hallynfa366ad2006-06-25 05:49:00 -070020#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/ip.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/system.h>
24#include <asm/uaccess.h>
25
Arnd Bergmann2116b7a2010-10-04 22:55:57 +020026#include "smb_fs.h"
27#include "smbno.h"
28#include "smb_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "smb_debug.h"
30#include "request.h"
31#include "proto.h"
32
33enum smbiod_state {
34 SMBIOD_DEAD,
35 SMBIOD_STARTING,
36 SMBIOD_RUNNING,
37};
38
39static enum smbiod_state smbiod_state = SMBIOD_DEAD;
Serge E. Hallynfa366ad2006-06-25 05:49:00 -070040static struct task_struct *smbiod_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
42static LIST_HEAD(smb_servers);
43static DEFINE_SPINLOCK(servers_lock);
44
45#define SMBIOD_DATA_READY (1<<0)
Al Viro5ba25332007-10-14 19:35:50 +010046static unsigned long smbiod_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static int smbiod(void *);
49static int smbiod_start(void);
50
51/*
52 * called when there's work for us to do
53 */
54void smbiod_wake_up(void)
55{
56 if (smbiod_state == SMBIOD_DEAD)
57 return;
58 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
59 wake_up_interruptible(&smbiod_wait);
60}
61
62/*
63 * start smbiod if none is running
64 */
65static int smbiod_start(void)
66{
Serge E. Hallynfa366ad2006-06-25 05:49:00 -070067 struct task_struct *tsk;
68 int err = 0;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (smbiod_state != SMBIOD_DEAD)
71 return 0;
72 smbiod_state = SMBIOD_STARTING;
73 __module_get(THIS_MODULE);
74 spin_unlock(&servers_lock);
Serge E. Hallynfa366ad2006-06-25 05:49:00 -070075 tsk = kthread_run(smbiod, NULL, "smbiod");
76 if (IS_ERR(tsk)) {
77 err = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 module_put(THIS_MODULE);
Serge E. Hallynfa366ad2006-06-25 05:49:00 -070079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 spin_lock(&servers_lock);
Serge E. Hallynfa366ad2006-06-25 05:49:00 -070082 if (err < 0) {
83 smbiod_state = SMBIOD_DEAD;
84 smbiod_thread = NULL;
85 } else {
86 smbiod_state = SMBIOD_RUNNING;
87 smbiod_thread = tsk;
88 }
89 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/*
93 * register a server & start smbiod if necessary
94 */
95int smbiod_register_server(struct smb_sb_info *server)
96{
97 int ret;
98 spin_lock(&servers_lock);
99 list_add(&server->entry, &smb_servers);
100 VERBOSE("%p\n", server);
101 ret = smbiod_start();
102 spin_unlock(&servers_lock);
103 return ret;
104}
105
106/*
107 * Unregister a server
108 * Must be called with the server lock held.
109 */
110void smbiod_unregister_server(struct smb_sb_info *server)
111{
112 spin_lock(&servers_lock);
113 list_del_init(&server->entry);
114 VERBOSE("%p\n", server);
115 spin_unlock(&servers_lock);
116
117 smbiod_wake_up();
118 smbiod_flush(server);
119}
120
121void smbiod_flush(struct smb_sb_info *server)
122{
123 struct list_head *tmp, *n;
124 struct smb_request *req;
125
126 list_for_each_safe(tmp, n, &server->xmitq) {
127 req = list_entry(tmp, struct smb_request, rq_queue);
128 req->rq_errno = -EIO;
129 list_del_init(&req->rq_queue);
130 smb_rput(req);
131 wake_up_interruptible(&req->rq_wait);
132 }
133 list_for_each_safe(tmp, n, &server->recvq) {
134 req = list_entry(tmp, struct smb_request, rq_queue);
135 req->rq_errno = -EIO;
136 list_del_init(&req->rq_queue);
137 smb_rput(req);
138 wake_up_interruptible(&req->rq_wait);
139 }
140}
141
142/*
143 * Wake up smbmount and make it reconnect to the server.
144 * This must be called with the server locked.
145 *
146 * FIXME: add smbconnect version to this
147 */
148int smbiod_retry(struct smb_sb_info *server)
149{
150 struct list_head *head;
151 struct smb_request *req;
Eric W. Biedermana71113d2006-12-13 00:35:10 -0800152 struct pid *pid = get_pid(server->conn_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int result = 0;
154
155 VERBOSE("state: %d\n", server->state);
156 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
157 goto out;
158
159 smb_invalidate_inodes(server);
160
161 /*
162 * Some requests are meaningless after a retry, so we abort them.
163 * One example are all requests using 'fileid' since the files are
164 * closed on retry.
165 */
166 head = server->xmitq.next;
167 while (head != &server->xmitq) {
168 req = list_entry(head, struct smb_request, rq_queue);
169 head = head->next;
170
171 req->rq_bytes_sent = 0;
172 if (req->rq_flags & SMB_REQ_NORETRY) {
173 VERBOSE("aborting request %p on xmitq\n", req);
174 req->rq_errno = -EIO;
175 list_del_init(&req->rq_queue);
176 smb_rput(req);
177 wake_up_interruptible(&req->rq_wait);
178 }
179 }
180
181 /*
182 * FIXME: test the code for retrying request we already sent
183 */
184 head = server->recvq.next;
185 while (head != &server->recvq) {
186 req = list_entry(head, struct smb_request, rq_queue);
187 head = head->next;
188#if 0
189 if (req->rq_flags & SMB_REQ_RETRY) {
190 /* must move the request to the xmitq */
191 VERBOSE("retrying request %p on recvq\n", req);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700192 list_move(&req->rq_queue, &server->xmitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 continue;
194 }
195#endif
196
197 VERBOSE("aborting request %p on recvq\n", req);
198 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
199 req->rq_errno = -EIO;
200 list_del_init(&req->rq_queue);
201 smb_rput(req);
202 wake_up_interruptible(&req->rq_wait);
203 }
204
205 smb_close_socket(server);
206
Al Viro9dce07f2008-03-29 03:07:28 +0000207 if (!pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* FIXME: this is fatal, umount? */
209 printk(KERN_ERR "smb_retry: no connection process\n");
210 server->state = CONN_RETRIED;
211 goto out;
212 }
213
214 /*
215 * Change state so that only one retry per server will be started.
216 */
217 server->state = CONN_RETRYING;
218
219 /*
220 * Note: use the "priv" flag, as a user process may need to reconnect.
221 */
Eric W. Biedermana71113d2006-12-13 00:35:10 -0800222 result = kill_pid(pid, SIGUSR1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (result) {
224 /* FIXME: this is most likely fatal, umount? */
225 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
226 goto out;
227 }
Jeff Laytondbaf4c02007-11-14 17:00:18 -0800228 VERBOSE("signalled pid %d\n", pid_nr(pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* FIXME: The retried requests should perhaps get a "time boost". */
231
232out:
Eric W. Biedermana71113d2006-12-13 00:35:10 -0800233 put_pid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return result;
235}
236
237/*
238 * Currently handles lockingX packets.
239 */
240static void smbiod_handle_request(struct smb_sb_info *server)
241{
242 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
243 server->rstate = SMB_RECV_DROP;
244}
245
246/*
247 * Do some IO for one server.
248 */
249static void smbiod_doio(struct smb_sb_info *server)
250{
251 int result;
252 int maxwork = 7;
253
254 if (server->state != CONN_VALID)
255 goto out;
256
257 do {
258 result = smb_request_recv(server);
259 if (result < 0) {
260 server->state = CONN_INVALID;
261 smbiod_retry(server);
262 goto out; /* reconnecting is slow */
263 } else if (server->rstate == SMB_RECV_REQUEST)
264 smbiod_handle_request(server);
265 } while (result > 0 && maxwork-- > 0);
266
267 /*
268 * If there is more to read then we want to be sure to wake up again.
269 */
270 if (server->state != CONN_VALID)
271 goto out;
272 if (smb_recv_available(server) > 0)
273 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
274
275 do {
276 result = smb_request_send_server(server);
277 if (result < 0) {
278 server->state = CONN_INVALID;
279 smbiod_retry(server);
280 goto out; /* reconnecting is slow */
281 }
282 } while (result > 0);
283
284 /*
285 * If the last request was not sent out we want to wake up again.
286 */
287 if (!list_empty(&server->xmitq))
288 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
289
290out:
291 return;
292}
293
294/*
295 * smbiod kernel thread
296 */
297static int smbiod(void *unused)
298{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
300
301 for (;;) {
302 struct smb_sb_info *server;
303 struct list_head *pos, *n;
304
305 /* FIXME: Use poll? */
306 wait_event_interruptible(smbiod_wait,
307 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
308 if (signal_pending(current)) {
309 spin_lock(&servers_lock);
310 smbiod_state = SMBIOD_DEAD;
311 spin_unlock(&servers_lock);
312 break;
313 }
314
315 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
316
317 spin_lock(&servers_lock);
318 if (list_empty(&smb_servers)) {
319 smbiod_state = SMBIOD_DEAD;
320 spin_unlock(&servers_lock);
321 break;
322 }
323
324 list_for_each_safe(pos, n, &smb_servers) {
325 server = list_entry(pos, struct smb_sb_info, entry);
326 VERBOSE("checking server %p\n", server);
327
328 if (server->state == CONN_VALID) {
329 spin_unlock(&servers_lock);
330
331 smb_lock_server(server);
332 smbiod_doio(server);
333 smb_unlock_server(server);
334
335 spin_lock(&servers_lock);
336 }
337 }
338 spin_unlock(&servers_lock);
339 }
340
341 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
342 module_put_and_exit(0);
343}