xref: /openbsd/sbin/fsck_ffs/fsck.h (revision 8529ddd3)
1 /*	$OpenBSD: fsck.h,v 1.31 2015/01/19 18:20:47 deraadt Exp $	*/
2 /*	$NetBSD: fsck.h,v 1.13 1996/10/11 20:15:46 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
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  * Copyright (c) 1980, 1986, 1993
15  *	The Regents of the University of California.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)fsck.h	8.1 (Berkeley) 6/5/93
42  */
43 
44 #define	MAXDUP		10	/* limit on dup blks (per inode) */
45 #define	MAXBAD		10	/* limit on bad blks (per inode) */
46 #define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
47 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
48 
49 union dinode {
50 	struct ufs1_dinode dp1;
51 	struct ufs2_dinode dp2;
52 };
53 
54 #define	DIP(dp, field)				\
55 	((sblock.fs_magic == FS_UFS1_MAGIC) ?	\
56 	(dp)->dp1.field : (dp)->dp2.field)
57 
58 #define	DIP_SET(dp, field, val) do { \
59 	if (sblock.fs_magic == FS_UFS1_MAGIC)	\
60 		(dp)->dp1.field = (val);	\
61 	else					\
62 		(dp)->dp2.field = (val);	\
63 	} while (0)
64 
65 /*
66  * Each inode on the file system is described by the following structure.
67  * The linkcnt is initially set to the value in the inode. Each time it
68  * is found during the descent in passes 2, 3, and 4 the count is
69  * decremented. Any inodes whose count is non-zero after pass 4 needs to
70  * have its link count adjusted by the value remaining in ino_linkcnt.
71  */
72 struct inostat {
73 	char    ino_state;      /* state of inode, see below */
74 	char    ino_type;       /* type of inode */
75 	short   ino_linkcnt;    /* number of links not found */
76 };
77 
78 #define	USTATE	01		/* inode not allocated */
79 #define	FSTATE	02		/* inode is file */
80 #define	DSTATE	03		/* inode is directory */
81 #define	DFOUND	04		/* directory found during descent */
82 #define	DCLEAR	05		/* directory is to be cleared */
83 #define	FCLEAR	06		/* file is to be cleared */
84 
85 /*
86  * Inode state information is contained on per cylinder group lists
87  * which are described by the following structure.
88  */
89 struct inostatlist {
90 	long    il_numalloced;  /* number of inodes allocated in this cg */
91 	struct inostat *il_stat;/* inostat info for this cylinder group */
92 } *inostathead;
93 
94 #define GET_ISTATE(ino)		(inoinfo(ino)->ino_state)
95 #define GET_ITYPE(ino)		(inoinfo(ino)->ino_type)
96 #define SET_ISTATE(ino, v)	do { GET_ISTATE(ino) = (v); } while (0)
97 #define SET_ITYPE(ino, v)	do { GET_ITYPE(ino) = (v); } while (0)
98 #define ILNCOUNT(ino)		(inoinfo(ino)->ino_linkcnt)
99 
100 /*
101  * buffer cache structure.
102  */
103 struct bufarea {
104 	daddr_t	b_bno;
105 	struct bufarea	*b_next;		/* free list queue */
106 	struct bufarea	*b_prev;		/* free list queue */
107 	int	b_size;
108 	int	b_errs;
109 	int	b_flags;
110 	union {
111 		char	*b_buf;			/* buffer space */
112 		int32_t	*b_indir1;		/* FFS1 indirect block */
113 		int64_t	*b_indir2;		/* FFS2 indirect block */
114 		struct	fs *b_fs;		/* super block */
115 		struct	cg *b_cg;		/* cylinder group */
116 		struct	ufs1_dinode *b_dinode1;	/* FFS1 inode block */
117 		struct	ufs2_dinode *b_dinode2;	/* FFS2 inode block */
118 	} b_un;
119 	char	b_dirty;
120 };
121 
122 #define IBLK(bp, i)				\
123 	((sblock.fs_magic == FS_UFS1_MAGIC) ?	\
124 	(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
125 
126 #define IBLK_SET(bp, i, val) do {		\
127 	if (sblock.fs_magic == FS_UFS1_MAGIC)	\
128 		(bp)->b_un.b_indir1[i] = (val);	\
129 	else					\
130 		(bp)->b_un.b_indir2[i] = (val);	\
131 	} while (0)
132 
133 #define	B_INUSE 1
134 
135 #define	MINBUFS		5	/* minimum number of buffers required */
136 struct bufarea bufhead;		/* head of list of other blks in filesys */
137 struct bufarea sblk;		/* file system superblock */
138 struct bufarea asblk;		/* alternate file system superblock */
139 struct bufarea cgblk;		/* cylinder group blocks */
140 struct bufarea *pdirbp;		/* current directory contents */
141 struct bufarea *pbp;		/* current inode block */
142 struct bufarea *getdatablk(daddr_t, long);
143 
144 #define	dirty(bp)	(bp)->b_dirty = 1
145 #define	initbarea(bp) \
146 	(bp)->b_dirty = 0; \
147 	(bp)->b_bno = -1; \
148 	(bp)->b_flags = 0;
149 
150 #define	sbdirty()	sblk.b_dirty = 1
151 #define	cgdirty()	cgblk.b_dirty = 1
152 #define	sblock		(*sblk.b_un.b_fs)
153 #define	cgrp		(*cgblk.b_un.b_cg)
154 
155 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
156 
157 struct inodesc {
158 	daddr_t id_blkno;	/* current block number being examined */
159 	quad_t id_filesize;	/* for DATA nodes, the size of the directory */
160 	u_int64_t id_entryno;	/* for DATA nodes, current entry number */
161 	ino_t id_number;	/* inode number described */
162 	ino_t id_parent;	/* for DATA nodes, their parent */
163 	int (*id_func)		/* function to be applied to blocks of inode */
164 (struct inodesc *);
165 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
166 	char *id_name;		/* for DATA nodes, name to find or enter */
167 	int id_numfrags;	/* number of frags contained in block */
168 	int id_loc;		/* for DATA nodes, current location in dir */
169 	enum fixstate id_fix;	/* policy on fixing errors */
170 	char id_type;		/* type of descriptor, DATA or ADDR */
171 };
172 /* file types */
173 #define	DATA	1
174 #define	ADDR	2
175 
176 /*
177  * Linked list of duplicate blocks.
178  *
179  * The list is composed of two parts. The first part of the
180  * list (from duplist through the node pointed to by muldup)
181  * contains a single copy of each duplicate block that has been
182  * found. The second part of the list (from muldup to the end)
183  * contains duplicate blocks that have been found more than once.
184  * To check if a block has been found as a duplicate it is only
185  * necessary to search from duplist through muldup. To find the
186  * total number of times that a block has been found as a duplicate
187  * the entire list must be searched for occurrences of the block
188  * in question. The following diagram shows a sample list where
189  * w (found twice), x (found once), y (found three times), and z
190  * (found once) are duplicate block numbers:
191  *
192  *    w -> y -> x -> z -> y -> w -> y
193  *    ^		     ^
194  *    |		     |
195  * duplist	  muldup
196  */
197 struct dups {
198 	struct dups *next;
199 	daddr_t dup;
200 };
201 struct dups *duplist;		/* head of dup list */
202 struct dups *muldup;		/* end of unique duplicate dup block numbers */
203 
204 /*
205  * Linked list of inodes with zero link counts.
206  */
207 struct zlncnt {
208 	struct zlncnt *next;
209 	ino_t zlncnt;
210 };
211 struct zlncnt *zlnhead;		/* head of zero link count list */
212 
213 /*
214  * Inode cache data structures.
215  */
216 struct inoinfo {
217 	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
218 	struct	inoinfo	*i_child, *i_sibling;
219 	size_t	i_isize;		/* size of inode */
220 	ino_t	i_number;		/* inode number of this entry */
221 	ino_t	i_parent;		/* inode number of parent */
222 	ino_t	i_dotdot;		/* inode number of `..' */
223 	u_int	i_numblks;		/* size of block array in bytes */
224 	daddr_t	i_blks[1];		/* actually longer */
225 } **inphead, **inpsort;
226 
227 extern long numdirs, listmax, inplast;
228 
229 long	secsize;		/* actual disk sector size */
230 char	nflag;			/* assume a no response */
231 char	yflag;			/* assume a yes response */
232 int	bflag;			/* location of alternate super block */
233 int	debug;			/* output debugging info */
234 int	cvtlevel;		/* convert to newer file system format */
235 char    usedsoftdep;            /* just fix soft dependency inconsistencies */
236 int	preen;			/* just fix normal inconsistencies */
237 char    resolved;               /* cleared if unresolved changes => not clean */
238 char	havesb;			/* superblock has been read */
239 char	skipclean;		/* skip clean file systems if preening */
240 int	fsmodified;		/* 1 => write done to file system */
241 int	fsreadfd;		/* file descriptor for reading file system */
242 int	fswritefd;		/* file descriptor for writing file system */
243 int	rerun;			/* rerun fsck.  Only used in non-preen mode */
244 
245 daddr_t	maxfsblock;		/* number of blocks in the file system */
246 char	*blockmap;		/* ptr to primary blk allocation map */
247 ino_t	maxino;			/* number of inodes in file system */
248 ino_t	lastino;		/* last inode in use */
249 
250 ino_t	lfdir;			/* lost & found directory inode number */
251 char	*lfname;		/* lost & found directory name */
252 int	lfmode;			/* lost & found directory creation mode */
253 
254 daddr_t	n_blks;			/* number of blocks in use */
255 int64_t	n_files;		/* number of files in use */
256 
257 #define	clearinode(dp)	\
258 	if (sblock.fs_magic == FS_UFS1_MAGIC) {	\
259 		(dp)->dp1 = ufs1_zino;		\
260 	} else {				\
261 		(dp)->dp2 = ufs2_zino;		\
262 	}
263 
264 struct ufs1_dinode ufs1_zino;
265 struct ufs2_dinode ufs2_zino;
266 
267 #define	setbmap(blkno)	setbit(blockmap, blkno)
268 #define	testbmap(blkno)	isset(blockmap, blkno)
269 #define	clrbmap(blkno)	clrbit(blockmap, blkno)
270 
271 #define	STOP	0x01
272 #define	SKIP	0x02
273 #define	KEEPON	0x04
274 #define	ALTERED	0x08
275 #define	FOUND	0x10
276 
277 union dinode *ginode(ino_t);
278 struct inoinfo *getinoinfo(ino_t);
279 void getblk(struct bufarea *, daddr_t, long);
280 ino_t allocino(ino_t, int);
281 
282 int	(*info_fn)(char *, size_t);
283 char	*info_filesys;
284