debuggers.hg

view tools/fs-back/fs-xenbus.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 243614f8360c
children
line source
1 #undef NDEBUG
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <sys/select.h>
8 #include <xenctrl.h>
9 #include <xs.h>
10 #include <xen/io/fsif.h>
11 #include "fs-backend.h"
12 #include "fs-debug.h"
15 static bool xenbus_printf(struct xs_handle *xsh,
16 xs_transaction_t xbt,
17 char* node,
18 char* path,
19 char* fmt,
20 ...)
21 {
22 char fullpath[1024];
23 char val[1024];
24 va_list args;
26 va_start(args, fmt);
27 snprintf(fullpath, sizeof(fullpath), "%s/%s", node, path);
28 vsnprintf(val, sizeof(val), fmt, args);
29 va_end(args);
30 FS_DEBUG("xenbus_printf (%s) <= %s.\n", fullpath, val);
32 return xs_write(xsh, xbt, fullpath, val, strlen(val));
33 }
35 bool xenbus_create_request_node(void)
36 {
37 bool ret;
38 struct xs_permissions perms;
40 assert(xsh != NULL);
41 xs_rm(xsh, XBT_NULL, WATCH_NODE);
42 ret = xs_mkdir(xsh, XBT_NULL, WATCH_NODE);
43 if (!ret)
44 return false;
46 perms.id = 0;
47 perms.perms = XS_PERM_WRITE;
48 ret = xs_set_permissions(xsh, XBT_NULL, WATCH_NODE, &perms, 1);
50 return ret;
51 }
53 int xenbus_register_export(struct fs_export *export)
54 {
55 xs_transaction_t xst = 0;
56 char node[1024];
57 struct xs_permissions perms;
59 assert(xsh != NULL);
60 if(xsh == NULL)
61 {
62 FS_DEBUG("Could not open connection to xenbus deamon.\n");
63 goto error_exit;
64 }
65 FS_DEBUG("Connection to the xenbus deamon opened successfully.\n");
67 /* Start transaction */
68 xst = xs_transaction_start(xsh);
69 if(xst == 0)
70 {
71 FS_DEBUG("Could not start a transaction.\n");
72 goto error_exit;
73 }
74 FS_DEBUG("XS transaction is %d\n", xst);
76 /* Create node string */
77 snprintf(node, sizeof(node), "%s/%d", EXPORTS_NODE, export->export_id);
78 /* Remove old export (if exists) */
79 xs_rm(xsh, xst, node);
81 if(!xenbus_printf(xsh, xst, node, "name", "%s", export->name))
82 {
83 FS_DEBUG("Could not write the export node.\n");
84 goto error_exit;
85 }
87 /* People need to be able to read our export */
88 perms.id = 0;
89 perms.perms = XS_PERM_READ;
90 if(!xs_set_permissions(xsh, xst, EXPORTS_NODE, &perms, 1))
91 {
92 FS_DEBUG("Could not set permissions on the export node.\n");
93 goto error_exit;
94 }
96 xs_transaction_end(xsh, xst, 0);
97 return 0;
99 error_exit:
100 if(xst != 0)
101 xs_transaction_end(xsh, xst, 1);
102 return -1;
103 }
105 int xenbus_get_watch_fd(void)
106 {
107 int res;
108 #if DEBUG
109 int errno_orig;
110 #endif
111 assert(xsh != NULL);
112 res = xs_watch(xsh, WATCH_NODE, "conn-watch");
113 if (!res) {
114 #if DEBUG
115 errno_orig = errno;
116 FS_DEBUG("ERROR: xs_watch %s failed ret=%d errno=%d\n",
117 WATCH_NODE, res, errno);
118 errno = errno_orig;
119 #endif
120 return -1;
121 }
122 return xs_fileno(xsh);
123 }
125 int xenbus_read_mount_request(struct fs_mount *mount, char *frontend)
126 {
127 char node[1024];
128 char *s;
129 int i;
131 assert(xsh != NULL);
132 #if 0
133 snprintf(node, sizeof(node), WATCH_NODE"/%d/%d/frontend",
134 mount->dom_id, mount->export->export_id);
135 frontend = xs_read(xsh, XBT_NULL, node, NULL);
136 #endif
137 mount->frontend = frontend;
138 snprintf(node, sizeof(node), "%s/state", frontend);
139 s = xs_read(xsh, XBT_NULL, node, NULL);
140 if (strcmp(s, STATE_READY) != 0) {
141 FS_DEBUG("ERROR: frontend not read\n");
142 goto error;
143 }
144 free(s);
145 snprintf(node, sizeof(node), "%s/ring-size", frontend);
146 s = xs_read(xsh, XBT_NULL, node, NULL);
147 mount->shared_ring_size = atoi(s);
148 if (mount->shared_ring_size > MAX_RING_SIZE) {
149 FS_DEBUG("ERROR: shared_ring_size (%d) > MAX_RING_SIZE\n", mount->shared_ring_size);
150 goto error;
151 }
152 free(s);
153 for(i=0; i<mount->shared_ring_size; i++)
154 {
155 snprintf(node, sizeof(node), "%s/ring-ref-%d", frontend, i);
156 s = xs_read(xsh, XBT_NULL, node, NULL);
157 mount->grefs[i] = atoi(s);
158 free(s);
159 }
160 snprintf(node, sizeof(node), "%s/event-channel", frontend);
161 s = xs_read(xsh, XBT_NULL, node, NULL);
162 mount->remote_evtchn = atoi(s);
163 free(s);
164 return 0;
166 error:
167 free(s);
168 return -1;
169 }
171 /* Small utility function to figure out our domain id */
172 static int get_self_id(void)
173 {
174 char *dom_id;
175 int ret;
177 assert(xsh != NULL);
178 dom_id = xs_read(xsh, XBT_NULL, "domid", NULL);
179 sscanf(dom_id, "%d", &ret);
180 free(dom_id);
182 return ret;
183 }
186 bool xenbus_write_backend_node(struct fs_mount *mount)
187 {
188 char node[1024], backend_node[1024];
189 int self_id;
191 assert(xsh != NULL);
192 self_id = get_self_id();
193 FS_DEBUG("Our own dom_id=%d\n", self_id);
194 snprintf(node, sizeof(node), "%s/backend", mount->frontend);
195 snprintf(backend_node, sizeof(backend_node), "/local/domain/%d/"ROOT_NODE"/%d",
196 self_id, mount->mount_id);
197 xs_write(xsh, XBT_NULL, node, backend_node, strlen(backend_node));
199 snprintf(node, sizeof(node), ROOT_NODE"/%d/state", mount->mount_id);
200 return xs_write(xsh, XBT_NULL, node, STATE_INITIALISED, strlen(STATE_INITIALISED));
201 }
203 bool xenbus_write_backend_state(struct fs_mount *mount, const char *state)
204 {
205 char node[1024];
206 int self_id;
208 assert(xsh != NULL);
209 self_id = get_self_id();
210 snprintf(node, sizeof(node), ROOT_NODE"/%d/state", mount->mount_id);
211 return xs_write(xsh, XBT_NULL, node, state, strlen(state));
212 }
214 void xenbus_free_backend_node(struct fs_mount *mount)
215 {
216 char node[1024];
217 int self_id;
219 assert(xsh != NULL);
220 self_id = get_self_id();
221 snprintf(node, sizeof(node), ROOT_NODE"/%d", mount->mount_id);
222 xs_rm(xsh, XBT_NULL, node);
223 }
225 bool xenbus_watch_frontend_state(struct fs_mount *mount)
226 {
227 char statepath[1024];
229 assert(xsh != NULL);
230 snprintf(statepath, sizeof(statepath), "%s/state", mount->frontend);
231 return xs_watch(xsh, statepath, "frontend-state");
232 }
234 bool xenbus_unwatch_frontend_state(struct fs_mount *mount)
235 {
236 char statepath[1024];
238 assert(xsh != NULL);
239 snprintf(statepath, sizeof(statepath), "%s/state", mount->frontend);
240 return xs_unwatch(xsh, statepath, "frontend-state");
241 }
243 int xenbus_frontend_state_changed(struct fs_mount *mount, const char *oldstate)
244 {
245 unsigned int len;
246 char statepath[1024];
247 char *state = NULL;
249 assert(xsh != NULL);
250 snprintf(statepath, sizeof(statepath), "%s/state", mount->frontend);
251 state = xs_read(xsh, XBT_NULL, statepath, &len);
252 if (state && len > 0) {
253 if (strcmp(state, oldstate)) {
254 free(state);
255 return 1;
256 } else {
257 free(state);
258 return 0;
259 }
260 } else
261 return 1;
262 }
264 char* xenbus_read_frontend_state(struct fs_mount *mount)
265 {
266 unsigned int len;
267 char statepath[1024];
269 assert(xsh != NULL);
270 snprintf(statepath, sizeof(statepath), "%s/state", mount->frontend);
271 return xs_read(xsh, XBT_NULL, statepath, &len);
272 }