smbiod.c 7.5 KB

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