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