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