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