2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/types.h>
36 #include <sys/diskslice.h>
37 #include <sys/diskmbr.h>
40 #include <sys/sysctl.h>
41 #include <vfs/hammer2/hammer2_disk.h>
55 static hammer2_off_t check_volume(const char *path, int *fdp);
56 static int64_t getsize(const char *str, int64_t minval, int64_t maxval, int pw);
57 static const char *sizetostr(hammer2_off_t size);
58 static uint64_t nowtime(void);
59 static void usage(void);
61 static void format_hammer2(int fd, hammer2_off_t total_space,
62 hammer2_off_t free_space);
63 static void alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref,
65 static hammer2_key_t dirhash(const unsigned char *name, size_t len);
67 static int Hammer2Version = -1;
68 static int ForceOpt = 0;
69 static uuid_t Hammer2_FSType; /* static filesystem type id for HAMMER2 */
70 static uuid_t Hammer2_FSId; /* unique filesystem id in volu header */
71 static uuid_t Hammer2_SPFSId; /* PFS id in super-root inode */
72 static uuid_t Hammer2_RPFSId; /* PFS id in root inode */
73 static const char *Label = "ROOT";
74 static hammer2_off_t BootAreaSize;
75 static hammer2_off_t AuxAreaSize;
77 #define GIG ((hammer2_off_t)1024*1024*1024)
80 main(int ac, char **av)
83 hammer2_off_t total_space;
84 hammer2_off_t free_space;
85 hammer2_off_t reserved_space;
93 * Sanity check basic filesystem structures. No cookies for us
96 assert(sizeof(hammer2_volume_data_t) == HAMMER2_VOLUME_BYTES);
97 assert(sizeof(hammer2_inode_data_t) == HAMMER2_INODE_BYTES);
98 assert(sizeof(hammer2_blockref_t) == HAMMER2_BLOCKREF_BYTES);
101 * Generate a filesystem id and lookup the filesystem type
104 uuidgen(&Hammer2_FSId, 1);
105 uuidgen(&Hammer2_SPFSId, 1);
106 uuidgen(&Hammer2_RPFSId, 1);
107 uuid_from_string(HAMMER2_UUID_STRING, &Hammer2_FSType, &status);
108 /*uuid_name_lookup(&Hammer2_FSType, "DragonFly HAMMER2", &status);*/
109 if (status != uuid_s_ok) {
110 errx(1, "uuids file does not have the DragonFly "
111 "HAMMER filesystem type");
117 while ((ch = getopt(ac, av, "fL:b:m:r:V:")) != -1) {
124 if (strlen(Label) > HAMMER2_INODE_MAXNAME) {
125 errx(1, "Root directory label too long "
130 BootAreaSize = getsize(optarg,
132 HAMMER2_BOOT_MAX_BYTES, 2);
135 AuxAreaSize = getsize(optarg,
137 HAMMER2_REDO_MAX_BYTES, 2);
140 Hammer2Version = strtol(optarg, NULL, 0);
141 if (Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
142 Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
144 "I don't understand how to format "
145 "HAMMER2 version %d\n",
155 if (Hammer2Version < 0) {
156 size_t olen = sizeof(Hammer2Version);
157 Hammer2Version = HAMMER2_VOL_VERSION_DEFAULT;
158 if (sysctlbyname("vfs.hammer2.supported_version",
159 &Hammer2Version, &olen, NULL, 0) == 0) {
160 if (Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
161 Hammer2Version = HAMMER2_VOL_VERSION_WIP - 1;
163 "newfs_hammer: WARNING: HAMMER2 VFS "
164 "supports higher version than I "
166 "using version %d\n",
171 "newfs_hammer: WARNING: HAMMER2 VFS not "
172 "loaded, cannot get version info.\n"
173 "Using version %d\n",
174 HAMMER2_VOL_VERSION_DEFAULT);
179 * Collect volume information.
185 fprintf(stderr, "Exactly one disk device must be specified\n");
188 total_space = check_volume(av[0], &fd);
191 * ~typically 8MB alignment to avoid edge cases for reserved blocks
192 * and so raid stripes (if any) operate efficiently.
194 total_space &= ~HAMMER2_VOLUME_ALIGNMASK64;
197 * Calculate defaults for the boot area size and round to the
198 * volume alignment boundary.
200 if (BootAreaSize == 0) {
201 BootAreaSize = HAMMER2_BOOT_NOM_BYTES;
202 while (BootAreaSize > total_space / 20)
204 if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES)
205 BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
206 } else if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES) {
207 BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
209 BootAreaSize = (BootAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
210 ~HAMMER2_VOLUME_ALIGNMASK64;
213 * Calculate defaults for the redo area size and round to the
214 * volume alignment boundary.
216 if (AuxAreaSize == 0) {
217 AuxAreaSize = HAMMER2_REDO_NOM_BYTES;
218 while (AuxAreaSize > total_space / 20)
220 if (AuxAreaSize < HAMMER2_REDO_MIN_BYTES)
221 AuxAreaSize = HAMMER2_REDO_MIN_BYTES;
222 } else if (AuxAreaSize < HAMMER2_REDO_MIN_BYTES) {
223 AuxAreaSize = HAMMER2_REDO_MIN_BYTES;
225 AuxAreaSize = (AuxAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
226 ~HAMMER2_VOLUME_ALIGNMASK64;
229 * We'll need to stuff this in the volume header soon.
231 uuid_to_string(&Hammer2_FSId, &fsidstr, &status);
232 uuid_to_string(&Hammer2_SPFSId, &spfsidstr, &status);
233 uuid_to_string(&Hammer2_RPFSId, &rpfsidstr, &status);
236 * Calculate the amount of reserved space. HAMMER2_ZONE_SEG (4MB)
237 * is reserved at the beginning of every 2GB of storage, rounded up.
238 * Thus a 200MB filesystem will still have a 4MB reserve area.
240 * We also include the boot and redo areas in the reserve. The
241 * reserve is used to help 'df' calculate the amount of available
244 reserved_space = ((total_space + HAMMER2_ZONE_MASK64) /
245 HAMMER2_ZONE_BYTES64) * HAMMER2_ZONE_SEG64;
247 free_space = total_space - reserved_space -
248 BootAreaSize - AuxAreaSize;
250 format_hammer2(fd, total_space, free_space);
254 printf("---------------------------------------------\n");
255 printf("total-size: %s (%jd bytes)\n",
256 sizetostr(total_space),
257 (intmax_t)total_space);
258 printf("root-label: %s\n", Label);
259 printf("version: %d\n", Hammer2Version);
260 printf("boot-area-size: %s\n", sizetostr(BootAreaSize));
261 printf("aux-area-size: %s\n", sizetostr(AuxAreaSize));
262 printf("topo-reserved: %s\n", sizetostr(reserved_space));
263 printf("free-space: %s\n", sizetostr(free_space));
264 printf("fsid: %s\n", fsidstr);
265 printf("supr-pfsid: %s\n", spfsidstr);
266 printf("root-pfsid: %s\n", rpfsidstr);
277 "usage: newfs_hammer -L label [-f] [-b bootsize] "
278 "[-r redosize] [-V version] special ...\n"
284 * Convert the size in bytes to a human readable string.
288 sizetostr(hammer2_off_t size)
292 if (size < 1024 / 2) {
293 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
294 } else if (size < 1024 * 1024 / 2) {
295 snprintf(buf, sizeof(buf), "%6.2fKB",
296 (double)size / 1024);
297 } else if (size < 1024 * 1024 * 1024LL / 2) {
298 snprintf(buf, sizeof(buf), "%6.2fMB",
299 (double)size / (1024 * 1024));
300 } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
301 snprintf(buf, sizeof(buf), "%6.2fGB",
302 (double)size / (1024 * 1024 * 1024LL));
304 snprintf(buf, sizeof(buf), "%6.2fTB",
305 (double)size / (1024 * 1024 * 1024LL * 1024LL));
311 * Convert a string to a 64 bit signed integer with various requirements.
314 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
319 val = strtoll(str, &ptr, 0);
338 errx(1, "Unknown suffix in number '%s'\n", str);
342 errx(1, "Unknown suffix in number '%s'\n", str);
346 errx(1, "Value too small: %s, min is %s\n",
347 str, sizetostr(minval));
351 errx(1, "Value too large: %s, max is %s\n",
352 str, sizetostr(maxval));
355 if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
356 errx(1, "Value not power of 2: %s\n", str);
359 if ((powerof2 & 2) && (val & HAMMER2_NEWFS_ALIGNMASK)) {
360 errx(1, "Value not an integral multiple of %dK: %s",
361 HAMMER2_NEWFS_ALIGN / 1024, str);
373 gettimeofday(&tv, NULL);
374 xtime = tv.tv_sec * 1000000LL + tv.tv_usec;
379 * Figure out how big the volume is.
383 check_volume(const char *path, int *fdp)
385 struct partinfo pinfo;
390 * Get basic information about the volume
392 *fdp = open(path, O_RDWR);
394 err(1, "Unable to open %s R+W", path);
395 if (ioctl(*fdp, DIOCGPART, &pinfo) < 0) {
397 * Allow the formatting of regular files as HAMMER2 volumes
399 if (fstat(*fdp, &st) < 0)
400 err(1, "Unable to stat %s", path);
404 * When formatting a block device as a HAMMER2 volume the
405 * sector size must be compatible. HAMMER2 uses 64K
406 * filesystem buffers but logical buffers for direct I/O
407 * can be as small as HAMMER2_LOGSIZE (16KB).
409 if (pinfo.reserved_blocks) {
410 errx(1, "HAMMER cannot be placed in a partition "
411 "which overlaps the disklabel or MBR");
413 if (pinfo.media_blksize > HAMMER2_PBUFSIZE ||
414 HAMMER2_PBUFSIZE % pinfo.media_blksize) {
415 errx(1, "A media sector size of %d is not supported",
416 pinfo.media_blksize);
418 size = pinfo.media_size;
420 printf("Volume %-15s size %s\n", path, sizetostr(size));
425 * Create the volume header, the super-root directory inode, and
426 * the writable snapshot subdirectory (named via the label) which
427 * is to be the initial mount point, or at least the first mount point.
429 * [----reserved_area----][boot_area][aux_area]
430 * [[vol_hdr]... ] [sroot][root]
432 * The sroot and root inodes eat 512 bytes each. newfs labels can only be
433 * 64 bytes so the root (snapshot) inode does not need to extend past 512
434 * bytes. We use the correct hash slot correct but note that because
435 * directory hashes are chained 16x, any slot in the inode will work.
437 * Also format the allocation map.
439 * NOTE: The passed total_space is 8MB-aligned to avoid edge cases.
443 format_hammer2(int fd, hammer2_off_t total_space, hammer2_off_t free_space)
445 char *buf = malloc(HAMMER2_PBUFSIZE);
446 hammer2_volume_data_t *vol;
447 hammer2_inode_data_t *rawip;
448 hammer2_blockref_t sroot_blockref;
449 hammer2_blockref_t root_blockref;
451 hammer2_off_t volu_base = 0;
452 hammer2_off_t boot_base = HAMMER2_ZONE_SEG;
453 hammer2_off_t aux_base = boot_base + BootAreaSize;
454 hammer2_off_t alloc_base = aux_base + AuxAreaSize;
455 hammer2_off_t tmp_base;
460 * Clear the entire reserve for the first 2G segment and
461 * make sure we can write to the last block.
463 bzero(buf, HAMMER2_PBUFSIZE);
464 tmp_base = volu_base;
465 for (i = 0; i < HAMMER2_ZONE_BLOCKS_SEG; ++i) {
466 n = pwrite(fd, buf, HAMMER2_PBUFSIZE, tmp_base);
467 if (n != HAMMER2_PBUFSIZE) {
471 tmp_base += HAMMER2_PBUFSIZE;
474 n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
475 volu_base + total_space - HAMMER2_PBUFSIZE);
476 if (n != HAMMER2_PBUFSIZE) {
477 perror("write (at-end-of-volume)");
482 * Reserve space for the super-root inode and the root inode.
483 * Put them in the same 64K block.
485 assert((alloc_base & HAMMER2_PBUFMASK) == 0);
487 alloc_base &= ~HAMMER2_PBUFMASK64;
488 alloc_direct(&alloc_base, &sroot_blockref, HAMMER2_INODE_BYTES);
489 alloc_direct(&alloc_base, &root_blockref, HAMMER2_INODE_BYTES);
490 assert(((sroot_blockref.data_off ^ root_blockref.data_off) &
491 HAMMER2_OFF_MASK_HI) == 0);
493 bzero(buf, HAMMER2_PBUFSIZE);
497 * Format the root directory inode, which is left empty.
499 rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO & root_blockref.data_off));
500 rawip->version = HAMMER2_INODE_VERSION_ONE;
503 /* rawip->atime = now; NOT IMPL MUST BE ZERO */
505 rawip->type = HAMMER2_OBJTYPE_DIRECTORY;
507 rawip->inum = 1; /* root inode, inumber 1 */
508 rawip->nlinks = 1; /* directory link count compat */
510 rawip->name_len = strlen(Label);
511 bcopy(Label, rawip->filename, rawip->name_len);
512 rawip->name_key = dirhash(rawip->filename, rawip->name_len);
515 * Compression mode and supported copyids.
517 rawip->comp_algo = HAMMER2_COMP_AUTOZERO;
519 rawip->pfs_clid = Hammer2_RPFSId;
520 rawip->pfs_type = HAMMER2_PFSTYPE_MASTER;
521 rawip->op_flags |= HAMMER2_OPFLAG_PFSROOT;
523 /* rawip->u.blockset is left empty */
526 * The root blockref will be stored in the super-root inode as
527 * the only directory entry. The copyid here is the actual copyid
528 * of the storage ref.
530 * The key field for a directory entry's blockref is essentially
531 * the name key for the entry.
533 root_blockref.key = rawip->name_key;
534 root_blockref.copyid = HAMMER2_COPYID_LOCAL;
535 root_blockref.keybits = 0;
536 root_blockref.check.iscsi32.value =
537 hammer2_icrc32(rawip, sizeof(*rawip));
538 root_blockref.type = HAMMER2_BREF_TYPE_INODE;
539 root_blockref.methods = HAMMER2_ENC_CHECKMETHOD(HAMMER2_CHECK_ICRC) |
540 HAMMER2_ENC_COMPMETHOD(HAMMER2_COMP_AUTOZERO);
543 * Format the super-root directory inode, giving it one directory
544 * entry (root_blockref) and fixup the icrc method.
546 * The superroot contains one directory entry pointing at the root
547 * inode (named via the label). Inodes contain one blockset which
548 * is fully associative so we can put the entry anywhere without
549 * having to worry about the hash. Use index 0.
551 rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO & sroot_blockref.data_off));
552 rawip->version = HAMMER2_INODE_VERSION_ONE;
555 /* rawip->atime = now; NOT IMPL MUST BE ZERO */
557 rawip->type = HAMMER2_OBJTYPE_DIRECTORY;
558 rawip->mode = 0700; /* super-root - root only */
559 rawip->inum = 0; /* super root inode, inumber 0 */
560 rawip->nlinks = 2; /* directory link count compat */
562 rawip->name_len = 0; /* super-root is unnamed */
565 rawip->comp_algo = HAMMER2_COMP_AUTOZERO;
568 * The super-root is flagged as a PFS and typically given its own
569 * random FSID, making it possible to mirror an entire HAMMER2 disk
570 * snapshots and all if desired. PFS ids are used to match up
571 * mirror sources and targets and cluster copy sources and targets.
573 rawip->pfs_clid = Hammer2_SPFSId;
574 rawip->pfs_type = HAMMER2_PFSTYPE_MASTER;
575 rawip->op_flags |= HAMMER2_OPFLAG_PFSROOT;
578 * The super-root has one directory entry pointing at the named
581 rawip->u.blockset.blockref[0] = root_blockref;
584 * The sroot blockref will be stored in the volume header.
586 sroot_blockref.copyid = HAMMER2_COPYID_LOCAL;
587 sroot_blockref.keybits = 0;
588 sroot_blockref.check.iscsi32.value =
589 hammer2_icrc32(rawip, sizeof(*rawip));
590 sroot_blockref.type = HAMMER2_BREF_TYPE_INODE;
591 sroot_blockref.methods = HAMMER2_ENC_CHECKMETHOD(HAMMER2_CHECK_ICRC) |
592 HAMMER2_ENC_COMPMETHOD(HAMMER2_COMP_AUTOZERO);
595 * Write out the 64K HAMMER2 block containing the root and sroot.
597 n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
598 root_blockref.data_off & HAMMER2_OFF_MASK_HI);
599 if (n != HAMMER2_PBUFSIZE) {
605 * Format the volume header.
607 * The volume header points to sroot_blockref. Also be absolutely
608 * sure that allocator_beg is set.
610 bzero(buf, HAMMER2_PBUFSIZE);
613 vol->magic = HAMMER2_VOLUME_ID_HBO;
614 vol->boot_beg = boot_base;
615 vol->boot_end = boot_base + BootAreaSize;
616 vol->aux_beg = aux_base;
617 vol->aux_end = aux_base + AuxAreaSize;
618 vol->volu_size = total_space;
619 vol->version = Hammer2Version;
622 vol->fsid = Hammer2_FSId;
623 vol->fstype = Hammer2_FSType;
625 vol->allocator_size = free_space;
626 vol->allocator_free = free_space;
627 vol->allocator_beg = alloc_base;
629 vol->sroot_blockset.blockref[0] = sroot_blockref;
632 vol->icrc_sects[HAMMER2_VOL_ICRC_SECT1] =
633 hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC1_OFF,
634 HAMMER2_VOLUME_ICRC1_SIZE);
637 * Set ICRC_SECT0 after all remaining elements of sect0 have been
638 * populated in the volume header. Note hat ICRC_SECT* (except for
639 * SECT0) are part of sect0.
641 vol->icrc_sects[HAMMER2_VOL_ICRC_SECT0] =
642 hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC0_OFF,
643 HAMMER2_VOLUME_ICRC0_SIZE);
644 vol->icrc_volheader =
645 hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRCVH_OFF,
646 HAMMER2_VOLUME_ICRCVH_SIZE);
649 * Write the volume header and all alternates.
651 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
652 if (i * HAMMER2_ZONE_BYTES64 >= total_space)
654 n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
655 volu_base + i * HAMMER2_ZONE_BYTES64);
656 if (n != HAMMER2_PBUFSIZE) {
669 alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref, size_t bytes)
675 while ((bytes & 1) == 0) {
680 if (radix < HAMMER2_MIN_RADIX)
681 radix = HAMMER2_MIN_RADIX;
683 bzero(bref, sizeof(*bref));
684 bref->data_off = *basep | radix;
685 bref->vradix = radix;
687 *basep += 1U << radix;
691 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
692 * The filename is split into fields which are hashed separately and then
695 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
696 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
697 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
699 * Also, the iscsi crc code is used instead of the old crc32 code.
702 dirhash(const unsigned char *name, size_t len)
704 const unsigned char *aname = name;
711 * Filesystem version 6 or better will create directories
712 * using the ALG1 dirhash. This hash breaks the filename
713 * up into domains separated by special characters and
714 * hashes each domain independently.
716 * We also do a simple sub-sort using the first character
717 * of the filename in the top 5-bits.
725 for (i = j = 0; i < len; ++i) {
726 if (aname[i] == '.' ||
731 crcx += hammer2_icrc32(aname + j, i - j);
736 crcx += hammer2_icrc32(aname + j, i - j);
739 * The directory hash utilizes the top 32 bits of the 64-bit key.
740 * Bit 63 must be set to 1.
743 key |= (uint64_t)crcx << 32;
746 * l16 - crc of entire filename
748 * This crc reduces degenerate hash collision conditions
750 crcx = hammer2_icrc32(aname, len);
751 crcx = crcx ^ (crcx << 16);
752 key |= crcx & 0xFFFF0000U;
755 * Set bit 15. This allows readdir to strip bit 63 so a positive
756 * 64-bit cookie/offset can always be returned, and still guarantee
757 * that the values 0x0000-0x7FFF are available for artificial entries.