Home | History | Annotate | Line # | Download | only in fsck_ffs
fsck.h revision 1.37
      1 /*	$NetBSD: fsck.h,v 1.37 2004/01/09 19:12:31 dbj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  *
      8  * This software was developed for the FreeBSD Project by Marshall
      9  * Kirk McKusick and Network Associates Laboratories, the Security
     10  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     11  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     12  * research program
     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  * 3. 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  */
     40 
     41 #include <stdio.h>
     42 #include <machine/bswap.h>
     43 
     44 #define	MAXDUP		10	/* limit on dup blks (per inode) */
     45 #define	MAXBAD		10	/* limit on bad blks (per inode) */
     46 #define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
     47 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
     48 
     49 union dinode {
     50 	struct ufs1_dinode dp1;
     51 	struct ufs2_dinode dp2;
     52 };
     53 #define       DIP(dp, field) \
     54 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
     55 
     56 #ifndef BUFSIZ
     57 #define BUFSIZ 1024
     58 #endif
     59 
     60 /*
     61  * Each inode on the filesystem is described by the following structure.
     62  * The linkcnt is initially set to the value in the inode. Each time it
     63  * is found during the descent in passes 2, 3, and 4 the count is
     64  * decremented. Any inodes whose count is non-zero after pass 4 needs to
     65  * have its link count adjusted by the value remaining in ino_linkcnt.
     66  */
     67 struct inostat {
     68 	char    ino_state;	/* state of inode, see below */
     69 	char    ino_type;	/* type of inode */
     70 	short   ino_linkcnt;	/* number of links not found */
     71 };
     72 /*
     73  * Inode states.
     74  */
     75 #define	USTATE	01		/* inode not allocated */
     76 #define	FSTATE	02		/* inode is file */
     77 #define	DSTATE	03		/* inode is directory */
     78 #define	DFOUND	04		/* directory found during descent */
     79 #define	DCLEAR	05		/* directory is to be cleared */
     80 #define	FCLEAR	06		/* file is to be cleared */
     81 #define	DMARK	07		/* used in propagate()'s traversal algorithm */
     82 
     83 /*
     84  * Inode state information is contained on per cylinder group lists
     85  * which are described by the following structure.
     86  */
     87 struct inostatlist {
     88 	long    il_numalloced;  /* number of inodes allocated in this cg */
     89 	struct inostat *il_stat;/* inostat info for this cylinder group */
     90 } *inostathead;
     91 
     92 
     93 /*
     94  * buffer cache structure.
     95  */
     96 struct bufarea {
     97 	struct bufarea *b_next;		/* free list queue */
     98 	struct bufarea *b_prev;		/* free list queue */
     99 	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 		int32_t *b_indir1;		/* indirect block */
    106 		int64_t *b_indir2;		/* 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 		struct appleufslabel *b_appleufs;		/* Apple UFS volume label */
    112 	} b_un;
    113 	char b_dirty;
    114 };
    115 
    116 #define       IBLK(bp, i) \
    117 	(is_ufs2 ?  (bp)->b_un.b_indir2[i] : (bp)->b_un.b_indir1[i])
    118 
    119 
    120 #define	B_INUSE 1
    121 
    122 #define	MINBUFS		5	/* minimum number of buffers required */
    123 struct bufarea bufhead;		/* head of list of other blks in filesys */
    124 struct bufarea sblk;		/* file system superblock */
    125 struct bufarea asblk;		/* file system superblock */
    126 struct bufarea cgblk;		/* cylinder group blocks */
    127 struct bufarea appleufsblk;		/* Apple UFS volume label */
    128 struct bufarea *pdirbp;		/* current directory contents */
    129 struct bufarea *pbp;		/* current inode block */
    130 
    131 #define	dirty(bp)	(bp)->b_dirty = 1
    132 #define	initbarea(bp) \
    133 	(bp)->b_dirty = 0; \
    134 	(bp)->b_bno = (daddr_t)-1; \
    135 	(bp)->b_flags = 0;
    136 
    137 struct fs *sblock;
    138 struct fs *altsblock;
    139 struct cg *cgrp;
    140 struct fs *sblocksave;
    141 #define	sbdirty() \
    142 	do { \
    143 		memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE); \
    144 		sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave); \
    145 		if (needswap) \
    146 			ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs); \
    147 		sblk.b_dirty = 1; \
    148 	} while (0)
    149 #define	cgdirty()	do {copyback_cg(&cgblk); cgblk.b_dirty = 1;} while (0)
    150 
    151 #define appleufsdirty() \
    152 	do { \
    153 		appleufsblk.b_un.b_appleufs->ul_checksum = 0; \
    154 		appleufsblk.b_un.b_appleufs->ul_checksum =  \
    155 			ffs_appleufs_cksum(appleufsblk.b_un.b_appleufs); \
    156 		appleufsblk.b_dirty = 1; \
    157 	} while (0)
    158 
    159 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
    160 
    161 struct inodesc {
    162 	enum fixstate id_fix;	/* policy on fixing errors */
    163 	int (*id_func)		/* function to be applied to blocks of inode */
    164 	    __P((struct inodesc *));
    165 	ino_t id_number;	/* inode number described */
    166 	ino_t id_parent;	/* for DATA nodes, their parent */
    167 	daddr_t id_blkno;	/* current block number being examined */
    168 	int id_numfrags;	/* number of frags contained in block */
    169 	int64_t id_filesize;	/* for DATA nodes, the size of the directory */
    170 	int id_loc;		/* for DATA nodes, current location in dir */
    171 	int64_t id_entryno;	/* for DATA nodes, current entry number */
    172 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
    173 	char *id_name;		/* for DATA nodes, name to find or enter */
    174 	char id_type;		/* type of descriptor, DATA or ADDR */
    175 };
    176 /* file types */
    177 #define	DATA	1
    178 #define	ADDR	2
    179 
    180 /*
    181  * Linked list of duplicate blocks.
    182  *
    183  * The list is composed of two parts. The first part of the
    184  * list (from duplist through the node pointed to by muldup)
    185  * contains a single copy of each duplicate block that has been
    186  * found. The second part of the list (from muldup to the end)
    187  * contains duplicate blocks that have been found more than once.
    188  * To check if a block has been found as a duplicate it is only
    189  * necessary to search from duplist through muldup. To find the
    190  * total number of times that a block has been found as a duplicate
    191  * the entire list must be searched for occurrences of the block
    192  * in question. The following diagram shows a sample list where
    193  * w (found twice), x (found once), y (found three times), and z
    194  * (found once) are duplicate block numbers:
    195  *
    196  *    w -> y -> x -> z -> y -> w -> y
    197  *    ^		     ^
    198  *    |		     |
    199  * duplist	  muldup
    200  */
    201 struct dups {
    202 	struct dups *next;
    203 	daddr_t dup;
    204 };
    205 struct dups *duplist;		/* head of dup list */
    206 struct dups *muldup;		/* end of unique duplicate dup block numbers */
    207 
    208 /*
    209  * Linked list of inodes with zero link counts.
    210  */
    211 struct zlncnt {
    212 	struct zlncnt *next;
    213 	ino_t zlncnt;
    214 };
    215 struct zlncnt *zlnhead;		/* head of zero link count list */
    216 
    217 /*
    218  * Inode cache data structures.
    219  */
    220 struct inoinfo {
    221 	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
    222 	struct	inoinfo	*i_child, *i_sibling, *i_parentp;
    223 	ino_t	i_number;		/* inode number of this entry */
    224 	ino_t	i_parent;		/* inode number of parent */
    225 	ino_t	i_dotdot;		/* inode number of `..' */
    226 	size_t	i_isize;		/* size of inode */
    227 	u_int	i_numblks;		/* size of block array in bytes */
    228 	int64_t i_blks[1];		/* actually longer */
    229 } **inphead, **inpsort;
    230 long numdirs, dirhash, listmax, inplast;
    231 
    232 long	dev_bsize;		/* computed value of DEV_BSIZE */
    233 long	secsize;		/* actual disk sector size */
    234 char	nflag;			/* assume a no response */
    235 char	yflag;			/* assume a yes response */
    236 int	bflag;			/* location of alternate super block */
    237 int	debug;			/* output debugging info */
    238 int	cvtlevel;		/* convert to newer file system format */
    239 int	doinglevel1;		/* converting to new cylinder group format */
    240 int	doinglevel2;		/* converting to new inode format */
    241 int	newinofmt;		/* filesystem has new inode format */
    242 char	usedsoftdep;		/* just fix soft dependency inconsistencies */
    243 int	preen;			/* just fix normal inconsistencies */
    244 int	quiet;			/* Don't print anything if clean */
    245 int	forceimage;		/* file system is an image file */
    246 int	doswap;			/* convert byte order */
    247 int	needswap;		/* need to convert byte order in memory */
    248 int	is_ufs2;		/* we're dealing with an UFS2 filesystem */
    249 int	do_blkswap;		/* need to do block addr byteswap */
    250 int	do_dirswap;		/* need to do dir entry byteswap */
    251 int	endian;			/* endian coversion */
    252 int	markclean;		/* mark file system clean when done */
    253 char	havesb;			/* superblock has been read */
    254 char	skipclean;		/* skip clean file systems if preening */
    255 int	fsmodified;		/* 1 => write done to file system */
    256 int	fsreadfd;		/* file descriptor for reading file system */
    257 int	fswritefd;		/* file descriptor for writing file system */
    258 int	rerun;			/* rerun fsck.  Only used in non-preen mode */
    259 char	resolved;		/* cleared if unresolved changes => not clean */
    260 int	isappleufs;		/* filesystem is Apple UFS */
    261 
    262 daddr_t maxfsblock;		/* number of blocks in the file system */
    263 char	*blockmap;		/* ptr to primary blk allocation map */
    264 ino_t	maxino;			/* number of inodes in file system */
    265 
    266 int	dirblksiz;
    267 
    268 extern ino_t	lfdir;		/* lost & found directory inode number */
    269 extern char	*lfname;	/* lost & found directory name */
    270 extern int	lfmode;		/* lost & found directory creation mode */
    271 
    272 daddr_t n_blks;		/* number of blocks in use */
    273 ino_t n_files;		/* number of files in use */
    274 
    275 long countdirs;
    276 
    277 int	got_siginfo;		/* received a SIGINFO */
    278 
    279 #define	clearinode(dp) \
    280 do { \
    281 	if (is_ufs2) 			\
    282 		(dp)->dp2 = ufs2_zino;	\
    283 	else				\
    284 		(dp)->dp1 = ufs1_zino;	\
    285 } while (0)
    286 
    287 struct	ufs1_dinode ufs1_zino;
    288 struct	ufs2_dinode ufs2_zino;
    289 
    290 #define	setbmap(blkno)	setbit(blockmap, blkno)
    291 #define	testbmap(blkno)	isset(blockmap, blkno)
    292 #define	clrbmap(blkno)	clrbit(blockmap, blkno)
    293 
    294 #define	STOP	0x01
    295 #define	SKIP	0x02
    296 #define	KEEPON	0x04
    297 #define	ALTERED	0x08
    298 #define	FOUND	0x10
    299 
    300 #define	EEXIT	8		/* Standard error exit. */
    301 
    302 /* some inline functs to help the byte-swapping mess */
    303 static __inline u_int16_t iswap16 __P((u_int16_t));
    304 static __inline u_int32_t iswap32 __P((u_int32_t));
    305 static __inline u_int64_t iswap64 __P((u_int64_t));
    306 
    307 static __inline u_int16_t iswap16(x)
    308 	u_int16_t x;
    309 {
    310 	if (needswap)
    311 		return bswap16(x);
    312 	else return x;
    313 }
    314 
    315 static __inline u_int32_t iswap32(x)
    316 	u_int32_t x;
    317 {
    318 	if (needswap)
    319 		return bswap32(x);
    320 	else return x;
    321 }
    322 
    323 static __inline u_int64_t iswap64(x)
    324 	u_int64_t x;
    325 {
    326 	if (needswap)
    327 		return bswap64(x);
    328 	else return x;
    329 }
    330 
    331 
    332