Home | History | Annotate | Line # | Download | only in ffs
      1  1.50   hannken /*	$NetBSD: ffs_wapbl.c,v 1.50 2024/12/30 09:03:07 hannken Exp $	*/
      2   1.2    simonb 
      3   1.2    simonb /*-
      4   1.2    simonb  * Copyright (c) 2003,2006,2008 The NetBSD Foundation, Inc.
      5   1.2    simonb  * All rights reserved.
      6   1.2    simonb  *
      7   1.2    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2    simonb  * by Wasabi Systems, Inc.
      9   1.2    simonb  *
     10   1.2    simonb  * Redistribution and use in source and binary forms, with or without
     11   1.2    simonb  * modification, are permitted provided that the following conditions
     12   1.2    simonb  * are met:
     13   1.2    simonb  * 1. Redistributions of source code must retain the above copyright
     14   1.2    simonb  *    notice, this list of conditions and the following disclaimer.
     15   1.2    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2    simonb  *    notice, this list of conditions and the following disclaimer in the
     17   1.2    simonb  *    documentation and/or other materials provided with the distribution.
     18   1.2    simonb  *
     19   1.2    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2    simonb  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2    simonb  */
     31   1.2    simonb 
     32   1.2    simonb #include <sys/cdefs.h>
     33  1.50   hannken __KERNEL_RCSID(0, "$NetBSD: ffs_wapbl.c,v 1.50 2024/12/30 09:03:07 hannken Exp $");
     34   1.7     joerg 
     35   1.7     joerg #define WAPBL_INTERNAL
     36   1.2    simonb 
     37   1.2    simonb #if defined(_KERNEL_OPT)
     38   1.2    simonb #include "opt_ffs.h"
     39   1.2    simonb #endif
     40   1.2    simonb 
     41   1.2    simonb #include <sys/param.h>
     42   1.2    simonb #include <sys/systm.h>
     43   1.2    simonb #include <sys/kernel.h>
     44   1.2    simonb #include <sys/vnode.h>
     45   1.2    simonb #include <sys/mount.h>
     46   1.2    simonb #include <sys/file.h>
     47   1.2    simonb #include <sys/disk.h>
     48   1.2    simonb #include <sys/ioctl.h>
     49   1.2    simonb #include <sys/errno.h>
     50   1.2    simonb #include <sys/kauth.h>
     51   1.2    simonb #include <sys/wapbl.h>
     52   1.2    simonb 
     53   1.2    simonb #include <ufs/ufs/inode.h>
     54   1.2    simonb #include <ufs/ufs/quota.h>
     55   1.2    simonb #include <ufs/ufs/ufsmount.h>
     56   1.2    simonb #include <ufs/ufs/ufs_bswap.h>
     57   1.2    simonb #include <ufs/ufs/ufs_extern.h>
     58   1.2    simonb #include <ufs/ufs/ufs_wapbl.h>
     59   1.2    simonb 
     60   1.2    simonb #include <ufs/ffs/fs.h>
     61   1.2    simonb #include <ufs/ffs/ffs_extern.h>
     62   1.2    simonb 
     63   1.2    simonb #undef	WAPBL_DEBUG
     64   1.2    simonb #ifdef WAPBL_DEBUG
     65   1.2    simonb int ffs_wapbl_debug = 1;
     66   1.2    simonb #define DPRINTF(fmt, args...)						\
     67   1.2    simonb do {									\
     68   1.2    simonb 	if (ffs_wapbl_debug)						\
     69   1.2    simonb 		printf("%s:%d "fmt, __func__ , __LINE__, ##args);	\
     70   1.2    simonb } while (/* CONSTCOND */0)
     71   1.2    simonb #else
     72   1.2    simonb #define	DPRINTF(fmt, args...)						\
     73   1.2    simonb do {									\
     74   1.2    simonb 	/* nothing */							\
     75   1.2    simonb } while (/* CONSTCOND */0)
     76   1.2    simonb #endif
     77   1.2    simonb 
     78   1.4    simonb static int ffs_superblock_layout(struct fs *);
     79   1.2    simonb static int wapbl_log_position(struct mount *, struct fs *, struct vnode *,
     80   1.2    simonb     daddr_t *, size_t *, size_t *, uint64_t *);
     81   1.2    simonb static int wapbl_create_infs_log(struct mount *, struct fs *, struct vnode *,
     82  1.14   mlelstv     daddr_t *, size_t *, uint64_t *);
     83   1.2    simonb static void wapbl_find_log_start(struct mount *, struct vnode *, off_t,
     84   1.2    simonb     daddr_t *, daddr_t *, size_t *);
     85   1.2    simonb static int wapbl_remove_log(struct mount *);
     86  1.14   mlelstv static int wapbl_allocate_log_file(struct mount *, struct vnode *,
     87  1.14   mlelstv     daddr_t *, size_t *, uint64_t *);
     88   1.2    simonb 
     89   1.2    simonb /*
     90   1.4    simonb  * Return the super block layout format - UFS1 or UFS2.
     91   1.4    simonb  * WAPBL only works with UFS2 layout (which is still available
     92   1.4    simonb  * with FFSv1).
     93   1.4    simonb  *
     94   1.4    simonb  * XXX Should this be in ufs/ffs/fs.h?  Same style of check is
     95   1.4    simonb  * also used in ffs_alloc.c in a few places.
     96   1.4    simonb  */
     97   1.4    simonb static int
     98   1.4    simonb ffs_superblock_layout(struct fs *fs)
     99   1.4    simonb {
    100   1.4    simonb 	if ((fs->fs_magic == FS_UFS1_MAGIC) &&
    101   1.4    simonb 	    ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))
    102   1.4    simonb 		return 1;
    103   1.4    simonb 	else
    104   1.4    simonb 		return 2;
    105   1.4    simonb }
    106   1.4    simonb 
    107   1.4    simonb /*
    108   1.2    simonb  * This function is invoked after a log is replayed to
    109   1.2    simonb  * disk to perform logical cleanup actions as described by
    110   1.2    simonb  * the log
    111   1.2    simonb  */
    112   1.2    simonb void
    113   1.2    simonb ffs_wapbl_replay_finish(struct mount *mp)
    114   1.2    simonb {
    115   1.2    simonb 	struct wapbl_replay *wr = mp->mnt_wapbl_replay;
    116   1.2    simonb 	int i;
    117   1.2    simonb 	int error;
    118   1.2    simonb 
    119   1.2    simonb 	if (!wr)
    120   1.2    simonb 		return;
    121   1.2    simonb 
    122   1.2    simonb 	KDASSERT((mp->mnt_flag & MNT_RDONLY) == 0);
    123   1.2    simonb 
    124   1.2    simonb 	for (i = 0; i < wr->wr_inodescnt; i++) {
    125   1.2    simonb 		struct vnode *vp;
    126   1.2    simonb 		struct inode *ip;
    127  1.45        ad 		error = VFS_VGET(mp, wr->wr_inodes[i].wr_inumber,
    128  1.45        ad 		    LK_EXCLUSIVE, &vp);
    129   1.2    simonb 		if (error) {
    130  1.35  christos 			printf("%s: %s: unable to cleanup inode %" PRIu32 "\n",
    131  1.35  christos 			    __func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
    132   1.2    simonb 			    wr->wr_inodes[i].wr_inumber);
    133   1.2    simonb 			continue;
    134   1.2    simonb 		}
    135   1.2    simonb 		ip = VTOI(vp);
    136   1.2    simonb 		KDASSERT(wr->wr_inodes[i].wr_inumber == ip->i_number);
    137   1.6     joerg #ifdef WAPBL_DEBUG
    138  1.35  christos 		printf("%s%s: %s: cleaning inode %" PRIu64 " size=%" PRIu64
    139  1.35  christos 		    " mode=%o nlink=%d\n",
    140  1.35  christos 		    __func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
    141   1.2    simonb 		    ip->i_number, ip->i_size, ip->i_mode, ip->i_nlink);
    142   1.6     joerg #endif
    143   1.2    simonb 		KASSERT(ip->i_nlink == 0);
    144   1.2    simonb 
    145   1.2    simonb 		/*
    146   1.2    simonb 		 * The journal may have left partially allocated inodes in mode
    147  1.48    andvar 		 * zero.  This may occur if a crash occurs between the node
    148   1.2    simonb 		 * allocation in ffs_nodeallocg and when the node is properly
    149  1.47   reinoud 		 * initialized in ufs_makeinode.  If so, just deallocate them.
    150   1.2    simonb 		 */
    151   1.2    simonb 		if (ip->i_mode == 0) {
    152  1.26  dholland 			error = UFS_WAPBL_BEGIN(mp);
    153  1.26  dholland 			if (error) {
    154  1.35  christos 				printf("%s: %s: "
    155  1.26  dholland 				    "unable to cleanup inode %" PRIu32 "\n",
    156  1.35  christos 				    __func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
    157  1.26  dholland 				    wr->wr_inodes[i].wr_inumber);
    158  1.26  dholland 			} else {
    159  1.26  dholland 				ffs_vfree(vp, ip->i_number,
    160  1.26  dholland 				    wr->wr_inodes[i].wr_imode);
    161  1.26  dholland 				UFS_WAPBL_END(mp);
    162  1.26  dholland 			}
    163   1.2    simonb 		}
    164   1.2    simonb 		vput(vp);
    165   1.2    simonb 	}
    166   1.8     joerg 	wapbl_replay_stop(wr);
    167   1.2    simonb 	wapbl_replay_free(wr);
    168   1.8     joerg 	mp->mnt_wapbl_replay = NULL;
    169   1.2    simonb }
    170   1.2    simonb 
    171   1.2    simonb /* Callback for wapbl */
    172   1.2    simonb void
    173  1.34  jdolecek ffs_wapbl_sync_metadata(struct mount *mp, struct wapbl_dealloc *fdealloc)
    174   1.2    simonb {
    175   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    176   1.2    simonb 	struct fs *fs = ump->um_fs;
    177  1.34  jdolecek 	int error __diagused;
    178  1.34  jdolecek 	struct wapbl_dealloc *wd;
    179   1.2    simonb 
    180  1.43  jdolecek 	UFS_WAPBL_JLOCK_ASSERT(ump->um_mountp);
    181  1.31  jdolecek 
    182  1.36  jdolecek 	for (wd = fdealloc; wd != NULL; wd = TAILQ_NEXT(wd, wd_entries)) {
    183   1.2    simonb 		/*
    184   1.2    simonb 		 * blkfree errors are unreported, might silently fail
    185   1.2    simonb 		 * if it cannot read the cylinder group block
    186   1.2    simonb 		 */
    187   1.2    simonb 		ffs_blkfree(fs, ump->um_devvp,
    188  1.34  jdolecek 		    FFS_DBTOFSB(fs, wd->wd_blkno), wd->wd_len, -1);
    189   1.2    simonb 	}
    190   1.2    simonb 
    191  1.50   hannken 	mutex_enter(&ump->um_lock);
    192  1.31  jdolecek 	if (fs->fs_fmod != 0) {
    193  1.31  jdolecek 		fs->fs_fmod = 0;
    194  1.31  jdolecek 		fs->fs_time = time_second;
    195  1.50   hannken 		mutex_exit(&ump->um_lock);
    196  1.31  jdolecek 		error = ffs_cgupdate(ump, 0);
    197  1.32  jdolecek 		KASSERT(error == 0);
    198  1.50   hannken 	} else {
    199  1.50   hannken 		mutex_exit(&ump->um_lock);
    200  1.31  jdolecek 	}
    201   1.2    simonb }
    202   1.2    simonb 
    203   1.2    simonb void
    204  1.34  jdolecek ffs_wapbl_abort_sync_metadata(struct mount *mp, struct wapbl_dealloc *fdealloc)
    205   1.2    simonb {
    206   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    207   1.2    simonb 	struct fs *fs = ump->um_fs;
    208  1.34  jdolecek 	struct wapbl_dealloc *wd;
    209   1.2    simonb 
    210  1.36  jdolecek 	for (wd = fdealloc; wd != NULL; wd = TAILQ_NEXT(wd, wd_entries)) {
    211   1.2    simonb 		/*
    212   1.2    simonb 		 * Since the above blkfree may have failed, this blkalloc might
    213   1.2    simonb 		 * fail as well, so don't check its error.  Note that if the
    214   1.2    simonb 		 * blkfree succeeded above, then this shouldn't fail because
    215   1.2    simonb 		 * the buffer will be locked in the current transaction.
    216   1.2    simonb 		 */
    217  1.34  jdolecek 		ffs_blkalloc_ump(ump, FFS_DBTOFSB(fs, wd->wd_blkno),
    218  1.34  jdolecek 		    wd->wd_len);
    219   1.2    simonb 	}
    220   1.2    simonb }
    221   1.2    simonb 
    222   1.2    simonb static int
    223   1.2    simonb wapbl_remove_log(struct mount *mp)
    224   1.2    simonb {
    225   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    226   1.2    simonb 	struct fs *fs = ump->um_fs;
    227   1.2    simonb 	struct vnode *vp;
    228   1.2    simonb 	struct inode *ip;
    229   1.2    simonb 	ino_t log_ino;
    230   1.2    simonb 	int error;
    231   1.2    simonb 
    232   1.4    simonb 	/* If super block layout is too old to support WAPBL, return */
    233   1.4    simonb 	if (ffs_superblock_layout(fs) < 2)
    234   1.4    simonb 		return 0;
    235   1.4    simonb 
    236   1.2    simonb 	/* If all the log locators are 0, just clean up */
    237   1.2    simonb 	if (fs->fs_journallocs[0] == 0 &&
    238   1.2    simonb 	    fs->fs_journallocs[1] == 0 &&
    239   1.2    simonb 	    fs->fs_journallocs[2] == 0 &&
    240   1.2    simonb 	    fs->fs_journallocs[3] == 0) {
    241   1.2    simonb 		DPRINTF("empty locators, just clear\n");
    242   1.2    simonb 		goto done;
    243   1.2    simonb 	}
    244   1.2    simonb 
    245   1.2    simonb 	switch (fs->fs_journal_location) {
    246   1.2    simonb 	case UFS_WAPBL_JOURNALLOC_NONE:
    247   1.2    simonb 		/* nothing! */
    248   1.2    simonb 		DPRINTF("no log\n");
    249   1.2    simonb 		break;
    250   1.2    simonb 
    251   1.2    simonb 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    252   1.2    simonb 		log_ino = fs->fs_journallocs[UFS_WAPBL_INFS_INO];
    253   1.2    simonb 		DPRINTF("in-fs log, ino = %" PRId64 "\n",log_ino);
    254   1.2    simonb 
    255   1.2    simonb 		/* if no existing log inode, just clear all fields and bail */
    256   1.2    simonb 		if (log_ino == 0)
    257   1.2    simonb 			goto done;
    258  1.45        ad 		error = VFS_VGET(mp, log_ino, LK_EXCLUSIVE, &vp);
    259   1.2    simonb 		if (error != 0) {
    260  1.35  christos 			printf("%s: %s: vget failed %d\n", __func__,
    261  1.35  christos 			    fs->fs_fsmnt, error);
    262   1.2    simonb 			/* clear out log info on error */
    263   1.2    simonb 			goto done;
    264   1.2    simonb 		}
    265   1.2    simonb 		ip = VTOI(vp);
    266   1.2    simonb 		KASSERT(log_ino == ip->i_number);
    267   1.2    simonb 		if ((ip->i_flags & SF_LOG) == 0) {
    268  1.35  christos 			printf("%s: %s: try to clear non-log inode "
    269  1.35  christos 			    "%" PRId64 "\n", __func__, fs->fs_fsmnt, log_ino);
    270   1.2    simonb 			vput(vp);
    271   1.2    simonb 			/* clear out log info on error */
    272   1.2    simonb 			goto done;
    273   1.2    simonb 		}
    274   1.2    simonb 
    275   1.2    simonb 		/*
    276   1.2    simonb 		 * remove the log inode by setting its link count back
    277   1.2    simonb 		 * to zero and bail.
    278   1.2    simonb 		 */
    279   1.2    simonb 		ip->i_nlink = 0;
    280   1.2    simonb 		DIP_ASSIGN(ip, nlink, 0);
    281   1.2    simonb 		vput(vp);
    282  1.33  jdolecek 		break;
    283   1.2    simonb 
    284   1.2    simonb 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    285   1.2    simonb 		DPRINTF("end-of-partition log\n");
    286   1.2    simonb 		/* no extra work required */
    287   1.2    simonb 		break;
    288   1.2    simonb 
    289   1.2    simonb 	default:
    290  1.35  christos 		printf("%s: %s: unknown journal type %d\n", __func__,
    291  1.35  christos 		    fs->fs_fsmnt, fs->fs_journal_location);
    292  1.13    bouyer 		break;
    293   1.2    simonb 	}
    294   1.2    simonb 
    295   1.2    simonb 
    296   1.2    simonb done:
    297   1.2    simonb 	/* Clear out all previous knowledge of journal */
    298   1.2    simonb 	fs->fs_journal_version = 0;
    299   1.2    simonb 	fs->fs_journal_location = 0;
    300   1.2    simonb 	fs->fs_journal_flags = 0;
    301   1.2    simonb 	fs->fs_journallocs[0] = 0;
    302   1.2    simonb 	fs->fs_journallocs[1] = 0;
    303   1.2    simonb 	fs->fs_journallocs[2] = 0;
    304   1.2    simonb 	fs->fs_journallocs[3] = 0;
    305   1.2    simonb 	(void) ffs_sbupdate(ump, MNT_WAIT);
    306   1.2    simonb 
    307   1.2    simonb 	return 0;
    308   1.2    simonb }
    309   1.2    simonb 
    310   1.2    simonb int
    311   1.2    simonb ffs_wapbl_start(struct mount *mp)
    312   1.2    simonb {
    313   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    314   1.2    simonb 	struct fs *fs = ump->um_fs;
    315   1.2    simonb 	struct vnode *devvp = ump->um_devvp;
    316   1.2    simonb 	daddr_t off;
    317   1.2    simonb 	size_t count;
    318   1.2    simonb 	size_t blksize;
    319   1.2    simonb 	uint64_t extradata;
    320   1.2    simonb 	int error;
    321   1.2    simonb 
    322  1.11      yamt 	if (mp->mnt_wapbl == NULL) {
    323   1.2    simonb 		if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG) {
    324   1.2    simonb 			/* Clear out any existing journal file */
    325   1.2    simonb 			error = wapbl_remove_log(mp);
    326   1.2    simonb 			if (error != 0)
    327   1.2    simonb 				return error;
    328   1.2    simonb 		}
    329   1.2    simonb 
    330   1.2    simonb 		if (mp->mnt_flag & MNT_LOG) {
    331   1.2    simonb 			KDASSERT(fs->fs_ronly == 0);
    332   1.2    simonb 
    333   1.4    simonb 			/* WAPBL needs UFS2 format super block */
    334   1.4    simonb 			if (ffs_superblock_layout(fs) < 2) {
    335  1.35  christos 				printf("%s: %s: fs superblock in old format, "
    336  1.35  christos 				   "not journaling\n", __func__,
    337   1.4    simonb 				   VFSTOUFS(mp)->um_fs->fs_fsmnt);
    338   1.4    simonb 				mp->mnt_flag &= ~MNT_LOG;
    339   1.4    simonb 				return EINVAL;
    340   1.4    simonb 			}
    341   1.4    simonb 
    342  1.40  jdolecek 			error = wapbl_log_position(mp, fs, devvp, &off,
    343  1.40  jdolecek 			    &count, &blksize, &extradata);
    344  1.40  jdolecek 			if (error)
    345  1.40  jdolecek 				return error;
    346  1.40  jdolecek 
    347  1.38  jdolecek 			/*
    348  1.38  jdolecek 			 * Make sure we don't carry over any delayed write
    349  1.39  jdolecek 			 * buffers when updating to log. Need to turn off
    350  1.39  jdolecek 			 * async termporarily, to prevent ffs_sync() writes
    351  1.39  jdolecek 			 * themselves being turned into delayed writes.
    352  1.38  jdolecek 			 */
    353  1.39  jdolecek 			if (mp->mnt_flag & MNT_UPDATE) {
    354  1.39  jdolecek 				int saveflag = mp->mnt_flag & MNT_ASYNC;
    355  1.39  jdolecek 				mp->mnt_flag &= ~MNT_ASYNC;
    356  1.38  jdolecek 				ffs_sync(mp, MNT_WAIT, FSCRED);
    357  1.39  jdolecek 				mp->mnt_flag |= saveflag;
    358  1.39  jdolecek 			}
    359  1.38  jdolecek 
    360   1.2    simonb 			error = wapbl_start(&mp->mnt_wapbl, mp, devvp, off,
    361   1.2    simonb 			    count, blksize, mp->mnt_wapbl_replay,
    362   1.2    simonb 			    ffs_wapbl_sync_metadata,
    363   1.2    simonb 			    ffs_wapbl_abort_sync_metadata);
    364   1.2    simonb 			if (error)
    365   1.2    simonb 				return error;
    366   1.2    simonb 
    367   1.2    simonb 			mp->mnt_wapbl_op = &wapbl_ops;
    368   1.2    simonb 
    369   1.2    simonb #ifdef WAPBL_DEBUG
    370  1.35  christos 			printf("%s: %s: enabling logging\n", __func__,
    371  1.35  christos 			    fs->fs_fsmnt);
    372   1.2    simonb #endif
    373   1.2    simonb 
    374   1.2    simonb 			if ((fs->fs_flags & FS_DOWAPBL) == 0) {
    375  1.28  christos 				fs->fs_flags |= FS_DOWAPBL;
    376  1.27  christos 				if ((error = UFS_WAPBL_BEGIN(mp)) != 0)
    377  1.27  christos 					goto out;
    378   1.2    simonb 				error = ffs_sbupdate(ump, MNT_WAIT);
    379   1.2    simonb 				if (error) {
    380   1.2    simonb 					UFS_WAPBL_END(mp);
    381  1.27  christos 					goto out;
    382   1.2    simonb 				}
    383   1.2    simonb 				UFS_WAPBL_END(mp);
    384   1.2    simonb 				error = wapbl_flush(mp->mnt_wapbl, 1);
    385  1.27  christos 				if (error)
    386  1.27  christos 					goto out;
    387   1.2    simonb 			}
    388  1.37  jdolecek 
    389  1.37  jdolecek 			/*
    390  1.37  jdolecek 			 * XXX discard interferes with block deallocation
    391  1.37  jdolecek 			 * registration and hence log consistency
    392  1.37  jdolecek 			 */
    393  1.37  jdolecek 			if (mp->mnt_flag & MNT_DISCARD) {
    394  1.37  jdolecek 				CLR(mp->mnt_flag, MNT_DISCARD);
    395  1.37  jdolecek 				printf("%s: %s: disabling discard to preserve log consistency\n", __func__,
    396  1.37  jdolecek 				    fs->fs_fsmnt);
    397  1.37  jdolecek 
    398  1.37  jdolecek 				if (ump->um_discarddata != NULL) {
    399  1.37  jdolecek 		                	ffs_discard_finish(ump->um_discarddata,
    400  1.37  jdolecek 					    0);
    401  1.37  jdolecek 	                		ump->um_discarddata = NULL;
    402  1.37  jdolecek 				}
    403  1.37  jdolecek 			}
    404  1.37  jdolecek 
    405   1.2    simonb 		} else if (fs->fs_flags & FS_DOWAPBL) {
    406   1.2    simonb 			fs->fs_fmod = 1;
    407   1.2    simonb 			fs->fs_flags &= ~FS_DOWAPBL;
    408   1.2    simonb 		}
    409   1.2    simonb 	}
    410   1.2    simonb 
    411   1.2    simonb 	/*
    412   1.2    simonb 	 * It is recommended that you finish replay with logging enabled.
    413   1.2    simonb 	 * However, even if logging is not enabled, the remaining log
    414   1.2    simonb 	 * replay should be safely recoverable with an fsck, so perform
    415   1.2    simonb 	 * it anyway.
    416   1.2    simonb 	 */
    417   1.2    simonb 	if ((fs->fs_ronly == 0) && mp->mnt_wapbl_replay) {
    418   1.2    simonb 		int saveflag = mp->mnt_flag & MNT_RDONLY;
    419   1.2    simonb 		/*
    420   1.2    simonb 		 * Make sure MNT_RDONLY is not set so that the inode
    421   1.2    simonb 		 * cleanup in ufs_inactive will actually do its work.
    422   1.2    simonb 		 */
    423   1.2    simonb 		mp->mnt_flag &= ~MNT_RDONLY;
    424   1.2    simonb 		ffs_wapbl_replay_finish(mp);
    425   1.2    simonb 		mp->mnt_flag |= saveflag;
    426   1.2    simonb 		KASSERT(fs->fs_ronly == 0);
    427   1.2    simonb 	}
    428   1.2    simonb 
    429   1.2    simonb 	return 0;
    430  1.27  christos out:
    431  1.27  christos 	ffs_wapbl_stop(mp, MNT_FORCE);
    432  1.27  christos 	return error;
    433   1.2    simonb }
    434   1.2    simonb 
    435   1.2    simonb int
    436   1.2    simonb ffs_wapbl_stop(struct mount *mp, int force)
    437   1.2    simonb {
    438   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    439   1.2    simonb 	struct fs *fs = ump->um_fs;
    440   1.2    simonb 	int error;
    441   1.2    simonb 
    442   1.2    simonb 	if (mp->mnt_wapbl) {
    443   1.2    simonb 		KDASSERT(fs->fs_ronly == 0);
    444   1.2    simonb 
    445   1.2    simonb 		/*
    446   1.2    simonb 		 * Make sure turning off FS_DOWAPBL is only removed
    447   1.2    simonb 		 * as the only change in the final flush since otherwise
    448   1.2    simonb 		 * a transaction may reorder writes.
    449   1.2    simonb 		 */
    450   1.2    simonb 		error = wapbl_flush(mp->mnt_wapbl, 1);
    451   1.2    simonb 		if (error && !force)
    452   1.2    simonb 			return error;
    453   1.2    simonb 		if (error && force)
    454   1.2    simonb 			goto forceout;
    455   1.2    simonb 		error = UFS_WAPBL_BEGIN(mp);
    456   1.2    simonb 		if (error && !force)
    457   1.2    simonb 			return error;
    458   1.2    simonb 		if (error && force)
    459   1.2    simonb 			goto forceout;
    460   1.2    simonb 		KASSERT(fs->fs_flags & FS_DOWAPBL);
    461   1.2    simonb 
    462   1.2    simonb 		fs->fs_flags &= ~FS_DOWAPBL;
    463   1.2    simonb 		error = ffs_sbupdate(ump, MNT_WAIT);
    464   1.2    simonb 		KASSERT(error == 0);	/* XXX a bit drastic! */
    465   1.2    simonb 		UFS_WAPBL_END(mp);
    466   1.2    simonb 	forceout:
    467   1.2    simonb 		error = wapbl_stop(mp->mnt_wapbl, force);
    468   1.2    simonb 		if (error) {
    469   1.2    simonb 			KASSERT(!force);
    470   1.2    simonb 			fs->fs_flags |= FS_DOWAPBL;
    471   1.2    simonb 			return error;
    472   1.2    simonb 		}
    473   1.2    simonb 		fs->fs_flags &= ~FS_DOWAPBL; /* Repeat in case of forced error */
    474  1.11      yamt 		mp->mnt_wapbl = NULL;
    475   1.2    simonb 
    476   1.2    simonb #ifdef WAPBL_DEBUG
    477  1.35  christos 		printf("%s: %s: disabled logging\n", __func__, fs->fs_fsmnt);
    478   1.2    simonb #endif
    479   1.2    simonb 	}
    480   1.2    simonb 
    481   1.2    simonb 	return 0;
    482   1.2    simonb }
    483   1.2    simonb 
    484   1.2    simonb int
    485   1.2    simonb ffs_wapbl_replay_start(struct mount *mp, struct fs *fs, struct vnode *devvp)
    486   1.2    simonb {
    487   1.2    simonb 	int error;
    488   1.2    simonb 	daddr_t off;
    489   1.2    simonb 	size_t count;
    490   1.2    simonb 	size_t blksize;
    491   1.2    simonb 	uint64_t extradata;
    492   1.2    simonb 
    493   1.4    simonb 	/*
    494   1.4    simonb 	 * WAPBL needs UFS2 format super block, if we got here with a
    495   1.4    simonb 	 * UFS1 format super block something is amiss...
    496   1.4    simonb 	 */
    497   1.4    simonb 	if (ffs_superblock_layout(fs) < 2)
    498   1.4    simonb 		return EINVAL;
    499   1.4    simonb 
    500   1.2    simonb 	error = wapbl_log_position(mp, fs, devvp, &off, &count, &blksize,
    501   1.2    simonb 	    &extradata);
    502   1.2    simonb 
    503   1.2    simonb 	if (error)
    504   1.2    simonb 		return error;
    505   1.2    simonb 
    506   1.2    simonb 	error = wapbl_replay_start(&mp->mnt_wapbl_replay, devvp, off,
    507   1.2    simonb 		count, blksize);
    508   1.2    simonb 	if (error)
    509   1.2    simonb 		return error;
    510   1.2    simonb 
    511   1.2    simonb 	mp->mnt_wapbl_op = &wapbl_ops;
    512   1.2    simonb 
    513   1.2    simonb 	return 0;
    514   1.2    simonb }
    515   1.2    simonb 
    516   1.2    simonb /*
    517   1.2    simonb  * If the superblock doesn't already have a recorded journal location
    518   1.2    simonb  * then we allocate the journal in one of two positions:
    519   1.2    simonb  *
    520   1.2    simonb  *  - At the end of the partition after the filesystem if there's
    521   1.2    simonb  *    enough space.  "Enough space" is defined as >= 1MB of journal
    522   1.2    simonb  *    per 1GB of filesystem or 64MB, whichever is smaller.
    523   1.2    simonb  *
    524   1.2    simonb  *  - Inside the filesystem.  We try to allocate a contiguous journal
    525   1.2    simonb  *    based on the total filesystem size - the target is 1MB of journal
    526   1.2    simonb  *    per 1GB of filesystem, up to a maximum journal size of 64MB.  As
    527   1.2    simonb  *    a worst case allowing for fragmentation, we'll allocate a journal
    528   1.2    simonb  *    1/4 of the desired size but never smaller than 1MB.
    529   1.2    simonb  *
    530   1.2    simonb  *    XXX In the future if we allow for non-contiguous journal files we
    531   1.2    simonb  *    can tighten the above restrictions.
    532   1.2    simonb  *
    533   1.2    simonb  * XXX
    534   1.2    simonb  * These seems like a lot of duplication both here and in some of
    535   1.2    simonb  * the userland tools (fsck_ffs, dumpfs, tunefs) with similar
    536   1.2    simonb  * "switch (fs_journal_location)" constructs.  Can we centralise
    537   1.2    simonb  * this sort of code somehow/somewhere?
    538   1.2    simonb  */
    539   1.2    simonb static int
    540   1.2    simonb wapbl_log_position(struct mount *mp, struct fs *fs, struct vnode *devvp,
    541   1.2    simonb     daddr_t *startp, size_t *countp, size_t *blksizep, uint64_t *extradatap)
    542   1.2    simonb {
    543   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    544   1.2    simonb 	daddr_t logstart, logend, desired_logsize;
    545  1.14   mlelstv 	uint64_t numsecs;
    546  1.14   mlelstv 	unsigned secsize;
    547  1.14   mlelstv 	int error, location;
    548   1.2    simonb 
    549   1.2    simonb 	if (fs->fs_journal_version == UFS_WAPBL_VERSION) {
    550   1.2    simonb 		switch (fs->fs_journal_location) {
    551   1.2    simonb 		case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    552   1.2    simonb 			DPRINTF("found existing end-of-partition log\n");
    553   1.2    simonb 			*startp = fs->fs_journallocs[UFS_WAPBL_EPART_ADDR];
    554   1.2    simonb 			*countp = fs->fs_journallocs[UFS_WAPBL_EPART_COUNT];
    555   1.2    simonb 			*blksizep = fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
    556   1.5     pooka 			DPRINTF(" start = %" PRId64 ", size = %zu, "
    557   1.5     pooka 			    "blksize = %zu\n", *startp, *countp, *blksizep);
    558   1.2    simonb 			return 0;
    559   1.2    simonb 
    560   1.2    simonb 		case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    561   1.2    simonb 			DPRINTF("found existing in-filesystem log\n");
    562   1.2    simonb 			*startp = fs->fs_journallocs[UFS_WAPBL_INFS_ADDR];
    563   1.2    simonb 			*countp = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT];
    564   1.2    simonb 			*blksizep = fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
    565   1.5     pooka 			DPRINTF(" start = %" PRId64 ", size = %zu, "
    566   1.5     pooka 			    "blksize = %zu\n", *startp, *countp, *blksizep);
    567   1.2    simonb 			return 0;
    568   1.2    simonb 
    569   1.2    simonb 		default:
    570  1.35  christos 			printf("%s: %s: unknown journal type %d\n", __func__,
    571  1.35  christos 			    fs->fs_fsmnt, fs->fs_journal_location);
    572   1.2    simonb 			return EINVAL;
    573   1.2    simonb 		}
    574   1.2    simonb 	}
    575   1.2    simonb 
    576   1.2    simonb 	desired_logsize =
    577  1.21  dholland 	    ffs_lfragtosize(fs, fs->fs_size) / UFS_WAPBL_JOURNAL_SCALE;
    578   1.2    simonb 	DPRINTF("desired log size = %" PRId64 " kB\n", desired_logsize / 1024);
    579  1.42  riastrad 	desired_logsize = uimax(desired_logsize, UFS_WAPBL_MIN_JOURNAL_SIZE);
    580  1.42  riastrad 	desired_logsize = uimin(desired_logsize, UFS_WAPBL_MAX_JOURNAL_SIZE);
    581   1.2    simonb 	DPRINTF("adjusted desired log size = %" PRId64 " kB\n",
    582   1.2    simonb 	    desired_logsize / 1024);
    583   1.2    simonb 
    584   1.2    simonb 	/* Is there space after after filesystem on partition for log? */
    585  1.20  dholland 	logstart = FFS_FSBTODB(fs, fs->fs_size);
    586  1.14   mlelstv 	error = getdisksize(devvp, &numsecs, &secsize);
    587  1.14   mlelstv 	if (error)
    588  1.14   mlelstv 		return error;
    589  1.14   mlelstv 	KDASSERT(secsize != 0);
    590  1.14   mlelstv 	logend = btodb(numsecs * secsize);
    591   1.2    simonb 
    592  1.14   mlelstv 	if (dbtob(logend - logstart) >= desired_logsize) {
    593  1.14   mlelstv 		DPRINTF("enough space, use end-of-partition log\n");
    594   1.2    simonb 
    595  1.14   mlelstv 		location = UFS_WAPBL_JOURNALLOC_END_PARTITION;
    596  1.14   mlelstv 		*blksizep = secsize;
    597   1.2    simonb 
    598   1.2    simonb 		*startp = logstart;
    599   1.2    simonb 		*countp = (logend - logstart);
    600   1.2    simonb 		*extradatap = 0;
    601   1.2    simonb 
    602  1.15   mlelstv 		/* convert to physical block numbers */
    603  1.15   mlelstv 		*startp = dbtob(*startp) / secsize;
    604  1.15   mlelstv 		*countp = dbtob(*countp) / secsize;
    605  1.15   mlelstv 
    606   1.2    simonb 		fs->fs_journallocs[UFS_WAPBL_EPART_ADDR] = *startp;
    607   1.2    simonb 		fs->fs_journallocs[UFS_WAPBL_EPART_COUNT] = *countp;
    608   1.2    simonb 		fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ] = *blksizep;
    609   1.2    simonb 		fs->fs_journallocs[UFS_WAPBL_EPART_UNUSED] = *extradatap;
    610  1.14   mlelstv 	} else {
    611  1.14   mlelstv 		DPRINTF("end-of-partition has only %" PRId64 " free\n",
    612  1.14   mlelstv 		    logend - logstart);
    613  1.14   mlelstv 
    614  1.14   mlelstv 		location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
    615  1.14   mlelstv 		*blksizep = secsize;
    616   1.2    simonb 
    617  1.14   mlelstv 		error = wapbl_create_infs_log(mp, fs, devvp,
    618  1.14   mlelstv 		                  startp, countp, extradatap);
    619  1.14   mlelstv 		ffs_sync(mp, MNT_WAIT, FSCRED);
    620  1.14   mlelstv 
    621  1.15   mlelstv 		/* convert to physical block numbers */
    622  1.15   mlelstv 		*startp = dbtob(*startp) / secsize;
    623  1.15   mlelstv 		*countp = dbtob(*countp) / secsize;
    624  1.15   mlelstv 
    625  1.14   mlelstv 		fs->fs_journallocs[UFS_WAPBL_INFS_ADDR] = *startp;
    626  1.14   mlelstv 		fs->fs_journallocs[UFS_WAPBL_INFS_COUNT] = *countp;
    627  1.14   mlelstv 		fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = *blksizep;
    628  1.14   mlelstv 		fs->fs_journallocs[UFS_WAPBL_INFS_INO] = *extradatap;
    629   1.2    simonb 	}
    630   1.2    simonb 
    631  1.14   mlelstv 	if (error == 0) {
    632  1.14   mlelstv 		/* update superblock with log location */
    633  1.14   mlelstv 		fs->fs_journal_version = UFS_WAPBL_VERSION;
    634  1.14   mlelstv 		fs->fs_journal_location = location;
    635  1.14   mlelstv 		fs->fs_journal_flags = 0;
    636   1.2    simonb 
    637  1.14   mlelstv 		error = ffs_sbupdate(ump, MNT_WAIT);
    638  1.14   mlelstv 	}
    639   1.2    simonb 
    640   1.2    simonb 	return error;
    641   1.2    simonb }
    642   1.2    simonb 
    643   1.2    simonb /*
    644   1.2    simonb  * Try to create a journal log inside the filesystem.
    645   1.2    simonb  */
    646   1.2    simonb static int
    647   1.2    simonb wapbl_create_infs_log(struct mount *mp, struct fs *fs, struct vnode *devvp,
    648  1.14   mlelstv     daddr_t *startp, size_t *countp, uint64_t *extradatap)
    649   1.2    simonb {
    650   1.2    simonb 	struct vnode *vp, *rvp;
    651  1.29   hannken 	struct vattr va;
    652   1.2    simonb 	struct inode *ip;
    653   1.2    simonb 	int error;
    654   1.2    simonb 
    655  1.45        ad 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)) != 0)
    656   1.2    simonb 		return error;
    657   1.2    simonb 
    658  1.29   hannken 	vattr_null(&va);
    659  1.29   hannken 	va.va_type = VREG;
    660  1.29   hannken 	va.va_mode = 0;
    661  1.29   hannken 
    662  1.44   hannken 	error = vcache_new(mp, rvp, &va, NOCRED, NULL, &vp);
    663  1.29   hannken 	vput(rvp);
    664  1.29   hannken 	if (error)
    665  1.29   hannken 		return error;
    666  1.29   hannken 
    667  1.29   hannken 	error = vn_lock(vp, LK_EXCLUSIVE);
    668  1.29   hannken 	if (error) {
    669  1.29   hannken 		vrele(vp);
    670  1.29   hannken 		return error;
    671  1.17   mlelstv 	}
    672   1.2    simonb 
    673   1.2    simonb 	ip = VTOI(vp);
    674   1.2    simonb 	ip->i_flags = SF_LOG;
    675   1.2    simonb 	DIP_ASSIGN(ip, flags, ip->i_flags);
    676   1.2    simonb 	ip->i_nlink = 1;
    677   1.2    simonb 	DIP_ASSIGN(ip, nlink, 1);
    678  1.29   hannken 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    679   1.2    simonb 	ffs_update(vp, NULL, NULL, UPDATE_WAIT);
    680   1.2    simonb 
    681  1.14   mlelstv 	if ((error = wapbl_allocate_log_file(mp, vp,
    682  1.14   mlelstv 	                 startp, countp, extradatap)) != 0) {
    683   1.2    simonb 		/*
    684   1.2    simonb 		 * If we couldn't allocate the space for the log file,
    685   1.2    simonb 		 * remove the inode by setting its link count back to
    686   1.2    simonb 		 * zero and bail.
    687   1.2    simonb 		 */
    688   1.2    simonb 		ip->i_nlink = 0;
    689   1.2    simonb 		DIP_ASSIGN(ip, nlink, 0);
    690  1.41   hannken 		vput(vp);
    691   1.2    simonb 
    692   1.2    simonb 		return error;
    693   1.2    simonb 	}
    694   1.2    simonb 
    695   1.2    simonb 	/*
    696   1.2    simonb 	 * Now that we have the place-holder inode for the journal,
    697   1.2    simonb 	 * we don't need the vnode ever again.
    698   1.2    simonb 	 */
    699  1.41   hannken 	vput(vp);
    700   1.2    simonb 
    701   1.2    simonb 	return 0;
    702   1.2    simonb }
    703   1.2    simonb 
    704   1.2    simonb int
    705  1.14   mlelstv wapbl_allocate_log_file(struct mount *mp, struct vnode *vp,
    706  1.14   mlelstv     daddr_t *startp, size_t *countp, uint64_t *extradatap)
    707   1.2    simonb {
    708   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    709   1.2    simonb 	struct fs *fs = ump->um_fs;
    710   1.2    simonb 	daddr_t addr, indir_addr;
    711   1.2    simonb 	off_t logsize;
    712   1.2    simonb 	size_t size;
    713   1.2    simonb 	int error;
    714   1.2    simonb 
    715   1.2    simonb 	logsize = 0;
    716   1.2    simonb 	/* check if there's a suggested log size */
    717   1.2    simonb 	if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG &&
    718   1.2    simonb 	    fs->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM)
    719   1.2    simonb 		logsize = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT];
    720   1.2    simonb 
    721   1.2    simonb 	if (vp->v_size > 0) {
    722  1.35  christos 		printf("%s: %s: file size (%" PRId64 ") non zero\n", __func__,
    723  1.35  christos 		    fs->fs_fsmnt, vp->v_size);
    724   1.2    simonb 		return EEXIST;
    725   1.2    simonb 	}
    726   1.2    simonb 	wapbl_find_log_start(mp, vp, logsize, &addr, &indir_addr, &size);
    727   1.2    simonb 	if (addr == 0) {
    728  1.35  christos 		printf("%s: %s: log not allocated, largest extent is "
    729  1.35  christos 		    "%" PRId64 "MB\n", __func__, fs->fs_fsmnt,
    730  1.21  dholland 		    ffs_lblktosize(fs, size) / (1024 * 1024));
    731   1.2    simonb 		return ENOSPC;
    732   1.2    simonb 	}
    733   1.2    simonb 
    734  1.21  dholland 	logsize = ffs_lblktosize(fs, size);	/* final log size */
    735   1.2    simonb 
    736   1.2    simonb 	VTOI(vp)->i_ffs_first_data_blk = addr;
    737   1.2    simonb 	VTOI(vp)->i_ffs_first_indir_blk = indir_addr;
    738   1.2    simonb 
    739   1.2    simonb 	error = GOP_ALLOC(vp, 0, logsize, B_CONTIG, FSCRED);
    740   1.2    simonb 	if (error) {
    741  1.35  christos 		printf("%s: %s: GOP_ALLOC error %d\n", __func__, fs->fs_fsmnt,
    742  1.35  christos 		    error);
    743   1.2    simonb 		return error;
    744   1.2    simonb 	}
    745   1.2    simonb 
    746  1.20  dholland 	*startp     = FFS_FSBTODB(fs, addr);
    747  1.14   mlelstv 	*countp     = btodb(logsize);
    748  1.14   mlelstv 	*extradatap = VTOI(vp)->i_number;
    749   1.2    simonb 
    750  1.14   mlelstv 	return 0;
    751   1.2    simonb }
    752   1.2    simonb 
    753   1.2    simonb /*
    754   1.2    simonb  * Find a suitable location for the journal in the filesystem.
    755   1.2    simonb  *
    756   1.2    simonb  * Our strategy here is to look for a contiguous block of free space
    757   1.2    simonb  * at least "logfile" MB in size (plus room for any indirect blocks).
    758   1.2    simonb  * We start at the middle of the filesystem and check each cylinder
    759   1.2    simonb  * group working outwards.  If "logfile" MB is not available as a
    760  1.49   msaitoh  * single contiguous chunk, then return the address and size of the
    761   1.2    simonb  * largest chunk found.
    762   1.2    simonb  *
    763   1.2    simonb  * XXX
    764   1.2    simonb  * At what stage does the search fail?  Is if the largest space we could
    765   1.2    simonb  * find is less than a quarter the requested space reasonable?  If the
    766   1.2    simonb  * search fails entirely, return a block address if "0" it indicate this.
    767   1.2    simonb  */
    768   1.2    simonb static void
    769   1.2    simonb wapbl_find_log_start(struct mount *mp, struct vnode *vp, off_t logsize,
    770   1.2    simonb     daddr_t *addr, daddr_t *indir_addr, size_t *size)
    771   1.2    simonb {
    772   1.2    simonb 	struct ufsmount *ump = VFSTOUFS(mp);
    773   1.2    simonb 	struct fs *fs = ump->um_fs;
    774   1.2    simonb 	struct vnode *devvp = ump->um_devvp;
    775   1.2    simonb 	struct cg *cgp;
    776   1.2    simonb 	struct buf *bp;
    777   1.2    simonb 	uint8_t *blksfree;
    778   1.2    simonb 	daddr_t blkno, best_addr, start_addr;
    779   1.2    simonb 	daddr_t desired_blks, min_desired_blks;
    780   1.2    simonb 	daddr_t freeblks, best_blks;
    781   1.2    simonb 	int bpcg, cg, error, fixedsize, indir_blks, n, s;
    782   1.2    simonb 	const int needswap = UFS_FSNEEDSWAP(fs);
    783   1.2    simonb 
    784   1.2    simonb 	if (logsize == 0) {
    785   1.2    simonb 		fixedsize = 0;	/* We can adjust the size if tight */
    786  1.21  dholland 		logsize = ffs_lfragtosize(fs, fs->fs_dsize) /
    787   1.2    simonb 		    UFS_WAPBL_JOURNAL_SCALE;
    788   1.2    simonb 		DPRINTF("suggested log size = %" PRId64 "\n", logsize);
    789  1.42  riastrad 		logsize = uimax(logsize, UFS_WAPBL_MIN_JOURNAL_SIZE);
    790  1.42  riastrad 		logsize = uimin(logsize, UFS_WAPBL_MAX_JOURNAL_SIZE);
    791   1.2    simonb 		DPRINTF("adjusted log size = %" PRId64 "\n", logsize);
    792   1.2    simonb 	} else {
    793   1.2    simonb 		fixedsize = 1;
    794   1.2    simonb 		DPRINTF("fixed log size = %" PRId64 "\n", logsize);
    795   1.2    simonb 	}
    796   1.2    simonb 
    797   1.2    simonb 	desired_blks = logsize / fs->fs_bsize;
    798   1.2    simonb 	DPRINTF("desired blocks = %" PRId64 "\n", desired_blks);
    799   1.2    simonb 
    800   1.2    simonb 	/* add in number of indirect blocks needed */
    801   1.2    simonb 	indir_blks = 0;
    802  1.19  dholland 	if (desired_blks >= UFS_NDADDR) {
    803  1.19  dholland 		struct indir indirs[UFS_NIADDR + 2];
    804   1.2    simonb 		int num;
    805   1.2    simonb 
    806   1.2    simonb 		error = ufs_getlbns(vp, desired_blks, indirs, &num);
    807   1.2    simonb 		if (error) {
    808  1.35  christos 			printf("%s: %s:  ufs_getlbns failed, error %d!\n",
    809  1.35  christos 			    __func__, fs->fs_fsmnt, error);
    810   1.2    simonb 			goto bad;
    811   1.2    simonb 		}
    812   1.2    simonb 
    813   1.2    simonb 		switch (num) {
    814   1.2    simonb 		case 2:
    815   1.2    simonb 			indir_blks = 1;		/* 1st level indirect */
    816   1.2    simonb 			break;
    817   1.2    simonb 		case 3:
    818   1.2    simonb 			indir_blks = 1 +	/* 1st level indirect */
    819   1.2    simonb 			    1 +			/* 2nd level indirect */
    820   1.2    simonb 			    indirs[1].in_off + 1; /* extra 1st level indirect */
    821   1.2    simonb 			break;
    822   1.2    simonb 		default:
    823  1.35  christos 			printf("%s: %s: unexpected numlevels %d from "
    824  1.35  christos 			    "ufs_getlbns\n", __func__, fs->fs_fsmnt, num);
    825   1.2    simonb 			*size = 0;
    826   1.2    simonb 			goto bad;
    827   1.2    simonb 		}
    828   1.2    simonb 		desired_blks += indir_blks;
    829   1.2    simonb 	}
    830   1.2    simonb 	DPRINTF("desired blocks = %" PRId64 " (including indirect)\n",
    831   1.2    simonb 	    desired_blks);
    832   1.2    simonb 
    833   1.2    simonb 	/*
    834   1.2    simonb 	 * If a specific size wasn't requested, allow for a smaller log
    835   1.2    simonb 	 * if we're really tight for space...
    836   1.2    simonb 	 */
    837   1.2    simonb 	min_desired_blks = desired_blks;
    838   1.2    simonb 	if (!fixedsize)
    839   1.2    simonb 		min_desired_blks = desired_blks / 4;
    840   1.2    simonb 
    841   1.2    simonb 	/* Look at number of blocks per CG.  If it's too small, bail early. */
    842  1.22  dholland 	bpcg = ffs_fragstoblks(fs, fs->fs_fpg);
    843   1.2    simonb 	if (min_desired_blks > bpcg) {
    844  1.35  christos 		printf("%s: %s: cylinder group size of %" PRId64 " MB "
    845  1.35  christos 		    " is not big enough for journal\n", __func__, fs->fs_fsmnt,
    846  1.21  dholland 		    ffs_lblktosize(fs, bpcg) / (1024 * 1024));
    847   1.2    simonb 		goto bad;
    848   1.2    simonb 	}
    849   1.2    simonb 
    850   1.2    simonb 	/*
    851   1.2    simonb 	 * Start with the middle cylinder group, and search outwards in
    852   1.2    simonb 	 * both directions until we either find the requested log size
    853   1.2    simonb 	 * or reach the start/end of the file system.  If we reach the
    854   1.2    simonb 	 * start/end without finding enough space for the full requested
    855   1.2    simonb 	 * log size, use the largest extent found if it is large enough
    856   1.2    simonb 	 * to satisfy the our minimum size.
    857   1.2    simonb 	 *
    858   1.2    simonb 	 * XXX
    859   1.2    simonb 	 * Can we just use the cluster contigsum stuff (esp on UFS2)
    860   1.2    simonb 	 * here to simplify this search code?
    861   1.2    simonb 	 */
    862   1.2    simonb 	best_addr = 0;
    863   1.2    simonb 	best_blks = 0;
    864   1.2    simonb 	for (cg = fs->fs_ncg / 2, s = 0, n = 1;
    865   1.2    simonb 	    best_blks < desired_blks && cg >= 0 && cg < fs->fs_ncg;
    866   1.2    simonb 	    s++, n = -n, cg += n * s) {
    867   1.2    simonb 		DPRINTF("check cg %d of %d\n", cg, fs->fs_ncg);
    868  1.20  dholland 		error = bread(devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
    869  1.30      maxv 		    fs->fs_cgsize, 0, &bp);
    870  1.18   hannken 		if (error) {
    871  1.18   hannken 			continue;
    872  1.18   hannken 		}
    873   1.2    simonb 		cgp = (struct cg *)bp->b_data;
    874  1.18   hannken 		if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
    875   1.2    simonb 			brelse(bp, 0);
    876   1.2    simonb 			continue;
    877   1.2    simonb 		}
    878   1.2    simonb 
    879   1.2    simonb 		blksfree = cg_blksfree(cgp, needswap);
    880   1.2    simonb 
    881   1.2    simonb 		for (blkno = 0; blkno < bpcg;) {
    882   1.2    simonb 			/* look for next free block */
    883   1.2    simonb 			/* XXX use scanc() and fragtbl[] here? */
    884   1.2    simonb 			for (; blkno < bpcg - min_desired_blks; blkno++)
    885   1.2    simonb 				if (ffs_isblock(fs, blksfree, blkno))
    886   1.2    simonb 					break;
    887   1.2    simonb 
    888   1.2    simonb 			/* past end of search space in this CG? */
    889   1.2    simonb 			if (blkno >= bpcg - min_desired_blks)
    890   1.2    simonb 				break;
    891   1.2    simonb 
    892   1.2    simonb 			/* count how many free blocks in this extent */
    893   1.2    simonb 			start_addr = blkno;
    894   1.2    simonb 			for (freeblks = 0; blkno < bpcg; blkno++, freeblks++)
    895   1.2    simonb 				if (!ffs_isblock(fs, blksfree, blkno))
    896   1.2    simonb 					break;
    897   1.2    simonb 
    898   1.2    simonb 			if (freeblks > best_blks) {
    899   1.2    simonb 				best_blks = freeblks;
    900  1.22  dholland 				best_addr = ffs_blkstofrags(fs, start_addr) +
    901   1.2    simonb 				    cgbase(fs, cg);
    902   1.2    simonb 
    903   1.2    simonb 				if (freeblks >= desired_blks) {
    904   1.2    simonb 					DPRINTF("found len %" PRId64
    905   1.2    simonb 					    " at offset %" PRId64 " in gc\n",
    906   1.2    simonb 					    freeblks, start_addr);
    907   1.2    simonb 					break;
    908   1.2    simonb 				}
    909   1.2    simonb 			}
    910   1.2    simonb 		}
    911   1.2    simonb 		brelse(bp, 0);
    912   1.2    simonb 	}
    913   1.2    simonb 	DPRINTF("best found len = %" PRId64 ", wanted %" PRId64
    914   1.2    simonb 	    " at addr %" PRId64 "\n", best_blks, desired_blks, best_addr);
    915   1.2    simonb 
    916   1.2    simonb 	if (best_blks < min_desired_blks) {
    917   1.2    simonb 		*addr = 0;
    918   1.2    simonb 		*indir_addr = 0;
    919   1.2    simonb 	} else {
    920   1.2    simonb 		/* put indirect blocks at start, and data blocks after */
    921  1.22  dholland 		*addr = best_addr + ffs_blkstofrags(fs, indir_blks);
    922   1.2    simonb 		*indir_addr = best_addr;
    923   1.2    simonb 	}
    924  1.42  riastrad 	*size = uimin(desired_blks, best_blks) - indir_blks;
    925   1.2    simonb 	return;
    926   1.2    simonb 
    927   1.2    simonb bad:
    928   1.2    simonb 	*addr = 0;
    929   1.2    simonb 	*indir_addr = 0;
    930   1.2    simonb 	*size = 0;
    931   1.2    simonb 	return;
    932   1.2    simonb }
    933