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