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