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