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
34 * $DragonFly: src/sbin/newfs_hammer/newfs_hammer.c,v 1.12 2008/01/03 06:48:48 dillon Exp $
37 #include "newfs_hammer.h"
39 static int64_t getsize(const char *str, int64_t minval, int64_t maxval, int pw);
40 static const char *sizetostr(off_t size);
41 static void check_volume(struct volume_info *vol);
42 static void format_volume(struct volume_info *vol, int nvols,const char *label);
43 static int32_t format_cluster(struct volume_info *vol, int isroot);
44 static void format_root(struct cluster_info *cluster);
45 static void usage(void);
48 main(int ac, char **av)
54 int64_t max_volume_size;
55 const char *label = NULL;
58 * Sanity check basic filesystem structures. No cookies for us
61 assert(sizeof(struct hammer_almeta) == HAMMER_ALMETA_SIZE);
62 assert(sizeof(struct hammer_fsbuf_head) == HAMMER_FSBUF_HEAD_SIZE);
63 assert(sizeof(struct hammer_volume_ondisk) <= HAMMER_BUFSIZE);
64 assert(sizeof(struct hammer_cluster_ondisk) <= HAMMER_BUFSIZE);
65 assert(sizeof(struct hammer_fsbuf_data) == HAMMER_BUFSIZE);
66 assert(sizeof(struct hammer_fsbuf_recs) == HAMMER_BUFSIZE);
67 assert(sizeof(struct hammer_fsbuf_btree) == HAMMER_BUFSIZE);
68 assert(sizeof(union hammer_fsbuf_ondisk) == HAMMER_BUFSIZE);
71 * Generate a filesysem id and lookup the filesystem type
73 uuidgen(&Hammer_FSId, 1);
74 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
75 if (status != uuid_s_ok) {
76 errx(1, "uuids file does not have the DragonFly "
77 "HAMMER filesystem type");
80 init_alist_templates();
85 while ((ch = getopt(ac, av, "L:b:c:m:S")) != -1) {
91 BootAreaSize = getsize(optarg,
93 HAMMER_BOOT_MAXBYTES, 2);
96 ClusterSize = getsize(optarg,
97 HAMMER_BUFSIZE * 256LL,
98 HAMMER_CLU_MAXBYTES, 1);
101 MemAreaSize = getsize(optarg,
103 HAMMER_MEM_MAXBYTES, 2);
107 * Force the use of super-clusters
109 UsingSuperClusters = 1;
119 "newfs_hammer: A filesystem label must be specified\n");
124 * Collect volume information
131 for (i = 0; i < NumVolumes; ++i) {
132 struct volume_info *vol;
134 vol = setup_volume(i, av[i], 1, O_RDWR);
137 * Load up information on the volume and initialize
138 * its remaining fields.
145 * Calculate the size of a cluster. A cluster is broken
146 * down into 256 chunks which must be at least filesystem buffer
147 * sized. This gives us a minimum chunk size of around 4MB.
149 if (ClusterSize == 0) {
150 ClusterSize = HAMMER_BUFSIZE * 256;
151 while (ClusterSize < total / NumVolumes / 256 &&
152 ClusterSize < HAMMER_CLU_MAXBYTES) {
158 * Calculate defaults for the boot and memory area sizes.
160 if (BootAreaSize == 0) {
161 BootAreaSize = HAMMER_BOOT_NOMBYTES;
162 while (BootAreaSize > total / NumVolumes / 256)
164 if (BootAreaSize < HAMMER_BOOT_MINBYTES)
166 } else if (BootAreaSize < HAMMER_BOOT_MINBYTES) {
167 BootAreaSize = HAMMER_BOOT_MINBYTES;
169 if (MemAreaSize == 0) {
170 MemAreaSize = HAMMER_MEM_NOMBYTES;
171 while (MemAreaSize > total / NumVolumes / 256)
173 if (MemAreaSize < HAMMER_MEM_MINBYTES)
175 } else if (MemAreaSize < HAMMER_MEM_MINBYTES) {
176 MemAreaSize = HAMMER_MEM_MINBYTES;
179 printf("---------------------------------------------\n");
180 printf("%d volume%s total size %s\n",
181 NumVolumes, (NumVolumes == 1 ? "" : "s"), sizetostr(total));
182 printf("cluster-size: %s\n", sizetostr(ClusterSize));
184 if (UsingSuperClusters) {
185 max_volume_size = (int64_t)HAMMER_VOL_MAXSUPERCLUSTERS * \
186 HAMMER_SCL_MAXCLUSTERS * ClusterSize;
188 max_volume_size = HAMMER_VOL_MAXCLUSTERS * ClusterSize;
190 printf("max-volume-size: %s\n", sizetostr(max_volume_size));
192 printf("max-filesystem-size: %s\n",
193 (max_volume_size * 32768LL < max_volume_size) ?
195 sizetostr(max_volume_size * 32768LL));
196 printf("boot-area-size: %s\n", sizetostr(BootAreaSize));
197 printf("memory-log-size: %s\n", sizetostr(MemAreaSize));
201 * Format the volumes.
203 for (i = 0; i < NumVolumes; ++i) {
204 format_volume(get_volume(i), NumVolumes, label);
214 fprintf(stderr, "newfs_hammer vol0 [vol1 ...]\n");
219 * Convert the size in bytes to a human readable string.
222 sizetostr(off_t size)
226 if (size < 1024 / 2) {
227 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
228 } else if (size < 1024 * 1024 / 2) {
229 snprintf(buf, sizeof(buf), "%6.2fKB",
230 (double)size / 1024);
231 } else if (size < 1024 * 1024 * 1024LL / 2) {
232 snprintf(buf, sizeof(buf), "%6.2fMB",
233 (double)size / (1024 * 1024));
234 } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
235 snprintf(buf, sizeof(buf), "%6.2fGB",
236 (double)size / (1024 * 1024 * 1024LL));
238 snprintf(buf, sizeof(buf), "%6.2fTB",
239 (double)size / (1024 * 1024 * 1024LL * 1024LL));
245 * Convert a string to a 64 bit signed integer with various requirements.
248 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
253 val = strtoll(str, &ptr, 0);
272 errx(1, "Unknown suffix in number '%s'\n", str);
276 errx(1, "Unknown suffix in number '%s'\n", str);
280 errx(1, "Value too small: %s, min is %s\n",
281 str, sizetostr(minval));
285 errx(1, "Value too large: %s, max is %s\n",
286 str, sizetostr(maxval));
289 if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
290 errx(1, "Value not power of 2: %s\n", str);
293 if ((powerof2 & 2) && (val & HAMMER_BUFMASK)) {
294 errx(1, "Value not an integral multiple of %dK: %s",
295 HAMMER_BUFSIZE / 1024, str);
302 * Generate a transaction id
307 static hammer_tid_t lasttid;
311 gettimeofday(&tv, NULL);
312 lasttid = tv.tv_sec * 1000000000LL +
319 * Check basic volume characteristics. HAMMER filesystems use a minimum
320 * of a 16KB filesystem buffer size.
324 check_volume(struct volume_info *vol)
326 struct partinfo pinfo;
330 * Get basic information about the volume
332 vol->fd = open(vol->name, O_RDWR);
334 err(1, "Unable to open %s R+W", vol->name);
335 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
337 * Allow the formatting of regular filews as HAMMER volumes
339 if (fstat(vol->fd, &st) < 0)
340 err(1, "Unable to stat %s", vol->name);
341 vol->size = st.st_size;
342 vol->type = "REGFILE";
345 * When formatting a block device as a HAMMER volume the
346 * sector size must be compatible. HAMMER uses 16384 byte
347 * filesystem buffers.
349 if (pinfo.reserved_blocks) {
350 errx(1, "HAMMER cannot be placed in a partition "
351 "which overlaps the disklabel or MBR");
353 if (pinfo.media_blksize > 16384 ||
354 16384 % pinfo.media_blksize) {
355 errx(1, "A media sector size of %d is not supported",
356 pinfo.media_blksize);
359 vol->size = pinfo.media_size;
360 vol->type = "DEVICE";
362 printf("Volume %d %s %-15s size %s\n",
363 vol->vol_no, vol->type, vol->name,
364 sizetostr(vol->size));
367 * Strictly speaking we do not need to enable super clusters unless
368 * we have volumes > 2TB, but turning them on doesn't really hurt
369 * and if we don't the user may get confused if he tries to expand
370 * the size of an existing volume.
372 if (vol->size > 200LL * 1024 * 1024 * 1024 && !UsingSuperClusters) {
373 UsingSuperClusters = 1;
374 printf("Enabling super-clusters\n");
378 * Reserve space for (future) header junk
380 vol->vol_alloc = HAMMER_BUFSIZE * 16;
384 * Format a HAMMER volume. Cluster 0 will be initially placed in volume 0.
388 format_volume(struct volume_info *vol, int nvols, const char *label)
390 struct hammer_volume_ondisk *ondisk;
394 int64_t scl_group_size;
395 int64_t scl_header_size;
399 * The last cluster in a volume may wind up truncated. It must be
400 * at least minclsize to really be workable as a cluster.
402 minclsize = (int32_t)(ClusterSize / 4);
403 if (minclsize < HAMMER_BUFSIZE * 64)
404 minclsize = HAMMER_BUFSIZE * 64;
407 * Initialize basic information in the on-disk volume structure.
409 ondisk = vol->ondisk;
411 ondisk->vol_fsid = Hammer_FSId;
412 ondisk->vol_fstype = Hammer_FSType;
413 snprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", label);
414 ondisk->vol_no = vol->vol_no;
415 ondisk->vol_count = nvols;
416 ondisk->vol_version = 1;
417 ondisk->vol_clsize = (int32_t)ClusterSize;
418 if (UsingSuperClusters)
419 ondisk->vol_flags = HAMMER_VOLF_USINGSUPERCL;
421 ondisk->vol_bot_beg = vol->vol_alloc;
422 vol->vol_alloc += BootAreaSize;
423 ondisk->vol_mem_beg = vol->vol_alloc;
424 vol->vol_alloc += MemAreaSize;
425 ondisk->vol_clo_beg = vol->vol_alloc;
426 ondisk->vol_clo_end = vol->size;
428 if (ondisk->vol_clo_end < ondisk->vol_clo_beg) {
429 errx(1, "volume %d %s is too small to hold the volume header",
430 vol->vol_no, vol->name);
434 * Our A-lists have been initialized but are marked all-allocated.
435 * Calculate the actual number of clusters in the volume and free
436 * them to get the filesystem ready for work. The clusters will
437 * be initialized on-demand.
439 * If using super-clusters we must still calculate nclusters but
440 * we only need to initialize superclusters that are not going
441 * to wind up in the all-free state, which will only be the last
442 * supercluster. hammer_alist_free() will recurse into the
443 * supercluster infrastructure and create the necessary superclusters.
445 * NOTE: The nclusters calculation ensures that the volume EOF does
446 * not occur in the middle of a supercluster buffer array.
448 if (UsingSuperClusters) {
450 * Figure out how many full super-cluster groups we will have.
451 * This calculation does not include the partial supercluster
454 scl_header_size = (int64_t)HAMMER_BUFSIZE *
455 HAMMER_VOL_SUPERCLUSTER_GROUP;
456 scl_group_size = scl_header_size +
457 (int64_t)HAMMER_VOL_SUPERCLUSTER_GROUP *
458 ClusterSize * HAMMER_SCL_MAXCLUSTERS;
459 nscl_groups = (ondisk->vol_clo_end - ondisk->vol_clo_beg) /
461 nclusters = nscl_groups * HAMMER_SCL_MAXCLUSTERS *
462 HAMMER_VOL_SUPERCLUSTER_GROUP;
465 * Figure out how much space we have left and calculate the
466 * remaining number of clusters.
468 n64 = (ondisk->vol_clo_end - ondisk->vol_clo_beg) -
469 (nscl_groups * scl_group_size);
470 if (n64 > scl_header_size) {
471 nclusters += (n64 + minclsize) / ClusterSize;
473 printf("%d clusters, %d full super-cluster groups\n",
474 nclusters, nscl_groups);
475 hammer_alist_init(&vol->clu_alist, 0, nclusters,
478 nclusters = (ondisk->vol_clo_end - ondisk->vol_clo_beg +
479 minclsize) / ClusterSize;
480 if (nclusters > HAMMER_VOL_MAXCLUSTERS) {
481 errx(1, "Volume is too large, max %s\n",
482 sizetostr(nclusters * ClusterSize));
484 hammer_alist_init(&vol->clu_alist, 0, nclusters,
487 ondisk->vol_nclusters = nclusters;
488 ondisk->vol_nblocks = nclusters * ClusterSize / HAMMER_BUFSIZE -
490 ondisk->vol_blocksize = HAMMER_BUFSIZE;
493 * Place the root cluster in volume 0.
495 ondisk->vol_rootvol = 0;
496 if (ondisk->vol_no == (int)ondisk->vol_rootvol) {
497 ondisk->vol0_root_clu_id = format_cluster(vol, 1);
498 ondisk->vol0_recid = 1;
499 /* global next TID */
500 ondisk->vol0_nexttid = createtid();
505 * Format a hammer cluster. Returns byte offset in volume of cluster.
509 format_cluster(struct volume_info *vol, int isroot)
511 hammer_tid_t clu_id = createtid();
512 struct cluster_info *cluster;
513 struct hammer_cluster_ondisk *ondisk;
520 clno = hammer_alist_alloc(&vol->clu_alist, 1);
521 if (clno == HAMMER_ALIST_BLOCK_NONE) {
522 fprintf(stderr, "volume %d %s has insufficient space\n",
523 vol->vol_no, vol->name);
526 cluster = get_cluster(vol, clno, 1);
527 printf("allocate cluster id=%016llx %d@%08llx\n",
528 clu_id, clno, cluster->clu_offset);
530 ondisk = cluster->ondisk;
532 ondisk->vol_fsid = vol->ondisk->vol_fsid;
533 ondisk->vol_fstype = vol->ondisk->vol_fstype;
535 ondisk->clu_id = clu_id;
536 ondisk->clu_no = clno;
537 ondisk->clu_flags = 0;
538 ondisk->clu_start = HAMMER_BUFSIZE;
539 if (vol->size - cluster->clu_offset > ClusterSize)
540 ondisk->clu_limit = (u_int32_t)ClusterSize;
542 ondisk->clu_limit = (u_int32_t)(vol->size - cluster->clu_offset);
545 * In-band filesystem buffer management A-List. The first filesystem
546 * buffer is the cluster header itself.
548 nbuffers = ondisk->clu_limit / HAMMER_BUFSIZE;
549 hammer_alist_free(&cluster->alist_master, 1, nbuffers - 1);
550 printf("cluster %d has %d buffers\n", cluster->clu_no, nbuffers);
553 * Buffer Iterators in elements. Each buffer has 256 elements.
554 * The data and B-Tree indices are forward allocations while the
555 * record index allocates backwards.
557 ondisk->idx_data = 1 * HAMMER_FSBUF_MAXBLKS;
558 ondisk->idx_index = 0 * HAMMER_FSBUF_MAXBLKS;
559 ondisk->idx_record = nbuffers * HAMMER_FSBUF_MAXBLKS;
562 * Iterator for whole-buffer data allocations. The iterator is
565 ondisk->idx_ldata = 1;
568 * Initialize root cluster's parent cluster info. -1's
569 * indicate we are the root cluster and no parent exists.
571 ondisk->clu_btree_parent_vol_no = -1;
572 ondisk->clu_btree_parent_clu_no = -1;
573 ondisk->clu_btree_parent_offset = -1;
574 ondisk->clu_btree_parent_clu_gen = -1;
577 * Cluster 0 is the root cluster. Set the B-Tree range for this
578 * cluster to the entire key space and format the root directory.
580 * Note that delete_tid for the ending range must be set to 0,
581 * 0 indicates 'not deleted', aka 'the most recent'. See
582 * hammer_btree_cmp() in sys/vfs/hammer/hammer_btree.c.
584 * The root cluster's key space represents the entire key space for
585 * the filesystem. The btree_end element appears to be inclusive
586 * only because we can't overflow our variables. It's actually
587 * non-inclusive... that is, it is a right-side boundary element.
590 ondisk->clu_btree_beg.obj_id = -0x8000000000000000LL;
591 ondisk->clu_btree_beg.key = -0x8000000000000000LL;
592 ondisk->clu_btree_beg.create_tid = 0;
593 ondisk->clu_btree_beg.delete_tid = 0;
594 ondisk->clu_btree_beg.rec_type = 0;
595 ondisk->clu_btree_beg.obj_type = 0;
597 ondisk->clu_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
598 ondisk->clu_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
599 ondisk->clu_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
600 ondisk->clu_btree_end.delete_tid = 0; /* special case */
601 ondisk->clu_btree_end.rec_type = 0xFFFFU;
602 ondisk->clu_btree_end.obj_type = 0;
604 format_root(cluster);
608 * Write-out and update the index, record, and cluster buffers
614 * Format the root directory.
618 format_root(struct cluster_info *cluster)
623 hammer_node_ondisk_t bnode;
624 union hammer_record_ondisk *rec;
625 struct hammer_inode_data *idata;
626 hammer_btree_elm_t elm;
628 bnode = alloc_btree_element(cluster, &btree_off);
629 rec = alloc_record_element(cluster, &rec_off);
630 idata = alloc_data_element(cluster, sizeof(*idata), &data_off);
633 * Populate the inode data and inode record for the root directory.
635 idata->version = HAMMER_INODE_DATA_VERSION;
638 rec->base.base.obj_id = HAMMER_OBJID_ROOT;
639 rec->base.base.key = 0;
640 rec->base.base.create_tid = createtid();
641 rec->base.base.delete_tid = 0;
642 rec->base.base.rec_type = HAMMER_RECTYPE_INODE;
643 rec->base.base.obj_type = HAMMER_OBJTYPE_DIRECTORY;
644 rec->base.data_offset = data_off;
645 rec->base.data_len = sizeof(*idata);
646 rec->base.data_crc = crc32(idata, sizeof(*idata));
647 rec->inode.ino_atime = rec->base.base.create_tid;
648 rec->inode.ino_mtime = rec->base.base.create_tid;
649 rec->inode.ino_size = 0;
650 rec->inode.ino_nlinks = 1;
652 ++cluster->volume->ondisk->vol0_stat_inodes;
655 * Assign the cluster's root B-Tree node.
657 assert(cluster->ondisk->clu_btree_root == 0);
658 cluster->ondisk->clu_btree_root = btree_off;
661 * Create the root of the B-Tree. The root is a leaf node so we
662 * do not have to worry about boundary elements.
665 bnode->type = HAMMER_BTREE_TYPE_LEAF;
667 elm = &bnode->elms[0];
668 elm->base = rec->base.base;
669 elm->leaf.rec_offset = rec_off;
670 elm->leaf.data_offset = rec->base.data_offset;
671 elm->leaf.data_len = rec->base.data_len;
672 elm->leaf.data_crc = rec->base.data_crc;