xref: /freebsd/sbin/fsck_ffs/fsck.h (revision c11e094d96120a2e0e726ed9705ae0ec08db49b6)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)fsck.h	8.4 (Berkeley) 5/9/95
34  * $FreeBSD$
35  */
36 
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 
41 #define	MAXDUP		10	/* limit on dup blks (per inode) */
42 #define	MAXBAD		10	/* limit on bad blks (per inode) */
43 #define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
44 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
45 
46 /*
47  * Each inode on the filesystem is described by the following structure.
48  * The linkcnt is initially set to the value in the inode. Each time it
49  * is found during the descent in passes 2, 3, and 4 the count is
50  * decremented. Any inodes whose count is non-zero after pass 4 needs to
51  * have its link count adjusted by the value remaining in ino_linkcnt.
52  */
53 struct inostat {
54 	char	ino_state;	/* state of inode, see below */
55 	char	ino_type;	/* type of inode */
56 	short	ino_linkcnt;	/* number of links not found */
57 };
58 /*
59  * Inode states.
60  */
61 #define	USTATE	01		/* inode not allocated */
62 #define	FSTATE	02		/* inode is file */
63 #define	DSTATE	03		/* inode is directory */
64 #define	DFOUND	04		/* directory found during descent */
65 #define	DCLEAR	05		/* directory is to be cleared */
66 #define	FCLEAR	06		/* file is to be cleared */
67 /*
68  * Inode state information is contained on per cylinder group lists
69  * which are described by the following structure.
70  */
71 struct inostatlist {
72 	long	il_numalloced;	/* number of inodes allocated in this cg */
73 	struct inostat *il_stat;/* inostat info for this cylinder group */
74 } *inostathead;
75 
76 /*
77  * buffer cache structure.
78  */
79 struct bufarea {
80 	struct bufarea *b_next;		/* free list queue */
81 	struct bufarea *b_prev;		/* free list queue */
82 	ufs_daddr_t b_bno;
83 	int b_size;
84 	int b_errs;
85 	int b_flags;
86 	union {
87 		char *b_buf;			/* buffer space */
88 		ufs_daddr_t *b_indir;		/* indirect block */
89 		struct fs *b_fs;		/* super block */
90 		struct cg *b_cg;		/* cylinder group */
91 		struct dinode *b_dinode;	/* inode block */
92 	} b_un;
93 	char b_dirty;
94 };
95 
96 #define	B_INUSE 1
97 
98 #define	MINBUFS		5	/* minimum number of buffers required */
99 struct bufarea bufhead;		/* head of list of other blks in filesys */
100 struct bufarea sblk;		/* filesystem superblock */
101 struct bufarea cgblk;		/* cylinder group blocks */
102 struct bufarea *pdirbp;		/* current directory contents */
103 struct bufarea *pbp;		/* current inode block */
104 
105 #define	dirty(bp) do { \
106 	if (fswritefd < 0) \
107 		pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \
108 	else \
109 		(bp)->b_dirty = 1; \
110 } while (0)
111 #define	initbarea(bp) do { \
112 	(bp)->b_dirty = 0; \
113 	(bp)->b_bno = (ufs_daddr_t)-1; \
114 	(bp)->b_flags = 0; \
115 } while (0)
116 
117 #define	sbdirty()	dirty(&sblk)
118 #define	cgdirty()	dirty(&cgblk)
119 #define	sblock		(*sblk.b_un.b_fs)
120 #define	cgrp		(*cgblk.b_un.b_cg)
121 
122 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
123 ino_t cursnapshot;
124 
125 struct inodesc {
126 	enum fixstate id_fix;	/* policy on fixing errors */
127 	int (*id_func)();	/* function to be applied to blocks of inode */
128 	ino_t id_number;	/* inode number described */
129 	ino_t id_parent;	/* for DATA nodes, their parent */
130 	int id_lbn;		/* logical block number of current block */
131 	ufs_daddr_t id_blkno;	/* current block number being examined */
132 	int id_numfrags;	/* number of frags contained in block */
133 	quad_t id_filesize;	/* for DATA nodes, the size of the directory */
134 	int id_loc;		/* for DATA nodes, current location in dir */
135 	int id_entryno;		/* for DATA nodes, current entry number */
136 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
137 	char *id_name;		/* for DATA nodes, name to find or enter */
138 	char id_type;		/* type of descriptor, DATA or ADDR */
139 };
140 /* file types */
141 #define	DATA	1	/* a directory */
142 #define	SNAP	2	/* a snapshot */
143 #define	ADDR	3	/* anything but a directory or a snapshot */
144 
145 /*
146  * Linked list of duplicate blocks.
147  *
148  * The list is composed of two parts. The first part of the
149  * list (from duplist through the node pointed to by muldup)
150  * contains a single copy of each duplicate block that has been
151  * found. The second part of the list (from muldup to the end)
152  * contains duplicate blocks that have been found more than once.
153  * To check if a block has been found as a duplicate it is only
154  * necessary to search from duplist through muldup. To find the
155  * total number of times that a block has been found as a duplicate
156  * the entire list must be searched for occurences of the block
157  * in question. The following diagram shows a sample list where
158  * w (found twice), x (found once), y (found three times), and z
159  * (found once) are duplicate block numbers:
160  *
161  *    w -> y -> x -> z -> y -> w -> y
162  *    ^		     ^
163  *    |		     |
164  * duplist	  muldup
165  */
166 struct dups {
167 	struct dups *next;
168 	ufs_daddr_t dup;
169 };
170 struct dups *duplist;		/* head of dup list */
171 struct dups *muldup;		/* end of unique duplicate dup block numbers */
172 
173 /*
174  * Linked list of inodes with zero link counts.
175  */
176 struct zlncnt {
177 	struct zlncnt *next;
178 	ino_t zlncnt;
179 };
180 struct zlncnt *zlnhead;		/* head of zero link count list */
181 
182 /*
183  * Inode cache data structures.
184  */
185 struct inoinfo {
186 	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
187 	ino_t	i_number;		/* inode number of this entry */
188 	ino_t	i_parent;		/* inode number of parent */
189 	ino_t	i_dotdot;		/* inode number of `..' */
190 	size_t	i_isize;		/* size of inode */
191 	u_int	i_numblks;		/* size of block array in bytes */
192 	ufs_daddr_t i_blks[1];		/* actually longer */
193 } **inphead, **inpsort;
194 long numdirs, dirhash, listmax, inplast;
195 long countdirs;			/* number of directories we actually found */
196 
197 #define MIBSIZE	3		/* size of fsck sysctl MIBs */
198 int	adjrefcnt[MIBSIZE];	/* MIB command to adjust inode reference cnt */
199 int	adjblkcnt[MIBSIZE];	/* MIB command to adjust inode block count */
200 int	freefiles[MIBSIZE];	/* MIB command to free a set of files */
201 int	freedirs[MIBSIZE];	/* MIB command to free a set of directories */
202 int	freeblks[MIBSIZE];	/* MIB command to free a set of data blocks */
203 struct	fsck_cmd cmd;		/* sysctl filesystem update commands */
204 char	snapname[BUFSIZ];	/* when doing snapshots, the name of the file */
205 char	*cdevname;		/* name of device being checked */
206 long	dev_bsize;		/* computed value of DEV_BSIZE */
207 long	secsize;		/* actual disk sector size */
208 char	nflag;			/* assume a no response */
209 char	yflag;			/* assume a yes response */
210 int	bkgrdflag;		/* use a snapshot to run on an active system */
211 int	bflag;			/* location of alternate super block */
212 int	debug;			/* output debugging info */
213 int	cvtlevel;		/* convert to newer filesystem format */
214 int	bkgrdcheck;		/* determine if background check is possible */
215 char	usedsoftdep;		/* just fix soft dependency inconsistencies */
216 char	preen;			/* just fix normal inconsistencies */
217 char	rerun;			/* rerun fsck. Only used in non-preen mode */
218 int	returntosingle;		/* 1 => return to single user mode on exit */
219 char	resolved;		/* cleared if unresolved changes => not clean */
220 char	havesb;			/* superblock has been read */
221 char	skipclean;		/* skip clean filesystems if preening */
222 int	fsmodified;		/* 1 => write done to filesystem */
223 int	fsreadfd;		/* file descriptor for reading filesystem */
224 int	fswritefd;		/* file descriptor for writing filesystem */
225 
226 ufs_daddr_t maxfsblock;		/* number of blocks in the filesystem */
227 char	*blockmap;		/* ptr to primary blk allocation map */
228 ino_t	maxino;			/* number of inodes in filesystem */
229 
230 ino_t	lfdir;			/* lost & found directory inode number */
231 char	*lfname;		/* lost & found directory name */
232 int	lfmode;			/* lost & found directory creation mode */
233 
234 ufs_daddr_t n_blks;		/* number of blocks in use */
235 ufs_daddr_t n_files;		/* number of files in use */
236 
237 int	got_siginfo;		/* received a SIGINFO */
238 
239 #define	clearinode(dp)	(*(dp) = zino)
240 struct	dinode zino;
241 
242 #define	setbmap(blkno)	setbit(blockmap, blkno)
243 #define	testbmap(blkno)	isset(blockmap, blkno)
244 #define	clrbmap(blkno)	clrbit(blockmap, blkno)
245 
246 #define	STOP	0x01
247 #define	SKIP	0x02
248 #define	KEEPON	0x04
249 #define	ALTERED	0x08
250 #define	FOUND	0x10
251 
252 #define	EEXIT	8		/* Standard error exit. */
253 
254 struct fstab;
255 
256 
257 void		adjust(struct inodesc *, int lcnt);
258 ufs_daddr_t	allocblk(long frags);
259 ino_t		allocdir(ino_t parent, ino_t request, int mode);
260 ino_t		allocino(ino_t request, int type);
261 void		blkerror(ino_t ino, char *type, ufs_daddr_t blk);
262 char	       *blockcheck(char *name);
263 int		bread(int fd, char *buf, ufs_daddr_t blk, long size);
264 void		bufinit(void);
265 void		bwrite(int fd, char *buf, ufs_daddr_t blk, long size);
266 void		cacheino(struct dinode *dp, ino_t inumber);
267 void		catch(int);
268 void		catchquit(int);
269 int		changeino(ino_t dir, char *name, ino_t newnum);
270 int		chkrange(ufs_daddr_t blk, int cnt);
271 void		ckfini(int markclean);
272 int		ckinode(struct dinode *dp, struct inodesc *);
273 void		clri(struct inodesc *, char *type, int flag);
274 int		clearentry(struct inodesc *);
275 void		direrror(ino_t ino, char *errmesg);
276 int		dirscan(struct inodesc *);
277 int		dofix(struct inodesc *, char *msg);
278 void		ffs_clrblock(struct fs *, u_char *, ufs_daddr_t);
279 void		ffs_fragacct(struct fs *, int, int32_t [], int);
280 int		ffs_isblock(struct fs *, u_char *, ufs_daddr_t);
281 void		ffs_setblock(struct fs *, u_char *, ufs_daddr_t);
282 void		fileerror(ino_t cwd, ino_t ino, char *errmesg);
283 int		findino(struct inodesc *);
284 int		findname(struct inodesc *);
285 void		flush(int fd, struct bufarea *bp);
286 void		freeblk(ufs_daddr_t blkno, long frags);
287 void		freeino(ino_t ino);
288 void		freeinodebuf(void);
289 int		ftypeok(struct dinode *dp);
290 void		getblk(struct bufarea *bp, ufs_daddr_t blk, long size);
291 struct bufarea *getdatablk(ufs_daddr_t blkno, long size);
292 struct inoinfo *getinoinfo(ino_t inumber);
293 struct dinode  *getnextinode(ino_t inumber);
294 void		getpathname(char *namebuf, ino_t curdir, ino_t ino);
295 struct dinode  *ginode(ino_t inumber);
296 void		infohandler(int sig);
297 void		inocleanup(void);
298 void		inodirty(void);
299 struct inostat *inoinfo(ino_t inum);
300 int		linkup(ino_t orphan, ino_t parentdir, char *name);
301 int		makeentry(ino_t parent, ino_t ino, char *name);
302 void		panic(const char *fmt, ...) __printflike(1, 2);
303 void		pass1(void);
304 void		pass1b(void);
305 int		pass1check(struct inodesc *);
306 void		pass2(void);
307 void		pass3(void);
308 void		pass4(void);
309 int		pass4check(struct inodesc *);
310 void		pass5(void);
311 void		pfatal(const char *fmt, ...) __printflike(1, 2);
312 void		pinode(ino_t ino);
313 void		propagate(void);
314 void		pwarn(const char *fmt, ...) __printflike(1, 2);
315 int		readsb(int listerr);
316 int		reply(char *question);
317 void		rwerror(char *mesg, ufs_daddr_t blk);
318 void		sblock_init(void);
319 void		setinodebuf(ino_t);
320 int		setup(char *dev);
321