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