trans_fd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. set_fs(get_ds());
  73. /* The cast to a user pointer is valid due to the set_fs() */
  74. ret = vfs_write(ts->out_file, (void __user *)v, len, &ts->out_file->f_pos);
  75. set_fs(oldfs);
  76. return ret;
  77. }
  78. /**
  79. * v9fs_fd_init - initialize file descriptor transport
  80. * @v9ses: session information
  81. * @addr: address of server to mount
  82. * @data: mount options
  83. *
  84. */
  85. static int
  86. v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
  87. {
  88. struct v9fs_trans_fd *ts = NULL;
  89. struct v9fs_transport *trans = v9ses->transport;
  90. if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
  91. printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
  92. return -ENOPROTOOPT;
  93. }
  94. ts = kmalloc(sizeof(struct v9fs_trans_fd), GFP_KERNEL);
  95. if (!ts)
  96. return -ENOMEM;
  97. ts->in_file = fget( v9ses->rfdno );
  98. ts->out_file = fget( v9ses->wfdno );
  99. if (!ts->in_file || !ts->out_file) {
  100. if (ts->in_file)
  101. fput(ts->in_file);
  102. if (ts->out_file)
  103. fput(ts->out_file);
  104. kfree(ts);
  105. return -EIO;
  106. }
  107. trans->priv = ts;
  108. trans->status = Connected;
  109. return 0;
  110. }
  111. /**
  112. * v9fs_fd_close - shutdown file descriptor
  113. * @trans: private socket structure
  114. *
  115. */
  116. static void v9fs_fd_close(struct v9fs_transport *trans)
  117. {
  118. struct v9fs_trans_fd *ts;
  119. if (!trans)
  120. return;
  121. ts = xchg(&trans->priv, NULL);
  122. if (!ts)
  123. return;
  124. trans->status = Disconnected;
  125. if (ts->in_file)
  126. fput(ts->in_file);
  127. if (ts->out_file)
  128. fput(ts->out_file);
  129. kfree(ts);
  130. }
  131. static unsigned int
  132. v9fs_fd_poll(struct v9fs_transport *trans, struct poll_table_struct *pt)
  133. {
  134. int ret, n;
  135. struct v9fs_trans_fd *ts;
  136. mm_segment_t oldfs;
  137. if (!trans)
  138. return -EIO;
  139. ts = trans->priv;
  140. if (trans->status != Connected || !ts)
  141. return -EIO;
  142. oldfs = get_fs();
  143. set_fs(get_ds());
  144. if (!ts->in_file->f_op || !ts->in_file->f_op->poll) {
  145. ret = -EIO;
  146. goto end;
  147. }
  148. ret = ts->in_file->f_op->poll(ts->in_file, pt);
  149. if (ts->out_file != ts->in_file) {
  150. if (!ts->out_file->f_op || !ts->out_file->f_op->poll) {
  151. ret = -EIO;
  152. goto end;
  153. }
  154. n = ts->out_file->f_op->poll(ts->out_file, pt);
  155. ret &= ~POLLOUT;
  156. n &= ~POLLIN;
  157. ret |= n;
  158. }
  159. end:
  160. set_fs(oldfs);
  161. return ret;
  162. }
  163. struct v9fs_transport v9fs_trans_fd = {
  164. .init = v9fs_fd_init,
  165. .write = v9fs_fd_send,
  166. .read = v9fs_fd_recv,
  167. .close = v9fs_fd_close,
  168. .poll = v9fs_fd_poll,
  169. };