fsck.h revision 1.28
1/*	$OpenBSD: fsck.h,v 1.28 2013/11/02 00:08:17 krw Exp $	*/
2/*	$NetBSD: fsck.h,v 1.13 1996/10/11 20:15:46 thorpej Exp $	*/
3
4/*
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by Marshall
9 * Kirk McKusick and Network Associates Laboratories, the Security
10 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
11 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
12 * research program.
13 *
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. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *	@(#)fsck.h	8.1 (Berkeley) 6/5/93
42 */
43
44#define	MAXDUP		10	/* limit on dup blks (per inode) */
45#define	MAXBAD		10	/* limit on bad blks (per inode) */
46#define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
47#define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
48
49union dinode {
50	struct ufs1_dinode dp1;
51	struct ufs2_dinode dp2;
52};
53
54#define	DIP(dp, field)				\
55	((sblock.fs_magic == FS_UFS1_MAGIC) ?	\
56	(dp)->dp1.field : (dp)->dp2.field)
57
58#define	DIP_SET(dp, field, val) do { \
59	if (sblock.fs_magic == FS_UFS1_MAGIC)	\
60		(dp)->dp1.field = (val);	\
61	else					\
62		(dp)->dp2.field = (val);	\
63	} while (0)
64
65#ifndef BUFSIZ
66#define BUFSIZ 1024
67#endif
68
69/*
70 * Each inode on the file system is described by the following structure.
71 * The linkcnt is initially set to the value in the inode. Each time it
72 * is found during the descent in passes 2, 3, and 4 the count is
73 * decremented. Any inodes whose count is non-zero after pass 4 needs to
74 * have its link count adjusted by the value remaining in ino_linkcnt.
75 */
76struct inostat {
77	char    ino_state;      /* state of inode, see below */
78	char    ino_type;       /* type of inode */
79	short   ino_linkcnt;    /* number of links not found */
80};
81
82#define	USTATE	01		/* inode not allocated */
83#define	FSTATE	02		/* inode is file */
84#define	DSTATE	03		/* inode is directory */
85#define	DFOUND	04		/* directory found during descent */
86#define	DCLEAR	05		/* directory is to be cleared */
87#define	FCLEAR	06		/* file is to be cleared */
88
89/*
90 * Inode state information is contained on per cylinder group lists
91 * which are described by the following structure.
92 */
93struct inostatlist {
94	long    il_numalloced;  /* number of inodes allocated in this cg */
95	struct inostat *il_stat;/* inostat info for this cylinder group */
96} *inostathead;
97
98#define GET_ISTATE(ino)		(inoinfo(ino)->ino_state)
99#define GET_ITYPE(ino)		(inoinfo(ino)->ino_type)
100#define SET_ISTATE(ino, v)	do { GET_ISTATE(ino) = (v); } while (0)
101#define SET_ITYPE(ino, v)	do { GET_ITYPE(ino) = (v); } while (0)
102#define ILNCOUNT(ino)		(inoinfo(ino)->ino_linkcnt)
103
104/*
105 * buffer cache structure.
106 */
107struct bufarea {
108	daddr_t	b_bno;
109	struct bufarea	*b_next;		/* free list queue */
110	struct bufarea	*b_prev;		/* free list queue */
111	int	b_size;
112	int	b_errs;
113	int	b_flags;
114	union {
115		char	*b_buf;			/* buffer space */
116		int32_t	*b_indir1;		/* FFS1 indirect block */
117		int64_t	*b_indir2;		/* FFS2 indirect block */
118		struct	fs *b_fs;		/* super block */
119		struct	cg *b_cg;		/* cylinder group */
120		struct	ufs1_dinode *b_dinode1;	/* FFS1 inode block */
121		struct	ufs2_dinode *b_dinode2;	/* FFS2 inode block */
122	} b_un;
123	char	b_dirty;
124};
125
126#define IBLK(bp, i)				\
127	((sblock.fs_magic == FS_UFS1_MAGIC) ?	\
128	(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
129
130#define IBLK_SET(bp, i, val) do {		\
131	if (sblock.fs_magic == FS_UFS1_MAGIC)	\
132		(bp)->b_un.b_indir1[i] = (val);	\
133	else					\
134		(bp)->b_un.b_indir2[i] = (val);	\
135	} while (0)
136
137#define	B_INUSE 1
138
139#define	MINBUFS		5	/* minimum number of buffers required */
140struct bufarea bufhead;		/* head of list of other blks in filesys */
141struct bufarea sblk;		/* file system superblock */
142struct bufarea asblk;		/* alternate file system superblock */
143struct bufarea cgblk;		/* cylinder group blocks */
144struct bufarea *pdirbp;		/* current directory contents */
145struct bufarea *pbp;		/* current inode block */
146struct bufarea *getdatablk(daddr_t, long);
147
148#define	dirty(bp)	(bp)->b_dirty = 1
149#define	initbarea(bp) \
150	(bp)->b_dirty = 0; \
151	(bp)->b_bno = -1; \
152	(bp)->b_flags = 0;
153
154#define	sbdirty()	sblk.b_dirty = 1
155#define	cgdirty()	cgblk.b_dirty = 1
156#define	sblock		(*sblk.b_un.b_fs)
157#define	cgrp		(*cgblk.b_un.b_cg)
158
159enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
160
161struct inodesc {
162	daddr_t id_blkno;	/* current block number being examined */
163	quad_t id_filesize;	/* for DATA nodes, the size of the directory */
164	int (*id_func)		/* function to be applied to blocks of inode */
165(struct inodesc *);
166	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
167	char *id_name;		/* for DATA nodes, name to find or enter */
168	ino_t id_number;	/* inode number described */
169	ino_t id_parent;	/* for DATA nodes, their parent */
170	enum fixstate id_fix;	/* policy on fixing errors */
171	int id_numfrags;	/* number of frags contained in block */
172	int id_loc;		/* for DATA nodes, current location in dir */
173	int id_entryno;		/* for DATA nodes, current entry number */
174	char id_type;		/* type of descriptor, DATA or ADDR */
175};
176/* file types */
177#define	DATA	1
178#define	ADDR	2
179
180/*
181 * Linked list of duplicate blocks.
182 *
183 * The list is composed of two parts. The first part of the
184 * list (from duplist through the node pointed to by muldup)
185 * contains a single copy of each duplicate block that has been
186 * found. The second part of the list (from muldup to the end)
187 * contains duplicate blocks that have been found more than once.
188 * To check if a block has been found as a duplicate it is only
189 * necessary to search from duplist through muldup. To find the
190 * total number of times that a block has been found as a duplicate
191 * the entire list must be searched for occurrences of the block
192 * in question. The following diagram shows a sample list where
193 * w (found twice), x (found once), y (found three times), and z
194 * (found once) are duplicate block numbers:
195 *
196 *    w -> y -> x -> z -> y -> w -> y
197 *    ^		     ^
198 *    |		     |
199 * duplist	  muldup
200 */
201struct dups {
202	struct dups *next;
203	daddr_t dup;
204};
205struct dups *duplist;		/* head of dup list */
206struct dups *muldup;		/* end of unique duplicate dup block numbers */
207
208/*
209 * Linked list of inodes with zero link counts.
210 */
211struct zlncnt {
212	struct zlncnt *next;
213	ino_t zlncnt;
214};
215struct zlncnt *zlnhead;		/* head of zero link count list */
216
217/*
218 * Inode cache data structures.
219 */
220struct inoinfo {
221	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
222	struct	inoinfo	*i_child, *i_sibling;
223	size_t	i_isize;		/* size of inode */
224	ino_t	i_number;		/* inode number of this entry */
225	ino_t	i_parent;		/* inode number of parent */
226	ino_t	i_dotdot;		/* inode number of `..' */
227	u_int	i_numblks;		/* size of block array in bytes */
228	daddr_t	i_blks[1];		/* actually longer */
229} **inphead, **inpsort;
230
231extern long numdirs, listmax, inplast;
232
233long	dev_bsize;		/* computed value of DEV_BSIZE */
234long	secsize;		/* actual disk sector size */
235char	nflag;			/* assume a no response */
236char	yflag;			/* assume a yes response */
237int	bflag;			/* location of alternate super block */
238int	debug;			/* output debugging info */
239int	cvtlevel;		/* convert to newer file system format */
240char    usedsoftdep;            /* just fix soft dependency inconsistencies */
241int	preen;			/* just fix normal inconsistencies */
242char    resolved;               /* cleared if unresolved changes => not clean */
243char	havesb;			/* superblock has been read */
244char	skipclean;		/* skip clean file systems if preening */
245int	fsmodified;		/* 1 => write done to file system */
246int	fsreadfd;		/* file descriptor for reading file system */
247int	fswritefd;		/* file descriptor for writing file system */
248int	rerun;			/* rerun fsck.  Only used in non-preen mode */
249
250daddr_t	maxfsblock;		/* number of blocks in the file system */
251char	*blockmap;		/* ptr to primary blk allocation map */
252ino_t	maxino;			/* number of inodes in file system */
253ino_t	lastino;		/* last inode in use */
254
255ino_t	lfdir;			/* lost & found directory inode number */
256char	*lfname;		/* lost & found directory name */
257int	lfmode;			/* lost & found directory creation mode */
258
259daddr_t	n_blks;			/* number of blocks in use */
260int64_t	n_files;		/* number of files in use */
261
262#define	clearinode(dp)	\
263	if (sblock.fs_magic == FS_UFS1_MAGIC) {	\
264		(dp)->dp1 = ufs1_zino;		\
265	} else {				\
266		(dp)->dp2 = ufs2_zino;		\
267	}
268
269struct ufs1_dinode ufs1_zino;
270struct ufs2_dinode ufs2_zino;
271
272#define	setbmap(blkno)	setbit(blockmap, blkno)
273#define	testbmap(blkno)	isset(blockmap, blkno)
274#define	clrbmap(blkno)	clrbit(blockmap, blkno)
275
276#define	STOP	0x01
277#define	SKIP	0x02
278#define	KEEPON	0x04
279#define	ALTERED	0x08
280#define	FOUND	0x10
281
282union dinode *ginode(ino_t);
283struct inoinfo *getinoinfo(ino_t);
284void getblk(struct bufarea *, daddr_t, long);
285ino_t allocino(ino_t, int);
286
287int	(*info_fn)(char *, size_t);
288char	*info_filesys;
289