lfs.h revision 1.29 1 /* $NetBSD: lfs.h,v 1.29 2000/07/05 22:25:43 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
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 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs.h 8.9 (Berkeley) 5/8/95
71 */
72
73 /*
74 * Compile-time options for LFS.
75 */
76 #define LFS_EAGAIN_FAIL /* markv fail with EAGAIN if ino is locked */
77 #define LFS_TRACK_IOS /* attempt to avoid cleaning segments not yet fully written to disk */
78 #define LFS_CANNOT_ROLLFW /* No roll-forward agent exists */
79
80 /* #define DEBUG_LFS */ /* Intensive debugging of LFS subsystem */
81
82 /* #define LFS_ATIME_IFILE */ /* Store atime in Ifile, don't push */
83
84 /*
85 * Parameters and generic definitions
86 */
87 #define BW_CLEAN 1
88 #define MIN_FREE_SEGS 2
89 #define LFS_MAX_ACTIVE 10
90 #define LFS_MAXDIROP (desiredvnodes>>2)
91
92 /* For convenience */
93 #define IN_ALLMOD (IN_MODIFIED|IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_ACCESSED|IN_CLEANING)
94 #define LFS_SET_UINO(ip, flags) do { \
95 if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED)) \
96 ++(ip)->i_lfs->lfs_uinodes; \
97 if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING)) \
98 ++(ip)->i_lfs->lfs_uinodes; \
99 if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED)) \
100 ++(ip)->i_lfs->lfs_uinodes; \
101 (ip)->i_flag |= (flags); \
102 } while(0)
103
104 #define LFS_CLR_UINO(ip, flags) do { \
105 if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED)) \
106 --(ip)->i_lfs->lfs_uinodes; \
107 if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING)) \
108 --(ip)->i_lfs->lfs_uinodes; \
109 if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED)) \
110 --(ip)->i_lfs->lfs_uinodes; \
111 (ip)->i_flag &= ~(flags); \
112 if ((ip)->i_lfs->lfs_uinodes < 0) { \
113 panic("lfs_uinodes < 0"); \
114 } \
115 } while(0)
116
117
118 #ifndef LFS_ATIME_IFILE
119 #define LFS_ITIMES(ip, acc, mod, cre) { \
120 if ((ip)->i_flag & IN_ACCESS) { \
121 (ip)->i_ffs_atime = (acc)->tv_sec; \
122 (ip)->i_ffs_atimensec = (acc)->tv_nsec; \
123 LFS_SET_UINO(ip, IN_ACCESSED); \
124 } \
125 if ((ip)->i_flag & (IN_CHANGE | IN_UPDATE)) { \
126 if ((ip)->i_flag & IN_UPDATE) { \
127 (ip)->i_ffs_mtime = (mod)->tv_sec; \
128 (ip)->i_ffs_mtimensec = (mod)->tv_nsec; \
129 (ip)->i_modrev++; \
130 } \
131 if ((ip)->i_flag & IN_CHANGE) { \
132 (ip)->i_ffs_ctime = (cre)->tv_sec; \
133 (ip)->i_ffs_ctimensec = (cre)->tv_nsec; \
134 } \
135 LFS_SET_UINO(ip, IN_MODIFIED); \
136 } \
137 (ip)->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE); \
138 }
139 #else
140 # define LFS_ITIMES(ip, acc, mod, cre) { \
141 struct buf *ibp; \
142 IFILE *ifp; \
143 \
144 if ((ip)->i_flag & IN_ACCESS) { \
145 LFS_IENTRY(ifp, ip->i_lfs, ip->i_number, ibp); \
146 ifp->if_atime = (mod); \
147 VOP_BWRITE(bp); \
148 (ip)->i_flag &= ~IN_ACCESS; \
149 } \
150 if ((ip)->i_flag & (IN_CHANGE | IN_UPDATE)) { \
151 LFS_SET_UINO(ip, IN_MODIFIED); \
152 if ((ip)->i_flag & IN_UPDATE) { \
153 (ip)->i_ffs_mtime = (mod)->tv_sec; \
154 (ip)->i_ffs_mtimensec = (mod)->tv_nsec; \
155 (ip)->i_modrev++; \
156 } \
157 if ((ip)->i_flag & IN_CHANGE) { \
158 (ip)->i_ffs_ctime = (cre)->tv_sec; \
159 (ip)->i_ffs_ctimensec = (cre)->tv_nsec; \
160 } \
161 (ip)->i_flag &= ~(IN_CHANGE | IN_UPDATE); \
162 } \
163 }
164 #endif
165
166 #define WRITEINPROG(vp) (vp->v_dirtyblkhd.lh_first && !(VTOI(vp)->i_flag & (IN_MODIFIED|IN_ACCESSED|IN_CLEANING)))
167
168 /* Here begins the berkeley code */
169
170 #define LFS_LABELPAD 8192 /* LFS label size */
171 #define LFS_SBPAD 8192 /* LFS superblock size */
172
173 /* On-disk and in-memory checkpoint segment usage structure. */
174 typedef struct segusage SEGUSE;
175 struct segusage {
176 u_int32_t su_nbytes; /* number of live bytes */
177 u_int32_t su_lastmod; /* SEGUSE last modified timestamp */
178 u_int16_t su_nsums; /* number of summaries in segment */
179 u_int16_t su_ninos; /* number of inode blocks in seg */
180
181 #define SEGUSE_ACTIVE 0x01 /* segment is currently being written */
182 #define SEGUSE_DIRTY 0x02 /* segment has data in it */
183 #define SEGUSE_SUPERBLOCK 0x04 /* segment contains a superblock */
184 u_int32_t su_flags;
185 };
186
187 #define SEGUPB(fs) (1 << (fs)->lfs_sushift)
188 #define SEGTABSIZE_SU(fs) \
189 (((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift)
190
191 /* On-disk file information. One per file with data blocks in the segment. */
192 typedef struct finfo FINFO;
193 struct finfo {
194 u_int32_t fi_nblocks; /* number of blocks */
195 u_int32_t fi_version; /* version number */
196 u_int32_t fi_ino; /* inode number */
197 u_int32_t fi_lastlength; /* length of last block in array */
198 ufs_daddr_t fi_blocks[1]; /* array of logical block numbers */
199 };
200
201 /* On-disk super block. */
202 struct dlfs {
203 #define LFS_MAGIC 0x070162
204 u_int32_t dlfs_magic; /* 0: magic number */
205 #define LFS_VERSION 1
206 u_int32_t dlfs_version; /* 4: version number */
207
208 u_int32_t dlfs_size; /* 8: number of blocks in fs */
209 u_int32_t dlfs_ssize; /* 12: number of blocks per segment */
210 u_int32_t dlfs_dsize; /* 16: number of disk blocks in fs */
211 u_int32_t dlfs_bsize; /* 20: file system block size */
212 u_int32_t dlfs_fsize; /* 24: size of frag blocks in fs */
213 u_int32_t dlfs_frag; /* 28: number of frags in a block in fs */
214
215 /* Checkpoint region. */
216 u_int32_t dlfs_free; /* 32: start of the free list */
217 u_int32_t dlfs_bfree; /* 36: number of free disk blocks */
218 u_int32_t dlfs_nfiles; /* 40: number of allocated inodes */
219 int32_t dlfs_avail; /* 44: blocks available for writing */
220 int32_t dlfs_uinodes; /* 48: inodes in cache not yet on disk */
221 ufs_daddr_t dlfs_idaddr; /* 52: inode file disk address */
222 u_int32_t dlfs_ifile; /* 56: inode file inode number */
223 ufs_daddr_t dlfs_lastseg; /* 60: address of last segment written */
224 ufs_daddr_t dlfs_nextseg; /* 64: address of next segment to write */
225 ufs_daddr_t dlfs_curseg; /* 68: current segment being written */
226 ufs_daddr_t dlfs_offset; /* 72: offset in curseg for next partial */
227 ufs_daddr_t dlfs_lastpseg; /* 76: address of last partial written */
228 u_int32_t dlfs_tstamp; /* 80: time stamp */
229
230 /* These are configuration parameters. */
231 u_int32_t dlfs_minfree; /* 84: minimum percentage of free blocks */
232
233 /* These fields can be computed from the others. */
234 u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
235 u_int32_t dlfs_dbpseg; /* 96: disk blocks per segment */
236 u_int32_t dlfs_inopb; /* 100: inodes per block */
237 u_int32_t dlfs_ifpb; /* 104: IFILE entries per block */
238 u_int32_t dlfs_sepb; /* 108: SEGUSE entries per block */
239 u_int32_t dlfs_nindir; /* 112: indirect pointers per block */
240 u_int32_t dlfs_nseg; /* 116: number of segments */
241 u_int32_t dlfs_nspf; /* 120: number of sectors per fragment */
242 u_int32_t dlfs_cleansz; /* 124: cleaner info size in blocks */
243 u_int32_t dlfs_segtabsz; /* 128: segment table size in blocks */
244 u_int32_t dlfs_segmask; /* 132: calculate offset within a segment */
245 u_int32_t dlfs_segshift; /* 136: fast mult/div for segments */
246 u_int32_t dlfs_bshift; /* 140: calc block number from file offset */
247 u_int32_t dlfs_ffshift; /* 144: fast mult/div for frag from file */
248 u_int32_t dlfs_fbshift; /* 148: fast mult/div for frag from block */
249 u_int64_t dlfs_bmask; /* 152: calc block offset from file offset */
250 u_int64_t dlfs_ffmask; /* 160: calc frag offset from file offset */
251 u_int64_t dlfs_fbmask; /* 168: calc frag offset from block offset */
252 u_int32_t dlfs_fsbtodb; /* 176: fsbtodb and dbtofsb shift constant */
253 u_int32_t dlfs_sushift; /* 180: fast mult/div for segusage table */
254
255 int32_t dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
256
257 #define LFS_MIN_SBINTERVAL 5 /* minimum superblock segment spacing */
258 #define LFS_MAXNUMSB 10 /* 188: superblock disk offsets */
259 ufs_daddr_t dlfs_sboffs[LFS_MAXNUMSB];
260
261 u_int32_t dlfs_nclean; /* 228: Number of clean segments */
262 u_char dlfs_fsmnt[MNAMELEN]; /* 232: name mounted on */
263 /* XXX this is 2 bytes only to pad to a quad boundary */
264 u_int16_t dlfs_clean; /* 322: file system is clean flag */
265 u_int32_t dlfs_dmeta; /* 324: total number of dirty summaries */
266 u_int32_t dlfs_minfreeseg; /* 328: segs reserved for cleaner */
267 int8_t dlfs_pad[176]; /* 332: round to 512 bytes */
268 /* Checksum -- last valid disk field. */
269 u_int32_t dlfs_cksum; /* 508: checksum for superblock checking */
270 };
271
272 /* Maximum number of io's we can have pending at once */
273 #define LFS_THROTTLE 16 /* XXX should be better paramtrized - ? */
274
275 /* In-memory super block. */
276 struct lfs {
277 struct dlfs lfs_dlfs; /* on-disk parameters */
278 #define lfs_magic lfs_dlfs.dlfs_magic
279 #define lfs_version lfs_dlfs.dlfs_version
280 #define lfs_size lfs_dlfs.dlfs_size
281 #define lfs_ssize lfs_dlfs.dlfs_ssize
282 #define lfs_dsize lfs_dlfs.dlfs_dsize
283 #define lfs_bsize lfs_dlfs.dlfs_bsize
284 #define lfs_fsize lfs_dlfs.dlfs_fsize
285 #define lfs_frag lfs_dlfs.dlfs_frag
286 #define lfs_free lfs_dlfs.dlfs_free
287 #define lfs_bfree lfs_dlfs.dlfs_bfree
288 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
289 #define lfs_avail lfs_dlfs.dlfs_avail
290 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
291 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
292 #define lfs_ifile lfs_dlfs.dlfs_ifile
293 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
294 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
295 #define lfs_curseg lfs_dlfs.dlfs_curseg
296 #define lfs_offset lfs_dlfs.dlfs_offset
297 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
298 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
299 #define lfs_minfree lfs_dlfs.dlfs_minfree
300 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
301 #define lfs_dbpseg lfs_dlfs.dlfs_dbpseg
302 #define lfs_inopb lfs_dlfs.dlfs_inopb
303 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
304 #define lfs_sepb lfs_dlfs.dlfs_sepb
305 #define lfs_nindir lfs_dlfs.dlfs_nindir
306 #define lfs_nseg lfs_dlfs.dlfs_nseg
307 #define lfs_nspf lfs_dlfs.dlfs_nspf
308 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
309 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
310 #define lfs_segmask lfs_dlfs.dlfs_segmask
311 #define lfs_segshift lfs_dlfs.dlfs_segshift
312 #define lfs_bmask lfs_dlfs.dlfs_bmask
313 #define lfs_bshift lfs_dlfs.dlfs_bshift
314 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
315 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
316 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
317 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
318 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
319 #define lfs_sushift lfs_dlfs.dlfs_sushift
320 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
321 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
322 #define lfs_cksum lfs_dlfs.dlfs_cksum
323 #define lfs_clean lfs_dlfs.dlfs_clean
324 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
325 #define lfs_nclean lfs_dlfs.dlfs_nclean
326 #define lfs_dmeta lfs_dlfs.dlfs_dmeta
327 #define lfs_minfreeseg lfs_dlfs.dlfs_minfreeseg
328
329 /* These fields are set at mount time and are meaningless on disk. */
330 struct segment *lfs_sp; /* current segment being written */
331 struct vnode *lfs_ivnode; /* vnode for the ifile */
332 u_int32_t lfs_seglock; /* single-thread the segment writer */
333 pid_t lfs_lockpid; /* pid of lock holder */
334 u_int32_t lfs_iocount; /* number of ios pending */
335 u_int32_t lfs_writer; /* don't allow any dirops to start */
336 u_int32_t lfs_dirops; /* count of active directory ops */
337 u_int32_t lfs_doifile; /* Write ifile blocks on next write */
338 u_int32_t lfs_nactive; /* Number of segments since last ckp */
339 int8_t lfs_fmod; /* super block modified flag */
340 int8_t lfs_ronly; /* mounted read-only flag */
341 #define LFS_NOTYET 0x01
342 int8_t lfs_flags; /* currently unused flag */
343 u_int16_t lfs_activesb; /* toggle between superblocks */
344 #ifdef LFS_TRACK_IOS
345 daddr_t lfs_pending[LFS_THROTTLE]; /* daddrs of pending writes */
346 #endif /* LFS_TRACK_IOS */
347 #ifdef LFS_CANNOT_ROLLFW
348 daddr_t lfs_sbactive; /* disk address of in-progress sb write */
349 #endif
350 struct vnode *lfs_flushvp; /* vnode being flushed */
351 struct vnode *lfs_unlockvp; /* being inactivated in lfs_segunlock */
352 u_int32_t lfs_diropwait; /* # procs waiting on dirop flush */
353 struct lock lfs_freelock;
354 pid_t lfs_rfpid; /* Process ID of roll-forward agent */
355 int lfs_nadirop; /* number of active dirop nodes */
356 };
357
358 /*
359 * Inode 0: out-of-band inode number
360 * Inode 1: IFILE inode number
361 * Inode 2: root inode
362 * Inode 3: lost+found inode number
363 */
364 #define LFS_UNUSED_INUM 0 /* out of band inode number */
365 #define LFS_IFILE_INUM 1 /* IFILE inode number */
366 #define LOSTFOUNDINO 3 /* lost+found inode number */
367 #define LFS_FIRST_INUM 4 /* first free inode number */
368
369 /* Address calculations for metadata located in the inode */
370 #define S_INDIR(fs) -NDADDR
371 #define D_INDIR(fs) (S_INDIR(fs) - NINDIR(fs) - 1)
372 #define T_INDIR(fs) (D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
373
374 /* Unassigned disk addresses. */
375 #define UNASSIGNED -1
376 #define UNWRITTEN -2
377
378 /* Unused logical block number */
379 #define LFS_UNUSED_LBN -1
380
381 typedef struct ifile IFILE;
382 struct ifile {
383 u_int32_t if_version; /* inode version number */
384 #define LFS_UNUSED_DADDR 0 /* out-of-band daddr */
385 ufs_daddr_t if_daddr; /* inode disk address */
386 ino_t if_nextfree; /* next-unallocated inode */
387 #ifdef LFS_ATIME_IFILE
388 struct timespec if_atime; /* Last access time */
389 #endif
390 };
391
392 /*
393 * Cleaner information structure. This resides in the ifile and is used
394 * to pass information between the cleaner and the kernel.
395 */
396 typedef struct _cleanerinfo {
397 u_int32_t clean; /* number of clean segments */
398 u_int32_t dirty; /* number of dirty segments */
399 u_int32_t bfree; /* disk blocks free */
400 u_int32_t avail; /* disk blocks available */
401 } CLEANERINFO;
402
403 #define CLEANSIZE_SU(fs) \
404 ((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
405
406 /*
407 * All summary blocks are the same size, so we can always read a summary
408 * block easily from a segment.
409 */
410 #define LFS_SUMMARY_SIZE 512
411
412 /* On-disk segment summary information */
413 typedef struct segsum SEGSUM;
414 struct segsum {
415 u_int32_t ss_sumsum; /* check sum of summary block */
416 u_int32_t ss_datasum; /* check sum of data */
417 u_int32_t ss_magic; /* segment summary magic number */
418 #define SS_MAGIC 0x061561
419 ufs_daddr_t ss_next; /* next segment */
420 u_int32_t ss_create; /* creation time stamp */
421 u_int16_t ss_nfinfo; /* number of file info structures */
422 u_int16_t ss_ninos; /* number of inodes in summary */
423
424 #define SS_DIROP 0x01 /* segment begins a dirop */
425 #define SS_CONT 0x02 /* more partials to finish this write*/
426 u_int16_t ss_flags; /* used for directory operations */
427 u_int16_t ss_pad; /* extra space */
428 /* FINFO's and inode daddr's... */
429 };
430
431 /* NINDIR is the number of indirects in a file system block. */
432 #define NINDIR(fs) ((fs)->lfs_nindir)
433
434 /* INOPB is the number of inodes in a secondary storage block. */
435 #define INOPB(fs) ((fs)->lfs_inopb)
436
437 #define blksize(fs, ip, lbn) \
438 (((lbn) >= NDADDR || (ip)->i_ffs_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
439 ? (fs)->lfs_bsize \
440 : (fragroundup(fs, blkoff(fs, (ip)->i_ffs_size))))
441 #define blkoff(fs, loc) ((int)(loc) & (fs)->lfs_bmask)
442 #define fragoff(fs, loc) /* calculates (loc % fs->lfs_fsize) */ \
443 ((int)((loc) & (fs)->lfs_ffmask))
444 #define fsbtodb(fs, b) ((b) << (fs)->lfs_fsbtodb)
445 #define dbtofsb(fs, b) ((b) >> (fs)->lfs_fsbtodb)
446 #define fragstodb(fs, b) ((b) << ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
447 #define dbtofrags(fs, b) ((b) >> ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
448 #define lblkno(fs, loc) ((loc) >> (fs)->lfs_bshift)
449 #define lblktosize(fs, blk) ((blk) << (fs)->lfs_bshift)
450 #define numfrags(fs, loc) /* calculates (loc / fs->lfs_fsize) */ \
451 ((loc) >> (fs)->lfs_ffshift)
452 #define blkroundup(fs, size) /* calculates roundup(size, fs->lfs_bsize) */ \
453 ((int)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
454 #define fragroundup(fs, size) /* calculates roundup(size, fs->lfs_fsize) */ \
455 ((int)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
456 #define fragstoblks(fs, frags) /* calculates (frags / fs->lfs_frag) */ \
457 ((frags) >> (fs)->lfs_fbshift)
458 #define blkstofrags(fs, blks) /* calculates (blks * fs->lfs_frag) */ \
459 ((blks) << (fs)->lfs_fbshift)
460 #define fragnum(fs, fsb) /* calculates (fsb % fs->lfs_frag) */ \
461 ((fsb) & ((fs)->lfs_frag - 1))
462 #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->lfs_frag) */ \
463 ((fsb) &~ ((fs)->lfs_frag - 1))
464 #define dblksize(fs, dip, lbn) \
465 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
466 ? (fs)->lfs_bsize \
467 : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
468 #define datosn(fs, daddr) /* disk address to segment number */ \
469 (((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
470 #define sntoda(fs, sn) /* segment number to disk address */ \
471 ((ufs_daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) + \
472 (fs)->lfs_sboffs[0]))
473
474 /* Read in the block with the cleaner info from the ifile. */
475 #define LFS_CLEANERINFO(CP, F, BP) { \
476 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
477 if (bread((F)->lfs_ivnode, \
478 (ufs_daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP))) \
479 panic("lfs: ifile read"); \
480 (CP) = (CLEANERINFO *)(BP)->b_data; \
481 }
482
483 /* Read in the block with a specific inode from the ifile. */
484 #define LFS_IENTRY(IP, F, IN, BP) { \
485 int _e; \
486 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
487 if ((_e = bread((F)->lfs_ivnode, \
488 (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
489 (F)->lfs_bsize, NOCRED, &(BP))) != 0) \
490 panic("lfs: ifile read %d", _e); \
491 (IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb; \
492 }
493
494 /* Read in the block with a specific segment usage entry from the ifile. */
495 #define LFS_SEGENTRY(SP, F, IN, BP) { \
496 int _e; \
497 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
498 if ((_e = bread((F)->lfs_ivnode, \
499 ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz, \
500 (F)->lfs_bsize, NOCRED, &(BP))) != 0) \
501 panic("lfs: ifile read: %d", _e); \
502 (SP) = (SEGUSE *)(BP)->b_data + ((IN) & ((F)->lfs_sepb - 1)); \
503 }
504
505 /* Determine if a buffer belongs to the ifile */
506 #define IS_IFILE(bp) (VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
507
508 /*
509 * Structures used by lfs_bmapv and lfs_markv to communicate information
510 * about inodes and data blocks.
511 */
512 typedef struct block_info {
513 ino_t bi_inode; /* inode # */
514 ufs_daddr_t bi_lbn; /* logical block w/in file */
515 ufs_daddr_t bi_daddr; /* disk address of block */
516 time_t bi_segcreate; /* origin segment create time */
517 int bi_version; /* file version number */
518 void *bi_bp; /* data buffer */
519 int bi_size; /* size of the block (if fragment) */
520 } BLOCK_INFO;
521
522 /* In-memory description of a segment about to be written. */
523 struct segment {
524 struct lfs *fs; /* file system pointer */
525 struct buf **bpp; /* pointer to buffer array */
526 struct buf **cbpp; /* pointer to next available bp */
527 struct buf **start_bpp; /* pointer to first bp in this set */
528 struct buf *ibp; /* buffer pointer to inode page */
529 struct dinode *idp; /* pointer to ifile dinode */
530 struct finfo *fip; /* current fileinfo pointer */
531 struct vnode *vp; /* vnode being gathered */
532 void *segsum; /* segment summary info */
533 u_int32_t ninodes; /* number of inodes in this segment */
534 u_int32_t seg_bytes_left; /* bytes left in segment */
535 u_int32_t sum_bytes_left; /* bytes left in summary block */
536 u_int32_t seg_number; /* number of this segment */
537 ufs_daddr_t *start_lbp; /* beginning lbn for this set */
538
539 #define SEGM_CKP 0x01 /* doing a checkpoint */
540 #define SEGM_CLEAN 0x02 /* cleaner call; don't sort */
541 #define SEGM_SYNC 0x04 /* wait for segment */
542 #define SEGM_PROT 0x08 /* don't inactivate at segunlock */
543 u_int16_t seg_flags; /* run-time flags for this segment */
544 };
545
546 /*
547 * Macros for determining free space on the disk, with the variable metadata
548 * of segment summaries and inode blocks taken into account.
549 */
550 /* Estimate number of clean blocks not available for writing */
551 #define LFS_EST_CMETA(F) ((((F)->lfs_dmeta * (u_int64_t)(F)->lfs_nclean) / \
552 ((F)->lfs_nseg - (F)->lfs_nclean)))
553
554 /* Estimate total size of the disk not including metadata */
555 #define LFS_EST_NONMETA(F) ((F)->lfs_dsize - (F)->lfs_dmeta - LFS_EST_CMETA(F))
556
557 /* Estimate number of blocks actually available for writing */
558 #define LFS_EST_BFREE(F) ((F)->lfs_bfree - LFS_EST_CMETA(F) - (F)->lfs_dmeta)
559
560 /* Amount of non-meta space not available to mortal man */
561 #define LFS_EST_RSVD(F) ((LFS_EST_NONMETA(F) * (u_int64_t)(F)->lfs_minfree) / \
562 100)
563
564 /* Can credential C write BB blocks */
565 #define ISSPACE(F, BB, C) \
566 ((((C) == NOCRED || (C)->cr_uid == 0) && \
567 LFS_EST_BFREE(F) >= (BB)) || \
568 ((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
569
570 /* Can an ordinary user write BB blocks */
571 #define IS_FREESPACE(F, BB) \
572 (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
573
574 /* Statistics Counters */
575 struct lfs_stats {
576 u_int segsused;
577 u_int psegwrites;
578 u_int psyncwrites;
579 u_int pcleanwrites;
580 u_int blocktot;
581 u_int cleanblocks;
582 u_int ncheckpoints;
583 u_int nwrites;
584 u_int nsync_writes;
585 u_int wait_exceeded;
586 u_int write_exceeded;
587 u_int flush_invoked;
588 u_int vflush_invoked;
589 };
590 extern struct lfs_stats lfs_stats;
591