18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause and BSD-2-Clause-FreeBSD 38a16b7a1SPedro F. Giffuni * 41c85e6a3SKirk McKusick * Copyright (c) 2002 Networks Associates Technology, Inc. 51c85e6a3SKirk McKusick * All rights reserved. 61c85e6a3SKirk McKusick * 71c85e6a3SKirk McKusick * This software was developed for the FreeBSD Project by Marshall 81c85e6a3SKirk McKusick * Kirk McKusick and Network Associates Laboratories, the Security 91c85e6a3SKirk McKusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR 101c85e6a3SKirk McKusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 11363c1852SKirk McKusick * research program. 121c85e6a3SKirk McKusick * 1360c97629SRobert Watson * Redistribution and use in source and binary forms, with or without 1460c97629SRobert Watson * modification, are permitted provided that the following conditions 1560c97629SRobert Watson * are met: 1660c97629SRobert Watson * 1. Redistributions of source code must retain the above copyright 1760c97629SRobert Watson * notice, this list of conditions and the following disclaimer. 1860c97629SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 1960c97629SRobert Watson * notice, this list of conditions and the following disclaimer in the 2060c97629SRobert Watson * documentation and/or other materials provided with the distribution. 2160c97629SRobert Watson * 2260c97629SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2360c97629SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2460c97629SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2560c97629SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2660c97629SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2760c97629SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2860c97629SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2960c97629SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3060c97629SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3160c97629SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3260c97629SRobert Watson * SUCH DAMAGE. 3360c97629SRobert Watson * 348fae3551SRodney W. Grimes * Copyright (c) 1980, 1986, 1993 358fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 368fae3551SRodney W. Grimes * 378fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 388fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 398fae3551SRodney W. Grimes * are met: 408fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 418fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 428fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 438fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 448fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 45fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 468fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 478fae3551SRodney W. Grimes * without specific prior written permission. 488fae3551SRodney W. Grimes * 498fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 508fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 518fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 528fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 538fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 548fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 558fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 568fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 578fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 588fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 598fae3551SRodney W. Grimes * SUCH DAMAGE. 608fae3551SRodney W. Grimes * 61780a5c1eSPeter Wemm * @(#)fsck.h 8.4 (Berkeley) 5/9/95 6269205369SPeter Wemm * $FreeBSD$ 638fae3551SRodney W. Grimes */ 648fae3551SRodney W. Grimes 65d00690aeSPawel Jakub Dawidek #ifndef _FSCK_H_ 66d00690aeSPawel Jakub Dawidek #define _FSCK_H_ 67d00690aeSPawel Jakub Dawidek 68780a5c1eSPeter Wemm #include <unistd.h> 69780a5c1eSPeter Wemm #include <stdlib.h> 70780a5c1eSPeter Wemm #include <stdio.h> 71780a5c1eSPeter Wemm 722ec5c914SKirk McKusick #include <sys/queue.h> 732ec5c914SKirk McKusick 748fae3551SRodney W. Grimes #define MAXDUP 10 /* limit on dup blks (per inode) */ 758fae3551SRodney W. Grimes #define MAXBAD 10 /* limit on bad blks (per inode) */ 765cc52631SKirk McKusick #define MINBUFS 100 /* minimum number of buffers required */ 772ec5c914SKirk McKusick #define INOBUFSIZE 64*1024 /* size of buffer to read inodes in pass1 */ 782b5373deSDag-Erling Smørgrav #define ZEROBUFSIZE (dev_bsize * 128) /* size of zero buffer used by -Z */ 798fae3551SRodney W. Grimes 801c85e6a3SKirk McKusick union dinode { 811c85e6a3SKirk McKusick struct ufs1_dinode dp1; 821c85e6a3SKirk McKusick struct ufs2_dinode dp2; 831c85e6a3SKirk McKusick }; 841c85e6a3SKirk McKusick #define DIP(dp, field) \ 851c85e6a3SKirk McKusick ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 861c85e6a3SKirk McKusick (dp)->dp1.field : (dp)->dp2.field) 871c85e6a3SKirk McKusick 88c3b2344bSScott Long #define DIP_SET(dp, field, val) do { \ 89c3b2344bSScott Long if (sblock.fs_magic == FS_UFS1_MAGIC) \ 90c3b2344bSScott Long (dp)->dp1.field = (val); \ 91c3b2344bSScott Long else \ 92c3b2344bSScott Long (dp)->dp2.field = (val); \ 93c3b2344bSScott Long } while (0) 94c3b2344bSScott Long 95d33e92f9SJulian Elischer /* 96d33e92f9SJulian Elischer * Each inode on the file system is described by the following structure. 97d33e92f9SJulian Elischer * The linkcnt is initially set to the value in the inode. Each time it 98d33e92f9SJulian Elischer * is found during the descent in passes 2, 3, and 4 the count is 99d33e92f9SJulian Elischer * decremented. Any inodes whose count is non-zero after pass 4 needs to 100d33e92f9SJulian Elischer * have its link count adjusted by the value remaining in ino_linkcnt. 101d33e92f9SJulian Elischer */ 102d33e92f9SJulian Elischer struct inostat { 1035cc52631SKirk McKusick u_char ino_state; /* state of inode, see below */ 1045cc52631SKirk McKusick u_char ino_type:4; /* type of inode */ 1055cc52631SKirk McKusick u_char ino_idtype:4; /* idesc id_type, SNAP or ADDR */ 1065cc52631SKirk McKusick u_short ino_linkcnt; /* number of links not found */ 107d33e92f9SJulian Elischer }; 108d33e92f9SJulian Elischer /* 109d33e92f9SJulian Elischer * Inode states. 110d33e92f9SJulian Elischer */ 111af6726e6SDon Lewis #define USTATE 0x1 /* inode not allocated */ 112af6726e6SDon Lewis #define FSTATE 0x2 /* inode is file */ 113af6726e6SDon Lewis #define FZLINK 0x3 /* inode is file with a link count of zero */ 114af6726e6SDon Lewis #define DSTATE 0x4 /* inode is directory */ 115af6726e6SDon Lewis #define DZLINK 0x5 /* inode is directory with a zero link count */ 116af6726e6SDon Lewis #define DFOUND 0x6 /* directory found during descent */ 117af6726e6SDon Lewis /* 0x7 UNUSED - see S_IS_DVALID() definition */ 118af6726e6SDon Lewis #define DCLEAR 0x8 /* directory is to be cleared */ 119af6726e6SDon Lewis #define FCLEAR 0x9 /* file is to be cleared */ 120af6726e6SDon Lewis /* DUNFOUND === (state == DSTATE || state == DZLINK) */ 121af6726e6SDon Lewis #define S_IS_DUNFOUND(state) (((state) & ~0x1) == DSTATE) 122af6726e6SDon Lewis /* DVALID === (state == DSTATE || state == DZLINK || state == DFOUND) */ 123af6726e6SDon Lewis #define S_IS_DVALID(state) (((state) & ~0x3) == DSTATE) 124af6726e6SDon Lewis #define INO_IS_DUNFOUND(ino) S_IS_DUNFOUND(inoinfo(ino)->ino_state) 125af6726e6SDon Lewis #define INO_IS_DVALID(ino) S_IS_DVALID(inoinfo(ino)->ino_state) 126d33e92f9SJulian Elischer /* 127d33e92f9SJulian Elischer * Inode state information is contained on per cylinder group lists 128d33e92f9SJulian Elischer * which are described by the following structure. 129d33e92f9SJulian Elischer */ 130c3e9752eSKyle Evans extern struct inostatlist { 131d33e92f9SJulian Elischer long il_numalloced; /* number of inodes allocated in this cg */ 132d33e92f9SJulian Elischer struct inostat *il_stat;/* inostat info for this cylinder group */ 133d33e92f9SJulian Elischer } *inostathead; 1348fae3551SRodney W. Grimes 1358fae3551SRodney W. Grimes /* 1365cc52631SKirk McKusick * Structure to reference a dinode. 1375cc52631SKirk McKusick */ 1385cc52631SKirk McKusick struct inode { 1395cc52631SKirk McKusick struct bufarea *i_bp; /* buffer containing the dinode */ 1405cc52631SKirk McKusick union dinode *i_dp; /* pointer to dinode in buffer */ 1415cc52631SKirk McKusick ino_t i_number; /* inode number */ 1425cc52631SKirk McKusick }; 1435cc52631SKirk McKusick 1445cc52631SKirk McKusick /* 1455cc52631SKirk McKusick * Size of hash tables 1465cc52631SKirk McKusick */ 1475cc52631SKirk McKusick #define HASHSIZE 2048 1485cc52631SKirk McKusick #define HASH(x) ((x * 2654435761) & (HASHSIZE - 1)) 1495cc52631SKirk McKusick 1505cc52631SKirk McKusick /* 1518fae3551SRodney W. Grimes * buffer cache structure. 1528fae3551SRodney W. Grimes */ 1538fae3551SRodney W. Grimes struct bufarea { 1545cc52631SKirk McKusick TAILQ_ENTRY(bufarea) b_list; /* LRU buffer queue */ 1555cc52631SKirk McKusick LIST_ENTRY(bufarea) b_hash; /* hash list */ 15668dc94c7SKirk McKusick ufs2_daddr_t b_bno; /* disk block number */ 15768dc94c7SKirk McKusick int b_size; /* size of I/O */ 15868dc94c7SKirk McKusick int b_errs; /* I/O error */ 15968dc94c7SKirk McKusick int b_flags; /* B_ flags below */ 16068dc94c7SKirk McKusick int b_type; /* BT_ type below */ 1615cc52631SKirk McKusick int b_refcnt; /* ref count of users */ 1625cc52631SKirk McKusick int b_index; /* for BT_LEVEL, ptr index */ 1635cc52631SKirk McKusick /* for BT_INODES, first inum */ 1648fae3551SRodney W. Grimes union { 1658fae3551SRodney W. Grimes char *b_buf; /* buffer space */ 1661c85e6a3SKirk McKusick ufs1_daddr_t *b_indir1; /* UFS1 indirect block */ 1671c85e6a3SKirk McKusick ufs2_daddr_t *b_indir2; /* UFS2 indirect block */ 1688fae3551SRodney W. Grimes struct fs *b_fs; /* super block */ 1698fae3551SRodney W. Grimes struct cg *b_cg; /* cylinder group */ 1701c85e6a3SKirk McKusick struct ufs1_dinode *b_dinode1; /* UFS1 inode block */ 1711c85e6a3SKirk McKusick struct ufs2_dinode *b_dinode2; /* UFS2 inode block */ 1728fae3551SRodney W. Grimes } b_un; 1738fae3551SRodney W. Grimes }; 174c3b2344bSScott Long 1751c85e6a3SKirk McKusick #define IBLK(bp, i) \ 1761c85e6a3SKirk McKusick ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 1771c85e6a3SKirk McKusick (bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i]) 1788fae3551SRodney W. Grimes 179c3b2344bSScott Long #define IBLK_SET(bp, i, val) do { \ 180c3b2344bSScott Long if (sblock.fs_magic == FS_UFS1_MAGIC) \ 181c3b2344bSScott Long (bp)->b_un.b_indir1[i] = (val); \ 182c3b2344bSScott Long else \ 183c3b2344bSScott Long (bp)->b_un.b_indir2[i] = (val); \ 184c3b2344bSScott Long } while (0) 185c3b2344bSScott Long 1862ec5c914SKirk McKusick /* 1872ec5c914SKirk McKusick * Buffer flags 1882ec5c914SKirk McKusick */ 1895cc52631SKirk McKusick #define B_DIRTY 0x00000001 /* Buffer is dirty */ 190ed75b5a1SKirk McKusick /* 191ed75b5a1SKirk McKusick * Type of data in buffer 192ed75b5a1SKirk McKusick */ 19368dc94c7SKirk McKusick #define BT_UNKNOWN 0 /* Buffer type is unknown */ 194ed75b5a1SKirk McKusick #define BT_SUPERBLK 1 /* Buffer holds a superblock */ 195ed75b5a1SKirk McKusick #define BT_CYLGRP 2 /* Buffer holds a cylinder group map */ 196ed75b5a1SKirk McKusick #define BT_LEVEL1 3 /* Buffer holds single level indirect */ 197ed75b5a1SKirk McKusick #define BT_LEVEL2 4 /* Buffer holds double level indirect */ 198ed75b5a1SKirk McKusick #define BT_LEVEL3 5 /* Buffer holds triple level indirect */ 199ed75b5a1SKirk McKusick #define BT_EXTATTR 6 /* Buffer holds external attribute data */ 20068dc94c7SKirk McKusick #define BT_INODES 7 /* Buffer holds inodes */ 201ed75b5a1SKirk McKusick #define BT_DIRDATA 8 /* Buffer holds directory data */ 202ed75b5a1SKirk McKusick #define BT_DATA 9 /* Buffer holds user data */ 2035cc52631SKirk McKusick #define BT_EMPTY 10 /* Buffer allocated but not filled */ 2045cc52631SKirk McKusick #define BT_NUMBUFTYPES 11 205ed75b5a1SKirk McKusick #define BT_NAMES { \ 206ed75b5a1SKirk McKusick "unknown", \ 207ed75b5a1SKirk McKusick "Superblock", \ 208ed75b5a1SKirk McKusick "Cylinder Group", \ 209ed75b5a1SKirk McKusick "Single Level Indirect", \ 210ed75b5a1SKirk McKusick "Double Level Indirect", \ 211ed75b5a1SKirk McKusick "Triple Level Indirect", \ 212ed75b5a1SKirk McKusick "External Attribute", \ 213ed75b5a1SKirk McKusick "Inode Block", \ 214ed75b5a1SKirk McKusick "Directory Contents", \ 2155cc52631SKirk McKusick "User Data", \ 2165cc52631SKirk McKusick "Allocated but not filled" } 2175cc52631SKirk McKusick extern char *buftype[]; 2185cc52631SKirk McKusick #define BT_BUFTYPE(type) \ 2195cc52631SKirk McKusick type < BT_NUMBUFTYPES ? buftype[type] : buftype[BT_UNKNOWN] 2207703a6ffSScott Long extern long readcnt[BT_NUMBUFTYPES]; 2217703a6ffSScott Long extern long totalreadcnt[BT_NUMBUFTYPES]; 2227703a6ffSScott Long extern struct timespec readtime[BT_NUMBUFTYPES]; 2237703a6ffSScott Long extern struct timespec totalreadtime[BT_NUMBUFTYPES]; 2247703a6ffSScott Long extern struct timespec startprog; 2258fae3551SRodney W. Grimes 2265cc52631SKirk McKusick extern struct bufarea *icachebp; /* inode cache buffer */ 2277703a6ffSScott Long extern struct bufarea sblk; /* file system superblock */ 2287703a6ffSScott Long extern struct bufarea *pdirbp; /* current directory contents */ 2295cc52631SKirk McKusick extern int sujrecovery; /* 1 => doing check using the journal */ 2308fae3551SRodney W. Grimes 231bf58d635SIan Dowse #define dirty(bp) do { \ 2327578c6abSKirk McKusick if (fswritefd < 0) \ 2337578c6abSKirk McKusick pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \ 2347578c6abSKirk McKusick else \ 2355cc52631SKirk McKusick (bp)->b_flags |= B_DIRTY; \ 236bf58d635SIan Dowse } while (0) 237ed75b5a1SKirk McKusick #define initbarea(bp, type) do { \ 2381c85e6a3SKirk McKusick (bp)->b_bno = (ufs2_daddr_t)-1; \ 239fc56fd26SKirk McKusick (bp)->b_size = 0; \ 240fc56fd26SKirk McKusick (bp)->b_errs = 0; \ 241bf58d635SIan Dowse (bp)->b_flags = 0; \ 242fc56fd26SKirk McKusick (bp)->b_type = type; \ 2435cc52631SKirk McKusick (bp)->b_refcnt = 0; \ 2445cc52631SKirk McKusick (bp)->b_index = 0; \ 245bf58d635SIan Dowse } while (0) 2468fae3551SRodney W. Grimes 2477578c6abSKirk McKusick #define sbdirty() dirty(&sblk) 2488fae3551SRodney W. Grimes #define sblock (*sblk.b_un.b_fs) 2498fae3551SRodney W. Grimes 2508fae3551SRodney W. Grimes enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE}; 2517703a6ffSScott Long extern ino_t cursnapshot; 2528fae3551SRodney W. Grimes 2538fae3551SRodney W. Grimes struct inodesc { 2548fae3551SRodney W. Grimes enum fixstate id_fix; /* policy on fixing errors */ 255599304a4SPoul-Henning Kamp int (*id_func)(struct inodesc *); 256599304a4SPoul-Henning Kamp /* function to be applied to blocks of inode */ 2575cc52631SKirk McKusick struct bufarea *id_bp; /* ckinode: buffer with indirect pointers */ 2585cc52631SKirk McKusick union dinode *id_dp; /* ckinode: dinode being traversed */ 2598fae3551SRodney W. Grimes ino_t id_number; /* inode number described */ 2608fae3551SRodney W. Grimes ino_t id_parent; /* for DATA nodes, their parent */ 2611c85e6a3SKirk McKusick ufs_lbn_t id_lbn; /* logical block number of current block */ 2621c85e6a3SKirk McKusick ufs2_daddr_t id_blkno; /* current block number being examined */ 263d4833913SKirk McKusick int id_level; /* level of indirection of this block */ 2648fae3551SRodney W. Grimes int id_numfrags; /* number of frags contained in block */ 265ac4b20a0SKirk McKusick ufs_lbn_t id_lballoc; /* pass1: last LBN that is allocated */ 2661c85e6a3SKirk McKusick off_t id_filesize; /* for DATA nodes, the size of the directory */ 2671c85e6a3SKirk McKusick ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */ 2688fae3551SRodney W. Grimes int id_loc; /* for DATA nodes, current location in dir */ 2698fae3551SRodney W. Grimes struct direct *id_dirp; /* for DATA nodes, ptr to current entry */ 2708fae3551SRodney W. Grimes char *id_name; /* for DATA nodes, name to find or enter */ 2715cc52631SKirk McKusick char id_type; /* type of descriptor, DATA, ADDR, or SNAP */ 2728fae3551SRodney W. Grimes }; 2738fae3551SRodney W. Grimes /* file types */ 274142d8d2fSKirk McKusick #define DATA 1 /* a directory */ 275142d8d2fSKirk McKusick #define SNAP 2 /* a snapshot */ 276142d8d2fSKirk McKusick #define ADDR 3 /* anything but a directory or a snapshot */ 2778fae3551SRodney W. Grimes 2788fae3551SRodney W. Grimes /* 2798fae3551SRodney W. Grimes * Linked list of duplicate blocks. 2808fae3551SRodney W. Grimes * 2818fae3551SRodney W. Grimes * The list is composed of two parts. The first part of the 2828fae3551SRodney W. Grimes * list (from duplist through the node pointed to by muldup) 2838fae3551SRodney W. Grimes * contains a single copy of each duplicate block that has been 2848fae3551SRodney W. Grimes * found. The second part of the list (from muldup to the end) 2858fae3551SRodney W. Grimes * contains duplicate blocks that have been found more than once. 2868fae3551SRodney W. Grimes * To check if a block has been found as a duplicate it is only 2878fae3551SRodney W. Grimes * necessary to search from duplist through muldup. To find the 2888fae3551SRodney W. Grimes * total number of times that a block has been found as a duplicate 2894b85a12fSUlrich Spörlein * the entire list must be searched for occurrences of the block 2908fae3551SRodney W. Grimes * in question. The following diagram shows a sample list where 2918fae3551SRodney W. Grimes * w (found twice), x (found once), y (found three times), and z 2928fae3551SRodney W. Grimes * (found once) are duplicate block numbers: 2938fae3551SRodney W. Grimes * 2948fae3551SRodney W. Grimes * w -> y -> x -> z -> y -> w -> y 2958fae3551SRodney W. Grimes * ^ ^ 2968fae3551SRodney W. Grimes * | | 2978fae3551SRodney W. Grimes * duplist muldup 2988fae3551SRodney W. Grimes */ 2998fae3551SRodney W. Grimes struct dups { 3008fae3551SRodney W. Grimes struct dups *next; 3011c85e6a3SKirk McKusick ufs2_daddr_t dup; 3028fae3551SRodney W. Grimes }; 303c3e9752eSKyle Evans extern struct dups *duplist; /* head of dup list */ 304c3e9752eSKyle Evans extern struct dups *muldup; /* end of unique duplicate dup block numbers */ 3058fae3551SRodney W. Grimes 3068fae3551SRodney W. Grimes /* 3078fae3551SRodney W. Grimes * Inode cache data structures. 3088fae3551SRodney W. Grimes */ 309c3e9752eSKyle Evans extern struct inoinfo { 3108fae3551SRodney W. Grimes struct inoinfo *i_nexthash; /* next entry in hash chain */ 3118fae3551SRodney W. Grimes ino_t i_number; /* inode number of this entry */ 3128fae3551SRodney W. Grimes ino_t i_parent; /* inode number of parent */ 3138fae3551SRodney W. Grimes ino_t i_dotdot; /* inode number of `..' */ 3148fae3551SRodney W. Grimes size_t i_isize; /* size of inode */ 3158fae3551SRodney W. Grimes u_int i_numblks; /* size of block array in bytes */ 3161c85e6a3SKirk McKusick ufs2_daddr_t i_blks[1]; /* actually longer */ 3178fae3551SRodney W. Grimes } **inphead, **inpsort; 318f6717697SPedro F. Giffuni extern long dirhash, inplast; 319f6717697SPedro F. Giffuni extern unsigned long numdirs, listmax; 3207703a6ffSScott Long extern long countdirs; /* number of directories we actually found */ 3218fae3551SRodney W. Grimes 3227578c6abSKirk McKusick #define MIBSIZE 3 /* size of fsck sysctl MIBs */ 3237703a6ffSScott Long extern int adjrefcnt[MIBSIZE]; /* MIB command to adjust inode reference cnt */ 3247703a6ffSScott Long extern int adjblkcnt[MIBSIZE]; /* MIB command to adjust inode block count */ 325ac4b20a0SKirk McKusick extern int setsize[MIBSIZE]; /* MIB command to set inode size */ 3267703a6ffSScott Long extern int adjndir[MIBSIZE]; /* MIB command to adjust number of directories */ 3277703a6ffSScott Long extern int adjnbfree[MIBSIZE]; /* MIB command to adjust number of free blocks */ 3287703a6ffSScott Long extern int adjnifree[MIBSIZE]; /* MIB command to adjust number of free inodes */ 3297703a6ffSScott Long extern int adjnffree[MIBSIZE]; /* MIB command to adjust number of free frags */ 3307703a6ffSScott Long extern int adjnumclusters[MIBSIZE]; /* MIB command to adjust number of free clusters */ 3317703a6ffSScott Long extern int freefiles[MIBSIZE]; /* MIB command to free a set of files */ 3327703a6ffSScott Long extern int freedirs[MIBSIZE]; /* MIB command to free a set of directories */ 3337703a6ffSScott Long extern int freeblks[MIBSIZE]; /* MIB command to free a set of data blocks */ 3347703a6ffSScott Long extern struct fsck_cmd cmd; /* sysctl file system update commands */ 3357703a6ffSScott Long extern char snapname[BUFSIZ]; /* when doing snapshots, the name of the file */ 3367703a6ffSScott Long extern char *cdevname; /* name of device being checked */ 3377703a6ffSScott Long extern long dev_bsize; /* computed value of DEV_BSIZE */ 3387703a6ffSScott Long extern long secsize; /* actual disk sector size */ 339463a577bSEitan Adler extern u_int real_dev_bsize; /* actual disk sector size, not overridden */ 3407703a6ffSScott Long extern char nflag; /* assume a no response */ 3417703a6ffSScott Long extern char yflag; /* assume a yes response */ 3427703a6ffSScott Long extern int bkgrdflag; /* use a snapshot to run on an active system */ 343dffce215SKirk McKusick extern off_t bflag; /* location of alternate super block */ 3447703a6ffSScott Long extern int debug; /* output debugging info */ 3457703a6ffSScott Long extern int Eflag; /* delete empty data blocks */ 3467703a6ffSScott Long extern int Zflag; /* zero empty data blocks */ 3470061238fSKirk McKusick extern int zflag; /* zero unused directory space */ 3487703a6ffSScott Long extern int inoopt; /* trim out unused inodes */ 3497703a6ffSScott Long extern char ckclean; /* only do work if not cleanly unmounted */ 3507703a6ffSScott Long extern int cvtlevel; /* convert to newer file system format */ 35131461aa2SKirk McKusick extern int ckhashadd; /* check hashes to be added */ 3527703a6ffSScott Long extern int bkgrdcheck; /* determine if background check is possible */ 3537703a6ffSScott Long extern int bkgrdsumadj; /* whether the kernel have ability to adjust superblock summary */ 3547703a6ffSScott Long extern char usedsoftdep; /* just fix soft dependency inconsistencies */ 3557703a6ffSScott Long extern char preen; /* just fix normal inconsistencies */ 3567703a6ffSScott Long extern char rerun; /* rerun fsck. Only used in non-preen mode */ 3577703a6ffSScott Long extern int returntosingle; /* 1 => return to single user mode on exit */ 3587703a6ffSScott Long extern char resolved; /* cleared if unresolved changes => not clean */ 359c0bfa109SKirk McKusick extern int sbhashfailed; /* when reading superblock check hash failed */ 3607703a6ffSScott Long extern char havesb; /* superblock has been read */ 3617703a6ffSScott Long extern char skipclean; /* skip clean file systems if preening */ 3627703a6ffSScott Long extern int fsmodified; /* 1 => write done to file system */ 3637703a6ffSScott Long extern int fsreadfd; /* file descriptor for reading file system */ 3647703a6ffSScott Long extern int fswritefd; /* file descriptor for writing file system */ 3657703a6ffSScott Long extern int surrender; /* Give up if reads fail */ 3667703a6ffSScott Long extern int wantrestart; /* Restart fsck on early termination */ 3678fae3551SRodney W. Grimes 3687703a6ffSScott Long extern ufs2_daddr_t maxfsblock; /* number of blocks in the file system */ 3697703a6ffSScott Long extern char *blockmap; /* ptr to primary blk allocation map */ 3707703a6ffSScott Long extern ino_t maxino; /* number of inodes in file system */ 3718fae3551SRodney W. Grimes 3727703a6ffSScott Long extern ino_t lfdir; /* lost & found directory inode number */ 3737703a6ffSScott Long extern const char *lfname; /* lost & found directory name */ 3747703a6ffSScott Long extern int lfmode; /* lost & found directory creation mode */ 3758fae3551SRodney W. Grimes 3767703a6ffSScott Long extern ufs2_daddr_t n_blks; /* number of blocks in use */ 3777703a6ffSScott Long extern ino_t n_files; /* number of files in use */ 3788fae3551SRodney W. Grimes 3797703a6ffSScott Long extern volatile sig_atomic_t got_siginfo; /* received a SIGINFO */ 3807703a6ffSScott Long extern volatile sig_atomic_t got_sigalarm; /* received a SIGALRM */ 3816db798caSIan Dowse 3821c85e6a3SKirk McKusick #define clearinode(dp) \ 3831c85e6a3SKirk McKusick if (sblock.fs_magic == FS_UFS1_MAGIC) { \ 3845cc52631SKirk McKusick (dp)->dp1 = zino.dp1; \ 3851c85e6a3SKirk McKusick } else { \ 3865cc52631SKirk McKusick (dp)->dp2 = zino.dp2; \ 3871c85e6a3SKirk McKusick } 3885cc52631SKirk McKusick extern union dinode zino; 3898fae3551SRodney W. Grimes 3908fae3551SRodney W. Grimes #define setbmap(blkno) setbit(blockmap, blkno) 3918fae3551SRodney W. Grimes #define testbmap(blkno) isset(blockmap, blkno) 3928fae3551SRodney W. Grimes #define clrbmap(blkno) clrbit(blockmap, blkno) 3938fae3551SRodney W. Grimes 3948fae3551SRodney W. Grimes #define STOP 0x01 3958fae3551SRodney W. Grimes #define SKIP 0x02 3968fae3551SRodney W. Grimes #define KEEPON 0x04 3978fae3551SRodney W. Grimes #define ALTERED 0x08 3988fae3551SRodney W. Grimes #define FOUND 0x10 3998fae3551SRodney W. Grimes 400780a5c1eSPeter Wemm #define EEXIT 8 /* Standard error exit. */ 401469759f8SDavid Bright #define ERERUN 16 /* fsck needs to be re-run. */ 4027703a6ffSScott Long #define ERESTART -1 403780a5c1eSPeter Wemm 40481fbded2SKirk McKusick int flushentry(void); 40581fbded2SKirk McKusick /* 40681fbded2SKirk McKusick * Wrapper for malloc() that flushes the cylinder group cache to try 40781fbded2SKirk McKusick * to get space. 40881fbded2SKirk McKusick */ 40981fbded2SKirk McKusick static inline void* 410eff68496SKirk McKusick Malloc(size_t size) 41181fbded2SKirk McKusick { 41281fbded2SKirk McKusick void *retval; 41381fbded2SKirk McKusick 41481fbded2SKirk McKusick while ((retval = malloc(size)) == NULL) 41581fbded2SKirk McKusick if (flushentry() == 0) 41681fbded2SKirk McKusick break; 41781fbded2SKirk McKusick return (retval); 41881fbded2SKirk McKusick } 41981fbded2SKirk McKusick 42081fbded2SKirk McKusick /* 42181fbded2SKirk McKusick * Wrapper for calloc() that flushes the cylinder group cache to try 42281fbded2SKirk McKusick * to get space. 42381fbded2SKirk McKusick */ 42481fbded2SKirk McKusick static inline void* 425eff68496SKirk McKusick Calloc(size_t cnt, size_t size) 42681fbded2SKirk McKusick { 42781fbded2SKirk McKusick void *retval; 42881fbded2SKirk McKusick 42981fbded2SKirk McKusick while ((retval = calloc(cnt, size)) == NULL) 43081fbded2SKirk McKusick if (flushentry() == 0) 43181fbded2SKirk McKusick break; 43281fbded2SKirk McKusick return (retval); 43381fbded2SKirk McKusick } 43481fbded2SKirk McKusick 435780a5c1eSPeter Wemm struct fstab; 436780a5c1eSPeter Wemm 437d33e92f9SJulian Elischer 438b70cd7eeSWarner Losh void adjust(struct inodesc *, int lcnt); 4395cc52631SKirk McKusick void alarmhandler(int sig); 4401c85e6a3SKirk McKusick ufs2_daddr_t allocblk(long frags); 441b70cd7eeSWarner Losh ino_t allocdir(ino_t parent, ino_t request, int mode); 442b70cd7eeSWarner Losh ino_t allocino(ino_t request, int type); 443599304a4SPoul-Henning Kamp void blkerror(ino_t ino, const char *type, ufs2_daddr_t blk); 444b70cd7eeSWarner Losh char *blockcheck(char *name); 445aef8d244SPawel Jakub Dawidek int blread(int fd, char *buf, ufs2_daddr_t blk, long size); 446b70cd7eeSWarner Losh void bufinit(void); 4474a835375SDavid E. O'Brien void blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size); 4488d3dfc26SDag-Erling Smørgrav void blerase(int fd, ufs2_daddr_t blk, long size); 4492b5373deSDag-Erling Smørgrav void blzero(int fd, ufs2_daddr_t blk, long size); 4505cc52631SKirk McKusick void brelse(struct bufarea *); 4511c85e6a3SKirk McKusick void cacheino(union dinode *dp, ino_t inumber); 452b70cd7eeSWarner Losh void catch(int); 453b70cd7eeSWarner Losh void catchquit(int); 4548ebae128SKirk McKusick void cgdirty(struct bufarea *); 4555cc52631SKirk McKusick struct bufarea *cglookup(int cg); 456599304a4SPoul-Henning Kamp int changeino(ino_t dir, const char *name, ino_t newnum); 4575cc52631SKirk McKusick int check_cgmagic(int cg, struct bufarea *cgbp, int requestrebuild); 4581c85e6a3SKirk McKusick int chkrange(ufs2_daddr_t blk, int cnt); 459b70cd7eeSWarner Losh void ckfini(int markclean); 4601c85e6a3SKirk McKusick int ckinode(union dinode *dp, struct inodesc *); 461599304a4SPoul-Henning Kamp void clri(struct inodesc *, const char *type, int flag); 462b70cd7eeSWarner Losh int clearentry(struct inodesc *); 463599304a4SPoul-Henning Kamp void direrror(ino_t ino, const char *errmesg); 464b70cd7eeSWarner Losh int dirscan(struct inodesc *); 465599304a4SPoul-Henning Kamp int dofix(struct inodesc *, const char *msg); 46651869213SPoul-Henning Kamp int eascan(struct inodesc *, struct ufs2_dinode *dp); 467599304a4SPoul-Henning Kamp void fileerror(ino_t cwd, ino_t ino, const char *errmesg); 468ed75b5a1SKirk McKusick void finalIOstats(void); 469b70cd7eeSWarner Losh int findino(struct inodesc *); 470b70cd7eeSWarner Losh int findname(struct inodesc *); 471b70cd7eeSWarner Losh void flush(int fd, struct bufarea *bp); 4727180f1abSKirk McKusick int freeblock(struct inodesc *); 473b70cd7eeSWarner Losh void freeino(ino_t ino); 474b70cd7eeSWarner Losh void freeinodebuf(void); 4757703a6ffSScott Long void fsutilinit(void); 4761c85e6a3SKirk McKusick int ftypeok(union dinode *dp); 4771c85e6a3SKirk McKusick void getblk(struct bufarea *bp, ufs2_daddr_t blk, long size); 478ed75b5a1SKirk McKusick struct bufarea *getdatablk(ufs2_daddr_t blkno, long size, int type); 479b70cd7eeSWarner Losh struct inoinfo *getinoinfo(ino_t inumber); 480910b491eSKirk McKusick union dinode *getnextinode(ino_t inumber, int rebuildcg); 481b70cd7eeSWarner Losh void getpathname(char *namebuf, ino_t curdir, ino_t ino); 4825cc52631SKirk McKusick void ginode(ino_t, struct inode *); 483b70cd7eeSWarner Losh void infohandler(int sig); 4845cc52631SKirk McKusick void irelse(struct inode *); 4855cc52631SKirk McKusick ufs2_daddr_t ino_blkatoff(union dinode *, ino_t, ufs_lbn_t, int *, 4865cc52631SKirk McKusick struct bufarea **); 487b70cd7eeSWarner Losh void inocleanup(void); 4885cc52631SKirk McKusick void inodirty(struct inode *); 489b70cd7eeSWarner Losh struct inostat *inoinfo(ino_t inum); 490ed75b5a1SKirk McKusick void IOstats(char *what); 491b70cd7eeSWarner Losh int linkup(ino_t orphan, ino_t parentdir, char *name); 492599304a4SPoul-Henning Kamp int makeentry(ino_t parent, ino_t ino, const char *name); 493*c5d476c9SKirk McKusick int openfilesys(char *dev); 494b70cd7eeSWarner Losh void panic(const char *fmt, ...) __printflike(1, 2); 495b70cd7eeSWarner Losh void pass1(void); 496b70cd7eeSWarner Losh void pass1b(void); 497b70cd7eeSWarner Losh int pass1check(struct inodesc *); 498b70cd7eeSWarner Losh void pass2(void); 499b70cd7eeSWarner Losh void pass3(void); 500b70cd7eeSWarner Losh void pass4(void); 501b70cd7eeSWarner Losh void pass5(void); 502b70cd7eeSWarner Losh void pfatal(const char *fmt, ...) __printflike(1, 2); 503b70cd7eeSWarner Losh void propagate(void); 5045cc52631SKirk McKusick void prtinode(struct inode *); 505b70cd7eeSWarner Losh void pwarn(const char *fmt, ...) __printflike(1, 2); 506b70cd7eeSWarner Losh int readsb(int listerr); 507599304a4SPoul-Henning Kamp int reply(const char *question); 508599304a4SPoul-Henning Kamp void rwerror(const char *mesg, ufs2_daddr_t blk); 509b70cd7eeSWarner Losh void sblock_init(void); 5105cc52631SKirk McKusick void setinodebuf(int, ino_t); 511b70cd7eeSWarner Losh int setup(char *dev); 512aef8d244SPawel Jakub Dawidek void gjournal_check(const char *filesys); 513113db2ddSJeff Roberson int suj_check(const char *filesys); 514d2404f04SKirk McKusick void update_maps(struct cg *, struct cg*, int); 5157703a6ffSScott Long void fsckinit(void); 516d00690aeSPawel Jakub Dawidek 517d00690aeSPawel Jakub Dawidek #endif /* !_FSCK_H_ */ 518