Whamcloud - gitweb
ChangeLog, openfs.c:
[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,sgrp;
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         struct ext2fs_sb *sb, *sb_shadow;
70         
71         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
72
73         fs_state = fs->super->s_state;
74
75         fs->super->s_wtime = time(NULL);
76         sb = (struct ext2fs_sb *) fs->super;
77         sb->s_block_group_nr = 0;
78         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
79                 retval = EXT2_ET_NO_MEMORY;
80                 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
81                                         (void **) &super_shadow);
82                 if (retval)
83                         goto errout;
84                 retval = ext2fs_get_mem((size_t)(fs->blocksize *
85                                                  fs->desc_blocks),
86                                         (void **) &group_shadow);
87                 if (retval)
88                         goto errout;
89                 memset(group_shadow, 0, (size_t) fs->blocksize *
90                        fs->desc_blocks);
91
92                 /* swap the superblock */
93                 *super_shadow = *fs->super;
94                 ext2fs_swap_super(super_shadow);
95                 sb_shadow = (struct ext2fs_sb *) super_shadow;
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;
105                 sb_shadow = (struct ext2fs_sb *) fs->super;
106                 group_shadow = fs->group_desc;
107         }
108         
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          */
114         io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
115         retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
116                                       super_shadow);
117         if (retval)
118                 goto errout;
119         io_channel_set_blksize(fs->io, fs->blocksize);
120
121         /*
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.)
125          */
126         fs->super->s_state &= ~EXT2_VALID_FS;
127         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
128                 *super_shadow = *fs->super;
129                 ext2fs_swap_super(super_shadow);
130         }
131
132         /*
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;
137         maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
138                 fs->group_desc_count;
139         for (i = 0; i < maxgroup; i++) {
140                 if (!ext2fs_bg_has_super(fs, i))
141                         goto next_group;
142
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
151                 if (i !=0 ) {
152                         retval = io_channel_write_blk(fs->io, group_block,
153                                                       -SUPERBLOCK_SIZE,
154                                                       super_shadow);
155                         if (retval)
156                                 goto errout;
157                 }
158                 group_ptr = (char *) group_shadow;
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);
163                         if (retval)
164                                 goto errout;
165                         group_ptr += fs->blocksize;
166                 }
167         next_group:
168                 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
169         }
170         sb->s_block_group_nr = 0;
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)
181                         goto errout;
182         }
183
184         /*
185          * Flush the blocks out to disk
186          */
187         retval = io_channel_flush(fs->io);
188 errout:
189         fs->super->s_state = fs_state;
190         if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
191                 if (super_shadow)
192                         ext2fs_free_mem((void **) &super_shadow);
193                 if (group_shadow)
194                         ext2fs_free_mem((void **) &group_shadow);
195         }
196         return retval;
197 }
198
199 errcode_t ext2fs_close(ext2_filsys fs)
200 {
201         errcode_t       retval;
202         
203         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
204
205         if (fs->flags & EXT2_FLAG_DIRTY) {
206                 retval = ext2fs_flush(fs);
207                 if (retval)
208                         return retval;
209         }
210         if (fs->write_bitmaps) {
211                 retval = fs->write_bitmaps(fs);
212                 if (retval)
213                         return retval;
214         }
215         ext2fs_free(fs);
216         return 0;
217 }
218