Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * bmove.c --- Move blocks around to make way for a particular
      3  * 	filesystem structure.
      4  *
      5  * Copyright (C) 1997 Theodore Ts'o.
      6  *
      7  * %Begin-Header%
      8  * This file may be redistributed under the terms of the GNU Library
      9  * General Public License, version 2.
     10  * %End-Header%
     11  */
     12 
     13 #include <stdio.h>
     14 #include <string.h>
     15 #if HAVE_UNISTD_H
     16 #include <unistd.h>
     17 #endif
     18 #if HAVE_SYS_TYPES_H
     19 #include <sys/types.h>
     20 #endif
     21 #if HAVE_SYS_TIME_H
     22 #include <sys/time.h>
     23 #endif
     24 
     25 #include "ext2_fs.h"
     26 #include "ext2fsP.h"
     27 
     28 struct process_block_struct {
     29 	ext2_ino_t		ino;
     30 	struct ext2_inode *	inode;
     31 	ext2fs_block_bitmap	reserve;
     32 	ext2fs_block_bitmap	alloc_map;
     33 	errcode_t		error;
     34 	char			*buf;
     35 	int			add_dir;
     36 	int			flags;
     37 };
     38 
     39 static int process_block(ext2_filsys fs, blk_t	*block_nr,
     40 			 e2_blkcnt_t blockcnt, blk_t ref_block,
     41 			 int ref_offset, void *priv_data)
     42 {
     43 	struct process_block_struct *pb;
     44 	errcode_t	retval;
     45 	int		ret;
     46 	blk_t		block, orig;
     47 
     48 	pb = (struct process_block_struct *) priv_data;
     49 	block = orig = *block_nr;
     50 	ret = 0;
     51 
     52 	/*
     53 	 * Let's see if this is one which we need to relocate
     54 	 */
     55 	if (ext2fs_test_block_bitmap(pb->reserve, block)) {
     56 		do {
     57 			if (++block >= fs->super->s_blocks_count)
     58 				block = fs->super->s_first_data_block;
     59 			if (block == orig) {
     60 				pb->error = EXT2_ET_BLOCK_ALLOC_FAIL;
     61 				return BLOCK_ABORT;
     62 			}
     63 		} while (ext2fs_test_block_bitmap(pb->reserve, block) ||
     64 			 ext2fs_test_block_bitmap(pb->alloc_map, block));
     65 
     66 		retval = io_channel_read_blk(fs->io, orig, 1, pb->buf);
     67 		if (retval) {
     68 			pb->error = retval;
     69 			return BLOCK_ABORT;
     70 		}
     71 		retval = io_channel_write_blk(fs->io, block, 1, pb->buf);
     72 		if (retval) {
     73 			pb->error = retval;
     74 			return BLOCK_ABORT;
     75 		}
     76 		*block_nr = block;
     77 		ext2fs_mark_block_bitmap(pb->alloc_map, block);
     78 		ret = BLOCK_CHANGED;
     79 		if (pb->flags & EXT2_BMOVE_DEBUG)
     80 			printf("ino=%ld, blockcnt=%lld, %u->%u\n", pb->ino,
     81 			       blockcnt, orig, block);
     82 	}
     83 	if (pb->add_dir) {
     84 		retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
     85 					      block, (int) blockcnt);
     86 		if (retval) {
     87 			pb->error = retval;
     88 			ret |= BLOCK_ABORT;
     89 		}
     90 	}
     91 	return ret;
     92 }
     93 
     94 errcode_t ext2fs_move_blocks(ext2_filsys fs,
     95 			     ext2fs_block_bitmap reserve,
     96 			     ext2fs_block_bitmap alloc_map,
     97 			     int flags)
     98 {
     99 	ext2_ino_t	ino;
    100 	struct ext2_inode inode;
    101 	errcode_t	retval;
    102 	struct process_block_struct pb;
    103 	ext2_inode_scan	scan;
    104 	char		*block_buf;
    105 
    106 	retval = ext2fs_open_inode_scan(fs, 0, &scan);
    107 	if (retval)
    108 		return retval;
    109 
    110 	pb.reserve = reserve;
    111 	pb.error = 0;
    112 	pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
    113 	pb.flags = flags;
    114 
    115 	retval = ext2fs_get_array(4, fs->blocksize, &block_buf);
    116 	if (retval)
    117 		return retval;
    118 	pb.buf = block_buf + fs->blocksize * 3;
    119 
    120 	/*
    121 	 * If GET_DBLIST is set in the flags field, then we should
    122 	 * gather directory block information while we're doing the
    123 	 * block move.
    124 	 */
    125 	if (flags & EXT2_BMOVE_GET_DBLIST) {
    126 		if (fs->dblist) {
    127 			ext2fs_free_dblist(fs->dblist);
    128 			fs->dblist = NULL;
    129 		}
    130 		retval = ext2fs_init_dblist(fs, 0);
    131 		if (retval)
    132 			return retval;
    133 	}
    134 
    135 	retval = ext2fs_get_next_inode(scan, &ino, &inode);
    136 	if (retval)
    137 		return retval;
    138 
    139 	while (ino) {
    140 		if ((inode.i_links_count == 0) ||
    141 		    !ext2fs_inode_has_valid_blocks(&inode))
    142 			goto next;
    143 
    144 		pb.ino = ino;
    145 		pb.inode = &inode;
    146 
    147 		pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
    148 			      flags & EXT2_BMOVE_GET_DBLIST);
    149 
    150 		retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
    151 					      process_block, &pb);
    152 		if (retval)
    153 			return retval;
    154 		if (pb.error)
    155 			return pb.error;
    156 
    157 	next:
    158 		retval = ext2fs_get_next_inode(scan, &ino, &inode);
    159 		if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
    160 			goto next;
    161 	}
    162 	return 0;
    163 }
    164 
    165