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