trans_fd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * linux/fs/9p/trans_fd.c
  3. *
  4. * File Descriptor Transport Layer
  5. *
  6. * Copyright (C) 2005 by Latchesar Ionkov <lucho@ionkov.net>
  7. * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation
  22. * 51 Franklin Street, Fifth Floor
  23. * Boston, MA 02111-1301 USA
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/net.h>
  29. #include <linux/ipv6.h>
  30. #include <linux/errno.h>
  31. #include <linux/kernel.h>
  32. #include <linux/un.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/inet.h>
  35. #include <linux/idr.h>
  36. #include <linux/file.h>
  37. #include "debug.h"
  38. #include "v9fs.h"
  39. #include "transport.h"
  40. struct v9fs_trans_fd {
  41. struct file *in_file;
  42. struct file *out_file;
  43. };
  44. /**
  45. * v9fs_fd_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_fd_recv(struct v9fs_transport *trans, void *v, int len)
  52. {
  53. struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
  54. if (!trans || trans->status != Connected || !ts)
  55. return -EIO;
  56. return kernel_read(ts->in_file, ts->in_file->f_pos, v, len);
  57. }
  58. /**
  59. * v9fs_fd_send - send to a socket
  60. * @v9ses: session information
  61. * @v: buffer to send data from
  62. * @len: size of send buffer
  63. *
  64. */
  65. static int v9fs_fd_send(struct v9fs_transport *trans, void *v, int len)
  66. {
  67. struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
  68. mm_segment_t oldfs = get_fs();
  69. int ret = 0;
  70. if (!trans || trans->status != Connected || !ts)
  71. return -EIO;
  72. oldfs = get_fs();
  73. set_fs(get_ds());
  74. /* The cast to a user pointer is valid due to the set_fs() */
  75. ret = vfs_write(ts->out_file, (void __user *)v, len, &ts->out_file->f_pos);
  76. set_fs(oldfs);
  77. return ret;
  78. }
  79. /**
  80. * v9fs_fd_init - initialize file descriptor transport
  81. * @v9ses: session information
  82. * @addr: address of server to mount
  83. * @data: mount options
  84. *
  85. */
  86. static int
  87. v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
  88. {
  89. struct v9fs_trans_fd *ts = NULL;
  90. struct v9fs_transport *trans = v9ses->transport;
  91. if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
  92. printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
  93. return -ENOPROTOOPT;
  94. }
  95. ts = kmalloc(sizeof(struct v9fs_trans_fd), GFP_KERNEL);
  96. if (!ts)
  97. return -ENOMEM;
  98. ts->in_file = fget( v9ses->rfdno );
  99. ts->out_file = fget( v9ses->wfdno );
  100. if (!ts->in_file || !ts->out_file) {
  101. if (ts->in_file)
  102. fput(ts->in_file);
  103. if (ts->out_file)
  104. fput(ts->out_file);
  105. kfree(ts);
  106. return -EIO;
  107. }
  108. trans->priv = ts;
  109. trans->status = Connected;
  110. return 0;
  111. }
  112. /**
  113. * v9fs_fd_close - shutdown file descriptor
  114. * @trans: private socket structure
  115. *
  116. */
  117. static void v9fs_fd_close(struct v9fs_transport *trans)
  118. {
  119. struct v9fs_trans_fd *ts;
  120. if (!trans)
  121. return;
  122. ts = xchg(&trans->priv, NULL);
  123. if (!ts)
  124. return;
  125. trans->status = Disconnected;
  126. if (ts->in_file)
  127. fput(ts->in_file);
  128. if (ts->out_file)
  129. fput(ts->out_file);
  130. kfree(ts);
  131. }
  132. static unsigned int
  133. v9fs_fd_poll(struct v9fs_transport *trans, struct poll_table_struct *pt)
  134. {
  135. int ret, n;
  136. struct v9fs_trans_fd *ts;
  137. mm_segment_t oldfs;
  138. if (!trans)
  139. return -EIO;
  140. ts = trans->priv;
  141. if (trans->status != Connected || !ts)
  142. return -EIO;
  143. oldfs = get_fs();
  144. set_fs(get_ds());
  145. if (!ts->in_file->f_op || !ts->in_file->f_op->poll) {
  146. ret = -EIO;
  147. goto end;
  148. }
  149. ret = ts->in_file->f_op->poll(ts->in_file, pt);
  150. if (ts->out_file != ts->in_file) {
  151. if (!ts->out_file->f_op || !ts->out_file->f_op->poll) {
  152. ret = -EIO;
  153. goto end;
  154. }
  155. n = ts->out_file->f_op->poll(ts->out_file, pt);
  156. ret &= ~POLLOUT;
  157. n &= ~POLLIN;
  158. ret |= n;
  159. }
  160. end:
  161. set_fs(oldfs);
  162. return ret;
  163. }
  164. struct v9fs_transport v9fs_trans_fd = {
  165. .init = v9fs_fd_init,
  166. .write = v9fs_fd_send,
  167. .read = v9fs_fd_recv,
  168. .close = v9fs_fd_close,
  169. .poll = v9fs_fd_poll,
  170. };