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