2 * block.c --- iterate over all blocks in an inode
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
18 #include <linux/ext2_fs.h>
22 struct block_context {
24 int (*func)(ext2_filsys fs,
40 static int block_iterate_ind(blk_t *ind_block, blk_t ref_block,
41 int ref_offset, struct block_context *ctx)
43 int ret = 0, changed = 0;
44 int i, flags, limit, offset;
47 limit = ctx->fs->blocksize >> 2;
48 if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
49 !(ctx->flags & BLOCK_FLAG_DATA_ONLY))
50 ret = (*ctx->func)(ctx->fs, ind_block,
51 BLOCK_COUNT_IND, ref_block,
52 ref_offset, ctx->private);
53 if (!*ind_block || (ret & BLOCK_ABORT)) {
57 if (*ind_block >= ctx->fs->super->s_blocks_count ||
58 *ind_block < ctx->fs->super->s_first_data_block) {
59 ctx->errcode = EXT2_ET_BAD_IND_BLOCK;
63 ctx->errcode = io_channel_read_blk(ctx->fs->io, *ind_block,
69 if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
70 (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
71 block_nr = (blk_t *) ctx->ind_buf;
72 for (i = 0; i < limit; i++, block_nr++)
73 *block_nr = ext2fs_swab32(*block_nr);
75 block_nr = (blk_t *) ctx->ind_buf;
77 if (ctx->flags & BLOCK_FLAG_APPEND) {
78 for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) {
79 flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount,
83 if (flags & BLOCK_ABORT) {
87 offset += sizeof(blk_t);
90 for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) {
93 flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount,
97 if (flags & BLOCK_ABORT) {
101 offset += sizeof(blk_t);
104 if (changed & BLOCK_CHANGED) {
105 if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
106 (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
107 block_nr = (blk_t *) ctx->ind_buf;
108 for (i = 0; i < limit; i++, block_nr++)
109 *block_nr = ext2fs_swab32(*block_nr);
111 ctx->errcode = io_channel_write_blk(ctx->fs->io, *ind_block,
114 ret |= BLOCK_ERROR | BLOCK_ABORT;
116 if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
117 !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
118 !(ret & BLOCK_ABORT))
119 ret |= (*ctx->func)(ctx->fs, ind_block,
120 BLOCK_COUNT_IND, ref_block,
121 ref_offset, ctx->private);
125 static int block_iterate_dind(blk_t *dind_block, blk_t ref_block,
126 int ref_offset, struct block_context *ctx)
128 int ret = 0, changed = 0;
129 int i, flags, limit, offset;
132 limit = ctx->fs->blocksize >> 2;
133 if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
134 !(ctx->flags & BLOCK_FLAG_DATA_ONLY))
135 ret = (*ctx->func)(ctx->fs, dind_block,
136 BLOCK_COUNT_DIND, ref_block,
137 ref_offset, ctx->private);
138 if (!*dind_block || (ret & BLOCK_ABORT)) {
139 ctx->bcount += limit*limit;
142 if (*dind_block >= ctx->fs->super->s_blocks_count ||
143 *dind_block < ctx->fs->super->s_first_data_block) {
144 ctx->errcode = EXT2_ET_BAD_DIND_BLOCK;
148 ctx->errcode = io_channel_read_blk(ctx->fs->io, *dind_block,
154 if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
155 (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
156 block_nr = (blk_t *) ctx->dind_buf;
157 for (i = 0; i < limit; i++, block_nr++)
158 *block_nr = ext2fs_swab32(*block_nr);
160 block_nr = (blk_t *) ctx->dind_buf;
162 if (ctx->flags & BLOCK_FLAG_APPEND) {
163 for (i = 0; i < limit; i++, block_nr++) {
164 flags = block_iterate_ind(block_nr,
168 if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
169 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
172 offset += sizeof(blk_t);
175 for (i = 0; i < limit; i++, block_nr++) {
176 if (*block_nr == 0) {
177 ctx->bcount += limit;
180 flags = block_iterate_ind(block_nr,
184 if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
185 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
188 offset += sizeof(blk_t);
191 if (changed & BLOCK_CHANGED) {
192 if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
193 (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
194 block_nr = (blk_t *) ctx->dind_buf;
195 for (i = 0; i < limit; i++, block_nr++)
196 *block_nr = ext2fs_swab32(*block_nr);
198 ctx->errcode = io_channel_write_blk(ctx->fs->io, *dind_block,
201 ret |= BLOCK_ERROR | BLOCK_ABORT;
203 if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
204 !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
205 !(ret & BLOCK_ABORT))
206 ret |= (*ctx->func)(ctx->fs, dind_block,
207 BLOCK_COUNT_DIND, ref_block,
208 ref_offset, ctx->private);
212 static int block_iterate_tind(blk_t *tind_block, blk_t ref_block,
213 int ref_offset, struct block_context *ctx)
215 int ret = 0, changed = 0;
216 int i, flags, limit, offset;
219 limit = ctx->fs->blocksize >> 2;
220 if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
221 !(ctx->flags & BLOCK_FLAG_DATA_ONLY))
222 ret = (*ctx->func)(ctx->fs, tind_block,
223 BLOCK_COUNT_TIND, ref_block,
224 ref_offset, ctx->private);
225 if (!*tind_block || (ret & BLOCK_ABORT)) {
226 ctx->bcount += limit*limit*limit;
229 if (*tind_block >= ctx->fs->super->s_blocks_count ||
230 *tind_block < ctx->fs->super->s_first_data_block) {
231 ctx->errcode = EXT2_ET_BAD_TIND_BLOCK;
235 ctx->errcode = io_channel_read_blk(ctx->fs->io, *tind_block,
241 if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
242 (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
243 block_nr = (blk_t *) ctx->tind_buf;
244 for (i = 0; i < limit; i++, block_nr++)
245 *block_nr = ext2fs_swab32(*block_nr);
247 block_nr = (blk_t *) ctx->tind_buf;
249 if (ctx->flags & BLOCK_FLAG_APPEND) {
250 for (i = 0; i < limit; i++, block_nr++) {
251 flags = block_iterate_dind(block_nr,
255 if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
256 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
259 offset += sizeof(blk_t);
262 for (i = 0; i < limit; i++, block_nr++) {
263 if (*block_nr == 0) {
264 ctx->bcount += limit*limit;
267 flags = block_iterate_dind(block_nr,
271 if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
272 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
275 offset += sizeof(blk_t);
278 if (changed & BLOCK_CHANGED) {
279 if ((ctx->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
280 (ctx->fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
281 block_nr = (blk_t *) ctx->tind_buf;
282 for (i = 0; i < limit; i++, block_nr++)
283 *block_nr = ext2fs_swab32(*block_nr);
285 ctx->errcode = io_channel_write_blk(ctx->fs->io, *tind_block,
288 ret |= BLOCK_ERROR | BLOCK_ABORT;
290 if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
291 !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
292 !(ret & BLOCK_ABORT))
293 ret |= (*ctx->func)(ctx->fs, tind_block,
294 BLOCK_COUNT_TIND, ref_block,
295 ref_offset, ctx->private);
300 errcode_t ext2fs_block_iterate2(ext2_filsys fs,
304 int (*func)(ext2_filsys fs,
315 blk_t blocks[EXT2_N_BLOCKS]; /* directory data blocks */
316 struct ext2_inode inode;
318 struct block_context ctx;
320 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
322 retval = ext2fs_get_blocks(fs, ino, blocks);
328 ctx.private = private;
332 ctx.ind_buf = block_buf;
334 retval = ext2fs_get_mem(fs->blocksize * 3,
335 (void **) &ctx.ind_buf);
339 ctx.dind_buf = ctx.ind_buf + fs->blocksize;
340 ctx.tind_buf = ctx.dind_buf + fs->blocksize;
343 * Iterate over the HURD translator block (if present)
345 if ((fs->super->s_creator_os == EXT2_OS_HURD) &&
346 !(flags & BLOCK_FLAG_DATA_ONLY)) {
347 ctx.errcode = ext2fs_read_inode(fs, ino, &inode);
351 if (inode.osd1.hurd1.h_i_translator) {
352 ret |= (*ctx.func)(fs,
353 &inode.osd1.hurd1.h_i_translator,
354 BLOCK_COUNT_TRANSLATOR,
356 if (ret & BLOCK_ABORT)
362 * Iterate over normal data blocks
364 for (i = 0; i < EXT2_NDIR_BLOCKS ; i++, ctx.bcount++) {
365 if (blocks[i] || (flags & BLOCK_FLAG_APPEND)) {
366 ret |= (*ctx.func)(fs, &blocks[i],
367 ctx.bcount, 0, 0, private);
368 if (ret & BLOCK_ABORT)
372 if (*(blocks + EXT2_IND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) {
373 ret |= block_iterate_ind(blocks + EXT2_IND_BLOCK,
375 if (ret & BLOCK_ABORT)
378 if (*(blocks + EXT2_DIND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) {
379 ret |= block_iterate_dind(blocks + EXT2_DIND_BLOCK,
381 if (ret & BLOCK_ABORT)
384 if (*(blocks + EXT2_TIND_BLOCK) || (flags & BLOCK_FLAG_APPEND)) {
385 ret |= block_iterate_tind(blocks + EXT2_TIND_BLOCK,
387 if (ret & BLOCK_ABORT)
392 if (ret & BLOCK_CHANGED) {
394 retval = ext2fs_read_inode(fs, ino, &inode);
398 for (i=0; i < EXT2_N_BLOCKS; i++)
399 inode.i_block[i] = blocks[i];
400 retval = ext2fs_write_inode(fs, ino, &inode);
406 ext2fs_free_mem((void **) &ctx.ind_buf);
408 return (ret & BLOCK_ERROR) ? ctx.errcode : 0;
412 int (*func)(ext2_filsys fs,
422 static int xlate_func(ext2_filsys fs, blk_t *blocknr, int blockcnt,
423 blk_t ref_block, int ref_offset, void *private)
425 struct xlate *xl = private;
427 return (*xl->func)(fs, blocknr, blockcnt, xl->real_private);
430 errcode_t ext2fs_block_iterate(ext2_filsys fs,
434 int (*func)(ext2_filsys fs,
442 xl.real_private = private;
445 return ext2fs_block_iterate2(fs, ino, flags, block_buf,