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