Coverage Report

Created: 2026-06-15 06:49

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