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