fsck.h revision 1.19
1/*	$OpenBSD: fsck.h,v 1.19 2007/06/01 06:41:33 deraadt 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#define	USTATE	01		/* inode not allocated */
70#define	FSTATE	02		/* inode is file */
71#define	DSTATE	03		/* inode is directory */
72#define	DFOUND	04		/* directory found during descent */
73#define	DCLEAR	05		/* directory is to be cleared */
74#define	FCLEAR	06		/* file is to be cleared */
75
76/*
77 * buffer cache structure.
78 */
79struct bufarea {
80	struct bufarea	*b_next;		/* free list queue */
81	struct bufarea	*b_prev;		/* free list queue */
82	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		ufs1_daddr_t	*b_indir1;	/* FFS1 indirect block */
89		daddr64_t	*b_indir2;	/* FFS2 indirect block */
90		struct	fs *b_fs;		/* super block */
91		struct	cg *b_cg;		/* cylinder group */
92		struct	ufs1_dinode *b_dinode1;	/* FFS1 inode block */
93		struct	ufs2_dinode *b_dinode2;	/* FFS2 inode block */
94	} b_un;
95	char	b_dirty;
96};
97
98#define IBLK(bp, i)				\
99	((sblock.fs_magic == FS_UFS1_MAGIC) ?	\
100	(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
101
102#define IBLK_SET(bp, i, val) do {		\
103	if (sblock.fs_magic == FS_UFS1_MAGIC)	\
104		(bp)->b_un.b_indir1[i] = (val);	\
105	else					\
106		(bp)->b_un.b_indir2[i] = (val);	\
107	} while (0)
108
109#define	B_INUSE 1
110
111#define	MINBUFS		5	/* minimum number of buffers required */
112struct bufarea bufhead;		/* head of list of other blks in filesys */
113struct bufarea sblk;		/* file system superblock */
114struct bufarea asblk;		/* alternate file system superblock */
115struct bufarea cgblk;		/* cylinder group blocks */
116struct bufarea *pdirbp;		/* current directory contents */
117struct bufarea *pbp;		/* current inode block */
118struct bufarea *getdatablk(daddr_t, long);
119
120#define	dirty(bp)	(bp)->b_dirty = 1
121#define	initbarea(bp) \
122	(bp)->b_dirty = 0; \
123	(bp)->b_bno = (daddr_t)-1; \
124	(bp)->b_flags = 0;
125
126#define	sbdirty()	sblk.b_dirty = 1
127#define	cgdirty()	cgblk.b_dirty = 1
128#define	sblock		(*sblk.b_un.b_fs)
129#define	cgrp		(*cgblk.b_un.b_cg)
130
131enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
132
133struct inodesc {
134	enum fixstate id_fix;	/* policy on fixing errors */
135	int (*id_func)		/* function to be applied to blocks of inode */
136(struct inodesc *);
137	ino_t id_number;	/* inode number described */
138	ino_t id_parent;	/* for DATA nodes, their parent */
139	daddr_t id_blkno;	/* current block number being examined */
140	int id_numfrags;	/* number of frags contained in block */
141	quad_t id_filesize;	/* for DATA nodes, the size of the directory */
142	int id_loc;		/* for DATA nodes, current location in dir */
143	int id_entryno;		/* for DATA nodes, current entry number */
144	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
145	char *id_name;		/* for DATA nodes, name to find or enter */
146	char id_type;		/* type of descriptor, DATA or ADDR */
147};
148/* file types */
149#define	DATA	1
150#define	ADDR	2
151
152/*
153 * Linked list of duplicate blocks.
154 *
155 * The list is composed of two parts. The first part of the
156 * list (from duplist through the node pointed to by muldup)
157 * contains a single copy of each duplicate block that has been
158 * found. The second part of the list (from muldup to the end)
159 * contains duplicate blocks that have been found more than once.
160 * To check if a block has been found as a duplicate it is only
161 * necessary to search from duplist through muldup. To find the
162 * total number of times that a block has been found as a duplicate
163 * the entire list must be searched for occurrences of the block
164 * in question. The following diagram shows a sample list where
165 * w (found twice), x (found once), y (found three times), and z
166 * (found once) are duplicate block numbers:
167 *
168 *    w -> y -> x -> z -> y -> w -> y
169 *    ^		     ^
170 *    |		     |
171 * duplist	  muldup
172 */
173struct dups {
174	struct dups *next;
175	daddr_t dup;
176};
177struct dups *duplist;		/* head of dup list */
178struct dups *muldup;		/* end of unique duplicate dup block numbers */
179
180/*
181 * Linked list of inodes with zero link counts.
182 */
183struct zlncnt {
184	struct zlncnt *next;
185	ino_t zlncnt;
186};
187struct zlncnt *zlnhead;		/* head of zero link count list */
188
189/*
190 * Inode cache data structures.
191 */
192struct inoinfo {
193	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
194	struct	inoinfo	*i_child, *i_sibling, *i_parentp;
195	ino_t	i_number;		/* inode number of this entry */
196	ino_t	i_parent;		/* inode number of parent */
197	ino_t	i_dotdot;		/* inode number of `..' */
198	size_t	i_isize;		/* size of inode */
199	u_int	i_numblks;		/* size of block array in bytes */
200	daddr_t	i_blks[1];		/* actually longer */
201} **inphead, **inpsort;
202
203extern long numdirs, listmax, inplast;
204
205long	dev_bsize;		/* computed value of DEV_BSIZE */
206long	secsize;		/* actual disk sector size */
207char	nflag;			/* assume a no response */
208char	yflag;			/* assume a yes response */
209int	bflag;			/* location of alternate super block */
210int	debug;			/* output debugging info */
211int	cvtlevel;		/* convert to newer file system format */
212int	doinglevel1;		/* converting to new cylinder group format */
213int	doinglevel2;		/* converting to new inode format */
214int	newinofmt;		/* filesystem has new inode format */
215char    usedsoftdep;            /* just fix soft dependency inconsistencies */
216int	preen;			/* just fix normal inconsistencies */
217char    resolved;               /* cleared if unresolved changes => not clean */
218char	havesb;			/* superblock has been read */
219char	skipclean;		/* skip clean file systems if preening */
220int	fsmodified;		/* 1 => write done to file system */
221int	fsreadfd;		/* file descriptor for reading file system */
222int	fswritefd;		/* file descriptor for writing file system */
223int	rerun;			/* rerun fsck.  Only used in non-preen mode */
224
225daddr_t	maxfsblock;		/* number of blocks in the file system */
226char	*blockmap;		/* ptr to primary blk allocation map */
227ino_t	maxino;			/* number of inodes in file system */
228ino_t	lastino;		/* last inode in use */
229char	*statemap;		/* ptr to inode state table */
230char	*typemap;		/* ptr to inode type table */
231int16_t	*lncntp;		/* ptr to link count table */
232
233ino_t	lfdir;			/* lost & found directory inode number */
234char	*lfname;		/* lost & found directory name */
235int	lfmode;			/* lost & found directory creation mode */
236
237daddr_t	n_blks;			/* number of blocks in use */
238daddr_t	n_files;		/* number of files in use */
239long   *cginosused;		/* # of allocated inodes in each cg */
240
241#define	clearinode(dp)	\
242	if (sblock.fs_magic == FS_UFS1_MAGIC) {	\
243		(dp)->dp1 = ufs1_zino;		\
244	} else {				\
245		(dp)->dp2 = ufs2_zino;		\
246	}
247
248struct ufs1_dinode ufs1_zino;
249struct ufs2_dinode ufs2_zino;
250
251#define	setbmap(blkno)	setbit(blockmap, blkno)
252#define	testbmap(blkno)	isset(blockmap, blkno)
253#define	clrbmap(blkno)	clrbit(blockmap, blkno)
254
255#define	STOP	0x01
256#define	SKIP	0x02
257#define	KEEPON	0x04
258#define	ALTERED	0x08
259#define	FOUND	0x10
260
261union dinode *ginode(ino_t);
262struct inoinfo *getinoinfo(ino_t);
263void getblk(struct bufarea *, daddr_t, long);
264ino_t allocino(ino_t, int);
265
266int	(*info_fn)(char *, size_t);
267char	*info_filesys;
268