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