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