Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * closefs.c --- close an ext2 filesystem |
| 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 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% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 13 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 15 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 16 | #include <time.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 17 | #include <string.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 19 | #if EXT2_FLAT_INCLUDES |
| 20 | #include "ext2_fs.h" |
| 21 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 22 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 23 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 24 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 25 | #include "ext2fsP.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 26 | |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 27 | static int test_root(int a, int b) |
| 28 | { |
| 29 | if (a == 0) |
| 30 | return 1; |
| 31 | while (1) { |
| 32 | if (a == 1) |
| 33 | return 1; |
| 34 | if (a % b) |
| 35 | return 0; |
| 36 | a = a / b; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | int ext2fs_bg_has_super(ext2_filsys fs, int group_block) |
| 41 | { |
| 42 | #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
| 43 | struct ext2fs_sb *s; |
| 44 | |
| 45 | s = (struct ext2fs_sb *) fs->super; |
| 46 | if (!(s->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) |
| 47 | return 1; |
| 48 | |
| 49 | if (test_root(group_block, 3) || (test_root(group_block, 5)) || |
| 50 | test_root(group_block, 7)) |
| 51 | return 1; |
| 52 | |
| 53 | return 0; |
| 54 | #else |
| 55 | return 1; |
| 56 | #endif |
| 57 | } |
| 58 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | errcode_t ext2fs_flush(ext2_filsys fs) |
| 60 | { |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 61 | dgrp_t i,j,maxgroup,sgrp; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 62 | blk_t group_block; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 63 | errcode_t retval; |
| 64 | char *group_ptr; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 65 | unsigned long fs_state; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 66 | struct ext2_super_block *super_shadow = 0; |
| 67 | struct ext2_group_desc *group_shadow = 0; |
| 68 | struct ext2_group_desc *s, *t; |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 69 | struct ext2fs_sb *sb, *sb_shadow; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 70 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 71 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 72 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 73 | fs_state = fs->super->s_state; |
| 74 | |
| 75 | fs->super->s_wtime = time(NULL); |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 76 | sb = (struct ext2fs_sb *) fs->super; |
| 77 | sb->s_block_group_nr = 0; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 78 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) { |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 79 | retval = EXT2_ET_NO_MEMORY; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 80 | retval = ext2fs_get_mem(SUPERBLOCK_SIZE, |
| 81 | (void **) &super_shadow); |
| 82 | if (retval) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 83 | goto errout; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 84 | retval = ext2fs_get_mem((size_t)(fs->blocksize * |
| 85 | fs->desc_blocks), |
| 86 | (void **) &group_shadow); |
| 87 | if (retval) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 88 | goto errout; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 89 | memset(group_shadow, 0, (size_t) fs->blocksize * |
| 90 | fs->desc_blocks); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 91 | |
| 92 | /* swap the superblock */ |
| 93 | *super_shadow = *fs->super; |
| 94 | ext2fs_swap_super(super_shadow); |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 95 | sb_shadow = (struct ext2fs_sb *) super_shadow; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 96 | |
| 97 | /* swap the group descriptors */ |
| 98 | for (j=0, s=fs->group_desc, t=group_shadow; |
| 99 | j < fs->group_desc_count; j++, t++, s++) { |
| 100 | *t = *s; |
| 101 | ext2fs_swap_group_desc(t); |
| 102 | } |
| 103 | } else { |
| 104 | super_shadow = fs->super; |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 105 | sb_shadow = (struct ext2fs_sb *) fs->super; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 106 | group_shadow = fs->group_desc; |
| 107 | } |
| 108 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | /* |
| 110 | * Write out master superblock. This has to be done |
| 111 | * separately, since it is located at a fixed location |
| 112 | * (SUPERBLOCK_OFFSET). |
| 113 | */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 114 | io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 115 | retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE, |
| 116 | super_shadow); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | if (retval) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 118 | goto errout; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 119 | io_channel_set_blksize(fs->io, fs->blocksize); |
| 120 | |
| 121 | /* |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 122 | * Set the state of the FS to be non-valid. (The state has |
| 123 | * already been backed up earlier, and will be restored when |
| 124 | * we exit.) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 125 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 126 | fs->super->s_state &= ~EXT2_VALID_FS; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 127 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 128 | *super_shadow = *fs->super; |
| 129 | ext2fs_swap_super(super_shadow); |
| 130 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 133 | * Write out the master group descriptors, and the backup |
| 134 | * superblocks and group descriptors. |
| 135 | */ |
| 136 | group_block = fs->super->s_first_data_block; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 137 | maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 : |
| 138 | fs->group_desc_count; |
| 139 | for (i = 0; i < maxgroup; i++) { |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 140 | if (!ext2fs_bg_has_super(fs, i)) |
| 141 | goto next_group; |
| 142 | |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 143 | sgrp = i; |
| 144 | if (sgrp > ((1 << 16) - 1)) |
| 145 | sgrp = (1 << 16) - 1; |
| 146 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) |
| 147 | sb_shadow->s_block_group_nr = ext2fs_swab16(sgrp); |
| 148 | else |
| 149 | sb->s_block_group_nr = sgrp; |
| 150 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 151 | if (i !=0 ) { |
| 152 | retval = io_channel_write_blk(fs->io, group_block, |
| 153 | -SUPERBLOCK_SIZE, |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 154 | super_shadow); |
| 155 | if (retval) |
| 156 | goto errout; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 157 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 158 | group_ptr = (char *) group_shadow; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 159 | for (j=0; j < fs->desc_blocks; j++) { |
| 160 | retval = io_channel_write_blk(fs->io, |
| 161 | group_block+1+j, 1, |
| 162 | group_ptr); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 163 | if (retval) |
| 164 | goto errout; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 165 | group_ptr += fs->blocksize; |
| 166 | } |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 167 | next_group: |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 168 | group_block += EXT2_BLOCKS_PER_GROUP(fs->super); |
| 169 | } |
Theodore Ts'o | 30c4261 | 1998-06-30 05:33:11 +0000 | [diff] [blame] | 170 | sb->s_block_group_nr = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * If the write_bitmaps() function is present, call it to |
| 174 | * flush the bitmaps. This is done this way so that a simple |
| 175 | * program that doesn't mess with the bitmaps doesn't need to |
| 176 | * drag in the bitmaps.c code. |
| 177 | */ |
| 178 | if (fs->write_bitmaps) { |
| 179 | retval = fs->write_bitmaps(fs); |
| 180 | if (retval) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 181 | goto errout; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 182 | } |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Flush the blocks out to disk |
| 186 | */ |
| 187 | retval = io_channel_flush(fs->io); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 188 | errout: |
| 189 | fs->super->s_state = fs_state; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 190 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 191 | if (super_shadow) |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 192 | ext2fs_free_mem((void **) &super_shadow); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 193 | if (group_shadow) |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 194 | ext2fs_free_mem((void **) &group_shadow); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 195 | } |
| 196 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | errcode_t ext2fs_close(ext2_filsys fs) |
| 200 | { |
| 201 | errcode_t retval; |
| 202 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 203 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 204 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 205 | if (fs->flags & EXT2_FLAG_DIRTY) { |
| 206 | retval = ext2fs_flush(fs); |
| 207 | if (retval) |
| 208 | return retval; |
| 209 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 210 | if (fs->write_bitmaps) { |
| 211 | retval = fs->write_bitmaps(fs); |
| 212 | if (retval) |
| 213 | return retval; |
| 214 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 215 | ext2fs_free(fs); |
| 216 | return 0; |
| 217 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 218 | |