Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * block.c --- iterate over all blocks in an inode |
| 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 17 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 18 | #if EXT2_FLAT_INCLUDES |
| 19 | #include "ext2_fs.h" |
| 20 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 22 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | |
| 24 | #include "ext2fs.h" |
| 25 | |
| 26 | struct block_context { |
| 27 | ext2_filsys fs; |
| 28 | int (*func)(ext2_filsys fs, |
| 29 | blk_t *blocknr, |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 30 | blkcnt_t bcount, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 31 | blk_t ref_blk, |
| 32 | int ref_offset, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 33 | void *priv_data); |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 34 | blkcnt_t bcount; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | int bsize; |
| 36 | int flags; |
| 37 | errcode_t errcode; |
| 38 | char *ind_buf; |
| 39 | char *dind_buf; |
| 40 | char *tind_buf; |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 41 | void *priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 44 | static int block_iterate_ind(blk_t *ind_block, blk_t ref_block, |
| 45 | int ref_offset, struct block_context *ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 46 | { |
| 47 | int ret = 0, changed = 0; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 48 | int i, flags, limit, offset; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 49 | blk_t *block_nr; |
| 50 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 51 | limit = ctx->fs->blocksize >> 2; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 52 | if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| 53 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY)) |
| 54 | ret = (*ctx->func)(ctx->fs, ind_block, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 55 | BLOCK_COUNT_IND, ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 56 | ref_offset, ctx->priv_data); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 57 | if (!*ind_block || (ret & BLOCK_ABORT)) { |
| 58 | ctx->bcount += limit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | return ret; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 60 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 61 | if (*ind_block >= ctx->fs->super->s_blocks_count || |
| 62 | *ind_block < ctx->fs->super->s_first_data_block) { |
| 63 | ctx->errcode = EXT2_ET_BAD_IND_BLOCK; |
| 64 | ret |= BLOCK_ERROR; |
| 65 | return ret; |
| 66 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | ctx->errcode = io_channel_read_blk(ctx->fs->io, *ind_block, |
| 68 | 1, ctx->ind_buf); |
| 69 | if (ctx->errcode) { |
| 70 | ret |= BLOCK_ERROR; |
| 71 | return ret; |
| 72 | } |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 73 | if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 74 | (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 75 | block_nr = (blk_t *) ctx->ind_buf; |
| 76 | for (i = 0; i < limit; i++, block_nr++) |
| 77 | *block_nr = ext2fs_swab32(*block_nr); |
| 78 | } |
| 79 | block_nr = (blk_t *) ctx->ind_buf; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 80 | offset = 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 81 | if (ctx->flags & BLOCK_FLAG_APPEND) { |
| 82 | for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 83 | flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 84 | *ind_block, offset, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 85 | ctx->priv_data); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 86 | changed |= flags; |
| 87 | if (flags & BLOCK_ABORT) { |
| 88 | ret |= BLOCK_ABORT; |
| 89 | break; |
| 90 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 91 | offset += sizeof(blk_t); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 92 | } |
| 93 | } else { |
| 94 | for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) { |
| 95 | if (*block_nr == 0) |
| 96 | continue; |
| 97 | flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 98 | *ind_block, offset, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 99 | ctx->priv_data); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 100 | changed |= flags; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 101 | if (flags & BLOCK_ABORT) { |
| 102 | ret |= BLOCK_ABORT; |
| 103 | break; |
| 104 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 105 | offset += sizeof(blk_t); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 108 | if (changed & BLOCK_CHANGED) { |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 109 | if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 110 | (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 111 | block_nr = (blk_t *) ctx->ind_buf; |
| 112 | for (i = 0; i < limit; i++, block_nr++) |
| 113 | *block_nr = ext2fs_swab32(*block_nr); |
| 114 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 115 | ctx->errcode = io_channel_write_blk(ctx->fs->io, *ind_block, |
| 116 | 1, ctx->ind_buf); |
| 117 | if (ctx->errcode) |
| 118 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| 119 | } |
| 120 | if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 121 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY) && |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 122 | !(ret & BLOCK_ABORT)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 123 | ret |= (*ctx->func)(ctx->fs, ind_block, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 124 | BLOCK_COUNT_IND, ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 125 | ref_offset, ctx->priv_data); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 129 | static int block_iterate_dind(blk_t *dind_block, blk_t ref_block, |
| 130 | int ref_offset, struct block_context *ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 131 | { |
| 132 | int ret = 0, changed = 0; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 133 | int i, flags, limit, offset; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 134 | blk_t *block_nr; |
| 135 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 136 | limit = ctx->fs->blocksize >> 2; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 137 | if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| 138 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY)) |
| 139 | ret = (*ctx->func)(ctx->fs, dind_block, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 140 | BLOCK_COUNT_DIND, ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 141 | ref_offset, ctx->priv_data); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 142 | if (!*dind_block || (ret & BLOCK_ABORT)) { |
| 143 | ctx->bcount += limit*limit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 144 | return ret; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 145 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 146 | if (*dind_block >= ctx->fs->super->s_blocks_count || |
| 147 | *dind_block < ctx->fs->super->s_first_data_block) { |
| 148 | ctx->errcode = EXT2_ET_BAD_DIND_BLOCK; |
| 149 | ret |= BLOCK_ERROR; |
| 150 | return ret; |
| 151 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 152 | ctx->errcode = io_channel_read_blk(ctx->fs->io, *dind_block, |
| 153 | 1, ctx->dind_buf); |
| 154 | if (ctx->errcode) { |
| 155 | ret |= BLOCK_ERROR; |
| 156 | return ret; |
| 157 | } |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 158 | if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 159 | (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 160 | block_nr = (blk_t *) ctx->dind_buf; |
| 161 | for (i = 0; i < limit; i++, block_nr++) |
| 162 | *block_nr = ext2fs_swab32(*block_nr); |
| 163 | } |
| 164 | block_nr = (blk_t *) ctx->dind_buf; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 165 | offset = 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 166 | if (ctx->flags & BLOCK_FLAG_APPEND) { |
| 167 | for (i = 0; i < limit; i++, block_nr++) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 168 | flags = block_iterate_ind(block_nr, |
| 169 | *dind_block, offset, |
| 170 | ctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 171 | changed |= flags; |
| 172 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 173 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 174 | break; |
| 175 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 176 | offset += sizeof(blk_t); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 177 | } |
| 178 | } else { |
| 179 | for (i = 0; i < limit; i++, block_nr++) { |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 180 | if (*block_nr == 0) { |
| 181 | ctx->bcount += limit; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 182 | continue; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 183 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 184 | flags = block_iterate_ind(block_nr, |
| 185 | *dind_block, offset, |
| 186 | ctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 187 | changed |= flags; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 188 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 189 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 190 | break; |
| 191 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 192 | offset += sizeof(blk_t); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 195 | if (changed & BLOCK_CHANGED) { |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 196 | if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 197 | (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 198 | block_nr = (blk_t *) ctx->dind_buf; |
| 199 | for (i = 0; i < limit; i++, block_nr++) |
| 200 | *block_nr = ext2fs_swab32(*block_nr); |
| 201 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 202 | ctx->errcode = io_channel_write_blk(ctx->fs->io, *dind_block, |
| 203 | 1, ctx->dind_buf); |
| 204 | if (ctx->errcode) |
| 205 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| 206 | } |
| 207 | if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 208 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY) && |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 209 | !(ret & BLOCK_ABORT)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 210 | ret |= (*ctx->func)(ctx->fs, dind_block, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 211 | BLOCK_COUNT_DIND, ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 212 | ref_offset, ctx->priv_data); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 216 | static int block_iterate_tind(blk_t *tind_block, blk_t ref_block, |
| 217 | int ref_offset, struct block_context *ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 218 | { |
| 219 | int ret = 0, changed = 0; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 220 | int i, flags, limit, offset; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 221 | blk_t *block_nr; |
| 222 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 223 | limit = ctx->fs->blocksize >> 2; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 224 | if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| 225 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY)) |
| 226 | ret = (*ctx->func)(ctx->fs, tind_block, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 227 | BLOCK_COUNT_TIND, ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 228 | ref_offset, ctx->priv_data); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 229 | if (!*tind_block || (ret & BLOCK_ABORT)) { |
| 230 | ctx->bcount += limit*limit*limit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 231 | return ret; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 232 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 233 | if (*tind_block >= ctx->fs->super->s_blocks_count || |
| 234 | *tind_block < ctx->fs->super->s_first_data_block) { |
| 235 | ctx->errcode = EXT2_ET_BAD_TIND_BLOCK; |
| 236 | ret |= BLOCK_ERROR; |
| 237 | return ret; |
| 238 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 239 | ctx->errcode = io_channel_read_blk(ctx->fs->io, *tind_block, |
| 240 | 1, ctx->tind_buf); |
| 241 | if (ctx->errcode) { |
| 242 | ret |= BLOCK_ERROR; |
| 243 | return ret; |
| 244 | } |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 245 | if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 246 | (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 247 | block_nr = (blk_t *) ctx->tind_buf; |
| 248 | for (i = 0; i < limit; i++, block_nr++) |
| 249 | *block_nr = ext2fs_swab32(*block_nr); |
| 250 | } |
| 251 | block_nr = (blk_t *) ctx->tind_buf; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 252 | offset = 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 253 | if (ctx->flags & BLOCK_FLAG_APPEND) { |
| 254 | for (i = 0; i < limit; i++, block_nr++) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 255 | flags = block_iterate_dind(block_nr, |
| 256 | *tind_block, |
| 257 | offset, ctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 258 | changed |= flags; |
| 259 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 260 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 261 | break; |
| 262 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 263 | offset += sizeof(blk_t); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 264 | } |
| 265 | } else { |
| 266 | for (i = 0; i < limit; i++, block_nr++) { |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 267 | if (*block_nr == 0) { |
| 268 | ctx->bcount += limit*limit; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 269 | continue; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 270 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 271 | flags = block_iterate_dind(block_nr, |
| 272 | *tind_block, |
| 273 | offset, ctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 274 | changed |= flags; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 275 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 276 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 277 | break; |
| 278 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 279 | offset += sizeof(blk_t); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 282 | if (changed & BLOCK_CHANGED) { |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 283 | if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 284 | (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 285 | block_nr = (blk_t *) ctx->tind_buf; |
| 286 | for (i = 0; i < limit; i++, block_nr++) |
| 287 | *block_nr = ext2fs_swab32(*block_nr); |
| 288 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 289 | ctx->errcode = io_channel_write_blk(ctx->fs->io, *tind_block, |
| 290 | 1, ctx->tind_buf); |
| 291 | if (ctx->errcode) |
| 292 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| 293 | } |
| 294 | if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 295 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY) && |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 296 | !(ret & BLOCK_ABORT)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 297 | ret |= (*ctx->func)(ctx->fs, tind_block, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 298 | BLOCK_COUNT_TIND, ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 299 | ref_offset, ctx->priv_data); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 304 | errcode_t ext2fs_block_iterate2(ext2_filsys fs, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 305 | ino_t ino, |
| 306 | int flags, |
| 307 | char *block_buf, |
| 308 | int (*func)(ext2_filsys fs, |
| 309 | blk_t *blocknr, |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 310 | blkcnt_t blockcnt, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 311 | blk_t ref_blk, |
| 312 | int ref_offset, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 313 | void *priv_data), |
| 314 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 315 | { |
| 316 | int i; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 317 | int got_inode = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 318 | int ret = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 319 | blk_t blocks[EXT2_N_BLOCKS]; /* directory data blocks */ |
| 320 | struct ext2_inode inode; |
| 321 | errcode_t retval; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 322 | struct block_context ctx; |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 323 | int limit; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 324 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 325 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 326 | |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 327 | /* |
| 328 | * Check to see if we need to limit large files |
| 329 | */ |
| 330 | if (flags & BLOCK_FLAG_NO_LARGE) { |
| 331 | ctx.errcode = ext2fs_read_inode(fs, ino, &inode); |
| 332 | if (ctx.errcode) |
| 333 | goto abort; |
| 334 | got_inode = 1; |
| 335 | if (!LINUX_S_ISDIR(inode.i_mode) && |
| 336 | (inode.i_size_high != 0)) |
| 337 | return EXT2_ET_FILE_TOO_BIG; |
| 338 | } |
| 339 | |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 340 | retval = ext2fs_get_blocks(fs, ino, blocks); |
| 341 | if (retval) |
| 342 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 343 | |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 344 | limit = fs->blocksize >> 2; |
| 345 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 346 | ctx.fs = fs; |
| 347 | ctx.func = func; |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 348 | ctx.priv_data = priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 349 | ctx.flags = flags; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 350 | ctx.bcount = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 351 | if (block_buf) { |
| 352 | ctx.ind_buf = block_buf; |
| 353 | } else { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 354 | retval = ext2fs_get_mem(fs->blocksize * 3, |
| 355 | (void **) &ctx.ind_buf); |
| 356 | if (retval) |
| 357 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 358 | } |
| 359 | ctx.dind_buf = ctx.ind_buf + fs->blocksize; |
| 360 | ctx.tind_buf = ctx.dind_buf + fs->blocksize; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Iterate over the HURD translator block (if present) |
| 364 | */ |
| 365 | if ((fs->super->s_creator_os == EXT2_OS_HURD) && |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 366 | !(flags & BLOCK_FLAG_DATA_ONLY)) { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 367 | ctx.errcode = ext2fs_read_inode(fs, ino, &inode); |
| 368 | if (ctx.errcode) |
| 369 | goto abort; |
| 370 | got_inode = 1; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 371 | if (inode.osd1.hurd1.h_i_translator) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 372 | ret |= (*ctx.func)(fs, |
| 373 | &inode.osd1.hurd1.h_i_translator, |
| 374 | BLOCK_COUNT_TRANSLATOR, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 375 | 0, 0, priv_data); |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 376 | if (ret & BLOCK_ABORT) |
| 377 | goto abort; |
| 378 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 379 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 380 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 381 | /* |
| 382 | * Iterate over normal data blocks |
| 383 | */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 384 | for (i = 0; i < EXT2_NDIR_BLOCKS ; i++, ctx.bcount++) { |
| 385 | if (blocks[i] || (flags & BLOCK_FLAG_APPEND)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 386 | ret |= (*ctx.func)(fs, &blocks[i], |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 387 | ctx.bcount, 0, i, priv_data); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 388 | if (ret & BLOCK_ABORT) |
| 389 | goto abort; |
| 390 | } |
| 391 | } |
| 392 | if (*(blocks + EXT2_IND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 393 | ret |= block_iterate_ind(blocks + EXT2_IND_BLOCK, |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 394 | 0, EXT2_IND_BLOCK, &ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 395 | if (ret & BLOCK_ABORT) |
| 396 | goto abort; |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 397 | } else |
| 398 | ctx.bcount += limit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 399 | if (*(blocks + EXT2_DIND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 400 | ret |= block_iterate_dind(blocks + EXT2_DIND_BLOCK, |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 401 | 0, EXT2_DIND_BLOCK, &ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 402 | if (ret & BLOCK_ABORT) |
| 403 | goto abort; |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 404 | } else |
| 405 | ctx.bcount += limit * limit; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 406 | if (*(blocks + EXT2_TIND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 407 | ret |= block_iterate_tind(blocks + EXT2_TIND_BLOCK, |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 408 | 0, EXT2_TIND_BLOCK, &ctx); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 409 | if (ret & BLOCK_ABORT) |
| 410 | goto abort; |
| 411 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 412 | |
| 413 | abort: |
| 414 | if (ret & BLOCK_CHANGED) { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 415 | if (!got_inode) { |
| 416 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 417 | if (retval) |
| 418 | return retval; |
| 419 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 420 | for (i=0; i < EXT2_N_BLOCKS; i++) |
| 421 | inode.i_block[i] = blocks[i]; |
| 422 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 423 | if (retval) |
| 424 | return retval; |
| 425 | } |
| 426 | |
| 427 | if (!block_buf) |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 428 | ext2fs_free_mem((void **) &ctx.ind_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 429 | |
| 430 | return (ret & BLOCK_ERROR) ? ctx.errcode : 0; |
| 431 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 432 | |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 433 | /* |
| 434 | * Emulate the old ext2fs_block_iterate function! |
| 435 | */ |
| 436 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 437 | struct xlate { |
| 438 | int (*func)(ext2_filsys fs, |
| 439 | blk_t *blocknr, |
| 440 | int bcount, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 441 | void *priv_data); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 442 | void *real_private; |
| 443 | }; |
| 444 | |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 445 | #ifdef __TURBOC__ |
| 446 | #pragma argsused |
| 447 | #endif |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 448 | static int xlate_func(ext2_filsys fs, blk_t *blocknr, blkcnt_t blockcnt, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 449 | blk_t ref_block, int ref_offset, void *priv_data) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 450 | { |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 451 | struct xlate *xl = (struct xlate *) priv_data; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 452 | |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 453 | return (*xl->func)(fs, blocknr, (int) blockcnt, xl->real_private); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | errcode_t ext2fs_block_iterate(ext2_filsys fs, |
| 457 | ino_t ino, |
| 458 | int flags, |
| 459 | char *block_buf, |
| 460 | int (*func)(ext2_filsys fs, |
| 461 | blk_t *blocknr, |
| 462 | int blockcnt, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 463 | void *priv_data), |
| 464 | void *priv_data) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 465 | { |
| 466 | struct xlate xl; |
| 467 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 468 | xl.real_private = priv_data; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 469 | xl.func = func; |
| 470 | |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 471 | return ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_NO_LARGE | flags, |
Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 472 | block_buf, xlate_func, &xl); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 473 | } |
| 474 | |