xref: /freebsd/sbin/fsck_ffs/fsck.h (revision 80ff58b89dcacfe07fe20b045890df9db5ca0af0)
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Copyright (c) 1980, 1986, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 4. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  *	@(#)fsck.h	8.4 (Berkeley) 5/9/95
60  * $FreeBSD$
61  */
62 
63 #ifndef _FSCK_H_
64 #define	_FSCK_H_
65 
66 #include <unistd.h>
67 #include <stdlib.h>
68 #include <stdio.h>
69 
70 #include <sys/queue.h>
71 
72 #define	MAXDUP		10	/* limit on dup blks (per inode) */
73 #define	MAXBAD		10	/* limit on bad blks (per inode) */
74 #define	MINBUFS		10	/* minimum number of buffers required */
75 #define	MAXBUFS		40	/* maximum space to allocate to buffers */
76 #define	INOBUFSIZE	64*1024	/* size of buffer to read inodes in pass1 */
77 
78 union dinode {
79 	struct ufs1_dinode dp1;
80 	struct ufs2_dinode dp2;
81 };
82 #define	DIP(dp, field) \
83 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
84 	(dp)->dp1.field : (dp)->dp2.field)
85 
86 #define DIP_SET(dp, field, val) do { \
87 	if (sblock.fs_magic == FS_UFS1_MAGIC) \
88 		(dp)->dp1.field = (val); \
89 	else \
90 		(dp)->dp2.field = (val); \
91 	} while (0)
92 
93 /*
94  * Each inode on the file system is described by the following structure.
95  * The linkcnt is initially set to the value in the inode. Each time it
96  * is found during the descent in passes 2, 3, and 4 the count is
97  * decremented. Any inodes whose count is non-zero after pass 4 needs to
98  * have its link count adjusted by the value remaining in ino_linkcnt.
99  */
100 struct inostat {
101 	char	ino_state;	/* state of inode, see below */
102 	char	ino_type;	/* type of inode */
103 	short	ino_linkcnt;	/* number of links not found */
104 };
105 /*
106  * Inode states.
107  */
108 #define	USTATE	0x1		/* inode not allocated */
109 #define	FSTATE	0x2		/* inode is file */
110 #define	FZLINK	0x3		/* inode is file with a link count of zero */
111 #define	DSTATE	0x4		/* inode is directory */
112 #define	DZLINK	0x5		/* inode is directory with a zero link count  */
113 #define	DFOUND	0x6		/* directory found during descent */
114 /*     		0x7		   UNUSED - see S_IS_DVALID() definition */
115 #define	DCLEAR	0x8		/* directory is to be cleared */
116 #define	FCLEAR	0x9		/* file is to be cleared */
117 /*     	DUNFOUND === (state == DSTATE || state == DZLINK) */
118 #define	S_IS_DUNFOUND(state)	(((state) & ~0x1) == DSTATE)
119 /*     	DVALID   === (state == DSTATE || state == DZLINK || state == DFOUND) */
120 #define	S_IS_DVALID(state)	(((state) & ~0x3) == DSTATE)
121 #define	INO_IS_DUNFOUND(ino)	S_IS_DUNFOUND(inoinfo(ino)->ino_state)
122 #define	INO_IS_DVALID(ino)	S_IS_DVALID(inoinfo(ino)->ino_state)
123 /*
124  * Inode state information is contained on per cylinder group lists
125  * which are described by the following structure.
126  */
127 struct inostatlist {
128 	long	il_numalloced;	/* number of inodes allocated in this cg */
129 	struct inostat *il_stat;/* inostat info for this cylinder group */
130 } *inostathead;
131 
132 /*
133  * buffer cache structure.
134  */
135 struct bufarea {
136 	TAILQ_ENTRY(bufarea) b_list;		/* buffer list */
137 	ufs2_daddr_t b_bno;
138 	int b_size;
139 	int b_errs;
140 	int b_flags;
141 	int b_type;
142 	union {
143 		char *b_buf;			/* buffer space */
144 		ufs1_daddr_t *b_indir1;		/* UFS1 indirect block */
145 		ufs2_daddr_t *b_indir2;		/* UFS2 indirect block */
146 		struct fs *b_fs;		/* super block */
147 		struct cg *b_cg;		/* cylinder group */
148 		struct ufs1_dinode *b_dinode1;	/* UFS1 inode block */
149 		struct ufs2_dinode *b_dinode2;	/* UFS2 inode block */
150 	} b_un;
151 	char b_dirty;
152 };
153 
154 #define	IBLK(bp, i) \
155 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
156 	(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
157 
158 #define IBLK_SET(bp, i, val) do { \
159 	if (sblock.fs_magic == FS_UFS1_MAGIC) \
160 		(bp)->b_un.b_indir1[i] = (val); \
161 	else \
162 		(bp)->b_un.b_indir2[i] = (val); \
163 	} while (0)
164 
165 /*
166  * Buffer flags
167  */
168 #define	B_INUSE 	0x00000001	/* Buffer is in use */
169 /*
170  * Type of data in buffer
171  */
172 #define	BT_UNKNOWN 	 0	/* Buffer holds a superblock */
173 #define	BT_SUPERBLK 	 1	/* Buffer holds a superblock */
174 #define	BT_CYLGRP 	 2	/* Buffer holds a cylinder group map */
175 #define	BT_LEVEL1 	 3	/* Buffer holds single level indirect */
176 #define	BT_LEVEL2 	 4	/* Buffer holds double level indirect */
177 #define	BT_LEVEL3 	 5	/* Buffer holds triple level indirect */
178 #define	BT_EXTATTR 	 6	/* Buffer holds external attribute data */
179 #define	BT_INODES 	 7	/* Buffer holds external attribute data */
180 #define	BT_DIRDATA 	 8	/* Buffer holds directory data */
181 #define	BT_DATA	 	 9	/* Buffer holds user data */
182 #define BT_NUMBUFTYPES	10
183 #define BT_NAMES {			\
184 	"unknown",			\
185 	"Superblock",			\
186 	"Cylinder Group",		\
187 	"Single Level Indirect",	\
188 	"Double Level Indirect",	\
189 	"Triple Level Indirect",	\
190 	"External Attribute",		\
191 	"Inode Block",			\
192 	"Directory Contents",		\
193 	"User Data" }
194 long readcnt[BT_NUMBUFTYPES];
195 long totalreadcnt[BT_NUMBUFTYPES];
196 struct timespec readtime[BT_NUMBUFTYPES];
197 struct timespec totalreadtime[BT_NUMBUFTYPES];
198 struct timespec startprog;
199 
200 struct bufarea sblk;		/* file system superblock */
201 struct bufarea cgblk;		/* cylinder group blocks */
202 struct bufarea *pdirbp;		/* current directory contents */
203 struct bufarea *pbp;		/* current inode block */
204 
205 #define	dirty(bp) do { \
206 	if (fswritefd < 0) \
207 		pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \
208 	else \
209 		(bp)->b_dirty = 1; \
210 } while (0)
211 #define	initbarea(bp, type) do { \
212 	(bp)->b_dirty = 0; \
213 	(bp)->b_bno = (ufs2_daddr_t)-1; \
214 	(bp)->b_flags = 0; \
215 	(bp)->b_type = type; \
216 } while (0)
217 
218 #define	sbdirty()	dirty(&sblk)
219 #define	cgdirty()	dirty(&cgblk)
220 #define	sblock		(*sblk.b_un.b_fs)
221 #define	cgrp		(*cgblk.b_un.b_cg)
222 
223 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
224 ino_t cursnapshot;
225 
226 struct inodesc {
227 	enum fixstate id_fix;	/* policy on fixing errors */
228 	int (*id_func)(struct inodesc *);
229 				/* function to be applied to blocks of inode */
230 	ino_t id_number;	/* inode number described */
231 	ino_t id_parent;	/* for DATA nodes, their parent */
232 	ufs_lbn_t id_lbn;	/* logical block number of current block */
233 	ufs2_daddr_t id_blkno;	/* current block number being examined */
234 	int id_numfrags;	/* number of frags contained in block */
235 	off_t id_filesize;	/* for DATA nodes, the size of the directory */
236 	ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */
237 	int id_loc;		/* for DATA nodes, current location in dir */
238 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
239 	char *id_name;		/* for DATA nodes, name to find or enter */
240 	char id_type;		/* type of descriptor, DATA or ADDR */
241 };
242 /* file types */
243 #define	DATA	1	/* a directory */
244 #define	SNAP	2	/* a snapshot */
245 #define	ADDR	3	/* anything but a directory or a snapshot */
246 
247 /*
248  * Linked list of duplicate blocks.
249  *
250  * The list is composed of two parts. The first part of the
251  * list (from duplist through the node pointed to by muldup)
252  * contains a single copy of each duplicate block that has been
253  * found. The second part of the list (from muldup to the end)
254  * contains duplicate blocks that have been found more than once.
255  * To check if a block has been found as a duplicate it is only
256  * necessary to search from duplist through muldup. To find the
257  * total number of times that a block has been found as a duplicate
258  * the entire list must be searched for occurrences of the block
259  * in question. The following diagram shows a sample list where
260  * w (found twice), x (found once), y (found three times), and z
261  * (found once) are duplicate block numbers:
262  *
263  *    w -> y -> x -> z -> y -> w -> y
264  *    ^		     ^
265  *    |		     |
266  * duplist	  muldup
267  */
268 struct dups {
269 	struct dups *next;
270 	ufs2_daddr_t dup;
271 };
272 struct dups *duplist;		/* head of dup list */
273 struct dups *muldup;		/* end of unique duplicate dup block numbers */
274 
275 /*
276  * Inode cache data structures.
277  */
278 struct inoinfo {
279 	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
280 	ino_t	i_number;		/* inode number of this entry */
281 	ino_t	i_parent;		/* inode number of parent */
282 	ino_t	i_dotdot;		/* inode number of `..' */
283 	size_t	i_isize;		/* size of inode */
284 	u_int	i_numblks;		/* size of block array in bytes */
285 	ufs2_daddr_t i_blks[1];		/* actually longer */
286 } **inphead, **inpsort;
287 long numdirs, dirhash, listmax, inplast;
288 long countdirs;			/* number of directories we actually found */
289 
290 #define MIBSIZE	3		/* size of fsck sysctl MIBs */
291 int	adjrefcnt[MIBSIZE];	/* MIB command to adjust inode reference cnt */
292 int	adjblkcnt[MIBSIZE];	/* MIB command to adjust inode block count */
293 int	adjndir[MIBSIZE];	/* MIB command to adjust number of directories */
294 int	adjnbfree[MIBSIZE];	/* MIB command to adjust number of free blocks */
295 int	adjnifree[MIBSIZE];	/* MIB command to adjust number of free inodes */
296 int	adjnffree[MIBSIZE];	/* MIB command to adjust number of free frags */
297 int	adjnumclusters[MIBSIZE];	/* MIB command to adjust number of free clusters */
298 int	freefiles[MIBSIZE];	/* MIB command to free a set of files */
299 int	freedirs[MIBSIZE];	/* MIB command to free a set of directories */
300 int	freeblks[MIBSIZE];	/* MIB command to free a set of data blocks */
301 struct	fsck_cmd cmd;		/* sysctl file system update commands */
302 char	snapname[BUFSIZ];	/* when doing snapshots, the name of the file */
303 char	*cdevname;		/* name of device being checked */
304 long	dev_bsize;		/* computed value of DEV_BSIZE */
305 long	secsize;		/* actual disk sector size */
306 u_int	real_dev_bsize;		/* actual disk sector size, not overriden */
307 char	nflag;			/* assume a no response */
308 char	yflag;			/* assume a yes response */
309 int	bkgrdflag;		/* use a snapshot to run on an active system */
310 int	bflag;			/* location of alternate super block */
311 int	debug;			/* output debugging info */
312 int	Eflag;			/* zero out empty data blocks */
313 int	inoopt;			/* trim out unused inodes */
314 char	ckclean;		/* only do work if not cleanly unmounted */
315 int	cvtlevel;		/* convert to newer file system format */
316 int	bkgrdcheck;		/* determine if background check is possible */
317 int	bkgrdsumadj;		/* whether the kernel have ability to adjust superblock summary */
318 char	usedsoftdep;		/* just fix soft dependency inconsistencies */
319 char	preen;			/* just fix normal inconsistencies */
320 char	rerun;			/* rerun fsck. Only used in non-preen mode */
321 int	returntosingle;		/* 1 => return to single user mode on exit */
322 char	resolved;		/* cleared if unresolved changes => not clean */
323 char	havesb;			/* superblock has been read */
324 char	skipclean;		/* skip clean file systems if preening */
325 int	fsmodified;		/* 1 => write done to file system */
326 int	fsreadfd;		/* file descriptor for reading file system */
327 int	fswritefd;		/* file descriptor for writing file system */
328 
329 ufs2_daddr_t maxfsblock;	/* number of blocks in the file system */
330 char	*blockmap;		/* ptr to primary blk allocation map */
331 ino_t	maxino;			/* number of inodes in file system */
332 
333 ino_t	lfdir;			/* lost & found directory inode number */
334 const char *lfname;		/* lost & found directory name */
335 int	lfmode;			/* lost & found directory creation mode */
336 
337 ufs2_daddr_t n_blks;		/* number of blocks in use */
338 ino_t n_files;			/* number of files in use */
339 
340 volatile sig_atomic_t	got_siginfo;	/* received a SIGINFO */
341 volatile sig_atomic_t	got_sigalarm;	/* received a SIGALRM */
342 
343 #define	clearinode(dp) \
344 	if (sblock.fs_magic == FS_UFS1_MAGIC) { \
345 		(dp)->dp1 = ufs1_zino; \
346 	} else { \
347 		(dp)->dp2 = ufs2_zino; \
348 	}
349 struct	ufs1_dinode ufs1_zino;
350 struct	ufs2_dinode ufs2_zino;
351 
352 #define	setbmap(blkno)	setbit(blockmap, blkno)
353 #define	testbmap(blkno)	isset(blockmap, blkno)
354 #define	clrbmap(blkno)	clrbit(blockmap, blkno)
355 
356 #define	STOP	0x01
357 #define	SKIP	0x02
358 #define	KEEPON	0x04
359 #define	ALTERED	0x08
360 #define	FOUND	0x10
361 
362 #define	EEXIT	8		/* Standard error exit. */
363 
364 struct fstab;
365 
366 
367 void		adjust(struct inodesc *, int lcnt);
368 ufs2_daddr_t	allocblk(long frags);
369 ino_t		allocdir(ino_t parent, ino_t request, int mode);
370 ino_t		allocino(ino_t request, int type);
371 void		blkerror(ino_t ino, const char *type, ufs2_daddr_t blk);
372 char	       *blockcheck(char *name);
373 int		blread(int fd, char *buf, ufs2_daddr_t blk, long size);
374 void		bufinit(void);
375 void		blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size);
376 void		blerase(int fd, ufs2_daddr_t blk, long size);
377 void		cacheino(union dinode *dp, ino_t inumber);
378 void		catch(int);
379 void		catchquit(int);
380 int		changeino(ino_t dir, const char *name, ino_t newnum);
381 int		check_cgmagic(int cg, struct cg *cgp);
382 int		chkrange(ufs2_daddr_t blk, int cnt);
383 void		ckfini(int markclean);
384 int		ckinode(union dinode *dp, struct inodesc *);
385 void		clri(struct inodesc *, const char *type, int flag);
386 int		clearentry(struct inodesc *);
387 void		direrror(ino_t ino, const char *errmesg);
388 int		dirscan(struct inodesc *);
389 int		dofix(struct inodesc *, const char *msg);
390 int		eascan(struct inodesc *, struct ufs2_dinode *dp);
391 void		fileerror(ino_t cwd, ino_t ino, const char *errmesg);
392 void		finalIOstats(void);
393 int		findino(struct inodesc *);
394 int		findname(struct inodesc *);
395 void		flush(int fd, struct bufarea *bp);
396 void		freeblk(ufs2_daddr_t blkno, long frags);
397 void		freeino(ino_t ino);
398 void		freeinodebuf(void);
399 int		ftypeok(union dinode *dp);
400 void		getblk(struct bufarea *bp, ufs2_daddr_t blk, long size);
401 struct bufarea *getdatablk(ufs2_daddr_t blkno, long size, int type);
402 struct inoinfo *getinoinfo(ino_t inumber);
403 union dinode   *getnextinode(ino_t inumber, int rebuildcg);
404 void		getpathname(char *namebuf, ino_t curdir, ino_t ino);
405 union dinode   *ginode(ino_t inumber);
406 void		infohandler(int sig);
407 void		alarmhandler(int sig);
408 void		inocleanup(void);
409 void		inodirty(void);
410 struct inostat *inoinfo(ino_t inum);
411 void		IOstats(char *what);
412 int		linkup(ino_t orphan, ino_t parentdir, char *name);
413 int		makeentry(ino_t parent, ino_t ino, const char *name);
414 void		panic(const char *fmt, ...) __printflike(1, 2);
415 void		pass1(void);
416 void		pass1b(void);
417 int		pass1check(struct inodesc *);
418 void		pass2(void);
419 void		pass3(void);
420 void		pass4(void);
421 int		pass4check(struct inodesc *);
422 void		pass5(void);
423 void		pfatal(const char *fmt, ...) __printflike(1, 2);
424 void		pinode(ino_t ino);
425 void		propagate(void);
426 void		pwarn(const char *fmt, ...) __printflike(1, 2);
427 int		readsb(int listerr);
428 int		reply(const char *question);
429 void		rwerror(const char *mesg, ufs2_daddr_t blk);
430 void		sblock_init(void);
431 void		setinodebuf(ino_t);
432 int		setup(char *dev);
433 void		gjournal_check(const char *filesys);
434 int		suj_check(const char *filesys);
435 void		update_maps(struct cg *, struct cg*, int);
436 
437 #endif	/* !_FSCK_H_ */
438