2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
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/sysctl.h>
36 #include <sys/ioctl_compat.h>
38 #include "hammer_util.h"
40 static int64_t getsize(const char *str
, int pw
);
41 static int trim_volume(volume_info_t volume
);
42 static void format_volume(volume_info_t volume
, int nvols
,const char *label
);
43 static hammer_off_t
format_root_directory(const char *label
);
44 static uint64_t nowtime(void);
45 static void print_volume(const volume_info_t volume
);
46 static void usage(int exit_code
);
47 static void test_header_junk_size(int64_t size
);
48 static void test_boot_area_size(int64_t size
);
49 static void test_memory_log_size(int64_t size
);
50 static void test_undo_buffer_size(int64_t size
);
53 static int64_t HeaderJunkSize
= -1;
54 static int64_t BootAreaSize
= -1;
55 static int64_t MemoryLogSize
= -1;
56 static int64_t UndoBufferSize
;
58 #define GIG (1024LL*1024*1024)
61 main(int ac
, char **av
)
70 const char *label
= NULL
;
74 * Sanity check basic filesystem structures. No cookies for us
77 assert(sizeof(struct hammer_volume_ondisk
) <= HAMMER_BUFSIZE
);
78 assert(sizeof(struct hammer_volume_ondisk
) <= HAMMER_MIN_VOL_JUNK
);
79 assert(sizeof(struct hammer_blockmap_layer1
) == 32);
80 assert(sizeof(struct hammer_blockmap_layer2
) == 16);
85 while ((ch
= getopt(ac
, av
, "dfEL:j:b:m:u:hC:V:")) != -1) {
99 case 'j': /* Not mentioned in newfs_hammer(8) */
100 HeaderJunkSize
= getsize(optarg
, 2);
101 test_header_junk_size(HeaderJunkSize
);
104 BootAreaSize
= getsize(optarg
, 2);
105 test_boot_area_size(BootAreaSize
);
108 MemoryLogSize
= getsize(optarg
, 2);
109 test_memory_log_size(MemoryLogSize
);
112 UndoBufferSize
= getsize(optarg
, 2);
113 test_undo_buffer_size(UndoBufferSize
);
120 if (hammer_parse_cache_size(optarg
) == -1) {
126 HammerVersion
= strtol(optarg
, NULL
, 0);
127 if (HammerVersion
< HAMMER_VOL_VERSION_MIN
||
128 HammerVersion
>= HAMMER_VOL_VERSION_WIP
) {
130 "I don't understand how to format "
146 if (HammerVersion
== (uint32_t)-1) {
147 size_t olen
= sizeof(HammerVersion
);
148 HammerVersion
= HAMMER_VOL_VERSION_DEFAULT
;
150 if (sysctlbyname("vfs.hammer.supported_version",
151 &HammerVersion
, &olen
, NULL
, 0)) {
152 hwarn("HAMMER VFS not loaded, cannot get version info, "
155 } else if (HammerVersion
>= HAMMER_VOL_VERSION_WIP
) {
156 HammerVersion
= HAMMER_VOL_VERSION_WIP
- 1;
157 hwarn("HAMMER VFS supports higher version than "
158 "I understand, using version %d",
164 errx(1, "You must specify at least one special file (volume)");
167 if (nvols
> HAMMER_MAX_VOLUMES
) {
168 errx(1, "The maximum number of volumes is %d",
174 hwarnx("A filesystem label must be specified");
180 * Generate a filesystem id and lookup the filesystem type
182 uuidgen(&Hammer_FSId
, 1);
183 uuid_name_lookup(&Hammer_FSType
, HAMMER_FSTYPE_STRING
, &status
);
184 if (status
!= uuid_s_ok
) {
185 errx(1, "uuids file does not have the DragonFly "
186 "HAMMER filesystem type");
191 for (i
= 0; i
< nvols
; ++i
) {
192 volume
= init_volume(av
[i
], O_RDWR
, i
);
193 printf("Volume %d %s %-15s size %s\n",
194 volume
->vol_no
, volume
->type
, volume
->name
,
195 sizetostr(volume
->size
));
198 if (trim_volume(volume
) == -1 && ForceOpt
== 0) {
199 errx(1, "Use -f option to proceed");
203 total
+= volume
->size
;
207 * Reserve space for (future) header junk, setup our poor-man's
208 * big-block allocator. Note that the header junk space includes
209 * volume header which is 1928 bytes.
211 if (HeaderJunkSize
== -1)
212 HeaderJunkSize
= HAMMER_VOL_JUNK_SIZE
;
213 else if (HeaderJunkSize
< (int64_t)sizeof(struct hammer_volume_ondisk
))
214 HeaderJunkSize
= sizeof(struct hammer_volume_ondisk
);
215 HeaderJunkSize
= HAMMER_BUFSIZE_DOALIGN(HeaderJunkSize
);
218 * Calculate defaults for the boot area and memory log sizes,
219 * only if not specified by -b or -m option.
221 avg_vol_size
= total
/ nvols
;
222 if (BootAreaSize
== -1)
223 BootAreaSize
= init_boot_area_size(BootAreaSize
, avg_vol_size
);
224 if (MemoryLogSize
== -1)
225 MemoryLogSize
= init_memory_log_size(MemoryLogSize
, avg_vol_size
);
228 * Format the volumes. Format the root volume first so we can
229 * bootstrap the freemap.
231 format_volume(get_root_volume(), nvols
, label
);
232 for (i
= 0; i
< nvols
; ++i
) {
233 if (i
!= HAMMER_ROOT_VOLNO
)
234 format_volume(get_volume(i
), nvols
, label
);
237 print_volume(get_root_volume());
245 print_volume(const volume_info_t volume
)
247 hammer_volume_ondisk_t ondisk
;
248 hammer_blockmap_t blockmap
;
249 hammer_off_t total
= 0;
252 const char *name
= NULL
;
255 ondisk
= volume
->ondisk
;
256 blockmap
= &ondisk
->vol0_blockmap
[HAMMER_ZONE_UNDO_INDEX
];
258 nvols
= ondisk
->vol_count
;
259 for (i
= 0; i
< nvols
; ++i
) {
260 volume_info_t p
= get_volume(i
);
262 if (p
->vol_no
== HAMMER_ROOT_VOLNO
) {
263 assert(name
== NULL
);
268 uuid_to_string(&Hammer_FSId
, &fsidstr
, &status
);
270 printf("---------------------------------------------\n");
271 printf("HAMMER version %d\n", HammerVersion
);
272 printf("%d volume%s total size %s\n",
273 nvols
, (nvols
== 1 ? "" : "s"), sizetostr(total
));
274 printf("root-volume: %s\n", name
);
276 printf("header-junk-size: %s\n",
277 sizetostr(ondisk
->vol_bot_beg
));
279 printf("boot-area-size: %s\n",
280 sizetostr(ondisk
->vol_mem_beg
- ondisk
->vol_bot_beg
));
281 printf("memory-log-size: %s\n",
282 sizetostr(ondisk
->vol_buf_beg
- ondisk
->vol_mem_beg
));
283 printf("undo-buffer-size: %s\n",
284 sizetostr(HAMMER_OFF_LONG_ENCODE(blockmap
->alloc_offset
)));
285 printf("total-pre-allocated: %s\n",
286 sizetostr(HAMMER_OFF_SHORT_ENCODE(volume
->vol_free_off
)));
287 printf("fsid: %s\n", fsidstr
);
289 printf("NOTE: Please remember that you may have to manually set up a\n"
290 "cron(8) job to prune and reblock the filesystem regularly.\n"
291 "By default, the system automatically runs 'hammer cleanup'\n"
292 "on a nightly basis. The periodic.conf(5) variable\n"
293 "'daily_clean_hammer_enable' can be unset to disable this.\n"
294 "Also see 'man hammer' and 'man HAMMER' for more information.\n");
295 if (total
< 10*GIG
) {
296 printf("\nWARNING: The minimum UNDO/REDO FIFO is %s, "
297 "you really should not\n"
298 "try to format a HAMMER filesystem this small.\n",
299 sizetostr(HAMMER_BIGBLOCK_SIZE
*
300 HAMMER_MIN_UNDO_BIGBLOCKS
));
302 if (total
< 50*GIG
) {
303 printf("\nWARNING: HAMMER filesystems less than 50GB are "
305 "You may have to run 'hammer prune-everything' and "
307 "quite often, even if using a nohistory mount.\n");
316 "usage: newfs_hammer -L label [-Efh] [-b bootsize] [-m savesize] [-u undosize]\n"
317 " [-C cachesize[:readahead]] [-V version] special ...\n"
324 test_header_junk_size(int64_t size
)
326 if (size
< HAMMER_MIN_VOL_JUNK
) {
328 errx(1, "The minimum header junk size is %s",
329 sizetostr(HAMMER_MIN_VOL_JUNK
));
332 hwarnx("You have specified header junk size less than %s",
333 sizetostr(HAMMER_MIN_VOL_JUNK
));
335 } else if (size
> HAMMER_MAX_VOL_JUNK
) {
336 errx(1, "The maximum header junk size is %s",
337 sizetostr(HAMMER_MAX_VOL_JUNK
));
344 test_boot_area_size(int64_t size
)
346 if (size
< HAMMER_BOOT_MINBYTES
) {
348 errx(1, "The minimum boot area size is %s",
349 sizetostr(HAMMER_BOOT_MINBYTES
));
352 hwarnx("You have specified boot area size less than %s",
353 sizetostr(HAMMER_BOOT_MINBYTES
));
355 } else if (size
> HAMMER_BOOT_MAXBYTES
) {
356 errx(1, "The maximum boot area size is %s",
357 sizetostr(HAMMER_BOOT_MAXBYTES
));
364 test_memory_log_size(int64_t size
)
366 if (size
< HAMMER_MEM_MINBYTES
) {
368 errx(1, "The minimum memory log size is %s",
369 sizetostr(HAMMER_MEM_MINBYTES
));
372 hwarnx("You have specified memory log size less than %s",
373 sizetostr(HAMMER_MEM_MINBYTES
));
375 } else if (size
> HAMMER_MEM_MAXBYTES
) {
376 errx(1, "The maximum memory log size is %s",
377 sizetostr(HAMMER_MEM_MAXBYTES
));
384 test_undo_buffer_size(int64_t size
)
386 int64_t minbuf
, maxbuf
;
388 minbuf
= HAMMER_BIGBLOCK_SIZE
* HAMMER_MIN_UNDO_BIGBLOCKS
;
389 maxbuf
= HAMMER_BIGBLOCK_SIZE
* HAMMER_MAX_UNDO_BIGBLOCKS
;
393 errx(1, "The minimum UNDO/REDO FIFO size is %s",
397 hwarnx("You have specified an UNDO/REDO FIFO size less "
398 "than %s, which may lead to VFS panics",
401 } else if (size
> maxbuf
) {
402 errx(1, "The maximum UNDO/REDO FIFO size is %s",
409 * Convert a string to a 64 bit signed integer with various requirements.
413 getsize(const char *str
, int powerof2
)
418 val
= strtoll(str
, &ptr
, 0);
437 errx(1, "Unknown suffix in number '%s'", str
);
442 errx(1, "Unknown suffix in number '%s'", str
);
445 if ((powerof2
& 1) && (val
^ (val
- 1)) != ((val
<< 1) - 1)) {
446 errx(1, "Value not power of 2: %s", str
);
449 if ((powerof2
& 2) && (val
& HAMMER_BUFMASK
)) {
450 errx(1, "Value not an integral multiple of %dK: %s",
451 HAMMER_BUFSIZE
/ 1024, str
);
458 * Generate a transaction id. Transaction ids are no longer time-based.
459 * Put the nail in the coffin by not making the first one time-based.
461 * We could start at 1 here but start at 2^32 to reserve a small domain for
462 * possible future use.
468 static hammer_tid_t lasttid
;
471 lasttid
= 0x0000000100000000ULL
;
482 gettimeofday(&tv
, NULL
);
483 xtime
= tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
488 * TRIM the volume, but only if the backing store is not a regular file
492 trim_volume(volume_info_t volume
)
496 char sysctl_name
[64];
500 if (is_regfile(volume
)) {
501 hwarnx("Cannot TRIM regular file %s", volume
->name
);
504 if (strncmp(volume
->name
, "/dev/da", 7)) {
505 hwarnx("%s does not support the TRIM command", volume
->name
);
509 /* Extract a number from /dev/da?s? */
510 dev_name
= strdup(volume
->name
);
511 p
= strtok(dev_name
+ strlen("/dev/da"), "s");
512 sprintf(sysctl_name
, "kern.cam.da.%s.trim_enabled", p
);
516 olen
= sizeof(trim_enabled
);
518 if (sysctlbyname(sysctl_name
, &trim_enabled
, &olen
, NULL
, 0) == -1) {
519 hwarnx("%s (%s) does not support the TRIM command",
520 volume
->name
, sysctl_name
);
524 hwarnx("Erase device option selected, but sysctl (%s) "
525 "is not enabled", sysctl_name
);
529 ioarg
[0] = volume
->device_offset
;
530 ioarg
[1] = volume
->size
;
532 printf("Trimming %s %s, sectors %llu-%llu\n",
533 volume
->type
, volume
->name
,
534 (unsigned long long)ioarg
[0] / 512,
535 (unsigned long long)ioarg
[1] / 512);
537 if (ioctl(volume
->fd
, IOCTLTRIM
, ioarg
) == -1) {
538 err(1, "Trimming %s failed", volume
->name
);
546 * Format a HAMMER volume.
550 format_volume(volume_info_t volume
, int nvols
, const char *label
)
552 volume_info_t root_vol
;
553 hammer_volume_ondisk_t ondisk
;
556 int64_t vol_buf_size
;
557 hammer_off_t vol_alloc
;
561 * Initialize basic information in the on-disk volume structure.
563 ondisk
= volume
->ondisk
;
565 ondisk
->vol_fsid
= Hammer_FSId
;
566 ondisk
->vol_fstype
= Hammer_FSType
;
567 snprintf(ondisk
->vol_label
, sizeof(ondisk
->vol_label
), "%s", label
);
568 ondisk
->vol_no
= volume
->vol_no
;
569 ondisk
->vol_count
= nvols
;
570 ondisk
->vol_version
= HammerVersion
;
572 vol_alloc
= HeaderJunkSize
;
573 ondisk
->vol_bot_beg
= vol_alloc
;
574 vol_alloc
+= BootAreaSize
;
575 ondisk
->vol_mem_beg
= vol_alloc
;
576 vol_alloc
+= MemoryLogSize
;
579 * The remaining area is the zone 2 buffer allocation area.
581 ondisk
->vol_buf_beg
= vol_alloc
;
582 ondisk
->vol_buf_end
= volume
->size
& ~(int64_t)HAMMER_BUFMASK
;
583 vol_buf_size
= HAMMER_VOL_BUF_SIZE(ondisk
);
585 if (vol_buf_size
< (int64_t)sizeof(*ondisk
)) {
586 errx(1, "volume %d %s is too small to hold the volume header",
587 volume
->vol_no
, volume
->name
);
590 if ((vol_buf_size
& ~HAMMER_OFF_SHORT_MASK
) != 0) {
591 errx(1, "volume %d %s is too large", volume
->vol_no
, volume
->name
);
595 ondisk
->vol_rootvol
= HAMMER_ROOT_VOLNO
;
596 ondisk
->vol_signature
= HAMMER_FSBUF_VOLUME
;
598 volume
->vol_free_off
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
, 0);
599 volume
->vol_free_end
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
,
600 vol_buf_size
& ~HAMMER_BIGBLOCK_MASK64
);
603 * Format the root volume.
605 if (volume
->vol_no
== HAMMER_ROOT_VOLNO
) {
607 * Check freemap counts before formatting
609 freeblks
= count_freemap(volume
);
610 freebytes
= freeblks
* HAMMER_BIGBLOCK_SIZE64
;
611 if (freebytes
< 10*GIG
&& ForceOpt
== 0) {
612 errx(1, "Cannot create a HAMMER filesystem less than 10GB "
613 "unless you use -f\n(for the size of Volume %d). "
614 "HAMMER filesystems less than 50GB are not "
615 "recommended.", HAMMER_ROOT_VOLNO
);
622 ondisk
->vol0_next_tid
= createtid();
625 * Format freemap. vol0_stat_freebigblocks is
626 * the number of big-blocks available for anything
627 * other than freemap zone at this point.
629 format_freemap(volume
);
630 assert(ondisk
->vol0_stat_freebigblocks
== 0);
631 ondisk
->vol0_stat_freebigblocks
= initialize_freemap(volume
);
634 * Format zones that are mapped to zone-2.
636 for (i
= 0; i
< HAMMER_MAX_ZONES
; ++i
) {
637 if (hammer_is_index_record(i
))
638 format_blockmap(volume
, i
, 0);
642 * Format undo zone. Formatting decrements
643 * vol0_stat_freebigblocks whenever a new big-block
644 * is allocated for undo zone.
646 format_undomap(volume
, &UndoBufferSize
);
647 assert(ondisk
->vol0_stat_bigblocks
== 0);
648 ondisk
->vol0_stat_bigblocks
= ondisk
->vol0_stat_freebigblocks
;
651 * Format the root directory. Formatting decrements
652 * vol0_stat_freebigblocks whenever a new big-block
653 * is allocated for required zones.
655 ondisk
->vol0_btree_root
= format_root_directory(label
);
656 ++ondisk
->vol0_stat_inodes
; /* root inode */
658 freeblks
= initialize_freemap(volume
);
659 root_vol
= get_root_volume();
660 root_vol
->ondisk
->vol0_stat_freebigblocks
+= freeblks
;
661 root_vol
->ondisk
->vol0_stat_bigblocks
+= freeblks
;
666 * Format the root directory.
670 format_root_directory(const char *label
)
672 hammer_off_t btree_off
;
673 hammer_off_t idata_off
;
674 hammer_off_t pfsd_off
;
675 hammer_tid_t create_tid
;
676 hammer_node_ondisk_t bnode
;
677 hammer_inode_data_t idata
;
678 hammer_pseudofs_data_t pfsd
;
679 buffer_info_t data_buffer0
= NULL
;
680 buffer_info_t data_buffer1
= NULL
;
681 buffer_info_t data_buffer2
= NULL
;
682 hammer_btree_elm_t elm
;
686 * Allocate zero-filled root btree node, inode and pfs
688 bnode
= alloc_btree_node(&btree_off
, &data_buffer0
);
689 idata
= alloc_meta_element(&idata_off
, sizeof(*idata
), &data_buffer1
);
690 pfsd
= alloc_meta_element(&pfsd_off
, sizeof(*pfsd
), &data_buffer2
);
691 create_tid
= createtid();
695 * Populate the inode data and inode record for the root directory.
697 idata
->version
= HAMMER_INODE_DATA_VERSION
;
699 idata
->ctime
= xtime
;
700 idata
->mtime
= xtime
;
701 idata
->atime
= xtime
;
702 idata
->obj_type
= HAMMER_OBJTYPE_DIRECTORY
;
705 if (HammerVersion
>= HAMMER_VOL_VERSION_TWO
)
706 idata
->cap_flags
|= HAMMER_INODE_CAP_DIR_LOCAL_INO
;
707 if (HammerVersion
>= HAMMER_VOL_VERSION_SIX
)
708 idata
->cap_flags
|= HAMMER_INODE_CAP_DIRHASH_ALG1
;
711 * Populate the PFS data for the root PFS.
713 pfsd
->sync_low_tid
= 1;
714 pfsd
->sync_beg_tid
= 0;
715 pfsd
->sync_end_tid
= 0; /* overriden by vol0_next_tid on root PFS */
716 pfsd
->shared_uuid
= Hammer_FSId
;
717 pfsd
->unique_uuid
= Hammer_FSId
;
718 pfsd
->mirror_flags
= 0;
719 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", label
);
722 * Create the root of the B-Tree. The root is a leaf node so we
723 * do not have to worry about boundary elements.
725 bnode
->parent
= 0; /* no parent */
727 bnode
->type
= HAMMER_BTREE_TYPE_LEAF
;
728 bnode
->mirror_tid
= 0;
731 * Create the first node element for the inode.
733 elm
= &bnode
->elms
[0];
734 elm
->leaf
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
735 elm
->leaf
.base
.localization
= HAMMER_DEF_LOCALIZATION
|
736 HAMMER_LOCALIZE_INODE
;
737 elm
->leaf
.base
.obj_id
= HAMMER_OBJID_ROOT
;
738 elm
->leaf
.base
.key
= 0;
739 elm
->leaf
.base
.create_tid
= create_tid
;
740 elm
->leaf
.base
.delete_tid
= 0;
741 elm
->leaf
.base
.rec_type
= HAMMER_RECTYPE_INODE
;
742 elm
->leaf
.base
.obj_type
= HAMMER_OBJTYPE_DIRECTORY
;
743 elm
->leaf
.create_ts
= (uint32_t)time(NULL
);
745 elm
->leaf
.data_offset
= idata_off
;
746 elm
->leaf
.data_len
= sizeof(*idata
);
747 hammer_crc_set_leaf(HammerVersion
, idata
, &elm
->leaf
);
750 * Create the second node element for the PFS data.
751 * This is supposed to be a record part of the root ip (inode),
752 * so it should have the same obj_type value as above.
754 elm
= &bnode
->elms
[1];
755 elm
->leaf
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
756 elm
->leaf
.base
.localization
= HAMMER_DEF_LOCALIZATION
|
757 HAMMER_LOCALIZE_MISC
;
758 elm
->leaf
.base
.obj_id
= HAMMER_OBJID_ROOT
;
759 elm
->leaf
.base
.key
= 0;
760 elm
->leaf
.base
.create_tid
= create_tid
;
761 elm
->leaf
.base
.delete_tid
= 0;
762 elm
->leaf
.base
.rec_type
= HAMMER_RECTYPE_PFS
;
763 elm
->leaf
.base
.obj_type
= HAMMER_OBJTYPE_DIRECTORY
;
764 elm
->leaf
.create_ts
= (uint32_t)time(NULL
);
766 elm
->leaf
.data_offset
= pfsd_off
;
767 elm
->leaf
.data_len
= sizeof(*pfsd
);
768 hammer_crc_set_leaf(HammerVersion
, pfsd
, &elm
->leaf
);
770 hammer_crc_set_btree(HammerVersion
, bnode
);
772 rel_buffer(data_buffer0
);
773 rel_buffer(data_buffer1
);
774 rel_buffer(data_buffer2
);