2 * Copyright (c) 2002 Networks Associates Technology, Inc.
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
32 * Copyright (c) 1980, 1986, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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
59 * @(#)fsck.h 8.4 (Berkeley) 5/9/95
70 #include <sys/queue.h>
72 #define MAXDUP 10 /* limit on dup blks (per inode) */
73 #define MAXBAD 10 /* limit on bad blks (per inode) */
74 #define MINBUFS 10 /* minimum number of buffers required */
75 #define MAXBUFS 40 /* maximum space to allocate to buffers */
76 #define INOBUFSIZE 64*1024 /* size of buffer to read inodes in pass1 */
77 #define ZEROBUFSIZE (dev_bsize * 128) /* size of zero buffer used by -Z */
80 struct ufs1_dinode dp1;
81 struct ufs2_dinode dp2;
83 #define DIP(dp, field) \
84 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
85 (dp)->dp1.field : (dp)->dp2.field)
87 #define DIP_SET(dp, field, val) do { \
88 if (sblock.fs_magic == FS_UFS1_MAGIC) \
89 (dp)->dp1.field = (val); \
91 (dp)->dp2.field = (val); \
95 * Each inode on the file system is described by the following structure.
96 * The linkcnt is initially set to the value in the inode. Each time it
97 * is found during the descent in passes 2, 3, and 4 the count is
98 * decremented. Any inodes whose count is non-zero after pass 4 needs to
99 * have its link count adjusted by the value remaining in ino_linkcnt.
102 char ino_state; /* state of inode, see below */
103 char ino_type; /* type of inode */
104 short ino_linkcnt; /* number of links not found */
109 #define USTATE 0x1 /* inode not allocated */
110 #define FSTATE 0x2 /* inode is file */
111 #define FZLINK 0x3 /* inode is file with a link count of zero */
112 #define DSTATE 0x4 /* inode is directory */
113 #define DZLINK 0x5 /* inode is directory with a zero link count */
114 #define DFOUND 0x6 /* directory found during descent */
115 /* 0x7 UNUSED - see S_IS_DVALID() definition */
116 #define DCLEAR 0x8 /* directory is to be cleared */
117 #define FCLEAR 0x9 /* file is to be cleared */
118 /* DUNFOUND === (state == DSTATE || state == DZLINK) */
119 #define S_IS_DUNFOUND(state) (((state) & ~0x1) == DSTATE)
120 /* DVALID === (state == DSTATE || state == DZLINK || state == DFOUND) */
121 #define S_IS_DVALID(state) (((state) & ~0x3) == DSTATE)
122 #define INO_IS_DUNFOUND(ino) S_IS_DUNFOUND(inoinfo(ino)->ino_state)
123 #define INO_IS_DVALID(ino) S_IS_DVALID(inoinfo(ino)->ino_state)
125 * Inode state information is contained on per cylinder group lists
126 * which are described by the following structure.
129 long il_numalloced; /* number of inodes allocated in this cg */
130 struct inostat *il_stat;/* inostat info for this cylinder group */
134 * buffer cache structure.
137 TAILQ_ENTRY(bufarea) b_list; /* buffer list */
144 char *b_buf; /* buffer space */
145 ufs1_daddr_t *b_indir1; /* UFS1 indirect block */
146 ufs2_daddr_t *b_indir2; /* UFS2 indirect block */
147 struct fs *b_fs; /* super block */
148 struct cg *b_cg; /* cylinder group */
149 struct ufs1_dinode *b_dinode1; /* UFS1 inode block */
150 struct ufs2_dinode *b_dinode2; /* UFS2 inode block */
155 #define IBLK(bp, i) \
156 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
157 (bp)->b_un.b_indir1[i] : (bp)->b_un.b_indir2[i])
159 #define IBLK_SET(bp, i, val) do { \
160 if (sblock.fs_magic == FS_UFS1_MAGIC) \
161 (bp)->b_un.b_indir1[i] = (val); \
163 (bp)->b_un.b_indir2[i] = (val); \
169 #define B_INUSE 0x00000001 /* Buffer is in use */
171 * Type of data in buffer
173 #define BT_UNKNOWN 0 /* Buffer holds a superblock */
174 #define BT_SUPERBLK 1 /* Buffer holds a superblock */
175 #define BT_CYLGRP 2 /* Buffer holds a cylinder group map */
176 #define BT_LEVEL1 3 /* Buffer holds single level indirect */
177 #define BT_LEVEL2 4 /* Buffer holds double level indirect */
178 #define BT_LEVEL3 5 /* Buffer holds triple level indirect */
179 #define BT_EXTATTR 6 /* Buffer holds external attribute data */
180 #define BT_INODES 7 /* Buffer holds external attribute data */
181 #define BT_DIRDATA 8 /* Buffer holds directory data */
182 #define BT_DATA 9 /* Buffer holds user data */
183 #define BT_NUMBUFTYPES 10
188 "Single Level Indirect", \
189 "Double Level Indirect", \
190 "Triple Level Indirect", \
191 "External Attribute", \
193 "Directory Contents", \
195 extern long readcnt[BT_NUMBUFTYPES];
196 extern long totalreadcnt[BT_NUMBUFTYPES];
197 extern struct timespec readtime[BT_NUMBUFTYPES];
198 extern struct timespec totalreadtime[BT_NUMBUFTYPES];
199 extern struct timespec startprog;
201 extern struct bufarea sblk; /* file system superblock */
202 extern struct bufarea *pdirbp; /* current directory contents */
203 extern struct bufarea *pbp; /* current inode block */
205 #define dirty(bp) do { \
207 pfatal("SETTING DIRTY FLAG IN READ_ONLY MODE\n"); \
211 #define initbarea(bp, type) do { \
213 (bp)->b_bno = (ufs2_daddr_t)-1; \
215 (bp)->b_type = type; \
218 #define sbdirty() dirty(&sblk)
219 #define sblock (*sblk.b_un.b_fs)
221 enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
222 extern ino_t cursnapshot;
225 enum fixstate id_fix; /* policy on fixing errors */
226 int (*id_func)(struct inodesc *);
227 /* function to be applied to blocks of inode */
228 ino_t id_number; /* inode number described */
229 ino_t id_parent; /* for DATA nodes, their parent */
230 ufs_lbn_t id_lbn; /* logical block number of current block */
231 ufs2_daddr_t id_blkno; /* current block number being examined */
232 int id_numfrags; /* number of frags contained in block */
233 off_t id_filesize; /* for DATA nodes, the size of the directory */
234 ufs2_daddr_t id_entryno;/* for DATA nodes, current entry number */
235 int id_loc; /* for DATA nodes, current location in dir */
236 struct direct *id_dirp; /* for DATA nodes, ptr to current entry */
237 char *id_name; /* for DATA nodes, name to find or enter */
238 char id_type; /* type of descriptor, DATA or ADDR */
241 #define DATA 1 /* a directory */
242 #define SNAP 2 /* a snapshot */
243 #define ADDR 3 /* anything but a directory or a snapshot */
246 * Linked list of duplicate blocks.
248 * The list is composed of two parts. The first part of the
249 * list (from duplist through the node pointed to by muldup)
250 * contains a single copy of each duplicate block that has been
251 * found. The second part of the list (from muldup to the end)
252 * contains duplicate blocks that have been found more than once.
253 * To check if a block has been found as a duplicate it is only
254 * necessary to search from duplist through muldup. To find the
255 * total number of times that a block has been found as a duplicate
256 * the entire list must be searched for occurrences of the block
257 * in question. The following diagram shows a sample list where
258 * w (found twice), x (found once), y (found three times), and z
259 * (found once) are duplicate block numbers:
261 * w -> y -> x -> z -> y -> w -> y
270 struct dups *duplist; /* head of dup list */
271 struct dups *muldup; /* end of unique duplicate dup block numbers */
274 * Inode cache data structures.
277 struct inoinfo *i_nexthash; /* next entry in hash chain */
278 ino_t i_number; /* inode number of this entry */
279 ino_t i_parent; /* inode number of parent */
280 ino_t i_dotdot; /* inode number of `..' */
281 size_t i_isize; /* size of inode */
282 u_int i_numblks; /* size of block array in bytes */
283 ufs2_daddr_t i_blks[1]; /* actually longer */
284 } **inphead, **inpsort;
285 extern long dirhash, inplast;
286 extern unsigned long numdirs, listmax;
287 extern long countdirs; /* number of directories we actually found */
289 #define MIBSIZE 3 /* size of fsck sysctl MIBs */
290 extern int adjrefcnt[MIBSIZE]; /* MIB command to adjust inode reference cnt */
291 extern int adjblkcnt[MIBSIZE]; /* MIB command to adjust inode block count */
292 extern int adjndir[MIBSIZE]; /* MIB command to adjust number of directories */
293 extern int adjnbfree[MIBSIZE]; /* MIB command to adjust number of free blocks */
294 extern int adjnifree[MIBSIZE]; /* MIB command to adjust number of free inodes */
295 extern int adjnffree[MIBSIZE]; /* MIB command to adjust number of free frags */
296 extern int adjnumclusters[MIBSIZE]; /* MIB command to adjust number of free clusters */
297 extern int freefiles[MIBSIZE]; /* MIB command to free a set of files */
298 extern int freedirs[MIBSIZE]; /* MIB command to free a set of directories */
299 extern int freeblks[MIBSIZE]; /* MIB command to free a set of data blocks */
300 extern struct fsck_cmd cmd; /* sysctl file system update commands */
301 extern char snapname[BUFSIZ]; /* when doing snapshots, the name of the file */
302 extern char *cdevname; /* name of device being checked */
303 extern long dev_bsize; /* computed value of DEV_BSIZE */
304 extern long secsize; /* actual disk sector size */
305 extern u_int real_dev_bsize; /* actual disk sector size, not overridden */
306 extern char nflag; /* assume a no response */
307 extern char yflag; /* assume a yes response */
308 extern int bkgrdflag; /* use a snapshot to run on an active system */
309 extern ufs2_daddr_t bflag; /* location of alternate super block */
310 extern int debug; /* output debugging info */
311 extern int Eflag; /* delete empty data blocks */
312 extern int Zflag; /* zero empty data blocks */
313 extern int inoopt; /* trim out unused inodes */
314 extern char ckclean; /* only do work if not cleanly unmounted */
315 extern int cvtlevel; /* convert to newer file system format */
316 extern int bkgrdcheck; /* determine if background check is possible */
317 extern int bkgrdsumadj; /* whether the kernel have ability to adjust superblock summary */
318 extern char usedsoftdep; /* just fix soft dependency inconsistencies */
319 extern char preen; /* just fix normal inconsistencies */
320 extern char rerun; /* rerun fsck. Only used in non-preen mode */
321 extern int returntosingle; /* 1 => return to single user mode on exit */
322 extern char resolved; /* cleared if unresolved changes => not clean */
323 extern char havesb; /* superblock has been read */
324 extern char skipclean; /* skip clean file systems if preening */
325 extern int fsmodified; /* 1 => write done to file system */
326 extern int fsreadfd; /* file descriptor for reading file system */
327 extern int fswritefd; /* file descriptor for writing file system */
328 extern int surrender; /* Give up if reads fail */
329 extern int wantrestart; /* Restart fsck on early termination */
331 extern ufs2_daddr_t maxfsblock; /* number of blocks in the file system */
332 extern char *blockmap; /* ptr to primary blk allocation map */
333 extern ino_t maxino; /* number of inodes in file system */
335 extern ino_t lfdir; /* lost & found directory inode number */
336 extern const char *lfname; /* lost & found directory name */
337 extern int lfmode; /* lost & found directory creation mode */
339 extern ufs2_daddr_t n_blks; /* number of blocks in use */
340 extern ino_t n_files; /* number of files in use */
342 extern volatile sig_atomic_t got_siginfo; /* received a SIGINFO */
343 extern volatile sig_atomic_t got_sigalarm; /* received a SIGALRM */
345 #define clearinode(dp) \
346 if (sblock.fs_magic == FS_UFS1_MAGIC) { \
347 (dp)->dp1 = ufs1_zino; \
349 (dp)->dp2 = ufs2_zino; \
351 extern struct ufs1_dinode ufs1_zino;
352 extern struct ufs2_dinode ufs2_zino;
354 #define setbmap(blkno) setbit(blockmap, blkno)
355 #define testbmap(blkno) isset(blockmap, blkno)
356 #define clrbmap(blkno) clrbit(blockmap, blkno)
364 #define EEXIT 8 /* Standard error exit. */
365 #define ERERUN 16 /* fsck needs to be re-run. */
368 int flushentry(void);
370 * Wrapper for malloc() that flushes the cylinder group cache to try
378 while ((retval = malloc(size)) == NULL)
379 if (flushentry() == 0)
385 * Wrapper for calloc() that flushes the cylinder group cache to try
389 Calloc(size_t cnt, size_t size)
393 while ((retval = calloc(cnt, size)) == NULL)
394 if (flushentry() == 0)
402 void adjust(struct inodesc *, int lcnt);
403 ufs2_daddr_t allocblk(long frags);
404 ino_t allocdir(ino_t parent, ino_t request, int mode);
405 ino_t allocino(ino_t request, int type);
406 void blkerror(ino_t ino, const char *type, ufs2_daddr_t blk);
407 char *blockcheck(char *name);
408 int blread(int fd, char *buf, ufs2_daddr_t blk, long size);
410 void blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size);
411 void blerase(int fd, ufs2_daddr_t blk, long size);
412 void blzero(int fd, ufs2_daddr_t blk, long size);
413 void cacheino(union dinode *dp, ino_t inumber);
416 int changeino(ino_t dir, const char *name, ino_t newnum);
417 int check_cgmagic(int cg, struct bufarea *cgbp);
418 int chkrange(ufs2_daddr_t blk, int cnt);
419 void ckfini(int markclean);
420 int ckinode(union dinode *dp, struct inodesc *);
421 void clri(struct inodesc *, const char *type, int flag);
422 int clearentry(struct inodesc *);
423 void direrror(ino_t ino, const char *errmesg);
424 int dirscan(struct inodesc *);
425 int dofix(struct inodesc *, const char *msg);
426 int eascan(struct inodesc *, struct ufs2_dinode *dp);
427 void fileerror(ino_t cwd, ino_t ino, const char *errmesg);
428 void finalIOstats(void);
429 int findino(struct inodesc *);
430 int findname(struct inodesc *);
431 void flush(int fd, struct bufarea *bp);
432 void freeblk(ufs2_daddr_t blkno, long frags);
433 void freeino(ino_t ino);
434 void freeinodebuf(void);
435 void fsutilinit(void);
436 int ftypeok(union dinode *dp);
437 void getblk(struct bufarea *bp, ufs2_daddr_t blk, long size);
438 struct bufarea *cgget(int cg);
439 struct bufarea *getdatablk(ufs2_daddr_t blkno, long size, int type);
440 struct inoinfo *getinoinfo(ino_t inumber);
441 union dinode *getnextinode(ino_t inumber, int rebuildcg);
442 void getpathname(char *namebuf, ino_t curdir, ino_t ino);
443 union dinode *ginode(ino_t inumber);
444 void infohandler(int sig);
445 void alarmhandler(int sig);
446 void inocleanup(void);
448 struct inostat *inoinfo(ino_t inum);
449 void IOstats(char *what);
450 int linkup(ino_t orphan, ino_t parentdir, char *name);
451 int makeentry(ino_t parent, ino_t ino, const char *name);
452 void panic(const char *fmt, ...) __printflike(1, 2);
455 int pass1check(struct inodesc *);
459 int pass4check(struct inodesc *);
461 void pfatal(const char *fmt, ...) __printflike(1, 2);
462 void pinode(ino_t ino);
463 void propagate(void);
464 void pwarn(const char *fmt, ...) __printflike(1, 2);
465 int readsb(int listerr);
466 int reply(const char *question);
467 void rwerror(const char *mesg, ufs2_daddr_t blk);
468 void sblock_init(void);
469 void setinodebuf(ino_t);
470 int setup(char *dev);
471 void gjournal_check(const char *filesys);
472 int suj_check(const char *filesys);
473 void update_maps(struct cg *, struct cg*, int);
476 #endif /* !_FSCK_H_ */