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