xref: /original-bsd/sys/ufs/lfs/lfs.h (revision deff14a8)
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.5 (Berkeley) 07/08/94
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 	daddr_t	  fi_blocks[1];		/* array of logical block numbers */
54 };
55 
56 /* On-disk and in-memory super block. */
57 struct lfs {
58 #define	LFS_MAGIC	0x070162
59 	u_int32_t lfs_magic;		/* magic number */
60 #define	LFS_VERSION	1
61 	u_int32_t lfs_version;		/* version number */
62 
63 	u_int32_t lfs_size;		/* number of blocks in fs */
64 	u_int32_t lfs_ssize;		/* number of blocks per segment */
65 	u_int32_t lfs_dsize;		/* number of disk blocks in fs */
66 	u_int32_t lfs_bsize;		/* file system block size */
67 	u_int32_t lfs_fsize;		/* size of frag blocks in fs */
68 	u_int32_t lfs_frag;		/* number of frags in a block in fs */
69 
70 /* Checkpoint region. */
71 	ino_t	  lfs_free;		/* start of the free list */
72 	u_int32_t lfs_bfree;		/* number of free disk blocks */
73 	u_int32_t lfs_nfiles;		/* number of allocated inodes */
74 	int32_t	  lfs_avail;		/* blocks available for writing */
75 	u_int32_t lfs_uinodes;		/* inodes in cache not yet on disk */
76 	daddr_t	  lfs_idaddr;		/* inode file disk address */
77 	ino_t	  lfs_ifile;		/* inode file inode number */
78 	daddr_t	  lfs_lastseg;		/* address of last segment written */
79 	daddr_t	  lfs_nextseg;		/* address of next segment to write */
80 	daddr_t	  lfs_curseg;		/* current segment being written */
81 	daddr_t	  lfs_offset;		/* offset in curseg for next partial */
82 	daddr_t	  lfs_lastpseg;		/* address of last partial written */
83 	u_int32_t lfs_tstamp;		/* time stamp */
84 
85 /* These are configuration parameters. */
86 	u_int32_t lfs_minfree;		/* minimum percentage of free blocks */
87 
88 /* These fields can be computed from the others. */
89 	u_quad_t  lfs_maxfilesize;	/* maximum representable file size */
90 	u_int32_t lfs_dbpseg;		/* disk blocks per segment */
91 	u_int32_t lfs_inopb;		/* inodes per block */
92 	u_int32_t lfs_ifpb;		/* IFILE entries per block */
93 	u_int32_t lfs_sepb;		/* SEGUSE entries per block */
94 	u_int32_t lfs_nindir;		/* indirect pointers per block */
95 	u_int32_t lfs_nseg;		/* number of segments */
96 	u_int32_t lfs_nspf;		/* number of sectors per fragment */
97 	u_int32_t lfs_cleansz;		/* cleaner info size in blocks */
98 	u_int32_t lfs_segtabsz;		/* segment table size in blocks */
99 
100 	u_int32_t lfs_segmask;		/* calculate offset within a segment */
101 	u_int32_t lfs_segshift;		/* fast mult/div for segments */
102 	u_int32_t lfs_bmask;		/* calc block offset from file offset */
103 	u_int32_t lfs_bshift;		/* calc block number from file offset */
104 	u_int32_t lfs_ffmask;		/* calc frag offset from file offset */
105 	u_int32_t lfs_ffshift;		/* fast mult/div for frag from file */
106 	u_int32_t lfs_fbmask;		/* calc frag offset from block offset */
107 	u_int32_t lfs_fbshift;		/* fast mult/div for frag from block */
108 	u_int32_t lfs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
109 	u_int32_t lfs_sushift;		/* fast mult/div for segusage table */
110 
111 	int32_t	  lfs_maxsymlinklen;	/* max length of an internal symlink */
112 
113 #define	LFS_MIN_SBINTERVAL	5	/* minimum superblock segment spacing */
114 #define	LFS_MAXNUMSB		10	/* superblock disk offsets */
115 	daddr_t	  lfs_sboffs[LFS_MAXNUMSB];
116 
117 /* Checksum -- last valid disk field. */
118 	u_int32_t lfs_cksum;		/* checksum for superblock checking */
119 
120 /* These fields are set at mount time and are meaningless on disk. */
121 	struct segment *lfs_sp;		/* current segment being written */
122 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
123 	u_long	  lfs_seglock;		/* single-thread the segment writer */
124 	pid_t	  lfs_lockpid;		/* pid of lock holder */
125 	u_long	  lfs_iocount;		/* number of ios pending */
126 	u_long	  lfs_writer;		/* don't allow any dirops to start */
127 	u_long	  lfs_dirops;		/* count of active directory ops */
128 	u_long	  lfs_doifile;		/* Write ifile blocks on next write */
129 	u_long	  lfs_nactive;		/* Number of segments since last ckp */
130 	int8_t	  lfs_fmod;		/* super block modified flag */
131 	int8_t	  lfs_clean;		/* file system is clean flag */
132 	int8_t	  lfs_ronly;		/* mounted read-only flag */
133 	int8_t	  lfs_flags;		/* currently unused flag */
134 	u_char	  lfs_fsmnt[MNAMELEN];	/* name mounted on */
135 };
136 
137 /*
138  * Inode 0:	out-of-band inode number
139  * Inode 1:	IFILE inode number
140  * Inode 2:	root inode
141  * Inode 3:	lost+found inode number
142  */
143 #define	LFS_UNUSED_INUM	0		/* out of band inode number */
144 #define	LFS_IFILE_INUM	1		/* IFILE inode number */
145 #define	LOSTFOUNDINO	3		/* lost+found inode number */
146 #define	LFS_FIRST_INUM	4		/* first free inode number */
147 
148 /* Address calculations for metadata located in the inode */
149 #define	S_INDIR(fs)	-NDADDR
150 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
151 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
152 
153 /* Unassigned disk address. */
154 #define	UNASSIGNED	-1
155 
156 /* Unused logical block number */
157 #define LFS_UNUSED_LBN	-1
158 
159 typedef struct ifile IFILE;
160 struct ifile {
161 	u_int32_t if_version;		/* inode version number */
162 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
163 	daddr_t	  if_daddr;		/* inode disk address */
164 	ino_t	  if_nextfree;		/* next-unallocated inode */
165 };
166 
167 /*
168  * Cleaner information structure.  This resides in the ifile and is used
169  * to pass information between the cleaner and the kernel.
170  */
171 typedef struct _cleanerinfo {
172 	u_int32_t clean;		/* K: number of clean segments */
173 	u_int32_t dirty;		/* K: number of dirty segments */
174 } CLEANERINFO;
175 
176 #define	CLEANSIZE_SU(fs)						\
177 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
178 
179 /*
180  * All summary blocks are the same size, so we can always read a summary
181  * block easily from a segment.
182  */
183 #define	LFS_SUMMARY_SIZE	512
184 
185 /* On-disk segment summary information */
186 typedef struct segsum SEGSUM;
187 struct segsum {
188 	u_int32_t ss_sumsum;		/* check sum of summary block */
189 	u_int32_t ss_datasum;		/* check sum of data */
190 	daddr_t	  ss_next;		/* next segment */
191 	u_int32_t ss_create;		/* creation time stamp */
192 	u_int16_t ss_nfinfo;		/* number of file info structures */
193 	u_int16_t ss_ninos;		/* number of inodes in summary */
194 
195 #define	SS_DIROP	0x01		/* segment begins a dirop */
196 #define	SS_CONT		0x02		/* more partials to finish this write*/
197 	u_int16_t ss_flags;		/* used for directory operations */
198 	u_int16_t ss_pad;		/* extra space */
199 	/* FINFO's and inode daddr's... */
200 };
201 
202 /* NINDIR is the number of indirects in a file system block. */
203 #define	NINDIR(fs)	((fs)->lfs_nindir)
204 
205 /* INOPB is the number of inodes in a secondary storage block. */
206 #define	INOPB(fs)	((fs)->lfs_inopb)
207 
208 #define	blksize(fs)		((fs)->lfs_bsize)
209 #define	blkoff(fs, loc)		((loc) & (fs)->lfs_bmask)
210 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
211 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
212 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
213 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
214 #define numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */	\
215 	((loc) >> (fs)->lfs_bshift)
216 
217 #define	datosn(fs, daddr)	/* disk address to segment number */	\
218 	(((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
219 #define sntoda(fs, sn) 		/* segment number to disk address */	\
220 	((daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) +	\
221 	    (fs)->lfs_sboffs[0]))
222 
223 /* Read in the block with the cleaner info from the ifile. */
224 #define LFS_CLEANERINFO(CP, F, BP) {					\
225 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
226 	if (bread((F)->lfs_ivnode,					\
227 	    (daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP)))			\
228 		panic("lfs: ifile read");				\
229 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
230 }
231 
232 /* Read in the block with a specific inode from the ifile. */
233 #define	LFS_IENTRY(IP, F, IN, BP) {					\
234 	int _e;								\
235 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
236 	if (_e = bread((F)->lfs_ivnode,					\
237 	    (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
238 	    (F)->lfs_bsize, NOCRED, &(BP)))				\
239 		panic("lfs: ifile read %d", _e);			\
240 	(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;		\
241 }
242 
243 /* Read in the block with a specific segment usage entry from the ifile. */
244 #define	LFS_SEGENTRY(SP, F, IN, BP) {					\
245 	int _e;								\
246 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
247 	if (_e = bread((F)->lfs_ivnode,					\
248 	    ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz,		\
249 	    (F)->lfs_bsize, NOCRED, &(BP)))				\
250 		panic("lfs: ifile read: %d", _e);			\
251 	(SP) = (SEGUSE *)(BP)->b_data + ((IN) & (F)->lfs_sepb - 1);	\
252 }
253 
254 /*
255  * Determine if there is enough room currently available to write db
256  * disk blocks.  We need enough blocks for the new blocks, the current,
257  * inode blocks, a summary block, plus potentially the ifile inode and
258  * the segment usage table, plus an ifile page.
259  */
260 #define LFS_FITS(fs, db)						\
261 	((int32_t)((db + ((fs)->lfs_uinodes + INOPB((fs))) / 		\
262 	INOPB((fs)) + fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE +	\
263 	(fs)->lfs_segtabsz)) < (fs)->lfs_avail)
264 
265 /* Determine if a buffer belongs to the ifile */
266 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
267 
268 /*
269  * Structures used by lfs_bmapv and lfs_markv to communicate information
270  * about inodes and data blocks.
271  */
272 typedef struct block_info {
273 	ino_t	bi_inode;		/* inode # */
274 	daddr_t	bi_lbn;			/* logical block w/in file */
275 	daddr_t	bi_daddr;		/* disk address of block */
276 	time_t	bi_segcreate;		/* origin segment create time */
277 	int	bi_version;		/* file version number */
278 	void	*bi_bp;			/* data buffer */
279 } BLOCK_INFO;
280 
281 /* In-memory description of a segment about to be written. */
282 struct segment {
283 	struct lfs	 *fs;		/* file system pointer */
284 	struct buf	**bpp;		/* pointer to buffer array */
285 	struct buf	**cbpp;		/* pointer to next available bp */
286 	struct buf	**start_bpp;	/* pointer to first bp in this set */
287 	struct buf	 *ibp;		/* buffer pointer to inode page */
288 	struct finfo	 *fip;		/* current fileinfo pointer */
289 	struct vnode	 *vp;		/* vnode being gathered */
290 	void	 *segsum;		/* segment summary info */
291 	u_int32_t ninodes;		/* number of inodes in this segment */
292 	u_int32_t seg_bytes_left;	/* bytes left in segment */
293 	u_int32_t sum_bytes_left;	/* bytes left in summary block */
294 	u_int32_t seg_number;		/* number of this segment */
295 	daddr_t  *start_lbp;		/* beginning lbn for this set */
296 
297 #define	SEGM_CKP	0x01		/* doing a checkpoint */
298 #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
299 #define	SEGM_SYNC	0x04		/* wait for segment */
300 	u_int16_t seg_flags;		/* run-time flags for this segment */
301 };
302 
303 #define ISSPACE(F, BB, C)						\
304 	(((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) ||		\
305 	((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
306 
307 #define IS_FREESPACE(F, BB)						\
308 	((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB)))
309 
310 #define ISSPACE_XXX(F, BB)						\
311 	((F)->lfs_bfree >= (BB))
312 
313 #define DOSTATS
314 #ifdef DOSTATS
315 /* Statistics Counters */
316 struct lfs_stats {
317 	u_int	segsused;
318 	u_int	psegwrites;
319 	u_int	psyncwrites;
320 	u_int	pcleanwrites;
321 	u_int	blocktot;
322 	u_int	cleanblocks;
323 	u_int	ncheckpoints;
324 	u_int	nwrites;
325 	u_int	nsync_writes;
326 	u_int	wait_exceeded;
327 	u_int	write_exceeded;
328 	u_int	flush_invoked;
329 };
330 extern struct lfs_stats lfs_stats;
331 #endif
332