1 /* $NetBSD: fss.c,v 1.8 2004/05/25 14:54:56 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Juergen Hannken-Illjes. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * File system snapshot disk driver. 41 * 42 * Block/character interface to the snapshot of a mounted file system. 43 */ 44 45 #include <sys/cdefs.h> 46 __KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.8 2004/05/25 14:54:56 hannken Exp $"); 47 48 #include "fss.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/namei.h> 53 #include <sys/proc.h> 54 #include <sys/errno.h> 55 #include <sys/buf.h> 56 #include <sys/malloc.h> 57 #include <sys/ioctl.h> 58 #include <sys/disklabel.h> 59 #include <sys/device.h> 60 #include <sys/disk.h> 61 #include <sys/stat.h> 62 #include <sys/mount.h> 63 #include <sys/vnode.h> 64 #include <sys/file.h> 65 #include <sys/uio.h> 66 #include <sys/conf.h> 67 #include <sys/kthread.h> 68 69 #include <miscfs/specfs/specdev.h> 70 71 #include <dev/fssvar.h> 72 73 #include <machine/stdarg.h> 74 75 #ifdef DEBUG 76 #define FSS_STATISTICS 77 #endif 78 79 #ifdef FSS_STATISTICS 80 struct fss_stat { 81 u_int64_t cow_calls; 82 u_int64_t cow_copied; 83 u_int64_t cow_cache_full; 84 u_int64_t indir_read; 85 u_int64_t indir_write; 86 }; 87 88 static struct fss_stat fss_stat[NFSS]; 89 90 #define FSS_STAT_INC(sc, field) \ 91 do { \ 92 fss_stat[sc->sc_unit].field++; \ 93 } while (0) 94 #define FSS_STAT_SET(sc, field, value) \ 95 do { \ 96 fss_stat[sc->sc_unit].field = value; \ 97 } while (0) 98 #define FSS_STAT_ADD(sc, field, value) \ 99 do { \ 100 fss_stat[sc->sc_unit].field += value; \ 101 } while (0) 102 #define FSS_STAT_VAL(sc, field) fss_stat[sc->sc_unit].field 103 #define FSS_STAT_CLEAR(sc) \ 104 do { \ 105 memset(&fss_stat[sc->sc_unit], 0, \ 106 sizeof(struct fss_stat)); \ 107 } while (0) 108 #else /* FSS_STATISTICS */ 109 #define FSS_STAT_INC(sc, field) 110 #define FSS_STAT_SET(sc, field, value) 111 #define FSS_STAT_ADD(sc, field, value) 112 #define FSS_STAT_CLEAR(sc) 113 #endif /* FSS_STATISTICS */ 114 115 static struct fss_softc fss_softc[NFSS]; 116 117 void fssattach(int); 118 119 dev_type_open(fss_open); 120 dev_type_close(fss_close); 121 dev_type_read(fss_read); 122 dev_type_write(fss_write); 123 dev_type_ioctl(fss_ioctl); 124 dev_type_strategy(fss_strategy); 125 dev_type_dump(fss_dump); 126 dev_type_size(fss_size); 127 128 static int fss_copy_on_write(void *, struct buf *); 129 static inline void fss_error(struct fss_softc *, const char *, ...); 130 static int fss_create_files(struct fss_softc *, struct fss_set *, 131 off_t *, struct proc *); 132 static int fss_create_snapshot(struct fss_softc *, struct fss_set *, 133 struct proc *); 134 static int fss_delete_snapshot(struct fss_softc *, struct proc *); 135 static int fss_softc_alloc(struct fss_softc *); 136 static void fss_softc_free(struct fss_softc *); 137 static void fss_cluster_iodone(struct buf *); 138 static void fss_read_cluster(struct fss_softc *, u_int32_t); 139 static int fss_write_cluster(struct fss_cache *, u_int32_t); 140 static void fss_bs_thread(void *); 141 static int fss_bmap(struct fss_softc *, off_t, int, 142 struct vnode **, daddr_t *, int *); 143 static int fss_bs_io(struct fss_softc *, fss_io_type, 144 u_int32_t, long, int, caddr_t); 145 static u_int32_t *fss_bs_indir(struct fss_softc *, u_int32_t); 146 147 const struct bdevsw fss_bdevsw = { 148 fss_open, fss_close, fss_strategy, fss_ioctl, 149 fss_dump, fss_size, D_DISK 150 }; 151 152 const struct cdevsw fss_cdevsw = { 153 fss_open, fss_close, fss_read, fss_write, fss_ioctl, 154 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 155 }; 156 157 void 158 fssattach(int num) 159 { 160 int i; 161 struct fss_softc *sc; 162 163 for (i = 0; i < NFSS; i++) { 164 sc = &fss_softc[i]; 165 sc->sc_unit = i; 166 sc->sc_bdev = NODEV; 167 simple_lock_init(&sc->sc_slock); 168 bufq_alloc(&sc->sc_bufq, BUFQ_FCFS|BUFQ_SORT_RAWBLOCK); 169 } 170 } 171 172 int 173 fss_open(dev_t dev, int flags, int mode, struct proc *p) 174 { 175 struct fss_softc *sc; 176 177 if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL) 178 return ENODEV; 179 180 return 0; 181 } 182 183 int 184 fss_close(dev_t dev, int flags, int mode, struct proc *p) 185 { 186 struct fss_softc *sc; 187 188 if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL) 189 return ENODEV; 190 191 return 0; 192 } 193 194 void 195 fss_strategy(struct buf *bp) 196 { 197 int s; 198 struct fss_softc *sc; 199 200 sc = FSS_DEV_TO_SOFTC(bp->b_dev); 201 202 FSS_LOCK(sc, s); 203 204 if ((bp->b_flags & B_READ) != B_READ || 205 sc == NULL || !FSS_ISVALID(sc)) { 206 207 FSS_UNLOCK(sc, s); 208 209 bp->b_error = (sc == NULL ? ENODEV : EROFS); 210 bp->b_flags |= B_ERROR; 211 bp->b_resid = bp->b_bcount; 212 biodone(bp); 213 return; 214 } 215 216 bp->b_rawblkno = bp->b_blkno; 217 BUFQ_PUT(&sc->sc_bufq, bp); 218 wakeup(&sc->sc_bs_proc); 219 220 FSS_UNLOCK(sc, s); 221 } 222 223 int 224 fss_read(dev_t dev, struct uio *uio, int flags) 225 { 226 return physio(fss_strategy, NULL, dev, B_READ, minphys, uio); 227 } 228 229 int 230 fss_write(dev_t dev, struct uio *uio, int flags) 231 { 232 return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio); 233 } 234 235 int 236 fss_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 237 { 238 int s, error; 239 struct fss_softc *sc; 240 struct fss_set *fss = (struct fss_set *)data; 241 struct fss_get *fsg = (struct fss_get *)data; 242 243 if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL) 244 return ENODEV; 245 246 FSS_LOCK(sc, s); 247 while ((sc->sc_flags & FSS_EXCL) == FSS_EXCL) { 248 error = ltsleep(sc, PRIBIO|PCATCH, "fsslock", 0, &sc->sc_slock); 249 if (error) { 250 FSS_UNLOCK(sc, s); 251 return error; 252 } 253 } 254 sc->sc_flags |= FSS_EXCL; 255 FSS_UNLOCK(sc, s); 256 257 error = EINVAL; 258 259 switch (cmd) { 260 case FSSIOCSET: 261 if ((flag & FWRITE) == 0) 262 error = EPERM; 263 else if ((sc->sc_flags & FSS_ACTIVE) != 0) 264 error = EBUSY; 265 else 266 error = fss_create_snapshot(sc, fss, p); 267 break; 268 269 case FSSIOCCLR: 270 if ((flag & FWRITE) == 0) 271 error = EPERM; 272 else if ((sc->sc_flags & FSS_ACTIVE) == 0) 273 error = ENXIO; 274 else 275 error = fss_delete_snapshot(sc, p); 276 break; 277 278 case FSSIOCGET: 279 switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) { 280 case FSS_ACTIVE: 281 memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN); 282 fsg->fsg_csize = FSS_CLSIZE(sc); 283 fsg->fsg_time = sc->sc_time; 284 fsg->fsg_mount_size = sc->sc_clcount; 285 fsg->fsg_bs_size = sc->sc_clnext; 286 error = 0; 287 break; 288 case FSS_PERSISTENT | FSS_ACTIVE: 289 memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN); 290 fsg->fsg_csize = 0; 291 fsg->fsg_time = sc->sc_time; 292 fsg->fsg_mount_size = 0; 293 fsg->fsg_bs_size = 0; 294 error = 0; 295 break; 296 default: 297 error = ENXIO; 298 break; 299 } 300 break; 301 } 302 303 FSS_LOCK(sc, s); 304 sc->sc_flags &= ~FSS_EXCL; 305 FSS_UNLOCK(sc, s); 306 wakeup(sc); 307 308 return error; 309 } 310 311 int 312 fss_size(dev_t dev) 313 { 314 return -1; 315 } 316 317 int 318 fss_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 319 { 320 return EROFS; 321 } 322 323 /* 324 * An error occurred reading or writing the snapshot or backing store. 325 * If it is the first error log to console. 326 * The caller holds the simplelock. 327 */ 328 static inline void 329 fss_error(struct fss_softc *sc, const char *fmt, ...) 330 { 331 va_list ap; 332 333 if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE) { 334 va_start(ap, fmt); 335 printf("fss%d: snapshot invalid: ", sc->sc_unit); 336 vprintf(fmt, ap); 337 printf("\n"); 338 va_end(ap); 339 } 340 if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE) 341 sc->sc_flags |= FSS_ERROR; 342 } 343 344 /* 345 * Allocate the variable sized parts of the softc and 346 * fork the kernel thread. 347 * 348 * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size 349 * must be initialized. 350 */ 351 static int 352 fss_softc_alloc(struct fss_softc *sc) 353 { 354 int i, len, error; 355 356 len = (sc->sc_clcount+NBBY-1)/NBBY; 357 sc->sc_copied = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 358 if (sc->sc_copied == NULL) 359 return(ENOMEM); 360 361 len = sc->sc_cache_size*sizeof(struct fss_cache); 362 sc->sc_cache = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 363 if (sc->sc_cache == NULL) 364 return(ENOMEM); 365 366 len = FSS_CLSIZE(sc); 367 for (i = 0; i < sc->sc_cache_size; i++) { 368 sc->sc_cache[i].fc_type = FSS_CACHE_FREE; 369 sc->sc_cache[i].fc_softc = sc; 370 sc->sc_cache[i].fc_xfercount = 0; 371 sc->sc_cache[i].fc_data = malloc(len, M_TEMP, 372 M_WAITOK|M_CANFAIL); 373 if (sc->sc_cache[i].fc_data == NULL) 374 return(ENOMEM); 375 } 376 377 len = (sc->sc_indir_size+NBBY-1)/NBBY; 378 sc->sc_indir_valid = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 379 if (sc->sc_indir_valid == NULL) 380 return(ENOMEM); 381 382 len = FSS_CLSIZE(sc); 383 sc->sc_indir_data = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 384 if (sc->sc_indir_data == NULL) 385 return(ENOMEM); 386 387 if ((error = kthread_create1(fss_bs_thread, sc, &sc->sc_bs_proc, 388 "fssbs%d", sc->sc_unit)) != 0) 389 return error; 390 391 sc->sc_flags |= FSS_BS_THREAD; 392 return 0; 393 } 394 395 /* 396 * Free the variable sized parts of the softc. 397 */ 398 static void 399 fss_softc_free(struct fss_softc *sc) 400 { 401 int s, i; 402 403 if ((sc->sc_flags & FSS_BS_THREAD) != 0) { 404 FSS_LOCK(sc, s); 405 sc->sc_flags &= ~FSS_BS_THREAD; 406 wakeup(&sc->sc_bs_proc); 407 while (sc->sc_bs_proc != NULL) 408 ltsleep(&sc->sc_bs_proc, PRIBIO, "fssthread", 0, 409 &sc->sc_slock); 410 FSS_UNLOCK(sc, s); 411 } 412 413 if (sc->sc_copied != NULL) 414 free(sc->sc_copied, M_TEMP); 415 sc->sc_copied = NULL; 416 417 if (sc->sc_cache != NULL) { 418 for (i = 0; i < sc->sc_cache_size; i++) 419 if (sc->sc_cache[i].fc_data != NULL) 420 free(sc->sc_cache[i].fc_data, M_TEMP); 421 free(sc->sc_cache, M_TEMP); 422 } 423 sc->sc_cache = NULL; 424 425 if (sc->sc_indir_valid != NULL) 426 free(sc->sc_indir_valid, M_TEMP); 427 sc->sc_indir_valid = NULL; 428 429 if (sc->sc_indir_data != NULL) 430 free(sc->sc_indir_data, M_TEMP); 431 sc->sc_indir_data = NULL; 432 } 433 434 /* 435 * Check if an unmount is ok. If forced, set this snapshot into ERROR state. 436 */ 437 int 438 fss_umount_hook(struct mount *mp, int forced) 439 { 440 int i, s; 441 442 for (i = 0; i < NFSS; i++) { 443 FSS_LOCK(&fss_softc[i], s); 444 if ((fss_softc[i].sc_flags & FSS_ACTIVE) != 0 && 445 fss_softc[i].sc_mount == mp) { 446 if (forced) 447 fss_error(&fss_softc[i], "forced unmount"); 448 else { 449 FSS_UNLOCK(&fss_softc[i], s); 450 return EBUSY; 451 } 452 } 453 FSS_UNLOCK(&fss_softc[i], s); 454 } 455 456 return 0; 457 } 458 459 /* 460 * A buffer is written to the snapshotted block device. Copy to 461 * backing store if needed. 462 */ 463 static int 464 fss_copy_on_write(void *v, struct buf *bp) 465 { 466 int s; 467 u_int32_t cl, ch, c; 468 struct fss_softc *sc = v; 469 470 FSS_LOCK(sc, s); 471 if (!FSS_ISVALID(sc)) { 472 FSS_UNLOCK(sc, s); 473 return 0; 474 } 475 476 FSS_UNLOCK(sc, s); 477 478 FSS_STAT_INC(sc, cow_calls); 479 480 cl = FSS_BTOCL(sc, dbtob(bp->b_blkno)); 481 ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1); 482 483 for (c = cl; c <= ch; c++) 484 fss_read_cluster(sc, c); 485 486 return 0; 487 } 488 489 /* 490 * Lookup and open needed files. 491 * 492 * For file system internal snapshot initializes sc_mntname, sc_mount, 493 * sc_bs_vp and sc_time. 494 * 495 * Otherwise returns dev and size of the underlying block device. 496 * Initializes sc_mntname, sc_mount_vp, sc_bdev, sc_bs_vp and sc_mount 497 */ 498 static int 499 fss_create_files(struct fss_softc *sc, struct fss_set *fss, 500 off_t *bsize, struct proc *p) 501 { 502 int error, fsbsize; 503 struct timespec ts; 504 struct partinfo dpart; 505 struct vattr va; 506 struct nameidata nd; 507 508 /* 509 * Get the mounted file system. 510 */ 511 512 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_mount, p); 513 if ((error = namei(&nd)) != 0) 514 return error; 515 516 if ((nd.ni_vp->v_flag & VROOT) != VROOT) { 517 vrele(nd.ni_vp); 518 return EINVAL; 519 } 520 521 sc->sc_mount = nd.ni_vp->v_mount; 522 memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN); 523 524 vrele(nd.ni_vp); 525 526 /* 527 * Check for file system internal snapshot. 528 */ 529 530 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p); 531 if ((error = namei(&nd)) != 0) 532 return error; 533 534 if (nd.ni_vp->v_mount == sc->sc_mount) { 535 vrele(nd.ni_vp); 536 sc->sc_flags |= FSS_PERSISTENT; 537 538 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p); 539 if ((error = vn_open(&nd, FREAD, 0)) != 0) 540 return error; 541 sc->sc_bs_vp = nd.ni_vp; 542 543 error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts); 544 TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts); 545 return error; 546 } 547 vrele(nd.ni_vp); 548 549 /* 550 * Get the block device it is mounted on. 551 */ 552 553 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, 554 sc->sc_mount->mnt_stat.f_mntfromname, p); 555 if ((error = namei(&nd)) != 0) 556 return error; 557 558 if (nd.ni_vp->v_type != VBLK) { 559 vrele(nd.ni_vp); 560 return EINVAL; 561 } 562 563 error = VOP_IOCTL(nd.ni_vp, DIOCGPART, &dpart, FREAD, p->p_ucred, p); 564 if (error) { 565 vrele(nd.ni_vp); 566 return error; 567 } 568 569 sc->sc_mount_vp = nd.ni_vp; 570 sc->sc_bdev = nd.ni_vp->v_rdev; 571 *bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size; 572 vrele(nd.ni_vp); 573 574 /* 575 * Get the backing store 576 */ 577 578 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p); 579 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) 580 return error; 581 VOP_UNLOCK(nd.ni_vp, 0); 582 583 sc->sc_bs_vp = nd.ni_vp; 584 585 if (nd.ni_vp->v_type != VREG && nd.ni_vp->v_type != VCHR) 586 return EINVAL; 587 588 if (sc->sc_bs_vp->v_type == VREG) { 589 error = VOP_GETATTR(sc->sc_bs_vp, &va, p->p_ucred, p); 590 if (error != 0) 591 return error; 592 sc->sc_bs_size = va.va_size; 593 fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize; 594 if (fsbsize & (fsbsize-1)) /* No power of two */ 595 return EINVAL; 596 for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32; 597 sc->sc_bs_bshift++) 598 if (FSS_FSBSIZE(sc) == fsbsize) 599 break; 600 if (sc->sc_bs_bshift >= 32) 601 return EINVAL; 602 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 603 sc->sc_flags |= FSS_BS_ALLOC; 604 } else { 605 sc->sc_bs_bshift = DEV_BSHIFT; 606 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 607 sc->sc_flags &= ~FSS_BS_ALLOC; 608 } 609 610 /* 611 * As all IO to from/to the backing store goes through 612 * VOP_STRATEGY() clean the buffer cache to prevent 613 * cache incoherencies. 614 */ 615 if ((error = vinvalbuf(sc->sc_bs_vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) 616 return error; 617 618 return 0; 619 } 620 621 /* 622 * Create a snapshot. 623 */ 624 static int 625 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct proc *p) 626 { 627 int len, error; 628 u_int32_t csize; 629 off_t bsize; 630 631 /* 632 * Open needed files. 633 */ 634 if ((error = fss_create_files(sc, fss, &bsize, p)) != 0) 635 goto bad; 636 637 if (sc->sc_flags & FSS_PERSISTENT) { 638 fss_softc_alloc(sc); 639 sc->sc_flags |= FSS_ACTIVE; 640 return 0; 641 } 642 643 /* 644 * Set cluster size. Must be a power of two and 645 * a multiple of backing store block size. 646 */ 647 if (fss->fss_csize <= 0) 648 csize = MAXPHYS; 649 else 650 csize = fss->fss_csize; 651 if (bsize/csize > FSS_CLUSTER_MAX) 652 csize = bsize/FSS_CLUSTER_MAX+1; 653 654 for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32; 655 sc->sc_clshift++) 656 if (FSS_CLSIZE(sc) >= csize) 657 break; 658 if (sc->sc_clshift >= 32) { 659 error = EINVAL; 660 goto bad; 661 } 662 sc->sc_clmask = FSS_CLSIZE(sc)-1; 663 664 /* 665 * Set number of cache slots. 666 */ 667 if (FSS_CLSIZE(sc) <= 8192) 668 sc->sc_cache_size = 32; 669 else if (FSS_CLSIZE(sc) <= 65536) 670 sc->sc_cache_size = 8; 671 else 672 sc->sc_cache_size = 4; 673 674 /* 675 * Set number of clusters and size of last cluster. 676 */ 677 sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1; 678 sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1; 679 680 /* 681 * Set size of indirect table. 682 */ 683 len = sc->sc_clcount*sizeof(u_int32_t); 684 sc->sc_indir_size = FSS_BTOCL(sc, len)+1; 685 sc->sc_clnext = sc->sc_indir_size; 686 sc->sc_indir_cur = 0; 687 688 if ((error = fss_softc_alloc(sc)) != 0) 689 goto bad; 690 691 /* 692 * Activate the snapshot. 693 */ 694 695 if ((error = vfs_write_suspend(sc->sc_mount, PUSER|PCATCH, 0)) != 0) 696 goto bad; 697 698 microtime(&sc->sc_time); 699 700 if (error == 0) 701 error = vn_cow_establish(sc->sc_mount_vp, 702 fss_copy_on_write, sc); 703 if (error == 0) 704 sc->sc_flags |= FSS_ACTIVE; 705 706 vfs_write_resume(sc->sc_mount); 707 708 if (error != 0) 709 goto bad; 710 711 #ifdef DEBUG 712 printf("fss%d: %s snapshot active\n", sc->sc_unit, sc->sc_mntname); 713 printf("fss%d: %u clusters of %u, %u cache slots, %u indir clusters\n", 714 sc->sc_unit, sc->sc_clcount, FSS_CLSIZE(sc), 715 sc->sc_cache_size, sc->sc_indir_size); 716 #endif 717 718 return 0; 719 720 bad: 721 fss_softc_free(sc); 722 if (sc->sc_bs_vp != NULL) { 723 if (sc->sc_flags & FSS_PERSISTENT) 724 vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p); 725 else 726 vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p); 727 } 728 sc->sc_bs_vp = NULL; 729 730 return error; 731 } 732 733 /* 734 * Delete a snapshot. 735 */ 736 static int 737 fss_delete_snapshot(struct fss_softc *sc, struct proc *p) 738 { 739 int s; 740 741 if ((sc->sc_flags & FSS_PERSISTENT) == 0) 742 vn_cow_disestablish(sc->sc_mount_vp, fss_copy_on_write, sc); 743 744 FSS_LOCK(sc, s); 745 sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR); 746 sc->sc_mount = NULL; 747 sc->sc_bdev = NODEV; 748 FSS_UNLOCK(sc, s); 749 750 fss_softc_free(sc); 751 if (sc->sc_flags & FSS_PERSISTENT) 752 vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p); 753 else 754 vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p); 755 sc->sc_bs_vp = NULL; 756 sc->sc_flags &= ~FSS_PERSISTENT; 757 758 FSS_STAT_CLEAR(sc); 759 760 return 0; 761 } 762 763 /* 764 * Get the block address and number of contiguous blocks. 765 * If the file contains a hole, try to allocate. 766 */ 767 static int 768 fss_bmap(struct fss_softc *sc, off_t start, int len, 769 struct vnode **vpp, daddr_t *bnp, int *runp) 770 { 771 int l, s, error; 772 struct buf *bp, **bpp; 773 774 if ((sc->sc_bs_vp->v_mount->mnt_flag & MNT_SOFTDEP) != 0) 775 bpp = &bp; 776 else 777 bpp = NULL; 778 779 vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY); 780 781 error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp); 782 if ((error == 0 && *bnp != (daddr_t)-1) || 783 (sc->sc_flags & FSS_BS_ALLOC) == 0) 784 goto out; 785 786 if (start+len >= sc->sc_bs_size) { 787 error = ENOSPC; 788 goto out; 789 } 790 791 for (l = 0; l < len; l += FSS_FSBSIZE(sc)) { 792 error = VOP_BALLOC(sc->sc_bs_vp, start+l, FSS_FSBSIZE(sc), 793 sc->sc_bs_proc->p_ucred, 0, bpp); 794 if (error) 795 goto out; 796 797 if (bpp == NULL) 798 continue; 799 800 s = splbio(); 801 simple_lock(&bp->b_interlock); 802 803 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_start) 804 (*bioops.io_start)(bp); 805 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete) 806 (*bioops.io_complete)(bp); 807 808 bp->b_flags |= B_INVAL; 809 simple_unlock(&bp->b_interlock); 810 splx(s); 811 812 brelse(bp); 813 } 814 815 error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp); 816 817 out: 818 819 VOP_UNLOCK(sc->sc_bs_vp, 0); 820 if (error == 0 && *bnp == (daddr_t)-1) 821 error = ENOSPC; 822 823 return error; 824 } 825 826 /* 827 * A read from the snapshotted block device has completed. 828 */ 829 static void 830 fss_cluster_iodone(struct buf *bp) 831 { 832 int s; 833 struct fss_cache *scp = bp->b_private; 834 835 FSS_LOCK(scp->fc_softc, s); 836 837 if (bp->b_flags & B_EINTR) 838 fss_error(scp->fc_softc, "fs read interrupted"); 839 if (bp->b_flags & B_ERROR) 840 fss_error(scp->fc_softc, "fs read error %d", bp->b_error); 841 842 if (bp->b_vp != NULL) 843 brelvp(bp); 844 845 if (--scp->fc_xfercount == 0) 846 wakeup(&scp->fc_data); 847 848 FSS_UNLOCK(scp->fc_softc, s); 849 850 s = splbio(); 851 pool_put(&bufpool, bp); 852 splx(s); 853 } 854 855 /* 856 * Read a cluster from the snapshotted block device to the cache. 857 */ 858 static void 859 fss_read_cluster(struct fss_softc *sc, u_int32_t cl) 860 { 861 int s, todo, len; 862 caddr_t addr; 863 daddr_t dblk; 864 struct buf *bp; 865 struct fss_cache *scp, *scl; 866 867 /* 868 * Get a free cache slot. 869 */ 870 scl = sc->sc_cache+sc->sc_cache_size; 871 872 FSS_LOCK(sc, s); 873 874 restart: 875 if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) { 876 FSS_UNLOCK(sc, s); 877 return; 878 } 879 880 for (scp = sc->sc_cache; scp < scl; scp++) 881 if (scp->fc_type != FSS_CACHE_FREE && 882 scp->fc_cluster == cl) { 883 ltsleep(&scp->fc_type, PRIBIO, "cowwait2", 0, 884 &sc->sc_slock); 885 goto restart; 886 } 887 888 for (scp = sc->sc_cache; scp < scl; scp++) 889 if (scp->fc_type == FSS_CACHE_FREE) { 890 scp->fc_type = FSS_CACHE_BUSY; 891 scp->fc_cluster = cl; 892 break; 893 } 894 if (scp >= scl) { 895 FSS_STAT_INC(sc, cow_cache_full); 896 ltsleep(&sc->sc_cache, PRIBIO, "cowwait3", 0, &sc->sc_slock); 897 goto restart; 898 } 899 900 FSS_UNLOCK(sc, s); 901 902 /* 903 * Start the read. 904 */ 905 FSS_STAT_INC(sc, cow_copied); 906 907 dblk = btodb(FSS_CLTOB(sc, cl)); 908 addr = scp->fc_data; 909 if (cl == sc->sc_clcount-1) { 910 todo = sc->sc_clresid; 911 memset(addr+todo, 0, FSS_CLSIZE(sc)-todo); 912 } else 913 todo = FSS_CLSIZE(sc); 914 while (todo > 0) { 915 len = todo; 916 if (len > MAXPHYS) 917 len = MAXPHYS; 918 919 s = splbio(); 920 bp = pool_get(&bufpool, PR_WAITOK); 921 splx(s); 922 923 BUF_INIT(bp); 924 bp->b_flags = B_READ|B_CALL; 925 bp->b_bcount = len; 926 bp->b_bufsize = bp->b_bcount; 927 bp->b_error = 0; 928 bp->b_data = addr; 929 bp->b_blkno = bp->b_rawblkno = dblk; 930 bp->b_proc = NULL; 931 bp->b_dev = sc->sc_bdev; 932 bp->b_vp = NULLVP; 933 bp->b_private = scp; 934 bp->b_iodone = fss_cluster_iodone; 935 936 DEV_STRATEGY(bp); 937 938 FSS_LOCK(sc, s); 939 scp->fc_xfercount++; 940 FSS_UNLOCK(sc, s); 941 942 dblk += btodb(len); 943 addr += len; 944 todo -= len; 945 } 946 947 /* 948 * Wait for all read requests to complete. 949 */ 950 FSS_LOCK(sc, s); 951 while (scp->fc_xfercount > 0) 952 ltsleep(&scp->fc_data, PRIBIO, "cowwait", 0, &sc->sc_slock); 953 954 scp->fc_type = FSS_CACHE_VALID; 955 setbit(sc->sc_copied, scp->fc_cluster); 956 FSS_UNLOCK(sc, s); 957 958 wakeup(&sc->sc_bs_proc); 959 } 960 961 /* 962 * Write a cluster from the cache to the backing store. 963 */ 964 static int 965 fss_write_cluster(struct fss_cache *scp, u_int32_t cl) 966 { 967 int s, error, todo, len, nra; 968 daddr_t nbn; 969 caddr_t addr; 970 off_t pos; 971 struct buf *bp; 972 struct vnode *vp; 973 struct fss_softc *sc; 974 975 error = 0; 976 sc = scp->fc_softc; 977 978 pos = FSS_CLTOB(sc, cl); 979 addr = scp->fc_data; 980 todo = FSS_CLSIZE(sc); 981 982 while (todo > 0) { 983 error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra); 984 if (error) 985 break; 986 987 len = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos); 988 if (len > todo) 989 len = todo; 990 991 s = splbio(); 992 bp = pool_get(&bufpool, PR_WAITOK); 993 splx(s); 994 995 BUF_INIT(bp); 996 bp->b_flags = B_CALL; 997 bp->b_bcount = len; 998 bp->b_bufsize = bp->b_bcount; 999 bp->b_error = 0; 1000 bp->b_data = addr; 1001 bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos)); 1002 bp->b_proc = NULL; 1003 bp->b_vp = NULLVP; 1004 bp->b_private = scp; 1005 bp->b_iodone = fss_cluster_iodone; 1006 bgetvp(vp, bp); 1007 bp->b_vp->v_numoutput++; 1008 1009 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 1010 VOP_STRATEGY(vp, bp); 1011 1012 FSS_LOCK(sc, s); 1013 scp->fc_xfercount++; 1014 FSS_UNLOCK(sc, s); 1015 1016 pos += len; 1017 addr += len; 1018 todo -= len; 1019 } 1020 1021 /* 1022 * Wait for all write requests to complete. 1023 */ 1024 FSS_LOCK(sc, s); 1025 while (scp->fc_xfercount > 0) 1026 ltsleep(&scp->fc_data, PRIBIO, "bswwait", 0, &sc->sc_slock); 1027 FSS_UNLOCK(sc, s); 1028 1029 return error; 1030 } 1031 1032 /* 1033 * Read/write clusters from/to backing store. 1034 */ 1035 static int 1036 fss_bs_io(struct fss_softc *sc, fss_io_type rw, 1037 u_int32_t cl, long off, int len, caddr_t data) 1038 { 1039 int s, error, todo, count, nra; 1040 off_t pos; 1041 daddr_t nbn; 1042 struct buf *bp; 1043 struct vnode *vp; 1044 1045 todo = len; 1046 pos = FSS_CLTOB(sc, cl)+off; 1047 error = 0; 1048 1049 while (todo > 0) { 1050 error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra); 1051 if (error) 1052 break; 1053 1054 count = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos); 1055 if (count > todo) 1056 count = todo; 1057 1058 s = splbio(); 1059 bp = pool_get(&bufpool, PR_WAITOK); 1060 splx(s); 1061 1062 BUF_INIT(bp); 1063 bp->b_flags = (rw == FSS_READ ? B_READ : 0); 1064 bp->b_bcount = count; 1065 bp->b_bufsize = bp->b_bcount; 1066 bp->b_error = 0; 1067 bp->b_data = data; 1068 bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos)); 1069 bp->b_proc = NULL; 1070 bp->b_vp = NULLVP; 1071 bgetvp(vp, bp); 1072 if ((bp->b_flags & B_READ) == 0) 1073 bp->b_vp->v_numoutput++; 1074 1075 if ((bp->b_flags & B_READ) == 0 || cl < sc->sc_indir_size) 1076 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 1077 VOP_STRATEGY(vp, bp); 1078 1079 error = biowait(bp); 1080 1081 if (bp->b_vp != NULL) 1082 brelvp(bp); 1083 1084 s = splbio(); 1085 pool_put(&bufpool, bp); 1086 splx(s); 1087 1088 if (error) 1089 break; 1090 1091 todo -= count; 1092 data += count; 1093 pos += count; 1094 } 1095 1096 return error; 1097 } 1098 1099 /* 1100 * Get a pointer to the indirect slot for this cluster. 1101 */ 1102 static u_int32_t * 1103 fss_bs_indir(struct fss_softc *sc, u_int32_t cl) 1104 { 1105 u_int32_t icl; 1106 int ioff; 1107 1108 icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t)); 1109 ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t)); 1110 1111 if (sc->sc_indir_cur == icl) 1112 return &sc->sc_indir_data[ioff]; 1113 1114 if (sc->sc_indir_dirty) { 1115 FSS_STAT_INC(sc, indir_write); 1116 if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0, 1117 FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0) 1118 return NULL; 1119 setbit(sc->sc_indir_valid, sc->sc_indir_cur); 1120 } 1121 1122 sc->sc_indir_dirty = 0; 1123 sc->sc_indir_cur = icl; 1124 1125 if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) { 1126 FSS_STAT_INC(sc, indir_read); 1127 if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0, 1128 FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0) 1129 return NULL; 1130 } else 1131 memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc)); 1132 1133 return &sc->sc_indir_data[ioff]; 1134 } 1135 1136 /* 1137 * The kernel thread (one for every active snapshot). 1138 * 1139 * After wakeup it cleans the cache and runs the I/O requests. 1140 */ 1141 static void 1142 fss_bs_thread(void *arg) 1143 { 1144 int error, len, nfreed, nio, s; 1145 long off; 1146 caddr_t addr; 1147 u_int32_t c, cl, ch, *indirp; 1148 struct buf *bp, *nbp; 1149 struct fss_softc *sc; 1150 struct fss_cache *scp, *scl; 1151 1152 sc = arg; 1153 1154 scl = sc->sc_cache+sc->sc_cache_size; 1155 1156 s = splbio(); 1157 nbp = pool_get(&bufpool, PR_WAITOK); 1158 splx(s); 1159 1160 nfreed = nio = 1; /* Dont sleep the first time */ 1161 1162 FSS_LOCK(sc, s); 1163 1164 for (;;) { 1165 if (nfreed == 0 && nio == 0) 1166 ltsleep(&sc->sc_bs_proc, PVM-1, "fssbs", 0, 1167 &sc->sc_slock); 1168 1169 if ((sc->sc_flags & FSS_BS_THREAD) == 0) { 1170 sc->sc_bs_proc = NULL; 1171 wakeup(&sc->sc_bs_proc); 1172 1173 FSS_UNLOCK(sc, s); 1174 1175 s = splbio(); 1176 pool_put(&bufpool, nbp); 1177 splx(s); 1178 #ifdef FSS_STATISTICS 1179 if ((sc->sc_flags & FSS_PERSISTENT) == 0) { 1180 printf("fss%d: cow called %" PRId64 " times," 1181 " copied %" PRId64 " clusters," 1182 " cache full %" PRId64 " times\n", 1183 sc->sc_unit, 1184 FSS_STAT_VAL(sc, cow_calls), 1185 FSS_STAT_VAL(sc, cow_copied), 1186 FSS_STAT_VAL(sc, cow_cache_full)); 1187 printf("fss%d: %" PRId64 " indir reads," 1188 " %" PRId64 " indir writes\n", 1189 sc->sc_unit, 1190 FSS_STAT_VAL(sc, indir_read), 1191 FSS_STAT_VAL(sc, indir_write)); 1192 } 1193 #endif /* FSS_STATISTICS */ 1194 kthread_exit(0); 1195 } 1196 1197 /* 1198 * Process I/O requests (persistent) 1199 */ 1200 1201 if (sc->sc_flags & FSS_PERSISTENT) { 1202 nfreed = nio = 0; 1203 1204 if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL) 1205 continue; 1206 1207 nio++; 1208 1209 if (FSS_ISVALID(sc)) { 1210 FSS_UNLOCK(sc, s); 1211 1212 error = vn_rdwr(UIO_READ, sc->sc_bs_vp, 1213 bp->b_data, bp->b_bcount, 1214 dbtob(bp->b_blkno), UIO_SYSSPACE, IO_UNIT, 1215 sc->sc_bs_proc->p_ucred, NULL, 1216 sc->sc_bs_proc); 1217 1218 FSS_LOCK(sc, s); 1219 } else 1220 error = ENXIO; 1221 1222 if (error) { 1223 bp->b_error = error; 1224 bp->b_flags |= B_ERROR; 1225 bp->b_resid = bp->b_bcount; 1226 } 1227 biodone(bp); 1228 1229 continue; 1230 } 1231 1232 /* 1233 * Clean the cache 1234 */ 1235 nfreed = 0; 1236 for (scp = sc->sc_cache; scp < scl; scp++) { 1237 if (scp->fc_type != FSS_CACHE_VALID) 1238 continue; 1239 1240 FSS_UNLOCK(sc, s); 1241 1242 indirp = fss_bs_indir(sc, scp->fc_cluster); 1243 if (indirp != NULL) { 1244 error = fss_write_cluster(scp, sc->sc_clnext); 1245 } else 1246 error = EIO; 1247 1248 FSS_LOCK(sc, s); 1249 1250 if (error == 0) { 1251 *indirp = sc->sc_clnext++; 1252 sc->sc_indir_dirty = 1; 1253 } else 1254 fss_error(sc, "write bs error %d", error); 1255 1256 scp->fc_type = FSS_CACHE_FREE; 1257 nfreed++; 1258 wakeup(&scp->fc_type); 1259 } 1260 1261 if (nfreed) 1262 wakeup(&sc->sc_cache); 1263 1264 /* 1265 * Process I/O requests 1266 */ 1267 nio = 0; 1268 1269 if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL) 1270 continue; 1271 1272 nio++; 1273 1274 if (!FSS_ISVALID(sc)) { 1275 bp->b_error = ENXIO; 1276 bp->b_flags |= B_ERROR; 1277 bp->b_resid = bp->b_bcount; 1278 biodone(bp); 1279 continue; 1280 } 1281 1282 /* 1283 * First read from the snapshotted block device. 1284 * XXX Split to only read those parts that have not 1285 * been saved to backing store? 1286 */ 1287 1288 FSS_UNLOCK(sc, s); 1289 1290 BUF_INIT(nbp); 1291 nbp->b_flags = B_READ; 1292 nbp->b_bcount = bp->b_bcount; 1293 nbp->b_bufsize = bp->b_bcount; 1294 nbp->b_error = 0; 1295 nbp->b_data = bp->b_data; 1296 nbp->b_blkno = nbp->b_rawblkno = bp->b_blkno; 1297 nbp->b_proc = bp->b_proc; 1298 nbp->b_dev = sc->sc_bdev; 1299 nbp->b_vp = NULLVP; 1300 1301 DEV_STRATEGY(nbp); 1302 1303 if (biowait(nbp) != 0) { 1304 bp->b_resid = bp->b_bcount; 1305 bp->b_error = nbp->b_error; 1306 bp->b_flags |= B_ERROR; 1307 biodone(bp); 1308 continue; 1309 } 1310 1311 cl = FSS_BTOCL(sc, dbtob(bp->b_blkno)); 1312 off = FSS_CLOFF(sc, dbtob(bp->b_blkno)); 1313 ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1); 1314 bp->b_resid = bp->b_bcount; 1315 addr = bp->b_data; 1316 1317 FSS_LOCK(sc, s); 1318 1319 /* 1320 * Replace those parts that have been saved to backing store. 1321 */ 1322 1323 for (c = cl; c <= ch; 1324 c++, off = 0, bp->b_resid -= len, addr += len) { 1325 len = FSS_CLSIZE(sc)-off; 1326 if (len > bp->b_resid) 1327 len = bp->b_resid; 1328 1329 if (isclr(sc->sc_copied, c)) 1330 continue; 1331 1332 FSS_UNLOCK(sc, s); 1333 1334 indirp = fss_bs_indir(sc, c); 1335 1336 FSS_LOCK(sc, s); 1337 1338 if (indirp == NULL || *indirp == 0) { 1339 /* 1340 * Not on backing store. Either in cache 1341 * or hole in the snapshotted block device. 1342 */ 1343 for (scp = sc->sc_cache; scp < scl; scp++) 1344 if (scp->fc_type == FSS_CACHE_VALID && 1345 scp->fc_cluster == c) 1346 break; 1347 if (scp < scl) 1348 memcpy(addr, scp->fc_data+off, len); 1349 else 1350 memset(addr, 0, len); 1351 continue; 1352 } 1353 /* 1354 * Read from backing store. 1355 */ 1356 1357 FSS_UNLOCK(sc, s); 1358 1359 if ((error = fss_bs_io(sc, FSS_READ, *indirp, 1360 off, len, addr)) != 0) { 1361 bp->b_resid = bp->b_bcount; 1362 bp->b_error = error; 1363 bp->b_flags |= B_ERROR; 1364 break; 1365 } 1366 1367 FSS_LOCK(sc, s); 1368 1369 } 1370 1371 biodone(bp); 1372 } 1373 } 1374