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/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. struct pid *pid = get_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_pid(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. put_pid(pid);
  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. }