Rob Landley | 43ac888 | 2006-04-01 00:40:33 +0000 | [diff] [blame] | 1 | #include <sys/types.h> |
| 2 | #include <inttypes.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <time.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <ctype.h> |
| 10 | #include <setjmp.h> |
| 11 | #include <errno.h> |
| 12 | #include <getopt.h> |
| 13 | #include <limits.h> |
| 14 | #include <stddef.h> |
| 15 | #include <assert.h> |
| 16 | #include <signal.h> |
| 17 | #include <sys/time.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <sys/resource.h> |
| 20 | #include <sys/param.h> |
| 21 | #include <sys/mount.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <malloc.h> |
| 24 | #include <termios.h> |
| 25 | #include <mntent.h> |
| 26 | #include <dirent.h> |
| 27 | #include "ext2fs/kernel-list.h" |
| 28 | #include <sys/types.h> |
| 29 | #include <linux/types.h> |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | * Now pull in the real linux/jfs.h definitions. |
| 34 | */ |
| 35 | #include "ext2fs/kernel-jbd.h" |
| 36 | |
| 37 | |
| 38 | |
| 39 | #include "fsck.h" |
| 40 | |
| 41 | #include "ext2fs/ext2_fs.h" |
| 42 | #include "blkid/blkid.h" |
| 43 | #include "ext2fs/ext2_ext_attr.h" |
| 44 | #include "uuid/uuid.h" |
Bernhard Reutner-Fischer | 89aaf4e | 2006-04-04 12:10:28 +0000 | [diff] [blame^] | 45 | #include "busybox.h" |
Rob Landley | 43ac888 | 2006-04-01 00:40:33 +0000 | [diff] [blame] | 46 | |
| 47 | #ifdef HAVE_CONIO_H |
| 48 | #undef HAVE_TERMIOS_H |
| 49 | #include <conio.h> |
| 50 | #define read_a_char() getch() |
| 51 | #else |
| 52 | #ifdef HAVE_TERMIOS_H |
| 53 | #include <termios.h> |
| 54 | #endif |
| 55 | #endif |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * The last ext2fs revision level that this version of e2fsck is able to |
| 60 | * support |
| 61 | */ |
| 62 | #define E2FSCK_CURRENT_REV 1 |
| 63 | |
| 64 | /* Used by the region allocation code */ |
| 65 | typedef __u32 region_addr_t; |
| 66 | typedef struct region_struct *region_t; |
| 67 | |
| 68 | struct dx_dirblock_info { |
| 69 | int type; |
| 70 | blk_t phys; |
| 71 | int flags; |
| 72 | blk_t parent; |
| 73 | ext2_dirhash_t min_hash; |
| 74 | ext2_dirhash_t max_hash; |
| 75 | ext2_dirhash_t node_min_hash; |
| 76 | ext2_dirhash_t node_max_hash; |
| 77 | }; |
| 78 | |
| 79 | /* |
| 80 | These defines are used in the type field of dx_dirblock_info |
| 81 | */ |
| 82 | |
| 83 | #define DX_DIRBLOCK_ROOT 1 |
| 84 | #define DX_DIRBLOCK_LEAF 2 |
| 85 | #define DX_DIRBLOCK_NODE 3 |
| 86 | #define DX_DIRBLOCK_CORRUPT 4 |
| 87 | #define DX_DIRBLOCK_CLEARED 8 |
| 88 | |
| 89 | |
| 90 | /* |
| 91 | The following defines are used in the 'flags' field of a dx_dirblock_info |
| 92 | */ |
| 93 | #define DX_FLAG_REFERENCED 1 |
| 94 | #define DX_FLAG_DUP_REF 2 |
| 95 | #define DX_FLAG_FIRST 4 |
| 96 | #define DX_FLAG_LAST 8 |
| 97 | |
| 98 | #ifdef RESOURCE_TRACK |
| 99 | /* |
| 100 | * This structure is used for keeping track of how much resources have |
| 101 | * been used for a particular pass of e2fsck. |
| 102 | */ |
| 103 | struct resource_track { |
| 104 | struct timeval time_start; |
| 105 | struct timeval user_start; |
| 106 | struct timeval system_start; |
| 107 | void *brk_start; |
| 108 | }; |
| 109 | #endif |
| 110 | |
| 111 | /* |
| 112 | * E2fsck options |
| 113 | */ |
| 114 | #define E2F_OPT_READONLY 0x0001 |
| 115 | #define E2F_OPT_PREEN 0x0002 |
| 116 | #define E2F_OPT_YES 0x0004 |
| 117 | #define E2F_OPT_NO 0x0008 |
| 118 | #define E2F_OPT_TIME 0x0010 |
| 119 | #define E2F_OPT_TIME2 0x0020 |
| 120 | #define E2F_OPT_CHECKBLOCKS 0x0040 |
| 121 | #define E2F_OPT_DEBUG 0x0080 |
| 122 | #define E2F_OPT_FORCE 0x0100 |
| 123 | #define E2F_OPT_WRITECHECK 0x0200 |
| 124 | #define E2F_OPT_COMPRESS_DIRS 0x0400 |
| 125 | |
| 126 | /* |
| 127 | * E2fsck flags |
| 128 | */ |
| 129 | #define E2F_FLAG_ABORT 0x0001 /* Abort signaled */ |
| 130 | #define E2F_FLAG_CANCEL 0x0002 /* Cancel signaled */ |
| 131 | #define E2F_FLAG_SIGNAL_MASK 0x0003 |
| 132 | #define E2F_FLAG_RESTART 0x0004 /* Restart signaled */ |
| 133 | |
| 134 | #define E2F_FLAG_SETJMP_OK 0x0010 /* Setjmp valid for abort */ |
| 135 | |
| 136 | #define E2F_FLAG_PROG_BAR 0x0020 /* Progress bar on screen */ |
| 137 | #define E2F_FLAG_PROG_SUPPRESS 0x0040 /* Progress suspended */ |
| 138 | #define E2F_FLAG_JOURNAL_INODE 0x0080 /* Create a new ext3 journal inode */ |
| 139 | #define E2F_FLAG_SB_SPECIFIED 0x0100 /* The superblock was explicitly |
| 140 | * specified by the user */ |
| 141 | #define E2F_FLAG_RESTARTED 0x0200 /* E2fsck has been restarted */ |
| 142 | #define E2F_FLAG_RESIZE_INODE 0x0400 /* Request to recreate resize inode */ |
| 143 | |
| 144 | /* |
| 145 | * Defines for indicating the e2fsck pass number |
| 146 | */ |
| 147 | #define E2F_PASS_1 1 |
| 148 | #define E2F_PASS_2 2 |
| 149 | #define E2F_PASS_3 3 |
| 150 | #define E2F_PASS_4 4 |
| 151 | #define E2F_PASS_5 5 |
| 152 | #define E2F_PASS_1B 6 |
| 153 | |
| 154 | /*Don't know where these come from*/ |
| 155 | #define READ 0 |
| 156 | #define WRITE 1 |
| 157 | #define KERN_ERR "" |
| 158 | #define KERN_DEBUG "" |
| 159 | #define cpu_to_be32(n) htonl(n) |
| 160 | #define be32_to_cpu(n) ntohl(n) |
| 161 | |
| 162 | |
| 163 | /* |
| 164 | * The directory information structure; stores directory information |
| 165 | * collected in earlier passes, to avoid disk i/o in fetching the |
| 166 | * directory information. |
| 167 | */ |
| 168 | struct dir_info { |
| 169 | ext2_ino_t ino; /* Inode number */ |
| 170 | ext2_ino_t dotdot; /* Parent according to '..' */ |
| 171 | ext2_ino_t parent; /* Parent according to treewalk */ |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * The indexed directory information structure; stores information for |
| 178 | * directories which contain a hash tree index. |
| 179 | */ |
| 180 | struct dx_dir_info { |
| 181 | ext2_ino_t ino; /* Inode number */ |
| 182 | int numblocks; /* number of blocks */ |
| 183 | int hashversion; |
| 184 | short depth; /* depth of tree */ |
| 185 | struct dx_dirblock_info *dx_block; /* Array of size numblocks */ |
| 186 | }; |
| 187 | |
| 188 | /* |
| 189 | * Define the extended attribute refcount structure |
| 190 | */ |
| 191 | typedef struct ea_refcount *ext2_refcount_t; |
| 192 | |
| 193 | struct e2fsck_struct { |
| 194 | ext2_filsys fs; |
| 195 | const char *program_name; |
| 196 | char *filesystem_name; |
| 197 | char *device_name; |
| 198 | char *io_options; |
| 199 | int flags; /* E2fsck internal flags */ |
| 200 | int options; |
| 201 | blk_t use_superblock; /* sb requested by user */ |
| 202 | blk_t superblock; /* sb used to open fs */ |
| 203 | int blocksize; /* blocksize */ |
| 204 | blk_t num_blocks; /* Total number of blocks */ |
| 205 | int mount_flags; |
| 206 | blkid_cache blkid; /* blkid cache */ |
| 207 | |
| 208 | jmp_buf abort_loc; |
| 209 | |
| 210 | unsigned long abort_code; |
| 211 | |
| 212 | int (*progress)(e2fsck_t ctx, int pass, unsigned long cur, |
| 213 | unsigned long max); |
| 214 | |
| 215 | ext2fs_inode_bitmap inode_used_map; /* Inodes which are in use */ |
| 216 | ext2fs_inode_bitmap inode_bad_map; /* Inodes which are bad somehow */ |
| 217 | ext2fs_inode_bitmap inode_dir_map; /* Inodes which are directories */ |
| 218 | ext2fs_inode_bitmap inode_bb_map; /* Inodes which are in bad blocks */ |
| 219 | ext2fs_inode_bitmap inode_imagic_map; /* AFS inodes */ |
| 220 | ext2fs_inode_bitmap inode_reg_map; /* Inodes which are regular files*/ |
| 221 | |
| 222 | ext2fs_block_bitmap block_found_map; /* Blocks which are in use */ |
| 223 | ext2fs_block_bitmap block_dup_map; /* Blks referenced more than once */ |
| 224 | ext2fs_block_bitmap block_ea_map; /* Blocks which are used by EA's */ |
| 225 | |
| 226 | /* |
| 227 | * Inode count arrays |
| 228 | */ |
| 229 | ext2_icount_t inode_count; |
| 230 | ext2_icount_t inode_link_info; |
| 231 | |
| 232 | ext2_refcount_t refcount; |
| 233 | ext2_refcount_t refcount_extra; |
| 234 | |
| 235 | /* |
| 236 | * Array of flags indicating whether an inode bitmap, block |
| 237 | * bitmap, or inode table is invalid |
| 238 | */ |
| 239 | int *invalid_inode_bitmap_flag; |
| 240 | int *invalid_block_bitmap_flag; |
| 241 | int *invalid_inode_table_flag; |
| 242 | int invalid_bitmaps; /* There are invalid bitmaps/itable */ |
| 243 | |
| 244 | /* |
| 245 | * Block buffer |
| 246 | */ |
| 247 | char *block_buf; |
| 248 | |
| 249 | /* |
| 250 | * For pass1_check_directory and pass1_get_blocks |
| 251 | */ |
| 252 | ext2_ino_t stashed_ino; |
| 253 | struct ext2_inode *stashed_inode; |
| 254 | |
| 255 | /* |
| 256 | * Location of the lost and found directory |
| 257 | */ |
| 258 | ext2_ino_t lost_and_found; |
| 259 | int bad_lost_and_found; |
| 260 | |
| 261 | /* |
| 262 | * Directory information |
| 263 | */ |
| 264 | int dir_info_count; |
| 265 | int dir_info_size; |
| 266 | struct dir_info *dir_info; |
| 267 | |
| 268 | /* |
| 269 | * Indexed directory information |
| 270 | */ |
| 271 | int dx_dir_info_count; |
| 272 | int dx_dir_info_size; |
| 273 | struct dx_dir_info *dx_dir_info; |
| 274 | |
| 275 | /* |
| 276 | * Directories to hash |
| 277 | */ |
| 278 | ext2_u32_list dirs_to_hash; |
| 279 | |
| 280 | /* |
| 281 | * Tuning parameters |
| 282 | */ |
| 283 | int process_inode_size; |
| 284 | int inode_buffer_blocks; |
| 285 | |
| 286 | /* |
| 287 | * ext3 journal support |
| 288 | */ |
| 289 | io_channel journal_io; |
| 290 | char *journal_name; |
| 291 | |
| 292 | #ifdef RESOURCE_TRACK |
| 293 | /* |
| 294 | * For timing purposes |
| 295 | */ |
| 296 | struct resource_track global_rtrack; |
| 297 | #endif |
| 298 | |
| 299 | /* |
| 300 | * How we display the progress update (for unix) |
| 301 | */ |
| 302 | int progress_fd; |
| 303 | int progress_pos; |
| 304 | int progress_last_percent; |
| 305 | unsigned int progress_last_time; |
| 306 | int interactive; /* Are we connected directly to a tty? */ |
| 307 | char start_meta[2], stop_meta[2]; |
| 308 | |
| 309 | /* File counts */ |
| 310 | int fs_directory_count; |
| 311 | int fs_regular_count; |
| 312 | int fs_blockdev_count; |
| 313 | int fs_chardev_count; |
| 314 | int fs_links_count; |
| 315 | int fs_symlinks_count; |
| 316 | int fs_fast_symlinks_count; |
| 317 | int fs_fifo_count; |
| 318 | int fs_total_count; |
| 319 | int fs_badblocks_count; |
| 320 | int fs_sockets_count; |
| 321 | int fs_ind_count; |
| 322 | int fs_dind_count; |
| 323 | int fs_tind_count; |
| 324 | int fs_fragmented; |
| 325 | int large_files; |
| 326 | int fs_ext_attr_inodes; |
| 327 | int fs_ext_attr_blocks; |
| 328 | |
| 329 | int ext_attr_ver; |
| 330 | |
| 331 | /* |
| 332 | * For the use of callers of the e2fsck functions; not used by |
| 333 | * e2fsck functions themselves. |
| 334 | */ |
| 335 | void *priv_data; |
| 336 | }; |
| 337 | |
| 338 | |
| 339 | |
| 340 | static inline int tid_gt(tid_t x, tid_t y) |
| 341 | { |
| 342 | int difference = (x - y); |
| 343 | return (difference > 0); |
| 344 | } |
| 345 | |
| 346 | static inline int tid_geq(tid_t x, tid_t y) |
| 347 | { |
| 348 | int difference = (x - y); |
| 349 | return (difference >= 0); |
| 350 | } |
| 351 | |
| 352 | |