blob: 6a416b6df4ce3a6e7348c01e8a7c2e60e9002442 [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
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 Public
9 * License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#include <time.h>
16#include "ext2_fs.h"
17#include "ext2fs.h"
18
19/*
20 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
21 * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
22 * calling this for the first time. In a sparse filesystem it will be the
23 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
24 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
25 */
26static unsigned int list_backups(ext2_filsys fs, unsigned int *three,
27 unsigned int *five, unsigned int *seven)
28{
29 unsigned int *min = three;
30 int mult = 3;
31 unsigned int ret;
32
33 if (!(fs->super->s_feature_ro_compat &
34 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
35 ret = *min;
36 *min += 1;
37 return ret;
38 }
39
40 if (*five < *min) {
41 min = five;
42 mult = 5;
43 }
44 if (*seven < *min) {
45 min = seven;
46 mult = 7;
47 }
48
49 ret = *min;
50 *min *= mult;
51
52 return ret;
53}
54
55/*
56 * This code assumes that the reserved blocks have already been marked in-use
57 * during ext2fs_initialize(), so that they are not allocated for other
58 * uses before we can add them to the resize inode (which has to come
59 * after the creation of the inode table).
60 */
61errcode_t ext2fs_create_resize_inode(ext2_filsys fs)
62{
63 errcode_t retval, retval2;
64 struct ext2_super_block *sb;
65 struct ext2_inode inode;
66 __u32 *dindir_buf, *gdt_buf;
67 int rsv_add;
68 unsigned long long apb, inode_size;
69 blk_t dindir_blk, rsv_off, gdt_off, gdt_blk;
70 int dindir_dirty = 0, inode_dirty = 0;
71
72 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
73
74 sb = fs->super;
75
Rob Landley20a825a2005-09-07 07:04:56 +000076 retval = ext2fs_get_mem(2 * fs->blocksize, (void *)&dindir_buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000077 if (retval)
78 goto out_free;
79 gdt_buf = (__u32 *)((char *)dindir_buf + fs->blocksize);
80
81 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
82 if (retval)
83 goto out_free;
84
85 /* Maximum possible file size (we donly use the dindirect blocks) */
86 apb = EXT2_ADDR_PER_BLOCK(sb);
87 rsv_add = fs->blocksize / 512;
88 if ((dindir_blk = inode.i_block[EXT2_DIND_BLOCK])) {
89#ifdef RES_GDT_DEBUG
90 printf("reading GDT dindir %u\n", dindir_blk);
91#endif
92 retval = ext2fs_read_ind_block(fs, dindir_blk, dindir_buf);
93 if (retval)
94 goto out_inode;
95 } else {
96 blk_t goal = 3 + sb->s_reserved_gdt_blocks +
97 fs->desc_blocks + fs->inode_blocks_per_group;
98
99 retval = ext2fs_alloc_block(fs, goal, 0, &dindir_blk);
100 if (retval)
101 goto out_free;
102 inode.i_mode = LINUX_S_IFREG | 0600;
103 inode.i_links_count = 1;
104 inode.i_block[EXT2_DIND_BLOCK] = dindir_blk;
105 inode.i_blocks = rsv_add;
106 memset(dindir_buf, 0, fs->blocksize);
107#ifdef RES_GDT_DEBUG
108 printf("allocated GDT dindir %u\n", dindir_blk);
109#endif
110 dindir_dirty = inode_dirty = 1;
111 inode_size = apb*apb + apb + EXT2_NDIR_BLOCKS;
112 inode_size *= fs->blocksize;
113 inode.i_size = inode_size & 0xFFFFFFFF;
114 inode.i_size_high = (inode_size >> 32) & 0xFFFFFFFF;
115 if(inode.i_size_high) {
116 sb->s_feature_ro_compat |=
117 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
118 }
119 inode.i_ctime = time(0);
120 }
121
122 for (rsv_off = 0, gdt_off = fs->desc_blocks,
123 gdt_blk = sb->s_first_data_block + 1 + fs->desc_blocks;
124 rsv_off < sb->s_reserved_gdt_blocks;
125 rsv_off++, gdt_off++, gdt_blk++) {
126 unsigned int three = 1, five = 5, seven = 7;
127 unsigned int grp, last = 0;
128 int gdt_dirty = 0;
129
130 gdt_off %= apb;
131 if (!dindir_buf[gdt_off]) {
132 /* FIXME XXX XXX
133 blk_t new_blk;
134
135 retval = ext2fs_new_block(fs, gdt_blk, 0, &new_blk);
136 if (retval)
137 goto out_free;
138 if (new_blk != gdt_blk) {
139 // XXX free block
140 retval = -1; // XXX
141 }
142 */
143 gdt_dirty = dindir_dirty = inode_dirty = 1;
144 memset(gdt_buf, 0, fs->blocksize);
145 dindir_buf[gdt_off] = gdt_blk;
146 inode.i_blocks += rsv_add;
147#ifdef RES_GDT_DEBUG
148 printf("added primary GDT block %u at %u[%u]\n",
149 gdt_blk, dindir_blk, gdt_off);
150#endif
151 } else if (dindir_buf[gdt_off] == gdt_blk) {
152#ifdef RES_GDT_DEBUG
153 printf("reading primary GDT block %u\n", gdt_blk);
154#endif
155 retval = ext2fs_read_ind_block(fs, gdt_blk, gdt_buf);
156 if (retval)
157 goto out_dindir;
158 } else {
159#ifdef RES_GDT_DEBUG
160 printf("bad primary GDT %u != %u at %u[%u]\n",
161 dindir_buf[gdt_off], gdt_blk,dindir_blk,gdt_off);
162#endif
163 retval = EXT2_ET_RESIZE_INODE_CORRUPT;
164 goto out_dindir;
165 }
166
167 while ((grp = list_backups(fs, &three, &five, &seven)) <
168 fs->group_desc_count) {
169 blk_t expect = gdt_blk + grp * sb->s_blocks_per_group;
170
171 if (!gdt_buf[last]) {
172#ifdef RES_GDT_DEBUG
173 printf("added backup GDT %u grp %u@%u[%u]\n",
174 expect, grp, gdt_blk, last);
175#endif
176 gdt_buf[last] = expect;
177 inode.i_blocks += rsv_add;
178 gdt_dirty = inode_dirty = 1;
179 } else if (gdt_buf[last] != expect) {
180#ifdef RES_GDT_DEBUG
181 printf("bad backup GDT %u != %u at %u[%u]\n",
182 gdt_buf[last], expect, gdt_blk, last);
183#endif
184 retval = EXT2_ET_RESIZE_INODE_CORRUPT;
185 goto out_dindir;
186 }
187 last++;
188 }
189 if (gdt_dirty) {
190#ifdef RES_GDT_DEBUG
191 printf("writing primary GDT block %u\n", gdt_blk);
192#endif
193 retval = ext2fs_write_ind_block(fs, gdt_blk, gdt_buf);
194 if (retval)
195 goto out_dindir;
196 }
197 }
198
199out_dindir:
200 if (dindir_dirty) {
201 retval2 = ext2fs_write_ind_block(fs, dindir_blk, dindir_buf);
202 if (!retval)
203 retval = retval2;
204 }
205out_inode:
206#ifdef RES_GDT_DEBUG
207 printf("inode.i_blocks = %u, i_size = %u\n", inode.i_blocks,
208 inode.i_size);
209#endif
210 if (inode_dirty) {
211 inode.i_atime = inode.i_mtime = time(0);
212 retval2 = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
213 if (!retval)
214 retval = retval2;
215 }
216out_free:
Rob Landley20a825a2005-09-07 07:04:56 +0000217 ext2fs_free_mem((void *)&dindir_buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000218 return retval;
219}
220