fssvar.h revision 1.2 1 1.2 hannken /* $NetBSD: fssvar.h,v 1.2 2004/01/11 19:05:27 hannken Exp $ */
2 1.1 hannken
3 1.1 hannken /*-
4 1.1 hannken * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 hannken * All rights reserved.
6 1.1 hannken *
7 1.1 hannken * This code is derived from software contributed to The NetBSD Foundation
8 1.1 hannken * by Juergen Hannken-Illjes.
9 1.1 hannken *
10 1.1 hannken * Redistribution and use in source and binary forms, with or without
11 1.1 hannken * modification, are permitted provided that the following conditions
12 1.1 hannken * are met:
13 1.1 hannken * 1. Redistributions of source code must retain the above copyright
14 1.1 hannken * notice, this list of conditions and the following disclaimer.
15 1.1 hannken * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 hannken * notice, this list of conditions and the following disclaimer in the
17 1.1 hannken * documentation and/or other materials provided with the distribution.
18 1.1 hannken * 3. All advertising materials mentioning features or use of this software
19 1.1 hannken * must display the following acknowledgement:
20 1.1 hannken * This product includes software developed by the NetBSD
21 1.1 hannken * Foundation, Inc. and its contributors.
22 1.1 hannken * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 hannken * contributors may be used to endorse or promote products derived
24 1.1 hannken * from this software without specific prior written permission.
25 1.1 hannken *
26 1.1 hannken * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 hannken * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 hannken * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 hannken * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 hannken * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 hannken * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 hannken * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 hannken * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 hannken * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 hannken * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 hannken * POSSIBILITY OF SUCH DAMAGE.
37 1.1 hannken */
38 1.1 hannken
39 1.1 hannken #ifndef _SYS_DEV_FSSVAR_H
40 1.1 hannken #define _SYS_DEV_FSSVAR_H
41 1.1 hannken
42 1.1 hannken struct fss_set {
43 1.1 hannken char *fss_mount; /* Mount point of file system */
44 1.1 hannken char *fss_bstore; /* Path of backing store */
45 1.1 hannken blksize_t fss_csize; /* Preferred cluster size */
46 1.1 hannken };
47 1.1 hannken
48 1.1 hannken struct fss_get {
49 1.1 hannken char fsg_mount[MNAMELEN]; /* Mount point of file system */
50 1.1 hannken struct timeval fsg_time; /* Time this snapshot was taken */
51 1.1 hannken blksize_t fsg_csize; /* Current cluster size */
52 1.1 hannken blkcnt_t fsg_mount_size; /* # clusters on file system */
53 1.1 hannken blkcnt_t fsg_bs_size; /* # clusters on backing store */
54 1.1 hannken };
55 1.1 hannken
56 1.1 hannken #define FSSIOCSET _IOW('F', 0, struct fss_set) /* Configure */
57 1.1 hannken #define FSSIOCGET _IOR('F', 1, struct fss_get) /* Status */
58 1.1 hannken #define FSSIOCCLR _IO('F', 2) /* Unconfigure */
59 1.1 hannken
60 1.1 hannken #ifdef _KERNEL
61 1.1 hannken
62 1.1 hannken #define FSS_CLUSTER_MAX (1<<24) /* Upper bound of clusters. The
63 1.2 hannken sc_copied map uses up to
64 1.1 hannken FSS_CLUSTER_MAX/NBBY bytes */
65 1.2 hannken
66 1.2 hannken #define FSS_LOCK(sc, s) \
67 1.2 hannken do { \
68 1.2 hannken (s) = splbio(); \
69 1.2 hannken simple_lock(&(sc)->sc_slock); \
70 1.2 hannken } while (/*CONSTCOND*/0)
71 1.2 hannken
72 1.2 hannken #define FSS_UNLOCK(sc, s) \
73 1.2 hannken do { \
74 1.2 hannken simple_unlock(&(sc)->sc_slock); \
75 1.2 hannken splx((s)); \
76 1.2 hannken } while (/*CONSTCOND*/0)
77 1.2 hannken
78 1.2 hannken /* Device to softc, NULL on error */
79 1.2 hannken #define FSS_DEV_TO_SOFTC(dev) \
80 1.2 hannken (minor((dev)) < 0 || minor((dev)) >= NFSS ? NULL : \
81 1.2 hannken &fss_softc[minor((dev))])
82 1.2 hannken
83 1.2 hannken /* Check if still valid */
84 1.2 hannken #define FSS_ISVALID(sc) \
85 1.2 hannken (((sc)->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE)
86 1.2 hannken
87 1.2 hannken /* Offset to cluster */
88 1.2 hannken #define FSS_BTOCL(sc, off) \
89 1.2 hannken ((off) >> (sc)->sc_clshift)
90 1.2 hannken
91 1.2 hannken /* Cluster to offset */
92 1.2 hannken #define FSS_CLTOB(sc, cl) \
93 1.2 hannken ((off_t)(cl) << (sc)->sc_clshift)
94 1.2 hannken
95 1.2 hannken /* Offset from start of cluster */
96 1.2 hannken #define FSS_CLOFF(sc, off) \
97 1.2 hannken ((off) & (sc)->sc_clmask)
98 1.2 hannken
99 1.2 hannken /* Size of cluster */
100 1.2 hannken #define FSS_CLSIZE(sc) \
101 1.2 hannken (1 << (sc)->sc_clshift)
102 1.2 hannken
103 1.2 hannken /* Offset to backing store block */
104 1.2 hannken #define FSS_BTOFSB(sc, off) \
105 1.2 hannken ((off) >> (sc)->sc_bs_bshift)
106 1.2 hannken
107 1.2 hannken /* Backing store block to offset */
108 1.2 hannken #define FSS_FSBTOB(sc, blk) \
109 1.2 hannken ((off_t)(blk) << (sc)->sc_bs_bshift)
110 1.2 hannken
111 1.2 hannken /* Offset from start of backing store block */
112 1.2 hannken #define FSS_FSBOFF(sc, off) \
113 1.2 hannken ((off) & (sc)->sc_bs_bmask)
114 1.2 hannken
115 1.2 hannken /* Size of backing store block */
116 1.2 hannken #define FSS_FSBSIZE(sc) \
117 1.2 hannken (1 << (sc)->sc_bs_bshift)
118 1.2 hannken
119 1.1 hannken typedef enum {
120 1.1 hannken FSS_CACHE_FREE = 0, /* Cache entry is free */
121 1.1 hannken FSS_CACHE_BUSY = 1, /* Cache entry is read from device */
122 1.1 hannken FSS_CACHE_VALID = 2 /* Cache entry contains valid data */
123 1.1 hannken } fss_cache_type;
124 1.1 hannken
125 1.1 hannken struct fss_cache {
126 1.1 hannken fss_cache_type fc_type; /* Current state */
127 1.1 hannken struct fss_softc *fc_softc; /* Backlink to our softc */
128 1.1 hannken volatile int fc_xfercount; /* Number of outstanding transfers */
129 1.1 hannken u_int32_t fc_cluster; /* Cluster number of this entry */
130 1.1 hannken caddr_t fc_data; /* Data */
131 1.1 hannken };
132 1.1 hannken
133 1.1 hannken struct fss_softc {
134 1.1 hannken int sc_unit; /* Logical unit number */
135 1.2 hannken struct simplelock sc_slock; /* Protect this softc */
136 1.1 hannken volatile int sc_flags; /* Flags */
137 1.1 hannken #define FSS_ACTIVE 0x01 /* Snapshot is active */
138 1.1 hannken #define FSS_ERROR 0x02 /* I/o error occured */
139 1.1 hannken #define FSS_BS_THREAD 0x04 /* Kernel thread is running */
140 1.2 hannken #define FSS_EXCL 0x08 /* Exclusive access granted */
141 1.2 hannken #define FSS_BS_ALLOC 0x10 /* Allocate backing store */
142 1.1 hannken struct mount *sc_mount; /* Mount point */
143 1.1 hannken char sc_mntname[MNAMELEN]; /* Mount point */
144 1.1 hannken struct timeval sc_time; /* Time this snapshot was taken */
145 1.1 hannken dev_t sc_bdev; /* Underlying block device */
146 1.1 hannken void (*sc_strategy)(struct buf *); /* And its strategy */
147 1.1 hannken struct vnode *sc_bs_vp; /* Our backing store */
148 1.2 hannken off_t sc_bs_size; /* Its size in bytes */
149 1.2 hannken int sc_bs_bshift; /* Shift of backing store block */
150 1.2 hannken u_int32_t sc_bs_bmask; /* Mask of backing store block */
151 1.1 hannken struct proc *sc_bs_proc; /* Our kernel thread */
152 1.2 hannken int sc_clshift; /* Shift of cluster size */
153 1.2 hannken u_int32_t sc_clmask; /* Mask of cluster size */
154 1.1 hannken u_int32_t sc_clcount; /* # clusters in file system */
155 1.1 hannken u_int8_t *sc_copied; /* Map of clusters already copied */
156 1.2 hannken long sc_clresid; /* Bytes in last cluster */
157 1.1 hannken int sc_cowcount; /* Number of cow in progress */
158 1.1 hannken int sc_cache_size; /* Number of entries in sc_cache */
159 1.1 hannken struct fss_cache *sc_cache; /* Cluster cache */
160 1.1 hannken struct bufq_state sc_bufq; /* Transfer queue */
161 1.1 hannken u_int32_t sc_clnext; /* Next free cluster on backing store */
162 1.1 hannken int sc_indir_size; /* # clusters for indirect mapping */
163 1.1 hannken u_int8_t *sc_indir_valid; /* Map of valid indirect clusters */
164 1.1 hannken u_int32_t sc_indir_cur; /* Current indir cluster number */
165 1.1 hannken int sc_indir_dirty; /* Current indir cluster modified */
166 1.1 hannken u_int32_t *sc_indir_data; /* Current indir cluster data */
167 1.1 hannken };
168 1.1 hannken
169 1.1 hannken struct fss_softc fss_softc[NFSS];
170 1.1 hannken
171 1.1 hannken void fss_copy_on_write(struct fss_softc *, struct buf *);
172 1.1 hannken int fss_umount_hook(struct mount *, int);
173 1.1 hannken
174 1.1 hannken /*
175 1.1 hannken * Check if this buf needs to be copied on write.
176 1.1 hannken * Unroll the loop for small NFSS.
177 1.1 hannken */
178 1.1 hannken static inline void
179 1.1 hannken fss_cow_hook(struct buf *bp)
180 1.1 hannken {
181 1.1 hannken int i;
182 1.1 hannken
183 1.1 hannken if (bp->b_flags & B_READ)
184 1.1 hannken return;
185 1.1 hannken
186 1.1 hannken if (NFSS <= 4) {
187 1.1 hannken if (__predict_false(fss_softc[0].sc_bdev == bp->b_dev ||
188 1.1 hannken (NFSS > 1 && fss_softc[1].sc_bdev == bp->b_dev) ||
189 1.1 hannken (NFSS > 2 && fss_softc[2].sc_bdev == bp->b_dev) ||
190 1.1 hannken (NFSS > 3 && fss_softc[3].sc_bdev == bp->b_dev)))
191 1.1 hannken for (i = 0; i < NFSS; i++)
192 1.1 hannken if (fss_softc[i].sc_bdev == bp->b_dev)
193 1.1 hannken fss_copy_on_write(&fss_softc[i], bp);
194 1.1 hannken } else {
195 1.1 hannken for (i = 0; i < NFSS; i++)
196 1.1 hannken if (__predict_false(fss_softc[i].sc_bdev == bp->b_dev))
197 1.1 hannken fss_copy_on_write(&fss_softc[i], bp);
198 1.1 hannken }
199 1.1 hannken }
200 1.1 hannken
201 1.1 hannken #endif /* _KERNEL */
202 1.1 hannken
203 1.1 hannken #endif /* !_SYS_DEV_FSSVAR_H */
204