1 /*
2  * Copyright (c) 2011-2014 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 <vfs/hammer2/hammer2_disk.h>
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <stddef.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <assert.h>
52 #include <err.h>
53 #include <uuid.h>
54 
55 #define MAXLABELS	4
56 
57 #define hammer2_icrc32(buf, size)	iscsi_crc32((buf), (size))
58 #define hammer2_icrc32c(buf, size, crc)	iscsi_crc32_ext((buf), (size), (crc))
59 uint32_t iscsi_crc32(const void *buf, size_t size);
60 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
61 
62 static hammer2_off_t check_volume(const char *path, int *fdp);
63 static int64_t getsize(const char *str, int64_t minval, int64_t maxval, int pw);
64 static const char *sizetostr(hammer2_off_t size);
65 static uint64_t nowtime(void);
66 static int blkrefary_cmp(const void *b1, const void *b2);
67 static void usage(void);
68 
69 static void format_hammer2(int fd, hammer2_off_t total_space,
70 				hammer2_off_t free_space);
71 static void alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref,
72 				size_t bytes);
73 static hammer2_key_t dirhash(const unsigned char *name, size_t len);
74 
75 static int Hammer2Version = -1;
76 static int ForceOpt = 0;
77 static uuid_t Hammer2_FSType;	/* static filesystem type id for HAMMER2 */
78 static uuid_t Hammer2_VolFSID;	/* unique filesystem id in volu header */
79 static uuid_t Hammer2_SupCLID;	/* PFS cluster id in super-root inode */
80 static uuid_t Hammer2_SupFSID;	/* PFS unique id in super-root inode */
81 static uuid_t Hammer2_PfsCLID[MAXLABELS];
82 static uuid_t Hammer2_PfsFSID[MAXLABELS];
83 static const char *Label[MAXLABELS];
84 static hammer2_off_t BootAreaSize;
85 static hammer2_off_t AuxAreaSize;
86 static int NLabels;
87 
88 #define GIG	((hammer2_off_t)1024*1024*1024)
89 
90 int
91 main(int ac, char **av)
92 {
93 	uint32_t status;
94 	hammer2_off_t total_space;
95 	hammer2_off_t free_space;
96 	hammer2_off_t reserved_space;
97 	int ch;
98 	int fd = -1;
99 	int i;
100 	int nolabels = 0;
101 	char *vol_fsid;
102 	char *sup_clid_name;
103 	char *sup_fsid_name;
104 	char *pfs_clid_name;
105 	char *pfs_fsid_name;
106 
107 	Label[NLabels++] = "LOCAL";
108 
109 	/*
110 	 * Sanity check basic filesystem structures.  No cookies for us
111 	 * if it gets broken!
112 	 */
113 	assert(sizeof(hammer2_volume_data_t) == HAMMER2_VOLUME_BYTES);
114 	assert(sizeof(hammer2_inode_data_t) == HAMMER2_INODE_BYTES);
115 	assert(sizeof(hammer2_blockref_t) == HAMMER2_BLOCKREF_BYTES);
116 
117 	/*
118 	 * Generate a filesystem id and lookup the filesystem type
119 	 */
120 	srandomdev();
121 	uuidgen(&Hammer2_VolFSID, 1);
122 	uuidgen(&Hammer2_SupCLID, 1);
123 	uuidgen(&Hammer2_SupFSID, 1);
124 	uuid_from_string(HAMMER2_UUID_STRING, &Hammer2_FSType, &status);
125 	/*uuid_name_lookup(&Hammer2_FSType, "DragonFly HAMMER2", &status);*/
126 	if (status != uuid_s_ok) {
127 		errx(1, "uuids file does not have the DragonFly "
128 			"HAMMER2 filesystem type");
129 	}
130 
131 	/*
132 	 * Parse arguments
133 	 */
134 	while ((ch = getopt(ac, av, "fL:b:m:r:V:")) != -1) {
135 		switch(ch) {
136 		case 'f':
137 			ForceOpt = 1;
138 			break;
139 		case 'L':
140 			if (strcasecmp(optarg, "none") == 0) {
141 				nolabels = 1;
142 				break;
143 			}
144 			if (NLabels >= MAXLABELS) {
145 				errx(1, "Limit of 3 local labels");
146 			}
147 			Label[NLabels++] = optarg;
148 			if (strlen(Label[NLabels-1]) > HAMMER2_INODE_MAXNAME) {
149 				errx(1, "Root directory label too long "
150 					"(64 chars max)\n");
151 			}
152 			break;
153 		case 'b':
154 			BootAreaSize = getsize(optarg,
155 					 HAMMER2_NEWFS_ALIGN,
156 					 HAMMER2_BOOT_MAX_BYTES, 2);
157 			break;
158 		case 'r':
159 			AuxAreaSize = getsize(optarg,
160 					 HAMMER2_NEWFS_ALIGN,
161 					 HAMMER2_REDO_MAX_BYTES, 2);
162 			break;
163 		case 'V':
164 			Hammer2Version = strtol(optarg, NULL, 0);
165 			if (Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
166 			    Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
167 				errx(1,
168 				     "I don't understand how to format "
169 				     "HAMMER2 version %d\n",
170 				     Hammer2Version);
171 			}
172 			break;
173 		default:
174 			usage();
175 			break;
176 		}
177 	}
178 
179 	/*
180 	 * Adjust Label[] and NLabels
181 	 */
182 	if (nolabels) {
183 		NLabels = 1;
184 	} else if (NLabels == 1) {
185 		Label[NLabels++] = "BOOT";
186 		Label[NLabels++] = "ROOT";
187 	}
188 
189 	/*
190 	 * Check Hammer2 version
191 	 */
192 	if (Hammer2Version < 0) {
193 		size_t olen = sizeof(Hammer2Version);
194 		Hammer2Version = HAMMER2_VOL_VERSION_DEFAULT;
195 		if (sysctlbyname("vfs.hammer2.supported_version",
196 				 &Hammer2Version, &olen, NULL, 0) == 0) {
197 			if (Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
198 				Hammer2Version = HAMMER2_VOL_VERSION_WIP - 1;
199 				fprintf(stderr,
200 					"newfs_hammer: WARNING: HAMMER2 VFS "
201 					"supports higher version than I "
202 					"understand,\n"
203 					"using version %d\n",
204 					Hammer2Version);
205 			}
206 		} else {
207 			fprintf(stderr,
208 				"newfs_hammer: WARNING: HAMMER2 VFS not "
209 				"loaded, cannot get version info.\n"
210 				"Using version %d\n",
211 				HAMMER2_VOL_VERSION_DEFAULT);
212 		}
213 	}
214 
215 	/*
216 	 * Collect volume information.
217 	 */
218 	ac -= optind;
219 	av += optind;
220 
221 	if (ac != 1) {
222 		fprintf(stderr, "Exactly one disk device must be specified\n");
223 		exit(1);
224 	}
225 	total_space = check_volume(av[0], &fd);
226 
227 	/*
228 	 * ~typically 8MB alignment to avoid edge cases for reserved blocks
229 	 * and so raid stripes (if any) operate efficiently.
230 	 */
231 	total_space &= ~HAMMER2_VOLUME_ALIGNMASK64;
232 
233 	/*
234 	 * Calculate defaults for the boot area size and round to the
235 	 * volume alignment boundary.
236 	 */
237 	if (BootAreaSize == 0) {
238 		BootAreaSize = HAMMER2_BOOT_NOM_BYTES;
239 		while (BootAreaSize > total_space / 20)
240 			BootAreaSize >>= 1;
241 		if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES)
242 			BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
243 	} else if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES) {
244 		BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
245 	}
246 	BootAreaSize = (BootAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
247 		       ~HAMMER2_VOLUME_ALIGNMASK64;
248 
249 	/*
250 	 * Calculate defaults for the redo area size and round to the
251 	 * volume alignment boundary.
252 	 */
253 	if (AuxAreaSize == 0) {
254 		AuxAreaSize = HAMMER2_REDO_NOM_BYTES;
255 		while (AuxAreaSize > total_space / 20)
256 			AuxAreaSize >>= 1;
257 		if (AuxAreaSize < HAMMER2_REDO_MIN_BYTES)
258 			AuxAreaSize = HAMMER2_REDO_MIN_BYTES;
259 	} else if (AuxAreaSize < HAMMER2_REDO_MIN_BYTES) {
260 		AuxAreaSize = HAMMER2_REDO_MIN_BYTES;
261 	}
262 	AuxAreaSize = (AuxAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
263 		       ~HAMMER2_VOLUME_ALIGNMASK64;
264 
265 	/*
266 	 * We'll need to stuff this in the volume header soon.
267 	 */
268 	uuid_to_string(&Hammer2_VolFSID, &vol_fsid, &status);
269 	uuid_to_string(&Hammer2_SupCLID, &sup_clid_name, &status);
270 	uuid_to_string(&Hammer2_SupFSID, &sup_fsid_name, &status);
271 
272 	/*
273 	 * Calculate the amount of reserved space.  HAMMER2_ZONE_SEG (4MB)
274 	 * is reserved at the beginning of every 2GB of storage, rounded up.
275 	 * Thus a 200MB filesystem will still have a 4MB reserve area.
276 	 *
277 	 * We also include the boot and redo areas in the reserve.  The
278 	 * reserve is used to help 'df' calculate the amount of available
279 	 * space.
280 	 */
281 	reserved_space = ((total_space + HAMMER2_ZONE_MASK64) /
282 			  HAMMER2_ZONE_BYTES64) * HAMMER2_ZONE_SEG64;
283 
284 	free_space = total_space - reserved_space -
285 		     BootAreaSize - AuxAreaSize;
286 
287 	format_hammer2(fd, total_space, free_space);
288 	fsync(fd);
289 	close(fd);
290 
291 	printf("---------------------------------------------\n");
292 	printf("version:          %d\n", Hammer2Version);
293 	printf("total-size:       %s (%jd bytes)\n",
294 	       sizetostr(total_space),
295 	       (intmax_t)total_space);
296 	printf("boot-area-size:   %s\n", sizetostr(BootAreaSize));
297 	printf("aux-area-size:    %s\n", sizetostr(AuxAreaSize));
298 	printf("topo-reserved:	  %s\n", sizetostr(reserved_space));
299 	printf("free-space:       %s\n", sizetostr(free_space));
300 	printf("vol-fsid:         %s\n", vol_fsid);
301 	printf("sup-clid:         %s\n", sup_clid_name);
302 	printf("sup-fsid:         %s\n", sup_fsid_name);
303 	for (i = 0; i < NLabels; ++i) {
304 		printf("PFS \"%s\"\n", Label[i]);
305 		uuid_to_string(&Hammer2_PfsCLID[i], &pfs_clid_name, &status);
306 		uuid_to_string(&Hammer2_PfsFSID[i], &pfs_fsid_name, &status);
307 		printf("    clid %s\n", pfs_clid_name);
308 		printf("    fsid %s\n", pfs_fsid_name);
309 	}
310 	printf("\n");
311 
312 	return(0);
313 }
314 
315 static
316 void
317 usage(void)
318 {
319 	fprintf(stderr,
320 		"usage: newfs_hammer -L label [-f] [-b bootsize] "
321 		"[-r redosize] [-V version] special ...\n"
322 	);
323 	exit(1);
324 }
325 
326 /*
327  * Convert the size in bytes to a human readable string.
328  */
329 static
330 const char *
331 sizetostr(hammer2_off_t size)
332 {
333 	static char buf[32];
334 
335 	if (size < 1024 / 2) {
336 		snprintf(buf, sizeof(buf), "%6.2f", (double)size);
337 	} else if (size < 1024 * 1024 / 2) {
338 		snprintf(buf, sizeof(buf), "%6.2fKB",
339 			(double)size / 1024);
340 	} else if (size < 1024 * 1024 * 1024LL / 2) {
341 		snprintf(buf, sizeof(buf), "%6.2fMB",
342 			(double)size / (1024 * 1024));
343 	} else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
344 		snprintf(buf, sizeof(buf), "%6.2fGB",
345 			(double)size / (1024 * 1024 * 1024LL));
346 	} else {
347 		snprintf(buf, sizeof(buf), "%6.2fTB",
348 			(double)size / (1024 * 1024 * 1024LL * 1024LL));
349 	}
350 	return(buf);
351 }
352 
353 /*
354  * Convert a string to a 64 bit signed integer with various requirements.
355  */
356 static int64_t
357 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
358 {
359 	int64_t val;
360 	char *ptr;
361 
362 	val = strtoll(str, &ptr, 0);
363 	switch(*ptr) {
364 	case 't':
365 	case 'T':
366 		val *= 1024;
367 		/* fall through */
368 	case 'g':
369 	case 'G':
370 		val *= 1024;
371 		/* fall through */
372 	case 'm':
373 	case 'M':
374 		val *= 1024;
375 		/* fall through */
376 	case 'k':
377 	case 'K':
378 		val *= 1024;
379 		break;
380 	default:
381 		errx(1, "Unknown suffix in number '%s'\n", str);
382 		/* not reached */
383 	}
384 	if (ptr[1]) {
385 		errx(1, "Unknown suffix in number '%s'\n", str);
386 		/* not reached */
387 	}
388 	if (val < minval) {
389 		errx(1, "Value too small: %s, min is %s\n",
390 		     str, sizetostr(minval));
391 		/* not reached */
392 	}
393 	if (val > maxval) {
394 		errx(1, "Value too large: %s, max is %s\n",
395 		     str, sizetostr(maxval));
396 		/* not reached */
397 	}
398 	if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
399 		errx(1, "Value not power of 2: %s\n", str);
400 		/* not reached */
401 	}
402 	if ((powerof2 & 2) && (val & HAMMER2_NEWFS_ALIGNMASK)) {
403 		errx(1, "Value not an integral multiple of %dK: %s",
404 		     HAMMER2_NEWFS_ALIGN / 1024, str);
405 		/* not reached */
406 	}
407 	return(val);
408 }
409 
410 static uint64_t
411 nowtime(void)
412 {
413 	struct timeval tv;
414 	uint64_t xtime;
415 
416 	gettimeofday(&tv, NULL);
417 	xtime = tv.tv_sec * 1000000LL + tv.tv_usec;
418 	return(xtime);
419 }
420 
421 /*
422  * Figure out how big the volume is.
423  */
424 static
425 hammer2_off_t
426 check_volume(const char *path, int *fdp)
427 {
428 	struct partinfo pinfo;
429 	struct stat st;
430 	hammer2_off_t size;
431 
432 	/*
433 	 * Get basic information about the volume
434 	 */
435 	*fdp = open(path, O_RDWR);
436 	if (*fdp < 0)
437 		err(1, "Unable to open %s R+W", path);
438 	if (ioctl(*fdp, DIOCGPART, &pinfo) < 0) {
439 		/*
440 		 * Allow the formatting of regular files as HAMMER2 volumes
441 		 */
442 		if (fstat(*fdp, &st) < 0)
443 			err(1, "Unable to stat %s", path);
444 		size = st.st_size;
445 	} else {
446 		/*
447 		 * When formatting a block device as a HAMMER2 volume the
448 		 * sector size must be compatible.  HAMMER2 uses 64K
449 		 * filesystem buffers but logical buffers for direct I/O
450 		 * can be as small as HAMMER2_LOGSIZE (16KB).
451 		 */
452 		if (pinfo.reserved_blocks) {
453 			errx(1, "HAMMER cannot be placed in a partition "
454 				"which overlaps the disklabel or MBR");
455 		}
456 		if (pinfo.media_blksize > HAMMER2_PBUFSIZE ||
457 		    HAMMER2_PBUFSIZE % pinfo.media_blksize) {
458 			errx(1, "A media sector size of %d is not supported",
459 			     pinfo.media_blksize);
460 		}
461 		size = pinfo.media_size;
462 	}
463 	printf("Volume %-15s size %s\n", path, sizetostr(size));
464 	return (size);
465 }
466 
467 /*
468  * Create the volume header, the super-root directory inode, and
469  * the writable snapshot subdirectory (named via the label) which
470  * is to be the initial mount point, or at least the first mount point.
471  *
472  * [----reserved_area----][boot_area][aux_area]
473  * [[vol_hdr]...         ]                      [sroot][root]
474  *
475  * The sroot and root inodes eat 512 bytes each.  newfs labels can only be
476  * 64 bytes so the root (snapshot) inode does not need to extend past 512
477  * bytes.  We use the correct hash slot correct but note that because
478  * directory hashes are chained 16x, any slot in the inode will work.
479  *
480  * Also format the allocation map.
481  *
482  * NOTE: The passed total_space is 8MB-aligned to avoid edge cases.
483  */
484 static
485 void
486 format_hammer2(int fd, hammer2_off_t total_space, hammer2_off_t free_space)
487 {
488 	char *buf = malloc(HAMMER2_PBUFSIZE);
489 	hammer2_volume_data_t *vol;
490 	hammer2_inode_data_t *rawip;
491 	hammer2_blockref_t sroot_blockref;
492 	hammer2_blockref_t root_blockref[MAXLABELS];	/* Max 4 labels */
493 	uint64_t now;
494 	hammer2_off_t volu_base = 0;
495 	hammer2_off_t boot_base = HAMMER2_ZONE_SEG;
496 	hammer2_off_t aux_base = boot_base + BootAreaSize;
497 	hammer2_off_t alloc_base = aux_base + AuxAreaSize;
498 	hammer2_off_t tmp_base;
499 	size_t n;
500 	int i;
501 
502 	/*
503 	 * Clear the entire reserve for the first 2G segment and
504 	 * make sure we can write to the last block.
505 	 */
506 	bzero(buf, HAMMER2_PBUFSIZE);
507 	tmp_base = volu_base;
508 	for (i = 0; i < HAMMER2_ZONE_BLOCKS_SEG; ++i) {
509 		n = pwrite(fd, buf, HAMMER2_PBUFSIZE, tmp_base);
510 		if (n != HAMMER2_PBUFSIZE) {
511 			perror("write");
512 			exit(1);
513 		}
514 		tmp_base += HAMMER2_PBUFSIZE;
515 	}
516 
517 	n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
518 		   volu_base + total_space - HAMMER2_PBUFSIZE);
519 	if (n != HAMMER2_PBUFSIZE) {
520 		perror("write (at-end-of-volume)");
521 		exit(1);
522 	}
523 
524 	/*
525 	 * Make sure alloc_base won't cross the reserved area at the
526 	 * beginning of each 2GB zone.
527 	 *
528 	 * Reserve space for the super-root inode and the root inode.
529 	 * Make sure they are in the same 64K block to simplify our code.
530 	 */
531 	assert((alloc_base & HAMMER2_PBUFMASK) == 0);
532 	assert(alloc_base < HAMMER2_ZONE_BYTES64 - HAMMER2_ZONE_SEG);
533 	now = nowtime();
534 	bzero(buf, HAMMER2_PBUFSIZE);
535 
536 	alloc_base &= ~HAMMER2_PBUFMASK64;
537 	alloc_direct(&alloc_base, &sroot_blockref, HAMMER2_INODE_BYTES);
538 
539 	for (i = 0; i < NLabels; ++i) {
540 		uuidgen(&Hammer2_PfsCLID[i], 1);
541 		uuidgen(&Hammer2_PfsFSID[i], 1);
542 
543 		alloc_direct(&alloc_base, &root_blockref[i],
544 			     HAMMER2_INODE_BYTES);
545 		assert(((sroot_blockref.data_off ^ root_blockref[i].data_off) &
546 			HAMMER2_OFF_MASK_HI) == 0);
547 
548 		/*
549 		 * Format the root directory inode, which is left empty.
550 		 */
551 		rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO &
552 					root_blockref[i].data_off));
553 		rawip->version = HAMMER2_INODE_VERSION_ONE;
554 		rawip->ctime = now;
555 		rawip->mtime = now;
556 		/* rawip->atime = now; NOT IMPL MUST BE ZERO */
557 		rawip->btime = now;
558 		rawip->type = HAMMER2_OBJTYPE_DIRECTORY;
559 		rawip->mode = 0755;
560 		rawip->inum = 1;	/* root inode, inumber 1 */
561 		rawip->nlinks = 1; 	/* directory link count compat */
562 
563 		rawip->name_len = strlen(Label[i]);
564 		bcopy(Label[i], rawip->filename, rawip->name_len);
565 		rawip->name_key = dirhash(rawip->filename, rawip->name_len);
566 
567 		/*
568 		 * Compression mode and supported copyids.
569 		 *
570 		 * Do not allow compression when creating any "BOOT" label
571 		 * (pfs-create also does the same if the pfs is named "BOOT")
572 		 */
573 		if (strcasecmp(Label[i], "BOOT") == 0) {
574 			rawip->comp_algo = HAMMER2_ENC_ALGO(
575 						HAMMER2_COMP_AUTOZERO);
576 			rawip->check_algo = HAMMER2_ENC_ALGO(
577 						HAMMER2_CHECK_ISCSI32);
578 		} else  {
579 			rawip->comp_algo = HAMMER2_ENC_ALGO(
580 						HAMMER2_COMP_NEWFS_DEFAULT);
581 			rawip->check_algo = HAMMER2_ENC_ALGO(
582 						HAMMER2_CHECK_ISCSI32);
583 		}
584 
585 		rawip->pfs_clid = Hammer2_PfsCLID[i];
586 		rawip->pfs_fsid = Hammer2_PfsFSID[i];
587 		rawip->pfs_type = HAMMER2_PFSTYPE_MASTER;
588 		rawip->op_flags |= HAMMER2_OPFLAG_PFSROOT;
589 		rawip->pfs_inum = 16;	/* first allocatable inode number */
590 
591 		/* rawip->u.blockset is left empty */
592 
593 		/*
594 		 * The root blockref will be stored in the super-root inode as
595 		 * the only directory entry.  The copyid here is the actual
596 		 * copyid of the storage ref.
597 		 *
598 		 * The key field for a directory entry's blockref is
599 		 * essentially the name key for the entry.
600 		 */
601 		root_blockref[i].key = rawip->name_key;
602 		root_blockref[i].copyid = HAMMER2_COPYID_LOCAL;
603 		root_blockref[i].keybits = 0;
604 		root_blockref[i].check.iscsi32.value =
605 				hammer2_icrc32(rawip, sizeof(*rawip));
606 		root_blockref[i].type = HAMMER2_BREF_TYPE_INODE;
607 		root_blockref[i].methods =
608 				HAMMER2_ENC_CHECK(HAMMER2_CHECK_ISCSI32) |
609 				HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
610 		root_blockref[i].mirror_tid = 16;
611 		root_blockref[i].flags = HAMMER2_BREF_FLAG_PFSROOT;
612 	}
613 
614 	/*
615 	 * Format the super-root directory inode, giving it one directory
616 	 * entry (root_blockref) and fixup the icrc method.
617 	 *
618 	 * The superroot contains one directory entry pointing at the root
619 	 * inode (named via the label).  Inodes contain one blockset which
620 	 * is fully associative so we can put the entry anywhere without
621 	 * having to worry about the hash.  Use index 0.
622 	 */
623 	rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO & sroot_blockref.data_off));
624 	rawip->version = HAMMER2_INODE_VERSION_ONE;
625 	rawip->ctime = now;
626 	rawip->mtime = now;
627 	/* rawip->atime = now; NOT IMPL MUST BE ZERO */
628 	rawip->btime = now;
629 	rawip->type = HAMMER2_OBJTYPE_DIRECTORY;
630 	rawip->mode = 0700;		/* super-root - root only */
631 	rawip->inum = 0;		/* super root inode, inumber 0 */
632 	rawip->nlinks = 2; 		/* directory link count compat */
633 
634 	rawip->name_len = 0;		/* super-root is unnamed */
635 	rawip->name_key = 0;
636 
637 	rawip->comp_algo = HAMMER2_ENC_ALGO(HAMMER2_COMP_AUTOZERO);
638 	rawip->check_algo = HAMMER2_ENC_ALGO(HAMMER2_CHECK_ISCSI32);
639 
640 	/*
641 	 * The super-root is flagged as a PFS and typically given its own
642 	 * random FSID, making it possible to mirror an entire HAMMER2 disk
643 	 * snapshots and all if desired.  PFS ids are used to match up
644 	 * mirror sources and targets and cluster copy sources and targets.
645 	 *
646 	 * (XXX whole-disk logical mirroring is not really supported in
647 	 *  the first attempt because each PFS is in its own modify/mirror
648 	 *  transaction id domain, so normal mechanics cannot cross a PFS
649 	 *  boundary).
650 	 */
651 	rawip->pfs_clid = Hammer2_SupCLID;
652 	rawip->pfs_fsid = Hammer2_SupFSID;
653 	rawip->pfs_type = HAMMER2_PFSTYPE_SUPROOT;
654 	rawip->pfs_inum = 16;	/* first allocatable inode number */
655 
656 	/*
657 	 * The super-root has a directory entry pointing to each local
658 	 * PFS.  To avoid having to deal with indirect blocks we can't load
659 	 * up more than 8 entries, but NLabels is restricted to 4 entries
660 	 * to leave room for possible future mandatory PFSs.
661 	 */
662 	qsort(root_blockref, NLabels, sizeof(root_blockref[0]), blkrefary_cmp);
663 	for (i = 0; i < NLabels; ++i)
664 		rawip->u.blockset.blockref[i] = root_blockref[i];
665 
666 	/*
667 	 * The sroot blockref will be stored in the volume header.
668 	 */
669 	sroot_blockref.copyid = HAMMER2_COPYID_LOCAL;
670 	sroot_blockref.keybits = 0;
671 	sroot_blockref.check.iscsi32.value =
672 					hammer2_icrc32(rawip, sizeof(*rawip));
673 	sroot_blockref.type = HAMMER2_BREF_TYPE_INODE;
674 	sroot_blockref.methods = HAMMER2_ENC_CHECK(HAMMER2_CHECK_ISCSI32) |
675 			         HAMMER2_ENC_COMP(HAMMER2_COMP_AUTOZERO);
676 	sroot_blockref.mirror_tid = 16;
677 	rawip = NULL;
678 
679 	/*
680 	 * Write out the 64K HAMMER2 block containing the root and sroot.
681 	 */
682 	n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
683 		   sroot_blockref.data_off & HAMMER2_OFF_MASK_HI);
684 	if (n != HAMMER2_PBUFSIZE) {
685 		perror("write");
686 		exit(1);
687 	}
688 
689 	/*
690 	 * Format the volume header.
691 	 *
692 	 * The volume header points to sroot_blockref.  Also be absolutely
693 	 * sure that allocator_beg is set.
694 	 */
695 	bzero(buf, HAMMER2_PBUFSIZE);
696 	vol = (void *)buf;
697 
698 	vol->magic = HAMMER2_VOLUME_ID_HBO;
699 	vol->boot_beg = boot_base;
700 	vol->boot_end = boot_base + BootAreaSize;
701 	vol->aux_beg = aux_base;
702 	vol->aux_end = aux_base + AuxAreaSize;
703 	vol->volu_size = total_space;
704 	vol->version = Hammer2Version;
705 	vol->flags = 0;
706 
707 	vol->fsid = Hammer2_VolFSID;
708 	vol->fstype = Hammer2_FSType;
709 
710 	vol->peer_type = DMSG_PEER_HAMMER2;	/* LNK_CONN identification */
711 
712 	vol->allocator_size = free_space;
713 	vol->allocator_free = free_space;
714 	vol->allocator_beg = alloc_base;
715 
716 	vol->sroot_blockset.blockref[0] = sroot_blockref;
717 	vol->mirror_tid = 16;	/* all blockref mirror TIDs set to 16 */
718 	vol->freemap_tid = 16;	/* all blockref mirror TIDs set to 16 */
719 	vol->icrc_sects[HAMMER2_VOL_ICRC_SECT1] =
720 			hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC1_OFF,
721 				       HAMMER2_VOLUME_ICRC1_SIZE);
722 
723 	/*
724 	 * Set ICRC_SECT0 after all remaining elements of sect0 have been
725 	 * populated in the volume header.  Note hat ICRC_SECT* (except for
726 	 * SECT0) are part of sect0.
727 	 */
728 	vol->icrc_sects[HAMMER2_VOL_ICRC_SECT0] =
729 			hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC0_OFF,
730 				       HAMMER2_VOLUME_ICRC0_SIZE);
731 	vol->icrc_volheader =
732 			hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRCVH_OFF,
733 				       HAMMER2_VOLUME_ICRCVH_SIZE);
734 
735 	/*
736 	 * Write the volume header and all alternates.
737 	 */
738 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
739 		if (i * HAMMER2_ZONE_BYTES64 >= total_space)
740 			break;
741 		n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
742 			   volu_base + i * HAMMER2_ZONE_BYTES64);
743 		if (n != HAMMER2_PBUFSIZE) {
744 			perror("write");
745 			exit(1);
746 		}
747 	}
748 
749 	/*
750 	 * Cleanup
751 	 */
752 	free(buf);
753 }
754 
755 static void
756 alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref, size_t bytes)
757 {
758 	int radix;
759 
760 	radix = 0;
761 	assert(bytes);
762 	while ((bytes & 1) == 0) {
763 		bytes >>= 1;
764 		++radix;
765 	}
766 	assert(bytes == 1);
767 	if (radix < HAMMER2_RADIX_MIN)
768 		radix = HAMMER2_RADIX_MIN;
769 
770 	bzero(bref, sizeof(*bref));
771 	bref->data_off = *basep | radix;
772 	bref->vradix = radix;
773 
774 	*basep += 1U << radix;
775 }
776 
777 /*
778  * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
779  * The filename is split into fields which are hashed separately and then
780  * added together.
781  *
782  * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
783  * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
784  * (This means we do not need to do a 0-check/or-with-0x100000000 either).
785  *
786  * Also, the iscsi crc code is used instead of the old crc32 code.
787  */
788 static hammer2_key_t
789 dirhash(const unsigned char *name, size_t len)
790 {
791 	const unsigned char *aname = name;
792 	uint32_t crcx;
793 	uint64_t key;
794 	size_t i;
795 	size_t j;
796 
797 	/*
798 	 * Filesystem version 6 or better will create directories
799 	 * using the ALG1 dirhash.  This hash breaks the filename
800 	 * up into domains separated by special characters and
801 	 * hashes each domain independently.
802 	 *
803 	 * We also do a simple sub-sort using the first character
804 	 * of the filename in the top 5-bits.
805 	 */
806 	key = 0;
807 
808 	/*
809 	 * m32
810 	 */
811 	crcx = 0;
812 	for (i = j = 0; i < len; ++i) {
813 		if (aname[i] == '.' ||
814 		    aname[i] == '-' ||
815 		    aname[i] == '_' ||
816 		    aname[i] == '~') {
817 			if (i != j)
818 				crcx += hammer2_icrc32(aname + j, i - j);
819 			j = i + 1;
820 		}
821 	}
822 	if (i != j)
823 		crcx += hammer2_icrc32(aname + j, i - j);
824 
825 	/*
826 	 * The directory hash utilizes the top 32 bits of the 64-bit key.
827 	 * Bit 63 must be set to 1.
828 	 */
829 	crcx |= 0x80000000U;
830 	key |= (uint64_t)crcx << 32;
831 
832 	/*
833 	 * l16 - crc of entire filename
834 	 *
835 	 * This crc reduces degenerate hash collision conditions
836 	 */
837 	crcx = hammer2_icrc32(aname, len);
838 	crcx = crcx ^ (crcx << 16);
839 	key |= crcx & 0xFFFF0000U;
840 
841 	/*
842 	 * Set bit 15.  This allows readdir to strip bit 63 so a positive
843 	 * 64-bit cookie/offset can always be returned, and still guarantee
844 	 * that the values 0x0000-0x7FFF are available for artificial entries.
845 	 * ('.' and '..').
846 	 */
847 	key |= 0x8000U;
848 
849 	return (key);
850 }
851 
852 static int
853 blkrefary_cmp(const void *b1, const void *b2)
854 {
855 	const hammer2_blockref_t *bref1 = b1;
856 	const hammer2_blockref_t *bref2 = b2;
857 	if (bref1->key < bref2->key)
858 		return(-1);
859 	if (bref1->key > bref2->key)
860 		return(1);
861 	return 0;
862 }
863