xref: /original-bsd/sys/ufs/lfs/lfs.h (revision d1f811c7)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs.h	7.19 (Berkeley) 07/24/92
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_long	su_nbytes;		/* number of live bytes */
33 	u_long	su_lastmod;		/* SEGUSE last modified timestamp */
34 	u_short	su_nsums;		/* number of summaries in segment */
35 	u_short	su_ninos;		/* number of inode blocks in seg */
36 #define	SEGUSE_ACTIVE		0x1	/* segment is currently being written */
37 #define	SEGUSE_DIRTY		0x2	/* segment has data in it */
38 #define	SEGUSE_SUPERBLOCK	0x4	/* segment contains a superblock */
39 	u_long	su_flags;
40 };
41 
42 #define	SEGUPB(fs)	(1 << (fs)->lfs_sushift);
43 #define	SEGTABSIZE_SU(fs) \
44 	((fs)->lfs_nseg >> ((fs)->lfs_bshift - (fs)->lfs_sushift))
45 
46 /* On-disk file information.  One per file with data blocks in the segment. */
47 typedef struct finfo FINFO;
48 struct finfo {
49 	u_long	fi_nblocks;		/* number of blocks */
50 	u_long	fi_version;		/* version number */
51 	u_long	fi_ino;			/* inode number */
52 	long	fi_blocks[1];		/* array of logical block numbers */
53 };
54 
55 /* On-disk and in-memory super block. */
56 struct lfs {
57 #define	LFS_MAGIC	0x070162
58 	u_long	lfs_magic;		/* magic number */
59 #define	LFS_VERSION	1
60 	u_long	lfs_version;		/* version number */
61 
62 	u_long	lfs_size;		/* number of blocks in fs */
63 	u_long	lfs_ssize;		/* number of blocks per segment */
64 	u_long	lfs_dsize;		/* number of data blocks in fs */
65 	u_long	lfs_bsize;		/* size of basic blocks in fs */
66 	u_long	lfs_fsize;		/* size of frag blocks in fs */
67 	u_long	lfs_frag;		/* number of frags in a block in fs */
68 
69 /* Checkpoint region. */
70 	ino_t	lfs_free;		/* start of the free list */
71 	u_long	lfs_bfree;		/* number of free disk blocks */
72 	u_long	lfs_nfiles;		/* number of allocated inodes */
73 	daddr_t	lfs_idaddr;		/* inode file disk address */
74 	ino_t	lfs_ifile;		/* inode file inode number */
75 	daddr_t	lfs_lastseg;		/* address of last segment written */
76 	daddr_t	lfs_nextseg;		/* address of next segment to write */
77 	daddr_t	lfs_curseg;		/* current segment being written */
78 	daddr_t	lfs_offset;		/* offset in curseg for next partial */
79 	daddr_t	lfs_lastpseg;		/* address of last partial written */
80 	u_long	lfs_tstamp;		/* time stamp */
81 
82 /* These are configuration parameters. */
83 	u_long	lfs_minfree;		/* minimum percentage of free blocks */
84 
85 /* These fields can be computed from the others. */
86 	u_quad_t lfs_maxfilesize;	/* maximum representable file size */
87 	u_long	lfs_dbpseg;		/* disk blocks per segment */
88 	u_long	lfs_inopb;		/* inodes per block */
89 	u_long	lfs_ifpb;		/* IFILE entries per block */
90 	u_long	lfs_sepb;		/* SEGUSE entries per block */
91 	u_long	lfs_nindir;		/* indirect pointers per block */
92 	u_long	lfs_nseg;		/* number of segments */
93 	u_long	lfs_nspf;		/* number of sectors per fragment */
94 	u_long	lfs_cleansz;		/* cleaner info size in blocks */
95 	u_long	lfs_segtabsz;		/* segment table size in blocks */
96 
97 	u_long	lfs_segmask;		/* calculate offset within a segment */
98 	u_long	lfs_segshift;		/* fast mult/div for segments */
99 	u_long	lfs_bmask;		/* calc block offset from file offset */
100 	u_long	lfs_bshift;		/* calc block number from file offset */
101 	u_long	lfs_ffmask;		/* calc frag offset from file offset */
102 	u_long	lfs_ffshift;		/* fast mult/div for frag from file */
103 	u_long	lfs_fbmask;		/* calc frag offset from block offset */
104 	u_long	lfs_fbshift;		/* fast mult/div for frag from block */
105 	u_long	lfs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
106 	u_long	lfs_sushift;		/* fast mult/div for segusage table */
107 
108 #define	LFS_MIN_SBINTERVAL	5	/* minimum superblock segment spacing */
109 #define	LFS_MAXNUMSB		10	/* superblock disk offsets */
110 	daddr_t	lfs_sboffs[LFS_MAXNUMSB];
111 
112 /* These fields are set at mount time and are meaningless on disk. */
113 	struct	vnode *lfs_ivnode;	/* vnode for the ifile */
114 	u_long	lfs_seglock;		/* single-thread the segment writer */
115 	u_long	lfs_iocount;		/* number of ios pending */
116 	u_long	lfs_writer;		/* don't allow any dirops to start */
117 	u_long	lfs_dirops;		/* count of active directory ops */
118 	u_long	lfs_doifile;		/* Write ifile blocks on next write */
119 	u_char	lfs_fmod;		/* super block modified flag */
120 	u_char	lfs_clean;		/* file system is clean flag */
121 	u_char	lfs_ronly;		/* mounted read-only flag */
122 	u_char	lfs_flags;		/* currently unused flag */
123 	u_char	lfs_fsmnt[MNAMELEN];	/* name mounted on */
124 	u_char	pad[3];			/* long-align */
125 
126 /* Checksum; valid on disk. */
127 	u_long	lfs_cksum;		/* checksum for superblock checking */
128 };
129 
130 /*
131  * Inode 0 is the out-of-band inode number, inode 1 is the inode number for
132  * the IFILE, the root inode is 2 and the lost+found inode is 3.
133  */
134 
135 /* Fixed inode numbers. */
136 #define	LFS_UNUSED_INUM	0		/* out of band inode number */
137 #define	LFS_IFILE_INUM	1		/* IFILE inode number */
138 #define	LOSTFOUNDINO	3		/* lost+found inode number */
139 #define	LFS_FIRST_INUM	4		/* first free inode number */
140 
141 /*
142  * Used to access the first spare of the dinode which we use to store
143  * the ifile number so we can identify them
144  */
145 #define	di_inum	di_spare[0]
146 
147 /* Address calculations for metadata located in the inode */
148 #define	S_INDIR(fs)	-NDADDR
149 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
150 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
151 
152 /* Structure used to pass around logical block paths. */
153 typedef struct _indir {
154 	long	in_lbn;			/* logical block number */
155 	int	in_off;			/* offset in buffer */
156 } INDIR;
157 
158 /* Unassigned disk address. */
159 #define	UNASSIGNED	-1
160 
161 typedef struct ifile IFILE;
162 struct ifile {
163 	u_long	if_version;		/* inode version number */
164 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
165 	daddr_t	if_daddr;		/* inode disk address */
166 	ino_t	if_nextfree;		/* next-unallocated inode */
167 };
168 
169 /*
170  * Cleaner information structure.  This resides in the ifile and is used
171  * to pass information between the cleaner and the kernel.
172  */
173 typedef struct _cleanerinfo {
174 	u_long	clean;			/* K: number of clean segments */
175 	u_long	dirty;			/* K: number of dirty segments */
176 } CLEANERINFO;
177 
178 #define	CLEANSIZE_SU(fs) \
179 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
180 
181 /*
182  * All summary blocks are the same size, so we can always read a summary
183  * block easily from a segment.
184  */
185 #define	LFS_SUMMARY_SIZE	512
186 
187 /* On-disk segment summary information */
188 typedef struct segsum SEGSUM;
189 struct segsum {
190 	u_long	ss_sumsum;		/* check sum of summary block */
191 	u_long	ss_datasum;		/* check sum of data */
192 	daddr_t	ss_next;		/* next segment */
193 	u_long	ss_create;		/* creation time stamp */
194 	u_short	ss_nfinfo;		/* number of file info structures */
195 	u_short	ss_ninos;		/* number of inodes in summary */
196 #define	SS_DIROP	0x01		/* segment begins a dirop */
197 #define	SS_CONT		0x02		/* more partials to finish this write*/
198 	u_short	ss_flags;		/* used for directory operations */
199 	u_short	ss_pad;			/* extra space */
200 	/* FINFO's and inode daddr's... */
201 };
202 
203 /* NINDIR is the number of indirects in a file system block. */
204 #define	NINDIR(fs)	((fs)->lfs_nindir)
205 
206 /* INOPB is the number of inodes in a secondary storage block. */
207 #define	INOPB(fs)	((fs)->lfs_inopb)
208 
209 #define	blksize(fs)		((fs)->lfs_bsize)
210 #define	blkoff(fs, loc)		((loc) & (fs)->lfs_bmask)
211 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
212 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
213 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
214 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
215 #define numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
216 	((loc) >> (fs)->lfs_bshift)
217 
218 #define	datosn(fs, daddr)	/* disk address to segment number */ \
219 	(((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
220 #define sntoda(fs, sn) 		/* segment number to disk address */ \
221 	((daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) + \
222 	    (fs)->lfs_sboffs[0]))
223 
224 /* Read in the block with the cleaner info from the ifile. */
225 #define LFS_CLEANERINFO(CP, F, BP) { \
226 	VTOI((F)->lfs_ivnode)->i_flag |= IACC; \
227 	if (bread((F)->lfs_ivnode, (daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP))) \
228 		panic("lfs: ifile read"); \
229 	(CP) = (CLEANERINFO *)(BP)->b_un.b_addr; \
230 }
231 
232 /* Read in the block with a specific inode from the ifile. */
233 #define	LFS_IENTRY(IP, F, IN, BP) { \
234 	VTOI((F)->lfs_ivnode)->i_flag |= IACC; \
235 	if (bread((F)->lfs_ivnode, \
236 	    (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz, \
237 	    (F)->lfs_bsize, NOCRED, &(BP))) \
238 		panic("lfs: ifile read"); \
239 	(IP) = (IFILE *)(BP)->b_un.b_addr + (IN) % (F)->lfs_ifpb; \
240 }
241 
242 /* Read in the block with a specific segment usage entry from the ifile. */
243 #define	LFS_SEGENTRY(SP, F, IN, BP) { \
244 	VTOI((F)->lfs_ivnode)->i_flag |= IACC; \
245 	if (bread((F)->lfs_ivnode, \
246 	    ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz, \
247 	    (F)->lfs_bsize, NOCRED, &(BP))) \
248 		panic("lfs: ifile read"); \
249 	(SP) = (SEGUSE *)(BP)->b_un.b_addr + ((IN) & (F)->lfs_sepb - 1); \
250 }
251 
252 /* Write a block and update the inode change times. */
253 #define	LFS_UBWRITE(BP) { \
254 	VTOI((BP)->b_vp)->i_flag |= ICHG | IUPD; \
255 	VOP_BWRITE(BP); \
256 }
257 
258 /*
259  * Structures used by lfs_bmapv and lfs_markv to communicate information
260  * about inodes and data blocks.
261  */
262 typedef struct block_info {
263 	ino_t	bi_inode;		/* inode # */
264 	daddr_t	bi_lbn;			/* logical block w/in file */
265 	daddr_t	bi_daddr;		/* disk address of block */
266 	time_t	bi_segcreate;		/* origin segment create time */
267 	void	*bi_bp;			/* data buffer */
268 } BLOCK_INFO;
269 
270 typedef struct inode_info {
271 	ino_t	ii_inode;		/* inode # */
272 	daddr_t	ii_daddr;		/* disk address of block */
273 	time_t	ii_segcreate;		/* origin segment create time */
274 	struct dinode *ii_dinode;	/* data buffer */
275 } INODE_INFO;
276