lfs.h revision 1.3 1 /* $NetBSD: lfs.h,v 1.3 1994/10/20 04:21:10 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)lfs.h 8.3 (Berkeley) 9/23/93
36 */
37
38 #define LFS_LABELPAD 8192 /* LFS label size */
39 #define LFS_SBPAD 8192 /* LFS superblock size */
40
41 /*
42 * XXX
43 * This is a kluge and NEEDS to go away.
44 *
45 * Right now, ufs code handles most of the calls for directory operations
46 * such as create, mkdir, link, etc. As a result VOP_UPDATE is being
47 * called with waitfor set (since ffs does these things synchronously).
48 * Since LFS does not want to do these synchronously, we treat the last
49 * argument to lfs_update as a set of flags. If LFS_SYNC is set, then
50 * the update should be synchronous, if not, do it asynchronously.
51 * Unfortunately, this means that LFS won't work with NFS yet because
52 * NFS goes through paths that will make normal calls to ufs which will
53 * call lfs with a last argument of 1.
54 */
55 #define LFS_SYNC 0x02
56
57 /* On-disk and in-memory checkpoint segment usage structure. */
58 typedef struct segusage SEGUSE;
59 struct segusage {
60 u_int32_t su_nbytes; /* number of live bytes */
61 u_int32_t su_lastmod; /* SEGUSE last modified timestamp */
62 u_int16_t su_nsums; /* number of summaries in segment */
63 u_int16_t su_ninos; /* number of inode blocks in seg */
64 #define SEGUSE_ACTIVE 0x1 /* segment is currently being written */
65 #define SEGUSE_DIRTY 0x2 /* segment has data in it */
66 #define SEGUSE_SUPERBLOCK 0x4 /* segment contains a superblock */
67 u_int32_t su_flags;
68 };
69
70 #define SEGUPB(fs) (1 << (fs)->lfs_sushift)
71 #define SEGTABSIZE_SU(fs) \
72 (((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift)
73
74 /* On-disk file information. One per file with data blocks in the segment. */
75 typedef struct finfo FINFO;
76 struct finfo {
77 u_int32_t fi_nblocks; /* number of blocks */
78 u_int32_t fi_version; /* version number */
79 u_int32_t fi_ino; /* inode number */
80 int32_t fi_blocks[1]; /* array of logical block numbers */
81 };
82
83 /* On-disk and in-memory super block. */
84 struct lfs {
85 #define LFS_MAGIC 0x070162
86 u_int32_t lfs_magic; /* magic number */
87 #define LFS_VERSION 1
88 u_int32_t lfs_version; /* version number */
89
90 u_int32_t lfs_size; /* number of blocks in fs */
91 u_int32_t lfs_ssize; /* number of blocks per segment */
92 u_int32_t lfs_dsize; /* number of disk blocks in fs */
93 u_int32_t lfs_bsize; /* file system block size */
94 u_int32_t lfs_fsize; /* size of frag blocks in fs */
95 u_int32_t lfs_frag; /* number of frags in a block in fs */
96
97 /* Checkpoint region. */
98 ino_t lfs_free; /* start of the free list */
99 u_int32_t lfs_bfree; /* number of free disk blocks */
100 u_int32_t lfs_nfiles; /* number of allocated inodes */
101 int32_t lfs_avail; /* blocks available for writing */
102 u_int32_t lfs_uinodes; /* inodes in cache not yet on disk */
103 daddr_t lfs_idaddr; /* inode file disk address */
104 ino_t lfs_ifile; /* inode file inode number */
105 daddr_t lfs_lastseg; /* address of last segment written */
106 daddr_t lfs_nextseg; /* address of next segment to write */
107 daddr_t lfs_curseg; /* current segment being written */
108 daddr_t lfs_offset; /* offset in curseg for next partial */
109 daddr_t lfs_lastpseg; /* address of last partial written */
110 u_int32_t lfs_tstamp; /* time stamp */
111
112 /* These are configuration parameters. */
113 u_int32_t lfs_minfree; /* minimum percentage of free blocks */
114
115 /* These fields can be computed from the others. */
116 u_int64_t lfs_maxfilesize; /* maximum representable file size */
117 u_int32_t lfs_dbpseg; /* disk blocks per segment */
118 u_int32_t lfs_inopb; /* inodes per block */
119 u_int32_t lfs_ifpb; /* IFILE entries per block */
120 u_int32_t lfs_sepb; /* SEGUSE entries per block */
121 u_int32_t lfs_nindir; /* indirect pointers per block */
122 u_int32_t lfs_nseg; /* number of segments */
123 u_int32_t lfs_nspf; /* number of sectors per fragment */
124 u_int32_t lfs_cleansz; /* cleaner info size in blocks */
125 u_int32_t lfs_segtabsz; /* segment table size in blocks */
126
127 u_int32_t lfs_segmask; /* calculate offset within a segment */
128 u_int32_t lfs_segshift; /* fast mult/div for segments */
129 u_int32_t lfs_bmask; /* calc block offset from file offset */
130 u_int32_t lfs_bshift; /* calc block number from file offset */
131 u_int32_t lfs_ffmask; /* calc frag offset from file offset */
132 u_int32_t lfs_ffshift; /* fast mult/div for frag from file */
133 u_int32_t lfs_fbmask; /* calc frag offset from block offset */
134 u_int32_t lfs_fbshift; /* fast mult/div for frag from block */
135 u_int32_t lfs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
136 u_int32_t lfs_sushift; /* fast mult/div for segusage table */
137
138 int32_t lfs_maxsymlinklen; /* max length of an internal symlink */
139
140 #define LFS_MIN_SBINTERVAL 5 /* minimum superblock segment spacing */
141 #define LFS_MAXNUMSB 10 /* superblock disk offsets */
142 daddr_t lfs_sboffs[LFS_MAXNUMSB];
143
144 u_int32_t lfs_cksum; /* checksum for superblock checking */
145
146 /* These fields are set at mount time and are meaningless on disk. */
147 struct segment *lfs_sp; /* current segment being written */
148 struct vnode *lfs_ivnode; /* vnode for the ifile */
149 u_long lfs_seglock; /* single-thread the segment writer */
150 pid_t lfs_lockpid; /* pid of lock holder */
151 u_long lfs_iocount; /* number of ios pending */
152 u_long lfs_writer; /* don't allow any dirops to start */
153 u_long lfs_dirops; /* count of active directory ops */
154 u_long lfs_doifile; /* Write ifile blocks on next write */
155 u_long lfs_nactive; /* Number of segments since last ckp */
156 u_int8_t lfs_fmod; /* super block modified flag */
157 u_int8_t lfs_clean; /* file system is clean flag */
158 u_int8_t lfs_ronly; /* mounted read-only flag */
159 u_char lfs_fsmnt[MNAMELEN]; /* name mounted on */
160 };
161
162 /*
163 * Inode 0: out-of-band inode number
164 * Inode 1: IFILE inode number
165 * Inode 2: root inode
166 * Inode 3: lost+found inode number
167 */
168 #define LFS_UNUSED_INUM 0 /* out of band inode number */
169 #define LFS_IFILE_INUM 1 /* IFILE inode number */
170 #define LOSTFOUNDINO 3 /* lost+found inode number */
171 #define LFS_FIRST_INUM 4 /* first free inode number */
172
173 /* Address calculations for metadata located in the inode */
174 #define S_INDIR(fs) -NDADDR
175 #define D_INDIR(fs) (S_INDIR(fs) - NINDIR(fs) - 1)
176 #define T_INDIR(fs) (D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
177
178 /* Unassigned disk address. */
179 #define UNASSIGNED -1
180
181 /* Unused logical block number */
182 #define LFS_UNUSED_LBN -1
183
184 typedef struct ifile IFILE;
185 struct ifile {
186 u_int32_t if_version; /* inode version number */
187 #define LFS_UNUSED_DADDR 0 /* out-of-band daddr */
188 daddr_t if_daddr; /* inode disk address */
189 ino_t if_nextfree; /* next-unallocated inode */
190 };
191
192 /*
193 * Cleaner information structure. This resides in the ifile and is used
194 * to pass information between the cleaner and the kernel.
195 */
196 typedef struct _cleanerinfo {
197 u_int32_t clean; /* K: number of clean segments */
198 u_int32_t dirty; /* K: number of dirty segments */
199 } CLEANERINFO;
200
201 #define CLEANSIZE_SU(fs) \
202 ((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
203
204 /*
205 * All summary blocks are the same size, so we can always read a summary
206 * block easily from a segment.
207 */
208 #define LFS_SUMMARY_SIZE 512
209
210 /* On-disk segment summary information */
211 typedef struct segsum SEGSUM;
212 struct segsum {
213 u_int32_t ss_sumsum; /* check sum of summary block */
214 u_int32_t ss_datasum; /* check sum of data */
215 daddr_t ss_next; /* next segment */
216 u_int32_t ss_create; /* creation time stamp */
217 u_int16_t ss_nfinfo; /* number of file info structures */
218 u_int16_t ss_ninos; /* number of inodes in summary */
219 #define SS_DIROP 0x01 /* segment begins a dirop */
220 #define SS_CONT 0x02 /* more partials to finish this write*/
221 u_int16_t ss_flags; /* used for directory operations */
222 u_int16_t ss_pad; /* extra space */
223 /* FINFO's and inode daddr's... */
224 };
225
226 /* NINDIR is the number of indirects in a file system block. */
227 #define NINDIR(fs) ((fs)->lfs_nindir)
228
229 /* INOPB is the number of inodes in a secondary storage block. */
230 #define INOPB(fs) ((fs)->lfs_inopb)
231
232 #define blksize(fs) ((fs)->lfs_bsize)
233 #define blkoff(fs, loc) ((loc) & (fs)->lfs_bmask)
234 #define fsbtodb(fs, b) ((b) << (fs)->lfs_fsbtodb)
235 #define dbtofsb(fs, b) ((b) >> (fs)->lfs_fsbtodb)
236 #define lblkno(fs, loc) ((loc) >> (fs)->lfs_bshift)
237 #define lblktosize(fs, blk) ((blk) << (fs)->lfs_bshift)
238 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
239 ((loc) >> (fs)->lfs_bshift)
240
241 #define datosn(fs, daddr) /* disk address to segment number */ \
242 (((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
243 #define sntoda(fs, sn) /* segment number to disk address */ \
244 ((daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) + \
245 (fs)->lfs_sboffs[0]))
246
247 /* Read in the block with the cleaner info from the ifile. */
248 #define LFS_CLEANERINFO(CP, F, BP) { \
249 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
250 if (bread((F)->lfs_ivnode, \
251 (daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP))) \
252 panic("lfs: ifile read"); \
253 (CP) = (CLEANERINFO *)(BP)->b_data; \
254 }
255
256 /* Read in the block with a specific inode from the ifile. */
257 #define LFS_IENTRY(IP, F, IN, BP) { \
258 int _e; \
259 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
260 if (_e = bread((F)->lfs_ivnode, \
261 (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
262 (F)->lfs_bsize, NOCRED, &(BP))) \
263 panic("lfs: ifile read %d", _e); \
264 (IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb; \
265 }
266
267 /* Read in the block with a specific segment usage entry from the ifile. */
268 #define LFS_SEGENTRY(SP, F, IN, BP) { \
269 int _e; \
270 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
271 if (_e = bread((F)->lfs_ivnode, \
272 ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz, \
273 (F)->lfs_bsize, NOCRED, &(BP))) \
274 panic("lfs: ifile read: %d", _e); \
275 (SP) = (SEGUSE *)(BP)->b_data + ((IN) & (F)->lfs_sepb - 1); \
276 }
277
278 /*
279 * Determine if there is enough room currently available to write db
280 * disk blocks. We need enough blocks for the new blocks, the current,
281 * inode blocks, a summary block, plus potentially the ifile inode and
282 * the segment usage table, plus an ifile page.
283 */
284 #define LFS_FITS(fs, db) \
285 ((int32_t)((db + ((fs)->lfs_uinodes + INOPB((fs))) / \
286 INOPB((fs)) + fsbtodb(fs, 1) + \
287 LFS_SUMMARY_SIZE / DEV_BSIZE + \
288 (fs)->lfs_segtabsz)) < (fs)->lfs_avail)
289
290 /* Determine if a buffer belongs to the ifile */
291 #define IS_IFILE(bp) (VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
292
293 /*
294 * Structures used by lfs_bmapv and lfs_markv to communicate information
295 * about inodes and data blocks.
296 */
297 typedef struct block_info {
298 ino_t bi_inode; /* inode # */
299 daddr_t bi_lbn; /* logical block w/in file */
300 daddr_t bi_daddr; /* disk address of block */
301 time_t bi_segcreate; /* origin segment create time */
302 int bi_version; /* file version number */
303 void *bi_bp; /* data buffer */
304 } BLOCK_INFO;
305
306 /* In-memory description of a segment about to be written. */
307 struct segment {
308 struct lfs *fs; /* file system pointer */
309 struct buf **bpp; /* pointer to buffer array */
310 struct buf **cbpp; /* pointer to next available bp */
311 struct buf **start_bpp; /* pointer to first bp in this set */
312 struct buf *ibp; /* buffer pointer to inode page */
313 struct finfo *fip; /* current fileinfo pointer */
314 struct vnode *vp; /* vnode being gathered */
315 void *segsum; /* segment summary info */
316 u_int32_t ninodes; /* number of inodes in this segment */
317 u_int32_t seg_bytes_left; /* bytes left in segment */
318 u_int32_t sum_bytes_left; /* bytes left in summary block */
319 u_int32_t seg_number; /* number of this segment */
320 daddr_t *start_lbp; /* beginning lbn for this set */
321 #define SEGM_CKP 0x01 /* doing a checkpoint */
322 #define SEGM_CLEAN 0x02 /* cleaner call; don't sort */
323 #define SEGM_SYNC 0x04 /* wait for segment */
324 u_int8_t seg_flags; /* run-time flags for this segment */
325 };
326
327 #define ISSPACE(F, BB, C) \
328 (((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) || \
329 ((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
330
331 #define IS_FREESPACE(F, BB) \
332 ((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB)))
333
334 #define ISSPACE_XXX(F, BB) \
335 ((F)->lfs_bfree >= (BB))
336
337 #define DOSTATS
338 #ifdef DOSTATS
339 /* Statistics Counters */
340 struct lfs_stats {
341 int blocktot;
342 int cleanblocks;
343 int flush_invoked;
344 int ncheckpoints;
345 int nsync_writes;
346 int nwrites;
347 int pcleanwrites;
348 int psegwrites;
349 int psyncwrites;
350 int segsused;
351 int wait_exceeded;
352 int write_exceeded;
353 };
354 extern struct lfs_stats lfs_stats;
355 #endif
356