blob: ec81b944e94a4da0112ea41985046a15d7d3ea69 [file] [log] [blame]
Theodore Ts'oc762c8e1997-06-17 03:52:12 +00001/*
2 * extent.c --- ext2 extent abstraction
3 *
4 * This abstraction is used to provide a compact way of representing a
5 * translation table, for moving multiple contiguous ranges (extents)
6 * of blocks or inodes.
7 *
Theodore Ts'o0cee8a52000-04-06 21:38:34 +00008 * Copyright (C) 1997, 1998 by Theodore Ts'o and
9 * PowerQuest, Inc.
10 *
11 * Copyright (C) 1999, 2000 by Theosore Ts'o
Theodore Ts'oefc6f622008-08-27 23:07:54 -040012 *
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000013 * %Begin-Header%
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000014 * This file may be redistributed under the terms of the GNU Public
15 * License.
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000016 * %End-Header%
17 */
18
Theodore Ts'od1154eb2011-09-18 17:34:37 -040019#include "config.h"
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000020#include "resize2fs.h"
21
22struct ext2_extent_entry {
Valerie Aurora Henson8728d502010-06-13 18:00:00 -040023 __u64 old_loc, new_loc;
24 __u64 size;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000025};
26
27struct _ext2_extent {
28 struct ext2_extent_entry *list;
Valerie Aurora Henson8728d502010-06-13 18:00:00 -040029 __u64 cursor;
30 __u64 size;
31 __u64 num;
32 __u64 sorted;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000033};
34
35/*
36 * Create an extent table
37 */
Valerie Aurora Henson8728d502010-06-13 18:00:00 -040038errcode_t ext2fs_create_extent_table(ext2_extent *ret_extent, __u64 size)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000039{
Theodore Ts'oca8abba1998-01-19 14:55:24 +000040 ext2_extent extent;
41 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040042
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040043 retval = ext2fs_get_mem(sizeof(struct _ext2_extent), &extent);
Theodore Ts'oca8abba1998-01-19 14:55:24 +000044 if (retval)
45 return retval;
46 memset(extent, 0, sizeof(struct _ext2_extent));
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000047
Theodore Ts'oca8abba1998-01-19 14:55:24 +000048 extent->size = size ? size : 50;
49 extent->cursor = 0;
50 extent->num = 0;
51 extent->sorted = 1;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000052
Theodore Ts'oe5aace92007-12-27 10:08:13 -050053 retval = ext2fs_get_array(sizeof(struct ext2_extent_entry),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040054 extent->size, &extent->list);
Theodore Ts'oca8abba1998-01-19 14:55:24 +000055 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040056 ext2fs_free_mem(&extent);
Theodore Ts'oca8abba1998-01-19 14:55:24 +000057 return retval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000058 }
Theodore Ts'oca8abba1998-01-19 14:55:24 +000059 memset(extent->list, 0,
60 sizeof(struct ext2_extent_entry) * extent->size);
61 *ret_extent = extent;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000062 return 0;
63}
64
65/*
66 * Free an extent table
67 */
68void ext2fs_free_extent_table(ext2_extent extent)
69{
70 if (extent->list)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040071 ext2fs_free_mem(&extent->list);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000072 extent->list = 0;
73 extent->size = 0;
74 extent->num = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040075 ext2fs_free_mem(&extent);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000076}
77
78/*
79 * Add an entry to the extent table
80 */
Valerie Aurora Henson8728d502010-06-13 18:00:00 -040081errcode_t ext2fs_add_extent_entry(ext2_extent extent, __u64 old_loc, __u64 new_loc)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000082{
Theodore Ts'oca8abba1998-01-19 14:55:24 +000083 struct ext2_extent_entry *ent;
84 errcode_t retval;
Valerie Aurora Henson8728d502010-06-13 18:00:00 -040085 __u64 newsize;
86 __u64 curr;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000087
88 if (extent->num >= extent->size) {
89 newsize = extent->size + 100;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040090 retval = ext2fs_resize_mem(sizeof(struct ext2_extent_entry) *
91 extent->size,
92 sizeof(struct ext2_extent_entry) *
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040093 newsize, &extent->list);
Theodore Ts'oca8abba1998-01-19 14:55:24 +000094 if (retval)
95 return retval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000096 extent->size = newsize;
97 }
98 curr = extent->num;
99 ent = extent->list + curr;
100 if (curr) {
101 /*
102 * Check to see if this can be coalesced with the last
103 * extent
104 */
105 ent--;
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000106 if ((ent->old_loc + ent->size == old_loc) &&
107 (ent->new_loc + ent->size == new_loc)) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000108 ent->size++;
109 return 0;
110 }
111 /*
112 * Now see if we're going to ruin the sorting
113 */
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000114 if (ent->old_loc + ent->size > old_loc)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000115 extent->sorted = 0;
116 ent++;
117 }
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000118 ent->old_loc = old_loc;
119 ent->new_loc = new_loc;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000120 ent->size = 1;
121 extent->num++;
122 return 0;
123}
124
125/*
126 * Helper function for qsort
127 */
Theodore Ts'o4c77fe51998-04-30 17:35:59 +0000128static EXT2_QSORT_TYPE extent_cmp(const void *a, const void *b)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000129{
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000130 const struct ext2_extent_entry *db_a;
131 const struct ext2_extent_entry *db_b;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400132
Theodore Ts'o2a3013b1998-03-30 01:08:41 +0000133 db_a = (const struct ext2_extent_entry *) a;
134 db_b = (const struct ext2_extent_entry *) b;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400135
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000136 return (db_a->old_loc - db_b->old_loc);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400137}
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000138
139/*
140 * Given an inode map and inode number, look up the old inode number
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000141 * and return the new inode number.
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000142 */
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400143__u64 ext2fs_extent_translate(ext2_extent extent, __u64 old_loc)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000144{
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400145 __s64 low, high, mid;
146 __u64 lowval, highval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000147 float range;
148
149 if (!extent->sorted) {
150 qsort(extent->list, extent->num,
151 sizeof(struct ext2_extent_entry), extent_cmp);
152 extent->sorted = 1;
153 }
154 low = 0;
155 high = extent->num-1;
156 while (low <= high) {
157#if 0
158 mid = (low+high)/2;
159#else
160 if (low == high)
161 mid = low;
162 else {
163 /* Interpolate for efficiency */
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000164 lowval = extent->list[low].old_loc;
165 highval = extent->list[high].old_loc;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000166
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000167 if (old_loc < lowval)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000168 range = 0;
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000169 else if (old_loc > highval)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000170 range = 1;
Theodore Ts'oac92f3c2010-07-05 20:40:41 -0400171 else {
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000172 range = ((float) (old_loc - lowval)) /
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000173 (highval - lowval);
Theodore Ts'oac92f3c2010-07-05 20:40:41 -0400174 if (range > 0.9)
175 range = 0.9;
176 if (range < 0.1)
177 range = 0.1;
178 }
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400179 mid = low + ((__u64) (range * (high-low)));
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000180 }
181#endif
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000182 if ((old_loc >= extent->list[mid].old_loc) &&
183 (old_loc < extent->list[mid].old_loc + extent->list[mid].size))
184 return (extent->list[mid].new_loc +
185 (old_loc - extent->list[mid].old_loc));
186 if (old_loc < extent->list[mid].old_loc)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000187 high = mid-1;
188 else
189 low = mid+1;
190 }
191 return 0;
192}
193
194/*
195 * For debugging only
196 */
197void ext2fs_extent_dump(ext2_extent extent, FILE *out)
198{
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400199 __u64 i;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000200 struct ext2_extent_entry *ent;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400201
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000202 fputs(_("# Extent dump:\n"), out);
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400203 fprintf(out, _("#\tNum=%llu, Size=%llu, Cursor=%llu, Sorted=%llu\n"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000204 extent->num, extent->size, extent->cursor, extent->sorted);
205 for (i=0, ent=extent->list; i < extent->num; i++, ent++) {
Theodore Ts'o84888e52011-10-08 13:32:00 -0400206 fprintf(out, "#\t\t %llu -> %llu (%llu)\n", ent->old_loc,
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000207 ent->new_loc, ent->size);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000208 }
209}
210
211/*
212 * Iterate over the contents of the extent table
213 */
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400214errcode_t ext2fs_iterate_extent(ext2_extent extent, __u64 *old_loc,
215 __u64 *new_loc, __u64 *size)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000216{
217 struct ext2_extent_entry *ent;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400218
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000219 if (!old_loc) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000220 extent->cursor = 0;
221 return 0;
222 }
223
224 if (extent->cursor >= extent->num) {
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000225 *old_loc = 0;
226 *new_loc = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000227 *size = 0;
228 return 0;
229 }
230
231 ent = extent->list + extent->cursor++;
232
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000233 *old_loc = ent->old_loc;
234 *new_loc = ent->new_loc;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000235 *size = ent->size;
236 return 0;
237}
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400238
239
240
241