fsck.h revision 126345
11558Srgrimes/* 298542Smckusick * Copyright (c) 2002 Networks Associates Technology, Inc. 398542Smckusick * All rights reserved. 498542Smckusick * 598542Smckusick * This software was developed for the FreeBSD Project by Marshall 698542Smckusick * Kirk McKusick and Network Associates Laboratories, the Security 798542Smckusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR 898542Smckusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9110884Smckusick * research program. 1098542Smckusick * 111558Srgrimes * Copyright (c) 1980, 1986, 1993 121558Srgrimes * The Regents of the University of California. All rights reserved. 131558Srgrimes * 141558Srgrimes * Redistribution and use in source and binary forms, with or without 151558Srgrimes * modification, are permitted provided that the following conditions 161558Srgrimes * are met: 171558Srgrimes * 1. Redistributions of source code must retain the above copyright 181558Srgrimes * notice, this list of conditions and the following disclaimer. 191558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 201558Srgrimes * notice, this list of conditions and the following disclaimer in the 211558Srgrimes * documentation and/or other materials provided with the distribution. 221558Srgrimes * 3. All advertising materials mentioning features or use of this software 231558Srgrimes * must display the following acknowledgement: 241558Srgrimes * This product includes software developed by the University of 251558Srgrimes * California, Berkeley and its contributors. 261558Srgrimes * 4. Neither the name of the University nor the names of its contributors 271558Srgrimes * may be used to endorse or promote products derived from this software 281558Srgrimes * without specific prior written permission. 291558Srgrimes * 301558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 311558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 321558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 331558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 341558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 351558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 361558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 371558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 381558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 391558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 401558Srgrimes * SUCH DAMAGE. 411558Srgrimes * 4223675Speter * @(#)fsck.h 8.4 (Berkeley) 5/9/95 4355724Speter * $FreeBSD: head/sbin/fsck_ffs/fsck.h 126345 2004-02-28 07:50:42Z scottl $ 441558Srgrimes */ 451558Srgrimes 4623675Speter#include <unistd.h> 4723675Speter#include <stdlib.h> 4823675Speter#include <stdio.h> 4923675Speter 501558Srgrimes#define MAXDUP 10 /* limit on dup blks (per inode) */ 511558Srgrimes#define MAXBAD 10 /* limit on bad blks (per inode) */ 521558Srgrimes#define MAXBUFSPACE 40*1024 /* maximum space to allocate to buffers */ 531558Srgrimes#define INOBUFSIZE 56*1024 /* size of buffer to read inodes in pass1 */ 541558Srgrimes 5598542Smckusickunion dinode { 5698542Smckusick struct ufs1_dinode dp1; 5798542Smckusick struct ufs2_dinode dp2; 5898542Smckusick}; 5998542Smckusick#define DIP(dp, field) \ 6098542Smckusick ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 6198542Smckusick (dp)->dp1.field : (dp)->dp2.field) 6298542Smckusick 6341474Sjulian/* 64102231Strhodes * Each inode on the file system is described by the following structure. 6541474Sjulian * The linkcnt is initially set to the value in the inode. Each time it 6641474Sjulian * is found during the descent in passes 2, 3, and 4 the count is 6741474Sjulian * decremented. Any inodes whose count is non-zero after pass 4 needs to 6841474Sjulian * have its link count adjusted by the value remaining in ino_linkcnt. 6941474Sjulian */ 7041474Sjulianstruct inostat { 7141474Sjulian char ino_state; /* state of inode, see below */ 7241474Sjulian char ino_type; /* type of inode */ 7341474Sjulian short ino_linkcnt; /* number of links not found */ 7441474Sjulian}; 7541474Sjulian/* 7641474Sjulian * Inode states. 7741474Sjulian */ 781558Srgrimes#define USTATE 01 /* inode not allocated */ 791558Srgrimes#define FSTATE 02 /* inode is file */ 801558Srgrimes#define DSTATE 03 /* inode is directory */ 811558Srgrimes#define DFOUND 04 /* directory found during descent */ 821558Srgrimes#define DCLEAR 05 /* directory is to be cleared */ 831558Srgrimes#define FCLEAR 06 /* file is to be cleared */ 8441474Sjulian/* 8541474Sjulian * Inode state information is contained on per cylinder group lists 8641474Sjulian * which are described by the following structure. 8741474Sjulian */ 8841474Sjulianstruct inostatlist { 8941474Sjulian long il_numalloced; /* number of inodes allocated in this cg */ 9041474Sjulian struct inostat *il_stat;/* inostat info for this cylinder group */ 9141474Sjulian} *inostathead; 921558Srgrimes 931558Srgrimes/* 941558Srgrimes * buffer cache structure. 951558Srgrimes */ 961558Srgrimesstruct bufarea { 9723675Speter struct bufarea *b_next; /* free list queue */ 9823675Speter struct bufarea *b_prev; /* free list queue */ 9998542Smckusick ufs2_daddr_t b_bno; 10023675Speter int b_size; 10123675Speter int b_errs; 10223675Speter int b_flags; 1031558Srgrimes union { 10423675Speter char *b_buf; /* buffer space */ 10598542Smckusick ufs1_daddr_t *b_indir1; /* UFS1 indirect block */ 10698542Smckusick ufs2_daddr_t *b_indir2; /* UFS2 indirect block */ 10723675Speter struct fs *b_fs; /* super block */ 10823675Speter struct cg *b_cg; /* cylinder group */ 10998542Smckusick struct ufs1_dinode *b_dinode1; /* UFS1 inode block */ 11098542Smckusick struct ufs2_dinode *b_dinode2; /* UFS2 inode block */ 1111558Srgrimes } b_un; 11223675Speter char b_dirty; 1131558Srgrimes}; 11498542Smckusick#define IBLK(bp, i) \ 11598542Smckusick ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 11698542Smckusick (bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i]) 1171558Srgrimes 1181558Srgrimes#define B_INUSE 1 1191558Srgrimes 1201558Srgrimes#define MINBUFS 5 /* minimum number of buffers required */ 1211558Srgrimesstruct bufarea bufhead; /* head of list of other blks in filesys */ 122102231Strhodesstruct bufarea sblk; /* file system superblock */ 1231558Srgrimesstruct bufarea cgblk; /* cylinder group blocks */ 1241558Srgrimesstruct bufarea *pdirbp; /* current directory contents */ 1251558Srgrimesstruct bufarea *pbp; /* current inode block */ 1261558Srgrimes 12786514Siedowse#define dirty(bp) do { \ 12874556Smckusick if (fswritefd < 0) \ 12974556Smckusick pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \ 13074556Smckusick else \ 13186514Siedowse (bp)->b_dirty = 1; \ 13286514Siedowse} while (0) 13386514Siedowse#define initbarea(bp) do { \ 1341558Srgrimes (bp)->b_dirty = 0; \ 13598542Smckusick (bp)->b_bno = (ufs2_daddr_t)-1; \ 13686514Siedowse (bp)->b_flags = 0; \ 13786514Siedowse} while (0) 1381558Srgrimes 13974556Smckusick#define sbdirty() dirty(&sblk) 14074556Smckusick#define cgdirty() dirty(&cgblk) 1411558Srgrimes#define sblock (*sblk.b_un.b_fs) 1421558Srgrimes#define cgrp (*cgblk.b_un.b_cg) 1431558Srgrimes 1441558Srgrimesenum fixstate {DONTKNOW, NOFIX, FIX, IGNORE}; 14562668Smckusickino_t cursnapshot; 1461558Srgrimes 1471558Srgrimesstruct inodesc { 1481558Srgrimes enum fixstate id_fix; /* policy on fixing errors */ 149100935Sphk int (*id_func)(struct inodesc *); 150100935Sphk /* function to be applied to blocks of inode */ 1511558Srgrimes ino_t id_number; /* inode number described */ 1521558Srgrimes ino_t id_parent; /* for DATA nodes, their parent */ 15398542Smckusick ufs_lbn_t id_lbn; /* logical block number of current block */ 15498542Smckusick ufs2_daddr_t id_blkno; /* current block number being examined */ 1551558Srgrimes int id_numfrags; /* number of frags contained in block */ 15698542Smckusick off_t id_filesize; /* for DATA nodes, the size of the directory */ 15798542Smckusick ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */ 1581558Srgrimes int id_loc; /* for DATA nodes, current location in dir */ 1591558Srgrimes struct direct *id_dirp; /* for DATA nodes, ptr to current entry */ 1601558Srgrimes char *id_name; /* for DATA nodes, name to find or enter */ 1611558Srgrimes char id_type; /* type of descriptor, DATA or ADDR */ 1621558Srgrimes}; 1631558Srgrimes/* file types */ 16462668Smckusick#define DATA 1 /* a directory */ 16562668Smckusick#define SNAP 2 /* a snapshot */ 16662668Smckusick#define ADDR 3 /* anything but a directory or a snapshot */ 1671558Srgrimes 1681558Srgrimes/* 1691558Srgrimes * Linked list of duplicate blocks. 1708871Srgrimes * 1711558Srgrimes * The list is composed of two parts. The first part of the 1721558Srgrimes * list (from duplist through the node pointed to by muldup) 1738871Srgrimes * contains a single copy of each duplicate block that has been 1741558Srgrimes * found. The second part of the list (from muldup to the end) 1751558Srgrimes * contains duplicate blocks that have been found more than once. 1761558Srgrimes * To check if a block has been found as a duplicate it is only 1778871Srgrimes * necessary to search from duplist through muldup. To find the 1781558Srgrimes * total number of times that a block has been found as a duplicate 1791558Srgrimes * the entire list must be searched for occurences of the block 1801558Srgrimes * in question. The following diagram shows a sample list where 1811558Srgrimes * w (found twice), x (found once), y (found three times), and z 1821558Srgrimes * (found once) are duplicate block numbers: 1831558Srgrimes * 1841558Srgrimes * w -> y -> x -> z -> y -> w -> y 1851558Srgrimes * ^ ^ 1861558Srgrimes * | | 1871558Srgrimes * duplist muldup 1881558Srgrimes */ 1891558Srgrimesstruct dups { 1901558Srgrimes struct dups *next; 19198542Smckusick ufs2_daddr_t dup; 1921558Srgrimes}; 1931558Srgrimesstruct dups *duplist; /* head of dup list */ 1941558Srgrimesstruct dups *muldup; /* end of unique duplicate dup block numbers */ 1951558Srgrimes 1961558Srgrimes/* 1971558Srgrimes * Linked list of inodes with zero link counts. 1981558Srgrimes */ 1991558Srgrimesstruct zlncnt { 2001558Srgrimes struct zlncnt *next; 2011558Srgrimes ino_t zlncnt; 2021558Srgrimes}; 2031558Srgrimesstruct zlncnt *zlnhead; /* head of zero link count list */ 2041558Srgrimes 2051558Srgrimes/* 2061558Srgrimes * Inode cache data structures. 2071558Srgrimes */ 2081558Srgrimesstruct inoinfo { 2091558Srgrimes struct inoinfo *i_nexthash; /* next entry in hash chain */ 2101558Srgrimes ino_t i_number; /* inode number of this entry */ 2111558Srgrimes ino_t i_parent; /* inode number of parent */ 2121558Srgrimes ino_t i_dotdot; /* inode number of `..' */ 2131558Srgrimes size_t i_isize; /* size of inode */ 2141558Srgrimes u_int i_numblks; /* size of block array in bytes */ 21598542Smckusick ufs2_daddr_t i_blks[1]; /* actually longer */ 2161558Srgrimes} **inphead, **inpsort; 21757573Smckusicklong numdirs, dirhash, listmax, inplast; 21841474Sjulianlong countdirs; /* number of directories we actually found */ 2191558Srgrimes 22074556Smckusick#define MIBSIZE 3 /* size of fsck sysctl MIBs */ 22174556Smckusickint adjrefcnt[MIBSIZE]; /* MIB command to adjust inode reference cnt */ 22274556Smckusickint adjblkcnt[MIBSIZE]; /* MIB command to adjust inode block count */ 22374556Smckusickint freefiles[MIBSIZE]; /* MIB command to free a set of files */ 22474556Smckusickint freedirs[MIBSIZE]; /* MIB command to free a set of directories */ 22574556Smckusickint freeblks[MIBSIZE]; /* MIB command to free a set of data blocks */ 226102231Strhodesstruct fsck_cmd cmd; /* sysctl file system update commands */ 22774556Smckusickchar snapname[BUFSIZ]; /* when doing snapshots, the name of the file */ 2281558Srgrimeschar *cdevname; /* name of device being checked */ 2291558Srgrimeslong dev_bsize; /* computed value of DEV_BSIZE */ 2301558Srgrimeslong secsize; /* actual disk sector size */ 2311558Srgrimeschar nflag; /* assume a no response */ 2321558Srgrimeschar yflag; /* assume a yes response */ 23374556Smckusickint bkgrdflag; /* use a snapshot to run on an active system */ 2341558Srgrimesint bflag; /* location of alternate super block */ 2351558Srgrimesint debug; /* output debugging info */ 236102231Strhodesint cvtlevel; /* convert to newer file system format */ 23775927Smckusickint bkgrdcheck; /* determine if background check is possible */ 23834266Sjulianchar usedsoftdep; /* just fix soft dependency inconsistencies */ 23941474Sjulianchar preen; /* just fix normal inconsistencies */ 24041474Sjulianchar rerun; /* rerun fsck. Only used in non-preen mode */ 24141474Sjulianint returntosingle; /* 1 => return to single user mode on exit */ 24234266Sjulianchar resolved; /* cleared if unresolved changes => not clean */ 2431558Srgrimeschar havesb; /* superblock has been read */ 244102231Strhodeschar skipclean; /* skip clean file systems if preening */ 245102231Strhodesint fsmodified; /* 1 => write done to file system */ 246102231Strhodesint fsreadfd; /* file descriptor for reading file system */ 247102231Strhodesint fswritefd; /* file descriptor for writing file system */ 2481558Srgrimes 249102231Strhodesufs2_daddr_t maxfsblock; /* number of blocks in the file system */ 2501558Srgrimeschar *blockmap; /* ptr to primary blk allocation map */ 251102231Strhodesino_t maxino; /* number of inodes in file system */ 2521558Srgrimes 2531558Srgrimesino_t lfdir; /* lost & found directory inode number */ 254100935Sphkconst char *lfname; /* lost & found directory name */ 2551558Srgrimesint lfmode; /* lost & found directory creation mode */ 2561558Srgrimes 25798542Smckusickufs2_daddr_t n_blks; /* number of blocks in use */ 25898542Smckusickino_t n_files; /* number of files in use */ 2591558Srgrimes 26070050Siedowseint got_siginfo; /* received a SIGINFO */ 261126345Sscottlint got_sigalarm; /* received a SIGALRM */ 26270050Siedowse 26398542Smckusick#define clearinode(dp) \ 26498542Smckusick if (sblock.fs_magic == FS_UFS1_MAGIC) { \ 26598542Smckusick (dp)->dp1 = ufs1_zino; \ 26698542Smckusick } else { \ 26798542Smckusick (dp)->dp2 = ufs2_zino; \ 26898542Smckusick } 26998542Smckusickstruct ufs1_dinode ufs1_zino; 27098542Smckusickstruct ufs2_dinode ufs2_zino; 2711558Srgrimes 2721558Srgrimes#define setbmap(blkno) setbit(blockmap, blkno) 2731558Srgrimes#define testbmap(blkno) isset(blockmap, blkno) 2741558Srgrimes#define clrbmap(blkno) clrbit(blockmap, blkno) 2751558Srgrimes 2761558Srgrimes#define STOP 0x01 2771558Srgrimes#define SKIP 0x02 2781558Srgrimes#define KEEPON 0x04 2791558Srgrimes#define ALTERED 0x08 2801558Srgrimes#define FOUND 0x10 2811558Srgrimes 28223675Speter#define EEXIT 8 /* Standard error exit. */ 2837585Sbde 28423675Speterstruct fstab; 2857585Sbde 28641474Sjulian 28792839Simpvoid adjust(struct inodesc *, int lcnt); 28898542Smckusickufs2_daddr_t allocblk(long frags); 28992839Simpino_t allocdir(ino_t parent, ino_t request, int mode); 29092839Simpino_t allocino(ino_t request, int type); 291100935Sphkvoid blkerror(ino_t ino, const char *type, ufs2_daddr_t blk); 29292839Simpchar *blockcheck(char *name); 29398542Smckusickint bread(int fd, char *buf, ufs2_daddr_t blk, long size); 29492839Simpvoid bufinit(void); 29598542Smckusickvoid bwrite(int fd, char *buf, ufs2_daddr_t blk, long size); 29698542Smckusickvoid cacheino(union dinode *dp, ino_t inumber); 29792839Simpvoid catch(int); 29892839Simpvoid catchquit(int); 299100935Sphkint changeino(ino_t dir, const char *name, ino_t newnum); 30098542Smckusickint chkrange(ufs2_daddr_t blk, int cnt); 30192839Simpvoid ckfini(int markclean); 30298542Smckusickint ckinode(union dinode *dp, struct inodesc *); 303100935Sphkvoid clri(struct inodesc *, const char *type, int flag); 30492839Simpint clearentry(struct inodesc *); 305100935Sphkvoid direrror(ino_t ino, const char *errmesg); 30692839Simpint dirscan(struct inodesc *); 307100935Sphkint dofix(struct inodesc *, const char *msg); 308103398Sphkint eascan(struct inodesc *, struct ufs2_dinode *dp); 30998542Smckusickvoid ffs_clrblock(struct fs *, u_char *, ufs1_daddr_t); 31092839Simpvoid ffs_fragacct(struct fs *, int, int32_t [], int); 31198542Smckusickint ffs_isblock(struct fs *, u_char *, ufs1_daddr_t); 31298542Smckusickvoid ffs_setblock(struct fs *, u_char *, ufs1_daddr_t); 313100935Sphkvoid fileerror(ino_t cwd, ino_t ino, const char *errmesg); 31492839Simpint findino(struct inodesc *); 31592839Simpint findname(struct inodesc *); 31692839Simpvoid flush(int fd, struct bufarea *bp); 31798542Smckusickvoid freeblk(ufs2_daddr_t blkno, long frags); 31892839Simpvoid freeino(ino_t ino); 31992839Simpvoid freeinodebuf(void); 32098542Smckusickint ftypeok(union dinode *dp); 32198542Smckusickvoid getblk(struct bufarea *bp, ufs2_daddr_t blk, long size); 32298542Smckusickstruct bufarea *getdatablk(ufs2_daddr_t blkno, long size); 32392839Simpstruct inoinfo *getinoinfo(ino_t inumber); 32498542Smckusickunion dinode *getnextinode(ino_t inumber); 32592839Simpvoid getpathname(char *namebuf, ino_t curdir, ino_t ino); 32698542Smckusickunion dinode *ginode(ino_t inumber); 32792839Simpvoid infohandler(int sig); 328126345Sscottlvoid alarmhandler(int sig); 32992839Simpvoid inocleanup(void); 33092839Simpvoid inodirty(void); 33192839Simpstruct inostat *inoinfo(ino_t inum); 33292839Simpint linkup(ino_t orphan, ino_t parentdir, char *name); 333100935Sphkint makeentry(ino_t parent, ino_t ino, const char *name); 33492839Simpvoid panic(const char *fmt, ...) __printflike(1, 2); 33592839Simpvoid pass1(void); 33692839Simpvoid pass1b(void); 33792839Simpint pass1check(struct inodesc *); 33892839Simpvoid pass2(void); 33992839Simpvoid pass3(void); 34092839Simpvoid pass4(void); 34192839Simpint pass4check(struct inodesc *); 34292839Simpvoid pass5(void); 34392839Simpvoid pfatal(const char *fmt, ...) __printflike(1, 2); 34492839Simpvoid pinode(ino_t ino); 34592839Simpvoid propagate(void); 34692839Simpvoid pwarn(const char *fmt, ...) __printflike(1, 2); 34792839Simpint readsb(int listerr); 348100935Sphkint reply(const char *question); 349100935Sphkvoid rwerror(const char *mesg, ufs2_daddr_t blk); 35092839Simpvoid sblock_init(void); 35192839Simpvoid setinodebuf(ino_t); 35292839Simpint setup(char *dev); 353