1 /*
2  * e2fsck.h
3  *
4  * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be
5  * redistributed under the terms of the GNU Public License.
6  *
7  */
8 
9 #ifndef _E2FSCK_H
10 #define _E2FSCK_H
11 
12 #include <stdio.h>
13 #include <string.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #include <time.h>
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25 #ifdef HAVE_SETJMP_H
26 #include <setjmp.h>
27 #endif
28 
29 #if EXT2_FLAT_INCLUDES
30 #include "ext2_fs.h"
31 #include "ext2fs.h"
32 #include "blkid.h"
33 #else
34 #include "ext2fs/ext2_fs.h"
35 #include "ext2fs/ext2fs.h"
36 #include "blkid/blkid.h"
37 #endif
38 
39 #include "profile.h"
40 #include "prof_err.h"
41 
42 #ifdef ENABLE_NLS
43 #include <libintl.h>
44 #include <locale.h>
45 #define _(a) (gettext (a))
46 #ifdef gettext_noop
47 #define N_(a) gettext_noop (a)
48 #else
49 #define N_(a) (a)
50 #endif
51 #define P_(singular, plural, n) (ngettext (singular, plural, n))
52 #ifndef NLS_CAT_NAME
53 #define NLS_CAT_NAME "e2fsprogs"
54 #endif
55 #ifndef LOCALEDIR
56 #define LOCALEDIR "/usr/share/locale"
57 #endif
58 #else
59 #define _(a) (a)
60 #define N_(a) a
61 #define P_(singular, plural, n) ((n) == 1 ? (singular) : (plural))
62 #endif
63 
64 #ifdef __GNUC__
65 #define E2FSCK_ATTR(x) __attribute__(x)
66 #else
67 #define E2FSCK_ATTR(x)
68 #endif
69 
70 #include "quota/mkquota.h"
71 
72 /*
73  * Exit codes used by fsck-type programs
74  */
75 #define FSCK_OK          0	/* No errors */
76 #define FSCK_NONDESTRUCT 1	/* File system errors corrected */
77 #define FSCK_REBOOT      2	/* System should be rebooted */
78 #define FSCK_UNCORRECTED 4	/* File system errors left uncorrected */
79 #define FSCK_ERROR       8	/* Operational error */
80 #define FSCK_USAGE       16	/* Usage or syntax error */
81 #define FSCK_CANCELED	 32	/* Aborted with a signal or ^C */
82 #define FSCK_LIBRARY     128	/* Shared library error */
83 
84 /*
85  * The last ext2fs revision level that this version of e2fsck is able to
86  * support
87  */
88 #define E2FSCK_CURRENT_REV	1
89 
90 /*
91  * The directory information structure; stores directory information
92  * collected in earlier passes, to avoid disk i/o in fetching the
93  * directory information.
94  */
95 struct dir_info {
96 	ext2_ino_t		ino;	/* Inode number */
97 	ext2_ino_t		dotdot;	/* Parent according to '..' */
98 	ext2_ino_t		parent; /* Parent according to treewalk */
99 };
100 
101 
102 /*
103  * The indexed directory information structure; stores information for
104  * directories which contain a hash tree index.
105  */
106 struct dx_dir_info {
107 	ext2_ino_t		ino; 		/* Inode number */
108 	int			numblocks;	/* number of blocks */
109 	int			hashversion;
110 	short			depth;		/* depth of tree */
111 	struct dx_dirblock_info	*dx_block; 	/* Array of size numblocks */
112 };
113 
114 #define DX_DIRBLOCK_ROOT	1
115 #define DX_DIRBLOCK_LEAF	2
116 #define DX_DIRBLOCK_NODE	3
117 #define DX_DIRBLOCK_CORRUPT	4
118 #define DX_DIRBLOCK_CLEARED	8
119 
120 struct dx_dirblock_info {
121 	int		type;
122 	blk64_t		phys;
123 	int		flags;
124 	blk64_t		parent;
125 	ext2_dirhash_t	min_hash;
126 	ext2_dirhash_t	max_hash;
127 	ext2_dirhash_t	node_min_hash;
128 	ext2_dirhash_t	node_max_hash;
129 };
130 
131 #define DX_FLAG_REFERENCED	1
132 #define DX_FLAG_DUP_REF		2
133 #define DX_FLAG_FIRST		4
134 #define DX_FLAG_LAST		8
135 
136 #define RESOURCE_TRACK
137 
138 #ifdef RESOURCE_TRACK
139 /*
140  * This structure is used for keeping track of how much resources have
141  * been used for a particular pass of e2fsck.
142  */
143 struct resource_track {
144 	struct timeval time_start;
145 	struct timeval user_start;
146 	struct timeval system_start;
147 	void	*brk_start;
148 	unsigned long long bytes_read;
149 	unsigned long long bytes_written;
150 };
151 #endif
152 
153 /*
154  * E2fsck options
155  */
156 #define E2F_OPT_READONLY	0x0001
157 #define E2F_OPT_PREEN		0x0002
158 #define E2F_OPT_YES		0x0004
159 #define E2F_OPT_NO		0x0008
160 #define E2F_OPT_TIME		0x0010
161 #define E2F_OPT_TIME2		0x0020
162 #define E2F_OPT_CHECKBLOCKS	0x0040
163 #define E2F_OPT_DEBUG		0x0080
164 #define E2F_OPT_FORCE		0x0100
165 #define E2F_OPT_WRITECHECK	0x0200
166 #define E2F_OPT_COMPRESS_DIRS	0x0400
167 #define E2F_OPT_FRAGCHECK	0x0800
168 #define E2F_OPT_JOURNAL_ONLY	0x1000 /* only replay the journal */
169 #define E2F_OPT_DISCARD		0x2000
170 
171 /*
172  * E2fsck flags
173  */
174 #define E2F_FLAG_ABORT		0x0001 /* Abort signaled */
175 #define E2F_FLAG_CANCEL		0x0002 /* Cancel signaled */
176 #define E2F_FLAG_SIGNAL_MASK	0x0003
177 #define E2F_FLAG_RESTART	0x0004 /* Restart signaled */
178 #define E2F_FLAG_RESTART_LATER	0x0008 /* Restart after all iterations done */
179 
180 #define E2F_FLAG_SETJMP_OK	0x0010 /* Setjmp valid for abort */
181 
182 #define E2F_FLAG_PROG_BAR	0x0020 /* Progress bar on screen */
183 #define E2F_FLAG_PROG_SUPPRESS	0x0040 /* Progress suspended */
184 #define E2F_FLAG_JOURNAL_INODE	0x0080 /* Create a new ext3 journal inode */
185 #define E2F_FLAG_SB_SPECIFIED	0x0100 /* The superblock was explicitly
186 					* specified by the user */
187 #define E2F_FLAG_RESTARTED	0x0200 /* E2fsck has been restarted */
188 #define E2F_FLAG_RESIZE_INODE	0x0400 /* Request to recreate resize inode */
189 #define E2F_FLAG_GOT_DEVSIZE	0x0800 /* Device size has been fetched */
190 #define E2F_FLAG_EXITING	0x1000 /* E2fsck exiting due to errors */
191 #define E2F_FLAG_TIME_INSANE	0x2000 /* Time is insane */
192 
193 #define E2F_RESET_FLAGS (E2F_FLAG_TIME_INSANE)
194 
195 /*
196  * Defines for indicating the e2fsck pass number
197  */
198 #define E2F_PASS_1	1
199 #define E2F_PASS_2	2
200 #define E2F_PASS_3	3
201 #define E2F_PASS_4	4
202 #define E2F_PASS_5	5
203 #define E2F_PASS_1B	6
204 
205 /*
206  * Define the extended attribute refcount structure
207  */
208 typedef struct ea_refcount *ext2_refcount_t;
209 
210 /*
211  * This is the global e2fsck structure.
212  */
213 typedef struct e2fsck_struct *e2fsck_t;
214 
215 #define MAX_EXTENT_DEPTH_COUNT 5
216 
217 struct e2fsck_struct {
218 	ext2_filsys fs;
219 	const char *program_name;
220 	char *filesystem_name;
221 	char *device_name;
222 	char *io_options;
223 	FILE	*logf;
224 	char	*log_fn;
225 	int	flags;		/* E2fsck internal flags */
226 	int	options;
227 	int	blocksize;	/* blocksize */
228 	blk64_t	use_superblock;	/* sb requested by user */
229 	blk64_t	superblock;	/* sb used to open fs */
230 	blk64_t	num_blocks;	/* Total number of blocks */
231 	blk64_t free_blocks;
232 	ino_t	free_inodes;
233 	int	mount_flags;
234 	blkid_cache blkid;	/* blkid cache */
235 
236 #ifdef HAVE_SETJMP_H
237 	jmp_buf	abort_loc;
238 #endif
239 	unsigned long abort_code;
240 
241 	int (*progress)(e2fsck_t ctx, int pass, unsigned long cur,
242 			unsigned long max);
243 
244 	ext2fs_inode_bitmap inode_used_map; /* Inodes which are in use */
245 	ext2fs_inode_bitmap inode_bad_map; /* Inodes which are bad somehow */
246 	ext2fs_inode_bitmap inode_dir_map; /* Inodes which are directories */
247 	ext2fs_inode_bitmap inode_bb_map; /* Inodes which are in bad blocks */
248 	ext2fs_inode_bitmap inode_imagic_map; /* AFS inodes */
249 	ext2fs_inode_bitmap inode_reg_map; /* Inodes which are regular files*/
250 
251 	ext2fs_block_bitmap block_found_map; /* Blocks which are in use */
252 	ext2fs_block_bitmap block_dup_map; /* Blks referenced more than once */
253 	ext2fs_block_bitmap block_ea_map; /* Blocks which are used by EA's */
254 
255 	/*
256 	 * Inode count arrays
257 	 */
258 	ext2_icount_t	inode_count;
259 	ext2_icount_t inode_link_info;
260 
261 	ext2_refcount_t	refcount;
262 	ext2_refcount_t refcount_extra;
263 
264 	/*
265 	 * Array of flags indicating whether an inode bitmap, block
266 	 * bitmap, or inode table is invalid
267 	 */
268 	int *invalid_inode_bitmap_flag;
269 	int *invalid_block_bitmap_flag;
270 	int *invalid_inode_table_flag;
271 	int invalid_bitmaps;	/* There are invalid bitmaps/itable */
272 
273 	/*
274 	 * Block buffer
275 	 */
276 	char *block_buf;
277 
278 	/*
279 	 * For pass1_check_directory and pass1_get_blocks
280 	 */
281 	ext2_ino_t stashed_ino;
282 	struct ext2_inode *stashed_inode;
283 
284 	/*
285 	 * Location of the lost and found directory
286 	 */
287 	ext2_ino_t lost_and_found;
288 	int bad_lost_and_found;
289 
290 	/*
291 	 * Directory information
292 	 */
293 	struct dir_info_db	*dir_info;
294 
295 	/*
296 	 * Indexed directory information
297 	 */
298 	int		dx_dir_info_count;
299 	int		dx_dir_info_size;
300 	struct dx_dir_info *dx_dir_info;
301 
302 	/*
303 	 * Directories to hash
304 	 */
305 	ext2_u32_list	dirs_to_hash;
306 
307 	/*
308 	 * Tuning parameters
309 	 */
310 	int process_inode_size;
311 	int inode_buffer_blocks;
312 	unsigned int htree_slack_percentage;
313 
314 	/*
315 	 * ext3 journal support
316 	 */
317 	io_channel	journal_io;
318 	char	*journal_name;
319 
320 	/*
321 	 * Ext4 quota support
322 	 */
323 	quota_ctx_t qctx;
324 #ifdef RESOURCE_TRACK
325 	/*
326 	 * For timing purposes
327 	 */
328 	struct resource_track	global_rtrack;
329 #endif
330 
331 	/*
332 	 * How we display the progress update (for unix)
333 	 */
334 	int progress_fd;
335 	int progress_pos;
336 	int progress_last_percent;
337 	unsigned int progress_last_time;
338 	int interactive;	/* Are we connected directly to a tty? */
339 	char start_meta[2], stop_meta[2];
340 
341 	/* File counts */
342 	__u32 fs_directory_count;
343 	__u32 fs_regular_count;
344 	__u32 fs_blockdev_count;
345 	__u32 fs_chardev_count;
346 	__u32 fs_links_count;
347 	__u32 fs_symlinks_count;
348 	__u32 fs_fast_symlinks_count;
349 	__u32 fs_fifo_count;
350 	__u32 fs_total_count;
351 	__u32 fs_badblocks_count;
352 	__u32 fs_sockets_count;
353 	__u32 fs_ind_count;
354 	__u32 fs_dind_count;
355 	__u32 fs_tind_count;
356 	__u32 fs_fragmented;
357 	__u32 fs_fragmented_dir;
358 	__u32 large_files;
359 	__u32 fs_ext_attr_inodes;
360 	__u32 fs_ext_attr_blocks;
361 	__u32 extent_depth_count[MAX_EXTENT_DEPTH_COUNT];
362 
363 	/* misc fields */
364 	time_t now;
365 	time_t time_fudge;	/* For working around buggy init scripts */
366 	int ext_attr_ver;
367 	profile_t	profile;
368 	int blocks_per_page;
369 
370 	/*
371 	 * For the use of callers of the e2fsck functions; not used by
372 	 * e2fsck functions themselves.
373 	 */
374 	void *priv_data;
375 };
376 
377 /* Used by the region allocation code */
378 typedef __u32 region_addr_t;
379 typedef struct region_struct *region_t;
380 
381 #ifndef HAVE_STRNLEN
382 #define strnlen(str, x) e2fsck_strnlen((str),(x))
383 extern int e2fsck_strnlen(const char * s, int count);
384 #endif
385 
386 /*
387  * Procedure declarations
388  */
389 
390 extern void e2fsck_pass1(e2fsck_t ctx);
391 extern void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf);
392 extern void e2fsck_pass2(e2fsck_t ctx);
393 extern void e2fsck_pass3(e2fsck_t ctx);
394 extern void e2fsck_pass4(e2fsck_t ctx);
395 extern void e2fsck_pass5(e2fsck_t ctx);
396 
397 /* e2fsck.c */
398 extern errcode_t e2fsck_allocate_context(e2fsck_t *ret);
399 extern errcode_t e2fsck_reset_context(e2fsck_t ctx);
400 extern void e2fsck_free_context(e2fsck_t ctx);
401 extern int e2fsck_run(e2fsck_t ctx);
402 
403 
404 /* badblock.c */
405 extern void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
406 				 int replace_bad_blocks);
407 
408 /* crc32.c */
409 extern __u32 crc32_be(__u32 crc, unsigned char const *p, size_t len);
410 
411 /* dirinfo.c */
412 extern void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent);
413 extern void e2fsck_free_dir_info(e2fsck_t ctx);
414 extern int e2fsck_get_num_dirinfo(e2fsck_t ctx);
415 extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx);
416 extern struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx,
417 					     struct dir_info_iter *);
418 extern void e2fsck_dir_info_iter_end(e2fsck_t ctx, struct dir_info_iter *);
419 extern int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
420 				      ext2_ino_t parent);
421 extern int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
422 				      ext2_ino_t dotdot);
423 extern int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
424 				      ext2_ino_t *parent);
425 extern int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
426 				      ext2_ino_t *dotdot);
427 
428 /* dx_dirinfo.c */
429 extern void e2fsck_add_dx_dir(e2fsck_t ctx, ext2_ino_t ino, int num_blocks);
430 extern struct dx_dir_info *e2fsck_get_dx_dir_info(e2fsck_t ctx, ext2_ino_t ino);
431 extern void e2fsck_free_dx_dir_info(e2fsck_t ctx);
432 extern int e2fsck_get_num_dx_dirinfo(e2fsck_t ctx);
433 extern struct dx_dir_info *e2fsck_dx_dir_info_iter(e2fsck_t ctx, int *control);
434 
435 /* ea_refcount.c */
436 extern errcode_t ea_refcount_create(int size, ext2_refcount_t *ret);
437 extern void ea_refcount_free(ext2_refcount_t refcount);
438 extern errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk64_t blk, int *ret);
439 extern errcode_t ea_refcount_increment(ext2_refcount_t refcount,
440 				       blk64_t blk, int *ret);
441 extern errcode_t ea_refcount_decrement(ext2_refcount_t refcount,
442 				       blk64_t blk, int *ret);
443 extern errcode_t ea_refcount_store(ext2_refcount_t refcount,
444 				   blk64_t blk, int count);
445 extern blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount);
446 extern void ea_refcount_intr_begin(ext2_refcount_t refcount);
447 extern blk64_t ea_refcount_intr_next(ext2_refcount_t refcount, int *ret);
448 
449 /* ehandler.c */
450 extern const char *ehandler_operation(const char *op);
451 extern void ehandler_init(io_channel channel);
452 
453 /* journal.c */
454 extern errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx);
455 extern errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx);
456 extern void e2fsck_move_ext3_journal(e2fsck_t ctx);
457 extern int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx);
458 
459 /* logfile.c */
460 extern void set_up_logging(e2fsck_t ctx);
461 
462 /* quota.c */
463 extern void e2fsck_hide_quota(e2fsck_t ctx);
464 
465 /* pass1.c */
466 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
467 				    ext2_icount_t *ret);
468 extern void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts);
469 extern int e2fsck_pass1_check_device_inode(ext2_filsys fs,
470 					   struct ext2_inode *inode);
471 extern int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
472 				      struct ext2_inode *inode, char *buf);
473 extern void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
474 			       struct ext2_inode *inode, int restart_flag,
475 			       const char *source);
476 
477 /* pass2.c */
478 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
479 				    ext2_ino_t ino, char *buf);
480 
481 /* pass3.c */
482 extern int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t inode);
483 extern errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
484 					 int num, int gauranteed_size);
485 extern ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix);
486 extern errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino,
487 					   int adj);
488 
489 
490 /* region.c */
491 extern region_t region_create(region_addr_t min, region_addr_t max);
492 extern void region_free(region_t region);
493 extern int region_allocate(region_t region, region_addr_t start, int n);
494 
495 /* rehash.c */
496 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino);
497 void e2fsck_rehash_directories(e2fsck_t ctx);
498 
499 /* sigcatcher.c */
500 void sigcatcher_setup(void);
501 
502 /* super.c */
503 void check_super_block(e2fsck_t ctx);
504 int check_backup_super_block(e2fsck_t ctx);
505 void check_resize_inode(e2fsck_t ctx);
506 
507 /* util.c */
508 extern void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
509 				    const char *description);
510 extern int ask(e2fsck_t ctx, const char * string, int def);
511 extern int ask_yn(e2fsck_t ctx, const char * string, int def);
512 extern void fatal_error(e2fsck_t ctx, const char * fmt_string);
513 extern void log_out(e2fsck_t ctx, const char *fmt, ...)
514 	E2FSCK_ATTR((format(printf, 2, 3)));
515 extern void log_err(e2fsck_t ctx, const char *fmt, ...)
516 	E2FSCK_ATTR((format(printf, 2, 3)));
517 extern void e2fsck_read_bitmaps(e2fsck_t ctx);
518 extern void e2fsck_write_bitmaps(e2fsck_t ctx);
519 extern void preenhalt(e2fsck_t ctx);
520 extern char *string_copy(e2fsck_t ctx, const char *str, int len);
521 extern errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
522 				    blk_t *ret_blk, int *ret_count);
523 extern int fs_proc_check(const char *fs_name);
524 extern int check_for_modules(const char *fs_name);
525 #ifdef RESOURCE_TRACK
526 extern void print_resource_track(e2fsck_t ctx,
527 				 const char *desc,
528 				 struct resource_track *track,
529 				 io_channel channel);
530 extern void init_resource_track(struct resource_track *track,
531 				io_channel channel);
532 #else
533 #define print_resource_track(ctx, desc, track, channel) do { } while (0)
534 #define init_resource_track(track, channel) do { } while (0)
535 #endif
536 extern int inode_has_valid_blocks(struct ext2_inode *inode);
537 extern void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
538 			      struct ext2_inode * inode, const char * proc);
539 extern void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
540 				   struct ext2_inode *inode,
541 				   const int bufsize, const char *proc);
542 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
543 			       struct ext2_inode * inode, const char * proc);
544 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
545                                struct ext2_inode * inode, int bufsize,
546                                const char *proc);
547 #ifdef MTRACE
548 extern void mtrace_print(char *mesg);
549 #endif
550 extern blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs,
551 			   const char *name, io_manager manager);
552 extern int ext2_file_type(unsigned int mode);
553 extern int write_all(int fd, char *buf, size_t count);
554 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg);
555 errcode_t e2fsck_mmp_update(ext2_filsys fs);
556 
557 extern void e2fsck_set_bitmap_type(ext2_filsys fs,
558 				   unsigned int default_type,
559 				   const char *profile_name,
560 				   unsigned int *old_type);
561 extern errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs,
562 					      const char *descr,
563 					      int default_type,
564 					      const char *profile_name,
565 					      ext2fs_inode_bitmap *ret);
566 extern errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs,
567 					      const char *descr,
568 					      int default_type,
569 					      const char *profile_name,
570 					      ext2fs_block_bitmap *ret);
571 extern errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs,
572 						   const char *descr,
573 						   int default_type,
574 						   const char *profile_name,
575 						   ext2fs_block_bitmap *ret);
576 
577 /* unix.c */
578 extern void e2fsck_clear_progbar(e2fsck_t ctx);
579 extern int e2fsck_simple_progress(e2fsck_t ctx, const char *label,
580 				  float percent, unsigned int dpynum);
581 #endif /* _E2FSCK_H */
582