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