1 /* $NetBSD: fss.c,v 1.15 2005/04/17 16:28:26 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.15 2005/04/17 16:28:26 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, off_t, 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 int s, mflag; 176 struct fss_softc *sc; 177 178 mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN); 179 180 if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL) 181 return ENODEV; 182 183 FSS_LOCK(sc, s); 184 185 sc->sc_flags |= mflag; 186 187 FSS_UNLOCK(sc, s); 188 189 return 0; 190 } 191 192 int 193 fss_close(dev_t dev, int flags, int mode, struct proc *p) 194 { 195 int s, mflag, error; 196 struct fss_softc *sc; 197 198 mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN); 199 200 if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL) 201 return ENODEV; 202 203 FSS_LOCK(sc, s); 204 205 if ((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) == mflag) { 206 if ((sc->sc_uflags & FSS_UNCONFIG_ON_CLOSE) != 0 && 207 (sc->sc_flags & FSS_ACTIVE) != 0) { 208 FSS_UNLOCK(sc, s); 209 error = fss_ioctl(dev, FSSIOCCLR, NULL, FWRITE, p); 210 if (error) 211 return error; 212 FSS_LOCK(sc, s); 213 } 214 sc->sc_uflags &= ~FSS_UNCONFIG_ON_CLOSE; 215 } 216 217 sc->sc_flags &= ~mflag; 218 219 FSS_UNLOCK(sc, s); 220 221 return 0; 222 } 223 224 void 225 fss_strategy(struct buf *bp) 226 { 227 int s; 228 struct fss_softc *sc; 229 230 sc = FSS_DEV_TO_SOFTC(bp->b_dev); 231 232 FSS_LOCK(sc, s); 233 234 if ((bp->b_flags & B_READ) != B_READ || 235 sc == NULL || !FSS_ISVALID(sc)) { 236 237 FSS_UNLOCK(sc, s); 238 239 bp->b_error = (sc == NULL ? ENODEV : EROFS); 240 bp->b_flags |= B_ERROR; 241 bp->b_resid = bp->b_bcount; 242 biodone(bp); 243 return; 244 } 245 246 bp->b_rawblkno = bp->b_blkno; 247 BUFQ_PUT(&sc->sc_bufq, bp); 248 wakeup(&sc->sc_bs_proc); 249 250 FSS_UNLOCK(sc, s); 251 } 252 253 int 254 fss_read(dev_t dev, struct uio *uio, int flags) 255 { 256 return physio(fss_strategy, NULL, dev, B_READ, minphys, uio); 257 } 258 259 int 260 fss_write(dev_t dev, struct uio *uio, int flags) 261 { 262 return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio); 263 } 264 265 int 266 fss_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 267 { 268 int s, error; 269 struct fss_softc *sc; 270 struct fss_set *fss = (struct fss_set *)data; 271 struct fss_get *fsg = (struct fss_get *)data; 272 273 if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL) 274 return ENODEV; 275 276 FSS_LOCK(sc, s); 277 while ((sc->sc_flags & FSS_EXCL) == FSS_EXCL) { 278 error = ltsleep(sc, PRIBIO|PCATCH, "fsslock", 0, &sc->sc_slock); 279 if (error) { 280 FSS_UNLOCK(sc, s); 281 return error; 282 } 283 } 284 sc->sc_flags |= FSS_EXCL; 285 FSS_UNLOCK(sc, s); 286 287 switch (cmd) { 288 case FSSIOCSET: 289 if ((flag & FWRITE) == 0) 290 error = EPERM; 291 else if ((sc->sc_flags & FSS_ACTIVE) != 0) 292 error = EBUSY; 293 else 294 error = fss_create_snapshot(sc, fss, p); 295 break; 296 297 case FSSIOCCLR: 298 if ((flag & FWRITE) == 0) 299 error = EPERM; 300 else if ((sc->sc_flags & FSS_ACTIVE) == 0) 301 error = ENXIO; 302 else 303 error = fss_delete_snapshot(sc, p); 304 break; 305 306 case FSSIOCGET: 307 switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) { 308 case FSS_ACTIVE: 309 memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN); 310 fsg->fsg_csize = FSS_CLSIZE(sc); 311 fsg->fsg_time = sc->sc_time; 312 fsg->fsg_mount_size = sc->sc_clcount; 313 fsg->fsg_bs_size = sc->sc_clnext; 314 error = 0; 315 break; 316 case FSS_PERSISTENT | FSS_ACTIVE: 317 memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN); 318 fsg->fsg_csize = 0; 319 fsg->fsg_time = sc->sc_time; 320 fsg->fsg_mount_size = 0; 321 fsg->fsg_bs_size = 0; 322 error = 0; 323 break; 324 default: 325 error = ENXIO; 326 break; 327 } 328 break; 329 330 case FSSIOFSET: 331 sc->sc_uflags = *(int *)data; 332 error = 0; 333 break; 334 335 case FSSIOFGET: 336 *(int *)data = sc->sc_uflags; 337 error = 0; 338 break; 339 340 default: 341 error = EINVAL; 342 break; 343 } 344 345 FSS_LOCK(sc, s); 346 sc->sc_flags &= ~FSS_EXCL; 347 FSS_UNLOCK(sc, s); 348 wakeup(sc); 349 350 return error; 351 } 352 353 int 354 fss_size(dev_t dev) 355 { 356 return -1; 357 } 358 359 int 360 fss_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 361 { 362 return EROFS; 363 } 364 365 /* 366 * An error occurred reading or writing the snapshot or backing store. 367 * If it is the first error log to console. 368 * The caller holds the simplelock. 369 */ 370 static inline void 371 fss_error(struct fss_softc *sc, const char *fmt, ...) 372 { 373 va_list ap; 374 375 if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE) { 376 va_start(ap, fmt); 377 printf("fss%d: snapshot invalid: ", sc->sc_unit); 378 vprintf(fmt, ap); 379 printf("\n"); 380 va_end(ap); 381 } 382 if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE) 383 sc->sc_flags |= FSS_ERROR; 384 } 385 386 /* 387 * Allocate the variable sized parts of the softc and 388 * fork the kernel thread. 389 * 390 * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size 391 * must be initialized. 392 */ 393 static int 394 fss_softc_alloc(struct fss_softc *sc) 395 { 396 int i, len, error; 397 398 len = (sc->sc_clcount+NBBY-1)/NBBY; 399 sc->sc_copied = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 400 if (sc->sc_copied == NULL) 401 return(ENOMEM); 402 403 len = sc->sc_cache_size*sizeof(struct fss_cache); 404 sc->sc_cache = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 405 if (sc->sc_cache == NULL) 406 return(ENOMEM); 407 408 len = FSS_CLSIZE(sc); 409 for (i = 0; i < sc->sc_cache_size; i++) { 410 sc->sc_cache[i].fc_type = FSS_CACHE_FREE; 411 sc->sc_cache[i].fc_softc = sc; 412 sc->sc_cache[i].fc_xfercount = 0; 413 sc->sc_cache[i].fc_data = malloc(len, M_TEMP, 414 M_WAITOK|M_CANFAIL); 415 if (sc->sc_cache[i].fc_data == NULL) 416 return(ENOMEM); 417 } 418 419 len = (sc->sc_indir_size+NBBY-1)/NBBY; 420 sc->sc_indir_valid = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 421 if (sc->sc_indir_valid == NULL) 422 return(ENOMEM); 423 424 len = FSS_CLSIZE(sc); 425 sc->sc_indir_data = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL); 426 if (sc->sc_indir_data == NULL) 427 return(ENOMEM); 428 429 if ((error = kthread_create1(fss_bs_thread, sc, &sc->sc_bs_proc, 430 "fssbs%d", sc->sc_unit)) != 0) 431 return error; 432 433 sc->sc_flags |= FSS_BS_THREAD; 434 return 0; 435 } 436 437 /* 438 * Free the variable sized parts of the softc. 439 */ 440 static void 441 fss_softc_free(struct fss_softc *sc) 442 { 443 int s, i; 444 445 if ((sc->sc_flags & FSS_BS_THREAD) != 0) { 446 FSS_LOCK(sc, s); 447 sc->sc_flags &= ~FSS_BS_THREAD; 448 wakeup(&sc->sc_bs_proc); 449 while (sc->sc_bs_proc != NULL) 450 ltsleep(&sc->sc_bs_proc, PRIBIO, "fssthread", 0, 451 &sc->sc_slock); 452 FSS_UNLOCK(sc, s); 453 } 454 455 if (sc->sc_copied != NULL) 456 free(sc->sc_copied, M_TEMP); 457 sc->sc_copied = NULL; 458 459 if (sc->sc_cache != NULL) { 460 for (i = 0; i < sc->sc_cache_size; i++) 461 if (sc->sc_cache[i].fc_data != NULL) 462 free(sc->sc_cache[i].fc_data, M_TEMP); 463 free(sc->sc_cache, M_TEMP); 464 } 465 sc->sc_cache = NULL; 466 467 if (sc->sc_indir_valid != NULL) 468 free(sc->sc_indir_valid, M_TEMP); 469 sc->sc_indir_valid = NULL; 470 471 if (sc->sc_indir_data != NULL) 472 free(sc->sc_indir_data, M_TEMP); 473 sc->sc_indir_data = NULL; 474 } 475 476 /* 477 * Check if an unmount is ok. If forced, set this snapshot into ERROR state. 478 */ 479 int 480 fss_umount_hook(struct mount *mp, int forced) 481 { 482 int i, s; 483 484 for (i = 0; i < NFSS; i++) { 485 FSS_LOCK(&fss_softc[i], s); 486 if ((fss_softc[i].sc_flags & FSS_ACTIVE) != 0 && 487 fss_softc[i].sc_mount == mp) { 488 if (forced) 489 fss_error(&fss_softc[i], "forced unmount"); 490 else { 491 FSS_UNLOCK(&fss_softc[i], s); 492 return EBUSY; 493 } 494 } 495 FSS_UNLOCK(&fss_softc[i], s); 496 } 497 498 return 0; 499 } 500 501 /* 502 * A buffer is written to the snapshotted block device. Copy to 503 * backing store if needed. 504 */ 505 static int 506 fss_copy_on_write(void *v, struct buf *bp) 507 { 508 int s; 509 u_int32_t cl, ch, c; 510 struct fss_softc *sc = v; 511 512 FSS_LOCK(sc, s); 513 if (!FSS_ISVALID(sc)) { 514 FSS_UNLOCK(sc, s); 515 return 0; 516 } 517 518 FSS_UNLOCK(sc, s); 519 520 FSS_STAT_INC(sc, cow_calls); 521 522 cl = FSS_BTOCL(sc, dbtob(bp->b_blkno)); 523 ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1); 524 525 for (c = cl; c <= ch; c++) 526 fss_read_cluster(sc, c); 527 528 return 0; 529 } 530 531 /* 532 * Lookup and open needed files. 533 * 534 * For file system internal snapshot initializes sc_mntname, sc_mount, 535 * sc_bs_vp and sc_time. 536 * 537 * Otherwise returns dev and size of the underlying block device. 538 * Initializes sc_mntname, sc_mount_vp, sc_bdev, sc_bs_vp and sc_mount 539 */ 540 static int 541 fss_create_files(struct fss_softc *sc, struct fss_set *fss, 542 off_t *bsize, struct proc *p) 543 { 544 int error, bits, fsbsize; 545 struct timespec ts; 546 struct partinfo dpart; 547 struct vattr va; 548 struct nameidata nd; 549 550 /* 551 * Get the mounted file system. 552 */ 553 554 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_mount, p); 555 if ((error = namei(&nd)) != 0) 556 return error; 557 558 if ((nd.ni_vp->v_flag & VROOT) != VROOT) { 559 vrele(nd.ni_vp); 560 return EINVAL; 561 } 562 563 sc->sc_mount = nd.ni_vp->v_mount; 564 memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN); 565 566 vrele(nd.ni_vp); 567 568 /* 569 * Check for file system internal snapshot. 570 */ 571 572 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p); 573 if ((error = namei(&nd)) != 0) 574 return error; 575 576 if (nd.ni_vp->v_type == VREG && nd.ni_vp->v_mount == sc->sc_mount) { 577 vrele(nd.ni_vp); 578 sc->sc_flags |= FSS_PERSISTENT; 579 580 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p); 581 if ((error = vn_open(&nd, FREAD, 0)) != 0) 582 return error; 583 sc->sc_bs_vp = nd.ni_vp; 584 585 fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize; 586 bits = sizeof(sc->sc_bs_bshift)*NBBY; 587 for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < bits; 588 sc->sc_bs_bshift++) 589 if (FSS_FSBSIZE(sc) == fsbsize) 590 break; 591 if (sc->sc_bs_bshift >= bits) { 592 VOP_UNLOCK(sc->sc_bs_vp, 0); 593 return EINVAL; 594 } 595 596 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 597 sc->sc_clshift = 0; 598 599 error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts); 600 TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts); 601 602 VOP_UNLOCK(sc->sc_bs_vp, 0); 603 604 return error; 605 } 606 vrele(nd.ni_vp); 607 608 /* 609 * Get the block device it is mounted on. 610 */ 611 612 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, 613 sc->sc_mount->mnt_stat.f_mntfromname, p); 614 if ((error = namei(&nd)) != 0) 615 return error; 616 617 if (nd.ni_vp->v_type != VBLK) { 618 vrele(nd.ni_vp); 619 return EINVAL; 620 } 621 622 error = VOP_IOCTL(nd.ni_vp, DIOCGPART, &dpart, FREAD, p->p_ucred, p); 623 if (error) { 624 vrele(nd.ni_vp); 625 return error; 626 } 627 628 sc->sc_mount_vp = nd.ni_vp; 629 sc->sc_bdev = nd.ni_vp->v_rdev; 630 *bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size; 631 vrele(nd.ni_vp); 632 633 /* 634 * Get the backing store 635 */ 636 637 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p); 638 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) 639 return error; 640 VOP_UNLOCK(nd.ni_vp, 0); 641 642 sc->sc_bs_vp = nd.ni_vp; 643 644 if (nd.ni_vp->v_type != VREG && nd.ni_vp->v_type != VCHR) 645 return EINVAL; 646 647 if (sc->sc_bs_vp->v_type == VREG) { 648 error = VOP_GETATTR(sc->sc_bs_vp, &va, p->p_ucred, p); 649 if (error != 0) 650 return error; 651 sc->sc_bs_size = va.va_size; 652 fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize; 653 if (fsbsize & (fsbsize-1)) /* No power of two */ 654 return EINVAL; 655 for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32; 656 sc->sc_bs_bshift++) 657 if (FSS_FSBSIZE(sc) == fsbsize) 658 break; 659 if (sc->sc_bs_bshift >= 32) 660 return EINVAL; 661 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 662 sc->sc_flags |= FSS_BS_ALLOC; 663 } else { 664 sc->sc_bs_bshift = DEV_BSHIFT; 665 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 666 sc->sc_flags &= ~FSS_BS_ALLOC; 667 } 668 669 /* 670 * As all IO to from/to the backing store goes through 671 * VOP_STRATEGY() clean the buffer cache to prevent 672 * cache incoherencies. 673 */ 674 if ((error = vinvalbuf(sc->sc_bs_vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) 675 return error; 676 677 return 0; 678 } 679 680 /* 681 * Create a snapshot. 682 */ 683 static int 684 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct proc *p) 685 { 686 int len, error; 687 u_int32_t csize; 688 off_t bsize; 689 690 /* 691 * Open needed files. 692 */ 693 if ((error = fss_create_files(sc, fss, &bsize, p)) != 0) 694 goto bad; 695 696 if (sc->sc_flags & FSS_PERSISTENT) { 697 fss_softc_alloc(sc); 698 sc->sc_flags |= FSS_ACTIVE; 699 return 0; 700 } 701 702 /* 703 * Set cluster size. Must be a power of two and 704 * a multiple of backing store block size. 705 */ 706 if (fss->fss_csize <= 0) 707 csize = MAXPHYS; 708 else 709 csize = fss->fss_csize; 710 if (bsize/csize > FSS_CLUSTER_MAX) 711 csize = bsize/FSS_CLUSTER_MAX+1; 712 713 for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32; 714 sc->sc_clshift++) 715 if (FSS_CLSIZE(sc) >= csize) 716 break; 717 if (sc->sc_clshift >= 32) { 718 error = EINVAL; 719 goto bad; 720 } 721 sc->sc_clmask = FSS_CLSIZE(sc)-1; 722 723 /* 724 * Set number of cache slots. 725 */ 726 if (FSS_CLSIZE(sc) <= 8192) 727 sc->sc_cache_size = 32; 728 else if (FSS_CLSIZE(sc) <= 65536) 729 sc->sc_cache_size = 8; 730 else 731 sc->sc_cache_size = 4; 732 733 /* 734 * Set number of clusters and size of last cluster. 735 */ 736 sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1; 737 sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1; 738 739 /* 740 * Set size of indirect table. 741 */ 742 len = sc->sc_clcount*sizeof(u_int32_t); 743 sc->sc_indir_size = FSS_BTOCL(sc, len)+1; 744 sc->sc_clnext = sc->sc_indir_size; 745 sc->sc_indir_cur = 0; 746 747 if ((error = fss_softc_alloc(sc)) != 0) 748 goto bad; 749 750 /* 751 * Activate the snapshot. 752 */ 753 754 if ((error = vfs_write_suspend(sc->sc_mount, PUSER|PCATCH, 0)) != 0) 755 goto bad; 756 757 microtime(&sc->sc_time); 758 759 if (error == 0) 760 error = vn_cow_establish(sc->sc_mount_vp, 761 fss_copy_on_write, sc); 762 if (error == 0) 763 sc->sc_flags |= FSS_ACTIVE; 764 765 vfs_write_resume(sc->sc_mount); 766 767 if (error != 0) 768 goto bad; 769 770 #ifdef DEBUG 771 printf("fss%d: %s snapshot active\n", sc->sc_unit, sc->sc_mntname); 772 printf("fss%d: %u clusters of %u, %u cache slots, %u indir clusters\n", 773 sc->sc_unit, sc->sc_clcount, FSS_CLSIZE(sc), 774 sc->sc_cache_size, sc->sc_indir_size); 775 #endif 776 777 return 0; 778 779 bad: 780 fss_softc_free(sc); 781 if (sc->sc_bs_vp != NULL) { 782 if (sc->sc_flags & FSS_PERSISTENT) 783 vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p); 784 else 785 vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p); 786 } 787 sc->sc_bs_vp = NULL; 788 789 return error; 790 } 791 792 /* 793 * Delete a snapshot. 794 */ 795 static int 796 fss_delete_snapshot(struct fss_softc *sc, struct proc *p) 797 { 798 int s; 799 800 if ((sc->sc_flags & FSS_PERSISTENT) == 0) 801 vn_cow_disestablish(sc->sc_mount_vp, fss_copy_on_write, sc); 802 803 FSS_LOCK(sc, s); 804 sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR); 805 sc->sc_mount = NULL; 806 sc->sc_bdev = NODEV; 807 FSS_UNLOCK(sc, s); 808 809 fss_softc_free(sc); 810 if (sc->sc_flags & FSS_PERSISTENT) 811 vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p); 812 else 813 vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p); 814 sc->sc_bs_vp = NULL; 815 sc->sc_flags &= ~(FSS_PERSISTENT|FSS_BS_ALLOC); 816 817 FSS_STAT_CLEAR(sc); 818 819 return 0; 820 } 821 822 /* 823 * Get the block address and number of contiguous blocks. 824 * If the file contains a hole, try to allocate. 825 * Backing store is locked by caller. 826 */ 827 static int 828 fss_bmap(struct fss_softc *sc, off_t start, int len, 829 struct vnode **vpp, daddr_t *bnp, int *runp) 830 { 831 int l, s, error; 832 struct buf *bp, **bpp; 833 834 if ((sc->sc_bs_vp->v_mount->mnt_flag & MNT_SOFTDEP) != 0) 835 bpp = &bp; 836 else 837 bpp = NULL; 838 839 error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp); 840 if ((error == 0 && *bnp != (daddr_t)-1) || 841 (sc->sc_flags & FSS_BS_ALLOC) == 0) 842 goto out; 843 844 if (start+len >= sc->sc_bs_size) { 845 error = ENOSPC; 846 goto out; 847 } 848 849 for (l = 0; l < len; l += FSS_FSBSIZE(sc)) { 850 error = VOP_BALLOC(sc->sc_bs_vp, start+l, FSS_FSBSIZE(sc), 851 sc->sc_bs_proc->p_ucred, 0, bpp); 852 if (error) 853 goto out; 854 855 if (bpp == NULL) 856 continue; 857 858 s = splbio(); 859 simple_lock(&bp->b_interlock); 860 861 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_start) 862 (*bioops.io_start)(bp); 863 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete) 864 (*bioops.io_complete)(bp); 865 866 bp->b_flags |= B_INVAL; 867 simple_unlock(&bp->b_interlock); 868 splx(s); 869 870 brelse(bp); 871 } 872 873 error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp); 874 875 out: 876 877 if ((sc->sc_flags & FSS_PERSISTENT) == 0 && 878 error == 0 && *bnp == (daddr_t)-1) 879 error = ENOSPC; 880 881 return error; 882 } 883 884 /* 885 * A read from the snapshotted block device has completed. 886 */ 887 static void 888 fss_cluster_iodone(struct buf *bp) 889 { 890 int s; 891 struct fss_cache *scp = bp->b_private; 892 893 FSS_LOCK(scp->fc_softc, s); 894 895 if (bp->b_flags & B_EINTR) 896 fss_error(scp->fc_softc, "fs read interrupted"); 897 if (bp->b_flags & B_ERROR) 898 fss_error(scp->fc_softc, "fs read error %d", bp->b_error); 899 900 if (bp->b_vp != NULL) 901 brelvp(bp); 902 903 if (--scp->fc_xfercount == 0) 904 wakeup(&scp->fc_data); 905 906 FSS_UNLOCK(scp->fc_softc, s); 907 908 s = splbio(); 909 pool_put(&bufpool, bp); 910 splx(s); 911 } 912 913 /* 914 * Read a cluster from the snapshotted block device to the cache. 915 */ 916 static void 917 fss_read_cluster(struct fss_softc *sc, u_int32_t cl) 918 { 919 int s, todo, len; 920 caddr_t addr; 921 daddr_t dblk; 922 struct buf *bp; 923 struct fss_cache *scp, *scl; 924 925 /* 926 * Get a free cache slot. 927 */ 928 scl = sc->sc_cache+sc->sc_cache_size; 929 930 FSS_LOCK(sc, s); 931 932 restart: 933 if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) { 934 FSS_UNLOCK(sc, s); 935 return; 936 } 937 938 for (scp = sc->sc_cache; scp < scl; scp++) 939 if (scp->fc_type != FSS_CACHE_FREE && 940 scp->fc_cluster == cl) { 941 ltsleep(&scp->fc_type, PRIBIO, "cowwait2", 0, 942 &sc->sc_slock); 943 goto restart; 944 } 945 946 for (scp = sc->sc_cache; scp < scl; scp++) 947 if (scp->fc_type == FSS_CACHE_FREE) { 948 scp->fc_type = FSS_CACHE_BUSY; 949 scp->fc_cluster = cl; 950 break; 951 } 952 if (scp >= scl) { 953 FSS_STAT_INC(sc, cow_cache_full); 954 ltsleep(&sc->sc_cache, PRIBIO, "cowwait3", 0, &sc->sc_slock); 955 goto restart; 956 } 957 958 FSS_UNLOCK(sc, s); 959 960 /* 961 * Start the read. 962 */ 963 FSS_STAT_INC(sc, cow_copied); 964 965 dblk = btodb(FSS_CLTOB(sc, cl)); 966 addr = scp->fc_data; 967 if (cl == sc->sc_clcount-1) { 968 todo = sc->sc_clresid; 969 memset(addr+todo, 0, FSS_CLSIZE(sc)-todo); 970 } else 971 todo = FSS_CLSIZE(sc); 972 while (todo > 0) { 973 len = todo; 974 if (len > MAXPHYS) 975 len = MAXPHYS; 976 977 s = splbio(); 978 bp = pool_get(&bufpool, PR_WAITOK); 979 splx(s); 980 981 BUF_INIT(bp); 982 bp->b_flags = B_READ|B_CALL; 983 bp->b_bcount = len; 984 bp->b_bufsize = bp->b_bcount; 985 bp->b_error = 0; 986 bp->b_data = addr; 987 bp->b_blkno = bp->b_rawblkno = dblk; 988 bp->b_proc = NULL; 989 bp->b_dev = sc->sc_bdev; 990 bp->b_vp = NULLVP; 991 bp->b_private = scp; 992 bp->b_iodone = fss_cluster_iodone; 993 994 DEV_STRATEGY(bp); 995 996 FSS_LOCK(sc, s); 997 scp->fc_xfercount++; 998 FSS_UNLOCK(sc, s); 999 1000 dblk += btodb(len); 1001 addr += len; 1002 todo -= len; 1003 } 1004 1005 /* 1006 * Wait for all read requests to complete. 1007 */ 1008 FSS_LOCK(sc, s); 1009 while (scp->fc_xfercount > 0) 1010 ltsleep(&scp->fc_data, PRIBIO, "cowwait", 0, &sc->sc_slock); 1011 1012 scp->fc_type = FSS_CACHE_VALID; 1013 setbit(sc->sc_copied, scp->fc_cluster); 1014 FSS_UNLOCK(sc, s); 1015 1016 wakeup(&sc->sc_bs_proc); 1017 } 1018 1019 /* 1020 * Write a cluster from the cache to the backing store. 1021 */ 1022 static int 1023 fss_write_cluster(struct fss_cache *scp, u_int32_t cl) 1024 { 1025 int s, error, todo, len, nra; 1026 daddr_t nbn; 1027 caddr_t addr; 1028 off_t pos; 1029 struct buf *bp; 1030 struct vnode *vp; 1031 struct fss_softc *sc; 1032 1033 error = 0; 1034 sc = scp->fc_softc; 1035 1036 pos = FSS_CLTOB(sc, cl); 1037 addr = scp->fc_data; 1038 todo = FSS_CLSIZE(sc); 1039 1040 vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY); 1041 simple_lock(&sc->sc_bs_vp->v_interlock); 1042 error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(pos), 1043 round_page(pos+todo), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE); 1044 1045 while (error == 0 && todo > 0) { 1046 error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra); 1047 if (error) 1048 break; 1049 1050 len = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos); 1051 if (len > todo) 1052 len = todo; 1053 1054 s = splbio(); 1055 bp = pool_get(&bufpool, PR_WAITOK); 1056 splx(s); 1057 1058 BUF_INIT(bp); 1059 bp->b_flags = B_CALL; 1060 bp->b_bcount = len; 1061 bp->b_bufsize = bp->b_bcount; 1062 bp->b_error = 0; 1063 bp->b_data = addr; 1064 bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos)); 1065 bp->b_proc = NULL; 1066 bp->b_vp = NULLVP; 1067 bp->b_private = scp; 1068 bp->b_iodone = fss_cluster_iodone; 1069 bgetvp(vp, bp); 1070 bp->b_vp->v_numoutput++; 1071 1072 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 1073 VOP_STRATEGY(vp, bp); 1074 1075 FSS_LOCK(sc, s); 1076 scp->fc_xfercount++; 1077 FSS_UNLOCK(sc, s); 1078 1079 pos += len; 1080 addr += len; 1081 todo -= len; 1082 } 1083 1084 /* 1085 * Wait for all write requests to complete. 1086 */ 1087 FSS_LOCK(sc, s); 1088 while (scp->fc_xfercount > 0) 1089 ltsleep(&scp->fc_data, PRIBIO, "bswwait", 0, &sc->sc_slock); 1090 FSS_UNLOCK(sc, s); 1091 1092 VOP_UNLOCK(sc->sc_bs_vp, 0); 1093 1094 return error; 1095 } 1096 1097 /* 1098 * Read/write clusters from/to backing store. 1099 * For persistent snapshots must be called with cl == 0. off is the 1100 * offset into the snapshot. 1101 */ 1102 static int 1103 fss_bs_io(struct fss_softc *sc, fss_io_type rw, 1104 u_int32_t cl, off_t off, int len, caddr_t data) 1105 { 1106 int s, error, todo, count, nra; 1107 off_t pos; 1108 daddr_t nbn; 1109 struct buf *bp; 1110 struct vnode *vp; 1111 1112 todo = len; 1113 pos = FSS_CLTOB(sc, cl)+off; 1114 error = 0; 1115 1116 vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY); 1117 simple_lock(&sc->sc_bs_vp->v_interlock); 1118 error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(pos), 1119 round_page(pos+todo), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE); 1120 1121 while (error == 0 && todo > 0) { 1122 error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra); 1123 if (error) 1124 break; 1125 1126 count = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos); 1127 if (count > todo) 1128 count = todo; 1129 1130 if (nbn == (daddr_t)-1) { 1131 bzero(data, count); 1132 todo -= count; 1133 data += count; 1134 pos += count; 1135 continue; 1136 } 1137 1138 s = splbio(); 1139 bp = pool_get(&bufpool, PR_WAITOK); 1140 splx(s); 1141 1142 BUF_INIT(bp); 1143 bp->b_flags = (rw == FSS_READ ? B_READ : 0); 1144 bp->b_bcount = count; 1145 bp->b_bufsize = bp->b_bcount; 1146 bp->b_error = 0; 1147 bp->b_data = data; 1148 bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos)); 1149 bp->b_proc = NULL; 1150 bp->b_vp = NULLVP; 1151 bgetvp(vp, bp); 1152 if ((bp->b_flags & B_READ) == 0) 1153 bp->b_vp->v_numoutput++; 1154 1155 if ((bp->b_flags & B_READ) == 0 || cl < sc->sc_indir_size) 1156 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 1157 VOP_STRATEGY(vp, bp); 1158 1159 error = biowait(bp); 1160 1161 if (bp->b_vp != NULL) 1162 brelvp(bp); 1163 1164 s = splbio(); 1165 pool_put(&bufpool, bp); 1166 splx(s); 1167 1168 if (error) 1169 break; 1170 1171 todo -= count; 1172 data += count; 1173 pos += count; 1174 } 1175 1176 VOP_UNLOCK(sc->sc_bs_vp, 0); 1177 1178 return error; 1179 } 1180 1181 /* 1182 * Get a pointer to the indirect slot for this cluster. 1183 */ 1184 static u_int32_t * 1185 fss_bs_indir(struct fss_softc *sc, u_int32_t cl) 1186 { 1187 u_int32_t icl; 1188 int ioff; 1189 1190 icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t)); 1191 ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t)); 1192 1193 if (sc->sc_indir_cur == icl) 1194 return &sc->sc_indir_data[ioff]; 1195 1196 if (sc->sc_indir_dirty) { 1197 FSS_STAT_INC(sc, indir_write); 1198 if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0, 1199 FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0) 1200 return NULL; 1201 setbit(sc->sc_indir_valid, sc->sc_indir_cur); 1202 } 1203 1204 sc->sc_indir_dirty = 0; 1205 sc->sc_indir_cur = icl; 1206 1207 if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) { 1208 FSS_STAT_INC(sc, indir_read); 1209 if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0, 1210 FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0) 1211 return NULL; 1212 } else 1213 memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc)); 1214 1215 return &sc->sc_indir_data[ioff]; 1216 } 1217 1218 /* 1219 * The kernel thread (one for every active snapshot). 1220 * 1221 * After wakeup it cleans the cache and runs the I/O requests. 1222 */ 1223 static void 1224 fss_bs_thread(void *arg) 1225 { 1226 int error, len, nfreed, nio, s; 1227 long off; 1228 caddr_t addr; 1229 u_int32_t c, cl, ch, *indirp; 1230 struct buf *bp, *nbp; 1231 struct fss_softc *sc; 1232 struct fss_cache *scp, *scl; 1233 1234 sc = arg; 1235 1236 scl = sc->sc_cache+sc->sc_cache_size; 1237 1238 s = splbio(); 1239 nbp = pool_get(&bufpool, PR_WAITOK); 1240 splx(s); 1241 1242 nfreed = nio = 1; /* Dont sleep the first time */ 1243 1244 FSS_LOCK(sc, s); 1245 1246 for (;;) { 1247 if (nfreed == 0 && nio == 0) 1248 ltsleep(&sc->sc_bs_proc, PVM-1, "fssbs", 0, 1249 &sc->sc_slock); 1250 1251 if ((sc->sc_flags & FSS_BS_THREAD) == 0) { 1252 sc->sc_bs_proc = NULL; 1253 wakeup(&sc->sc_bs_proc); 1254 1255 FSS_UNLOCK(sc, s); 1256 1257 s = splbio(); 1258 pool_put(&bufpool, nbp); 1259 splx(s); 1260 #ifdef FSS_STATISTICS 1261 if ((sc->sc_flags & FSS_PERSISTENT) == 0) { 1262 printf("fss%d: cow called %" PRId64 " times," 1263 " copied %" PRId64 " clusters," 1264 " cache full %" PRId64 " times\n", 1265 sc->sc_unit, 1266 FSS_STAT_VAL(sc, cow_calls), 1267 FSS_STAT_VAL(sc, cow_copied), 1268 FSS_STAT_VAL(sc, cow_cache_full)); 1269 printf("fss%d: %" PRId64 " indir reads," 1270 " %" PRId64 " indir writes\n", 1271 sc->sc_unit, 1272 FSS_STAT_VAL(sc, indir_read), 1273 FSS_STAT_VAL(sc, indir_write)); 1274 } 1275 #endif /* FSS_STATISTICS */ 1276 kthread_exit(0); 1277 } 1278 1279 /* 1280 * Process I/O requests (persistent) 1281 */ 1282 1283 if (sc->sc_flags & FSS_PERSISTENT) { 1284 nfreed = nio = 0; 1285 1286 if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL) 1287 continue; 1288 1289 nio++; 1290 1291 if (FSS_ISVALID(sc)) { 1292 FSS_UNLOCK(sc, s); 1293 1294 error = fss_bs_io(sc, FSS_READ, 0, 1295 dbtob(bp->b_blkno), bp->b_bcount, 1296 bp->b_data); 1297 1298 FSS_LOCK(sc, s); 1299 } else 1300 error = ENXIO; 1301 1302 if (error) { 1303 bp->b_error = error; 1304 bp->b_flags |= B_ERROR; 1305 bp->b_resid = bp->b_bcount; 1306 } 1307 biodone(bp); 1308 1309 continue; 1310 } 1311 1312 /* 1313 * Clean the cache 1314 */ 1315 nfreed = 0; 1316 for (scp = sc->sc_cache; scp < scl; scp++) { 1317 if (scp->fc_type != FSS_CACHE_VALID) 1318 continue; 1319 1320 FSS_UNLOCK(sc, s); 1321 1322 indirp = fss_bs_indir(sc, scp->fc_cluster); 1323 if (indirp != NULL) { 1324 error = fss_write_cluster(scp, sc->sc_clnext); 1325 } else 1326 error = EIO; 1327 1328 FSS_LOCK(sc, s); 1329 1330 if (error == 0) { 1331 *indirp = sc->sc_clnext++; 1332 sc->sc_indir_dirty = 1; 1333 } else 1334 fss_error(sc, "write bs error %d", error); 1335 1336 scp->fc_type = FSS_CACHE_FREE; 1337 nfreed++; 1338 wakeup(&scp->fc_type); 1339 } 1340 1341 if (nfreed) 1342 wakeup(&sc->sc_cache); 1343 1344 /* 1345 * Process I/O requests 1346 */ 1347 nio = 0; 1348 1349 if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL) 1350 continue; 1351 1352 nio++; 1353 1354 if (!FSS_ISVALID(sc)) { 1355 bp->b_error = ENXIO; 1356 bp->b_flags |= B_ERROR; 1357 bp->b_resid = bp->b_bcount; 1358 biodone(bp); 1359 continue; 1360 } 1361 1362 /* 1363 * First read from the snapshotted block device. 1364 * XXX Split to only read those parts that have not 1365 * been saved to backing store? 1366 */ 1367 1368 FSS_UNLOCK(sc, s); 1369 1370 BUF_INIT(nbp); 1371 nbp->b_flags = B_READ; 1372 nbp->b_bcount = bp->b_bcount; 1373 nbp->b_bufsize = bp->b_bcount; 1374 nbp->b_error = 0; 1375 nbp->b_data = bp->b_data; 1376 nbp->b_blkno = nbp->b_rawblkno = bp->b_blkno; 1377 nbp->b_proc = bp->b_proc; 1378 nbp->b_dev = sc->sc_bdev; 1379 nbp->b_vp = NULLVP; 1380 1381 DEV_STRATEGY(nbp); 1382 1383 if (biowait(nbp) != 0) { 1384 bp->b_resid = bp->b_bcount; 1385 bp->b_error = nbp->b_error; 1386 bp->b_flags |= B_ERROR; 1387 biodone(bp); 1388 continue; 1389 } 1390 1391 cl = FSS_BTOCL(sc, dbtob(bp->b_blkno)); 1392 off = FSS_CLOFF(sc, dbtob(bp->b_blkno)); 1393 ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1); 1394 bp->b_resid = bp->b_bcount; 1395 addr = bp->b_data; 1396 1397 FSS_LOCK(sc, s); 1398 1399 /* 1400 * Replace those parts that have been saved to backing store. 1401 */ 1402 1403 for (c = cl; c <= ch; 1404 c++, off = 0, bp->b_resid -= len, addr += len) { 1405 len = FSS_CLSIZE(sc)-off; 1406 if (len > bp->b_resid) 1407 len = bp->b_resid; 1408 1409 if (isclr(sc->sc_copied, c)) 1410 continue; 1411 1412 FSS_UNLOCK(sc, s); 1413 1414 indirp = fss_bs_indir(sc, c); 1415 1416 FSS_LOCK(sc, s); 1417 1418 if (indirp == NULL || *indirp == 0) { 1419 /* 1420 * Not on backing store. Either in cache 1421 * or hole in the snapshotted block device. 1422 */ 1423 for (scp = sc->sc_cache; scp < scl; scp++) 1424 if (scp->fc_type == FSS_CACHE_VALID && 1425 scp->fc_cluster == c) 1426 break; 1427 if (scp < scl) 1428 memcpy(addr, scp->fc_data+off, len); 1429 else 1430 memset(addr, 0, len); 1431 continue; 1432 } 1433 /* 1434 * Read from backing store. 1435 */ 1436 1437 FSS_UNLOCK(sc, s); 1438 1439 if ((error = fss_bs_io(sc, FSS_READ, *indirp, 1440 off, len, addr)) != 0) { 1441 bp->b_resid = bp->b_bcount; 1442 bp->b_error = error; 1443 bp->b_flags |= B_ERROR; 1444 break; 1445 } 1446 1447 FSS_LOCK(sc, s); 1448 1449 } 1450 1451 biodone(bp); 1452 } 1453 } 1454