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