trans_sock.c 6.3 KB

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