xref: /illumos-gate/usr/src/cmd/fs.d/ufs/fsck/fsck.h (revision bfbf29e2)
17c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*bfbf29e2SToomas Soome /*	  All Rights Reserved	*/
37c478bd9Sstevel@tonic-gate 
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
67c478bd9Sstevel@tonic-gate  * All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
97c478bd9Sstevel@tonic-gate  * provided that: (1) source distributions retain this entire copyright
107c478bd9Sstevel@tonic-gate  * notice and comment, and (2) distributions including binaries display
117c478bd9Sstevel@tonic-gate  * the following acknowledgement:  ``This product includes software
127c478bd9Sstevel@tonic-gate  * developed by the University of California, Berkeley and its contributors''
137c478bd9Sstevel@tonic-gate  * in the documentation or other materials provided with the distribution
147c478bd9Sstevel@tonic-gate  * and in all advertising materials mentioning features or use of this
157c478bd9Sstevel@tonic-gate  * software. Neither the name of the University nor the names of its
167c478bd9Sstevel@tonic-gate  * contributors may be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
2439542a18Sabalfour  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef	_FSCK_FSCK_H
297c478bd9Sstevel@tonic-gate #define	_FSCK_FSCK_H
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
327c478bd9Sstevel@tonic-gate extern "C" {
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 
35355d6bb5Sswilcox #include <stdio.h>
36355d6bb5Sswilcox #include <stdarg.h>
37355d6bb5Sswilcox #include <search.h>
38355d6bb5Sswilcox #include <sys/param.h>
39355d6bb5Sswilcox #include <sys/types.h>
40355d6bb5Sswilcox #include <sys/mnttab.h>
41355d6bb5Sswilcox #include <sys/vfstab.h>
42355d6bb5Sswilcox #include <sys/fs/ufs_fs.h>
43355d6bb5Sswilcox #include <sys/fs/ufs_inode.h>
44355d6bb5Sswilcox 
457c478bd9Sstevel@tonic-gate #define	MAXDUP		10	/* limit on dup blks (per inode) */
467c478bd9Sstevel@tonic-gate #define	MAXBAD		10	/* limit on bad blks (per inode) */
47355d6bb5Sswilcox #define	MAXBUFSPACE	40*1024 /* initial space to allocate to buffers */
487c478bd9Sstevel@tonic-gate #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes in pass1 */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifndef BUFSIZ
51355d6bb5Sswilcox #define	BUFSIZ MAXPATHLEN
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
54355d6bb5Sswilcox /*
55355d6bb5Sswilcox  * Inode states in statemap[].
56355d6bb5Sswilcox  */
57355d6bb5Sswilcox #define	USTATE	0x01		/* inode not allocated */
58355d6bb5Sswilcox #define	FSTATE	0x02		/* inode is file */
59355d6bb5Sswilcox #define	DSTATE	0x04		/* inode is directory */
60355d6bb5Sswilcox #define	SSTATE	0x08		/* inode is a shadow/acl */
61355d6bb5Sswilcox #define	STMASK	0x0f		/* pick off the basic state/type */
62355d6bb5Sswilcox 
63355d6bb5Sswilcox /* flags OR'd into the above */
64355d6bb5Sswilcox #define	INZLINK  0x0010		/* inode has zero links */
65355d6bb5Sswilcox #define	INFOUND  0x0020		/* inode was found during descent */
66355d6bb5Sswilcox #define	INCLEAR  0x0040		/* inode is to be cleared */
67355d6bb5Sswilcox #define	INORPHAN 0x0080		/* inode is a known orphan (pass3 only) */
68355d6bb5Sswilcox #define	INDELAYD 0x0200		/* link count update delayed */
69355d6bb5Sswilcox #define	INMASK   0xfff0		/* pick off the modifiers */
70355d6bb5Sswilcox 
71355d6bb5Sswilcox #define	FZLINK	(FSTATE | INZLINK)
72355d6bb5Sswilcox #define	DZLINK	(DSTATE | INZLINK)
73355d6bb5Sswilcox #define	SZLINK	(SSTATE | INZLINK)
74355d6bb5Sswilcox 
75355d6bb5Sswilcox #define	DFOUND	(DSTATE | INFOUND)
76355d6bb5Sswilcox 
77355d6bb5Sswilcox #define	DCLEAR	(DSTATE | INCLEAR)
78355d6bb5Sswilcox #define	FCLEAR	(FSTATE | INCLEAR)
79355d6bb5Sswilcox #define	SCLEAR	(SSTATE | INCLEAR)
80355d6bb5Sswilcox 
81355d6bb5Sswilcox /*
82355d6bb5Sswilcox  * These tests depend on the state/type defines above not overlapping bits.
83355d6bb5Sswilcox  *
84*bfbf29e2SToomas Soome  *	DUNFOUND === (state == DSTATE || state == DZLINK)
85355d6bb5Sswilcox  *          INCLEAR is irrelevant to the determination of
86355d6bb5Sswilcox  *          connectedness, so it's not included in this test.
87355d6bb5Sswilcox  *
88*bfbf29e2SToomas Soome  *	DVALID   === (state == DSTATE || state == DZLINK || state == DFOUND)
89355d6bb5Sswilcox  */
90355d6bb5Sswilcox #define	S_IS_DUNFOUND(state)	(((state) & (DSTATE | INZLINK)) \
91355d6bb5Sswilcox 				== (state))
92355d6bb5Sswilcox #define	S_IS_DVALID(state)	(((state) & (DSTATE | INZLINK | INFOUND | \
93355d6bb5Sswilcox 				INORPHAN)) == (state))
94355d6bb5Sswilcox #define	S_IS_ZLINK(state)	(((state) & INZLINK) != 0)
95355d6bb5Sswilcox #define	INO_IS_DUNFOUND(ino)	S_IS_DUNFOUND(statemap[ino])
96355d6bb5Sswilcox #define	INO_IS_DVALID(ino)	S_IS_DVALID(statemap[ino])
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * buffer cache structure.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate struct bufarea {
1027c478bd9Sstevel@tonic-gate 	struct bufarea	*b_next;		/* free list queue */
1037c478bd9Sstevel@tonic-gate 	struct bufarea	*b_prev;		/* free list queue */
104355d6bb5Sswilcox 	diskaddr_t	b_bno;			/* physical sector number */
1057c478bd9Sstevel@tonic-gate 	int	b_size;
1067c478bd9Sstevel@tonic-gate 	int	b_errs;
1077c478bd9Sstevel@tonic-gate 	int	b_flags;
1087c478bd9Sstevel@tonic-gate 	int	b_cnt;				/* reference cnt */
1097c478bd9Sstevel@tonic-gate 	union {
1107c478bd9Sstevel@tonic-gate 		char	*b_buf;			/* buffer space */
1117c478bd9Sstevel@tonic-gate 		daddr32_t	*b_indir;	/* indirect block */
1127c478bd9Sstevel@tonic-gate 		struct	fs *b_fs;		/* super block */
1137c478bd9Sstevel@tonic-gate 		struct	cg *b_cg;		/* cylinder group */
1147c478bd9Sstevel@tonic-gate 		struct	dinode *b_dinode;	/* inode block */
1157c478bd9Sstevel@tonic-gate 	} b_un;
1167c478bd9Sstevel@tonic-gate 	char	b_dirty;
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #define	B_INUSE 1
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	MINBUFS		5	/* minimum number of buffers required */
122*bfbf29e2SToomas Soome extern struct bufarea sblk;	/* file system superblock */
123*bfbf29e2SToomas Soome extern struct bufarea cgblk;	/* cylinder group blocks */
124*bfbf29e2SToomas Soome extern struct bufarea *pbp;	/* pointer to inode data in buffer pool */
125*bfbf29e2SToomas Soome extern struct bufarea *pdirbp;	/* pointer to directory data in buffer pool */
1267c478bd9Sstevel@tonic-gate 
127355d6bb5Sswilcox #define	sbdirty()	dirty(&sblk)
128355d6bb5Sswilcox #define	cgdirty()	dirty(&cgblk)
1297c478bd9Sstevel@tonic-gate #define	sblock		(*sblk.b_un.b_fs)
1307c478bd9Sstevel@tonic-gate #define	cgrp		(*cgblk.b_un.b_cg)
1317c478bd9Sstevel@tonic-gate 
132355d6bb5Sswilcox /*
133355d6bb5Sswilcox  * inodesc.id_fix values.  See inode.c for a description of their usage.
134355d6bb5Sswilcox  */
135355d6bb5Sswilcox enum fixstate {
136355d6bb5Sswilcox 	DONTKNOW, NOFIX, FIX, IGNORE
137355d6bb5Sswilcox };
138355d6bb5Sswilcox 
139355d6bb5Sswilcox /*
140355d6bb5Sswilcox  * Tells truncino() whether or not to attempt to update the parent
141355d6bb5Sswilcox  * directory's link count.  Also, TI_NODUP flags when we're discarding
142355d6bb5Sswilcox  * fragments that are beyond the original end of the file, and so
143355d6bb5Sswilcox  * should not be considered duplicate-claim candidates.
144355d6bb5Sswilcox  */
145355d6bb5Sswilcox #define	TI_NOPARENT	0x0001	/* leave parent's di_nlink alone */
146355d6bb5Sswilcox #define	TI_PARENT	0x0002	/* update parent's di_nlink */
147355d6bb5Sswilcox #define	TI_NODUP	0x0004	/* not a dup candidate */
148355d6bb5Sswilcox 
149355d6bb5Sswilcox /*
150355d6bb5Sswilcox  * Modes for ckinode() and ckinode_common().
151355d6bb5Sswilcox  *
152355d6bb5Sswilcox  * CKI_TRAVERSE is the common case, and requests a traditional
153355d6bb5Sswilcox  * traversal of blocks or directory entries.
154355d6bb5Sswilcox  *
155355d6bb5Sswilcox  * CKI_TRUNCATE indicates that we're truncating the file, and that any
156355d6bb5Sswilcox  * block indices beyond the end of the target length should be cleared
157355d6bb5Sswilcox  * after the callback has returned (i.e., this is a superset of
158355d6bb5Sswilcox  * CKI_TRAVERSE).  idesc->id_truncto is the first logical block number
159355d6bb5Sswilcox  * to clear.  If it is less than zero, then the traversal will be
160355d6bb5Sswilcox  * equivalent to a simple CKI_TRAVERSE.
161355d6bb5Sswilcox  */
162355d6bb5Sswilcox enum cki_action { CKI_TRAVERSE, CKI_TRUNCATE };
163355d6bb5Sswilcox 
164355d6bb5Sswilcox /*
165355d6bb5Sswilcox  * The general definition of an ino_t is an unsigned quantity.
166355d6bb5Sswilcox  * However, the on-disk version is an int32_t, which is signed.
167355d6bb5Sswilcox  * Since we really want to be able to detect wrapped-around
168355d6bb5Sswilcox  * inode numbers and such, we'll use something that's compatible
169355d6bb5Sswilcox  * with what's on disk since that's the only context that really
170355d6bb5Sswilcox  * matters.  If an int32_t is found not to be sufficiently large,
171355d6bb5Sswilcox  * this will make it much easier to change later.
172355d6bb5Sswilcox  *
173355d6bb5Sswilcox  * Note that there is one unsigned inode field in the on-disk
174355d6bb5Sswilcox  * inode, ic_oeftflag.  Since all other inode fields are signed,
175355d6bb5Sswilcox  * no legitimate inode number can be put into ic_oeftflag that
176355d6bb5Sswilcox  * would overflow into the high bit.  Essentially, it should
177355d6bb5Sswilcox  * actually be declared as int32_t just like all the others, and
178355d6bb5Sswilcox  * we're going to pretend that it was.
179355d6bb5Sswilcox  *
180355d6bb5Sswilcox  * None of the routines that we use in ufs_subr.c do anything with
181355d6bb5Sswilcox  * inode numbers.  If that changes, then great care will be needed
182355d6bb5Sswilcox  * to deal with the differences in definition of ino_t and fsck_ino_t.
183355d6bb5Sswilcox  * Lint is your friend.
184355d6bb5Sswilcox  */
185355d6bb5Sswilcox typedef int32_t		fsck_ino_t;
1867c478bd9Sstevel@tonic-gate 
187355d6bb5Sswilcox /*
188355d6bb5Sswilcox  * See the full discussion of the interactions between struct inodesc
189355d6bb5Sswilcox  * and ckinode() in inode.c
190355d6bb5Sswilcox  */
1917c478bd9Sstevel@tonic-gate struct inodesc {
1927c478bd9Sstevel@tonic-gate 	enum fixstate id_fix;	/* policy on fixing errors */
193355d6bb5Sswilcox 	int (*id_func)(struct inodesc *);
194355d6bb5Sswilcox 				/* function to be applied to blocks of inode */
195355d6bb5Sswilcox 	fsck_ino_t id_number;	/* inode number described */
196355d6bb5Sswilcox 	fsck_ino_t id_parent;	/* for DATA nodes, their parent */
197355d6bb5Sswilcox 				/* also used for extra (*id_func) parameter */
198355d6bb5Sswilcox 				/* and return values */
199355d6bb5Sswilcox 	daddr32_t id_lbn;	/* logical fragment number of current block */
200355d6bb5Sswilcox 	daddr32_t id_blkno;	/* physical fragment number being examined */
2017c478bd9Sstevel@tonic-gate 	int id_numfrags;	/* number of frags contained in block */
202355d6bb5Sswilcox 	daddr32_t id_truncto;	/* # blocks to truncate to, -1 for no trunc. */
2037c478bd9Sstevel@tonic-gate 	offset_t id_filesize;	/* for DATA nodes, the size of the directory */
204355d6bb5Sswilcox 	uint_t id_loc;		/* for DATA nodes, current location in dir */
205355d6bb5Sswilcox 	daddr32_t id_entryno;	/* for DATA nodes, current dir entry number */
206355d6bb5Sswilcox 	daddr32_t id_firsthole;	/* for DATA inode, logical block that is */
207355d6bb5Sswilcox 				/* zero but shouldn't be, -1 for no holes */
2087c478bd9Sstevel@tonic-gate 	struct direct *id_dirp;	/* for DATA nodes, ptr to current entry */
209355d6bb5Sswilcox 	caddr_t id_name;	/* for DATA nodes, name to find or enter */
2107c478bd9Sstevel@tonic-gate 	char id_type;		/* type of descriptor, DATA or ADDR */
2117c478bd9Sstevel@tonic-gate };
212355d6bb5Sswilcox 
213355d6bb5Sswilcox /* file types (0 is reserved for catching bugs) */
214355d6bb5Sswilcox #define	DATA	1	/* a directory */
215355d6bb5Sswilcox #define	ACL	2	/* an acl/shadow */
216355d6bb5Sswilcox #define	ADDR	3	/* anything but a directory or an acl/shadow */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
219355d6bb5Sswilcox  * OR'd flags for find_dup_ref()'s mode argument
2207c478bd9Sstevel@tonic-gate  */
221355d6bb5Sswilcox #define	DB_CREATE	0x01	/* if dup record found, make one */
222355d6bb5Sswilcox #define	DB_INCR		0x02	/* increment block's reference count */
223355d6bb5Sswilcox #define	DB_DECR		0x04	/* decrement block's reference count */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
226355d6bb5Sswilcox  * Cache data structures
2277c478bd9Sstevel@tonic-gate  */
228355d6bb5Sswilcox struct inoinfo {
229355d6bb5Sswilcox 	struct inoinfo	*i_nextlist;	/* next inode/acl cache entry */
230355d6bb5Sswilcox 	fsck_ino_t	i_number;	/* inode number of this entry */
231355d6bb5Sswilcox 	fsck_ino_t	i_parent;	/* inode number of parent */
232355d6bb5Sswilcox 	fsck_ino_t	i_dotdot;	/* inode number of .. */
233355d6bb5Sswilcox 	fsck_ino_t	i_extattr;	/* inode of hidden attr dir */
234355d6bb5Sswilcox 	offset_t	i_isize;	/* size of inode */
235355d6bb5Sswilcox 	size_t		i_blkssize;	/* size of block array in bytes */
236355d6bb5Sswilcox 	daddr32_t	i_blks[1];	/* actually longer */
2377c478bd9Sstevel@tonic-gate };
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
240355d6bb5Sswilcox  * Inode cache
2417c478bd9Sstevel@tonic-gate  */
242*bfbf29e2SToomas Soome extern struct inoinfo **inphead, **inpsort;
243*bfbf29e2SToomas Soome extern int64_t numdirs, listmax, inplast;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
246355d6bb5Sswilcox  * ACL cache
2477c478bd9Sstevel@tonic-gate  */
248*bfbf29e2SToomas Soome extern struct inoinfo **aclphead, **aclpsort;
249*bfbf29e2SToomas Soome extern int64_t numacls, aclmax, aclplast;
2507c478bd9Sstevel@tonic-gate 
251355d6bb5Sswilcox /*
252355d6bb5Sswilcox  * Tree of directories we haven't reconnected or cleared.  Any
253355d6bb5Sswilcox  * dir inode that linkup() fails on gets added, any that clri()
254355d6bb5Sswilcox  * succeeds on gets removed.  If there are any left at the end of
255355d6bb5Sswilcox  * pass four, then we have a user-forced corrupt filesystem, and
256355d6bb5Sswilcox  * need to set iscorrupt.
257355d6bb5Sswilcox  *
258355d6bb5Sswilcox  * Elements are fsck_ino_t instances (not pointers).
259355d6bb5Sswilcox  */
260*bfbf29e2SToomas Soome extern void *limbo_dirs;
261355d6bb5Sswilcox 
262355d6bb5Sswilcox /*
263355d6bb5Sswilcox  * Number of directories we actually found in the filesystem,
264355d6bb5Sswilcox  * as opposed to how many the superblock claims there are.
265355d6bb5Sswilcox  */
266*bfbf29e2SToomas Soome extern fsck_ino_t countdirs;
267355d6bb5Sswilcox 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * shadowclients and shadowclientinfo are structures for keeping track of
2707c478bd9Sstevel@tonic-gate  * shadow inodes that exist, and which regular inodes use them (i.e. are
2717c478bd9Sstevel@tonic-gate  * their clients).
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate struct shadowclients {
275355d6bb5Sswilcox 	fsck_ino_t *client;	/* an array of inode numbers */
2767c478bd9Sstevel@tonic-gate 	int nclients; /* how many inodes in the array are in use (valid) */
2777c478bd9Sstevel@tonic-gate 	struct shadowclients *next; /* link to more client inode numbers */
2787c478bd9Sstevel@tonic-gate };
2797c478bd9Sstevel@tonic-gate struct shadowclientinfo {
280355d6bb5Sswilcox 	fsck_ino_t shadow;	/* the shadow inode that this info is for */
2817c478bd9Sstevel@tonic-gate 	int totalClients;	/* how many inodes total refer to this */
2827c478bd9Sstevel@tonic-gate 	struct shadowclients *clients; /* a linked list of wads of clients */
2837c478bd9Sstevel@tonic-gate 	struct shadowclientinfo *next; /* link to the next shadow inode */
2847c478bd9Sstevel@tonic-gate };
2857c478bd9Sstevel@tonic-gate /* global pointer to this shadow/client information */
286*bfbf29e2SToomas Soome extern struct shadowclientinfo *shadowclientinfo;
287*bfbf29e2SToomas Soome extern struct shadowclientinfo *attrclientinfo;
288355d6bb5Sswilcox 
289355d6bb5Sswilcox /*
290355d6bb5Sswilcox  * In ufs_inode.h ifdef _KERNEL, this is defined as `/@/'.  However,
291355d6bb5Sswilcox  * to avoid all sorts of potential confusion (you can't actually use
292355d6bb5Sswilcox  * `foo/@/bar' to get to an attribute), we use something that doesn't
293355d6bb5Sswilcox  * look quite so much like a simple pathname.
294355d6bb5Sswilcox  */
295355d6bb5Sswilcox #define	XATTR_DIR_NAME	" <xattr> "
296355d6bb5Sswilcox 
297355d6bb5Sswilcox /*
298355d6bb5Sswilcox  * granularity -- how many client inodes do we make space for at a time
299355d6bb5Sswilcox  * initialized in setup.c;
300355d6bb5Sswilcox  */
3017c478bd9Sstevel@tonic-gate extern int maxshadowclients;
3027c478bd9Sstevel@tonic-gate 
303355d6bb5Sswilcox /*
304355d6bb5Sswilcox  * Initialized global variables.
305355d6bb5Sswilcox  */
306355d6bb5Sswilcox extern caddr_t lfname;
307355d6bb5Sswilcox 
308355d6bb5Sswilcox /*
309355d6bb5Sswilcox  * Unitialized globals.
310355d6bb5Sswilcox  */
311*bfbf29e2SToomas Soome extern char	*devname;	/* name of device being checked */
312*bfbf29e2SToomas Soome extern size_t	dev_bsize;	/* computed value of DEV_BSIZE */
313*bfbf29e2SToomas Soome extern int	secsize;	/* actual disk sector size */
314*bfbf29e2SToomas Soome extern char	nflag;		/* assume a no response */
315*bfbf29e2SToomas Soome extern char	yflag;		/* assume a yes response */
316*bfbf29e2SToomas Soome extern daddr32_t	bflag;	/* location of alternate super block */
317*bfbf29e2SToomas Soome extern int	debug;		/* output debugging info */
318*bfbf29e2SToomas Soome extern int	rflag;		/* check raw file systems */
319*bfbf29e2SToomas Soome extern int	fflag;		/* check regardless of clean flag (force) */
320*bfbf29e2SToomas Soome extern int	mflag;		/* sanity check only */
321*bfbf29e2SToomas Soome extern int	verbose;	/* be chatty */
322*bfbf29e2SToomas Soome extern char	preen;		/* just fix normal inconsistencies */
323*bfbf29e2SToomas Soome extern char	mountedfs;	/* checking mounted device */
324*bfbf29e2SToomas Soome extern int	exitstat;	/* exit status (see EX* defines below) */
325*bfbf29e2SToomas Soome extern char	hotroot;	/* checking root device */
326*bfbf29e2SToomas Soome extern char	rerun;		/* rerun fsck. Only used in non-preen mode */
327*bfbf29e2SToomas Soome extern int	interrupted;	/* 1 => exit EXSIGNAL on exit */
328*bfbf29e2SToomas Soome extern char	havesb;		/* superblock has been read */
329*bfbf29e2SToomas Soome extern int	fsmodified;	/* 1 => write done to file system */
330*bfbf29e2SToomas Soome extern int	fsreadfd;	/* file descriptor for reading file system */
331*bfbf29e2SToomas Soome extern int	fswritefd;	/* file descriptor for writing file system */
332*bfbf29e2SToomas Soome extern int	iscorrupt;	/* known to be corrupt/inconsistent */
333355d6bb5Sswilcox 				/* -1 means mark clean so user can mount+fix */
334*bfbf29e2SToomas Soome extern int	isdirty;	/* 1 => write pending to file system */
3357c478bd9Sstevel@tonic-gate 
336*bfbf29e2SToomas Soome extern int	islog;		/* logging file system */
337*bfbf29e2SToomas Soome extern int	islogok;	/* log is okay */
3387c478bd9Sstevel@tonic-gate 
339*bfbf29e2SToomas Soome extern int	errorlocked;	/* set => mounted fs has been error-locked */
3407c478bd9Sstevel@tonic-gate 				/* implies fflag "force check flag" */
341*bfbf29e2SToomas Soome extern char	*elock_combuf;	/* error lock comment buffer */
342*bfbf29e2SToomas Soome extern char	*elock_mountp;	/* mount point; used to unlock error-lock */
343*bfbf29e2SToomas Soome extern int	pid;		/* fsck's process id (put in lockfs comment) */
344*bfbf29e2SToomas Soome extern int	mountfd;	/* fd of mount point */
345*bfbf29e2SToomas Soome 
346*bfbf29e2SToomas Soome extern daddr32_t maxfsblock;	/* number of blocks in the file system */
347*bfbf29e2SToomas Soome extern uint_t	largefile_count; /* global largefile counter */
348*bfbf29e2SToomas Soome extern char	*mount_point;	/* if mounted, this is where */
349*bfbf29e2SToomas Soome extern char	*blockmap;	/* ptr to primary blk allocation map */
350*bfbf29e2SToomas Soome extern fsck_ino_t	maxino;	/* number of inodes in file system */
351*bfbf29e2SToomas Soome extern fsck_ino_t	lastino; /* last inode in use */
352*bfbf29e2SToomas Soome extern ushort_t *statemap;	/* ptr to inode state table */
353*bfbf29e2SToomas Soome extern short	*lncntp;	/* ptr to link count table */
354*bfbf29e2SToomas Soome 
355*bfbf29e2SToomas Soome extern fsck_ino_t	lfdir;	/* lost & found directory inode number */
356*bfbf29e2SToomas Soome extern int	overflowed_lf;	/* tried to wrap lost & found's link count */
357*bfbf29e2SToomas Soome extern int	reattached_dir;	/* reconnected at least one directory */
358*bfbf29e2SToomas Soome extern int	broke_dir_link;	/* broke at least one directory hardlink */
359*bfbf29e2SToomas Soome 
360*bfbf29e2SToomas Soome extern daddr32_t	n_blks;		/* number of blocks in use */
361*bfbf29e2SToomas Soome extern fsck_ino_t	n_files;	/* number of files in use */
3627c478bd9Sstevel@tonic-gate 
363355d6bb5Sswilcox #define	clearinode(dp)	{ \
364355d6bb5Sswilcox 	*(dp) = zino; \
365355d6bb5Sswilcox }
366*bfbf29e2SToomas Soome extern struct	dinode zino;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #define	testbmap(blkno)	isset(blockmap, blkno)
369355d6bb5Sswilcox #define	setbmap(blkno)	setbit(blockmap, blkno)
3707c478bd9Sstevel@tonic-gate #define	clrbmap(blkno)	clrbit(blockmap, blkno)
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate #define	STOP	0x01
3737c478bd9Sstevel@tonic-gate #define	SKIP	0x02
3747c478bd9Sstevel@tonic-gate #define	KEEPON	0x04
3757c478bd9Sstevel@tonic-gate #define	ALTERED	0x08
3767c478bd9Sstevel@tonic-gate #define	FOUND	0x10
3777c478bd9Sstevel@tonic-gate 
378355d6bb5Sswilcox /*
379355d6bb5Sswilcox  * Support relatively easy debugging of lncntp[] updates.  This can't
380355d6bb5Sswilcox  * be a function, because of the (_op) step.  Normally, we just do that.
381355d6bb5Sswilcox  */
382355d6bb5Sswilcox #define	TRACK_LNCNTP(_ino, _op) (_op)
383355d6bb5Sswilcox 
384355d6bb5Sswilcox /*
385355d6bb5Sswilcox  * See if the net link count for an inode has gone outside
386355d6bb5Sswilcox  * what can be represented on disk.  Returning text as NULL
387355d6bb5Sswilcox  * indicates no.
388355d6bb5Sswilcox  *
389355d6bb5Sswilcox  * Remember that link counts are effectively inverted, so
390355d6bb5Sswilcox  * underflow and overflow are reversed as well.
391355d6bb5Sswilcox  *
392355d6bb5Sswilcox  * This check should be done before modifying the actual link
393355d6bb5Sswilcox  * count.
394355d6bb5Sswilcox  */
395355d6bb5Sswilcox #define	LINK_RANGE(text, current, offset) { \
396355d6bb5Sswilcox 	int net = ((int)(current)) + ((int)(offset)); \
397355d6bb5Sswilcox 	text = NULL; \
398355d6bb5Sswilcox 	if (net > (MAXLINK)) \
399355d6bb5Sswilcox 		text = "UNDERFLOW"; \
400355d6bb5Sswilcox 	else if (net < -(MAXLINK)) \
401355d6bb5Sswilcox 		text = "OVERFLOW"; \
402355d6bb5Sswilcox }
4037c478bd9Sstevel@tonic-gate 
404355d6bb5Sswilcox /*
405355d6bb5Sswilcox  * If LINK_RANGE() indicated a problem, this is the boiler-plate
406355d6bb5Sswilcox  * for dealing with it.  Usage is:
407355d6bb5Sswilcox  *
408355d6bb5Sswilcox  *     LINK_RANGE(text, current, offset);
409355d6bb5Sswilcox  *     if (text != NULL) {
410355d6bb5Sswilcox  *         LINK_CLEAR(text, ino, mode, idp);
411355d6bb5Sswilcox  *         if (statemap[ino] == USTATE)
412355d6bb5Sswilcox  *             ...inode was cleared...
413355d6bb5Sswilcox  *     }
414355d6bb5Sswilcox  *
415355d6bb5Sswilcox  * Note that clri() will set iscorrupt if the user elects not to
416355d6bb5Sswilcox  * clear the problem inode, so the filesystem won't get reported
417355d6bb5Sswilcox  * as clean when it shouldn't be.
418355d6bb5Sswilcox  */
419355d6bb5Sswilcox #define	LINK_CLEAR(text, ino, mode, idp) { \
420355d6bb5Sswilcox 	pwarn("%s LINK COUNT %s", file_id((ino), (mode)), (text)); \
421355d6bb5Sswilcox 	pinode((ino)); \
422355d6bb5Sswilcox 	pfatal(""); \
423355d6bb5Sswilcox 	init_inodesc((idp)); \
424355d6bb5Sswilcox 	(idp)->id_type = ADDR; \
425355d6bb5Sswilcox 	(idp)->id_func = pass4check; \
426355d6bb5Sswilcox 	(idp)->id_number = ino; \
427355d6bb5Sswilcox 	(idp)->id_fix = DONTKNOW; \
428355d6bb5Sswilcox 	clri((idp), (text), CLRI_QUIET, CLRI_NOP_CORRUPT); \
429355d6bb5Sswilcox }
430355d6bb5Sswilcox 
431355d6bb5Sswilcox /*
432355d6bb5Sswilcox  * Used for checking link count under/overflow specifically on
433355d6bb5Sswilcox  * the lost+found directory.  If the user decides not to do the
434355d6bb5Sswilcox  * clri(), then flag that we've hit this problem and refuse to do
435355d6bb5Sswilcox  * the reconnect.
436355d6bb5Sswilcox  */
437355d6bb5Sswilcox #define	LFDIR_LINK_RANGE_RVAL(text, current, offset, idp, rval) { \
438355d6bb5Sswilcox 	LINK_RANGE(text, current, offset); \
439355d6bb5Sswilcox 	if (text != NULL) { \
440355d6bb5Sswilcox 		LINK_CLEAR(text, lfdir, IFDIR, idp); \
441355d6bb5Sswilcox 		if (statemap[lfdir] == USTATE) { \
442355d6bb5Sswilcox 			lfdir = 0; \
443355d6bb5Sswilcox 			return (rval); \
444355d6bb5Sswilcox 		} else { \
445355d6bb5Sswilcox 			overflowed_lf++; \
446355d6bb5Sswilcox 		} \
447355d6bb5Sswilcox 	} \
448355d6bb5Sswilcox }
449355d6bb5Sswilcox 
450355d6bb5Sswilcox #define	LFDIR_LINK_RANGE_NORVAL(text, current, offset, idp) { \
451355d6bb5Sswilcox 	LINK_RANGE(text, current, offset); \
452355d6bb5Sswilcox 	if (text != NULL) { \
453355d6bb5Sswilcox 		LINK_CLEAR(text, lfdir, IFDIR, idp); \
454355d6bb5Sswilcox 		if (statemap[lfdir] == USTATE) { \
455355d6bb5Sswilcox 			lfdir = 0; \
456355d6bb5Sswilcox 			return; \
457355d6bb5Sswilcox 		} else { \
458355d6bb5Sswilcox 			overflowed_lf++; \
459355d6bb5Sswilcox 		} \
460355d6bb5Sswilcox 	} \
461355d6bb5Sswilcox }
462355d6bb5Sswilcox 
463355d6bb5Sswilcox /*
464355d6bb5Sswilcox  * Values for mounted() and mountedfs.
465355d6bb5Sswilcox  */
466355d6bb5Sswilcox #define	M_NOMNT		0	/* filesystem is not mounted */
467355d6bb5Sswilcox #define	M_RO		1	/* filesystem is mounted read-only */
468355d6bb5Sswilcox #define	M_RW		2	/* filesystem is mounted read-write */
469355d6bb5Sswilcox 
470355d6bb5Sswilcox #define	EXOKAY		0	/* file system is unmounted and ok */
471355d6bb5Sswilcox #define	EXBADPARM	1	/* bad parameter(s) given */
472355d6bb5Sswilcox #define	EXUMNTCHK	32	/* fsck -m: unmounted, needs checking */
473355d6bb5Sswilcox #define	EXMOUNTED	33	/* file system already mounted, not magic, */
474355d6bb5Sswilcox 				/* or it is magic and mounted read/write */
475355d6bb5Sswilcox #define	EXNOSTAT	34	/* cannot stat device */
476355d6bb5Sswilcox #define	EXREBOOTNOW	35	/* modified root or something equally scary */
477355d6bb5Sswilcox #define	EXFNDERRS	36	/* uncorrectable errors, terminate normally */
478355d6bb5Sswilcox #define	EXSIGNAL	37	/* a signal was caught during processing */
479355d6bb5Sswilcox #define	EXERRFATAL	39	/* uncorrectable errors, exit immediately */
480355d6bb5Sswilcox #define	EXROOTOKAY	40	/* for root, same as 0 */
481355d6bb5Sswilcox 
482355d6bb5Sswilcox /*
483355d6bb5Sswilcox  * Values for clri()'s `verbose' and `corrupting' arguments (third
484355d6bb5Sswilcox  * and fourth, respectively).
485355d6bb5Sswilcox  */
486355d6bb5Sswilcox #define	CLRI_QUIET		1
487355d6bb5Sswilcox #define	CLRI_VERBOSE		2
488355d6bb5Sswilcox 
489355d6bb5Sswilcox #define	CLRI_NOP_OK		1
490355d6bb5Sswilcox #define	CLRI_NOP_CORRUPT	2
491355d6bb5Sswilcox 
492355d6bb5Sswilcox /*
493355d6bb5Sswilcox  * Filesystems that are `magical' - if they exist in vfstab,
494355d6bb5Sswilcox  * then they have to be mounted for the system to have gotten
495355d6bb5Sswilcox  * far enough to be able to run fsck.  Thus, don't get all
496355d6bb5Sswilcox  * bent out of shape if we're asked to check it and it is mounted.
497355d6bb5Sswilcox  * Actual initialization of the array is in main.c
498355d6bb5Sswilcox  */
499355d6bb5Sswilcox enum magic {
500355d6bb5Sswilcox 	MAGIC_NONE = 0,
501355d6bb5Sswilcox 	MAGIC_ROOT = 1,
502355d6bb5Sswilcox 	MAGIC_USR = 2,
503b9a41fd3Sswilcox 	MAGIC_LIMIT = 3
504355d6bb5Sswilcox };
505355d6bb5Sswilcox extern char *magic_fs[];
506355d6bb5Sswilcox 
507355d6bb5Sswilcox /*
508355d6bb5Sswilcox  * Paths needed by calcsb().
509355d6bb5Sswilcox  */
510355d6bb5Sswilcox #define	MKFS_PATH	"/usr/lib/fs/ufs/mkfs"
511355d6bb5Sswilcox #define	NEWFS_PATH	"/usr/lib/fs/ufs/newfs"
512355d6bb5Sswilcox 
513355d6bb5Sswilcox int		acltypeok(struct dinode *);
514355d6bb5Sswilcox void		add_orphan_dir(fsck_ino_t);
515355d6bb5Sswilcox void		adjust(struct inodesc *, int);
516355d6bb5Sswilcox daddr32_t	allocblk(int);
517355d6bb5Sswilcox fsck_ino_t	allocdir(fsck_ino_t, fsck_ino_t, int, int);
518355d6bb5Sswilcox fsck_ino_t	allocino(fsck_ino_t, int);
519355d6bb5Sswilcox void		blkerror(fsck_ino_t, caddr_t, daddr32_t, daddr32_t);
520355d6bb5Sswilcox void		brelse(struct bufarea *);
521355d6bb5Sswilcox void		bufinit(void);
522355d6bb5Sswilcox void		bwrite(int, caddr_t, diskaddr_t, int64_t);
523355d6bb5Sswilcox void		cacheacl(struct dinode *, fsck_ino_t);
524355d6bb5Sswilcox void		cacheino(struct dinode *, fsck_ino_t);
525355d6bb5Sswilcox void		catch(int);
526355d6bb5Sswilcox void		catchquit(int);
52777a343abSabalfour caddr_t		cg_sanity(struct cg *, int);
528355d6bb5Sswilcox void		cgflush(void);
529355d6bb5Sswilcox int		cgisdirty(void);
530355d6bb5Sswilcox int		changeino(fsck_ino_t, caddr_t, fsck_ino_t);
531355d6bb5Sswilcox int		check_mnttab(caddr_t, caddr_t, size_t);
532355d6bb5Sswilcox int		check_vfstab(caddr_t, caddr_t, size_t);
533355d6bb5Sswilcox int		chkrange(daddr32_t, int);
534355d6bb5Sswilcox void		ckfini(void);
535355d6bb5Sswilcox int		ckinode(struct dinode *, struct inodesc *, enum cki_action);
536355d6bb5Sswilcox void		clearattrref(fsck_ino_t);
537355d6bb5Sswilcox int		cleardirentry(fsck_ino_t, fsck_ino_t);
538355d6bb5Sswilcox void		clearshadow(fsck_ino_t, struct shadowclientinfo **);
539355d6bb5Sswilcox void		clri(struct inodesc *, caddr_t, int, int);
540355d6bb5Sswilcox void		deshadow(struct shadowclientinfo *, void (*)(fsck_ino_t));
541355d6bb5Sswilcox void		direrror(fsck_ino_t, caddr_t, ...);
542355d6bb5Sswilcox int		dirscan(struct inodesc *);
543355d6bb5Sswilcox void		dirty(struct bufarea *);
544355d6bb5Sswilcox int		do_errorlock(int);
545355d6bb5Sswilcox int		dofix(struct inodesc *, caddr_t, ...);
54639542a18Sabalfour void		examinelog(void (*)(daddr32_t));
547355d6bb5Sswilcox void		errexit(caddr_t, ...);
548355d6bb5Sswilcox void		fileerror(fsck_ino_t, fsck_ino_t, caddr_t, ...);
549355d6bb5Sswilcox caddr_t		file_id(fsck_ino_t, mode_t);
550355d6bb5Sswilcox int		find_dup_ref(daddr32_t, fsck_ino_t, daddr32_t, int);
551355d6bb5Sswilcox int		findino(struct inodesc *);
552355d6bb5Sswilcox int		findname(struct inodesc *);
553355d6bb5Sswilcox void		fix_cg(struct cg *, int);
554355d6bb5Sswilcox void		flush(int, struct bufarea *);
555355d6bb5Sswilcox void		free_dup_state(void);
556355d6bb5Sswilcox void		freeblk(fsck_ino_t, daddr32_t, int);
557355d6bb5Sswilcox void		freeino(fsck_ino_t, int);
558355d6bb5Sswilcox void		freeinodebuf(void);
559355d6bb5Sswilcox int		fsck_asprintf(caddr_t *, caddr_t, ...);
560355d6bb5Sswilcox int		fsck_bread(int, caddr_t, diskaddr_t, size_t);
561355d6bb5Sswilcox int		ftypeok(struct dinode *);
562355d6bb5Sswilcox struct bufarea	*getblk(struct bufarea *, daddr32_t, size_t);
563355d6bb5Sswilcox struct bufarea	*getdatablk(daddr32_t, size_t size);
564355d6bb5Sswilcox diskaddr_t	getdisksize(caddr_t, int);
565355d6bb5Sswilcox struct inoinfo	*getinoinfo(fsck_ino_t);
566355d6bb5Sswilcox struct dinode	*getnextinode(fsck_ino_t);
567355d6bb5Sswilcox struct dinode	*getnextrefresh(void);
568355d6bb5Sswilcox void		getpathname(caddr_t, fsck_ino_t, fsck_ino_t);
569355d6bb5Sswilcox struct dinode	*ginode(fsck_ino_t);
570355d6bb5Sswilcox caddr_t		hasvfsopt(struct vfstab *, caddr_t);
571355d6bb5Sswilcox int		have_dups(void);
572355d6bb5Sswilcox void		init_inodesc(struct inodesc *);
573355d6bb5Sswilcox void		init_inoinfo(struct inoinfo *, struct dinode *, fsck_ino_t);
574355d6bb5Sswilcox void		initbarea(struct bufarea *);
575355d6bb5Sswilcox int		ino_t_cmp(const void *, const void *);
576355d6bb5Sswilcox int		inocached(fsck_ino_t);
577355d6bb5Sswilcox void		inocleanup(void);
578355d6bb5Sswilcox void		inodirty(void);
579355d6bb5Sswilcox int		is_errorlocked(caddr_t);
580355d6bb5Sswilcox int		linkup(fsck_ino_t, fsck_ino_t, caddr_t);
581355d6bb5Sswilcox int		lookup_named_ino(fsck_ino_t, caddr_t);
582355d6bb5Sswilcox int		makeentry(fsck_ino_t, fsck_ino_t, caddr_t);
583355d6bb5Sswilcox void		maybe_convert_attrdir_to_dir(fsck_ino_t);
584355d6bb5Sswilcox int		mounted(caddr_t, caddr_t, size_t);
585355d6bb5Sswilcox void		pass1(void);
586355d6bb5Sswilcox void		pass1b(void);
587355d6bb5Sswilcox int		pass1check(struct inodesc *);
588b9a41fd3Sswilcox void		pass2(void);
589355d6bb5Sswilcox void		pass3a(void);
590355d6bb5Sswilcox void		pass3b(void);
591355d6bb5Sswilcox int		pass3bcheck(struct inodesc *);
592355d6bb5Sswilcox void		pass4(void);
593355d6bb5Sswilcox int		pass4check(struct inodesc *);
594355d6bb5Sswilcox void		pass5(void);
595355d6bb5Sswilcox void		pfatal(caddr_t, ...);
596355d6bb5Sswilcox void		pinode(fsck_ino_t);
597355d6bb5Sswilcox void		printclean(void);
598355d6bb5Sswilcox void		propagate(void);
599355d6bb5Sswilcox void		pwarn(caddr_t, ...);
600355d6bb5Sswilcox caddr_t		rawname(caddr_t);
601355d6bb5Sswilcox void		registershadowclient(fsck_ino_t, fsck_ino_t,
602355d6bb5Sswilcox 		    struct shadowclientinfo **);
603355d6bb5Sswilcox void		remove_orphan_dir(fsck_ino_t);
604355d6bb5Sswilcox int		reply(caddr_t, ...);
605355d6bb5Sswilcox int		report_dups(int);
606355d6bb5Sswilcox void		resetinodebuf(void);
607355d6bb5Sswilcox char		*setup(caddr_t);
608355d6bb5Sswilcox void		truncino(fsck_ino_t, offset_t, int);
609355d6bb5Sswilcox void		unbufinit(void);
610355d6bb5Sswilcox caddr_t		unrawname(caddr_t);
611355d6bb5Sswilcox void		unregistershadow(fsck_ino_t, struct shadowclientinfo **);
612355d6bb5Sswilcox int		updateclean(void);
613355d6bb5Sswilcox int		writable(caddr_t);
614355d6bb5Sswilcox void		write_altsb(int);
615355d6bb5Sswilcox 
616355d6bb5Sswilcox /*
617355d6bb5Sswilcox  * Functions from the kernel sources (ufs_subr.c, etc).
618355d6bb5Sswilcox  */
619355d6bb5Sswilcox extern void	fragacct(struct fs *, int, int32_t *, int);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate #endif
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate #endif	/* _FSCK_FSCK_H */
626