Whamcloud - gitweb
libext2fs: fix Direct I/O fallback code so it implements RMW correctly
[tools/e2fsprogs.git] / lib / ext2fs / res_gdt.c
1 /*
2  * res_gdt.c --- reserve blocks for growing the group descriptor table
3  *               during online resizing.
4  *
5  * Copyright (C) 2002 Andreas Dilger
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Library
9  * General Public License, version 2.
10  * %End-Header%
11  */
12
13 #include "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <time.h>
17 #include "ext2_fs.h"
18 #include "ext2fs.h"
19
20 /*
21  * Iterate through the groups which hold BACKUP superblock/GDT copies in an
22  * ext3 filesystem.  The counters should be initialized to 1, 5, and 7 before
23  * calling this for the first time.  In a sparse filesystem it will be the
24  * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
25  * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
26  */
27 static unsigned int list_backups(ext2_filsys fs, unsigned int *three,
28                                  unsigned int *five, unsigned int *seven)
29 {
30         unsigned int *min = three;
31         int mult = 3;
32         unsigned int ret;
33
34         if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
35                 if (*min == 1) {
36                         *min += 1;
37                         if (fs->super->s_backup_bgs[0])
38                                 return fs->super->s_backup_bgs[0];
39                 }
40                 if (*min == 2) {
41                         *min += 1;
42                         if (fs->super->s_backup_bgs[1])
43                                 return fs->super->s_backup_bgs[1];
44                 }
45                 return fs->group_desc_count;
46         }
47         if (!(fs->super->s_feature_ro_compat &
48               EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
49                 ret = *min;
50                 *min += 1;
51                 return ret;
52         }
53
54         if (*five < *min) {
55                 min = five;
56                 mult = 5;
57         }
58         if (*seven < *min) {
59                 min = seven;
60                 mult = 7;
61         }
62
63         ret = *min;
64         *min *= mult;
65
66         return ret;
67 }
68
69 /*
70  * This code assumes that the reserved blocks have already been marked in-use
71  * during ext2fs_initialize(), so that they are not allocated for other
72  * uses before we can add them to the resize inode (which has to come
73  * after the creation of the inode table).
74  */
75 errcode_t ext2fs_create_resize_inode(ext2_filsys fs)
76 {
77         errcode_t               retval, retval2;
78         struct ext2_super_block *sb;
79         struct ext2_inode       inode;
80         __u32                   *dindir_buf, *gdt_buf;
81         unsigned long long      apb, inode_size;
82         /* FIXME-64 - can't deal with extents */
83         blk_t                   dindir_blk, rsv_off, gdt_off, gdt_blk;
84         int                     dindir_dirty = 0, inode_dirty = 0, sb_blk = 0;
85
86         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
87
88         sb = fs->super;
89
90         retval = ext2fs_get_array(2, fs->blocksize, &dindir_buf);
91         if (retval)
92                 return retval;
93         gdt_buf = (__u32 *)((char *)dindir_buf + fs->blocksize);
94
95         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
96         if (retval)
97                 goto out_free;
98
99         /*
100          * File systems with a blocksize of 1024 and bigalloc have
101          * sb->s_first_data_block of 0; yet the superblock is still at
102          * block #1.  We compensate for it here.
103          */
104         sb_blk = sb->s_first_data_block;
105         if (fs->blocksize == 1024 && sb_blk == 0)
106                 sb_blk = 1;
107
108         /* Maximum possible file size (we donly use the dindirect blocks) */
109         apb = EXT2_ADDR_PER_BLOCK(sb);
110         if ((dindir_blk = inode.i_block[EXT2_DIND_BLOCK])) {
111 #ifdef RES_GDT_DEBUG
112                 printf("reading GDT dindir %u\n", dindir_blk);
113 #endif
114                 retval = ext2fs_read_ind_block(fs, dindir_blk, dindir_buf);
115                 if (retval)
116                         goto out_inode;
117         } else {
118                 blk_t goal = sb_blk + fs->desc_blocks +
119                         sb->s_reserved_gdt_blocks + 2 +
120                         fs->inode_blocks_per_group;
121
122                 retval = ext2fs_alloc_block(fs, goal, 0, &dindir_blk);
123                 if (retval)
124                         goto out_free;
125                 inode.i_mode = LINUX_S_IFREG | 0600;
126                 inode.i_links_count = 1;
127                 inode.i_block[EXT2_DIND_BLOCK] = dindir_blk;
128                 ext2fs_iblk_set(fs, &inode, 1);
129                 memset(dindir_buf, 0, fs->blocksize);
130 #ifdef RES_GDT_DEBUG
131                 printf("allocated GDT dindir %u\n", dindir_blk);
132 #endif
133                 dindir_dirty = inode_dirty = 1;
134                 inode_size = apb*apb + apb + EXT2_NDIR_BLOCKS;
135                 inode_size *= fs->blocksize;
136                 retval = ext2fs_inode_size_set(fs, &inode, inode_size);
137                 if (retval)
138                         goto out_free;
139                 inode.i_ctime = fs->now ? fs->now : time(0);
140         }
141
142         for (rsv_off = 0, gdt_off = fs->desc_blocks,
143              gdt_blk = sb_blk + 1 + fs->desc_blocks;
144              rsv_off < sb->s_reserved_gdt_blocks;
145              rsv_off++, gdt_off++, gdt_blk++) {
146                 unsigned int three = 1, five = 5, seven = 7;
147                 unsigned int grp, last = 0;
148                 int gdt_dirty = 0;
149
150                 gdt_off %= apb;
151                 if (!dindir_buf[gdt_off]) {
152                         /* FIXME XXX XXX
153                         blk_t new_blk;
154
155                         retval = ext2fs_new_block(fs, gdt_blk, 0, &new_blk);
156                         if (retval)
157                                 goto out_free;
158                         if (new_blk != gdt_blk) {
159                                 // XXX free block
160                                 retval = -1; // XXX
161                         }
162                         */
163                         gdt_dirty = dindir_dirty = inode_dirty = 1;
164                         memset(gdt_buf, 0, fs->blocksize);
165                         dindir_buf[gdt_off] = gdt_blk;
166                         ext2fs_iblk_add_blocks(fs, &inode, 1);
167 #ifdef RES_GDT_DEBUG
168                         printf("added primary GDT block %u at %u[%u]\n",
169                                gdt_blk, dindir_blk, gdt_off);
170 #endif
171                 } else if (dindir_buf[gdt_off] == gdt_blk) {
172 #ifdef RES_GDT_DEBUG
173                         printf("reading primary GDT block %u\n", gdt_blk);
174 #endif
175                         retval = ext2fs_read_ind_block(fs, gdt_blk, gdt_buf);
176                         if (retval)
177                                 goto out_dindir;
178                 } else {
179 #ifdef RES_GDT_DEBUG
180                         printf("bad primary GDT %u != %u at %u[%u]\n",
181                                dindir_buf[gdt_off], gdt_blk,dindir_blk,gdt_off);
182 #endif
183                         retval = EXT2_ET_RESIZE_INODE_CORRUPT;
184                         goto out_dindir;
185                 }
186
187                 while ((grp = list_backups(fs, &three, &five, &seven)) <
188                        fs->group_desc_count) {
189                         blk_t expect = gdt_blk + grp * sb->s_blocks_per_group;
190
191                         if (!gdt_buf[last]) {
192 #ifdef RES_GDT_DEBUG
193                                 printf("added backup GDT %u grp %u@%u[%u]\n",
194                                        expect, grp, gdt_blk, last);
195 #endif
196                                 gdt_buf[last] = expect;
197                                 ext2fs_iblk_add_blocks(fs, &inode, 1);
198                                 gdt_dirty = inode_dirty = 1;
199                         } else if (gdt_buf[last] != expect) {
200 #ifdef RES_GDT_DEBUG
201                                 printf("bad backup GDT %u != %u at %u[%u]\n",
202                                        gdt_buf[last], expect, gdt_blk, last);
203 #endif
204                                 retval = EXT2_ET_RESIZE_INODE_CORRUPT;
205                                 goto out_dindir;
206                         }
207                         last++;
208                 }
209                 if (gdt_dirty) {
210 #ifdef RES_GDT_DEBUG
211                         printf("writing primary GDT block %u\n", gdt_blk);
212 #endif
213                         retval = ext2fs_write_ind_block(fs, gdt_blk, gdt_buf);
214                         if (retval)
215                                 goto out_dindir;
216                 }
217         }
218
219 out_dindir:
220         if (dindir_dirty) {
221                 retval2 = ext2fs_write_ind_block(fs, dindir_blk, dindir_buf);
222                 if (!retval)
223                         retval = retval2;
224         }
225 out_inode:
226 #ifdef RES_GDT_DEBUG
227         printf("inode.i_blocks = %u, i_size = %u\n", inode.i_blocks,
228                inode.i_size);
229 #endif
230         if (inode_dirty) {
231                 inode.i_atime = inode.i_mtime = fs->now ? fs->now : time(0);
232                 retval2 = ext2fs_write_new_inode(fs, EXT2_RESIZE_INO, &inode);
233                 if (!retval)
234                         retval = retval2;
235         }
236 out_free:
237         ext2fs_free_mem(&dindir_buf);
238         return retval;
239 }
240