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