Whamcloud - gitweb
libext2fs: 32-bit bitmap refactorization, part 3
[tools/e2fsprogs.git] / lib / ext2fs / rw_bitmaps.c
1 /*
2  * rw_bitmaps.c --- routines to read and write the  inode and block bitmaps.
3  *
4  * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <fcntl.h>
18 #include <time.h>
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #include "ext2_fs.h"
27 #include "ext2fs.h"
28 #include "e2image.h"
29
30 #if defined(__powerpc__) && defined(EXT2FS_ENABLE_SWAPFS)
31 /*
32  * On the PowerPC, the big-endian variant of the ext2 filesystem
33  * has its bitmaps stored as 32-bit words with bit 0 as the LSB
34  * of each word.  Thus a bitmap with only bit 0 set would be, as
35  * a string of bytes, 00 00 00 01 00 ...
36  * To cope with this, we byte-reverse each word of a bitmap if
37  * we have a big-endian filesystem, that is, if we are *not*
38  * byte-swapping other word-sized numbers.
39  */
40 #define EXT2_BIG_ENDIAN_BITMAPS
41 #endif
42
43 #ifdef EXT2_BIG_ENDIAN_BITMAPS
44 static void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes)
45 {
46         __u32 *p = (__u32 *) bitmap;
47         int n;
48                 
49         for (n = nbytes / sizeof(__u32); n > 0; --n, ++p)
50                 *p = ext2fs_swab32(*p);
51 }
52 #endif
53
54 static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
55 {
56         dgrp_t          i;
57         unsigned int    j;
58         int             block_nbytes, inode_nbytes;
59         unsigned int    nbits;
60         errcode_t       retval;
61         char            *block_buf, *inode_buf;
62         int             lazy_flag = 0;
63         blk_t           blk;
64         blk_t           blk_itr = fs->super->s_first_data_block;
65         ext2_ino_t      ino_itr = 1;
66
67         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
68
69         if (!(fs->flags & EXT2_FLAG_RW))
70                 return EXT2_ET_RO_FILSYS;
71         if (EXT2_HAS_COMPAT_FEATURE(fs->super, 
72                                     EXT2_FEATURE_COMPAT_LAZY_BG))
73                 lazy_flag = 1;
74         inode_nbytes = block_nbytes = 0;
75         if (do_block) {
76                 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
77                 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
78                 if (retval)
79                         return retval;
80                 memset(block_buf, 0xff, fs->blocksize);
81         }
82         if (do_inode) {
83                 inode_nbytes = (size_t) 
84                         ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
85                 retval = ext2fs_get_mem(fs->blocksize, &inode_buf);
86                 if (retval)
87                         return retval;
88                 memset(inode_buf, 0xff, fs->blocksize);
89         }
90
91         for (i = 0; i < fs->group_desc_count; i++) {
92                 if (!do_block)
93                         goto skip_block_bitmap;
94
95                 if (lazy_flag && fs->group_desc[i].bg_flags &
96                     EXT2_BG_BLOCK_UNINIT) 
97                         goto skip_this_block_bitmap;
98  
99                 retval = ext2fs_get_block_bitmap_range(fs->block_map, 
100                                 blk_itr, block_nbytes << 3, block_buf);
101                 if (retval)
102                         return retval;
103
104                 if (i == fs->group_desc_count - 1) {
105                         /* Force bitmap padding for the last group */
106                         nbits = ((fs->super->s_blocks_count
107                                   - fs->super->s_first_data_block)
108                                  % EXT2_BLOCKS_PER_GROUP(fs->super));
109                         if (nbits)
110                                 for (j = nbits; j < fs->blocksize * 8; j++)
111                                         ext2fs_set_bit(j, block_buf);
112                 }
113                 blk = fs->group_desc[i].bg_block_bitmap;
114                 if (blk) {
115 #ifdef EXT2_BIG_ENDIAN_BITMAPS
116                         if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
117                               (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
118                                 ext2fs_swap_bitmap(fs, block_buf, 
119                                                    block_nbytes);
120 #endif
121                         retval = io_channel_write_blk(fs->io, blk, 1,
122                                                       block_buf);
123                         if (retval)
124                                 return EXT2_ET_BLOCK_BITMAP_WRITE;
125                 }
126         skip_this_block_bitmap:
127                 blk_itr += block_nbytes << 3;
128         skip_block_bitmap:
129
130                 if (!do_inode)
131                         continue;
132
133                 if (lazy_flag && fs->group_desc[i].bg_flags &
134                     EXT2_BG_INODE_UNINIT) 
135                         goto skip_this_inode_bitmap;
136  
137                 retval = ext2fs_get_inode_bitmap_range(fs->inode_map, 
138                                 ino_itr, inode_nbytes << 3, inode_buf);
139                 if (retval)
140                         return retval;
141
142                 blk = fs->group_desc[i].bg_inode_bitmap;
143                 if (blk) {
144 #ifdef EXT2_BIG_ENDIAN_BITMAPS
145                         if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
146                               (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
147                                 ext2fs_swap_bitmap(fs, inode_buf, 
148                                                    inode_nbytes);
149 #endif
150                         retval = io_channel_write_blk(fs->io, blk, 1,
151                                                       inode_buf);
152                         if (retval)
153                                 return EXT2_ET_INODE_BITMAP_WRITE;
154                 }
155         skip_this_inode_bitmap:
156                 ino_itr += inode_nbytes << 3;
157
158         }
159         if (do_block) {
160                 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
161                 ext2fs_free_mem(&block_buf);
162         }
163         if (do_inode) {
164                 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
165                 ext2fs_free_mem(&inode_buf);
166         }
167         return 0;
168 }
169
170 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
171 {
172         dgrp_t i;
173         char *block_bitmap = 0, *inode_bitmap = 0;
174         char *buf;
175         errcode_t retval;
176         unsigned int block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
177         unsigned inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
178         int lazy_flag = 0;
179         int do_image = fs->flags & EXT2_FLAG_IMAGE_FILE;
180         unsigned int    cnt;
181         blk_t   blk;
182         blk_t   blk_itr = fs->super->s_first_data_block;
183         blk_t   blk_cnt;
184         ext2_ino_t ino_itr = 1;
185         ext2_ino_t ino_cnt;
186
187         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
188
189         fs->write_bitmaps = ext2fs_write_bitmaps;
190
191         if (EXT2_HAS_COMPAT_FEATURE(fs->super, 
192                                     EXT2_FEATURE_COMPAT_LAZY_BG))
193                 lazy_flag = 1;
194
195         retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
196         if (retval)
197                 return retval;
198         if (do_block) {
199                 if (fs->block_map)
200                         ext2fs_free_block_bitmap(fs->block_map);
201                 sprintf(buf, "block bitmap for %s", fs->device_name);
202                 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
203                 if (retval)
204                         goto cleanup;
205                 retval = ext2fs_get_mem(do_image ? fs->blocksize : 
206                                         block_nbytes, &block_bitmap);
207                 if (retval)
208                         goto cleanup;
209         } else
210                 block_nbytes = 0;
211         if (do_inode) {
212                 if (fs->inode_map)
213                         ext2fs_free_inode_bitmap(fs->inode_map);
214                 sprintf(buf, "inode bitmap for %s", fs->device_name);
215                 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
216                 if (retval)
217                         goto cleanup;
218                 retval = ext2fs_get_mem(do_image ? fs->blocksize : 
219                                         inode_nbytes, &inode_bitmap);
220                 if (retval)
221                         goto cleanup;
222         } else
223                 inode_nbytes = 0;
224         ext2fs_free_mem(&buf);
225
226         if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
227                 blk = (fs->image_header->offset_inodemap / fs->blocksize);
228                 ino_cnt = fs->super->s_inodes_count;
229                 while (inode_nbytes > 0) {
230                         retval = io_channel_read_blk(fs->image_io, blk++,
231                                                      1, inode_bitmap);
232                         if (retval)
233                                 goto cleanup;
234                         cnt = fs->blocksize << 3;
235                         if (cnt > ino_cnt)
236                                 cnt = ino_cnt;
237                         retval = ext2fs_set_inode_bitmap_range(fs->inode_map, 
238                                                ino_itr, cnt, inode_bitmap);
239                         if (retval)
240                                 goto cleanup;
241                         ino_itr += fs->blocksize << 3;
242                         ino_cnt -= fs->blocksize << 3;
243                         inode_nbytes -= fs->blocksize;
244                 }
245                 blk = (fs->image_header->offset_blockmap /
246                        fs->blocksize);
247                 blk_cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * 
248                         fs->group_desc_count;
249                 while (block_nbytes > 0) {
250                         retval = io_channel_read_blk(fs->image_io, blk++,
251                                                      1, block_bitmap);
252                         if (retval)
253                                 goto cleanup;
254                         cnt = fs->blocksize << 3;
255                         if (cnt > blk_cnt)
256                                 cnt = blk_cnt;
257                         retval = ext2fs_set_block_bitmap_range(fs->block_map, 
258                                        blk_itr, cnt, block_bitmap);
259                         if (retval)
260                                 goto cleanup;
261                         blk_itr += fs->blocksize << 3;
262                         blk_cnt -= fs->blocksize << 3;
263                         block_nbytes -= fs->blocksize;
264                 }
265                 goto success_cleanup;
266         }
267
268         for (i = 0; i < fs->group_desc_count; i++) {
269                 if (block_bitmap) {
270                         blk = fs->group_desc[i].bg_block_bitmap;
271                         if (lazy_flag && fs->group_desc[i].bg_flags &
272                             EXT2_BG_BLOCK_UNINIT)
273                                 blk = 0;
274                         if (blk) {
275                                 retval = io_channel_read_blk(fs->io, blk,
276                                              -block_nbytes, block_bitmap);
277                                 if (retval) {
278                                         retval = EXT2_ET_BLOCK_BITMAP_READ;
279                                         goto cleanup;
280                                 }
281 #ifdef EXT2_BIG_ENDIAN_BITMAPS
282                                 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
283                                       (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
284                                         ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
285 #endif
286                         } else
287                                 memset(block_bitmap, 0xff, block_nbytes);
288                         cnt = block_nbytes << 3;
289                         retval = ext2fs_set_block_bitmap_range(fs->block_map, 
290                                                blk_itr, cnt, block_bitmap);
291                         if (retval)
292                                 goto cleanup;
293                         blk_itr += block_nbytes << 3;
294                 }
295                 if (inode_bitmap) {
296                         blk = fs->group_desc[i].bg_inode_bitmap;
297                         if (lazy_flag && fs->group_desc[i].bg_flags &
298                             EXT2_BG_INODE_UNINIT)
299                                 blk = 0;
300                         if (blk) {
301                                 retval = io_channel_read_blk(fs->io, blk,
302                                              -inode_nbytes, inode_bitmap);
303                                 if (retval) {
304                                         retval = EXT2_ET_INODE_BITMAP_READ;
305                                         goto cleanup;
306                                 }
307 #ifdef EXT2_BIG_ENDIAN_BITMAPS
308                                 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
309                                       (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
310                                         ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
311 #endif
312                         } else
313                                 memset(inode_bitmap, 0xff, inode_nbytes);
314                         cnt = inode_nbytes << 3;
315                         retval = ext2fs_set_inode_bitmap_range(fs->inode_map, 
316                                                ino_itr, cnt, inode_bitmap);
317                         if (retval)
318                                 goto cleanup;
319                         ino_itr += inode_nbytes << 3;
320                 }
321         }
322 success_cleanup:
323         if (inode_bitmap)
324                 ext2fs_free_mem(&inode_bitmap);
325         if (block_bitmap)
326                 ext2fs_free_mem(&block_bitmap);
327         return 0;
328         
329 cleanup:
330         if (do_block) {
331                 ext2fs_free_mem(&fs->block_map);
332                 fs->block_map = 0;
333         }
334         if (do_inode) {
335                 ext2fs_free_mem(&fs->inode_map);
336                 fs->inode_map = 0;
337         }
338         if (inode_bitmap)
339                 ext2fs_free_mem(&inode_bitmap);
340         if (block_bitmap)
341                 ext2fs_free_mem(&block_bitmap);
342         if (buf)
343                 ext2fs_free_mem(&buf);
344         return retval;
345 }
346
347 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
348 {
349         return read_bitmaps(fs, 1, 0);
350 }
351
352 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
353 {
354         return read_bitmaps(fs, 0, 1);
355 }
356
357 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
358 {
359         return write_bitmaps(fs, 1, 0);
360 }
361
362 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
363 {
364         return write_bitmaps(fs, 0, 1);
365 }
366
367 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
368 {
369         if (fs->inode_map && fs->block_map)
370                 return 0;
371
372         return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
373 }
374
375 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
376 {
377         int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
378         int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
379
380         if (!do_inode && !do_block)
381                 return 0;
382
383         return write_bitmaps(fs, do_inode, do_block);
384 }