Whamcloud - gitweb
Add support for a new inode flag, which is to be used
[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         memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
95         return 0;
96 }
97
98
99 /*
100  * Updates the revision to EXT2_DYNAMIC_REV
101  */
102 void ext2fs_update_dynamic_rev(ext2_filsys fs)
103 {
104         struct ext2_super_block *sb = fs->super;
105
106         if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
107                 return;
108
109         sb->s_rev_level = EXT2_DYNAMIC_REV;
110         sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
111         sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
112         /* s_uuid is handled by e2fsck already */
113         /* other fields should be left alone */
114 }
115
116 /*
117  * This writes out the block group descriptors the original,
118  * old-fashioned way.
119  */
120 static errcode_t write_bgdesc(ext2_filsys fs, dgrp_t group, blk_t group_block,
121                               struct ext2_group_desc *group_shadow)
122 {
123         errcode_t       retval;
124         char            *group_ptr = (char *) group_shadow;
125         int             j, old_desc_blocks;
126         int             has_super = ext2fs_bg_has_super(fs, group);
127         dgrp_t          meta_bg_size, meta_bg;
128
129         meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
130         meta_bg = group / meta_bg_size;
131         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
132                 old_desc_blocks = fs->super->s_first_meta_bg;
133         else
134                 old_desc_blocks = fs->desc_blocks;
135         if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
136             (meta_bg < fs->super->s_first_meta_bg)) {
137                 if (!has_super)
138                         return 0;
139                 for (j=0; j < old_desc_blocks; j++) {
140                         retval = io_channel_write_blk(fs->io,
141                                                       group_block+1+j, 1,
142                                                       group_ptr);
143                         if (retval)
144                                 return retval;
145                         group_ptr += fs->blocksize;
146                 }
147         } else {
148                 if (has_super)
149                         group_block++;
150                 if (((group % meta_bg_size) == 0) ||
151                     ((group % meta_bg_size) == 1) ||
152                     ((group % meta_bg_size) == (meta_bg_size-1))) {
153                         return io_channel_write_blk(fs->io, group_block,
154                                 1, group_ptr + (meta_bg*fs->blocksize));
155                 }
156         }
157         return 0;
158 }
159
160
161 static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
162                                     blk_t group_block,
163                                     struct ext2_super_block *super_shadow)
164 {
165         dgrp_t  sgrp = group;
166         
167         if (sgrp > ((1 << 16) - 1))
168                 sgrp = (1 << 16) - 1;
169 #ifdef EXT2FS_ENABLE_SWAPFS
170         if (fs->flags & EXT2_FLAG_SWAP_BYTES)
171                 super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
172         else
173 #endif
174                 fs->super->s_block_group_nr = sgrp;
175
176         return io_channel_write_blk(fs->io, group_block, -SUPERBLOCK_SIZE, 
177                                     super_shadow);
178 }
179
180
181 errcode_t ext2fs_flush(ext2_filsys fs)
182 {
183         dgrp_t          i,j,maxgroup;
184         blk_t           group_block;
185         errcode_t       retval;
186         unsigned long   fs_state;
187         struct ext2_super_block *super_shadow = 0;
188         struct ext2_group_desc *group_shadow = 0;
189         struct ext2_group_desc *s, *t;
190         
191         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
192
193         fs_state = fs->super->s_state;
194
195         fs->super->s_wtime = time(NULL);
196         fs->super->s_block_group_nr = 0;
197 #ifdef EXT2FS_ENABLE_SWAPFS
198         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
199                 retval = EXT2_ET_NO_MEMORY;
200                 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
201                                         (void **) &super_shadow);
202                 if (retval)
203                         goto errout;
204                 retval = ext2fs_get_mem((size_t)(fs->blocksize *
205                                                  fs->desc_blocks),
206                                         (void **) &group_shadow);
207                 if (retval)
208                         goto errout;
209                 memset(group_shadow, 0, (size_t) fs->blocksize *
210                        fs->desc_blocks);
211
212                 /* swap the superblock */
213                 *super_shadow = *fs->super;
214                 ext2fs_swap_super(super_shadow);
215
216                 /* swap the group descriptors */
217                 for (j=0, s=fs->group_desc, t=group_shadow;
218                      j < fs->group_desc_count; j++, t++, s++) {
219                         *t = *s;
220                         ext2fs_swap_group_desc(t);
221                 }
222         } else {
223                 super_shadow = fs->super;
224                 group_shadow = fs->group_desc;
225         }
226 #else
227         super_shadow = fs->super;
228         group_shadow = fs->group_desc;
229 #endif
230         
231         /*
232          * Write out master superblock.  This has to be done
233          * separately, since it is located at a fixed location
234          * (SUPERBLOCK_OFFSET).
235          */
236         retval = write_primary_superblock(fs, super_shadow);
237         if (retval)
238                 goto errout;
239
240         /*
241          * If this is an external journal device, don't write out the
242          * block group descriptors or any of the backup superblocks
243          */
244         if (fs->super->s_feature_incompat &
245             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
246                 retval = 0;
247                 goto errout;
248         }
249
250         /*
251          * Set the state of the FS to be non-valid.  (The state has
252          * already been backed up earlier, and will be restored when
253          * we exit.)
254          */
255         fs->super->s_state &= ~EXT2_VALID_FS;
256 #ifdef EXT2FS_ENABLE_SWAPFS
257         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
258                 *super_shadow = *fs->super;
259                 ext2fs_swap_super(super_shadow);
260         }
261 #endif
262
263         /*
264          * Write out the master group descriptors, and the backup
265          * superblocks and group descriptors.
266          */
267         group_block = fs->super->s_first_data_block;
268         maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
269                 fs->group_desc_count;
270         for (i = 0; i < maxgroup; i++) {
271                 if (i && ext2fs_bg_has_super(fs, i)) {
272                         retval = write_backup_super(fs, i, group_block,
273                                                     super_shadow);
274                         if (retval)
275                                 goto errout;
276                 }
277                 if (!(fs->flags & EXT2_FLAG_SUPER_ONLY)) {
278                         if ((retval = write_bgdesc(fs, i, group_block, 
279                                                    group_shadow)))
280                                 goto errout;
281                 }
282                 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
283         }
284         fs->super->s_block_group_nr = 0;
285
286         /*
287          * If the write_bitmaps() function is present, call it to
288          * flush the bitmaps.  This is done this way so that a simple
289          * program that doesn't mess with the bitmaps doesn't need to
290          * drag in the bitmaps.c code.
291          */
292         if (fs->write_bitmaps) {
293                 retval = fs->write_bitmaps(fs);
294                 if (retval)
295                         goto errout;
296         }
297
298         fs->flags &= ~EXT2_FLAG_DIRTY;
299
300         /*
301          * Flush the blocks out to disk
302          */
303         retval = io_channel_flush(fs->io);
304 errout:
305         fs->super->s_state = fs_state;
306         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
307                 if (super_shadow)
308                         ext2fs_free_mem((void **) &super_shadow);
309                 if (group_shadow)
310                         ext2fs_free_mem((void **) &group_shadow);
311         }
312         return retval;
313 }
314
315 errcode_t ext2fs_close(ext2_filsys fs)
316 {
317         errcode_t       retval;
318         
319         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
320
321         if (fs->flags & EXT2_FLAG_DIRTY) {
322                 retval = ext2fs_flush(fs);
323                 if (retval)
324                         return retval;
325         }
326         if (fs->write_bitmaps) {
327                 retval = fs->write_bitmaps(fs);
328                 if (retval)
329                         return retval;
330         }
331         ext2fs_free(fs);
332         return 0;
333 }
334