smbiod.c 7.5 KB

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