revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / net / 9p / trans_virtio.c
blob40b71a29fc3faa11b8360ddab2d3fbcc6fe29276
1 /*
2 * The Guest 9p transport driver
4 * This is a trivial pipe-based transport driver based on the lguest console
5 * code: we use lguest's DMA mechanism to send bytes out, and register a
6 * DMA buffer to receive bytes in. It is assumed to be present and available
7 * from the very beginning of boot.
9 * This may be have been done by just instaniating another HVC console,
10 * but HVC's blocksize of 16 bytes is annoying and painful to performance.
12 * A more efficient transport could be built based on the virtio block driver
13 * but it requires some changes in the 9p transport model (which are in
14 * progress)
18 * Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
20 * Based on virtio console driver
21 * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License version 2
25 * as published by the Free Software Foundation.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to:
34 * Free Software Foundation
35 * 51 Franklin Street, Fifth Floor
36 * Boston, MA 02111-1301 USA
40 #include <linux/in.h>
41 #include <linux/module.h>
42 #include <linux/net.h>
43 #include <linux/ipv6.h>
44 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/un.h>
47 #include <linux/uaccess.h>
48 #include <linux/inet.h>
49 #include <linux/idr.h>
50 #include <linux/file.h>
51 #include <net/9p/9p.h>
52 #include <linux/parser.h>
53 #include <net/9p/transport.h>
54 #include <linux/scatterlist.h>
55 #include <linux/virtio.h>
56 #include <linux/virtio_9p.h>
58 /* a single mutex to manage channel initialization and attachment */
59 static DECLARE_MUTEX(virtio_9p_lock);
60 /* global which tracks highest initialized channel */
61 static int chan_index;
63 /* We keep all per-channel information in a structure.
64 * This structure is allocated within the devices dev->mem space.
65 * A pointer to the structure will get put in the transport private.
67 static struct virtio_chan {
68 bool initialized; /* channel is initialized */
69 bool inuse; /* channel is in use */
71 struct virtqueue *in_vq, *out_vq;
72 struct virtio_device *vdev;
74 /* This is our input buffer, and how much data is left in it. */
75 unsigned int in_len;
76 char *in, *inbuf;
78 wait_queue_head_t wq; /* waitq for buffer */
79 } channels[MAX_9P_CHAN];
81 /* How many bytes left in this page. */
82 static unsigned int rest_of_page(void *data)
84 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
87 static int p9_virtio_write(struct p9_trans *trans, void *buf, int count)
89 struct virtio_chan *chan = (struct virtio_chan *) trans->priv;
90 struct virtqueue *out_vq = chan->out_vq;
91 struct scatterlist sg[1];
92 unsigned int len;
94 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio write (%d)\n", count);
96 /* keep it simple - make sure we don't overflow a page */
97 if (rest_of_page(buf) < count)
98 count = rest_of_page(buf);
100 sg_init_one(sg, buf, count);
102 /* add_buf wants a token to identify this buffer: we hand it any
103 * non-NULL pointer, since there's only ever one buffer. */
104 if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) == 0) {
105 /* Tell Host to go! */
106 out_vq->vq_ops->kick(out_vq);
107 /* Chill out until it's done with the buffer. */
108 while (!out_vq->vq_ops->get_buf(out_vq, &len))
109 cpu_relax();
112 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio wrote (%d)\n", count);
114 /* We're expected to return the amount of data we wrote: all of it. */
115 return count;
118 /* Create a scatter-gather list representing our input buffer and put it in the
119 * queue. */
120 static void add_inbuf(struct virtio_chan *chan)
122 struct scatterlist sg[1];
124 sg_init_one(sg, chan->inbuf, PAGE_SIZE);
126 /* We should always be able to add one buffer to an empty queue. */
127 if (chan->in_vq->vq_ops->add_buf(chan->in_vq, sg, 0, 1, chan->inbuf))
128 BUG();
129 chan->in_vq->vq_ops->kick(chan->in_vq);
132 static int p9_virtio_read(struct p9_trans *trans, void *buf, int count)
134 struct virtio_chan *chan = (struct virtio_chan *) trans->priv;
135 struct virtqueue *in_vq = chan->in_vq;
137 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio read (%d)\n", count);
139 /* If we don't have an input queue yet, we can't get input. */
140 BUG_ON(!in_vq);
142 /* No buffer? Try to get one. */
143 if (!chan->in_len) {
144 chan->in = in_vq->vq_ops->get_buf(in_vq, &chan->in_len);
145 if (!chan->in)
146 return 0;
149 /* You want more than we have to give? Well, try wanting less! */
150 if (chan->in_len < count)
151 count = chan->in_len;
153 /* Copy across to their buffer and increment offset. */
154 memcpy(buf, chan->in, count);
155 chan->in += count;
156 chan->in_len -= count;
158 /* Finished? Re-register buffer so Host will use it again. */
159 if (chan->in_len == 0)
160 add_inbuf(chan);
162 P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio finished read (%d)\n",
163 count);
165 return count;
168 /* The poll function is used by 9p transports to determine if there
169 * is there is activity available on a particular channel. In our case
170 * we use it to wait for a callback from the input routines.
172 static unsigned int
173 p9_virtio_poll(struct p9_trans *trans, struct poll_table_struct *pt)
175 struct virtio_chan *chan = (struct virtio_chan *)trans->priv;
176 struct virtqueue *in_vq = chan->in_vq;
177 int ret = POLLOUT; /* we can always handle more output */
179 poll_wait(NULL, &chan->wq, pt);
181 /* No buffer? Try to get one. */
182 if (!chan->in_len)
183 chan->in = in_vq->vq_ops->get_buf(in_vq, &chan->in_len);
185 if (chan->in_len)
186 ret |= POLLIN;
188 return ret;
191 static void p9_virtio_close(struct p9_trans *trans)
193 struct virtio_chan *chan = trans->priv;
195 down(&virtio_9p_lock);
196 chan->inuse = false;
197 up(&virtio_9p_lock);
199 kfree(trans);
202 static bool p9_virtio_intr(struct virtqueue *q)
204 struct virtio_chan *chan = q->vdev->priv;
206 P9_DPRINTK(P9_DEBUG_TRANS, "9p poll_wakeup: %p\n", &chan->wq);
207 wake_up_interruptible(&chan->wq);
209 return true;
212 static int p9_virtio_probe(struct virtio_device *dev)
214 int err;
215 struct virtio_chan *chan;
216 int index;
218 down(&virtio_9p_lock);
219 index = chan_index++;
220 chan = &channels[index];
221 up(&virtio_9p_lock);
223 if (chan_index > MAX_9P_CHAN) {
224 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
225 BUG();
228 chan->vdev = dev;
230 /* This is the scratch page we use to receive console input */
231 chan->inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
232 if (!chan->inbuf) {
233 err = -ENOMEM;
234 goto fail;
237 /* Find the input queue. */
238 dev->priv = chan;
239 chan->in_vq = dev->config->find_vq(dev, p9_virtio_intr);
240 if (IS_ERR(chan->in_vq)) {
241 err = PTR_ERR(chan->in_vq);
242 goto free;
245 chan->out_vq = dev->config->find_vq(dev, NULL);
246 if (IS_ERR(chan->out_vq)) {
247 err = PTR_ERR(chan->out_vq);
248 goto free_in_vq;
251 init_waitqueue_head(&chan->wq);
253 /* Register the input buffer the first time. */
254 add_inbuf(chan);
255 chan->inuse = false;
256 chan->initialized = true;
258 return 0;
260 free_in_vq:
261 dev->config->del_vq(chan->in_vq);
262 free:
263 kfree(chan->inbuf);
264 fail:
265 down(&virtio_9p_lock);
266 chan_index--;
267 up(&virtio_9p_lock);
268 return err;
271 /* This sets up a transport channel for 9p communication. Right now
272 * we only match the first available channel, but eventually we couldlook up
273 * alternate channels by matching devname versus a virtio_config entry.
274 * We use a simple reference count mechanism to ensure that only a single
275 * mount has a channel open at a time. */
276 static struct p9_trans *p9_virtio_create(const char *devname, char *args)
278 struct p9_trans *trans;
279 int index = 0;
280 struct virtio_chan *chan = channels;
282 down(&virtio_9p_lock);
283 while (index < MAX_9P_CHAN) {
284 if (chan->initialized && !chan->inuse) {
285 chan->inuse = true;
286 break;
287 } else {
288 index++;
289 chan = &channels[index];
292 up(&virtio_9p_lock);
294 if (index >= MAX_9P_CHAN) {
295 printk(KERN_ERR "9p: virtio: couldn't find a free channel\n");
296 return NULL;
299 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
300 if (!trans) {
301 printk(KERN_ERR "9p: couldn't allocate transport\n");
302 return ERR_PTR(-ENOMEM);
305 trans->write = p9_virtio_write;
306 trans->read = p9_virtio_read;
307 trans->close = p9_virtio_close;
308 trans->poll = p9_virtio_poll;
309 trans->priv = chan;
311 return trans;
314 #define VIRTIO_ID_9P 9
316 static struct virtio_device_id id_table[] = {
317 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
318 { 0 },
321 /* The standard "struct lguest_driver": */
322 static struct virtio_driver p9_virtio_drv = {
323 .driver.name = KBUILD_MODNAME,
324 .driver.owner = THIS_MODULE,
325 .id_table = id_table,
326 .probe = p9_virtio_probe,
329 static struct p9_trans_module p9_virtio_trans = {
330 .name = "virtio",
331 .create = p9_virtio_create,
332 .maxsize = PAGE_SIZE,
333 .def = 0,
336 /* The standard init function */
337 static int __init p9_virtio_init(void)
339 int count;
341 for (count = 0; count < MAX_9P_CHAN; count++)
342 channels[count].initialized = false;
344 v9fs_register_trans(&p9_virtio_trans);
345 return register_virtio_driver(&p9_virtio_drv);
348 module_init(p9_virtio_init);
350 MODULE_DEVICE_TABLE(virtio, id_table);
351 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
352 MODULE_DESCRIPTION("Virtio 9p Transport");
353 MODULE_LICENSE("GPL");