Whamcloud - gitweb
Many files:
[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 #include <unistd.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <time.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #if HAVE_ERRNO_H
21 #include <errno.h>
22 #endif
23
24 #include <linux/ext2_fs.h>
25
26 #include "ext2fs.h"
27
28 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
29 {
30         int             i;
31         int             nbytes;
32         errcode_t       retval;
33         char * inode_bitmap = fs->inode_map->bitmap;
34         char * bitmap_block = NULL;
35         blk_t           blk;
36
37         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
38
39         if (!(fs->flags & EXT2_FLAG_RW))
40                 return EXT2_ET_RO_FILSYS;
41         if (!inode_bitmap)
42                 return 0;
43         nbytes = (EXT2_INODES_PER_GROUP(fs->super)+7) / 8;
44         
45         bitmap_block = malloc(fs->blocksize);
46         if (!bitmap_block)
47                 return ENOMEM;
48         memset(bitmap_block, 0xff, fs->blocksize);
49         for (i = 0; i < fs->group_desc_count; i++) {
50                 memcpy(bitmap_block, inode_bitmap, nbytes);
51                 blk = fs->group_desc[i].bg_inode_bitmap;
52                 if (blk) {
53                         retval = io_channel_write_blk(fs->io, blk, 1,
54                                                       bitmap_block);
55                         if (retval)
56                                 return EXT2_ET_INODE_BITMAP_WRITE;
57                 }
58                 inode_bitmap += nbytes;
59         }
60         fs->flags |= EXT2_FLAG_CHANGED;
61         fs->flags &= ~EXT2_FLAG_IB_DIRTY;
62         free(bitmap_block);
63         return 0;
64 }
65
66 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
67 {
68         int             i;
69         int             j;
70         int             nbytes;
71         int             nbits;
72         errcode_t       retval;
73         char * block_bitmap = fs->block_map->bitmap;
74         char * bitmap_block = NULL;
75         blk_t           blk;
76
77         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
78
79         if (!(fs->flags & EXT2_FLAG_RW))
80                 return EXT2_ET_RO_FILSYS;
81         if (!block_bitmap)
82                 return 0;
83         nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
84         bitmap_block = malloc(fs->blocksize);
85         if (!bitmap_block)
86                 return ENOMEM;
87         memset(bitmap_block, 0xff, fs->blocksize);
88         for (i = 0; i < fs->group_desc_count; i++) {
89                 memcpy(bitmap_block, block_bitmap, nbytes);
90                 if (i == fs->group_desc_count - 1) {
91                         /* Force bitmap padding for the last group */
92                         nbits = (fs->super->s_blocks_count
93                                  - fs->super->s_first_data_block)
94                                 % EXT2_BLOCKS_PER_GROUP(fs->super);
95                         if (nbits)
96                                 for (j = nbits; j < fs->blocksize * 8; j++)
97                                         ext2fs_set_bit(j, bitmap_block);
98                 }
99                 blk = fs->group_desc[i].bg_block_bitmap;
100                 if (blk) {
101                         retval = io_channel_write_blk(fs->io, blk, 1,
102                                                       bitmap_block);
103                         if (retval)
104                                 return EXT2_ET_BLOCK_BITMAP_WRITE;
105                 }
106                 block_bitmap += nbytes;
107         }
108         fs->flags |= EXT2_FLAG_CHANGED;
109         fs->flags &= ~EXT2_FLAG_BB_DIRTY;
110         free(bitmap_block);
111         return 0;
112 }
113
114 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
115 {
116         int i;
117         char *block_bitmap = 0, *inode_bitmap = 0;
118         char *buf;
119         errcode_t retval;
120         int block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
121         int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
122         blk_t   blk;
123
124         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
125
126         fs->write_bitmaps = ext2fs_write_bitmaps;
127
128         buf = malloc(strlen(fs->device_name) + 80);
129         if (do_block) {
130                 if (fs->block_map)
131                         ext2fs_free_block_bitmap(fs->block_map);
132                 sprintf(buf, "block bitmap for %s", fs->device_name);
133                 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
134                 if (retval)
135                         goto cleanup;
136                 block_bitmap = fs->block_map->bitmap;
137         }
138         if (do_inode) {
139                 if (fs->inode_map)
140                         ext2fs_free_inode_bitmap(fs->inode_map);
141                 sprintf(buf, "inode bitmap for %s", fs->device_name);
142                 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
143                 if (retval)
144                         goto cleanup;
145                 inode_bitmap = fs->inode_map->bitmap;
146         }
147         free(buf);
148
149         for (i = 0; i < fs->group_desc_count; i++) {
150                 if (block_bitmap) {
151                         blk = fs->group_desc[i].bg_block_bitmap;
152                         if (blk) {
153                                 retval = io_channel_read_blk(fs->io, blk,
154                                              -block_nbytes, block_bitmap);
155                                 if (retval) {
156                                         retval = EXT2_ET_BLOCK_BITMAP_READ;
157                                         goto cleanup;
158                                 }
159                         } else
160                                 memset(block_bitmap, 0, block_nbytes);
161                         block_bitmap += block_nbytes;
162                 }
163                 if (inode_bitmap) {
164                         blk = fs->group_desc[i].bg_inode_bitmap;
165                         if (blk) {
166                                 retval = io_channel_read_blk(fs->io, blk,
167                                              -inode_nbytes, inode_bitmap);
168                                 if (retval) {
169                                         retval = EXT2_ET_INODE_BITMAP_READ;
170                                         goto cleanup;
171                                 }
172                         } else
173                                 memset(inode_bitmap, 0, inode_nbytes);
174                         inode_bitmap += inode_nbytes;
175                 }
176         }
177         return 0;
178         
179 cleanup:
180         if (do_block) {
181                 free(fs->block_map);
182                 fs->block_map = 0;
183         }
184         if (do_inode) {
185                 free(fs->inode_map);
186                 fs->inode_map = 0;
187         }
188         if (buf)
189                 free(buf);
190         return retval;
191 }
192
193 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
194 {
195         return read_bitmaps(fs, 1, 0);
196 }
197
198 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
199 {
200         return read_bitmaps(fs, 0, 1);
201 }
202
203 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
204 {
205
206         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
207
208         return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
209 }
210
211 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
212 {
213         errcode_t       retval;
214
215         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
216
217         if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
218                 retval = ext2fs_write_block_bitmap(fs);
219                 if (retval)
220                         return retval;
221         }
222         if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
223                 retval = ext2fs_write_inode_bitmap(fs);
224                 if (retval)
225                         return retval;
226         }
227         return 0;
228 }       
229