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