HAMMER utilities: Misc documentation and new options.
[dfdiff.git] / sbin / newfs_hammer / newfs_hammer.c
blob654761bd9ddc05dcae5a3af70e013015cfcf2fa6
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
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:
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.
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.
34 * $DragonFly: src/sbin/newfs_hammer/newfs_hammer.c,v 1.22 2008/04/27 00:43:57 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 hammer_off_t format_root(void);
44 static void usage(void);
46 int
47 main(int ac, char **av)
49 int i;
50 int ch;
51 u_int32_t status;
52 off_t total;
53 const char *label = NULL;
56 * Sanity check basic filesystem structures. No cookies for us
57 * if it gets broken!
59 assert(sizeof(struct hammer_volume_ondisk) <= HAMMER_BUFSIZE);
60 assert(sizeof(union hammer_record_ondisk) == HAMMER_RECORD_SIZE);
61 assert(sizeof(struct hammer_blockmap_layer1) == 32);
62 assert(sizeof(struct hammer_blockmap_layer2) == 16);
65 * Generate a filesysem id and lookup the filesystem type
67 uuidgen(&Hammer_FSId, 1);
68 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
69 if (status != uuid_s_ok) {
70 errx(1, "uuids file does not have the DragonFly "
71 "HAMMER filesystem type");
75 * Parse arguments
77 while ((ch = getopt(ac, av, "L:b:m:u:")) != -1) {
78 switch(ch) {
79 case 'L':
80 label = optarg;
81 break;
82 case 'b':
83 BootAreaSize = getsize(optarg,
84 HAMMER_BUFSIZE,
85 HAMMER_BOOT_MAXBYTES, 2);
86 break;
87 case 'm':
88 MemAreaSize = getsize(optarg,
89 HAMMER_BUFSIZE,
90 HAMMER_MEM_MAXBYTES, 2);
91 break;
92 case 'u':
93 UndoBufferSize = getsize(optarg,
94 HAMMER_LARGEBLOCK_SIZE,
95 HAMMER_LARGEBLOCK_SIZE *
96 HAMMER_UNDO_LAYER2, 2);
97 break;
98 default:
99 usage();
100 break;
104 if (label == NULL) {
105 fprintf(stderr,
106 "newfs_hammer: A filesystem label must be specified\n");
107 exit(1);
111 * Collect volume information
113 ac -= optind;
114 av += optind;
115 NumVolumes = ac;
116 RootVolNo = 0;
118 total = 0;
119 for (i = 0; i < NumVolumes; ++i) {
120 struct volume_info *vol;
122 vol = setup_volume(i, av[i], 1, O_RDWR);
125 * Load up information on the volume and initialize
126 * its remaining fields.
128 check_volume(vol);
129 total += vol->size;
133 * Calculate defaults for the boot and memory area sizes.
135 if (BootAreaSize == 0) {
136 BootAreaSize = HAMMER_BOOT_NOMBYTES;
137 while (BootAreaSize > total / NumVolumes / 256)
138 BootAreaSize >>= 1;
139 if (BootAreaSize < HAMMER_BOOT_MINBYTES)
140 BootAreaSize = 0;
141 } else if (BootAreaSize < HAMMER_BOOT_MINBYTES) {
142 BootAreaSize = HAMMER_BOOT_MINBYTES;
144 if (MemAreaSize == 0) {
145 MemAreaSize = HAMMER_MEM_NOMBYTES;
146 while (MemAreaSize > total / NumVolumes / 256)
147 MemAreaSize >>= 1;
148 if (MemAreaSize < HAMMER_MEM_MINBYTES)
149 MemAreaSize = 0;
150 } else if (MemAreaSize < HAMMER_MEM_MINBYTES) {
151 MemAreaSize = HAMMER_MEM_MINBYTES;
155 * Format the volumes. Format the root volume first so we can
156 * bootstrap the freemap.
158 format_volume(get_volume(RootVolNo), NumVolumes, label);
159 for (i = 0; i < NumVolumes; ++i) {
160 if (i != RootVolNo)
161 format_volume(get_volume(i), NumVolumes, label);
163 printf("---------------------------------------------\n");
164 printf("%d volume%s total size %s\n",
165 NumVolumes, (NumVolumes == 1 ? "" : "s"), sizetostr(total));
166 printf("boot-area-size: %s\n", sizetostr(BootAreaSize));
167 printf("memory-log-size: %s\n", sizetostr(MemAreaSize));
168 printf("undo-buffer-size: %s\n", sizetostr(UndoBufferSize));
169 printf("\n");
171 flush_all_volumes();
172 return(0);
175 static
176 void
177 usage(void)
179 fprintf(stderr, "newfs_hammer vol0 [vol1 ...]\n");
180 exit(1);
184 * Convert the size in bytes to a human readable string.
186 static
187 const char *
188 sizetostr(off_t size)
190 static char buf[32];
192 if (size < 1024 / 2) {
193 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
194 } else if (size < 1024 * 1024 / 2) {
195 snprintf(buf, sizeof(buf), "%6.2fKB",
196 (double)size / 1024);
197 } else if (size < 1024 * 1024 * 1024LL / 2) {
198 snprintf(buf, sizeof(buf), "%6.2fMB",
199 (double)size / (1024 * 1024));
200 } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
201 snprintf(buf, sizeof(buf), "%6.2fGB",
202 (double)size / (1024 * 1024 * 1024LL));
203 } else {
204 snprintf(buf, sizeof(buf), "%6.2fTB",
205 (double)size / (1024 * 1024 * 1024LL * 1024LL));
207 return(buf);
211 * Convert a string to a 64 bit signed integer with various requirements.
213 static int64_t
214 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
216 int64_t val;
217 char *ptr;
219 val = strtoll(str, &ptr, 0);
220 switch(*ptr) {
221 case 't':
222 case 'T':
223 val *= 1024;
224 /* fall through */
225 case 'g':
226 case 'G':
227 val *= 1024;
228 /* fall through */
229 case 'm':
230 case 'M':
231 val *= 1024;
232 /* fall through */
233 case 'k':
234 case 'K':
235 val *= 1024;
236 break;
237 default:
238 errx(1, "Unknown suffix in number '%s'\n", str);
239 /* not reached */
241 if (ptr[1]) {
242 errx(1, "Unknown suffix in number '%s'\n", str);
243 /* not reached */
245 if (val < minval) {
246 errx(1, "Value too small: %s, min is %s\n",
247 str, sizetostr(minval));
248 /* not reached */
250 if (val > maxval) {
251 errx(1, "Value too large: %s, max is %s\n",
252 str, sizetostr(maxval));
253 /* not reached */
255 if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
256 errx(1, "Value not power of 2: %s\n", str);
257 /* not reached */
259 if ((powerof2 & 2) && (val & HAMMER_BUFMASK)) {
260 errx(1, "Value not an integral multiple of %dK: %s",
261 HAMMER_BUFSIZE / 1024, str);
262 /* not reached */
264 return(val);
268 * Generate a transaction id
270 static hammer_tid_t
271 createtid(void)
273 static hammer_tid_t lasttid;
274 struct timeval tv;
276 if (lasttid == 0) {
277 gettimeofday(&tv, NULL);
278 lasttid = tv.tv_sec * 1000000000LL +
279 tv.tv_usec * 1000LL;
281 return(lasttid++);
285 * Check basic volume characteristics. HAMMER filesystems use a minimum
286 * of a 16KB filesystem buffer size.
288 static
289 void
290 check_volume(struct volume_info *vol)
292 struct partinfo pinfo;
293 struct stat st;
296 * Get basic information about the volume
298 vol->fd = open(vol->name, O_RDWR);
299 if (vol->fd < 0)
300 err(1, "Unable to open %s R+W", vol->name);
301 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
303 * Allow the formatting of regular filews as HAMMER volumes
305 if (fstat(vol->fd, &st) < 0)
306 err(1, "Unable to stat %s", vol->name);
307 vol->size = st.st_size;
308 vol->type = "REGFILE";
309 } else {
311 * When formatting a block device as a HAMMER volume the
312 * sector size must be compatible. HAMMER uses 16384 byte
313 * filesystem buffers.
315 if (pinfo.reserved_blocks) {
316 errx(1, "HAMMER cannot be placed in a partition "
317 "which overlaps the disklabel or MBR");
319 if (pinfo.media_blksize > 16384 ||
320 16384 % pinfo.media_blksize) {
321 errx(1, "A media sector size of %d is not supported",
322 pinfo.media_blksize);
325 vol->size = pinfo.media_size;
326 vol->type = "DEVICE";
328 printf("Volume %d %s %-15s size %s\n",
329 vol->vol_no, vol->type, vol->name,
330 sizetostr(vol->size));
333 * Reserve space for (future) header junk, setup our poor-man's
334 * bigblock allocator.
336 vol->vol_alloc = HAMMER_BUFSIZE * 16;
340 * Format a HAMMER volume. Cluster 0 will be initially placed in volume 0.
342 static
343 void
344 format_volume(struct volume_info *vol, int nvols, const char *label)
346 struct volume_info *root_vol;
347 struct hammer_volume_ondisk *ondisk;
348 int64_t freeblks;
351 * Initialize basic information in the on-disk volume structure.
353 ondisk = vol->ondisk;
355 ondisk->vol_fsid = Hammer_FSId;
356 ondisk->vol_fstype = Hammer_FSType;
357 snprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", label);
358 ondisk->vol_no = vol->vol_no;
359 ondisk->vol_count = nvols;
360 ondisk->vol_version = 1;
362 ondisk->vol_bot_beg = vol->vol_alloc;
363 vol->vol_alloc += BootAreaSize;
364 ondisk->vol_mem_beg = vol->vol_alloc;
365 vol->vol_alloc += MemAreaSize;
368 * The remaining area is the zone 2 buffer allocation area. These
369 * buffers
371 ondisk->vol_buf_beg = vol->vol_alloc;
372 ondisk->vol_buf_end = vol->size & ~(int64_t)HAMMER_BUFMASK;
374 if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
375 errx(1, "volume %d %s is too small to hold the volume header",
376 vol->vol_no, vol->name);
379 ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
380 HAMMER_BUFSIZE;
381 ondisk->vol_blocksize = HAMMER_BUFSIZE;
383 ondisk->vol_rootvol = RootVolNo;
384 ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
386 vol->vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
387 vol->vol_free_end = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, (ondisk->vol_buf_end - ondisk->vol_buf_beg) & ~HAMMER_LARGEBLOCK_MASK64);
390 * Format the root volume.
392 if (vol->vol_no == RootVolNo) {
393 ondisk->vol0_next_tid = createtid();
395 format_freemap(vol,
396 &ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX]);
398 freeblks = initialize_freemap(vol);
399 ondisk->vol0_stat_freebigblocks = freeblks;
400 ondisk->vol0_stat_bigblocks = freeblks;
402 format_blockmap(
403 &ondisk->vol0_blockmap[HAMMER_ZONE_BTREE_INDEX],
404 HAMMER_ZONE_BTREE);
405 format_blockmap(
406 &ondisk->vol0_blockmap[HAMMER_ZONE_RECORD_INDEX],
407 HAMMER_ZONE_RECORD);
408 format_blockmap(
409 &ondisk->vol0_blockmap[HAMMER_ZONE_LARGE_DATA_INDEX],
410 HAMMER_ZONE_LARGE_DATA);
411 format_blockmap(
412 &ondisk->vol0_blockmap[HAMMER_ZONE_SMALL_DATA_INDEX],
413 HAMMER_ZONE_SMALL_DATA);
415 format_undomap(ondisk);
417 ondisk->vol0_btree_root = format_root();
418 ++ondisk->vol0_stat_inodes; /* root inode */
419 } else {
420 freeblks = initialize_freemap(vol);
421 root_vol = get_volume(RootVolNo);
422 root_vol->cache.modified = 1;
423 root_vol->ondisk->vol0_stat_freebigblocks += freeblks;
424 root_vol->ondisk->vol0_stat_bigblocks += freeblks;
425 rel_volume(root_vol);
430 * Format the root directory.
432 static
433 hammer_off_t
434 format_root(void)
436 hammer_off_t btree_off;
437 hammer_off_t rec_off;
438 hammer_node_ondisk_t bnode;
439 hammer_record_ondisk_t rec;
440 struct hammer_inode_data *idata;
441 hammer_btree_elm_t elm;
443 bnode = alloc_btree_element(&btree_off);
444 rec = alloc_record_element(&rec_off, sizeof(*idata), (void **)&idata);
447 * Populate the inode data and inode record for the root directory.
449 idata->version = HAMMER_INODE_DATA_VERSION;
450 idata->mode = 0755;
452 rec->base.base.btype = HAMMER_BTREE_TYPE_RECORD;
453 rec->base.base.obj_id = HAMMER_OBJID_ROOT;
454 rec->base.base.key = 0;
455 rec->base.base.create_tid = createtid();
456 rec->base.base.delete_tid = 0;
457 rec->base.base.rec_type = HAMMER_RECTYPE_INODE;
458 rec->base.base.obj_type = HAMMER_OBJTYPE_DIRECTORY;
459 /* rec->base.data_offset - initialized by alloc_record_element */
460 /* rec->base.data_len - initialized by alloc_record_element */
461 rec->base.data_crc = crc32(idata, sizeof(*idata));
462 rec->inode.ino_atime = rec->base.base.create_tid;
463 rec->inode.ino_mtime = rec->base.base.create_tid;
464 rec->inode.ino_size = 0;
465 rec->inode.ino_nlinks = 1;
468 * Create the root of the B-Tree. The root is a leaf node so we
469 * do not have to worry about boundary elements.
471 bnode->count = 1;
472 bnode->type = HAMMER_BTREE_TYPE_LEAF;
474 elm = &bnode->elms[0];
475 elm->base = rec->base.base;
476 elm->leaf.rec_offset = rec_off;
477 elm->leaf.data_offset = rec->base.data_off;
478 elm->leaf.data_len = rec->base.data_len;
479 elm->leaf.data_crc = rec->base.data_crc;
480 return(btree_off);