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.39.2.2 2008/07/19 20:26:17 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
,
44 static hammer_off_t
format_root(const char *label
);
45 static u_int64_t
nowtime(void);
46 static void usage(void);
49 main(int ac
, char **av
)
55 const char *label
= NULL
;
56 struct volume_info
*vol
;
60 * Sanity check basic filesystem structures. No cookies for us
63 assert(sizeof(struct hammer_volume_ondisk
) <= HAMMER_BUFSIZE
);
64 assert(sizeof(struct hammer_blockmap_layer1
) == 32);
65 assert(sizeof(struct hammer_blockmap_layer2
) == 16);
68 * Generate a filesysem id and lookup the filesystem type
70 uuidgen(&Hammer_FSId
, 1);
71 uuid_name_lookup(&Hammer_FSType
, "DragonFly HAMMER", &status
);
72 if (status
!= uuid_s_ok
) {
73 errx(1, "uuids file does not have the DragonFly "
74 "HAMMER filesystem type");
80 while ((ch
= getopt(ac
, av
, "L:b:m:u:")) != -1) {
86 BootAreaSize
= getsize(optarg
,
88 HAMMER_BOOT_MAXBYTES
, 2);
91 MemAreaSize
= getsize(optarg
,
93 HAMMER_MEM_MAXBYTES
, 2);
96 UndoBufferSize
= getsize(optarg
,
97 HAMMER_LARGEBLOCK_SIZE
,
98 HAMMER_LARGEBLOCK_SIZE
*
99 HAMMER_UNDO_LAYER2
, 2);
109 "newfs_hammer: A filesystem label must be specified\n");
114 * Collect volume information
121 if (NumVolumes
== 0) {
123 "newfs_hammer: You must specify at least one special file (volume)\n");
128 for (i
= 0; i
< NumVolumes
; ++i
) {
129 vol
= setup_volume(i
, av
[i
], 1, O_RDWR
);
132 * Load up information on the volume and initialize
133 * its remaining fields.
140 * Calculate defaults for the boot and memory area sizes.
142 if (BootAreaSize
== 0) {
143 BootAreaSize
= HAMMER_BOOT_NOMBYTES
;
144 while (BootAreaSize
> total
/ NumVolumes
/ 256)
146 if (BootAreaSize
< HAMMER_BOOT_MINBYTES
)
148 } else if (BootAreaSize
< HAMMER_BOOT_MINBYTES
) {
149 BootAreaSize
= HAMMER_BOOT_MINBYTES
;
151 if (MemAreaSize
== 0) {
152 MemAreaSize
= HAMMER_MEM_NOMBYTES
;
153 while (MemAreaSize
> total
/ NumVolumes
/ 256)
155 if (MemAreaSize
< HAMMER_MEM_MINBYTES
)
157 } else if (MemAreaSize
< HAMMER_MEM_MINBYTES
) {
158 MemAreaSize
= HAMMER_MEM_MINBYTES
;
162 * Format the volumes. Format the root volume first so we can
163 * bootstrap the freemap.
165 format_volume(get_volume(RootVolNo
), NumVolumes
, label
, total
);
166 for (i
= 0; i
< NumVolumes
; ++i
) {
168 format_volume(get_volume(i
), NumVolumes
, label
, total
);
172 * Pre-size the blockmap layer1/layer2 infrastructure to the zone
173 * limit. If we do this the filesystem does not have to allocate
174 * new layer2 blocks which reduces the chances of the reblocker
175 * having to fallback to an extremely inefficient algorithm.
177 vol
= get_volume(RootVolNo
);
178 vol
->ondisk
->vol0_stat_bigblocks
= vol
->ondisk
->vol0_stat_freebigblocks
;
179 vol
->cache
.modified
= 1;
180 uuid_to_string(&Hammer_FSId
, &fsidstr
, &status
);
182 printf("---------------------------------------------\n");
183 printf("%d volume%s total size %s\n",
184 NumVolumes
, (NumVolumes
== 1 ? "" : "s"), sizetostr(total
));
185 printf("boot-area-size: %s\n", sizetostr(BootAreaSize
));
186 printf("memory-log-size: %s\n", sizetostr(MemAreaSize
));
187 printf("undo-buffer-size: %s\n", sizetostr(UndoBufferSize
));
188 printf("total-pre-allocated: %s\n",
189 sizetostr(vol
->vol_free_off
& HAMMER_OFF_SHORT_MASK
));
190 printf("fsid: %s\n", fsidstr
);
202 "newfs_hammer -L label [-b bootsize] [-m savesize] [ -u undosize] "
209 * Convert the size in bytes to a human readable string.
213 sizetostr(off_t size
)
217 if (size
< 1024 / 2) {
218 snprintf(buf
, sizeof(buf
), "%6.2f", (double)size
);
219 } else if (size
< 1024 * 1024 / 2) {
220 snprintf(buf
, sizeof(buf
), "%6.2fKB",
221 (double)size
/ 1024);
222 } else if (size
< 1024 * 1024 * 1024LL / 2) {
223 snprintf(buf
, sizeof(buf
), "%6.2fMB",
224 (double)size
/ (1024 * 1024));
225 } else if (size
< 1024 * 1024 * 1024LL * 1024LL / 2) {
226 snprintf(buf
, sizeof(buf
), "%6.2fGB",
227 (double)size
/ (1024 * 1024 * 1024LL));
229 snprintf(buf
, sizeof(buf
), "%6.2fTB",
230 (double)size
/ (1024 * 1024 * 1024LL * 1024LL));
236 * Convert a string to a 64 bit signed integer with various requirements.
239 getsize(const char *str
, int64_t minval
, int64_t maxval
, int powerof2
)
244 val
= strtoll(str
, &ptr
, 0);
263 errx(1, "Unknown suffix in number '%s'\n", str
);
267 errx(1, "Unknown suffix in number '%s'\n", str
);
271 errx(1, "Value too small: %s, min is %s\n",
272 str
, sizetostr(minval
));
276 errx(1, "Value too large: %s, max is %s\n",
277 str
, sizetostr(maxval
));
280 if ((powerof2
& 1) && (val
^ (val
- 1)) != ((val
<< 1) - 1)) {
281 errx(1, "Value not power of 2: %s\n", str
);
284 if ((powerof2
& 2) && (val
& HAMMER_BUFMASK
)) {
285 errx(1, "Value not an integral multiple of %dK: %s",
286 HAMMER_BUFSIZE
/ 1024, str
);
293 * Generate a transaction id. Transaction ids are no longer time-based.
294 * Put the nail in the coffin by not making the first one time-based.
296 * We could start at 1 here but start at 2^32 to reserve a small domain for
297 * possible future use.
302 static hammer_tid_t lasttid
;
305 lasttid
= 0x0000000100000000ULL
;
315 gettimeofday(&tv
, NULL
);
316 xtime
= tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
321 * Check basic volume characteristics. HAMMER filesystems use a minimum
322 * of a 16KB filesystem buffer size.
326 check_volume(struct volume_info
*vol
)
328 struct partinfo pinfo
;
332 * Get basic information about the volume
334 vol
->fd
= open(vol
->name
, O_RDWR
);
336 err(1, "Unable to open %s R+W", vol
->name
);
337 if (ioctl(vol
->fd
, DIOCGPART
, &pinfo
) < 0) {
339 * Allow the formatting of regular files as HAMMER volumes
341 if (fstat(vol
->fd
, &st
) < 0)
342 err(1, "Unable to stat %s", vol
->name
);
343 vol
->size
= st
.st_size
;
344 vol
->type
= "REGFILE";
347 * When formatting a block device as a HAMMER volume the
348 * sector size must be compatible. HAMMER uses 16384 byte
349 * filesystem buffers.
351 if (pinfo
.reserved_blocks
) {
352 errx(1, "HAMMER cannot be placed in a partition "
353 "which overlaps the disklabel or MBR");
355 if (pinfo
.media_blksize
> 16384 ||
356 16384 % pinfo
.media_blksize
) {
357 errx(1, "A media sector size of %d is not supported",
358 pinfo
.media_blksize
);
361 vol
->size
= pinfo
.media_size
;
362 vol
->type
= "DEVICE";
364 printf("Volume %d %s %-15s size %s\n",
365 vol
->vol_no
, vol
->type
, vol
->name
,
366 sizetostr(vol
->size
));
369 * Reserve space for (future) header junk, setup our poor-man's
370 * bigblock allocator.
372 vol
->vol_alloc
= HAMMER_BUFSIZE
* 16;
376 * Format a HAMMER volume. Cluster 0 will be initially placed in volume 0.
380 format_volume(struct volume_info
*vol
, int nvols
, const char *label
,
381 off_t total_size __unused
)
383 struct volume_info
*root_vol
;
384 struct hammer_volume_ondisk
*ondisk
;
389 * Initialize basic information in the on-disk volume structure.
391 ondisk
= vol
->ondisk
;
393 ondisk
->vol_fsid
= Hammer_FSId
;
394 ondisk
->vol_fstype
= Hammer_FSType
;
395 snprintf(ondisk
->vol_name
, sizeof(ondisk
->vol_name
), "%s", label
);
396 ondisk
->vol_no
= vol
->vol_no
;
397 ondisk
->vol_count
= nvols
;
398 ondisk
->vol_version
= 1;
400 ondisk
->vol_bot_beg
= vol
->vol_alloc
;
401 vol
->vol_alloc
+= BootAreaSize
;
402 ondisk
->vol_mem_beg
= vol
->vol_alloc
;
403 vol
->vol_alloc
+= MemAreaSize
;
406 * The remaining area is the zone 2 buffer allocation area. These
409 ondisk
->vol_buf_beg
= vol
->vol_alloc
;
410 ondisk
->vol_buf_end
= vol
->size
& ~(int64_t)HAMMER_BUFMASK
;
412 if (ondisk
->vol_buf_end
< ondisk
->vol_buf_beg
) {
413 errx(1, "volume %d %s is too small to hold the volume header",
414 vol
->vol_no
, vol
->name
);
417 ondisk
->vol_nblocks
= (ondisk
->vol_buf_end
- ondisk
->vol_buf_beg
) /
419 ondisk
->vol_blocksize
= HAMMER_BUFSIZE
;
421 ondisk
->vol_rootvol
= RootVolNo
;
422 ondisk
->vol_signature
= HAMMER_FSBUF_VOLUME
;
424 vol
->vol_free_off
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, 0);
425 vol
->vol_free_end
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, (ondisk
->vol_buf_end
- ondisk
->vol_buf_beg
) & ~HAMMER_LARGEBLOCK_MASK64
);
428 * Format the root volume.
430 if (vol
->vol_no
== RootVolNo
) {
434 ondisk
->vol0_next_tid
= createtid();
437 &ondisk
->vol0_blockmap
[HAMMER_ZONE_FREEMAP_INDEX
]);
439 freeblks
= initialize_freemap(vol
);
440 ondisk
->vol0_stat_freebigblocks
= freeblks
;
442 for (i
= 8; i
< HAMMER_MAX_ZONES
; ++i
) {
443 format_blockmap(&ondisk
->vol0_blockmap
[i
],
444 HAMMER_ZONE_ENCODE(i
, 0));
446 format_undomap(ondisk
);
448 ondisk
->vol0_btree_root
= format_root(label
);
449 ++ondisk
->vol0_stat_inodes
; /* root inode */
451 freeblks
= initialize_freemap(vol
);
452 root_vol
= get_volume(RootVolNo
);
453 root_vol
->cache
.modified
= 1;
454 root_vol
->ondisk
->vol0_stat_freebigblocks
+= freeblks
;
455 root_vol
->ondisk
->vol0_stat_bigblocks
+= freeblks
;
456 rel_volume(root_vol
);
461 * Format the root directory.
465 format_root(const char *label
)
467 hammer_off_t btree_off
;
468 hammer_off_t pfsd_off
;
469 hammer_off_t data_off
;
470 hammer_tid_t create_tid
;
471 hammer_node_ondisk_t bnode
;
472 struct hammer_inode_data
*idata
;
473 hammer_pseudofs_data_t pfsd
;
474 struct buffer_info
*data_buffer1
= NULL
;
475 struct buffer_info
*data_buffer2
= NULL
;
476 hammer_btree_elm_t elm
;
479 bnode
= alloc_btree_element(&btree_off
);
480 idata
= alloc_data_element(&data_off
, sizeof(*idata
), &data_buffer1
);
481 pfsd
= alloc_data_element(&pfsd_off
, sizeof(*pfsd
), &data_buffer2
);
482 create_tid
= createtid();
486 * Populate the inode data and inode record for the root directory.
488 idata
->version
= HAMMER_INODE_DATA_VERSION
;
490 idata
->ctime
= xtime
;
491 idata
->mtime
= xtime
;
492 idata
->atime
= xtime
;
493 idata
->obj_type
= HAMMER_OBJTYPE_DIRECTORY
;
497 pfsd
->sync_low_tid
= 1;
498 pfsd
->sync_beg_tid
= 0;
499 pfsd
->sync_end_tid
= 0; /* overriden by vol0_next_tid on pfs0 */
500 pfsd
->shared_uuid
= Hammer_FSId
;
501 pfsd
->unique_uuid
= Hammer_FSId
;
502 pfsd
->reserved01
= 0;
503 pfsd
->mirror_flags
= 0;
504 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", label
);
507 * Create the root of the B-Tree. The root is a leaf node so we
508 * do not have to worry about boundary elements.
510 bnode
->signature
= HAMMER_BTREE_SIGNATURE_GOOD
;
512 bnode
->type
= HAMMER_BTREE_TYPE_LEAF
;
514 elm
= &bnode
->elms
[0];
515 elm
->leaf
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
516 elm
->leaf
.base
.localization
= HAMMER_LOCALIZE_INODE
;
517 elm
->leaf
.base
.obj_id
= HAMMER_OBJID_ROOT
;
518 elm
->leaf
.base
.key
= 0;
519 elm
->leaf
.base
.create_tid
= create_tid
;
520 elm
->leaf
.base
.delete_tid
= 0;
521 elm
->leaf
.base
.rec_type
= HAMMER_RECTYPE_INODE
;
522 elm
->leaf
.base
.obj_type
= HAMMER_OBJTYPE_DIRECTORY
;
523 elm
->leaf
.create_ts
= (u_int32_t
)time(NULL
);
525 elm
->leaf
.data_offset
= data_off
;
526 elm
->leaf
.data_len
= sizeof(*idata
);
527 elm
->leaf
.data_crc
= crc32(idata
, HAMMER_INODE_CRCSIZE
);
529 elm
= &bnode
->elms
[1];
530 elm
->leaf
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
531 elm
->leaf
.base
.localization
= HAMMER_LOCALIZE_MISC
;
532 elm
->leaf
.base
.obj_id
= HAMMER_OBJID_ROOT
;
533 elm
->leaf
.base
.key
= 0;
534 elm
->leaf
.base
.create_tid
= create_tid
;
535 elm
->leaf
.base
.delete_tid
= 0;
536 elm
->leaf
.base
.rec_type
= HAMMER_RECTYPE_PFS
;
537 elm
->leaf
.base
.obj_type
= 0;
538 elm
->leaf
.create_ts
= (u_int32_t
)time(NULL
);
540 elm
->leaf
.data_offset
= pfsd_off
;
541 elm
->leaf
.data_len
= sizeof(*pfsd
);
542 elm
->leaf
.data_crc
= crc32(pfsd
, sizeof(*pfsd
));
544 bnode
->crc
= crc32(&bnode
->crc
+ 1, HAMMER_BTREE_CRCSIZE
);