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