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