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