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