Home | History | Annotate | Line # | Download | only in fsck_ffs
      1 /*	$NetBSD: fsck.h,v 1.59 2025/03/04 07:42:19 andvar 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 <sys/queue.h>
     43 #include <machine/bswap.h>
     44 
     45 #ifdef PROGRESS
     46 #include "progress.h"
     47 #endif /* PROGRESS */
     48 
     49 #define	MAXDUP		10	/* limit on dup blks (per inode) */
     50 #define	MAXBAD		10	/* limit on bad blks (per inode) */
     51 #define	MAXBUFSPACE	40*1024	/* maximum space to allocate to buffers */
     52 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
     53 
     54 union dinode {
     55 	struct ufs1_dinode dp1;
     56 	struct ufs2_dinode dp2;
     57 };
     58 #define       DIP(dp, field) \
     59 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
     60 
     61 #define       DIP_SET(dp, field, val) do {	\
     62 	if (is_ufs2)				\
     63 		(dp)->dp2.di_##field = (val);	\
     64 	else					\
     65 		(dp)->dp1.di_##field = (val);	\
     66 } while (0)
     67 
     68 #ifndef BUFSIZ
     69 #define BUFSIZ 1024
     70 #endif
     71 
     72 /*
     73  * Each inode on the filesystem is described by the following structure.
     74  * The linkcnt is initially set to the value in the inode. Each time it
     75  * is found during the descent in passes 2, 3, and 4 the count is
     76  * decremented. Any inodes whose count is non-zero after pass 4 needs to
     77  * have its link count adjusted by the value remaining in ino_linkcnt.
     78  */
     79 struct inostat {
     80 	char    ino_state;	/* state of inode, see below */
     81 	char    ino_type;	/* type of inode */
     82 	short   ino_linkcnt;	/* number of links not found */
     83 };
     84 /*
     85  * Inode states.
     86  */
     87 #define	USTATE	01		/* inode not allocated */
     88 #define	FSTATE	02		/* inode is file */
     89 #define	DSTATE	03		/* inode is directory */
     90 #define	DFOUND	04		/* directory found during descent */
     91 #define	DCLEAR	05		/* directory is to be cleared */
     92 #define	FCLEAR	06		/* file is to be cleared */
     93 #define	DMARK	07		/* used in propagate()'s traversal algorithm */
     94 
     95 /*
     96  * Inode state information is contained on per cylinder group lists
     97  * which are described by the following structure.
     98  */
     99 extern struct inostatlist {
    100 	size_t  il_numalloced;  /* number of inodes allocated in this cg */
    101 	struct inostat *il_stat;/* inostat info for this cylinder group */
    102 } *inostathead;
    103 
    104 /*
    105  * buffer cache structure.
    106  */
    107 struct bufarea {
    108 	struct bufarea *b_next;		/* free list queue */
    109 	struct bufarea *b_prev;		/* free list queue */
    110 	daddr_t b_bno;
    111 	int b_size;
    112 	int b_errs;
    113 	int b_flags;
    114 	union {
    115 		char *b_buf;			/* buffer space */
    116 		int32_t *b_indir1;		/* indirect block */
    117 		int64_t *b_indir2;		/* indirect block */
    118 		struct fs *b_fs;		/* super block */
    119 		struct cg *b_cg;		/* cylinder group */
    120 		struct ufs1_dinode *b_dinode1;	/* UFS1 inode block */
    121 		struct ufs2_dinode *b_dinode2;	/* UFS2 inode block */
    122 #ifndef NO_APPLE_UFS
    123 		struct appleufslabel *b_appleufs;		/* Apple UFS volume label */
    124 #endif
    125 	} b_un;
    126 	char b_dirty;
    127 };
    128 
    129 #define       IBLK(bp, i) \
    130 	(is_ufs2 ?  (bp)->b_un.b_indir2[i] : (bp)->b_un.b_indir1[i])
    131 
    132 #define       IBLK_SET(bp, i, val) do {		\
    133 	if (is_ufs2)				\
    134 		(bp)->b_un.b_indir2[i] = (val);	\
    135 	else					\
    136 		(bp)->b_un.b_indir1[i] = (val);	\
    137 } while (0)
    138 
    139 #define	B_INUSE 1
    140 
    141 #define	MINBUFS		5	/* minimum number of buffers required */
    142 extern struct bufarea bufhead;	/* head of list of other blks in filesys */
    143 extern struct bufarea sblk;	/* file system superblock */
    144 extern struct bufarea asblk;	/* file system superblock */
    145 extern struct bufarea cgblk;	/* cylinder group blocks */
    146 #ifndef NO_APPLE_UFS
    147 extern struct bufarea appleufsblk; /* Apple UFS volume label */
    148 #endif
    149 extern struct bufarea *pdirbp;	/* current directory contents */
    150 extern struct bufarea *pbp;	/* current inode block */
    151 
    152 #define	dirty(bp)	(bp)->b_dirty = 1
    153 #define	initbarea(bp) \
    154 	(bp)->b_dirty = 0; \
    155 	(bp)->b_bno = (daddr_t)-1; \
    156 	(bp)->b_flags = 0;
    157 
    158 extern struct fs *sblock;
    159 extern struct fs *altsblock;
    160 extern struct cg *cgrp;
    161 extern struct fs *sblocksave;
    162 #define	sbdirty() \
    163 	do { \
    164 		memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE); \
    165 		sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave); \
    166 		if (needswap) \
    167 			ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs); \
    168 		cvt_magic(sblk.b_un.b_fs); \
    169 		sblk.b_dirty = 1; \
    170 	} while (0)
    171 #define	cgdirty()	do {copyback_cg(&cgblk); cgblk.b_dirty = 1;} while (0)
    172 
    173 #define appleufsdirty() \
    174 	do { \
    175 		appleufsblk.b_un.b_appleufs->ul_checksum = 0; \
    176 		appleufsblk.b_un.b_appleufs->ul_checksum =  \
    177 			ffs_appleufs_cksum(appleufsblk.b_un.b_appleufs); \
    178 		appleufsblk.b_dirty = 1; \
    179 	} while (0)
    180 
    181 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
    182 
    183 struct inodesc {
    184 	enum fixstate id_fix;	/* policy on fixing errors */
    185 	int (*id_func)		/* function to be applied to blocks of inode */
    186 	    (struct inodesc *);
    187 	ino_t id_number;	/* inode number described */
    188 	ino_t id_parent;	/* for DATA nodes, their parent */
    189 	daddr_t id_blkno;	/* current block number being examined */
    190 	int id_numfrags;	/* number of frags contained in block */
    191 	int64_t id_filesize;	/* for DATA nodes, the size of the directory */
    192 	int id_loc;		/* for DATA nodes, current location in dir */
    193 	int64_t id_entryno;	/* for DATA nodes, current entry number */
    194 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
    195 	const char *id_name;	/* for DATA nodes, name to find or enter */
    196 	char id_type;		/* type of descriptor, DATA or ADDR */
    197 	uid_t id_uid;		/* ownership of inode described */
    198 	gid_t id_gid;
    199 };
    200 /* file types */
    201 #define	DATA	1
    202 #define	SNAP	2
    203 #define	ADDR	3
    204 
    205 /*
    206  * Linked list of duplicate blocks.
    207  *
    208  * The list is composed of two parts. The first part of the
    209  * list (from duplist through the node pointed to by muldup)
    210  * contains a single copy of each duplicate block that has been
    211  * found. The second part of the list (from muldup to the end)
    212  * contains duplicate blocks that have been found more than once.
    213  * To check if a block has been found as a duplicate it is only
    214  * necessary to search from duplist through muldup. To find the
    215  * total number of times that a block has been found as a duplicate
    216  * the entire list must be searched for occurrences of the block
    217  * in question. The following diagram shows a sample list where
    218  * w (found twice), x (found once), y (found three times), and z
    219  * (found once) are duplicate block numbers:
    220  *
    221  *    w -> y -> x -> z -> y -> w -> y
    222  *    ^		     ^
    223  *    |		     |
    224  * duplist	  muldup
    225  */
    226 struct dups {
    227 	struct dups *next;
    228 	daddr_t dup;
    229 };
    230 extern struct dups *duplist;	/* head of dup list */
    231 extern struct dups *muldup;	/* end of unique duplicate dup block numbers */
    232 
    233 /*
    234  * Linked list of inodes with zero link counts.
    235  */
    236 struct zlncnt {
    237 	struct zlncnt *next;
    238 	ino_t zlncnt;
    239 };
    240 extern struct zlncnt *zlnhead;		/* head of zero link count list */
    241 
    242 /*
    243  * Inode cache data structures.
    244  */
    245 extern struct inoinfo {
    246 	struct	inoinfo *i_nexthash;	/* next entry in hash chain */
    247 	struct	inoinfo	*i_child, *i_sibling;
    248 	ino_t	i_number;		/* inode number of this entry */
    249 	ino_t	i_parent;		/* inode number of parent */
    250 	ino_t	i_dotdot;		/* inode number of `..' */
    251 	size_t	i_isize;		/* size of inode */
    252 	u_int	i_numblks;		/* size of block array in bytes */
    253 	int64_t i_blks[1];		/* actually longer */
    254 } **inphead, **inpsort;
    255 extern long numdirs, dirhash, listmax, inplast;
    256 
    257 /*
    258  * quota usage structures
    259  */
    260 struct uquot {
    261 	uint64_t  uq_b; /* block usage */
    262 	uint64_t  uq_i; /* inode usage */
    263 	SLIST_ENTRY(uquot) uq_entries;
    264 	uint32_t uq_uid; /* uid/gid of the owner */
    265 };
    266 SLIST_HEAD(uquot_hash, uquot);
    267 extern struct uquot_hash *uquot_user_hash;
    268 extern struct uquot_hash *uquot_group_hash;
    269 extern uint8_t q2h_hash_shift;
    270 extern uint16_t q2h_hash_mask;
    271 
    272 extern long	dev_bsize;	/* computed value of DEV_BSIZE */
    273 extern long	secsize;	/* actual disk sector size */
    274 extern char	nflag;		/* assume a no response */
    275 extern char	yflag;		/* assume a yes response */
    276 extern int	Uflag;		/* resolve user names */
    277 extern int	bflag;		/* location of alternate super block */
    278 extern int	debug;		/* output debugging info */
    279 extern int	zflag;		/* zero unused directory space */
    280 extern int	cvtlevel;	/* convert to newer file system format */
    281 extern int	doinglevel1;	/* converting to new cylinder group format */
    282 extern int	doinglevel2;	/* converting to new inode format */
    283 extern int	doing2ea;	/* converting UFS2 to UFS2ea */
    284 extern int	doing2noea;	/* converting UFS2ea to UFS2 */
    285 extern int	newinofmt;	/* filesystem has new inode format */
    286 extern char	usedsoftdep;	/* just fix soft dependency inconsistencies */
    287 extern int	preen;		/* just fix normal inconsistencies */
    288 extern int	quiet;		/* Don't print anything if clean */
    289 extern int	forceimage;	/* file system is an image file */
    290 extern int	is_ufs2;	/* we're dealing with an UFS2 filesystem */
    291 extern int	is_ufs2ea;	/* is the variant that supports exattrs */
    292 extern int	markclean;	/* mark file system clean when done */
    293 extern char	havesb;		/* superblock has been read */
    294 extern char	skipclean;	/* skip clean file systems if preening */
    295 extern int	fsmodified;	/* 1 => write done to file system */
    296 extern int	fsreadfd;	/* file descriptor for reading file system */
    297 extern int	fswritefd;	/* file descriptor for writing file system */
    298 extern int	rerun;		/* rerun fsck.  Only used in non-preen mode */
    299 extern char	resolved;	/* cleared if unresolved changes => not clean */
    300 
    301 #ifndef NO_FFS_EI
    302 extern int	endian;		/* endian coversion */
    303 extern int	doswap;		/* convert byte order */
    304 extern int	needswap;	/* need to convert byte order in memory */
    305 extern int	do_blkswap;	/* need to do block addr byteswap */
    306 extern int	do_dirswap;	/* need to do dir entry byteswap */
    307 #else
    308 /* Disable Endian-Independent FFS support for install media */
    309 #define	endian			(0)
    310 #define	doswap			(0)
    311 #define	needswap 		(0)
    312 #define	do_blkswap		(0)
    313 #define	do_dirswap		(0)
    314 #define	ffs_cg_swap(a, b, c)	__nothing
    315 #define	ffs_csum_swap(a, b, c)	__nothing
    316 #define	ffs_sb_swap(a, b)	__nothing
    317 #define	swap_dinode1(a, b)	__nothing
    318 #define	swap_dinode2(a, b)	__nothing
    319 #endif
    320 
    321 #ifndef NO_APPLE_UFS
    322 extern int	isappleufs;		/* filesystem is Apple UFS */
    323 #else
    324 /* Disable Apple UFS support for install media */
    325 #define	isappleufs		(0)
    326 #endif
    327 
    328 extern daddr_t maxfsblock;	/* number of blocks in the file system */
    329 extern char	*blockmap;	/* ptr to primary blk allocation map */
    330 extern ino_t	maxino;		/* number of inodes in file system */
    331 
    332 extern int	dirblksiz;
    333 
    334 extern ino_t	lfdir;		/* lost & found directory inode number */
    335 extern const char *lfname;	/* lost & found directory name */
    336 extern int	lfmode;		/* lost & found directory creation mode */
    337 
    338 extern daddr_t n_blks;		/* number of blocks in use */
    339 extern ino_t n_files;		/* number of files in use */
    340 
    341 extern long countdirs;
    342 
    343 extern int	got_siginfo;		/* received a SIGINFO */
    344 
    345 #define	clearinode(dp) \
    346 do { \
    347 	if (is_ufs2) 			\
    348 		(dp)->dp2 = ufs2_zino;	\
    349 	else				\
    350 		(dp)->dp1 = ufs1_zino;	\
    351 } while (0)
    352 
    353 extern struct	ufs1_dinode ufs1_zino;
    354 extern struct	ufs2_dinode ufs2_zino;
    355 
    356 #define	setbmap(blkno)	setbit(blockmap, blkno)
    357 #define	testbmap(blkno)	isset(blockmap, blkno)
    358 #define	clrbmap(blkno)	clrbit(blockmap, blkno)
    359 
    360 #define	STOP	0x01
    361 #define	SKIP	0x02
    362 #define	KEEPON	0x04
    363 #define	ALTERED	0x08
    364 #define	FOUND	0x10
    365 
    366 #ifndef NO_FFS_EI
    367 /* some inline functs to help the byte-swapping mess */
    368 static inline u_int16_t iswap16 (u_int16_t);
    369 static inline u_int32_t iswap32 (u_int32_t);
    370 static inline u_int64_t iswap64 (u_int64_t);
    371 
    372 static inline u_int16_t
    373 iswap16(u_int16_t x)
    374 {
    375 	if (needswap)
    376 		return bswap16(x);
    377 	else return x;
    378 }
    379 
    380 static inline u_int32_t
    381 iswap32(u_int32_t x)
    382 {
    383 	if (needswap)
    384 		return bswap32(x);
    385 	else return x;
    386 }
    387 
    388 static inline u_int64_t
    389 iswap64(u_int64_t x)
    390 {
    391 	if (needswap)
    392 		return bswap64(x);
    393 	else return x;
    394 }
    395 #else
    396 #define	iswap16(x) (x)
    397 #define	iswap32(x) (x)
    398 #define	iswap64(x) (x)
    399 #endif /* NO_FFS_EI */
    400 
    401 #ifdef NO_IOBUF_ALIGNED
    402 #define	aligned_alloc(align, size)	malloc((size))
    403 #endif
    404