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