bb397bbbbd69368ded28af5f11cf4c835bde6cfd
[dragonfly.git] / sbin / newfs_hammer2 / newfs_hammer2.c
1 /*
2  * Copyright (c) 2011-2014 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
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
35 #include <sys/types.h>
36 #include <sys/diskslice.h>
37 #include <sys/diskmbr.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <sys/sysctl.h>
41 #include <vfs/hammer2/hammer2_disk.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <stddef.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <assert.h>
52 #include <err.h>
53 #include <uuid.h>
54
55 #define MAXLABELS       HAMMER2_SET_COUNT
56
57 #define hammer2_icrc32(buf, size)       iscsi_crc32((buf), (size))
58 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
59 uint32_t iscsi_crc32(const void *buf, size_t size);
60 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
61
62 static hammer2_off_t check_volume(const char *path, int *fdp);
63 static int64_t getsize(const char *str, int64_t minval, int64_t maxval, int pw);
64 static const char *sizetostr(hammer2_off_t size);
65 static uint64_t nowtime(void);
66 static int blkrefary_cmp(const void *b1, const void *b2);
67 static void usage(void);
68
69 static void format_hammer2(int fd, hammer2_off_t total_space,
70                                 hammer2_off_t free_space);
71 static void alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref,
72                                 size_t bytes);
73 static hammer2_key_t dirhash(const unsigned char *name, size_t len);
74
75 static int Hammer2Version = -1;
76 static int ForceOpt = 0;
77 static uuid_t Hammer2_FSType;   /* static filesystem type id for HAMMER2 */
78 static uuid_t Hammer2_VolFSID;  /* unique filesystem id in volu header */
79 static uuid_t Hammer2_SupCLID;  /* PFS cluster id in super-root inode */
80 static uuid_t Hammer2_SupFSID;  /* PFS unique id in super-root inode */
81 static uuid_t Hammer2_PfsCLID[MAXLABELS];
82 static uuid_t Hammer2_PfsFSID[MAXLABELS];
83 static const char *Label[MAXLABELS];
84 static hammer2_off_t BootAreaSize;
85 static hammer2_off_t AuxAreaSize;
86 static int NLabels;
87
88 #define GIG     ((hammer2_off_t)1024*1024*1024)
89
90 int
91 main(int ac, char **av)
92 {
93         uint32_t status;
94         hammer2_off_t total_space;
95         hammer2_off_t free_space;
96         hammer2_off_t reserved_space;
97         int ch;
98         int fd = -1;
99         int i;
100         int nolabels = 0;
101         char *vol_fsid;
102         char *sup_clid_name;
103         char *sup_fsid_name;
104         char *pfs_clid_name;
105         char *pfs_fsid_name;
106
107         Label[NLabels++] = "LOCAL";
108
109         /*
110          * Sanity check basic filesystem structures.  No cookies for us
111          * if it gets broken!
112          */
113         assert(sizeof(hammer2_volume_data_t) == HAMMER2_VOLUME_BYTES);
114         assert(sizeof(hammer2_inode_data_t) == HAMMER2_INODE_BYTES);
115         assert(sizeof(hammer2_blockref_t) == HAMMER2_BLOCKREF_BYTES);
116
117         /*
118          * Generate a filesystem id and lookup the filesystem type
119          */
120         srandomdev();
121         uuidgen(&Hammer2_VolFSID, 1);
122         uuidgen(&Hammer2_SupCLID, 1);
123         uuidgen(&Hammer2_SupFSID, 1);
124         uuid_from_string(HAMMER2_UUID_STRING, &Hammer2_FSType, &status);
125         /*uuid_name_lookup(&Hammer2_FSType, "DragonFly HAMMER2", &status);*/
126         if (status != uuid_s_ok) {
127                 errx(1, "uuids file does not have the DragonFly "
128                         "HAMMER2 filesystem type");
129         }
130
131         /*
132          * Parse arguments
133          */
134         while ((ch = getopt(ac, av, "fL:b:m:r:V:")) != -1) {
135                 switch(ch) {
136                 case 'f':
137                         ForceOpt = 1;
138                         break;
139                 case 'L':
140                         if (strcasecmp(optarg, "none") == 0) {
141                                 nolabels = 1;
142                                 break;
143                         }
144                         if (NLabels >= MAXLABELS) {
145                                 errx(1,
146                                      "Limit of %d local labels",
147                                      MAXLABELS - 1);
148                         }
149                         Label[NLabels++] = optarg;
150                         if (strlen(Label[NLabels-1]) > HAMMER2_INODE_MAXNAME) {
151                                 errx(1, "Root directory label too long "
152                                         "(64 chars max)\n");
153                         }
154                         break;
155                 case 'b':
156                         BootAreaSize = getsize(optarg,
157                                          HAMMER2_NEWFS_ALIGN,
158                                          HAMMER2_BOOT_MAX_BYTES, 2);
159                         break;
160                 case 'r':
161                         AuxAreaSize = getsize(optarg,
162                                          HAMMER2_NEWFS_ALIGN,
163                                          HAMMER2_REDO_MAX_BYTES, 2);
164                         break;
165                 case 'V':
166                         Hammer2Version = strtol(optarg, NULL, 0);
167                         if (Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
168                             Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
169                                 errx(1,
170                                      "I don't understand how to format "
171                                      "HAMMER2 version %d\n",
172                                      Hammer2Version);
173                         }
174                         break;
175                 default:
176                         usage();
177                         break;
178                 }
179         }
180
181         /*
182          * Adjust Label[] and NLabels
183          */
184         if (nolabels) {
185                 NLabels = 1;
186         } else if (NLabels == 1) {
187                 Label[NLabels++] = "BOOT";
188                 Label[NLabels++] = "ROOT";
189         }
190
191         /*
192          * Check Hammer2 version
193          */
194         if (Hammer2Version < 0) {
195                 size_t olen = sizeof(Hammer2Version);
196                 Hammer2Version = HAMMER2_VOL_VERSION_DEFAULT;
197                 if (sysctlbyname("vfs.hammer2.supported_version",
198                                  &Hammer2Version, &olen, NULL, 0) == 0) {
199                         if (Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
200                                 Hammer2Version = HAMMER2_VOL_VERSION_WIP - 1;
201                                 fprintf(stderr,
202                                         "newfs_hammer: WARNING: HAMMER2 VFS "
203                                         "supports higher version than I "
204                                         "understand,\n"
205                                         "using version %d\n",
206                                         Hammer2Version);
207                         }
208                 } else {
209                         fprintf(stderr,
210                                 "newfs_hammer: WARNING: HAMMER2 VFS not "
211                                 "loaded, cannot get version info.\n"
212                                 "Using version %d\n",
213                                 HAMMER2_VOL_VERSION_DEFAULT);
214                 }
215         }
216
217         /*
218          * Collect volume information.
219          */
220         ac -= optind;
221         av += optind;
222
223         if (ac != 1) {
224                 fprintf(stderr, "Exactly one disk device must be specified\n");
225                 exit(1);
226         }
227         total_space = check_volume(av[0], &fd);
228
229         /*
230          * ~typically 8MB alignment to avoid edge cases for reserved blocks
231          * and so raid stripes (if any) operate efficiently.
232          */
233         total_space &= ~HAMMER2_VOLUME_ALIGNMASK64;
234
235         /*
236          * Calculate defaults for the boot area size and round to the
237          * volume alignment boundary.
238          */
239         if (BootAreaSize == 0) {
240                 BootAreaSize = HAMMER2_BOOT_NOM_BYTES;
241                 while (BootAreaSize > total_space / 20)
242                         BootAreaSize >>= 1;
243                 if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES)
244                         BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
245         } else if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES) {
246                 BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
247         }
248         BootAreaSize = (BootAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
249                        ~HAMMER2_VOLUME_ALIGNMASK64;
250
251         /*
252          * Calculate defaults for the redo area size and round to the
253          * volume alignment boundary.
254          */
255         if (AuxAreaSize == 0) {
256                 AuxAreaSize = HAMMER2_REDO_NOM_BYTES;
257                 while (AuxAreaSize > total_space / 20)
258                         AuxAreaSize >>= 1;
259                 if (AuxAreaSize < HAMMER2_REDO_MIN_BYTES)
260                         AuxAreaSize = HAMMER2_REDO_MIN_BYTES;
261         } else if (AuxAreaSize < HAMMER2_REDO_MIN_BYTES) {
262                 AuxAreaSize = HAMMER2_REDO_MIN_BYTES;
263         }
264         AuxAreaSize = (AuxAreaSize + HAMMER2_VOLUME_ALIGNMASK64) &
265                        ~HAMMER2_VOLUME_ALIGNMASK64;
266
267         /*
268          * We'll need to stuff this in the volume header soon.
269          */
270         uuid_to_string(&Hammer2_VolFSID, &vol_fsid, &status);
271         uuid_to_string(&Hammer2_SupCLID, &sup_clid_name, &status);
272         uuid_to_string(&Hammer2_SupFSID, &sup_fsid_name, &status);
273
274         /*
275          * Calculate the amount of reserved space.  HAMMER2_ZONE_SEG (4MB)
276          * is reserved at the beginning of every 2GB of storage, rounded up.
277          * Thus a 200MB filesystem will still have a 4MB reserve area.
278          *
279          * We also include the boot and redo areas in the reserve.  The
280          * reserve is used to help 'df' calculate the amount of available
281          * space.
282          */
283         reserved_space = ((total_space + HAMMER2_ZONE_MASK64) /
284                           HAMMER2_ZONE_BYTES64) * HAMMER2_ZONE_SEG64;
285
286         free_space = total_space - reserved_space -
287                      BootAreaSize - AuxAreaSize;
288
289         format_hammer2(fd, total_space, free_space);
290         fsync(fd);
291         close(fd);
292
293         printf("---------------------------------------------\n");
294         printf("version:          %d\n", Hammer2Version);
295         printf("total-size:       %s (%jd bytes)\n",
296                sizetostr(total_space),
297                (intmax_t)total_space);
298         printf("boot-area-size:   %s\n", sizetostr(BootAreaSize));
299         printf("aux-area-size:    %s\n", sizetostr(AuxAreaSize));
300         printf("topo-reserved:    %s\n", sizetostr(reserved_space));
301         printf("free-space:       %s\n", sizetostr(free_space));
302         printf("vol-fsid:         %s\n", vol_fsid);
303         printf("sup-clid:         %s\n", sup_clid_name);
304         printf("sup-fsid:         %s\n", sup_fsid_name);
305         for (i = 0; i < NLabels; ++i) {
306                 printf("PFS \"%s\"\n", Label[i]);
307                 uuid_to_string(&Hammer2_PfsCLID[i], &pfs_clid_name, &status);
308                 uuid_to_string(&Hammer2_PfsFSID[i], &pfs_fsid_name, &status);
309                 printf("    clid %s\n", pfs_clid_name);
310                 printf("    fsid %s\n", pfs_fsid_name);
311         }
312         printf("\n");
313
314         return(0);
315 }
316
317 static
318 void
319 usage(void)
320 {
321         fprintf(stderr,
322                 "usage: newfs_hammer -L label [-f] [-b bootsize] "
323                 "[-r redosize] [-V version] special ...\n"
324         );
325         exit(1);
326 }
327
328 /*
329  * Convert the size in bytes to a human readable string.
330  */
331 static
332 const char *
333 sizetostr(hammer2_off_t size)
334 {
335         static char buf[32];
336
337         if (size < 1024 / 2) {
338                 snprintf(buf, sizeof(buf), "%6.2f", (double)size);
339         } else if (size < 1024 * 1024 / 2) {
340                 snprintf(buf, sizeof(buf), "%6.2fKB",
341                         (double)size / 1024);
342         } else if (size < 1024 * 1024 * 1024LL / 2) {
343                 snprintf(buf, sizeof(buf), "%6.2fMB",
344                         (double)size / (1024 * 1024));
345         } else if (size < 1024 * 1024 * 1024LL * 1024LL / 2) {
346                 snprintf(buf, sizeof(buf), "%6.2fGB",
347                         (double)size / (1024 * 1024 * 1024LL));
348         } else {
349                 snprintf(buf, sizeof(buf), "%6.2fTB",
350                         (double)size / (1024 * 1024 * 1024LL * 1024LL));
351         }
352         return(buf);
353 }
354
355 /*
356  * Convert a string to a 64 bit signed integer with various requirements.
357  */
358 static int64_t
359 getsize(const char *str, int64_t minval, int64_t maxval, int powerof2)
360 {
361         int64_t val;
362         char *ptr;
363
364         val = strtoll(str, &ptr, 0);
365         switch(*ptr) {
366         case 't':
367         case 'T':
368                 val *= 1024;
369                 /* fall through */
370         case 'g':
371         case 'G':
372                 val *= 1024;
373                 /* fall through */
374         case 'm':
375         case 'M':
376                 val *= 1024;
377                 /* fall through */
378         case 'k':
379         case 'K':
380                 val *= 1024;
381                 break;
382         default:
383                 errx(1, "Unknown suffix in number '%s'\n", str);
384                 /* not reached */
385         }
386         if (ptr[1]) {
387                 errx(1, "Unknown suffix in number '%s'\n", str);
388                 /* not reached */
389         }
390         if (val < minval) {
391                 errx(1, "Value too small: %s, min is %s\n",
392                      str, sizetostr(minval));
393                 /* not reached */
394         }
395         if (val > maxval) {
396                 errx(1, "Value too large: %s, max is %s\n",
397                      str, sizetostr(maxval));
398                 /* not reached */
399         }
400         if ((powerof2 & 1) && (val ^ (val - 1)) != ((val << 1) - 1)) {
401                 errx(1, "Value not power of 2: %s\n", str);
402                 /* not reached */
403         }
404         if ((powerof2 & 2) && (val & HAMMER2_NEWFS_ALIGNMASK)) {
405                 errx(1, "Value not an integral multiple of %dK: %s",
406                      HAMMER2_NEWFS_ALIGN / 1024, str);
407                 /* not reached */
408         }
409         return(val);
410 }
411
412 static uint64_t
413 nowtime(void)
414 {
415         struct timeval tv;
416         uint64_t xtime;
417
418         gettimeofday(&tv, NULL);
419         xtime = tv.tv_sec * 1000000LL + tv.tv_usec;
420         return(xtime);
421 }
422
423 /*
424  * Figure out how big the volume is.
425  */
426 static
427 hammer2_off_t
428 check_volume(const char *path, int *fdp)
429 {
430         struct partinfo pinfo;
431         struct stat st;
432         hammer2_off_t size;
433
434         /*
435          * Get basic information about the volume
436          */
437         *fdp = open(path, O_RDWR);
438         if (*fdp < 0)
439                 err(1, "Unable to open %s R+W", path);
440         if (ioctl(*fdp, DIOCGPART, &pinfo) < 0) {
441                 /*
442                  * Allow the formatting of regular files as HAMMER2 volumes
443                  */
444                 if (fstat(*fdp, &st) < 0)
445                         err(1, "Unable to stat %s", path);
446                 size = st.st_size;
447         } else {
448                 /*
449                  * When formatting a block device as a HAMMER2 volume the
450                  * sector size must be compatible.  HAMMER2 uses 64K
451                  * filesystem buffers but logical buffers for direct I/O
452                  * can be as small as HAMMER2_LOGSIZE (16KB).
453                  */
454                 if (pinfo.reserved_blocks) {
455                         errx(1, "HAMMER cannot be placed in a partition "
456                                 "which overlaps the disklabel or MBR");
457                 }
458                 if (pinfo.media_blksize > HAMMER2_PBUFSIZE ||
459                     HAMMER2_PBUFSIZE % pinfo.media_blksize) {
460                         errx(1, "A media sector size of %d is not supported",
461                              pinfo.media_blksize);
462                 }
463                 size = pinfo.media_size;
464         }
465         printf("Volume %-15s size %s\n", path, sizetostr(size));
466         return (size);
467 }
468
469 /*
470  * Create the volume header, the super-root directory inode, and
471  * the writable snapshot subdirectory (named via the label) which
472  * is to be the initial mount point, or at least the first mount point.
473  *
474  * [----reserved_area----][boot_area][aux_area]
475  * [[vol_hdr]...         ]                      [sroot][root]
476  *
477  * The sroot and root inodes eat 512 bytes each.  newfs labels can only be
478  * 64 bytes so the root (snapshot) inode does not need to extend past 512
479  * bytes.  We use the correct hash slot correct but note that because
480  * directory hashes are chained 16x, any slot in the inode will work.
481  *
482  * Also format the allocation map.
483  *
484  * NOTE: The passed total_space is 8MB-aligned to avoid edge cases.
485  */
486 static
487 void
488 format_hammer2(int fd, hammer2_off_t total_space, hammer2_off_t free_space)
489 {
490         char *buf = malloc(HAMMER2_PBUFSIZE);
491         hammer2_volume_data_t *vol;
492         hammer2_inode_data_t *rawip;
493         hammer2_blockref_t sroot_blockref;
494         hammer2_blockref_t root_blockref[MAXLABELS];
495         uint64_t now;
496         hammer2_off_t volu_base = 0;
497         hammer2_off_t boot_base = HAMMER2_ZONE_SEG;
498         hammer2_off_t aux_base = boot_base + BootAreaSize;
499         hammer2_off_t alloc_base = aux_base + AuxAreaSize;
500         hammer2_off_t tmp_base;
501         size_t n;
502         int i;
503
504         /*
505          * Clear the entire reserve for the first 2G segment and
506          * make sure we can write to the last block.
507          */
508         bzero(buf, HAMMER2_PBUFSIZE);
509         tmp_base = volu_base;
510         for (i = 0; i < HAMMER2_ZONE_BLOCKS_SEG; ++i) {
511                 n = pwrite(fd, buf, HAMMER2_PBUFSIZE, tmp_base);
512                 if (n != HAMMER2_PBUFSIZE) {
513                         perror("write");
514                         exit(1);
515                 }
516                 tmp_base += HAMMER2_PBUFSIZE;
517         }
518
519         n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
520                    volu_base + total_space - HAMMER2_PBUFSIZE);
521         if (n != HAMMER2_PBUFSIZE) {
522                 perror("write (at-end-of-volume)");
523                 exit(1);
524         }
525
526         /*
527          * Make sure alloc_base won't cross the reserved area at the
528          * beginning of each 2GB zone.
529          *
530          * Reserve space for the super-root inode and the root inode.
531          * Make sure they are in the same 64K block to simplify our code.
532          */
533         assert((alloc_base & HAMMER2_PBUFMASK) == 0);
534         assert(alloc_base < HAMMER2_ZONE_BYTES64 - HAMMER2_ZONE_SEG);
535         now = nowtime();
536         bzero(buf, HAMMER2_PBUFSIZE);
537
538         alloc_base &= ~HAMMER2_PBUFMASK64;
539         alloc_direct(&alloc_base, &sroot_blockref, HAMMER2_INODE_BYTES);
540
541         for (i = 0; i < NLabels; ++i) {
542                 uuidgen(&Hammer2_PfsCLID[i], 1);
543                 uuidgen(&Hammer2_PfsFSID[i], 1);
544
545                 alloc_direct(&alloc_base, &root_blockref[i],
546                              HAMMER2_INODE_BYTES);
547                 assert(((sroot_blockref.data_off ^ root_blockref[i].data_off) &
548                         HAMMER2_OFF_MASK_HI) == 0);
549
550                 /*
551                  * Format the root directory inode, which is left empty.
552                  */
553                 rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO &
554                                         root_blockref[i].data_off));
555                 rawip->version = HAMMER2_INODE_VERSION_ONE;
556                 rawip->ctime = now;
557                 rawip->mtime = now;
558                 /* rawip->atime = now; NOT IMPL MUST BE ZERO */
559                 rawip->btime = now;
560                 rawip->type = HAMMER2_OBJTYPE_DIRECTORY;
561                 rawip->mode = 0755;
562                 rawip->inum = 1;        /* root inode, inumber 1 */
563                 rawip->nlinks = 1;      /* directory link count compat */
564
565                 rawip->name_len = strlen(Label[i]);
566                 bcopy(Label[i], rawip->filename, rawip->name_len);
567                 rawip->name_key = dirhash(rawip->filename, rawip->name_len);
568
569                 /*
570                  * Compression mode and supported copyids.
571                  *
572                  * Do not allow compression when creating any "BOOT" label
573                  * (pfs-create also does the same if the pfs is named "BOOT")
574                  */
575                 if (strcasecmp(Label[i], "BOOT") == 0) {
576                         rawip->comp_algo = HAMMER2_ENC_ALGO(
577                                                 HAMMER2_COMP_AUTOZERO);
578                         rawip->check_algo = HAMMER2_ENC_ALGO(
579                                                 HAMMER2_CHECK_ISCSI32);
580                 } else  {
581                         rawip->comp_algo = HAMMER2_ENC_ALGO(
582                                                 HAMMER2_COMP_NEWFS_DEFAULT);
583                         rawip->check_algo = HAMMER2_ENC_ALGO(
584                                                 HAMMER2_CHECK_ISCSI32);
585                 }
586
587                 /*
588                  * NOTE: We leave nmasters set to 0, which means that we
589                  *       don't know how many masters there are.  The quorum
590                  *       calculation will effectively be 1 ( 0 / 2 + 1 ).
591                  */
592                 rawip->pfs_clid = Hammer2_PfsCLID[i];
593                 rawip->pfs_fsid = Hammer2_PfsFSID[i];
594                 rawip->pfs_type = HAMMER2_PFSTYPE_MASTER;
595                 rawip->op_flags |= HAMMER2_OPFLAG_PFSROOT;
596                 rawip->pfs_inum = 16;   /* first allocatable inode number */
597
598                 /* rawip->u.blockset is left empty */
599
600                 /*
601                  * The root blockref will be stored in the super-root inode as
602                  * the only directory entry.  The copyid here is the actual
603                  * copyid of the storage ref.
604                  *
605                  * The key field for a directory entry's blockref is
606                  * essentially the name key for the entry.
607                  */
608                 root_blockref[i].key = rawip->name_key;
609                 root_blockref[i].copyid = HAMMER2_COPYID_LOCAL;
610                 root_blockref[i].keybits = 0;
611                 root_blockref[i].check.iscsi32.value =
612                                 hammer2_icrc32(rawip, sizeof(*rawip));
613                 root_blockref[i].type = HAMMER2_BREF_TYPE_INODE;
614                 root_blockref[i].methods =
615                                 HAMMER2_ENC_CHECK(HAMMER2_CHECK_ISCSI32) |
616                                 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
617                 root_blockref[i].mirror_tid = 16;
618                 root_blockref[i].flags = HAMMER2_BREF_FLAG_PFSROOT;
619         }
620
621         /*
622          * Format the super-root directory inode, giving it one directory
623          * entry (root_blockref) and fixup the icrc method.
624          *
625          * The superroot contains one directory entry pointing at the root
626          * inode (named via the label).  Inodes contain one blockset which
627          * is fully associative so we can put the entry anywhere without
628          * having to worry about the hash.  Use index 0.
629          */
630         rawip = (void *)(buf + (HAMMER2_OFF_MASK_LO & sroot_blockref.data_off));
631         rawip->version = HAMMER2_INODE_VERSION_ONE;
632         rawip->ctime = now;
633         rawip->mtime = now;
634         /* rawip->atime = now; NOT IMPL MUST BE ZERO */
635         rawip->btime = now;
636         rawip->type = HAMMER2_OBJTYPE_DIRECTORY;
637         rawip->mode = 0700;             /* super-root - root only */
638         rawip->inum = 0;                /* super root inode, inumber 0 */
639         rawip->nlinks = 2;              /* directory link count compat */
640
641         rawip->name_len = 0;            /* super-root is unnamed */
642         rawip->name_key = 0;
643
644         rawip->comp_algo = HAMMER2_ENC_ALGO(HAMMER2_COMP_AUTOZERO);
645         rawip->check_algo = HAMMER2_ENC_ALGO(HAMMER2_CHECK_ISCSI32);
646
647         /*
648          * The super-root is flagged as a PFS and typically given its own
649          * random FSID, making it possible to mirror an entire HAMMER2 disk
650          * snapshots and all if desired.  PFS ids are used to match up
651          * mirror sources and targets and cluster copy sources and targets.
652          *
653          * (XXX whole-disk logical mirroring is not really supported in
654          *  the first attempt because each PFS is in its own modify/mirror
655          *  transaction id domain, so normal mechanics cannot cross a PFS
656          *  boundary).
657          */
658         rawip->pfs_clid = Hammer2_SupCLID;
659         rawip->pfs_fsid = Hammer2_SupFSID;
660         rawip->pfs_type = HAMMER2_PFSTYPE_SUPROOT;
661         rawip->pfs_inum = 16;   /* first allocatable inode number */
662
663         /*
664          * Currently newfs_hammer2 just throws the PFS inodes into the
665          * top-level block table at the volume root and doesn't try to
666          * create an indirect block, so we are limited to ~4 at filesystem
667          * creation time.  More can be added after mounting.
668          */
669         qsort(root_blockref, NLabels, sizeof(root_blockref[0]), blkrefary_cmp);
670         for (i = 0; i < NLabels; ++i)
671                 rawip->u.blockset.blockref[i] = root_blockref[i];
672
673         /*
674          * The sroot blockref will be stored in the volume header.
675          */
676         sroot_blockref.copyid = HAMMER2_COPYID_LOCAL;
677         sroot_blockref.keybits = 0;
678         sroot_blockref.check.iscsi32.value =
679                                         hammer2_icrc32(rawip, sizeof(*rawip));
680         sroot_blockref.type = HAMMER2_BREF_TYPE_INODE;
681         sroot_blockref.methods = HAMMER2_ENC_CHECK(HAMMER2_CHECK_ISCSI32) |
682                                  HAMMER2_ENC_COMP(HAMMER2_COMP_AUTOZERO);
683         sroot_blockref.mirror_tid = 16;
684         rawip = NULL;
685
686         /*
687          * Write out the 64K HAMMER2 block containing the root and sroot.
688          */
689         n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
690                    sroot_blockref.data_off & HAMMER2_OFF_MASK_HI);
691         if (n != HAMMER2_PBUFSIZE) {
692                 perror("write");
693                 exit(1);
694         }
695
696         /*
697          * Format the volume header.
698          *
699          * The volume header points to sroot_blockref.  Also be absolutely
700          * sure that allocator_beg is set.
701          */
702         bzero(buf, HAMMER2_PBUFSIZE);
703         vol = (void *)buf;
704
705         vol->magic = HAMMER2_VOLUME_ID_HBO;
706         vol->boot_beg = boot_base;
707         vol->boot_end = boot_base + BootAreaSize;
708         vol->aux_beg = aux_base;
709         vol->aux_end = aux_base + AuxAreaSize;
710         vol->volu_size = total_space;
711         vol->version = Hammer2Version;
712         vol->flags = 0;
713
714         vol->fsid = Hammer2_VolFSID;
715         vol->fstype = Hammer2_FSType;
716
717         vol->peer_type = DMSG_PEER_HAMMER2;     /* LNK_CONN identification */
718
719         vol->allocator_size = free_space;
720         vol->allocator_free = free_space;
721         vol->allocator_beg = alloc_base;
722
723         vol->sroot_blockset.blockref[0] = sroot_blockref;
724         vol->mirror_tid = 16;   /* all blockref mirror TIDs set to 16 */
725         vol->freemap_tid = 16;  /* all blockref mirror TIDs set to 16 */
726         vol->icrc_sects[HAMMER2_VOL_ICRC_SECT1] =
727                         hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC1_OFF,
728                                        HAMMER2_VOLUME_ICRC1_SIZE);
729
730         /*
731          * Set ICRC_SECT0 after all remaining elements of sect0 have been
732          * populated in the volume header.  Note hat ICRC_SECT* (except for
733          * SECT0) are part of sect0.
734          */
735         vol->icrc_sects[HAMMER2_VOL_ICRC_SECT0] =
736                         hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRC0_OFF,
737                                        HAMMER2_VOLUME_ICRC0_SIZE);
738         vol->icrc_volheader =
739                         hammer2_icrc32((char *)vol + HAMMER2_VOLUME_ICRCVH_OFF,
740                                        HAMMER2_VOLUME_ICRCVH_SIZE);
741
742         /*
743          * Write the volume header and all alternates.
744          */
745         for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
746                 if (i * HAMMER2_ZONE_BYTES64 >= total_space)
747                         break;
748                 n = pwrite(fd, buf, HAMMER2_PBUFSIZE,
749                            volu_base + i * HAMMER2_ZONE_BYTES64);
750                 if (n != HAMMER2_PBUFSIZE) {
751                         perror("write");
752                         exit(1);
753                 }
754         }
755
756         /*
757          * Cleanup
758          */
759         free(buf);
760 }
761
762 static void
763 alloc_direct(hammer2_off_t *basep, hammer2_blockref_t *bref, size_t bytes)
764 {
765         int radix;
766
767         radix = 0;
768         assert(bytes);
769         while ((bytes & 1) == 0) {
770                 bytes >>= 1;
771                 ++radix;
772         }
773         assert(bytes == 1);
774         if (radix < HAMMER2_RADIX_MIN)
775                 radix = HAMMER2_RADIX_MIN;
776
777         bzero(bref, sizeof(*bref));
778         bref->data_off = *basep | radix;
779         bref->vradix = radix;
780
781         *basep += 1U << radix;
782 }
783
784 /*
785  * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
786  * The filename is split into fields which are hashed separately and then
787  * added together.
788  *
789  * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
790  * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
791  * (This means we do not need to do a 0-check/or-with-0x100000000 either).
792  *
793  * Also, the iscsi crc code is used instead of the old crc32 code.
794  */
795 static hammer2_key_t
796 dirhash(const unsigned char *name, size_t len)
797 {
798         const unsigned char *aname = name;
799         uint32_t crcx;
800         uint64_t key;
801         size_t i;
802         size_t j;
803
804         /*
805          * Filesystem version 6 or better will create directories
806          * using the ALG1 dirhash.  This hash breaks the filename
807          * up into domains separated by special characters and
808          * hashes each domain independently.
809          *
810          * We also do a simple sub-sort using the first character
811          * of the filename in the top 5-bits.
812          */
813         key = 0;
814
815         /*
816          * m32
817          */
818         crcx = 0;
819         for (i = j = 0; i < len; ++i) {
820                 if (aname[i] == '.' ||
821                     aname[i] == '-' ||
822                     aname[i] == '_' ||
823                     aname[i] == '~') {
824                         if (i != j)
825                                 crcx += hammer2_icrc32(aname + j, i - j);
826                         j = i + 1;
827                 }
828         }
829         if (i != j)
830                 crcx += hammer2_icrc32(aname + j, i - j);
831
832         /*
833          * The directory hash utilizes the top 32 bits of the 64-bit key.
834          * Bit 63 must be set to 1.
835          */
836         crcx |= 0x80000000U;
837         key |= (uint64_t)crcx << 32;
838
839         /*
840          * l16 - crc of entire filename
841          *
842          * This crc reduces degenerate hash collision conditions
843          */
844         crcx = hammer2_icrc32(aname, len);
845         crcx = crcx ^ (crcx << 16);
846         key |= crcx & 0xFFFF0000U;
847
848         /*
849          * Set bit 15.  This allows readdir to strip bit 63 so a positive
850          * 64-bit cookie/offset can always be returned, and still guarantee
851          * that the values 0x0000-0x7FFF are available for artificial entries.
852          * ('.' and '..').
853          */
854         key |= 0x8000U;
855
856         return (key);
857 }
858
859 static int
860 blkrefary_cmp(const void *b1, const void *b2)
861 {
862         const hammer2_blockref_t *bref1 = b1;
863         const hammer2_blockref_t *bref2 = b2;
864         if (bref1->key < bref2->key)
865                 return(-1);
866         if (bref1->key > bref2->key)
867                 return(1);
868         return 0;
869 }