]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/ext2fs/closefs.c
Many files:
[thirdparty/e2fsprogs.git] / lib / ext2fs / closefs.c
CommitLineData
3839e657
TT
1/*
2 * closefs.c --- close an ext2 filesystem
3 *
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8#include <stdio.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <time.h>
12
3839e657
TT
13#include <linux/ext2_fs.h>
14
15#include "ext2fs.h"
16
17errcode_t ext2fs_flush(ext2_filsys fs)
18{
19 int i,j;
20 int group_block;
21 errcode_t retval;
22 char *group_ptr;
f3db3566 23 unsigned long fs_state;
3839e657 24
f3db3566
TT
25 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
26
3839e657
TT
27 /*
28 * Write out master superblock. This has to be done
29 * separately, since it is located at a fixed location
30 * (SUPERBLOCK_OFFSET).
31 */
32 fs->super->s_wtime = time(NULL);
33 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
34 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE, fs->super);
35 if (retval)
36 return retval;
37 io_channel_set_blksize(fs->io, fs->blocksize);
38
f3db3566
TT
39 /*
40 * Save the state of the FS and set it to non valid for the
41 * backup superblocks
42 */
43 fs_state = fs->super->s_state;
44 fs->super->s_state &= ~EXT2_VALID_FS;
45
3839e657
TT
46 /*
47 * Write out the master group descriptors, and the backup
48 * superblocks and group descriptors.
49 */
50 group_block = fs->super->s_first_data_block;
51 for (i = 0; i < fs->group_desc_count; i++) {
52 if (i !=0 ) {
53 retval = io_channel_write_blk(fs->io, group_block,
54 -SUPERBLOCK_SIZE,
55 fs->super);
f3db3566
TT
56 if (retval) {
57 fs->super->s_state = fs_state;
3839e657 58 return retval;
f3db3566 59 }
3839e657
TT
60 }
61 group_ptr = (char *) fs->group_desc;
62 for (j=0; j < fs->desc_blocks; j++) {
63 retval = io_channel_write_blk(fs->io,
64 group_block+1+j, 1,
65 group_ptr);
f3db3566
TT
66 if (retval) {
67 fs->super->s_state = fs_state;
3839e657 68 return retval;
f3db3566 69 }
3839e657
TT
70 group_ptr += fs->blocksize;
71 }
72 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
73 }
74
f3db3566
TT
75 fs->super->s_state = fs_state;
76
3839e657
TT
77 /*
78 * If the write_bitmaps() function is present, call it to
79 * flush the bitmaps. This is done this way so that a simple
80 * program that doesn't mess with the bitmaps doesn't need to
81 * drag in the bitmaps.c code.
82 */
83 if (fs->write_bitmaps) {
84 retval = fs->write_bitmaps(fs);
85 if (retval)
86 return retval;
87 }
88
89 return 0;
90}
91
92errcode_t ext2fs_close(ext2_filsys fs)
93{
94 errcode_t retval;
95
f3db3566
TT
96 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
97
3839e657
TT
98 if (fs->flags & EXT2_FLAG_DIRTY) {
99 retval = ext2fs_flush(fs);
100 if (retval)
101 return retval;
102 }
103 ext2fs_free(fs);
104 return 0;
105}