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