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