blob: e2ea405a59105323a91dd9a913b07bd74e5d1423 [file] [log] [blame]
Theodore Ts'o1e1da291997-06-09 14:51:29 +00001/*
2 * bmove.c --- Move blocks around to make way for a particular
3 * filesystem structure.
4 *
Theodore Ts'o543547a2010-05-17 21:31:56 -04005 * 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%
Theodore Ts'o1e1da291997-06-09 14:51:29 +000011 */
12
Theodore Ts'od1154eb2011-09-18 17:34:37 -040013#include "config.h"
Theodore Ts'o1e1da291997-06-09 14:51:29 +000014#include <stdio.h>
15#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#if HAVE_UNISTD_H
Theodore Ts'o1e1da291997-06-09 14:51:29 +000017#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000018#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_TYPES_H
Theodore Ts'o1e1da291997-06-09 14:51:29 +000020#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TIME_H
Theodore Ts'o1e1da291997-06-09 14:51:29 +000023#include <sys/time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'o1e1da291997-06-09 14:51:29 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'o9abd2ce1998-02-16 22:00:37 +000027#include "ext2fsP.h"
Theodore Ts'o1e1da291997-06-09 14:51:29 +000028
29struct process_block_struct {
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000030 ext2_ino_t ino;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000031 struct ext2_inode * inode;
32 ext2fs_block_bitmap reserve;
Theodore Ts'o9941fb71997-06-11 22:27:41 +000033 ext2fs_block_bitmap alloc_map;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000034 errcode_t error;
35 char *buf;
36 int add_dir;
Theodore Ts'o36f21431997-06-14 07:25:40 +000037 int flags;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000038};
39
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -040040static int process_block(ext2_filsys fs, blk64_t *block_nr,
41 e2_blkcnt_t blockcnt, blk64_t ref_block,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000042 int ref_offset, void *priv_data)
Theodore Ts'o1e1da291997-06-09 14:51:29 +000043{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000044 struct process_block_struct *pb;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000045 errcode_t retval;
46 int ret;
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -040047 blk64_t block, orig;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000048
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000049 pb = (struct process_block_struct *) priv_data;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000050 block = orig = *block_nr;
51 ret = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040052
Theodore Ts'o1e1da291997-06-09 14:51:29 +000053 /*
54 * Let's see if this is one which we need to relocate
55 */
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040056 if (ext2fs_test_block_bitmap2(pb->reserve, block)) {
Theodore Ts'o1e1da291997-06-09 14:51:29 +000057 do {
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -040058 if (++block >= ext2fs_blocks_count(fs->super))
Theodore Ts'o1e1da291997-06-09 14:51:29 +000059 block = fs->super->s_first_data_block;
60 if (block == orig) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +000061 pb->error = EXT2_ET_BLOCK_ALLOC_FAIL;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000062 return BLOCK_ABORT;
63 }
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040064 } while (ext2fs_test_block_bitmap2(pb->reserve, block) ||
65 ext2fs_test_block_bitmap2(pb->alloc_map, block));
Theodore Ts'o1e1da291997-06-09 14:51:29 +000066
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -040067 retval = io_channel_read_blk64(fs->io, orig, 1, pb->buf);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000068 if (retval) {
69 pb->error = retval;
70 return BLOCK_ABORT;
71 }
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -040072 retval = io_channel_write_blk64(fs->io, block, 1, pb->buf);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000073 if (retval) {
74 pb->error = retval;
75 return BLOCK_ABORT;
76 }
77 *block_nr = block;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040078 ext2fs_mark_block_bitmap2(pb->alloc_map, block);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000079 ret = BLOCK_CHANGED;
Theodore Ts'o36f21431997-06-14 07:25:40 +000080 if (pb->flags & EXT2_BMOVE_DEBUG)
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -040081 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
82 (unsigned) pb->ino, blockcnt,
83 (unsigned long long) orig,
84 (unsigned long long) block);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000085 }
86 if (pb->add_dir) {
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -040087 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
88 block, blockcnt);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000089 if (retval) {
90 pb->error = retval;
91 ret |= BLOCK_ABORT;
92 }
93 }
94 return ret;
95}
96
97errcode_t ext2fs_move_blocks(ext2_filsys fs,
98 ext2fs_block_bitmap reserve,
Theodore Ts'o9941fb71997-06-11 22:27:41 +000099 ext2fs_block_bitmap alloc_map,
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000100 int flags)
101{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000102 ext2_ino_t ino;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000103 struct ext2_inode inode;
104 errcode_t retval;
105 struct process_block_struct pb;
106 ext2_inode_scan scan;
107 char *block_buf;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400108
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000109 retval = ext2fs_open_inode_scan(fs, 0, &scan);
110 if (retval)
111 return retval;
112
113 pb.reserve = reserve;
114 pb.error = 0;
Theodore Ts'o9941fb71997-06-11 22:27:41 +0000115 pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
Theodore Ts'o36f21431997-06-14 07:25:40 +0000116 pb.flags = flags;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400117
Theodore Ts'oee010792007-11-09 19:01:06 -0500118 retval = ext2fs_get_array(4, fs->blocksize, &block_buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000119 if (retval)
120 return retval;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000121 pb.buf = block_buf + fs->blocksize * 3;
122
123 /*
124 * If GET_DBLIST is set in the flags field, then we should
125 * gather directory block information while we're doing the
126 * block move.
127 */
128 if (flags & EXT2_BMOVE_GET_DBLIST) {
129 if (fs->dblist) {
130 ext2fs_free_dblist(fs->dblist);
131 fs->dblist = NULL;
132 }
133 retval = ext2fs_init_dblist(fs, 0);
134 if (retval)
135 return retval;
136 }
137
138 retval = ext2fs_get_next_inode(scan, &ino, &inode);
139 if (retval)
140 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400141
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000142 while (ino) {
143 if ((inode.i_links_count == 0) ||
Theodore Ts'o0c80c442011-10-16 20:29:00 -0400144 !ext2fs_inode_has_valid_blocks2(fs, &inode))
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000145 goto next;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400146
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000147 pb.ino = ino;
148 pb.inode = &inode;
149
150 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
151 flags & EXT2_BMOVE_GET_DBLIST);
152
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -0400153 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
154 process_block, &pb);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000155 if (retval)
156 return retval;
157 if (pb.error)
158 return pb.error;
159
160 next:
161 retval = ext2fs_get_next_inode(scan, &ino, &inode);
162 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
163 goto next;
164 }
165 return 0;
166}
167