trans_fd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * linux/fs/9p/trans_fd.c
  3. *
  4. * File Descriptor Transport Layer
  5. *
  6. * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #include <linux/config.h>
  26. #include <linux/module.h>
  27. #include <linux/net.h>
  28. #include <linux/ipv6.h>
  29. #include <linux/errno.h>
  30. #include <linux/kernel.h>
  31. #include <linux/un.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/inet.h>
  34. #include <linux/idr.h>
  35. #include <linux/file.h>
  36. #include "debug.h"
  37. #include "v9fs.h"
  38. #include "transport.h"
  39. struct v9fs_trans_fd {
  40. struct file *in_file;
  41. struct file *out_file;
  42. };
  43. /**
  44. * v9fs_fd_recv - receive from a socket
  45. * @v9ses: session information
  46. * @v: buffer to receive data into
  47. * @len: size of receive buffer
  48. *
  49. */
  50. static int v9fs_fd_recv(struct v9fs_transport *trans, void *v, int len)
  51. {
  52. struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
  53. if (!trans || trans->status != Connected || !ts)
  54. return -EIO;
  55. return kernel_read(ts->in_file, ts->in_file->f_pos, v, len);
  56. }
  57. /**
  58. * v9fs_fd_send - send to a socket
  59. * @v9ses: session information
  60. * @v: buffer to send data from
  61. * @len: size of send buffer
  62. *
  63. */
  64. static int v9fs_fd_send(struct v9fs_transport *trans, void *v, int len)
  65. {
  66. struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
  67. mm_segment_t oldfs = get_fs();
  68. int ret = 0;
  69. if (!trans || trans->status != Connected || !ts)
  70. return -EIO;
  71. set_fs(get_ds());
  72. /* The cast to a user pointer is valid due to the set_fs() */
  73. ret = vfs_write(ts->out_file, (void __user *)v, len, &ts->out_file->f_pos);
  74. set_fs(oldfs);
  75. return ret;
  76. }
  77. /**
  78. * v9fs_fd_init - initialize file descriptor transport
  79. * @v9ses: session information
  80. * @addr: address of server to mount
  81. * @data: mount options
  82. *
  83. */
  84. static int
  85. v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
  86. {
  87. struct v9fs_trans_fd *ts = NULL;
  88. struct v9fs_transport *trans = v9ses->transport;
  89. if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
  90. printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
  91. return -ENOPROTOOPT;
  92. }
  93. sema_init(&trans->writelock, 1);
  94. sema_init(&trans->readlock, 1);
  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. trans->status = Disconnected;
  123. ts = trans->priv;
  124. if (!ts)
  125. return;
  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. struct v9fs_transport v9fs_trans_fd = {
  133. .init = v9fs_fd_init,
  134. .write = v9fs_fd_send,
  135. .read = v9fs_fd_recv,
  136. .close = v9fs_fd_close,
  137. };