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