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