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