#include <sys/types.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <atf-c.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <rump/rump.h>
#include <rump/rump_syscalls.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/lfs/lfs.h>
#include <ufs/lfs/lfs_extern.h>
#include "h_macros.h"
#include "util.h"
#define UNCHANGED_CONTROL MP "/3-a-random-file"
#define BIGSIZE 15000
#define SMALLSIZE 10000
__CTASSERT(BIGSIZE > SMALLSIZE);
void resize(int, size_t);
void test(int);
ATF_TC(resize32);
ATF_TC_HEAD(resize32, tc)
{
atf_tc_set_md_var(tc, "descr",
"LFS32 resize_lfs creates an inconsistent filesystem");
atf_tc_set_md_var(tc, "timeout", "20");
}
ATF_TC(resize64);
ATF_TC_HEAD(resize64, tc)
{
atf_tc_set_md_var(tc, "descr",
"LFS64 resize_lfs creates an inconsistent filesystem");
atf_tc_set_md_var(tc, "timeout", "20");
}
ATF_TC_BODY(resize32, tc)
{
test(32);
}
ATF_TC_BODY(resize64, tc)
{
test(64);
}
void test(int width)
{
struct ufs_args args;
int fd;
setvbuf(stdout, NULL, _IONBF, 0);
create_lfs(BIGSIZE, SMALLSIZE, width, 1);
fprintf(stderr, "* Mount fs [1]\n");
memset(&args, 0, sizeof(args));
args.fspec = __UNCONST(FAKEBLK);
if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
atf_tc_fail_errno("rump_sys_mount failed");
fprintf(stderr, "* Initial payload\n");
write_file(UNCHANGED_CONTROL, CHUNKSIZE, 1, 0);
rump_sys_unmount(MP, 0);
if (fsck())
atf_tc_fail_errno("fsck found errors after first unmount");
fprintf(stderr, "* Remount fs [2, to enlarge]\n");
if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
atf_tc_fail_errno("rump_sys_mount failed [2]");
fd = rump_sys_open(MP, O_RDONLY);
if (fd < 0)
atf_tc_fail_errno("rump_sys_open mount point root failed");
fprintf(stderr, "* Resize (enlarge)\n");
resize(fd, BIGSIZE);
rump_sys_close(fd);
rump_sys_unmount(MP, 0);
if (fsck())
atf_tc_fail_errno("fsck found errors after enlarge");
fprintf(stderr, "* Mount fs [3, to shrink]\n");
if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
atf_tc_fail_errno("rump_sys_mount failed [3]");
fd = rump_sys_open(MP, O_RDONLY);
if (fd < 0)
atf_tc_fail_errno("rump_sys_open mount point root failed");
fprintf(stderr, "* Resize (shrink)\n");
resize(fd, SMALLSIZE);
rump_sys_close(fd);
if (rump_sys_unmount(MP, 0) != 0)
atf_tc_fail_errno("rump_sys_umount failed after shrink");
fprintf(stderr, "* Fsck after shrink\n");
if (fsck())
atf_tc_fail("fsck found errors after shrink");
fprintf(stderr, "* Mount fs [4, to check contents]\n");
if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
atf_tc_fail_errno("rump_sys_mount failed [4]");
if (check_file(UNCHANGED_CONTROL, CHUNKSIZE, 0) != 0)
atf_tc_fail("Unchanged control file differs(!)");
rump_sys_unmount(MP, 0);
fprintf(stderr, "* Fsck after final unmount\n");
if (fsck())
atf_tc_fail("fsck found errors after final unmount");
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, resize32);
ATF_TP_ADD_TC(tp, resize64);
return atf_no_error();
}
void
resize(int fd, size_t size)
{
int newnseg = (size * DEV_BSIZE) / SEGSIZE;
if (rump_sys_fcntl(fd, LFCNRESIZE, &newnseg) != 0)
atf_tc_fail_errno("LFCNRESIZE failed");
}