smbiod.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <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 pid_t smbiod_pid;
  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. pid_t pid;
  60. if (smbiod_state != SMBIOD_DEAD)
  61. return 0;
  62. smbiod_state = SMBIOD_STARTING;
  63. __module_get(THIS_MODULE);
  64. spin_unlock(&servers_lock);
  65. pid = kernel_thread(smbiod, NULL, 0);
  66. if (pid < 0)
  67. module_put(THIS_MODULE);
  68. spin_lock(&servers_lock);
  69. smbiod_state = pid < 0 ? SMBIOD_DEAD : SMBIOD_RUNNING;
  70. smbiod_pid = pid;
  71. return pid;
  72. }
  73. /*
  74. * register a server & start smbiod if necessary
  75. */
  76. int smbiod_register_server(struct smb_sb_info *server)
  77. {
  78. int ret;
  79. spin_lock(&servers_lock);
  80. list_add(&server->entry, &smb_servers);
  81. VERBOSE("%p\n", server);
  82. ret = smbiod_start();
  83. spin_unlock(&servers_lock);
  84. return ret;
  85. }
  86. /*
  87. * Unregister a server
  88. * Must be called with the server lock held.
  89. */
  90. void smbiod_unregister_server(struct smb_sb_info *server)
  91. {
  92. spin_lock(&servers_lock);
  93. list_del_init(&server->entry);
  94. VERBOSE("%p\n", server);
  95. spin_unlock(&servers_lock);
  96. smbiod_wake_up();
  97. smbiod_flush(server);
  98. }
  99. void smbiod_flush(struct smb_sb_info *server)
  100. {
  101. struct list_head *tmp, *n;
  102. struct smb_request *req;
  103. list_for_each_safe(tmp, n, &server->xmitq) {
  104. req = list_entry(tmp, struct smb_request, rq_queue);
  105. req->rq_errno = -EIO;
  106. list_del_init(&req->rq_queue);
  107. smb_rput(req);
  108. wake_up_interruptible(&req->rq_wait);
  109. }
  110. list_for_each_safe(tmp, n, &server->recvq) {
  111. req = list_entry(tmp, struct smb_request, rq_queue);
  112. req->rq_errno = -EIO;
  113. list_del_init(&req->rq_queue);
  114. smb_rput(req);
  115. wake_up_interruptible(&req->rq_wait);
  116. }
  117. }
  118. /*
  119. * Wake up smbmount and make it reconnect to the server.
  120. * This must be called with the server locked.
  121. *
  122. * FIXME: add smbconnect version to this
  123. */
  124. int smbiod_retry(struct smb_sb_info *server)
  125. {
  126. struct list_head *head;
  127. struct smb_request *req;
  128. pid_t pid = server->conn_pid;
  129. int result = 0;
  130. VERBOSE("state: %d\n", server->state);
  131. if (server->state == CONN_VALID || server->state == CONN_RETRYING)
  132. goto out;
  133. smb_invalidate_inodes(server);
  134. /*
  135. * Some requests are meaningless after a retry, so we abort them.
  136. * One example are all requests using 'fileid' since the files are
  137. * closed on retry.
  138. */
  139. head = server->xmitq.next;
  140. while (head != &server->xmitq) {
  141. req = list_entry(head, struct smb_request, rq_queue);
  142. head = head->next;
  143. req->rq_bytes_sent = 0;
  144. if (req->rq_flags & SMB_REQ_NORETRY) {
  145. VERBOSE("aborting request %p on xmitq\n", req);
  146. req->rq_errno = -EIO;
  147. list_del_init(&req->rq_queue);
  148. smb_rput(req);
  149. wake_up_interruptible(&req->rq_wait);
  150. }
  151. }
  152. /*
  153. * FIXME: test the code for retrying request we already sent
  154. */
  155. head = server->recvq.next;
  156. while (head != &server->recvq) {
  157. req = list_entry(head, struct smb_request, rq_queue);
  158. head = head->next;
  159. #if 0
  160. if (req->rq_flags & SMB_REQ_RETRY) {
  161. /* must move the request to the xmitq */
  162. VERBOSE("retrying request %p on recvq\n", req);
  163. list_del(&req->rq_queue);
  164. list_add(&req->rq_queue, &server->xmitq);
  165. continue;
  166. }
  167. #endif
  168. VERBOSE("aborting request %p on recvq\n", req);
  169. /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
  170. req->rq_errno = -EIO;
  171. list_del_init(&req->rq_queue);
  172. smb_rput(req);
  173. wake_up_interruptible(&req->rq_wait);
  174. }
  175. smb_close_socket(server);
  176. if (pid == 0) {
  177. /* FIXME: this is fatal, umount? */
  178. printk(KERN_ERR "smb_retry: no connection process\n");
  179. server->state = CONN_RETRIED;
  180. goto out;
  181. }
  182. /*
  183. * Change state so that only one retry per server will be started.
  184. */
  185. server->state = CONN_RETRYING;
  186. /*
  187. * Note: use the "priv" flag, as a user process may need to reconnect.
  188. */
  189. result = kill_proc(pid, SIGUSR1, 1);
  190. if (result) {
  191. /* FIXME: this is most likely fatal, umount? */
  192. printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
  193. goto out;
  194. }
  195. VERBOSE("signalled pid %d\n", pid);
  196. /* FIXME: The retried requests should perhaps get a "time boost". */
  197. out:
  198. return result;
  199. }
  200. /*
  201. * Currently handles lockingX packets.
  202. */
  203. static void smbiod_handle_request(struct smb_sb_info *server)
  204. {
  205. PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
  206. server->rstate = SMB_RECV_DROP;
  207. }
  208. /*
  209. * Do some IO for one server.
  210. */
  211. static void smbiod_doio(struct smb_sb_info *server)
  212. {
  213. int result;
  214. int maxwork = 7;
  215. if (server->state != CONN_VALID)
  216. goto out;
  217. do {
  218. result = smb_request_recv(server);
  219. if (result < 0) {
  220. server->state = CONN_INVALID;
  221. smbiod_retry(server);
  222. goto out; /* reconnecting is slow */
  223. } else if (server->rstate == SMB_RECV_REQUEST)
  224. smbiod_handle_request(server);
  225. } while (result > 0 && maxwork-- > 0);
  226. /*
  227. * If there is more to read then we want to be sure to wake up again.
  228. */
  229. if (server->state != CONN_VALID)
  230. goto out;
  231. if (smb_recv_available(server) > 0)
  232. set_bit(SMBIOD_DATA_READY, &smbiod_flags);
  233. do {
  234. result = smb_request_send_server(server);
  235. if (result < 0) {
  236. server->state = CONN_INVALID;
  237. smbiod_retry(server);
  238. goto out; /* reconnecting is slow */
  239. }
  240. } while (result > 0);
  241. /*
  242. * If the last request was not sent out we want to wake up again.
  243. */
  244. if (!list_empty(&server->xmitq))
  245. set_bit(SMBIOD_DATA_READY, &smbiod_flags);
  246. out:
  247. return;
  248. }
  249. /*
  250. * smbiod kernel thread
  251. */
  252. static int smbiod(void *unused)
  253. {
  254. daemonize("smbiod");
  255. allow_signal(SIGKILL);
  256. VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
  257. for (;;) {
  258. struct smb_sb_info *server;
  259. struct list_head *pos, *n;
  260. /* FIXME: Use poll? */
  261. wait_event_interruptible(smbiod_wait,
  262. test_bit(SMBIOD_DATA_READY, &smbiod_flags));
  263. if (signal_pending(current)) {
  264. spin_lock(&servers_lock);
  265. smbiod_state = SMBIOD_DEAD;
  266. spin_unlock(&servers_lock);
  267. break;
  268. }
  269. clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
  270. spin_lock(&servers_lock);
  271. if (list_empty(&smb_servers)) {
  272. smbiod_state = SMBIOD_DEAD;
  273. spin_unlock(&servers_lock);
  274. break;
  275. }
  276. list_for_each_safe(pos, n, &smb_servers) {
  277. server = list_entry(pos, struct smb_sb_info, entry);
  278. VERBOSE("checking server %p\n", server);
  279. if (server->state == CONN_VALID) {
  280. spin_unlock(&servers_lock);
  281. smb_lock_server(server);
  282. smbiod_doio(server);
  283. smb_unlock_server(server);
  284. spin_lock(&servers_lock);
  285. }
  286. }
  287. spin_unlock(&servers_lock);
  288. }
  289. VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
  290. module_put_and_exit(0);
  291. }