Linux v2.6.15-rc7
[pohmelfs.git] / fs / 9p / trans_fd.c
blob63b58ce98ff45cbc4c1f19eb668b8fb61f18b706
1 /*
2 * linux/fs/9p/trans_fd.c
4 * File Descriptor Transport Layer
6 * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
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.
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.
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
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>
38 #include "debug.h"
39 #include "v9fs.h"
40 #include "transport.h"
42 struct v9fs_trans_fd {
43 struct file *in_file;
44 struct file *out_file;
47 /**
48 * v9fs_fd_recv - receive from a socket
49 * @v9ses: session information
50 * @v: buffer to receive data into
51 * @len: size of receive buffer
55 static int v9fs_fd_recv(struct v9fs_transport *trans, void *v, int len)
57 struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
59 if (!trans || trans->status != Connected || !ts)
60 return -EIO;
62 return kernel_read(ts->in_file, ts->in_file->f_pos, v, len);
65 /**
66 * v9fs_fd_send - send to a socket
67 * @v9ses: session information
68 * @v: buffer to send data from
69 * @len: size of send buffer
73 static int v9fs_fd_send(struct v9fs_transport *trans, void *v, int len)
75 struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
76 mm_segment_t oldfs = get_fs();
77 int ret = 0;
79 if (!trans || trans->status != Connected || !ts)
80 return -EIO;
82 set_fs(get_ds());
83 /* The cast to a user pointer is valid due to the set_fs() */
84 ret = vfs_write(ts->out_file, (void __user *)v, len, &ts->out_file->f_pos);
85 set_fs(oldfs);
87 return ret;
90 /**
91 * v9fs_fd_init - initialize file descriptor transport
92 * @v9ses: session information
93 * @addr: address of server to mount
94 * @data: mount options
98 static int
99 v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
101 struct v9fs_trans_fd *ts = NULL;
102 struct v9fs_transport *trans = v9ses->transport;
104 if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
105 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
106 return -ENOPROTOOPT;
109 sema_init(&trans->writelock, 1);
110 sema_init(&trans->readlock, 1);
112 ts = kmalloc(sizeof(struct v9fs_trans_fd), GFP_KERNEL);
114 if (!ts)
115 return -ENOMEM;
117 ts->in_file = fget( v9ses->rfdno );
118 ts->out_file = fget( v9ses->wfdno );
120 if (!ts->in_file || !ts->out_file) {
121 if (ts->in_file)
122 fput(ts->in_file);
124 if (ts->out_file)
125 fput(ts->out_file);
127 kfree(ts);
128 return -EIO;
131 trans->priv = ts;
132 trans->status = Connected;
134 return 0;
139 * v9fs_fd_close - shutdown file descriptor
140 * @trans: private socket structure
144 static void v9fs_fd_close(struct v9fs_transport *trans)
146 struct v9fs_trans_fd *ts;
148 if (!trans)
149 return;
151 trans->status = Disconnected;
152 ts = trans->priv;
154 if (!ts)
155 return;
157 if (ts->in_file)
158 fput(ts->in_file);
160 if (ts->out_file)
161 fput(ts->out_file);
163 kfree(ts);
166 struct v9fs_transport v9fs_trans_fd = {
167 .init = v9fs_fd_init,
168 .write = v9fs_fd_send,
169 .read = v9fs_fd_recv,
170 .close = v9fs_fd_close,