Whamcloud - gitweb
Merge branch 'maint' into next
[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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <time.h>
18 #include <string.h>
19
20 #include "ext2_fs.h"
21 #include "ext2fsP.h"
22
23 static int test_root(unsigned int a, unsigned int b)
24 {
25         while (1) {
26                 if (a < b)
27                         return 0;
28                 if (a == b)
29                         return 1;
30                 if (a % b)
31                         return 0;
32                 a = a / b;
33         }
34 }
35
36 int ext2fs_bg_has_super(ext2_filsys fs, dgrp_t group)
37 {
38         if (group == 0)
39                 return 1;
40         if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
41                 if (group == fs->super->s_backup_bgs[0] ||
42                     group == fs->super->s_backup_bgs[1])
43                         return 1;
44                 return 0;
45         }
46         if ((group <= 1) || !(fs->super->s_feature_ro_compat &
47                               EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
48                 return 1;
49         if (!(group & 1))
50                 return 0;
51         if (test_root(group, 3) || (test_root(group, 5)) ||
52             test_root(group, 7))
53                 return 1;
54
55         return 0;
56 }
57
58 /*
59  * ext2fs_super_and_bgd_loc2()
60  * @fs:                 ext2 fs pointer
61  * @group               given block group
62  * @ret_super_blk:      if !NULL, returns super block location
63  * @ret_old_desc_blk:   if !NULL, returns location of the old block
64  *                      group descriptor
65  * @ret_new_desc_blk:   if !NULL, returns location of meta_bg block
66  *                      group descriptor
67  * @ret_used_blks:      if !NULL, returns number of blocks used by
68  *                      super block and group_descriptors.
69  *
70  * Returns errcode_t of 0
71  */
72 errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
73                                            dgrp_t group,
74                                            blk64_t *ret_super_blk,
75                                            blk64_t *ret_old_desc_blk,
76                                            blk64_t *ret_new_desc_blk,
77                                            blk_t *ret_used_blks)
78 {
79         blk64_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
80         unsigned int meta_bg, meta_bg_size;
81         blk_t   numblocks = 0;
82         blk64_t old_desc_blocks;
83         int     has_super;
84
85         group_block = ext2fs_group_first_block2(fs, group);
86         if (group_block == 0 && fs->blocksize == 1024)
87                 group_block = 1; /* Deal with 1024 blocksize && bigalloc */
88
89         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
90                 old_desc_blocks = fs->super->s_first_meta_bg;
91         else
92                 old_desc_blocks =
93                         fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
94
95         has_super = ext2fs_bg_has_super(fs, group);
96
97         if (has_super) {
98                 super_blk = group_block;
99                 numblocks++;
100         }
101         meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
102         meta_bg = group / meta_bg_size;
103
104         if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
105             (meta_bg < fs->super->s_first_meta_bg)) {
106                 if (has_super) {
107                         old_desc_blk = group_block + 1;
108                         numblocks += old_desc_blocks;
109                 }
110         } else {
111                 if (((group % meta_bg_size) == 0) ||
112                     ((group % meta_bg_size) == 1) ||
113                     ((group % meta_bg_size) == (meta_bg_size-1))) {
114                         if (has_super)
115                                 has_super = 1;
116                         new_desc_blk = group_block + has_super;
117                         numblocks++;
118                 }
119         }
120
121         if (ret_super_blk)
122                 *ret_super_blk = super_blk;
123         if (ret_old_desc_blk)
124                 *ret_old_desc_blk = old_desc_blk;
125         if (ret_new_desc_blk)
126                 *ret_new_desc_blk = new_desc_blk;
127         if (ret_used_blks)
128                 *ret_used_blks = numblocks;
129
130         return 0;
131 }
132
133 /*
134  * This function returns the location of the superblock, block group
135  * descriptors for a given block group.  It currently returns the
136  * number of free blocks assuming that inode table and allocation
137  * bitmaps will be in the group.  This is not necessarily the case
138  * when the flex_bg feature is enabled, so callers should take care!
139  * It was only really intended for use by mke2fs, and even there it's
140  * not that useful.
141  *
142  * The ext2fs_super_and_bgd_loc2() function is 64-bit block number
143  * capable and returns the number of blocks used by super block and
144  * group descriptors.
145  */
146 int ext2fs_super_and_bgd_loc(ext2_filsys fs,
147                              dgrp_t group,
148                              blk_t *ret_super_blk,
149                              blk_t *ret_old_desc_blk,
150                              blk_t *ret_new_desc_blk,
151                              int *ret_meta_bg)
152 {
153         blk64_t ret_super_blk2;
154         blk64_t ret_old_desc_blk2;
155         blk64_t ret_new_desc_blk2;
156         blk_t ret_used_blks;
157         blk_t numblocks;
158         unsigned int meta_bg_size;
159
160         ext2fs_super_and_bgd_loc2(fs, group, &ret_super_blk2,
161                                         &ret_old_desc_blk2,
162                                         &ret_new_desc_blk2,
163                                         &ret_used_blks);
164
165         numblocks = ext2fs_group_blocks_count(fs, group);
166
167         if (ret_super_blk)
168                 *ret_super_blk = (blk_t)ret_super_blk2;
169         if (ret_old_desc_blk)
170                 *ret_old_desc_blk = (blk_t)ret_old_desc_blk2;
171         if (ret_new_desc_blk)
172                 *ret_new_desc_blk = (blk_t)ret_new_desc_blk2;
173         if (ret_meta_bg) {
174                 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
175                 *ret_meta_bg = group / meta_bg_size;
176         }
177
178         numblocks -= 2 + fs->inode_blocks_per_group + ret_used_blks;
179
180         return numblocks;
181 }
182
183 /*
184  * This function forces out the primary superblock.  We need to only
185  * write out those fields which we have changed, since if the
186  * filesystem is mounted, it may have changed some of the other
187  * fields.
188  *
189  * It takes as input a superblock which has already been byte swapped
190  * (if necessary).
191  *
192  */
193 static errcode_t write_primary_superblock(ext2_filsys fs,
194                                           struct ext2_super_block *super)
195 {
196         __u16           *old_super, *new_super;
197         int             check_idx, write_idx, size;
198         errcode_t       retval;
199
200         if (!fs->io->manager->write_byte || !fs->orig_super) {
201         fallback:
202                 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
203                 retval = io_channel_write_blk64(fs->io, 1, -SUPERBLOCK_SIZE,
204                                               super);
205                 io_channel_set_blksize(fs->io, fs->blocksize);
206                 return retval;
207         }
208
209         old_super = (__u16 *) fs->orig_super;
210         new_super = (__u16 *) super;
211
212         for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
213                 if (old_super[check_idx] == new_super[check_idx])
214                         continue;
215                 write_idx = check_idx;
216                 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
217                         if (old_super[check_idx] == new_super[check_idx])
218                                 break;
219                 size = 2 * (check_idx - write_idx);
220 #if 0
221                 printf("Writing %d bytes starting at %d\n",
222                        size, write_idx*2);
223 #endif
224                 retval = io_channel_write_byte(fs->io,
225                                SUPERBLOCK_OFFSET + (2 * write_idx), size,
226                                                new_super + write_idx);
227                 if (retval == EXT2_ET_UNIMPLEMENTED)
228                         goto fallback;
229                 if (retval)
230                         return retval;
231         }
232         memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
233         return 0;
234 }
235
236
237 /*
238  * Updates the revision to EXT2_DYNAMIC_REV
239  */
240 void ext2fs_update_dynamic_rev(ext2_filsys fs)
241 {
242         struct ext2_super_block *sb = fs->super;
243
244         if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
245                 return;
246
247         sb->s_rev_level = EXT2_DYNAMIC_REV;
248         sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
249         sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
250         /* s_uuid is handled by e2fsck already */
251         /* other fields should be left alone */
252 }
253
254 static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
255                                     blk64_t group_block,
256                                     struct ext2_super_block *super_shadow)
257 {
258         errcode_t retval;
259         dgrp_t  sgrp = group;
260
261         if (sgrp > ((1 << 16) - 1))
262                 sgrp = (1 << 16) - 1;
263
264         super_shadow->s_block_group_nr = sgrp;
265 #ifdef WORDS_BIGENDIAN
266         ext2fs_swap_super(super_shadow);
267 #endif
268         retval = ext2fs_superblock_csum_set(fs, super_shadow);
269         if (retval)
270                 return retval;
271
272         return io_channel_write_blk64(fs->io, group_block, -SUPERBLOCK_SIZE,
273                                     super_shadow);
274 }
275
276 errcode_t ext2fs_flush(ext2_filsys fs)
277 {
278         return ext2fs_flush2(fs, 0);
279 }
280
281 errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
282 {
283         dgrp_t          i;
284         errcode_t       retval;
285         unsigned long   fs_state;
286         __u32           feature_incompat;
287         struct ext2_super_block *super_shadow = 0;
288         struct ext2_group_desc *group_shadow = 0;
289 #ifdef WORDS_BIGENDIAN
290         struct ext2_group_desc *gdp;
291         dgrp_t          j;
292 #endif
293         char    *group_ptr;
294         int     old_desc_blocks;
295         struct ext2fs_numeric_progress_struct progress;
296
297         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
298
299         fs_state = fs->super->s_state;
300         feature_incompat = fs->super->s_feature_incompat;
301
302         fs->super->s_wtime = fs->now ? fs->now : time(NULL);
303         fs->super->s_block_group_nr = 0;
304
305         /*
306          * If the write_bitmaps() function is present, call it to
307          * flush the bitmaps.  This is done this way so that a simple
308          * program that doesn't mess with the bitmaps doesn't need to
309          * drag in the bitmaps.c code.
310          *
311          * Bitmap checksums live in the group descriptor, so the
312          * bitmaps need to be written before the descriptors.
313          */
314         if (fs->write_bitmaps) {
315                 retval = fs->write_bitmaps(fs);
316                 if (retval)
317                         goto errout;
318         }
319
320         /* Prepare the group descriptors for writing */
321 #ifdef WORDS_BIGENDIAN
322         retval = EXT2_ET_NO_MEMORY;
323         retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
324         if (retval)
325                 goto errout;
326         retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
327                                   &group_shadow);
328         if (retval)
329                 goto errout;
330         memcpy(super_shadow, fs->super, sizeof(struct ext2_super_block));
331         memcpy(group_shadow, fs->group_desc, (size_t) fs->blocksize *
332                fs->desc_blocks);
333
334         /* swap the group descriptors */
335         for (j = 0; j < fs->group_desc_count; j++) {
336                 gdp = ext2fs_group_desc(fs, group_shadow, j);
337                 ext2fs_swap_group_desc2(fs, gdp);
338         }
339 #else
340         super_shadow = fs->super;
341         group_shadow = ext2fs_group_desc(fs, fs->group_desc, 0);
342 #endif
343
344         /*
345          * Set the state of the FS to be non-valid.  (The state has
346          * already been backed up earlier, and will be restored after
347          * we write out the backup superblocks.)
348          */
349         fs->super->s_state &= ~EXT2_VALID_FS;
350         fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
351
352         /*
353          * If this is an external journal device, don't write out the
354          * block group descriptors or any of the backup superblocks
355          */
356         if (fs->super->s_feature_incompat &
357             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
358                 goto write_primary_superblock_only;
359
360         /*
361          * Write out the master group descriptors, and the backup
362          * superblocks and group descriptors.
363          */
364         group_ptr = (char *) group_shadow;
365         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
366                 old_desc_blocks = fs->super->s_first_meta_bg;
367                 if (old_desc_blocks > fs->super->s_first_meta_bg)
368                         old_desc_blocks = fs->desc_blocks;
369         } else
370                 old_desc_blocks = fs->desc_blocks;
371
372         if (fs->progress_ops && fs->progress_ops->init)
373                 (fs->progress_ops->init)(fs, &progress, NULL,
374                                          fs->group_desc_count);
375
376
377         for (i = 0; i < fs->group_desc_count; i++) {
378                 blk64_t super_blk, old_desc_blk, new_desc_blk;
379
380                 if (fs->progress_ops && fs->progress_ops->update)
381                         (fs->progress_ops->update)(fs, &progress, i);
382                 ext2fs_super_and_bgd_loc2(fs, i, &super_blk, &old_desc_blk,
383                                          &new_desc_blk, 0);
384
385                 if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
386                         retval = write_backup_super(fs, i, super_blk,
387                                                     super_shadow);
388                         if (retval)
389                                 goto errout;
390                 }
391                 if (fs->flags & EXT2_FLAG_SUPER_ONLY)
392                         continue;
393                 if ((old_desc_blk) &&
394                     (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
395                         retval = io_channel_write_blk64(fs->io,
396                               old_desc_blk, old_desc_blocks, group_ptr);
397                         if (retval)
398                                 goto errout;
399                 }
400                 if (new_desc_blk) {
401                         int meta_bg = i / EXT2_DESC_PER_BLOCK(fs->super);
402
403                         retval = io_channel_write_blk64(fs->io, new_desc_blk,
404                                 1, group_ptr + (meta_bg*fs->blocksize));
405                         if (retval)
406                                 goto errout;
407                 }
408         }
409
410         if (fs->progress_ops && fs->progress_ops->close)
411                 (fs->progress_ops->close)(fs, &progress, NULL);
412
413 write_primary_superblock_only:
414         /*
415          * Write out master superblock.  This has to be done
416          * separately, since it is located at a fixed location
417          * (SUPERBLOCK_OFFSET).  We flush all other pending changes
418          * out to disk first, just to avoid a race condition with an
419          * insy-tinsy window....
420          */
421
422         fs->super->s_block_group_nr = 0;
423         fs->super->s_state = fs_state;
424         fs->super->s_feature_incompat = feature_incompat;
425 #ifdef WORDS_BIGENDIAN
426         *super_shadow = *fs->super;
427         ext2fs_swap_super(super_shadow);
428 #endif
429
430         retval = ext2fs_superblock_csum_set(fs, super_shadow);
431         if (retval)
432                 return retval;
433
434         if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
435                 retval = io_channel_flush(fs->io);
436         retval = write_primary_superblock(fs, super_shadow);
437         if (retval)
438                 goto errout;
439
440         fs->flags &= ~EXT2_FLAG_DIRTY;
441
442         if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
443                 retval = io_channel_flush(fs->io);
444 errout:
445         fs->super->s_state = fs_state;
446 #ifdef WORDS_BIGENDIAN
447         if (super_shadow)
448                 ext2fs_free_mem(&super_shadow);
449         if (group_shadow)
450                 ext2fs_free_mem(&group_shadow);
451 #endif
452         return retval;
453 }
454
455 errcode_t ext2fs_close_free(ext2_filsys *fs_ptr)
456 {
457         errcode_t ret;
458         ext2_filsys fs = *fs_ptr;
459
460         ret = ext2fs_close2(fs, 0);
461         if (ret)
462                 ext2fs_free(fs);
463         *fs_ptr = NULL;
464         return ret;
465 }
466
467 errcode_t ext2fs_close(ext2_filsys fs)
468 {
469         return ext2fs_close2(fs, 0);
470 }
471
472 errcode_t ext2fs_close2(ext2_filsys fs, int flags)
473 {
474         errcode_t       retval;
475         int             meta_blks;
476         io_stats stats = 0;
477
478         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
479
480         if (fs->write_bitmaps) {
481                 retval = fs->write_bitmaps(fs);
482                 if (retval)
483                         return retval;
484         }
485         if (fs->super->s_kbytes_written &&
486             fs->io->manager->get_stats)
487                 fs->io->manager->get_stats(fs->io, &stats);
488         if (stats && stats->bytes_written && (fs->flags & EXT2_FLAG_RW)) {
489                 fs->super->s_kbytes_written += stats->bytes_written >> 10;
490                 meta_blks = fs->desc_blocks + 1;
491                 if (!(fs->flags & EXT2_FLAG_SUPER_ONLY))
492                         fs->super->s_kbytes_written += meta_blks /
493                                 (fs->blocksize / 1024);
494                 if ((fs->flags & EXT2_FLAG_DIRTY) == 0)
495                         fs->flags |= EXT2_FLAG_SUPER_ONLY | EXT2_FLAG_DIRTY;
496         }
497         if (fs->flags & EXT2_FLAG_DIRTY) {
498                 retval = ext2fs_flush2(fs, flags);
499                 if (retval)
500                         return retval;
501         }
502
503         retval = ext2fs_mmp_stop(fs);
504         if (retval)
505                 return retval;
506
507         ext2fs_free(fs);
508         return 0;
509 }
510