ia64/xen-unstable

view tools/libfsimage/common/fsimage_grub.c @ 19848:5839491bbf20

[IA64] replace MAX_VCPUS with d->max_vcpus where necessary.

don't use MAX_VCPUS, and use vcpu::max_vcpus.
The changeset of 2f9e1348aa98 introduced max_vcpus to allow more vcpus
per guest. This patch is ia64 counter part.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 11:26:05 2009 +0900 (2009-06-29)
parents 64f790e90d3d
children
line source
1 /*
2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 * DEALINGS IN THE SOFTWARE.
19 *
20 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
21 * Use is subject to license terms.
22 */
24 #ifndef __sun__
25 #define _XOPEN_SOURCE 500
26 #endif
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <errno.h>
31 #include "fsimage_grub.h"
32 #include "fsimage_priv.h"
34 static char *disk_read_junk;
36 typedef struct fsig_data {
37 char fd_buf[FSYS_BUFLEN];
38 } fsig_data_t;
40 typedef struct fsig_file_data {
41 char ffd_buf[FSYS_BUFLEN];
42 uint64_t ffd_curpos;
43 uint64_t ffd_filepos;
44 uint64_t ffd_filemax;
45 int ffd_int1;
46 int ffd_int2;
47 int ffd_errnum;
48 } fsig_file_data_t;
50 fsi_file_t *
51 fsig_file_alloc(fsi_t *fsi)
52 {
53 fsi_file_t *ffi;
54 fsig_file_data_t *data = malloc(sizeof (fsig_file_data_t));
56 if (data == NULL)
57 return (NULL);
59 bzero(data, sizeof (fsig_file_data_t));
60 bcopy(fsig_fs_buf(fsi), data->ffd_buf, FSYS_BUFLEN);
62 if ((ffi = fsip_file_alloc(fsi, data)) == NULL) {
63 free(data);
64 return (NULL);
65 }
67 return (ffi);
68 }
70 void *
71 fsig_fs_buf(fsi_t *fsi)
72 {
73 fsig_data_t *data = fsip_fs_data(fsi);
74 return ((void *)data->fd_buf);
75 }
77 void *
78 fsig_file_buf(fsi_file_t *ffi)
79 {
80 fsig_file_data_t *data = fsip_file_data(ffi);
81 return ((void *)data->ffd_buf);
82 }
84 uint64_t *
85 fsig_filepos(fsi_file_t *ffi)
86 {
87 fsig_file_data_t *data = fsip_file_data(ffi);
88 return (&data->ffd_filepos);
89 }
91 uint64_t *
92 fsig_filemax(fsi_file_t *ffi)
93 {
94 fsig_file_data_t *data = fsip_file_data(ffi);
95 return (&data->ffd_filemax);
96 }
98 int *
99 fsig_int1(fsi_file_t *ffi)
100 {
101 fsig_file_data_t *data = fsip_file_data(ffi);
102 return (&data->ffd_int1);
103 }
105 int *
106 fsig_int2(fsi_file_t *ffi)
107 {
108 fsig_file_data_t *data = fsip_file_data(ffi);
109 return (&data->ffd_int2);
110 }
112 int *
113 fsig_errnum(fsi_file_t *ffi)
114 {
115 fsig_file_data_t *data = fsip_file_data(ffi);
116 return (&data->ffd_errnum);
117 }
119 char **
120 fsig_disk_read_junk(void)
121 {
122 return (&disk_read_junk);
123 }
125 #if defined(__i386__) || defined(__x86_64__)
127 #ifdef __amd64
128 #define BSF "bsfq"
129 #else
130 #define BSF "bsfl"
131 #endif
132 unsigned long
133 fsig_log2 (unsigned long word)
134 {
135 __asm__ (BSF " %1,%0"
136 : "=r" (word)
137 : "r" (word));
138 return word;
139 }
141 #elif defined(__ia64__)
143 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
144 # define ia64_popcnt(x) __builtin_popcountl(x)
145 #else
146 # define ia64_popcnt(x) \
147 ({ \
148 uint64_t ia64_intri_res; \
149 asm ("popcnt %0=%1" : "=r" (ia64_intri_res) : "r" (x)); \
150 ia64_intri_res; \
151 })
152 #endif
154 unsigned long
155 fsig_log2 (unsigned long word)
156 {
157 unsigned long result;
159 result = ia64_popcnt((word - 1) & ~word);
160 return result;
161 }
163 #elif defined(__powerpc__)
165 #ifdef __powerpc64__
166 #define PPC_CNTLZL "cntlzd"
167 #else
168 #define PPC_CNTLZL "cntlzw"
169 #endif
170 #define BITS_PER_LONG (sizeof(long) * 8)
172 static int
173 __ilog2(unsigned long x)
174 {
175 int lz;
177 asm (PPC_CNTLZL " %0,%1" : "=r" (lz) : "r" (x));
178 return BITS_PER_LONG - 1 - lz;
179 }
181 unsigned long
182 fsig_log2 (unsigned long word)
183 {
184 return __ilog2(word & -word);
185 }
187 #else /* Unoptimized */
189 unsigned long
190 fsig_log2 (unsigned long word)
191 {
192 unsigned long result = 0;
194 while (!(word & 1UL))
195 {
196 result++;
197 word >>= 1;
198 }
199 return result;
200 }
201 #endif
203 int
204 fsig_devread(fsi_file_t *ffi, unsigned int sector, unsigned int offset,
205 unsigned int bufsize, char *buf)
206 {
207 off_t off;
208 ssize_t ret;
209 int n, r;
210 char tmp[SECTOR_SIZE];
212 off = ffi->ff_fsi->f_off + ((off_t)sector * SECTOR_SIZE) + offset;
214 /*
215 * Make reads from a raw disk sector-aligned. This is a requirement
216 * for NetBSD. Split the read up into to three parts to meet this
217 * requirement.
218 */
220 n = (off & (SECTOR_SIZE - 1));
221 if (n > 0) {
222 r = SECTOR_SIZE - n;
223 if (r > bufsize)
224 r = bufsize;
225 ret = pread(ffi->ff_fsi->f_fd, tmp, SECTOR_SIZE, off - n);
226 if (ret < n + r)
227 return (0);
228 memcpy(buf, tmp + n, r);
229 buf += r;
230 bufsize -= r;
231 off += r;
232 }
234 n = (bufsize & ~(SECTOR_SIZE - 1));
235 if (n > 0) {
236 ret = pread(ffi->ff_fsi->f_fd, buf, n, off);
237 if (ret < n)
238 return (0);
239 buf += n;
240 bufsize -= n;
241 off += n;
242 }
243 if (bufsize > 0) {
244 ret = pread(ffi->ff_fsi->f_fd, tmp, SECTOR_SIZE, off);
245 if (ret < bufsize)
246 return (0);
247 memcpy(buf, tmp, bufsize);
248 }
250 return (1);
251 }
253 int
254 fsig_substring(const char *s1, const char *s2)
255 {
256 while (*s1 == *s2) {
257 if (*s1 == '\0')
258 return (0);
259 s1++;
260 s2++;
261 }
263 if (*s1 == '\0')
264 return (-1);
266 return (1);
267 }
269 static int
270 fsig_mount(fsi_t *fsi, const char *path, const char *options)
271 {
272 fsig_plugin_ops_t *ops = fsi->f_plugin->fp_data;
273 fsi_file_t *ffi;
274 fsi->f_data = malloc(sizeof (fsig_data_t));
276 if (fsi->f_data == NULL)
277 return (-1);
279 if ((ffi = fsig_file_alloc(fsi)) == NULL) {
280 free(fsi->f_data);
281 fsi->f_data = NULL;
282 return (-1);
283 }
285 bzero(fsi->f_data, sizeof (fsig_data_t));
287 if (!ops->fpo_mount(ffi, options)) {
288 fsip_file_free(ffi);
289 fsi_bootstring_free(fsi);
290 free(fsi->f_data);
291 fsi->f_data = NULL;
292 return (-1);
293 }
295 bcopy(fsig_file_buf(ffi), fsig_fs_buf(fsi), FSYS_BUFLEN);
296 fsip_file_free(ffi);
297 return (0);
298 }
300 static int
301 fsig_umount(fsi_t *fsi)
302 {
303 fsi_bootstring_free(fsi);
304 free(fsi->f_data);
305 return (0);
306 }
308 static fsi_file_t *
309 fsig_open(fsi_t *fsi, const char *name)
310 {
311 fsig_plugin_ops_t *ops = fsi->f_plugin->fp_data;
312 char *path = strdup(name);
313 fsi_file_t *ffi = NULL;
315 if (path == NULL || (ffi = fsig_file_alloc(fsi)) == NULL)
316 goto out;
318 if (ops->fpo_dir(ffi, path) == 0) {
319 fsip_file_free(ffi);
320 ffi = NULL;
321 errno = ENOENT;
322 }
324 out:
325 free(path);
326 return (ffi);
327 }
329 static ssize_t
330 fsig_pread(fsi_file_t *ffi, void *buf, size_t nbytes, uint64_t off)
331 {
332 fsig_plugin_ops_t *ops = ffi->ff_fsi->f_plugin->fp_data;
333 fsig_file_data_t *data = fsip_file_data(ffi);
335 data->ffd_filepos = off;
337 if (data->ffd_filepos >= data->ffd_filemax)
338 return (0);
340 /* FIXME: check */
341 if (data->ffd_filepos + nbytes > data->ffd_filemax)
342 nbytes = data->ffd_filemax - data->ffd_filepos;
344 errnum = 0;
345 return (ops->fpo_read(ffi, buf, nbytes));
346 }
348 static ssize_t
349 fsig_read(fsi_file_t *ffi, void *buf, size_t nbytes)
350 {
351 fsig_file_data_t *data = fsip_file_data(ffi);
352 ssize_t ret;
354 ret = fsig_pread(ffi, buf, nbytes, data->ffd_curpos);
355 data->ffd_curpos = data->ffd_filepos;
356 return (ret);
357 }
359 static int
360 fsig_close(fsi_file_t *ffi)
361 {
362 free(ffi->ff_data);
363 fsip_file_free(ffi);
364 return (0);
365 }
367 static fsi_plugin_ops_t fsig_grub_ops = {
368 .fpo_version = FSIMAGE_PLUGIN_VERSION,
369 .fpo_mount = fsig_mount,
370 .fpo_umount = fsig_umount,
371 .fpo_open = fsig_open,
372 .fpo_read = fsig_read,
373 .fpo_pread = fsig_pread,
374 .fpo_close = fsig_close
375 };
377 fsi_plugin_ops_t *
378 fsig_init(fsi_plugin_t *plugin, fsig_plugin_ops_t *ops)
379 {
380 if (ops->fpo_version > FSIMAGE_PLUGIN_VERSION)
381 return (NULL);
383 plugin->fp_data = ops;
385 return (&fsig_grub_ops);
386 }