f42f8b3a2ef439926633043f2f2fe90ec08f2c83
[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.37 2008/07/07 01:29:32 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(const char *label);
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         if (NumVolumes == 0) {
121                 fprintf(stderr,
122                         "newfs_hammer: You must specify at least one volume\n");
123                 exit(1);
124         }
125
126         total = 0;
127         for (i = 0; i < NumVolumes; ++i) {
128                 vol = setup_volume(i, av[i], 1, O_RDWR);
129
130                 /*
131                  * Load up information on the volume and initialize
132                  * its remaining fields.
133                  */
134                 check_volume(vol);
135                 total += vol->size;
136         }
137
138         /*
139          * Calculate defaults for the boot and memory area sizes.
140          */
141         if (BootAreaSize == 0) {
142                 BootAreaSize = HAMMER_BOOT_NOMBYTES;
143                 while (BootAreaSize > total / NumVolumes / 256)
144                         BootAreaSize >>= 1;
145                 if (BootAreaSize < HAMMER_BOOT_MINBYTES)
146                         BootAreaSize = 0;
147         } else if (BootAreaSize < HAMMER_BOOT_MINBYTES) {
148                 BootAreaSize = HAMMER_BOOT_MINBYTES;
149         }
150         if (MemAreaSize == 0) {
151                 MemAreaSize = HAMMER_MEM_NOMBYTES;
152                 while (MemAreaSize > total / NumVolumes / 256)
153                         MemAreaSize >>= 1;
154                 if (MemAreaSize < HAMMER_MEM_MINBYTES)
155                         MemAreaSize = 0;
156         } else if (MemAreaSize < HAMMER_MEM_MINBYTES) {
157                 MemAreaSize = HAMMER_MEM_MINBYTES;
158         }
159
160         /*
161          * Format the volumes.  Format the root volume first so we can
162          * bootstrap the freemap.
163          */
164         format_volume(get_volume(RootVolNo), NumVolumes, label, total);
165         for (i = 0; i < NumVolumes; ++i) {
166                 if (i != RootVolNo)
167                         format_volume(get_volume(i), NumVolumes, label, total);
168         }
169
170         /*
171          * Pre-size the blockmap layer1/layer2 infrastructure to the zone
172          * limit.  If we do this the filesystem does not have to allocate
173          * new layer2 blocks which reduces the chances of the reblocker
174          * having to fallback to an extremely inefficient algorithm.
175          */
176         vol = get_volume(RootVolNo);
177         vol->ondisk->vol0_stat_bigblocks = vol->ondisk->vol0_stat_freebigblocks;
178         vol->cache.modified = 1;
179
180         printf("---------------------------------------------\n");
181         printf("%d volume%s total size %s\n",
182                 NumVolumes, (NumVolumes == 1 ? "" : "s"), sizetostr(total));
183         printf("boot-area-size:      %s\n", sizetostr(BootAreaSize));
184         printf("memory-log-size:     %s\n", sizetostr(MemAreaSize));
185         printf("undo-buffer-size:    %s\n", sizetostr(UndoBufferSize));
186         printf("total-pre-allocated: %s\n",
187                 sizetostr(vol->vol_free_off & HAMMER_OFF_SHORT_MASK));
188         printf("\n");
189
190         flush_all_volumes();
191         return(0);
192 }
193
194 static
195 void
196 usage(void)
197 {
198         fprintf(stderr, "newfs_hammer vol0 [vol1 ...]\n");
199         exit(1);
200 }
201
202 /*
203  * Convert the size in bytes to a human readable string.
204  */
205 static
206 const char *
207 sizetostr(off_t size)
208 {
209         static char buf[32];
210
211         if (size < 1024 / 2) {
212                 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
213         } else if (size < 1024 * 1024 / 2) {
214                 snprintf(buf, sizeof(buf), "%6.2fKB",
215                         (double)size / 1024);
216         } else if (size < 1024 * 1024 * 1024LL / 2) {
217                 snprintf(buf, sizeof(buf), "%6.2fMB",
218                         (double)size / (1024 * 1024));
219         } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
220                 snprintf(buf, sizeof(buf), "%6.2fGB",
221                         (double)size / (1024 * 1024 * 1024LL));
222         } else {
223                 snprintf(buf, sizeof(buf), "%6.2fTB",
224                         (double)size / (1024 * 1024 * 1024LL * 1024LL));
225         }
226         return(buf);
227 }
228
229 /*
230  * Convert a string to a 64 bit signed integer with various requirements.
231  */
232 static int64_t
233 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
234 {
235         int64_t val;
236         char *ptr;
237
238         val = strtoll(str, &ptr, 0);
239         switch(*ptr) {
240         case 't':
241         case 'T':
242                 val *= 1024;
243                 /* fall through */
244         case 'g':
245         case 'G':
246                 val *= 1024;
247                 /* fall through */
248         case 'm':
249         case 'M':
250                 val *= 1024;
251                 /* fall through */
252         case 'k':
253         case 'K':
254                 val *= 1024;
255                 break;
256         default:
257                 errx(1, "Unknown suffix in number '%s'\n", str);
258                 /* not reached */
259         }
260         if (ptr[1]) {
261                 errx(1, "Unknown suffix in number '%s'\n", str);
262                 /* not reached */
263         }
264         if (val < minval) {
265                 errx(1, "Value too small: %s, min is %s\n",
266                      str, sizetostr(minval));
267                 /* not reached */
268         }
269         if (val > maxval) {
270                 errx(1, "Value too large: %s, max is %s\n",
271                      str, sizetostr(maxval));
272                 /* not reached */
273         }
274         if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
275                 errx(1, "Value not power of 2: %s\n", str);
276                 /* not reached */
277         }
278         if ((powerof2 & 2) && (val & HAMMER_BUFMASK)) {
279                 errx(1, "Value not an integral multiple of %dK: %s", 
280                      HAMMER_BUFSIZE / 1024, str);
281                 /* not reached */
282         }
283         return(val);
284 }
285
286 /*
287  * Generate a transaction id.  Transaction ids are no longer time-based.
288  * Put the nail in the coffin by not making the first one time-based.
289  *
290  * We could start at 1 here but start at 2^32 to reserve a small domain for
291  * possible future use.
292  */
293 static hammer_tid_t
294 createtid(void)
295 {
296         static hammer_tid_t lasttid;
297
298         if (lasttid == 0)
299                 lasttid = 0x0000000100000000ULL;
300         return(lasttid++);
301 }
302
303 static u_int64_t
304 nowtime(void)
305 {
306         struct timeval tv;
307         u_int64_t xtime;
308
309         gettimeofday(&tv, NULL);
310         xtime = tv.tv_sec * 1000000LL + tv.tv_usec;
311         return(xtime);
312 }
313
314 /*
315  * Check basic volume characteristics.  HAMMER filesystems use a minimum
316  * of a 16KB filesystem buffer size.
317  */
318 static
319 void
320 check_volume(struct volume_info *vol)
321 {
322         struct partinfo pinfo;
323         struct stat st;
324
325         /*
326          * Get basic information about the volume
327          */
328         vol->fd = open(vol->name, O_RDWR);
329         if (vol->fd < 0)
330                 err(1, "Unable to open %s R+W", vol->name);
331         if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
332                 /*
333                  * Allow the formatting of regular filews as HAMMER volumes
334                  */
335                 if (fstat(vol->fd, &st) < 0)
336                         err(1, "Unable to stat %s", vol->name);
337                 vol->size = st.st_size;
338                 vol->type = "REGFILE";
339         } else {
340                 /*
341                  * When formatting a block device as a HAMMER volume the
342                  * sector size must be compatible.  HAMMER uses 16384 byte
343                  * filesystem buffers.
344                  */
345                 if (pinfo.reserved_blocks) {
346                         errx(1, "HAMMER cannot be placed in a partition "
347                                 "which overlaps the disklabel or MBR");
348                 }
349                 if (pinfo.media_blksize > 16384 ||
350                     16384 % pinfo.media_blksize) {
351                         errx(1, "A media sector size of %d is not supported",
352                              pinfo.media_blksize);
353                 }
354
355                 vol->size = pinfo.media_size;
356                 vol->type = "DEVICE";
357         }
358         printf("Volume %d %s %-15s size %s\n",
359                vol->vol_no, vol->type, vol->name,
360                sizetostr(vol->size));
361
362         /*
363          * Reserve space for (future) header junk, setup our poor-man's
364          * bigblock allocator.
365          */
366         vol->vol_alloc = HAMMER_BUFSIZE * 16;
367 }
368
369 /*
370  * Format a HAMMER volume.  Cluster 0 will be initially placed in volume 0.
371  */
372 static
373 void
374 format_volume(struct volume_info *vol, int nvols, const char *label,
375               off_t total_size __unused)
376 {
377         struct volume_info *root_vol;
378         struct hammer_volume_ondisk *ondisk;
379         int64_t freeblks;
380         int i;
381
382         /*
383          * Initialize basic information in the on-disk volume structure.
384          */
385         ondisk = vol->ondisk;
386
387         ondisk->vol_fsid = Hammer_FSId;
388         ondisk->vol_fstype = Hammer_FSType;
389         snprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", label);
390         ondisk->vol_no = vol->vol_no;
391         ondisk->vol_count = nvols;
392         ondisk->vol_version = 1;
393
394         ondisk->vol_bot_beg = vol->vol_alloc;
395         vol->vol_alloc += BootAreaSize;
396         ondisk->vol_mem_beg = vol->vol_alloc;
397         vol->vol_alloc += MemAreaSize;
398
399         /*
400          * The remaining area is the zone 2 buffer allocation area.  These
401          * buffers
402          */
403         ondisk->vol_buf_beg = vol->vol_alloc;
404         ondisk->vol_buf_end = vol->size & ~(int64_t)HAMMER_BUFMASK;
405
406         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
407                 errx(1, "volume %d %s is too small to hold the volume header",
408                      vol->vol_no, vol->name);
409         }
410
411         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
412                               HAMMER_BUFSIZE;
413         ondisk->vol_blocksize = HAMMER_BUFSIZE;
414
415         ondisk->vol_rootvol = RootVolNo;
416         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
417
418         vol->vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
419         vol->vol_free_end = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, (ondisk->vol_buf_end - ondisk->vol_buf_beg) & ~HAMMER_LARGEBLOCK_MASK64);
420
421         /*
422          * Format the root volume.
423          */
424         if (vol->vol_no == RootVolNo) {
425                 /*
426                  * Starting TID
427                  */
428                 ondisk->vol0_next_tid = createtid();
429
430                 format_freemap(vol,
431                         &ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX]);
432
433                 freeblks = initialize_freemap(vol);
434                 ondisk->vol0_stat_freebigblocks = freeblks;
435
436                 for (i = 8; i < HAMMER_MAX_ZONES; ++i) {
437                         format_blockmap(&ondisk->vol0_blockmap[i],
438                                         HAMMER_ZONE_ENCODE(i, 0));
439                 }
440                 format_undomap(ondisk);
441
442                 ondisk->vol0_btree_root = format_root(label);
443                 ++ondisk->vol0_stat_inodes;     /* root inode */
444         } else {
445                 freeblks = initialize_freemap(vol);
446                 root_vol = get_volume(RootVolNo);
447                 root_vol->cache.modified = 1;
448                 root_vol->ondisk->vol0_stat_freebigblocks += freeblks;
449                 root_vol->ondisk->vol0_stat_bigblocks += freeblks;
450                 rel_volume(root_vol);
451         }
452 }
453
454 /*
455  * Format the root directory.
456  */
457 static
458 hammer_off_t
459 format_root(const char *label)
460 {
461         hammer_off_t btree_off;
462         hammer_off_t pfsd_off;
463         hammer_off_t data_off;
464         hammer_tid_t create_tid;
465         hammer_node_ondisk_t bnode;
466         struct hammer_inode_data *idata;
467         hammer_pseudofs_data_t pfsd;
468         struct buffer_info *data_buffer1 = NULL;
469         struct buffer_info *data_buffer2 = NULL;
470         hammer_btree_elm_t elm;
471         u_int64_t xtime;
472
473         bnode = alloc_btree_element(&btree_off);
474         idata = alloc_data_element(&data_off, sizeof(*idata), &data_buffer1);
475         pfsd = alloc_data_element(&pfsd_off, sizeof(*pfsd), &data_buffer2);
476         create_tid = createtid();
477         xtime = nowtime();
478
479         /*
480          * Populate the inode data and inode record for the root directory.
481          */
482         idata->version = HAMMER_INODE_DATA_VERSION;
483         idata->mode = 0755;
484         idata->ctime = xtime;
485         idata->mtime = xtime;
486         idata->atime = xtime;
487         idata->obj_type = HAMMER_OBJTYPE_DIRECTORY;
488         idata->size = 0;
489         idata->nlinks = 1;
490
491         pfsd->sync_low_tid = 1;
492         pfsd->sync_beg_tid = 1;
493         pfsd->sync_end_tid = 0; /* overriden by vol0_next_tid on pfs0 */
494         pfsd->shared_uuid = Hammer_FSId;
495         pfsd->unique_uuid = Hammer_FSId;
496         pfsd->master_id = 0;
497         pfsd->mirror_flags = 0;
498         snprintf(pfsd->label, sizeof(pfsd->label), "%s", label);
499
500         /*
501          * Create the root of the B-Tree.  The root is a leaf node so we
502          * do not have to worry about boundary elements.
503          */
504         bnode->signature = HAMMER_BTREE_SIGNATURE_GOOD;
505         bnode->count = 2;
506         bnode->type = HAMMER_BTREE_TYPE_LEAF;
507
508         elm = &bnode->elms[0];
509         elm->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
510         elm->leaf.base.localization = HAMMER_LOCALIZE_INODE;
511         elm->leaf.base.obj_id = HAMMER_OBJID_ROOT;
512         elm->leaf.base.key = 0;
513         elm->leaf.base.create_tid = create_tid;
514         elm->leaf.base.delete_tid = 0;
515         elm->leaf.base.rec_type = HAMMER_RECTYPE_INODE;
516         elm->leaf.base.obj_type = HAMMER_OBJTYPE_DIRECTORY;
517         elm->leaf.create_ts = (u_int32_t)time(NULL);
518
519         elm->leaf.data_offset = data_off;
520         elm->leaf.data_len = sizeof(*idata);
521         elm->leaf.data_crc = crc32(idata, HAMMER_INODE_CRCSIZE);
522
523         elm = &bnode->elms[1];
524         elm->leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
525         elm->leaf.base.localization = HAMMER_LOCALIZE_MISC;
526         elm->leaf.base.obj_id = HAMMER_OBJID_ROOT;
527         elm->leaf.base.key = HAMMER_FIXKEY_PSEUDOFS;
528         elm->leaf.base.create_tid = create_tid;
529         elm->leaf.base.delete_tid = 0;
530         elm->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
531         elm->leaf.base.obj_type = 0;
532         elm->leaf.create_ts = (u_int32_t)time(NULL);
533
534         elm->leaf.data_offset = pfsd_off;
535         elm->leaf.data_len = sizeof(*pfsd);
536         elm->leaf.data_crc = crc32(pfsd, sizeof(*pfsd));
537
538         bnode->crc = crc32(&bnode->crc + 1, HAMMER_BTREE_CRCSIZE);
539
540         return(btree_off);
541 }
542