xref: /netbsd-src/sbin/fsck_ffs/fsck.h (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: fsck.h,v 1.46 2008/02/23 21:41:48 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  *
8  * This software was developed for the FreeBSD Project by Marshall
9  * Kirk McKusick and Network Associates Laboratories, the Security
10  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
11  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
12  * research program
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)fsck.h	8.4 (Berkeley) 5/9/95
39  */
40 
41 #include <stdio.h>
42 #include <machine/bswap.h>
43 
44 #ifdef PROGRESS
45 #include "progress.h"
46 #endif /* PROGRESS */
47 
48 #define	MAXDUP		10	/* limit on dup blks (per inode) */
49 #define	MAXBAD		10	/* limit on bad blks (per inode) */
50 #define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
51 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
52 
53 union dinode {
54 	struct ufs1_dinode dp1;
55 	struct ufs2_dinode dp2;
56 };
57 #define       DIP(dp, field) \
58 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
59 
60 #define       DIP_SET(dp, field, val) do {	\
61 	if (is_ufs2)				\
62 		(dp)->dp2.di_##field = (val);	\
63 	else					\
64 		(dp)->dp1.di_##field = (val);	\
65 } while (0)
66 
67 #ifndef BUFSIZ
68 #define BUFSIZ 1024
69 #endif
70 
71 /*
72  * Each inode on the filesystem is described by the following structure.
73  * The linkcnt is initially set to the value in the inode. Each time it
74  * is found during the descent in passes 2, 3, and 4 the count is
75  * decremented. Any inodes whose count is non-zero after pass 4 needs to
76  * have its link count adjusted by the value remaining in ino_linkcnt.
77  */
78 struct inostat {
79 	char    ino_state;	/* state of inode, see below */
80 	char    ino_type;	/* type of inode */
81 	short   ino_linkcnt;	/* number of links not found */
82 };
83 /*
84  * Inode states.
85  */
86 #define	USTATE	01		/* inode not allocated */
87 #define	FSTATE	02		/* inode is file */
88 #define	DSTATE	03		/* inode is directory */
89 #define	DFOUND	04		/* directory found during descent */
90 #define	DCLEAR	05		/* directory is to be cleared */
91 #define	FCLEAR	06		/* file is to be cleared */
92 #define	DMARK	07		/* used in propagate()'s traversal algorithm */
93 
94 /*
95  * Inode state information is contained on per cylinder group lists
96  * which are described by the following structure.
97  */
98 struct inostatlist {
99 	long    il_numalloced;  /* number of inodes allocated in this cg */
100 	struct inostat *il_stat;/* inostat info for this cylinder group */
101 } *inostathead;
102 
103 
104 /*
105  * buffer cache structure.
106  */
107 struct bufarea {
108 	struct bufarea *b_next;		/* free list queue */
109 	struct bufarea *b_prev;		/* free list queue */
110 	daddr_t b_bno;
111 	int b_size;
112 	int b_errs;
113 	int b_flags;
114 	union {
115 		char *b_buf;			/* buffer space */
116 		int32_t *b_indir1;		/* indirect block */
117 		int64_t *b_indir2;		/* indirect block */
118 		struct fs *b_fs;		/* super block */
119 		struct cg *b_cg;		/* cylinder group */
120 		struct ufs1_dinode *b_dinode1;	/* UFS1 inode block */
121 		struct ufs2_dinode *b_dinode2;	/* UFS2 inode block */
122 		struct appleufslabel *b_appleufs;		/* Apple UFS volume label */
123 	} b_un;
124 	char b_dirty;
125 };
126 
127 #define       IBLK(bp, i) \
128 	(is_ufs2 ?  (bp)->b_un.b_indir2[i] : (bp)->b_un.b_indir1[i])
129 
130 #define       IBLK_SET(bp, i, val) do {		\
131 	if (is_ufs2)				\
132 		(bp)->b_un.b_indir2[i] = (val);	\
133 	else					\
134 		(bp)->b_un.b_indir1[i] = (val);	\
135 } while (0)
136 
137 #define	B_INUSE 1
138 
139 #define	MINBUFS		5	/* minimum number of buffers required */
140 struct bufarea bufhead;		/* head of list of other blks in filesys */
141 struct bufarea sblk;		/* file system superblock */
142 struct bufarea asblk;		/* file system superblock */
143 struct bufarea cgblk;		/* cylinder group blocks */
144 struct bufarea appleufsblk;		/* Apple UFS volume label */
145 struct bufarea *pdirbp;		/* current directory contents */
146 struct bufarea *pbp;		/* current inode block */
147 
148 #define	dirty(bp)	(bp)->b_dirty = 1
149 #define	initbarea(bp) \
150 	(bp)->b_dirty = 0; \
151 	(bp)->b_bno = (daddr_t)-1; \
152 	(bp)->b_flags = 0;
153 
154 struct fs *sblock;
155 struct fs *altsblock;
156 struct cg *cgrp;
157 struct fs *sblocksave;
158 #define	sbdirty() \
159 	do { \
160 		memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE); \
161 		sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave); \
162 		if (needswap) \
163 			ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs); \
164 		sblk.b_dirty = 1; \
165 	} while (0)
166 #define	cgdirty()	do {copyback_cg(&cgblk); cgblk.b_dirty = 1;} while (0)
167 
168 #define appleufsdirty() \
169 	do { \
170 		appleufsblk.b_un.b_appleufs->ul_checksum = 0; \
171 		appleufsblk.b_un.b_appleufs->ul_checksum =  \
172 			ffs_appleufs_cksum(appleufsblk.b_un.b_appleufs); \
173 		appleufsblk.b_dirty = 1; \
174 	} while (0)
175 
176 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
177 
178 struct inodesc {
179 	enum fixstate id_fix;	/* policy on fixing errors */
180 	int (*id_func)		/* function to be applied to blocks of inode */
181 	    (struct inodesc *);
182 	ino_t id_number;	/* inode number described */
183 	ino_t id_parent;	/* for DATA nodes, their parent */
184 	daddr_t id_blkno;	/* current block number being examined */
185 	int id_numfrags;	/* number of frags contained in block */
186 	int64_t id_filesize;	/* for DATA nodes, the size of the directory */
187 	int id_loc;		/* for DATA nodes, current location in dir */
188 	int64_t id_entryno;	/* for DATA nodes, current entry number */
189 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
190 	const char *id_name;	/* for DATA nodes, name to find or enter */
191 	char id_type;		/* type of descriptor, DATA or ADDR */
192 };
193 /* file types */
194 #define	DATA	1
195 #define	SNAP	2
196 #define	ADDR	3
197 
198 /*
199  * Linked list of duplicate blocks.
200  *
201  * The list is composed of two parts. The first part of the
202  * list (from duplist through the node pointed to by muldup)
203  * contains a single copy of each duplicate block that has been
204  * found. The second part of the list (from muldup to the end)
205  * contains duplicate blocks that have been found more than once.
206  * To check if a block has been found as a duplicate it is only
207  * necessary to search from duplist through muldup. To find the
208  * total number of times that a block has been found as a duplicate
209  * the entire list must be searched for occurrences of the block
210  * in question. The following diagram shows a sample list where
211  * w (found twice), x (found once), y (found three times), and z
212  * (found once) are duplicate block numbers:
213  *
214  *    w -> y -> x -> z -> y -> w -> y
215  *    ^		     ^
216  *    |		     |
217  * duplist	  muldup
218  */
219 struct dups {
220 	struct dups *next;
221 	daddr_t dup;
222 };
223 struct dups *duplist;		/* head of dup list */
224 struct dups *muldup;		/* end of unique duplicate dup block numbers */
225 
226 /*
227  * Linked list of inodes with zero link counts.
228  */
229 struct zlncnt {
230 	struct zlncnt *next;
231 	ino_t zlncnt;
232 };
233 struct zlncnt *zlnhead;		/* head of zero link count list */
234 
235 /*
236  * Inode cache data structures.
237  */
238 struct inoinfo {
239 	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
240 	struct	inoinfo	*i_child, *i_sibling;
241 	ino_t	i_number;		/* inode number of this entry */
242 	ino_t	i_parent;		/* inode number of parent */
243 	ino_t	i_dotdot;		/* inode number of `..' */
244 	size_t	i_isize;		/* size of inode */
245 	u_int	i_numblks;		/* size of block array in bytes */
246 	int64_t i_blks[1];		/* actually longer */
247 } **inphead, **inpsort;
248 long numdirs, dirhash, listmax, inplast;
249 
250 long	dev_bsize;		/* computed value of DEV_BSIZE */
251 long	secsize;		/* actual disk sector size */
252 char	nflag;			/* assume a no response */
253 char	yflag;			/* assume a yes response */
254 int	bflag;			/* location of alternate super block */
255 int	debug;			/* output debugging info */
256 int	cvtlevel;		/* convert to newer file system format */
257 int	doinglevel1;		/* converting to new cylinder group format */
258 int	doinglevel2;		/* converting to new inode format */
259 int	newinofmt;		/* filesystem has new inode format */
260 char	usedsoftdep;		/* just fix soft dependency inconsistencies */
261 int	preen;			/* just fix normal inconsistencies */
262 int	quiet;			/* Don't print anything if clean */
263 int	forceimage;		/* file system is an image file */
264 int	doswap;			/* convert byte order */
265 int	needswap;		/* need to convert byte order in memory */
266 int	is_ufs2;		/* we're dealing with an UFS2 filesystem */
267 int	do_blkswap;		/* need to do block addr byteswap */
268 int	do_dirswap;		/* need to do dir entry byteswap */
269 int	endian;			/* endian coversion */
270 int	markclean;		/* mark file system clean when done */
271 char	havesb;			/* superblock has been read */
272 char	skipclean;		/* skip clean file systems if preening */
273 int	fsmodified;		/* 1 => write done to file system */
274 int	fsreadfd;		/* file descriptor for reading file system */
275 int	fswritefd;		/* file descriptor for writing file system */
276 int	rerun;			/* rerun fsck.  Only used in non-preen mode */
277 char	resolved;		/* cleared if unresolved changes => not clean */
278 int	isappleufs;		/* filesystem is Apple UFS */
279 
280 daddr_t maxfsblock;		/* number of blocks in the file system */
281 char	*blockmap;		/* ptr to primary blk allocation map */
282 ino_t	maxino;			/* number of inodes in file system */
283 
284 int	dirblksiz;
285 
286 extern ino_t	lfdir;		/* lost & found directory inode number */
287 extern const char *lfname;	/* lost & found directory name */
288 extern int	lfmode;		/* lost & found directory creation mode */
289 
290 daddr_t n_blks;		/* number of blocks in use */
291 ino_t n_files;		/* number of files in use */
292 
293 long countdirs;
294 
295 int	got_siginfo;		/* received a SIGINFO */
296 
297 #define	clearinode(dp) \
298 do { \
299 	if (is_ufs2) 			\
300 		(dp)->dp2 = ufs2_zino;	\
301 	else				\
302 		(dp)->dp1 = ufs1_zino;	\
303 } while (0)
304 
305 struct	ufs1_dinode ufs1_zino;
306 struct	ufs2_dinode ufs2_zino;
307 
308 #define	setbmap(blkno)	setbit(blockmap, blkno)
309 #define	testbmap(blkno)	isset(blockmap, blkno)
310 #define	clrbmap(blkno)	clrbit(blockmap, blkno)
311 
312 #define	STOP	0x01
313 #define	SKIP	0x02
314 #define	KEEPON	0x04
315 #define	ALTERED	0x08
316 #define	FOUND	0x10
317 
318 /* some inline functs to help the byte-swapping mess */
319 static inline u_int16_t iswap16 (u_int16_t);
320 static inline u_int32_t iswap32 (u_int32_t);
321 static inline u_int64_t iswap64 (u_int64_t);
322 
323 static inline u_int16_t iswap16(x)
324 	u_int16_t x;
325 {
326 	if (needswap)
327 		return bswap16(x);
328 	else return x;
329 }
330 
331 static inline u_int32_t iswap32(x)
332 	u_int32_t x;
333 {
334 	if (needswap)
335 		return bswap32(x);
336 	else return x;
337 }
338 
339 static inline u_int64_t iswap64(x)
340 	u_int64_t x;
341 {
342 	if (needswap)
343 		return bswap64(x);
344 	else return x;
345 }
346