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