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