trans_fd.c 7.0 KB

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