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