1 /* $OpenBSD: fsck.h,v 1.35 2024/02/03 18:51:57 beck Exp $ */ 2 /* $NetBSD: fsck.h,v 1.13 1996/10/11 20:15:46 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Networks Associates Technology, Inc. 6 * All rights reserved. 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 * 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. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)fsck.h 8.1 (Berkeley) 6/5/93 42 */ 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 54 #define DIP(dp, field) \ 55 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 56 (dp)->dp1.field : (dp)->dp2.field) 57 58 #define DIP_SET(dp, field, val) do { \ 59 if (sblock.fs_magic == FS_UFS1_MAGIC) \ 60 (dp)->dp1.field = (val); \ 61 else \ 62 (dp)->dp2.field = (val); \ 63 } while (0) 64 65 /* 66 * Each inode on the file system is described by the following structure. 67 * The linkcnt is initially set to the value in the inode. Each time it 68 * is found during the descent in passes 2, 3, and 4 the count is 69 * decremented. Any inodes whose count is non-zero after pass 4 needs to 70 * have its link count adjusted by the value remaining in ino_linkcnt. 71 */ 72 struct inostat { 73 char ino_state; /* state of inode, see below */ 74 char ino_type; /* type of inode */ 75 short ino_linkcnt; /* number of links not found */ 76 }; 77 78 #define USTATE 01 /* inode not allocated */ 79 #define FSTATE 02 /* inode is file */ 80 #define DSTATE 03 /* inode is directory */ 81 #define DFOUND 04 /* directory found during descent */ 82 #define DCLEAR 05 /* directory is to be cleared */ 83 #define FCLEAR 06 /* file is to be cleared */ 84 85 /* 86 * Inode state information is contained on per cylinder group lists 87 * which are described by the following structure. 88 */ 89 extern struct inostatlist { 90 long il_numalloced; /* number of inodes allocated in this cg */ 91 struct inostat *il_stat;/* inostat info for this cylinder group */ 92 } *inostathead; 93 94 #define GET_ISTATE(ino) (inoinfo(ino)->ino_state) 95 #define GET_ITYPE(ino) (inoinfo(ino)->ino_type) 96 #define SET_ISTATE(ino, v) do { GET_ISTATE(ino) = (v); } while (0) 97 #define SET_ITYPE(ino, v) do { GET_ITYPE(ino) = (v); } while (0) 98 #define ILNCOUNT(ino) (inoinfo(ino)->ino_linkcnt) 99 100 /* 101 * buffer cache structure. 102 */ 103 struct bufarea { 104 daddr_t b_bno; 105 struct bufarea *b_next; /* free list queue */ 106 struct bufarea *b_prev; /* free list queue */ 107 int b_size; 108 int b_errs; 109 int b_flags; 110 union { 111 char *b_buf; /* buffer space */ 112 int32_t *b_indir1; /* FFS1 indirect block */ 113 int64_t *b_indir2; /* FFS2 indirect block */ 114 struct fs *b_fs; /* super block */ 115 struct cg *b_cg; /* cylinder group */ 116 struct ufs1_dinode *b_dinode1; /* FFS1 inode block */ 117 struct ufs2_dinode *b_dinode2; /* FFS2 inode block */ 118 } b_un; 119 char b_dirty; 120 }; 121 122 #define IBLK(bp, i) \ 123 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 124 (bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i]) 125 126 #define IBLK_SET(bp, i, val) do { \ 127 if (sblock.fs_magic == FS_UFS1_MAGIC) \ 128 (bp)->b_un.b_indir1[i] = (val); \ 129 else \ 130 (bp)->b_un.b_indir2[i] = (val); \ 131 } while (0) 132 133 #define B_INUSE 1 134 135 #define MINBUFS 5 /* minimum number of buffers required */ 136 extern struct bufarea bufhead; /* head of list of other blks in filesys */ 137 extern struct bufarea sblk; /* file system superblock */ 138 extern struct bufarea asblk; /* alternate file system superblock */ 139 extern struct bufarea *pdirbp; /* current directory contents */ 140 extern struct bufarea *pbp; /* current inode block */ 141 extern struct bufarea *getdatablk(daddr_t, long); 142 143 #define dirty(bp) (bp)->b_dirty = 1 144 #define initbarea(bp) \ 145 (bp)->b_dirty = 0; \ 146 (bp)->b_bno = -1; \ 147 (bp)->b_flags = 0; 148 149 #define sbdirty() sblk.b_dirty = 1 150 #define sblock (*sblk.b_un.b_fs) 151 152 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE}; 153 154 struct inodesc { 155 daddr_t id_blkno; /* current block number being examined */ 156 quad_t id_filesize; /* for DATA nodes, the size of the directory */ 157 u_int64_t id_entryno; /* for DATA nodes, current entry number */ 158 ino_t id_number; /* inode number described */ 159 ino_t id_parent; /* for DATA nodes, their parent */ 160 int (*id_func) /* function to be applied to blocks of inode */ 161 (struct inodesc *); 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 int id_numfrags; /* number of frags contained in block */ 165 int id_loc; /* for DATA nodes, current location in dir */ 166 enum fixstate id_fix; /* policy on fixing errors */ 167 char id_type; /* type of descriptor, DATA or ADDR */ 168 }; 169 /* file types */ 170 #define DATA 1 171 #define ADDR 2 172 173 /* 174 * Linked list of duplicate blocks. 175 * 176 * The list is composed of two parts. The first part of the 177 * list (from duplist through the node pointed to by muldup) 178 * contains a single copy of each duplicate block that has been 179 * found. The second part of the list (from muldup to the end) 180 * contains duplicate blocks that have been found more than once. 181 * To check if a block has been found as a duplicate it is only 182 * necessary to search from duplist through muldup. To find the 183 * total number of times that a block has been found as a duplicate 184 * the entire list must be searched for occurrences of the block 185 * in question. The following diagram shows a sample list where 186 * w (found twice), x (found once), y (found three times), and z 187 * (found once) are duplicate block numbers: 188 * 189 * w -> y -> x -> z -> y -> w -> y 190 * ^ ^ 191 * | | 192 * duplist muldup 193 */ 194 struct dups { 195 struct dups *next; 196 daddr_t dup; 197 }; 198 extern struct dups *duplist; /* head of dup list */ 199 extern struct dups *muldup; /* end of unique duplicate dup block numbers */ 200 201 /* 202 * Linked list of inodes with zero link counts. 203 */ 204 struct zlncnt { 205 struct zlncnt *next; 206 ino_t zlncnt; 207 }; 208 extern struct zlncnt *zlnhead; /* head of zero link count list */ 209 210 /* 211 * Inode cache data structures. 212 */ 213 extern struct inoinfo { 214 struct inoinfo *i_nexthash; /* next entry in hash chain */ 215 struct inoinfo *i_child, *i_sibling; 216 size_t i_isize; /* size of inode */ 217 ino_t i_number; /* inode number of this entry */ 218 ino_t i_parent; /* inode number of parent */ 219 ino_t i_dotdot; /* inode number of `..' */ 220 u_int i_numblks; /* size of block array in bytes */ 221 daddr_t i_blks[1]; /* actually longer */ 222 } **inphead, **inpsort; 223 224 extern long numdirs, listmax, inplast; 225 226 extern long secsize; /* actual disk sector size */ 227 extern char nflag; /* assume a no response */ 228 extern char yflag; /* assume a yes response */ 229 extern daddr_t bflag; /* location of alternate super block */ 230 extern int debug; /* output debugging info */ 231 extern int cvtlevel; /* convert to newer file system format */ 232 extern int preen; /* just fix normal inconsistencies */ 233 extern char resolved; /* cleared if unresolved changes => not clean */ 234 extern char havesb; /* superblock has been read */ 235 extern char skipclean; /* skip clean file systems if preening */ 236 extern int fsmodified; /* 1 => write done to file system */ 237 extern int fsreadfd; /* file descriptor for reading file system */ 238 extern int fswritefd; /* file descriptor for writing file system */ 239 extern int rerun; /* rerun fsck. Only used in non-preen mode */ 240 241 extern daddr_t maxfsblock; /* number of blocks in the file system */ 242 extern char *blockmap; /* ptr to primary blk allocation map */ 243 extern ino_t maxino; /* number of inodes in file system */ 244 extern ino_t lastino; /* last inode in use */ 245 246 extern ino_t lfdir; /* lost & found directory inode number */ 247 extern char *lfname; /* lost & found directory name */ 248 extern int lfmode; /* lost & found directory creation mode */ 249 250 extern daddr_t n_blks; /* number of blocks in use */ 251 extern int64_t n_files; /* number of files in use */ 252 253 #define clearinode(dp) \ 254 if (sblock.fs_magic == FS_UFS1_MAGIC) { \ 255 (dp)->dp1 = ufs1_zino; \ 256 } else { \ 257 (dp)->dp2 = ufs2_zino; \ 258 } 259 260 extern struct ufs1_dinode ufs1_zino; 261 extern struct ufs2_dinode ufs2_zino; 262 263 #define setbmap(blkno) setbit(blockmap, blkno) 264 #define testbmap(blkno) isset(blockmap, blkno) 265 #define clrbmap(blkno) clrbit(blockmap, blkno) 266 267 #define STOP 0x01 268 #define SKIP 0x02 269 #define KEEPON 0x04 270 #define ALTERED 0x08 271 #define FOUND 0x10 272 273 union dinode *ginode(ino_t); 274 struct bufarea *cglookup(u_int cg); 275 struct inoinfo *getinoinfo(ino_t); 276 void getblk(struct bufarea *, daddr_t, long); 277 ino_t allocino(ino_t, int); 278 void *Malloc(size_t); 279 void *Calloc(size_t, size_t); 280 void *Reallocarray(void *, size_t, size_t); 281 282 extern int (*info_fn)(char *, size_t); 283 extern char *info_filesys; 284