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