direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_client.c @ 11402:e036feb6a4ff

Add xenbus_strstate() helper function for more readable debug output.

From: Gerd Hoffmann <kraxel@suse.de>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Sep 01 00:20:42 2006 +0100 (2006-09-01)
parents 26576f1dbadb
children 90d3ca978b40
line source
1 /******************************************************************************
2 * Client-facing interface for the Xenbus driver. In other words, the
3 * interface between the Xenbus and the device-specific code, be it the
4 * frontend or the backend of that driver.
5 *
6 * Copyright (C) 2005 XenSource Ltd
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
33 #include <xen/evtchn.h>
34 #include <xen/gnttab.h>
35 #include <xen/xenbus.h>
36 #include <xen/driver_util.h>
38 /* xenbus_probe.c */
39 extern char *kasprintf(const char *fmt, ...);
41 #define DPRINTK(fmt, args...) \
42 pr_debug("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
44 char *xenbus_strstate(enum xenbus_state state)
45 {
46 static char *name[] = {
47 [ XenbusStateUnknown ] = "Unknown",
48 [ XenbusStateInitialising ] = "Initialising",
49 [ XenbusStateInitWait ] = "InitWait",
50 [ XenbusStateInitialised ] = "Initialised",
51 [ XenbusStateConnected ] = "Connected",
52 [ XenbusStateClosing ] = "Closing",
53 [ XenbusStateClosed ] = "Closed",
54 };
55 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
56 }
58 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
59 struct xenbus_watch *watch,
60 void (*callback)(struct xenbus_watch *,
61 const char **, unsigned int))
62 {
63 int err;
65 watch->node = path;
66 watch->callback = callback;
68 err = register_xenbus_watch(watch);
70 if (err) {
71 watch->node = NULL;
72 watch->callback = NULL;
73 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
74 }
76 return err;
77 }
78 EXPORT_SYMBOL_GPL(xenbus_watch_path);
81 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
82 const char *path2, struct xenbus_watch *watch,
83 void (*callback)(struct xenbus_watch *,
84 const char **, unsigned int))
85 {
86 int err;
87 char *state = kasprintf("%s/%s", path, path2);
88 if (!state) {
89 xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
90 return -ENOMEM;
91 }
92 err = xenbus_watch_path(dev, state, watch, callback);
94 if (err)
95 kfree(state);
96 return err;
97 }
98 EXPORT_SYMBOL_GPL(xenbus_watch_path2);
101 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
102 {
103 /* We check whether the state is currently set to the given value, and
104 if not, then the state is set. We don't want to unconditionally
105 write the given state, because we don't want to fire watches
106 unnecessarily. Furthermore, if the node has gone, we don't write
107 to it, as the device will be tearing down, and we don't want to
108 resurrect that directory.
110 Note that, because of this cached value of our state, this function
111 will not work inside a Xenstore transaction (something it was
112 trying to in the past) because dev->state would not get reset if
113 the transaction was aborted.
115 */
117 int current_state;
118 int err;
120 if (state == dev->state)
121 return 0;
123 err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
124 &current_state);
125 if (err != 1)
126 return 0;
128 err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
129 if (err) {
130 if (state != XenbusStateClosing) /* Avoid looping */
131 xenbus_dev_fatal(dev, err, "writing new state");
132 return err;
133 }
135 dev->state = state;
137 return 0;
138 }
139 EXPORT_SYMBOL_GPL(xenbus_switch_state);
142 /**
143 * Return the path to the error node for the given device, or NULL on failure.
144 * If the value returned is non-NULL, then it is the caller's to kfree.
145 */
146 static char *error_path(struct xenbus_device *dev)
147 {
148 return kasprintf("error/%s", dev->nodename);
149 }
152 void _dev_error(struct xenbus_device *dev, int err, const char *fmt,
153 va_list ap)
154 {
155 int ret;
156 unsigned int len;
157 char *printf_buffer = NULL, *path_buffer = NULL;
159 #define PRINTF_BUFFER_SIZE 4096
160 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
161 if (printf_buffer == NULL)
162 goto fail;
164 len = sprintf(printf_buffer, "%i ", -err);
165 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
167 BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
169 dev_err(&dev->dev, "%s\n", printf_buffer);
171 path_buffer = error_path(dev);
173 if (path_buffer == NULL) {
174 printk("xenbus: failed to write error node for %s (%s)\n",
175 dev->nodename, printf_buffer);
176 goto fail;
177 }
179 if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
180 printk("xenbus: failed to write error node for %s (%s)\n",
181 dev->nodename, printf_buffer);
182 goto fail;
183 }
185 fail:
186 if (printf_buffer)
187 kfree(printf_buffer);
188 if (path_buffer)
189 kfree(path_buffer);
190 }
193 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
194 ...)
195 {
196 va_list ap;
198 va_start(ap, fmt);
199 _dev_error(dev, err, fmt, ap);
200 va_end(ap);
201 }
202 EXPORT_SYMBOL_GPL(xenbus_dev_error);
205 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
206 ...)
207 {
208 va_list ap;
210 va_start(ap, fmt);
211 _dev_error(dev, err, fmt, ap);
212 va_end(ap);
214 xenbus_switch_state(dev, XenbusStateClosing);
215 }
216 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
219 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
220 {
221 int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
222 if (err < 0)
223 xenbus_dev_fatal(dev, err, "granting access to ring page");
224 return err;
225 }
226 EXPORT_SYMBOL_GPL(xenbus_grant_ring);
229 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
230 {
231 struct evtchn_alloc_unbound alloc_unbound;
232 int err;
234 alloc_unbound.dom = DOMID_SELF;
235 alloc_unbound.remote_dom = dev->otherend_id;
237 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
238 &alloc_unbound);
239 if (err)
240 xenbus_dev_fatal(dev, err, "allocating event channel");
241 else
242 *port = alloc_unbound.port;
244 return err;
245 }
246 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
249 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
250 {
251 struct evtchn_bind_interdomain bind_interdomain;
252 int err;
254 bind_interdomain.remote_dom = dev->otherend_id;
255 bind_interdomain.remote_port = remote_port,
257 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
258 &bind_interdomain);
259 if (err)
260 xenbus_dev_fatal(dev, err,
261 "binding to event channel %d from domain %d",
262 remote_port, dev->otherend_id);
263 else
264 *port = bind_interdomain.local_port;
266 return err;
267 }
268 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
271 int xenbus_free_evtchn(struct xenbus_device *dev, int port)
272 {
273 struct evtchn_close close;
274 int err;
276 close.port = port;
278 err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
279 if (err)
280 xenbus_dev_error(dev, err, "freeing event channel %d", port);
282 return err;
283 }
286 enum xenbus_state xenbus_read_driver_state(const char *path)
287 {
288 enum xenbus_state result;
289 int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
290 if (err)
291 result = XenbusStateUnknown;
293 return result;
294 }
295 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);