1 /* $NetBSD: fss.c,v 1.81 2011/11/30 09:51:18 bouyer 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * File system snapshot disk driver. 34 * 35 * Block/character interface to the snapshot of a mounted file system. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.81 2011/11/30 09:51:18 bouyer Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/namei.h> 44 #include <sys/proc.h> 45 #include <sys/errno.h> 46 #include <sys/malloc.h> 47 #include <sys/buf.h> 48 #include <sys/ioctl.h> 49 #include <sys/disklabel.h> 50 #include <sys/device.h> 51 #include <sys/disk.h> 52 #include <sys/stat.h> 53 #include <sys/mount.h> 54 #include <sys/vnode.h> 55 #include <sys/file.h> 56 #include <sys/uio.h> 57 #include <sys/conf.h> 58 #include <sys/kthread.h> 59 #include <sys/fstrans.h> 60 #include <sys/simplelock.h> 61 #include <sys/vfs_syscalls.h> /* For do_sys_unlink(). */ 62 63 #include <miscfs/specfs/specdev.h> 64 65 #include <dev/fssvar.h> 66 67 #include <uvm/uvm.h> 68 69 void fssattach(int); 70 71 dev_type_open(fss_open); 72 dev_type_close(fss_close); 73 dev_type_read(fss_read); 74 dev_type_write(fss_write); 75 dev_type_ioctl(fss_ioctl); 76 dev_type_strategy(fss_strategy); 77 dev_type_dump(fss_dump); 78 dev_type_size(fss_size); 79 80 static void fss_unmount_hook(struct mount *); 81 static int fss_copy_on_write(void *, struct buf *, bool); 82 static inline void fss_error(struct fss_softc *, const char *); 83 static int fss_create_files(struct fss_softc *, struct fss_set *, 84 off_t *, struct lwp *); 85 static int fss_create_snapshot(struct fss_softc *, struct fss_set *, 86 struct lwp *); 87 static int fss_delete_snapshot(struct fss_softc *, struct lwp *); 88 static int fss_softc_alloc(struct fss_softc *); 89 static void fss_softc_free(struct fss_softc *); 90 static int fss_read_cluster(struct fss_softc *, u_int32_t); 91 static void fss_bs_thread(void *); 92 static int fss_bs_io(struct fss_softc *, fss_io_type, 93 u_int32_t, off_t, int, void *); 94 static u_int32_t *fss_bs_indir(struct fss_softc *, u_int32_t); 95 96 static kmutex_t fss_device_lock; /* Protect all units. */ 97 static int fss_num_attached = 0; /* Number of attached devices. */ 98 static struct vfs_hooks fss_vfs_hooks = { 99 .vh_unmount = fss_unmount_hook 100 }; 101 102 const struct bdevsw fss_bdevsw = { 103 fss_open, fss_close, fss_strategy, fss_ioctl, 104 fss_dump, fss_size, D_DISK | D_MPSAFE 105 }; 106 107 const struct cdevsw fss_cdevsw = { 108 fss_open, fss_close, fss_read, fss_write, fss_ioctl, 109 nostop, notty, nopoll, nommap, nokqfilter, D_DISK | D_MPSAFE 110 }; 111 112 static int fss_match(device_t, cfdata_t, void *); 113 static void fss_attach(device_t, device_t, void *); 114 static int fss_detach(device_t, int); 115 116 CFATTACH_DECL_NEW(fss, sizeof(struct fss_softc), 117 fss_match, fss_attach, fss_detach, NULL); 118 extern struct cfdriver fss_cd; 119 120 void 121 fssattach(int num) 122 { 123 124 mutex_init(&fss_device_lock, MUTEX_DEFAULT, IPL_NONE); 125 if (config_cfattach_attach(fss_cd.cd_name, &fss_ca)) 126 aprint_error("%s: unable to register\n", fss_cd.cd_name); 127 } 128 129 static int 130 fss_match(device_t self, cfdata_t cfdata, void *aux) 131 { 132 return 1; 133 } 134 135 static void 136 fss_attach(device_t parent, device_t self, void *aux) 137 { 138 struct fss_softc *sc = device_private(self); 139 140 sc->sc_dev = self; 141 sc->sc_bdev = NODEV; 142 mutex_init(&sc->sc_slock, MUTEX_DEFAULT, IPL_NONE); 143 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 144 cv_init(&sc->sc_work_cv, "fssbs"); 145 cv_init(&sc->sc_cache_cv, "cowwait"); 146 bufq_alloc(&sc->sc_bufq, "fcfs", 0); 147 sc->sc_dkdev = malloc(sizeof(*sc->sc_dkdev), M_DEVBUF, M_WAITOK); 148 sc->sc_dkdev->dk_info = NULL; 149 disk_init(sc->sc_dkdev, device_xname(self), NULL); 150 if (!pmf_device_register(self, NULL, NULL)) 151 aprint_error_dev(self, "couldn't establish power handler\n"); 152 153 if (fss_num_attached++ == 0) 154 vfs_hooks_attach(&fss_vfs_hooks); 155 } 156 157 static int 158 fss_detach(device_t self, int flags) 159 { 160 struct fss_softc *sc = device_private(self); 161 162 if (sc->sc_flags & FSS_ACTIVE) 163 return EBUSY; 164 165 if (--fss_num_attached == 0) 166 vfs_hooks_detach(&fss_vfs_hooks); 167 168 pmf_device_deregister(self); 169 mutex_destroy(&sc->sc_slock); 170 mutex_destroy(&sc->sc_lock); 171 cv_destroy(&sc->sc_work_cv); 172 cv_destroy(&sc->sc_cache_cv); 173 bufq_drain(sc->sc_bufq); 174 bufq_free(sc->sc_bufq); 175 disk_destroy(sc->sc_dkdev); 176 free(sc->sc_dkdev, M_DEVBUF); 177 178 return 0; 179 } 180 181 int 182 fss_open(dev_t dev, int flags, int mode, struct lwp *l) 183 { 184 int mflag; 185 cfdata_t cf; 186 struct fss_softc *sc; 187 188 mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN); 189 190 mutex_enter(&fss_device_lock); 191 192 sc = device_lookup_private(&fss_cd, minor(dev)); 193 if (sc == NULL) { 194 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK); 195 cf->cf_name = fss_cd.cd_name; 196 cf->cf_atname = fss_cd.cd_name; 197 cf->cf_unit = minor(dev); 198 cf->cf_fstate = FSTATE_STAR; 199 sc = device_private(config_attach_pseudo(cf)); 200 if (sc == NULL) { 201 mutex_exit(&fss_device_lock); 202 return ENOMEM; 203 } 204 } 205 206 mutex_enter(&sc->sc_slock); 207 208 sc->sc_flags |= mflag; 209 210 mutex_exit(&sc->sc_slock); 211 mutex_exit(&fss_device_lock); 212 213 return 0; 214 } 215 216 int 217 fss_close(dev_t dev, int flags, int mode, struct lwp *l) 218 { 219 int mflag, error; 220 cfdata_t cf; 221 struct fss_softc *sc = device_lookup_private(&fss_cd, minor(dev)); 222 223 mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN); 224 error = 0; 225 226 restart: 227 mutex_enter(&sc->sc_slock); 228 if ((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) != mflag) { 229 sc->sc_flags &= ~mflag; 230 mutex_exit(&sc->sc_slock); 231 return 0; 232 } 233 if ((sc->sc_flags & FSS_ACTIVE) != 0 && 234 (sc->sc_uflags & FSS_UNCONFIG_ON_CLOSE) != 0) { 235 sc->sc_uflags &= ~FSS_UNCONFIG_ON_CLOSE; 236 mutex_exit(&sc->sc_slock); 237 error = fss_ioctl(dev, FSSIOCCLR, NULL, FWRITE, l); 238 goto restart; 239 } 240 if ((sc->sc_flags & FSS_ACTIVE) != 0) { 241 mutex_exit(&sc->sc_slock); 242 return error; 243 } 244 if (! mutex_tryenter(&fss_device_lock)) { 245 mutex_exit(&sc->sc_slock); 246 goto restart; 247 } 248 249 KASSERT((sc->sc_flags & FSS_ACTIVE) == 0); 250 KASSERT((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) == mflag); 251 mutex_exit(&sc->sc_slock); 252 cf = device_cfdata(sc->sc_dev); 253 error = config_detach(sc->sc_dev, DETACH_QUIET); 254 if (! error) 255 free(cf, M_DEVBUF); 256 mutex_exit(&fss_device_lock); 257 258 return error; 259 } 260 261 void 262 fss_strategy(struct buf *bp) 263 { 264 const bool write = ((bp->b_flags & B_READ) != B_READ); 265 struct fss_softc *sc = device_lookup_private(&fss_cd, minor(bp->b_dev)); 266 267 mutex_enter(&sc->sc_slock); 268 269 if (write || !FSS_ISVALID(sc)) { 270 271 mutex_exit(&sc->sc_slock); 272 273 bp->b_error = (write ? EROFS : ENXIO); 274 bp->b_resid = bp->b_bcount; 275 biodone(bp); 276 return; 277 } 278 279 bp->b_rawblkno = bp->b_blkno; 280 bufq_put(sc->sc_bufq, bp); 281 cv_signal(&sc->sc_work_cv); 282 283 mutex_exit(&sc->sc_slock); 284 } 285 286 int 287 fss_read(dev_t dev, struct uio *uio, int flags) 288 { 289 return physio(fss_strategy, NULL, dev, B_READ, minphys, uio); 290 } 291 292 int 293 fss_write(dev_t dev, struct uio *uio, int flags) 294 { 295 return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio); 296 } 297 298 int 299 fss_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 300 { 301 int error; 302 struct fss_softc *sc = device_lookup_private(&fss_cd, minor(dev)); 303 struct fss_set _fss; 304 struct fss_set *fss = (struct fss_set *)data; 305 struct fss_set50 *fss50 = (struct fss_set50 *)data; 306 struct fss_get *fsg = (struct fss_get *)data; 307 #ifndef _LP64 308 struct fss_get50 *fsg50 = (struct fss_get50 *)data; 309 #endif 310 311 switch (cmd) { 312 case FSSIOCSET50: 313 fss = &_fss; 314 fss->fss_mount = fss50->fss_mount; 315 fss->fss_bstore = fss50->fss_bstore; 316 fss->fss_csize = fss50->fss_csize; 317 fss->fss_flags = 0; 318 /* Fall through */ 319 case FSSIOCSET: 320 mutex_enter(&sc->sc_lock); 321 if ((flag & FWRITE) == 0) 322 error = EPERM; 323 else if ((sc->sc_flags & FSS_ACTIVE) != 0) 324 error = EBUSY; 325 else 326 error = fss_create_snapshot(sc, fss, l); 327 if (error == 0) 328 sc->sc_uflags = fss->fss_flags; 329 mutex_exit(&sc->sc_lock); 330 break; 331 332 case FSSIOCCLR: 333 mutex_enter(&sc->sc_lock); 334 if ((flag & FWRITE) == 0) 335 error = EPERM; 336 else if ((sc->sc_flags & FSS_ACTIVE) == 0) 337 error = ENXIO; 338 else 339 error = fss_delete_snapshot(sc, l); 340 mutex_exit(&sc->sc_lock); 341 break; 342 343 #ifndef _LP64 344 case FSSIOCGET50: 345 mutex_enter(&sc->sc_lock); 346 switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) { 347 case FSS_ACTIVE: 348 memcpy(fsg50->fsg_mount, sc->sc_mntname, MNAMELEN); 349 fsg50->fsg_csize = FSS_CLSIZE(sc); 350 timeval_to_timeval50(&sc->sc_time, &fsg50->fsg_time); 351 fsg50->fsg_mount_size = sc->sc_clcount; 352 fsg50->fsg_bs_size = sc->sc_clnext; 353 error = 0; 354 break; 355 case FSS_PERSISTENT | FSS_ACTIVE: 356 memcpy(fsg50->fsg_mount, sc->sc_mntname, MNAMELEN); 357 fsg50->fsg_csize = 0; 358 timeval_to_timeval50(&sc->sc_time, &fsg50->fsg_time); 359 fsg50->fsg_mount_size = 0; 360 fsg50->fsg_bs_size = 0; 361 error = 0; 362 break; 363 default: 364 error = ENXIO; 365 break; 366 } 367 mutex_exit(&sc->sc_lock); 368 break; 369 #endif /* _LP64 */ 370 371 case FSSIOCGET: 372 mutex_enter(&sc->sc_lock); 373 switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) { 374 case FSS_ACTIVE: 375 memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN); 376 fsg->fsg_csize = FSS_CLSIZE(sc); 377 fsg->fsg_time = sc->sc_time; 378 fsg->fsg_mount_size = sc->sc_clcount; 379 fsg->fsg_bs_size = sc->sc_clnext; 380 error = 0; 381 break; 382 case FSS_PERSISTENT | FSS_ACTIVE: 383 memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN); 384 fsg->fsg_csize = 0; 385 fsg->fsg_time = sc->sc_time; 386 fsg->fsg_mount_size = 0; 387 fsg->fsg_bs_size = 0; 388 error = 0; 389 break; 390 default: 391 error = ENXIO; 392 break; 393 } 394 mutex_exit(&sc->sc_lock); 395 break; 396 397 case FSSIOFSET: 398 mutex_enter(&sc->sc_slock); 399 sc->sc_uflags = *(int *)data; 400 mutex_exit(&sc->sc_slock); 401 error = 0; 402 break; 403 404 case FSSIOFGET: 405 mutex_enter(&sc->sc_slock); 406 *(int *)data = sc->sc_uflags; 407 mutex_exit(&sc->sc_slock); 408 error = 0; 409 break; 410 411 default: 412 error = EINVAL; 413 break; 414 } 415 416 return error; 417 } 418 419 int 420 fss_size(dev_t dev) 421 { 422 return -1; 423 } 424 425 int 426 fss_dump(dev_t dev, daddr_t blkno, void *va, 427 size_t size) 428 { 429 return EROFS; 430 } 431 432 /* 433 * An error occurred reading or writing the snapshot or backing store. 434 * If it is the first error log to console. 435 * The caller holds the mutex. 436 */ 437 static inline void 438 fss_error(struct fss_softc *sc, const char *msg) 439 { 440 441 if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE) 442 aprint_error_dev(sc->sc_dev, "snapshot invalid: %s\n", msg); 443 if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE) 444 sc->sc_flags |= FSS_ERROR; 445 } 446 447 /* 448 * Allocate the variable sized parts of the softc and 449 * fork the kernel thread. 450 * 451 * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size 452 * must be initialized. 453 */ 454 static int 455 fss_softc_alloc(struct fss_softc *sc) 456 { 457 int i, error; 458 459 if ((sc->sc_flags & FSS_PERSISTENT) == 0) { 460 sc->sc_copied = 461 kmem_zalloc(howmany(sc->sc_clcount, NBBY), KM_SLEEP); 462 if (sc->sc_copied == NULL) 463 return(ENOMEM); 464 465 sc->sc_cache = kmem_alloc(sc->sc_cache_size * 466 sizeof(struct fss_cache), KM_SLEEP); 467 if (sc->sc_cache == NULL) 468 return(ENOMEM); 469 470 for (i = 0; i < sc->sc_cache_size; i++) { 471 sc->sc_cache[i].fc_type = FSS_CACHE_FREE; 472 sc->sc_cache[i].fc_data = 473 kmem_alloc(FSS_CLSIZE(sc), KM_SLEEP); 474 if (sc->sc_cache[i].fc_data == NULL) 475 return(ENOMEM); 476 cv_init(&sc->sc_cache[i].fc_state_cv, "cowwait1"); 477 } 478 479 sc->sc_indir_valid = 480 kmem_zalloc(howmany(sc->sc_indir_size, NBBY), KM_SLEEP); 481 if (sc->sc_indir_valid == NULL) 482 return(ENOMEM); 483 484 sc->sc_indir_data = kmem_zalloc(FSS_CLSIZE(sc), KM_SLEEP); 485 if (sc->sc_indir_data == NULL) 486 return(ENOMEM); 487 } else { 488 sc->sc_copied = NULL; 489 sc->sc_cache = NULL; 490 sc->sc_indir_valid = NULL; 491 sc->sc_indir_data = NULL; 492 } 493 494 sc->sc_flags |= FSS_BS_THREAD; 495 if ((error = kthread_create(PRI_BIO, KTHREAD_MUSTJOIN, NULL, 496 fss_bs_thread, sc, &sc->sc_bs_lwp, 497 "%s", device_xname(sc->sc_dev))) != 0) { 498 sc->sc_flags &= ~FSS_BS_THREAD; 499 return error; 500 } 501 502 disk_attach(sc->sc_dkdev); 503 504 return 0; 505 } 506 507 /* 508 * Free the variable sized parts of the softc. 509 */ 510 static void 511 fss_softc_free(struct fss_softc *sc) 512 { 513 int i; 514 515 if ((sc->sc_flags & FSS_BS_THREAD) != 0) { 516 mutex_enter(&sc->sc_slock); 517 sc->sc_flags &= ~FSS_BS_THREAD; 518 cv_signal(&sc->sc_work_cv); 519 mutex_exit(&sc->sc_slock); 520 kthread_join(sc->sc_bs_lwp); 521 522 disk_detach(sc->sc_dkdev); 523 } 524 525 if (sc->sc_copied != NULL) 526 kmem_free(sc->sc_copied, howmany(sc->sc_clcount, NBBY)); 527 sc->sc_copied = NULL; 528 529 if (sc->sc_cache != NULL) { 530 for (i = 0; i < sc->sc_cache_size; i++) 531 if (sc->sc_cache[i].fc_data != NULL) { 532 cv_destroy(&sc->sc_cache[i].fc_state_cv); 533 kmem_free(sc->sc_cache[i].fc_data, 534 FSS_CLSIZE(sc)); 535 } 536 kmem_free(sc->sc_cache, 537 sc->sc_cache_size*sizeof(struct fss_cache)); 538 } 539 sc->sc_cache = NULL; 540 541 if (sc->sc_indir_valid != NULL) 542 kmem_free(sc->sc_indir_valid, howmany(sc->sc_indir_size, NBBY)); 543 sc->sc_indir_valid = NULL; 544 545 if (sc->sc_indir_data != NULL) 546 kmem_free(sc->sc_indir_data, FSS_CLSIZE(sc)); 547 sc->sc_indir_data = NULL; 548 } 549 550 /* 551 * Set all active snapshots on this file system into ERROR state. 552 */ 553 static void 554 fss_unmount_hook(struct mount *mp) 555 { 556 int i; 557 struct fss_softc *sc; 558 559 mutex_enter(&fss_device_lock); 560 for (i = 0; i < fss_cd.cd_ndevs; i++) { 561 if ((sc = device_lookup_private(&fss_cd, i)) == NULL) 562 continue; 563 mutex_enter(&sc->sc_slock); 564 if ((sc->sc_flags & FSS_ACTIVE) != 0 && 565 sc->sc_mount == mp) 566 fss_error(sc, "forced unmount"); 567 mutex_exit(&sc->sc_slock); 568 } 569 mutex_exit(&fss_device_lock); 570 } 571 572 /* 573 * A buffer is written to the snapshotted block device. Copy to 574 * backing store if needed. 575 */ 576 static int 577 fss_copy_on_write(void *v, struct buf *bp, bool data_valid) 578 { 579 int error; 580 u_int32_t cl, ch, c; 581 struct fss_softc *sc = v; 582 583 mutex_enter(&sc->sc_slock); 584 if (!FSS_ISVALID(sc)) { 585 mutex_exit(&sc->sc_slock); 586 return 0; 587 } 588 589 cl = FSS_BTOCL(sc, dbtob(bp->b_blkno)); 590 ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1); 591 error = 0; 592 if (curlwp == uvm.pagedaemon_lwp) { 593 for (c = cl; c <= ch; c++) 594 if (isclr(sc->sc_copied, c)) { 595 error = ENOMEM; 596 break; 597 } 598 } 599 mutex_exit(&sc->sc_slock); 600 601 if (error == 0) 602 for (c = cl; c <= ch; c++) { 603 error = fss_read_cluster(sc, c); 604 if (error) 605 break; 606 } 607 608 return error; 609 } 610 611 /* 612 * Lookup and open needed files. 613 * 614 * For file system internal snapshot initializes sc_mntname, sc_mount, 615 * sc_bs_vp and sc_time. 616 * 617 * Otherwise returns dev and size of the underlying block device. 618 * Initializes sc_mntname, sc_mount, sc_bdev, sc_bs_vp and sc_mount 619 */ 620 static int 621 fss_create_files(struct fss_softc *sc, struct fss_set *fss, 622 off_t *bsize, struct lwp *l) 623 { 624 int error, bits, fsbsize; 625 struct timespec ts; 626 struct partinfo dpart; 627 /* nd -> nd2 to reduce mistakes while updating only some namei calls */ 628 struct pathbuf *pb2; 629 struct nameidata nd2; 630 struct vnode *vp; 631 632 /* 633 * Get the mounted file system. 634 */ 635 636 error = namei_simple_user(fss->fss_mount, 637 NSM_FOLLOW_NOEMULROOT, &vp); 638 if (error != 0) 639 return error; 640 641 if ((vp->v_vflag & VV_ROOT) != VV_ROOT) { 642 vrele(vp); 643 return EINVAL; 644 } 645 646 sc->sc_mount = vp->v_mount; 647 memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN); 648 649 vrele(vp); 650 651 /* 652 * Check for file system internal snapshot. 653 */ 654 655 error = namei_simple_user(fss->fss_bstore, 656 NSM_FOLLOW_NOEMULROOT, &vp); 657 if (error != 0) 658 return error; 659 660 if (vp->v_type == VREG && vp->v_mount == sc->sc_mount) { 661 sc->sc_flags |= FSS_PERSISTENT; 662 sc->sc_bs_vp = vp; 663 664 fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize; 665 bits = sizeof(sc->sc_bs_bshift)*NBBY; 666 for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < bits; 667 sc->sc_bs_bshift++) 668 if (FSS_FSBSIZE(sc) == fsbsize) 669 break; 670 if (sc->sc_bs_bshift >= bits) 671 return EINVAL; 672 673 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 674 sc->sc_clshift = 0; 675 676 if ((fss->fss_flags & FSS_UNLINK_ON_CREATE) != 0) { 677 error = do_sys_unlink(fss->fss_bstore, UIO_USERSPACE); 678 if (error) 679 return error; 680 } 681 error = vn_lock(vp, LK_EXCLUSIVE); 682 if (error != 0) 683 return error; 684 error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts); 685 TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts); 686 687 VOP_UNLOCK(sc->sc_bs_vp); 688 689 return error; 690 } 691 vrele(vp); 692 693 /* 694 * Get the block device it is mounted on. 695 */ 696 697 error = namei_simple_kernel(sc->sc_mount->mnt_stat.f_mntfromname, 698 NSM_FOLLOW_NOEMULROOT, &vp); 699 if (error != 0) 700 return error; 701 702 if (vp->v_type != VBLK) { 703 vrele(vp); 704 return EINVAL; 705 } 706 707 sc->sc_bdev = vp->v_rdev; 708 vrele(vp); 709 710 /* 711 * Get the block device size. 712 */ 713 714 error = bdev_ioctl(sc->sc_bdev, DIOCGPART, &dpart, FREAD, l); 715 if (error) 716 return error; 717 718 *bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size; 719 720 /* 721 * Get the backing store 722 */ 723 724 error = pathbuf_copyin(fss->fss_bstore, &pb2); 725 if (error) { 726 return error; 727 } 728 NDINIT(&nd2, LOOKUP, FOLLOW, pb2); 729 if ((error = vn_open(&nd2, FREAD|FWRITE, 0)) != 0) { 730 pathbuf_destroy(pb2); 731 return error; 732 } 733 VOP_UNLOCK(nd2.ni_vp); 734 735 sc->sc_bs_vp = nd2.ni_vp; 736 737 if (nd2.ni_vp->v_type != VREG && nd2.ni_vp->v_type != VCHR) { 738 pathbuf_destroy(pb2); 739 return EINVAL; 740 } 741 pathbuf_destroy(pb2); 742 743 if ((fss->fss_flags & FSS_UNLINK_ON_CREATE) != 0) { 744 error = do_sys_unlink(fss->fss_bstore, UIO_USERSPACE); 745 if (error) 746 return error; 747 } 748 if (sc->sc_bs_vp->v_type == VREG) { 749 fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize; 750 if (fsbsize & (fsbsize-1)) /* No power of two */ 751 return EINVAL; 752 for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32; 753 sc->sc_bs_bshift++) 754 if (FSS_FSBSIZE(sc) == fsbsize) 755 break; 756 if (sc->sc_bs_bshift >= 32) 757 return EINVAL; 758 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 759 } else { 760 sc->sc_bs_bshift = DEV_BSHIFT; 761 sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1; 762 } 763 764 return 0; 765 } 766 767 /* 768 * Create a snapshot. 769 */ 770 static int 771 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct lwp *l) 772 { 773 int len, error; 774 u_int32_t csize; 775 off_t bsize; 776 777 bsize = 0; /* XXX gcc */ 778 779 /* 780 * Open needed files. 781 */ 782 if ((error = fss_create_files(sc, fss, &bsize, l)) != 0) 783 goto bad; 784 785 if (sc->sc_flags & FSS_PERSISTENT) { 786 fss_softc_alloc(sc); 787 sc->sc_flags |= FSS_ACTIVE; 788 return 0; 789 } 790 791 /* 792 * Set cluster size. Must be a power of two and 793 * a multiple of backing store block size. 794 */ 795 if (fss->fss_csize <= 0) 796 csize = MAXPHYS; 797 else 798 csize = fss->fss_csize; 799 if (bsize/csize > FSS_CLUSTER_MAX) 800 csize = bsize/FSS_CLUSTER_MAX+1; 801 802 for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32; 803 sc->sc_clshift++) 804 if (FSS_CLSIZE(sc) >= csize) 805 break; 806 if (sc->sc_clshift >= 32) { 807 error = EINVAL; 808 goto bad; 809 } 810 sc->sc_clmask = FSS_CLSIZE(sc)-1; 811 812 /* 813 * Set number of cache slots. 814 */ 815 if (FSS_CLSIZE(sc) <= 8192) 816 sc->sc_cache_size = 32; 817 else if (FSS_CLSIZE(sc) <= 65536) 818 sc->sc_cache_size = 8; 819 else 820 sc->sc_cache_size = 4; 821 822 /* 823 * Set number of clusters and size of last cluster. 824 */ 825 sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1; 826 sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1; 827 828 /* 829 * Set size of indirect table. 830 */ 831 len = sc->sc_clcount*sizeof(u_int32_t); 832 sc->sc_indir_size = FSS_BTOCL(sc, len)+1; 833 sc->sc_clnext = sc->sc_indir_size; 834 sc->sc_indir_cur = 0; 835 836 if ((error = fss_softc_alloc(sc)) != 0) 837 goto bad; 838 839 /* 840 * Activate the snapshot. 841 */ 842 843 if ((error = vfs_suspend(sc->sc_mount, 0)) != 0) 844 goto bad; 845 846 microtime(&sc->sc_time); 847 848 error = fscow_establish(sc->sc_mount, fss_copy_on_write, sc); 849 if (error == 0) 850 sc->sc_flags |= FSS_ACTIVE; 851 852 vfs_resume(sc->sc_mount); 853 854 if (error != 0) 855 goto bad; 856 857 aprint_debug_dev(sc->sc_dev, "%s snapshot active\n", sc->sc_mntname); 858 aprint_debug_dev(sc->sc_dev, 859 "%u clusters of %u, %u cache slots, %u indir clusters\n", 860 sc->sc_clcount, FSS_CLSIZE(sc), 861 sc->sc_cache_size, sc->sc_indir_size); 862 863 return 0; 864 865 bad: 866 fss_softc_free(sc); 867 if (sc->sc_bs_vp != NULL) { 868 if (sc->sc_flags & FSS_PERSISTENT) 869 vrele(sc->sc_bs_vp); 870 else 871 vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_cred); 872 } 873 sc->sc_bs_vp = NULL; 874 875 return error; 876 } 877 878 /* 879 * Delete a snapshot. 880 */ 881 static int 882 fss_delete_snapshot(struct fss_softc *sc, struct lwp *l) 883 { 884 885 if ((sc->sc_flags & FSS_PERSISTENT) == 0) 886 fscow_disestablish(sc->sc_mount, fss_copy_on_write, sc); 887 888 mutex_enter(&sc->sc_slock); 889 sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR); 890 sc->sc_mount = NULL; 891 sc->sc_bdev = NODEV; 892 mutex_exit(&sc->sc_slock); 893 894 fss_softc_free(sc); 895 if (sc->sc_flags & FSS_PERSISTENT) 896 vrele(sc->sc_bs_vp); 897 else 898 vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_cred); 899 sc->sc_bs_vp = NULL; 900 sc->sc_flags &= ~FSS_PERSISTENT; 901 902 return 0; 903 } 904 905 /* 906 * Read a cluster from the snapshotted block device to the cache. 907 */ 908 static int 909 fss_read_cluster(struct fss_softc *sc, u_int32_t cl) 910 { 911 int error, todo, offset, len; 912 daddr_t dblk; 913 struct buf *bp, *mbp; 914 struct fss_cache *scp, *scl; 915 916 /* 917 * Get a free cache slot. 918 */ 919 scl = sc->sc_cache+sc->sc_cache_size; 920 921 mutex_enter(&sc->sc_slock); 922 923 restart: 924 if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) { 925 mutex_exit(&sc->sc_slock); 926 return 0; 927 } 928 929 for (scp = sc->sc_cache; scp < scl; scp++) 930 if (scp->fc_cluster == cl) { 931 if (scp->fc_type == FSS_CACHE_VALID) { 932 mutex_exit(&sc->sc_slock); 933 return 0; 934 } else if (scp->fc_type == FSS_CACHE_BUSY) { 935 cv_wait(&scp->fc_state_cv, &sc->sc_slock); 936 goto restart; 937 } 938 } 939 940 for (scp = sc->sc_cache; scp < scl; scp++) 941 if (scp->fc_type == FSS_CACHE_FREE) { 942 scp->fc_type = FSS_CACHE_BUSY; 943 scp->fc_cluster = cl; 944 break; 945 } 946 if (scp >= scl) { 947 cv_wait(&sc->sc_cache_cv, &sc->sc_slock); 948 goto restart; 949 } 950 951 mutex_exit(&sc->sc_slock); 952 953 /* 954 * Start the read. 955 */ 956 dblk = btodb(FSS_CLTOB(sc, cl)); 957 if (cl == sc->sc_clcount-1) { 958 todo = sc->sc_clresid; 959 memset((char *)scp->fc_data + todo, 0, FSS_CLSIZE(sc) - todo); 960 } else 961 todo = FSS_CLSIZE(sc); 962 offset = 0; 963 mbp = getiobuf(NULL, true); 964 mbp->b_bufsize = todo; 965 mbp->b_data = scp->fc_data; 966 mbp->b_resid = mbp->b_bcount = todo; 967 mbp->b_flags = B_READ; 968 mbp->b_cflags = BC_BUSY; 969 mbp->b_dev = sc->sc_bdev; 970 while (todo > 0) { 971 len = todo; 972 if (len > MAXPHYS) 973 len = MAXPHYS; 974 if (btodb(FSS_CLTOB(sc, cl)) == dblk && len == todo) 975 bp = mbp; 976 else { 977 bp = getiobuf(NULL, true); 978 nestiobuf_setup(mbp, bp, offset, len); 979 } 980 bp->b_lblkno = 0; 981 bp->b_blkno = dblk; 982 bdev_strategy(bp); 983 dblk += btodb(len); 984 offset += len; 985 todo -= len; 986 } 987 error = biowait(mbp); 988 putiobuf(mbp); 989 990 mutex_enter(&sc->sc_slock); 991 scp->fc_type = (error ? FSS_CACHE_FREE : FSS_CACHE_VALID); 992 cv_broadcast(&scp->fc_state_cv); 993 if (error == 0) { 994 setbit(sc->sc_copied, scp->fc_cluster); 995 cv_signal(&sc->sc_work_cv); 996 } 997 mutex_exit(&sc->sc_slock); 998 999 return error; 1000 } 1001 1002 /* 1003 * Read/write clusters from/to backing store. 1004 * For persistent snapshots must be called with cl == 0. off is the 1005 * offset into the snapshot. 1006 */ 1007 static int 1008 fss_bs_io(struct fss_softc *sc, fss_io_type rw, 1009 u_int32_t cl, off_t off, int len, void *data) 1010 { 1011 int error; 1012 1013 off += FSS_CLTOB(sc, cl); 1014 1015 vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY); 1016 1017 error = vn_rdwr((rw == FSS_READ ? UIO_READ : UIO_WRITE), sc->sc_bs_vp, 1018 data, len, off, UIO_SYSSPACE, 1019 IO_ADV_ENCODE(POSIX_FADV_NOREUSE) | IO_NODELOCKED, 1020 sc->sc_bs_lwp->l_cred, NULL, NULL); 1021 if (error == 0) { 1022 mutex_enter(sc->sc_bs_vp->v_interlock); 1023 error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(off), 1024 round_page(off+len), PGO_CLEANIT | PGO_FREE | PGO_SYNCIO); 1025 } 1026 1027 VOP_UNLOCK(sc->sc_bs_vp); 1028 1029 return error; 1030 } 1031 1032 /* 1033 * Get a pointer to the indirect slot for this cluster. 1034 */ 1035 static u_int32_t * 1036 fss_bs_indir(struct fss_softc *sc, u_int32_t cl) 1037 { 1038 u_int32_t icl; 1039 int ioff; 1040 1041 icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t)); 1042 ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t)); 1043 1044 if (sc->sc_indir_cur == icl) 1045 return &sc->sc_indir_data[ioff]; 1046 1047 if (sc->sc_indir_dirty) { 1048 if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0, 1049 FSS_CLSIZE(sc), (void *)sc->sc_indir_data) != 0) 1050 return NULL; 1051 setbit(sc->sc_indir_valid, sc->sc_indir_cur); 1052 } 1053 1054 sc->sc_indir_dirty = 0; 1055 sc->sc_indir_cur = icl; 1056 1057 if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) { 1058 if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0, 1059 FSS_CLSIZE(sc), (void *)sc->sc_indir_data) != 0) 1060 return NULL; 1061 } else 1062 memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc)); 1063 1064 return &sc->sc_indir_data[ioff]; 1065 } 1066 1067 /* 1068 * The kernel thread (one for every active snapshot). 1069 * 1070 * After wakeup it cleans the cache and runs the I/O requests. 1071 */ 1072 static void 1073 fss_bs_thread(void *arg) 1074 { 1075 bool thread_idle, is_valid; 1076 int error, i, todo, len, crotor, is_read; 1077 long off; 1078 char *addr; 1079 u_int32_t c, cl, ch, *indirp; 1080 struct buf *bp, *nbp; 1081 struct fss_softc *sc; 1082 struct fss_cache *scp, *scl; 1083 1084 sc = arg; 1085 scl = sc->sc_cache+sc->sc_cache_size; 1086 crotor = 0; 1087 thread_idle = false; 1088 1089 mutex_enter(&sc->sc_slock); 1090 1091 for (;;) { 1092 if (thread_idle) 1093 cv_wait(&sc->sc_work_cv, &sc->sc_slock); 1094 thread_idle = true; 1095 if ((sc->sc_flags & FSS_BS_THREAD) == 0) { 1096 mutex_exit(&sc->sc_slock); 1097 kthread_exit(0); 1098 } 1099 1100 /* 1101 * Process I/O requests (persistent) 1102 */ 1103 1104 if (sc->sc_flags & FSS_PERSISTENT) { 1105 if ((bp = bufq_get(sc->sc_bufq)) == NULL) 1106 continue; 1107 is_valid = FSS_ISVALID(sc); 1108 is_read = (bp->b_flags & B_READ); 1109 thread_idle = false; 1110 mutex_exit(&sc->sc_slock); 1111 1112 if (is_valid) { 1113 disk_busy(sc->sc_dkdev); 1114 error = fss_bs_io(sc, FSS_READ, 0, 1115 dbtob(bp->b_blkno), bp->b_bcount, 1116 bp->b_data); 1117 disk_unbusy(sc->sc_dkdev, 1118 (error ? 0 : bp->b_bcount), is_read); 1119 } else 1120 error = ENXIO; 1121 1122 bp->b_error = error; 1123 bp->b_resid = (error ? bp->b_bcount : 0); 1124 biodone(bp); 1125 1126 mutex_enter(&sc->sc_slock); 1127 continue; 1128 } 1129 1130 /* 1131 * Clean the cache 1132 */ 1133 for (i = 0; i < sc->sc_cache_size; i++) { 1134 crotor = (crotor + 1) % sc->sc_cache_size; 1135 scp = sc->sc_cache + crotor; 1136 if (scp->fc_type != FSS_CACHE_VALID) 1137 continue; 1138 mutex_exit(&sc->sc_slock); 1139 1140 thread_idle = false; 1141 indirp = fss_bs_indir(sc, scp->fc_cluster); 1142 if (indirp != NULL) { 1143 error = fss_bs_io(sc, FSS_WRITE, sc->sc_clnext, 1144 0, FSS_CLSIZE(sc), scp->fc_data); 1145 } else 1146 error = EIO; 1147 1148 mutex_enter(&sc->sc_slock); 1149 if (error == 0) { 1150 *indirp = sc->sc_clnext++; 1151 sc->sc_indir_dirty = 1; 1152 } else 1153 fss_error(sc, "write error on backing store"); 1154 1155 scp->fc_type = FSS_CACHE_FREE; 1156 cv_signal(&sc->sc_cache_cv); 1157 break; 1158 } 1159 1160 /* 1161 * Process I/O requests 1162 */ 1163 if ((bp = bufq_get(sc->sc_bufq)) == NULL) 1164 continue; 1165 is_valid = FSS_ISVALID(sc); 1166 is_read = (bp->b_flags & B_READ); 1167 thread_idle = false; 1168 1169 if (!is_valid) { 1170 mutex_exit(&sc->sc_slock); 1171 1172 bp->b_error = ENXIO; 1173 bp->b_resid = bp->b_bcount; 1174 biodone(bp); 1175 1176 mutex_enter(&sc->sc_slock); 1177 continue; 1178 } 1179 1180 disk_busy(sc->sc_dkdev); 1181 1182 /* 1183 * First read from the snapshotted block device unless 1184 * this request is completely covered by backing store. 1185 */ 1186 1187 cl = FSS_BTOCL(sc, dbtob(bp->b_blkno)); 1188 off = FSS_CLOFF(sc, dbtob(bp->b_blkno)); 1189 ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1); 1190 error = 0; 1191 bp->b_resid = 0; 1192 bp->b_error = 0; 1193 for (c = cl; c <= ch; c++) { 1194 if (isset(sc->sc_copied, c)) 1195 continue; 1196 mutex_exit(&sc->sc_slock); 1197 1198 /* Not on backing store, read from device. */ 1199 nbp = getiobuf(NULL, true); 1200 nbp->b_flags = B_READ; 1201 nbp->b_resid = nbp->b_bcount = bp->b_bcount; 1202 nbp->b_bufsize = bp->b_bcount; 1203 nbp->b_data = bp->b_data; 1204 nbp->b_blkno = bp->b_blkno; 1205 nbp->b_lblkno = 0; 1206 nbp->b_dev = sc->sc_bdev; 1207 SET(nbp->b_cflags, BC_BUSY); /* mark buffer busy */ 1208 1209 bdev_strategy(nbp); 1210 1211 error = biowait(nbp); 1212 if (error != 0) { 1213 bp->b_resid = bp->b_bcount; 1214 bp->b_error = nbp->b_error; 1215 disk_unbusy(sc->sc_dkdev, 0, is_read); 1216 biodone(bp); 1217 } 1218 putiobuf(nbp); 1219 1220 mutex_enter(&sc->sc_slock); 1221 break; 1222 } 1223 if (error) 1224 continue; 1225 1226 /* 1227 * Replace those parts that have been saved to backing store. 1228 */ 1229 1230 addr = bp->b_data; 1231 todo = bp->b_bcount; 1232 for (c = cl; c <= ch; c++, off = 0, todo -= len, addr += len) { 1233 len = FSS_CLSIZE(sc)-off; 1234 if (len > todo) 1235 len = todo; 1236 if (isclr(sc->sc_copied, c)) 1237 continue; 1238 mutex_exit(&sc->sc_slock); 1239 1240 indirp = fss_bs_indir(sc, c); 1241 if (indirp == NULL || *indirp == 0) { 1242 /* 1243 * Not on backing store. Either in cache 1244 * or hole in the snapshotted block device. 1245 */ 1246 1247 mutex_enter(&sc->sc_slock); 1248 for (scp = sc->sc_cache; scp < scl; scp++) 1249 if (scp->fc_type == FSS_CACHE_VALID && 1250 scp->fc_cluster == c) 1251 break; 1252 if (scp < scl) 1253 memcpy(addr, (char *)scp->fc_data+off, 1254 len); 1255 else 1256 memset(addr, 0, len); 1257 continue; 1258 } 1259 1260 /* 1261 * Read from backing store. 1262 */ 1263 error = 1264 fss_bs_io(sc, FSS_READ, *indirp, off, len, addr); 1265 1266 mutex_enter(&sc->sc_slock); 1267 if (error) { 1268 bp->b_resid = bp->b_bcount; 1269 bp->b_error = error; 1270 break; 1271 } 1272 } 1273 mutex_exit(&sc->sc_slock); 1274 1275 disk_unbusy(sc->sc_dkdev, (error ? 0 : bp->b_bcount), is_read); 1276 biodone(bp); 1277 1278 mutex_enter(&sc->sc_slock); 1279 } 1280 } 1281 1282 #ifdef _MODULE 1283 1284 #include <sys/module.h> 1285 1286 MODULE(MODULE_CLASS_DRIVER, fss, NULL); 1287 CFDRIVER_DECL(fss, DV_DISK, NULL); 1288 1289 static int 1290 fss_modcmd(modcmd_t cmd, void *arg) 1291 { 1292 int bmajor = -1, cmajor = -1, error = 0; 1293 1294 switch (cmd) { 1295 case MODULE_CMD_INIT: 1296 mutex_init(&fss_device_lock, MUTEX_DEFAULT, IPL_NONE); 1297 error = config_cfdriver_attach(&fss_cd); 1298 if (error) { 1299 mutex_destroy(&fss_device_lock); 1300 break; 1301 } 1302 error = config_cfattach_attach(fss_cd.cd_name, &fss_ca); 1303 if (error) { 1304 config_cfdriver_detach(&fss_cd); 1305 mutex_destroy(&fss_device_lock); 1306 break; 1307 } 1308 error = devsw_attach(fss_cd.cd_name, 1309 &fss_bdevsw, &bmajor, &fss_cdevsw, &cmajor); 1310 if (error == EEXIST) 1311 error = 0; 1312 if (error) { 1313 config_cfattach_detach(fss_cd.cd_name, &fss_ca); 1314 config_cfdriver_detach(&fss_cd); 1315 mutex_destroy(&fss_device_lock); 1316 break; 1317 } 1318 break; 1319 1320 case MODULE_CMD_FINI: 1321 error = config_cfattach_detach(fss_cd.cd_name, &fss_ca); 1322 if (error) 1323 break; 1324 config_cfdriver_detach(&fss_cd); 1325 devsw_detach(&fss_bdevsw, &fss_cdevsw); 1326 mutex_destroy(&fss_device_lock); 1327 break; 1328 1329 default: 1330 error = ENOTTY; 1331 break; 1332 } 1333 1334 return error; 1335 } 1336 1337 #endif /* _MODULE */ 1338