1 /*
2 * closefs.c --- close an ext2 filesystem
3 *
4 * Copyright (C) 1993, 1994, 1995, 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 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <time.h>
17 #include <string.h>
18
19 #include "ext2_fs.h"
20 #include "ext2fsP.h"
21
test_root(int a,int b)22 static int test_root(int a, int b)
23 {
24 if (a == 0)
25 return 1;
26 while (1) {
27 if (a == 1)
28 return 1;
29 if (a % b)
30 return 0;
31 a = a / b;
32 }
33 }
34
ext2fs_bg_has_super(ext2_filsys fs,int group_block)35 int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
36 {
37 if (!(fs->super->s_feature_ro_compat &
38 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
39 return 1;
40
41 if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
42 test_root(group_block, 7))
43 return 1;
44
45 return 0;
46 }
47
48 /*
49 * This function returns the location of the superblock, block group
50 * descriptors for a given block group. It currently returns the
51 * number of free blocks assuming that inode table and allocation
52 * bitmaps will be in the group. This is not necessarily the case
53 * when the flex_bg feature is enabled, so callers should take care!
54 * It was only really intended for use by mke2fs, and even there it's
55 * not that useful. In the future, when we redo this function for
56 * 64-bit block numbers, we should probably return the number of
57 * blocks used by the super block and group descriptors instead.
58 *
59 * See also the comment for ext2fs_reserve_super_and_bgd()
60 */
ext2fs_super_and_bgd_loc(ext2_filsys fs,dgrp_t group,blk_t * ret_super_blk,blk_t * ret_old_desc_blk,blk_t * ret_new_desc_blk,int * ret_meta_bg)61 int ext2fs_super_and_bgd_loc(ext2_filsys fs,
62 dgrp_t group,
63 blk_t *ret_super_blk,
64 blk_t *ret_old_desc_blk,
65 blk_t *ret_new_desc_blk,
66 int *ret_meta_bg)
67 {
68 blk_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
69 unsigned int meta_bg, meta_bg_size;
70 blk_t numblocks, old_desc_blocks;
71 int has_super;
72
73 group_block = ext2fs_group_first_block(fs, group);
74
75 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
76 old_desc_blocks = fs->super->s_first_meta_bg;
77 else
78 old_desc_blocks =
79 fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
80
81 if (group == fs->group_desc_count-1) {
82 numblocks = (fs->super->s_blocks_count -
83 fs->super->s_first_data_block) %
84 fs->super->s_blocks_per_group;
85 if (!numblocks)
86 numblocks = fs->super->s_blocks_per_group;
87 } else
88 numblocks = fs->super->s_blocks_per_group;
89
90 has_super = ext2fs_bg_has_super(fs, group);
91
92 if (has_super) {
93 super_blk = group_block;
94 numblocks--;
95 }
96 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
97 meta_bg = group / meta_bg_size;
98
99 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
100 (meta_bg < fs->super->s_first_meta_bg)) {
101 if (has_super) {
102 old_desc_blk = group_block + 1;
103 numblocks -= old_desc_blocks;
104 }
105 } else {
106 if (((group % meta_bg_size) == 0) ||
107 ((group % meta_bg_size) == 1) ||
108 ((group % meta_bg_size) == (meta_bg_size-1))) {
109 if (has_super)
110 has_super = 1;
111 new_desc_blk = group_block + has_super;
112 numblocks--;
113 }
114 }
115
116 numblocks -= 2 + fs->inode_blocks_per_group;
117
118 if (ret_super_blk)
119 *ret_super_blk = super_blk;
120 if (ret_old_desc_blk)
121 *ret_old_desc_blk = old_desc_blk;
122 if (ret_new_desc_blk)
123 *ret_new_desc_blk = new_desc_blk;
124 if (ret_meta_bg)
125 *ret_meta_bg = meta_bg;
126 return (numblocks);
127 }
128
129
130 /*
131 * This function forces out the primary superblock. We need to only
132 * write out those fields which we have changed, since if the
133 * filesystem is mounted, it may have changed some of the other
134 * fields.
135 *
136 * It takes as input a superblock which has already been byte swapped
137 * (if necessary).
138 *
139 */
write_primary_superblock(ext2_filsys fs,struct ext2_super_block * super)140 static errcode_t write_primary_superblock(ext2_filsys fs,
141 struct ext2_super_block *super)
142 {
143 __u16 *old_super, *new_super;
144 int check_idx, write_idx, size;
145 errcode_t retval;
146
147 if (!fs->io->manager->write_byte || !fs->orig_super) {
148 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
149 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
150 super);
151 io_channel_set_blksize(fs->io, fs->blocksize);
152 return retval;
153 }
154
155 old_super = (__u16 *) fs->orig_super;
156 new_super = (__u16 *) super;
157
158 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
159 if (old_super[check_idx] == new_super[check_idx])
160 continue;
161 write_idx = check_idx;
162 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
163 if (old_super[check_idx] == new_super[check_idx])
164 break;
165 size = 2 * (check_idx - write_idx);
166 #if 0
167 printf("Writing %d bytes starting at %d\n",
168 size, write_idx*2);
169 #endif
170 retval = io_channel_write_byte(fs->io,
171 SUPERBLOCK_OFFSET + (2 * write_idx), size,
172 new_super + write_idx);
173 if (retval)
174 return retval;
175 }
176 memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
177 return 0;
178 }
179
180
181 /*
182 * Updates the revision to EXT2_DYNAMIC_REV
183 */
ext2fs_update_dynamic_rev(ext2_filsys fs)184 void ext2fs_update_dynamic_rev(ext2_filsys fs)
185 {
186 struct ext2_super_block *sb = fs->super;
187
188 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
189 return;
190
191 sb->s_rev_level = EXT2_DYNAMIC_REV;
192 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
193 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
194 /* s_uuid is handled by e2fsck already */
195 /* other fields should be left alone */
196 }
197
write_backup_super(ext2_filsys fs,dgrp_t group,blk_t group_block,struct ext2_super_block * super_shadow)198 static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
199 blk_t group_block,
200 struct ext2_super_block *super_shadow)
201 {
202 dgrp_t sgrp = group;
203
204 if (sgrp > ((1 << 16) - 1))
205 sgrp = (1 << 16) - 1;
206 #ifdef WORDS_BIGENDIAN
207 super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
208 #else
209 fs->super->s_block_group_nr = sgrp;
210 #endif
211
212 return io_channel_write_blk(fs->io, group_block, -SUPERBLOCK_SIZE,
213 super_shadow);
214 }
215
216
ext2fs_flush(ext2_filsys fs)217 errcode_t ext2fs_flush(ext2_filsys fs)
218 {
219 dgrp_t i;
220 errcode_t retval;
221 unsigned long fs_state;
222 __u32 feature_incompat;
223 struct ext2_super_block *super_shadow = 0;
224 struct ext2_group_desc *group_shadow = 0;
225 #ifdef WORDS_BIGENDIAN
226 struct ext2_group_desc *s, *t;
227 dgrp_t j;
228 #endif
229 char *group_ptr;
230 int old_desc_blocks;
231
232 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
233
234 fs_state = fs->super->s_state;
235 feature_incompat = fs->super->s_feature_incompat;
236
237 fs->super->s_wtime = fs->now ? fs->now : time(NULL);
238 fs->super->s_block_group_nr = 0;
239 #ifdef WORDS_BIGENDIAN
240 retval = EXT2_ET_NO_MEMORY;
241 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
242 if (retval)
243 goto errout;
244 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
245 &group_shadow);
246 if (retval)
247 goto errout;
248 memset(group_shadow, 0, (size_t) fs->blocksize *
249 fs->desc_blocks);
250
251 /* swap the group descriptors */
252 for (j=0, s=fs->group_desc, t=group_shadow;
253 j < fs->group_desc_count; j++, t++, s++) {
254 *t = *s;
255 ext2fs_swap_group_desc(t);
256 }
257 #else
258 super_shadow = fs->super;
259 group_shadow = fs->group_desc;
260 #endif
261
262 /*
263 * Set the state of the FS to be non-valid. (The state has
264 * already been backed up earlier, and will be restored after
265 * we write out the backup superblocks.)
266 */
267 fs->super->s_state &= ~EXT2_VALID_FS;
268 fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
269 #ifdef WORDS_BIGENDIAN
270 *super_shadow = *fs->super;
271 ext2fs_swap_super(super_shadow);
272 #endif
273
274 /*
275 * If this is an external journal device, don't write out the
276 * block group descriptors or any of the backup superblocks
277 */
278 if (fs->super->s_feature_incompat &
279 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
280 goto write_primary_superblock_only;
281
282 /*
283 * Write out the master group descriptors, and the backup
284 * superblocks and group descriptors.
285 */
286 group_ptr = (char *) group_shadow;
287 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
288 old_desc_blocks = fs->super->s_first_meta_bg;
289 else
290 old_desc_blocks = fs->desc_blocks;
291
292 for (i = 0; i < fs->group_desc_count; i++) {
293 blk_t super_blk, old_desc_blk, new_desc_blk;
294 int meta_bg;
295
296 ext2fs_super_and_bgd_loc(fs, i, &super_blk, &old_desc_blk,
297 &new_desc_blk, &meta_bg);
298
299 if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
300 retval = write_backup_super(fs, i, super_blk,
301 super_shadow);
302 if (retval)
303 goto errout;
304 }
305 if (fs->flags & EXT2_FLAG_SUPER_ONLY)
306 continue;
307 if ((old_desc_blk) &&
308 (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
309 retval = io_channel_write_blk(fs->io,
310 old_desc_blk, old_desc_blocks, group_ptr);
311 if (retval)
312 goto errout;
313 }
314 if (new_desc_blk) {
315 retval = io_channel_write_blk(fs->io, new_desc_blk,
316 1, group_ptr + (meta_bg*fs->blocksize));
317 if (retval)
318 goto errout;
319 }
320 }
321
322 /*
323 * If the write_bitmaps() function is present, call it to
324 * flush the bitmaps. This is done this way so that a simple
325 * program that doesn't mess with the bitmaps doesn't need to
326 * drag in the bitmaps.c code.
327 */
328 if (fs->write_bitmaps) {
329 retval = fs->write_bitmaps(fs);
330 if (retval)
331 goto errout;
332 }
333
334 write_primary_superblock_only:
335 /*
336 * Write out master superblock. This has to be done
337 * separately, since it is located at a fixed location
338 * (SUPERBLOCK_OFFSET). We flush all other pending changes
339 * out to disk first, just to avoid a race condition with an
340 * insy-tinsy window....
341 */
342
343 fs->super->s_block_group_nr = 0;
344 fs->super->s_state = fs_state;
345 fs->super->s_feature_incompat = feature_incompat;
346 #ifdef WORDS_BIGENDIAN
347 *super_shadow = *fs->super;
348 ext2fs_swap_super(super_shadow);
349 #endif
350
351 retval = io_channel_flush(fs->io);
352 retval = write_primary_superblock(fs, super_shadow);
353 if (retval)
354 goto errout;
355
356 fs->flags &= ~EXT2_FLAG_DIRTY;
357
358 retval = io_channel_flush(fs->io);
359 errout:
360 fs->super->s_state = fs_state;
361 #ifdef WORDS_BIGENDIAN
362 if (super_shadow)
363 ext2fs_free_mem(&super_shadow);
364 if (group_shadow)
365 ext2fs_free_mem(&group_shadow);
366 #endif
367 return retval;
368 }
369
ext2fs_close(ext2_filsys fs)370 errcode_t ext2fs_close(ext2_filsys fs)
371 {
372 errcode_t retval;
373 int meta_blks;
374 io_stats stats = 0;
375
376 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
377
378 if (fs->write_bitmaps) {
379 retval = fs->write_bitmaps(fs);
380 if (retval)
381 return retval;
382 }
383 if (fs->super->s_kbytes_written &&
384 fs->io->manager->get_stats)
385 fs->io->manager->get_stats(fs->io, &stats);
386 if (stats && stats->bytes_written && (fs->flags & EXT2_FLAG_RW)) {
387 fs->super->s_kbytes_written += stats->bytes_written >> 10;
388 meta_blks = fs->desc_blocks + 1;
389 if (!(fs->flags & EXT2_FLAG_SUPER_ONLY))
390 fs->super->s_kbytes_written += meta_blks /
391 (fs->blocksize / 1024);
392 if ((fs->flags & EXT2_FLAG_DIRTY) == 0)
393 fs->flags |= EXT2_FLAG_SUPER_ONLY | EXT2_FLAG_DIRTY;
394 }
395 if (fs->flags & EXT2_FLAG_DIRTY) {
396 retval = ext2fs_flush(fs);
397 if (retval)
398 return retval;
399 }
400 ext2fs_free(fs);
401 return 0;
402 }
403
404