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