trans_sock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * linux/fs/9p/trans_socket.c
  3. *
  4. * Socket Transport Layer
  5. *
  6. * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
  7. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  8. * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
  9. * Copyright (C) 1995, 1996 by Olaf Kirch <okir@monad.swb.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to:
  23. * Free Software Foundation
  24. * 51 Franklin Street, Fifth Floor
  25. * Boston, MA 02111-1301 USA
  26. *
  27. */
  28. #include <linux/config.h>
  29. #include <linux/in.h>
  30. #include <linux/module.h>
  31. #include <linux/net.h>
  32. #include <linux/ipv6.h>
  33. #include <linux/errno.h>
  34. #include <linux/kernel.h>
  35. #include <linux/un.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/inet.h>
  38. #include <linux/idr.h>
  39. #include <linux/file.h>
  40. #include "debug.h"
  41. #include "v9fs.h"
  42. #include "transport.h"
  43. #define V9FS_PORT 564
  44. struct v9fs_trans_sock {
  45. struct socket *s;
  46. struct file *filp;
  47. };
  48. /**
  49. * v9fs_sock_recv - receive from a socket
  50. * @v9ses: session information
  51. * @v: buffer to receive data into
  52. * @len: size of receive buffer
  53. *
  54. */
  55. static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
  56. {
  57. int ret;
  58. struct v9fs_trans_sock *ts;
  59. if (!trans || trans->status == Disconnected) {
  60. dprintk(DEBUG_ERROR, "disconnected ...\n");
  61. return -EREMOTEIO;
  62. }
  63. ts = trans->priv;
  64. if (!(ts->filp->f_flags & O_NONBLOCK))
  65. dprintk(DEBUG_ERROR, "blocking read ...\n");
  66. ret = kernel_read(ts->filp, ts->filp->f_pos, v, len);
  67. if (ret <= 0) {
  68. if (ret != -ERESTARTSYS && ret != -EAGAIN)
  69. trans->status = Disconnected;
  70. }
  71. return ret;
  72. }
  73. /**
  74. * v9fs_sock_send - send to a socket
  75. * @v9ses: session information
  76. * @v: buffer to send data from
  77. * @len: size of send buffer
  78. *
  79. */
  80. static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len)
  81. {
  82. int ret;
  83. mm_segment_t oldfs;
  84. struct v9fs_trans_sock *ts;
  85. if (!trans || trans->status == Disconnected) {
  86. dprintk(DEBUG_ERROR, "disconnected ...\n");
  87. return -EREMOTEIO;
  88. }
  89. ts = trans->priv;
  90. if (!ts) {
  91. dprintk(DEBUG_ERROR, "no transport ...\n");
  92. return -EREMOTEIO;
  93. }
  94. if (!(ts->filp->f_flags & O_NONBLOCK))
  95. dprintk(DEBUG_ERROR, "blocking write ...\n");
  96. oldfs = get_fs();
  97. set_fs(get_ds());
  98. ret = vfs_write(ts->filp, (void __user *)v, len, &ts->filp->f_pos);
  99. set_fs(oldfs);
  100. if (ret < 0) {
  101. if (ret != -ERESTARTSYS)
  102. trans->status = Disconnected;
  103. }
  104. return ret;
  105. }
  106. static unsigned int v9fs_sock_poll(struct v9fs_transport *trans,
  107. struct poll_table_struct *pt) {
  108. int ret;
  109. struct v9fs_trans_sock *ts;
  110. mm_segment_t oldfs;
  111. if (!trans) {
  112. dprintk(DEBUG_ERROR, "no transport\n");
  113. return -EIO;
  114. }
  115. ts = trans->priv;
  116. if (trans->status != Connected || !ts) {
  117. dprintk(DEBUG_ERROR, "transport disconnected: %d\n", trans->status);
  118. return -EIO;
  119. }
  120. oldfs = get_fs();
  121. set_fs(get_ds());
  122. if (!ts->filp->f_op || !ts->filp->f_op->poll) {
  123. dprintk(DEBUG_ERROR, "no poll operation\n");
  124. ret = -EIO;
  125. goto end;
  126. }
  127. ret = ts->filp->f_op->poll(ts->filp, pt);
  128. end:
  129. set_fs(oldfs);
  130. return ret;
  131. }
  132. /**
  133. * v9fs_tcp_init - initialize TCP socket
  134. * @v9ses: session information
  135. * @addr: address of server to mount
  136. * @data: mount options
  137. *
  138. */
  139. static int
  140. v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
  141. {
  142. struct socket *csocket = NULL;
  143. struct sockaddr_in sin_server;
  144. int rc = 0;
  145. struct v9fs_trans_sock *ts = NULL;
  146. struct v9fs_transport *trans = v9ses->transport;
  147. int fd;
  148. trans->status = Disconnected;
  149. ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
  150. if (!ts)
  151. return -ENOMEM;
  152. trans->priv = ts;
  153. ts->s = NULL;
  154. ts->filp = NULL;
  155. if (!addr)
  156. return -EINVAL;
  157. dprintk(DEBUG_TRANS, "Connecting to %s\n", addr);
  158. sin_server.sin_family = AF_INET;
  159. sin_server.sin_addr.s_addr = in_aton(addr);
  160. sin_server.sin_port = htons(v9ses->port);
  161. sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
  162. rc = csocket->ops->connect(csocket,
  163. (struct sockaddr *)&sin_server,
  164. sizeof(struct sockaddr_in), 0);
  165. if (rc < 0) {
  166. eprintk(KERN_ERR,
  167. "v9fs_trans_tcp: problem connecting socket to %s\n",
  168. addr);
  169. return rc;
  170. }
  171. csocket->sk->sk_allocation = GFP_NOIO;
  172. fd = sock_map_fd(csocket);
  173. if (fd < 0) {
  174. sock_release(csocket);
  175. kfree(ts);
  176. trans->priv = NULL;
  177. return fd;
  178. }
  179. ts->s = csocket;
  180. ts->filp = fget(fd);
  181. ts->filp->f_flags |= O_NONBLOCK;
  182. trans->status = Connected;
  183. return 0;
  184. }
  185. /**
  186. * v9fs_unix_init - initialize UNIX domain socket
  187. * @v9ses: session information
  188. * @dev_name: path to named pipe
  189. * @data: mount options
  190. *
  191. */
  192. static int
  193. v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
  194. char *data)
  195. {
  196. int rc, fd;
  197. struct socket *csocket;
  198. struct sockaddr_un sun_server;
  199. struct v9fs_transport *trans;
  200. struct v9fs_trans_sock *ts;
  201. rc = 0;
  202. csocket = NULL;
  203. trans = v9ses->transport;
  204. trans->status = Disconnected;
  205. if (strlen(dev_name) > UNIX_PATH_MAX) {
  206. eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
  207. dev_name);
  208. return -ENOMEM;
  209. }
  210. ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
  211. if (!ts)
  212. return -ENOMEM;
  213. trans->priv = ts;
  214. ts->s = NULL;
  215. ts->filp = NULL;
  216. sun_server.sun_family = PF_UNIX;
  217. strcpy(sun_server.sun_path, dev_name);
  218. sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
  219. rc = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
  220. sizeof(struct sockaddr_un) - 1, 0); /* -1 *is* important */
  221. if (rc < 0) {
  222. eprintk(KERN_ERR,
  223. "v9fs_trans_unix: problem connecting socket: %s: %d\n",
  224. dev_name, rc);
  225. return rc;
  226. }
  227. csocket->sk->sk_allocation = GFP_NOIO;
  228. fd = sock_map_fd(csocket);
  229. if (fd < 0) {
  230. sock_release(csocket);
  231. kfree(ts);
  232. trans->priv = NULL;
  233. return fd;
  234. }
  235. ts->s = csocket;
  236. ts->filp = fget(fd);
  237. ts->filp->f_flags |= O_NONBLOCK;
  238. trans->status = Connected;
  239. return 0;
  240. }
  241. /**
  242. * v9fs_sock_close - shutdown socket
  243. * @trans: private socket structure
  244. *
  245. */
  246. static void v9fs_sock_close(struct v9fs_transport *trans)
  247. {
  248. struct v9fs_trans_sock *ts;
  249. if (!trans)
  250. return;
  251. ts = trans->priv;
  252. if ((ts) && (ts->filp)) {
  253. fput(ts->filp);
  254. ts->filp = NULL;
  255. ts->s = NULL;
  256. trans->status = Disconnected;
  257. }
  258. kfree(ts);
  259. trans->priv = NULL;
  260. }
  261. struct v9fs_transport v9fs_trans_tcp = {
  262. .init = v9fs_tcp_init,
  263. .write = v9fs_sock_send,
  264. .read = v9fs_sock_recv,
  265. .close = v9fs_sock_close,
  266. .poll = v9fs_sock_poll,
  267. };
  268. struct v9fs_transport v9fs_trans_unix = {
  269. .init = v9fs_unix_init,
  270. .write = v9fs_sock_send,
  271. .read = v9fs_sock_recv,
  272. .close = v9fs_sock_close,
  273. .poll = v9fs_sock_poll,
  274. };