- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xenbus / xenbus_client.c
index 566d2ad..64dcfad 100644 (file)
  */
 
 #include <linux/slab.h>
+#if defined(CONFIG_XEN) || defined(MODULE)
+#include <xen/evtchn.h>
+#include <xen/gnttab.h>
+#else
 #include <linux/types.h>
 #include <linux/spinlock.h>
 #include <linux/vmalloc.h>
 #include <xen/balloon.h>
 #include <xen/events.h>
 #include <xen/grant_table.h>
+#endif
 #include <xen/xenbus.h>
 #include <xen/xen.h>
 
+#if defined(CONFIG_PARAVIRT_XEN)
 #include "xenbus_probe.h"
 
 struct xenbus_map_node {
@@ -60,11 +66,14 @@ static DEFINE_SPINLOCK(xenbus_valloc_lock);
 static LIST_HEAD(xenbus_valloc_pages);
 
 struct xenbus_ring_ops {
-       int (*map)(struct xenbus_device *dev, int gnt, void **vaddr);
+       int (*map)(struct xenbus_device *dev, grant_ref_t gnt, void **vaddr);
        int (*unmap)(struct xenbus_device *dev, void *vaddr);
 };
 
 static const struct xenbus_ring_ops *ring_ops __read_mostly;
+#elif defined(HAVE_XEN_PLATFORM_COMPAT_H)
+#include <xen/platform-compat.h>
+#endif
 
 const char *xenbus_strstate(enum xenbus_state state)
 {
@@ -75,9 +84,9 @@ const char *xenbus_strstate(enum xenbus_state state)
                [ XenbusStateInitialised  ] = "Initialised",
                [ XenbusStateConnected    ] = "Connected",
                [ XenbusStateClosing      ] = "Closing",
-               [ XenbusStateClosed       ] = "Closed",
-               [XenbusStateReconfiguring] = "Reconfiguring",
-               [XenbusStateReconfigured] = "Reconfigured",
+               [ XenbusStateClosed       ] = "Closed",
+               [ XenbusStateReconfiguring ] = "Reconfiguring",
+               [ XenbusStateReconfigured ] = "Reconfigured",
        };
        return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
 }
@@ -120,6 +129,26 @@ int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 EXPORT_SYMBOL_GPL(xenbus_watch_path);
 
 
+#if defined(CONFIG_XEN) || defined(MODULE)
+int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
+                      const char *path2, struct xenbus_watch *watch,
+                      void (*callback)(struct xenbus_watch *,
+                                       const char **, unsigned int))
+{
+       int err;
+       char *state = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", path, path2);
+       if (!state) {
+               xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
+               return -ENOMEM;
+       }
+       err = xenbus_watch_path(dev, state, watch, callback);
+
+       if (err)
+               kfree(state);
+       return err;
+}
+EXPORT_SYMBOL_GPL(xenbus_watch_path2);
+#else
 /**
  * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
  * @dev: xenbus device
@@ -160,6 +189,7 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev,
        return err;
 }
 EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
+#endif
 
 static void xenbus_switch_fatal(struct xenbus_device *, int, int,
                                const char *, ...);
@@ -233,7 +263,6 @@ int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
 {
        return __xenbus_switch_state(dev, state, 0);
 }
-
 EXPORT_SYMBOL_GPL(xenbus_switch_state);
 
 int xenbus_frontend_closed(struct xenbus_device *dev)
@@ -254,41 +283,23 @@ static char *error_path(struct xenbus_device *dev)
 }
 
 
-static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
-                               const char *fmt, va_list ap)
+static void _dev_error(struct xenbus_device *dev, int err,
+                       const char *fmt, va_list *ap)
 {
-       int ret;
-       unsigned int len;
-       char *printf_buffer = NULL;
-       char *path_buffer = NULL;
-
-#define PRINTF_BUFFER_SIZE 4096
-       printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
-       if (printf_buffer == NULL)
-               goto fail;
-
-       len = sprintf(printf_buffer, "%i ", -err);
-       ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
+       char *printf_buffer, *path_buffer;
+       struct va_format vaf = { .fmt = fmt, .va = ap };
 
-       BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
-
-       dev_err(&dev->dev, "%s\n", printf_buffer);
+       printf_buffer = kasprintf(GFP_KERNEL, "%i %pV", -err, &vaf);
+       if (printf_buffer)
+               dev_err(&dev->dev, "%s\n", printf_buffer);
 
        path_buffer = error_path(dev);
+       if (!printf_buffer || !path_buffer
+           || xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer))
+               dev_err(&dev->dev,
+                       "xenbus: failed to write error node for %s (%s)\n",
+                       dev->nodename, printf_buffer);
 
-       if (path_buffer == NULL) {
-               dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
-                      dev->nodename, printf_buffer);
-               goto fail;
-       }
-
-       if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
-               dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
-                      dev->nodename, printf_buffer);
-               goto fail;
-       }
-
-fail:
        kfree(printf_buffer);
        kfree(path_buffer);
 }
@@ -308,11 +319,12 @@ void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
        va_list ap;
 
        va_start(ap, fmt);
-       xenbus_va_dev_error(dev, err, fmt, ap);
+       _dev_error(dev, err, fmt, &ap);
        va_end(ap);
 }
 EXPORT_SYMBOL_GPL(xenbus_dev_error);
 
+
 /**
  * xenbus_dev_fatal
  * @dev: xenbus device
@@ -323,13 +335,12 @@ EXPORT_SYMBOL_GPL(xenbus_dev_error);
  * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
  * closedown of this driver and its peer.
  */
-
 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
 {
        va_list ap;
 
        va_start(ap, fmt);
-       xenbus_va_dev_error(dev, err, fmt, ap);
+       _dev_error(dev, err, fmt, &ap);
        va_end(ap);
 
        xenbus_switch_state(dev, XenbusStateClosing);
@@ -346,7 +357,7 @@ static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
        va_list ap;
 
        va_start(ap, fmt);
-       xenbus_va_dev_error(dev, err, fmt, ap);
+       _dev_error(dev, err, fmt, &ap);
        va_end(ap);
 
        if (!depth)
@@ -357,7 +368,7 @@ static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
  * xenbus_grant_ring
  * @dev: xenbus device
  * @ring_mfn: mfn of ring to grant
-
+ *
  * Grant access to the given @ring_mfn to the peer of the given device.  Return
  * 0 on success, or -errno on error.  On error, the device will switch to
  * XenbusStateClosing, and the error will be saved in the store.
@@ -383,7 +394,7 @@ int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
        struct evtchn_alloc_unbound alloc_unbound;
        int err;
 
-       alloc_unbound.dom = DOMID_SELF;
+       alloc_unbound.dom        = DOMID_SELF;
        alloc_unbound.remote_dom = dev->otherend_id;
 
        err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
@@ -398,6 +409,7 @@ int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
 
 
+#if 0 /* !defined(CONFIG_XEN) && !defined(MODULE) */
 /**
  * Bind to an existing interdomain event channel in another domain. Returns 0
  * on success and stores the local port in *port. On error, returns -errno,
@@ -423,6 +435,7 @@ int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
        return err;
 }
 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
+#endif
 
 
 /**
@@ -444,6 +457,7 @@ int xenbus_free_evtchn(struct xenbus_device *dev, int port)
 EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
 
 
+#if !defined(CONFIG_XEN) && !defined(MODULE)
 /**
  * xenbus_map_ring_valloc
  * @dev: xenbus device
@@ -458,14 +472,14 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
  * or -ENOMEM on error. If an error is returned, device will switch to
  * XenbusStateClosing and the error message will be saved in XenStore.
  */
-int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
+int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t gnt_ref, void **vaddr)
 {
        return ring_ops->map(dev, gnt_ref, vaddr);
 }
 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
 
 static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
-                                    int gnt_ref, void **vaddr)
+                                    grant_ref_t gnt_ref, void **vaddr)
 {
        struct gnttab_map_grant_ref op = {
                .flags = GNTMAP_host_map | GNTMAP_contains_pte,
@@ -514,7 +528,7 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
 }
 
 static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
-                                     int gnt_ref, void **vaddr)
+                                     grant_ref_t gnt_ref, void **vaddr)
 {
        struct xenbus_map_node *node;
        int err;
@@ -564,7 +578,7 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
  * or -ENOMEM on error. If an error is returned, device will switch to
  * XenbusStateClosing and the error message will be saved in XenStore.
  */
-int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
+int xenbus_map_ring(struct xenbus_device *dev, grant_ref_t gnt_ref,
                    grant_handle_t *handle, void *vaddr)
 {
        struct gnttab_map_grant_ref op;
@@ -711,6 +725,7 @@ int xenbus_unmap_ring(struct xenbus_device *dev,
        return op.status;
 }
 EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
+#endif
 
 
 /**
@@ -722,15 +737,16 @@ EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
  */
 enum xenbus_state xenbus_read_driver_state(const char *path)
 {
-       enum xenbus_state result;
-       int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
-       if (err)
+       int result;
+
+       if (xenbus_scanf(XBT_NIL, path, "state", "%d", &result) != 1)
                result = XenbusStateUnknown;
 
        return result;
 }
 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
 
+#if !defined(CONFIG_XEN) && !defined(MODULE)
 static const struct xenbus_ring_ops ring_ops_pv = {
        .map = xenbus_map_ring_valloc_pv,
        .unmap = xenbus_unmap_ring_vfree_pv,
@@ -748,3 +764,4 @@ void __init xenbus_ring_ops_init(void)
        else
                ring_ops = &ring_ops_hvm;
 }
+#endif