| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * block.c --- iterate over all blocks in an inode |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 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 | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 39 | #define check_for_ro_violation_return(ctx, ret) \ |
| Theodore Ts'o | 357d186 | 2008-02-02 21:26:54 -0500 | [diff] [blame] | 40 | do { \ |
| 41 | if (((ctx)->flags & BLOCK_FLAG_READ_ONLY) && \ |
| 42 | ((ret) & BLOCK_CHANGED)) { \ |
| 43 | (ctx)->errcode = EXT2_ET_RO_BLOCK_ITERATE; \ |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 44 | ret |= BLOCK_ABORT | BLOCK_ERROR; \ |
| 45 | return ret; \ |
| Theodore Ts'o | 357d186 | 2008-02-02 21:26:54 -0500 | [diff] [blame] | 46 | } \ |
| 47 | } while (0) |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 48 | |
| 49 | #define check_for_ro_violation_goto(ctx, ret, label) \ |
| 50 | do { \ |
| 51 | if (((ctx)->flags & BLOCK_FLAG_READ_ONLY) && \ |
| 52 | ((ret) & BLOCK_CHANGED)) { \ |
| 53 | (ctx)->errcode = EXT2_ET_RO_BLOCK_ITERATE; \ |
| 54 | ret |= BLOCK_ABORT | BLOCK_ERROR; \ |
| 55 | goto label; \ |
| 56 | } \ |
| 57 | } while (0) |
| Theodore Ts'o | 357d186 | 2008-02-02 21:26:54 -0500 | [diff] [blame] | 58 | |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 59 | static int block_iterate_ind(blk_t *ind_block, blk_t ref_block, |
| 60 | int ref_offset, struct block_context *ctx) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 61 | { |
| 62 | int ret = 0, changed = 0; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 63 | int i, flags, limit, offset; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | blk_t *block_nr; |
| 65 | |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 66 | limit = ctx->fs->blocksize >> 2; |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 67 | if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| 68 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY)) |
| 69 | ret = (*ctx->func)(ctx->fs, ind_block, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 70 | BLOCK_COUNT_IND, ref_block, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 71 | ref_offset, ctx->priv_data); |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 72 | check_for_ro_violation_return(ctx, ret); |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 73 | if (!*ind_block || (ret & BLOCK_ABORT)) { |
| 74 | ctx->bcount += limit; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | return ret; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 76 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 77 | if (*ind_block >= ctx->fs->super->s_blocks_count || |
| 78 | *ind_block < ctx->fs->super->s_first_data_block) { |
| 79 | ctx->errcode = EXT2_ET_BAD_IND_BLOCK; |
| 80 | ret |= BLOCK_ERROR; |
| 81 | return ret; |
| 82 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 83 | ctx->errcode = ext2fs_read_ind_block(ctx->fs, *ind_block, |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 84 | ctx->ind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 85 | if (ctx->errcode) { |
| 86 | ret |= BLOCK_ERROR; |
| 87 | return ret; |
| 88 | } |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 89 | |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 90 | block_nr = (blk_t *) ctx->ind_buf; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 91 | offset = 0; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 92 | if (ctx->flags & BLOCK_FLAG_APPEND) { |
| 93 | for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) { |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 94 | flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount, |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 95 | *ind_block, offset, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 96 | ctx->priv_data); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 97 | changed |= flags; |
| 98 | if (flags & BLOCK_ABORT) { |
| 99 | ret |= BLOCK_ABORT; |
| 100 | break; |
| 101 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 102 | offset += sizeof(blk_t); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 103 | } |
| 104 | } else { |
| 105 | for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) { |
| 106 | if (*block_nr == 0) |
| 107 | continue; |
| 108 | flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount, |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 109 | *ind_block, offset, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 110 | ctx->priv_data); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 111 | changed |= flags; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 112 | if (flags & BLOCK_ABORT) { |
| 113 | ret |= BLOCK_ABORT; |
| 114 | break; |
| 115 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 116 | offset += sizeof(blk_t); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 119 | check_for_ro_violation_return(ctx, changed); |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 120 | if (changed & BLOCK_CHANGED) { |
| 121 | ctx->errcode = ext2fs_write_ind_block(ctx->fs, *ind_block, |
| 122 | ctx->ind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 123 | if (ctx->errcode) |
| 124 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| 125 | } |
| 126 | if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 127 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY) && |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 128 | !(ret & BLOCK_ABORT)) |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 129 | ret |= (*ctx->func)(ctx->fs, ind_block, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 130 | BLOCK_COUNT_IND, ref_block, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 131 | ref_offset, ctx->priv_data); |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 132 | check_for_ro_violation_return(ctx, ret); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 133 | return ret; |
| 134 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 135 | |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 136 | static int block_iterate_dind(blk_t *dind_block, blk_t ref_block, |
| 137 | int ref_offset, struct block_context *ctx) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 138 | { |
| 139 | int ret = 0, changed = 0; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 140 | int i, flags, limit, offset; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 141 | blk_t *block_nr; |
| 142 | |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 143 | limit = ctx->fs->blocksize >> 2; |
| Theodore Ts'o | 06af47f | 2000-04-03 13:51:00 +0000 | [diff] [blame] | 144 | if (!(ctx->flags & (BLOCK_FLAG_DEPTH_TRAVERSE | |
| 145 | BLOCK_FLAG_DATA_ONLY))) |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 146 | ret = (*ctx->func)(ctx->fs, dind_block, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 147 | BLOCK_COUNT_DIND, ref_block, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 148 | ref_offset, ctx->priv_data); |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 149 | check_for_ro_violation_return(ctx, ret); |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 150 | if (!*dind_block || (ret & BLOCK_ABORT)) { |
| 151 | ctx->bcount += limit*limit; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 152 | return ret; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 153 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 154 | if (*dind_block >= ctx->fs->super->s_blocks_count || |
| 155 | *dind_block < ctx->fs->super->s_first_data_block) { |
| 156 | ctx->errcode = EXT2_ET_BAD_DIND_BLOCK; |
| 157 | ret |= BLOCK_ERROR; |
| 158 | return ret; |
| 159 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 160 | ctx->errcode = ext2fs_read_ind_block(ctx->fs, *dind_block, |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 161 | ctx->dind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 162 | if (ctx->errcode) { |
| 163 | ret |= BLOCK_ERROR; |
| 164 | return ret; |
| 165 | } |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 166 | |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 167 | block_nr = (blk_t *) ctx->dind_buf; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 168 | offset = 0; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 169 | if (ctx->flags & BLOCK_FLAG_APPEND) { |
| 170 | for (i = 0; i < limit; i++, block_nr++) { |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 171 | flags = block_iterate_ind(block_nr, |
| 172 | *dind_block, offset, |
| 173 | ctx); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 174 | changed |= flags; |
| 175 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 176 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 177 | break; |
| 178 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 179 | offset += sizeof(blk_t); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | for (i = 0; i < limit; i++, block_nr++) { |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 183 | if (*block_nr == 0) { |
| 184 | ctx->bcount += limit; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 185 | continue; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 186 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 187 | flags = block_iterate_ind(block_nr, |
| 188 | *dind_block, offset, |
| 189 | ctx); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 190 | changed |= flags; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 191 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 192 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 193 | break; |
| 194 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 195 | offset += sizeof(blk_t); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 198 | check_for_ro_violation_return(ctx, changed); |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 199 | if (changed & BLOCK_CHANGED) { |
| 200 | ctx->errcode = ext2fs_write_ind_block(ctx->fs, *dind_block, |
| 201 | ctx->dind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 202 | if (ctx->errcode) |
| 203 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| 204 | } |
| 205 | if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 206 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY) && |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 207 | !(ret & BLOCK_ABORT)) |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 208 | ret |= (*ctx->func)(ctx->fs, dind_block, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 209 | BLOCK_COUNT_DIND, ref_block, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 210 | ref_offset, ctx->priv_data); |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 211 | check_for_ro_violation_return(ctx, ret); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 212 | return ret; |
| 213 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 214 | |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 215 | static int block_iterate_tind(blk_t *tind_block, blk_t ref_block, |
| 216 | int ref_offset, struct block_context *ctx) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 217 | { |
| 218 | int ret = 0, changed = 0; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 219 | int i, flags, limit, offset; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 220 | blk_t *block_nr; |
| 221 | |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 222 | limit = ctx->fs->blocksize >> 2; |
| Theodore Ts'o | 06af47f | 2000-04-03 13:51:00 +0000 | [diff] [blame] | 223 | if (!(ctx->flags & (BLOCK_FLAG_DEPTH_TRAVERSE | |
| 224 | BLOCK_FLAG_DATA_ONLY))) |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 225 | ret = (*ctx->func)(ctx->fs, tind_block, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 226 | BLOCK_COUNT_TIND, ref_block, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 227 | ref_offset, ctx->priv_data); |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 228 | check_for_ro_violation_return(ctx, ret); |
| 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 | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 239 | ctx->errcode = ext2fs_read_ind_block(ctx->fs, *tind_block, |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 240 | ctx->tind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 241 | if (ctx->errcode) { |
| 242 | ret |= BLOCK_ERROR; |
| 243 | return ret; |
| 244 | } |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 245 | |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 246 | block_nr = (blk_t *) ctx->tind_buf; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 247 | offset = 0; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 248 | if (ctx->flags & BLOCK_FLAG_APPEND) { |
| 249 | for (i = 0; i < limit; i++, block_nr++) { |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 250 | flags = block_iterate_dind(block_nr, |
| 251 | *tind_block, |
| 252 | offset, ctx); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 253 | changed |= flags; |
| 254 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 255 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 256 | break; |
| 257 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 258 | offset += sizeof(blk_t); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 259 | } |
| 260 | } else { |
| 261 | for (i = 0; i < limit; i++, block_nr++) { |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 262 | if (*block_nr == 0) { |
| 263 | ctx->bcount += limit*limit; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 264 | continue; |
| Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 265 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 266 | flags = block_iterate_dind(block_nr, |
| 267 | *tind_block, |
| 268 | offset, ctx); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 269 | changed |= flags; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 270 | if (flags & (BLOCK_ABORT | BLOCK_ERROR)) { |
| 271 | ret |= flags & (BLOCK_ABORT | BLOCK_ERROR); |
| 272 | break; |
| 273 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 274 | offset += sizeof(blk_t); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 277 | check_for_ro_violation_return(ctx, changed); |
| Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 278 | if (changed & BLOCK_CHANGED) { |
| 279 | ctx->errcode = ext2fs_write_ind_block(ctx->fs, *tind_block, |
| 280 | ctx->tind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 281 | if (ctx->errcode) |
| 282 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| 283 | } |
| 284 | if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) && |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 285 | !(ctx->flags & BLOCK_FLAG_DATA_ONLY) && |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 286 | !(ret & BLOCK_ABORT)) |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 287 | ret |= (*ctx->func)(ctx->fs, tind_block, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 288 | BLOCK_COUNT_TIND, ref_block, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 289 | ref_offset, ctx->priv_data); |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 290 | check_for_ro_violation_return(ctx, ret); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 291 | return ret; |
| 292 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 293 | |
| Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 294 | errcode_t ext2fs_block_iterate2(ext2_filsys fs, |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 295 | ext2_ino_t ino, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 296 | int flags, |
| 297 | char *block_buf, |
| 298 | int (*func)(ext2_filsys fs, |
| 299 | blk_t *blocknr, |
| Theodore Ts'o | 03673db | 1998-06-10 20:39:43 +0000 | [diff] [blame] | 300 | e2_blkcnt_t blockcnt, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 301 | blk_t ref_blk, |
| 302 | int ref_offset, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 303 | void *priv_data), |
| 304 | void *priv_data) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 305 | { |
| 306 | int i; |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 307 | int r, ret = 0; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 308 | struct ext2_inode inode; |
| 309 | errcode_t retval; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 310 | struct block_context ctx; |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 311 | int limit; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 312 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 313 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 314 | |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 315 | ctx.errcode = ext2fs_read_inode(fs, ino, &inode); |
| 316 | if (ctx.errcode) |
| 317 | return ctx.errcode; |
| 318 | |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 319 | /* |
| 320 | * Check to see if we need to limit large files |
| 321 | */ |
| 322 | if (flags & BLOCK_FLAG_NO_LARGE) { |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 323 | if (!LINUX_S_ISDIR(inode.i_mode) && |
| 324 | (inode.i_size_high != 0)) |
| 325 | return EXT2_ET_FILE_TOO_BIG; |
| 326 | } |
| 327 | |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 328 | limit = fs->blocksize >> 2; |
| 329 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 330 | ctx.fs = fs; |
| 331 | ctx.func = func; |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 332 | ctx.priv_data = priv_data; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 333 | ctx.flags = flags; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 334 | ctx.bcount = 0; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 335 | if (block_buf) { |
| 336 | ctx.ind_buf = block_buf; |
| 337 | } else { |
| Theodore Ts'o | ee01079 | 2007-11-09 19:01:06 -0500 | [diff] [blame] | 338 | retval = ext2fs_get_array(3, fs->blocksize, &ctx.ind_buf); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 339 | if (retval) |
| 340 | return retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 341 | } |
| 342 | ctx.dind_buf = ctx.ind_buf + fs->blocksize; |
| 343 | ctx.tind_buf = ctx.dind_buf + fs->blocksize; |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | * Iterate over the HURD translator block (if present) |
| 347 | */ |
| 348 | if ((fs->super->s_creator_os == EXT2_OS_HURD) && |
| Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 349 | !(flags & BLOCK_FLAG_DATA_ONLY)) { |
| Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 350 | if (inode.osd1.hurd1.h_i_translator) { |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 351 | ret |= (*ctx.func)(fs, |
| 352 | &inode.osd1.hurd1.h_i_translator, |
| 353 | BLOCK_COUNT_TRANSLATOR, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 354 | 0, 0, priv_data); |
| Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 355 | if (ret & BLOCK_ABORT) |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 356 | goto abort_exit; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 357 | check_for_ro_violation_goto(&ctx, ret, abort_exit); |
| Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 358 | } |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 359 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 360 | |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 361 | if (inode.i_flags & EXT4_EXTENTS_FL) { |
| 362 | ext2_extent_handle_t handle; |
| 363 | struct ext2fs_extent extent; |
| Theodore Ts'o | 685d544 | 2008-08-27 15:17:36 -0400 | [diff] [blame] | 364 | e2_blkcnt_t blockcnt = 0; |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 365 | blk_t blk, new_blk; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 366 | int op = EXT2_EXTENT_ROOT; |
| Theodore Ts'o | 07f1a07 | 2009-01-19 19:30:59 -0500 | [diff] [blame^] | 367 | int uninit; |
| Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 368 | unsigned int j; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 369 | |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 370 | ctx.errcode = ext2fs_extent_open(fs, ino, &handle); |
| 371 | if (ctx.errcode) |
| 372 | goto abort_exit; |
| 373 | |
| 374 | while (1) { |
| 375 | ctx.errcode = ext2fs_extent_get(handle, op, &extent); |
| 376 | if (ctx.errcode) { |
| Theodore Ts'o | 685d544 | 2008-08-27 15:17:36 -0400 | [diff] [blame] | 377 | if (ctx.errcode != EXT2_ET_EXTENT_NO_NEXT) |
| 378 | break; |
| 379 | ctx.errcode = 0; |
| 380 | if (!(flags & BLOCK_FLAG_APPEND)) |
| 381 | break; |
| 382 | blk = 0; |
| 383 | r = (*ctx.func)(fs, &blk, blockcnt, |
| 384 | 0, 0, priv_data); |
| 385 | ret |= r; |
| 386 | check_for_ro_violation_goto(&ctx, ret, |
| 387 | extent_errout); |
| 388 | if (r & BLOCK_CHANGED) { |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 389 | ctx.errcode = |
| Theodore Ts'o | 685d544 | 2008-08-27 15:17:36 -0400 | [diff] [blame] | 390 | ext2fs_extent_set_bmap(handle, |
| 391 | (blk64_t) blockcnt++, |
| 392 | (blk64_t) blk, 0); |
| 393 | if (ctx.errcode || (ret & BLOCK_ABORT)) |
| Theodore Ts'o | 64987c0 | 2008-08-27 17:50:14 -0400 | [diff] [blame] | 394 | break; |
| Theodore Ts'o | 685d544 | 2008-08-27 15:17:36 -0400 | [diff] [blame] | 395 | continue; |
| Theodore Ts'o | 685d544 | 2008-08-27 15:17:36 -0400 | [diff] [blame] | 396 | } |
| Theodore Ts'o | 64987c0 | 2008-08-27 17:50:14 -0400 | [diff] [blame] | 397 | break; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | op = EXT2_EXTENT_NEXT; |
| 401 | blk = extent.e_pblk; |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 402 | if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) { |
| 403 | if (ctx.flags & BLOCK_FLAG_DATA_ONLY) |
| 404 | continue; |
| 405 | if ((!(extent.e_flags & |
| 406 | EXT2_EXTENT_FLAGS_SECOND_VISIT) && |
| 407 | !(ctx.flags & BLOCK_FLAG_DEPTH_TRAVERSE)) || |
| 408 | ((extent.e_flags & |
| 409 | EXT2_EXTENT_FLAGS_SECOND_VISIT) && |
| 410 | (ctx.flags & BLOCK_FLAG_DEPTH_TRAVERSE))) { |
| 411 | ret |= (*ctx.func)(fs, &blk, |
| 412 | -1, 0, 0, priv_data); |
| 413 | if (ret & BLOCK_CHANGED) { |
| Theodore Ts'o | 213fe92 | 2008-08-22 02:50:02 -0400 | [diff] [blame] | 414 | extent.e_pblk = blk; |
| 415 | ctx.errcode = |
| 416 | ext2fs_extent_replace(handle, 0, &extent); |
| 417 | if (ctx.errcode) |
| Theodore Ts'o | 64987c0 | 2008-08-27 17:50:14 -0400 | [diff] [blame] | 418 | break; |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 419 | } |
| 420 | } |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 421 | continue; |
| 422 | } |
| Theodore Ts'o | 07f1a07 | 2009-01-19 19:30:59 -0500 | [diff] [blame^] | 423 | uninit = 0; |
| 424 | if (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) |
| 425 | uninit = EXT2_EXTENT_SET_BMAP_UNINIT; |
| Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 426 | for (blockcnt = extent.e_lblk, j = 0; |
| 427 | j < extent.e_len; |
| 428 | blk++, blockcnt++, j++) { |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 429 | new_blk = blk; |
| 430 | r = (*ctx.func)(fs, &new_blk, blockcnt, |
| 431 | 0, 0, priv_data); |
| 432 | ret |= r; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 433 | check_for_ro_violation_goto(&ctx, ret, |
| 434 | extent_errout); |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 435 | if (r & BLOCK_CHANGED) { |
| 436 | ctx.errcode = |
| 437 | ext2fs_extent_set_bmap(handle, |
| 438 | (blk64_t) blockcnt, |
| Theodore Ts'o | 07f1a07 | 2009-01-19 19:30:59 -0500 | [diff] [blame^] | 439 | (blk64_t) new_blk, |
| 440 | uninit); |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 441 | if (ctx.errcode) |
| Theodore Ts'o | d3a8fc5 | 2009-01-19 14:22:52 -0500 | [diff] [blame] | 442 | goto extent_errout; |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 443 | } |
| Theodore Ts'o | 64987c0 | 2008-08-27 17:50:14 -0400 | [diff] [blame] | 444 | if (ret & BLOCK_ABORT) |
| 445 | break; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | extent_errout: |
| 450 | ext2fs_extent_free(handle); |
| 451 | ret |= BLOCK_ERROR | BLOCK_ABORT; |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 452 | goto errout; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 453 | } |
| 454 | |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 455 | /* |
| 456 | * Iterate over normal data blocks |
| 457 | */ |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 458 | for (i = 0; i < EXT2_NDIR_BLOCKS ; i++, ctx.bcount++) { |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 459 | if (inode.i_block[i] || (flags & BLOCK_FLAG_APPEND)) { |
| 460 | ret |= (*ctx.func)(fs, &inode.i_block[i], |
| Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 461 | ctx.bcount, 0, i, priv_data); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 462 | if (ret & BLOCK_ABORT) |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 463 | goto abort_exit; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 466 | check_for_ro_violation_goto(&ctx, ret, abort_exit); |
| 467 | if (inode.i_block[EXT2_IND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) { |
| 468 | ret |= block_iterate_ind(&inode.i_block[EXT2_IND_BLOCK], |
| Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 469 | 0, EXT2_IND_BLOCK, &ctx); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 470 | if (ret & BLOCK_ABORT) |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 471 | goto abort_exit; |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 472 | } else |
| 473 | ctx.bcount += limit; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 474 | if (inode.i_block[EXT2_DIND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) { |
| 475 | ret |= block_iterate_dind(&inode.i_block[EXT2_DIND_BLOCK], |
| Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 476 | 0, EXT2_DIND_BLOCK, &ctx); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 477 | if (ret & BLOCK_ABORT) |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 478 | goto abort_exit; |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 479 | } else |
| 480 | ctx.bcount += limit * limit; |
| Theodore Ts'o | 206fea6 | 2008-02-03 22:29:16 -0500 | [diff] [blame] | 481 | if (inode.i_block[EXT2_TIND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) { |
| 482 | ret |= block_iterate_tind(&inode.i_block[EXT2_TIND_BLOCK], |
| Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 483 | 0, EXT2_TIND_BLOCK, &ctx); |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 484 | if (ret & BLOCK_ABORT) |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 485 | goto abort_exit; |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 486 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 487 | |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 488 | abort_exit: |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 489 | if (ret & BLOCK_CHANGED) { |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 490 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 491 | if (retval) |
| 492 | return retval; |
| 493 | } |
| Theodore Ts'o | d7b9220 | 2008-05-27 06:59:32 -0400 | [diff] [blame] | 494 | errout: |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 495 | if (!block_buf) |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 496 | ext2fs_free_mem(&ctx.ind_buf); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 497 | |
| 498 | return (ret & BLOCK_ERROR) ? ctx.errcode : 0; |
| 499 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 500 | |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 501 | /* |
| 502 | * Emulate the old ext2fs_block_iterate function! |
| 503 | */ |
| 504 | |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 505 | struct xlate { |
| 506 | int (*func)(ext2_filsys fs, |
| 507 | blk_t *blocknr, |
| 508 | int bcount, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 509 | void *priv_data); |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 510 | void *real_private; |
| 511 | }; |
| 512 | |
| Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 513 | #ifdef __TURBOC__ |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 514 | #pragma argsused |
| Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 515 | #endif |
| Theodore Ts'o | 03673db | 1998-06-10 20:39:43 +0000 | [diff] [blame] | 516 | 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] | 517 | blk_t ref_block EXT2FS_ATTR((unused)), |
| 518 | int ref_offset EXT2FS_ATTR((unused)), |
| 519 | void *priv_data) |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 520 | { |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 521 | struct xlate *xl = (struct xlate *) priv_data; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 522 | |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 523 | return (*xl->func)(fs, blocknr, (int) blockcnt, xl->real_private); |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | errcode_t ext2fs_block_iterate(ext2_filsys fs, |
| Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 527 | ext2_ino_t ino, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 528 | int flags, |
| 529 | char *block_buf, |
| 530 | int (*func)(ext2_filsys fs, |
| 531 | blk_t *blocknr, |
| 532 | int blockcnt, |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 533 | void *priv_data), |
| 534 | void *priv_data) |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 535 | { |
| 536 | struct xlate xl; |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 537 | |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 538 | xl.real_private = priv_data; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 539 | xl.func = func; |
| 540 | |
| Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 541 | return ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_NO_LARGE | flags, |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame] | 542 | block_buf, xlate_func, &xl); |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 543 | } |
| 544 | |