Whamcloud - gitweb
LU-4017 e2fsprogs: always read full inode structure
[tools/e2fsprogs.git] / lib / ext2fs / bmove.c
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         int inode_size;
105         errcode_t       retval;
106         struct process_block_struct pb;
107         ext2_inode_scan scan;
108         char            *block_buf = NULL;
109
110         inode_size = EXT2_INODE_SIZE(fs->super);
111         retval = ext2fs_get_mem(inode_size, &inode);
112         if (retval)
113                 return retval;
114
115         retval = ext2fs_open_inode_scan(fs, 0, &scan);
116         if (retval)
117                 return retval;
118
119         pb.reserve = reserve;
120         pb.error = 0;
121         pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
122         pb.flags = flags;
123
124         retval = ext2fs_get_array(4, fs->blocksize, &block_buf);
125         if (retval)
126                 return retval;
127         pb.buf = block_buf + fs->blocksize * 3;
128
129         /*
130          * If GET_DBLIST is set in the flags field, then we should
131          * gather directory block information while we're doing the
132          * block move.
133          */
134         if (flags & EXT2_BMOVE_GET_DBLIST) {
135                 if (fs->dblist) {
136                         ext2fs_free_dblist(fs->dblist);
137                         fs->dblist = NULL;
138                 }
139                 retval = ext2fs_init_dblist(fs, 0);
140                 if (retval)
141                         goto error;
142         }
143
144         retval = ext2fs_get_next_inode_full(scan, &ino,
145                                             inode, inode_size);
146         if (retval)
147                 goto error;
148
149         while (ino) {
150                 if ((inode->i_links_count == 0) ||
151                     !ext2fs_inode_has_valid_blocks2(fs, inode))
152                         goto next;
153
154                 pb.ino = ino;
155                 pb.inode = inode;
156
157                 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
158                               flags & EXT2_BMOVE_GET_DBLIST);
159
160                 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
161                                                process_block, &pb);
162                 if (retval)
163                         goto error;
164                 if (pb.error) {
165                         retval = pb.error;
166                         goto error;
167                 }
168
169         next:
170                 retval = ext2fs_get_next_inode_full(scan, &ino,
171                                                     inode, inode_size);
172                 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
173                         goto next;
174         }
175 error:
176         ext2fs_free_mem(&inode);
177         ext2fs_free_mem(&block_buf);
178         return 0;
179 }
180