xref: /freebsd/sbin/fsck_ffs/fsck.h (revision 406475581246360faa7af9891dc8ad2423284e18)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause and BSD-2-Clause-FreeBSD
38a16b7a1SPedro F. Giffuni  *
41c85e6a3SKirk McKusick  * Copyright (c) 2002 Networks Associates Technology, Inc.
51c85e6a3SKirk McKusick  * All rights reserved.
61c85e6a3SKirk McKusick  *
71c85e6a3SKirk McKusick  * This software was developed for the FreeBSD Project by Marshall
81c85e6a3SKirk McKusick  * Kirk McKusick and Network Associates Laboratories, the Security
91c85e6a3SKirk McKusick  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
101c85e6a3SKirk McKusick  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11363c1852SKirk McKusick  * research program.
121c85e6a3SKirk McKusick  *
1360c97629SRobert Watson  * Redistribution and use in source and binary forms, with or without
1460c97629SRobert Watson  * modification, are permitted provided that the following conditions
1560c97629SRobert Watson  * are met:
1660c97629SRobert Watson  * 1. Redistributions of source code must retain the above copyright
1760c97629SRobert Watson  *    notice, this list of conditions and the following disclaimer.
1860c97629SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1960c97629SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
2060c97629SRobert Watson  *    documentation and/or other materials provided with the distribution.
2160c97629SRobert Watson  *
2260c97629SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2360c97629SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2460c97629SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2560c97629SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2660c97629SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2760c97629SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2860c97629SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2960c97629SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3060c97629SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3160c97629SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3260c97629SRobert Watson  * SUCH DAMAGE.
3360c97629SRobert Watson  *
348fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
358fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
368fae3551SRodney W. Grimes  *
378fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
388fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
398fae3551SRodney W. Grimes  * are met:
408fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
418fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
428fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
438fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
448fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
45fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
468fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
478fae3551SRodney W. Grimes  *    without specific prior written permission.
488fae3551SRodney W. Grimes  *
498fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
508fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
518fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
528fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
538fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
548fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
558fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
568fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
578fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
588fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
598fae3551SRodney W. Grimes  * SUCH DAMAGE.
608fae3551SRodney W. Grimes  *
61780a5c1eSPeter Wemm  *	@(#)fsck.h	8.4 (Berkeley) 5/9/95
6269205369SPeter Wemm  * $FreeBSD$
638fae3551SRodney W. Grimes  */
648fae3551SRodney W. Grimes 
65d00690aeSPawel Jakub Dawidek #ifndef _FSCK_H_
66d00690aeSPawel Jakub Dawidek #define	_FSCK_H_
67d00690aeSPawel Jakub Dawidek 
68780a5c1eSPeter Wemm #include <unistd.h>
69780a5c1eSPeter Wemm #include <stdlib.h>
70780a5c1eSPeter Wemm #include <stdio.h>
71780a5c1eSPeter Wemm 
722ec5c914SKirk McKusick #include <sys/queue.h>
732ec5c914SKirk McKusick 
748fae3551SRodney W. Grimes #define	MAXDUP		10	/* limit on dup blks (per inode) */
758fae3551SRodney W. Grimes #define	MAXBAD		10	/* limit on bad blks (per inode) */
765cc52631SKirk McKusick #define	MINBUFS		100	/* minimum number of buffers required */
772ec5c914SKirk McKusick #define	INOBUFSIZE	64*1024	/* size of buffer to read inodes in pass1 */
782b5373deSDag-Erling Smørgrav #define	ZEROBUFSIZE	(dev_bsize * 128) /* size of zero buffer used by -Z */
798fae3551SRodney W. Grimes 
801c85e6a3SKirk McKusick union dinode {
811c85e6a3SKirk McKusick 	struct ufs1_dinode dp1;
821c85e6a3SKirk McKusick 	struct ufs2_dinode dp2;
831c85e6a3SKirk McKusick };
841c85e6a3SKirk McKusick #define	DIP(dp, field) \
851c85e6a3SKirk McKusick 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
861c85e6a3SKirk McKusick 	(dp)->dp1.field : (dp)->dp2.field)
871c85e6a3SKirk McKusick 
88c3b2344bSScott Long #define DIP_SET(dp, field, val) do { \
89c3b2344bSScott Long 	if (sblock.fs_magic == FS_UFS1_MAGIC) \
90c3b2344bSScott Long 		(dp)->dp1.field = (val); \
91c3b2344bSScott Long 	else \
92c3b2344bSScott Long 		(dp)->dp2.field = (val); \
93c3b2344bSScott Long 	} while (0)
94c3b2344bSScott Long 
95d33e92f9SJulian Elischer /*
96d33e92f9SJulian Elischer  * Each inode on the file system is described by the following structure.
97d33e92f9SJulian Elischer  * The linkcnt is initially set to the value in the inode. Each time it
98d33e92f9SJulian Elischer  * is found during the descent in passes 2, 3, and 4 the count is
99d33e92f9SJulian Elischer  * decremented. Any inodes whose count is non-zero after pass 4 needs to
100d33e92f9SJulian Elischer  * have its link count adjusted by the value remaining in ino_linkcnt.
101d33e92f9SJulian Elischer  */
102d33e92f9SJulian Elischer struct inostat {
1035cc52631SKirk McKusick 	u_char	ino_state;	/* state of inode, see below */
1045cc52631SKirk McKusick 	u_char	ino_type:4;	/* type of inode */
1055cc52631SKirk McKusick 	u_char	ino_idtype:4;	/* idesc id_type, SNAP or ADDR */
1065cc52631SKirk McKusick 	u_short	ino_linkcnt;	/* number of links not found */
107d33e92f9SJulian Elischer };
108d33e92f9SJulian Elischer /*
109d33e92f9SJulian Elischer  * Inode states.
110d33e92f9SJulian Elischer  */
111af6726e6SDon Lewis #define	USTATE	0x1		/* inode not allocated */
112af6726e6SDon Lewis #define	FSTATE	0x2		/* inode is file */
113af6726e6SDon Lewis #define	FZLINK	0x3		/* inode is file with a link count of zero */
114af6726e6SDon Lewis #define	DSTATE	0x4		/* inode is directory */
115af6726e6SDon Lewis #define	DZLINK	0x5		/* inode is directory with a zero link count  */
116af6726e6SDon Lewis #define	DFOUND	0x6		/* directory found during descent */
117af6726e6SDon Lewis /*     		0x7		   UNUSED - see S_IS_DVALID() definition */
118af6726e6SDon Lewis #define	DCLEAR	0x8		/* directory is to be cleared */
119af6726e6SDon Lewis #define	FCLEAR	0x9		/* file is to be cleared */
120af6726e6SDon Lewis /*     	DUNFOUND === (state == DSTATE || state == DZLINK) */
121af6726e6SDon Lewis #define	S_IS_DUNFOUND(state)	(((state) & ~0x1) == DSTATE)
122af6726e6SDon Lewis /*     	DVALID   === (state == DSTATE || state == DZLINK || state == DFOUND) */
123af6726e6SDon Lewis #define	S_IS_DVALID(state)	(((state) & ~0x3) == DSTATE)
124af6726e6SDon Lewis #define	INO_IS_DUNFOUND(ino)	S_IS_DUNFOUND(inoinfo(ino)->ino_state)
125af6726e6SDon Lewis #define	INO_IS_DVALID(ino)	S_IS_DVALID(inoinfo(ino)->ino_state)
126d33e92f9SJulian Elischer /*
127d33e92f9SJulian Elischer  * Inode state information is contained on per cylinder group lists
128d33e92f9SJulian Elischer  * which are described by the following structure.
129d33e92f9SJulian Elischer  */
130c3e9752eSKyle Evans extern struct inostatlist {
131d33e92f9SJulian Elischer 	long	il_numalloced;	/* number of inodes allocated in this cg */
132d33e92f9SJulian Elischer 	struct inostat *il_stat;/* inostat info for this cylinder group */
133d33e92f9SJulian Elischer } *inostathead;
1348fae3551SRodney W. Grimes 
1358fae3551SRodney W. Grimes /*
1365cc52631SKirk McKusick  * Structure to reference a dinode.
1375cc52631SKirk McKusick  */
1385cc52631SKirk McKusick struct inode {
1395cc52631SKirk McKusick 	struct bufarea *i_bp;	/* buffer containing the dinode */
1405cc52631SKirk McKusick 	union dinode *i_dp;	/* pointer to dinode in buffer */
1415cc52631SKirk McKusick 	ino_t i_number;		/* inode number */
1425cc52631SKirk McKusick };
1435cc52631SKirk McKusick 
1445cc52631SKirk McKusick /*
1455cc52631SKirk McKusick  * Size of hash tables
1465cc52631SKirk McKusick  */
1475cc52631SKirk McKusick #define	HASHSIZE	2048
1485cc52631SKirk McKusick #define	HASH(x)		((x * 2654435761) & (HASHSIZE - 1))
1495cc52631SKirk McKusick 
1505cc52631SKirk McKusick /*
1518fae3551SRodney W. Grimes  * buffer cache structure.
1528fae3551SRodney W. Grimes  */
1538fae3551SRodney W. Grimes struct bufarea {
1545cc52631SKirk McKusick 	TAILQ_ENTRY(bufarea) b_list;		/* LRU buffer queue */
1555cc52631SKirk McKusick 	LIST_ENTRY(bufarea) b_hash;		/* hash list */
15668dc94c7SKirk McKusick 	ufs2_daddr_t b_bno;			/* disk block number */
15768dc94c7SKirk McKusick 	int b_size;				/* size of I/O */
15868dc94c7SKirk McKusick 	int b_errs;				/* I/O error */
15968dc94c7SKirk McKusick 	int b_flags;				/* B_ flags below */
16068dc94c7SKirk McKusick 	int b_type;				/* BT_ type below */
1615cc52631SKirk McKusick 	int b_refcnt;				/* ref count of users */
1625cc52631SKirk McKusick 	int b_index;				/* for BT_LEVEL, ptr index */
1635cc52631SKirk McKusick 						/* for BT_INODES, first inum */
1648fae3551SRodney W. Grimes 	union {
1658fae3551SRodney W. Grimes 		char *b_buf;			/* buffer space */
1661c85e6a3SKirk McKusick 		ufs1_daddr_t *b_indir1;		/* UFS1 indirect block */
1671c85e6a3SKirk McKusick 		ufs2_daddr_t *b_indir2;		/* UFS2 indirect block */
1688fae3551SRodney W. Grimes 		struct fs *b_fs;		/* super block */
1698fae3551SRodney W. Grimes 		struct cg *b_cg;		/* cylinder group */
1701c85e6a3SKirk McKusick 		struct ufs1_dinode *b_dinode1;	/* UFS1 inode block */
1711c85e6a3SKirk McKusick 		struct ufs2_dinode *b_dinode2;	/* UFS2 inode block */
1728fae3551SRodney W. Grimes 	} b_un;
1738fae3551SRodney W. Grimes };
174c3b2344bSScott Long 
1751c85e6a3SKirk McKusick #define	IBLK(bp, i) \
1761c85e6a3SKirk McKusick 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
1771c85e6a3SKirk McKusick 	(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
1788fae3551SRodney W. Grimes 
179c3b2344bSScott Long #define IBLK_SET(bp, i, val) do { \
180c3b2344bSScott Long 	if (sblock.fs_magic == FS_UFS1_MAGIC) \
181c3b2344bSScott Long 		(bp)->b_un.b_indir1[i] = (val); \
182c3b2344bSScott Long 	else \
183c3b2344bSScott Long 		(bp)->b_un.b_indir2[i] = (val); \
184c3b2344bSScott Long 	} while (0)
185c3b2344bSScott Long 
1862ec5c914SKirk McKusick /*
1872ec5c914SKirk McKusick  * Buffer flags
1882ec5c914SKirk McKusick  */
1895cc52631SKirk McKusick #define	B_DIRTY 	0x00000001	/* Buffer is dirty */
190ed75b5a1SKirk McKusick /*
191ed75b5a1SKirk McKusick  * Type of data in buffer
192ed75b5a1SKirk McKusick  */
19368dc94c7SKirk McKusick #define	BT_UNKNOWN 	 0	/* Buffer type is unknown */
194ed75b5a1SKirk McKusick #define	BT_SUPERBLK 	 1	/* Buffer holds a superblock */
195ed75b5a1SKirk McKusick #define	BT_CYLGRP 	 2	/* Buffer holds a cylinder group map */
196ed75b5a1SKirk McKusick #define	BT_LEVEL1 	 3	/* Buffer holds single level indirect */
197ed75b5a1SKirk McKusick #define	BT_LEVEL2 	 4	/* Buffer holds double level indirect */
198ed75b5a1SKirk McKusick #define	BT_LEVEL3 	 5	/* Buffer holds triple level indirect */
199ed75b5a1SKirk McKusick #define	BT_EXTATTR 	 6	/* Buffer holds external attribute data */
20068dc94c7SKirk McKusick #define	BT_INODES 	 7	/* Buffer holds inodes */
201ed75b5a1SKirk McKusick #define	BT_DIRDATA 	 8	/* Buffer holds directory data */
202ed75b5a1SKirk McKusick #define	BT_DATA	 	 9	/* Buffer holds user data */
203460ed610SKirk McKusick #define BT_NUMBUFTYPES	10
204ed75b5a1SKirk McKusick #define BT_NAMES {			\
205ed75b5a1SKirk McKusick 	"unknown",			\
206ed75b5a1SKirk McKusick 	"Superblock",			\
207ed75b5a1SKirk McKusick 	"Cylinder Group",		\
208ed75b5a1SKirk McKusick 	"Single Level Indirect",	\
209ed75b5a1SKirk McKusick 	"Double Level Indirect",	\
210ed75b5a1SKirk McKusick 	"Triple Level Indirect",	\
211ed75b5a1SKirk McKusick 	"External Attribute",		\
212ed75b5a1SKirk McKusick 	"Inode Block",			\
213ed75b5a1SKirk McKusick 	"Directory Contents",		\
214460ed610SKirk McKusick 	"User Data" }
2155cc52631SKirk McKusick extern char *buftype[];
2165cc52631SKirk McKusick #define BT_BUFTYPE(type) \
2175cc52631SKirk McKusick 	type < BT_NUMBUFTYPES ? buftype[type] : buftype[BT_UNKNOWN]
2187703a6ffSScott Long extern long readcnt[BT_NUMBUFTYPES];
2197703a6ffSScott Long extern long totalreadcnt[BT_NUMBUFTYPES];
2207703a6ffSScott Long extern struct timespec readtime[BT_NUMBUFTYPES];
2217703a6ffSScott Long extern struct timespec totalreadtime[BT_NUMBUFTYPES];
2227703a6ffSScott Long extern struct timespec startprog;
2238fae3551SRodney W. Grimes 
2245cc52631SKirk McKusick extern struct bufarea *icachebp;	/* inode cache buffer */
2257703a6ffSScott Long extern struct bufarea sblk;		/* file system superblock */
2267703a6ffSScott Long extern struct bufarea *pdirbp;		/* current directory contents */
2278fae3551SRodney W. Grimes 
228bf58d635SIan Dowse #define	dirty(bp) do { \
2297578c6abSKirk McKusick 	if (fswritefd < 0) \
2307578c6abSKirk McKusick 		pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \
2317578c6abSKirk McKusick 	else \
2325cc52631SKirk McKusick 		(bp)->b_flags |= B_DIRTY; \
233bf58d635SIan Dowse } while (0)
234ed75b5a1SKirk McKusick #define	initbarea(bp, type) do { \
235460ed610SKirk McKusick 	(bp)->b_bno = (ufs2_daddr_t)-4; \
236fc56fd26SKirk McKusick 	(bp)->b_size = 0; \
237fc56fd26SKirk McKusick 	(bp)->b_errs = 0; \
238bf58d635SIan Dowse 	(bp)->b_flags = 0; \
239fc56fd26SKirk McKusick 	(bp)->b_type = type; \
2405cc52631SKirk McKusick 	(bp)->b_refcnt = 0; \
2415cc52631SKirk McKusick 	(bp)->b_index = 0; \
242bf58d635SIan Dowse } while (0)
2438fae3551SRodney W. Grimes 
2447578c6abSKirk McKusick #define	sbdirty()	dirty(&sblk)
2458fae3551SRodney W. Grimes #define	sblock		(*sblk.b_un.b_fs)
2468fae3551SRodney W. Grimes 
2478fae3551SRodney W. Grimes enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
2487703a6ffSScott Long extern ino_t cursnapshot;
2498fae3551SRodney W. Grimes 
2508fae3551SRodney W. Grimes struct inodesc {
2518fae3551SRodney W. Grimes 	enum fixstate id_fix;	/* policy on fixing errors */
252599304a4SPoul-Henning Kamp 	int (*id_func)(struct inodesc *);
253599304a4SPoul-Henning Kamp 				/* function to be applied to blocks of inode */
2545cc52631SKirk McKusick 	struct bufarea *id_bp;	/* ckinode: buffer with indirect pointers */
2555cc52631SKirk McKusick 	union dinode *id_dp;	/* ckinode: dinode being traversed */
2568fae3551SRodney W. Grimes 	ino_t id_number;	/* inode number described */
2578fae3551SRodney W. Grimes 	ino_t id_parent;	/* for DATA nodes, their parent */
2581c85e6a3SKirk McKusick 	ufs_lbn_t id_lbn;	/* logical block number of current block */
2591c85e6a3SKirk McKusick 	ufs2_daddr_t id_blkno;	/* current block number being examined */
260d4833913SKirk McKusick 	int id_level;		/* level of indirection of this block */
2618fae3551SRodney W. Grimes 	int id_numfrags;	/* number of frags contained in block */
262ac4b20a0SKirk McKusick 	ufs_lbn_t id_lballoc;	/* pass1: last LBN that is allocated */
2631c85e6a3SKirk McKusick 	off_t id_filesize;	/* for DATA nodes, the size of the directory */
2641c85e6a3SKirk McKusick 	ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */
2658fae3551SRodney W. Grimes 	int id_loc;		/* for DATA nodes, current location in dir */
2668fae3551SRodney W. Grimes 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
2678fae3551SRodney W. Grimes 	char *id_name;		/* for DATA nodes, name to find or enter */
2685cc52631SKirk McKusick 	char id_type;		/* type of descriptor, DATA, ADDR, or SNAP */
2698fae3551SRodney W. Grimes };
2708fae3551SRodney W. Grimes /* file types */
271142d8d2fSKirk McKusick #define	DATA	1	/* a directory */
272142d8d2fSKirk McKusick #define	SNAP	2	/* a snapshot */
273142d8d2fSKirk McKusick #define	ADDR	3	/* anything but a directory or a snapshot */
2748fae3551SRodney W. Grimes 
2758fae3551SRodney W. Grimes /*
2768fae3551SRodney W. Grimes  * Linked list of duplicate blocks.
2778fae3551SRodney W. Grimes  *
2788fae3551SRodney W. Grimes  * The list is composed of two parts. The first part of the
2798fae3551SRodney W. Grimes  * list (from duplist through the node pointed to by muldup)
2808fae3551SRodney W. Grimes  * contains a single copy of each duplicate block that has been
2818fae3551SRodney W. Grimes  * found. The second part of the list (from muldup to the end)
2828fae3551SRodney W. Grimes  * contains duplicate blocks that have been found more than once.
2838fae3551SRodney W. Grimes  * To check if a block has been found as a duplicate it is only
2848fae3551SRodney W. Grimes  * necessary to search from duplist through muldup. To find the
2858fae3551SRodney W. Grimes  * total number of times that a block has been found as a duplicate
2864b85a12fSUlrich Spörlein  * the entire list must be searched for occurrences of the block
2878fae3551SRodney W. Grimes  * in question. The following diagram shows a sample list where
2888fae3551SRodney W. Grimes  * w (found twice), x (found once), y (found three times), and z
2898fae3551SRodney W. Grimes  * (found once) are duplicate block numbers:
2908fae3551SRodney W. Grimes  *
2918fae3551SRodney W. Grimes  *    w -> y -> x -> z -> y -> w -> y
2928fae3551SRodney W. Grimes  *    ^		     ^
2938fae3551SRodney W. Grimes  *    |		     |
2948fae3551SRodney W. Grimes  * duplist	  muldup
2958fae3551SRodney W. Grimes  */
2968fae3551SRodney W. Grimes struct dups {
2978fae3551SRodney W. Grimes 	struct dups *next;
2981c85e6a3SKirk McKusick 	ufs2_daddr_t dup;
2998fae3551SRodney W. Grimes };
300c3e9752eSKyle Evans extern struct dups *duplist;		/* head of dup list */
301c3e9752eSKyle Evans extern struct dups *muldup;		/* end of unique duplicate dup block numbers */
3028fae3551SRodney W. Grimes 
3038fae3551SRodney W. Grimes /*
3048fae3551SRodney W. Grimes  * Inode cache data structures.
3058fae3551SRodney W. Grimes  */
30652f97104SKirk McKusick struct inoinfo {
30752f97104SKirk McKusick 	SLIST_ENTRY(inoinfo) i_hash;	/* hash list */
3088fae3551SRodney W. Grimes 	ino_t	i_number;		/* inode number of this entry */
3098fae3551SRodney W. Grimes 	ino_t	i_parent;		/* inode number of parent */
3108fae3551SRodney W. Grimes 	ino_t	i_dotdot;		/* inode number of `..' */
3118fae3551SRodney W. Grimes 	size_t	i_isize;		/* size of inode */
312fe5e6e2cSKirk McKusick 	u_int	i_depth;		/* depth of directory from root */
313f4fc3895SKirk McKusick 	u_int	i_flags;		/* flags, see below */
3148fae3551SRodney W. Grimes 	u_int	i_numblks;		/* size of block array in bytes */
3151c85e6a3SKirk McKusick 	ufs2_daddr_t i_blks[1];		/* actually longer */
31652f97104SKirk McKusick };
31752f97104SKirk McKusick extern SLIST_HEAD(inohash, inoinfo) *inphash;
31852f97104SKirk McKusick extern struct inoinfo **inpsort;
319f4fc3895SKirk McKusick /*
320f4fc3895SKirk McKusick  * flags for struct inoinfo
321f4fc3895SKirk McKusick  */
322f4fc3895SKirk McKusick #define INFO_NEW	0x0000001	/* replaced broken directory */
323f4fc3895SKirk McKusick 
324f6717697SPedro F. Giffuni extern long dirhash, inplast;
325f6717697SPedro F. Giffuni extern unsigned long numdirs, listmax;
3267703a6ffSScott Long extern long countdirs;		/* number of directories we actually found */
3278fae3551SRodney W. Grimes 
3287578c6abSKirk McKusick #define MIBSIZE	3		/* size of fsck sysctl MIBs */
329bf46c0a9SKirk McKusick extern int adjblkcnt[MIBSIZE];	/* MIB cmd to adjust inode block count */
330bf46c0a9SKirk McKusick extern int adjrefcnt[MIBSIZE];	/* MIB cmd to adjust inode reference count */
331bf46c0a9SKirk McKusick extern int adjndir[MIBSIZE];	/* MIB cmd to adjust number of directories */
332bf46c0a9SKirk McKusick extern int adjnbfree[MIBSIZE];	/* MIB cmd to adjust number of free blocks */
333bf46c0a9SKirk McKusick extern int adjnifree[MIBSIZE];	/* MIB cmd to adjust number of free inodes */
334bf46c0a9SKirk McKusick extern int adjnffree[MIBSIZE];	/* MIB cmd to adjust number of free frags */
335bf46c0a9SKirk McKusick extern int adjnumclusters[MIBSIZE]; /* MIB cmd adjust number of free clusters */
336bf46c0a9SKirk McKusick extern int freefiles[MIBSIZE];	/* MIB cmd to free a set of files */
337bf46c0a9SKirk McKusick extern int freedirs[MIBSIZE];	/* MIB cmd to free a set of directories */
338bf46c0a9SKirk McKusick extern int freeblks[MIBSIZE];	/* MIB cmd to free a set of data blocks */
339bf46c0a9SKirk McKusick extern int setsize[MIBSIZE];	/* MIB cmd to set inode size */
3407703a6ffSScott Long extern struct fsck_cmd cmd;	/* sysctl file system update commands */
341bf46c0a9SKirk McKusick 
342bf46c0a9SKirk McKusick extern int bkgrdcheck;		/* determine if background check is possible */
343bf46c0a9SKirk McKusick extern int bkgrdsumadj;		/* whether the kernel has the ability to adjust
344bf46c0a9SKirk McKusick 				   the superblock summary fields */
345dffce215SKirk McKusick extern off_t bflag;		/* location of alternate super block */
346bf46c0a9SKirk McKusick extern int bkgrdflag;		/* use a snapshot to run on an active system */
347bf46c0a9SKirk McKusick extern char *blockmap;		/* ptr to primary blk allocation map */
348bf46c0a9SKirk McKusick extern char *cdevname;		/* name of device being checked */
349da86e7a2SKirk McKusick extern int cgheader_corrupt;	/* one or more CG headers are corrupt */
350bf46c0a9SKirk McKusick extern char ckclean;		/* only do work if not cleanly unmounted */
351bf46c0a9SKirk McKusick extern int ckhashadd;		/* check hashes to be added */
352460ed610SKirk McKusick extern char *copybuf;		/* buffer to copy snapshot blocks */
353bf46c0a9SKirk McKusick extern int cvtlevel;		/* convert to newer file system format */
354bf46c0a9SKirk McKusick extern long dev_bsize;		/* computed value of DEV_BSIZE */
355bf46c0a9SKirk McKusick extern u_int real_dev_bsize;	/* actual disk sector size, not overridden */
3567703a6ffSScott Long extern int debug;		/* output debugging info */
3577703a6ffSScott Long extern int Eflag;		/* delete empty data blocks */
3587703a6ffSScott Long extern int fsmodified;		/* 1 => write done to file system */
3597703a6ffSScott Long extern int fsreadfd;		/* file descriptor for reading file system */
3607703a6ffSScott Long extern int fswritefd;		/* file descriptor for writing file system */
361bf46c0a9SKirk McKusick extern char havesb;		/* superblock has been read */
362bf46c0a9SKirk McKusick extern int inoopt;		/* trim out unused inodes */
3637703a6ffSScott Long extern ino_t lfdir;		/* lost & found directory inode number */
3647703a6ffSScott Long extern int lfmode;		/* lost & found directory creation mode */
365bf46c0a9SKirk McKusick extern const char *lfname; 	/* lost & found directory name */
366bf46c0a9SKirk McKusick extern ufs2_daddr_t maxfsblock; /* number of blocks in the file system */
367bf46c0a9SKirk McKusick extern ino_t maxino;		/* number of inodes in file system */
3687703a6ffSScott Long extern ufs2_daddr_t n_blks;	/* number of blocks in use */
3697703a6ffSScott Long extern ino_t n_files;		/* number of files in use */
370bf46c0a9SKirk McKusick extern char nflag;		/* assume a no response */
371bf46c0a9SKirk McKusick extern char preen;		/* just fix normal inconsistencies */
372bf46c0a9SKirk McKusick extern char rerun;		/* rerun fsck. Only used in non-preen mode */
373bf46c0a9SKirk McKusick extern char resolved;		/* cleared if unresolved changes => not clean */
374bf46c0a9SKirk McKusick extern int returntosingle;	/* 1 => return to single user mode on exit */
375bf46c0a9SKirk McKusick extern long secsize;		/* actual disk sector size */
376bf46c0a9SKirk McKusick extern char skipclean;		/* skip clean file systems if preening */
377460ed610SKirk McKusick extern int snapcnt;		/* number of active snapshots */
378460ed610SKirk McKusick extern struct inode snaplist[FSMAXSNAP + 1]; /* list of active snapshots */
379bf46c0a9SKirk McKusick extern char snapname[BUFSIZ];	/* when doing snapshots, the name of the file */
380bf46c0a9SKirk McKusick extern int sujrecovery;		/* 1 => doing check using the journal */
381bf46c0a9SKirk McKusick extern int surrender;		/* Give up if reads fail */
382bf46c0a9SKirk McKusick extern char usedsoftdep;	/* just fix soft dependency inconsistencies */
383bf46c0a9SKirk McKusick extern int wantrestart;		/* Restart fsck on early termination */
384bf46c0a9SKirk McKusick extern char yflag;		/* assume a yes response */
385bf46c0a9SKirk McKusick extern int zflag;		/* zero unused directory space */
386bf46c0a9SKirk McKusick extern int Zflag;		/* zero empty data blocks */
3878fae3551SRodney W. Grimes 
3887703a6ffSScott Long extern volatile sig_atomic_t	got_siginfo;	/* received a SIGINFO */
3897703a6ffSScott Long extern volatile sig_atomic_t	got_sigalarm;	/* received a SIGALRM */
3906db798caSIan Dowse 
3911c85e6a3SKirk McKusick #define	clearinode(dp) \
3921c85e6a3SKirk McKusick 	if (sblock.fs_magic == FS_UFS1_MAGIC) { \
3935cc52631SKirk McKusick 		(dp)->dp1 = zino.dp1; \
3941c85e6a3SKirk McKusick 	} else { \
3955cc52631SKirk McKusick 		(dp)->dp2 = zino.dp2; \
3961c85e6a3SKirk McKusick 	}
3975cc52631SKirk McKusick extern union dinode zino;
3988fae3551SRodney W. Grimes 
3998fae3551SRodney W. Grimes #define	setbmap(blkno)	setbit(blockmap, blkno)
4008fae3551SRodney W. Grimes #define	testbmap(blkno)	isset(blockmap, blkno)
4018fae3551SRodney W. Grimes #define	clrbmap(blkno)	clrbit(blockmap, blkno)
4028fae3551SRodney W. Grimes 
4038fae3551SRodney W. Grimes #define	STOP	0x01
4048fae3551SRodney W. Grimes #define	SKIP	0x02
4058fae3551SRodney W. Grimes #define	KEEPON	0x04
4068fae3551SRodney W. Grimes #define	ALTERED	0x08
4078fae3551SRodney W. Grimes #define	FOUND	0x10
4088fae3551SRodney W. Grimes 
409780a5c1eSPeter Wemm #define	EEXIT	8		/* Standard error exit. */
410469759f8SDavid Bright #define	ERERUN	16		/* fsck needs to be re-run. */
4117703a6ffSScott Long #define	ERESTART -1
412780a5c1eSPeter Wemm 
41381fbded2SKirk McKusick int flushentry(void);
41481fbded2SKirk McKusick /*
41581fbded2SKirk McKusick  * Wrapper for malloc() that flushes the cylinder group cache to try
41681fbded2SKirk McKusick  * to get space.
41781fbded2SKirk McKusick  */
41881fbded2SKirk McKusick static inline void*
419eff68496SKirk McKusick Malloc(size_t size)
42081fbded2SKirk McKusick {
42181fbded2SKirk McKusick 	void *retval;
42281fbded2SKirk McKusick 
42381fbded2SKirk McKusick 	while ((retval = malloc(size)) == NULL)
42481fbded2SKirk McKusick 		if (flushentry() == 0)
42581fbded2SKirk McKusick 			break;
42681fbded2SKirk McKusick 	return (retval);
42781fbded2SKirk McKusick }
42881fbded2SKirk McKusick 
42981fbded2SKirk McKusick /*
43081fbded2SKirk McKusick  * Wrapper for calloc() that flushes the cylinder group cache to try
43181fbded2SKirk McKusick  * to get space.
43281fbded2SKirk McKusick  */
43381fbded2SKirk McKusick static inline void*
434eff68496SKirk McKusick Calloc(size_t cnt, size_t size)
43581fbded2SKirk McKusick {
43681fbded2SKirk McKusick 	void *retval;
43781fbded2SKirk McKusick 
43881fbded2SKirk McKusick 	while ((retval = calloc(cnt, size)) == NULL)
43981fbded2SKirk McKusick 		if (flushentry() == 0)
44081fbded2SKirk McKusick 			break;
44181fbded2SKirk McKusick 	return (retval);
44281fbded2SKirk McKusick }
44381fbded2SKirk McKusick 
444780a5c1eSPeter Wemm struct fstab;
445780a5c1eSPeter Wemm 
446d33e92f9SJulian Elischer 
447b70cd7eeSWarner Losh void		adjust(struct inodesc *, int lcnt);
4485cc52631SKirk McKusick void		alarmhandler(int sig);
449460ed610SKirk McKusick ufs2_daddr_t	allocblk(long cg, long frags, ufs2_daddr_t (*checkblkavail)
450460ed610SKirk McKusick 		    (ufs2_daddr_t blkno, long frags));
451b70cd7eeSWarner Losh ino_t		allocdir(ino_t parent, ino_t request, int mode);
452b70cd7eeSWarner Losh ino_t		allocino(ino_t request, int type);
453460ed610SKirk McKusick void		binval(struct bufarea *);
454599304a4SPoul-Henning Kamp void		blkerror(ino_t ino, const char *type, ufs2_daddr_t blk);
455b70cd7eeSWarner Losh char	       *blockcheck(char *name);
456aef8d244SPawel Jakub Dawidek int		blread(int fd, char *buf, ufs2_daddr_t blk, long size);
457b70cd7eeSWarner Losh void		bufinit(void);
4584a835375SDavid E. O'Brien void		blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size);
4598d3dfc26SDag-Erling Smørgrav void		blerase(int fd, ufs2_daddr_t blk, long size);
4602b5373deSDag-Erling Smørgrav void		blzero(int fd, ufs2_daddr_t blk, long size);
4615cc52631SKirk McKusick void		brelse(struct bufarea *);
462f4fc3895SKirk McKusick struct inoinfo *cacheino(union dinode *dp, ino_t inumber);
463b70cd7eeSWarner Losh void		catch(int);
464b70cd7eeSWarner Losh void		catchquit(int);
4658ebae128SKirk McKusick void		cgdirty(struct bufarea *);
4665cc52631SKirk McKusick struct bufarea *cglookup(int cg);
467fe5e6e2cSKirk McKusick int		changeino(ino_t dir, const char *name, ino_t newnum, int depth);
468460ed610SKirk McKusick void		check_blkcnt(struct inode *ip);
469*40647558SChuck Silvers int		check_cgmagic(int cg, struct bufarea *cgbp);
470*40647558SChuck Silvers void		rebuild_cg(int cg, struct bufarea *cgbp);
471fe5e6e2cSKirk McKusick void		check_dirdepth(struct inoinfo *inp);
4721c85e6a3SKirk McKusick int		chkrange(ufs2_daddr_t blk, int cnt);
473b70cd7eeSWarner Losh void		ckfini(int markclean);
4741c85e6a3SKirk McKusick int		ckinode(union dinode *dp, struct inodesc *);
475599304a4SPoul-Henning Kamp void		clri(struct inodesc *, const char *type, int flag);
476b70cd7eeSWarner Losh int		clearentry(struct inodesc *);
477460ed610SKirk McKusick void		copyonwrite(struct fs *, struct bufarea *,
478689a9368SKirk McKusick 		    ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long));
479599304a4SPoul-Henning Kamp void		direrror(ino_t ino, const char *errmesg);
480b70cd7eeSWarner Losh int		dirscan(struct inodesc *);
481599304a4SPoul-Henning Kamp int		dofix(struct inodesc *, const char *msg);
48251869213SPoul-Henning Kamp int		eascan(struct inodesc *, struct ufs2_dinode *dp);
483599304a4SPoul-Henning Kamp void		fileerror(ino_t cwd, ino_t ino, const char *errmesg);
484ed75b5a1SKirk McKusick void		finalIOstats(void);
485b70cd7eeSWarner Losh int		findino(struct inodesc *);
486b70cd7eeSWarner Losh int		findname(struct inodesc *);
487b70cd7eeSWarner Losh void		flush(int fd, struct bufarea *bp);
4887180f1abSKirk McKusick int		freeblock(struct inodesc *);
48952f97104SKirk McKusick void		freedirino(ino_t ino, ino_t parent);
490b70cd7eeSWarner Losh void		freeino(ino_t ino);
491b70cd7eeSWarner Losh void		freeinodebuf(void);
492460ed610SKirk McKusick void		fsckinit(void);
4937703a6ffSScott Long void		fsutilinit(void);
4941c85e6a3SKirk McKusick int		ftypeok(union dinode *dp);
4951c85e6a3SKirk McKusick void		getblk(struct bufarea *bp, ufs2_daddr_t blk, long size);
496ed75b5a1SKirk McKusick struct bufarea *getdatablk(ufs2_daddr_t blkno, long size, int type);
497b70cd7eeSWarner Losh struct inoinfo *getinoinfo(ino_t inumber);
498da86e7a2SKirk McKusick union dinode   *getnextinode(ino_t inumber, int rebuiltcg);
499b70cd7eeSWarner Losh void		getpathname(char *namebuf, ino_t curdir, ino_t ino);
5005cc52631SKirk McKusick void		ginode(ino_t, struct inode *);
501460ed610SKirk McKusick void		gjournal_check(const char *filesys);
502b70cd7eeSWarner Losh void		infohandler(int sig);
5035cc52631SKirk McKusick void		irelse(struct inode *);
5045cc52631SKirk McKusick ufs2_daddr_t	ino_blkatoff(union dinode *, ino_t, ufs_lbn_t, int *,
5055cc52631SKirk McKusick 		    struct bufarea **);
506b70cd7eeSWarner Losh void		inocleanup(void);
5075cc52631SKirk McKusick void		inodirty(struct inode *);
508b70cd7eeSWarner Losh struct inostat *inoinfo(ino_t inum);
509ed75b5a1SKirk McKusick void		IOstats(char *what);
510b70cd7eeSWarner Losh int		linkup(ino_t orphan, ino_t parentdir, char *name);
511599304a4SPoul-Henning Kamp int		makeentry(ino_t parent, ino_t ino, const char *name);
512c5d476c9SKirk McKusick int		openfilesys(char *dev);
513b70cd7eeSWarner Losh void		panic(const char *fmt, ...) __printflike(1, 2);
514b70cd7eeSWarner Losh void		pass1(void);
515b70cd7eeSWarner Losh void		pass1b(void);
516b70cd7eeSWarner Losh int		pass1check(struct inodesc *);
517b70cd7eeSWarner Losh void		pass2(void);
518b70cd7eeSWarner Losh void		pass3(void);
519b70cd7eeSWarner Losh void		pass4(void);
520b70cd7eeSWarner Losh void		pass5(void);
521b70cd7eeSWarner Losh void		pfatal(const char *fmt, ...) __printflike(1, 2);
522b70cd7eeSWarner Losh void		propagate(void);
523460ed610SKirk McKusick void		prtbuf(struct bufarea *, const char *, ...) __printflike(2, 3);
5245cc52631SKirk McKusick void		prtinode(struct inode *);
525b70cd7eeSWarner Losh void		pwarn(const char *fmt, ...) __printflike(1, 2);
526e6886616SKirk McKusick int		readsb(void);
52752f97104SKirk McKusick int		removecachedino(ino_t);
528599304a4SPoul-Henning Kamp int		reply(const char *question);
529599304a4SPoul-Henning Kamp void		rwerror(const char *mesg, ufs2_daddr_t blk);
530b70cd7eeSWarner Losh void		sblock_init(void);
5315cc52631SKirk McKusick void		setinodebuf(int, ino_t);
532b70cd7eeSWarner Losh int		setup(char *dev);
533460ed610SKirk McKusick int		snapblkfree(struct fs *, ufs2_daddr_t, long, ino_t,
534460ed610SKirk McKusick 		    ufs2_daddr_t (*)(ufs2_daddr_t, long));
535460ed610SKirk McKusick void		snapremove(ino_t);
536689a9368SKirk McKusick void		snapflush(ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long));
537460ed610SKirk McKusick ufs2_daddr_t	std_checkblkavail(ufs2_daddr_t blkno, long frags);
538460ed610SKirk McKusick ufs2_daddr_t	suj_checkblkavail(ufs2_daddr_t, long);
539113db2ddSJeff Roberson int		suj_check(const char *filesys);
540d2404f04SKirk McKusick void		update_maps(struct cg *, struct cg*, int);
541d00690aeSPawel Jakub Dawidek 
542d00690aeSPawel Jakub Dawidek #endif	/* !_FSCK_H_ */
543