smbiod.c 7.5 KB

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