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 "config.h"
     14 #include <stdio.h>
     15 #include <string.h>
     16 #if HAVE_UNISTD_H
     17 #include <unistd.h>
     18 #endif
     19 #if HAVE_SYS_TYPES_H
     20 #include <sys/types.h>
     21 #endif
     22 #if HAVE_SYS_TIME_H
     23 #include <sys/time.h>
     24 #endif
     25 
     26 #include "ext2_fs.h"
     27 #include "ext2fsP.h"
     28 
     29 struct process_block_struct {
     30 	ext2_ino_t		ino;
     31 	struct ext2_inode *	inode;
     32 	ext2fs_block_bitmap	reserve;
     33 	ext2fs_block_bitmap	alloc_map;
     34 	errcode_t		error;
     35 	char			*buf;
     36 	int			add_dir;
     37 	int			flags;
     38 };
     39 
     40 static int process_block(ext2_filsys fs, blk64_t *block_nr,
     41 			 e2_blkcnt_t blockcnt, blk64_t ref_block,
     42 			 int ref_offset, void *priv_data)
     43 {
     44 	struct process_block_struct *pb;
     45 	errcode_t	retval;
     46 	int		ret;
     47 	blk64_t		block, orig;
     48 
     49 	pb = (struct process_block_struct *) priv_data;
     50 	block = orig = *block_nr;
     51 	ret = 0;
     52 
     53 	/*
     54 	 * Let's see if this is one which we need to relocate
     55 	 */
     56 	if (ext2fs_test_block_bitmap2(pb->reserve, block)) {
     57 		do {
     58 			if (++block >= ext2fs_blocks_count(fs->super))
     59 				block = fs->super->s_first_data_block;
     60 			if (block == orig) {
     61 				pb->error = EXT2_ET_BLOCK_ALLOC_FAIL;
     62 				return BLOCK_ABORT;
     63 			}
     64 		} while (ext2fs_test_block_bitmap2(pb->reserve, block) ||
     65 			 ext2fs_test_block_bitmap2(pb->alloc_map, block));
     66 
     67 		retval = io_channel_read_blk64(fs->io, orig, 1, pb->buf);
     68 		if (retval) {
     69 			pb->error = retval;
     70 			return BLOCK_ABORT;
     71 		}
     72 		retval = io_channel_write_blk64(fs->io, block, 1, pb->buf);
     73 		if (retval) {
     74 			pb->error = retval;
     75 			return BLOCK_ABORT;
     76 		}
     77 		*block_nr = block;
     78 		ext2fs_mark_block_bitmap2(pb->alloc_map, block);
     79 		ret = BLOCK_CHANGED;
     80 		if (pb->flags & EXT2_BMOVE_DEBUG)
     81 			printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
     82 			       (unsigned) pb->ino, blockcnt,
     83 			       (unsigned long long) orig,
     84 			       (unsigned long long) block);
     85 	}
     86 	if (pb->add_dir) {
     87 		retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
     88 					       block, blockcnt);
     89 		if (retval) {
     90 			pb->error = retval;
     91 			ret |= BLOCK_ABORT;
     92 		}
     93 	}
     94 	return ret;
     95 }
     96 
     97 errcode_t ext2fs_move_blocks(ext2_filsys fs,
     98 			     ext2fs_block_bitmap reserve,
     99 			     ext2fs_block_bitmap alloc_map,
    100 			     int flags)
    101 {
    102 	ext2_ino_t	ino;
    103 	struct ext2_inode inode;
    104 	errcode_t	retval;
    105 	struct process_block_struct pb;
    106 	ext2_inode_scan	scan;
    107 	char		*block_buf;
    108 
    109 	retval = ext2fs_open_inode_scan(fs, 0, &scan);
    110 	if (retval)
    111 		return retval;
    112 
    113 	pb.reserve = reserve;
    114 	pb.error = 0;
    115 	pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
    116 	pb.flags = flags;
    117 
    118 	retval = ext2fs_get_array(4, fs->blocksize, &block_buf);
    119 	if (retval)
    120 		return retval;
    121 	pb.buf = block_buf + fs->blocksize * 3;
    122 
    123 	/*
    124 	 * If GET_DBLIST is set in the flags field, then we should
    125 	 * gather directory block information while we're doing the
    126 	 * block move.
    127 	 */
    128 	if (flags & EXT2_BMOVE_GET_DBLIST) {
    129 		if (fs->dblist) {
    130 			ext2fs_free_dblist(fs->dblist);
    131 			fs->dblist = NULL;
    132 		}
    133 		retval = ext2fs_init_dblist(fs, 0);
    134 		if (retval)
    135 			return retval;
    136 	}
    137 
    138 	retval = ext2fs_get_next_inode(scan, &ino, &inode);
    139 	if (retval)
    140 		return retval;
    141 
    142 	while (ino) {
    143 		if ((inode.i_links_count == 0) ||
    144 		    !ext2fs_inode_has_valid_blocks2(fs, &inode))
    145 			goto next;
    146 
    147 		pb.ino = ino;
    148 		pb.inode = &inode;
    149 
    150 		pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
    151 			      flags & EXT2_BMOVE_GET_DBLIST);
    152 
    153 		retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
    154 					       process_block, &pb);
    155 		if (retval)
    156 			return retval;
    157 		if (pb.error)
    158 			return pb.error;
    159 
    160 	next:
    161 		retval = ext2fs_get_next_inode(scan, &ino, &inode);
    162 		if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
    163 			goto next;
    164 	}
    165 	return 0;
    166 }
    167 
    168