Whamcloud - gitweb
015334f73d7289ba0dfc87a8652c9fe841ee1383
[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 <stdlib.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #if HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29
30 #include <linux/ext2_fs.h>
31
32 #include "ext2fs.h"
33
34 #ifdef __powerpc__
35 /*
36  * On the PowerPC, the big-endian variant of the ext2 filesystem
37  * has its bitmaps stored as 32-bit words with bit 0 as the LSB
38  * of each word.  Thus a bitmap with only bit 0 set would be, as
39  * a string of bytes, 00 00 00 01 00 ...
40  * To cope with this, we byte-reverse each word of a bitmap if
41  * we have a big-endian filesystem, that is, if we are *not*
42  * byte-swapping other word-sized numbers.
43  */
44 #define EXT2_BIG_ENDIAN_BITMAPS
45 #endif
46
47 #ifdef EXT2_BIG_ENDIAN_BITMAPS
48 void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes)
49 {
50         __u32 *p = (__u32 *) bitmap;
51         int n;
52                 
53         for (n = nbytes / sizeof(__u32); n > 0; --n, ++p)
54                 *p = ext2fs_swab32(*p);
55 }
56 #endif
57
58 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
59 {
60         int             i;
61         size_t          nbytes;
62         errcode_t       retval;
63         char * inode_bitmap = fs->inode_map->bitmap;
64         char * bitmap_block = NULL;
65         blk_t           blk;
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 (!inode_bitmap)
72                 return 0;
73         nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
74         
75         bitmap_block = malloc(fs->blocksize);
76         if (!bitmap_block)
77                 return ENOMEM;
78         memset(bitmap_block, 0xff, fs->blocksize);
79         for (i = 0; i < fs->group_desc_count; i++) {
80                 memcpy(bitmap_block, inode_bitmap, nbytes);
81                 blk = fs->group_desc[i].bg_inode_bitmap;
82                 if (blk) {
83 #ifdef EXT2_BIG_ENDIAN_BITMAPS
84                         if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
85                               (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
86                                 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
87 #endif
88                         retval = io_channel_write_blk(fs->io, blk, 1,
89                                                       bitmap_block);
90                         if (retval)
91                                 return EXT2_ET_INODE_BITMAP_WRITE;
92                 }
93                 inode_bitmap += nbytes;
94         }
95         fs->flags |= EXT2_FLAG_CHANGED;
96         fs->flags &= ~EXT2_FLAG_IB_DIRTY;
97         free(bitmap_block);
98         return 0;
99 }
100
101 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
102 {
103         int             i;
104         int             j;
105         int             nbytes;
106         int             nbits;
107         errcode_t       retval;
108         char * block_bitmap = fs->block_map->bitmap;
109         char * bitmap_block = NULL;
110         blk_t           blk;
111
112         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
113
114         if (!(fs->flags & EXT2_FLAG_RW))
115                 return EXT2_ET_RO_FILSYS;
116         if (!block_bitmap)
117                 return 0;
118         nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
119         bitmap_block = malloc(fs->blocksize);
120         if (!bitmap_block)
121                 return ENOMEM;
122         memset(bitmap_block, 0xff, fs->blocksize);
123         for (i = 0; i < fs->group_desc_count; i++) {
124                 memcpy(bitmap_block, block_bitmap, nbytes);
125                 if (i == fs->group_desc_count - 1) {
126                         /* Force bitmap padding for the last group */
127                         nbits = (int) ((fs->super->s_blocks_count
128                                         - fs->super->s_first_data_block)
129                                        % EXT2_BLOCKS_PER_GROUP(fs->super));
130                         if (nbits)
131                                 for (j = nbits; j < fs->blocksize * 8; j++)
132                                         ext2fs_set_bit(j, bitmap_block);
133                 }
134                 blk = fs->group_desc[i].bg_block_bitmap;
135                 if (blk) {
136 #ifdef EXT2_BIG_ENDIAN_BITMAPS
137                         if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
138                               (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
139                                 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
140 #endif
141                         retval = io_channel_write_blk(fs->io, blk, 1,
142                                                       bitmap_block);
143                         if (retval)
144                                 return EXT2_ET_BLOCK_BITMAP_WRITE;
145                 }
146                 block_bitmap += nbytes;
147         }
148         fs->flags |= EXT2_FLAG_CHANGED;
149         fs->flags &= ~EXT2_FLAG_BB_DIRTY;
150         free(bitmap_block);
151         return 0;
152 }
153
154 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
155 {
156         int i;
157         char *block_bitmap = 0, *inode_bitmap = 0;
158         char *buf;
159         errcode_t retval;
160         int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
161         int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
162         blk_t   blk;
163
164         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
165
166         fs->write_bitmaps = ext2fs_write_bitmaps;
167
168         buf = malloc(strlen(fs->device_name) + 80);
169         if (do_block) {
170                 if (fs->block_map)
171                         ext2fs_free_block_bitmap(fs->block_map);
172                 sprintf(buf, "block bitmap for %s", fs->device_name);
173                 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
174                 if (retval)
175                         goto cleanup;
176                 block_bitmap = fs->block_map->bitmap;
177         }
178         if (do_inode) {
179                 if (fs->inode_map)
180                         ext2fs_free_inode_bitmap(fs->inode_map);
181                 sprintf(buf, "inode bitmap for %s", fs->device_name);
182                 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
183                 if (retval)
184                         goto cleanup;
185                 inode_bitmap = fs->inode_map->bitmap;
186         }
187         free(buf);
188
189         for (i = 0; i < fs->group_desc_count; i++) {
190                 if (block_bitmap) {
191                         blk = fs->group_desc[i].bg_block_bitmap;
192                         if (blk) {
193                                 retval = io_channel_read_blk(fs->io, blk,
194                                              -block_nbytes, block_bitmap);
195                                 if (retval) {
196                                         retval = EXT2_ET_BLOCK_BITMAP_READ;
197                                         goto cleanup;
198                                 }
199 #ifdef EXT2_BIG_ENDIAN_BITMAPS
200                                 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
201                                       (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
202                                         ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
203 #endif
204                         } else
205                                 memset(block_bitmap, 0, block_nbytes);
206                         block_bitmap += block_nbytes;
207                 }
208                 if (inode_bitmap) {
209                         blk = fs->group_desc[i].bg_inode_bitmap;
210                         if (blk) {
211                                 retval = io_channel_read_blk(fs->io, blk,
212                                              -inode_nbytes, inode_bitmap);
213                                 if (retval) {
214                                         retval = EXT2_ET_INODE_BITMAP_READ;
215                                         goto cleanup;
216                                 }
217 #ifdef EXT2_BIG_ENDIAN_BITMAPS
218                                 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
219                                       (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
220                                         ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
221 #endif
222                         } else
223                                 memset(inode_bitmap, 0, inode_nbytes);
224                         inode_bitmap += inode_nbytes;
225                 }
226         }
227         return 0;
228         
229 cleanup:
230         if (do_block) {
231                 free(fs->block_map);
232                 fs->block_map = 0;
233         }
234         if (do_inode) {
235                 free(fs->inode_map);
236                 fs->inode_map = 0;
237         }
238         if (buf)
239                 free(buf);
240         return retval;
241 }
242
243 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
244 {
245         return read_bitmaps(fs, 1, 0);
246 }
247
248 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
249 {
250         return read_bitmaps(fs, 0, 1);
251 }
252
253 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
254 {
255
256         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
257
258         return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
259 }
260
261 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
262 {
263         errcode_t       retval;
264
265         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
266
267         if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
268                 retval = ext2fs_write_block_bitmap(fs);
269                 if (retval)
270                         return retval;
271         }
272         if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
273                 retval = ext2fs_write_inode_bitmap(fs);
274                 if (retval)
275                         return retval;
276         }
277         return 0;
278 }       
279