closefs.c revision 544349270e4c74a6feb971123884a8cf5052a7ee
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 = 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 = fs->desc_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 EXT2FS_ENABLE_SWAPFS
194	if (fs->flags & EXT2_FLAG_SWAP_BYTES)
195		super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
196	else
197#endif
198		fs->super->s_block_group_nr = sgrp;
199
200	return io_channel_write_blk(fs->io, group_block, -SUPERBLOCK_SIZE,
201				    super_shadow);
202}
203
204
205errcode_t ext2fs_flush(ext2_filsys fs)
206{
207	dgrp_t		i,j;
208	blk_t		group_block;
209	errcode_t	retval;
210	unsigned long	fs_state;
211	struct ext2_super_block *super_shadow = 0;
212	struct ext2_group_desc *group_shadow = 0;
213	struct ext2_group_desc *s, *t;
214	char	*group_ptr;
215	int	old_desc_blocks;
216
217	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
218
219	fs_state = fs->super->s_state;
220
221	fs->super->s_wtime = time(NULL);
222	fs->super->s_block_group_nr = 0;
223#ifdef EXT2FS_ENABLE_SWAPFS
224	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
225		retval = EXT2_ET_NO_MEMORY;
226		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
227		if (retval)
228			goto errout;
229		retval = ext2fs_get_mem((size_t)(fs->blocksize *
230						 fs->desc_blocks),
231					&group_shadow);
232		if (retval)
233			goto errout;
234		memset(group_shadow, 0, (size_t) fs->blocksize *
235		       fs->desc_blocks);
236
237		/* swap the superblock */
238		*super_shadow = *fs->super;
239		ext2fs_swap_super(super_shadow);
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	 * Write out master superblock.  This has to be done
258	 * separately, since it is located at a fixed location
259	 * (SUPERBLOCK_OFFSET).
260	 */
261	retval = write_primary_superblock(fs, super_shadow);
262	if (retval)
263		goto errout;
264
265	/*
266	 * If this is an external journal device, don't write out the
267	 * block group descriptors or any of the backup superblocks
268	 */
269	if (fs->super->s_feature_incompat &
270	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
271		retval = 0;
272		goto errout;
273	}
274
275	/*
276	 * Set the state of the FS to be non-valid.  (The state has
277	 * already been backed up earlier, and will be restored when
278	 * we exit.)
279	 */
280	fs->super->s_state &= ~EXT2_VALID_FS;
281#ifdef EXT2FS_ENABLE_SWAPFS
282	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
283		*super_shadow = *fs->super;
284		ext2fs_swap_super(super_shadow);
285	}
286#endif
287
288	/*
289	 * Write out the master group descriptors, and the backup
290	 * superblocks and group descriptors.
291	 */
292	group_block = fs->super->s_first_data_block;
293	group_ptr = (char *) group_shadow;
294	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
295		old_desc_blocks = fs->super->s_first_meta_bg;
296	else
297		old_desc_blocks = fs->desc_blocks;
298
299	for (i = 0; i < fs->group_desc_count; i++) {
300		blk_t	super_blk, old_desc_blk, new_desc_blk;
301		int	meta_bg;
302
303		ext2fs_super_and_bgd_loc(fs, i, &super_blk, &old_desc_blk,
304					 &new_desc_blk, &meta_bg);
305
306		if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
307			retval = write_backup_super(fs, i, super_blk,
308						    super_shadow);
309			if (retval)
310				goto errout;
311		}
312		if (fs->flags & EXT2_FLAG_SUPER_ONLY)
313			continue;
314		if ((old_desc_blk) &&
315		    (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
316			retval = io_channel_write_blk(fs->io,
317			      old_desc_blk, old_desc_blocks, group_ptr);
318			if (retval)
319				goto errout;
320		}
321		if (new_desc_blk) {
322			retval = io_channel_write_blk(fs->io, new_desc_blk,
323				1, group_ptr + (meta_bg*fs->blocksize));
324			if (retval)
325				goto errout;
326		}
327	}
328	fs->super->s_block_group_nr = 0;
329
330	/*
331	 * If the write_bitmaps() function is present, call it to
332	 * flush the bitmaps.  This is done this way so that a simple
333	 * program that doesn't mess with the bitmaps doesn't need to
334	 * drag in the bitmaps.c code.
335	 */
336	if (fs->write_bitmaps) {
337		retval = fs->write_bitmaps(fs);
338		if (retval)
339			goto errout;
340	}
341
342	fs->flags &= ~EXT2_FLAG_DIRTY;
343
344	/*
345	 * Flush the blocks out to disk
346	 */
347	retval = io_channel_flush(fs->io);
348errout:
349	fs->super->s_state = fs_state;
350	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
351		if (super_shadow)
352			ext2fs_free_mem(&super_shadow);
353		if (group_shadow)
354			ext2fs_free_mem(&group_shadow);
355	}
356	return retval;
357}
358
359errcode_t ext2fs_close(ext2_filsys fs)
360{
361	errcode_t	retval;
362
363	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
364
365	if (fs->flags & EXT2_FLAG_DIRTY) {
366		retval = ext2fs_flush(fs);
367		if (retval)
368			return retval;
369	}
370	if (fs->write_bitmaps) {
371		retval = fs->write_bitmaps(fs);
372		if (retval)
373			return retval;
374	}
375	ext2fs_free(fs);
376	return 0;
377}
378
379