smbiod.c 7.5 KB

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