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