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