rump_allserver.c revision 1.28 1 1.28 pooka /* $NetBSD: rump_allserver.c,v 1.28 2013/11/13 17:47:27 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.16 pooka * Copyright (c) 2010, 2011 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.24 pooka #include <rump/rumpuser_port.h>
29 1.24 pooka
30 1.1 pooka #ifndef lint
31 1.28 pooka __RCSID("$NetBSD: rump_allserver.c,v 1.28 2013/11/13 17:47:27 pooka Exp $");
32 1.1 pooka #endif /* !lint */
33 1.1 pooka
34 1.1 pooka #include <sys/types.h>
35 1.4 pooka #include <sys/signal.h>
36 1.24 pooka #include <sys/stat.h>
37 1.24 pooka
38 1.6 pooka #include <dlfcn.h>
39 1.1 pooka #include <errno.h>
40 1.9 pooka #include <fcntl.h>
41 1.4 pooka #include <semaphore.h>
42 1.1 pooka #include <stdio.h>
43 1.1 pooka #include <stdlib.h>
44 1.24 pooka #include <stdint.h>
45 1.1 pooka #include <string.h>
46 1.1 pooka #include <unistd.h>
47 1.24 pooka
48 1.24 pooka #include <rump/rump.h>
49 1.24 pooka #include <rump/rump_syscalls.h>
50 1.27 pooka #include <rump/rumpdefs.h>
51 1.1 pooka
52 1.22 joerg __dead static void
53 1.1 pooka usage(void)
54 1.1 pooka {
55 1.1 pooka
56 1.24 pooka #ifndef PLATFORM_HAS_SETGETPROGNAME
57 1.24 pooka #define getprogname() "rump_server"
58 1.24 pooka #endif
59 1.13 wiz fprintf(stderr, "usage: %s [-s] [-c ncpu] [-d drivespec] [-l libs] "
60 1.12 pooka "[-m modules] bindurl\n", getprogname());
61 1.1 pooka exit(1);
62 1.1 pooka }
63 1.1 pooka
64 1.22 joerg __dead static void
65 1.2 pooka die(int sflag, int error, const char *reason)
66 1.2 pooka {
67 1.2 pooka
68 1.26 pooka fprintf(stderr, "%s: %s", reason, strerror(error));
69 1.2 pooka if (!sflag)
70 1.2 pooka rump_daemonize_done(error);
71 1.2 pooka exit(1);
72 1.2 pooka }
73 1.2 pooka
74 1.4 pooka static sem_t sigsem;
75 1.4 pooka static void
76 1.4 pooka sigreboot(int sig)
77 1.4 pooka {
78 1.4 pooka
79 1.4 pooka sem_post(&sigsem);
80 1.4 pooka }
81 1.4 pooka
82 1.9 pooka static const char *const disktokens[] = {
83 1.9 pooka #define DKEY 0
84 1.9 pooka "key",
85 1.9 pooka #define DFILE 1
86 1.9 pooka "hostpath",
87 1.9 pooka #define DSIZE 2
88 1.18 pooka #define DSIZE_E -1
89 1.9 pooka "size",
90 1.14 pooka #define DOFFSET 3
91 1.14 pooka "offset",
92 1.16 pooka #define DLABEL 4
93 1.16 pooka "disklabel",
94 1.17 pooka #define DTYPE 5
95 1.17 pooka "type",
96 1.9 pooka NULL
97 1.9 pooka };
98 1.9 pooka
99 1.9 pooka struct etfsreg {
100 1.9 pooka const char *key;
101 1.9 pooka const char *hostpath;
102 1.9 pooka off_t flen;
103 1.14 pooka off_t foffset;
104 1.16 pooka char partition;
105 1.9 pooka enum rump_etfs_type type;
106 1.9 pooka };
107 1.9 pooka
108 1.17 pooka struct etfstype {
109 1.17 pooka const char *name;
110 1.17 pooka enum rump_etfs_type type;
111 1.17 pooka } etfstypes[] = {
112 1.17 pooka { "blk", RUMP_ETFS_BLK },
113 1.17 pooka { "chr", RUMP_ETFS_CHR },
114 1.17 pooka { "reg", RUMP_ETFS_REG },
115 1.17 pooka };
116 1.17 pooka
117 1.28 pooka static void processlabel(int, int, int, off_t *, off_t *);
118 1.28 pooka
119 1.1 pooka int
120 1.1 pooka main(int argc, char *argv[])
121 1.1 pooka {
122 1.2 pooka const char *serverurl;
123 1.9 pooka struct etfsreg *etfs = NULL;
124 1.9 pooka unsigned netfs = 0, curetfs = 0;
125 1.1 pooka int error;
126 1.2 pooka int ch, sflag;
127 1.24 pooka unsigned i;
128 1.24 pooka char **modarray = NULL;
129 1.24 pooka unsigned nmods = 0, curmod = 0;
130 1.24 pooka
131 1.24 pooka #ifdef PLATFORM_HAS_SETGETPROGNAME
132 1.1 pooka setprogname(argv[0]);
133 1.24 pooka #endif
134 1.1 pooka
135 1.2 pooka sflag = 0;
136 1.20 pooka while ((ch = getopt(argc, argv, "c:d:l:m:r:sv")) != -1) {
137 1.2 pooka switch (ch) {
138 1.12 pooka case 'c':
139 1.12 pooka setenv("RUMP_NCPU", optarg, 1);
140 1.12 pooka break;
141 1.9 pooka case 'd': {
142 1.9 pooka char *options, *value;
143 1.9 pooka char *key, *hostpath;
144 1.14 pooka long long flen, foffset;
145 1.16 pooka char partition;
146 1.17 pooka int ftype;
147 1.9 pooka
148 1.14 pooka flen = foffset = 0;
149 1.16 pooka partition = 0;
150 1.9 pooka key = hostpath = NULL;
151 1.17 pooka ftype = -1;
152 1.9 pooka options = optarg;
153 1.9 pooka while (*options) {
154 1.9 pooka switch (getsubopt(&options,
155 1.9 pooka __UNCONST(disktokens), &value)) {
156 1.9 pooka case DKEY:
157 1.11 pooka if (key != NULL) {
158 1.11 pooka fprintf(stderr,
159 1.11 pooka "key already given\n");
160 1.11 pooka usage();
161 1.11 pooka }
162 1.9 pooka key = value;
163 1.9 pooka break;
164 1.16 pooka
165 1.9 pooka case DFILE:
166 1.11 pooka if (hostpath != NULL) {
167 1.11 pooka fprintf(stderr,
168 1.11 pooka "hostpath already given\n");
169 1.11 pooka usage();
170 1.11 pooka }
171 1.9 pooka hostpath = value;
172 1.9 pooka break;
173 1.16 pooka
174 1.9 pooka case DSIZE:
175 1.11 pooka if (flen != 0) {
176 1.11 pooka fprintf(stderr,
177 1.11 pooka "size already given\n");
178 1.11 pooka usage();
179 1.11 pooka }
180 1.19 pooka if (strcmp(value, "host") == 0) {
181 1.18 pooka if (foffset != 0) {
182 1.18 pooka fprintf(stderr,
183 1.18 pooka "cannot specify "
184 1.18 pooka "offset with "
185 1.21 pooka "size=host\n");
186 1.18 pooka usage();
187 1.18 pooka }
188 1.18 pooka flen = DSIZE_E;
189 1.18 pooka } else {
190 1.24 pooka #ifdef PLATFORM_HAS_STRSUFTOLL
191 1.18 pooka /* XXX: off_t max? */
192 1.18 pooka flen = strsuftoll("-d size",
193 1.18 pooka value, 0, LLONG_MAX);
194 1.24 pooka #else
195 1.24 pooka flen = strtoull(value,
196 1.24 pooka NULL, 10);
197 1.24 pooka #endif
198 1.18 pooka }
199 1.9 pooka break;
200 1.14 pooka case DOFFSET:
201 1.14 pooka if (foffset != 0) {
202 1.14 pooka fprintf(stderr,
203 1.14 pooka "offset already given\n");
204 1.14 pooka usage();
205 1.14 pooka }
206 1.18 pooka if (flen == DSIZE_E) {
207 1.18 pooka fprintf(stderr, "cannot "
208 1.18 pooka "specify offset with "
209 1.21 pooka "size=host\n");
210 1.18 pooka usage();
211 1.18 pooka }
212 1.24 pooka #ifdef PLATFORM_HAS_STRSUFTOLL
213 1.14 pooka /* XXX: off_t max? */
214 1.14 pooka foffset = strsuftoll("-d offset", value,
215 1.14 pooka 0, LLONG_MAX);
216 1.24 pooka #else
217 1.24 pooka foffset = strtoull(value, NULL, 10);
218 1.24 pooka #endif
219 1.14 pooka break;
220 1.16 pooka
221 1.16 pooka case DLABEL:
222 1.16 pooka if (foffset != 0 || flen != 0) {
223 1.16 pooka fprintf(stderr,
224 1.16 pooka "disklabel needs to be "
225 1.16 pooka "used alone\n");
226 1.16 pooka usage();
227 1.16 pooka }
228 1.16 pooka if (strlen(value) != 1 ||
229 1.16 pooka *value < 'a' || *value > 'z') {
230 1.16 pooka fprintf(stderr,
231 1.16 pooka "invalid label part\n");
232 1.16 pooka usage();
233 1.16 pooka }
234 1.16 pooka partition = *value;
235 1.16 pooka break;
236 1.16 pooka
237 1.17 pooka case DTYPE:
238 1.17 pooka if (ftype != -1) {
239 1.17 pooka fprintf(stderr,
240 1.17 pooka "type already specified\n");
241 1.17 pooka usage();
242 1.17 pooka }
243 1.17 pooka
244 1.17 pooka for (i = 0;
245 1.17 pooka i < __arraycount(etfstypes);
246 1.17 pooka i++) {
247 1.17 pooka if (strcmp(etfstypes[i].name,
248 1.17 pooka value) == 0)
249 1.17 pooka break;
250 1.17 pooka }
251 1.17 pooka if (i == __arraycount(etfstypes)) {
252 1.17 pooka fprintf(stderr,
253 1.17 pooka "invalid type %s\n", value);
254 1.17 pooka usage();
255 1.17 pooka }
256 1.17 pooka ftype = etfstypes[i].type;
257 1.17 pooka break;
258 1.17 pooka
259 1.9 pooka default:
260 1.9 pooka fprintf(stderr, "invalid dtoken\n");
261 1.9 pooka usage();
262 1.9 pooka break;
263 1.9 pooka }
264 1.9 pooka }
265 1.9 pooka
266 1.16 pooka if (key == NULL || hostpath == NULL ||
267 1.16 pooka (flen == 0 && partition == 0)) {
268 1.9 pooka fprintf(stderr, "incomplete drivespec\n");
269 1.9 pooka usage();
270 1.9 pooka }
271 1.17 pooka if (ftype == -1)
272 1.17 pooka ftype = RUMP_ETFS_BLK;
273 1.9 pooka
274 1.9 pooka if (netfs - curetfs == 0) {
275 1.9 pooka etfs = realloc(etfs, (netfs+16)*sizeof(*etfs));
276 1.9 pooka if (etfs == NULL)
277 1.26 pooka die(1, errno, "realloc etfs");
278 1.9 pooka netfs += 16;
279 1.9 pooka }
280 1.9 pooka
281 1.9 pooka etfs[curetfs].key = key;
282 1.9 pooka etfs[curetfs].hostpath = hostpath;
283 1.9 pooka etfs[curetfs].flen = flen;
284 1.14 pooka etfs[curetfs].foffset = foffset;
285 1.16 pooka etfs[curetfs].partition = partition;
286 1.17 pooka etfs[curetfs].type = ftype;
287 1.9 pooka curetfs++;
288 1.9 pooka
289 1.9 pooka break;
290 1.9 pooka }
291 1.6 pooka case 'l':
292 1.8 pooka if (dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
293 1.8 pooka char pb[MAXPATHLEN];
294 1.8 pooka /* try to mimic linker -l syntax */
295 1.8 pooka
296 1.8 pooka snprintf(pb, sizeof(pb), "lib%s.so", optarg);
297 1.8 pooka if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
298 1.26 pooka die(1, 0, "dlopen lib");
299 1.8 pooka }
300 1.8 pooka }
301 1.6 pooka break;
302 1.24 pooka case 'm': {
303 1.24 pooka
304 1.7 pooka if (nmods - curmod == 0) {
305 1.7 pooka modarray = realloc(modarray,
306 1.7 pooka (nmods+16) * sizeof(char *));
307 1.7 pooka if (modarray == NULL)
308 1.26 pooka die(1, errno, "realloc");
309 1.7 pooka nmods += 16;
310 1.7 pooka }
311 1.7 pooka modarray[curmod++] = optarg;
312 1.24 pooka break; }
313 1.20 pooka case 'r':
314 1.20 pooka setenv("RUMP_MEMLIMIT", optarg, 1);
315 1.20 pooka break;
316 1.2 pooka case 's':
317 1.2 pooka sflag = 1;
318 1.2 pooka break;
319 1.20 pooka case 'v':
320 1.20 pooka setenv("RUMP_VERBOSE", "1", 1);
321 1.20 pooka break;
322 1.2 pooka default:
323 1.2 pooka usage();
324 1.2 pooka /*NOTREACHED*/
325 1.2 pooka }
326 1.2 pooka }
327 1.2 pooka
328 1.2 pooka argc -= optind;
329 1.2 pooka argv += optind;
330 1.2 pooka
331 1.2 pooka if (argc != 1)
332 1.1 pooka usage();
333 1.1 pooka
334 1.2 pooka serverurl = argv[0];
335 1.2 pooka
336 1.2 pooka if (!sflag) {
337 1.2 pooka error = rump_daemonize_begin();
338 1.2 pooka if (error)
339 1.26 pooka die(1, error, "rump daemonize");
340 1.2 pooka }
341 1.2 pooka
342 1.1 pooka error = rump_init();
343 1.1 pooka if (error)
344 1.2 pooka die(sflag, error, "rump init failed");
345 1.7 pooka
346 1.9 pooka /* load modules */
347 1.7 pooka for (i = 0; i < curmod; i++) {
348 1.27 pooka struct rump_modctl_load ml;
349 1.7 pooka
350 1.7 pooka #define ETFSKEY "/module.mod"
351 1.7 pooka if ((error = rump_pub_etfs_register(ETFSKEY,
352 1.7 pooka modarray[0], RUMP_ETFS_REG)) != 0)
353 1.7 pooka die(sflag, error, "module etfs register failed");
354 1.7 pooka memset(&ml, 0, sizeof(ml));
355 1.7 pooka ml.ml_filename = ETFSKEY;
356 1.27 pooka if (rump_sys_modctl(RUMP_MODCTL_LOAD, &ml) == -1)
357 1.7 pooka die(sflag, errno, "module load failed");
358 1.7 pooka rump_pub_etfs_remove(ETFSKEY);
359 1.9 pooka #undef ETFSKEY
360 1.9 pooka }
361 1.9 pooka
362 1.9 pooka /* register host drives */
363 1.9 pooka for (i = 0; i < curetfs; i++) {
364 1.14 pooka struct stat sb;
365 1.16 pooka off_t foffset, flen, fendoff;
366 1.18 pooka int fd, oflags;
367 1.9 pooka
368 1.18 pooka oflags = etfs[i].flen == DSIZE_E ? 0 : O_CREAT;
369 1.18 pooka fd = open(etfs[i].hostpath, O_RDWR | oflags, 0644);
370 1.9 pooka if (fd == -1)
371 1.18 pooka die(sflag, errno, "etfs hostpath open");
372 1.16 pooka
373 1.16 pooka if (etfs[i].partition) {
374 1.28 pooka processlabel(sflag, fd, etfs[i].partition - 'a',
375 1.28 pooka &foffset, &flen);
376 1.16 pooka } else {
377 1.16 pooka foffset = etfs[i].foffset;
378 1.16 pooka flen = etfs[i].flen;
379 1.16 pooka }
380 1.16 pooka
381 1.14 pooka if (fstat(fd, &sb) == -1)
382 1.14 pooka die(sflag, errno, "fstat etfs hostpath");
383 1.18 pooka if (flen == DSIZE_E) {
384 1.21 pooka if (sb.st_size == 0)
385 1.21 pooka die(sflag, EINVAL, "size=host, but cannot "
386 1.21 pooka "query non-zero size");
387 1.18 pooka flen = sb.st_size;
388 1.18 pooka }
389 1.18 pooka fendoff = foffset + flen;
390 1.16 pooka if (S_ISREG(sb.st_mode) && sb.st_size < fendoff) {
391 1.16 pooka if (ftruncate(fd, fendoff) == -1)
392 1.14 pooka die(sflag, errno, "truncate");
393 1.14 pooka }
394 1.9 pooka close(fd);
395 1.9 pooka
396 1.14 pooka if ((error = rump_pub_etfs_register_withsize(etfs[i].key,
397 1.16 pooka etfs[i].hostpath, etfs[i].type, foffset, flen)) != 0)
398 1.9 pooka die(sflag, error, "etfs register");
399 1.7 pooka }
400 1.7 pooka
401 1.2 pooka error = rump_init_server(serverurl);
402 1.1 pooka if (error)
403 1.2 pooka die(sflag, error, "rump server init failed");
404 1.2 pooka
405 1.2 pooka if (!sflag)
406 1.3 pooka rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
407 1.2 pooka
408 1.4 pooka sem_init(&sigsem, 0, 0);
409 1.4 pooka signal(SIGTERM, sigreboot);
410 1.4 pooka signal(SIGINT, sigreboot);
411 1.4 pooka sem_wait(&sigsem);
412 1.4 pooka
413 1.4 pooka rump_sys_reboot(0, NULL);
414 1.5 pooka /*NOTREACHED*/
415 1.5 pooka
416 1.4 pooka return 0;
417 1.1 pooka }
418 1.28 pooka
419 1.28 pooka /*
420 1.28 pooka * Copyright (c) 1987, 1988, 1993
421 1.28 pooka * The Regents of the University of California. All rights reserved.
422 1.28 pooka *
423 1.28 pooka * Redistribution and use in source and binary forms, with or without
424 1.28 pooka * modification, are permitted provided that the following conditions
425 1.28 pooka * are met:
426 1.28 pooka * 1. Redistributions of source code must retain the above copyright
427 1.28 pooka * notice, this list of conditions and the following disclaimer.
428 1.28 pooka * 2. Redistributions in binary form must reproduce the above copyright
429 1.28 pooka * notice, this list of conditions and the following disclaimer in the
430 1.28 pooka * documentation and/or other materials provided with the distribution.
431 1.28 pooka * 3. Neither the name of the University nor the names of its contributors
432 1.28 pooka * may be used to endorse or promote products derived from this software
433 1.28 pooka * without specific prior written permission.
434 1.28 pooka *
435 1.28 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
436 1.28 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
437 1.28 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
438 1.28 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
439 1.28 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
440 1.28 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
441 1.28 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
442 1.28 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
443 1.28 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
444 1.28 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
445 1.28 pooka * SUCH DAMAGE.
446 1.28 pooka *
447 1.28 pooka * @(#)disklabel.h 8.2 (Berkeley) 7/10/94
448 1.28 pooka */
449 1.28 pooka
450 1.28 pooka #define RUMPSERVER_MAXPARTITIONS 22
451 1.28 pooka #define RUMPSERVER_DISKMAGIC ((uint32_t)0x82564557) /* magic */
452 1.28 pooka #define RUMPSERVER_DEVSHIFT 9
453 1.28 pooka
454 1.28 pooka struct rumpserver_disklabel {
455 1.28 pooka uint32_t d_magic; /* the magic number */
456 1.28 pooka uint16_t d_type; /* drive type */
457 1.28 pooka uint16_t d_subtype; /* controller/d_type specific */
458 1.28 pooka char d_typename[16]; /* type name, e.g. "eagle" */
459 1.28 pooka
460 1.28 pooka /*
461 1.28 pooka * d_packname contains the pack identifier and is returned when
462 1.28 pooka * the disklabel is read off the disk or in-core copy.
463 1.28 pooka * d_boot0 and d_boot1 are the (optional) names of the
464 1.28 pooka * primary (block 0) and secondary (block 1-15) bootstraps
465 1.28 pooka * as found in /usr/mdec. These are returned when using
466 1.28 pooka * getdiskbyname(3) to retrieve the values from /etc/disktab.
467 1.28 pooka */
468 1.28 pooka union {
469 1.28 pooka char un_d_packname[16]; /* pack identifier */
470 1.28 pooka struct {
471 1.28 pooka char *un_d_boot0; /* primary bootstrap name */
472 1.28 pooka char *un_d_boot1; /* secondary bootstrap name */
473 1.28 pooka } un_b;
474 1.28 pooka } d_un;
475 1.28 pooka #define d_packname d_un.un_d_packname
476 1.28 pooka #define d_boot0 d_un.un_b.un_d_boot0
477 1.28 pooka #define d_boot1 d_un.un_b.un_d_boot1
478 1.28 pooka
479 1.28 pooka /* disk geometry: */
480 1.28 pooka uint32_t d_secsize; /* # of bytes per sector */
481 1.28 pooka uint32_t d_nsectors; /* # of data sectors per track */
482 1.28 pooka uint32_t d_ntracks; /* # of tracks per cylinder */
483 1.28 pooka uint32_t d_ncylinders; /* # of data cylinders per unit */
484 1.28 pooka uint32_t d_secpercyl; /* # of data sectors per cylinder */
485 1.28 pooka uint32_t d_secperunit; /* # of data sectors per unit */
486 1.28 pooka
487 1.28 pooka /*
488 1.28 pooka * Spares (bad sector replacements) below are not counted in
489 1.28 pooka * d_nsectors or d_secpercyl. Spare sectors are assumed to
490 1.28 pooka * be physical sectors which occupy space at the end of each
491 1.28 pooka * track and/or cylinder.
492 1.28 pooka */
493 1.28 pooka uint16_t d_sparespertrack; /* # of spare sectors per track */
494 1.28 pooka uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
495 1.28 pooka /*
496 1.28 pooka * Alternative cylinders include maintenance, replacement,
497 1.28 pooka * configuration description areas, etc.
498 1.28 pooka */
499 1.28 pooka uint32_t d_acylinders; /* # of alt. cylinders per unit */
500 1.28 pooka
501 1.28 pooka /* hardware characteristics: */
502 1.28 pooka /*
503 1.28 pooka * d_interleave, d_trackskew and d_cylskew describe perturbations
504 1.28 pooka * in the media format used to compensate for a slow controller.
505 1.28 pooka * Interleave is physical sector interleave, set up by the
506 1.28 pooka * formatter or controller when formatting. When interleaving is
507 1.28 pooka * in use, logically adjacent sectors are not physically
508 1.28 pooka * contiguous, but instead are separated by some number of
509 1.28 pooka * sectors. It is specified as the ratio of physical sectors
510 1.28 pooka * traversed per logical sector. Thus an interleave of 1:1
511 1.28 pooka * implies contiguous layout, while 2:1 implies that logical
512 1.28 pooka * sector 0 is separated by one sector from logical sector 1.
513 1.28 pooka * d_trackskew is the offset of sector 0 on track N relative to
514 1.28 pooka * sector 0 on track N-1 on the same cylinder. Finally, d_cylskew
515 1.28 pooka * is the offset of sector 0 on cylinder N relative to sector 0
516 1.28 pooka * on cylinder N-1.
517 1.28 pooka */
518 1.28 pooka uint16_t d_rpm; /* rotational speed */
519 1.28 pooka uint16_t d_interleave; /* hardware sector interleave */
520 1.28 pooka uint16_t d_trackskew; /* sector 0 skew, per track */
521 1.28 pooka uint16_t d_cylskew; /* sector 0 skew, per cylinder */
522 1.28 pooka uint32_t d_headswitch; /* head switch time, usec */
523 1.28 pooka uint32_t d_trkseek; /* track-to-track seek, usec */
524 1.28 pooka uint32_t d_flags; /* generic flags */
525 1.28 pooka #define NDDATA 5
526 1.28 pooka uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
527 1.28 pooka #define NSPARE 5
528 1.28 pooka uint32_t d_spare[NSPARE]; /* reserved for future use */
529 1.28 pooka uint32_t d_magic2; /* the magic number (again) */
530 1.28 pooka uint16_t d_checksum; /* xor of data incl. partitions */
531 1.28 pooka
532 1.28 pooka /* filesystem and partition information: */
533 1.28 pooka uint16_t d_npartitions; /* number of partitions in following */
534 1.28 pooka uint32_t d_bbsize; /* size of boot area at sn0, bytes */
535 1.28 pooka uint32_t d_sbsize; /* max size of fs superblock, bytes */
536 1.28 pooka struct rumpserver_partition { /* the partition table */
537 1.28 pooka uint32_t p_size; /* number of sectors in partition */
538 1.28 pooka uint32_t p_offset; /* starting sector */
539 1.28 pooka union {
540 1.28 pooka uint32_t fsize; /* FFS, ADOS:
541 1.28 pooka filesystem basic fragment size */
542 1.28 pooka uint32_t cdsession; /* ISO9660: session offset */
543 1.28 pooka } __partition_u2;
544 1.28 pooka #define p_fsize __partition_u2.fsize
545 1.28 pooka #define p_cdsession __partition_u2.cdsession
546 1.28 pooka uint8_t p_fstype; /* filesystem type, see below */
547 1.28 pooka uint8_t p_frag; /* filesystem fragments per block */
548 1.28 pooka union {
549 1.28 pooka uint16_t cpg; /* UFS: FS cylinders per group */
550 1.28 pooka uint16_t sgs; /* LFS: FS segment shift */
551 1.28 pooka } __partition_u1;
552 1.28 pooka #define p_cpg __partition_u1.cpg
553 1.28 pooka #define p_sgs __partition_u1.sgs
554 1.28 pooka } d_partitions[RUMPSERVER_MAXPARTITIONS]; /* actually may be more */
555 1.28 pooka };
556 1.28 pooka
557 1.28 pooka
558 1.28 pooka /* for swapping disklabel, so don't care about perf, just portability */
559 1.28 pooka #define bs32(x) \
560 1.28 pooka ((((x) & 0xff000000) >> 24)| \
561 1.28 pooka (((x) & 0x00ff0000) >> 8) | \
562 1.28 pooka (((x) & 0x0000ff00) << 8) | \
563 1.28 pooka (((x) & 0x000000ff) << 24))
564 1.28 pooka #define bs16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
565 1.28 pooka
566 1.28 pooka /*
567 1.28 pooka * From:
568 1.28 pooka * $NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
569 1.28 pooka */
570 1.28 pooka
571 1.28 pooka /*-
572 1.28 pooka * Copyright (c) 1991, 1993
573 1.28 pooka * The Regents of the University of California. All rights reserved.
574 1.28 pooka *
575 1.28 pooka * Redistribution and use in source and binary forms, with or without
576 1.28 pooka * modification, are permitted provided that the following conditions
577 1.28 pooka * are met:
578 1.28 pooka * 1. Redistributions of source code must retain the above copyright
579 1.28 pooka * notice, this list of conditions and the following disclaimer.
580 1.28 pooka * 2. Redistributions in binary form must reproduce the above copyright
581 1.28 pooka * notice, this list of conditions and the following disclaimer in the
582 1.28 pooka * documentation and/or other materials provided with the distribution.
583 1.28 pooka * 3. Neither the name of the University nor the names of its contributors
584 1.28 pooka * may be used to endorse or promote products derived from this software
585 1.28 pooka * without specific prior written permission.
586 1.28 pooka *
587 1.28 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
588 1.28 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
589 1.28 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
590 1.28 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
591 1.28 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
592 1.28 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
593 1.28 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
594 1.28 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
595 1.28 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
596 1.28 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
597 1.28 pooka * SUCH DAMAGE.
598 1.28 pooka */
599 1.28 pooka
600 1.28 pooka static uint16_t
601 1.28 pooka rs_dl_dkcksum(struct rumpserver_disklabel *lp, int imswapped)
602 1.28 pooka {
603 1.28 pooka uint16_t *start, *end;
604 1.28 pooka uint16_t sum;
605 1.28 pooka uint16_t npart;
606 1.28 pooka
607 1.28 pooka if (imswapped)
608 1.28 pooka npart = bs16(lp->d_npartitions);
609 1.28 pooka else
610 1.28 pooka npart = lp->d_npartitions;
611 1.28 pooka
612 1.28 pooka sum = 0;
613 1.28 pooka start = (uint16_t *)(void *)lp;
614 1.28 pooka end = (uint16_t *)(void *)&lp->d_partitions[npart];
615 1.28 pooka while (start < end) {
616 1.28 pooka if (imswapped)
617 1.28 pooka sum ^= bs16(*start);
618 1.28 pooka else
619 1.28 pooka sum ^= *start;
620 1.28 pooka start++;
621 1.28 pooka }
622 1.28 pooka return (sum);
623 1.28 pooka }
624 1.28 pooka
625 1.28 pooka /*
626 1.28 pooka * From:
627 1.28 pooka * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp
628 1.28 pooka */
629 1.28 pooka
630 1.28 pooka /*-
631 1.28 pooka * Copyright (c) 2002 The NetBSD Foundation, Inc.
632 1.28 pooka * All rights reserved.
633 1.28 pooka *
634 1.28 pooka * This code is derived from software contributed to The NetBSD Foundation
635 1.28 pooka * by Roland C. Dowdeswell.
636 1.28 pooka *
637 1.28 pooka * Redistribution and use in source and binary forms, with or without
638 1.28 pooka * modification, are permitted provided that the following conditions
639 1.28 pooka * are met:
640 1.28 pooka * 1. Redistributions of source code must retain the above copyright
641 1.28 pooka * notice, this list of conditions and the following disclaimer.
642 1.28 pooka * 2. Redistributions in binary form must reproduce the above copyright
643 1.28 pooka * notice, this list of conditions and the following disclaimer in the
644 1.28 pooka * documentation and/or other materials provided with the distribution.
645 1.28 pooka *
646 1.28 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
647 1.28 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
648 1.28 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
649 1.28 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
650 1.28 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
651 1.28 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
652 1.28 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
653 1.28 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
654 1.28 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
655 1.28 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
656 1.28 pooka * POSSIBILITY OF SUCH DAMAGE.
657 1.28 pooka */
658 1.28 pooka
659 1.28 pooka static int
660 1.28 pooka rs_dl_scan(struct rumpserver_disklabel *lp, int *isswapped,
661 1.28 pooka char *buf, size_t buflen)
662 1.28 pooka {
663 1.28 pooka size_t i;
664 1.28 pooka int imswapped;
665 1.28 pooka uint16_t npart;
666 1.28 pooka
667 1.28 pooka /* scan for the correct magic numbers. */
668 1.28 pooka
669 1.28 pooka for (i=0; i <= buflen - sizeof(*lp); i += 4) {
670 1.28 pooka memcpy(lp, buf + i, sizeof(*lp));
671 1.28 pooka if (lp->d_magic == RUMPSERVER_DISKMAGIC &&
672 1.28 pooka lp->d_magic2 == RUMPSERVER_DISKMAGIC) {
673 1.28 pooka imswapped = 0;
674 1.28 pooka goto sanity;
675 1.28 pooka }
676 1.28 pooka if (lp->d_magic == bs32(RUMPSERVER_DISKMAGIC) &&
677 1.28 pooka lp->d_magic2 == bs32(RUMPSERVER_DISKMAGIC)) {
678 1.28 pooka imswapped = 1;
679 1.28 pooka goto sanity;
680 1.28 pooka }
681 1.28 pooka }
682 1.28 pooka
683 1.28 pooka return 1;
684 1.28 pooka
685 1.28 pooka sanity:
686 1.28 pooka if (imswapped)
687 1.28 pooka npart = bs16(lp->d_npartitions);
688 1.28 pooka else
689 1.28 pooka npart = lp->d_npartitions;
690 1.28 pooka /* we've found something, let's sanity check it */
691 1.28 pooka if (npart > RUMPSERVER_MAXPARTITIONS
692 1.28 pooka || rs_dl_dkcksum(lp, imswapped))
693 1.28 pooka return 1;
694 1.28 pooka
695 1.28 pooka *isswapped = imswapped;
696 1.28 pooka return 0;
697 1.28 pooka }
698 1.28 pooka
699 1.28 pooka static void
700 1.28 pooka processlabel(int sflag, int fd, int partition, off_t *foffp, off_t *flenp)
701 1.28 pooka {
702 1.28 pooka struct rumpserver_disklabel dl;
703 1.28 pooka char buf[1<<16];
704 1.28 pooka uint32_t foffset, flen;
705 1.28 pooka int imswapped;
706 1.28 pooka
707 1.28 pooka if (pread(fd, buf, sizeof(buf), 0) == -1)
708 1.28 pooka die(sflag, errno, "could not read disk device");
709 1.28 pooka if (rs_dl_scan(&dl, &imswapped, buf, sizeof(buf)))
710 1.28 pooka die(sflag, ENOENT, "disklabel not found");
711 1.28 pooka
712 1.28 pooka if (partition >= dl.d_npartitions)
713 1.28 pooka die(sflag, ENOENT, "partition not available");
714 1.28 pooka
715 1.28 pooka foffset = dl.d_partitions[partition].p_offset << RUMPSERVER_DEVSHIFT;
716 1.28 pooka flen = dl.d_partitions[partition].p_size << RUMPSERVER_DEVSHIFT;
717 1.28 pooka if (imswapped) {
718 1.28 pooka foffset = bs32(foffset);
719 1.28 pooka flen = bs32(flen);
720 1.28 pooka }
721 1.28 pooka
722 1.28 pooka *foffp = (off_t)foffset;
723 1.28 pooka *flenp = (off_t)flen;
724 1.28 pooka }
725