fsck.h revision 134589
1139804Simp/* 2139013Sdavidxu * Copyright (c) 2002 Networks Associates Technology, Inc. 3112904Sjeff * All rights reserved. 4112904Sjeff * 5112904Sjeff * This software was developed for the FreeBSD Project by Marshall 6112904Sjeff * Kirk McKusick and Network Associates Laboratories, the Security 7112904Sjeff * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8112904Sjeff * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9112904Sjeff * research program. 10112904Sjeff * 11112904Sjeff * Copyright (c) 1980, 1986, 1993 12112904Sjeff * The Regents of the University of California. All rights reserved. 13112904Sjeff * 14112904Sjeff * Redistribution and use in source and binary forms, with or without 15112904Sjeff * modification, are permitted provided that the following conditions 16112904Sjeff * are met: 17112904Sjeff * 1. Redistributions of source code must retain the above copyright 18112904Sjeff * notice, this list of conditions and the following disclaimer. 19112904Sjeff * 2. Redistributions in binary form must reproduce the above copyright 20112904Sjeff * notice, this list of conditions and the following disclaimer in the 21112904Sjeff * documentation and/or other materials provided with the distribution. 22112904Sjeff * 4. Neither the name of the University nor the names of its contributors 23112904Sjeff * may be used to endorse or promote products derived from this software 24112904Sjeff * without specific prior written permission. 25112904Sjeff * 26112904Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27112904Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28116182Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29116182Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30116182Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31162536Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32112904Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33112904Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34131431Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35112904Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36115765Sjeff * SUCH DAMAGE. 37112904Sjeff * 38164033Srwatson * @(#)fsck.h 8.4 (Berkeley) 5/9/95 39112904Sjeff * $FreeBSD: head/sbin/fsck_ffs/fsck.h 134589 2004-09-01 05:48:06Z scottl $ 40161678Sdavidxu */ 41165369Sdavidxu 42161678Sdavidxu#include <unistd.h> 43112904Sjeff#include <stdlib.h> 44112904Sjeff#include <stdio.h> 45112904Sjeff 46139013Sdavidxu#define MAXDUP 10 /* limit on dup blks (per inode) */ 47112904Sjeff#define MAXBAD 10 /* limit on bad blks (per inode) */ 48112904Sjeff#define MAXBUFSPACE 40*1024 /* maximum space to allocate to buffers */ 49139013Sdavidxu#define INOBUFSIZE 56*1024 /* size of buffer to read inodes in pass1 */ 50139013Sdavidxu 51139013Sdavidxuunion dinode { 52139013Sdavidxu struct ufs1_dinode dp1; 53139013Sdavidxu struct ufs2_dinode dp2; 54139013Sdavidxu}; 55165369Sdavidxu#define DIP(dp, field) \ 56165369Sdavidxu ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 57162536Sdavidxu (dp)->dp1.field : (dp)->dp2.field) 58162536Sdavidxu 59162536Sdavidxu#define DIP_SET(dp, field, val) do { \ 60162536Sdavidxu if (sblock.fs_magic == FS_UFS1_MAGIC) \ 61161678Sdavidxu (dp)->dp1.field = (val); \ 62161678Sdavidxu else \ 63161678Sdavidxu (dp)->dp2.field = (val); \ 64161678Sdavidxu } while (0) 65161678Sdavidxu 66161678Sdavidxu/* 67177848Sdavidxu * Each inode on the file system is described by the following structure. 68139013Sdavidxu * The linkcnt is initially set to the value in the inode. Each time it 69161678Sdavidxu * is found during the descent in passes 2, 3, and 4 the count is 70139013Sdavidxu * decremented. Any inodes whose count is non-zero after pass 4 needs to 71161678Sdavidxu * have its link count adjusted by the value remaining in ino_linkcnt. 72139013Sdavidxu */ 73161678Sdavidxustruct inostat { 74139013Sdavidxu char ino_state; /* state of inode, see below */ 75139013Sdavidxu char ino_type; /* type of inode */ 76139013Sdavidxu short ino_linkcnt; /* number of links not found */ 77161678Sdavidxu}; 78139013Sdavidxu/* 79139013Sdavidxu * Inode states. 80161678Sdavidxu */ 81161678Sdavidxu#define USTATE 01 /* inode not allocated */ 82139013Sdavidxu#define FSTATE 02 /* inode is file */ 83139013Sdavidxu#define DSTATE 03 /* inode is directory */ 84161678Sdavidxu#define DFOUND 04 /* directory found during descent */ 85161678Sdavidxu#define DCLEAR 05 /* directory is to be cleared */ 86139013Sdavidxu#define FCLEAR 06 /* file is to be cleared */ 87139013Sdavidxu/* 88139013Sdavidxu * Inode state information is contained on per cylinder group lists 89139013Sdavidxu * which are described by the following structure. 90161678Sdavidxu */ 91161678Sdavidxustruct inostatlist { 92161678Sdavidxu long il_numalloced; /* number of inodes allocated in this cg */ 93161678Sdavidxu struct inostat *il_stat;/* inostat info for this cylinder group */ 94161678Sdavidxu} *inostathead; 95161678Sdavidxu 96161678Sdavidxu/* 97161678Sdavidxu * buffer cache structure. 98161678Sdavidxu */ 99161678Sdavidxustruct bufarea { 100161678Sdavidxu struct bufarea *b_next; /* free list queue */ 101161678Sdavidxu struct bufarea *b_prev; /* free list queue */ 102161678Sdavidxu ufs2_daddr_t b_bno; 103161678Sdavidxu int b_size; 104161678Sdavidxu int b_errs; 105161678Sdavidxu int b_flags; 106161678Sdavidxu union { 107161678Sdavidxu char *b_buf; /* buffer space */ 108161678Sdavidxu ufs1_daddr_t *b_indir1; /* UFS1 indirect block */ 109161678Sdavidxu ufs2_daddr_t *b_indir2; /* UFS2 indirect block */ 110161678Sdavidxu struct fs *b_fs; /* super block */ 111161678Sdavidxu struct cg *b_cg; /* cylinder group */ 112115765Sjeff struct ufs1_dinode *b_dinode1; /* UFS1 inode block */ 113161678Sdavidxu struct ufs2_dinode *b_dinode2; /* UFS2 inode block */ 114161678Sdavidxu } b_un; 115161678Sdavidxu char b_dirty; 116161678Sdavidxu}; 117161678Sdavidxu 118161678Sdavidxu#define IBLK(bp, i) \ 119161678Sdavidxu ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 120161678Sdavidxu (bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i]) 121161678Sdavidxu 122161678Sdavidxu#define IBLK_SET(bp, i, val) do { \ 123161678Sdavidxu if (sblock.fs_magic == FS_UFS1_MAGIC) \ 124161678Sdavidxu (bp)->b_un.b_indir1[i] = (val); \ 125161678Sdavidxu else \ 126161678Sdavidxu (bp)->b_un.b_indir2[i] = (val); \ 127161678Sdavidxu } while (0) 128170300Sjeff 129170300Sjeff#define B_INUSE 1 130161678Sdavidxu 131161678Sdavidxu#define MINBUFS 5 /* minimum number of buffers required */ 132161678Sdavidxustruct bufarea bufhead; /* head of list of other blks in filesys */ 133161678Sdavidxustruct bufarea sblk; /* file system superblock */ 134161678Sdavidxustruct bufarea cgblk; /* cylinder group blocks */ 135161678Sdavidxustruct bufarea *pdirbp; /* current directory contents */ 136161678Sdavidxustruct bufarea *pbp; /* current inode block */ 137161678Sdavidxu 138161678Sdavidxu#define dirty(bp) do { \ 139161742Sdavidxu if (fswritefd < 0) \ 140161678Sdavidxu pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \ 141115765Sjeff else \ 142115765Sjeff (bp)->b_dirty = 1; \ 143161678Sdavidxu} while (0) 144161678Sdavidxu#define initbarea(bp) do { \ 145161678Sdavidxu (bp)->b_dirty = 0; \ 146138224Sdavidxu (bp)->b_bno = (ufs2_daddr_t)-1; \ 147161678Sdavidxu (bp)->b_flags = 0; \ 148161678Sdavidxu} while (0) 149161678Sdavidxu 150161678Sdavidxu#define sbdirty() dirty(&sblk) 151177848Sdavidxu#define cgdirty() dirty(&cgblk) 152177848Sdavidxu#define sblock (*sblk.b_un.b_fs) 153177848Sdavidxu#define cgrp (*cgblk.b_un.b_cg) 154161678Sdavidxu 155161678Sdavidxuenum fixstate {DONTKNOW, NOFIX, FIX, IGNORE}; 156161678Sdavidxuino_t cursnapshot; 157161678Sdavidxu 158161678Sdavidxustruct inodesc { 159158377Sdavidxu enum fixstate id_fix; /* policy on fixing errors */ 160161678Sdavidxu int (*id_func)(struct inodesc *); 161161678Sdavidxu /* function to be applied to blocks of inode */ 162161678Sdavidxu ino_t id_number; /* inode number described */ 163138224Sdavidxu ino_t id_parent; /* for DATA nodes, their parent */ 164115765Sjeff ufs_lbn_t id_lbn; /* logical block number of current block */ 165161678Sdavidxu ufs2_daddr_t id_blkno; /* current block number being examined */ 166161678Sdavidxu int id_numfrags; /* number of frags contained in block */ 167161678Sdavidxu off_t id_filesize; /* for DATA nodes, the size of the directory */ 168161678Sdavidxu ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */ 169161678Sdavidxu int id_loc; /* for DATA nodes, current location in dir */ 170161678Sdavidxu struct direct *id_dirp; /* for DATA nodes, ptr to current entry */ 171161678Sdavidxu char *id_name; /* for DATA nodes, name to find or enter */ 172161678Sdavidxu char id_type; /* type of descriptor, DATA or ADDR */ 173161678Sdavidxu}; 174161678Sdavidxu/* file types */ 175161678Sdavidxu#define DATA 1 /* a directory */ 176163709Sjb#define SNAP 2 /* a snapshot */ 177163709Sjb#define ADDR 3 /* anything but a directory or a snapshot */ 178163709Sjb 179161678Sdavidxu/* 180138224Sdavidxu * Linked list of duplicate blocks. 181138224Sdavidxu * 182138224Sdavidxu * The list is composed of two parts. The first part of the 183115765Sjeff * list (from duplist through the node pointed to by muldup) 184161678Sdavidxu * contains a single copy of each duplicate block that has been 185161678Sdavidxu * found. The second part of the list (from muldup to the end) 186161678Sdavidxu * contains duplicate blocks that have been found more than once. 187161678Sdavidxu * To check if a block has been found as a duplicate it is only 188161678Sdavidxu * necessary to search from duplist through muldup. To find the 189161678Sdavidxu * total number of times that a block has been found as a duplicate 190161678Sdavidxu * the entire list must be searched for occurences of the block 191177848Sdavidxu * in question. The following diagram shows a sample list where 192177848Sdavidxu * w (found twice), x (found once), y (found three times), and z 193161678Sdavidxu * (found once) are duplicate block numbers: 194161678Sdavidxu * 195138224Sdavidxu * w -> y -> x -> z -> y -> w -> y 196161678Sdavidxu * ^ ^ 197115310Sjeff * | | 198161678Sdavidxu * duplist muldup 199161678Sdavidxu */ 200161678Sdavidxustruct dups { 201161678Sdavidxu struct dups *next; 202161678Sdavidxu ufs2_daddr_t dup; 203161678Sdavidxu}; 204161678Sdavidxustruct dups *duplist; /* head of dup list */ 205139013Sdavidxustruct dups *muldup; /* end of unique duplicate dup block numbers */ 206139013Sdavidxu 207139257Sdavidxu/* 208139257Sdavidxu * Linked list of inodes with zero link counts. 209177848Sdavidxu */ 210177848Sdavidxustruct zlncnt { 211161678Sdavidxu struct zlncnt *next; 212139257Sdavidxu ino_t zlncnt; 213139013Sdavidxu}; 214161678Sdavidxustruct zlncnt *zlnhead; /* head of zero link count list */ 215139013Sdavidxu 216139013Sdavidxu/* 217163697Sdavidxu * Inode cache data structures. 218161678Sdavidxu */ 219174701Sdavidxustruct inoinfo { 220161678Sdavidxu struct inoinfo *i_nexthash; /* next entry in hash chain */ 221161678Sdavidxu ino_t i_number; /* inode number of this entry */ 222161678Sdavidxu ino_t i_parent; /* inode number of parent */ 223161678Sdavidxu ino_t i_dotdot; /* inode number of `..' */ 224161678Sdavidxu size_t i_isize; /* size of inode */ 225115310Sjeff u_int i_numblks; /* size of block array in bytes */ 226177848Sdavidxu ufs2_daddr_t i_blks[1]; /* actually longer */ 227177848Sdavidxu} **inphead, **inpsort; 228177848Sdavidxulong numdirs, dirhash, listmax, inplast; 229177848Sdavidxulong countdirs; /* number of directories we actually found */ 230170300Sjeff 231170300Sjeff#define MIBSIZE 3 /* size of fsck sysctl MIBs */ 232161678Sdavidxuint adjrefcnt[MIBSIZE]; /* MIB command to adjust inode reference cnt */ 233161678Sdavidxuint adjblkcnt[MIBSIZE]; /* MIB command to adjust inode block count */ 234161678Sdavidxuint freefiles[MIBSIZE]; /* MIB command to free a set of files */ 235161678Sdavidxuint freedirs[MIBSIZE]; /* MIB command to free a set of directories */ 236138224Sdavidxuint freeblks[MIBSIZE]; /* MIB command to free a set of data blocks */ 237161678Sdavidxustruct fsck_cmd cmd; /* sysctl file system update commands */ 238161678Sdavidxuchar snapname[BUFSIZ]; /* when doing snapshots, the name of the file */ 239161678Sdavidxuchar *cdevname; /* name of device being checked */ 240161678Sdavidxulong dev_bsize; /* computed value of DEV_BSIZE */ 241161678Sdavidxulong secsize; /* actual disk sector size */ 242177848Sdavidxuchar nflag; /* assume a no response */ 243177848Sdavidxuchar yflag; /* assume a yes response */ 244161678Sdavidxuint bkgrdflag; /* use a snapshot to run on an active system */ 245161678Sdavidxuint bflag; /* location of alternate super block */ 246161678Sdavidxuint debug; /* output debugging info */ 247161678Sdavidxuint cvtlevel; /* convert to newer file system format */ 248170300Sjeffint bkgrdcheck; /* determine if background check is possible */ 249161678Sdavidxuchar usedsoftdep; /* just fix soft dependency inconsistencies */ 250161678Sdavidxuchar preen; /* just fix normal inconsistencies */ 251161678Sdavidxuchar rerun; /* rerun fsck. Only used in non-preen mode */ 252161678Sdavidxuint returntosingle; /* 1 => return to single user mode on exit */ 253143149Sdavidxuchar resolved; /* cleared if unresolved changes => not clean */ 254143149Sdavidxuchar havesb; /* superblock has been read */ 255143149Sdavidxuchar skipclean; /* skip clean file systems if preening */ 256161678Sdavidxuint fsmodified; /* 1 => write done to file system */ 257161678Sdavidxuint fsreadfd; /* file descriptor for reading file system */ 258161678Sdavidxuint fswritefd; /* file descriptor for writing file system */ 259161678Sdavidxu 260161678Sdavidxuufs2_daddr_t maxfsblock; /* number of blocks in the file system */ 261161678Sdavidxuchar *blockmap; /* ptr to primary blk allocation map */ 262143149Sdavidxuino_t maxino; /* number of inodes in file system */ 263143149Sdavidxu 264143149Sdavidxuino_t lfdir; /* lost & found directory inode number */ 265143149Sdavidxuconst char *lfname; /* lost & found directory name */ 266143149Sdavidxuint lfmode; /* lost & found directory creation mode */ 267143149Sdavidxu 268143149Sdavidxuufs2_daddr_t n_blks; /* number of blocks in use */ 269143149Sdavidxuino_t n_files; /* number of files in use */ 270161678Sdavidxu 271139013Sdavidxuint got_siginfo; /* received a SIGINFO */ 272138224Sdavidxuint got_sigalarm; /* received a SIGALRM */ 273161678Sdavidxu 274161678Sdavidxu#define clearinode(dp) \ 275138224Sdavidxu if (sblock.fs_magic == FS_UFS1_MAGIC) { \ 276138224Sdavidxu (dp)->dp1 = ufs1_zino; \ 277139013Sdavidxu } else { \ 278139013Sdavidxu (dp)->dp2 = ufs2_zino; \ 279139013Sdavidxu } 280139013Sdavidxustruct ufs1_dinode ufs1_zino; 281161678Sdavidxustruct ufs2_dinode ufs2_zino; 282161678Sdavidxu 283139013Sdavidxu#define setbmap(blkno) setbit(blockmap, blkno) 284139013Sdavidxu#define testbmap(blkno) isset(blockmap, blkno) 285161678Sdavidxu#define clrbmap(blkno) clrbit(blockmap, blkno) 286161678Sdavidxu 287139013Sdavidxu#define STOP 0x01 288161678Sdavidxu#define SKIP 0x02 289139013Sdavidxu#define KEEPON 0x04 290139013Sdavidxu#define ALTERED 0x08 291161678Sdavidxu#define FOUND 0x10 292177848Sdavidxu 293161678Sdavidxu#define EEXIT 8 /* Standard error exit. */ 294138224Sdavidxu 295177848Sdavidxustruct fstab; 296139257Sdavidxu 297161678Sdavidxu 298139257Sdavidxuvoid adjust(struct inodesc *, int lcnt); 299161678Sdavidxuufs2_daddr_t allocblk(long frags); 300177848Sdavidxuino_t allocdir(ino_t parent, ino_t request, int mode); 301139257Sdavidxuino_t allocino(ino_t request, int type); 302139257Sdavidxuvoid blkerror(ino_t ino, const char *type, ufs2_daddr_t blk); 303161678Sdavidxuchar *blockcheck(char *name); 304177848Sdavidxuint bread(int fd, char *buf, ufs2_daddr_t blk, long size); 305161678Sdavidxuvoid bufinit(void); 306139257Sdavidxuvoid bwrite(int fd, char *buf, ufs2_daddr_t blk, long size); 307177848Sdavidxuvoid cacheino(union dinode *dp, ino_t inumber); 308139257Sdavidxuvoid catch(int); 309161678Sdavidxuvoid catchquit(int); 310139257Sdavidxuint changeino(ino_t dir, const char *name, ino_t newnum); 311161678Sdavidxuint chkrange(ufs2_daddr_t blk, int cnt); 312177848Sdavidxuvoid ckfini(int markclean); 313139257Sdavidxuint ckinode(union dinode *dp, struct inodesc *); 314139257Sdavidxuvoid clri(struct inodesc *, const char *type, int flag); 315161678Sdavidxuint clearentry(struct inodesc *); 316177848Sdavidxuvoid direrror(ino_t ino, const char *errmesg); 317177848Sdavidxuint dirscan(struct inodesc *); 318161678Sdavidxuint dofix(struct inodesc *, const char *msg); 319139257Sdavidxuint eascan(struct inodesc *, struct ufs2_dinode *dp); 320177848Sdavidxuvoid ffs_clrblock(struct fs *, u_char *, ufs1_daddr_t); 321138224Sdavidxuvoid ffs_fragacct(struct fs *, int, int32_t [], int); 322161678Sdavidxuint ffs_isblock(struct fs *, u_char *, ufs1_daddr_t); 323161678Sdavidxuvoid ffs_setblock(struct fs *, u_char *, ufs1_daddr_t); 324161678Sdavidxuvoid fileerror(ino_t cwd, ino_t ino, const char *errmesg); 325177848Sdavidxuint findino(struct inodesc *); 326177848Sdavidxuint findname(struct inodesc *); 327177848Sdavidxuvoid flush(int fd, struct bufarea *bp); 328177848Sdavidxuvoid freeblk(ufs2_daddr_t blkno, long frags); 329177848Sdavidxuvoid freeino(ino_t ino); 330177848Sdavidxuvoid freeinodebuf(void); 331177848Sdavidxuint ftypeok(union dinode *dp); 332177848Sdavidxuvoid getblk(struct bufarea *bp, ufs2_daddr_t blk, long size); 333177848Sdavidxustruct bufarea *getdatablk(ufs2_daddr_t blkno, long size); 334177848Sdavidxustruct inoinfo *getinoinfo(ino_t inumber); 335177848Sdavidxuunion dinode *getnextinode(ino_t inumber); 336177848Sdavidxuvoid getpathname(char *namebuf, ino_t curdir, ino_t ino); 337177848Sdavidxuunion dinode *ginode(ino_t inumber); 338177848Sdavidxuvoid infohandler(int sig); 339177848Sdavidxuvoid alarmhandler(int sig); 340177848Sdavidxuvoid inocleanup(void); 341138224Sdavidxuvoid inodirty(void); 342138224Sdavidxustruct inostat *inoinfo(ino_t inum); 343161678Sdavidxuint linkup(ino_t orphan, ino_t parentdir, char *name); 344177848Sdavidxuint makeentry(ino_t parent, ino_t ino, const char *name); 345161678Sdavidxuvoid panic(const char *fmt, ...) __printflike(1, 2); 346138225Sdavidxuvoid pass1(void); 347177848Sdavidxuvoid pass1b(void); 348138224Sdavidxuint pass1check(struct inodesc *); 349161678Sdavidxuvoid pass2(void); 350161678Sdavidxuvoid pass3(void); 351161678Sdavidxuvoid pass4(void); 352177848Sdavidxuint pass4check(struct inodesc *); 353177848Sdavidxuvoid pass5(void); 354177848Sdavidxuvoid pfatal(const char *fmt, ...) __printflike(1, 2); 355177848Sdavidxuvoid pinode(ino_t ino); 356177848Sdavidxuvoid propagate(void); 357138224Sdavidxuvoid pwarn(const char *fmt, ...) __printflike(1, 2); 358138224Sdavidxuint readsb(int listerr); 359139013Sdavidxuint reply(const char *question); 360177848Sdavidxuvoid rwerror(const char *mesg, ufs2_daddr_t blk); 361115765Sjeffvoid sblock_init(void); 362161678Sdavidxuvoid setinodebuf(ino_t); 363139013Sdavidxuint setup(char *dev); 364161678Sdavidxu