/*-1* SPDX-License-Identifier: BSD-3-Clause and BSD-2-Clause2*3* Copyright (c) 2002 Networks Associates Technology, Inc.4* All rights reserved.5*6* This software was developed for the FreeBSD Project by Marshall7* Kirk McKusick and Network Associates Laboratories, the Security8* Research Division of Network Associates, Inc. under DARPA/SPAWAR9* contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS10* research program.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33* Copyright (c) 1980, 1986, 199334* The Regents of the University of California. All rights reserved.35*36* Redistribution and use in source and binary forms, with or without37* modification, are permitted provided that the following conditions38* are met:39* 1. Redistributions of source code must retain the above copyright40* notice, this list of conditions and the following disclaimer.41* 2. Redistributions in binary form must reproduce the above copyright42* notice, this list of conditions and the following disclaimer in the43* documentation and/or other materials provided with the distribution.44* 3. Neither the name of the University nor the names of its contributors45* may be used to endorse or promote products derived from this software46* without specific prior written permission.47*48* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND49* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE50* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE51* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE52* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL53* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS54* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)55* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT56* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY57* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF58* SUCH DAMAGE.59*/6061#ifndef _FSCK_H_62#define _FSCK_H_6364#include <unistd.h>65#include <stdlib.h>66#include <stdio.h>67#include <libufs.h>6869#include <sys/queue.h>7071#define MAXDUP 10 /* limit on dup blks (per inode) */72#define MAXBAD 10 /* limit on bad blks (per inode) */73#define MINBUFS 100 /* minimum number of buffers required */74#define INOBUFSIZE 64*1024 /* size of buffer to read inodes in pass1 */75#define ZEROBUFSIZE (dev_bsize * 128) /* size of zero buffer used by -Z */7677#define DIP(dp, field) \78((sblock.fs_magic == FS_UFS1_MAGIC) ? \79(dp)->dp1.field : (dp)->dp2.field)8081#define DIP_SET(dp, field, val) do { \82if (sblock.fs_magic == FS_UFS1_MAGIC) \83(dp)->dp1.field = (val); \84else \85(dp)->dp2.field = (val); \86} while (0)8788/*89* Each inode on the file system is described by the following structure.90* The linkcnt is initially set to the value in the inode. Each time it91* is found during the descent in passes 2, 3, and 4 the count is92* decremented. Any inodes whose count is non-zero after pass 4 needs to93* have its link count adjusted by the value remaining in ino_linkcnt.94*/95struct inostat {96u_char ino_state; /* state of inode, see below */97u_char ino_type:4; /* type of inode */98u_char ino_idtype:4; /* idesc id_type, SNAP or ADDR */99u_short ino_linkcnt; /* number of links not found */100};101/*102* Inode states.103*/104#define USTATE 0x1 /* inode not allocated */105#define FSTATE 0x2 /* inode is file */106#define FZLINK 0x3 /* inode is file with a link count of zero */107#define DSTATE 0x4 /* inode is directory */108#define DZLINK 0x5 /* inode is directory with a zero link count */109#define DFOUND 0x6 /* directory found during descent */110/* 0x7 UNUSED - see S_IS_DVALID() definition */111#define DCLEAR 0x8 /* directory is to be cleared */112#define FCLEAR 0x9 /* file is to be cleared */113/* DUNFOUND === (state == DSTATE || state == DZLINK) */114#define S_IS_DUNFOUND(state) (((state) & ~0x1) == DSTATE)115/* DVALID === (state == DSTATE || state == DZLINK || state == DFOUND) */116#define S_IS_DVALID(state) (((state) & ~0x3) == DSTATE)117#define INO_IS_DUNFOUND(ino) S_IS_DUNFOUND(inoinfo(ino)->ino_state)118#define INO_IS_DVALID(ino) S_IS_DVALID(inoinfo(ino)->ino_state)119/*120* Inode state information is contained on per cylinder group lists121* which are described by the following structure.122*/123extern struct inostatlist {124long il_numalloced; /* number of inodes allocated in this cg */125struct inostat *il_stat;/* inostat info for this cylinder group */126} *inostathead;127128/*129* Structure to reference a dinode.130*/131struct inode {132struct bufarea *i_bp; /* buffer containing the dinode */133union dinode *i_dp; /* pointer to dinode in buffer */134ino_t i_number; /* inode number */135};136137/*138* Size of hash tables139*/140#define HASHSIZE 2048141#define HASH(x) ((x * 2654435761) & (HASHSIZE - 1))142143/*144* buffer cache structure.145*/146struct bufarea {147TAILQ_ENTRY(bufarea) b_list; /* LRU buffer queue */148LIST_ENTRY(bufarea) b_hash; /* hash list */149ufs2_daddr_t b_bno; /* disk block number */150int b_size; /* size of I/O */151int b_errs; /* I/O error */152int b_flags; /* B_ flags below */153int b_type; /* BT_ type below */154int b_refcnt; /* ref count of users */155uint64_t b_index; /* for BT_LEVEL, ptr index */156/* for BT_INODES, first inum */157union {158char *b_buf; /* buffer space */159ufs1_daddr_t *b_indir1; /* UFS1 indirect block */160ufs2_daddr_t *b_indir2; /* UFS2 indirect block */161struct fs *b_fs; /* super block */162struct cg *b_cg; /* cylinder group */163struct ufs1_dinode *b_dinode1; /* UFS1 inode block */164struct ufs2_dinode *b_dinode2; /* UFS2 inode block */165} b_un;166};167168#define IBLK(bp, i) \169((sblock.fs_magic == FS_UFS1_MAGIC) ? \170(bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])171172#define IBLK_SET(bp, i, val) do { \173if (sblock.fs_magic == FS_UFS1_MAGIC) \174(bp)->b_un.b_indir1[i] = (val); \175else \176(bp)->b_un.b_indir2[i] = (val); \177} while (0)178179/*180* Buffer flags181*/182#define B_DIRTY 0x00000001 /* Buffer is dirty */183/*184* Type of data in buffer185*/186#define BT_UNKNOWN 0 /* Buffer type is unknown */187#define BT_SUPERBLK 1 /* Buffer holds a superblock */188#define BT_CYLGRP 2 /* Buffer holds a cylinder group map */189#define BT_LEVEL1 3 /* Buffer holds single level indirect */190#define BT_LEVEL2 4 /* Buffer holds double level indirect */191#define BT_LEVEL3 5 /* Buffer holds triple level indirect */192#define BT_EXTATTR 6 /* Buffer holds external attribute data */193#define BT_INODES 7 /* Buffer holds inodes */194#define BT_DIRDATA 8 /* Buffer holds directory data */195#define BT_DATA 9 /* Buffer holds user data */196#define BT_NUMBUFTYPES 10197#define BT_NAMES { \198"unknown", \199"Superblock", \200"Cylinder Group", \201"Single Level Indirect", \202"Double Level Indirect", \203"Triple Level Indirect", \204"External Attribute", \205"Inode Block", \206"Directory Contents", \207"User Data" }208extern char *buftype[];209#define BT_BUFTYPE(type) \210type < BT_NUMBUFTYPES ? buftype[type] : buftype[BT_UNKNOWN]211extern long readcnt[BT_NUMBUFTYPES];212extern long totalreadcnt[BT_NUMBUFTYPES];213extern struct timespec readtime[BT_NUMBUFTYPES];214extern struct timespec totalreadtime[BT_NUMBUFTYPES];215extern struct timespec startprog;216217extern struct bufarea *icachebp; /* inode cache buffer */218extern struct bufarea sblk; /* file system superblock */219extern struct bufarea *pdirbp; /* current directory contents */220221#define dirty(bp) do { \222if (fswritefd < 0) \223pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \224else \225(bp)->b_flags |= B_DIRTY; \226} while (0)227#define initbarea(bp, type) do { \228(bp)->b_bno = (ufs2_daddr_t)-4; \229(bp)->b_size = 0; \230(bp)->b_errs = 0; \231(bp)->b_flags = 0; \232(bp)->b_type = type; \233(bp)->b_refcnt = 0; \234(bp)->b_index = 0; \235} while (0)236237#define sbdirty() dirty(&sblk)238#define sblock (*sblk.b_un.b_fs)239240enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};241extern ino_t cursnapshot;242243struct inodesc {244enum fixstate id_fix; /* policy on fixing errors */245int (*id_func)(struct inodesc *);246/* function to be applied to blocks of inode */247struct bufarea *id_bp; /* ckinode: buffer with indirect pointers */248union dinode *id_dp; /* ckinode: dinode being traversed */249ino_t id_number; /* inode number described */250ino_t id_parent; /* for DATA nodes, their parent */251ufs_lbn_t id_lbn; /* logical block number of current block */252ufs2_daddr_t id_blkno; /* current block number being examined */253int id_level; /* level of indirection of this block */254int id_numfrags; /* number of frags contained in block */255ufs_lbn_t id_lballoc; /* pass1: last LBN that is allocated */256off_t id_filesize; /* for DATA nodes, the size of the directory */257ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */258int id_loc; /* for DATA nodes, current location in dir */259struct direct *id_dirp; /* for DATA nodes, ptr to current entry */260char *id_name; /* for DATA nodes, name to find or enter */261char id_type; /* type of descriptor, DATA, ADDR, or SNAP */262};263/* file types */264#define DATA 1 /* a directory */265#define SNAP 2 /* a snapshot */266#define ADDR 3 /* anything but a directory or a snapshot */267268/*269* Linked list of duplicate blocks.270*271* The list is composed of two parts. The first part of the272* list (from duplist through the node pointed to by muldup)273* contains a single copy of each duplicate block that has been274* found. The second part of the list (from muldup to the end)275* contains duplicate blocks that have been found more than once.276* To check if a block has been found as a duplicate it is only277* necessary to search from duplist through muldup. To find the278* total number of times that a block has been found as a duplicate279* the entire list must be searched for occurrences of the block280* in question. The following diagram shows a sample list where281* w (found twice), x (found once), y (found three times), and z282* (found once) are duplicate block numbers:283*284* w -> y -> x -> z -> y -> w -> y285* ^ ^286* | |287* duplist muldup288*/289struct dups {290struct dups *next;291ufs2_daddr_t dup;292};293extern struct dups *duplist; /* head of dup list */294extern struct dups *muldup; /* end of unique duplicate dup block numbers */295296/*297* Inode cache data structures.298*/299struct inoinfo {300SLIST_ENTRY(inoinfo) i_hash; /* hash list */301ino_t i_number; /* inode number of this entry */302ino_t i_parent; /* inode number of parent */303ino_t i_dotdot; /* inode number of `..' */304size_t i_isize; /* size of inode */305u_int i_depth; /* depth of directory from root */306u_int i_flags; /* flags, see below */307u_int i_numblks; /* size of block array in bytes */308ufs2_daddr_t i_blks[1]; /* actually longer */309};310extern SLIST_HEAD(inohash, inoinfo) *inphash;311extern struct inoinfo **inpsort;312/*313* flags for struct inoinfo314*/315#define INFO_NEW 0x0000001 /* replaced broken directory */316317extern long dirhash, inplast;318extern unsigned long numdirs, listmax;319extern long countdirs; /* number of directories we actually found */320321#define MIBSIZE 3 /* size of fsck sysctl MIBs */322extern int adjblkcnt[MIBSIZE]; /* MIB cmd to adjust inode block count */323extern int adjrefcnt[MIBSIZE]; /* MIB cmd to adjust inode reference count */324extern int adjndir[MIBSIZE]; /* MIB cmd to adjust number of directories */325extern int adjnbfree[MIBSIZE]; /* MIB cmd to adjust number of free blocks */326extern int adjnifree[MIBSIZE]; /* MIB cmd to adjust number of free inodes */327extern int adjnffree[MIBSIZE]; /* MIB cmd to adjust number of free frags */328extern int adjnumclusters[MIBSIZE]; /* MIB cmd adjust number of free clusters */329extern int adjdepth[MIBSIZE]; /* MIB cmd to adjust directory depth count */330extern int freefiles[MIBSIZE]; /* MIB cmd to free a set of files */331extern int freedirs[MIBSIZE]; /* MIB cmd to free a set of directories */332extern int freeblks[MIBSIZE]; /* MIB cmd to free a set of data blocks */333extern int setsize[MIBSIZE]; /* MIB cmd to set inode size */334extern struct fsck_cmd cmd; /* sysctl file system update commands */335336extern int bkgrdcheck; /* determine if background check is possible */337extern int bkgrdsumadj; /* whether the kernel has the ability to adjust338the superblock summary fields */339extern off_t bflag; /* location of alternate super block */340extern int bkgrdflag; /* use a snapshot to run on an active system */341extern char *blockmap; /* ptr to primary blk allocation map */342extern char *cdevname; /* name of device being checked */343extern int cgheader_corrupt; /* one or more CG headers are corrupt */344extern char ckclean; /* only do work if not cleanly unmounted */345extern int ckhashadd; /* check hashes to be added */346extern char *copybuf; /* buffer to copy snapshot blocks */347extern int cvtlevel; /* convert to newer file system format */348extern long dev_bsize; /* computed value of DEV_BSIZE */349extern u_int real_dev_bsize; /* actual disk sector size, not overridden */350extern int debug; /* output debugging info */351extern int Eflag; /* delete empty data blocks */352extern int fsmodified; /* 1 => write done to file system */353extern int fsreadfd; /* file descriptor for reading file system */354extern int fswritefd; /* file descriptor for writing file system */355extern char havesb; /* superblock has been read */356extern int inoopt; /* trim out unused inodes */357extern ino_t lfdir; /* lost & found directory inode number */358extern int lfmode; /* lost & found directory creation mode */359extern const char *lfname; /* lost & found directory name */360extern ufs2_daddr_t maxfsblock; /* number of blocks in the file system */361extern ino_t maxino; /* number of inodes in file system */362extern ufs2_daddr_t n_blks; /* number of blocks in use */363extern ino_t n_files; /* number of files in use */364extern char nflag; /* assume a no response */365extern char preen; /* just fix normal inconsistencies */366extern char rerun; /* rerun fsck. Only used in non-preen mode */367extern char resolved; /* cleared if unresolved changes => not clean */368extern int returntosingle; /* 1 => return to single user mode on exit */369extern long secsize; /* actual disk sector size */370extern char skipclean; /* skip clean file systems if preening */371extern int snapcnt; /* number of active snapshots */372extern struct inode snaplist[FSMAXSNAP + 1]; /* list of active snapshots */373extern int sujrecovery; /* 1 => doing check using the journal */374extern int surrender; /* Give up if reads fail */375extern char usedsoftdep; /* just fix soft dependency inconsistencies */376extern int wantrestart; /* Restart fsck on early termination */377extern char yflag; /* assume a yes response */378extern int zflag; /* zero unused directory space */379extern int Zflag; /* zero empty data blocks */380381extern volatile sig_atomic_t got_siginfo; /* received a SIGINFO */382extern volatile sig_atomic_t got_sigalarm; /* received a SIGALRM */383384#define clearinode(dp) \385if (sblock.fs_magic == FS_UFS1_MAGIC) { \386(dp)->dp1 = zino.dp1; \387} else { \388(dp)->dp2 = zino.dp2; \389}390extern union dinode zino;391392#define setbmap(blkno) setbit(blockmap, blkno)393#define testbmap(blkno) isset(blockmap, blkno)394#define clrbmap(blkno) clrbit(blockmap, blkno)395396#define STOP 0x01397#define SKIP 0x02398#define KEEPON 0x04399#define ALTERED 0x08400#define FOUND 0x10401402#define EEXIT 8 /* Standard error exit. */403#define ERERUN 16 /* fsck needs to be re-run. */404#define ERESTART -1405406int flushentry(void);407/*408* Wrapper for malloc() that flushes the cylinder group cache to try409* to get space.410*/411static inline void*412Malloc(size_t size)413{414void *retval;415416while ((retval = malloc(size)) == NULL)417if (flushentry() == 0)418break;419return (retval);420}421/*422* Allocate a block of memory to be used as an I/O buffer.423* Ensure that the buffer is aligned to the I/O subsystem requirements.424*/425static inline void*426Balloc(size_t size)427{428void *retval;429430while ((retval = aligned_alloc(LIBUFS_BUFALIGN, size)) == NULL)431if (flushentry() == 0)432break;433return (retval);434}435436/*437* Wrapper for calloc() that flushes the cylinder group cache to try438* to get space.439*/440static inline void*441Calloc(size_t cnt, size_t size)442{443void *retval;444445while ((retval = calloc(cnt, size)) == NULL)446if (flushentry() == 0)447break;448return (retval);449}450451struct fstab;452453454void adjust(struct inodesc *, int lcnt);455void alarmhandler(int sig);456ufs2_daddr_t allocblk(long cg, long frags, ufs2_daddr_t (*checkblkavail)457(ufs2_daddr_t blkno, long frags));458ino_t allocdir(ino_t parent, ino_t request, int mode);459ino_t allocino(ino_t request, int type);460void binval(struct bufarea *);461void blkerror(ino_t ino, const char *type, ufs2_daddr_t blk);462char *blockcheck(char *name);463int blread(int fd, char *buf, ufs2_daddr_t blk, long size);464void bufinit(void);465void blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size);466void blerase(int fd, ufs2_daddr_t blk, long size);467void blzero(int fd, ufs2_daddr_t blk, long size);468void brelse(struct bufarea *);469struct inoinfo *cacheino(union dinode *dp, ino_t inumber);470void catch(int);471void catchquit(int);472void cgdirty(struct bufarea *);473struct bufarea *cglookup(int cg);474int changeino(ino_t dir, const char *name, ino_t newnum, int depth);475void check_blkcnt(struct inode *ip);476int check_cgmagic(int cg, struct bufarea *cgbp);477void rebuild_cg(int cg, struct bufarea *cgbp);478void check_dirdepth(struct inoinfo *inp);479int chkfilesize(mode_t mode, u_int64_t filesize);480int chkrange(ufs2_daddr_t blk, int cnt);481void ckfini(int markclean);482int ckinode(union dinode *dp, struct inodesc *);483void clri(struct inodesc *, const char *type, int flag);484int clearentry(struct inodesc *);485void copyonwrite(struct fs *, struct bufarea *,486ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long));487void direrror(ino_t ino, const char *errmesg);488int dirscan(struct inodesc *);489int dofix(struct inodesc *, const char *msg);490int eascan(struct inodesc *, struct ufs2_dinode *dp);491void fileerror(ino_t cwd, ino_t ino, const char *errmesg);492void finalIOstats(void);493int findino(struct inodesc *);494int findname(struct inodesc *);495void flush(int fd, struct bufarea *bp);496int freeblock(struct inodesc *);497void freedirino(ino_t ino, ino_t parent);498void freeino(ino_t ino);499void freeinodebuf(void);500void fsckinit(void);501void fsutilinit(void);502int ftypeok(union dinode *dp);503void getblk(struct bufarea *bp, ufs2_daddr_t blk, long size);504struct bufarea *getdatablk(ufs2_daddr_t blkno, long size, int type);505struct inoinfo *getinoinfo(ino_t inumber);506union dinode *getnextinode(ino_t inumber, int rebuiltcg);507void getpathname(char *namebuf, ino_t curdir, ino_t ino);508void ginode(ino_t, struct inode *);509void gjournal_check(const char *filesys);510void infohandler(int sig);511void irelse(struct inode *);512ufs2_daddr_t ino_blkatoff(union dinode *, ino_t, ufs_lbn_t, int *,513struct bufarea **);514void inocleanup(void);515void inodirty(struct inode *);516struct inostat *inoinfo(ino_t inum);517void IOstats(char *what);518int linkup(ino_t orphan, ino_t parentdir, char *name);519int makeentry(ino_t parent, ino_t ino, const char *name);520int openfilesys(char *dev);521void panic(const char *fmt, ...) __printflike(1, 2);522void pass1(void);523void pass1b(void);524int pass1check(struct inodesc *);525void pass2(void);526void pass3(void);527void pass4(void);528void pass5(void);529void pfatal(const char *fmt, ...) __printflike(1, 2);530void propagate(void);531void prtbuf(struct bufarea *, const char *, ...) __printflike(2, 3);532void prtinode(struct inode *);533void pwarn(const char *fmt, ...) __printflike(1, 2);534int readsb(void);535int removecachedino(ino_t);536int reply(const char *question);537void rwerror(const char *mesg, ufs2_daddr_t blk);538void sblock_init(void);539void setinodebuf(int, ino_t);540int setup(char *dev);541int snapblkfree(struct fs *, ufs2_daddr_t, long, ino_t,542ufs2_daddr_t (*)(ufs2_daddr_t, long));543void snapremove(ino_t);544void snapflush(ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long));545ufs2_daddr_t std_checkblkavail(ufs2_daddr_t blkno, long frags);546ufs2_daddr_t suj_checkblkavail(ufs2_daddr_t, long);547int suj_check(const char *filesys);548void update_maps(struct cg *, struct cg*, int);549550#endif /* !_FSCK_H_ */551552553