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/module.h>
  29. #include <linux/net.h>
  30. #include <linux/ipv6.h>
  31. #include <linux/errno.h>
  32. #include <linux/kernel.h>
  33. #include <linux/un.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/inet.h>
  36. #include <linux/idr.h>
  37. #include "debug.h"
  38. #include "v9fs.h"
  39. #include "transport.h"
  40. #define V9FS_PORT 564
  41. struct v9fs_trans_sock {
  42. struct socket *s;
  43. };
  44. /**
  45. * v9fs_sock_recv - receive from a socket
  46. * @v9ses: session information
  47. * @v: buffer to receive data into
  48. * @len: size of receive buffer
  49. *
  50. */
  51. static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
  52. {
  53. struct msghdr msg;
  54. struct kvec iov;
  55. int result;
  56. mm_segment_t oldfs;
  57. struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
  58. if (trans->status == Disconnected)
  59. return -EREMOTEIO;
  60. result = -EINVAL;
  61. oldfs = get_fs();
  62. set_fs(get_ds());
  63. iov.iov_base = v;
  64. iov.iov_len = len;
  65. msg.msg_name = NULL;
  66. msg.msg_namelen = 0;
  67. msg.msg_iovlen = 1;
  68. msg.msg_control = NULL;
  69. msg.msg_controllen = 0;
  70. msg.msg_namelen = 0;
  71. msg.msg_flags = MSG_NOSIGNAL;
  72. result = kernel_recvmsg(ts->s, &msg, &iov, 1, len, 0);
  73. dprintk(DEBUG_TRANS, "socket state %d\n", ts->s->state);
  74. set_fs(oldfs);
  75. if (result <= 0) {
  76. if (result != -ERESTARTSYS)
  77. trans->status = Disconnected;
  78. }
  79. return result;
  80. }
  81. /**
  82. * v9fs_sock_send - send to a socket
  83. * @v9ses: session information
  84. * @v: buffer to send data from
  85. * @len: size of send buffer
  86. *
  87. */
  88. static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len)
  89. {
  90. struct kvec iov;
  91. struct msghdr msg;
  92. int result = -1;
  93. mm_segment_t oldfs;
  94. struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
  95. dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len);
  96. dump_data(v, len);
  97. down(&trans->writelock);
  98. oldfs = get_fs();
  99. set_fs(get_ds());
  100. iov.iov_base = v;
  101. iov.iov_len = len;
  102. msg.msg_name = NULL;
  103. msg.msg_namelen = 0;
  104. msg.msg_iovlen = 1;
  105. msg.msg_control = NULL;
  106. msg.msg_controllen = 0;
  107. msg.msg_namelen = 0;
  108. msg.msg_flags = MSG_NOSIGNAL;
  109. result = kernel_sendmsg(ts->s, &msg, &iov, 1, len);
  110. set_fs(oldfs);
  111. if (result < 0) {
  112. if (result != -ERESTARTSYS)
  113. trans->status = Disconnected;
  114. }
  115. up(&trans->writelock);
  116. return result;
  117. }
  118. /**
  119. * v9fs_tcp_init - initialize TCP socket
  120. * @v9ses: session information
  121. * @addr: address of server to mount
  122. * @data: mount options
  123. *
  124. */
  125. static int
  126. v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
  127. {
  128. struct socket *csocket = NULL;
  129. struct sockaddr_in sin_server;
  130. int rc = 0;
  131. struct v9fs_trans_sock *ts = NULL;
  132. struct v9fs_transport *trans = v9ses->transport;
  133. sema_init(&trans->writelock, 1);
  134. sema_init(&trans->readlock, 1);
  135. ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
  136. if (!ts)
  137. return -ENOMEM;
  138. trans->priv = ts;
  139. ts->s = NULL;
  140. if (!addr)
  141. return -EINVAL;
  142. dprintk(DEBUG_TRANS, "Connecting to %s\n", addr);
  143. sin_server.sin_family = AF_INET;
  144. sin_server.sin_addr.s_addr = in_aton(addr);
  145. sin_server.sin_port = htons(v9ses->port);
  146. sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
  147. rc = csocket->ops->connect(csocket,
  148. (struct sockaddr *)&sin_server,
  149. sizeof(struct sockaddr_in), 0);
  150. if (rc < 0) {
  151. eprintk(KERN_ERR,
  152. "v9fs_trans_tcp: problem connecting socket to %s\n",
  153. addr);
  154. return rc;
  155. }
  156. csocket->sk->sk_allocation = GFP_NOIO;
  157. ts->s = csocket;
  158. trans->status = Connected;
  159. return 0;
  160. }
  161. /**
  162. * v9fs_unix_init - initialize UNIX domain socket
  163. * @v9ses: session information
  164. * @dev_name: path to named pipe
  165. * @data: mount options
  166. *
  167. */
  168. static int
  169. v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
  170. char *data)
  171. {
  172. int rc;
  173. struct socket *csocket;
  174. struct sockaddr_un sun_server;
  175. struct v9fs_transport *trans;
  176. struct v9fs_trans_sock *ts;
  177. rc = 0;
  178. csocket = NULL;
  179. trans = v9ses->transport;
  180. if (strlen(dev_name) > UNIX_PATH_MAX) {
  181. eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
  182. dev_name);
  183. return -ENOMEM;
  184. }
  185. ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
  186. if (!ts)
  187. return -ENOMEM;
  188. trans->priv = ts;
  189. ts->s = NULL;
  190. sema_init(&trans->writelock, 1);
  191. sema_init(&trans->readlock, 1);
  192. sun_server.sun_family = PF_UNIX;
  193. strcpy(sun_server.sun_path, dev_name);
  194. sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
  195. rc = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
  196. sizeof(struct sockaddr_un) - 1, 0); /* -1 *is* important */
  197. if (rc < 0) {
  198. eprintk(KERN_ERR,
  199. "v9fs_trans_unix: problem connecting socket: %s: %d\n",
  200. dev_name, rc);
  201. return rc;
  202. }
  203. csocket->sk->sk_allocation = GFP_NOIO;
  204. ts->s = csocket;
  205. trans->status = Connected;
  206. return 0;
  207. }
  208. /**
  209. * v9fs_sock_close - shutdown socket
  210. * @trans: private socket structure
  211. *
  212. */
  213. static void v9fs_sock_close(struct v9fs_transport *trans)
  214. {
  215. struct v9fs_trans_sock *ts;
  216. if (!trans)
  217. return;
  218. ts = trans->priv;
  219. if ((ts) && (ts->s)) {
  220. dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s);
  221. sock_release(ts->s);
  222. ts->s = NULL;
  223. trans->status = Disconnected;
  224. dprintk(DEBUG_TRANS, "socket closed\n");
  225. }
  226. if (ts)
  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. };