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