1 /* $NetBSD: ffs_wapbl.c,v 1.9 2008/11/30 16:20:44 joerg 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.9 2008/11/30 16:20:44 joerg 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/disklabel.h> 49 #include <sys/ioctl.h> 50 #include <sys/errno.h> 51 #include <sys/kauth.h> 52 #include <sys/wapbl.h> 53 54 #include <ufs/ufs/inode.h> 55 #include <ufs/ufs/quota.h> 56 #include <ufs/ufs/ufsmount.h> 57 #include <ufs/ufs/ufs_bswap.h> 58 #include <ufs/ufs/ufs_extern.h> 59 #include <ufs/ufs/ufs_wapbl.h> 60 61 #include <ufs/ffs/fs.h> 62 #include <ufs/ffs/ffs_extern.h> 63 64 #undef WAPBL_DEBUG 65 #ifdef WAPBL_DEBUG 66 int ffs_wapbl_debug = 1; 67 #define DPRINTF(fmt, args...) \ 68 do { \ 69 if (ffs_wapbl_debug) \ 70 printf("%s:%d "fmt, __func__ , __LINE__, ##args); \ 71 } while (/* CONSTCOND */0) 72 #else 73 #define DPRINTF(fmt, args...) \ 74 do { \ 75 /* nothing */ \ 76 } while (/* CONSTCOND */0) 77 #endif 78 79 static int ffs_superblock_layout(struct fs *); 80 static int wapbl_log_position(struct mount *, struct fs *, struct vnode *, 81 daddr_t *, size_t *, size_t *, uint64_t *); 82 static int wapbl_create_infs_log(struct mount *, struct fs *, struct vnode *, 83 daddr_t *, size_t *, size_t *, uint64_t *); 84 static void wapbl_find_log_start(struct mount *, struct vnode *, off_t, 85 daddr_t *, daddr_t *, size_t *); 86 static int wapbl_remove_log(struct mount *); 87 static int wapbl_allocate_log_file(struct mount *, struct vnode *); 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_ffs_effnlink = 0; 267 ip->i_nlink = 0; 268 DIP_ASSIGN(ip, nlink, 0); 269 if (DOINGSOFTDEP(vp)) 270 softdep_change_linkcnt(ip); 271 vput(vp); 272 273 case UFS_WAPBL_JOURNALLOC_END_PARTITION: 274 DPRINTF("end-of-partition log\n"); 275 /* no extra work required */ 276 break; 277 278 default: 279 printf("ffs_wapbl: unknown journal type %d\n", 280 fs->fs_journal_location); 281 return EINVAL; 282 } 283 284 285 done: 286 /* Clear out all previous knowledge of journal */ 287 fs->fs_journal_version = 0; 288 fs->fs_journal_location = 0; 289 fs->fs_journal_flags = 0; 290 fs->fs_journallocs[0] = 0; 291 fs->fs_journallocs[1] = 0; 292 fs->fs_journallocs[2] = 0; 293 fs->fs_journallocs[3] = 0; 294 (void) ffs_sbupdate(ump, MNT_WAIT); 295 296 return 0; 297 } 298 299 int 300 ffs_wapbl_start(struct mount *mp) 301 { 302 struct ufsmount *ump = VFSTOUFS(mp); 303 struct fs *fs = ump->um_fs; 304 struct vnode *devvp = ump->um_devvp; 305 daddr_t off; 306 size_t count; 307 size_t blksize; 308 uint64_t extradata; 309 int error; 310 311 if (mp->mnt_wapbl == 0) { 312 if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG) { 313 /* Clear out any existing journal file */ 314 error = wapbl_remove_log(mp); 315 if (error != 0) 316 return error; 317 } 318 319 if (mp->mnt_flag & MNT_LOG) { 320 KDASSERT(fs->fs_ronly == 0); 321 322 /* WAPBL needs UFS2 format super block */ 323 if (ffs_superblock_layout(fs) < 2) { 324 printf("%s fs superblock in old format, " 325 "not journaling\n", 326 VFSTOUFS(mp)->um_fs->fs_fsmnt); 327 mp->mnt_flag &= ~MNT_LOG; 328 return EINVAL; 329 } 330 331 error = wapbl_log_position(mp, fs, devvp, &off, 332 &count, &blksize, &extradata); 333 if (error) 334 return error; 335 336 /* XXX any other consistancy checks here? */ 337 if (blksize != DEV_BSIZE) { 338 printf("%s: bad blocksize %zu\n", __func__, 339 blksize); 340 return EINVAL; 341 } 342 343 error = wapbl_start(&mp->mnt_wapbl, mp, devvp, off, 344 count, blksize, mp->mnt_wapbl_replay, 345 ffs_wapbl_sync_metadata, 346 ffs_wapbl_abort_sync_metadata); 347 if (error) 348 return error; 349 350 mp->mnt_wapbl_op = &wapbl_ops; 351 352 #ifdef WAPBL_DEBUG 353 printf("%s: enabling logging\n", fs->fs_fsmnt); 354 #endif 355 356 if ((fs->fs_flags & FS_DOWAPBL) == 0) { 357 UFS_WAPBL_BEGIN(mp); 358 fs->fs_flags |= FS_DOWAPBL; 359 error = ffs_sbupdate(ump, MNT_WAIT); 360 if (error) { 361 UFS_WAPBL_END(mp); 362 ffs_wapbl_stop(mp, MNT_FORCE); 363 return error; 364 } 365 UFS_WAPBL_END(mp); 366 error = wapbl_flush(mp->mnt_wapbl, 1); 367 if (error) { 368 ffs_wapbl_stop(mp, MNT_FORCE); 369 return error; 370 } 371 } 372 } else if (fs->fs_flags & FS_DOWAPBL) { 373 fs->fs_fmod = 1; 374 fs->fs_flags &= ~FS_DOWAPBL; 375 } 376 } 377 378 /* 379 * It is recommended that you finish replay with logging enabled. 380 * However, even if logging is not enabled, the remaining log 381 * replay should be safely recoverable with an fsck, so perform 382 * it anyway. 383 */ 384 if ((fs->fs_ronly == 0) && mp->mnt_wapbl_replay) { 385 int saveflag = mp->mnt_flag & MNT_RDONLY; 386 /* 387 * Make sure MNT_RDONLY is not set so that the inode 388 * cleanup in ufs_inactive will actually do its work. 389 */ 390 mp->mnt_flag &= ~MNT_RDONLY; 391 ffs_wapbl_replay_finish(mp); 392 mp->mnt_flag |= saveflag; 393 KASSERT(fs->fs_ronly == 0); 394 } 395 396 return 0; 397 } 398 399 int 400 ffs_wapbl_stop(struct mount *mp, int force) 401 { 402 struct ufsmount *ump = VFSTOUFS(mp); 403 struct fs *fs = ump->um_fs; 404 int error; 405 406 if (mp->mnt_wapbl) { 407 KDASSERT(fs->fs_ronly == 0); 408 409 /* 410 * Make sure turning off FS_DOWAPBL is only removed 411 * as the only change in the final flush since otherwise 412 * a transaction may reorder writes. 413 */ 414 error = wapbl_flush(mp->mnt_wapbl, 1); 415 if (error && !force) 416 return error; 417 if (error && force) 418 goto forceout; 419 error = UFS_WAPBL_BEGIN(mp); 420 if (error && !force) 421 return error; 422 if (error && force) 423 goto forceout; 424 KASSERT(fs->fs_flags & FS_DOWAPBL); 425 426 fs->fs_flags &= ~FS_DOWAPBL; 427 error = ffs_sbupdate(ump, MNT_WAIT); 428 KASSERT(error == 0); /* XXX a bit drastic! */ 429 UFS_WAPBL_END(mp); 430 forceout: 431 error = wapbl_stop(mp->mnt_wapbl, force); 432 if (error) { 433 KASSERT(!force); 434 fs->fs_flags |= FS_DOWAPBL; 435 return error; 436 } 437 fs->fs_flags &= ~FS_DOWAPBL; /* Repeat in case of forced error */ 438 mp->mnt_wapbl = 0; 439 440 #ifdef WAPBL_DEBUG 441 printf("%s: disabled logging\n", fs->fs_fsmnt); 442 #endif 443 } 444 445 return 0; 446 } 447 448 int 449 ffs_wapbl_replay_start(struct mount *mp, struct fs *fs, struct vnode *devvp) 450 { 451 int error; 452 daddr_t off; 453 size_t count; 454 size_t blksize; 455 uint64_t extradata; 456 457 /* 458 * WAPBL needs UFS2 format super block, if we got here with a 459 * UFS1 format super block something is amiss... 460 */ 461 if (ffs_superblock_layout(fs) < 2) 462 return EINVAL; 463 464 error = wapbl_log_position(mp, fs, devvp, &off, &count, &blksize, 465 &extradata); 466 467 if (error) 468 return error; 469 470 error = wapbl_replay_start(&mp->mnt_wapbl_replay, devvp, off, 471 count, blksize); 472 if (error) 473 return error; 474 475 mp->mnt_wapbl_op = &wapbl_ops; 476 477 return 0; 478 } 479 480 /* 481 * If the superblock doesn't already have a recorded journal location 482 * then we allocate the journal in one of two positions: 483 * 484 * - At the end of the partition after the filesystem if there's 485 * enough space. "Enough space" is defined as >= 1MB of journal 486 * per 1GB of filesystem or 64MB, whichever is smaller. 487 * 488 * - Inside the filesystem. We try to allocate a contiguous journal 489 * based on the total filesystem size - the target is 1MB of journal 490 * per 1GB of filesystem, up to a maximum journal size of 64MB. As 491 * a worst case allowing for fragmentation, we'll allocate a journal 492 * 1/4 of the desired size but never smaller than 1MB. 493 * 494 * XXX In the future if we allow for non-contiguous journal files we 495 * can tighten the above restrictions. 496 * 497 * XXX 498 * These seems like a lot of duplication both here and in some of 499 * the userland tools (fsck_ffs, dumpfs, tunefs) with similar 500 * "switch (fs_journal_location)" constructs. Can we centralise 501 * this sort of code somehow/somewhere? 502 */ 503 static int 504 wapbl_log_position(struct mount *mp, struct fs *fs, struct vnode *devvp, 505 daddr_t *startp, size_t *countp, size_t *blksizep, uint64_t *extradatap) 506 { 507 struct ufsmount *ump = VFSTOUFS(mp); 508 struct partinfo dpart; 509 daddr_t logstart, logend, desired_logsize; 510 size_t blksize; 511 int error; 512 513 if (fs->fs_journal_version == UFS_WAPBL_VERSION) { 514 switch (fs->fs_journal_location) { 515 case UFS_WAPBL_JOURNALLOC_END_PARTITION: 516 DPRINTF("found existing end-of-partition log\n"); 517 *startp = fs->fs_journallocs[UFS_WAPBL_EPART_ADDR]; 518 *countp = fs->fs_journallocs[UFS_WAPBL_EPART_COUNT]; 519 *blksizep = fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ]; 520 DPRINTF(" start = %" PRId64 ", size = %zu, " 521 "blksize = %zu\n", *startp, *countp, *blksizep); 522 return 0; 523 524 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM: 525 DPRINTF("found existing in-filesystem log\n"); 526 *startp = fs->fs_journallocs[UFS_WAPBL_INFS_ADDR]; 527 *countp = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT]; 528 *blksizep = fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ]; 529 DPRINTF(" start = %" PRId64 ", size = %zu, " 530 "blksize = %zu\n", *startp, *countp, *blksizep); 531 return 0; 532 533 default: 534 printf("ffs_wapbl: unknown journal type %d\n", 535 fs->fs_journal_location); 536 return EINVAL; 537 } 538 } 539 540 desired_logsize = 541 lfragtosize(fs, fs->fs_size) / UFS_WAPBL_JOURNAL_SCALE; 542 DPRINTF("desired log size = %" PRId64 " kB\n", desired_logsize / 1024); 543 desired_logsize = max(desired_logsize, UFS_WAPBL_MIN_JOURNAL_SIZE); 544 desired_logsize = min(desired_logsize, UFS_WAPBL_MAX_JOURNAL_SIZE); 545 DPRINTF("adjusted desired log size = %" PRId64 " kB\n", 546 desired_logsize / 1024); 547 548 /* Is there space after after filesystem on partition for log? */ 549 logstart = fsbtodb(fs, fs->fs_size); 550 error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, FSCRED); 551 if (!error) { 552 logend = dpart.part->p_size; 553 blksize = dpart.disklab->d_secsize; 554 } else { 555 struct dkwedge_info dkw; 556 error = VOP_IOCTL(devvp, DIOCGWEDGEINFO, &dkw, FREAD, FSCRED); 557 if (error) 558 return error; 559 560 blksize = DEV_BSIZE; 561 logend = dkw.dkw_size; 562 } 563 564 if ((logend - logstart) * blksize >= desired_logsize) { 565 KDASSERT(blksize != 0); 566 DPRINTF("enough space, use end-of-partition log\n"); 567 568 *startp = logstart; 569 *countp = (logend - logstart); 570 *blksizep = blksize; 571 *extradatap = 0; 572 573 /* update superblock with log location */ 574 fs->fs_journal_version = UFS_WAPBL_VERSION; 575 fs->fs_journal_location = UFS_WAPBL_JOURNALLOC_END_PARTITION; 576 fs->fs_journal_flags = 0; 577 fs->fs_journallocs[UFS_WAPBL_EPART_ADDR] = *startp; 578 fs->fs_journallocs[UFS_WAPBL_EPART_COUNT] = *countp; 579 fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ] = *blksizep; 580 fs->fs_journallocs[UFS_WAPBL_EPART_UNUSED] = *extradatap; 581 582 error = ffs_sbupdate(ump, MNT_WAIT); 583 return error; 584 } 585 DPRINTF("end-of-partition has only %" PRId64 " free\n", 586 logend - logstart); 587 588 error = wapbl_create_infs_log(mp, fs, devvp, startp, countp, blksizep, 589 extradatap); 590 591 ffs_sync(mp, 1, FSCRED); 592 593 return error; 594 } 595 596 /* 597 * Try to create a journal log inside the filesystem. 598 */ 599 static int 600 wapbl_create_infs_log(struct mount *mp, struct fs *fs, struct vnode *devvp, 601 daddr_t *startp, size_t *countp, size_t *blksizep, uint64_t *extradatap) 602 { 603 struct vnode *vp, *rvp; 604 struct inode *ip; 605 int error; 606 607 if ((error = VFS_ROOT(mp, &rvp)) != 0) 608 return error; 609 610 if ((error = UFS_VALLOC(rvp, 0 | S_IFREG, NOCRED, &vp)) != 0) { 611 vput(rvp); 612 return error; 613 } 614 vput(rvp); 615 616 vp->v_type = VREG; 617 ip = VTOI(vp); 618 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 619 ip->i_mode = 0 | IFREG; 620 DIP_ASSIGN(ip, mode, ip->i_mode); 621 ip->i_flags = SF_LOG; 622 DIP_ASSIGN(ip, flags, ip->i_flags); 623 ip->i_ffs_effnlink = 1; 624 ip->i_nlink = 1; 625 DIP_ASSIGN(ip, nlink, 1); 626 if (DOINGSOFTDEP(vp)) 627 softdep_change_linkcnt(ip); 628 ffs_update(vp, NULL, NULL, UPDATE_WAIT); 629 630 if ((error = wapbl_allocate_log_file(mp, vp)) != 0) { 631 /* 632 * If we couldn't allocate the space for the log file, 633 * remove the inode by setting its link count back to 634 * zero and bail. 635 */ 636 ip->i_ffs_effnlink = 0; 637 ip->i_nlink = 0; 638 DIP_ASSIGN(ip, nlink, 0); 639 if (DOINGSOFTDEP(vp)) 640 softdep_change_linkcnt(ip); 641 vput(vp); 642 643 return error; 644 } 645 646 /* 647 * Now that we have the place-holder inode for the journal, 648 * we don't need the vnode ever again. 649 */ 650 vput(vp); 651 652 *startp = fs->fs_journallocs[UFS_WAPBL_INFS_ADDR]; 653 *countp = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT]; 654 *blksizep = fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ]; 655 *extradatap = fs->fs_journallocs[UFS_WAPBL_INFS_INO]; 656 657 return 0; 658 } 659 660 int 661 wapbl_allocate_log_file(struct mount *mp, struct vnode *vp) 662 { 663 struct ufsmount *ump = VFSTOUFS(mp); 664 struct fs *fs = ump->um_fs; 665 daddr_t addr, indir_addr; 666 off_t logsize; 667 size_t size; 668 int error; 669 670 logsize = 0; 671 /* check if there's a suggested log size */ 672 if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG && 673 fs->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM) 674 logsize = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT]; 675 676 if (vp->v_size > 0) { 677 printf("%s: file size (%" PRId64 ") non zero\n", __func__, 678 vp->v_size); 679 return EEXIST; 680 } 681 wapbl_find_log_start(mp, vp, logsize, &addr, &indir_addr, &size); 682 if (addr == 0) { 683 printf("%s: log not allocated, largest extent is " 684 "%" PRId64 "MB\n", __func__, 685 lblktosize(fs, size) / (1024 * 1024)); 686 return ENOSPC; 687 } 688 689 logsize = lblktosize(fs, size); /* final log size */ 690 691 VTOI(vp)->i_ffs_first_data_blk = addr; 692 VTOI(vp)->i_ffs_first_indir_blk = indir_addr; 693 694 error = GOP_ALLOC(vp, 0, logsize, B_CONTIG, FSCRED); 695 if (error) { 696 printf("%s: GOP_ALLOC error %d\n", __func__, error); 697 return error; 698 } 699 700 fs->fs_journal_version = UFS_WAPBL_VERSION; 701 fs->fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM; 702 fs->fs_journal_flags = 0; 703 fs->fs_journallocs[UFS_WAPBL_INFS_ADDR] = 704 lfragtosize(fs, addr) / DEV_BSIZE; 705 fs->fs_journallocs[UFS_WAPBL_INFS_COUNT] = logsize / DEV_BSIZE; 706 fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = DEV_BSIZE; 707 fs->fs_journallocs[UFS_WAPBL_INFS_INO] = VTOI(vp)->i_number; 708 709 error = ffs_sbupdate(ump, MNT_WAIT); 710 return error; 711 } 712 713 /* 714 * Find a suitable location for the journal in the filesystem. 715 * 716 * Our strategy here is to look for a contiguous block of free space 717 * at least "logfile" MB in size (plus room for any indirect blocks). 718 * We start at the middle of the filesystem and check each cylinder 719 * group working outwards. If "logfile" MB is not available as a 720 * single contigous chunk, then return the address and size of the 721 * largest chunk found. 722 * 723 * XXX 724 * At what stage does the search fail? Is if the largest space we could 725 * find is less than a quarter the requested space reasonable? If the 726 * search fails entirely, return a block address if "0" it indicate this. 727 */ 728 static void 729 wapbl_find_log_start(struct mount *mp, struct vnode *vp, off_t logsize, 730 daddr_t *addr, daddr_t *indir_addr, size_t *size) 731 { 732 struct ufsmount *ump = VFSTOUFS(mp); 733 struct fs *fs = ump->um_fs; 734 struct vnode *devvp = ump->um_devvp; 735 struct cg *cgp; 736 struct buf *bp; 737 uint8_t *blksfree; 738 daddr_t blkno, best_addr, start_addr; 739 daddr_t desired_blks, min_desired_blks; 740 daddr_t freeblks, best_blks; 741 int bpcg, cg, error, fixedsize, indir_blks, n, s; 742 #ifdef FFS_EI 743 const int needswap = UFS_FSNEEDSWAP(fs); 744 #endif 745 746 if (logsize == 0) { 747 fixedsize = 0; /* We can adjust the size if tight */ 748 logsize = lfragtosize(fs, fs->fs_dsize) / 749 UFS_WAPBL_JOURNAL_SCALE; 750 DPRINTF("suggested log size = %" PRId64 "\n", logsize); 751 logsize = max(logsize, UFS_WAPBL_MIN_JOURNAL_SIZE); 752 logsize = min(logsize, UFS_WAPBL_MAX_JOURNAL_SIZE); 753 DPRINTF("adjusted log size = %" PRId64 "\n", logsize); 754 } else { 755 fixedsize = 1; 756 DPRINTF("fixed log size = %" PRId64 "\n", logsize); 757 } 758 759 desired_blks = logsize / fs->fs_bsize; 760 DPRINTF("desired blocks = %" PRId64 "\n", desired_blks); 761 762 /* add in number of indirect blocks needed */ 763 indir_blks = 0; 764 if (desired_blks >= NDADDR) { 765 struct indir indirs[NIADDR + 2]; 766 int num; 767 768 error = ufs_getlbns(vp, desired_blks, indirs, &num); 769 if (error) { 770 printf("%s: ufs_getlbns failed, error %d!\n", 771 __func__, error); 772 goto bad; 773 } 774 775 switch (num) { 776 case 2: 777 indir_blks = 1; /* 1st level indirect */ 778 break; 779 case 3: 780 indir_blks = 1 + /* 1st level indirect */ 781 1 + /* 2nd level indirect */ 782 indirs[1].in_off + 1; /* extra 1st level indirect */ 783 break; 784 default: 785 printf("%s: unexpected numlevels %d from ufs_getlbns\n", 786 __func__, num); 787 *size = 0; 788 goto bad; 789 } 790 desired_blks += indir_blks; 791 } 792 DPRINTF("desired blocks = %" PRId64 " (including indirect)\n", 793 desired_blks); 794 795 /* 796 * If a specific size wasn't requested, allow for a smaller log 797 * if we're really tight for space... 798 */ 799 min_desired_blks = desired_blks; 800 if (!fixedsize) 801 min_desired_blks = desired_blks / 4; 802 803 /* Look at number of blocks per CG. If it's too small, bail early. */ 804 bpcg = fragstoblks(fs, fs->fs_fpg); 805 if (min_desired_blks > bpcg) { 806 printf("ffs_wapbl: cylinder group size of %" PRId64 " MB " 807 " is not big enough for journal\n", 808 lblktosize(fs, bpcg) / (1024 * 1024)); 809 goto bad; 810 } 811 812 /* 813 * Start with the middle cylinder group, and search outwards in 814 * both directions until we either find the requested log size 815 * or reach the start/end of the file system. If we reach the 816 * start/end without finding enough space for the full requested 817 * log size, use the largest extent found if it is large enough 818 * to satisfy the our minimum size. 819 * 820 * XXX 821 * Can we just use the cluster contigsum stuff (esp on UFS2) 822 * here to simplify this search code? 823 */ 824 best_addr = 0; 825 best_blks = 0; 826 for (cg = fs->fs_ncg / 2, s = 0, n = 1; 827 best_blks < desired_blks && cg >= 0 && cg < fs->fs_ncg; 828 s++, n = -n, cg += n * s) { 829 DPRINTF("check cg %d of %d\n", cg, fs->fs_ncg); 830 error = bread(devvp, fsbtodb(fs, cgtod(fs, cg)), 831 fs->fs_cgsize, FSCRED, 0, &bp); 832 cgp = (struct cg *)bp->b_data; 833 if (error || !cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) { 834 brelse(bp, 0); 835 continue; 836 } 837 838 blksfree = cg_blksfree(cgp, needswap); 839 840 for (blkno = 0; blkno < bpcg;) { 841 /* look for next free block */ 842 /* XXX use scanc() and fragtbl[] here? */ 843 for (; blkno < bpcg - min_desired_blks; blkno++) 844 if (ffs_isblock(fs, blksfree, blkno)) 845 break; 846 847 /* past end of search space in this CG? */ 848 if (blkno >= bpcg - min_desired_blks) 849 break; 850 851 /* count how many free blocks in this extent */ 852 start_addr = blkno; 853 for (freeblks = 0; blkno < bpcg; blkno++, freeblks++) 854 if (!ffs_isblock(fs, blksfree, blkno)) 855 break; 856 857 if (freeblks > best_blks) { 858 best_blks = freeblks; 859 best_addr = blkstofrags(fs, start_addr) + 860 cgbase(fs, cg); 861 862 if (freeblks >= desired_blks) { 863 DPRINTF("found len %" PRId64 864 " at offset %" PRId64 " in gc\n", 865 freeblks, start_addr); 866 break; 867 } 868 } 869 } 870 brelse(bp, 0); 871 } 872 DPRINTF("best found len = %" PRId64 ", wanted %" PRId64 873 " at addr %" PRId64 "\n", best_blks, desired_blks, best_addr); 874 875 if (best_blks < min_desired_blks) { 876 *addr = 0; 877 *indir_addr = 0; 878 } else { 879 /* put indirect blocks at start, and data blocks after */ 880 *addr = best_addr + blkstofrags(fs, indir_blks); 881 *indir_addr = best_addr; 882 } 883 *size = min(desired_blks, best_blks) - indir_blks; 884 return; 885 886 bad: 887 *addr = 0; 888 *indir_addr = 0; 889 *size = 0; 890 return; 891 } 892