#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ffs_wapbl.c,v 1.50 2024/12/30 09:03:07 hannken Exp $");
#define WAPBL_INTERNAL
#if defined(_KERNEL_OPT)
#include "opt_ffs.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/file.h>
#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/kauth.h>
#include <sys/wapbl.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_bswap.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ufs/ufs_wapbl.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
#undef WAPBL_DEBUG
#ifdef WAPBL_DEBUG
int ffs_wapbl_debug = 1;
#define DPRINTF(fmt, args...) \
do { \
if (ffs_wapbl_debug) \
printf("%s:%d "fmt, __func__ , __LINE__, ##args); \
} while (0)
#else
#define DPRINTF(fmt, args...) \
do { \
\
} while (0)
#endif
static int ffs_superblock_layout(struct fs *);
static int wapbl_log_position(struct mount *, struct fs *, struct vnode *,
daddr_t *, size_t *, size_t *, uint64_t *);
static int wapbl_create_infs_log(struct mount *, struct fs *, struct vnode *,
daddr_t *, size_t *, uint64_t *);
static void wapbl_find_log_start(struct mount *, struct vnode *, off_t,
daddr_t *, daddr_t *, size_t *);
static int wapbl_remove_log(struct mount *);
static int wapbl_allocate_log_file(struct mount *, struct vnode *,
daddr_t *, size_t *, uint64_t *);
static int
ffs_superblock_layout(struct fs *fs)
{
if ((fs->fs_magic == FS_UFS1_MAGIC) &&
((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))
return 1;
else
return 2;
}
void
ffs_wapbl_replay_finish(struct mount *mp)
{
struct wapbl_replay *wr = mp->mnt_wapbl_replay;
int i;
int error;
if (!wr)
return;
KDASSERT((mp->mnt_flag & MNT_RDONLY) == 0);
for (i = 0; i < wr->wr_inodescnt; i++) {
struct vnode *vp;
struct inode *ip;
error = VFS_VGET(mp, wr->wr_inodes[i].wr_inumber,
LK_EXCLUSIVE, &vp);
if (error) {
printf("%s: %s: unable to cleanup inode %" PRIu32 "\n",
__func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
wr->wr_inodes[i].wr_inumber);
continue;
}
ip = VTOI(vp);
KDASSERT(wr->wr_inodes[i].wr_inumber == ip->i_number);
#ifdef WAPBL_DEBUG
printf("%s%s: %s: cleaning inode %" PRIu64 " size=%" PRIu64
" mode=%o nlink=%d\n",
__func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
ip->i_number, ip->i_size, ip->i_mode, ip->i_nlink);
#endif
KASSERT(ip->i_nlink == 0);
if (ip->i_mode == 0) {
error = UFS_WAPBL_BEGIN(mp);
if (error) {
printf("%s: %s: "
"unable to cleanup inode %" PRIu32 "\n",
__func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
wr->wr_inodes[i].wr_inumber);
} else {
ffs_vfree(vp, ip->i_number,
wr->wr_inodes[i].wr_imode);
UFS_WAPBL_END(mp);
}
}
vput(vp);
}
wapbl_replay_stop(wr);
wapbl_replay_free(wr);
mp->mnt_wapbl_replay = NULL;
}
void
ffs_wapbl_sync_metadata(struct mount *mp, struct wapbl_dealloc *fdealloc)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
int error __diagused;
struct wapbl_dealloc *wd;
UFS_WAPBL_JLOCK_ASSERT(ump->um_mountp);
for (wd = fdealloc; wd != NULL; wd = TAILQ_NEXT(wd, wd_entries)) {
ffs_blkfree(fs, ump->um_devvp,
FFS_DBTOFSB(fs, wd->wd_blkno), wd->wd_len, -1);
}
mutex_enter(&ump->um_lock);
if (fs->fs_fmod != 0) {
fs->fs_fmod = 0;
fs->fs_time = time_second;
mutex_exit(&ump->um_lock);
error = ffs_cgupdate(ump, 0);
KASSERT(error == 0);
} else {
mutex_exit(&ump->um_lock);
}
}
void
ffs_wapbl_abort_sync_metadata(struct mount *mp, struct wapbl_dealloc *fdealloc)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
struct wapbl_dealloc *wd;
for (wd = fdealloc; wd != NULL; wd = TAILQ_NEXT(wd, wd_entries)) {
ffs_blkalloc_ump(ump, FFS_DBTOFSB(fs, wd->wd_blkno),
wd->wd_len);
}
}
static int
wapbl_remove_log(struct mount *mp)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
struct vnode *vp;
struct inode *ip;
ino_t log_ino;
int error;
if (ffs_superblock_layout(fs) < 2)
return 0;
if (fs->fs_journallocs[0] == 0 &&
fs->fs_journallocs[1] == 0 &&
fs->fs_journallocs[2] == 0 &&
fs->fs_journallocs[3] == 0) {
DPRINTF("empty locators, just clear\n");
goto done;
}
switch (fs->fs_journal_location) {
case UFS_WAPBL_JOURNALLOC_NONE:
DPRINTF("no log\n");
break;
case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
log_ino = fs->fs_journallocs[UFS_WAPBL_INFS_INO];
DPRINTF("in-fs log, ino = %" PRId64 "\n",log_ino);
if (log_ino == 0)
goto done;
error = VFS_VGET(mp, log_ino, LK_EXCLUSIVE, &vp);
if (error != 0) {
printf("%s: %s: vget failed %d\n", __func__,
fs->fs_fsmnt, error);
goto done;
}
ip = VTOI(vp);
KASSERT(log_ino == ip->i_number);
if ((ip->i_flags & SF_LOG) == 0) {
printf("%s: %s: try to clear non-log inode "
"%" PRId64 "\n", __func__, fs->fs_fsmnt, log_ino);
vput(vp);
goto done;
}
ip->i_nlink = 0;
DIP_ASSIGN(ip, nlink, 0);
vput(vp);
break;
case UFS_WAPBL_JOURNALLOC_END_PARTITION:
DPRINTF("end-of-partition log\n");
break;
default:
printf("%s: %s: unknown journal type %d\n", __func__,
fs->fs_fsmnt, fs->fs_journal_location);
break;
}
done:
fs->fs_journal_version = 0;
fs->fs_journal_location = 0;
fs->fs_journal_flags = 0;
fs->fs_journallocs[0] = 0;
fs->fs_journallocs[1] = 0;
fs->fs_journallocs[2] = 0;
fs->fs_journallocs[3] = 0;
(void) ffs_sbupdate(ump, MNT_WAIT);
return 0;
}
int
ffs_wapbl_start(struct mount *mp)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
struct vnode *devvp = ump->um_devvp;
daddr_t off;
size_t count;
size_t blksize;
uint64_t extradata;
int error;
if (mp->mnt_wapbl == NULL) {
if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG) {
error = wapbl_remove_log(mp);
if (error != 0)
return error;
}
if (mp->mnt_flag & MNT_LOG) {
KDASSERT(fs->fs_ronly == 0);
if (ffs_superblock_layout(fs) < 2) {
printf("%s: %s: fs superblock in old format, "
"not journaling\n", __func__,
VFSTOUFS(mp)->um_fs->fs_fsmnt);
mp->mnt_flag &= ~MNT_LOG;
return EINVAL;
}
error = wapbl_log_position(mp, fs, devvp, &off,
&count, &blksize, &extradata);
if (error)
return error;
if (mp->mnt_flag & MNT_UPDATE) {
int saveflag = mp->mnt_flag & MNT_ASYNC;
mp->mnt_flag &= ~MNT_ASYNC;
ffs_sync(mp, MNT_WAIT, FSCRED);
mp->mnt_flag |= saveflag;
}
error = wapbl_start(&mp->mnt_wapbl, mp, devvp, off,
count, blksize, mp->mnt_wapbl_replay,
ffs_wapbl_sync_metadata,
ffs_wapbl_abort_sync_metadata);
if (error)
return error;
mp->mnt_wapbl_op = &wapbl_ops;
#ifdef WAPBL_DEBUG
printf("%s: %s: enabling logging\n", __func__,
fs->fs_fsmnt);
#endif
if ((fs->fs_flags & FS_DOWAPBL) == 0) {
fs->fs_flags |= FS_DOWAPBL;
if ((error = UFS_WAPBL_BEGIN(mp)) != 0)
goto out;
error = ffs_sbupdate(ump, MNT_WAIT);
if (error) {
UFS_WAPBL_END(mp);
goto out;
}
UFS_WAPBL_END(mp);
error = wapbl_flush(mp->mnt_wapbl, 1);
if (error)
goto out;
}
if (mp->mnt_flag & MNT_DISCARD) {
CLR(mp->mnt_flag, MNT_DISCARD);
printf("%s: %s: disabling discard to preserve log consistency\n", __func__,
fs->fs_fsmnt);
if (ump->um_discarddata != NULL) {
ffs_discard_finish(ump->um_discarddata,
0);
ump->um_discarddata = NULL;
}
}
} else if (fs->fs_flags & FS_DOWAPBL) {
fs->fs_fmod = 1;
fs->fs_flags &= ~FS_DOWAPBL;
}
}
if ((fs->fs_ronly == 0) && mp->mnt_wapbl_replay) {
int saveflag = mp->mnt_flag & MNT_RDONLY;
mp->mnt_flag &= ~MNT_RDONLY;
ffs_wapbl_replay_finish(mp);
mp->mnt_flag |= saveflag;
KASSERT(fs->fs_ronly == 0);
}
return 0;
out:
ffs_wapbl_stop(mp, MNT_FORCE);
return error;
}
int
ffs_wapbl_stop(struct mount *mp, int force)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
int error;
if (mp->mnt_wapbl) {
KDASSERT(fs->fs_ronly == 0);
error = wapbl_flush(mp->mnt_wapbl, 1);
if (error && !force)
return error;
if (error && force)
goto forceout;
error = UFS_WAPBL_BEGIN(mp);
if (error && !force)
return error;
if (error && force)
goto forceout;
KASSERT(fs->fs_flags & FS_DOWAPBL);
fs->fs_flags &= ~FS_DOWAPBL;
error = ffs_sbupdate(ump, MNT_WAIT);
KASSERT(error == 0);
UFS_WAPBL_END(mp);
forceout:
error = wapbl_stop(mp->mnt_wapbl, force);
if (error) {
KASSERT(!force);
fs->fs_flags |= FS_DOWAPBL;
return error;
}
fs->fs_flags &= ~FS_DOWAPBL;
mp->mnt_wapbl = NULL;
#ifdef WAPBL_DEBUG
printf("%s: %s: disabled logging\n", __func__, fs->fs_fsmnt);
#endif
}
return 0;
}
int
ffs_wapbl_replay_start(struct mount *mp, struct fs *fs, struct vnode *devvp)
{
int error;
daddr_t off;
size_t count;
size_t blksize;
uint64_t extradata;
if (ffs_superblock_layout(fs) < 2)
return EINVAL;
error = wapbl_log_position(mp, fs, devvp, &off, &count, &blksize,
&extradata);
if (error)
return error;
error = wapbl_replay_start(&mp->mnt_wapbl_replay, devvp, off,
count, blksize);
if (error)
return error;
mp->mnt_wapbl_op = &wapbl_ops;
return 0;
}
static int
wapbl_log_position(struct mount *mp, struct fs *fs, struct vnode *devvp,
daddr_t *startp, size_t *countp, size_t *blksizep, uint64_t *extradatap)
{
struct ufsmount *ump = VFSTOUFS(mp);
daddr_t logstart, logend, desired_logsize;
uint64_t numsecs;
unsigned secsize;
int error, location;
if (fs->fs_journal_version == UFS_WAPBL_VERSION) {
switch (fs->fs_journal_location) {
case UFS_WAPBL_JOURNALLOC_END_PARTITION:
DPRINTF("found existing end-of-partition log\n");
*startp = fs->fs_journallocs[UFS_WAPBL_EPART_ADDR];
*countp = fs->fs_journallocs[UFS_WAPBL_EPART_COUNT];
*blksizep = fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
DPRINTF(" start = %" PRId64 ", size = %zu, "
"blksize = %zu\n", *startp, *countp, *blksizep);
return 0;
case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
DPRINTF("found existing in-filesystem log\n");
*startp = fs->fs_journallocs[UFS_WAPBL_INFS_ADDR];
*countp = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT];
*blksizep = fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
DPRINTF(" start = %" PRId64 ", size = %zu, "
"blksize = %zu\n", *startp, *countp, *blksizep);
return 0;
default:
printf("%s: %s: unknown journal type %d\n", __func__,
fs->fs_fsmnt, fs->fs_journal_location);
return EINVAL;
}
}
desired_logsize =
ffs_lfragtosize(fs, fs->fs_size) / UFS_WAPBL_JOURNAL_SCALE;
DPRINTF("desired log size = %" PRId64 " kB\n", desired_logsize / 1024);
desired_logsize = uimax(desired_logsize, UFS_WAPBL_MIN_JOURNAL_SIZE);
desired_logsize = uimin(desired_logsize, UFS_WAPBL_MAX_JOURNAL_SIZE);
DPRINTF("adjusted desired log size = %" PRId64 " kB\n",
desired_logsize / 1024);
logstart = FFS_FSBTODB(fs, fs->fs_size);
error = getdisksize(devvp, &numsecs, &secsize);
if (error)
return error;
KDASSERT(secsize != 0);
logend = btodb(numsecs * secsize);
if (dbtob(logend - logstart) >= desired_logsize) {
DPRINTF("enough space, use end-of-partition log\n");
location = UFS_WAPBL_JOURNALLOC_END_PARTITION;
*blksizep = secsize;
*startp = logstart;
*countp = (logend - logstart);
*extradatap = 0;
*startp = dbtob(*startp) / secsize;
*countp = dbtob(*countp) / secsize;
fs->fs_journallocs[UFS_WAPBL_EPART_ADDR] = *startp;
fs->fs_journallocs[UFS_WAPBL_EPART_COUNT] = *countp;
fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ] = *blksizep;
fs->fs_journallocs[UFS_WAPBL_EPART_UNUSED] = *extradatap;
} else {
DPRINTF("end-of-partition has only %" PRId64 " free\n",
logend - logstart);
location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
*blksizep = secsize;
error = wapbl_create_infs_log(mp, fs, devvp,
startp, countp, extradatap);
ffs_sync(mp, MNT_WAIT, FSCRED);
*startp = dbtob(*startp) / secsize;
*countp = dbtob(*countp) / secsize;
fs->fs_journallocs[UFS_WAPBL_INFS_ADDR] = *startp;
fs->fs_journallocs[UFS_WAPBL_INFS_COUNT] = *countp;
fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = *blksizep;
fs->fs_journallocs[UFS_WAPBL_INFS_INO] = *extradatap;
}
if (error == 0) {
fs->fs_journal_version = UFS_WAPBL_VERSION;
fs->fs_journal_location = location;
fs->fs_journal_flags = 0;
error = ffs_sbupdate(ump, MNT_WAIT);
}
return error;
}
static int
wapbl_create_infs_log(struct mount *mp, struct fs *fs, struct vnode *devvp,
daddr_t *startp, size_t *countp, uint64_t *extradatap)
{
struct vnode *vp, *rvp;
struct vattr va;
struct inode *ip;
int error;
if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)) != 0)
return error;
vattr_null(&va);
va.va_type = VREG;
va.va_mode = 0;
error = vcache_new(mp, rvp, &va, NOCRED, NULL, &vp);
vput(rvp);
if (error)
return error;
error = vn_lock(vp, LK_EXCLUSIVE);
if (error) {
vrele(vp);
return error;
}
ip = VTOI(vp);
ip->i_flags = SF_LOG;
DIP_ASSIGN(ip, flags, ip->i_flags);
ip->i_nlink = 1;
DIP_ASSIGN(ip, nlink, 1);
ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
ffs_update(vp, NULL, NULL, UPDATE_WAIT);
if ((error = wapbl_allocate_log_file(mp, vp,
startp, countp, extradatap)) != 0) {
ip->i_nlink = 0;
DIP_ASSIGN(ip, nlink, 0);
vput(vp);
return error;
}
vput(vp);
return 0;
}
int
wapbl_allocate_log_file(struct mount *mp, struct vnode *vp,
daddr_t *startp, size_t *countp, uint64_t *extradatap)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
daddr_t addr, indir_addr;
off_t logsize;
size_t size;
int error;
logsize = 0;
if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG &&
fs->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM)
logsize = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT];
if (vp->v_size > 0) {
printf("%s: %s: file size (%" PRId64 ") non zero\n", __func__,
fs->fs_fsmnt, vp->v_size);
return EEXIST;
}
wapbl_find_log_start(mp, vp, logsize, &addr, &indir_addr, &size);
if (addr == 0) {
printf("%s: %s: log not allocated, largest extent is "
"%" PRId64 "MB\n", __func__, fs->fs_fsmnt,
ffs_lblktosize(fs, size) / (1024 * 1024));
return ENOSPC;
}
logsize = ffs_lblktosize(fs, size);
VTOI(vp)->i_ffs_first_data_blk = addr;
VTOI(vp)->i_ffs_first_indir_blk = indir_addr;
error = GOP_ALLOC(vp, 0, logsize, B_CONTIG, FSCRED);
if (error) {
printf("%s: %s: GOP_ALLOC error %d\n", __func__, fs->fs_fsmnt,
error);
return error;
}
*startp = FFS_FSBTODB(fs, addr);
*countp = btodb(logsize);
*extradatap = VTOI(vp)->i_number;
return 0;
}
static void
wapbl_find_log_start(struct mount *mp, struct vnode *vp, off_t logsize,
daddr_t *addr, daddr_t *indir_addr, size_t *size)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct fs *fs = ump->um_fs;
struct vnode *devvp = ump->um_devvp;
struct cg *cgp;
struct buf *bp;
uint8_t *blksfree;
daddr_t blkno, best_addr, start_addr;
daddr_t desired_blks, min_desired_blks;
daddr_t freeblks, best_blks;
int bpcg, cg, error, fixedsize, indir_blks, n, s;
const int needswap = UFS_FSNEEDSWAP(fs);
if (logsize == 0) {
fixedsize = 0;
logsize = ffs_lfragtosize(fs, fs->fs_dsize) /
UFS_WAPBL_JOURNAL_SCALE;
DPRINTF("suggested log size = %" PRId64 "\n", logsize);
logsize = uimax(logsize, UFS_WAPBL_MIN_JOURNAL_SIZE);
logsize = uimin(logsize, UFS_WAPBL_MAX_JOURNAL_SIZE);
DPRINTF("adjusted log size = %" PRId64 "\n", logsize);
} else {
fixedsize = 1;
DPRINTF("fixed log size = %" PRId64 "\n", logsize);
}
desired_blks = logsize / fs->fs_bsize;
DPRINTF("desired blocks = %" PRId64 "\n", desired_blks);
indir_blks = 0;
if (desired_blks >= UFS_NDADDR) {
struct indir indirs[UFS_NIADDR + 2];
int num;
error = ufs_getlbns(vp, desired_blks, indirs, &num);
if (error) {
printf("%s: %s: ufs_getlbns failed, error %d!\n",
__func__, fs->fs_fsmnt, error);
goto bad;
}
switch (num) {
case 2:
indir_blks = 1;
break;
case 3:
indir_blks = 1 +
1 +
indirs[1].in_off + 1;
break;
default:
printf("%s: %s: unexpected numlevels %d from "
"ufs_getlbns\n", __func__, fs->fs_fsmnt, num);
*size = 0;
goto bad;
}
desired_blks += indir_blks;
}
DPRINTF("desired blocks = %" PRId64 " (including indirect)\n",
desired_blks);
min_desired_blks = desired_blks;
if (!fixedsize)
min_desired_blks = desired_blks / 4;
bpcg = ffs_fragstoblks(fs, fs->fs_fpg);
if (min_desired_blks > bpcg) {
printf("%s: %s: cylinder group size of %" PRId64 " MB "
" is not big enough for journal\n", __func__, fs->fs_fsmnt,
ffs_lblktosize(fs, bpcg) / (1024 * 1024));
goto bad;
}
best_addr = 0;
best_blks = 0;
for (cg = fs->fs_ncg / 2, s = 0, n = 1;
best_blks < desired_blks && cg >= 0 && cg < fs->fs_ncg;
s++, n = -n, cg += n * s) {
DPRINTF("check cg %d of %d\n", cg, fs->fs_ncg);
error = bread(devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
fs->fs_cgsize, 0, &bp);
if (error) {
continue;
}
cgp = (struct cg *)bp->b_data;
if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
brelse(bp, 0);
continue;
}
blksfree = cg_blksfree(cgp, needswap);
for (blkno = 0; blkno < bpcg;) {
for (; blkno < bpcg - min_desired_blks; blkno++)
if (ffs_isblock(fs, blksfree, blkno))
break;
if (blkno >= bpcg - min_desired_blks)
break;
start_addr = blkno;
for (freeblks = 0; blkno < bpcg; blkno++, freeblks++)
if (!ffs_isblock(fs, blksfree, blkno))
break;
if (freeblks > best_blks) {
best_blks = freeblks;
best_addr = ffs_blkstofrags(fs, start_addr) +
cgbase(fs, cg);
if (freeblks >= desired_blks) {
DPRINTF("found len %" PRId64
" at offset %" PRId64 " in gc\n",
freeblks, start_addr);
break;
}
}
}
brelse(bp, 0);
}
DPRINTF("best found len = %" PRId64 ", wanted %" PRId64
" at addr %" PRId64 "\n", best_blks, desired_blks, best_addr);
if (best_blks < min_desired_blks) {
*addr = 0;
*indir_addr = 0;
} else {
*addr = best_addr + ffs_blkstofrags(fs, indir_blks);
*indir_addr = best_addr;
}
*size = uimin(desired_blks, best_blks) - indir_blks;
return;
bad:
*addr = 0;
*indir_addr = 0;
*size = 0;
return;
}