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