• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
list_backups(ext2_filsys fs,unsigned int * three,unsigned int * five,unsigned int * seven)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 (ext2fs_has_feature_sparse_super2(fs->super)) {
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 (!ext2fs_has_feature_sparse_super(fs->super)) {
48 		ret = *min;
49 		*min += 1;
50 		return ret;
51 	}
52 
53 	if (*five < *min) {
54 		min = five;
55 		mult = 5;
56 	}
57 	if (*seven < *min) {
58 		min = seven;
59 		mult = 7;
60 	}
61 
62 	ret = *min;
63 	*min *= mult;
64 
65 	return ret;
66 }
67 
68 /*
69  * This code assumes that the reserved blocks have already been marked in-use
70  * during ext2fs_initialize(), so that they are not allocated for other
71  * uses before we can add them to the resize inode (which has to come
72  * after the creation of the inode table).
73  */
ext2fs_create_resize_inode(ext2_filsys fs)74 errcode_t ext2fs_create_resize_inode(ext2_filsys fs)
75 {
76 	errcode_t		retval, retval2;
77 	struct ext2_super_block	*sb;
78 	struct ext2_inode	inode;
79 	__u32			*dindir_buf, *gdt_buf;
80 	unsigned long long	apb, inode_size;
81 	/* FIXME-64 - can't deal with extents */
82 	blk_t			dindir_blk, rsv_off, gdt_off, gdt_blk;
83 	int			dindir_dirty = 0, inode_dirty = 0, sb_blk = 0;
84 
85 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
86 
87 	sb = fs->super;
88 
89 	retval = ext2fs_get_array(2, fs->blocksize, &dindir_buf);
90 	if (retval)
91 		return retval;
92 	gdt_buf = (__u32 *)((char *)dindir_buf + fs->blocksize);
93 
94 	retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
95 	if (retval)
96 		goto out_free;
97 
98 	/*
99 	 * File systems with a blocksize of 1024 and bigalloc have
100 	 * sb->s_first_data_block of 0; yet the superblock is still at
101 	 * block #1.  We compensate for it here.
102 	 */
103 	sb_blk = sb->s_first_data_block;
104 	if (fs->blocksize == 1024 && sb_blk == 0)
105 		sb_blk = 1;
106 
107 	/* Maximum possible file size (we only use double indirect blocks) */
108 	apb = EXT2_ADDR_PER_BLOCK(sb);
109 	if ((dindir_blk = inode.i_block[EXT2_DIND_BLOCK])) {
110 #ifdef RES_GDT_DEBUG
111 		printf("reading GDT dindir %u\n", dindir_blk);
112 #endif
113 		retval = ext2fs_read_ind_block(fs, dindir_blk, dindir_buf);
114 		if (retval)
115 			goto out_inode;
116 	} else {
117 		blk_t goal = sb_blk + fs->desc_blocks +
118 			sb->s_reserved_gdt_blocks + 2 +
119 			fs->inode_blocks_per_group;
120 
121 		retval = ext2fs_alloc_block(fs, goal, 0, &dindir_blk);
122 		if (retval)
123 			goto out_free;
124 		inode.i_mode = LINUX_S_IFREG | 0600;
125 		inode.i_links_count = 1;
126 		inode.i_block[EXT2_DIND_BLOCK] = dindir_blk;
127 		ext2fs_iblk_set(fs, &inode, 1);
128 		memset(dindir_buf, 0, fs->blocksize);
129 #ifdef RES_GDT_DEBUG
130 		printf("allocated GDT dindir %u\n", dindir_blk);
131 #endif
132 		dindir_dirty = inode_dirty = 1;
133 		inode_size = apb*apb + apb + EXT2_NDIR_BLOCKS;
134 		inode_size *= fs->blocksize;
135 		retval = ext2fs_inode_size_set(fs, &inode, inode_size);
136 		if (retval)
137 			goto out_free;
138 		inode.i_ctime = fs->now ? fs->now : time(0);
139 	}
140 
141 	for (rsv_off = 0, gdt_off = fs->desc_blocks,
142 	     gdt_blk = sb_blk + 1 + fs->desc_blocks;
143 	     rsv_off < sb->s_reserved_gdt_blocks;
144 	     rsv_off++, gdt_off++, gdt_blk++) {
145 		unsigned int three = 1, five = 5, seven = 7;
146 		unsigned int grp, last = 0;
147 		int gdt_dirty = 0;
148 
149 		gdt_off %= apb;
150 		if (!dindir_buf[gdt_off]) {
151 			/* FIXME XXX XXX
152 			blk_t new_blk;
153 
154 			retval = ext2fs_new_block(fs, gdt_blk, 0, &new_blk);
155 			if (retval)
156 				goto out_free;
157 			if (new_blk != gdt_blk) {
158 				// XXX free block
159 				retval = -1; // XXX
160 			}
161 			*/
162 			gdt_dirty = dindir_dirty = inode_dirty = 1;
163 			memset(gdt_buf, 0, fs->blocksize);
164 			dindir_buf[gdt_off] = gdt_blk;
165 			ext2fs_iblk_add_blocks(fs, &inode, 1);
166 #ifdef RES_GDT_DEBUG
167 			printf("added primary GDT block %u at %u[%u]\n",
168 			       gdt_blk, dindir_blk, gdt_off);
169 #endif
170 		} else if (dindir_buf[gdt_off] == gdt_blk) {
171 #ifdef RES_GDT_DEBUG
172 			printf("reading primary GDT block %u\n", gdt_blk);
173 #endif
174 			retval = ext2fs_read_ind_block(fs, gdt_blk, gdt_buf);
175 			if (retval)
176 				goto out_dindir;
177 		} else {
178 #ifdef RES_GDT_DEBUG
179 			printf("bad primary GDT %u != %u at %u[%u]\n",
180 			       dindir_buf[gdt_off], gdt_blk,dindir_blk,gdt_off);
181 #endif
182 			retval = EXT2_ET_RESIZE_INODE_CORRUPT;
183 			goto out_dindir;
184 		}
185 
186 		while ((grp = list_backups(fs, &three, &five, &seven)) <
187 		       fs->group_desc_count) {
188 			blk_t expect = gdt_blk + grp * sb->s_blocks_per_group;
189 
190 			if (!gdt_buf[last]) {
191 #ifdef RES_GDT_DEBUG
192 				printf("added backup GDT %u grp %u@%u[%u]\n",
193 				       expect, grp, gdt_blk, last);
194 #endif
195 				gdt_buf[last] = expect;
196 				ext2fs_iblk_add_blocks(fs, &inode, 1);
197 				gdt_dirty = inode_dirty = 1;
198 			} else if (gdt_buf[last] != expect) {
199 #ifdef RES_GDT_DEBUG
200 				printf("bad backup GDT %u != %u at %u[%u]\n",
201 				       gdt_buf[last], expect, gdt_blk, last);
202 #endif
203 				retval = EXT2_ET_RESIZE_INODE_CORRUPT;
204 				goto out_dindir;
205 			}
206 			last++;
207 		}
208 		if (gdt_dirty) {
209 #ifdef RES_GDT_DEBUG
210 			printf("writing primary GDT block %u\n", gdt_blk);
211 #endif
212 			retval = ext2fs_write_ind_block(fs, gdt_blk, gdt_buf);
213 			if (retval)
214 				goto out_dindir;
215 		}
216 	}
217 
218 out_dindir:
219 	if (dindir_dirty) {
220 		retval2 = ext2fs_write_ind_block(fs, dindir_blk, dindir_buf);
221 		if (!retval)
222 			retval = retval2;
223 	}
224 out_inode:
225 #ifdef RES_GDT_DEBUG
226 	printf("inode.i_blocks = %u, i_size = %u\n", inode.i_blocks,
227 	       inode.i_size);
228 #endif
229 	if (inode_dirty) {
230 		inode.i_atime = inode.i_mtime = fs->now ? fs->now : time(0);
231 		retval2 = ext2fs_write_new_inode(fs, EXT2_RESIZE_INO, &inode);
232 		if (!retval)
233 			retval = retval2;
234 	}
235 out_free:
236 	ext2fs_free_mem(&dindir_buf);
237 	return retval;
238 }
239 
240