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 Library
8 * General Public License, version 2.
21 struct block_context {
23 int (*func)(ext2_filsys fs,
39 #define check_for_ro_violation_return(ctx, ret) \
41 if (((ctx)->flags & BLOCK_FLAG_READ_ONLY) && \
42 ((ret) & BLOCK_CHANGED)) { \
43 (ctx)->errcode = EXT2_ET_RO_BLOCK_ITERATE; \
44 ret |= BLOCK_ABORT | BLOCK_ERROR; \
49 #define check_for_ro_violation_goto(ctx, ret, label) \
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; \
59 static int block_iterate_ind(blk_t *ind_block, blk_t ref_block,
60 int ref_offset, struct block_context *ctx)
62 int ret = 0, changed = 0;
63 int i, flags, limit, offset;
66 limit = ctx->fs->blocksize >> 2;
67 if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
68 !(ctx->flags & BLOCK_FLAG_DATA_ONLY))
69 ret = (*ctx->func)(ctx->fs, ind_block,
70 BLOCK_COUNT_IND, ref_block,
71 ref_offset, ctx->priv_data);
72 check_for_ro_violation_return(ctx, ret);
73 if (!*ind_block || (ret & BLOCK_ABORT)) {
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;
83 ctx->errcode = ext2fs_read_ind_block(ctx->fs, *ind_block,
90 block_nr = (blk_t *) ctx->ind_buf;
92 if (ctx->flags & BLOCK_FLAG_APPEND) {
93 for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) {
94 flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount,
98 if (flags & BLOCK_ABORT) {
102 offset += sizeof(blk_t);
105 for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) {
108 flags = (*ctx->func)(ctx->fs, block_nr, ctx->bcount,
112 if (flags & BLOCK_ABORT) {
117 offset += sizeof(blk_t);
120 check_for_ro_violation_return(ctx, changed);
121 if (changed & BLOCK_CHANGED) {
122 ctx->errcode = ext2fs_write_ind_block(ctx->fs, *ind_block,
125 ret |= BLOCK_ERROR | BLOCK_ABORT;
127 if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
128 !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
129 !(ret & BLOCK_ABORT))
130 ret |= (*ctx->func)(ctx->fs, ind_block,
131 BLOCK_COUNT_IND, ref_block,
132 ref_offset, ctx->priv_data);
133 check_for_ro_violation_return(ctx, ret);
137 static int block_iterate_dind(blk_t *dind_block, blk_t ref_block,
138 int ref_offset, struct block_context *ctx)
140 int ret = 0, changed = 0;
141 int i, flags, limit, offset;
144 limit = ctx->fs->blocksize >> 2;
145 if (!(ctx->flags & (BLOCK_FLAG_DEPTH_TRAVERSE |
146 BLOCK_FLAG_DATA_ONLY)))
147 ret = (*ctx->func)(ctx->fs, dind_block,
148 BLOCK_COUNT_DIND, ref_block,
149 ref_offset, ctx->priv_data);
150 check_for_ro_violation_return(ctx, ret);
151 if (!*dind_block || (ret & BLOCK_ABORT)) {
152 ctx->bcount += limit*limit;
155 if (*dind_block >= ctx->fs->super->s_blocks_count ||
156 *dind_block < ctx->fs->super->s_first_data_block) {
157 ctx->errcode = EXT2_ET_BAD_DIND_BLOCK;
161 ctx->errcode = ext2fs_read_ind_block(ctx->fs, *dind_block,
168 block_nr = (blk_t *) ctx->dind_buf;
170 if (ctx->flags & BLOCK_FLAG_APPEND) {
171 for (i = 0; i < limit; i++, block_nr++) {
172 flags = block_iterate_ind(block_nr,
176 if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
177 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
180 offset += sizeof(blk_t);
183 for (i = 0; i < limit; i++, block_nr++) {
184 if (*block_nr == 0) {
185 ctx->bcount += limit;
188 flags = block_iterate_ind(block_nr,
192 if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
193 ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
196 offset += sizeof(blk_t);
199 check_for_ro_violation_return(ctx, changed);
200 if (changed & BLOCK_CHANGED) {
201 ctx->errcode = ext2fs_write_ind_block(ctx->fs, *dind_block,
204 ret |= BLOCK_ERROR | BLOCK_ABORT;
206 if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
207 !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
208 !(ret & BLOCK_ABORT))
209 ret |= (*ctx->func)(ctx->fs, dind_block,
210 BLOCK_COUNT_DIND, ref_block,
211 ref_offset, ctx->priv_data);
212 check_for_ro_violation_return(ctx, ret);
216 static int block_iterate_tind(blk_t *tind_block, blk_t ref_block,
217 int ref_offset, struct block_context *ctx)
219 int ret = 0, changed = 0;
220 int i, flags, limit, offset;
223 limit = ctx->fs->blocksize >> 2;
224 if (!(ctx->flags & (BLOCK_FLAG_DEPTH_TRAVERSE |
225 BLOCK_FLAG_DATA_ONLY)))
226 ret = (*ctx->func)(ctx->fs, tind_block,
227 BLOCK_COUNT_TIND, ref_block,
228 ref_offset, ctx->priv_data);
229 check_for_ro_violation_return(ctx, ret);
230 if (!*tind_block || (ret & BLOCK_ABORT)) {
231 ctx->bcount += limit*limit*limit;
234 if (*tind_block >= ctx->fs->super->s_blocks_count ||
235 *tind_block < ctx->fs->super->s_first_data_block) {
236 ctx->errcode = EXT2_ET_BAD_TIND_BLOCK;
240 ctx->errcode = ext2fs_read_ind_block(ctx->fs, *tind_block,
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 check_for_ro_violation_return(ctx, changed);
279 if (changed & BLOCK_CHANGED) {
280 ctx->errcode = ext2fs_write_ind_block(ctx->fs, *tind_block,
283 ret |= BLOCK_ERROR | BLOCK_ABORT;
285 if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
286 !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
287 !(ret & BLOCK_ABORT))
288 ret |= (*ctx->func)(ctx->fs, tind_block,
289 BLOCK_COUNT_TIND, ref_block,
290 ref_offset, ctx->priv_data);
291 check_for_ro_violation_return(ctx, ret);
295 errcode_t ext2fs_block_iterate2(ext2_filsys fs,
299 int (*func)(ext2_filsys fs,
301 e2_blkcnt_t blockcnt,
309 struct ext2_inode inode;
311 struct block_context ctx;
314 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
316 ctx.errcode = ext2fs_read_inode(fs, ino, &inode);
321 * Check to see if we need to limit large files
323 if (flags & BLOCK_FLAG_NO_LARGE) {
324 if (!LINUX_S_ISDIR(inode.i_mode) &&
325 (inode.i_size_high != 0))
326 return EXT2_ET_FILE_TOO_BIG;
329 limit = fs->blocksize >> 2;
333 ctx.priv_data = priv_data;
337 ctx.ind_buf = block_buf;
339 retval = ext2fs_get_array(3, fs->blocksize, &ctx.ind_buf);
343 ctx.dind_buf = ctx.ind_buf + fs->blocksize;
344 ctx.tind_buf = ctx.dind_buf + fs->blocksize;
347 * Iterate over the HURD translator block (if present)
349 if ((fs->super->s_creator_os == EXT2_OS_HURD) &&
350 !(flags & BLOCK_FLAG_DATA_ONLY)) {
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)
358 check_for_ro_violation_goto(&ctx, ret, abort_exit);
362 if (inode.i_flags & EXT4_EXTENTS_FL) {
363 ext2_extent_handle_t handle;
364 struct ext2fs_extent extent;
365 e2_blkcnt_t blockcnt = 0;
367 int op = EXT2_EXTENT_ROOT;
371 ctx.errcode = ext2fs_extent_open2(fs, ino, &inode, &handle);
376 ctx.errcode = ext2fs_extent_get(handle, op, &extent);
378 if (ctx.errcode != EXT2_ET_EXTENT_NO_NEXT)
381 if (!(flags & BLOCK_FLAG_APPEND))
385 r = (*ctx.func)(fs, &blk, blockcnt,
388 check_for_ro_violation_goto(&ctx, ret,
390 if (r & BLOCK_CHANGED) {
392 ext2fs_extent_set_bmap(handle,
393 (blk64_t) blockcnt++,
395 if (ctx.errcode || (ret & BLOCK_ABORT))
403 op = EXT2_EXTENT_NEXT;
405 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
406 if (ctx.flags & BLOCK_FLAG_DATA_ONLY)
408 if ((!(extent.e_flags &
409 EXT2_EXTENT_FLAGS_SECOND_VISIT) &&
410 !(ctx.flags & BLOCK_FLAG_DEPTH_TRAVERSE)) ||
412 EXT2_EXTENT_FLAGS_SECOND_VISIT) &&
413 (ctx.flags & BLOCK_FLAG_DEPTH_TRAVERSE))) {
414 ret |= (*ctx.func)(fs, &blk,
415 -1, 0, 0, priv_data);
416 if (ret & BLOCK_CHANGED) {
419 ext2fs_extent_replace(handle, 0, &extent);
427 if (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
428 uninit = EXT2_EXTENT_SET_BMAP_UNINIT;
429 for (blockcnt = extent.e_lblk, j = 0;
431 blk++, blockcnt++, j++) {
433 r = (*ctx.func)(fs, &new_blk, blockcnt,
436 check_for_ro_violation_goto(&ctx, ret,
438 if (r & BLOCK_CHANGED) {
440 ext2fs_extent_set_bmap(handle,
447 if (ret & BLOCK_ABORT)
453 ext2fs_extent_free(handle);
454 ret |= BLOCK_ERROR | BLOCK_ABORT;
459 * Iterate over normal data blocks
461 for (i = 0; i < EXT2_NDIR_BLOCKS ; i++, ctx.bcount++) {
462 if (inode.i_block[i] || (flags & BLOCK_FLAG_APPEND)) {
463 ret |= (*ctx.func)(fs, &inode.i_block[i],
464 ctx.bcount, 0, i, priv_data);
465 if (ret & BLOCK_ABORT)
469 check_for_ro_violation_goto(&ctx, ret, abort_exit);
470 if (inode.i_block[EXT2_IND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) {
471 ret |= block_iterate_ind(&inode.i_block[EXT2_IND_BLOCK],
472 0, EXT2_IND_BLOCK, &ctx);
473 if (ret & BLOCK_ABORT)
477 if (inode.i_block[EXT2_DIND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) {
478 ret |= block_iterate_dind(&inode.i_block[EXT2_DIND_BLOCK],
479 0, EXT2_DIND_BLOCK, &ctx);
480 if (ret & BLOCK_ABORT)
483 ctx.bcount += limit * limit;
484 if (inode.i_block[EXT2_TIND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) {
485 ret |= block_iterate_tind(&inode.i_block[EXT2_TIND_BLOCK],
486 0, EXT2_TIND_BLOCK, &ctx);
487 if (ret & BLOCK_ABORT)
492 if (ret & BLOCK_CHANGED) {
493 retval = ext2fs_write_inode(fs, ino, &inode);
496 ctx.errcode = retval;
501 ext2fs_free_mem(&ctx.ind_buf);
503 return (ret & BLOCK_ERROR) ? ctx.errcode : 0;
507 * Emulate the old ext2fs_block_iterate function!
511 int (*func)(ext2_filsys fs,
521 static int xlate_func(ext2_filsys fs, blk_t *blocknr, e2_blkcnt_t blockcnt,
522 blk_t ref_block EXT2FS_ATTR((unused)),
523 int ref_offset EXT2FS_ATTR((unused)),
526 struct xlate *xl = (struct xlate *) priv_data;
528 return (*xl->func)(fs, blocknr, (int) blockcnt, xl->real_private);
531 errcode_t ext2fs_block_iterate(ext2_filsys fs,
535 int (*func)(ext2_filsys fs,
543 xl.real_private = priv_data;
546 return ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_NO_LARGE | flags,
547 block_buf, xlate_func, &xl);