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