1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 2000 Marshall Kirk McKusick. All Rights Reserved. 5 * 6 * Further information about snapshots can be obtained from: 7 * 8 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 9 * 1614 Oxford Street mckusick@mckusick.com 10 * Berkeley, CA 94709-1608 +1-510-843-9542 11 * USA 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY 24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR 27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)ffs_snapshot.c 8.11 (McKusick) 7/23/00 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_quota.h" 42 43 #include <sys/param.h> 44 #include <sys/kernel.h> 45 #include <sys/systm.h> 46 #include <sys/conf.h> 47 #include <sys/gsb_crc32.h> 48 #include <sys/bio.h> 49 #include <sys/buf.h> 50 #include <sys/fcntl.h> 51 #include <sys/proc.h> 52 #include <sys/namei.h> 53 #include <sys/sched.h> 54 #include <sys/stat.h> 55 #include <sys/malloc.h> 56 #include <sys/mount.h> 57 #include <sys/resource.h> 58 #include <sys/resourcevar.h> 59 #include <sys/rwlock.h> 60 #include <sys/vnode.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_extern.h> 64 65 #include <geom/geom.h> 66 67 #include <ufs/ufs/extattr.h> 68 #include <ufs/ufs/quota.h> 69 #include <ufs/ufs/ufsmount.h> 70 #include <ufs/ufs/inode.h> 71 #include <ufs/ufs/ufs_extern.h> 72 73 #include <ufs/ffs/fs.h> 74 #include <ufs/ffs/ffs_extern.h> 75 76 #define KERNCRED thread0.td_ucred 77 78 #include "opt_ffs.h" 79 80 #ifdef NO_FFS_SNAPSHOT 81 int 82 ffs_snapshot(mp, snapfile) 83 struct mount *mp; 84 char *snapfile; 85 { 86 return (EINVAL); 87 } 88 89 int 90 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd) 91 struct fs *fs; 92 struct vnode *devvp; 93 ufs2_daddr_t bno; 94 long size; 95 ino_t inum; 96 enum vtype vtype; 97 struct workhead *wkhd; 98 { 99 return (EINVAL); 100 } 101 102 void 103 ffs_snapremove(vp) 104 struct vnode *vp; 105 { 106 } 107 108 void 109 ffs_snapshot_mount(mp) 110 struct mount *mp; 111 { 112 } 113 114 void 115 ffs_snapshot_unmount(mp) 116 struct mount *mp; 117 { 118 } 119 120 void 121 ffs_snapgone(ip) 122 struct inode *ip; 123 { 124 } 125 126 int 127 ffs_copyonwrite(devvp, bp) 128 struct vnode *devvp; 129 struct buf *bp; 130 { 131 return (EINVAL); 132 } 133 134 void 135 ffs_sync_snap(mp, waitfor) 136 struct mount *mp; 137 int waitfor; 138 { 139 } 140 141 #else 142 FEATURE(ffs_snapshot, "FFS snapshot support"); 143 144 LIST_HEAD(, snapdata) snapfree; 145 static struct mtx snapfree_lock; 146 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF); 147 148 static int cgaccount(int, struct vnode *, struct buf *, int); 149 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *, 150 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *, 151 ufs_lbn_t, int), int, int); 152 static int indiracct_ufs1(struct vnode *, struct vnode *, int, 153 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *, 154 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *, 155 ufs_lbn_t, int), int); 156 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, 157 struct fs *, ufs_lbn_t, int); 158 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, 159 struct fs *, ufs_lbn_t, int); 160 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, 161 struct fs *, ufs_lbn_t, int); 162 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *, 163 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *, 164 ufs_lbn_t, int), int, int); 165 static int indiracct_ufs2(struct vnode *, struct vnode *, int, 166 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *, 167 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *, 168 ufs_lbn_t, int), int); 169 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, 170 struct fs *, ufs_lbn_t, int); 171 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, 172 struct fs *, ufs_lbn_t, int); 173 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, 174 struct fs *, ufs_lbn_t, int); 175 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t); 176 static void try_free_snapdata(struct vnode *devvp); 177 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp); 178 static int ffs_bp_snapblk(struct vnode *, struct buf *); 179 180 /* 181 * To ensure the consistency of snapshots across crashes, we must 182 * synchronously write out copied blocks before allowing the 183 * originals to be modified. Because of the rather severe speed 184 * penalty that this imposes, the code normally only ensures 185 * persistence for the filesystem metadata contained within a 186 * snapshot. Setting the following flag allows this crash 187 * persistence to be enabled for file contents. 188 */ 189 int dopersistence = 0; 190 191 #ifdef DIAGNOSTIC 192 #include <sys/sysctl.h> 193 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, ""); 194 static int snapdebug = 0; 195 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, ""); 196 int collectsnapstats = 0; 197 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats, 198 0, ""); 199 #endif /* DIAGNOSTIC */ 200 201 /* 202 * Create a snapshot file and initialize it for the filesystem. 203 */ 204 int 205 ffs_snapshot(mp, snapfile) 206 struct mount *mp; 207 char *snapfile; 208 { 209 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist; 210 int error, cg, snaploc; 211 int i, size, len, loc; 212 ufs2_daddr_t blockno; 213 uint64_t flag; 214 char saved_nice = 0; 215 long redo = 0, snaplistsize = 0; 216 int32_t *lp; 217 void *space; 218 struct fs *copy_fs = NULL, *fs; 219 struct thread *td = curthread; 220 struct inode *ip, *xp; 221 struct buf *bp, *nbp, *ibp; 222 struct nameidata nd; 223 struct mount *wrtmp; 224 struct vattr vat; 225 struct vnode *vp, *xvp, *mvp, *devvp; 226 struct uio auio; 227 struct iovec aiov; 228 struct snapdata *sn; 229 struct ufsmount *ump; 230 #ifdef DIAGNOSTIC 231 struct timespec starttime = {0, 0}, endtime; 232 #endif 233 234 ump = VFSTOUFS(mp); 235 fs = ump->um_fs; 236 sn = NULL; 237 /* 238 * At the moment, journaled soft updates cannot support 239 * taking snapshots. 240 */ 241 if (MOUNTEDSUJ(mp)) { 242 vfs_mount_error(mp, "%s: Snapshots are not yet supported when " 243 "running with journaled soft updates", fs->fs_fsmnt); 244 return (EOPNOTSUPP); 245 } 246 MNT_ILOCK(mp); 247 flag = mp->mnt_flag; 248 MNT_IUNLOCK(mp); 249 /* 250 * Need to serialize access to snapshot code per filesystem. 251 */ 252 /* 253 * Assign a snapshot slot in the superblock. 254 */ 255 UFS_LOCK(ump); 256 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) 257 if (fs->fs_snapinum[snaploc] == 0) 258 break; 259 UFS_UNLOCK(ump); 260 if (snaploc == FSMAXSNAP) 261 return (ENOSPC); 262 /* 263 * Create the snapshot file. 264 */ 265 restart: 266 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE, UIO_SYSSPACE, 267 snapfile, td); 268 if ((error = namei(&nd)) != 0) 269 return (error); 270 if (nd.ni_vp != NULL) { 271 vput(nd.ni_vp); 272 error = EEXIST; 273 } 274 if (nd.ni_dvp->v_mount != mp) 275 error = EXDEV; 276 if (error) { 277 NDFREE(&nd, NDF_ONLY_PNBUF); 278 if (nd.ni_dvp == nd.ni_vp) 279 vrele(nd.ni_dvp); 280 else 281 vput(nd.ni_dvp); 282 return (error); 283 } 284 VATTR_NULL(&vat); 285 vat.va_type = VREG; 286 vat.va_mode = S_IRUSR; 287 vat.va_vaflags |= VA_EXCLUSIVE; 288 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp)) 289 wrtmp = NULL; 290 if (wrtmp != mp) 291 panic("ffs_snapshot: mount mismatch"); 292 vfs_rel(wrtmp); 293 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) { 294 NDFREE(&nd, NDF_ONLY_PNBUF); 295 vput(nd.ni_dvp); 296 if ((error = vn_start_write(NULL, &wrtmp, 297 V_XSLEEP | PCATCH)) != 0) 298 return (error); 299 goto restart; 300 } 301 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat); 302 if (error) { 303 VOP_VPUT_PAIR(nd.ni_dvp, NULL, true); 304 NDFREE(&nd, NDF_ONLY_PNBUF); 305 vn_finished_write(wrtmp); 306 if (error == ERELOOKUP) 307 goto restart; 308 return (error); 309 } 310 vp = nd.ni_vp; 311 vref(nd.ni_dvp); 312 VOP_VPUT_PAIR(nd.ni_dvp, &vp, false); 313 if (VN_IS_DOOMED(vp)) { 314 error = EBADF; 315 goto out; 316 } 317 vnode_create_vobject(nd.ni_vp, fs->fs_size, td); 318 vp->v_vflag |= VV_SYSTEM; 319 ip = VTOI(vp); 320 devvp = ITODEVVP(ip); 321 /* 322 * Calculate the size of the filesystem then allocate the block 323 * immediately following the last block of the filesystem that 324 * will contain the snapshot list. This operation allows us to 325 * set the size of the snapshot. 326 */ 327 numblks = howmany(fs->fs_size, fs->fs_frag); 328 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)numblks), 329 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp); 330 if (error) 331 goto out; 332 bawrite(bp); 333 ip->i_size = lblktosize(fs, (off_t)(numblks + 1)); 334 vnode_pager_setsize(vp, ip->i_size); 335 DIP_SET(ip, i_size, ip->i_size); 336 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 337 /* 338 * Preallocate critical data structures so that we can copy 339 * them in without further allocation after we suspend all 340 * operations on the filesystem. We would like to just release 341 * the allocated buffers without writing them since they will 342 * be filled in below once we are ready to go, but this upsets 343 * the soft update code, so we go ahead and write the new buffers. 344 * 345 * Allocate all indirect blocks and mark all of them as not 346 * needing to be copied. 347 */ 348 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) { 349 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno), 350 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp); 351 if (error) 352 goto out; 353 bawrite(ibp); 354 } 355 /* 356 * Allocate copies for the superblock and its summary information. 357 */ 358 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED, 359 0, &nbp); 360 if (error) 361 goto out; 362 bawrite(nbp); 363 blkno = fragstoblks(fs, fs->fs_csaddr); 364 len = howmany(fs->fs_cssize, fs->fs_bsize); 365 for (loc = 0; loc < len; loc++) { 366 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)), 367 fs->fs_bsize, KERNCRED, 0, &nbp); 368 if (error) 369 goto out; 370 bawrite(nbp); 371 } 372 /* 373 * Allocate all cylinder group blocks. 374 */ 375 for (cg = 0; cg < fs->fs_ncg; cg++) { 376 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)), 377 fs->fs_bsize, KERNCRED, 0, &nbp); 378 if (error) 379 goto out; 380 bawrite(nbp); 381 if (cg % 10 == 0) { 382 error = ffs_syncvnode(vp, MNT_WAIT, 0); 383 /* vp possibly reclaimed if unlocked */ 384 if (error != 0) 385 goto out; 386 } 387 } 388 /* 389 * Copy all the cylinder group maps. Although the 390 * filesystem is still active, we hope that only a few 391 * cylinder groups will change between now and when we 392 * suspend operations. Thus, we will be able to quickly 393 * touch up the few cylinder groups that changed during 394 * the suspension period. 395 */ 396 len = roundup2(howmany(fs->fs_ncg, NBBY), sizeof(int)); 397 space = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO); 398 UFS_LOCK(ump); 399 fs->fs_active = space; 400 UFS_UNLOCK(ump); 401 for (cg = 0; cg < fs->fs_ncg; cg++) { 402 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)), 403 fs->fs_bsize, KERNCRED, 0, &nbp); 404 if (error) 405 goto out; 406 error = cgaccount(cg, vp, nbp, 1); 407 bawrite(nbp); 408 if (cg % 10 == 0 && error == 0) 409 error = ffs_syncvnode(vp, MNT_WAIT, 0); 410 if (error) 411 goto out; 412 } 413 /* 414 * Change inode to snapshot type file. 415 */ 416 ip->i_flags |= SF_SNAPSHOT; 417 DIP_SET(ip, i_flags, ip->i_flags); 418 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 419 /* 420 * Ensure that the snapshot is completely on disk. 421 * Since we have marked it as a snapshot it is safe to 422 * unlock it as no process will be allowed to write to it. 423 */ 424 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0) 425 goto out; 426 VOP_UNLOCK(vp); 427 /* 428 * All allocations are done, so we can now snapshot the system. 429 * 430 * Recind nice scheduling while running with the filesystem suspended. 431 */ 432 if (td->td_proc->p_nice > 0) { 433 struct proc *p; 434 435 p = td->td_proc; 436 PROC_LOCK(p); 437 saved_nice = p->p_nice; 438 sched_nice(p, 0); 439 PROC_UNLOCK(p); 440 } 441 /* 442 * Suspend operation on filesystem. 443 */ 444 for (;;) { 445 vn_finished_write(wrtmp); 446 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) { 447 vn_start_write(NULL, &wrtmp, V_WAIT); 448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 449 goto out; 450 } 451 if (mp->mnt_kern_flag & MNTK_SUSPENDED) 452 break; 453 vn_start_write(NULL, &wrtmp, V_WAIT); 454 } 455 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 456 if (ip->i_effnlink == 0) { 457 error = ENOENT; /* Snapshot file unlinked */ 458 goto resumefs; 459 } 460 #ifdef DIAGNOSTIC 461 if (collectsnapstats) 462 nanotime(&starttime); 463 #endif 464 465 /* 466 * First, copy all the cylinder group maps that have changed. 467 */ 468 for (cg = 0; cg < fs->fs_ncg; cg++) { 469 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0) 470 continue; 471 redo++; 472 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)), 473 fs->fs_bsize, KERNCRED, 0, &nbp); 474 if (error) 475 goto resumefs; 476 error = cgaccount(cg, vp, nbp, 2); 477 bawrite(nbp); 478 if (error) 479 goto resumefs; 480 } 481 /* 482 * Grab a copy of the superblock and its summary information. 483 * We delay writing it until the suspension is released below. 484 */ 485 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK); 486 bcopy(fs, copy_fs, fs->fs_sbsize); 487 copy_fs->fs_si = malloc(sizeof(struct fs_summary_info), M_UFSMNT, 488 M_ZERO | M_WAITOK); 489 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) 490 copy_fs->fs_clean = 1; 491 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE; 492 if (fs->fs_sbsize < size) 493 bzero(&((char *)copy_fs)[fs->fs_sbsize], 494 size - fs->fs_sbsize); 495 size = blkroundup(fs, fs->fs_cssize); 496 if (fs->fs_contigsumsize > 0) 497 size += fs->fs_ncg * sizeof(int32_t); 498 space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 499 copy_fs->fs_csp = space; 500 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize); 501 space = (char *)space + fs->fs_cssize; 502 loc = howmany(fs->fs_cssize, fs->fs_fsize); 503 i = fs->fs_frag - loc % fs->fs_frag; 504 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize; 505 if (len > 0) { 506 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc), 507 len, KERNCRED, &bp)) != 0) { 508 brelse(bp); 509 goto resumefs; 510 } 511 bcopy(bp->b_data, space, (u_int)len); 512 space = (char *)space + len; 513 bp->b_flags |= B_INVAL | B_NOCACHE; 514 brelse(bp); 515 } 516 if (fs->fs_contigsumsize > 0) { 517 copy_fs->fs_maxcluster = lp = space; 518 for (i = 0; i < fs->fs_ncg; i++) 519 *lp++ = fs->fs_contigsumsize; 520 } 521 /* 522 * We must check for active files that have been unlinked 523 * (e.g., with a zero link count). We have to expunge all 524 * trace of these files from the snapshot so that they are 525 * not reclaimed prematurely by fsck or unnecessarily dumped. 526 * We turn off the MNTK_SUSPENDED flag to avoid a panic from 527 * spec_strategy about writing on a suspended filesystem. 528 * Note that we skip unlinked snapshot files as they will 529 * be handled separately below. 530 * 531 * We also calculate the size needed for the snapshot list. 532 * Initial number of entries is composed of: 533 * - one for each cylinder group map 534 * - one for each block used by superblock summary table 535 * - one for each snapshot inode block 536 * - one for the superblock 537 * - one for the snapshot list 538 * The direct block entries in the snapshot are always 539 * copied (see reason below). Note that the superblock and 540 * the first cylinder group will almost always be allocated 541 * in the direct blocks, but we add the slop for them in case 542 * they do not end up there. The snapshot list size may get 543 * expanded by one because of an update of an inode block for 544 * an unlinked but still open file when it is expunged. 545 * 546 * Because the direct block pointers are always copied, they 547 * are not added to the list. Instead ffs_copyonwrite() 548 * explicitly checks for them before checking the snapshot list. 549 */ 550 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) + 551 FSMAXSNAP + /* superblock */ 1 + /* snaplist */ 1; 552 MNT_ILOCK(mp); 553 mp->mnt_kern_flag &= ~MNTK_SUSPENDED; 554 MNT_IUNLOCK(mp); 555 loop: 556 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) { 557 if ((xvp->v_usecount == 0 && 558 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) || 559 xvp->v_type == VNON || 560 IS_SNAPSHOT(VTOI(xvp))) { 561 VI_UNLOCK(xvp); 562 continue; 563 } 564 /* 565 * We can skip parent directory vnode because it must have 566 * this snapshot file in it. 567 */ 568 if (xvp == nd.ni_dvp) { 569 VI_UNLOCK(xvp); 570 continue; 571 } 572 vholdl(xvp); 573 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) { 574 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 575 vdrop(xvp); 576 goto loop; 577 } 578 VI_LOCK(xvp); 579 if (xvp->v_usecount == 0 && 580 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) { 581 VI_UNLOCK(xvp); 582 VOP_UNLOCK(xvp); 583 vdrop(xvp); 584 continue; 585 } 586 VI_UNLOCK(xvp); 587 #ifdef DIAGNOSTIC 588 if (snapdebug) 589 vn_printf(xvp, "ffs_snapshot: busy vnode "); 590 #endif 591 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 && 592 vat.va_nlink > 0) { 593 VOP_UNLOCK(xvp); 594 vdrop(xvp); 595 continue; 596 } 597 xp = VTOI(xvp); 598 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) { 599 VOP_UNLOCK(xvp); 600 vdrop(xvp); 601 continue; 602 } 603 /* 604 * If there is a fragment, clear it here. 605 */ 606 blkno = 0; 607 loc = howmany(xp->i_size, fs->fs_bsize) - 1; 608 if (loc < UFS_NDADDR) { 609 len = fragroundup(fs, blkoff(fs, xp->i_size)); 610 if (len != 0 && len < fs->fs_bsize) { 611 ffs_blkfree(ump, copy_fs, vp, 612 DIP(xp, i_db[loc]), len, xp->i_number, 613 xvp->v_type, NULL, SINGLETON_KEY); 614 blkno = DIP(xp, i_db[loc]); 615 DIP_SET(xp, i_db[loc], 0); 616 } 617 } 618 snaplistsize += 1; 619 if (I_IS_UFS1(xp)) 620 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1, 621 BLK_NOCOPY, 1); 622 else 623 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2, 624 BLK_NOCOPY, 1); 625 if (blkno) 626 DIP_SET(xp, i_db[loc], blkno); 627 if (!error) 628 error = ffs_freefile(ump, copy_fs, vp, xp->i_number, 629 xp->i_mode, NULL); 630 VOP_UNLOCK(xvp); 631 vdrop(xvp); 632 if (error) { 633 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 634 goto resumefs; 635 } 636 } 637 /* 638 * Erase the journal file from the snapshot. 639 */ 640 if (fs->fs_flags & FS_SUJ) { 641 error = softdep_journal_lookup(mp, &xvp); 642 if (error) 643 goto resumefs; 644 xp = VTOI(xvp); 645 if (I_IS_UFS1(xp)) 646 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1, 647 BLK_NOCOPY, 0); 648 else 649 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2, 650 BLK_NOCOPY, 0); 651 vput(xvp); 652 } 653 /* 654 * Acquire a lock on the snapdata structure, creating it if necessary. 655 */ 656 sn = ffs_snapdata_acquire(devvp); 657 /* 658 * Change vnode to use shared snapshot lock instead of the original 659 * private lock. 660 */ 661 vp->v_vnlock = &sn->sn_lock; 662 lockmgr(&vp->v_lock, LK_RELEASE, NULL); 663 xp = TAILQ_FIRST(&sn->sn_head); 664 /* 665 * If this is the first snapshot on this filesystem, then we need 666 * to allocate the space for the list of preallocated snapshot blocks. 667 * This list will be refined below, but this preliminary one will 668 * keep us out of deadlock until the full one is ready. 669 */ 670 if (xp == NULL) { 671 snapblklist = malloc(snaplistsize * sizeof(daddr_t), 672 M_UFSMNT, M_WAITOK); 673 blkp = &snapblklist[1]; 674 *blkp++ = lblkno(fs, fs->fs_sblockloc); 675 blkno = fragstoblks(fs, fs->fs_csaddr); 676 for (cg = 0; cg < fs->fs_ncg; cg++) { 677 if (fragstoblks(fs, cgtod(fs, cg) > blkno)) 678 break; 679 *blkp++ = fragstoblks(fs, cgtod(fs, cg)); 680 } 681 len = howmany(fs->fs_cssize, fs->fs_bsize); 682 for (loc = 0; loc < len; loc++) 683 *blkp++ = blkno + loc; 684 for (; cg < fs->fs_ncg; cg++) 685 *blkp++ = fragstoblks(fs, cgtod(fs, cg)); 686 snapblklist[0] = blkp - snapblklist; 687 VI_LOCK(devvp); 688 if (sn->sn_blklist != NULL) 689 panic("ffs_snapshot: non-empty list"); 690 sn->sn_blklist = snapblklist; 691 sn->sn_listsize = blkp - snapblklist; 692 VI_UNLOCK(devvp); 693 } 694 /* 695 * Preallocate all the direct blocks in the snapshot inode so 696 * that we never have to write the inode itself to commit an 697 * update to the contents of the snapshot. Note that once 698 * created, the size of the snapshot will never change, so 699 * there will never be a need to write the inode except to 700 * update the non-integrity-critical time fields and 701 * allocated-block count. 702 */ 703 for (blockno = 0; blockno < UFS_NDADDR; blockno++) { 704 if (DIP(ip, i_db[blockno]) != 0) 705 continue; 706 error = UFS_BALLOC(vp, lblktosize(fs, blockno), 707 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp); 708 if (error) 709 goto resumefs; 710 error = readblock(vp, bp, blockno); 711 bawrite(bp); 712 if (error != 0) 713 goto resumefs; 714 } 715 /* 716 * Record snapshot inode. Since this is the newest snapshot, 717 * it must be placed at the end of the list. 718 */ 719 VI_LOCK(devvp); 720 fs->fs_snapinum[snaploc] = ip->i_number; 721 if (ip->i_nextsnap.tqe_prev != 0) 722 panic("ffs_snapshot: %ju already on list", 723 (uintmax_t)ip->i_number); 724 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap); 725 devvp->v_vflag |= VV_COPYONWRITE; 726 VI_UNLOCK(devvp); 727 resumefs: 728 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp"); 729 if (error != 0 && copy_fs != NULL) { 730 free(copy_fs->fs_csp, M_UFSMNT); 731 free(copy_fs->fs_si, M_UFSMNT); 732 free(copy_fs, M_UFSMNT); 733 copy_fs = NULL; 734 } 735 KASSERT(error != 0 || (sn != NULL && copy_fs != NULL), 736 ("missing snapshot setup parameters")); 737 /* 738 * Resume operation on filesystem. 739 */ 740 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR); 741 #ifdef DIAGNOSTIC 742 if (collectsnapstats && starttime.tv_sec > 0) { 743 nanotime(&endtime); 744 timespecsub(&endtime, &starttime, &endtime); 745 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n", 746 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec, 747 endtime.tv_nsec / 1000000, redo, fs->fs_ncg); 748 } 749 #endif 750 if (copy_fs == NULL) 751 goto out; 752 /* 753 * Copy allocation information from all the snapshots in 754 * this snapshot and then expunge them from its view. 755 */ 756 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) { 757 if (xp == ip) 758 break; 759 if (I_IS_UFS1(xp)) 760 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1, 761 BLK_SNAP, 0); 762 else 763 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2, 764 BLK_SNAP, 0); 765 if (error == 0 && xp->i_effnlink == 0) { 766 error = ffs_freefile(ump, 767 copy_fs, 768 vp, 769 xp->i_number, 770 xp->i_mode, NULL); 771 } 772 if (error) { 773 fs->fs_snapinum[snaploc] = 0; 774 goto done; 775 } 776 } 777 /* 778 * Allocate space for the full list of preallocated snapshot blocks. 779 */ 780 snapblklist = malloc(snaplistsize * sizeof(daddr_t), 781 M_UFSMNT, M_WAITOK); 782 ip->i_snapblklist = &snapblklist[1]; 783 /* 784 * Expunge the blocks used by the snapshots from the set of 785 * blocks marked as used in the snapshot bitmaps. Also, collect 786 * the list of allocated blocks in i_snapblklist. 787 */ 788 if (I_IS_UFS1(ip)) 789 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, 790 BLK_SNAP, 0); 791 else 792 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, 793 BLK_SNAP, 0); 794 if (error) { 795 fs->fs_snapinum[snaploc] = 0; 796 free(snapblklist, M_UFSMNT); 797 goto done; 798 } 799 if (snaplistsize < ip->i_snapblklist - snapblklist) 800 panic("ffs_snapshot: list too small"); 801 snaplistsize = ip->i_snapblklist - snapblklist; 802 snapblklist[0] = snaplistsize; 803 ip->i_snapblklist = 0; 804 /* 805 * Write out the list of allocated blocks to the end of the snapshot. 806 */ 807 auio.uio_iov = &aiov; 808 auio.uio_iovcnt = 1; 809 aiov.iov_base = (void *)snapblklist; 810 aiov.iov_len = snaplistsize * sizeof(daddr_t); 811 auio.uio_resid = aiov.iov_len; 812 auio.uio_offset = lblktosize(fs, (off_t)numblks); 813 auio.uio_segflg = UIO_SYSSPACE; 814 auio.uio_rw = UIO_WRITE; 815 auio.uio_td = td; 816 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) { 817 fs->fs_snapinum[snaploc] = 0; 818 free(snapblklist, M_UFSMNT); 819 goto done; 820 } 821 /* 822 * Write the superblock and its summary information 823 * to the snapshot. 824 */ 825 blkno = fragstoblks(fs, fs->fs_csaddr); 826 len = howmany(fs->fs_cssize, fs->fs_bsize); 827 space = copy_fs->fs_csp; 828 for (loc = 0; loc < len; loc++) { 829 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp); 830 if (error) { 831 fs->fs_snapinum[snaploc] = 0; 832 free(snapblklist, M_UFSMNT); 833 goto done; 834 } 835 bcopy(space, nbp->b_data, fs->fs_bsize); 836 space = (char *)space + fs->fs_bsize; 837 bawrite(nbp); 838 } 839 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize, 840 KERNCRED, &nbp); 841 if (error) { 842 brelse(nbp); 843 } else { 844 loc = blkoff(fs, fs->fs_sblockloc); 845 copy_fs->fs_fmod = 0; 846 copy_fs->fs_ckhash = ffs_calc_sbhash(copy_fs); 847 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize); 848 bawrite(nbp); 849 } 850 /* 851 * As this is the newest list, it is the most inclusive, so 852 * should replace the previous list. 853 */ 854 VI_LOCK(devvp); 855 space = sn->sn_blklist; 856 sn->sn_blklist = snapblklist; 857 sn->sn_listsize = snaplistsize; 858 VI_UNLOCK(devvp); 859 if (space != NULL) 860 free(space, M_UFSMNT); 861 done: 862 free(copy_fs->fs_csp, M_UFSMNT); 863 free(copy_fs->fs_si, M_UFSMNT); 864 free(copy_fs, M_UFSMNT); 865 copy_fs = NULL; 866 out: 867 NDFREE(&nd, NDF_ONLY_PNBUF); 868 if (saved_nice > 0) { 869 struct proc *p; 870 871 p = td->td_proc; 872 PROC_LOCK(p); 873 sched_nice(td->td_proc, saved_nice); 874 PROC_UNLOCK(td->td_proc); 875 } 876 UFS_LOCK(ump); 877 if (fs->fs_active != 0) { 878 free(fs->fs_active, M_DEVBUF); 879 fs->fs_active = 0; 880 } 881 UFS_UNLOCK(ump); 882 MNT_ILOCK(mp); 883 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA); 884 MNT_IUNLOCK(mp); 885 if (error) 886 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED); 887 (void) ffs_syncvnode(vp, MNT_WAIT, 0); 888 if (error) 889 vput(vp); 890 else 891 VOP_UNLOCK(vp); 892 vrele(nd.ni_dvp); 893 vn_finished_write(wrtmp); 894 process_deferred_inactive(mp); 895 return (error); 896 } 897 898 /* 899 * Copy a cylinder group map. All the unallocated blocks are marked 900 * BLK_NOCOPY so that the snapshot knows that it need not copy them 901 * if they are later written. If passno is one, then this is a first 902 * pass, so only setting needs to be done. If passno is 2, then this 903 * is a revision to a previous pass which must be undone as the 904 * replacement pass is done. 905 */ 906 static int 907 cgaccount(cg, vp, nbp, passno) 908 int cg; 909 struct vnode *vp; 910 struct buf *nbp; 911 int passno; 912 { 913 struct buf *bp, *ibp; 914 struct inode *ip; 915 struct cg *cgp; 916 struct fs *fs; 917 ufs2_daddr_t base, numblks; 918 int error, len, loc, indiroff; 919 920 ip = VTOI(vp); 921 fs = ITOFS(ip); 922 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, 0, &bp, &cgp)) != 0) 923 return (error); 924 UFS_LOCK(ITOUMP(ip)); 925 ACTIVESET(fs, cg); 926 /* 927 * Recomputation of summary information might not have been performed 928 * at mount time. Sync up summary information for current cylinder 929 * group while data is in memory to ensure that result of background 930 * fsck is slightly more consistent. 931 */ 932 fs->fs_cs(fs, cg) = cgp->cg_cs; 933 UFS_UNLOCK(ITOUMP(ip)); 934 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize); 935 if (fs->fs_cgsize < fs->fs_bsize) 936 bzero(&nbp->b_data[fs->fs_cgsize], 937 fs->fs_bsize - fs->fs_cgsize); 938 cgp = (struct cg *)nbp->b_data; 939 bqrelse(bp); 940 if (passno == 2) 941 nbp->b_flags |= B_VALIDSUSPWRT; 942 numblks = howmany(fs->fs_size, fs->fs_frag); 943 len = howmany(fs->fs_fpg, fs->fs_frag); 944 base = cgbase(fs, cg) / fs->fs_frag; 945 if (base + len >= numblks) 946 len = numblks - base - 1; 947 loc = 0; 948 if (base < UFS_NDADDR) { 949 for ( ; loc < UFS_NDADDR; loc++) { 950 if (ffs_isblock(fs, cg_blksfree(cgp), loc)) 951 DIP_SET(ip, i_db[loc], BLK_NOCOPY); 952 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY) 953 DIP_SET(ip, i_db[loc], 0); 954 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY) 955 panic("ffs_snapshot: lost direct block"); 956 } 957 } 958 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)), 959 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 960 if (error) { 961 goto out; 962 } 963 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs); 964 for ( ; loc < len; loc++, indiroff++) { 965 if (indiroff >= NINDIR(fs)) { 966 if (passno == 2) 967 ibp->b_flags |= B_VALIDSUSPWRT; 968 bawrite(ibp); 969 error = UFS_BALLOC(vp, 970 lblktosize(fs, (off_t)(base + loc)), 971 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 972 if (error) { 973 goto out; 974 } 975 indiroff = 0; 976 } 977 if (I_IS_UFS1(ip)) { 978 if (ffs_isblock(fs, cg_blksfree(cgp), loc)) 979 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 980 BLK_NOCOPY; 981 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data)) 982 [indiroff] == BLK_NOCOPY) 983 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0; 984 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data)) 985 [indiroff] == BLK_NOCOPY) 986 panic("ffs_snapshot: lost indirect block"); 987 continue; 988 } 989 if (ffs_isblock(fs, cg_blksfree(cgp), loc)) 990 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY; 991 else if (passno == 2 && 992 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY) 993 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0; 994 else if (passno == 1 && 995 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY) 996 panic("ffs_snapshot: lost indirect block"); 997 } 998 if (passno == 2) 999 ibp->b_flags |= B_VALIDSUSPWRT; 1000 bdwrite(ibp); 1001 out: 1002 /* 1003 * We have to calculate the crc32c here rather than just setting the 1004 * BX_CYLGRP b_xflags because the allocation of the block for the 1005 * the cylinder group map will always be a full size block (fs_bsize) 1006 * even though the cylinder group may be smaller (fs_cgsize). The 1007 * crc32c must be computed only over fs_cgsize whereas the BX_CYLGRP 1008 * flag causes it to be computed over the size of the buffer. 1009 */ 1010 if ((fs->fs_metackhash & CK_CYLGRP) != 0) { 1011 ((struct cg *)nbp->b_data)->cg_ckhash = 0; 1012 ((struct cg *)nbp->b_data)->cg_ckhash = 1013 calculate_crc32c(~0L, nbp->b_data, fs->fs_cgsize); 1014 } 1015 return (error); 1016 } 1017 1018 /* 1019 * Before expunging a snapshot inode, note all the 1020 * blocks that it claims with BLK_SNAP so that fsck will 1021 * be able to account for those blocks properly and so 1022 * that this snapshot knows that it need not copy them 1023 * if the other snapshot holding them is freed. This code 1024 * is reproduced once each for UFS1 and UFS2. 1025 */ 1026 static int 1027 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode) 1028 struct vnode *snapvp; 1029 struct inode *cancelip; 1030 struct fs *fs; 1031 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, 1032 struct fs *, ufs_lbn_t, int); 1033 int expungetype; 1034 int clearmode; 1035 { 1036 int i, error, indiroff; 1037 ufs_lbn_t lbn, rlbn; 1038 ufs2_daddr_t len, blkno, numblks, blksperindir; 1039 struct ufs1_dinode *dip; 1040 struct thread *td = curthread; 1041 struct buf *bp; 1042 1043 /* 1044 * Prepare to expunge the inode. If its inode block has not 1045 * yet been copied, then allocate and fill the copy. 1046 */ 1047 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number)); 1048 blkno = 0; 1049 if (lbn < UFS_NDADDR) { 1050 blkno = VTOI(snapvp)->i_din1->di_db[lbn]; 1051 } else { 1052 if (DOINGSOFTDEP(snapvp)) 1053 softdep_prealloc(snapvp, MNT_WAIT); 1054 td->td_pflags |= TDP_COWINPROGRESS; 1055 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn), 1056 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp); 1057 td->td_pflags &= ~TDP_COWINPROGRESS; 1058 if (error) 1059 return (error); 1060 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs); 1061 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff]; 1062 bqrelse(bp); 1063 } 1064 if (blkno != 0) { 1065 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp))) 1066 return (error); 1067 } else { 1068 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn), 1069 fs->fs_bsize, KERNCRED, 0, &bp); 1070 if (error) 1071 return (error); 1072 if ((error = readblock(snapvp, bp, lbn)) != 0) 1073 return (error); 1074 } 1075 /* 1076 * Set a snapshot inode to be a zero length file, regular files 1077 * or unlinked snapshots to be completely unallocated. 1078 */ 1079 dip = (struct ufs1_dinode *)bp->b_data + 1080 ino_to_fsbo(fs, cancelip->i_number); 1081 if (clearmode || cancelip->i_effnlink == 0) 1082 dip->di_mode = 0; 1083 dip->di_size = 0; 1084 dip->di_blocks = 0; 1085 dip->di_flags &= ~SF_SNAPSHOT; 1086 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs1_daddr_t)); 1087 bdwrite(bp); 1088 /* 1089 * Now go through and expunge all the blocks in the file 1090 * using the function requested. 1091 */ 1092 numblks = howmany(cancelip->i_size, fs->fs_bsize); 1093 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0], 1094 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype))) 1095 return (error); 1096 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0], 1097 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype))) 1098 return (error); 1099 blksperindir = 1; 1100 lbn = -UFS_NDADDR; 1101 len = numblks - UFS_NDADDR; 1102 rlbn = UFS_NDADDR; 1103 for (i = 0; len > 0 && i < UFS_NIADDR; i++) { 1104 error = indiracct_ufs1(snapvp, ITOV(cancelip), i, 1105 cancelip->i_din1->di_ib[i], lbn, rlbn, len, 1106 blksperindir, fs, acctfunc, expungetype); 1107 if (error) 1108 return (error); 1109 blksperindir *= NINDIR(fs); 1110 lbn -= blksperindir + 1; 1111 len -= blksperindir; 1112 rlbn += blksperindir; 1113 } 1114 return (0); 1115 } 1116 1117 /* 1118 * Descend an indirect block chain for vnode cancelvp accounting for all 1119 * its indirect blocks in snapvp. 1120 */ 1121 static int 1122 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks, 1123 blksperindir, fs, acctfunc, expungetype) 1124 struct vnode *snapvp; 1125 struct vnode *cancelvp; 1126 int level; 1127 ufs1_daddr_t blkno; 1128 ufs_lbn_t lbn; 1129 ufs_lbn_t rlbn; 1130 ufs_lbn_t remblks; 1131 ufs_lbn_t blksperindir; 1132 struct fs *fs; 1133 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, 1134 struct fs *, ufs_lbn_t, int); 1135 int expungetype; 1136 { 1137 int error, num, i; 1138 ufs_lbn_t subblksperindir; 1139 struct indir indirs[UFS_NIADDR + 2]; 1140 ufs1_daddr_t last, *bap; 1141 struct buf *bp; 1142 1143 if (blkno == 0) { 1144 if (expungetype == BLK_NOCOPY) 1145 return (0); 1146 panic("indiracct_ufs1: missing indir"); 1147 } 1148 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0) 1149 return (error); 1150 if (lbn != indirs[num - 1 - level].in_lbn || num < 2) 1151 panic("indiracct_ufs1: botched params"); 1152 /* 1153 * We have to expand bread here since it will deadlock looking 1154 * up the block number for any blocks that are not in the cache. 1155 */ 1156 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0); 1157 bp->b_blkno = fsbtodb(fs, blkno); 1158 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 && 1159 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) { 1160 brelse(bp); 1161 return (error); 1162 } 1163 /* 1164 * Account for the block pointers in this indirect block. 1165 */ 1166 last = howmany(remblks, blksperindir); 1167 if (last > NINDIR(fs)) 1168 last = NINDIR(fs); 1169 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK); 1170 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize); 1171 bqrelse(bp); 1172 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs, 1173 level == 0 ? rlbn : -1, expungetype); 1174 if (error || level == 0) 1175 goto out; 1176 /* 1177 * Account for the block pointers in each of the indirect blocks 1178 * in the levels below us. 1179 */ 1180 subblksperindir = blksperindir / NINDIR(fs); 1181 for (lbn++, level--, i = 0; i < last; i++) { 1182 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn, 1183 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype); 1184 if (error) 1185 goto out; 1186 rlbn += blksperindir; 1187 lbn -= blksperindir; 1188 remblks -= blksperindir; 1189 } 1190 out: 1191 free(bap, M_DEVBUF); 1192 return (error); 1193 } 1194 1195 /* 1196 * Do both snap accounting and map accounting. 1197 */ 1198 static int 1199 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype) 1200 struct vnode *vp; 1201 ufs1_daddr_t *oldblkp, *lastblkp; 1202 struct fs *fs; 1203 ufs_lbn_t lblkno; 1204 int exptype; /* BLK_SNAP or BLK_NOCOPY */ 1205 { 1206 int error; 1207 1208 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype))) 1209 return (error); 1210 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)); 1211 } 1212 1213 /* 1214 * Identify a set of blocks allocated in a snapshot inode. 1215 */ 1216 static int 1217 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype) 1218 struct vnode *vp; 1219 ufs1_daddr_t *oldblkp, *lastblkp; 1220 struct fs *fs; 1221 ufs_lbn_t lblkno; 1222 int expungetype; /* BLK_SNAP or BLK_NOCOPY */ 1223 { 1224 struct inode *ip = VTOI(vp); 1225 ufs1_daddr_t blkno, *blkp; 1226 ufs_lbn_t lbn; 1227 struct buf *ibp; 1228 int error; 1229 1230 for ( ; oldblkp < lastblkp; oldblkp++) { 1231 blkno = *oldblkp; 1232 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP) 1233 continue; 1234 lbn = fragstoblks(fs, blkno); 1235 if (lbn < UFS_NDADDR) { 1236 blkp = &ip->i_din1->di_db[lbn]; 1237 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 1238 } else { 1239 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn), 1240 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 1241 if (error) 1242 return (error); 1243 blkp = &((ufs1_daddr_t *)(ibp->b_data)) 1244 [(lbn - UFS_NDADDR) % NINDIR(fs)]; 1245 } 1246 /* 1247 * If we are expunging a snapshot vnode and we 1248 * find a block marked BLK_NOCOPY, then it is 1249 * one that has been allocated to this snapshot after 1250 * we took our current snapshot and can be ignored. 1251 */ 1252 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) { 1253 if (lbn >= UFS_NDADDR) 1254 brelse(ibp); 1255 } else { 1256 if (*blkp != 0) 1257 panic("snapacct_ufs1: bad block"); 1258 *blkp = expungetype; 1259 if (lbn >= UFS_NDADDR) 1260 bdwrite(ibp); 1261 } 1262 } 1263 return (0); 1264 } 1265 1266 /* 1267 * Account for a set of blocks allocated in a snapshot inode. 1268 */ 1269 static int 1270 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype) 1271 struct vnode *vp; 1272 ufs1_daddr_t *oldblkp, *lastblkp; 1273 struct fs *fs; 1274 ufs_lbn_t lblkno; 1275 int expungetype; 1276 { 1277 ufs1_daddr_t blkno; 1278 struct inode *ip; 1279 ino_t inum; 1280 int acctit; 1281 1282 ip = VTOI(vp); 1283 inum = ip->i_number; 1284 if (lblkno == -1) 1285 acctit = 0; 1286 else 1287 acctit = 1; 1288 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) { 1289 blkno = *oldblkp; 1290 if (blkno == 0 || blkno == BLK_NOCOPY) 1291 continue; 1292 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP) 1293 *ip->i_snapblklist++ = lblkno; 1294 if (blkno == BLK_SNAP) 1295 blkno = blkstofrags(fs, lblkno); 1296 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum, 1297 vp->v_type, NULL, SINGLETON_KEY); 1298 } 1299 return (0); 1300 } 1301 1302 /* 1303 * Before expunging a snapshot inode, note all the 1304 * blocks that it claims with BLK_SNAP so that fsck will 1305 * be able to account for those blocks properly and so 1306 * that this snapshot knows that it need not copy them 1307 * if the other snapshot holding them is freed. This code 1308 * is reproduced once each for UFS1 and UFS2. 1309 */ 1310 static int 1311 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode) 1312 struct vnode *snapvp; 1313 struct inode *cancelip; 1314 struct fs *fs; 1315 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, 1316 struct fs *, ufs_lbn_t, int); 1317 int expungetype; 1318 int clearmode; 1319 { 1320 int i, error, indiroff; 1321 ufs_lbn_t lbn, rlbn; 1322 ufs2_daddr_t len, blkno, numblks, blksperindir; 1323 struct ufs2_dinode *dip; 1324 struct thread *td = curthread; 1325 struct buf *bp; 1326 1327 /* 1328 * Prepare to expunge the inode. If its inode block has not 1329 * yet been copied, then allocate and fill the copy. 1330 */ 1331 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number)); 1332 blkno = 0; 1333 if (lbn < UFS_NDADDR) { 1334 blkno = VTOI(snapvp)->i_din2->di_db[lbn]; 1335 } else { 1336 if (DOINGSOFTDEP(snapvp)) 1337 softdep_prealloc(snapvp, MNT_WAIT); 1338 td->td_pflags |= TDP_COWINPROGRESS; 1339 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn), 1340 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp); 1341 td->td_pflags &= ~TDP_COWINPROGRESS; 1342 if (error) 1343 return (error); 1344 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs); 1345 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff]; 1346 bqrelse(bp); 1347 } 1348 if (blkno != 0) { 1349 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp))) 1350 return (error); 1351 } else { 1352 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn), 1353 fs->fs_bsize, KERNCRED, 0, &bp); 1354 if (error) 1355 return (error); 1356 if ((error = readblock(snapvp, bp, lbn)) != 0) 1357 return (error); 1358 } 1359 /* 1360 * Set a snapshot inode to be a zero length file, regular files 1361 * to be completely unallocated. 1362 */ 1363 dip = (struct ufs2_dinode *)bp->b_data + 1364 ino_to_fsbo(fs, cancelip->i_number); 1365 dip->di_size = 0; 1366 dip->di_blocks = 0; 1367 dip->di_flags &= ~SF_SNAPSHOT; 1368 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs2_daddr_t)); 1369 if (clearmode || cancelip->i_effnlink == 0) 1370 dip->di_mode = 0; 1371 else 1372 ffs_update_dinode_ckhash(fs, dip); 1373 bdwrite(bp); 1374 /* 1375 * Now go through and expunge all the blocks in the file 1376 * using the function requested. 1377 */ 1378 numblks = howmany(cancelip->i_size, fs->fs_bsize); 1379 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0], 1380 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype))) 1381 return (error); 1382 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0], 1383 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype))) 1384 return (error); 1385 blksperindir = 1; 1386 lbn = -UFS_NDADDR; 1387 len = numblks - UFS_NDADDR; 1388 rlbn = UFS_NDADDR; 1389 for (i = 0; len > 0 && i < UFS_NIADDR; i++) { 1390 error = indiracct_ufs2(snapvp, ITOV(cancelip), i, 1391 cancelip->i_din2->di_ib[i], lbn, rlbn, len, 1392 blksperindir, fs, acctfunc, expungetype); 1393 if (error) 1394 return (error); 1395 blksperindir *= NINDIR(fs); 1396 lbn -= blksperindir + 1; 1397 len -= blksperindir; 1398 rlbn += blksperindir; 1399 } 1400 return (0); 1401 } 1402 1403 /* 1404 * Descend an indirect block chain for vnode cancelvp accounting for all 1405 * its indirect blocks in snapvp. 1406 */ 1407 static int 1408 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks, 1409 blksperindir, fs, acctfunc, expungetype) 1410 struct vnode *snapvp; 1411 struct vnode *cancelvp; 1412 int level; 1413 ufs2_daddr_t blkno; 1414 ufs_lbn_t lbn; 1415 ufs_lbn_t rlbn; 1416 ufs_lbn_t remblks; 1417 ufs_lbn_t blksperindir; 1418 struct fs *fs; 1419 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, 1420 struct fs *, ufs_lbn_t, int); 1421 int expungetype; 1422 { 1423 int error, num, i; 1424 ufs_lbn_t subblksperindir; 1425 struct indir indirs[UFS_NIADDR + 2]; 1426 ufs2_daddr_t last, *bap; 1427 struct buf *bp; 1428 1429 if (blkno == 0) { 1430 if (expungetype == BLK_NOCOPY) 1431 return (0); 1432 panic("indiracct_ufs2: missing indir"); 1433 } 1434 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0) 1435 return (error); 1436 if (lbn != indirs[num - 1 - level].in_lbn || num < 2) 1437 panic("indiracct_ufs2: botched params"); 1438 /* 1439 * We have to expand bread here since it will deadlock looking 1440 * up the block number for any blocks that are not in the cache. 1441 */ 1442 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0); 1443 bp->b_blkno = fsbtodb(fs, blkno); 1444 if ((bp->b_flags & B_CACHE) == 0 && 1445 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) { 1446 brelse(bp); 1447 return (error); 1448 } 1449 /* 1450 * Account for the block pointers in this indirect block. 1451 */ 1452 last = howmany(remblks, blksperindir); 1453 if (last > NINDIR(fs)) 1454 last = NINDIR(fs); 1455 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK); 1456 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize); 1457 bqrelse(bp); 1458 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs, 1459 level == 0 ? rlbn : -1, expungetype); 1460 if (error || level == 0) 1461 goto out; 1462 /* 1463 * Account for the block pointers in each of the indirect blocks 1464 * in the levels below us. 1465 */ 1466 subblksperindir = blksperindir / NINDIR(fs); 1467 for (lbn++, level--, i = 0; i < last; i++) { 1468 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn, 1469 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype); 1470 if (error) 1471 goto out; 1472 rlbn += blksperindir; 1473 lbn -= blksperindir; 1474 remblks -= blksperindir; 1475 } 1476 out: 1477 free(bap, M_DEVBUF); 1478 return (error); 1479 } 1480 1481 /* 1482 * Do both snap accounting and map accounting. 1483 */ 1484 static int 1485 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype) 1486 struct vnode *vp; 1487 ufs2_daddr_t *oldblkp, *lastblkp; 1488 struct fs *fs; 1489 ufs_lbn_t lblkno; 1490 int exptype; /* BLK_SNAP or BLK_NOCOPY */ 1491 { 1492 int error; 1493 1494 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype))) 1495 return (error); 1496 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)); 1497 } 1498 1499 /* 1500 * Identify a set of blocks allocated in a snapshot inode. 1501 */ 1502 static int 1503 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype) 1504 struct vnode *vp; 1505 ufs2_daddr_t *oldblkp, *lastblkp; 1506 struct fs *fs; 1507 ufs_lbn_t lblkno; 1508 int expungetype; /* BLK_SNAP or BLK_NOCOPY */ 1509 { 1510 struct inode *ip = VTOI(vp); 1511 ufs2_daddr_t blkno, *blkp; 1512 ufs_lbn_t lbn; 1513 struct buf *ibp; 1514 int error; 1515 1516 for ( ; oldblkp < lastblkp; oldblkp++) { 1517 blkno = *oldblkp; 1518 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP) 1519 continue; 1520 lbn = fragstoblks(fs, blkno); 1521 if (lbn < UFS_NDADDR) { 1522 blkp = &ip->i_din2->di_db[lbn]; 1523 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 1524 } else { 1525 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn), 1526 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 1527 if (error) 1528 return (error); 1529 blkp = &((ufs2_daddr_t *)(ibp->b_data)) 1530 [(lbn - UFS_NDADDR) % NINDIR(fs)]; 1531 } 1532 /* 1533 * If we are expunging a snapshot vnode and we 1534 * find a block marked BLK_NOCOPY, then it is 1535 * one that has been allocated to this snapshot after 1536 * we took our current snapshot and can be ignored. 1537 */ 1538 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) { 1539 if (lbn >= UFS_NDADDR) 1540 brelse(ibp); 1541 } else { 1542 if (*blkp != 0) 1543 panic("snapacct_ufs2: bad block"); 1544 *blkp = expungetype; 1545 if (lbn >= UFS_NDADDR) 1546 bdwrite(ibp); 1547 } 1548 } 1549 return (0); 1550 } 1551 1552 /* 1553 * Account for a set of blocks allocated in a snapshot inode. 1554 */ 1555 static int 1556 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype) 1557 struct vnode *vp; 1558 ufs2_daddr_t *oldblkp, *lastblkp; 1559 struct fs *fs; 1560 ufs_lbn_t lblkno; 1561 int expungetype; 1562 { 1563 ufs2_daddr_t blkno; 1564 struct inode *ip; 1565 ino_t inum; 1566 int acctit; 1567 1568 ip = VTOI(vp); 1569 inum = ip->i_number; 1570 if (lblkno == -1) 1571 acctit = 0; 1572 else 1573 acctit = 1; 1574 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) { 1575 blkno = *oldblkp; 1576 if (blkno == 0 || blkno == BLK_NOCOPY) 1577 continue; 1578 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP && 1579 lblkno >= UFS_NDADDR) 1580 *ip->i_snapblklist++ = lblkno; 1581 if (blkno == BLK_SNAP) 1582 blkno = blkstofrags(fs, lblkno); 1583 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum, 1584 vp->v_type, NULL, SINGLETON_KEY); 1585 } 1586 return (0); 1587 } 1588 1589 /* 1590 * Decrement extra reference on snapshot when last name is removed. 1591 * It will not be freed until the last open reference goes away. 1592 */ 1593 void 1594 ffs_snapgone(ip) 1595 struct inode *ip; 1596 { 1597 struct inode *xp; 1598 struct fs *fs; 1599 int snaploc; 1600 struct snapdata *sn; 1601 struct ufsmount *ump; 1602 1603 /* 1604 * Find snapshot in incore list. 1605 */ 1606 xp = NULL; 1607 sn = ITODEVVP(ip)->v_rdev->si_snapdata; 1608 if (sn != NULL) 1609 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) 1610 if (xp == ip) 1611 break; 1612 if (xp != NULL) 1613 vrele(ITOV(ip)); 1614 #ifdef DIAGNOSTIC 1615 else if (snapdebug) 1616 printf("ffs_snapgone: lost snapshot vnode %ju\n", 1617 (uintmax_t)ip->i_number); 1618 #endif 1619 /* 1620 * Delete snapshot inode from superblock. Keep list dense. 1621 */ 1622 ump = ITOUMP(ip); 1623 fs = ump->um_fs; 1624 UFS_LOCK(ump); 1625 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) 1626 if (fs->fs_snapinum[snaploc] == ip->i_number) 1627 break; 1628 if (snaploc < FSMAXSNAP) { 1629 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) { 1630 if (fs->fs_snapinum[snaploc] == 0) 1631 break; 1632 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc]; 1633 } 1634 fs->fs_snapinum[snaploc - 1] = 0; 1635 } 1636 UFS_UNLOCK(ump); 1637 } 1638 1639 /* 1640 * Prepare a snapshot file for being removed. 1641 */ 1642 void 1643 ffs_snapremove(vp) 1644 struct vnode *vp; 1645 { 1646 struct inode *ip; 1647 struct vnode *devvp; 1648 struct buf *ibp; 1649 struct fs *fs; 1650 ufs2_daddr_t numblks, blkno, dblk; 1651 int error, i, last, loc; 1652 struct snapdata *sn; 1653 1654 ip = VTOI(vp); 1655 fs = ITOFS(ip); 1656 devvp = ITODEVVP(ip); 1657 /* 1658 * If active, delete from incore list (this snapshot may 1659 * already have been in the process of being deleted, so 1660 * would not have been active). 1661 * 1662 * Clear copy-on-write flag if last snapshot. 1663 */ 1664 VI_LOCK(devvp); 1665 if (ip->i_nextsnap.tqe_prev != 0) { 1666 sn = devvp->v_rdev->si_snapdata; 1667 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap); 1668 ip->i_nextsnap.tqe_prev = 0; 1669 VI_UNLOCK(devvp); 1670 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); 1671 for (i = 0; i < sn->sn_lock.lk_recurse; i++) 1672 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); 1673 KASSERT(vp->v_vnlock == &sn->sn_lock, 1674 ("ffs_snapremove: lost lock mutation")); 1675 vp->v_vnlock = &vp->v_lock; 1676 VI_LOCK(devvp); 1677 while (sn->sn_lock.lk_recurse > 0) 1678 lockmgr(&sn->sn_lock, LK_RELEASE, NULL); 1679 lockmgr(&sn->sn_lock, LK_RELEASE, NULL); 1680 try_free_snapdata(devvp); 1681 } else 1682 VI_UNLOCK(devvp); 1683 /* 1684 * Clear all BLK_NOCOPY fields. Pass any block claims to other 1685 * snapshots that want them (see ffs_snapblkfree below). 1686 */ 1687 for (blkno = 1; blkno < UFS_NDADDR; blkno++) { 1688 dblk = DIP(ip, i_db[blkno]); 1689 if (dblk == 0) 1690 continue; 1691 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP) 1692 DIP_SET(ip, i_db[blkno], 0); 1693 else if ((dblk == blkstofrags(fs, blkno) && 1694 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize, 1695 ip->i_number, vp->v_type, NULL))) { 1696 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - 1697 btodb(fs->fs_bsize)); 1698 DIP_SET(ip, i_db[blkno], 0); 1699 } 1700 } 1701 numblks = howmany(ip->i_size, fs->fs_bsize); 1702 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) { 1703 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno), 1704 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 1705 if (error) 1706 continue; 1707 if (fs->fs_size - blkno > NINDIR(fs)) 1708 last = NINDIR(fs); 1709 else 1710 last = fs->fs_size - blkno; 1711 for (loc = 0; loc < last; loc++) { 1712 if (I_IS_UFS1(ip)) { 1713 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc]; 1714 if (dblk == 0) 1715 continue; 1716 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP) 1717 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0; 1718 else if ((dblk == blkstofrags(fs, blkno) && 1719 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, 1720 fs->fs_bsize, ip->i_number, vp->v_type, 1721 NULL))) { 1722 ip->i_din1->di_blocks -= 1723 btodb(fs->fs_bsize); 1724 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0; 1725 } 1726 continue; 1727 } 1728 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc]; 1729 if (dblk == 0) 1730 continue; 1731 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP) 1732 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0; 1733 else if ((dblk == blkstofrags(fs, blkno) && 1734 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, 1735 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) { 1736 ip->i_din2->di_blocks -= btodb(fs->fs_bsize); 1737 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0; 1738 } 1739 } 1740 bawrite(ibp); 1741 } 1742 /* 1743 * Clear snapshot flag and drop reference. 1744 */ 1745 ip->i_flags &= ~SF_SNAPSHOT; 1746 DIP_SET(ip, i_flags, ip->i_flags); 1747 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 1748 /* 1749 * The dirtied indirects must be written out before 1750 * softdep_setup_freeblocks() is called. Otherwise indir_trunc() 1751 * may find indirect pointers using the magic BLK_* values. 1752 */ 1753 if (DOINGSOFTDEP(vp)) 1754 ffs_syncvnode(vp, MNT_WAIT, 0); 1755 #ifdef QUOTA 1756 /* 1757 * Reenable disk quotas for ex-snapshot file. 1758 */ 1759 if (!getinoquota(ip)) 1760 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE); 1761 #endif 1762 } 1763 1764 /* 1765 * Notification that a block is being freed. Return zero if the free 1766 * should be allowed to proceed. Return non-zero if the snapshot file 1767 * wants to claim the block. The block will be claimed if it is an 1768 * uncopied part of one of the snapshots. It will be freed if it is 1769 * either a BLK_NOCOPY or has already been copied in all of the snapshots. 1770 * If a fragment is being freed, then all snapshots that care about 1771 * it must make a copy since a snapshot file can only claim full sized 1772 * blocks. Note that if more than one snapshot file maps the block, 1773 * we can pick one at random to claim it. Since none of the snapshots 1774 * can change, we are assurred that they will all see the same unmodified 1775 * image. When deleting a snapshot file (see ffs_snapremove above), we 1776 * must push any of these claimed blocks to one of the other snapshots 1777 * that maps it. These claimed blocks are easily identified as they will 1778 * have a block number equal to their logical block number within the 1779 * snapshot. A copied block can never have this property because they 1780 * must always have been allocated from a BLK_NOCOPY location. 1781 */ 1782 int 1783 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd) 1784 struct fs *fs; 1785 struct vnode *devvp; 1786 ufs2_daddr_t bno; 1787 long size; 1788 ino_t inum; 1789 enum vtype vtype; 1790 struct workhead *wkhd; 1791 { 1792 struct buf *ibp, *cbp, *savedcbp = NULL; 1793 struct thread *td = curthread; 1794 struct inode *ip; 1795 struct vnode *vp = NULL; 1796 ufs_lbn_t lbn; 1797 ufs2_daddr_t blkno; 1798 int indiroff = 0, error = 0, claimedblk = 0; 1799 struct snapdata *sn; 1800 1801 lbn = fragstoblks(fs, bno); 1802 retry: 1803 VI_LOCK(devvp); 1804 sn = devvp->v_rdev->si_snapdata; 1805 if (sn == NULL) { 1806 VI_UNLOCK(devvp); 1807 return (0); 1808 } 1809 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL, 1810 VI_MTX(devvp)) != 0) 1811 goto retry; 1812 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) { 1813 vp = ITOV(ip); 1814 if (DOINGSOFTDEP(vp)) 1815 softdep_prealloc(vp, MNT_WAIT); 1816 /* 1817 * Lookup block being written. 1818 */ 1819 if (lbn < UFS_NDADDR) { 1820 blkno = DIP(ip, i_db[lbn]); 1821 } else { 1822 td->td_pflags |= TDP_COWINPROGRESS; 1823 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), 1824 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 1825 td->td_pflags &= ~TDP_COWINPROGRESS; 1826 if (error) 1827 break; 1828 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs); 1829 if (I_IS_UFS1(ip)) 1830 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff]; 1831 else 1832 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff]; 1833 } 1834 /* 1835 * Check to see if block needs to be copied. 1836 */ 1837 if (blkno == 0) { 1838 /* 1839 * A block that we map is being freed. If it has not 1840 * been claimed yet, we will claim or copy it (below). 1841 */ 1842 claimedblk = 1; 1843 } else if (blkno == BLK_SNAP) { 1844 /* 1845 * No previous snapshot claimed the block, 1846 * so it will be freed and become a BLK_NOCOPY 1847 * (don't care) for us. 1848 */ 1849 if (claimedblk) 1850 panic("snapblkfree: inconsistent block type"); 1851 if (lbn < UFS_NDADDR) { 1852 DIP_SET(ip, i_db[lbn], BLK_NOCOPY); 1853 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 1854 } else if (I_IS_UFS1(ip)) { 1855 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 1856 BLK_NOCOPY; 1857 bdwrite(ibp); 1858 } else { 1859 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 1860 BLK_NOCOPY; 1861 bdwrite(ibp); 1862 } 1863 continue; 1864 } else /* BLK_NOCOPY or default */ { 1865 /* 1866 * If the snapshot has already copied the block 1867 * (default), or does not care about the block, 1868 * it is not needed. 1869 */ 1870 if (lbn >= UFS_NDADDR) 1871 bqrelse(ibp); 1872 continue; 1873 } 1874 /* 1875 * If this is a full size block, we will just grab it 1876 * and assign it to the snapshot inode. Otherwise we 1877 * will proceed to copy it. See explanation for this 1878 * routine as to why only a single snapshot needs to 1879 * claim this block. 1880 */ 1881 if (size == fs->fs_bsize) { 1882 #ifdef DIAGNOSTIC 1883 if (snapdebug) 1884 printf("%s %ju lbn %jd from inum %ju\n", 1885 "Grabonremove: snapino", 1886 (uintmax_t)ip->i_number, 1887 (intmax_t)lbn, (uintmax_t)inum); 1888 #endif 1889 /* 1890 * If journaling is tracking this write we must add 1891 * the work to the inode or indirect being written. 1892 */ 1893 if (wkhd != NULL) { 1894 if (lbn < UFS_NDADDR) 1895 softdep_inode_append(ip, 1896 curthread->td_ucred, wkhd); 1897 else 1898 softdep_buf_append(ibp, wkhd); 1899 } 1900 if (lbn < UFS_NDADDR) { 1901 DIP_SET(ip, i_db[lbn], bno); 1902 } else if (I_IS_UFS1(ip)) { 1903 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno; 1904 bdwrite(ibp); 1905 } else { 1906 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno; 1907 bdwrite(ibp); 1908 } 1909 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size)); 1910 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 1911 lockmgr(vp->v_vnlock, LK_RELEASE, NULL); 1912 return (1); 1913 } 1914 if (lbn >= UFS_NDADDR) 1915 bqrelse(ibp); 1916 /* 1917 * Allocate the block into which to do the copy. Note that this 1918 * allocation will never require any additional allocations for 1919 * the snapshot inode. 1920 */ 1921 td->td_pflags |= TDP_COWINPROGRESS; 1922 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), 1923 fs->fs_bsize, KERNCRED, 0, &cbp); 1924 td->td_pflags &= ~TDP_COWINPROGRESS; 1925 if (error) 1926 break; 1927 #ifdef DIAGNOSTIC 1928 if (snapdebug) 1929 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n", 1930 "Copyonremove: snapino ", (uintmax_t)ip->i_number, 1931 (intmax_t)lbn, "for inum", (uintmax_t)inum, size, 1932 (intmax_t)cbp->b_blkno); 1933 #endif 1934 /* 1935 * If we have already read the old block contents, then 1936 * simply copy them to the new block. Note that we need 1937 * to synchronously write snapshots that have not been 1938 * unlinked, and hence will be visible after a crash, 1939 * to ensure their integrity. At a minimum we ensure the 1940 * integrity of the filesystem metadata, but use the 1941 * dopersistence sysctl-setable flag to decide on the 1942 * persistence needed for file content data. 1943 */ 1944 if (savedcbp != NULL) { 1945 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize); 1946 bawrite(cbp); 1947 if ((vtype == VDIR || dopersistence) && 1948 ip->i_effnlink > 0) 1949 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT); 1950 continue; 1951 } 1952 /* 1953 * Otherwise, read the old block contents into the buffer. 1954 */ 1955 if ((error = readblock(vp, cbp, lbn)) != 0) { 1956 bzero(cbp->b_data, fs->fs_bsize); 1957 bawrite(cbp); 1958 if ((vtype == VDIR || dopersistence) && 1959 ip->i_effnlink > 0) 1960 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT); 1961 break; 1962 } 1963 savedcbp = cbp; 1964 } 1965 /* 1966 * Note that we need to synchronously write snapshots that 1967 * have not been unlinked, and hence will be visible after 1968 * a crash, to ensure their integrity. At a minimum we 1969 * ensure the integrity of the filesystem metadata, but 1970 * use the dopersistence sysctl-setable flag to decide on 1971 * the persistence needed for file content data. 1972 */ 1973 if (savedcbp) { 1974 vp = savedcbp->b_vp; 1975 bawrite(savedcbp); 1976 if ((vtype == VDIR || dopersistence) && 1977 VTOI(vp)->i_effnlink > 0) 1978 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT); 1979 } 1980 /* 1981 * If we have been unable to allocate a block in which to do 1982 * the copy, then return non-zero so that the fragment will 1983 * not be freed. Although space will be lost, the snapshot 1984 * will stay consistent. 1985 */ 1986 if (error != 0 && wkhd != NULL) 1987 softdep_freework(wkhd); 1988 lockmgr(&sn->sn_lock, LK_RELEASE, NULL); 1989 return (error); 1990 } 1991 1992 /* 1993 * Associate snapshot files when mounting. 1994 */ 1995 void 1996 ffs_snapshot_mount(mp) 1997 struct mount *mp; 1998 { 1999 struct ufsmount *ump = VFSTOUFS(mp); 2000 struct vnode *devvp = ump->um_devvp; 2001 struct fs *fs = ump->um_fs; 2002 struct thread *td = curthread; 2003 struct snapdata *sn; 2004 struct vnode *vp; 2005 struct vnode *lastvp; 2006 struct inode *ip; 2007 struct uio auio; 2008 struct iovec aiov; 2009 void *snapblklist; 2010 char *reason; 2011 daddr_t snaplistsize; 2012 int error, snaploc, loc; 2013 2014 /* 2015 * XXX The following needs to be set before ffs_truncate or 2016 * VOP_READ can be called. 2017 */ 2018 mp->mnt_stat.f_iosize = fs->fs_bsize; 2019 /* 2020 * Process each snapshot listed in the superblock. 2021 */ 2022 vp = NULL; 2023 lastvp = NULL; 2024 sn = NULL; 2025 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) { 2026 if (fs->fs_snapinum[snaploc] == 0) 2027 break; 2028 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc], 2029 LK_EXCLUSIVE, &vp)) != 0){ 2030 printf("ffs_snapshot_mount: vget failed %d\n", error); 2031 continue; 2032 } 2033 ip = VTOI(vp); 2034 if (vp->v_type != VREG) { 2035 reason = "non-file snapshot"; 2036 } else if (!IS_SNAPSHOT(ip)) { 2037 reason = "non-snapshot"; 2038 } else if (ip->i_size == 2039 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) { 2040 reason = "old format snapshot"; 2041 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED); 2042 (void)ffs_syncvnode(vp, MNT_WAIT, 0); 2043 } else { 2044 reason = NULL; 2045 } 2046 if (reason != NULL) { 2047 printf("ffs_snapshot_mount: %s inode %d\n", 2048 reason, fs->fs_snapinum[snaploc]); 2049 vput(vp); 2050 vp = NULL; 2051 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) { 2052 if (fs->fs_snapinum[loc] == 0) 2053 break; 2054 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc]; 2055 } 2056 fs->fs_snapinum[loc - 1] = 0; 2057 snaploc--; 2058 continue; 2059 } 2060 /* 2061 * Acquire a lock on the snapdata structure, creating it if 2062 * necessary. 2063 */ 2064 sn = ffs_snapdata_acquire(devvp); 2065 /* 2066 * Change vnode to use shared snapshot lock instead of the 2067 * original private lock. 2068 */ 2069 vp->v_vnlock = &sn->sn_lock; 2070 lockmgr(&vp->v_lock, LK_RELEASE, NULL); 2071 /* 2072 * Link it onto the active snapshot list. 2073 */ 2074 VI_LOCK(devvp); 2075 if (ip->i_nextsnap.tqe_prev != 0) 2076 panic("ffs_snapshot_mount: %ju already on list", 2077 (uintmax_t)ip->i_number); 2078 else 2079 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap); 2080 vp->v_vflag |= VV_SYSTEM; 2081 VI_UNLOCK(devvp); 2082 VOP_UNLOCK(vp); 2083 lastvp = vp; 2084 } 2085 vp = lastvp; 2086 /* 2087 * No usable snapshots found. 2088 */ 2089 if (sn == NULL || vp == NULL) 2090 return; 2091 /* 2092 * Allocate the space for the block hints list. We always want to 2093 * use the list from the newest snapshot. 2094 */ 2095 auio.uio_iov = &aiov; 2096 auio.uio_iovcnt = 1; 2097 aiov.iov_base = (void *)&snaplistsize; 2098 aiov.iov_len = sizeof(snaplistsize); 2099 auio.uio_resid = aiov.iov_len; 2100 auio.uio_offset = 2101 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)); 2102 auio.uio_segflg = UIO_SYSSPACE; 2103 auio.uio_rw = UIO_READ; 2104 auio.uio_td = td; 2105 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2106 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) { 2107 printf("ffs_snapshot_mount: read_1 failed %d\n", error); 2108 VOP_UNLOCK(vp); 2109 return; 2110 } 2111 snapblklist = malloc(snaplistsize * sizeof(daddr_t), 2112 M_UFSMNT, M_WAITOK); 2113 auio.uio_iovcnt = 1; 2114 aiov.iov_base = snapblklist; 2115 aiov.iov_len = snaplistsize * sizeof (daddr_t); 2116 auio.uio_resid = aiov.iov_len; 2117 auio.uio_offset -= sizeof(snaplistsize); 2118 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) { 2119 printf("ffs_snapshot_mount: read_2 failed %d\n", error); 2120 VOP_UNLOCK(vp); 2121 free(snapblklist, M_UFSMNT); 2122 return; 2123 } 2124 VOP_UNLOCK(vp); 2125 VI_LOCK(devvp); 2126 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount"); 2127 sn->sn_listsize = snaplistsize; 2128 sn->sn_blklist = (daddr_t *)snapblklist; 2129 devvp->v_vflag |= VV_COPYONWRITE; 2130 VI_UNLOCK(devvp); 2131 } 2132 2133 /* 2134 * Disassociate snapshot files when unmounting. 2135 */ 2136 void 2137 ffs_snapshot_unmount(mp) 2138 struct mount *mp; 2139 { 2140 struct vnode *devvp = VFSTOUFS(mp)->um_devvp; 2141 struct snapdata *sn; 2142 struct inode *xp; 2143 struct vnode *vp; 2144 2145 VI_LOCK(devvp); 2146 sn = devvp->v_rdev->si_snapdata; 2147 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) { 2148 vp = ITOV(xp); 2149 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap); 2150 xp->i_nextsnap.tqe_prev = 0; 2151 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE, 2152 VI_MTX(devvp)); 2153 /* 2154 * Avoid LOR with above snapshot lock. The LK_NOWAIT should 2155 * never fail as the lock is currently unused. Rather than 2156 * panic, we recover by doing the blocking lock. 2157 */ 2158 if (lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 2159 printf("ffs_snapshot_unmount: Unexpected LK_NOWAIT " 2160 "failure\n"); 2161 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); 2162 } 2163 KASSERT(vp->v_vnlock == &sn->sn_lock, 2164 ("ffs_snapshot_unmount: lost lock mutation")); 2165 vp->v_vnlock = &vp->v_lock; 2166 lockmgr(&vp->v_lock, LK_RELEASE, NULL); 2167 lockmgr(&sn->sn_lock, LK_RELEASE, NULL); 2168 if (xp->i_effnlink > 0) 2169 vrele(vp); 2170 VI_LOCK(devvp); 2171 sn = devvp->v_rdev->si_snapdata; 2172 } 2173 try_free_snapdata(devvp); 2174 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount"); 2175 } 2176 2177 /* 2178 * Check the buffer block to be belong to device buffer that shall be 2179 * locked after snaplk. devvp shall be locked on entry, and will be 2180 * leaved locked upon exit. 2181 */ 2182 static int 2183 ffs_bp_snapblk(devvp, bp) 2184 struct vnode *devvp; 2185 struct buf *bp; 2186 { 2187 struct snapdata *sn; 2188 struct fs *fs; 2189 ufs2_daddr_t lbn, *snapblklist; 2190 int lower, upper, mid; 2191 2192 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk"); 2193 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp)); 2194 sn = devvp->v_rdev->si_snapdata; 2195 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL) 2196 return (0); 2197 fs = ITOFS(TAILQ_FIRST(&sn->sn_head)); 2198 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno)); 2199 snapblklist = sn->sn_blklist; 2200 upper = sn->sn_listsize - 1; 2201 lower = 1; 2202 while (lower <= upper) { 2203 mid = (lower + upper) / 2; 2204 if (snapblklist[mid] == lbn) 2205 break; 2206 if (snapblklist[mid] < lbn) 2207 lower = mid + 1; 2208 else 2209 upper = mid - 1; 2210 } 2211 if (lower <= upper) 2212 return (1); 2213 return (0); 2214 } 2215 2216 void 2217 ffs_bdflush(bo, bp) 2218 struct bufobj *bo; 2219 struct buf *bp; 2220 { 2221 struct thread *td; 2222 struct vnode *vp, *devvp; 2223 struct buf *nbp; 2224 int bp_bdskip; 2225 2226 if (bo->bo_dirty.bv_cnt <= dirtybufthresh) 2227 return; 2228 2229 td = curthread; 2230 vp = bp->b_vp; 2231 devvp = bo2vnode(bo); 2232 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp)); 2233 2234 VI_LOCK(devvp); 2235 bp_bdskip = ffs_bp_snapblk(devvp, bp); 2236 if (bp_bdskip) 2237 bdwriteskip++; 2238 VI_UNLOCK(devvp); 2239 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) { 2240 (void) VOP_FSYNC(vp, MNT_NOWAIT, td); 2241 altbufferflushes++; 2242 } else { 2243 BO_LOCK(bo); 2244 /* 2245 * Try to find a buffer to flush. 2246 */ 2247 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) { 2248 if ((nbp->b_vflags & BV_BKGRDINPROG) || 2249 BUF_LOCK(nbp, 2250 LK_EXCLUSIVE | LK_NOWAIT, NULL)) 2251 continue; 2252 if (bp == nbp) 2253 panic("bdwrite: found ourselves"); 2254 BO_UNLOCK(bo); 2255 /* 2256 * Don't countdeps with the bo lock 2257 * held. 2258 */ 2259 if (buf_countdeps(nbp, 0)) { 2260 BO_LOCK(bo); 2261 BUF_UNLOCK(nbp); 2262 continue; 2263 } 2264 if (bp_bdskip) { 2265 VI_LOCK(devvp); 2266 if (!ffs_bp_snapblk(vp, nbp)) { 2267 VI_UNLOCK(devvp); 2268 BO_LOCK(bo); 2269 BUF_UNLOCK(nbp); 2270 continue; 2271 } 2272 VI_UNLOCK(devvp); 2273 } 2274 if (nbp->b_flags & B_CLUSTEROK) { 2275 vfs_bio_awrite(nbp); 2276 } else { 2277 bremfree(nbp); 2278 bawrite(nbp); 2279 } 2280 dirtybufferflushes++; 2281 break; 2282 } 2283 if (nbp == NULL) 2284 BO_UNLOCK(bo); 2285 } 2286 } 2287 2288 /* 2289 * Check for need to copy block that is about to be written, 2290 * copying the block if necessary. 2291 */ 2292 int 2293 ffs_copyonwrite(devvp, bp) 2294 struct vnode *devvp; 2295 struct buf *bp; 2296 { 2297 struct snapdata *sn; 2298 struct buf *ibp, *cbp, *savedcbp = NULL; 2299 struct thread *td = curthread; 2300 struct fs *fs; 2301 struct inode *ip; 2302 struct vnode *vp = NULL; 2303 ufs2_daddr_t lbn, blkno, *snapblklist; 2304 int lower, upper, mid, indiroff, error = 0; 2305 int launched_async_io, prev_norunningbuf; 2306 long saved_runningbufspace; 2307 2308 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp))) 2309 return (0); /* Update on a snapshot file */ 2310 if (td->td_pflags & TDP_COWINPROGRESS) 2311 panic("ffs_copyonwrite: recursive call"); 2312 /* 2313 * First check to see if it is in the preallocated list. 2314 * By doing this check we avoid several potential deadlocks. 2315 */ 2316 VI_LOCK(devvp); 2317 sn = devvp->v_rdev->si_snapdata; 2318 if (sn == NULL || 2319 TAILQ_EMPTY(&sn->sn_head)) { 2320 VI_UNLOCK(devvp); 2321 return (0); /* No snapshot */ 2322 } 2323 ip = TAILQ_FIRST(&sn->sn_head); 2324 fs = ITOFS(ip); 2325 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno)); 2326 if (lbn < UFS_NDADDR) { 2327 VI_UNLOCK(devvp); 2328 return (0); /* Direct blocks are always copied */ 2329 } 2330 snapblklist = sn->sn_blklist; 2331 upper = sn->sn_listsize - 1; 2332 lower = 1; 2333 while (lower <= upper) { 2334 mid = (lower + upper) / 2; 2335 if (snapblklist[mid] == lbn) 2336 break; 2337 if (snapblklist[mid] < lbn) 2338 lower = mid + 1; 2339 else 2340 upper = mid - 1; 2341 } 2342 if (lower <= upper) { 2343 VI_UNLOCK(devvp); 2344 return (0); 2345 } 2346 launched_async_io = 0; 2347 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF; 2348 /* 2349 * Since I/O on bp isn't yet in progress and it may be blocked 2350 * for a long time waiting on snaplk, back it out of 2351 * runningbufspace, possibly waking other threads waiting for space. 2352 */ 2353 saved_runningbufspace = bp->b_runningbufspace; 2354 if (saved_runningbufspace != 0) 2355 runningbufwakeup(bp); 2356 /* 2357 * Not in the precomputed list, so check the snapshots. 2358 */ 2359 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL, 2360 VI_MTX(devvp)) != 0) { 2361 VI_LOCK(devvp); 2362 sn = devvp->v_rdev->si_snapdata; 2363 if (sn == NULL || 2364 TAILQ_EMPTY(&sn->sn_head)) { 2365 VI_UNLOCK(devvp); 2366 if (saved_runningbufspace != 0) { 2367 bp->b_runningbufspace = saved_runningbufspace; 2368 atomic_add_long(&runningbufspace, 2369 bp->b_runningbufspace); 2370 } 2371 return (0); /* Snapshot gone */ 2372 } 2373 } 2374 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) { 2375 vp = ITOV(ip); 2376 if (DOINGSOFTDEP(vp)) 2377 softdep_prealloc(vp, MNT_WAIT); 2378 /* 2379 * We ensure that everything of our own that needs to be 2380 * copied will be done at the time that ffs_snapshot is 2381 * called. Thus we can skip the check here which can 2382 * deadlock in doing the lookup in UFS_BALLOC. 2383 */ 2384 if (bp->b_vp == vp) 2385 continue; 2386 /* 2387 * Check to see if block needs to be copied. We do not have 2388 * to hold the snapshot lock while doing this lookup as it 2389 * will never require any additional allocations for the 2390 * snapshot inode. 2391 */ 2392 if (lbn < UFS_NDADDR) { 2393 blkno = DIP(ip, i_db[lbn]); 2394 } else { 2395 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF; 2396 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), 2397 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp); 2398 td->td_pflags &= ~TDP_COWINPROGRESS; 2399 if (error) 2400 break; 2401 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs); 2402 if (I_IS_UFS1(ip)) 2403 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff]; 2404 else 2405 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff]; 2406 bqrelse(ibp); 2407 } 2408 #ifdef INVARIANTS 2409 if (blkno == BLK_SNAP && bp->b_lblkno >= 0) 2410 panic("ffs_copyonwrite: bad copy block"); 2411 #endif 2412 if (blkno != 0) 2413 continue; 2414 /* 2415 * Allocate the block into which to do the copy. Since 2416 * multiple processes may all try to copy the same block, 2417 * we have to recheck our need to do a copy if we sleep 2418 * waiting for the lock. 2419 * 2420 * Because all snapshots on a filesystem share a single 2421 * lock, we ensure that we will never be in competition 2422 * with another process to allocate a block. 2423 */ 2424 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF; 2425 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), 2426 fs->fs_bsize, KERNCRED, 0, &cbp); 2427 td->td_pflags &= ~TDP_COWINPROGRESS; 2428 if (error) 2429 break; 2430 #ifdef DIAGNOSTIC 2431 if (snapdebug) { 2432 printf("Copyonwrite: snapino %ju lbn %jd for ", 2433 (uintmax_t)ip->i_number, (intmax_t)lbn); 2434 if (bp->b_vp == devvp) 2435 printf("fs metadata"); 2436 else 2437 printf("inum %ju", 2438 (uintmax_t)VTOI(bp->b_vp)->i_number); 2439 printf(" lblkno %jd to blkno %jd\n", 2440 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno); 2441 } 2442 #endif 2443 /* 2444 * If we have already read the old block contents, then 2445 * simply copy them to the new block. Note that we need 2446 * to synchronously write snapshots that have not been 2447 * unlinked, and hence will be visible after a crash, 2448 * to ensure their integrity. At a minimum we ensure the 2449 * integrity of the filesystem metadata, but use the 2450 * dopersistence sysctl-setable flag to decide on the 2451 * persistence needed for file content data. 2452 */ 2453 if (savedcbp != NULL) { 2454 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize); 2455 bawrite(cbp); 2456 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR || 2457 dopersistence) && ip->i_effnlink > 0) 2458 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT); 2459 else 2460 launched_async_io = 1; 2461 continue; 2462 } 2463 /* 2464 * Otherwise, read the old block contents into the buffer. 2465 */ 2466 if ((error = readblock(vp, cbp, lbn)) != 0) { 2467 bzero(cbp->b_data, fs->fs_bsize); 2468 bawrite(cbp); 2469 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR || 2470 dopersistence) && ip->i_effnlink > 0) 2471 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT); 2472 else 2473 launched_async_io = 1; 2474 break; 2475 } 2476 savedcbp = cbp; 2477 } 2478 /* 2479 * Note that we need to synchronously write snapshots that 2480 * have not been unlinked, and hence will be visible after 2481 * a crash, to ensure their integrity. At a minimum we 2482 * ensure the integrity of the filesystem metadata, but 2483 * use the dopersistence sysctl-setable flag to decide on 2484 * the persistence needed for file content data. 2485 */ 2486 if (savedcbp) { 2487 vp = savedcbp->b_vp; 2488 bawrite(savedcbp); 2489 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR || 2490 dopersistence) && VTOI(vp)->i_effnlink > 0) 2491 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT); 2492 else 2493 launched_async_io = 1; 2494 } 2495 lockmgr(vp->v_vnlock, LK_RELEASE, NULL); 2496 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) | 2497 prev_norunningbuf; 2498 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0) 2499 waitrunningbufspace(); 2500 /* 2501 * I/O on bp will now be started, so count it in runningbufspace. 2502 */ 2503 if (saved_runningbufspace != 0) { 2504 bp->b_runningbufspace = saved_runningbufspace; 2505 atomic_add_long(&runningbufspace, bp->b_runningbufspace); 2506 } 2507 return (error); 2508 } 2509 2510 /* 2511 * sync snapshots to force freework records waiting on snapshots to claim 2512 * blocks to free. 2513 */ 2514 void 2515 ffs_sync_snap(mp, waitfor) 2516 struct mount *mp; 2517 int waitfor; 2518 { 2519 struct snapdata *sn; 2520 struct vnode *devvp; 2521 struct vnode *vp; 2522 struct inode *ip; 2523 2524 devvp = VFSTOUFS(mp)->um_devvp; 2525 if ((devvp->v_vflag & VV_COPYONWRITE) == 0) 2526 return; 2527 for (;;) { 2528 VI_LOCK(devvp); 2529 sn = devvp->v_rdev->si_snapdata; 2530 if (sn == NULL) { 2531 VI_UNLOCK(devvp); 2532 return; 2533 } 2534 if (lockmgr(&sn->sn_lock, 2535 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL, 2536 VI_MTX(devvp)) == 0) 2537 break; 2538 } 2539 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) { 2540 vp = ITOV(ip); 2541 ffs_syncvnode(vp, waitfor, NO_INO_UPDT); 2542 } 2543 lockmgr(&sn->sn_lock, LK_RELEASE, NULL); 2544 } 2545 2546 /* 2547 * Read the specified block into the given buffer. 2548 * Much of this boiler-plate comes from bwrite(). 2549 */ 2550 static int 2551 readblock(vp, bp, lbn) 2552 struct vnode *vp; 2553 struct buf *bp; 2554 ufs2_daddr_t lbn; 2555 { 2556 struct inode *ip; 2557 struct bio *bip; 2558 struct fs *fs; 2559 2560 ip = VTOI(vp); 2561 fs = ITOFS(ip); 2562 2563 bip = g_alloc_bio(); 2564 bip->bio_cmd = BIO_READ; 2565 bip->bio_offset = dbtob(fsbtodb(fs, blkstofrags(fs, lbn))); 2566 bip->bio_data = bp->b_data; 2567 bip->bio_length = bp->b_bcount; 2568 bip->bio_done = NULL; 2569 2570 g_io_request(bip, ITODEVVP(ip)->v_bufobj.bo_private); 2571 bp->b_error = biowait(bip, "snaprdb"); 2572 g_destroy_bio(bip); 2573 return (bp->b_error); 2574 } 2575 2576 #endif 2577 2578 /* 2579 * Process file deletes that were deferred by ufs_inactive() due to 2580 * the file system being suspended. Transfer IN_LAZYACCESS into 2581 * IN_MODIFIED for vnodes that were accessed during suspension. 2582 */ 2583 void 2584 process_deferred_inactive(struct mount *mp) 2585 { 2586 struct vnode *vp, *mvp; 2587 struct inode *ip; 2588 int error; 2589 2590 (void) vn_start_secondary_write(NULL, &mp, V_WAIT); 2591 loop: 2592 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 2593 /* 2594 * IN_LAZYACCESS is checked here without holding any 2595 * vnode lock, but this flag is set only while holding 2596 * vnode interlock. 2597 */ 2598 if (vp->v_type == VNON || 2599 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 && 2600 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) { 2601 VI_UNLOCK(vp); 2602 continue; 2603 } 2604 vholdl(vp); 2605 retry_vnode: 2606 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 2607 if (error != 0) { 2608 vdrop(vp); 2609 if (error == ENOENT) 2610 continue; /* vnode recycled */ 2611 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 2612 goto loop; 2613 } 2614 ip = VTOI(vp); 2615 if ((ip->i_flag & IN_LAZYACCESS) != 0) { 2616 ip->i_flag &= ~IN_LAZYACCESS; 2617 UFS_INODE_SET_FLAG(ip, IN_MODIFIED); 2618 } 2619 VI_LOCK(vp); 2620 error = vinactive(vp); 2621 if (error == ERELOOKUP && vp->v_usecount == 0) { 2622 VI_UNLOCK(vp); 2623 VOP_UNLOCK(vp); 2624 goto retry_vnode; 2625 } 2626 VI_UNLOCK(vp); 2627 VOP_UNLOCK(vp); 2628 vdrop(vp); 2629 } 2630 vn_finished_secondary_write(mp); 2631 } 2632 2633 #ifndef NO_FFS_SNAPSHOT 2634 2635 static struct snapdata * 2636 ffs_snapdata_alloc(void) 2637 { 2638 struct snapdata *sn; 2639 2640 /* 2641 * Fetch a snapdata from the free list if there is one available. 2642 */ 2643 mtx_lock(&snapfree_lock); 2644 sn = LIST_FIRST(&snapfree); 2645 if (sn != NULL) 2646 LIST_REMOVE(sn, sn_link); 2647 mtx_unlock(&snapfree_lock); 2648 if (sn != NULL) 2649 return (sn); 2650 /* 2651 * If there were no free snapdatas allocate one. 2652 */ 2653 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO); 2654 TAILQ_INIT(&sn->sn_head); 2655 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT, 2656 LK_CANRECURSE | LK_NOSHARE); 2657 return (sn); 2658 } 2659 2660 /* 2661 * The snapdata is never freed because we can not be certain that 2662 * there are no threads sleeping on the snap lock. Persisting 2663 * them permanently avoids costly synchronization in ffs_lock(). 2664 */ 2665 static void 2666 ffs_snapdata_free(struct snapdata *sn) 2667 { 2668 mtx_lock(&snapfree_lock); 2669 LIST_INSERT_HEAD(&snapfree, sn, sn_link); 2670 mtx_unlock(&snapfree_lock); 2671 } 2672 2673 /* Try to free snapdata associated with devvp */ 2674 static void 2675 try_free_snapdata(struct vnode *devvp) 2676 { 2677 struct snapdata *sn; 2678 ufs2_daddr_t *snapblklist; 2679 2680 ASSERT_VI_LOCKED(devvp, "try_free_snapdata"); 2681 sn = devvp->v_rdev->si_snapdata; 2682 2683 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL || 2684 (devvp->v_vflag & VV_COPYONWRITE) == 0) { 2685 VI_UNLOCK(devvp); 2686 return; 2687 } 2688 2689 devvp->v_rdev->si_snapdata = NULL; 2690 devvp->v_vflag &= ~VV_COPYONWRITE; 2691 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp)); 2692 snapblklist = sn->sn_blklist; 2693 sn->sn_blklist = NULL; 2694 sn->sn_listsize = 0; 2695 lockmgr(&sn->sn_lock, LK_RELEASE, NULL); 2696 if (snapblklist != NULL) 2697 free(snapblklist, M_UFSMNT); 2698 ffs_snapdata_free(sn); 2699 } 2700 2701 static struct snapdata * 2702 ffs_snapdata_acquire(struct vnode *devvp) 2703 { 2704 struct snapdata *nsn, *sn; 2705 int error; 2706 2707 /* 2708 * Allocate a free snapdata. This is done before acquiring the 2709 * devvp lock to avoid allocation while the devvp interlock is 2710 * held. 2711 */ 2712 nsn = ffs_snapdata_alloc(); 2713 2714 for (;;) { 2715 VI_LOCK(devvp); 2716 sn = devvp->v_rdev->si_snapdata; 2717 if (sn == NULL) { 2718 /* 2719 * This is the first snapshot on this 2720 * filesystem and we use our pre-allocated 2721 * snapdata. Publish sn with the sn_lock 2722 * owned by us, to avoid the race. 2723 */ 2724 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE | 2725 LK_NOWAIT, NULL); 2726 if (error != 0) 2727 panic("leaked sn, lockmgr error %d", error); 2728 sn = devvp->v_rdev->si_snapdata = nsn; 2729 VI_UNLOCK(devvp); 2730 nsn = NULL; 2731 break; 2732 } 2733 2734 /* 2735 * There is a snapshots which already exists on this 2736 * filesystem, grab a reference to the common lock. 2737 */ 2738 error = lockmgr(&sn->sn_lock, LK_INTERLOCK | 2739 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp)); 2740 if (error == 0) 2741 break; 2742 } 2743 2744 /* 2745 * Free any unused snapdata. 2746 */ 2747 if (nsn != NULL) 2748 ffs_snapdata_free(nsn); 2749 2750 return (sn); 2751 } 2752 2753 #endif 2754