closefs.c revision ef344e13d2125e9dae3764b98f9fe3a170cd79e2
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 int numblocks, j, has_super, meta_bg_size, meta_bg; 57 int old_desc_blocks; 58 59 group_block = fs->super->s_first_data_block + 60 (group * fs->super->s_blocks_per_group); 61 62 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) 63 old_desc_blocks = fs->super->s_first_meta_bg; 64 else 65 old_desc_blocks = fs->desc_blocks; 66 67 if (group == fs->group_desc_count-1) { 68 numblocks = (fs->super->s_blocks_count - 69 fs->super->s_first_data_block) % 70 fs->super->s_blocks_per_group; 71 if (!numblocks) 72 numblocks = fs->super->s_blocks_per_group; 73 } else 74 numblocks = fs->super->s_blocks_per_group; 75 76 has_super = ext2fs_bg_has_super(fs, group); 77 78 if (has_super) { 79 super_blk = group_block; 80 numblocks--; 81 } 82 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc)); 83 meta_bg = group / meta_bg_size; 84 85 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) || 86 (meta_bg < fs->super->s_first_meta_bg)) { 87 if (has_super) { 88 old_desc_blk = group_block + 1; 89 numblocks -= old_desc_blocks; 90 } 91 } else { 92 if (((group % meta_bg_size) == 0) || 93 ((group % meta_bg_size) == 1) || 94 ((group % meta_bg_size) == (meta_bg_size-1))) { 95 if (has_super) 96 has_super = 1; 97 new_desc_blk = group_block + has_super; 98 numblocks--; 99 } 100 } 101 102 numblocks -= 2 + fs->inode_blocks_per_group; 103 104 if (ret_super_blk) 105 *ret_super_blk = super_blk; 106 if (ret_old_desc_blk) 107 *ret_old_desc_blk = old_desc_blk; 108 if (ret_new_desc_blk) 109 *ret_new_desc_blk = new_desc_blk; 110 if (ret_meta_bg) 111 *ret_meta_bg = meta_bg; 112 return (numblocks); 113} 114 115 116/* 117 * This function forces out the primary superblock. We need to only 118 * write out those fields which we have changed, since if the 119 * filesystem is mounted, it may have changed some of the other 120 * fields. 121 * 122 * It takes as input a superblock which has already been byte swapped 123 * (if necessary). 124 * 125 */ 126static errcode_t write_primary_superblock(ext2_filsys fs, 127 struct ext2_super_block *super) 128{ 129 __u16 *old_super, *new_super; 130 int check_idx, write_idx, size; 131 errcode_t retval; 132 133 if (!fs->io->manager->write_byte || !fs->orig_super) { 134 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); 135 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE, 136 super); 137 io_channel_set_blksize(fs->io, fs->blocksize); 138 return retval; 139 } 140 141 old_super = (__u16 *) fs->orig_super; 142 new_super = (__u16 *) super; 143 144 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) { 145 if (old_super[check_idx] == new_super[check_idx]) 146 continue; 147 write_idx = check_idx; 148 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++) 149 if (old_super[check_idx] == new_super[check_idx]) 150 break; 151 size = 2 * (check_idx - write_idx); 152#if 0 153 printf("Writing %d bytes starting at %d\n", 154 size, write_idx*2); 155#endif 156 retval = io_channel_write_byte(fs->io, 157 SUPERBLOCK_OFFSET + (2 * write_idx), size, 158 new_super + write_idx); 159 if (retval) 160 return retval; 161 } 162 memcpy(fs->orig_super, super, SUPERBLOCK_SIZE); 163 return 0; 164} 165 166 167/* 168 * Updates the revision to EXT2_DYNAMIC_REV 169 */ 170void ext2fs_update_dynamic_rev(ext2_filsys fs) 171{ 172 struct ext2_super_block *sb = fs->super; 173 174 if (sb->s_rev_level > EXT2_GOOD_OLD_REV) 175 return; 176 177 sb->s_rev_level = EXT2_DYNAMIC_REV; 178 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO; 179 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE; 180 /* s_uuid is handled by e2fsck already */ 181 /* other fields should be left alone */ 182} 183 184static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group, 185 blk_t group_block, 186 struct ext2_super_block *super_shadow) 187{ 188 dgrp_t sgrp = group; 189 190 if (sgrp > ((1 << 16) - 1)) 191 sgrp = (1 << 16) - 1; 192#ifdef EXT2FS_ENABLE_SWAPFS 193 if (fs->flags & EXT2_FLAG_SWAP_BYTES) 194 super_shadow->s_block_group_nr = ext2fs_swab16(sgrp); 195 else 196#endif 197 fs->super->s_block_group_nr = sgrp; 198 199 return io_channel_write_blk(fs->io, group_block, -SUPERBLOCK_SIZE, 200 super_shadow); 201} 202 203 204errcode_t ext2fs_flush(ext2_filsys fs) 205{ 206 dgrp_t i,j; 207 blk_t group_block; 208 errcode_t retval; 209 unsigned long fs_state; 210 struct ext2_super_block *super_shadow = 0; 211 struct ext2_group_desc *group_shadow = 0; 212 struct ext2_group_desc *s, *t; 213 char *group_ptr; 214 int old_desc_blocks; 215 216 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 217 218 fs_state = fs->super->s_state; 219 220 fs->super->s_wtime = time(NULL); 221 fs->super->s_block_group_nr = 0; 222#ifdef EXT2FS_ENABLE_SWAPFS 223 if (fs->flags & EXT2_FLAG_SWAP_BYTES) { 224 retval = EXT2_ET_NO_MEMORY; 225 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow); 226 if (retval) 227 goto errout; 228 retval = ext2fs_get_mem((size_t)(fs->blocksize * 229 fs->desc_blocks), 230 &group_shadow); 231 if (retval) 232 goto errout; 233 memset(group_shadow, 0, (size_t) fs->blocksize * 234 fs->desc_blocks); 235 236 /* swap the superblock */ 237 *super_shadow = *fs->super; 238 ext2fs_swap_super(super_shadow); 239 240 /* swap the group descriptors */ 241 for (j=0, s=fs->group_desc, t=group_shadow; 242 j < fs->group_desc_count; j++, t++, s++) { 243 *t = *s; 244 ext2fs_swap_group_desc(t); 245 } 246 } else { 247 super_shadow = fs->super; 248 group_shadow = fs->group_desc; 249 } 250#else 251 super_shadow = fs->super; 252 group_shadow = fs->group_desc; 253#endif 254 255 /* 256 * Write out master superblock. This has to be done 257 * separately, since it is located at a fixed location 258 * (SUPERBLOCK_OFFSET). 259 */ 260 retval = write_primary_superblock(fs, super_shadow); 261 if (retval) 262 goto errout; 263 264 /* 265 * If this is an external journal device, don't write out the 266 * block group descriptors or any of the backup superblocks 267 */ 268 if (fs->super->s_feature_incompat & 269 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 270 retval = 0; 271 goto errout; 272 } 273 274 /* 275 * Set the state of the FS to be non-valid. (The state has 276 * already been backed up earlier, and will be restored when 277 * we exit.) 278 */ 279 fs->super->s_state &= ~EXT2_VALID_FS; 280#ifdef EXT2FS_ENABLE_SWAPFS 281 if (fs->flags & EXT2_FLAG_SWAP_BYTES) { 282 *super_shadow = *fs->super; 283 ext2fs_swap_super(super_shadow); 284 } 285#endif 286 287 /* 288 * Write out the master group descriptors, and the backup 289 * superblocks and group descriptors. 290 */ 291 group_block = fs->super->s_first_data_block; 292 group_ptr = (char *) group_shadow; 293 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) 294 old_desc_blocks = fs->super->s_first_meta_bg; 295 else 296 old_desc_blocks = fs->desc_blocks; 297 298 for (i = 0; i < fs->group_desc_count; i++) { 299 blk_t super_blk, old_desc_blk, new_desc_blk; 300 int meta_bg; 301 302 ext2fs_super_and_bgd_loc(fs, i, &super_blk, &old_desc_blk, 303 &new_desc_blk, &meta_bg); 304 305 if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) { 306 retval = write_backup_super(fs, i, super_blk, 307 super_shadow); 308 if (retval) 309 goto errout; 310 } 311 if (fs->flags & EXT2_FLAG_SUPER_ONLY) 312 continue; 313 if ((old_desc_blk) && 314 (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) { 315 retval = io_channel_write_blk(fs->io, 316 old_desc_blk, old_desc_blocks, group_ptr); 317 if (retval) 318 goto errout; 319 } 320 if (new_desc_blk) { 321 retval = io_channel_write_blk(fs->io, new_desc_blk, 322 1, group_ptr + (meta_bg*fs->blocksize)); 323 if (retval) 324 goto errout; 325 } 326 } 327 fs->super->s_block_group_nr = 0; 328 329 /* 330 * If the write_bitmaps() function is present, call it to 331 * flush the bitmaps. This is done this way so that a simple 332 * program that doesn't mess with the bitmaps doesn't need to 333 * drag in the bitmaps.c code. 334 */ 335 if (fs->write_bitmaps) { 336 retval = fs->write_bitmaps(fs); 337 if (retval) 338 goto errout; 339 } 340 341 fs->flags &= ~EXT2_FLAG_DIRTY; 342 343 /* 344 * Flush the blocks out to disk 345 */ 346 retval = io_channel_flush(fs->io); 347errout: 348 fs->super->s_state = fs_state; 349 if (fs->flags & EXT2_FLAG_SWAP_BYTES) { 350 if (super_shadow) 351 ext2fs_free_mem(&super_shadow); 352 if (group_shadow) 353 ext2fs_free_mem(&group_shadow); 354 } 355 return retval; 356} 357 358errcode_t ext2fs_close(ext2_filsys fs) 359{ 360 errcode_t retval; 361 362 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 363 364 if (fs->flags & EXT2_FLAG_DIRTY) { 365 retval = ext2fs_flush(fs); 366 if (retval) 367 return retval; 368 } 369 if (fs->write_bitmaps) { 370 retval = fs->write_bitmaps(fs); 371 if (retval) 372 return retval; 373 } 374 ext2fs_free(fs); 375 return 0; 376} 377 378