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