fsck.h revision 100935
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) 1982, 1989, 1993
12 *	The Regents of the University of California.  All rights reserved.
13 * (c) UNIX System Laboratories, Inc.
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. All advertising materials mentioning features or use of this software
26 *    must display the following acknowledgement:
27 *	This product includes software developed by the University of
28 *	California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 *    may be used to endorse or promote products derived from this software
31 *    without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
45 *	@(#)fsck.h	8.4 (Berkeley) 5/9/95
46 * $FreeBSD: head/sbin/fsck_ffs/fsck.h 100935 2002-07-30 13:01:25Z phk $
47 */
48
49#include <unistd.h>
50#include <stdlib.h>
51#include <stdio.h>
52
53#define	MAXDUP		10	/* limit on dup blks (per inode) */
54#define	MAXBAD		10	/* limit on bad blks (per inode) */
55#define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
56#define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
57
58union dinode {
59	struct ufs1_dinode dp1;
60	struct ufs2_dinode dp2;
61};
62#define	DIP(dp, field) \
63	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
64	(dp)->dp1.field : (dp)->dp2.field)
65
66/*
67 * Each inode on the filesystem 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#define	IBLK(bp, i) \
118	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
119	(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
120
121#define	B_INUSE 1
122
123#define	MINBUFS		5	/* minimum number of buffers required */
124struct bufarea bufhead;		/* head of list of other blks in filesys */
125struct bufarea sblk;		/* filesystem superblock */
126struct bufarea cgblk;		/* cylinder group blocks */
127struct bufarea *pdirbp;		/* current directory contents */
128struct bufarea *pbp;		/* current inode block */
129
130#define	dirty(bp) do { \
131	if (fswritefd < 0) \
132		pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \
133	else \
134		(bp)->b_dirty = 1; \
135} while (0)
136#define	initbarea(bp) do { \
137	(bp)->b_dirty = 0; \
138	(bp)->b_bno = (ufs2_daddr_t)-1; \
139	(bp)->b_flags = 0; \
140} while (0)
141
142#define	sbdirty()	dirty(&sblk)
143#define	cgdirty()	dirty(&cgblk)
144#define	sblock		(*sblk.b_un.b_fs)
145#define	cgrp		(*cgblk.b_un.b_cg)
146
147enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
148ino_t cursnapshot;
149
150struct inodesc {
151	enum fixstate id_fix;	/* policy on fixing errors */
152	int (*id_func)(struct inodesc *);
153				/* function to be applied to blocks of inode */
154	ino_t id_number;	/* inode number described */
155	ino_t id_parent;	/* for DATA nodes, their parent */
156	ufs_lbn_t id_lbn;	/* logical block number of current block */
157	ufs2_daddr_t id_blkno;	/* current block number being examined */
158	int id_numfrags;	/* number of frags contained in block */
159	off_t id_filesize;	/* for DATA nodes, the size of the directory */
160	ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */
161	int id_loc;		/* for DATA nodes, current location in dir */
162	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
163	char *id_name;		/* for DATA nodes, name to find or enter */
164	char id_type;		/* type of descriptor, DATA or ADDR */
165};
166/* file types */
167#define	DATA	1	/* a directory */
168#define	SNAP	2	/* a snapshot */
169#define	ADDR	3	/* anything but a directory or a snapshot */
170
171/*
172 * Linked list of duplicate blocks.
173 *
174 * The list is composed of two parts. The first part of the
175 * list (from duplist through the node pointed to by muldup)
176 * contains a single copy of each duplicate block that has been
177 * found. The second part of the list (from muldup to the end)
178 * contains duplicate blocks that have been found more than once.
179 * To check if a block has been found as a duplicate it is only
180 * necessary to search from duplist through muldup. To find the
181 * total number of times that a block has been found as a duplicate
182 * the entire list must be searched for occurences of the block
183 * in question. The following diagram shows a sample list where
184 * w (found twice), x (found once), y (found three times), and z
185 * (found once) are duplicate block numbers:
186 *
187 *    w -> y -> x -> z -> y -> w -> y
188 *    ^		     ^
189 *    |		     |
190 * duplist	  muldup
191 */
192struct dups {
193	struct dups *next;
194	ufs2_daddr_t dup;
195};
196struct dups *duplist;		/* head of dup list */
197struct dups *muldup;		/* end of unique duplicate dup block numbers */
198
199/*
200 * Linked list of inodes with zero link counts.
201 */
202struct zlncnt {
203	struct zlncnt *next;
204	ino_t zlncnt;
205};
206struct zlncnt *zlnhead;		/* head of zero link count list */
207
208/*
209 * Inode cache data structures.
210 */
211struct inoinfo {
212	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
213	ino_t	i_number;		/* inode number of this entry */
214	ino_t	i_parent;		/* inode number of parent */
215	ino_t	i_dotdot;		/* inode number of `..' */
216	size_t	i_isize;		/* size of inode */
217	u_int	i_numblks;		/* size of block array in bytes */
218	ufs2_daddr_t i_blks[1];		/* actually longer */
219} **inphead, **inpsort;
220long numdirs, dirhash, listmax, inplast;
221long countdirs;			/* number of directories we actually found */
222
223#define MIBSIZE	3		/* size of fsck sysctl MIBs */
224int	adjrefcnt[MIBSIZE];	/* MIB command to adjust inode reference cnt */
225int	adjblkcnt[MIBSIZE];	/* MIB command to adjust inode block count */
226int	freefiles[MIBSIZE];	/* MIB command to free a set of files */
227int	freedirs[MIBSIZE];	/* MIB command to free a set of directories */
228int	freeblks[MIBSIZE];	/* MIB command to free a set of data blocks */
229struct	fsck_cmd cmd;		/* sysctl filesystem update commands */
230char	snapname[BUFSIZ];	/* when doing snapshots, the name of the file */
231char	*cdevname;		/* name of device being checked */
232long	dev_bsize;		/* computed value of DEV_BSIZE */
233long	secsize;		/* actual disk sector size */
234char	nflag;			/* assume a no response */
235char	yflag;			/* assume a yes response */
236int	bkgrdflag;		/* use a snapshot to run on an active system */
237int	bflag;			/* location of alternate super block */
238int	debug;			/* output debugging info */
239int	cvtlevel;		/* convert to newer filesystem format */
240int	bkgrdcheck;		/* determine if background check is possible */
241char	usedsoftdep;		/* just fix soft dependency inconsistencies */
242char	preen;			/* just fix normal inconsistencies */
243char	rerun;			/* rerun fsck. Only used in non-preen mode */
244int	returntosingle;		/* 1 => return to single user mode on exit */
245char	resolved;		/* cleared if unresolved changes => not clean */
246char	havesb;			/* superblock has been read */
247char	skipclean;		/* skip clean filesystems if preening */
248int	fsmodified;		/* 1 => write done to filesystem */
249int	fsreadfd;		/* file descriptor for reading filesystem */
250int	fswritefd;		/* file descriptor for writing filesystem */
251
252ufs2_daddr_t maxfsblock;	/* number of blocks in the filesystem */
253char	*blockmap;		/* ptr to primary blk allocation map */
254ino_t	maxino;			/* number of inodes in filesystem */
255
256ino_t	lfdir;			/* lost & found directory inode number */
257const char *lfname;		/* lost & found directory name */
258int	lfmode;			/* lost & found directory creation mode */
259
260ufs2_daddr_t n_blks;		/* number of blocks in use */
261ino_t n_files;			/* number of files in use */
262
263int	got_siginfo;		/* received a SIGINFO */
264
265#define	clearinode(dp) \
266	if (sblock.fs_magic == FS_UFS1_MAGIC) { \
267		(dp)->dp1 = ufs1_zino; \
268	} else { \
269		(dp)->dp2 = ufs2_zino; \
270	}
271struct	ufs1_dinode ufs1_zino;
272struct	ufs2_dinode ufs2_zino;
273
274#define	setbmap(blkno)	setbit(blockmap, blkno)
275#define	testbmap(blkno)	isset(blockmap, blkno)
276#define	clrbmap(blkno)	clrbit(blockmap, blkno)
277
278#define	STOP	0x01
279#define	SKIP	0x02
280#define	KEEPON	0x04
281#define	ALTERED	0x08
282#define	FOUND	0x10
283
284#define	EEXIT	8		/* Standard error exit. */
285
286struct fstab;
287
288
289void		adjust(struct inodesc *, int lcnt);
290ufs2_daddr_t	allocblk(long frags);
291ino_t		allocdir(ino_t parent, ino_t request, int mode);
292ino_t		allocino(ino_t request, int type);
293void		blkerror(ino_t ino, const char *type, ufs2_daddr_t blk);
294char	       *blockcheck(char *name);
295int		bread(int fd, char *buf, ufs2_daddr_t blk, long size);
296void		bufinit(void);
297void		bwrite(int fd, char *buf, ufs2_daddr_t blk, long size);
298void		cacheino(union dinode *dp, ino_t inumber);
299void		catch(int);
300void		catchquit(int);
301int		changeino(ino_t dir, const char *name, ino_t newnum);
302int		chkrange(ufs2_daddr_t blk, int cnt);
303void		ckfini(int markclean);
304int		ckinode(union dinode *dp, struct inodesc *);
305void		clri(struct inodesc *, const char *type, int flag);
306int		clearentry(struct inodesc *);
307void		direrror(ino_t ino, const char *errmesg);
308int		dirscan(struct inodesc *);
309int		dofix(struct inodesc *, const char *msg);
310void		ffs_clrblock(struct fs *, u_char *, ufs1_daddr_t);
311void		ffs_fragacct(struct fs *, int, int32_t [], int);
312int		ffs_isblock(struct fs *, u_char *, ufs1_daddr_t);
313void		ffs_setblock(struct fs *, u_char *, ufs1_daddr_t);
314void		fileerror(ino_t cwd, ino_t ino, const char *errmesg);
315int		findino(struct inodesc *);
316int		findname(struct inodesc *);
317void		flush(int fd, struct bufarea *bp);
318void		freeblk(ufs2_daddr_t blkno, long frags);
319void		freeino(ino_t ino);
320void		freeinodebuf(void);
321int		ftypeok(union dinode *dp);
322void		getblk(struct bufarea *bp, ufs2_daddr_t blk, long size);
323struct bufarea *getdatablk(ufs2_daddr_t blkno, long size);
324struct inoinfo *getinoinfo(ino_t inumber);
325union dinode   *getnextinode(ino_t inumber);
326void		getpathname(char *namebuf, ino_t curdir, ino_t ino);
327union dinode   *ginode(ino_t inumber);
328void		infohandler(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