"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * brel_ma.c |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 1996, 1997 Theodore Ts'o. |
| 6 | * |
| 7 | * TODO: rewrite to not use a direct array!!! (Fortunately this |
| 8 | * module isn't really used yet.) |
| 9 | * |
| 10 | * %Begin-Header% |
| 11 | * This file may be redistributed under the terms of the GNU Public |
| 12 | * License. |
| 13 | * %End-Header% |
| 14 | */ |
| 15 | |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #if HAVE_UNISTD_H |
| 20 | #include <unistd.h> |
| 21 | #endif |
| 22 | #if HAVE_ERRNO_H |
| 23 | #include <errno.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "ext2_fs.h" |
| 27 | #include "ext2fs.h" |
| 28 | #include "brel.h" |
| 29 | |
| 30 | static errcode_t bma_put(ext2_brel brel, blk_t old, |
| 31 | struct ext2_block_relocate_entry *ent); |
| 32 | static errcode_t bma_get(ext2_brel brel, blk_t old, |
| 33 | struct ext2_block_relocate_entry *ent); |
| 34 | static errcode_t bma_start_iter(ext2_brel brel); |
| 35 | static errcode_t bma_next(ext2_brel brel, blk_t *old, |
| 36 | struct ext2_block_relocate_entry *ent); |
| 37 | static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new); |
| 38 | static errcode_t bma_delete(ext2_brel brel, blk_t old); |
| 39 | static errcode_t bma_free(ext2_brel brel); |
| 40 | |
| 41 | struct brel_ma { |
| 42 | __u32 magic; |
| 43 | blk_t max_block; |
| 44 | struct ext2_block_relocate_entry *entries; |
| 45 | }; |
| 46 | |
| 47 | errcode_t ext2fs_brel_memarray_create(char *name, blk_t max_block, |
| 48 | ext2_brel *new_brel) |
| 49 | { |
| 50 | ext2_brel brel = 0; |
| 51 | errcode_t retval; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 52 | struct brel_ma *ma = 0; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 53 | size_t size; |
| 54 | |
| 55 | *new_brel = 0; |
| 56 | |
| 57 | /* |
| 58 | * Allocate memory structures |
| 59 | */ |
| 60 | retval = ext2fs_get_mem(sizeof(struct ext2_block_relocation_table), |
| 61 | &brel); |
| 62 | if (retval) |
| 63 | goto errout; |
| 64 | memset(brel, 0, sizeof(struct ext2_block_relocation_table)); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 65 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 66 | retval = ext2fs_get_mem(strlen(name)+1, &brel->name); |
| 67 | if (retval) |
| 68 | goto errout; |
| 69 | strcpy(brel->name, name); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 70 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 71 | retval = ext2fs_get_mem(sizeof(struct brel_ma), &ma); |
| 72 | if (retval) |
| 73 | goto errout; |
| 74 | memset(ma, 0, sizeof(struct brel_ma)); |
| 75 | brel->priv_data = ma; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 76 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 77 | size = (size_t) (sizeof(struct ext2_block_relocate_entry) * |
| 78 | (max_block+1)); |
| 79 | retval = ext2fs_get_mem(size, &ma->entries); |
| 80 | if (retval) |
| 81 | goto errout; |
| 82 | memset(ma->entries, 0, size); |
| 83 | ma->max_block = max_block; |
| 84 | |
| 85 | /* |
| 86 | * Fill in the brel data structure |
| 87 | */ |
| 88 | brel->put = bma_put; |
| 89 | brel->get = bma_get; |
| 90 | brel->start_iter = bma_start_iter; |
| 91 | brel->next = bma_next; |
| 92 | brel->move = bma_move; |
| 93 | brel->delete = bma_delete; |
| 94 | brel->free = bma_free; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 95 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 96 | *new_brel = brel; |
| 97 | return 0; |
| 98 | |
| 99 | errout: |
| 100 | bma_free(brel); |
| 101 | return retval; |
| 102 | } |
| 103 | |
| 104 | static errcode_t bma_put(ext2_brel brel, blk_t old, |
| 105 | struct ext2_block_relocate_entry *ent) |
| 106 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 107 | struct brel_ma *ma; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 108 | |
| 109 | ma = brel->priv_data; |
| 110 | if (old > ma->max_block) |
| 111 | return EXT2_ET_INVALID_ARGUMENT; |
| 112 | ma->entries[(unsigned)old] = *ent; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static errcode_t bma_get(ext2_brel brel, blk_t old, |
| 117 | struct ext2_block_relocate_entry *ent) |
| 118 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 119 | struct brel_ma *ma; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 120 | |
| 121 | ma = brel->priv_data; |
| 122 | if (old > ma->max_block) |
| 123 | return EXT2_ET_INVALID_ARGUMENT; |
| 124 | if (ma->entries[(unsigned)old].new == 0) |
| 125 | return ENOENT; |
| 126 | *ent = ma->entries[old]; |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static errcode_t bma_start_iter(ext2_brel brel) |
| 131 | { |
| 132 | brel->current = 0; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static errcode_t bma_next(ext2_brel brel, blk_t *old, |
| 137 | struct ext2_block_relocate_entry *ent) |
| 138 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 139 | struct brel_ma *ma; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 140 | |
| 141 | ma = brel->priv_data; |
| 142 | while (++brel->current < ma->max_block) { |
| 143 | if (ma->entries[(unsigned)brel->current].new == 0) |
| 144 | continue; |
| 145 | *old = brel->current; |
| 146 | *ent = ma->entries[(unsigned)brel->current]; |
| 147 | return 0; |
| 148 | } |
| 149 | *old = 0; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new) |
| 154 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 155 | struct brel_ma *ma; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 156 | |
| 157 | ma = brel->priv_data; |
| 158 | if ((old > ma->max_block) || (new > ma->max_block)) |
| 159 | return EXT2_ET_INVALID_ARGUMENT; |
| 160 | if (ma->entries[(unsigned)old].new == 0) |
| 161 | return ENOENT; |
| 162 | ma->entries[(unsigned)new] = ma->entries[old]; |
| 163 | ma->entries[(unsigned)old].new = 0; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static errcode_t bma_delete(ext2_brel brel, blk_t old) |
| 168 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 169 | struct brel_ma *ma; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 170 | |
| 171 | ma = brel->priv_data; |
| 172 | if (old > ma->max_block) |
| 173 | return EXT2_ET_INVALID_ARGUMENT; |
| 174 | if (ma->entries[(unsigned)old].new == 0) |
| 175 | return ENOENT; |
| 176 | ma->entries[(unsigned)old].new = 0; |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static errcode_t bma_free(ext2_brel brel) |
| 181 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 182 | struct brel_ma *ma; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 183 | |
| 184 | if (!brel) |
| 185 | return 0; |
| 186 | |
| 187 | ma = brel->priv_data; |
| 188 | |
| 189 | if (ma) { |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 190 | ext2fs_free_mem(&ma->entries); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 191 | ext2fs_free_mem(&ma); |
| 192 | } |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 193 | ext2fs_free_mem(&brel->name); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 194 | ext2fs_free_mem(&brel); |
| 195 | return 0; |
| 196 | } |