Whamcloud - gitweb
Many files:
[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 #if EXT2_FLAT_INCLUDES
20 #include "ext2_fs.h"
21 #else
22 #include <linux/ext2_fs.h>
23 #endif
24
25 #include "ext2fsP.h"
26
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
59 errcode_t ext2fs_flush(ext2_filsys fs)
60 {
61         dgrp_t          i,j,maxgroup;
62         blk_t           group_block;
63         errcode_t       retval;
64         char            *group_ptr;
65         unsigned long   fs_state;
66         struct ext2_super_block *super_shadow = 0;
67         struct ext2_group_desc *group_shadow = 0;
68         struct ext2_group_desc *s, *t;
69         
70         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
71
72         fs_state = fs->super->s_state;
73
74         fs->super->s_wtime = time(NULL);
75         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
76                 retval = EXT2_ET_NO_MEMORY;
77                 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
78                                         (void **) &super_shadow);
79                 if (retval)
80                         goto errout;
81                 retval = ext2fs_get_mem((size_t)(fs->blocksize *
82                                                  fs->desc_blocks),
83                                         (void **) &group_shadow);
84                 if (retval)
85                         goto errout;
86                 memset(group_shadow, 0, (size_t) fs->blocksize *
87                        fs->desc_blocks);
88
89                 /* swap the superblock */
90                 *super_shadow = *fs->super;
91                 ext2fs_swap_super(super_shadow);
92
93                 /* swap the group descriptors */
94                 for (j=0, s=fs->group_desc, t=group_shadow;
95                      j < fs->group_desc_count; j++, t++, s++) {
96                         *t = *s;
97                         ext2fs_swap_group_desc(t);
98                 }
99         } else {
100                 super_shadow = fs->super;
101                 group_shadow = fs->group_desc;
102         }
103         
104         /*
105          * Write out master superblock.  This has to be done
106          * separately, since it is located at a fixed location
107          * (SUPERBLOCK_OFFSET).
108          */
109         io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
110         retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
111                                       super_shadow);
112         if (retval)
113                 goto errout;
114         io_channel_set_blksize(fs->io, fs->blocksize);
115
116         /*
117          * Set the state of the FS to be non-valid.  (The state has
118          * already been backed up earlier, and will be restored when
119          * we exit.)
120          */
121         fs->super->s_state &= ~EXT2_VALID_FS;
122         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
123                 *super_shadow = *fs->super;
124                 ext2fs_swap_super(super_shadow);
125         }
126
127         /*
128          * Write out the master group descriptors, and the backup
129          * superblocks and group descriptors.
130          */
131         group_block = fs->super->s_first_data_block;
132         maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
133                 fs->group_desc_count;
134         for (i = 0; i < maxgroup; i++) {
135                 if (!ext2fs_bg_has_super(fs, i))
136                         goto next_group;
137
138                 if (i !=0 ) {
139                         retval = io_channel_write_blk(fs->io, group_block,
140                                                       -SUPERBLOCK_SIZE,
141                                                       super_shadow);
142                         if (retval)
143                                 goto errout;
144                 }
145                 group_ptr = (char *) group_shadow;
146                 for (j=0; j < fs->desc_blocks; j++) {
147                         retval = io_channel_write_blk(fs->io,
148                                                       group_block+1+j, 1,
149                                                       group_ptr);
150                         if (retval)
151                                 goto errout;
152                         group_ptr += fs->blocksize;
153                 }
154         next_group:
155                 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
156         }
157
158         /*
159          * If the write_bitmaps() function is present, call it to
160          * flush the bitmaps.  This is done this way so that a simple
161          * program that doesn't mess with the bitmaps doesn't need to
162          * drag in the bitmaps.c code.
163          */
164         if (fs->write_bitmaps) {
165                 retval = fs->write_bitmaps(fs);
166                 if (retval)
167                         goto errout;
168         }
169
170         /*
171          * Flush the blocks out to disk
172          */
173         retval = io_channel_flush(fs->io);
174 errout:
175         fs->super->s_state = fs_state;
176         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
177                 if (super_shadow)
178                         ext2fs_free_mem((void **) &super_shadow);
179                 if (group_shadow)
180                         ext2fs_free_mem((void **) &group_shadow);
181         }
182         return retval;
183 }
184
185 errcode_t ext2fs_close(ext2_filsys fs)
186 {
187         errcode_t       retval;
188         
189         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
190
191         if (fs->flags & EXT2_FLAG_DIRTY) {
192                 retval = ext2fs_flush(fs);
193                 if (retval)
194                         return retval;
195         }
196         if (fs->write_bitmaps) {
197                 retval = fs->write_bitmaps(fs);
198                 if (retval)
199                         return retval;
200         }
201         ext2fs_free(fs);
202         return 0;
203 }
204