can: avoids a false warning
[safe/jmp/linux-2.6] / net / 9p / trans_virtio.c
index 2d7781e..7eb78ec 100644 (file)
@@ -37,6 +37,7 @@
 #include <linux/inet.h>
 #include <linux/idr.h>
 #include <linux/file.h>
+#include <linux/slab.h>
 #include <net/9p/9p.h>
 #include <linux/parser.h>
 #include <net/9p/client.h>
 
 /* a single mutex to manage channel initialization and attachment */
 static DEFINE_MUTEX(virtio_9p_lock);
-/* global which tracks highest initialized channel */
-static int chan_index;
 
 /**
  * struct virtio_chan - per-instance transport information
  * @initialized: whether the channel is initialized
  * @inuse: whether the channel is in use
  * @lock: protects multiple elements within this structure
+ * @client: client instance
  * @vdev: virtio dev associated with this channel
  * @vq: virtio queue associated with this channel
- * @tagpool: accounting for tag ids (and request slots)
- * @reqs: array of request slots
- * @max_tag: current number of request_slots allocated
  * @sg: scatter gather list which is used to pack a request (protected?)
  *
  * We keep all per-channel information in a structure.
@@ -70,8 +67,7 @@ static int chan_index;
  *
  */
 
-static struct virtio_chan {
-       bool initialized;
+struct virtio_chan {
        bool inuse;
 
        spinlock_t lock;
@@ -82,7 +78,17 @@ static struct virtio_chan {
 
        /* Scatterlist: can be too big for stack. */
        struct scatterlist sg[VIRTQUEUE_NUM];
-} channels[MAX_9P_CHAN];
+
+       int tag_len;
+       /*
+        * tag name to identify a mount Non-null terminated
+        */
+       char *tag;
+
+       struct list_head chan_list;
+};
+
+static struct list_head virtio_chan_list;
 
 /* How many bytes left in this page. */
 static unsigned int rest_of_page(void *data)
@@ -92,7 +98,7 @@ static unsigned int rest_of_page(void *data)
 
 /**
  * p9_virtio_close - reclaim resources of a channel
- * @trans: transport state
+ * @client: client instance
  *
  * This reclaims a channel by freeing its resources and
  * reseting its inuse flag.
@@ -104,7 +110,8 @@ static void p9_virtio_close(struct p9_client *client)
        struct virtio_chan *chan = client->trans;
 
        mutex_lock(&virtio_9p_lock);
-       chan->inuse = false;
+       if (chan)
+               chan->inuse = false;
        mutex_unlock(&virtio_9p_lock);
 }
 
@@ -134,6 +141,7 @@ static void req_done(struct virtqueue *vq)
                P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc);
                P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag);
                req = p9_tag_lookup(chan->client, rc->tag);
+               req->status = REQ_STATUS_RCVD;
                p9_client_cb(chan->client, req);
        }
 }
@@ -180,9 +188,8 @@ static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
 
 /**
  * p9_virtio_request - issue a request
- * @t: transport state
- * @tc: &p9_fcall request to transmit
- * @rc: &p9_fcall to put reponse into
+ * @client: client instance issuing the request
+ * @req: request to be issued
  *
  */
 
@@ -202,7 +209,7 @@ p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
 
        req->status = REQ_STATUS_SENT;
 
-       if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, req->tc)) {
+       if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, req->tc) < 0) {
                P9_DPRINTK(P9_DEBUG_TRANS,
                        "9p debug: virtio rpc add_buf returned failure");
                return -EIO;
@@ -214,30 +221,38 @@ p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
        return 0;
 }
 
+static ssize_t p9_mount_tag_show(struct device *dev,
+                               struct device_attribute *attr, char *buf)
+{
+       struct virtio_chan *chan;
+       struct virtio_device *vdev;
+
+       vdev = dev_to_virtio(dev);
+       chan = vdev->priv;
+
+       return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
+}
+
+static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
+
 /**
  * p9_virtio_probe - probe for existence of 9P virtio channels
  * @vdev: virtio device to probe
  *
- * This probes for existing virtio channels.  At present only
- * a single channel is in use, so in the future more work may need
- * to be done here.
+ * This probes for existing virtio channels.
  *
  */
 
 static int p9_virtio_probe(struct virtio_device *vdev)
 {
+       __u16 tag_len;
+       char *tag;
        int err;
        struct virtio_chan *chan;
-       int index;
 
-       mutex_lock(&virtio_9p_lock);
-       index = chan_index++;
-       chan = &channels[index];
-       mutex_unlock(&virtio_9p_lock);
-
-       if (chan_index > MAX_9P_CHAN) {
-               printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
-               BUG();
+       chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
+       if (!chan) {
+               printk(KERN_ERR "9p: Failed to allocate virtio 9P channel\n");
                err = -ENOMEM;
                goto fail;
        }
@@ -245,7 +260,7 @@ static int p9_virtio_probe(struct virtio_device *vdev)
        chan->vdev = vdev;
 
        /* We expect one virtqueue, for requests. */
-       chan->vq = vdev->config->find_vq(vdev, 0, req_done);
+       chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
        if (IS_ERR(chan->vq)) {
                err = PTR_ERR(chan->vq);
                goto out_free_vq;
@@ -256,15 +271,37 @@ static int p9_virtio_probe(struct virtio_device *vdev)
        sg_init_table(chan->sg, VIRTQUEUE_NUM);
 
        chan->inuse = false;
-       chan->initialized = true;
+       if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
+               vdev->config->get(vdev,
+                               offsetof(struct virtio_9p_config, tag_len),
+                               &tag_len, sizeof(tag_len));
+       } else {
+               err = -EINVAL;
+               goto out_free_vq;
+       }
+       tag = kmalloc(tag_len, GFP_KERNEL);
+       if (!tag) {
+               err = -ENOMEM;
+               goto out_free_vq;
+       }
+       vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
+                       tag, tag_len);
+       chan->tag = tag;
+       chan->tag_len = tag_len;
+       err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
+       if (err) {
+               kfree(tag);
+               goto out_free_vq;
+       }
+       mutex_lock(&virtio_9p_lock);
+       list_add_tail(&chan->chan_list, &virtio_chan_list);
+       mutex_unlock(&virtio_9p_lock);
        return 0;
 
 out_free_vq:
-       vdev->config->del_vq(chan->vq);
+       vdev->config->del_vqs(vdev);
+       kfree(chan);
 fail:
-       mutex_lock(&virtio_9p_lock);
-       chan_index--;
-       mutex_unlock(&virtio_9p_lock);
        return err;
 }
 
@@ -281,38 +318,35 @@ fail:
  * We use a simple reference count mechanism to ensure that only a single
  * mount has a channel open at a time.
  *
- * Bugs: doesn't allow identification of a specific channel
- * to allocate, channels are allocated sequentially. This was
- * a pragmatic decision to get things rolling, but ideally some
- * way of identifying the channel to attach to would be nice
- * if we are going to support multiple channels.
- *
  */
 
 static int
 p9_virtio_create(struct p9_client *client, const char *devname, char *args)
 {
-       struct virtio_chan *chan = channels;
-       int index = 0;
+       struct virtio_chan *chan;
+       int ret = -ENOENT;
+       int found = 0;
 
        mutex_lock(&virtio_9p_lock);
-       while (index < MAX_9P_CHAN) {
-               if (chan->initialized && !chan->inuse) {
-                       chan->inuse = true;
-                       break;
-               } else {
-                       index++;
-                       chan = &channels[index];
+       list_for_each_entry(chan, &virtio_chan_list, chan_list) {
+               if (!strncmp(devname, chan->tag, chan->tag_len)) {
+                       if (!chan->inuse) {
+                               chan->inuse = true;
+                               found = 1;
+                               break;
+                       }
+                       ret = -EBUSY;
                }
        }
        mutex_unlock(&virtio_9p_lock);
 
-       if (index >= MAX_9P_CHAN) {
+       if (!found) {
                printk(KERN_ERR "9p: no channels available\n");
-               return -ENODEV;
+               return ret;
        }
 
        client->trans = (void *)chan;
+       client->status = Connected;
        chan->client = client;
 
        return 0;
@@ -329,27 +363,35 @@ static void p9_virtio_remove(struct virtio_device *vdev)
        struct virtio_chan *chan = vdev->priv;
 
        BUG_ON(chan->inuse);
+       vdev->config->del_vqs(vdev);
 
-       if (chan->initialized) {
-               vdev->config->del_vq(chan->vq);
-               chan->initialized = false;
-       }
-}
+       mutex_lock(&virtio_9p_lock);
+       list_del(&chan->chan_list);
+       mutex_unlock(&virtio_9p_lock);
+       sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
+       kfree(chan->tag);
+       kfree(chan);
 
-#define VIRTIO_ID_9P 9
+}
 
 static struct virtio_device_id id_table[] = {
        { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
        { 0 },
 };
 
+static unsigned int features[] = {
+       VIRTIO_9P_MOUNT_TAG,
+};
+
 /* The standard "struct lguest_driver": */
 static struct virtio_driver p9_virtio_drv = {
-       .driver.name =  KBUILD_MODNAME,
-       .driver.owner = THIS_MODULE,
-       .id_table =     id_table,
-       .probe =        p9_virtio_probe,
-       .remove =       p9_virtio_remove,
+       .feature_table  = features,
+       .feature_table_size = ARRAY_SIZE(features),
+       .driver.name    = KBUILD_MODNAME,
+       .driver.owner   = THIS_MODULE,
+       .id_table       = id_table,
+       .probe          = p9_virtio_probe,
+       .remove         = p9_virtio_remove,
 };
 
 static struct p9_trans_module p9_virtio_trans = {
@@ -366,10 +408,7 @@ static struct p9_trans_module p9_virtio_trans = {
 /* The standard init function */
 static int __init p9_virtio_init(void)
 {
-       int count;
-
-       for (count = 0; count < MAX_9P_CHAN; count++)
-               channels[count].initialized = false;
+       INIT_LIST_HEAD(&virtio_chan_list);
 
        v9fs_register_trans(&p9_virtio_trans);
        return register_virtio_driver(&p9_virtio_drv);