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