#include <sys/param.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <ufs/ext2fs/ext2fs_dinode.h>
#include <ufs/ext2fs/ext2fs_dir.h>
#include <ufs/ext2fs/ext2fs.h>
#include <sys/ioctl.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include "extern.h"
static void initcg(uint);
static void zap_old_sblock(daddr32_t);
static uint cgoverhead(uint);
static int fsinit(const struct timeval *);
static int makedir(struct ext2fs_direct *, int);
static void copy_dir(struct ext2fs_direct *, struct ext2fs_direct *);
static void init_resizeino(const struct timeval *);
static uint32_t alloc(uint32_t, uint16_t);
static void iput(struct ext2fs_dinode *, ino_t);
static void rdfs(daddr32_t, int, void *);
static void wtfs(daddr32_t, int, void *);
static int ilog2(uint);
static int skpc(int, size_t, uint8_t *);
static void uuid_get(struct m_ext2fs *);
#define EXT2_DEF_MAX_MNT_COUNT 20
#define EXT2_DEF_FSCKINTV (180 * 24 * 60 * 60)
#define EXT2_RESERVED_INODES (EXT2_FIRSTINO - 1)
#define EXT2_UMASK 0755
#define EXT2_INO_INDEX(ino) ((ino) - 1)
#define EXT2_LOSTFOUNDSIZE 16384
#define EXT2_LOSTFOUNDINO EXT2_FIRSTINO
#define EXT2_LOSTFOUNDUMASK 0700
#define EXT2_RESIZEINOUMASK 0600
#define NBLOCK_SUPERBLOCK 1
#define NBLOCK_BLOCK_BITMAP 1
#define NBLOCK_INODE_BITMAP 1
#define cgbase(fs, c) \
((fs)->e2fs.e2fs_first_dblock + (fs)->e2fs.e2fs_bpg * (c))
#define rounddown(x,y) (((x)/(y))*(y))
union {
struct m_ext2fs m_ext2fs;
char pad[SBSIZE];
} ext2fsun;
#define sblock ext2fsun.m_ext2fs
#define gd ext2fsun.m_ext2fs.e2fs_gd
static uint8_t *iobuf;
static int iobufsize;
static uint8_t buf[MAXBSIZE];
static int fd;
extern int max_cols;
void
mke2fs(const char *fsys, int f)
{
struct timeval tv;
int64_t minfssize;
uint bcount, fbcount, ficount;
uint blocks_gd, blocks_per_cg, inodes_per_cg, iblocks_per_cg;
uint minblocks_per_cg, blocks_lastcg;
uint ncg, cylno, sboff;
int i, len, col, delta, fld_width;
gettimeofday(&tv, NULL);
fd = f;
if (!powerof2(bsize)) {
errx(EXIT_FAILURE,
"block size must be a power of 2, not %u\n",
bsize);
}
if (!powerof2(fsize)) {
errx(EXIT_FAILURE,
"fragment size must be a power of 2, not %u\n",
fsize);
}
if (fsize < sectorsize) {
errx(EXIT_FAILURE,
"fragment size %u is too small, minimum is %u\n",
fsize, sectorsize);
}
if (bsize < MINBSIZE) {
errx(EXIT_FAILURE,
"block size %u is too small, minimum is %u\n",
bsize, MINBSIZE);
}
if (bsize > EXT2_MAXBSIZE) {
errx(EXIT_FAILURE,
"block size %u is too large, maximum is %u\n",
bsize, MAXBSIZE);
}
if (bsize != fsize) {
errx(EXIT_FAILURE,
"block size (%u) can't be different from "
"fragment size (%u)\n",
bsize, fsize);
}
if (Oflag == 0 && inodesize != EXT2_REV0_DINODE_SIZE) {
errx(EXIT_FAILURE, "GOOD_OLD_REV file system format"
" doesn't support %d byte inode\n", inodesize);
}
sblock.e2fs.e2fs_log_bsize = ilog2(bsize) - LOG_MINBSIZE;
sblock.e2fs.e2fs_log_fsize = ilog2(fsize) - LOG_MINFSIZE;
sblock.e2fs_bsize = bsize;
sblock.e2fs_fsize = fsize;
sblock.e2fs_bshift = sblock.e2fs.e2fs_log_bsize + LOG_MINBSIZE;
sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
sblock.e2fs_fsbtodb = ilog2(sblock.e2fs_bsize) - ilog2(sectorsize);
sblock.e2fs_ipb = sblock.e2fs_bsize / inodesize;
sblock.e2fs.e2fs_first_dblock = (sblock.e2fs_bsize > BBSIZE) ? 0 : 1;
minfssize = fsbtodb(&sblock,
sblock.e2fs.e2fs_first_dblock +
NBLOCK_SUPERBLOCK +
1 +
NBLOCK_BLOCK_BITMAP +
NBLOCK_INODE_BITMAP +
1 +
1 +
1
);
if (fssize < minfssize)
errx(EXIT_FAILURE, "Filesystem size %" PRId64
" < minimum size of %" PRId64 "\n", fssize, minfssize);
bcount = dbtofsb(&sblock, fssize);
blocks_per_cg = sblock.e2fs_bsize * NBBY;
ncg = howmany(bcount - sblock.e2fs.e2fs_first_dblock, blocks_per_cg);
blocks_gd = howmany(sizeof(struct ext2_gd) * ncg, bsize);
if (num_inodes < EXT2_FIRSTINO)
num_inodes = EXT2_FIRSTINO;
if (num_inodes > UINT16_MAX * ncg)
num_inodes = UINT16_MAX * ncg;
inodes_per_cg = num_inodes / ncg;
iblocks_per_cg = howmany(inodesize * inodes_per_cg, bsize);
minblocks_per_cg =
NBLOCK_BLOCK_BITMAP +
NBLOCK_INODE_BITMAP +
iblocks_per_cg +
1;
if (Oflag == 0 || cg_has_sb(ncg - 1) != 0)
minblocks_per_cg += NBLOCK_SUPERBLOCK + blocks_gd;
blocks_lastcg = bcount - sblock.e2fs.e2fs_first_dblock -
blocks_per_cg * (ncg - 1);
if (blocks_lastcg < minblocks_per_cg) {
bcount -= blocks_lastcg;
ncg--;
blocks_lastcg = blocks_per_cg;
blocks_gd = howmany(sizeof(struct ext2_gd) * ncg, bsize);
inodes_per_cg = num_inodes / ncg;
}
inodes_per_cg = roundup(inodes_per_cg, sblock.e2fs_ipb);
num_inodes = inodes_per_cg * ncg;
iblocks_per_cg = inodes_per_cg / sblock.e2fs_ipb;
sblock.e2fs.e2fs_bcount = bcount;
sblock.e2fs.e2fs_icount = num_inodes;
sblock.e2fs_ncg = ncg;
sblock.e2fs_ngdb = blocks_gd;
sblock.e2fs_itpg = iblocks_per_cg;
sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * minfree / 100;
sblock.e2fs.e2fs_bpg = blocks_per_cg;
sblock.e2fs.e2fs_fpg = blocks_per_cg;
sblock.e2fs.e2fs_ipg = inodes_per_cg;
sblock.e2fs.e2fs_mtime = 0;
sblock.e2fs.e2fs_wtime = (u_int32_t)tv.tv_sec;
sblock.e2fs.e2fs_mnt_count = 0;
sblock.e2fs.e2fs_max_mnt_count = EXT2_DEF_MAX_MNT_COUNT;
sblock.e2fs.e2fs_magic = E2FS_MAGIC;
sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
sblock.e2fs.e2fs_beh = E2FS_BEH_DEFAULT;
sblock.e2fs.e2fs_minrev = 0;
sblock.e2fs.e2fs_lastfsck = (u_int32_t)tv.tv_sec;
sblock.e2fs.e2fs_fsckintv = EXT2_DEF_FSCKINTV;
sblock.e2fs.e2fs_creator = E2FS_OS_LINUX;
if (Oflag == 0) {
sblock.e2fs.e2fs_rev = E2FS_REV0;
sblock.e2fs.e2fs_features_compat = 0;
sblock.e2fs.e2fs_features_incompat = 0;
sblock.e2fs.e2fs_features_rocompat = 0;
} else {
sblock.e2fs.e2fs_rev = E2FS_REV1;
sblock.e2fs.e2fs_features_compat = EXT2F_COMPAT_RESIZE;
sblock.e2fs.e2fs_features_incompat = EXT2F_INCOMPAT_FTYPE;
sblock.e2fs.e2fs_features_rocompat =
EXT2F_ROCOMPAT_SPARSE_SUPER | EXT2F_ROCOMPAT_LARGE_FILE;
}
sblock.e2fs.e2fs_ruid = geteuid();
sblock.e2fs.e2fs_rgid = getegid();
sblock.e2fs.e2fs_first_ino = EXT2_FIRSTINO;
sblock.e2fs.e2fs_inode_size = inodesize;
uuid_get(&sblock);
if (volname != NULL) {
if (strlen(volname) > sizeof(sblock.e2fs.e2fs_vname))
errx(EXIT_FAILURE, "Volume name is too long");
strlcpy(sblock.e2fs.e2fs_vname, volname,
sizeof(sblock.e2fs.e2fs_vname));
}
sblock.e2fs.e2fs_fsmnt[0] = '\0';
sblock.e2fs_fsmnt[0] = '\0';
sblock.e2fs.e2fs_algo = 0;
sblock.e2fs.e2fs_prealloc = 0;
sblock.e2fs.e2fs_dir_prealloc = 0;
sblock.e2fs.e2fs_reserved_ngdb = 0;
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
(sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0) {
uint64_t target_blocks;
uint target_ncg, target_ngdb, reserved_ngdb;
target_blocks =
(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock)
* 1024ULL;
if (target_blocks > UINT32_MAX)
target_blocks = UINT32_MAX;
target_ncg = howmany(target_blocks, sblock.e2fs.e2fs_bpg);
target_ngdb = howmany(sizeof(struct ext2_gd) * target_ncg,
sblock.e2fs_bsize);
if (target_ngdb > NINDIR(&sblock))
target_ngdb = NINDIR(&sblock);
reserved_ngdb = target_ngdb - sblock.e2fs_ngdb;
if (reserved_ngdb >= blocks_lastcg - cgoverhead(ncg - 1))
reserved_ngdb = blocks_lastcg - cgoverhead(ncg - 1);
if (reserved_ngdb == 0) {
sblock.e2fs.e2fs_features_compat &=
~EXT2F_COMPAT_RESIZE;
}
sblock.e2fs.e2fs_reserved_ngdb = reserved_ngdb;
}
gd = calloc(sblock.e2fs_ngdb, bsize);
if (gd == NULL)
errx(EXIT_FAILURE, "Can't allocate descriptors buffer");
fbcount = 0;
ficount = 0;
for (cylno = 0; cylno < ncg; cylno++) {
uint boffset;
boffset = cgbase(&sblock, cylno);
if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
(sblock.e2fs.e2fs_features_rocompat &
EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
cg_has_sb(cylno)) {
boffset += NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb;
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
(sblock.e2fs.e2fs_features_compat &
EXT2F_COMPAT_RESIZE) != 0)
boffset += sblock.e2fs.e2fs_reserved_ngdb;
}
gd[cylno].ext2bgd_b_bitmap = boffset;
boffset += NBLOCK_BLOCK_BITMAP;
gd[cylno].ext2bgd_i_bitmap = boffset;
boffset += NBLOCK_INODE_BITMAP;
gd[cylno].ext2bgd_i_tables = boffset;
if (cylno == (ncg - 1))
gd[cylno].ext2bgd_nbfree =
blocks_lastcg - cgoverhead(cylno);
else
gd[cylno].ext2bgd_nbfree =
sblock.e2fs.e2fs_bpg - cgoverhead(cylno);
fbcount += gd[cylno].ext2bgd_nbfree;
gd[cylno].ext2bgd_nifree = sblock.e2fs.e2fs_ipg;
if (cylno == 0) {
gd[cylno].ext2bgd_nifree -= EXT2_RESERVED_INODES;
}
ficount += gd[cylno].ext2bgd_nifree;
gd[cylno].ext2bgd_ndirs = 0;
}
sblock.e2fs.e2fs_fbcount = fbcount;
sblock.e2fs.e2fs_ficount = ficount;
if (verbosity > 0) {
printf("%s: %u.%1uMB (%" PRId64 " sectors) "
"block size %u, fragment size %u\n",
fsys,
(uint)(((uint64_t)bcount * bsize) / (1024 * 1024)),
(uint)((uint64_t)bcount * bsize -
rounddown((uint64_t)bcount * bsize, 1024 * 1024))
/ 1024 / 100,
fssize, bsize, fsize);
printf("\tusing %u block groups of %u.0MB, %u blks, "
"%u inodes.\n",
ncg, bsize * sblock.e2fs.e2fs_bpg / (1024 * 1024),
sblock.e2fs.e2fs_bpg, sblock.e2fs.e2fs_ipg);
}
iobufsize = (NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb) * sblock.e2fs_bsize;
iobuf = mmap(0, iobufsize, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
if (iobuf == MAP_FAILED)
errx(EXIT_FAILURE, "Cannot allocate I/O buffer\n");
if (!Nflag) {
static const uint pbsize[] = { 1024, 2048, 4096, 0 };
uint pblock, epblock;
if (fssize <= 0)
errx(EXIT_FAILURE, "Preposterous size %" PRId64 "\n",
fssize);
wtfs(fssize - 1, sectorsize, iobuf);
for (i = 0; pbsize[i] != 0; i++) {
epblock = (uint64_t)bcount * bsize / pbsize[i];
for (pblock = ((pbsize[i] == SBSIZE) ? 1 : 0);
pblock < epblock;
pblock += pbsize[i] * NBBY )
zap_old_sblock((daddr32_t)pblock *
pbsize[i] / sectorsize);
}
}
if (verbosity >= 3)
printf("super-block backups (for fsck_ext2fs -b #) at:\n");
fld_width = verbosity < 4 ? 1 : snprintf(NULL, 0, "%" PRIu64,
(uint64_t)cgbase(&sblock, ncg - 1));
if (Nflag && verbosity == 3)
max_cols -= 4;
#define BASE 0x10000
col = 0;
delta = verbosity > 2 ? 0 : max_cols * BASE / ncg;
for (cylno = 0; cylno < ncg; cylno++) {
fflush(stdout);
initcg(cylno);
if (verbosity < 2)
continue;
if (cylno == 0)
continue;
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
(sblock.e2fs.e2fs_features_rocompat &
EXT2F_ROCOMPAT_SPARSE_SUPER) != 0 &&
cg_has_sb(cylno) == 0)
continue;
if (delta > 0) {
if (Nflag)
break;
for (col += delta; col > BASE; col -= BASE)
printf(".");
continue;
}
len = printf("%s%*" PRIu64 ",", (col ? " " : ""), fld_width,
(uint64_t)cgbase(&sblock, cylno));
col += len;
if (col + len < max_cols)
continue;
if (verbosity <= 3) {
delta = sblock.e2fs_ncg - cylno - 1;
if (delta != 0) {
if (Nflag) {
printf(" ...");
break;
}
delta = max_cols * BASE / delta;
}
}
col = 0;
printf("\n");
}
#undef BASE
if (col > 0)
printf("\n");
if (Nflag)
return;
if (fsinit(&tv) == 0)
errx(EXIT_FAILURE, "Error making filesystem");
sblock.e2fs.e2fs_block_group_nr = 0;
sboff = 0;
if (cgbase(&sblock, 0) == 0) {
sboff = SBOFF;
}
e2fs_sbsave(&sblock.e2fs, (struct ext2fs *)(iobuf + sboff));
e2fs_cgsave(gd, (struct ext2_gd *)(iobuf + sblock.e2fs_bsize),
sizeof(struct ext2_gd) * sblock.e2fs_ncg);
wtfs(fsbtodb(&sblock, cgbase(&sblock, 0)) + sboff / sectorsize,
iobufsize - sboff, iobuf + sboff);
munmap(iobuf, iobufsize);
}
void
initcg(uint cylno)
{
uint nblcg, i, j, sboff;
struct ext2fs_dinode *dp;
if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
(sblock.e2fs.e2fs_features_rocompat &
EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
cg_has_sb(cylno)) {
sblock.e2fs.e2fs_block_group_nr = cylno;
sboff = 0;
if (cgbase(&sblock, cylno) == 0) {
sboff = SBOFF;
}
e2fs_sbsave(&sblock.e2fs, (struct ext2fs *)(iobuf + sboff));
e2fs_cgsave(gd, (struct ext2_gd *)(iobuf +
sblock.e2fs_bsize * NBLOCK_SUPERBLOCK),
sizeof(struct ext2_gd) * sblock.e2fs_ncg);
wtfs(fsbtodb(&sblock, cgbase(&sblock, cylno)) +
sboff / sectorsize, iobufsize - sboff, iobuf + sboff);
}
memset(buf, 0, sblock.e2fs_bsize);
if (cylno == (sblock.e2fs_ncg - 1)) {
nblcg = sblock.e2fs.e2fs_bcount -
cgbase(&sblock, sblock.e2fs_ncg - 1);
for (i = nblcg; i < roundup(nblcg, NBBY); i++)
setbit(buf, i);
memset(&buf[i / NBBY], ~0U, sblock.e2fs.e2fs_bpg - i);
}
for (i = 0; i < cgoverhead(cylno) / NBBY; i++)
buf[i] = ~0;
i = i * NBBY;
for (; i < cgoverhead(cylno); i++)
setbit(buf, i);
wtfs(fsbtodb(&sblock, gd[cylno].ext2bgd_b_bitmap), sblock.e2fs_bsize,
buf);
i = sblock.e2fs.e2fs_ipg / NBBY;
memset(buf, 0, i);
memset(buf + i, ~0U, sblock.e2fs_bsize - i);
if (cylno == 0) {
for (i = 1; i < EXT2_FIRSTINO; i++)
setbit(buf, EXT2_INO_INDEX(i));
}
wtfs(fsbtodb(&sblock, gd[cylno].ext2bgd_i_bitmap), sblock.e2fs_bsize,
buf);
memset(buf, 0, sblock.e2fs_bsize);
for (i = 0; i < sblock.e2fs_itpg; i++) {
for (j = 0; j < sblock.e2fs_ipb; j++) {
dp = (struct ext2fs_dinode *)(buf + inodesize * j);
dp->e2di_gen = htole32(arc4random());
}
wtfs(fsbtodb(&sblock, gd[cylno].ext2bgd_i_tables + i),
sblock.e2fs_bsize, buf);
}
}
static void
zap_old_sblock(daddr32_t sec)
{
static daddr32_t cg0_data;
uint32_t oldfs[SBSIZE / sizeof(uint32_t)];
static const struct fsm {
uint32_t offset;
uint32_t magic;
uint32_t mask;
} fs_magics[] = {
{offsetof(struct ext2fs, e2fs_magic) / 4, E2FS_MAGIC, 0xffff},
{offsetof(struct ext2fs, e2fs_magic) / 4,
E2FS_MAGIC << 16, 0xffff0000},
{14, 0xef530000, 0xffff0000},
{0x55c / 4, 0x00011954, ~0U},
{0x55c / 4, 0x19540119, ~0U},
{0, 0x70162, ~0U},
{.offset = ~0U},
};
const struct fsm *fsm;
if (Nflag)
return;
if (sec < SBOFF / sectorsize)
return;
if (cg0_data == 0) {
cg0_data =
((daddr32_t)sblock.e2fs.e2fs_first_dblock + cgoverhead(0)) *
sblock.e2fs_bsize / sectorsize;
}
if (sec >= fssize)
return;
if (sec >= sblock.e2fs.e2fs_first_dblock * bsize / sectorsize) {
if (sec >= cg0_data)
wtfs(sec, roundup(SBSIZE, sectorsize), iobuf);
return;
}
rdfs(sec, sizeof(oldfs), &oldfs);
for (fsm = fs_magics;; fsm++) {
uint32_t v;
if (fsm->mask == 0)
return;
v = oldfs[fsm->offset];
if ((v & fsm->mask) == fsm->magic ||
(swap32(v) & fsm->mask) == fsm->magic)
break;
}
oldfs[fsm->offset] = 0;
wtfs(sec, sizeof(oldfs), &oldfs);
}
uint
cgoverhead(uint c)
{
uint overh;
overh = NBLOCK_BLOCK_BITMAP + NBLOCK_INODE_BITMAP + sblock.e2fs_itpg;
if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
(sblock.e2fs.e2fs_features_rocompat &
EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
cg_has_sb(c) != 0) {
overh += NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb;
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
(sblock.e2fs.e2fs_features_compat &
EXT2F_COMPAT_RESIZE) != 0)
overh += sblock.e2fs.e2fs_reserved_ngdb;
}
return overh;
}
#define LOSTDIR
#define PREDEFDIR 2
#ifdef LOSTDIR
#define PREDEFROOTDIR (PREDEFDIR + 1)
#else
#define PREDEFROOTDIR PREDEFDIR
#endif
struct ext2fs_direct root_dir[] = {
{ EXT2_ROOTINO, 0, 1, 0, "." },
{ EXT2_ROOTINO, 0, 2, 0, ".." },
#ifdef LOSTDIR
{ EXT2_LOSTFOUNDINO, 0, 10, 0, "lost+found" },
#endif
};
#ifdef LOSTDIR
struct ext2fs_direct lost_found_dir[] = {
{ EXT2_LOSTFOUNDINO, 0, 1, 0, "." },
{ EXT2_ROOTINO, 0, 2, 0, ".." },
};
struct ext2fs_direct pad_dir = { 0, sizeof(struct ext2fs_direct), 0, 0, "" };
#endif
int
fsinit(const struct timeval *tv)
{
struct ext2fs_dinode node;
#ifdef LOSTDIR
uint i, nblks_lostfound, blk;
#endif
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
(sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0)
init_resizeino(tv);
#ifdef LOSTDIR
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) {
lost_found_dir[0].e2d_type = EXT2_FT_DIR;
lost_found_dir[1].e2d_type = EXT2_FT_DIR;
}
(void)makedir(lost_found_dir, nitems(lost_found_dir));
nblks_lostfound = EXT2_LOSTFOUNDSIZE / sblock.e2fs_bsize;
if (nblks_lostfound > NDADDR)
nblks_lostfound = NDADDR;
memset(&node, 0, sizeof(node));
node.e2di_mode = EXT2_IFDIR | EXT2_LOSTFOUNDUMASK;
node.e2di_uid_low = geteuid();
node.e2di_size = sblock.e2fs_bsize * nblks_lostfound;
node.e2di_atime = (u_int32_t)tv->tv_sec;
node.e2di_ctime = (u_int32_t)tv->tv_sec;
node.e2di_mtime = (u_int32_t)tv->tv_sec;
node.e2di_gid_low = getegid();
node.e2di_nlink = PREDEFDIR;
node.e2di_nblock = fsbtodb(&sblock, nblks_lostfound);
node.e2di_blocks[0] = alloc(sblock.e2fs_bsize, node.e2di_mode);
if (node.e2di_blocks[0] == 0) {
printf("%s: can't allocate block for lost+found\n", __func__);
return 0;
}
for (i = 1; i < nblks_lostfound; i++) {
blk = alloc(sblock.e2fs_bsize, 0);
if (blk == 0) {
printf("%s: can't allocate blocks for lost+found\n",
__func__);
return 0;
}
node.e2di_blocks[i] = blk;
}
wtfs(fsbtodb(&sblock, node.e2di_blocks[0]), sblock.e2fs_bsize, buf);
pad_dir.e2d_reclen = sblock.e2fs_bsize;
for (i = 1; i < nblks_lostfound; i++) {
memset(buf, 0, sblock.e2fs_bsize);
copy_dir(&pad_dir, (struct ext2fs_direct *)buf);
wtfs(fsbtodb(&sblock, node.e2di_blocks[i]), sblock.e2fs_bsize,
buf);
}
iput(&node, EXT2_LOSTFOUNDINO);
#endif
memset(&node, 0, sizeof(node));
if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) {
root_dir[0].e2d_type = EXT2_FT_DIR;
root_dir[1].e2d_type = EXT2_FT_DIR;
#ifdef LOSTDIR
root_dir[2].e2d_type = EXT2_FT_DIR;
#endif
}
node.e2di_mode = EXT2_IFDIR | EXT2_UMASK;
node.e2di_uid_low = geteuid();
node.e2di_size = makedir(root_dir, nitems(root_dir));
node.e2di_atime = (u_int32_t)tv->tv_sec;
node.e2di_ctime = (u_int32_t)tv->tv_sec;
node.e2di_mtime = (u_int32_t)tv->tv_sec;
node.e2di_gid_low = getegid();
node.e2di_nlink = PREDEFROOTDIR;
node.e2di_nblock = fsbtodb(&sblock, 1);
node.e2di_blocks[0] = alloc(node.e2di_size, node.e2di_mode);
if (node.e2di_blocks[0] == 0) {
printf("%s: can't allocate block for root dir\n", __func__);
return 0;
}
wtfs(fsbtodb(&sblock, node.e2di_blocks[0]), sblock.e2fs_bsize, buf);
iput(&node, EXT2_ROOTINO);
return 1;
}
int
makedir(struct ext2fs_direct *protodir, int entries)
{
uint8_t *cp;
uint i, spcleft;
uint dirblksiz;
dirblksiz = sblock.e2fs_bsize;
memset(buf, 0, dirblksiz);
spcleft = dirblksiz;
for (cp = buf, i = 0; i < entries - 1; i++) {
protodir[i].e2d_reclen = EXT2FS_DIRSIZ(protodir[i].e2d_namlen);
copy_dir(&protodir[i], (struct ext2fs_direct *)cp);
cp += protodir[i].e2d_reclen;
spcleft -= protodir[i].e2d_reclen;
}
protodir[i].e2d_reclen = spcleft;
copy_dir(&protodir[i], (struct ext2fs_direct *)cp);
return dirblksiz;
}
static void
copy_dir(struct ext2fs_direct *dir, struct ext2fs_direct *dbuf)
{
memcpy(dbuf, dir, EXT2FS_DIRSIZ(dir->e2d_namlen));
dbuf->e2d_ino = htole32(dir->e2d_ino);
dbuf->e2d_reclen = htole16(dir->e2d_reclen);
}
void
init_resizeino(const struct timeval *tv)
{
struct ext2fs_dinode node;
uint64_t isize;
uint32_t *dindir_block, *reserved_gdb;
uint nblock, i, cylno, n;
memset(&node, 0, sizeof(node));
node.e2di_mode = EXT2_IFREG | EXT2_RESIZEINOUMASK;
node.e2di_uid_low = geteuid();
node.e2di_atime = (u_int32_t)tv->tv_sec;
node.e2di_ctime = (u_int32_t)tv->tv_sec;
node.e2di_mtime = (u_int32_t)tv->tv_sec;
node.e2di_gid_low = getegid();
node.e2di_nlink = 1;
isize = (uint64_t)sblock.e2fs_bsize * NDADDR +
(uint64_t)sblock.e2fs_bsize * NINDIR(&sblock) +
(uint64_t)sblock.e2fs_bsize * NINDIR(&sblock) * NINDIR(&sblock);
if (isize > UINT32_MAX &&
(sblock.e2fs.e2fs_features_rocompat &
EXT2F_ROCOMPAT_LARGE_FILE) == 0) {
errx(EXIT_FAILURE, "%s: large_file rocompat feature is "
"required to enable resize feature for this filesystem\n",
__func__);
}
node.e2di_size = isize & UINT32_MAX;
node.e2di_size_hi = isize >> 32;
#define SINGLE 0
#define DOUBLE 1
#define TRIPLE 2
for (i = 0; i < NDADDR; i++)
node.e2di_blocks[i] = 0;
node.e2di_blocks[NDADDR + SINGLE] = 0;
node.e2di_blocks[NDADDR + TRIPLE] = 0;
node.e2di_blocks[NDADDR + DOUBLE] =
alloc(sblock.e2fs_bsize, node.e2di_mode);
if (node.e2di_blocks[NDADDR + DOUBLE] == 0)
errx(EXIT_FAILURE, "%s: Can't allocate a dindirect block",
__func__);
nblock = fsbtodb(&sblock, 1);
dindir_block = malloc(sblock.e2fs_bsize);
if (dindir_block == NULL)
errx(EXIT_FAILURE,
"%s: Can't allocate buffer for a dindirect block",
__func__);
reserved_gdb = malloc(sblock.e2fs_bsize);
if (reserved_gdb == NULL)
errx(EXIT_FAILURE,
"%s: Can't allocate buffer for group descriptor blocks",
__func__);
for (i = 0; i < sblock.e2fs_ngdb; i++) {
dindir_block[i] = 0;
}
for (; i < sblock.e2fs_ngdb + sblock.e2fs.e2fs_reserved_ngdb; i++) {
if (i >= NINDIR(&sblock))
errx(EXIT_FAILURE, "%s: too many reserved "
"group descriptors (%u) for resize inode",
__func__, sblock.e2fs.e2fs_reserved_ngdb);
dindir_block[i] =
htole32(cgbase(&sblock, 0) + NBLOCK_SUPERBLOCK + i);
for (n = 0, cylno = 1; cylno < sblock.e2fs_ncg; cylno++) {
if ((sblock.e2fs.e2fs_features_rocompat &
EXT2F_ROCOMPAT_SPARSE_SUPER) != 0 &&
cg_has_sb(cylno) == 0)
continue;
if (n >= NINDIR(&sblock))
errx(EXIT_FAILURE, "%s: too many block groups "
"for the resize feature", __func__);
reserved_gdb[n++] = htole32(cgbase(&sblock, cylno) +
NBLOCK_SUPERBLOCK + i);
nblock += fsbtodb(&sblock, 1);
}
for (; n < NINDIR(&sblock); n++)
reserved_gdb[n] = 0;
wtfs(fsbtodb(&sblock, letoh32(dindir_block[i])),
sblock.e2fs_bsize, reserved_gdb);
nblock += fsbtodb(&sblock, 1);
}
for (; i < NINDIR(&sblock); i++) {
dindir_block[i] = 0;
}
free(reserved_gdb);
wtfs(fsbtodb(&sblock, node.e2di_blocks[NDADDR + DOUBLE]),
sblock.e2fs_bsize, dindir_block);
free(dindir_block);
node.e2di_nblock = nblock;
iput(&node, EXT2_RESIZEINO);
}
uint32_t
alloc(uint32_t size, uint16_t mode)
{
uint32_t loc, bno;
uint8_t *bbp;
uint len, map, i;
if (gd[0].ext2bgd_nbfree == 0)
return 0;
if (size > sblock.e2fs_bsize)
return 0;
bbp = malloc(sblock.e2fs_bsize);
if (bbp == NULL)
return 0;
rdfs(fsbtodb(&sblock, gd[0].ext2bgd_b_bitmap), sblock.e2fs_bsize, bbp);
len = sblock.e2fs.e2fs_bpg / NBBY;
#if 0
for (loc = 0; loc < len; loc++) {
if (bbp[loc] == 0) {
bno = loc * NBBY;
goto gotit;
}
}
#endif
loc = skpc(~0U, len, bbp);
if (loc == 0) {
free(bbp);
return 0;
}
loc = len - loc;
map = bbp[loc];
bno = loc * NBBY;
for (i = 0; i < NBBY; i++, bno++) {
if ((map & (1 << i)) == 0)
goto gotit;
}
free(bbp);
return 0;
gotit:
if (isset(bbp, bno))
errx(EXIT_FAILURE, "%s: inconsistent bitmap\n", __func__);
setbit(bbp, bno);
wtfs(fsbtodb(&sblock, gd[0].ext2bgd_b_bitmap), sblock.e2fs_bsize, bbp);
free(bbp);
gd[0].ext2bgd_nbfree--;
if ((mode & EXT2_IFDIR) != 0)
gd[0].ext2bgd_ndirs++;
sblock.e2fs.e2fs_fbcount--;
return sblock.e2fs.e2fs_first_dblock + bno;
}
static void
iput(struct ext2fs_dinode *ip, ino_t ino)
{
daddr32_t d;
uint c, i;
struct ext2fs_dinode *dp;
uint8_t *bp;
bp = malloc(sblock.e2fs_bsize);
if (bp == NULL)
errx(EXIT_FAILURE, "%s: can't allocate buffer for inode\n",
__func__);
if (ino >= EXT2_FIRSTINO) {
c = ino_to_cg(&sblock, ino);
if (gd[c].ext2bgd_nifree == 0)
errx(EXIT_FAILURE,
"%s: no free inode %" PRIu64 " in block group %u\n",
__func__, (uint64_t)ino, c);
rdfs(fsbtodb(&sblock, gd[0].ext2bgd_i_bitmap),
sblock.e2fs_bsize, bp);
if (isset(bp, EXT2_INO_INDEX(ino)))
errx(EXIT_FAILURE, "%s: inode %" PRIu64
" already in use\n", __func__, (uint64_t)ino);
setbit(bp, EXT2_INO_INDEX(ino));
wtfs(fsbtodb(&sblock, gd[0].ext2bgd_i_bitmap),
sblock.e2fs_bsize, bp);
gd[c].ext2bgd_nifree--;
sblock.e2fs.e2fs_ficount--;
}
if (ino >= sblock.e2fs.e2fs_ipg * sblock.e2fs_ncg)
errx(EXIT_FAILURE, "%s: inode value out of range (%" PRIu64
").\n", __func__, (uint64_t)ino);
d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
rdfs(d, sblock.e2fs_bsize, bp);
dp = (struct ext2fs_dinode *)(bp +
inodesize * ino_to_fsbo(&sblock, ino));
e2fs_isave(&sblock, ip, dp);
if ((ip->e2di_mode & EXT2_IFMT) != EXT2_IFLNK) {
for (i = 0; i < NDADDR + NIADDR; i++)
dp->e2di_blocks[i] = htole32(ip->e2di_blocks[i]);
}
dp->e2di_gen = htole32(arc4random());
wtfs(d, sblock.e2fs_bsize, bp);
free(bp);
}
void
rdfs(daddr32_t bno, int size, void *bf)
{
int n;
off_t offset;
offset = bno;
n = pread(fd, bf, size, offset * sectorsize);
if (n != size)
err(EXIT_FAILURE, "%s: read error for sector %" PRId64,
__func__, (int64_t)bno);
}
void
wtfs(daddr32_t bno, int size, void *bf)
{
int n;
off_t offset;
if (Nflag)
return;
offset = bno;
n = pwrite(fd, bf, size, offset * sectorsize);
if (n != size)
err(EXIT_FAILURE, "%s: write error for sector %" PRId64,
__func__, (int64_t)bno);
}
int
ilog2(uint val)
{
if (val == 0 || !powerof2(val))
errx(EXIT_FAILURE, "%s: %u is not a power of 2\n",
__func__, val);
return ffs(val) - 1;
}
int
skpc(int mask, size_t size, uint8_t *cp)
{
uint8_t *end;
end = &cp[size];
while (cp < end && *cp == (uint8_t)mask)
cp++;
return end - cp;
}
static void
uuid_get(struct m_ext2fs *sb)
{
unsigned char buf[sizeof(sb->e2fs.e2fs_uuid)];
arc4random_buf(buf, sizeof(buf));
buf[6] &= 0x0f;
buf[6] |= 0x40;
buf[8] &= 0x3f;
buf[8] |= 0x80;
memcpy(sb->e2fs.e2fs_uuid, buf, sizeof(sb->e2fs.e2fs_uuid));
}