fsck.h revision 229256
1/* 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by Marshall 6 * Kirk McKusick and Network Associates Laboratories, the Security 7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9 * research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * Copyright (c) 1980, 1986, 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 4. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 * 59 * @(#)fsck.h 8.4 (Berkeley) 5/9/95 60 * $FreeBSD: stable/9/sbin/fsck_ffs/fsck.h 229256 2012-01-02 00:04:51Z kib $ 61 */ 62 63#ifndef _FSCK_H_ 64#define _FSCK_H_ 65 66#include <unistd.h> 67#include <stdlib.h> 68#include <stdio.h> 69 70#define MAXDUP 10 /* limit on dup blks (per inode) */ 71#define MAXBAD 10 /* limit on bad blks (per inode) */ 72#define MAXBUFSPACE 40*1024 /* maximum space to allocate to buffers */ 73#define INOBUFSIZE 56*1024 /* size of buffer to read inodes in pass1 */ 74 75union dinode { 76 struct ufs1_dinode dp1; 77 struct ufs2_dinode dp2; 78}; 79#define DIP(dp, field) \ 80 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 81 (dp)->dp1.field : (dp)->dp2.field) 82 83#define DIP_SET(dp, field, val) do { \ 84 if (sblock.fs_magic == FS_UFS1_MAGIC) \ 85 (dp)->dp1.field = (val); \ 86 else \ 87 (dp)->dp2.field = (val); \ 88 } while (0) 89 90/* 91 * Each inode on the file system is described by the following structure. 92 * The linkcnt is initially set to the value in the inode. Each time it 93 * is found during the descent in passes 2, 3, and 4 the count is 94 * decremented. Any inodes whose count is non-zero after pass 4 needs to 95 * have its link count adjusted by the value remaining in ino_linkcnt. 96 */ 97struct inostat { 98 char ino_state; /* state of inode, see below */ 99 char ino_type; /* type of inode */ 100 short ino_linkcnt; /* number of links not found */ 101}; 102/* 103 * Inode states. 104 */ 105#define USTATE 0x1 /* inode not allocated */ 106#define FSTATE 0x2 /* inode is file */ 107#define FZLINK 0x3 /* inode is file with a link count of zero */ 108#define DSTATE 0x4 /* inode is directory */ 109#define DZLINK 0x5 /* inode is directory with a zero link count */ 110#define DFOUND 0x6 /* directory found during descent */ 111/* 0x7 UNUSED - see S_IS_DVALID() definition */ 112#define DCLEAR 0x8 /* directory is to be cleared */ 113#define FCLEAR 0x9 /* file is to be cleared */ 114/* DUNFOUND === (state == DSTATE || state == DZLINK) */ 115#define S_IS_DUNFOUND(state) (((state) & ~0x1) == DSTATE) 116/* DVALID === (state == DSTATE || state == DZLINK || state == DFOUND) */ 117#define S_IS_DVALID(state) (((state) & ~0x3) == DSTATE) 118#define INO_IS_DUNFOUND(ino) S_IS_DUNFOUND(inoinfo(ino)->ino_state) 119#define INO_IS_DVALID(ino) S_IS_DVALID(inoinfo(ino)->ino_state) 120/* 121 * Inode state information is contained on per cylinder group lists 122 * which are described by the following structure. 123 */ 124struct inostatlist { 125 long il_numalloced; /* number of inodes allocated in this cg */ 126 struct inostat *il_stat;/* inostat info for this cylinder group */ 127} *inostathead; 128 129/* 130 * buffer cache structure. 131 */ 132struct bufarea { 133 struct bufarea *b_next; /* free list queue */ 134 struct bufarea *b_prev; /* free list queue */ 135 ufs2_daddr_t b_bno; 136 int b_size; 137 int b_errs; 138 int b_flags; 139 union { 140 char *b_buf; /* buffer space */ 141 ufs1_daddr_t *b_indir1; /* UFS1 indirect block */ 142 ufs2_daddr_t *b_indir2; /* UFS2 indirect block */ 143 struct fs *b_fs; /* super block */ 144 struct cg *b_cg; /* cylinder group */ 145 struct ufs1_dinode *b_dinode1; /* UFS1 inode block */ 146 struct ufs2_dinode *b_dinode2; /* UFS2 inode block */ 147 } b_un; 148 char b_dirty; 149}; 150 151#define IBLK(bp, i) \ 152 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 153 (bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i]) 154 155#define IBLK_SET(bp, i, val) do { \ 156 if (sblock.fs_magic == FS_UFS1_MAGIC) \ 157 (bp)->b_un.b_indir1[i] = (val); \ 158 else \ 159 (bp)->b_un.b_indir2[i] = (val); \ 160 } while (0) 161 162#define B_INUSE 1 163 164#define MINBUFS 5 /* minimum number of buffers required */ 165struct bufarea bufhead; /* head of list of other blks in filesys */ 166struct bufarea sblk; /* file system superblock */ 167struct bufarea cgblk; /* cylinder group blocks */ 168struct bufarea *pdirbp; /* current directory contents */ 169struct bufarea *pbp; /* current inode block */ 170 171#define dirty(bp) do { \ 172 if (fswritefd < 0) \ 173 pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \ 174 else \ 175 (bp)->b_dirty = 1; \ 176} while (0) 177#define initbarea(bp) do { \ 178 (bp)->b_dirty = 0; \ 179 (bp)->b_bno = (ufs2_daddr_t)-1; \ 180 (bp)->b_flags = 0; \ 181} while (0) 182 183#define sbdirty() dirty(&sblk) 184#define cgdirty() dirty(&cgblk) 185#define sblock (*sblk.b_un.b_fs) 186#define cgrp (*cgblk.b_un.b_cg) 187 188enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE}; 189ino_t cursnapshot; 190 191struct inodesc { 192 enum fixstate id_fix; /* policy on fixing errors */ 193 int (*id_func)(struct inodesc *); 194 /* function to be applied to blocks of inode */ 195 ino_t id_number; /* inode number described */ 196 ino_t id_parent; /* for DATA nodes, their parent */ 197 ufs_lbn_t id_lbn; /* logical block number of current block */ 198 ufs2_daddr_t id_blkno; /* current block number being examined */ 199 int id_numfrags; /* number of frags contained in block */ 200 off_t id_filesize; /* for DATA nodes, the size of the directory */ 201 ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */ 202 int id_loc; /* for DATA nodes, current location in dir */ 203 struct direct *id_dirp; /* for DATA nodes, ptr to current entry */ 204 char *id_name; /* for DATA nodes, name to find or enter */ 205 char id_type; /* type of descriptor, DATA or ADDR */ 206}; 207/* file types */ 208#define DATA 1 /* a directory */ 209#define SNAP 2 /* a snapshot */ 210#define ADDR 3 /* anything but a directory or a snapshot */ 211 212/* 213 * Linked list of duplicate blocks. 214 * 215 * The list is composed of two parts. The first part of the 216 * list (from duplist through the node pointed to by muldup) 217 * contains a single copy of each duplicate block that has been 218 * found. The second part of the list (from muldup to the end) 219 * contains duplicate blocks that have been found more than once. 220 * To check if a block has been found as a duplicate it is only 221 * necessary to search from duplist through muldup. To find the 222 * total number of times that a block has been found as a duplicate 223 * the entire list must be searched for occurences of the block 224 * in question. The following diagram shows a sample list where 225 * w (found twice), x (found once), y (found three times), and z 226 * (found once) are duplicate block numbers: 227 * 228 * w -> y -> x -> z -> y -> w -> y 229 * ^ ^ 230 * | | 231 * duplist muldup 232 */ 233struct dups { 234 struct dups *next; 235 ufs2_daddr_t dup; 236}; 237struct dups *duplist; /* head of dup list */ 238struct dups *muldup; /* end of unique duplicate dup block numbers */ 239 240/* 241 * Inode cache data structures. 242 */ 243struct inoinfo { 244 struct inoinfo *i_nexthash; /* next entry in hash chain */ 245 ino_t i_number; /* inode number of this entry */ 246 ino_t i_parent; /* inode number of parent */ 247 ino_t i_dotdot; /* inode number of `..' */ 248 size_t i_isize; /* size of inode */ 249 u_int i_numblks; /* size of block array in bytes */ 250 ufs2_daddr_t i_blks[1]; /* actually longer */ 251} **inphead, **inpsort; 252long numdirs, dirhash, listmax, inplast; 253long countdirs; /* number of directories we actually found */ 254 255#define MIBSIZE 3 /* size of fsck sysctl MIBs */ 256int adjrefcnt[MIBSIZE]; /* MIB command to adjust inode reference cnt */ 257int adjblkcnt[MIBSIZE]; /* MIB command to adjust inode block count */ 258int adjndir[MIBSIZE]; /* MIB command to adjust number of directories */ 259int adjnbfree[MIBSIZE]; /* MIB command to adjust number of free blocks */ 260int adjnifree[MIBSIZE]; /* MIB command to adjust number of free inodes */ 261int adjnffree[MIBSIZE]; /* MIB command to adjust number of free frags */ 262int adjnumclusters[MIBSIZE]; /* MIB command to adjust number of free clusters */ 263int freefiles[MIBSIZE]; /* MIB command to free a set of files */ 264int freedirs[MIBSIZE]; /* MIB command to free a set of directories */ 265int freeblks[MIBSIZE]; /* MIB command to free a set of data blocks */ 266struct fsck_cmd cmd; /* sysctl file system update commands */ 267char snapname[BUFSIZ]; /* when doing snapshots, the name of the file */ 268char *cdevname; /* name of device being checked */ 269long dev_bsize; /* computed value of DEV_BSIZE */ 270long secsize; /* actual disk sector size */ 271u_int real_dev_bsize; /* actual disk sector size, not overriden */ 272char nflag; /* assume a no response */ 273char yflag; /* assume a yes response */ 274int bkgrdflag; /* use a snapshot to run on an active system */ 275int bflag; /* location of alternate super block */ 276int debug; /* output debugging info */ 277int Eflag; /* zero out empty data blocks */ 278int inoopt; /* trim out unused inodes */ 279char ckclean; /* only do work if not cleanly unmounted */ 280int cvtlevel; /* convert to newer file system format */ 281int bkgrdcheck; /* determine if background check is possible */ 282int bkgrdsumadj; /* whether the kernel have ability to adjust superblock summary */ 283char usedsoftdep; /* just fix soft dependency inconsistencies */ 284char preen; /* just fix normal inconsistencies */ 285char rerun; /* rerun fsck. Only used in non-preen mode */ 286int returntosingle; /* 1 => return to single user mode on exit */ 287char resolved; /* cleared if unresolved changes => not clean */ 288char havesb; /* superblock has been read */ 289char skipclean; /* skip clean file systems if preening */ 290int fsmodified; /* 1 => write done to file system */ 291int fsreadfd; /* file descriptor for reading file system */ 292int fswritefd; /* file descriptor for writing file system */ 293 294ufs2_daddr_t maxfsblock; /* number of blocks in the file system */ 295char *blockmap; /* ptr to primary blk allocation map */ 296ino_t maxino; /* number of inodes in file system */ 297 298ino_t lfdir; /* lost & found directory inode number */ 299const char *lfname; /* lost & found directory name */ 300int lfmode; /* lost & found directory creation mode */ 301 302ufs2_daddr_t n_blks; /* number of blocks in use */ 303ino_t n_files; /* number of files in use */ 304 305volatile sig_atomic_t got_siginfo; /* received a SIGINFO */ 306volatile sig_atomic_t got_sigalarm; /* received a SIGALRM */ 307 308#define clearinode(dp) \ 309 if (sblock.fs_magic == FS_UFS1_MAGIC) { \ 310 (dp)->dp1 = ufs1_zino; \ 311 } else { \ 312 (dp)->dp2 = ufs2_zino; \ 313 } 314struct ufs1_dinode ufs1_zino; 315struct ufs2_dinode ufs2_zino; 316 317#define setbmap(blkno) setbit(blockmap, blkno) 318#define testbmap(blkno) isset(blockmap, blkno) 319#define clrbmap(blkno) clrbit(blockmap, blkno) 320 321#define STOP 0x01 322#define SKIP 0x02 323#define KEEPON 0x04 324#define ALTERED 0x08 325#define FOUND 0x10 326 327#define EEXIT 8 /* Standard error exit. */ 328 329struct fstab; 330 331 332void adjust(struct inodesc *, int lcnt); 333ufs2_daddr_t allocblk(long frags); 334ino_t allocdir(ino_t parent, ino_t request, int mode); 335ino_t allocino(ino_t request, int type); 336void blkerror(ino_t ino, const char *type, ufs2_daddr_t blk); 337char *blockcheck(char *name); 338int blread(int fd, char *buf, ufs2_daddr_t blk, long size); 339void bufinit(void); 340void blwrite(int fd, char *buf, ufs2_daddr_t blk, long size); 341void blerase(int fd, ufs2_daddr_t blk, long size); 342void cacheino(union dinode *dp, ino_t inumber); 343void catch(int); 344void catchquit(int); 345int changeino(ino_t dir, const char *name, ino_t newnum); 346int check_cgmagic(int cg, struct cg *cgp); 347int chkrange(ufs2_daddr_t blk, int cnt); 348void ckfini(int markclean); 349int ckinode(union dinode *dp, struct inodesc *); 350void clri(struct inodesc *, const char *type, int flag); 351int clearentry(struct inodesc *); 352void direrror(ino_t ino, const char *errmesg); 353int dirscan(struct inodesc *); 354int dofix(struct inodesc *, const char *msg); 355int eascan(struct inodesc *, struct ufs2_dinode *dp); 356void fileerror(ino_t cwd, ino_t ino, const char *errmesg); 357int findino(struct inodesc *); 358int findname(struct inodesc *); 359void flush(int fd, struct bufarea *bp); 360void freeblk(ufs2_daddr_t blkno, long frags); 361void freeino(ino_t ino); 362void freeinodebuf(void); 363int ftypeok(union dinode *dp); 364void getblk(struct bufarea *bp, ufs2_daddr_t blk, long size); 365struct bufarea *getdatablk(ufs2_daddr_t blkno, long size); 366struct inoinfo *getinoinfo(ino_t inumber); 367union dinode *getnextinode(ino_t inumber, int rebuildcg); 368void getpathname(char *namebuf, ino_t curdir, ino_t ino); 369union dinode *ginode(ino_t inumber); 370void infohandler(int sig); 371void alarmhandler(int sig); 372void inocleanup(void); 373void inodirty(void); 374struct inostat *inoinfo(ino_t inum); 375int linkup(ino_t orphan, ino_t parentdir, char *name); 376int makeentry(ino_t parent, ino_t ino, const char *name); 377void panic(const char *fmt, ...) __printflike(1, 2); 378void pass1(void); 379void pass1b(void); 380int pass1check(struct inodesc *); 381void pass2(void); 382void pass3(void); 383void pass4(void); 384int pass4check(struct inodesc *); 385void pass5(void); 386void pfatal(const char *fmt, ...) __printflike(1, 2); 387void pinode(ino_t ino); 388void propagate(void); 389void pwarn(const char *fmt, ...) __printflike(1, 2); 390int readsb(int listerr); 391int reply(const char *question); 392void rwerror(const char *mesg, ufs2_daddr_t blk); 393void sblock_init(void); 394void setinodebuf(ino_t); 395int setup(char *dev); 396void gjournal_check(const char *filesys); 397int suj_check(const char *filesys); 398void update_maps(struct cg *, struct cg*, int); 399 400#endif /* !_FSCK_H_ */ 401