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 | * |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 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% |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 13 | #include "config.h" |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #if HAVE_UNISTD_H |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 18 | #endif |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 19 | #if HAVE_SYS_TYPES_H |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #if HAVE_SYS_TIME_H |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 23 | #include <sys/time.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 26 | #include "ext2_fs.h" |
Theodore Ts'o | 9abd2ce | 1998-02-16 22:00:37 +0000 | [diff] [blame] | 27 | #include "ext2fsP.h" |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 28 | |
| 29 | struct process_block_struct { |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 30 | ext2_ino_t ino; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 31 | struct ext2_inode * inode; |
| 32 | ext2fs_block_bitmap reserve; |
Theodore Ts'o | 9941fb7 | 1997-06-11 22:27:41 +0000 | [diff] [blame] | 33 | ext2fs_block_bitmap alloc_map; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 34 | errcode_t error; |
| 35 | char *buf; |
| 36 | int add_dir; |
Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 37 | int flags; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Valerie Aurora Henson | 6d8b37f | 2010-06-13 11:00:00 -0400 | [diff] [blame] | 40 | static int process_block(ext2_filsys fs, blk64_t *block_nr, |
| 41 | e2_blkcnt_t blockcnt, blk64_t ref_block, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 42 | int ref_offset, void *priv_data) |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 43 | { |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 44 | struct process_block_struct *pb; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 45 | errcode_t retval; |
| 46 | int ret; |
Valerie Aurora Henson | 6d8b37f | 2010-06-13 11:00:00 -0400 | [diff] [blame] | 47 | blk64_t block, orig; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 48 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 49 | pb = (struct process_block_struct *) priv_data; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 50 | block = orig = *block_nr; |
| 51 | ret = 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 52 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 53 | /* |
| 54 | * Let's see if this is one which we need to relocate |
| 55 | */ |
Valerie Aurora Henson | 8f82ef9 | 2009-08-05 00:27:10 -0400 | [diff] [blame] | 56 | if (ext2fs_test_block_bitmap2(pb->reserve, block)) { |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 57 | do { |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 58 | if (++block >= ext2fs_blocks_count(fs->super)) |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 59 | block = fs->super->s_first_data_block; |
| 60 | if (block == orig) { |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 61 | pb->error = EXT2_ET_BLOCK_ALLOC_FAIL; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 62 | return BLOCK_ABORT; |
| 63 | } |
Valerie Aurora Henson | 8f82ef9 | 2009-08-05 00:27:10 -0400 | [diff] [blame] | 64 | } while (ext2fs_test_block_bitmap2(pb->reserve, block) || |
| 65 | ext2fs_test_block_bitmap2(pb->alloc_map, block)); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 66 | |
Valerie Aurora Henson | 24a117a | 2009-09-07 21:14:24 -0400 | [diff] [blame] | 67 | retval = io_channel_read_blk64(fs->io, orig, 1, pb->buf); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 68 | if (retval) { |
| 69 | pb->error = retval; |
| 70 | return BLOCK_ABORT; |
| 71 | } |
Valerie Aurora Henson | 24a117a | 2009-09-07 21:14:24 -0400 | [diff] [blame] | 72 | retval = io_channel_write_blk64(fs->io, block, 1, pb->buf); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 73 | if (retval) { |
| 74 | pb->error = retval; |
| 75 | return BLOCK_ABORT; |
| 76 | } |
| 77 | *block_nr = block; |
Valerie Aurora Henson | 8f82ef9 | 2009-08-05 00:27:10 -0400 | [diff] [blame] | 78 | ext2fs_mark_block_bitmap2(pb->alloc_map, block); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 79 | ret = BLOCK_CHANGED; |
Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 80 | if (pb->flags & EXT2_BMOVE_DEBUG) |
Valerie Aurora Henson | 6d8b37f | 2010-06-13 11:00:00 -0400 | [diff] [blame] | 81 | printf("ino=%u, blockcnt=%lld, %llu->%llu\n", |
| 82 | (unsigned) pb->ino, blockcnt, |
| 83 | (unsigned long long) orig, |
| 84 | (unsigned long long) block); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 85 | } |
| 86 | if (pb->add_dir) { |
Valerie Aurora Henson | 6d8b37f | 2010-06-13 11:00:00 -0400 | [diff] [blame] | 87 | retval = ext2fs_add_dir_block2(fs->dblist, pb->ino, |
| 88 | block, blockcnt); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 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, |
Theodore Ts'o | 9941fb7 | 1997-06-11 22:27:41 +0000 | [diff] [blame] | 99 | ext2fs_block_bitmap alloc_map, |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 100 | int flags) |
| 101 | { |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 102 | ext2_ino_t ino; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 103 | struct ext2_inode inode; |
| 104 | errcode_t retval; |
| 105 | struct process_block_struct pb; |
| 106 | ext2_inode_scan scan; |
| 107 | char *block_buf; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 108 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 109 | retval = ext2fs_open_inode_scan(fs, 0, &scan); |
| 110 | if (retval) |
| 111 | return retval; |
| 112 | |
| 113 | pb.reserve = reserve; |
| 114 | pb.error = 0; |
Theodore Ts'o | 9941fb7 | 1997-06-11 22:27:41 +0000 | [diff] [blame] | 115 | pb.alloc_map = alloc_map ? alloc_map : fs->block_map; |
Theodore Ts'o | 36f2143 | 1997-06-14 07:25:40 +0000 | [diff] [blame] | 116 | pb.flags = flags; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 117 | |
Theodore Ts'o | ee01079 | 2007-11-09 19:01:06 -0500 | [diff] [blame] | 118 | retval = ext2fs_get_array(4, fs->blocksize, &block_buf); |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 119 | if (retval) |
| 120 | return retval; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 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; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 141 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 142 | while (ino) { |
| 143 | if ((inode.i_links_count == 0) || |
Theodore Ts'o | 0c80c44 | 2011-10-16 20:29:00 -0400 | [diff] [blame] | 144 | !ext2fs_inode_has_valid_blocks2(fs, &inode)) |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 145 | goto next; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 146 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 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 | |
Valerie Aurora Henson | 6d8b37f | 2010-06-13 11:00:00 -0400 | [diff] [blame] | 153 | retval = ext2fs_block_iterate3(fs, ino, 0, block_buf, |
| 154 | process_block, &pb); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 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 | |