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