• 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 <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  */
list_backups(ext2_filsys fs,unsigned int * three,unsigned int * five,unsigned int * seven)26 static 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  */
ext2fs_create_resize_inode(ext2_filsys fs)61 errcode_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 	unsigned long long	apb, inode_size;
68 	/* FIXME-64 - can't deal with extents */
69 	blk_t			dindir_blk, rsv_off, gdt_off, gdt_blk;
70 	int			dindir_dirty = 0, inode_dirty = 0, sb_blk = 0;
71 
72 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
73 
74 	sb = fs->super;
75 
76 	retval = ext2fs_get_array(2, fs->blocksize, &dindir_buf);
77 	if (retval)
78 		return retval;
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 	/*
86 	 * File systems with a blocksize of 1024 and bigalloc have
87 	 * sb->s_first_data_block of 0; yet the superblock is still at
88 	 * block #1.  We compensate for it here.
89 	 */
90 	sb_blk = sb->s_first_data_block;
91 	if (fs->blocksize == 1024 && sb_blk == 0)
92 		sb_blk = 1;
93 
94 	/* Maximum possible file size (we donly use the dindirect blocks) */
95 	apb = EXT2_ADDR_PER_BLOCK(sb);
96 	if ((dindir_blk = inode.i_block[EXT2_DIND_BLOCK])) {
97 #ifdef RES_GDT_DEBUG
98 		printf("reading GDT dindir %u\n", dindir_blk);
99 #endif
100 		retval = ext2fs_read_ind_block(fs, dindir_blk, dindir_buf);
101 		if (retval)
102 			goto out_inode;
103 	} else {
104 		blk_t goal = sb_blk + fs->desc_blocks +
105 			sb->s_reserved_gdt_blocks + 2 +
106 			fs->inode_blocks_per_group;
107 
108 		retval = ext2fs_alloc_block(fs, goal, 0, &dindir_blk);
109 		if (retval)
110 			goto out_free;
111 		inode.i_mode = LINUX_S_IFREG | 0600;
112 		inode.i_links_count = 1;
113 		inode.i_block[EXT2_DIND_BLOCK] = dindir_blk;
114 		ext2fs_iblk_set(fs, &inode, 1);
115 		memset(dindir_buf, 0, fs->blocksize);
116 #ifdef RES_GDT_DEBUG
117 		printf("allocated GDT dindir %u\n", dindir_blk);
118 #endif
119 		dindir_dirty = inode_dirty = 1;
120 		inode_size = apb*apb + apb + EXT2_NDIR_BLOCKS;
121 		inode_size *= fs->blocksize;
122 		inode.i_size = inode_size & 0xFFFFFFFF;
123 		inode.i_size_high = (inode_size >> 32) & 0xFFFFFFFF;
124 		if(inode.i_size_high) {
125 			sb->s_feature_ro_compat |=
126 				EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
127 		}
128 		inode.i_ctime = fs->now ? fs->now : time(0);
129 	}
130 
131 	for (rsv_off = 0, gdt_off = fs->desc_blocks,
132 	     gdt_blk = sb_blk + 1 + fs->desc_blocks;
133 	     rsv_off < sb->s_reserved_gdt_blocks;
134 	     rsv_off++, gdt_off++, gdt_blk++) {
135 		unsigned int three = 1, five = 5, seven = 7;
136 		unsigned int grp, last = 0;
137 		int gdt_dirty = 0;
138 
139 		gdt_off %= apb;
140 		if (!dindir_buf[gdt_off]) {
141 			/* FIXME XXX XXX
142 			blk_t new_blk;
143 
144 			retval = ext2fs_new_block(fs, gdt_blk, 0, &new_blk);
145 			if (retval)
146 				goto out_free;
147 			if (new_blk != gdt_blk) {
148 				// XXX free block
149 				retval = -1; // XXX
150 			}
151 			*/
152 			gdt_dirty = dindir_dirty = inode_dirty = 1;
153 			memset(gdt_buf, 0, fs->blocksize);
154 			dindir_buf[gdt_off] = gdt_blk;
155 			ext2fs_iblk_add_blocks(fs, &inode, 1);
156 #ifdef RES_GDT_DEBUG
157 			printf("added primary GDT block %u at %u[%u]\n",
158 			       gdt_blk, dindir_blk, gdt_off);
159 #endif
160 		} else if (dindir_buf[gdt_off] == gdt_blk) {
161 #ifdef RES_GDT_DEBUG
162 			printf("reading primary GDT block %u\n", gdt_blk);
163 #endif
164 			retval = ext2fs_read_ind_block(fs, gdt_blk, gdt_buf);
165 			if (retval)
166 				goto out_dindir;
167 		} else {
168 #ifdef RES_GDT_DEBUG
169 			printf("bad primary GDT %u != %u at %u[%u]\n",
170 			       dindir_buf[gdt_off], gdt_blk,dindir_blk,gdt_off);
171 #endif
172 			retval = EXT2_ET_RESIZE_INODE_CORRUPT;
173 			goto out_dindir;
174 		}
175 
176 		while ((grp = list_backups(fs, &three, &five, &seven)) <
177 		       fs->group_desc_count) {
178 			blk_t expect = gdt_blk + grp * sb->s_blocks_per_group;
179 
180 			if (!gdt_buf[last]) {
181 #ifdef RES_GDT_DEBUG
182 				printf("added backup GDT %u grp %u@%u[%u]\n",
183 				       expect, grp, gdt_blk, last);
184 #endif
185 				gdt_buf[last] = expect;
186 				ext2fs_iblk_add_blocks(fs, &inode, 1);
187 				gdt_dirty = inode_dirty = 1;
188 			} else if (gdt_buf[last] != expect) {
189 #ifdef RES_GDT_DEBUG
190 				printf("bad backup GDT %u != %u at %u[%u]\n",
191 				       gdt_buf[last], expect, gdt_blk, last);
192 #endif
193 				retval = EXT2_ET_RESIZE_INODE_CORRUPT;
194 				goto out_dindir;
195 			}
196 			last++;
197 		}
198 		if (gdt_dirty) {
199 #ifdef RES_GDT_DEBUG
200 			printf("writing primary GDT block %u\n", gdt_blk);
201 #endif
202 			retval = ext2fs_write_ind_block(fs, gdt_blk, gdt_buf);
203 			if (retval)
204 				goto out_dindir;
205 		}
206 	}
207 
208 out_dindir:
209 	if (dindir_dirty) {
210 		retval2 = ext2fs_write_ind_block(fs, dindir_blk, dindir_buf);
211 		if (!retval)
212 			retval = retval2;
213 	}
214 out_inode:
215 #ifdef RES_GDT_DEBUG
216 	printf("inode.i_blocks = %u, i_size = %u\n", inode.i_blocks,
217 	       inode.i_size);
218 #endif
219 	if (inode_dirty) {
220 		inode.i_atime = inode.i_mtime = fs->now ? fs->now : time(0);
221 		retval2 = ext2fs_write_new_inode(fs, EXT2_RESIZE_INO, &inode);
222 		if (!retval)
223 			retval = retval2;
224 	}
225 out_free:
226 	ext2fs_free_mem(&dindir_buf);
227 	return retval;
228 }
229 
230