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