Home | History | Annotate | Line # | Download | only in newfs_ext2fs
      1  1.28    andvar /*	$NetBSD: mke2fs.c,v 1.28 2025/01/05 22:11:18 andvar Exp $	*/
      2   1.1   tsutsui 
      3   1.8   tsutsui /*-
      4   1.8   tsutsui  * Copyright (c) 2007 Izumi Tsutsui.  All rights reserved.
      5   1.1   tsutsui  *
      6   1.1   tsutsui  * Redistribution and use in source and binary forms, with or without
      7   1.1   tsutsui  * modification, are permitted provided that the following conditions
      8   1.1   tsutsui  * are met:
      9   1.1   tsutsui  * 1. Redistributions of source code must retain the above copyright
     10   1.1   tsutsui  *    notice, this list of conditions and the following disclaimer.
     11   1.1   tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1   tsutsui  *    notice, this list of conditions and the following disclaimer in the
     13   1.1   tsutsui  *    documentation and/or other materials provided with the distribution.
     14   1.1   tsutsui  *
     15   1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1   tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1   tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1   tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1   tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.1   tsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.1   tsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.1   tsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.1   tsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.1   tsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25   1.1   tsutsui  */
     26   1.1   tsutsui 
     27   1.1   tsutsui /*
     28   1.1   tsutsui  * Copyright (c) 1980, 1989, 1993
     29   1.1   tsutsui  *	The Regents of the University of California.  All rights reserved.
     30   1.1   tsutsui  *
     31   1.1   tsutsui  * Redistribution and use in source and binary forms, with or without
     32   1.1   tsutsui  * modification, are permitted provided that the following conditions
     33   1.1   tsutsui  * are met:
     34   1.1   tsutsui  * 1. Redistributions of source code must retain the above copyright
     35   1.1   tsutsui  *    notice, this list of conditions and the following disclaimer.
     36   1.1   tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     37   1.1   tsutsui  *    notice, this list of conditions and the following disclaimer in the
     38   1.1   tsutsui  *    documentation and/or other materials provided with the distribution.
     39   1.1   tsutsui  * 3. Neither the name of the University nor the names of its contributors
     40   1.1   tsutsui  *    may be used to endorse or promote products derived from this software
     41   1.1   tsutsui  *    without specific prior written permission.
     42   1.1   tsutsui  *
     43   1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     44   1.1   tsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     45   1.1   tsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     46   1.1   tsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     47   1.1   tsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     48   1.1   tsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     49   1.1   tsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     50   1.1   tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     51   1.1   tsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     52   1.1   tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     53   1.1   tsutsui  * SUCH DAMAGE.
     54   1.1   tsutsui  */
     55   1.1   tsutsui 
     56   1.1   tsutsui /*
     57   1.1   tsutsui  * Copyright (c) 1997 Manuel Bouyer.
     58   1.1   tsutsui  *
     59   1.1   tsutsui  * Redistribution and use in source and binary forms, with or without
     60   1.1   tsutsui  * modification, are permitted provided that the following conditions
     61   1.1   tsutsui  * are met:
     62   1.1   tsutsui  * 1. Redistributions of source code must retain the above copyright
     63   1.1   tsutsui  *	notice, this list of conditions and the following disclaimer.
     64   1.1   tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     65   1.1   tsutsui  *	notice, this list of conditions and the following disclaimer in the
     66   1.1   tsutsui  *	documentation and/or other materials provided with the distribution.
     67   1.1   tsutsui  *
     68   1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     69   1.1   tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     70   1.1   tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     71   1.1   tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     72   1.1   tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     73   1.1   tsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     74   1.1   tsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     75   1.1   tsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     76   1.1   tsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     77   1.1   tsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     78   1.1   tsutsui  */
     79   1.1   tsutsui 
     80   1.1   tsutsui /*
     81   1.1   tsutsui  * mke2fs.c: "re-invent (dumb but non-GPLed) wheel as a fun project"
     82   1.1   tsutsui  *
     83   1.1   tsutsui  *	In spite of this name, there is no piece of code
     84   1.1   tsutsui  *	derived from GPLed e2fsprogs written for Linux.
     85   1.1   tsutsui  *	I referred them only to see how each structure
     86   1.1   tsutsui  *	member should be initialized.
     87   1.1   tsutsui  *
     88   1.1   tsutsui  * Reference:
     89   1.1   tsutsui  *	- All NetBSD sources under src/sys/ufs/ext2fs and src/sbin/fsck_ext2fs
     90   1.1   tsutsui  *	- Ext2fs Home Page
     91  1.26   tsutsui  *		https://e2fsprogs.sourceforge.net/ext2.html
     92   1.1   tsutsui  *	- Design and Implementation of the Second Extended Filesystem
     93  1.26   tsutsui  *		https://e2fsprogs.sourceforge.net/ext2intro.html
     94   1.1   tsutsui  *	- Linux Documentation "The Second Extended Filesystem"
     95  1.26   tsutsui  *		https://www.kernel.org/doc/Documentation/filesystems/ext2.txt
     96   1.1   tsutsui  */
     97   1.1   tsutsui 
     98   1.1   tsutsui #include <sys/cdefs.h>
     99   1.1   tsutsui #ifndef lint
    100   1.1   tsutsui #if 0
    101   1.1   tsutsui static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
    102   1.1   tsutsui #else
    103  1.28    andvar __RCSID("$NetBSD: mke2fs.c,v 1.28 2025/01/05 22:11:18 andvar Exp $");
    104   1.1   tsutsui #endif
    105   1.1   tsutsui #endif /* not lint */
    106   1.1   tsutsui 
    107   1.1   tsutsui #include <sys/param.h>
    108   1.1   tsutsui #include <sys/mman.h>
    109   1.1   tsutsui #include <sys/time.h>
    110   1.1   tsutsui #include <ufs/ext2fs/ext2fs_dinode.h>
    111   1.1   tsutsui #include <ufs/ext2fs/ext2fs_dir.h>
    112   1.1   tsutsui #include <ufs/ext2fs/ext2fs.h>
    113   1.1   tsutsui #include <sys/ioctl.h>
    114   1.1   tsutsui 
    115   1.1   tsutsui #include <err.h>
    116   1.1   tsutsui #include <errno.h>
    117   1.1   tsutsui #include <string.h>
    118   1.1   tsutsui #include <unistd.h>
    119   1.1   tsutsui #include <stdlib.h>
    120   1.1   tsutsui #include <stddef.h>
    121   1.1   tsutsui #include <stdio.h>
    122   1.1   tsutsui #include <uuid.h>
    123   1.1   tsutsui 
    124   1.1   tsutsui #include "extern.h"
    125   1.1   tsutsui 
    126   1.1   tsutsui static void initcg(uint);
    127  1.15   tsutsui static void zap_old_sblock(int);
    128   1.1   tsutsui static uint cgoverhead(uint);
    129   1.1   tsutsui static int fsinit(const struct timeval *);
    130   1.1   tsutsui static int makedir(struct ext2fs_direct *, int);
    131   1.1   tsutsui static void copy_dir(struct ext2fs_direct *, struct ext2fs_direct *);
    132   1.1   tsutsui static void init_resizeino(const struct timeval *);
    133   1.1   tsutsui static uint32_t alloc(uint32_t, uint16_t);
    134   1.1   tsutsui static void iput(struct ext2fs_dinode *, ino_t);
    135   1.1   tsutsui static void rdfs(daddr_t, int, void *);
    136   1.1   tsutsui static void wtfs(daddr_t, int, void *);
    137   1.1   tsutsui static int ilog2(uint);
    138   1.1   tsutsui static int skpc(int, size_t, uint8_t *);
    139   1.1   tsutsui 
    140   1.1   tsutsui /* XXX: some of these macro should be into <ufs/ext2fs/ext2fs.h>? */
    141   1.1   tsutsui #define EXT2_DEF_MAX_MNT_COUNT	20
    142   1.1   tsutsui #define EXT2_DEF_FSCKINTV	(180 * 24 * 60 * 60)	/* 180 days */
    143   1.1   tsutsui #define EXT2_RESERVED_INODES	(EXT2_FIRSTINO - 1)
    144   1.1   tsutsui #define EXT2_UMASK		0755
    145   1.1   tsutsui 
    146   1.1   tsutsui #define EXT2_INO_INDEX(ino)	((ino) - 1)		/* no inode zero */
    147   1.1   tsutsui 
    148   1.1   tsutsui #define EXT2_LOSTFOUNDSIZE	16384
    149   1.1   tsutsui #define EXT2_LOSTFOUNDINO	EXT2_FIRSTINO		/* XXX: not quite */
    150   1.1   tsutsui #define EXT2_LOSTFOUNDUMASK	0700
    151   1.1   tsutsui 
    152   1.2   tsutsui #define EXT2_RESIZEINOUMASK	0600
    153   1.2   tsutsui 
    154   1.1   tsutsui #define NBLOCK_SUPERBLOCK	1
    155   1.1   tsutsui #define NBLOCK_BLOCK_BITMAP	1
    156   1.1   tsutsui #define NBLOCK_INODE_BITMAP	1
    157   1.1   tsutsui 
    158   1.1   tsutsui #define cgbase(fs, c)	\
    159   1.1   tsutsui 	((fs)->e2fs.e2fs_first_dblock + (fs)->e2fs.e2fs_bpg * (c))
    160   1.1   tsutsui 
    161   1.1   tsutsui 
    162   1.1   tsutsui /*
    163   1.1   tsutsui  * ext2fs super block and group descriptor structures
    164   1.1   tsutsui  *
    165   1.1   tsutsui  *   We don't have to use or setup whole in-memory m_ext2fs structure,
    166   1.1   tsutsui  *   but prepare it to use several macro defined in kernel headers.
    167   1.1   tsutsui  */
    168   1.1   tsutsui union {
    169   1.1   tsutsui 	struct m_ext2fs m_ext2fs;
    170   1.1   tsutsui 	char pad[SBSIZE];
    171   1.1   tsutsui } ext2fsun;
    172   1.1   tsutsui #define sblock	ext2fsun.m_ext2fs
    173   1.1   tsutsui #define gd	ext2fsun.m_ext2fs.e2fs_gd
    174   1.1   tsutsui 
    175   1.1   tsutsui static uint8_t *iobuf;		/* for superblock and group descriptors */
    176   1.1   tsutsui static int iobufsize;
    177   1.1   tsutsui 
    178   1.1   tsutsui static uint8_t buf[MAXBSIZE];	/* for initcg() and makedir() ops */
    179   1.1   tsutsui 
    180   1.1   tsutsui static int fsi, fso;
    181   1.1   tsutsui 
    182   1.1   tsutsui void
    183   1.1   tsutsui mke2fs(const char *fsys, int fi, int fo)
    184   1.1   tsutsui {
    185   1.1   tsutsui 	struct timeval tv;
    186   1.1   tsutsui 	int64_t minfssize;
    187   1.1   tsutsui 	uint bcount, fbcount, ficount;
    188   1.1   tsutsui 	uint blocks_gd, blocks_per_cg, inodes_per_cg, iblocks_per_cg;
    189   1.1   tsutsui 	uint minblocks_per_cg, blocks_lastcg;
    190   1.1   tsutsui 	uint ncg, cylno, sboff;
    191   1.1   tsutsui 	uuid_t uuid;
    192   1.1   tsutsui 	uint32_t uustat;
    193   1.1   tsutsui 	int i, len, col, delta, fld_width, max_cols;
    194   1.1   tsutsui 	struct winsize winsize;
    195   1.1   tsutsui 
    196   1.1   tsutsui 	gettimeofday(&tv, NULL);
    197   1.1   tsutsui 	fsi = fi;
    198   1.1   tsutsui 	fso = fo;
    199   1.1   tsutsui 
    200   1.1   tsutsui 	/*
    201   1.1   tsutsui 	 * collect and verify the block and fragment sizes
    202   1.1   tsutsui 	 */
    203   1.1   tsutsui 	if (!powerof2(bsize)) {
    204   1.1   tsutsui 		errx(EXIT_FAILURE,
    205  1.22  christos 		    "block size must be a power of 2, not %u",
    206   1.1   tsutsui 		    bsize);
    207   1.1   tsutsui 	}
    208   1.1   tsutsui 	if (!powerof2(fsize)) {
    209   1.1   tsutsui 		errx(EXIT_FAILURE,
    210  1.22  christos 		    "fragment size must be a power of 2, not %u",
    211   1.1   tsutsui 		    fsize);
    212   1.1   tsutsui 	}
    213   1.1   tsutsui 	if (fsize < sectorsize) {
    214   1.1   tsutsui 		errx(EXIT_FAILURE,
    215  1.22  christos 		    "fragment size %u is too small, minimum is %u",
    216   1.1   tsutsui 		    fsize, sectorsize);
    217   1.1   tsutsui 	}
    218   1.1   tsutsui 	if (bsize < MINBSIZE) {
    219   1.1   tsutsui 		errx(EXIT_FAILURE,
    220  1.22  christos 		    "block size %u is too small, minimum is %u",
    221   1.1   tsutsui 		    bsize, MINBSIZE);
    222   1.1   tsutsui 	}
    223   1.7   tsutsui 	if (bsize > EXT2_MAXBSIZE) {
    224   1.1   tsutsui 		errx(EXIT_FAILURE,
    225  1.22  christos 		    "block size %u is too large, maximum is %u",
    226   1.1   tsutsui 		    bsize, MAXBSIZE);
    227   1.1   tsutsui 	}
    228   1.1   tsutsui 	if (bsize != fsize) {
    229   1.1   tsutsui 		/*
    230   1.1   tsutsui 		 * There is no fragment support on current ext2fs (yet?),
    231   1.1   tsutsui 		 * but some kernel code refers fsize or fpg as bsize or bpg
    232   1.1   tsutsui 		 * and Linux seems to set the same values to them.
    233   1.1   tsutsui 		 */
    234   1.1   tsutsui 		errx(EXIT_FAILURE,
    235   1.9   tsutsui 		    "block size (%u) can't be different from "
    236  1.22  christos 		    "fragment size (%u)",
    237   1.1   tsutsui 		    bsize, fsize);
    238   1.1   tsutsui 	}
    239   1.1   tsutsui 
    240  1.11   tsutsui 	/* variable inodesize is REV1 feature */
    241  1.11   tsutsui 	if (Oflag == 0 && inodesize != EXT2_REV0_DINODE_SIZE) {
    242  1.11   tsutsui 		errx(EXIT_FAILURE, "GOOD_OLD_REV file system format"
    243  1.22  christos 		    " doesn't support %d byte inode", inodesize);
    244  1.11   tsutsui 	}
    245  1.11   tsutsui 
    246   1.1   tsutsui 	sblock.e2fs.e2fs_log_bsize = ilog2(bsize) - LOG_MINBSIZE;
    247   1.1   tsutsui 	/* Umm, why not e2fs_log_fsize? */
    248   1.1   tsutsui 	sblock.e2fs.e2fs_fsize = ilog2(fsize) - LOG_MINBSIZE;
    249   1.1   tsutsui 
    250   1.1   tsutsui 	sblock.e2fs_bsize = bsize;
    251   1.1   tsutsui 	sblock.e2fs_bshift = sblock.e2fs.e2fs_log_bsize + LOG_MINBSIZE;
    252   1.1   tsutsui 	sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
    253   1.1   tsutsui 	sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
    254   1.1   tsutsui 	sblock.e2fs_fsbtodb = ilog2(sblock.e2fs_bsize) - ilog2(sectorsize);
    255  1.10  christos 	sblock.e2fs_ipb = sblock.e2fs_bsize / inodesize;
    256   1.1   tsutsui 
    257   1.1   tsutsui 	/*
    258   1.9   tsutsui 	 * Ext2fs preserves BBSIZE (1024 bytes) space at the top for
    259   1.1   tsutsui 	 * bootloader (though it is not enough at all for our bootloader).
    260   1.1   tsutsui 	 * If bsize == BBSIZE we have to preserve one block.
    261   1.1   tsutsui 	 * If bsize > BBSIZE, the first block already contains BBSIZE space
    262   1.1   tsutsui 	 * before superblock because superblock is allocated at SBOFF and
    263   1.1   tsutsui 	 * bsize is a power of two (i.e. 2048 bytes or more).
    264   1.1   tsutsui 	 */
    265   1.1   tsutsui 	sblock.e2fs.e2fs_first_dblock = (sblock.e2fs_bsize > BBSIZE) ? 0 : 1;
    266  1.20  dholland 	minfssize = EXT2_FSBTODB(&sblock,
    267   1.1   tsutsui 	    sblock.e2fs.e2fs_first_dblock +
    268   1.1   tsutsui 	    NBLOCK_SUPERBLOCK +
    269   1.1   tsutsui 	    1 /* at least one group descriptor */ +
    270   1.1   tsutsui 	    NBLOCK_BLOCK_BITMAP	+
    271   1.1   tsutsui 	    NBLOCK_INODE_BITMAP +
    272   1.1   tsutsui 	    1 /* at least one inode table block */ +
    273   1.1   tsutsui 	    1 /* at least one data block for rootdir */ +
    274   1.1   tsutsui 	    1 /* at least one data block for data */
    275   1.1   tsutsui 	    );			/* XXX and more? */
    276   1.1   tsutsui 
    277   1.1   tsutsui 	if (fssize < minfssize)
    278   1.1   tsutsui 		errx(EXIT_FAILURE, "Filesystem size %" PRId64
    279  1.22  christos 		    " < minimum size of %" PRId64, fssize, minfssize);
    280   1.1   tsutsui 
    281  1.20  dholland 	bcount = EXT2_DBTOFSB(&sblock, fssize);
    282   1.1   tsutsui 
    283   1.1   tsutsui 	/*
    284   1.1   tsutsui 	 * While many people claim that ext2fs is a (bad) clone of ufs/ffs,
    285   1.1   tsutsui 	 * it isn't actual ffs so maybe we should call it "block group"
    286   1.9   tsutsui 	 * as their native name rather than ffs derived "cylinder group."
    287   1.1   tsutsui 	 * But we'll use the latter here since other kernel sources use it.
    288   1.1   tsutsui 	 * (I also agree "cylinder" based allocation is obsolete though)
    289   1.1   tsutsui 	 */
    290   1.1   tsutsui 
    291   1.1   tsutsui 	/* maybe "simple is the best" */
    292   1.1   tsutsui 	blocks_per_cg = sblock.e2fs_bsize * NBBY;
    293   1.1   tsutsui 
    294   1.1   tsutsui 	ncg = howmany(bcount - sblock.e2fs.e2fs_first_dblock, blocks_per_cg);
    295   1.1   tsutsui 	blocks_gd = howmany(sizeof(struct ext2_gd) * ncg, bsize);
    296   1.1   tsutsui 
    297   1.1   tsutsui 	/* check range of inode number */
    298   1.1   tsutsui 	if (num_inodes < EXT2_FIRSTINO)
    299   1.1   tsutsui 		num_inodes = EXT2_FIRSTINO;	/* needs reserved inodes + 1 */
    300   1.1   tsutsui 	if (num_inodes > UINT16_MAX * ncg)
    301   1.1   tsutsui 		num_inodes = UINT16_MAX * ncg;	/* ext2bgd_nifree is uint16_t */
    302   1.1   tsutsui 
    303   1.1   tsutsui 	inodes_per_cg = num_inodes / ncg;
    304  1.10  christos 	iblocks_per_cg = howmany(inodesize * inodes_per_cg, bsize);
    305   1.1   tsutsui 
    306   1.1   tsutsui 	/* Check that the last cylinder group has enough space for inodes */
    307   1.1   tsutsui 	minblocks_per_cg =
    308   1.1   tsutsui 	    NBLOCK_BLOCK_BITMAP +
    309   1.1   tsutsui 	    NBLOCK_INODE_BITMAP +
    310   1.1   tsutsui 	    iblocks_per_cg +
    311   1.1   tsutsui 	    1;	/* at least one data block */
    312   1.1   tsutsui 	if (Oflag == 0 || cg_has_sb(ncg - 1) != 0)
    313   1.1   tsutsui 		minblocks_per_cg += NBLOCK_SUPERBLOCK + blocks_gd;
    314   1.1   tsutsui 
    315   1.1   tsutsui 	blocks_lastcg = bcount - sblock.e2fs.e2fs_first_dblock -
    316   1.1   tsutsui 	    blocks_per_cg * (ncg - 1);
    317   1.1   tsutsui 	if (blocks_lastcg < minblocks_per_cg) {
    318   1.1   tsutsui 		/*
    319   1.1   tsutsui 		 * Since we make all the cylinder groups the same size, the
    320   1.1   tsutsui 		 * last will only be small if there are more than one
    321   1.1   tsutsui 		 * cylinder groups. If the last one is too small to store
    322   1.1   tsutsui 		 * filesystem data, just kill it.
    323   1.1   tsutsui 		 *
    324   1.1   tsutsui 		 * XXX: Does fsck_ext2fs(8) properly handle this case?
    325   1.1   tsutsui 		 */
    326   1.1   tsutsui 		bcount -= blocks_lastcg;
    327   1.1   tsutsui 		ncg--;
    328   1.1   tsutsui 		blocks_lastcg = blocks_per_cg;
    329   1.1   tsutsui 		blocks_gd = howmany(sizeof(struct ext2_gd) * ncg, bsize);
    330   1.1   tsutsui 		inodes_per_cg = num_inodes / ncg;
    331   1.1   tsutsui 	}
    332   1.5   tsutsui 	/* roundup inodes_per_cg to make it use whole inode table blocks */
    333   1.5   tsutsui 	inodes_per_cg = roundup(inodes_per_cg, sblock.e2fs_ipb);
    334   1.1   tsutsui 	num_inodes = inodes_per_cg * ncg;
    335   1.5   tsutsui 	iblocks_per_cg = inodes_per_cg / sblock.e2fs_ipb;
    336   1.1   tsutsui 
    337   1.1   tsutsui 	/* XXX: probably we should check these adjusted values again */
    338   1.1   tsutsui 
    339   1.1   tsutsui 	sblock.e2fs.e2fs_bcount = bcount;
    340   1.1   tsutsui 	sblock.e2fs.e2fs_icount = num_inodes;
    341   1.1   tsutsui 
    342   1.1   tsutsui 	sblock.e2fs_ncg = ncg;
    343   1.1   tsutsui 	sblock.e2fs_ngdb = blocks_gd;
    344   1.1   tsutsui 	sblock.e2fs_itpg = iblocks_per_cg;
    345   1.1   tsutsui 
    346   1.1   tsutsui 	sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * minfree / 100;
    347   1.1   tsutsui 	/* e2fs_fbcount will be accounted later */
    348   1.1   tsutsui 	/* e2fs_ficount will be accounted later */
    349   1.1   tsutsui 
    350   1.1   tsutsui 	sblock.e2fs.e2fs_bpg = blocks_per_cg;
    351   1.1   tsutsui 	sblock.e2fs.e2fs_fpg = blocks_per_cg;
    352   1.1   tsutsui 
    353   1.1   tsutsui 	sblock.e2fs.e2fs_ipg = inodes_per_cg;
    354   1.1   tsutsui 
    355   1.1   tsutsui 	sblock.e2fs.e2fs_mtime = 0;
    356   1.1   tsutsui 	sblock.e2fs.e2fs_wtime = tv.tv_sec;
    357   1.1   tsutsui 	sblock.e2fs.e2fs_mnt_count = 0;
    358   1.1   tsutsui 	/* XXX: should add some entropy to avoid checking all fs at once? */
    359   1.1   tsutsui 	sblock.e2fs.e2fs_max_mnt_count = EXT2_DEF_MAX_MNT_COUNT;
    360   1.1   tsutsui 
    361   1.1   tsutsui 	sblock.e2fs.e2fs_magic = E2FS_MAGIC;
    362   1.1   tsutsui 	sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
    363   1.1   tsutsui 	sblock.e2fs.e2fs_beh = E2FS_BEH_DEFAULT;
    364   1.1   tsutsui 	sblock.e2fs.e2fs_minrev = 0;
    365   1.1   tsutsui 	sblock.e2fs.e2fs_lastfsck = tv.tv_sec;
    366   1.1   tsutsui 	sblock.e2fs.e2fs_fsckintv = EXT2_DEF_FSCKINTV;
    367   1.1   tsutsui 
    368   1.1   tsutsui 	/*
    369   1.1   tsutsui 	 * Maybe we can use E2FS_OS_FREEBSD here and it would be more proper,
    370   1.1   tsutsui 	 * but the purpose of this newfs_ext2fs(8) command is to provide
    371   1.9   tsutsui 	 * a filesystem which can be recognized by firmware on some
    372   1.1   tsutsui 	 * Linux based appliances that can load bootstrap files only from
    373   1.1   tsutsui 	 * (their native) ext2fs, and anyway we will (and should) try to
    374   1.1   tsutsui 	 * act like them as much as possible.
    375   1.1   tsutsui 	 *
    376   1.1   tsutsui 	 * Anyway, I hope that all newer such boxes will keep their support
    377   1.1   tsutsui 	 * for the "GOOD_OLD_REV" ext2fs.
    378   1.1   tsutsui 	 */
    379   1.1   tsutsui 	sblock.e2fs.e2fs_creator = E2FS_OS_LINUX;
    380   1.1   tsutsui 
    381   1.1   tsutsui 	if (Oflag == 0) {
    382   1.1   tsutsui 		sblock.e2fs.e2fs_rev = E2FS_REV0;
    383   1.1   tsutsui 		sblock.e2fs.e2fs_features_compat   = 0;
    384   1.1   tsutsui 		sblock.e2fs.e2fs_features_incompat = 0;
    385   1.1   tsutsui 		sblock.e2fs.e2fs_features_rocompat = 0;
    386   1.1   tsutsui 	} else {
    387   1.1   tsutsui 		sblock.e2fs.e2fs_rev = E2FS_REV1;
    388   1.1   tsutsui 		/*
    389   1.1   tsutsui 		 * e2fsprogs say "REV1" is "dynamic" so
    390   1.1   tsutsui 		 * it isn't quite a version and maybe it means
    391   1.1   tsutsui 		 * "extended from REV0 so check compat features."
    392   1.1   tsutsui 		 *
    393   1.1   tsutsui 		 * XXX: We don't have any native tool to activate
    394   1.1   tsutsui 		 *      the EXT2F_COMPAT_RESIZE feature and
    395   1.1   tsutsui 		 *      fsck_ext2fs(8) might not fix structures for it.
    396   1.1   tsutsui 		 */
    397   1.1   tsutsui 		sblock.e2fs.e2fs_features_compat   = EXT2F_COMPAT_RESIZE;
    398   1.1   tsutsui 		sblock.e2fs.e2fs_features_incompat = EXT2F_INCOMPAT_FTYPE;
    399   1.1   tsutsui 		sblock.e2fs.e2fs_features_rocompat =
    400   1.1   tsutsui 		    EXT2F_ROCOMPAT_SPARSESUPER | EXT2F_ROCOMPAT_LARGEFILE;
    401   1.1   tsutsui 	}
    402   1.1   tsutsui 
    403   1.1   tsutsui 	sblock.e2fs.e2fs_ruid = geteuid();
    404   1.1   tsutsui 	sblock.e2fs.e2fs_rgid = getegid();
    405   1.1   tsutsui 
    406   1.1   tsutsui 	sblock.e2fs.e2fs_first_ino = EXT2_FIRSTINO;
    407  1.11   tsutsui 	sblock.e2fs.e2fs_inode_size = inodesize;
    408   1.1   tsutsui 
    409   1.1   tsutsui 	/* e2fs_block_group_nr is set on writing superblock to each group */
    410   1.1   tsutsui 
    411   1.1   tsutsui 	uuid_create(&uuid, &uustat);
    412   1.1   tsutsui 	if (uustat != uuid_s_ok)
    413  1.22  christos 		errx(EXIT_FAILURE, "Failed to generate uuid");
    414   1.1   tsutsui 	uuid_enc_le(sblock.e2fs.e2fs_uuid, &uuid);
    415   1.1   tsutsui 	if (volname != NULL) {
    416   1.1   tsutsui 		if (strlen(volname) > sizeof(sblock.e2fs.e2fs_vname))
    417   1.1   tsutsui 			errx(EXIT_FAILURE, "Volume name is too long");
    418   1.1   tsutsui 		strlcpy(sblock.e2fs.e2fs_vname, volname,
    419   1.1   tsutsui 		    sizeof(sblock.e2fs.e2fs_vname));
    420   1.1   tsutsui 	}
    421   1.1   tsutsui 
    422   1.1   tsutsui 	sblock.e2fs.e2fs_fsmnt[0] = '\0';
    423   1.1   tsutsui 	sblock.e2fs_fsmnt[0] = '\0';
    424   1.1   tsutsui 
    425   1.1   tsutsui 	sblock.e2fs.e2fs_algo = 0;		/* XXX unsupported? */
    426   1.1   tsutsui 	sblock.e2fs.e2fs_prealloc = 0;		/* XXX unsupported? */
    427   1.1   tsutsui 	sblock.e2fs.e2fs_dir_prealloc = 0;	/* XXX unsupported? */
    428   1.1   tsutsui 
    429   1.1   tsutsui 	/* calculate blocks for reserved group descriptors for resize */
    430   1.1   tsutsui 	sblock.e2fs.e2fs_reserved_ngdb = 0;
    431   1.2   tsutsui 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    432   1.2   tsutsui 	    (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0) {
    433   1.1   tsutsui 		uint64_t target_blocks;
    434   1.1   tsutsui 		uint target_ncg, target_ngdb, reserved_ngdb;
    435   1.1   tsutsui 
    436   1.1   tsutsui 		/* reserve descriptors for size as 1024 times as current */
    437   1.1   tsutsui 		target_blocks =
    438   1.1   tsutsui 		    (sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock)
    439   1.1   tsutsui 		    * 1024ULL;
    440   1.1   tsutsui 		/* number of blocks must be in uint32_t */
    441   1.1   tsutsui 		if (target_blocks > UINT32_MAX)
    442   1.1   tsutsui 			target_blocks = UINT32_MAX;
    443   1.2   tsutsui 		target_ncg = howmany(target_blocks, sblock.e2fs.e2fs_bpg);
    444   1.1   tsutsui 		target_ngdb = howmany(sizeof(struct ext2_gd) * target_ncg,
    445   1.1   tsutsui 		    sblock.e2fs_bsize);
    446   1.1   tsutsui 		/*
    447   1.1   tsutsui 		 * Reserved group descriptor blocks are preserved as
    448   1.1   tsutsui 		 * the second level double indirect reference blocks in
    449   1.1   tsutsui 		 * the EXT2_RESIZEINO inode, so the maximum number of
    450  1.19  dholland 		 * the blocks is EXT2_NINDIR(fs).
    451   1.1   tsutsui 		 * (see also descriptions in init_resizeino() function)
    452   1.1   tsutsui 		 *
    453   1.1   tsutsui 		 * We check a number including current e2fs_ngdb here
    454   1.1   tsutsui 		 * because they will be moved into reserved gdb on
    455   1.1   tsutsui 		 * possible future size shrink, though e2fsprogs don't
    456   1.1   tsutsui 		 * seem to care about it.
    457   1.1   tsutsui 		 */
    458  1.19  dholland 		if (target_ngdb > EXT2_NINDIR(&sblock))
    459  1.19  dholland 			target_ngdb = EXT2_NINDIR(&sblock);
    460   1.1   tsutsui 
    461   1.1   tsutsui 		reserved_ngdb = target_ngdb - sblock.e2fs_ngdb;
    462   1.1   tsutsui 
    463   1.1   tsutsui 		/* make sure reserved_ngdb fits in the last cg */
    464   1.1   tsutsui 		if (reserved_ngdb >= blocks_lastcg - cgoverhead(ncg - 1))
    465   1.1   tsutsui 			reserved_ngdb = blocks_lastcg - cgoverhead(ncg - 1);
    466   1.1   tsutsui 		if (reserved_ngdb == 0) {
    467   1.1   tsutsui 			/* if no space for reserved gdb, disable the feature */
    468   1.1   tsutsui 			sblock.e2fs.e2fs_features_compat &=
    469   1.1   tsutsui 			    ~EXT2F_COMPAT_RESIZE;
    470   1.1   tsutsui 		}
    471   1.1   tsutsui 		sblock.e2fs.e2fs_reserved_ngdb = reserved_ngdb;
    472   1.1   tsutsui 	}
    473   1.1   tsutsui 
    474   1.1   tsutsui 	/*
    475   1.1   tsutsui 	 * Initialize group descriptors
    476   1.1   tsutsui 	 */
    477   1.1   tsutsui 	gd = malloc(sblock.e2fs_ngdb * bsize);
    478   1.1   tsutsui 	if (gd == NULL)
    479   1.1   tsutsui 		errx(EXIT_FAILURE, "Can't allocate descriptors buffer");
    480   1.1   tsutsui 	memset(gd, 0, sblock.e2fs_ngdb * bsize);
    481   1.1   tsutsui 
    482   1.1   tsutsui 	fbcount = 0;
    483   1.1   tsutsui 	ficount = 0;
    484   1.1   tsutsui 	for (cylno = 0; cylno < ncg; cylno++) {
    485   1.1   tsutsui 		uint boffset;
    486   1.1   tsutsui 
    487   1.1   tsutsui 		boffset = cgbase(&sblock, cylno);
    488   1.1   tsutsui 		if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
    489   1.1   tsutsui 		    (sblock.e2fs.e2fs_features_rocompat &
    490   1.1   tsutsui 		     EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
    491   1.1   tsutsui 		    cg_has_sb(cylno)) {
    492   1.1   tsutsui 			boffset += NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb;
    493   1.1   tsutsui 			if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    494   1.1   tsutsui 			    (sblock.e2fs.e2fs_features_compat &
    495   1.1   tsutsui 			     EXT2F_COMPAT_RESIZE) != 0)
    496   1.1   tsutsui 				boffset += sblock.e2fs.e2fs_reserved_ngdb;
    497   1.1   tsutsui 		}
    498  1.25    martin 		gd[cylno].ext2bgd_b_bitmap = h2fs32(boffset);
    499   1.1   tsutsui 		boffset += NBLOCK_BLOCK_BITMAP;
    500  1.25    martin 		gd[cylno].ext2bgd_i_bitmap = h2fs32(boffset);
    501   1.1   tsutsui 		boffset += NBLOCK_INODE_BITMAP;
    502  1.25    martin 		gd[cylno].ext2bgd_i_tables = h2fs32(boffset);
    503   1.1   tsutsui 		if (cylno == (ncg - 1))
    504  1.25    martin 			gd[cylno].ext2bgd_nbfree = h2fs16(
    505  1.25    martin 			    blocks_lastcg - cgoverhead(cylno));
    506   1.1   tsutsui 		else
    507  1.25    martin 			gd[cylno].ext2bgd_nbfree = h2fs16(
    508  1.25    martin 			    sblock.e2fs.e2fs_bpg - cgoverhead(cylno));
    509  1.25    martin 		fbcount += fs2h16(gd[cylno].ext2bgd_nbfree);
    510   1.1   tsutsui 		if (cylno == 0) {
    511   1.1   tsutsui 			/* take reserved inodes off nifree */
    512  1.25    martin 			gd[cylno].ext2bgd_nifree = h2fs16(
    513  1.25    martin 			    sblock.e2fs.e2fs_ipg-EXT2_RESERVED_INODES);
    514  1.25    martin 		} else {
    515  1.25    martin 			gd[cylno].ext2bgd_nifree =
    516  1.25    martin 			    h2fs16(sblock.e2fs.e2fs_ipg);
    517   1.1   tsutsui 		}
    518  1.25    martin 		ficount += fs2h16(gd[cylno].ext2bgd_nifree);
    519   1.1   tsutsui 		gd[cylno].ext2bgd_ndirs = 0;
    520   1.1   tsutsui 	}
    521   1.1   tsutsui 	sblock.e2fs.e2fs_fbcount = fbcount;
    522   1.1   tsutsui 	sblock.e2fs.e2fs_ficount = ficount;
    523   1.1   tsutsui 
    524   1.1   tsutsui 	/*
    525   1.1   tsutsui 	 * Dump out summary information about file system.
    526   1.1   tsutsui 	 */
    527   1.1   tsutsui 	if (verbosity > 0) {
    528   1.1   tsutsui 		printf("%s: %u.%1uMB (%" PRId64 " sectors) "
    529   1.1   tsutsui 		    "block size %u, fragment size %u\n",
    530   1.1   tsutsui 		    fsys,
    531   1.1   tsutsui 		    (uint)(((uint64_t)bcount * bsize) / (1024 * 1024)),
    532   1.1   tsutsui 		    (uint)((uint64_t)bcount * bsize -
    533   1.1   tsutsui 		    rounddown((uint64_t)bcount * bsize, 1024 * 1024))
    534   1.1   tsutsui 		    / 1024 / 100,
    535   1.1   tsutsui 		    fssize, bsize, fsize);
    536   1.1   tsutsui 		printf("\tusing %u block groups of %u.0MB, %u blks, "
    537   1.1   tsutsui 		    "%u inodes.\n",
    538   1.1   tsutsui 		    ncg, bsize * sblock.e2fs.e2fs_bpg / (1024 * 1024),
    539   1.1   tsutsui 		    sblock.e2fs.e2fs_bpg, sblock.e2fs.e2fs_ipg);
    540   1.1   tsutsui 	}
    541   1.1   tsutsui 
    542   1.1   tsutsui 	/*
    543   1.1   tsutsui 	 * allocate space for superblock and group descriptors
    544   1.1   tsutsui 	 */
    545   1.1   tsutsui 	iobufsize = (NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb) * sblock.e2fs_bsize;
    546   1.1   tsutsui 	iobuf = mmap(0, iobufsize, PROT_READ|PROT_WRITE,
    547   1.1   tsutsui 	    MAP_ANON|MAP_PRIVATE, -1, 0);
    548   1.1   tsutsui 	if (iobuf == NULL)
    549  1.22  christos 		errx(EXIT_FAILURE, "Cannot allocate I/O buffer");
    550   1.1   tsutsui 	memset(iobuf, 0, iobufsize);
    551   1.1   tsutsui 
    552   1.1   tsutsui 	/*
    553   1.1   tsutsui 	 * We now start writing to the filesystem
    554   1.1   tsutsui 	 */
    555   1.1   tsutsui 
    556   1.1   tsutsui 	if (!Nflag) {
    557   1.7   tsutsui 		static const uint pbsize[] = { 1024, 2048, 4096, 0 };
    558  1.15   tsutsui 		uint pblock;
    559   1.1   tsutsui 		/*
    560   1.1   tsutsui 		 * Validate the given file system size.
    561   1.1   tsutsui 		 * Verify that its last block can actually be accessed.
    562   1.1   tsutsui 		 * Convert to file system fragment sized units.
    563   1.1   tsutsui 		 */
    564   1.1   tsutsui 		if (fssize <= 0)
    565  1.22  christos 			errx(EXIT_FAILURE, "Preposterous size %" PRId64,
    566   1.1   tsutsui 			    fssize);
    567   1.1   tsutsui 		wtfs(fssize - 1, sectorsize, iobuf);
    568   1.1   tsutsui 
    569   1.1   tsutsui 		/*
    570   1.1   tsutsui 		 * Ensure there is nothing that looks like a filesystem
    571   1.9   tsutsui 		 * superblock anywhere other than where ours will be.
    572   1.1   tsutsui 		 *
    573  1.15   tsutsui 		 * Ext2fs superblock is always placed at the same SBOFF,
    574  1.15   tsutsui 		 * so we just zap possible first backups.
    575   1.1   tsutsui 		 */
    576   1.1   tsutsui 		for (i = 0; pbsize[i] != 0; i++) {
    577  1.15   tsutsui  			pblock = (pbsize[i] > BBSIZE) ? 0 : 1;	/* 1st dblk */
    578  1.15   tsutsui 			pblock += pbsize[i] * NBBY;		/* next bg */
    579  1.15   tsutsui 			/* zap first backup */
    580  1.15   tsutsui 			zap_old_sblock(pblock * pbsize[i]);
    581   1.1   tsutsui 		}
    582  1.15   tsutsui 		/*
    583  1.28    andvar 		 * Also zap possible FFS magic leftover to prevent
    584  1.28    andvar 		 * kernel vfs_mountroot() and bootloaders from mis-recognizing
    585  1.15   tsutsui 		 * this file system as FFS.
    586  1.15   tsutsui 		 */
    587  1.15   tsutsui 		zap_old_sblock(8192);	/* SBLOCK_UFS1 */
    588  1.15   tsutsui 		zap_old_sblock(65536);	/* SBLOCK_UFS2 */
    589   1.1   tsutsui 	}
    590   1.1   tsutsui 
    591   1.1   tsutsui 	if (verbosity >= 3)
    592   1.1   tsutsui 		printf("super-block backups (for fsck_ext2fs -b #) at:\n");
    593   1.1   tsutsui 	/* If we are printing more than one line of numbers, line up columns */
    594   1.1   tsutsui 	fld_width = verbosity < 4 ? 1 : snprintf(NULL, 0, "%" PRIu64,
    595   1.1   tsutsui 	    (uint64_t)cgbase(&sblock, ncg - 1));
    596   1.1   tsutsui 	/* Get terminal width */
    597  1.27   tsutsui 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0 &&
    598  1.27   tsutsui 	    winsize.ws_col != 0)
    599   1.1   tsutsui 		max_cols = winsize.ws_col;
    600   1.1   tsutsui 	else
    601   1.1   tsutsui 		max_cols = 80;
    602   1.1   tsutsui 	if (Nflag && verbosity == 3)
    603   1.1   tsutsui 		/* Leave space to add " ..." after one row of numbers */
    604   1.1   tsutsui 		max_cols -= 4;
    605   1.1   tsutsui #define BASE 0x10000	/* For some fixed-point maths */
    606   1.1   tsutsui 	col = 0;
    607   1.1   tsutsui 	delta = verbosity > 2 ? 0 : max_cols * BASE / ncg;
    608   1.1   tsutsui 	for (cylno = 0; cylno < ncg; cylno++) {
    609   1.1   tsutsui 		fflush(stdout);
    610   1.1   tsutsui 		initcg(cylno);
    611   1.1   tsutsui 		if (verbosity < 2)
    612   1.1   tsutsui 			continue;
    613   1.1   tsutsui 		/* the first one is a master, not backup */
    614   1.1   tsutsui 		if (cylno == 0)
    615   1.1   tsutsui 			continue;
    616   1.1   tsutsui 		/* skip if this cylinder doesn't have a backup */
    617   1.1   tsutsui 		if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    618   1.1   tsutsui 		    (sblock.e2fs.e2fs_features_rocompat &
    619   1.1   tsutsui 		     EXT2F_ROCOMPAT_SPARSESUPER) != 0 &&
    620   1.1   tsutsui 		    cg_has_sb(cylno) == 0)
    621   1.1   tsutsui 			continue;
    622   1.1   tsutsui 
    623   1.1   tsutsui 		if (delta > 0) {
    624   1.1   tsutsui 			if (Nflag)
    625   1.1   tsutsui 				/* No point doing dots for -N */
    626   1.1   tsutsui 				break;
    627   1.1   tsutsui 			/* Print dots scaled to end near RH margin */
    628   1.1   tsutsui 			for (col += delta; col > BASE; col -= BASE)
    629   1.1   tsutsui 				printf(".");
    630   1.1   tsutsui 			continue;
    631   1.1   tsutsui 		}
    632   1.1   tsutsui 		/* Print superblock numbers */
    633  1.16     joerg 		len = printf("%s%*" PRIu64 ",", (col ? " " : ""), fld_width,
    634   1.1   tsutsui 		    (uint64_t)cgbase(&sblock, cylno));
    635   1.1   tsutsui 		col += len;
    636   1.1   tsutsui 		if (col + len < max_cols)
    637   1.1   tsutsui 			/* Next number fits */
    638   1.1   tsutsui 			continue;
    639   1.1   tsutsui 		/* Next number won't fit, need a newline */
    640   1.1   tsutsui 		if (verbosity <= 3) {
    641   1.1   tsutsui 			/* Print dots for subsequent cylinder groups */
    642   1.1   tsutsui 			delta = sblock.e2fs_ncg - cylno - 1;
    643   1.1   tsutsui 			if (delta != 0) {
    644   1.1   tsutsui 				if (Nflag) {
    645   1.1   tsutsui 					printf(" ...");
    646   1.1   tsutsui 					break;
    647   1.1   tsutsui 				}
    648   1.1   tsutsui 				delta = max_cols * BASE / delta;
    649   1.1   tsutsui 			}
    650   1.1   tsutsui 		}
    651   1.1   tsutsui 		col = 0;
    652   1.1   tsutsui 		printf("\n");
    653   1.1   tsutsui 	}
    654   1.1   tsutsui #undef BASE
    655   1.1   tsutsui 	if (col > 0)
    656   1.1   tsutsui 		printf("\n");
    657   1.1   tsutsui 	if (Nflag)
    658   1.1   tsutsui 		return;
    659   1.1   tsutsui 
    660   1.1   tsutsui 	/*
    661   1.1   tsutsui 	 * Now construct the initial file system,
    662   1.1   tsutsui 	 */
    663   1.1   tsutsui 	if (fsinit(&tv) == 0)
    664   1.1   tsutsui 		errx(EXIT_FAILURE, "Error making filesystem");
    665   1.1   tsutsui 	/*
    666   1.1   tsutsui 	 * Write out the superblock and group descriptors
    667   1.1   tsutsui 	 */
    668   1.1   tsutsui 	sblock.e2fs.e2fs_block_group_nr = 0;
    669   1.1   tsutsui 	sboff = 0;
    670   1.1   tsutsui 	if (cgbase(&sblock, 0) == 0) {
    671   1.1   tsutsui 		/*
    672   1.1   tsutsui 		 * If the first block contains the boot block sectors,
    673   1.1   tsutsui 		 * (i.e. in case of sblock.e2fs.e2fs_bsize > BBSIZE)
    674   1.1   tsutsui 		 * we have to preserve data in it.
    675   1.1   tsutsui 		 */
    676   1.1   tsutsui 		sboff = SBOFF;
    677   1.1   tsutsui 	}
    678   1.1   tsutsui 	e2fs_sbsave(&sblock.e2fs, (struct ext2fs *)(iobuf + sboff));
    679   1.1   tsutsui 	e2fs_cgsave(gd, (struct ext2_gd *)(iobuf + sblock.e2fs_bsize),
    680   1.1   tsutsui 	   sizeof(struct ext2_gd) * sblock.e2fs_ncg);
    681  1.20  dholland 	wtfs(EXT2_FSBTODB(&sblock, cgbase(&sblock, 0)) + sboff / sectorsize,
    682   1.1   tsutsui 	    iobufsize - sboff, iobuf + sboff);
    683   1.1   tsutsui 
    684   1.1   tsutsui 	munmap(iobuf, iobufsize);
    685   1.1   tsutsui }
    686   1.1   tsutsui 
    687   1.1   tsutsui /*
    688   1.1   tsutsui  * Initialize a cylinder (block) group.
    689   1.1   tsutsui  */
    690   1.1   tsutsui void
    691   1.1   tsutsui initcg(uint cylno)
    692   1.1   tsutsui {
    693   1.3   tsutsui 	uint nblcg, i, j, sboff;
    694   1.3   tsutsui 	struct ext2fs_dinode *dp;
    695   1.1   tsutsui 
    696   1.1   tsutsui 	/*
    697   1.1   tsutsui 	 * Make a copy of the superblock and group descriptors.
    698   1.1   tsutsui 	 */
    699   1.1   tsutsui 	if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
    700   1.1   tsutsui 	    (sblock.e2fs.e2fs_features_rocompat &
    701   1.1   tsutsui 	     EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
    702   1.1   tsutsui 	    cg_has_sb(cylno)) {
    703   1.1   tsutsui 		sblock.e2fs.e2fs_block_group_nr = cylno;
    704   1.1   tsutsui 		sboff = 0;
    705   1.1   tsutsui 		if (cgbase(&sblock, cylno) == 0) {
    706   1.1   tsutsui 			/* preserve data in bootblock in cg0 */
    707   1.1   tsutsui 			sboff = SBOFF;
    708   1.1   tsutsui 		}
    709   1.1   tsutsui 		e2fs_sbsave(&sblock.e2fs, (struct ext2fs *)(iobuf + sboff));
    710   1.1   tsutsui 		e2fs_cgsave(gd, (struct ext2_gd *)(iobuf +
    711   1.1   tsutsui 		    sblock.e2fs_bsize * NBLOCK_SUPERBLOCK),
    712   1.1   tsutsui 		    sizeof(struct ext2_gd) * sblock.e2fs_ncg);
    713   1.1   tsutsui 		/* write superblock and group descriptor backups */
    714  1.20  dholland 		wtfs(EXT2_FSBTODB(&sblock, cgbase(&sblock, cylno)) +
    715   1.1   tsutsui 		    sboff / sectorsize, iobufsize - sboff, iobuf + sboff);
    716   1.1   tsutsui 	}
    717   1.1   tsutsui 
    718   1.1   tsutsui 	/*
    719   1.1   tsutsui 	 * Initialize block bitmap.
    720   1.1   tsutsui 	 */
    721   1.1   tsutsui 	memset(buf, 0, sblock.e2fs_bsize);
    722   1.1   tsutsui 	if (cylno == (sblock.e2fs_ncg - 1)) {
    723   1.1   tsutsui 		/* The last group could have less blocks than e2fs_bpg. */
    724   1.1   tsutsui 		nblcg = sblock.e2fs.e2fs_bcount -
    725   1.1   tsutsui 		    cgbase(&sblock, sblock.e2fs_ncg - 1);
    726   1.1   tsutsui 		for (i = nblcg; i < roundup(nblcg, NBBY); i++)
    727   1.1   tsutsui 			setbit(buf, i);
    728   1.1   tsutsui 		memset(&buf[i / NBBY], ~0U, sblock.e2fs.e2fs_bpg - i);
    729   1.1   tsutsui 	}
    730   1.1   tsutsui 	/* set overhead (superblock, group descriptor etc.) blocks used */
    731   1.1   tsutsui 	for (i = 0; i < cgoverhead(cylno) / NBBY; i++)
    732   1.1   tsutsui 		buf[i] = ~0;
    733   1.1   tsutsui 	i = i * NBBY;
    734   1.1   tsutsui 	for (; i < cgoverhead(cylno); i++)
    735   1.1   tsutsui 		setbit(buf, i);
    736  1.25    martin 	wtfs(EXT2_FSBTODB(&sblock, fs2h32(gd[cylno].ext2bgd_b_bitmap)),
    737  1.21   tsutsui 	    sblock.e2fs_bsize, buf);
    738   1.1   tsutsui 
    739   1.1   tsutsui 	/*
    740   1.1   tsutsui 	 * Initialize inode bitmap.
    741   1.1   tsutsui 	 *
    742   1.5   tsutsui 	 *  Assume e2fs_ipg is a multiple of NBBY since
    743   1.9   tsutsui 	 *  it's a multiple of e2fs_ipb (as we did above).
    744   1.1   tsutsui 	 *  Note even (possibly smaller) the last group has the same e2fs_ipg.
    745   1.1   tsutsui 	 */
    746   1.1   tsutsui 	i = sblock.e2fs.e2fs_ipg / NBBY;
    747   1.1   tsutsui 	memset(buf, 0, i);
    748   1.1   tsutsui 	memset(buf + i, ~0U, sblock.e2fs_bsize - i);
    749   1.1   tsutsui 	if (cylno == 0) {
    750   1.1   tsutsui 		/* mark reserved inodes */
    751   1.1   tsutsui 		for (i = 1; i < EXT2_FIRSTINO; i++)
    752   1.1   tsutsui 			setbit(buf, EXT2_INO_INDEX(i));
    753   1.1   tsutsui 	}
    754  1.25    martin 	wtfs(EXT2_FSBTODB(&sblock, fs2h32(gd[cylno].ext2bgd_i_bitmap)),
    755  1.21   tsutsui 	    sblock.e2fs_bsize, buf);
    756   1.1   tsutsui 
    757   1.1   tsutsui 	/*
    758   1.1   tsutsui 	 * Initialize inode tables.
    759   1.1   tsutsui 	 *
    760   1.3   tsutsui 	 *  Just initialize generation numbers for NFS security.
    761   1.3   tsutsui 	 *  XXX: sys/ufs/ext2fs/ext2fs_alloc.c:ext2fs_valloc() seems
    762   1.3   tsutsui 	 *       to override these generated numbers.
    763   1.1   tsutsui 	 */
    764   1.1   tsutsui 	memset(buf, 0, sblock.e2fs_bsize);
    765   1.1   tsutsui 	for (i = 0; i < sblock.e2fs_itpg; i++) {
    766   1.3   tsutsui 		for (j = 0; j < sblock.e2fs_ipb; j++) {
    767  1.11   tsutsui 			dp = (struct ext2fs_dinode *)(buf + inodesize * j);
    768   1.3   tsutsui 			/* h2fs32() just for consistency */
    769  1.11   tsutsui 			dp->e2di_gen = h2fs32(arc4random());
    770   1.3   tsutsui 		}
    771  1.25    martin 		wtfs(EXT2_FSBTODB(&sblock, fs2h32(gd[cylno].ext2bgd_i_tables) + i),
    772   1.1   tsutsui 		    sblock.e2fs_bsize, buf);
    773   1.1   tsutsui 	}
    774   1.1   tsutsui }
    775   1.1   tsutsui 
    776   1.1   tsutsui /*
    777   1.1   tsutsui  * Zap possible lingering old superblock data
    778   1.1   tsutsui  */
    779   1.1   tsutsui static void
    780  1.15   tsutsui zap_old_sblock(int sblkoff)
    781   1.1   tsutsui {
    782  1.15   tsutsui 	static int cg0_data;
    783   1.1   tsutsui 	uint32_t oldfs[SBSIZE / sizeof(uint32_t)];
    784   1.1   tsutsui 	static const struct fsm {
    785   1.1   tsutsui 		uint32_t offset;
    786   1.1   tsutsui 		uint32_t magic;
    787   1.1   tsutsui 		uint32_t mask;
    788   1.1   tsutsui 	} fs_magics[] = {
    789   1.1   tsutsui 		{offsetof(struct ext2fs, e2fs_magic) / 4, E2FS_MAGIC, 0xffff},
    790   1.1   tsutsui 		{offsetof(struct ext2fs, e2fs_magic) / 4,
    791   1.1   tsutsui 		    E2FS_MAGIC << 16, 0xffff0000},
    792   1.1   tsutsui 		{14, 0xef530000, 0xffff0000},	/* EXT2FS (big) */
    793   1.1   tsutsui 		{0x55c / 4, 0x00011954, ~0U},	/* FS_UFS1_MAGIC */
    794   1.1   tsutsui 		{0x55c / 4, 0x19540119, ~0U},	/* FS_UFS2_MAGIC */
    795   1.1   tsutsui 		{0, 0x70162, ~0U},		/* LFS_MAGIC */
    796   1.1   tsutsui 		{.offset = ~0U},
    797   1.1   tsutsui 	};
    798   1.1   tsutsui 	const struct fsm *fsm;
    799   1.1   tsutsui 
    800   1.1   tsutsui 	if (Nflag)
    801   1.1   tsutsui 		return;
    802   1.1   tsutsui 
    803   1.1   tsutsui 	/* don't override data before superblock */
    804  1.15   tsutsui 	if (sblkoff < SBOFF)
    805   1.1   tsutsui 		return;
    806   1.1   tsutsui 
    807   1.1   tsutsui 	if (cg0_data == 0) {
    808   1.1   tsutsui 		cg0_data =
    809   1.1   tsutsui 		    ((daddr_t)sblock.e2fs.e2fs_first_dblock + cgoverhead(0)) *
    810  1.15   tsutsui 		    sblock.e2fs_bsize;
    811   1.1   tsutsui 	}
    812   1.1   tsutsui 
    813   1.1   tsutsui 	/* Ignore anything that is beyond our filesystem */
    814  1.15   tsutsui 	if (sblkoff / sectorsize >= fssize)
    815   1.1   tsutsui 		return;
    816   1.1   tsutsui 	/* Zero anything inside our filesystem... */
    817  1.15   tsutsui 	if (sblkoff >= sblock.e2fs.e2fs_first_dblock * bsize) {
    818   1.1   tsutsui 		/* ...unless we will write that area anyway */
    819  1.15   tsutsui 		if (sblkoff >= cg0_data)
    820   1.1   tsutsui 			/* assume iobuf is zero'ed here */
    821  1.15   tsutsui 			wtfs(sblkoff / sectorsize,
    822  1.15   tsutsui 			    roundup(SBSIZE, sectorsize), iobuf);
    823   1.1   tsutsui 		return;
    824   1.1   tsutsui 	}
    825   1.1   tsutsui 
    826   1.1   tsutsui 	/*
    827   1.1   tsutsui 	 * The sector might contain boot code, so we must validate it
    828   1.1   tsutsui 	 *
    829   1.1   tsutsui 	 * XXX: ext2fs won't preserve data after SBOFF,
    830   1.9   tsutsui 	 *      but first_dblock could have a different value.
    831   1.1   tsutsui 	 */
    832  1.15   tsutsui 	rdfs(sblkoff / sectorsize, sizeof(oldfs), &oldfs);
    833   1.1   tsutsui 	for (fsm = fs_magics;; fsm++) {
    834   1.1   tsutsui 		uint32_t v;
    835   1.1   tsutsui 		if (fsm->mask == 0)
    836   1.1   tsutsui 			return;
    837   1.1   tsutsui 		v = oldfs[fsm->offset];
    838   1.1   tsutsui 		if ((v & fsm->mask) == fsm->magic ||
    839   1.1   tsutsui 		    (bswap32(v) & fsm->mask) == fsm->magic)
    840   1.1   tsutsui 			break;
    841   1.1   tsutsui 	}
    842   1.1   tsutsui 
    843   1.1   tsutsui 	/* Just zap the magic number */
    844   1.1   tsutsui 	oldfs[fsm->offset] = 0;
    845  1.15   tsutsui 	wtfs(sblkoff / sectorsize, sizeof(oldfs), &oldfs);
    846   1.1   tsutsui }
    847   1.1   tsutsui 
    848   1.1   tsutsui /*
    849   1.1   tsutsui  * uint cgoverhead(uint c)
    850   1.1   tsutsui  *
    851   1.1   tsutsui  * 	Return a number of reserved blocks on the specified group.
    852   1.6   tsutsui  * 	XXX: should be shared with src/sbin/fsck_ext2fs/setup.c
    853   1.1   tsutsui  */
    854   1.1   tsutsui uint
    855   1.1   tsutsui cgoverhead(uint c)
    856   1.1   tsutsui {
    857   1.1   tsutsui 	uint overh;
    858   1.1   tsutsui 
    859   1.1   tsutsui 	overh = NBLOCK_BLOCK_BITMAP + NBLOCK_INODE_BITMAP + sblock.e2fs_itpg;
    860   1.1   tsutsui 
    861   1.1   tsutsui 	if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
    862   1.1   tsutsui 	    (sblock.e2fs.e2fs_features_rocompat &
    863   1.1   tsutsui 	     EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
    864   1.1   tsutsui 	    cg_has_sb(c) != 0) {
    865   1.1   tsutsui 		overh += NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb;
    866   1.1   tsutsui 
    867   1.1   tsutsui 		if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    868   1.1   tsutsui 		    (sblock.e2fs.e2fs_features_compat &
    869   1.1   tsutsui 		     EXT2F_COMPAT_RESIZE) != 0)
    870   1.1   tsutsui 			overh += sblock.e2fs.e2fs_reserved_ngdb;
    871   1.1   tsutsui 	}
    872   1.1   tsutsui 
    873   1.1   tsutsui 	return overh;
    874   1.1   tsutsui }
    875   1.1   tsutsui 
    876   1.1   tsutsui /*
    877   1.1   tsutsui  * Initialize the file system
    878   1.1   tsutsui  */
    879   1.1   tsutsui 
    880   1.1   tsutsui #define LOSTDIR		/* e2fsck complains if there is no lost+found */
    881   1.1   tsutsui 
    882   1.1   tsutsui #define	PREDEFDIR	2
    883   1.1   tsutsui 
    884   1.1   tsutsui #ifdef LOSTDIR
    885   1.1   tsutsui #define	PREDEFROOTDIR	(PREDEFDIR + 1)
    886   1.1   tsutsui #else
    887   1.1   tsutsui #define	PREDEFROOTDIR	PREDEFDIR
    888   1.1   tsutsui #endif
    889   1.1   tsutsui 
    890   1.1   tsutsui struct ext2fs_direct root_dir[] = {
    891   1.1   tsutsui 	{ EXT2_ROOTINO, 0, 1, 0, "." },
    892   1.1   tsutsui 	{ EXT2_ROOTINO, 0, 2, 0, ".." },
    893   1.1   tsutsui #ifdef LOSTDIR
    894   1.1   tsutsui 	{ EXT2_LOSTFOUNDINO, 0, 10, 0, "lost+found" },
    895   1.1   tsutsui #endif
    896   1.1   tsutsui };
    897   1.1   tsutsui 
    898   1.1   tsutsui #ifdef LOSTDIR
    899   1.1   tsutsui struct ext2fs_direct lost_found_dir[] = {
    900   1.1   tsutsui 	{ EXT2_LOSTFOUNDINO, 0, 1, 0, "." },
    901   1.1   tsutsui 	{ EXT2_ROOTINO, 0, 2, 0, ".." },
    902   1.1   tsutsui };
    903   1.1   tsutsui struct ext2fs_direct pad_dir = { 0, sizeof(struct ext2fs_direct), 0, 0, "" };
    904   1.1   tsutsui #endif
    905   1.1   tsutsui 
    906   1.1   tsutsui int
    907   1.1   tsutsui fsinit(const struct timeval *tv)
    908   1.1   tsutsui {
    909   1.1   tsutsui 	struct ext2fs_dinode node;
    910   1.1   tsutsui #ifdef LOSTDIR
    911   1.1   tsutsui 	uint i, nblks_lostfound, blk;
    912   1.1   tsutsui #endif
    913   1.1   tsutsui 
    914   1.1   tsutsui 	/*
    915   1.1   tsutsui 	 * Initialize the inode for the resizefs feature
    916   1.1   tsutsui 	 */
    917   1.1   tsutsui 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    918   1.1   tsutsui 	    (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0)
    919   1.1   tsutsui 		init_resizeino(tv);
    920   1.1   tsutsui 
    921   1.1   tsutsui 	/*
    922   1.1   tsutsui 	 * Initialize the node
    923   1.1   tsutsui 	 */
    924   1.1   tsutsui 
    925   1.1   tsutsui #ifdef LOSTDIR
    926   1.1   tsutsui 	/*
    927   1.1   tsutsui 	 * Create the lost+found directory
    928   1.1   tsutsui 	 */
    929   1.1   tsutsui 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    930   1.1   tsutsui 	    sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) {
    931   1.1   tsutsui 		lost_found_dir[0].e2d_type = EXT2_FT_DIR;
    932   1.1   tsutsui 		lost_found_dir[1].e2d_type = EXT2_FT_DIR;
    933   1.1   tsutsui 	}
    934   1.1   tsutsui 	(void)makedir(lost_found_dir, __arraycount(lost_found_dir));
    935   1.1   tsutsui 
    936   1.1   tsutsui 	/* prepare a bit large directory for preserved files */
    937   1.1   tsutsui 	nblks_lostfound = EXT2_LOSTFOUNDSIZE / sblock.e2fs_bsize;
    938   1.1   tsutsui 	/* ...but only with direct blocks */
    939  1.17  dholland 	if (nblks_lostfound > EXT2FS_NDADDR)
    940  1.17  dholland 		nblks_lostfound = EXT2FS_NDADDR;
    941   1.1   tsutsui 
    942   1.1   tsutsui 	memset(&node, 0, sizeof(node));
    943   1.1   tsutsui 	node.e2di_mode = EXT2_IFDIR | EXT2_LOSTFOUNDUMASK;
    944   1.1   tsutsui 	node.e2di_uid = geteuid();
    945   1.1   tsutsui 	node.e2di_size = sblock.e2fs_bsize * nblks_lostfound;
    946   1.1   tsutsui 	node.e2di_atime = tv->tv_sec;
    947   1.1   tsutsui 	node.e2di_ctime = tv->tv_sec;
    948   1.1   tsutsui 	node.e2di_mtime = tv->tv_sec;
    949   1.1   tsutsui 	node.e2di_gid = getegid();
    950   1.1   tsutsui 	node.e2di_nlink = PREDEFDIR;
    951   1.9   tsutsui 	/* e2di_nblock is a number of disk blocks, not ext2fs blocks */
    952  1.20  dholland 	node.e2di_nblock = EXT2_FSBTODB(&sblock, nblks_lostfound);
    953   1.1   tsutsui 	node.e2di_blocks[0] = alloc(sblock.e2fs_bsize, node.e2di_mode);
    954   1.1   tsutsui 	if (node.e2di_blocks[0] == 0) {
    955   1.1   tsutsui 		printf("%s: can't allocate block for lost+found\n", __func__);
    956   1.1   tsutsui 		return 0;
    957   1.1   tsutsui 	}
    958   1.1   tsutsui 	for (i = 1; i < nblks_lostfound; i++) {
    959   1.1   tsutsui 		blk = alloc(sblock.e2fs_bsize, 0);
    960   1.1   tsutsui 		if (blk == 0) {
    961   1.1   tsutsui 			printf("%s: can't allocate blocks for lost+found\n",
    962   1.1   tsutsui 			    __func__);
    963   1.1   tsutsui 			return 0;
    964   1.1   tsutsui 		}
    965   1.1   tsutsui 		node.e2di_blocks[i] = blk;
    966   1.1   tsutsui 	}
    967  1.21   tsutsui 	wtfs(EXT2_FSBTODB(&sblock, node.e2di_blocks[0]),
    968  1.21   tsutsui 	    sblock.e2fs_bsize, buf);
    969   1.1   tsutsui 	pad_dir.e2d_reclen = sblock.e2fs_bsize;
    970   1.1   tsutsui 	for (i = 1; i < nblks_lostfound; i++) {
    971   1.1   tsutsui 		memset(buf, 0, sblock.e2fs_bsize);
    972   1.1   tsutsui 		copy_dir(&pad_dir, (struct ext2fs_direct *)buf);
    973  1.21   tsutsui 		wtfs(EXT2_FSBTODB(&sblock, node.e2di_blocks[i]),
    974  1.21   tsutsui 		    sblock.e2fs_bsize, buf);
    975   1.1   tsutsui 	}
    976   1.1   tsutsui 	iput(&node, EXT2_LOSTFOUNDINO);
    977   1.1   tsutsui #endif
    978   1.1   tsutsui 	/*
    979   1.1   tsutsui 	 * create the root directory
    980   1.1   tsutsui 	 */
    981   1.1   tsutsui 	memset(&node, 0, sizeof(node));
    982   1.1   tsutsui 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    983   1.1   tsutsui 	    sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) {
    984   1.1   tsutsui 		root_dir[0].e2d_type = EXT2_FT_DIR;
    985   1.1   tsutsui 		root_dir[1].e2d_type = EXT2_FT_DIR;
    986   1.1   tsutsui #ifdef LOSTDIR
    987   1.1   tsutsui 		root_dir[2].e2d_type = EXT2_FT_DIR;
    988   1.1   tsutsui #endif
    989   1.1   tsutsui 	}
    990   1.1   tsutsui 	node.e2di_mode = EXT2_IFDIR | EXT2_UMASK;
    991   1.1   tsutsui 	node.e2di_uid = geteuid();
    992   1.1   tsutsui 	node.e2di_size = makedir(root_dir, __arraycount(root_dir));
    993   1.1   tsutsui 	node.e2di_atime = tv->tv_sec;
    994   1.1   tsutsui 	node.e2di_ctime = tv->tv_sec;
    995   1.1   tsutsui 	node.e2di_mtime = tv->tv_sec;
    996   1.1   tsutsui 	node.e2di_gid = getegid();
    997   1.1   tsutsui 	node.e2di_nlink = PREDEFROOTDIR;
    998   1.1   tsutsui 	/* e2di_nblock is a number of disk block, not ext2fs block */
    999  1.20  dholland 	node.e2di_nblock = EXT2_FSBTODB(&sblock, 1);
   1000   1.1   tsutsui 	node.e2di_blocks[0] = alloc(node.e2di_size, node.e2di_mode);
   1001   1.1   tsutsui 	if (node.e2di_blocks[0] == 0) {
   1002   1.1   tsutsui 		printf("%s: can't allocate block for root dir\n", __func__);
   1003   1.1   tsutsui 		return 0;
   1004   1.1   tsutsui 	}
   1005  1.21   tsutsui 	wtfs(EXT2_FSBTODB(&sblock, node.e2di_blocks[0]),
   1006  1.21   tsutsui 	    sblock.e2fs_bsize, buf);
   1007   1.1   tsutsui 	iput(&node, EXT2_ROOTINO);
   1008   1.1   tsutsui 	return 1;
   1009   1.1   tsutsui }
   1010   1.1   tsutsui 
   1011   1.1   tsutsui /*
   1012   1.1   tsutsui  * Construct a set of directory entries in "buf".
   1013   1.1   tsutsui  * return size of directory.
   1014   1.1   tsutsui  */
   1015   1.1   tsutsui int
   1016   1.1   tsutsui makedir(struct ext2fs_direct *protodir, int entries)
   1017   1.1   tsutsui {
   1018   1.1   tsutsui 	uint8_t *cp;
   1019   1.1   tsutsui 	uint i, spcleft;
   1020   1.1   tsutsui 	uint dirblksiz;
   1021   1.1   tsutsui 
   1022   1.1   tsutsui 	dirblksiz = sblock.e2fs_bsize;
   1023   1.1   tsutsui 	memset(buf, 0, dirblksiz);
   1024   1.1   tsutsui 	spcleft = dirblksiz;
   1025   1.1   tsutsui 	for (cp = buf, i = 0; i < entries - 1; i++) {
   1026   1.1   tsutsui 		protodir[i].e2d_reclen = EXT2FS_DIRSIZ(protodir[i].e2d_namlen);
   1027   1.1   tsutsui 		copy_dir(&protodir[i], (struct ext2fs_direct *)cp);
   1028   1.1   tsutsui 		cp += protodir[i].e2d_reclen;
   1029   1.1   tsutsui 		spcleft -= protodir[i].e2d_reclen;
   1030   1.1   tsutsui 	}
   1031   1.1   tsutsui 	protodir[i].e2d_reclen = spcleft;
   1032   1.1   tsutsui 	copy_dir(&protodir[i], (struct ext2fs_direct *)cp);
   1033   1.1   tsutsui 	return dirblksiz;
   1034   1.1   tsutsui }
   1035   1.1   tsutsui 
   1036   1.1   tsutsui /*
   1037   1.1   tsutsui  * Copy a direntry to a buffer, in fs byte order
   1038   1.1   tsutsui  */
   1039   1.1   tsutsui static void
   1040   1.1   tsutsui copy_dir(struct ext2fs_direct *dir, struct ext2fs_direct *dbuf)
   1041   1.1   tsutsui {
   1042   1.1   tsutsui 
   1043   1.1   tsutsui 	memcpy(dbuf, dir, EXT2FS_DIRSIZ(dir->e2d_namlen));
   1044   1.1   tsutsui 	dbuf->e2d_ino = h2fs32(dir->e2d_ino);
   1045   1.1   tsutsui 	dbuf->e2d_reclen = h2fs16(dir->e2d_reclen);
   1046   1.1   tsutsui }
   1047   1.1   tsutsui 
   1048   1.1   tsutsui /*
   1049   1.1   tsutsui  * void init_resizeino(const struct timeval *tv);
   1050   1.1   tsutsui  *
   1051   1.9   tsutsui  *	Initialize the EXT2_RESEIZE_INO inode to preserve
   1052   1.1   tsutsui  *	reserved group descriptor blocks for future growth of this ext2fs.
   1053   1.1   tsutsui  */
   1054   1.1   tsutsui void
   1055   1.1   tsutsui init_resizeino(const struct timeval *tv)
   1056   1.1   tsutsui {
   1057   1.1   tsutsui 	struct ext2fs_dinode node;
   1058   1.1   tsutsui 	uint64_t isize;
   1059   1.1   tsutsui 	uint32_t *dindir_block, *reserved_gdb;
   1060   1.1   tsutsui 	uint nblock, i, cylno, n;
   1061   1.1   tsutsui 
   1062   1.1   tsutsui 	memset(&node, 0, sizeof(node));
   1063   1.1   tsutsui 
   1064   1.1   tsutsui 	/*
   1065   1.1   tsutsui 	 * Note this function only prepares required structures for
   1066   1.1   tsutsui 	 * future resize. It's a quite different work to implement
   1067   1.1   tsutsui 	 * a utility like resize_ext2fs(8) which handles actual
   1068   1.1   tsutsui 	 * resize ops even on offline.
   1069   1.1   tsutsui 	 *
   1070   1.1   tsutsui 	 * Anyway, I'm not sure if there is any documentation about
   1071   1.1   tsutsui 	 * this resize ext2fs feature and related data structures,
   1072   1.1   tsutsui 	 * and I've written this function based on things what I see
   1073   1.1   tsutsui 	 * on some existing implementation and real file system data
   1074   1.1   tsutsui 	 * created by existing tools. To be honest, they are not
   1075   1.1   tsutsui 	 * so easy to read, so I will try to implement it here without
   1076   1.1   tsutsui 	 * any dumb optimization for people who would eventually
   1077   1.1   tsutsui 	 * work on "yet another wheel" like resize_ext2fs(8).
   1078   1.1   tsutsui 	 */
   1079   1.1   tsutsui 
   1080   1.1   tsutsui 	/*
   1081   1.1   tsutsui 	 * I'm not sure what type is appropriate for this inode.
   1082   1.1   tsutsui 	 * The release notes of e2fsprogs says they changed e2fsck to allow
   1083   1.1   tsutsui 	 * IFREG for RESIZEINO since a certain resize tool used it. Hmm.
   1084   1.1   tsutsui 	 */
   1085   1.2   tsutsui 	node.e2di_mode = EXT2_IFREG | EXT2_RESIZEINOUMASK;
   1086   1.1   tsutsui 	node.e2di_uid = geteuid();
   1087   1.1   tsutsui 	node.e2di_atime = tv->tv_sec;
   1088   1.1   tsutsui 	node.e2di_ctime = tv->tv_sec;
   1089   1.1   tsutsui 	node.e2di_mtime = tv->tv_sec;
   1090   1.1   tsutsui 	node.e2di_gid = getegid();
   1091   1.1   tsutsui 	node.e2di_nlink = 1;
   1092   1.1   tsutsui 
   1093   1.1   tsutsui 	/*
   1094   1.1   tsutsui 	 * To preserve the reserved group descriptor blocks,
   1095   1.1   tsutsui 	 * EXT2_RESIZEINO uses only double indirect reference
   1096   1.1   tsutsui 	 * blocks in its inode entries.
   1097   1.1   tsutsui 	 *
   1098   1.1   tsutsui 	 * All entries for direct, single indirect and triple
   1099   1.1   tsutsui 	 * indirect references are left zero'ed. Maybe it's safe
   1100   1.1   tsutsui 	 * because no write operation will happen with this inode.
   1101   1.1   tsutsui 	 *
   1102   1.1   tsutsui 	 * We have to allocate a block for the first level double
   1103   1.1   tsutsui 	 * indirect reference block. Indexes of inode entries in
   1104   1.1   tsutsui 	 * this first level dindirect block are corresponding to
   1105   1.1   tsutsui 	 * indexes of group descriptors including both used (e2fs_ngdb)
   1106   1.1   tsutsui 	 * and reserved (e2fs_reserved_ngdb) group descriptor blocks.
   1107   1.1   tsutsui 	 *
   1108   1.1   tsutsui 	 * Inode entries of indexes for used (e2fs_ngdb) descriptors are
   1109   1.1   tsutsui 	 * left zero'ed. Entries for reserved (e2fs_reserved_ngdb) ones
   1110   1.1   tsutsui 	 * have block numbers of actual reserved group descriptors
   1111   1.1   tsutsui 	 * allocated at block group zero. This means e2fs_reserved_ngdb
   1112   1.1   tsutsui 	 * blocks are reserved as the second level dindirect reference
   1113   1.9   tsutsui 	 * blocks, and they actually contain block numbers of indirect
   1114   1.1   tsutsui 	 * references. It may be safe since they don't have to keep any
   1115   1.1   tsutsui 	 * data yet.
   1116   1.1   tsutsui 	 *
   1117   1.1   tsutsui 	 * Each these second dindirect blocks (i.e. reserved group
   1118   1.1   tsutsui 	 * descriptor blocks in the first block group) should have
   1119   1.1   tsutsui 	 * block numbers of its backups in all other block groups.
   1120   1.1   tsutsui 	 * I.e. reserved_ngdb[0] block in block group 0 contains block
   1121   1.1   tsutsui 	 * numbers of resreved_ngdb[0] from group 1 through (e2fs_ncg - 1).
   1122   1.1   tsutsui 	 * The number of backups can be determined by the
   1123   1.1   tsutsui 	 * EXT2_ROCOMPAT_SPARSESUPER feature and cg_has_sb() macro
   1124   1.1   tsutsui 	 * as done in the above initcg() function.
   1125   1.1   tsutsui 	 */
   1126   1.1   tsutsui 
   1127   1.1   tsutsui 	/* set e2di_size which occupies whole blocks through DINDIR blocks */
   1128  1.17  dholland 	isize = (uint64_t)sblock.e2fs_bsize * EXT2FS_NDADDR +
   1129  1.19  dholland 	    (uint64_t)sblock.e2fs_bsize * EXT2_NINDIR(&sblock) +
   1130  1.21   tsutsui 	    (uint64_t)sblock.e2fs_bsize * EXT2_NINDIR(&sblock) *
   1131  1.21   tsutsui 	    EXT2_NINDIR(&sblock);
   1132   1.1   tsutsui 	if (isize > UINT32_MAX &&
   1133   1.1   tsutsui 	    (sblock.e2fs.e2fs_features_rocompat &
   1134   1.1   tsutsui 	     EXT2F_ROCOMPAT_LARGEFILE) == 0) {
   1135   1.1   tsutsui 		/* XXX should enable it here and update all backups? */
   1136   1.1   tsutsui 		errx(EXIT_FAILURE, "%s: large_file rocompat feature is "
   1137  1.22  christos 		    "required to enable resize feature for this filesystem",
   1138   1.1   tsutsui 		    __func__);
   1139   1.1   tsutsui 	}
   1140  1.24  jdolecek 	/* upper 32bit is stored into e2di_size_high on REV1 feature */
   1141  1.24  jdolecek 	node.e2di_size 	    = isize & UINT32_MAX;
   1142  1.24  jdolecek 	node.e2di_size_high = isize >> 32;
   1143   1.1   tsutsui 
   1144   1.1   tsutsui #define SINGLE	0	/* index of single indirect block */
   1145   1.1   tsutsui #define DOUBLE	1	/* index of double indirect block */
   1146   1.1   tsutsui #define TRIPLE	2	/* index of triple indirect block */
   1147   1.1   tsutsui 
   1148   1.1   tsutsui 	/* zero out entries for direct references */
   1149  1.17  dholland 	for (i = 0; i < EXT2FS_NDADDR; i++)
   1150   1.1   tsutsui 		node.e2di_blocks[i] = 0;
   1151   1.1   tsutsui 	/* also zero out entries for single and triple indirect references */
   1152  1.17  dholland 	node.e2di_blocks[EXT2FS_NDADDR + SINGLE] = 0;
   1153  1.17  dholland 	node.e2di_blocks[EXT2FS_NDADDR + TRIPLE] = 0;
   1154   1.1   tsutsui 
   1155   1.1   tsutsui 	/* allocate a block for the first level double indirect reference */
   1156  1.17  dholland 	node.e2di_blocks[EXT2FS_NDADDR + DOUBLE] =
   1157   1.1   tsutsui 	    alloc(sblock.e2fs_bsize, node.e2di_mode);
   1158  1.17  dholland 	if (node.e2di_blocks[EXT2FS_NDADDR + DOUBLE] == 0)
   1159   1.1   tsutsui 		errx(EXIT_FAILURE, "%s: Can't allocate a dindirect block",
   1160   1.1   tsutsui 		    __func__);
   1161   1.1   tsutsui 
   1162   1.1   tsutsui 	/* account this first block */
   1163  1.20  dholland 	nblock = EXT2_FSBTODB(&sblock, 1);
   1164   1.1   tsutsui 
   1165   1.1   tsutsui 	/* allocate buffer to set data in the dindirect block */
   1166   1.1   tsutsui 	dindir_block = malloc(sblock.e2fs_bsize);
   1167   1.1   tsutsui 	if (dindir_block == NULL)
   1168   1.1   tsutsui 		errx(EXIT_FAILURE,
   1169   1.1   tsutsui 		    "%s: Can't allocate buffer for a dindirect block",
   1170   1.1   tsutsui 		    __func__);
   1171   1.1   tsutsui 
   1172   1.1   tsutsui 	/* allocate buffer to set data in the group descriptor blocks */
   1173   1.1   tsutsui 	reserved_gdb = malloc(sblock.e2fs_bsize);
   1174   1.1   tsutsui 	if (reserved_gdb == NULL)
   1175   1.1   tsutsui 		errx(EXIT_FAILURE,
   1176   1.1   tsutsui 		    "%s: Can't allocate buffer for group descriptor blocks",
   1177   1.1   tsutsui 		    __func__);
   1178   1.1   tsutsui 
   1179   1.1   tsutsui 	/*
   1180   1.1   tsutsui 	 * Setup block entries in the first level dindirect blocks
   1181   1.1   tsutsui 	 */
   1182   1.1   tsutsui 	for (i = 0; i < sblock.e2fs_ngdb; i++) {
   1183   1.1   tsutsui 		/* no need to handle used group descriptor blocks */
   1184   1.1   tsutsui 		dindir_block[i] = 0;
   1185   1.1   tsutsui 	}
   1186   1.1   tsutsui 	for (; i < sblock.e2fs_ngdb + sblock.e2fs.e2fs_reserved_ngdb; i++) {
   1187   1.1   tsutsui 		/*
   1188   1.1   tsutsui 		 * point reserved group descriptor block in the first
   1189   1.1   tsutsui 		 * (i.e. master) block group
   1190   1.1   tsutsui 		 *
   1191  1.19  dholland 		 * XXX: e2fsprogs seem to use "(i % EXT2_NINDIR(&sblock))" here
   1192  1.19  dholland 		 *      to store maximum EXT2_NINDIR(&sblock) reserved gdbs.
   1193   1.1   tsutsui 		 *      I'm not sure what will be done on future filesystem
   1194   1.1   tsutsui 		 *      shrink in that case on their way.
   1195   1.1   tsutsui 		 */
   1196  1.19  dholland 		if (i >= EXT2_NINDIR(&sblock))
   1197   1.1   tsutsui 			errx(EXIT_FAILURE, "%s: too many reserved "
   1198   1.1   tsutsui 			    "group descriptors (%u) for resize inode",
   1199   1.1   tsutsui 			    __func__, sblock.e2fs.e2fs_reserved_ngdb);
   1200   1.1   tsutsui 		dindir_block[i] =
   1201   1.1   tsutsui 		    h2fs32(cgbase(&sblock, 0) + NBLOCK_SUPERBLOCK + i);
   1202   1.1   tsutsui 
   1203   1.1   tsutsui 		/*
   1204   1.1   tsutsui 		 * Setup block entries in the second dindirect blocks
   1205   1.1   tsutsui 		 * (which are primary reserved group descriptor blocks)
   1206   1.1   tsutsui 		 * to point their backups.
   1207   1.1   tsutsui 		 */
   1208   1.1   tsutsui 		for (n = 0, cylno = 1; cylno < sblock.e2fs_ncg; cylno++) {
   1209   1.1   tsutsui 			/* skip block groups without backup */
   1210   1.1   tsutsui 			if ((sblock.e2fs.e2fs_features_rocompat &
   1211   1.1   tsutsui 			     EXT2F_ROCOMPAT_SPARSESUPER) != 0 &&
   1212   1.1   tsutsui 			    cg_has_sb(cylno) == 0)
   1213   1.1   tsutsui 				continue;
   1214   1.1   tsutsui 
   1215  1.19  dholland 			if (n >= EXT2_NINDIR(&sblock))
   1216   1.1   tsutsui 				errx(EXIT_FAILURE, "%s: too many block groups "
   1217   1.1   tsutsui 				    "for the resize feature", __func__);
   1218   1.1   tsutsui 			/*
   1219   1.1   tsutsui 			 * These blocks are already reserved in
   1220   1.1   tsutsui 			 * initcg() so no need to use alloc() here.
   1221   1.1   tsutsui 			 */
   1222   1.1   tsutsui 			reserved_gdb[n++] = h2fs32(cgbase(&sblock, cylno) +
   1223   1.1   tsutsui 			    NBLOCK_SUPERBLOCK + i);
   1224  1.20  dholland 			nblock += EXT2_FSBTODB(&sblock, 1);
   1225   1.1   tsutsui 		}
   1226  1.19  dholland 		for (; n < EXT2_NINDIR(&sblock); n++)
   1227   1.1   tsutsui 			reserved_gdb[n] = 0;
   1228   1.1   tsutsui 
   1229   1.1   tsutsui 		/* write group descriptor block as the second dindirect refs */
   1230  1.20  dholland 		wtfs(EXT2_FSBTODB(&sblock, fs2h32(dindir_block[i])),
   1231   1.1   tsutsui 		    sblock.e2fs_bsize, reserved_gdb);
   1232  1.20  dholland 		nblock += EXT2_FSBTODB(&sblock, 1);
   1233   1.1   tsutsui 	}
   1234  1.19  dholland 	for (; i < EXT2_NINDIR(&sblock); i++) {
   1235   1.1   tsutsui 		/* leave trailing entries unallocated */
   1236   1.1   tsutsui 		dindir_block[i] = 0;
   1237   1.1   tsutsui 	}
   1238   1.1   tsutsui 	free(reserved_gdb);
   1239   1.1   tsutsui 
   1240   1.1   tsutsui 	/* finally write the first level dindirect block */
   1241  1.20  dholland 	wtfs(EXT2_FSBTODB(&sblock, node.e2di_blocks[EXT2FS_NDADDR + DOUBLE]),
   1242   1.1   tsutsui 	    sblock.e2fs_bsize, dindir_block);
   1243   1.1   tsutsui 	free(dindir_block);
   1244   1.1   tsutsui 
   1245   1.1   tsutsui 	node.e2di_nblock = nblock;
   1246   1.1   tsutsui 	iput(&node, EXT2_RESIZEINO);
   1247   1.1   tsutsui }
   1248   1.1   tsutsui 
   1249   1.1   tsutsui /*
   1250   1.1   tsutsui  * uint32_t alloc(uint32_t size, uint16_t mode)
   1251   1.1   tsutsui  *
   1252   1.1   tsutsui  *	Allocate a block (from cylinder group 0)
   1253   1.1   tsutsui  *	Reference: src/sys/ufs/ext2fs/ext2fs_alloc.c:ext2fs_alloccg()
   1254   1.1   tsutsui  */
   1255   1.1   tsutsui uint32_t
   1256   1.1   tsutsui alloc(uint32_t size, uint16_t mode)
   1257   1.1   tsutsui {
   1258   1.1   tsutsui 	uint32_t loc, bno;
   1259   1.1   tsutsui 	uint8_t *bbp;
   1260   1.1   tsutsui 	uint len, map, i;
   1261   1.1   tsutsui 
   1262   1.1   tsutsui 	if (gd[0].ext2bgd_nbfree == 0)
   1263   1.1   tsutsui 		return 0;
   1264   1.1   tsutsui 
   1265   1.1   tsutsui 	if (size > sblock.e2fs_bsize)
   1266   1.1   tsutsui 		return 0;
   1267   1.1   tsutsui 
   1268   1.1   tsutsui 	bbp = malloc(sblock.e2fs_bsize);
   1269   1.1   tsutsui 	if (bbp == NULL)
   1270   1.1   tsutsui 		return 0;
   1271  1.25    martin 	rdfs(EXT2_FSBTODB(&sblock, fs2h32(gd[0].ext2bgd_b_bitmap)),
   1272  1.21   tsutsui 	    sblock.e2fs_bsize, bbp);
   1273   1.1   tsutsui 
   1274   1.1   tsutsui 	/* XXX: kernel uses e2fs_fpg here */
   1275   1.1   tsutsui 	len = sblock.e2fs.e2fs_bpg / NBBY;
   1276   1.1   tsutsui 
   1277   1.1   tsutsui #if 0	/* no need block allocation for root or lost+found dir */
   1278   1.1   tsutsui 	for (loc = 0; loc < len; loc++) {
   1279   1.1   tsutsui 		if (bbp[loc] == 0) {
   1280   1.1   tsutsui 			bno = loc * NBBY;
   1281   1.1   tsutsui 			goto gotit;
   1282   1.1   tsutsui 		}
   1283   1.1   tsutsui 	}
   1284   1.1   tsutsui #endif
   1285   1.1   tsutsui 
   1286   1.1   tsutsui 	loc = skpc(~0U, len, bbp);
   1287  1.18       apb 	if (loc == 0) {
   1288  1.18       apb 		free(bbp);
   1289   1.1   tsutsui 		return 0;
   1290  1.18       apb 	}
   1291   1.1   tsutsui 	loc = len - loc;
   1292   1.1   tsutsui 	map = bbp[loc];
   1293   1.1   tsutsui 	bno = loc * NBBY;
   1294   1.1   tsutsui 	for (i = 0; i < NBBY; i++, bno++) {
   1295   1.1   tsutsui 		if ((map & (1 << i)) == 0)
   1296   1.1   tsutsui 			goto gotit;
   1297   1.1   tsutsui 	}
   1298  1.18       apb 	free(bbp);
   1299   1.1   tsutsui 	return 0;
   1300   1.1   tsutsui 
   1301   1.1   tsutsui  gotit:
   1302   1.1   tsutsui 	if (isset(bbp, bno))
   1303  1.22  christos 		errx(EXIT_FAILURE, "%s: inconsistent bitmap", __func__);
   1304   1.1   tsutsui 
   1305   1.1   tsutsui 	setbit(bbp, bno);
   1306  1.25    martin 	wtfs(EXT2_FSBTODB(&sblock, fs2h32(gd[0].ext2bgd_b_bitmap)),
   1307  1.21   tsutsui 	    sblock.e2fs_bsize, bbp);
   1308   1.1   tsutsui 	free(bbp);
   1309   1.1   tsutsui 	/* XXX: modified group descriptors won't be written into backups */
   1310  1.25    martin 	gd[0].ext2bgd_nbfree = h2fs16(fs2h16(gd[0].ext2bgd_nbfree)-1);
   1311   1.1   tsutsui 	if ((mode & EXT2_IFDIR) != 0)
   1312  1.25    martin 		gd[0].ext2bgd_ndirs = h2fs16(fs2h16(gd[0].ext2bgd_ndirs)+1);
   1313   1.1   tsutsui 	sblock.e2fs.e2fs_fbcount--;
   1314   1.1   tsutsui 
   1315   1.1   tsutsui 	return sblock.e2fs.e2fs_first_dblock + bno;
   1316   1.1   tsutsui }
   1317   1.1   tsutsui 
   1318   1.1   tsutsui /*
   1319   1.1   tsutsui  * void iput(struct ext2fs_dinode *ip, ino_t ino)
   1320   1.1   tsutsui  *
   1321   1.1   tsutsui  *	Put an inode entry into the corresponding table.
   1322   1.1   tsutsui  */
   1323   1.1   tsutsui static void
   1324   1.1   tsutsui iput(struct ext2fs_dinode *ip, ino_t ino)
   1325   1.1   tsutsui {
   1326   1.1   tsutsui 	daddr_t d;
   1327   1.1   tsutsui 	uint c, i;
   1328   1.1   tsutsui 	struct ext2fs_dinode *dp;
   1329   1.1   tsutsui 	uint8_t *bp;
   1330   1.1   tsutsui 
   1331   1.1   tsutsui 	bp = malloc(sblock.e2fs_bsize);
   1332   1.1   tsutsui 	if (bp == NULL)
   1333  1.22  christos 		errx(EXIT_FAILURE, "%s: can't allocate buffer for inode",
   1334   1.1   tsutsui 		    __func__);
   1335   1.1   tsutsui 
   1336   1.1   tsutsui 	/*
   1337   1.1   tsutsui 	 * Reserved inodes are allocated and accounted in initcg()
   1338   1.1   tsutsui 	 * so skip checks of the bitmap and allocation for them.
   1339   1.1   tsutsui 	 */
   1340   1.1   tsutsui 	if (ino >= EXT2_FIRSTINO) {
   1341   1.1   tsutsui 		c = ino_to_cg(&sblock, ino);
   1342   1.1   tsutsui 
   1343   1.1   tsutsui 		/* sanity check */
   1344   1.1   tsutsui 		if (gd[c].ext2bgd_nifree == 0)
   1345   1.1   tsutsui 			errx(EXIT_FAILURE,
   1346  1.22  christos 			    "%s: no free inode %" PRIu64 " in block group %u",
   1347   1.2   tsutsui 			    __func__, (uint64_t)ino, c);
   1348   1.1   tsutsui 
   1349   1.1   tsutsui 		/* update inode bitmap */
   1350  1.25    martin 		rdfs(EXT2_FSBTODB(&sblock, fs2h32(gd[0].ext2bgd_i_bitmap)),
   1351   1.1   tsutsui 		    sblock.e2fs_bsize, bp);
   1352   1.1   tsutsui 
   1353   1.1   tsutsui 		/* more sanity */
   1354   1.1   tsutsui 		if (isset(bp, EXT2_INO_INDEX(ino)))
   1355   1.2   tsutsui 			errx(EXIT_FAILURE, "%s: inode %" PRIu64
   1356  1.22  christos 			    " already in use", __func__, (uint64_t)ino);
   1357   1.1   tsutsui 		setbit(bp, EXT2_INO_INDEX(ino));
   1358  1.25    martin 		wtfs(EXT2_FSBTODB(&sblock, fs2h32(gd[0].ext2bgd_i_bitmap)),
   1359   1.1   tsutsui 		    sblock.e2fs_bsize, bp);
   1360  1.25    martin 		gd[c].ext2bgd_nifree = h2fs16(fs2h16(gd[c].ext2bgd_nifree)-1);
   1361   1.1   tsutsui 		sblock.e2fs.e2fs_ficount--;
   1362   1.1   tsutsui 	}
   1363   1.1   tsutsui 
   1364   1.1   tsutsui 	if (ino >= sblock.e2fs.e2fs_ipg * sblock.e2fs_ncg)
   1365   1.2   tsutsui 		errx(EXIT_FAILURE, "%s: inode value out of range (%" PRIu64
   1366  1.22  christos 		    ")", __func__, (uint64_t)ino);
   1367   1.1   tsutsui 
   1368   1.1   tsutsui 	/* update an inode entry in the table */
   1369  1.20  dholland 	d = EXT2_FSBTODB(&sblock, ino_to_fsba(&sblock, ino));
   1370   1.1   tsutsui 	rdfs(d, sblock.e2fs_bsize, bp);
   1371   1.1   tsutsui 
   1372  1.12   tsutsui 	dp = (struct ext2fs_dinode *)(bp +
   1373  1.12   tsutsui 	    inodesize * ino_to_fsbo(&sblock, ino));
   1374  1.23    nonaka 	e2fs_isave(ip, dp, EXT2_DINODE_SIZE(&sblock));
   1375   1.1   tsutsui 	/* e2fs_i_bswap() doesn't swap e2di_blocks addrs */
   1376   1.1   tsutsui 	if ((ip->e2di_mode & EXT2_IFMT) != EXT2_IFLNK) {
   1377  1.17  dholland 		for (i = 0; i < EXT2FS_NDADDR + EXT2FS_NIADDR; i++)
   1378   1.1   tsutsui 			dp->e2di_blocks[i] = h2fs32(ip->e2di_blocks[i]);
   1379   1.1   tsutsui 	}
   1380   1.3   tsutsui 	/* h2fs32() just for consistency */
   1381   1.3   tsutsui 	dp->e2di_gen = h2fs32(arc4random());
   1382   1.1   tsutsui 
   1383   1.1   tsutsui 	wtfs(d, sblock.e2fs_bsize, bp);
   1384   1.1   tsutsui 	free(bp);
   1385   1.1   tsutsui }
   1386   1.1   tsutsui 
   1387   1.1   tsutsui /*
   1388   1.1   tsutsui  * Read a block from the file system
   1389   1.1   tsutsui  */
   1390   1.1   tsutsui void
   1391   1.1   tsutsui rdfs(daddr_t bno, int size, void *bf)
   1392   1.1   tsutsui {
   1393   1.1   tsutsui 	int n;
   1394   1.1   tsutsui 	off_t offset;
   1395   1.1   tsutsui 
   1396   1.1   tsutsui 	offset = bno;
   1397   1.1   tsutsui 	n = pread(fsi, bf, size, offset * sectorsize);
   1398   1.1   tsutsui 	if (n != size)
   1399   1.2   tsutsui 		err(EXIT_FAILURE, "%s: read error for sector %" PRId64,
   1400   1.2   tsutsui 		    __func__, (int64_t)bno);
   1401   1.1   tsutsui }
   1402   1.1   tsutsui 
   1403   1.1   tsutsui /*
   1404   1.1   tsutsui  * Write a block to the file system
   1405   1.1   tsutsui  */
   1406   1.1   tsutsui void
   1407   1.1   tsutsui wtfs(daddr_t bno, int size, void *bf)
   1408   1.1   tsutsui {
   1409   1.1   tsutsui 	int n;
   1410   1.1   tsutsui 	off_t offset;
   1411   1.1   tsutsui 
   1412   1.1   tsutsui 	if (Nflag)
   1413   1.1   tsutsui 		return;
   1414   1.1   tsutsui 	offset = bno;
   1415   1.1   tsutsui 	n = pwrite(fso, bf, size, offset * sectorsize);
   1416   1.1   tsutsui 	if (n != size)
   1417   1.2   tsutsui 		err(EXIT_FAILURE, "%s: write error for sector %" PRId64,
   1418   1.2   tsutsui 		    __func__, (int64_t)bno);
   1419   1.1   tsutsui }
   1420   1.1   tsutsui 
   1421   1.1   tsutsui int
   1422   1.1   tsutsui ilog2(uint val)
   1423   1.1   tsutsui {
   1424   1.1   tsutsui 
   1425   1.1   tsutsui 	if (val == 0 || !powerof2(val))
   1426  1.22  christos 		errx(EXIT_FAILURE, "%s: %u is not a power of 2",
   1427   1.1   tsutsui 		    __func__, val);
   1428   1.1   tsutsui 
   1429   1.1   tsutsui 	return ffs(val) - 1;
   1430   1.1   tsutsui }
   1431   1.1   tsutsui 
   1432   1.1   tsutsui /*
   1433   1.1   tsutsui  * int skpc(int mask, size_t size, uint8_t *cp)
   1434   1.1   tsutsui  *
   1435   1.1   tsutsui  *	Locate an unsigned character of value mask inside cp[].
   1436   1.1   tsutsui  * 	(from src/sys/lib/libkern/skpc.c)
   1437   1.1   tsutsui  */
   1438   1.1   tsutsui int
   1439   1.1   tsutsui skpc(int mask, size_t size, uint8_t *cp)
   1440   1.1   tsutsui {
   1441   1.1   tsutsui 	uint8_t *end;
   1442   1.1   tsutsui 
   1443   1.1   tsutsui 	end = &cp[size];
   1444   1.1   tsutsui 	while (cp < end && *cp == (uint8_t)mask)
   1445   1.1   tsutsui 		cp++;
   1446   1.1   tsutsui 
   1447   1.1   tsutsui 	return end - cp;
   1448   1.1   tsutsui }
   1449