Whamcloud - gitweb
e2fsck: merge bitmaps after thread completes
[tools/e2fsprogs.git] / lib / ext2fs / bitmaps.c
1 /*
2  * bitmaps.c --- routines to read, write, and manipulate the inode and
3  * block bitmaps.
4  *
5  * Copyright (C) 1993, 1994, 1995, 1996 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 "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <fcntl.h>
20 #include <time.h>
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27
28 #include "ext2_fs.h"
29 #include "ext2fs.h"
30 #include "ext2fsP.h"
31 #include "bmap64.h"
32
33 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
34 {
35         ext2fs_free_generic_bmap(bitmap);
36 }
37
38 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
39 {
40         ext2fs_free_generic_bmap(bitmap);
41 }
42
43 errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
44                              ext2fs_generic_bitmap *dest)
45 {
46         return (ext2fs_copy_generic_bmap(src, dest));
47 }
48
49 errcode_t ext2fs_merge_bitmap(ext2fs_generic_bitmap src,
50                               ext2fs_generic_bitmap dest,
51                               ext2fs_generic_bitmap dup,
52                               ext2fs_generic_bitmap dup_allowed)
53 {
54         return ext2fs_merge_generic_bmap(src, dest, dup,
55                                          dup_allowed);
56 }
57
58 void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
59 {
60         ext2fs_set_generic_bmap_padding(map);
61 }
62
63 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
64                                        const char *descr,
65                                        ext2fs_inode_bitmap *ret)
66 {
67         __u64           start, end, real_end;
68
69         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
70
71         if (ext2fs_has_feature_journal_dev(fs->super))
72                 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
73
74         fs->write_bitmaps = ext2fs_write_bitmaps;
75
76         start = 1;
77         end = fs->super->s_inodes_count;
78         real_end = (__u64)EXT2_INODES_PER_GROUP(fs->super) *
79                 fs->group_desc_count;
80
81         /* Are we permitted to use new-style bitmaps? */
82         if (fs->flags & EXT2_FLAG_64BITS)
83                 return (ext2fs_alloc_generic_bmap(fs,
84                                 EXT2_ET_MAGIC_INODE_BITMAP64,
85                                 fs->default_bitmap_type,
86                                 start, end, real_end, descr, ret));
87
88         /* Otherwise, check to see if the file system is small enough
89          * to use old-style 32-bit bitmaps */
90         if ((end > ~0U) || (real_end > ~0U))
91                 return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
92
93         return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP, fs,
94                                          start, end, real_end,
95                                          descr, 0,
96                                          (ext2fs_generic_bitmap *) ret));
97 }
98
99 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
100                                        const char *descr,
101                                        ext2fs_block_bitmap *ret)
102 {
103         __u64           start, end, real_end;
104
105         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
106
107         if (ext2fs_has_feature_journal_dev(fs->super))
108                 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
109
110         fs->write_bitmaps = ext2fs_write_bitmaps;
111
112         start = EXT2FS_B2C(fs, fs->super->s_first_data_block);
113         end = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1);
114         real_end = ((__u64) EXT2_CLUSTERS_PER_GROUP(fs->super)
115                     * (__u64) fs->group_desc_count)-1 + start;
116
117         if (fs->flags & EXT2_FLAG_64BITS)
118                 return (ext2fs_alloc_generic_bmap(fs,
119                                 EXT2_ET_MAGIC_BLOCK_BITMAP64,
120                                 fs->default_bitmap_type,
121                                 start, end, real_end, descr, ret));
122
123         if ((end > ~0U) || (real_end > ~0U))
124                 return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
125
126         return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP, fs,
127                                            start, end, real_end,
128                                            descr, 0,
129                                            (ext2fs_generic_bitmap *) ret));
130 }
131
132 /*
133  * ext2fs_allocate_block_bitmap() really allocates a per-cluster
134  * bitmap for backwards compatibility.  This function allocates a
135  * block bitmap which is truly per-block, even if clusters/bigalloc
136  * are enabled.  mke2fs and e2fsck need this for tracking the
137  * allocation of the file system metadata blocks.
138  */
139 errcode_t ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,
140                                             const char *descr,
141                                             ext2fs_block_bitmap *ret)
142 {
143         __u64                   start, end, real_end;
144         ext2fs_generic_bitmap   bmap;
145         ext2fs_generic_bitmap_64 bmap64;
146         errcode_t               retval;
147
148         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
149
150         if (ext2fs_has_feature_journal_dev(fs->super))
151                 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
152
153         fs->write_bitmaps = ext2fs_write_bitmaps;
154
155         if (!fs->cluster_ratio_bits)
156                 return ext2fs_allocate_block_bitmap(fs, descr, ret);
157
158         if ((fs->flags & EXT2_FLAG_64BITS) == 0)
159                 return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
160
161         start = fs->super->s_first_data_block;
162         end = ext2fs_blocks_count(fs->super)-1;
163         real_end = ((__u64) EXT2_BLOCKS_PER_GROUP(fs->super)
164                     * (__u64) fs->group_desc_count)-1 + start;
165
166         retval = ext2fs_alloc_generic_bmap(fs, EXT2_ET_MAGIC_BLOCK_BITMAP64,
167                                            fs->default_bitmap_type, start,
168                                            end, real_end, descr, &bmap);
169         if (retval)
170                 return retval;
171         bmap64 = (ext2fs_generic_bitmap_64) bmap;
172         bmap64->cluster_bits = 0;
173         *ret = bmap;
174         return 0;
175 }
176
177 int ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap)
178 {
179         ext2fs_generic_bitmap_64 bmap = (ext2fs_generic_bitmap_64) bitmap;
180
181         if (!EXT2FS_IS_64_BITMAP(bmap))
182                 return 0;
183
184         return bmap->cluster_bits;
185 }
186
187 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
188                                         ext2_ino_t end, ext2_ino_t *oend)
189 {
190         __u64 tmp_oend;
191         int retval;
192
193         retval = ext2fs_fudge_generic_bmap_end((ext2fs_generic_bitmap) bitmap,
194                                                EXT2_ET_FUDGE_INODE_BITMAP_END,
195                                                end, &tmp_oend);
196         if (oend)
197                 *oend = tmp_oend;
198         return retval;
199 }
200
201 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
202                                         blk_t end, blk_t *oend)
203 {
204         return (ext2fs_fudge_generic_bitmap_end(bitmap,
205                                                 EXT2_ET_MAGIC_BLOCK_BITMAP,
206                                                 EXT2_ET_FUDGE_BLOCK_BITMAP_END,
207                                                 end, oend));
208 }
209
210 errcode_t ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,
211                                          blk64_t end, blk64_t *oend)
212 {
213         return (ext2fs_fudge_generic_bmap_end(bitmap,
214                                               EXT2_ET_FUDGE_BLOCK_BITMAP_END,
215                                               end, oend));
216 }
217
218 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
219 {
220         ext2fs_clear_generic_bmap(bitmap);
221 }
222
223 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
224 {
225         ext2fs_clear_generic_bmap(bitmap);
226 }
227
228 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
229                                      ext2fs_inode_bitmap bmap)
230 {
231         return (ext2fs_resize_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP,
232                                              new_end, new_real_end, bmap));
233 }
234
235 errcode_t ext2fs_resize_inode_bitmap2(__u64 new_end, __u64 new_real_end,
236                                       ext2fs_inode_bitmap bmap)
237 {
238         return (ext2fs_resize_generic_bmap(bmap, new_end, new_real_end));
239 }
240
241 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
242                                      ext2fs_block_bitmap bmap)
243 {
244         return (ext2fs_resize_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP,
245                                              new_end, new_real_end, bmap));
246 }
247
248 errcode_t ext2fs_resize_block_bitmap2(__u64 new_end, __u64 new_real_end,
249                                       ext2fs_block_bitmap bmap)
250 {
251         return (ext2fs_resize_generic_bmap(bmap, new_end, new_real_end));
252 }
253
254 errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
255                                       ext2fs_block_bitmap bm2)
256 {
257         return (ext2fs_compare_generic_bmap(EXT2_ET_NEQ_BLOCK_BITMAP,
258                                             bm1, bm2));
259 }
260
261 errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
262                                       ext2fs_inode_bitmap bm2)
263 {
264         return (ext2fs_compare_generic_bmap(EXT2_ET_NEQ_INODE_BITMAP,
265                                             bm1, bm2));
266 }
267
268 errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
269                                         ext2_ino_t start, unsigned int num,
270                                         void *in)
271 {
272         return (ext2fs_set_generic_bitmap_range(bmap,
273                                                 EXT2_ET_MAGIC_INODE_BITMAP,
274                                                 start, num, in));
275 }
276
277 errcode_t ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
278                                          __u64 start, size_t num,
279                                          void *in)
280 {
281         return (ext2fs_set_generic_bmap_range(bmap, start, num, in));
282 }
283
284 errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
285                                         ext2_ino_t start, unsigned int num,
286                                         void *out)
287 {
288         return (ext2fs_get_generic_bitmap_range(bmap,
289                                                 EXT2_ET_MAGIC_INODE_BITMAP,
290                                                 start, num, out));
291 }
292
293 errcode_t ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
294                                          __u64 start, size_t num,
295                                          void *out)
296 {
297         return (ext2fs_get_generic_bmap_range(bmap, start, num, out));
298 }
299
300 errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
301                                         blk_t start, unsigned int num,
302                                         void *in)
303 {
304         return (ext2fs_set_generic_bitmap_range(bmap,
305                                                 EXT2_ET_MAGIC_BLOCK_BITMAP,
306                                                 start, num, in));
307 }
308
309 errcode_t ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,
310                                          blk64_t start, size_t num,
311                                          void *in)
312 {
313         return (ext2fs_set_generic_bmap_range(bmap, start, num, in));
314 }
315
316 errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
317                                         blk_t start, unsigned int num,
318                                         void *out)
319 {
320         return (ext2fs_get_generic_bitmap_range(bmap,
321                                                 EXT2_ET_MAGIC_BLOCK_BITMAP,
322                                                 start, num, out));
323 }
324
325 errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
326                                          blk64_t start, size_t num,
327                                          void *out)
328 {
329         return (ext2fs_get_generic_bmap_range(bmap, start, num, out));
330 }