smbiod.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_del(&req->rq_queue);
  173. list_add(&req->rq_queue, &server->xmitq);
  174. continue;
  175. }
  176. #endif
  177. VERBOSE("aborting request %p on recvq\n", req);
  178. /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
  179. req->rq_errno = -EIO;
  180. list_del_init(&req->rq_queue);
  181. smb_rput(req);
  182. wake_up_interruptible(&req->rq_wait);
  183. }
  184. smb_close_socket(server);
  185. if (pid == 0) {
  186. /* FIXME: this is fatal, umount? */
  187. printk(KERN_ERR "smb_retry: no connection process\n");
  188. server->state = CONN_RETRIED;
  189. goto out;
  190. }
  191. /*
  192. * Change state so that only one retry per server will be started.
  193. */
  194. server->state = CONN_RETRYING;
  195. /*
  196. * Note: use the "priv" flag, as a user process may need to reconnect.
  197. */
  198. result = kill_proc(pid, SIGUSR1, 1);
  199. if (result) {
  200. /* FIXME: this is most likely fatal, umount? */
  201. printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
  202. goto out;
  203. }
  204. VERBOSE("signalled pid %d\n", pid);
  205. /* FIXME: The retried requests should perhaps get a "time boost". */
  206. out:
  207. return result;
  208. }
  209. /*
  210. * Currently handles lockingX packets.
  211. */
  212. static void smbiod_handle_request(struct smb_sb_info *server)
  213. {
  214. PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
  215. server->rstate = SMB_RECV_DROP;
  216. }
  217. /*
  218. * Do some IO for one server.
  219. */
  220. static void smbiod_doio(struct smb_sb_info *server)
  221. {
  222. int result;
  223. int maxwork = 7;
  224. if (server->state != CONN_VALID)
  225. goto out;
  226. do {
  227. result = smb_request_recv(server);
  228. if (result < 0) {
  229. server->state = CONN_INVALID;
  230. smbiod_retry(server);
  231. goto out; /* reconnecting is slow */
  232. } else if (server->rstate == SMB_RECV_REQUEST)
  233. smbiod_handle_request(server);
  234. } while (result > 0 && maxwork-- > 0);
  235. /*
  236. * If there is more to read then we want to be sure to wake up again.
  237. */
  238. if (server->state != CONN_VALID)
  239. goto out;
  240. if (smb_recv_available(server) > 0)
  241. set_bit(SMBIOD_DATA_READY, &smbiod_flags);
  242. do {
  243. result = smb_request_send_server(server);
  244. if (result < 0) {
  245. server->state = CONN_INVALID;
  246. smbiod_retry(server);
  247. goto out; /* reconnecting is slow */
  248. }
  249. } while (result > 0);
  250. /*
  251. * If the last request was not sent out we want to wake up again.
  252. */
  253. if (!list_empty(&server->xmitq))
  254. set_bit(SMBIOD_DATA_READY, &smbiod_flags);
  255. out:
  256. return;
  257. }
  258. /*
  259. * smbiod kernel thread
  260. */
  261. static int smbiod(void *unused)
  262. {
  263. allow_signal(SIGKILL);
  264. VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
  265. for (;;) {
  266. struct smb_sb_info *server;
  267. struct list_head *pos, *n;
  268. /* FIXME: Use poll? */
  269. wait_event_interruptible(smbiod_wait,
  270. test_bit(SMBIOD_DATA_READY, &smbiod_flags));
  271. if (signal_pending(current)) {
  272. spin_lock(&servers_lock);
  273. smbiod_state = SMBIOD_DEAD;
  274. spin_unlock(&servers_lock);
  275. break;
  276. }
  277. clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
  278. spin_lock(&servers_lock);
  279. if (list_empty(&smb_servers)) {
  280. smbiod_state = SMBIOD_DEAD;
  281. spin_unlock(&servers_lock);
  282. break;
  283. }
  284. list_for_each_safe(pos, n, &smb_servers) {
  285. server = list_entry(pos, struct smb_sb_info, entry);
  286. VERBOSE("checking server %p\n", server);
  287. if (server->state == CONN_VALID) {
  288. spin_unlock(&servers_lock);
  289. smb_lock_server(server);
  290. smbiod_doio(server);
  291. smb_unlock_server(server);
  292. spin_lock(&servers_lock);
  293. }
  294. }
  295. spin_unlock(&servers_lock);
  296. }
  297. VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
  298. module_put_and_exit(0);
  299. }