1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/diskslice.h>
37 #include <sys/diskmbr.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <sys/sysctl.h>
41 #include <sys/ioctl.h>
42 #include <vfs/hammer2/hammer2_xxhash.h>
43 #include <vfs/hammer2/hammer2_disk.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdarg.h>
48 #include <stddef.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <assert.h>
54 #include <err.h>
55 #include <uuid.h>
56 
57 #include "hammer2_subs.h"
58 
59 #define MAXLABELS	HAMMER2_SET_COUNT
60 
61 static hammer2_off_t check_volume(const char *path, int *fdp);
62 static int64_t getsize(const char *str, int64_t minval, int64_t maxval, int pw);
63 static uint64_t nowtime(void);
64 static int blkrefary_cmp(const void *b1, const void *b2);
65 static void usage(void);
66 
67 static void format_hammer2(int fd, hammer2_off_t total_space,
68 				hammer2_off_t free_space);
69 static void alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref,
70 				size_t bytes);
71 
72 static int Hammer2Version = -1;
73 static uuid_t Hammer2_FSType;	/* static filesystem type id for HAMMER2 */
74 static uuid_t Hammer2_VolFSID;	/* unique filesystem id in volu header */
75 static uuid_t Hammer2_SupCLID;	/* PFS cluster id in super-root inode */
76 static uuid_t Hammer2_SupFSID;	/* PFS unique id in super-root inode */
77 static uuid_t Hammer2_PfsCLID[MAXLABELS];
78 static uuid_t Hammer2_PfsFSID[MAXLABELS];
79 static const char *Label[MAXLABELS];
80 static hammer2_off_t BootAreaSize;
81 static hammer2_off_t AuxAreaSize;
82 static int NLabels;
83 
84 int
85 main(int ac, char **av)
86 {
87 	uint32_t status;
88 	hammer2_off_t total_space;
89 	hammer2_off_t free_space;
90 	hammer2_off_t reserved_space;
91 	int ch;
92 	int fd = -1;
93 	int i;
94 	int defaultlabels = 1;
95 	char *vol_fsid = NULL;
96 	char *sup_clid_name = NULL;
97 	char *sup_fsid_name = NULL;
98 	char *pfs_clid_name = NULL;
99 	char *pfs_fsid_name = NULL;
100 
101 	Label[NLabels++] = "LOCAL";
102 
103 	/*
104 	 * Sanity check basic filesystem structures.  No cookies for us
105 	 * if it gets broken!
106 	 */
107 	assert(sizeof(hammer2_volume_data_t) == HAMMER2_VOLUME_BYTES);
108 	assert(sizeof(hammer2_inode_data_t) == HAMMER2_INODE_BYTES);
109 	assert(sizeof(hammer2_blockref_t) == HAMMER2_BLOCKREF_BYTES);
110 
111 	/*
112 	 * Generate a filesystem id and lookup the filesystem type
113 	 */
114 	srandomdev();
115 	uuidgen(&Hammer2_VolFSID, 1);
116 	uuidgen(&Hammer2_SupCLID, 1);
117 	uuidgen(&Hammer2_SupFSID, 1);
118 	uuid_from_string(HAMMER2_UUID_STRING, &Hammer2_FSType, &status);
119 	/*uuid_name_lookup(&Hammer2_FSType, "DragonFly HAMMER2", &status);*/
120 	if (status != uuid_s_ok) {
121 		errx(1, "uuids file does not have the DragonFly "
122 			"HAMMER2 filesystem type");
123 	}
124 
125 	/*
126 	 * Parse arguments
127 	 */
128 	while ((ch = getopt(ac, av, "L:b:r:V:")) != -1) {
129 		switch(ch) {
130 		case 'L':
131 			defaultlabels = 0;
132 			if (strcasecmp(optarg, "none") == 0) {
133 				break;
134 			}
135 			if (NLabels >= MAXLABELS) {
136 				errx(1, "Limit of %d local labels",
137 				     MAXLABELS - 1);
138 			}
139 			if (strlen(optarg) == 0) {
140 				errx(1, "Volume label '%s' "
141 					"cannot be 0-length\n",
142 					optarg);
143 			}
144 			if (strlen(optarg) >= HAMMER2_INODE_MAXNAME) {
145 				errx(1, "Volume label '%s' is too long "
146 					"(%d chars max)\n",
147 					optarg,
148 					HAMMER2_INODE_MAXNAME - 1);
149 			}
150 			Label[NLabels++] = optarg;
151 			break;
152 		case 'b':
153 			BootAreaSize = getsize(optarg,
154 					 HAMMER2_NEWFS_ALIGN,
155 					 HAMMER2_BOOT_MAX_BYTES, 2);
156 			break;
157 		case 'r':
158 			AuxAreaSize = getsize(optarg,
159 					 HAMMER2_NEWFS_ALIGN,
160 					 HAMMER2_AUX_MAX_BYTES, 2);
161 			break;
162 		case 'V':
163 			Hammer2Version = strtol(optarg, NULL, 0);
164 			if (Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
165 			    Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
166 				errx(1,
167 				     "I don't understand how to format "
168 				     "HAMMER2 version %d\n",
169 				     Hammer2Version);
170 			}
171 			break;
172 		default:
173 			usage();
174 			break;
175 		}
176 	}
177 
178 	/*
179 	 * Check Hammer2 version
180 	 */
181 	if (Hammer2Version < 0) {
182 		size_t olen = sizeof(Hammer2Version);
183 		Hammer2Version = HAMMER2_VOL_VERSION_DEFAULT;
184 		if (sysctlbyname("vfs.hammer2.supported_version",
185 				 &Hammer2Version, &olen, NULL, 0) == 0) {
186 			if (Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
187 				Hammer2Version = HAMMER2_VOL_VERSION_WIP - 1;
188 				fprintf(stderr,
189 					"newfs_hammer2: WARNING: HAMMER2 VFS "
190 					"supports higher version than I "
191 					"understand,\n"
192 					"using version %d\n",
193 					Hammer2Version);
194 			}
195 		} else {
196 			fprintf(stderr,
197 				"newfs_hammer2: WARNING: HAMMER2 VFS not "
198 				"loaded, cannot get version info.\n"
199 				"Using version %d\n",
200 				HAMMER2_VOL_VERSION_DEFAULT);
201 		}
202 	}
203 
204 	ac -= optind;
205 	av += optind;
206 
207 	if (ac != 1 || av[0][0] == 0) {
208 		fprintf(stderr, "Exactly one disk device must be specified\n");
209 		exit(1);
210 	}
211 
212 	/*
213 	 * Adjust Label[] and NLabels.
214 	 */
215 	if (defaultlabels) {
216 		char c = av[0][strlen(av[0]) - 1];
217 		if (c == 'a')
218 			Label[NLabels++] = "BOOT";
219 		else if (c == 'd')
220 			Label[NLabels++] = "ROOT";
221 		else
222 			Label[NLabels++] = "DATA";
223 	}
224 
225 	/*
226 	 * Collect volume information.
227 	 */
228 	total_space = check_volume(av[0], &fd);
229 
230 	/*
231 	 * ~typically 8MB alignment to avoid edge cases for reserved blocks
232 	 * and so raid stripes (if any) operate efficiently.
233 	 */
234 	total_space &= ~HAMMER2_VOLUME_ALIGNMASK64;
235 
236 	/*
237 	 * Calculate defaults for the boot area size and round to the
238 	 * volume alignment boundary.
239 	 *
240 	 * NOTE: These areas are currently not used for booting but are
241 	 *	 reserved for future filesystem expansion.
242 	 */
243 	if (BootAreaSize == 0) {
244 		BootAreaSize = HAMMER2_BOOT_NOM_BYTES;
245 		while (BootAreaSize > total_space / 20)
246 			BootAreaSize >>= 1;
247 		if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES)
248 			BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
249 	} else if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES) {
250 		BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
251 	}
252 	BootAreaSize = (BootAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
253 		        ~HAMMER2_VOLUME_ALIGNMASK64;
254 
255 	/*
256 	 * Calculate defaults for the aux area size and round to the
257 	 * volume alignment boundary.
258 	 *
259 	 * NOTE: These areas are currently not used for logging but are
260 	 *	 reserved for future filesystem expansion.
261 	 */
262 	if (AuxAreaSize == 0) {
263 		AuxAreaSize = HAMMER2_AUX_NOM_BYTES;
264 		while (AuxAreaSize > total_space / 20)
265 			AuxAreaSize >>= 1;
266 		if (AuxAreaSize < HAMMER2_AUX_MIN_BYTES)
267 			AuxAreaSize = HAMMER2_AUX_MIN_BYTES;
268 	} else if (AuxAreaSize < HAMMER2_AUX_MIN_BYTES) {
269 		AuxAreaSize = HAMMER2_AUX_MIN_BYTES;
270 	}
271 	AuxAreaSize = (AuxAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
272 		       ~HAMMER2_VOLUME_ALIGNMASK64;
273 
274 	/*
275 	 * We'll need to stuff this in the volume header soon.
276 	 */
277 	hammer2_uuid_to_str(&Hammer2_VolFSID, &vol_fsid);
278 	hammer2_uuid_to_str(&Hammer2_SupCLID, &sup_clid_name);
279 	hammer2_uuid_to_str(&Hammer2_SupFSID, &sup_fsid_name);
280 
281 	/*
282 	 * Calculate the amount of reserved space.  HAMMER2_ZONE_SEG (4MB)
283 	 * is reserved at the beginning of every 1GB of storage, rounded up.
284 	 * Thus a 200MB filesystem will still have a 4MB reserve area.
285 	 *
286 	 * We also include the boot and aux areas in the reserve.  The
287 	 * reserve is used to help 'df' calculate the amount of available
288 	 * space.
289 	 *
290 	 * XXX I kinda screwed up and made the reserved area on the LEVEL1
291 	 *     boundary rather than the ZONE boundary.  LEVEL1 is on 1GB
292 	 *     boundaries rather than 2GB boundaries.  Stick with the LEVEL1
293 	 *     boundary.
294 	 */
295 	reserved_space = ((total_space + HAMMER2_FREEMAP_LEVEL1_MASK) /
296 			  HAMMER2_FREEMAP_LEVEL1_SIZE) * HAMMER2_ZONE_SEG64;
297 
298 	free_space = total_space - reserved_space - BootAreaSize - AuxAreaSize;
299 	if ((int64_t)free_space < 0) {
300 		fprintf(stderr, "Not enough free space\n");
301 		exit(1);
302 	}
303 
304 	format_hammer2(fd, total_space, free_space);
305 	fsync(fd);
306 	close(fd);
307 
308 	printf("---------------------------------------------\n");
309 	printf("version:          %d\n", Hammer2Version);
310 	printf("total-size:       %s (%jd bytes)\n",
311 	       sizetostr(total_space),
312 	       (intmax_t)total_space);
313 	printf("boot-area-size:   %s\n", sizetostr(BootAreaSize));
314 	printf("aux-area-size:    %s\n", sizetostr(AuxAreaSize));
315 	printf("topo-reserved:    %s\n", sizetostr(reserved_space));
316 	printf("free-space:       %s\n", sizetostr(free_space));
317 	printf("vol-fsid:         %s\n", vol_fsid);
318 	printf("sup-clid:         %s\n", sup_clid_name);
319 	printf("sup-fsid:         %s\n", sup_fsid_name);
320 	for (i = 0; i < NLabels; ++i) {
321 		printf("PFS \"%s\"\n", Label[i]);
322 		hammer2_uuid_to_str(&Hammer2_PfsCLID[i], &pfs_clid_name);
323 		hammer2_uuid_to_str(&Hammer2_PfsFSID[i], &pfs_fsid_name);
324 		printf("    clid %s\n", pfs_clid_name);
325 		printf("    fsid %s\n", pfs_fsid_name);
326 	}
327 
328 	free(vol_fsid);
329 	free(sup_clid_name);
330 	free(sup_fsid_name);
331 	free(pfs_clid_name);
332 	free(pfs_fsid_name);
333 
334 	return(0);
335 }
336 
337 static
338 void
339 usage(void)
340 {
341 	fprintf(stderr,
342 		"usage: newfs_hammer2 [-b bootsize] [-r auxsize] "
343 		"[-V version] [-L label ...] special\n"
344 	);
345 	exit(1);
346 }
347 
348 /*
349  * Convert a string to a 64 bit signed integer with various requirements.
350  */
351 static int64_t
352 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
353 {
354 	int64_t val;
355 	char *ptr;
356 
357 	val = strtoll(str, &ptr, 0);
358 	switch(*ptr) {
359 	case 't':
360 	case 'T':
361 		val *= 1024;
362 		/* fall through */
363 	case 'g':
364 	case 'G':
365 		val *= 1024;
366 		/* fall through */
367 	case 'm':
368 	case 'M':
369 		val *= 1024;
370 		/* fall through */
371 	case 'k':
372 	case 'K':
373 		val *= 1024;
374 		break;
375 	default:
376 		errx(1, "Unknown suffix in number '%s'\n", str);
377 		/* not reached */
378 	}
379 	if (ptr[1]) {
380 		errx(1, "Unknown suffix in number '%s'\n", str);
381 		/* not reached */
382 	}
383 	if (val < minval) {
384 		errx(1, "Value too small: %s, min is %s\n",
385 		     str, sizetostr(minval));
386 		/* not reached */
387 	}
388 	if (val > maxval) {
389 		errx(1, "Value too large: %s, max is %s\n",
390 		     str, sizetostr(maxval));
391 		/* not reached */
392 	}
393 	if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
394 		errx(1, "Value not power of 2: %s\n", str);
395 		/* not reached */
396 	}
397 	if ((powerof2 & 2) && (val & HAMMER2_NEWFS_ALIGNMASK)) {
398 		errx(1, "Value not an integral multiple of %dK: %s",
399 		     HAMMER2_NEWFS_ALIGN / 1024, str);
400 		/* not reached */
401 	}
402 	return(val);
403 }
404 
405 static uint64_t
406 nowtime(void)
407 {
408 	struct timeval tv;
409 	uint64_t xtime;
410 
411 	gettimeofday(&tv, NULL);
412 	xtime = tv.tv_sec * 1000000LL + tv.tv_usec;
413 	return(xtime);
414 }
415 
416 /*
417  * Figure out how big the volume is.
418  */
419 static
420 hammer2_off_t
421 check_volume(const char *path, int *fdp)
422 {
423 	struct partinfo pinfo;
424 	struct stat st;
425 	hammer2_off_t size;
426 
427 	/*
428 	 * Get basic information about the volume
429 	 */
430 	*fdp = open(path, O_RDWR);
431 	if (*fdp < 0)
432 		err(1, "Unable to open %s R+W", path);
433 	if (ioctl(*fdp, DIOCGPART, &pinfo) < 0) {
434 		/*
435 		 * Allow the formatting of regular files as HAMMER2 volumes
436 		 */
437 		if (fstat(*fdp, &st) < 0)
438 			err(1, "Unable to stat %s", path);
439 		if (!S_ISREG(st.st_mode))
440 			errx(1, "Unsupported file type for %s", path);
441 		size = st.st_size;
442 	} else {
443 		/*
444 		 * When formatting a block device as a HAMMER2 volume the
445 		 * sector size must be compatible.  HAMMER2 uses 64K
446 		 * filesystem buffers but logical buffers for direct I/O
447 		 * can be as small as HAMMER2_LOGSIZE (16KB).
448 		 */
449 		if (pinfo.reserved_blocks) {
450 			errx(1, "HAMMER2 cannot be placed in a partition "
451 				"which overlaps the disklabel or MBR");
452 		}
453 		if (pinfo.media_blksize > HAMMER2_PBUFSIZE ||
454 		    HAMMER2_PBUFSIZE % pinfo.media_blksize) {
455 			errx(1, "A media sector size of %d is not supported",
456 			     pinfo.media_blksize);
457 		}
458 		size = pinfo.media_size;
459 	}
460 	printf("Volume %-15s size %s\n", path, sizetostr(size));
461 	return (size);
462 }
463 
464 /*
465  * Create the volume header, the super-root directory inode, and
466  * the writable snapshot subdirectory (named via the label) which
467  * is to be the initial mount point, or at least the first mount point.
468  * newfs_hammer2 doesn't format the freemap bitmaps for these.
469  *
470  * 0                      4MB
471  * [----reserved_area----][boot_area][aux_area]
472  * [[vol_hdr][freemap]...]                     [sroot][root][root]...
473  *     \                                        ^\     ^     ^
474  *      \--------------------------------------/  \---/-----/---...
475  *
476  * NOTE: The passed total_space is 8MB-aligned to avoid edge cases.
477  */
478 static
479 void
480 format_hammer2(int fd, hammer2_off_t total_space, hammer2_off_t free_space)
481 {
482 	char *buf = malloc(HAMMER2_PBUFSIZE);
483 	hammer2_volume_data_t *vol;
484 	hammer2_inode_data_t *rawip;
485 	hammer2_blockref_t sroot_blockref;
486 	hammer2_blockref_t root_blockref[MAXLABELS];
487 	uint64_t now;
488 	hammer2_off_t volu_base = 0;
489 	hammer2_off_t boot_base = HAMMER2_ZONE_SEG;
490 	hammer2_off_t aux_base = boot_base + BootAreaSize;
491 	hammer2_off_t alloc_base = aux_base + AuxAreaSize;
492 	hammer2_off_t tmp_base;
493 	size_t n;
494 	int i;
495 
496 	/*
497 	 * Clear the entire 4MB reserve for the first 2G zone and
498 	 * make sure we can write to the last block.
499 	 */
500 	bzero(buf, HAMMER2_PBUFSIZE);
501 	tmp_base = volu_base;
502 	for (i = 0; i < HAMMER2_ZONE_BLOCKS_SEG; ++i) {
503 		n = pwrite(fd, buf, HAMMER2_PBUFSIZE, tmp_base);
504 		if (n != HAMMER2_PBUFSIZE) {
505 			perror("write");
506 			exit(1);
507 		}
508 		tmp_base += HAMMER2_PBUFSIZE;
509 	}
510 
511 	n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
512 		   volu_base + total_space - HAMMER2_PBUFSIZE);
513 	if (n != HAMMER2_PBUFSIZE) {
514 		perror("write (at-end-of-volume)");
515 		exit(1);
516 	}
517 
518 	/*
519 	 * Make sure alloc_base won't cross the reserved area at the
520 	 * beginning of each 1GB.
521 	 *
522 	 * Reserve space for the super-root inode and the root inode.
523 	 * Make sure they are in the same 64K block to simplify our code.
524 	 */
525 	assert((alloc_base & HAMMER2_PBUFMASK) == 0);
526 	assert(alloc_base < HAMMER2_FREEMAP_LEVEL1_SIZE);
527 
528 	/*
529 	 * Clear the boot/aux area.
530 	 */
531 	for (tmp_base = boot_base; tmp_base < alloc_base;
532 	     tmp_base += HAMMER2_PBUFSIZE) {
533 		n = pwrite(fd, buf, HAMMER2_PBUFSIZE, tmp_base);
534 		if (n != HAMMER2_PBUFSIZE) {
535 			perror("write (boot/aux)");
536 			exit(1);
537 		}
538 	}
539 
540 	now = nowtime();
541 	alloc_base &= ~HAMMER2_PBUFMASK64;
542 	alloc_direct(&alloc_base, &sroot_blockref, HAMMER2_INODE_BYTES);
543 
544 	for (i = 0; i < NLabels; ++i) {
545 		uuidgen(&Hammer2_PfsCLID[i], 1);
546 		uuidgen(&Hammer2_PfsFSID[i], 1);
547 
548 		alloc_direct(&alloc_base, &root_blockref[i],
549 			     HAMMER2_INODE_BYTES);
550 		assert(((sroot_blockref.data_off ^ root_blockref[i].data_off) &
551 			~HAMMER2_PBUFMASK64) == 0);
552 
553 		/*
554 		 * Format the root directory inode, which is left empty.
555 		 */
556 		rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO &
557 					root_blockref[i].data_off));
558 		rawip->meta.version = HAMMER2_INODE_VERSION_ONE;
559 		rawip->meta.ctime = now;
560 		rawip->meta.mtime = now;
561 		/* rawip->atime = now; NOT IMPL MUST BE ZERO */
562 		rawip->meta.btime = now;
563 		rawip->meta.type = HAMMER2_OBJTYPE_DIRECTORY;
564 		rawip->meta.mode = 0755;
565 		rawip->meta.inum = 1;	/* root inode, inumber 1 */
566 		rawip->meta.nlinks = 1;	/* directory link count compat */
567 
568 		rawip->meta.name_len = strlen(Label[i]);
569 		bcopy(Label[i], rawip->filename, rawip->meta.name_len);
570 		rawip->meta.name_key =
571 				dirhash(rawip->filename, rawip->meta.name_len);
572 
573 		/*
574 		 * Compression mode and supported copyids.
575 		 *
576 		 * Do not allow compression when creating any "BOOT" label
577 		 * (pfs-create also does the same if the pfs is named "BOOT")
578 		 */
579 		if (strcasecmp(Label[i], "BOOT") == 0) {
580 			rawip->meta.comp_algo = HAMMER2_ENC_ALGO(
581 						    HAMMER2_COMP_AUTOZERO);
582 			rawip->meta.check_algo = HAMMER2_ENC_ALGO(
583 						    HAMMER2_CHECK_XXHASH64);
584 		} else {
585 			rawip->meta.comp_algo = HAMMER2_ENC_ALGO(
586 						    HAMMER2_COMP_NEWFS_DEFAULT);
587 			rawip->meta.check_algo = HAMMER2_ENC_ALGO(
588 						    HAMMER2_CHECK_XXHASH64);
589 		}
590 
591 		/*
592 		 * NOTE: We leave nmasters set to 0, which means that we
593 		 *	 don't know how many masters there are.  The quorum
594 		 *	 calculation will effectively be 1 ( 0 / 2 + 1 ).
595 		 */
596 		rawip->meta.pfs_clid = Hammer2_PfsCLID[i];
597 		rawip->meta.pfs_fsid = Hammer2_PfsFSID[i];
598 		rawip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
599 		rawip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
600 
601 		/* first allocatable inode number */
602 		rawip->meta.pfs_inum = 16;
603 
604 		/* rawip->u.blockset is left empty */
605 
606 		/*
607 		 * The root blockref will be stored in the super-root inode as
608 		 * one of the ~4 PFS root directories.  The copyid here is the
609 		 * actual copyid of the storage ref.
610 		 *
611 		 * The key field for a PFS root directory's blockref is
612 		 * essentially the name key for the entry.
613 		 */
614 		root_blockref[i].key = rawip->meta.name_key;
615 		root_blockref[i].copyid = HAMMER2_COPYID_LOCAL;
616 		root_blockref[i].keybits = 0;
617 		root_blockref[i].check.xxhash64.value =
618 				XXH64(rawip, sizeof(*rawip), XXH_HAMMER2_SEED);
619 		root_blockref[i].type = HAMMER2_BREF_TYPE_INODE;
620 		root_blockref[i].methods =
621 				HAMMER2_ENC_CHECK(HAMMER2_CHECK_XXHASH64) |
622 				HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
623 		root_blockref[i].mirror_tid = 16;
624 		root_blockref[i].flags = HAMMER2_BREF_FLAG_PFSROOT;
625 	}
626 
627 	/*
628 	 * Format the super-root directory inode, giving it ~4 PFS root
629 	 * directories (root_blockref).
630 	 *
631 	 * The superroot contains ~4 directories pointing at the PFS root
632 	 * inodes (named via the label).  Inodes contain one blockset which
633 	 * is fully associative so we can put the entry anywhere without
634 	 * having to worry about the hash.  Use index 0.
635 	 */
636 	rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO & sroot_blockref.data_off));
637 	rawip->meta.version = HAMMER2_INODE_VERSION_ONE;
638 	rawip->meta.ctime = now;
639 	rawip->meta.mtime = now;
640 	/* rawip->meta.atime = now; NOT IMPL MUST BE ZERO */
641 	rawip->meta.btime = now;
642 	rawip->meta.type = HAMMER2_OBJTYPE_DIRECTORY;
643 	rawip->meta.mode = 0700;	/* super-root - root only */
644 	rawip->meta.inum = 0;		/* super root inode, inumber 0 */
645 	rawip->meta.nlinks = 2;		/* directory link count compat */
646 
647 	rawip->meta.name_len = 0;	/* super-root is unnamed */
648 	rawip->meta.name_key = 0;
649 
650 	rawip->meta.comp_algo = HAMMER2_ENC_ALGO(HAMMER2_COMP_AUTOZERO);
651 	rawip->meta.check_algo = HAMMER2_ENC_ALGO(HAMMER2_CHECK_XXHASH64);
652 
653 	/*
654 	 * The super-root is flagged as a PFS and typically given its own
655 	 * random FSID, making it possible to mirror an entire HAMMER2 disk
656 	 * snapshots and all if desired.  PFS ids are used to match up
657 	 * mirror sources and targets and cluster copy sources and targets.
658 	 *
659 	 * (XXX whole-disk logical mirroring is not really supported in
660 	 *  the first attempt because each PFS is in its own modify/mirror
661 	 *  transaction id domain, so normal mechanics cannot cross a PFS
662 	 *  boundary).
663 	 */
664 	rawip->meta.pfs_clid = Hammer2_SupCLID;
665 	rawip->meta.pfs_fsid = Hammer2_SupFSID;
666 	rawip->meta.pfs_type = HAMMER2_PFSTYPE_SUPROOT;
667 	snprintf((char*)rawip->filename, sizeof(rawip->filename), "SUPROOT");
668 	rawip->meta.name_key = 0;
669 	rawip->meta.name_len = strlen((char*)rawip->filename);
670 
671 	/* The super-root has an inode number of 0 */
672 	rawip->meta.pfs_inum = 0;
673 
674 	/*
675 	 * Currently newfs_hammer2 just throws the PFS inodes into the
676 	 * top-level block table at the volume root and doesn't try to
677 	 * create an indirect block, so we are limited to ~4 at filesystem
678 	 * creation time.  More can be added after mounting.
679 	 */
680 	qsort(root_blockref, NLabels, sizeof(root_blockref[0]), blkrefary_cmp);
681 	for (i = 0; i < NLabels; ++i)
682 		rawip->u.blockset.blockref[i] = root_blockref[i];
683 
684 	/*
685 	 * The sroot blockref will be stored in the volume header.
686 	 */
687 	sroot_blockref.copyid = HAMMER2_COPYID_LOCAL;
688 	sroot_blockref.keybits = 0;
689 	sroot_blockref.check.xxhash64.value =
690 				XXH64(rawip, sizeof(*rawip), XXH_HAMMER2_SEED);
691 	sroot_blockref.type = HAMMER2_BREF_TYPE_INODE;
692 	sroot_blockref.methods = HAMMER2_ENC_CHECK(HAMMER2_CHECK_XXHASH64) |
693 			         HAMMER2_ENC_COMP(HAMMER2_COMP_AUTOZERO);
694 	sroot_blockref.mirror_tid = 16;
695 	rawip = NULL;
696 
697 	/*
698 	 * Write out the 64K HAMMER2 block containing the root and sroot.
699 	 */
700 	assert((sroot_blockref.data_off & ~HAMMER2_PBUFMASK64) ==
701 		((alloc_base - 1) & ~HAMMER2_PBUFMASK64));
702 	n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
703 		   sroot_blockref.data_off & ~HAMMER2_PBUFMASK64);
704 	if (n != HAMMER2_PBUFSIZE) {
705 		perror("write");
706 		exit(1);
707 	}
708 
709 	/*
710 	 * Format the volume header.
711 	 *
712 	 * The volume header points to sroot_blockref.  Also be absolutely
713 	 * sure that allocator_beg is set.
714 	 */
715 	assert(HAMMER2_VOLUME_BYTES <= HAMMER2_PBUFSIZE);
716 	bzero(buf, HAMMER2_PBUFSIZE);
717 	vol = (void *)buf;
718 
719 	vol->magic = HAMMER2_VOLUME_ID_HBO;
720 	vol->boot_beg = boot_base;
721 	vol->boot_end = boot_base + BootAreaSize;
722 	vol->aux_beg = aux_base;
723 	vol->aux_end = aux_base + AuxAreaSize;
724 	vol->volu_size = total_space;
725 	vol->version = Hammer2Version;
726 	vol->flags = 0;
727 
728 	vol->fsid = Hammer2_VolFSID;
729 	vol->fstype = Hammer2_FSType;
730 
731 	vol->peer_type = DMSG_PEER_HAMMER2;	/* LNK_CONN identification */
732 
733 	vol->allocator_size = free_space;
734 	vol->allocator_free = free_space;
735 	vol->allocator_beg = alloc_base;
736 
737 	vol->sroot_blockset.blockref[0] = sroot_blockref;
738 	vol->mirror_tid = 16;	/* all blockref mirror TIDs set to 16 */
739 	vol->freemap_tid = 16;	/* all blockref mirror TIDs set to 16 */
740 	vol->icrc_sects[HAMMER2_VOL_ICRC_SECT1] =
741 			hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC1_OFF,
742 				       HAMMER2_VOLUME_ICRC1_SIZE);
743 
744 	/*
745 	 * Set ICRC_SECT0 after all remaining elements of sect0 have been
746 	 * populated in the volume header.  Note hat ICRC_SECT* (except for
747 	 * SECT0) are part of sect0.
748 	 */
749 	vol->icrc_sects[HAMMER2_VOL_ICRC_SECT0] =
750 			hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC0_OFF,
751 				       HAMMER2_VOLUME_ICRC0_SIZE);
752 	vol->icrc_volheader =
753 			hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRCVH_OFF,
754 				       HAMMER2_VOLUME_ICRCVH_SIZE);
755 
756 	/*
757 	 * Write the volume header and all alternates.
758 	 */
759 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
760 		if (i * HAMMER2_ZONE_BYTES64 >= total_space)
761 			break;
762 		n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
763 			   volu_base + i * HAMMER2_ZONE_BYTES64);
764 		if (n != HAMMER2_PBUFSIZE) {
765 			perror("write");
766 			exit(1);
767 		}
768 	}
769 
770 	/*
771 	 * Cleanup
772 	 */
773 	free(buf);
774 }
775 
776 static void
777 alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref, size_t bytes)
778 {
779 	int radix;
780 
781 	radix = 0;
782 	assert(bytes);
783 	while ((bytes & 1) == 0) {
784 		bytes >>= 1;
785 		++radix;
786 	}
787 	assert(bytes == 1);
788 	if (radix < HAMMER2_RADIX_MIN)
789 		radix = HAMMER2_RADIX_MIN;
790 
791 	bzero(bref, sizeof(*bref));
792 	bref->data_off = *basep | radix;
793 	bref->vradix = radix;
794 
795 	*basep += 1U << radix;
796 }
797 
798 static int
799 blkrefary_cmp(const void *b1, const void *b2)
800 {
801 	const hammer2_blockref_t *bref1 = b1;
802 	const hammer2_blockref_t *bref2 = b2;
803 
804 	if (bref1->key < bref2->key)
805 		return(-1);
806 	if (bref1->key > bref2->key)
807 		return(1);
808 	return 0;
809 }
810