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