closefs.c revision 43ec8734f2ecd0a345e831f45fd3dfb077426811
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#if EXT2_FLAT_INCLUDES
20#include "ext2_fs.h"
21#else
22#include <linux/ext2_fs.h>
23#endif
24
25#include "ext2fsP.h"
26
27static int test_root(int a, int b)
28{
29	if (a == 0)
30		return 1;
31	while (1) {
32		if (a == 1)
33			return 1;
34		if (a % b)
35			return 0;
36		a = a / b;
37	}
38}
39
40int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
41{
42	if (!(fs->super->s_feature_ro_compat &
43	      EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
44		return 1;
45
46	if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
47	    test_root(group_block, 7))
48		return 1;
49
50	return 0;
51}
52
53/*
54 * This function forces out the primary superblock.  We need to only
55 * write out those fields which we have changed, since if the
56 * filesystem is mounted, it may have changed some of the other
57 * fields.
58 *
59 * It takes as input a superblock which has already been byte swapped
60 * (if necessary).
61 *
62 */
63static errcode_t write_primary_superblock(ext2_filsys fs,
64					  struct ext2_super_block *super)
65{
66	__u16		*old_super, *new_super;
67	int		check_idx, write_idx, size;
68	errcode_t	retval;
69
70	if (!fs->io->manager->write_byte || !fs->orig_super) {
71		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
72		retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
73					      super);
74		io_channel_set_blksize(fs->io, fs->blocksize);
75		return retval;
76	}
77
78	old_super = (__u16 *) fs->orig_super;
79	new_super = (__u16 *) super;
80
81	for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
82		if (old_super[check_idx] == new_super[check_idx])
83			continue;
84		write_idx = check_idx;
85		for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
86			if (old_super[check_idx] == new_super[check_idx])
87				break;
88		size = 2 * (check_idx - write_idx);
89#if 0
90		printf("Writing %d bytes starting at %d\n",
91		       size, write_idx*2);
92#endif
93		retval = io_channel_write_byte(fs->io,
94			       SUPERBLOCK_OFFSET + (2 * write_idx), size,
95					       new_super + write_idx);
96		if (retval)
97			return retval;
98	}
99	return 0;
100}
101
102
103/*
104 * Updates the revision to EXT2_DYNAMIC_REV
105 */
106void ext2fs_update_dynamic_rev(ext2_filsys fs)
107{
108	struct ext2_super_block *sb = fs->super;
109
110	if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
111		return;
112
113	sb->s_rev_level = EXT2_DYNAMIC_REV;
114	sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
115	sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
116	/* s_uuid is handled by e2fsck already */
117	/* other fields should be left alone */
118}
119
120errcode_t ext2fs_flush(ext2_filsys fs)
121{
122	dgrp_t		i,j,maxgroup,sgrp;
123	blk_t		group_block;
124	errcode_t	retval;
125	char		*group_ptr;
126	unsigned long	fs_state;
127	struct ext2_super_block *super_shadow = 0;
128	struct ext2_group_desc *group_shadow = 0;
129	struct ext2_group_desc *s, *t;
130
131	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
132
133	fs_state = fs->super->s_state;
134
135	fs->super->s_wtime = time(NULL);
136	fs->super->s_block_group_nr = 0;
137	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
138		retval = EXT2_ET_NO_MEMORY;
139		retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
140					(void **) &super_shadow);
141		if (retval)
142			goto errout;
143		retval = ext2fs_get_mem((size_t)(fs->blocksize *
144						 fs->desc_blocks),
145					(void **) &group_shadow);
146		if (retval)
147			goto errout;
148		memset(group_shadow, 0, (size_t) fs->blocksize *
149		       fs->desc_blocks);
150
151		/* swap the superblock */
152		*super_shadow = *fs->super;
153		ext2fs_swap_super(super_shadow);
154
155		/* swap the group descriptors */
156		for (j=0, s=fs->group_desc, t=group_shadow;
157		     j < fs->group_desc_count; j++, t++, s++) {
158			*t = *s;
159			ext2fs_swap_group_desc(t);
160		}
161	} else {
162		super_shadow = fs->super;
163		group_shadow = fs->group_desc;
164	}
165
166	/*
167	 * Write out master superblock.  This has to be done
168	 * separately, since it is located at a fixed location
169	 * (SUPERBLOCK_OFFSET).
170	 */
171	retval = write_primary_superblock(fs, super_shadow);
172	if (retval)
173		goto errout;
174
175	/*
176	 * Set the state of the FS to be non-valid.  (The state has
177	 * already been backed up earlier, and will be restored when
178	 * we exit.)
179	 */
180	fs->super->s_state &= ~EXT2_VALID_FS;
181	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
182		*super_shadow = *fs->super;
183		ext2fs_swap_super(super_shadow);
184	}
185
186	/*
187	 * Write out the master group descriptors, and the backup
188	 * superblocks and group descriptors.
189	 */
190	group_block = fs->super->s_first_data_block;
191	maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
192		fs->group_desc_count;
193	for (i = 0; i < maxgroup; i++) {
194		if (!ext2fs_bg_has_super(fs, i))
195			goto next_group;
196
197		sgrp = i;
198		if (sgrp > ((1 << 16) - 1))
199			sgrp = (1 << 16) - 1;
200		if (fs->flags & EXT2_FLAG_SWAP_BYTES)
201			super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
202		else
203			fs->super->s_block_group_nr = sgrp;
204
205		if (i !=0 ) {
206			retval = io_channel_write_blk(fs->io, group_block,
207						      -SUPERBLOCK_SIZE,
208						      super_shadow);
209			if (retval)
210				goto errout;
211		}
212		if (fs->flags & EXT2_FLAG_SUPER_ONLY)
213			goto next_group;
214		group_ptr = (char *) group_shadow;
215		for (j=0; j < fs->desc_blocks; j++) {
216			retval = io_channel_write_blk(fs->io,
217						      group_block+1+j, 1,
218						      group_ptr);
219			if (retval)
220				goto errout;
221			group_ptr += fs->blocksize;
222		}
223	next_group:
224		group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
225	}
226	fs->super->s_block_group_nr = 0;
227
228	/*
229	 * If the write_bitmaps() function is present, call it to
230	 * flush the bitmaps.  This is done this way so that a simple
231	 * program that doesn't mess with the bitmaps doesn't need to
232	 * drag in the bitmaps.c code.
233	 */
234	if (fs->write_bitmaps) {
235		retval = fs->write_bitmaps(fs);
236		if (retval)
237			goto errout;
238	}
239
240	/*
241	 * Flush the blocks out to disk
242	 */
243	retval = io_channel_flush(fs->io);
244errout:
245	fs->super->s_state = fs_state;
246	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
247		if (super_shadow)
248			ext2fs_free_mem((void **) &super_shadow);
249		if (group_shadow)
250			ext2fs_free_mem((void **) &group_shadow);
251	}
252	return retval;
253}
254
255errcode_t ext2fs_close(ext2_filsys fs)
256{
257	errcode_t	retval;
258
259	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
260
261	if (fs->flags & EXT2_FLAG_DIRTY) {
262		retval = ext2fs_flush(fs);
263		if (retval)
264			return retval;
265	}
266	if (fs->write_bitmaps) {
267		retval = fs->write_bitmaps(fs);
268		if (retval)
269			return retval;
270	}
271	ext2fs_free(fs);
272	return 0;
273}
274
275