HAMMER Utilities: Sync to 58B
[dragonfly.git] / sbin / newfs_hammer / newfs_hammer.c
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:
10  * 
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.
20  * 
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.
33  * 
34  * $DragonFly: src/sbin/newfs_hammer/newfs_hammer.c,v 1.35 2008/06/24 17:40:22 dillon Exp $
35  */
36
37 #include "newfs_hammer.h"
38
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                         off_t total_size);
44 static hammer_off_t format_root(void);
45 static u_int64_t nowtime(void);
46 static void usage(void);
47
48 int
49 main(int ac, char **av)
50 {
51         u_int32_t status;
52         off_t total;
53         int ch;
54         int i;
55         const char *label = NULL;
56         struct volume_info *vol;
57
58         /*
59          * Sanity check basic filesystem structures.  No cookies for us
60          * if it gets broken!
61          */
62         assert(sizeof(struct hammer_volume_ondisk) <= HAMMER_BUFSIZE);
63         assert(sizeof(struct hammer_blockmap_layer1) == 32);
64         assert(sizeof(struct hammer_blockmap_layer2) == 16);
65
66         /*
67          * Generate a filesysem id and lookup the filesystem type
68          */
69         uuidgen(&Hammer_FSId, 1);
70         uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
71         if (status != uuid_s_ok) {
72                 errx(1, "uuids file does not have the DragonFly "
73                         "HAMMER filesystem type");
74         }
75
76         /*
77          * Parse arguments
78          */
79         while ((ch = getopt(ac, av, "L:b:m:u:")) != -1) {
80                 switch(ch) {
81                 case 'L':
82                         label = optarg;
83                         break;
84                 case 'b':
85                         BootAreaSize = getsize(optarg,
86                                          HAMMER_BUFSIZE,
87                                          HAMMER_BOOT_MAXBYTES, 2);
88                         break;
89                 case 'm':
90                         MemAreaSize = getsize(optarg,
91                                          HAMMER_BUFSIZE,
92                                          HAMMER_MEM_MAXBYTES, 2);
93                         break;
94                 case 'u':
95                         UndoBufferSize = getsize(optarg,
96                                          HAMMER_LARGEBLOCK_SIZE,
97                                          HAMMER_LARGEBLOCK_SIZE *
98                                          HAMMER_UNDO_LAYER2, 2);
99                         break;
100                 default:
101                         usage();
102                         break;
103                 }
104         }
105
106         if (label == NULL) {
107                 fprintf(stderr,
108                         "newfs_hammer: A filesystem label must be specified\n");
109                 exit(1);
110         }
111
112         /*
113          * Collect volume information
114          */
115         ac -= optind;
116         av += optind;
117         NumVolumes = ac;
118         RootVolNo = 0;
119
120         total = 0;
121         for (i = 0; i < NumVolumes; ++i) {
122                 vol = setup_volume(i, av[i], 1, O_RDWR);
123
124                 /*
125                  * Load up information on the volume and initialize
126                  * its remaining fields.
127                  */
128                 check_volume(vol);
129                 total += vol->size;
130         }
131
132         /*
133          * Calculate defaults for the boot and memory area sizes.
134          */
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;
143         }
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;
152         }
153
154         /*
155          * Format the volumes.  Format the root volume first so we can
156          * bootstrap the freemap.
157          */
158         format_volume(get_volume(RootVolNo), NumVolumes, label, total);
159         for (i = 0; i < NumVolumes; ++i) {
160                 if (i != RootVolNo)
161                         format_volume(get_volume(i), NumVolumes, label, total);
162         }
163
164         /*
165          * Pre-size the blockmap layer1/layer2 infrastructure to the zone
166          * limit.  If we do this the filesystem does not have to allocate
167          * new layer2 blocks which reduces the chances of the reblocker
168          * having to fallback to an extremely inefficient algorithm.
169          */
170         vol = get_volume(RootVolNo);
171         vol->ondisk->vol0_stat_bigblocks = vol->ondisk->vol0_stat_freebigblocks;
172         vol->cache.modified = 1;
173
174         printf("---------------------------------------------\n");
175         printf("%d volume%s total size %s\n",
176                 NumVolumes, (NumVolumes == 1 ? "" : "s"), sizetostr(total));
177         printf("boot-area-size:      %s\n", sizetostr(BootAreaSize));
178         printf("memory-log-size:     %s\n", sizetostr(MemAreaSize));
179         printf("undo-buffer-size:    %s\n", sizetostr(UndoBufferSize));
180         printf("total-pre-allocated: %s\n",
181                 sizetostr(vol->vol_free_off & HAMMER_OFF_SHORT_MASK));
182         printf("\n");
183
184         flush_all_volumes();
185         return(0);
186 }
187
188 static
189 void
190 usage(void)
191 {
192         fprintf(stderr, "newfs_hammer vol0 [vol1 ...]\n");
193         exit(1);
194 }
195
196 /*
197  * Convert the size in bytes to a human readable string.
198  */
199 static
200 const char *
201 sizetostr(off_t size)
202 {
203         static char buf[32];
204
205         if (size < 1024 / 2) {
206                 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
207         } else if (size < 1024 * 1024 / 2) {
208                 snprintf(buf, sizeof(buf), "%6.2fKB",
209                         (double)size / 1024);
210         } else if (size < 1024 * 1024 * 1024LL / 2) {
211                 snprintf(buf, sizeof(buf), "%6.2fMB",
212                         (double)size / (1024 * 1024));
213         } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
214                 snprintf(buf, sizeof(buf), "%6.2fGB",
215                         (double)size / (1024 * 1024 * 1024LL));
216         } else {
217                 snprintf(buf, sizeof(buf), "%6.2fTB",
218                         (double)size / (1024 * 1024 * 1024LL * 1024LL));
219         }
220         return(buf);
221 }
222
223 /*
224  * Convert a string to a 64 bit signed integer with various requirements.
225  */
226 static int64_t
227 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
228 {
229         int64_t val;
230         char *ptr;
231
232         val = strtoll(str, &ptr, 0);
233         switch(*ptr) {
234         case 't':
235         case 'T':
236                 val *= 1024;
237                 /* fall through */
238         case 'g':
239         case 'G':
240                 val *= 1024;
241                 /* fall through */
242         case 'm':
243         case 'M':
244                 val *= 1024;
245                 /* fall through */
246         case 'k':
247         case 'K':
248                 val *= 1024;
249                 break;
250         default:
251                 errx(1, "Unknown suffix in number '%s'\n", str);
252                 /* not reached */
253         }
254         if (ptr[1]) {
255                 errx(1, "Unknown suffix in number '%s'\n", str);
256                 /* not reached */
257         }
258         if (val < minval) {
259                 errx(1, "Value too small: %s, min is %s\n",
260                      str, sizetostr(minval));
261                 /* not reached */
262         }
263         if (val > maxval) {
264                 errx(1, "Value too large: %s, max is %s\n",
265                      str, sizetostr(maxval));
266                 /* not reached */
267         }
268         if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
269                 errx(1, "Value not power of 2: %s\n", str);
270                 /* not reached */
271         }
272         if ((powerof2 & 2) && (val & HAMMER_BUFMASK)) {
273                 errx(1, "Value not an integral multiple of %dK: %s", 
274                      HAMMER_BUFSIZE / 1024, str);
275                 /* not reached */
276         }
277         return(val);
278 }
279
280 /*
281  * Generate a transaction id.  Transaction ids are no longer time-based.
282  * Put the nail in the coffin by not making the first one time-based.
283  *
284  * We could start at 1 here but start at 2^32 to reserve a small domain for
285  * possible future use.
286  */
287 static hammer_tid_t
288 createtid(void)
289 {
290         static hammer_tid_t lasttid;
291
292         if (lasttid == 0)
293                 lasttid = 0x0000000100000000ULL;
294         return(lasttid++);
295 }
296
297 static u_int64_t
298 nowtime(void)
299 {
300         struct timeval tv;
301         u_int64_t xtime;
302
303         gettimeofday(&tv, NULL);
304         xtime = tv.tv_sec * 1000000LL + tv.tv_usec;
305         return(xtime);
306 }
307
308 /*
309  * Check basic volume characteristics.  HAMMER filesystems use a minimum
310  * of a 16KB filesystem buffer size.
311  */
312 static
313 void
314 check_volume(struct volume_info *vol)
315 {
316         struct partinfo pinfo;
317         struct stat st;
318
319         /*
320          * Get basic information about the volume
321          */
322         vol->fd = open(vol->name, O_RDWR);
323         if (vol->fd < 0)
324                 err(1, "Unable to open %s R+W", vol->name);
325         if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
326                 /*
327                  * Allow the formatting of regular filews as HAMMER volumes
328                  */
329                 if (fstat(vol->fd, &st) < 0)
330                         err(1, "Unable to stat %s", vol->name);
331                 vol->size = st.st_size;
332                 vol->type = "REGFILE";
333         } else {
334                 /*
335                  * When formatting a block device as a HAMMER volume the
336                  * sector size must be compatible.  HAMMER uses 16384 byte
337                  * filesystem buffers.
338                  */
339                 if (pinfo.reserved_blocks) {
340                         errx(1, "HAMMER cannot be placed in a partition "
341                                 "which overlaps the disklabel or MBR");
342                 }
343                 if (pinfo.media_blksize > 16384 ||
344                     16384 % pinfo.media_blksize) {
345                         errx(1, "A media sector size of %d is not supported",
346                              pinfo.media_blksize);
347                 }
348
349                 vol->size = pinfo.media_size;
350                 vol->type = "DEVICE";
351         }
352         printf("Volume %d %s %-15s size %s\n",
353                vol->vol_no, vol->type, vol->name,
354                sizetostr(vol->size));
355
356         /*
357          * Reserve space for (future) header junk, setup our poor-man's
358          * bigblock allocator.
359          */
360         vol->vol_alloc = HAMMER_BUFSIZE * 16;
361 }
362
363 /*
364  * Format a HAMMER volume.  Cluster 0 will be initially placed in volume 0.
365  */
366 static
367 void
368 format_volume(struct volume_info *vol, int nvols, const char *label,
369               off_t total_size __unused)
370 {
371         struct volume_info *root_vol;
372         struct hammer_volume_ondisk *ondisk;
373         int64_t freeblks;
374         int i;
375
376         /*
377          * Initialize basic information in the on-disk volume structure.
378          */
379         ondisk = vol->ondisk;
380
381         ondisk->vol_fsid = Hammer_FSId;
382         ondisk->vol_fstype = Hammer_FSType;
383         snprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", label);
384         ondisk->vol_no = vol->vol_no;
385         ondisk->vol_count = nvols;
386         ondisk->vol_version = 1;
387
388         ondisk->vol_bot_beg = vol->vol_alloc;
389         vol->vol_alloc += BootAreaSize;
390         ondisk->vol_mem_beg = vol->vol_alloc;
391         vol->vol_alloc += MemAreaSize;
392
393         /*
394          * The remaining area is the zone 2 buffer allocation area.  These
395          * buffers
396          */
397         ondisk->vol_buf_beg = vol->vol_alloc;
398         ondisk->vol_buf_end = vol->size & ~(int64_t)HAMMER_BUFMASK;
399
400         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
401                 errx(1, "volume %d %s is too small to hold the volume header",
402                      vol->vol_no, vol->name);
403         }
404
405         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
406                               HAMMER_BUFSIZE;
407         ondisk->vol_blocksize = HAMMER_BUFSIZE;
408
409         ondisk->vol_rootvol = RootVolNo;
410         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
411
412         vol->vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
413         vol->vol_free_end = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, (ondisk->vol_buf_end - ondisk->vol_buf_beg) & ~HAMMER_LARGEBLOCK_MASK64);
414
415         /*
416          * Format the root volume.
417          */
418         if (vol->vol_no == RootVolNo) {
419                 /*
420                  * Starting TID
421                  */
422                 ondisk->vol0_next_tid = createtid();
423
424                 format_freemap(vol,
425                         &ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX]);
426
427                 freeblks = initialize_freemap(vol);
428                 ondisk->vol0_stat_freebigblocks = freeblks;
429
430                 for (i = 8; i < HAMMER_MAX_ZONES; ++i) {
431                         format_blockmap(&ondisk->vol0_blockmap[i],
432                                         HAMMER_ZONE_ENCODE(i, 0));
433                 }
434                 format_undomap(ondisk);
435
436                 ondisk->vol0_btree_root = format_root();
437                 ++ondisk->vol0_stat_inodes;     /* root inode */
438         } else {
439                 freeblks = initialize_freemap(vol);
440                 root_vol = get_volume(RootVolNo);
441                 root_vol->cache.modified = 1;
442                 root_vol->ondisk->vol0_stat_freebigblocks += freeblks;
443                 root_vol->ondisk->vol0_stat_bigblocks += freeblks;
444                 rel_volume(root_vol);
445         }
446 }
447
448 /*
449  * Format the root directory.
450  */
451 static
452 hammer_off_t
453 format_root(void)
454 {
455         hammer_off_t btree_off;
456         hammer_off_t data_off;
457         hammer_tid_t create_tid;
458         hammer_node_ondisk_t bnode;
459         struct hammer_inode_data *idata;
460         struct buffer_info *data_buffer = NULL;
461         hammer_btree_elm_t elm;
462         u_int64_t xtime;
463
464         bnode = alloc_btree_element(&btree_off);
465         idata = alloc_data_element(&data_off, sizeof(*idata), &data_buffer);
466         create_tid = createtid();
467         xtime = nowtime();
468
469         /*
470          * Populate the inode data and inode record for the root directory.
471          */
472         idata->version = HAMMER_INODE_DATA_VERSION;
473         idata->mode = 0755;
474         idata->ctime = xtime;
475         idata->mtime = xtime;
476         idata->atime = xtime;
477         idata->obj_type = HAMMER_OBJTYPE_DIRECTORY;
478         idata->size = 0;
479         idata->nlinks = 1;
480
481         /*
482          * Create the root of the B-Tree.  The root is a leaf node so we
483          * do not have to worry about boundary elements.
484          */
485         bnode->signature = HAMMER_BTREE_SIGNATURE_GOOD;
486         bnode->count = 1;
487         bnode->type = HAMMER_BTREE_TYPE_LEAF;
488
489         elm = &bnode->elms[0];
490         elm->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
491         elm->leaf.base.localization = HAMMER_LOCALIZE_INODE;
492         elm->leaf.base.obj_id = HAMMER_OBJID_ROOT;
493         elm->leaf.base.key = 0;
494         elm->leaf.base.create_tid = create_tid;
495         elm->leaf.base.delete_tid = 0;
496         elm->leaf.base.rec_type = HAMMER_RECTYPE_INODE;
497         elm->leaf.base.obj_type = HAMMER_OBJTYPE_DIRECTORY;
498         elm->leaf.create_ts = (u_int32_t)time(NULL);
499
500         elm->leaf.data_offset = data_off;
501         elm->leaf.data_len = sizeof(*idata);
502         elm->leaf.data_crc = crc32(idata, HAMMER_INODE_CRCSIZE);
503
504         bnode->crc = crc32(&bnode->crc + 1, HAMMER_BTREE_CRCSIZE);
505
506         return(btree_off);
507 }
508