Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * dirinfo.c --- maintains the directory information table for e2fsck. |
| 3 | * |
| 4 | * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 8 | #include "e2fsck.h" |
| 9 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | /* |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 11 | * This subroutine is called during pass1 to create a directory info |
| 12 | * entry. During pass1, the passed-in parent is 0; it will get filled |
| 13 | * in during pass2. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 15 | void e2fsck_add_dir_info(e2fsck_t ctx, ino_t ino, ino_t parent) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 16 | { |
| 17 | struct dir_info *dir; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 18 | int i, j; |
Theodore Ts'o | 5be8dc2 | 1997-12-01 18:24:10 +0000 | [diff] [blame] | 19 | ino_t num_dirs; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 20 | errcode_t retval; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame] | 21 | unsigned long old_size; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 22 | |
| 23 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 24 | printf("add_dir_info for inode %lu...\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #endif |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 26 | if (!ctx->dir_info) { |
| 27 | ctx->dir_info_count = 0; |
Theodore Ts'o | 5be8dc2 | 1997-12-01 18:24:10 +0000 | [diff] [blame] | 28 | retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs); |
| 29 | if (retval) |
| 30 | num_dirs = 1024; /* Guess */ |
| 31 | ctx->dir_info_size = num_dirs + 10; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 32 | ctx->dir_info = (struct dir_info *) |
| 33 | e2fsck_allocate_memory(ctx, ctx->dir_info_size |
| 34 | * sizeof (struct dir_info), |
| 35 | "directory map"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 38 | if (ctx->dir_info_count >= ctx->dir_info_size) { |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame] | 39 | old_size = ctx->dir_info_size * sizeof(struct dir_info); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 40 | ctx->dir_info_size += 10; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame] | 41 | retval = ext2fs_resize_mem(old_size, ctx->dir_info_size * |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 42 | sizeof(struct dir_info), |
| 43 | (void **) &ctx->dir_info); |
| 44 | if (retval) { |
| 45 | ctx->dir_info_size -= 10; |
| 46 | return; |
| 47 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Normally, add_dir_info is called with each inode in |
| 52 | * sequential order; but once in a while (like when pass 3 |
| 53 | * needs to recreate the root directory or lost+found |
| 54 | * directory) it is called out of order. In those cases, we |
| 55 | * need to move the dir_info entries down to make room, since |
| 56 | * the dir_info array needs to be sorted by inode number for |
| 57 | * get_dir_info()'s sake. |
| 58 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 59 | if (ctx->dir_info_count && |
| 60 | ctx->dir_info[ctx->dir_info_count-1].ino >= ino) { |
| 61 | for (i = ctx->dir_info_count-1; i > 0; i--) |
| 62 | if (ctx->dir_info[i-1].ino < ino) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 63 | break; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 64 | dir = &ctx->dir_info[i]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | if (dir->ino != ino) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 66 | for (j = ctx->dir_info_count++; j > i; j--) |
| 67 | ctx->dir_info[j] = ctx->dir_info[j-1]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 68 | } else |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 69 | dir = &ctx->dir_info[ctx->dir_info_count++]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 70 | |
| 71 | dir->ino = ino; |
| 72 | dir->dotdot = parent; |
| 73 | dir->parent = parent; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * get_dir_info() --- given an inode number, try to find the directory |
| 78 | * information entry for it. |
| 79 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 80 | struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ino_t ino) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 81 | { |
| 82 | int low, high, mid; |
| 83 | |
| 84 | low = 0; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 85 | high = ctx->dir_info_count-1; |
| 86 | if (!ctx->dir_info) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 87 | return 0; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 88 | if (ino == ctx->dir_info[low].ino) |
| 89 | return &ctx->dir_info[low]; |
| 90 | if (ino == ctx->dir_info[high].ino) |
| 91 | return &ctx->dir_info[high]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | |
| 93 | while (low < high) { |
| 94 | mid = (low+high)/2; |
| 95 | if (mid == low || mid == high) |
| 96 | break; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 97 | if (ino == ctx->dir_info[mid].ino) |
| 98 | return &ctx->dir_info[mid]; |
| 99 | if (ino < ctx->dir_info[mid].ino) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 100 | high = mid; |
| 101 | else |
| 102 | low = mid; |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Free the dir_info structure when it isn't needed any more. |
| 109 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 110 | void e2fsck_free_dir_info(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 111 | { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 112 | if (ctx->dir_info) { |
| 113 | ext2fs_free_mem((void **) &ctx->dir_info); |
| 114 | ctx->dir_info = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 115 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 116 | ctx->dir_info_size = 0; |
| 117 | ctx->dir_info_count = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 118 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 121 | * Return the count of number of directories in the dir_info structure |
| 122 | */ |
| 123 | int e2fsck_get_num_dirinfo(e2fsck_t ctx) |
| 124 | { |
| 125 | return ctx->dir_info_count; |
| 126 | } |
| 127 | |
| 128 | /* |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 129 | * A simple interator function |
| 130 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 131 | struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, int *control) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 132 | { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 133 | if (*control >= ctx->dir_info_count) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 136 | return(ctx->dir_info + (*control)++); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 137 | } |