resize.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * linux/fs/ext3/resize.c
  3. *
  4. * Support for resizing an ext3 filesystem while it is mounted.
  5. *
  6. * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
  7. *
  8. * This could probably be made into a module, because it is not often in use.
  9. */
  10. #include <linux/config.h>
  11. #define EXT3FS_DEBUG
  12. #include <linux/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/ext3_jbd.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #define outside(b, first, last) ((b) < (first) || (b) >= (last))
  18. #define inside(b, first, last) ((b) >= (first) && (b) < (last))
  19. static int verify_group_input(struct super_block *sb,
  20. struct ext3_new_group_data *input)
  21. {
  22. struct ext3_sb_info *sbi = EXT3_SB(sb);
  23. struct ext3_super_block *es = sbi->s_es;
  24. ext3_fsblk_t start = le32_to_cpu(es->s_blocks_count);
  25. ext3_fsblk_t end = start + input->blocks_count;
  26. unsigned group = input->group;
  27. ext3_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
  28. unsigned overhead = ext3_bg_has_super(sb, group) ?
  29. (1 + ext3_bg_num_gdb(sb, group) +
  30. le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
  31. ext3_fsblk_t metaend = start + overhead;
  32. struct buffer_head *bh = NULL;
  33. ext3_grpblk_t free_blocks_count;
  34. int err = -EINVAL;
  35. input->free_blocks_count = free_blocks_count =
  36. input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
  37. if (test_opt(sb, DEBUG))
  38. printk(KERN_DEBUG "EXT3-fs: adding %s group %u: %u blocks "
  39. "(%d free, %u reserved)\n",
  40. ext3_bg_has_super(sb, input->group) ? "normal" :
  41. "no-super", input->group, input->blocks_count,
  42. free_blocks_count, input->reserved_blocks);
  43. if (group != sbi->s_groups_count)
  44. ext3_warning(sb, __FUNCTION__,
  45. "Cannot add at group %u (only %lu groups)",
  46. input->group, sbi->s_groups_count);
  47. else if ((start - le32_to_cpu(es->s_first_data_block)) %
  48. EXT3_BLOCKS_PER_GROUP(sb))
  49. ext3_warning(sb, __FUNCTION__, "Last group not full");
  50. else if (input->reserved_blocks > input->blocks_count / 5)
  51. ext3_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)",
  52. input->reserved_blocks);
  53. else if (free_blocks_count < 0)
  54. ext3_warning(sb, __FUNCTION__, "Bad blocks count %u",
  55. input->blocks_count);
  56. else if (!(bh = sb_bread(sb, end - 1)))
  57. ext3_warning(sb, __FUNCTION__,
  58. "Cannot read last block ("E3FSBLK")",
  59. end - 1);
  60. else if (outside(input->block_bitmap, start, end))
  61. ext3_warning(sb, __FUNCTION__,
  62. "Block bitmap not in group (block %u)",
  63. input->block_bitmap);
  64. else if (outside(input->inode_bitmap, start, end))
  65. ext3_warning(sb, __FUNCTION__,
  66. "Inode bitmap not in group (block %u)",
  67. input->inode_bitmap);
  68. else if (outside(input->inode_table, start, end) ||
  69. outside(itend - 1, start, end))
  70. ext3_warning(sb, __FUNCTION__,
  71. "Inode table not in group (blocks %u-"E3FSBLK")",
  72. input->inode_table, itend - 1);
  73. else if (input->inode_bitmap == input->block_bitmap)
  74. ext3_warning(sb, __FUNCTION__,
  75. "Block bitmap same as inode bitmap (%u)",
  76. input->block_bitmap);
  77. else if (inside(input->block_bitmap, input->inode_table, itend))
  78. ext3_warning(sb, __FUNCTION__,
  79. "Block bitmap (%u) in inode table (%u-"E3FSBLK")",
  80. input->block_bitmap, input->inode_table, itend-1);
  81. else if (inside(input->inode_bitmap, input->inode_table, itend))
  82. ext3_warning(sb, __FUNCTION__,
  83. "Inode bitmap (%u) in inode table (%u-"E3FSBLK")",
  84. input->inode_bitmap, input->inode_table, itend-1);
  85. else if (inside(input->block_bitmap, start, metaend))
  86. ext3_warning(sb, __FUNCTION__,
  87. "Block bitmap (%u) in GDT table"
  88. " ("E3FSBLK"-"E3FSBLK")",
  89. input->block_bitmap, start, metaend - 1);
  90. else if (inside(input->inode_bitmap, start, metaend))
  91. ext3_warning(sb, __FUNCTION__,
  92. "Inode bitmap (%u) in GDT table"
  93. " ("E3FSBLK"-"E3FSBLK")",
  94. input->inode_bitmap, start, metaend - 1);
  95. else if (inside(input->inode_table, start, metaend) ||
  96. inside(itend - 1, start, metaend))
  97. ext3_warning(sb, __FUNCTION__,
  98. "Inode table (%u-"E3FSBLK") overlaps"
  99. "GDT table ("E3FSBLK"-"E3FSBLK")",
  100. input->inode_table, itend - 1, start, metaend - 1);
  101. else
  102. err = 0;
  103. brelse(bh);
  104. return err;
  105. }
  106. static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
  107. unsigned long blk)
  108. {
  109. struct buffer_head *bh;
  110. int err;
  111. bh = sb_getblk(sb, blk);
  112. if (!bh)
  113. return ERR_PTR(-EIO);
  114. if ((err = ext3_journal_get_write_access(handle, bh))) {
  115. brelse(bh);
  116. bh = ERR_PTR(err);
  117. } else {
  118. lock_buffer(bh);
  119. memset(bh->b_data, 0, sb->s_blocksize);
  120. set_buffer_uptodate(bh);
  121. unlock_buffer(bh);
  122. }
  123. return bh;
  124. }
  125. /*
  126. * To avoid calling the atomic setbit hundreds or thousands of times, we only
  127. * need to use it within a single byte (to ensure we get endianness right).
  128. * We can use memset for the rest of the bitmap as there are no other users.
  129. */
  130. static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
  131. {
  132. int i;
  133. if (start_bit >= end_bit)
  134. return;
  135. ext3_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
  136. for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
  137. ext3_set_bit(i, bitmap);
  138. if (i < end_bit)
  139. memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
  140. }
  141. /*
  142. * Set up the block and inode bitmaps, and the inode table for the new group.
  143. * This doesn't need to be part of the main transaction, since we are only
  144. * changing blocks outside the actual filesystem. We still do journaling to
  145. * ensure the recovery is correct in case of a failure just after resize.
  146. * If any part of this fails, we simply abort the resize.
  147. */
  148. static int setup_new_group_blocks(struct super_block *sb,
  149. struct ext3_new_group_data *input)
  150. {
  151. struct ext3_sb_info *sbi = EXT3_SB(sb);
  152. unsigned long start = input->group * sbi->s_blocks_per_group +
  153. le32_to_cpu(sbi->s_es->s_first_data_block);
  154. int reserved_gdb = ext3_bg_has_super(sb, input->group) ?
  155. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
  156. unsigned long gdblocks = ext3_bg_num_gdb(sb, input->group);
  157. struct buffer_head *bh;
  158. handle_t *handle;
  159. unsigned long block;
  160. ext3_grpblk_t bit;
  161. int i;
  162. int err = 0, err2;
  163. handle = ext3_journal_start_sb(sb, reserved_gdb + gdblocks +
  164. 2 + sbi->s_itb_per_group);
  165. if (IS_ERR(handle))
  166. return PTR_ERR(handle);
  167. lock_super(sb);
  168. if (input->group != sbi->s_groups_count) {
  169. err = -EBUSY;
  170. goto exit_journal;
  171. }
  172. if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
  173. err = PTR_ERR(bh);
  174. goto exit_journal;
  175. }
  176. if (ext3_bg_has_super(sb, input->group)) {
  177. ext3_debug("mark backup superblock %#04lx (+0)\n", start);
  178. ext3_set_bit(0, bh->b_data);
  179. }
  180. /* Copy all of the GDT blocks into the backup in this group */
  181. for (i = 0, bit = 1, block = start + 1;
  182. i < gdblocks; i++, block++, bit++) {
  183. struct buffer_head *gdb;
  184. ext3_debug("update backup group %#04lx (+%d)\n", block, bit);
  185. gdb = sb_getblk(sb, block);
  186. if (!gdb) {
  187. err = -EIO;
  188. goto exit_bh;
  189. }
  190. if ((err = ext3_journal_get_write_access(handle, gdb))) {
  191. brelse(gdb);
  192. goto exit_bh;
  193. }
  194. lock_buffer(bh);
  195. memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, bh->b_size);
  196. set_buffer_uptodate(gdb);
  197. unlock_buffer(bh);
  198. ext3_journal_dirty_metadata(handle, gdb);
  199. ext3_set_bit(bit, bh->b_data);
  200. brelse(gdb);
  201. }
  202. /* Zero out all of the reserved backup group descriptor table blocks */
  203. for (i = 0, bit = gdblocks + 1, block = start + bit;
  204. i < reserved_gdb; i++, block++, bit++) {
  205. struct buffer_head *gdb;
  206. ext3_debug("clear reserved block %#04lx (+%d)\n", block, bit);
  207. if (IS_ERR(gdb = bclean(handle, sb, block))) {
  208. err = PTR_ERR(bh);
  209. goto exit_bh;
  210. }
  211. ext3_journal_dirty_metadata(handle, gdb);
  212. ext3_set_bit(bit, bh->b_data);
  213. brelse(gdb);
  214. }
  215. ext3_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
  216. input->block_bitmap - start);
  217. ext3_set_bit(input->block_bitmap - start, bh->b_data);
  218. ext3_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
  219. input->inode_bitmap - start);
  220. ext3_set_bit(input->inode_bitmap - start, bh->b_data);
  221. /* Zero out all of the inode table blocks */
  222. for (i = 0, block = input->inode_table, bit = block - start;
  223. i < sbi->s_itb_per_group; i++, bit++, block++) {
  224. struct buffer_head *it;
  225. ext3_debug("clear inode block %#04lx (+%d)\n", block, bit);
  226. if (IS_ERR(it = bclean(handle, sb, block))) {
  227. err = PTR_ERR(it);
  228. goto exit_bh;
  229. }
  230. ext3_journal_dirty_metadata(handle, it);
  231. brelse(it);
  232. ext3_set_bit(bit, bh->b_data);
  233. }
  234. mark_bitmap_end(input->blocks_count, EXT3_BLOCKS_PER_GROUP(sb),
  235. bh->b_data);
  236. ext3_journal_dirty_metadata(handle, bh);
  237. brelse(bh);
  238. /* Mark unused entries in inode bitmap used */
  239. ext3_debug("clear inode bitmap %#04x (+%ld)\n",
  240. input->inode_bitmap, input->inode_bitmap - start);
  241. if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
  242. err = PTR_ERR(bh);
  243. goto exit_journal;
  244. }
  245. mark_bitmap_end(EXT3_INODES_PER_GROUP(sb), EXT3_BLOCKS_PER_GROUP(sb),
  246. bh->b_data);
  247. ext3_journal_dirty_metadata(handle, bh);
  248. exit_bh:
  249. brelse(bh);
  250. exit_journal:
  251. unlock_super(sb);
  252. if ((err2 = ext3_journal_stop(handle)) && !err)
  253. err = err2;
  254. return err;
  255. }
  256. /*
  257. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  258. * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
  259. * calling this for the first time. In a sparse filesystem it will be the
  260. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  261. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  262. */
  263. static unsigned ext3_list_backups(struct super_block *sb, unsigned *three,
  264. unsigned *five, unsigned *seven)
  265. {
  266. unsigned *min = three;
  267. int mult = 3;
  268. unsigned ret;
  269. if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
  270. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  271. ret = *min;
  272. *min += 1;
  273. return ret;
  274. }
  275. if (*five < *min) {
  276. min = five;
  277. mult = 5;
  278. }
  279. if (*seven < *min) {
  280. min = seven;
  281. mult = 7;
  282. }
  283. ret = *min;
  284. *min *= mult;
  285. return ret;
  286. }
  287. /*
  288. * Check that all of the backup GDT blocks are held in the primary GDT block.
  289. * It is assumed that they are stored in group order. Returns the number of
  290. * groups in current filesystem that have BACKUPS, or -ve error code.
  291. */
  292. static int verify_reserved_gdb(struct super_block *sb,
  293. struct buffer_head *primary)
  294. {
  295. const unsigned long blk = primary->b_blocknr;
  296. const unsigned long end = EXT3_SB(sb)->s_groups_count;
  297. unsigned three = 1;
  298. unsigned five = 5;
  299. unsigned seven = 7;
  300. unsigned grp;
  301. __u32 *p = (__u32 *)primary->b_data;
  302. int gdbackups = 0;
  303. while ((grp = ext3_list_backups(sb, &three, &five, &seven)) < end) {
  304. if (le32_to_cpu(*p++) != grp * EXT3_BLOCKS_PER_GROUP(sb) + blk){
  305. ext3_warning(sb, __FUNCTION__,
  306. "reserved GDT %lu missing grp %d (%lu)",
  307. blk, grp,
  308. grp * EXT3_BLOCKS_PER_GROUP(sb) + blk);
  309. return -EINVAL;
  310. }
  311. if (++gdbackups > EXT3_ADDR_PER_BLOCK(sb))
  312. return -EFBIG;
  313. }
  314. return gdbackups;
  315. }
  316. /*
  317. * Called when we need to bring a reserved group descriptor table block into
  318. * use from the resize inode. The primary copy of the new GDT block currently
  319. * is an indirect block (under the double indirect block in the resize inode).
  320. * The new backup GDT blocks will be stored as leaf blocks in this indirect
  321. * block, in group order. Even though we know all the block numbers we need,
  322. * we check to ensure that the resize inode has actually reserved these blocks.
  323. *
  324. * Don't need to update the block bitmaps because the blocks are still in use.
  325. *
  326. * We get all of the error cases out of the way, so that we are sure to not
  327. * fail once we start modifying the data on disk, because JBD has no rollback.
  328. */
  329. static int add_new_gdb(handle_t *handle, struct inode *inode,
  330. struct ext3_new_group_data *input,
  331. struct buffer_head **primary)
  332. {
  333. struct super_block *sb = inode->i_sb;
  334. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  335. unsigned long gdb_num = input->group / EXT3_DESC_PER_BLOCK(sb);
  336. unsigned long gdblock = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
  337. struct buffer_head **o_group_desc, **n_group_desc;
  338. struct buffer_head *dind;
  339. int gdbackups;
  340. struct ext3_iloc iloc;
  341. __u32 *data;
  342. int err;
  343. if (test_opt(sb, DEBUG))
  344. printk(KERN_DEBUG
  345. "EXT3-fs: ext3_add_new_gdb: adding group block %lu\n",
  346. gdb_num);
  347. /*
  348. * If we are not using the primary superblock/GDT copy don't resize,
  349. * because the user tools have no way of handling this. Probably a
  350. * bad time to do it anyways.
  351. */
  352. if (EXT3_SB(sb)->s_sbh->b_blocknr !=
  353. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) {
  354. ext3_warning(sb, __FUNCTION__,
  355. "won't resize using backup superblock at %llu",
  356. (unsigned long long)EXT3_SB(sb)->s_sbh->b_blocknr);
  357. return -EPERM;
  358. }
  359. *primary = sb_bread(sb, gdblock);
  360. if (!*primary)
  361. return -EIO;
  362. if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
  363. err = gdbackups;
  364. goto exit_bh;
  365. }
  366. data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
  367. dind = sb_bread(sb, le32_to_cpu(*data));
  368. if (!dind) {
  369. err = -EIO;
  370. goto exit_bh;
  371. }
  372. data = (__u32 *)dind->b_data;
  373. if (le32_to_cpu(data[gdb_num % EXT3_ADDR_PER_BLOCK(sb)]) != gdblock) {
  374. ext3_warning(sb, __FUNCTION__,
  375. "new group %u GDT block %lu not reserved",
  376. input->group, gdblock);
  377. err = -EINVAL;
  378. goto exit_dind;
  379. }
  380. if ((err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh)))
  381. goto exit_dind;
  382. if ((err = ext3_journal_get_write_access(handle, *primary)))
  383. goto exit_sbh;
  384. if ((err = ext3_journal_get_write_access(handle, dind)))
  385. goto exit_primary;
  386. /* ext3_reserve_inode_write() gets a reference on the iloc */
  387. if ((err = ext3_reserve_inode_write(handle, inode, &iloc)))
  388. goto exit_dindj;
  389. n_group_desc = (struct buffer_head **)kmalloc((gdb_num + 1) *
  390. sizeof(struct buffer_head *), GFP_KERNEL);
  391. if (!n_group_desc) {
  392. err = -ENOMEM;
  393. ext3_warning (sb, __FUNCTION__,
  394. "not enough memory for %lu groups", gdb_num + 1);
  395. goto exit_inode;
  396. }
  397. /*
  398. * Finally, we have all of the possible failures behind us...
  399. *
  400. * Remove new GDT block from inode double-indirect block and clear out
  401. * the new GDT block for use (which also "frees" the backup GDT blocks
  402. * from the reserved inode). We don't need to change the bitmaps for
  403. * these blocks, because they are marked as in-use from being in the
  404. * reserved inode, and will become GDT blocks (primary and backup).
  405. */
  406. data[gdb_num % EXT3_ADDR_PER_BLOCK(sb)] = 0;
  407. ext3_journal_dirty_metadata(handle, dind);
  408. brelse(dind);
  409. inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
  410. ext3_mark_iloc_dirty(handle, inode, &iloc);
  411. memset((*primary)->b_data, 0, sb->s_blocksize);
  412. ext3_journal_dirty_metadata(handle, *primary);
  413. o_group_desc = EXT3_SB(sb)->s_group_desc;
  414. memcpy(n_group_desc, o_group_desc,
  415. EXT3_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
  416. n_group_desc[gdb_num] = *primary;
  417. EXT3_SB(sb)->s_group_desc = n_group_desc;
  418. EXT3_SB(sb)->s_gdb_count++;
  419. kfree(o_group_desc);
  420. es->s_reserved_gdt_blocks =
  421. cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1);
  422. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  423. return 0;
  424. exit_inode:
  425. //ext3_journal_release_buffer(handle, iloc.bh);
  426. brelse(iloc.bh);
  427. exit_dindj:
  428. //ext3_journal_release_buffer(handle, dind);
  429. exit_primary:
  430. //ext3_journal_release_buffer(handle, *primary);
  431. exit_sbh:
  432. //ext3_journal_release_buffer(handle, *primary);
  433. exit_dind:
  434. brelse(dind);
  435. exit_bh:
  436. brelse(*primary);
  437. ext3_debug("leaving with error %d\n", err);
  438. return err;
  439. }
  440. /*
  441. * Called when we are adding a new group which has a backup copy of each of
  442. * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
  443. * We need to add these reserved backup GDT blocks to the resize inode, so
  444. * that they are kept for future resizing and not allocated to files.
  445. *
  446. * Each reserved backup GDT block will go into a different indirect block.
  447. * The indirect blocks are actually the primary reserved GDT blocks,
  448. * so we know in advance what their block numbers are. We only get the
  449. * double-indirect block to verify it is pointing to the primary reserved
  450. * GDT blocks so we don't overwrite a data block by accident. The reserved
  451. * backup GDT blocks are stored in their reserved primary GDT block.
  452. */
  453. static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
  454. struct ext3_new_group_data *input)
  455. {
  456. struct super_block *sb = inode->i_sb;
  457. int reserved_gdb =le16_to_cpu(EXT3_SB(sb)->s_es->s_reserved_gdt_blocks);
  458. struct buffer_head **primary;
  459. struct buffer_head *dind;
  460. struct ext3_iloc iloc;
  461. unsigned long blk;
  462. __u32 *data, *end;
  463. int gdbackups = 0;
  464. int res, i;
  465. int err;
  466. primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL);
  467. if (!primary)
  468. return -ENOMEM;
  469. data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
  470. dind = sb_bread(sb, le32_to_cpu(*data));
  471. if (!dind) {
  472. err = -EIO;
  473. goto exit_free;
  474. }
  475. blk = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + EXT3_SB(sb)->s_gdb_count;
  476. data = (__u32 *)dind->b_data + EXT3_SB(sb)->s_gdb_count;
  477. end = (__u32 *)dind->b_data + EXT3_ADDR_PER_BLOCK(sb);
  478. /* Get each reserved primary GDT block and verify it holds backups */
  479. for (res = 0; res < reserved_gdb; res++, blk++) {
  480. if (le32_to_cpu(*data) != blk) {
  481. ext3_warning(sb, __FUNCTION__,
  482. "reserved block %lu not at offset %ld",
  483. blk, (long)(data - (__u32 *)dind->b_data));
  484. err = -EINVAL;
  485. goto exit_bh;
  486. }
  487. primary[res] = sb_bread(sb, blk);
  488. if (!primary[res]) {
  489. err = -EIO;
  490. goto exit_bh;
  491. }
  492. if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
  493. brelse(primary[res]);
  494. err = gdbackups;
  495. goto exit_bh;
  496. }
  497. if (++data >= end)
  498. data = (__u32 *)dind->b_data;
  499. }
  500. for (i = 0; i < reserved_gdb; i++) {
  501. if ((err = ext3_journal_get_write_access(handle, primary[i]))) {
  502. /*
  503. int j;
  504. for (j = 0; j < i; j++)
  505. ext3_journal_release_buffer(handle, primary[j]);
  506. */
  507. goto exit_bh;
  508. }
  509. }
  510. if ((err = ext3_reserve_inode_write(handle, inode, &iloc)))
  511. goto exit_bh;
  512. /*
  513. * Finally we can add each of the reserved backup GDT blocks from
  514. * the new group to its reserved primary GDT block.
  515. */
  516. blk = input->group * EXT3_BLOCKS_PER_GROUP(sb);
  517. for (i = 0; i < reserved_gdb; i++) {
  518. int err2;
  519. data = (__u32 *)primary[i]->b_data;
  520. /* printk("reserving backup %lu[%u] = %lu\n",
  521. primary[i]->b_blocknr, gdbackups,
  522. blk + primary[i]->b_blocknr); */
  523. data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
  524. err2 = ext3_journal_dirty_metadata(handle, primary[i]);
  525. if (!err)
  526. err = err2;
  527. }
  528. inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
  529. ext3_mark_iloc_dirty(handle, inode, &iloc);
  530. exit_bh:
  531. while (--res >= 0)
  532. brelse(primary[res]);
  533. brelse(dind);
  534. exit_free:
  535. kfree(primary);
  536. return err;
  537. }
  538. /*
  539. * Update the backup copies of the ext3 metadata. These don't need to be part
  540. * of the main resize transaction, because e2fsck will re-write them if there
  541. * is a problem (basically only OOM will cause a problem). However, we
  542. * _should_ update the backups if possible, in case the primary gets trashed
  543. * for some reason and we need to run e2fsck from a backup superblock. The
  544. * important part is that the new block and inode counts are in the backup
  545. * superblocks, and the location of the new group metadata in the GDT backups.
  546. *
  547. * We do not need lock_super() for this, because these blocks are not
  548. * otherwise touched by the filesystem code when it is mounted. We don't
  549. * need to worry about last changing from sbi->s_groups_count, because the
  550. * worst that can happen is that we do not copy the full number of backups
  551. * at this time. The resize which changed s_groups_count will backup again.
  552. */
  553. static void update_backups(struct super_block *sb,
  554. int blk_off, char *data, int size)
  555. {
  556. struct ext3_sb_info *sbi = EXT3_SB(sb);
  557. const unsigned long last = sbi->s_groups_count;
  558. const int bpg = EXT3_BLOCKS_PER_GROUP(sb);
  559. unsigned three = 1;
  560. unsigned five = 5;
  561. unsigned seven = 7;
  562. unsigned group;
  563. int rest = sb->s_blocksize - size;
  564. handle_t *handle;
  565. int err = 0, err2;
  566. handle = ext3_journal_start_sb(sb, EXT3_MAX_TRANS_DATA);
  567. if (IS_ERR(handle)) {
  568. group = 1;
  569. err = PTR_ERR(handle);
  570. goto exit_err;
  571. }
  572. while ((group = ext3_list_backups(sb, &three, &five, &seven)) < last) {
  573. struct buffer_head *bh;
  574. /* Out of journal space, and can't get more - abort - so sad */
  575. if (handle->h_buffer_credits == 0 &&
  576. ext3_journal_extend(handle, EXT3_MAX_TRANS_DATA) &&
  577. (err = ext3_journal_restart(handle, EXT3_MAX_TRANS_DATA)))
  578. break;
  579. bh = sb_getblk(sb, group * bpg + blk_off);
  580. if (!bh) {
  581. err = -EIO;
  582. break;
  583. }
  584. ext3_debug("update metadata backup %#04lx\n",
  585. (unsigned long)bh->b_blocknr);
  586. if ((err = ext3_journal_get_write_access(handle, bh)))
  587. break;
  588. lock_buffer(bh);
  589. memcpy(bh->b_data, data, size);
  590. if (rest)
  591. memset(bh->b_data + size, 0, rest);
  592. set_buffer_uptodate(bh);
  593. unlock_buffer(bh);
  594. ext3_journal_dirty_metadata(handle, bh);
  595. brelse(bh);
  596. }
  597. if ((err2 = ext3_journal_stop(handle)) && !err)
  598. err = err2;
  599. /*
  600. * Ugh! Need to have e2fsck write the backup copies. It is too
  601. * late to revert the resize, we shouldn't fail just because of
  602. * the backup copies (they are only needed in case of corruption).
  603. *
  604. * However, if we got here we have a journal problem too, so we
  605. * can't really start a transaction to mark the superblock.
  606. * Chicken out and just set the flag on the hope it will be written
  607. * to disk, and if not - we will simply wait until next fsck.
  608. */
  609. exit_err:
  610. if (err) {
  611. ext3_warning(sb, __FUNCTION__,
  612. "can't update backup for group %d (err %d), "
  613. "forcing fsck on next reboot", group, err);
  614. sbi->s_mount_state &= ~EXT3_VALID_FS;
  615. sbi->s_es->s_state &= ~cpu_to_le16(EXT3_VALID_FS);
  616. mark_buffer_dirty(sbi->s_sbh);
  617. }
  618. }
  619. /* Add group descriptor data to an existing or new group descriptor block.
  620. * Ensure we handle all possible error conditions _before_ we start modifying
  621. * the filesystem, because we cannot abort the transaction and not have it
  622. * write the data to disk.
  623. *
  624. * If we are on a GDT block boundary, we need to get the reserved GDT block.
  625. * Otherwise, we may need to add backup GDT blocks for a sparse group.
  626. *
  627. * We only need to hold the superblock lock while we are actually adding
  628. * in the new group's counts to the superblock. Prior to that we have
  629. * not really "added" the group at all. We re-check that we are still
  630. * adding in the last group in case things have changed since verifying.
  631. */
  632. int ext3_group_add(struct super_block *sb, struct ext3_new_group_data *input)
  633. {
  634. struct ext3_sb_info *sbi = EXT3_SB(sb);
  635. struct ext3_super_block *es = sbi->s_es;
  636. int reserved_gdb = ext3_bg_has_super(sb, input->group) ?
  637. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  638. struct buffer_head *primary = NULL;
  639. struct ext3_group_desc *gdp;
  640. struct inode *inode = NULL;
  641. handle_t *handle;
  642. int gdb_off, gdb_num;
  643. int err, err2;
  644. gdb_num = input->group / EXT3_DESC_PER_BLOCK(sb);
  645. gdb_off = input->group % EXT3_DESC_PER_BLOCK(sb);
  646. if (gdb_off == 0 && !EXT3_HAS_RO_COMPAT_FEATURE(sb,
  647. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  648. ext3_warning(sb, __FUNCTION__,
  649. "Can't resize non-sparse filesystem further");
  650. return -EPERM;
  651. }
  652. if (reserved_gdb || gdb_off == 0) {
  653. if (!EXT3_HAS_COMPAT_FEATURE(sb,
  654. EXT3_FEATURE_COMPAT_RESIZE_INODE)){
  655. ext3_warning(sb, __FUNCTION__,
  656. "No reserved GDT blocks, can't resize");
  657. return -EPERM;
  658. }
  659. inode = iget(sb, EXT3_RESIZE_INO);
  660. if (!inode || is_bad_inode(inode)) {
  661. ext3_warning(sb, __FUNCTION__,
  662. "Error opening resize inode");
  663. iput(inode);
  664. return -ENOENT;
  665. }
  666. }
  667. if ((err = verify_group_input(sb, input)))
  668. goto exit_put;
  669. if ((err = setup_new_group_blocks(sb, input)))
  670. goto exit_put;
  671. /*
  672. * We will always be modifying at least the superblock and a GDT
  673. * block. If we are adding a group past the last current GDT block,
  674. * we will also modify the inode and the dindirect block. If we
  675. * are adding a group with superblock/GDT backups we will also
  676. * modify each of the reserved GDT dindirect blocks.
  677. */
  678. handle = ext3_journal_start_sb(sb,
  679. ext3_bg_has_super(sb, input->group) ?
  680. 3 + reserved_gdb : 4);
  681. if (IS_ERR(handle)) {
  682. err = PTR_ERR(handle);
  683. goto exit_put;
  684. }
  685. lock_super(sb);
  686. if (input->group != sbi->s_groups_count) {
  687. ext3_warning(sb, __FUNCTION__,
  688. "multiple resizers run on filesystem!");
  689. err = -EBUSY;
  690. goto exit_journal;
  691. }
  692. if ((err = ext3_journal_get_write_access(handle, sbi->s_sbh)))
  693. goto exit_journal;
  694. /*
  695. * We will only either add reserved group blocks to a backup group
  696. * or remove reserved blocks for the first group in a new group block.
  697. * Doing both would be mean more complex code, and sane people don't
  698. * use non-sparse filesystems anymore. This is already checked above.
  699. */
  700. if (gdb_off) {
  701. primary = sbi->s_group_desc[gdb_num];
  702. if ((err = ext3_journal_get_write_access(handle, primary)))
  703. goto exit_journal;
  704. if (reserved_gdb && ext3_bg_num_gdb(sb, input->group) &&
  705. (err = reserve_backup_gdb(handle, inode, input)))
  706. goto exit_journal;
  707. } else if ((err = add_new_gdb(handle, inode, input, &primary)))
  708. goto exit_journal;
  709. /*
  710. * OK, now we've set up the new group. Time to make it active.
  711. *
  712. * Current kernels don't lock all allocations via lock_super(),
  713. * so we have to be safe wrt. concurrent accesses the group
  714. * data. So we need to be careful to set all of the relevant
  715. * group descriptor data etc. *before* we enable the group.
  716. *
  717. * The key field here is sbi->s_groups_count: as long as
  718. * that retains its old value, nobody is going to access the new
  719. * group.
  720. *
  721. * So first we update all the descriptor metadata for the new
  722. * group; then we update the total disk blocks count; then we
  723. * update the groups count to enable the group; then finally we
  724. * update the free space counts so that the system can start
  725. * using the new disk blocks.
  726. */
  727. /* Update group descriptor block for new group */
  728. gdp = (struct ext3_group_desc *)primary->b_data + gdb_off;
  729. gdp->bg_block_bitmap = cpu_to_le32(input->block_bitmap);
  730. gdp->bg_inode_bitmap = cpu_to_le32(input->inode_bitmap);
  731. gdp->bg_inode_table = cpu_to_le32(input->inode_table);
  732. gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
  733. gdp->bg_free_inodes_count = cpu_to_le16(EXT3_INODES_PER_GROUP(sb));
  734. /*
  735. * Make the new blocks and inodes valid next. We do this before
  736. * increasing the group count so that once the group is enabled,
  737. * all of its blocks and inodes are already valid.
  738. *
  739. * We always allocate group-by-group, then block-by-block or
  740. * inode-by-inode within a group, so enabling these
  741. * blocks/inodes before the group is live won't actually let us
  742. * allocate the new space yet.
  743. */
  744. es->s_blocks_count = cpu_to_le32(le32_to_cpu(es->s_blocks_count) +
  745. input->blocks_count);
  746. es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) +
  747. EXT3_INODES_PER_GROUP(sb));
  748. /*
  749. * We need to protect s_groups_count against other CPUs seeing
  750. * inconsistent state in the superblock.
  751. *
  752. * The precise rules we use are:
  753. *
  754. * * Writers of s_groups_count *must* hold lock_super
  755. * AND
  756. * * Writers must perform a smp_wmb() after updating all dependent
  757. * data and before modifying the groups count
  758. *
  759. * * Readers must hold lock_super() over the access
  760. * OR
  761. * * Readers must perform an smp_rmb() after reading the groups count
  762. * and before reading any dependent data.
  763. *
  764. * NB. These rules can be relaxed when checking the group count
  765. * while freeing data, as we can only allocate from a block
  766. * group after serialising against the group count, and we can
  767. * only then free after serialising in turn against that
  768. * allocation.
  769. */
  770. smp_wmb();
  771. /* Update the global fs size fields */
  772. sbi->s_groups_count++;
  773. ext3_journal_dirty_metadata(handle, primary);
  774. /* Update the reserved block counts only once the new group is
  775. * active. */
  776. es->s_r_blocks_count = cpu_to_le32(le32_to_cpu(es->s_r_blocks_count) +
  777. input->reserved_blocks);
  778. /* Update the free space counts */
  779. percpu_counter_mod(&sbi->s_freeblocks_counter,
  780. input->free_blocks_count);
  781. percpu_counter_mod(&sbi->s_freeinodes_counter,
  782. EXT3_INODES_PER_GROUP(sb));
  783. ext3_journal_dirty_metadata(handle, sbi->s_sbh);
  784. sb->s_dirt = 1;
  785. exit_journal:
  786. unlock_super(sb);
  787. if ((err2 = ext3_journal_stop(handle)) && !err)
  788. err = err2;
  789. if (!err) {
  790. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  791. sizeof(struct ext3_super_block));
  792. update_backups(sb, primary->b_blocknr, primary->b_data,
  793. primary->b_size);
  794. }
  795. exit_put:
  796. iput(inode);
  797. return err;
  798. } /* ext3_group_add */
  799. /* Extend the filesystem to the new number of blocks specified. This entry
  800. * point is only used to extend the current filesystem to the end of the last
  801. * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
  802. * for emergencies (because it has no dependencies on reserved blocks).
  803. *
  804. * If we _really_ wanted, we could use default values to call ext3_group_add()
  805. * allow the "remount" trick to work for arbitrary resizing, assuming enough
  806. * GDT blocks are reserved to grow to the desired size.
  807. */
  808. int ext3_group_extend(struct super_block *sb, struct ext3_super_block *es,
  809. unsigned long n_blocks_count)
  810. {
  811. unsigned long o_blocks_count;
  812. unsigned long o_groups_count;
  813. ext3_grpblk_t last;
  814. ext3_grpblk_t add;
  815. struct buffer_head * bh;
  816. handle_t *handle;
  817. int err;
  818. unsigned long freed_blocks;
  819. /* We don't need to worry about locking wrt other resizers just
  820. * yet: we're going to revalidate es->s_blocks_count after
  821. * taking lock_super() below. */
  822. o_blocks_count = le32_to_cpu(es->s_blocks_count);
  823. o_groups_count = EXT3_SB(sb)->s_groups_count;
  824. if (test_opt(sb, DEBUG))
  825. printk(KERN_DEBUG "EXT3-fs: extending last group from %lu to %lu blocks\n",
  826. o_blocks_count, n_blocks_count);
  827. if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
  828. return 0;
  829. if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  830. printk(KERN_ERR "EXT3-fs: filesystem on %s:"
  831. " too large to resize to %lu blocks safely\n",
  832. sb->s_id, n_blocks_count);
  833. if (sizeof(sector_t) < 8)
  834. ext3_warning(sb, __FUNCTION__,
  835. "CONFIG_LBD not enabled\n");
  836. return -EINVAL;
  837. }
  838. if (n_blocks_count < o_blocks_count) {
  839. ext3_warning(sb, __FUNCTION__,
  840. "can't shrink FS - resize aborted");
  841. return -EBUSY;
  842. }
  843. /* Handle the remaining blocks in the last group only. */
  844. last = (o_blocks_count - le32_to_cpu(es->s_first_data_block)) %
  845. EXT3_BLOCKS_PER_GROUP(sb);
  846. if (last == 0) {
  847. ext3_warning(sb, __FUNCTION__,
  848. "need to use ext2online to resize further");
  849. return -EPERM;
  850. }
  851. add = EXT3_BLOCKS_PER_GROUP(sb) - last;
  852. if (o_blocks_count + add > n_blocks_count)
  853. add = n_blocks_count - o_blocks_count;
  854. if (o_blocks_count + add < n_blocks_count)
  855. ext3_warning(sb, __FUNCTION__,
  856. "will only finish group (%lu blocks, %u new)",
  857. o_blocks_count + add, add);
  858. /* See if the device is actually as big as what was requested */
  859. bh = sb_bread(sb, o_blocks_count + add -1);
  860. if (!bh) {
  861. ext3_warning(sb, __FUNCTION__,
  862. "can't read last block, resize aborted");
  863. return -ENOSPC;
  864. }
  865. brelse(bh);
  866. /* We will update the superblock, one block bitmap, and
  867. * one group descriptor via ext3_free_blocks().
  868. */
  869. handle = ext3_journal_start_sb(sb, 3);
  870. if (IS_ERR(handle)) {
  871. err = PTR_ERR(handle);
  872. ext3_warning(sb, __FUNCTION__, "error %d on journal start",err);
  873. goto exit_put;
  874. }
  875. lock_super(sb);
  876. if (o_blocks_count != le32_to_cpu(es->s_blocks_count)) {
  877. ext3_warning(sb, __FUNCTION__,
  878. "multiple resizers run on filesystem!");
  879. unlock_super(sb);
  880. err = -EBUSY;
  881. goto exit_put;
  882. }
  883. if ((err = ext3_journal_get_write_access(handle,
  884. EXT3_SB(sb)->s_sbh))) {
  885. ext3_warning(sb, __FUNCTION__,
  886. "error %d on journal write access", err);
  887. unlock_super(sb);
  888. ext3_journal_stop(handle);
  889. goto exit_put;
  890. }
  891. es->s_blocks_count = cpu_to_le32(o_blocks_count + add);
  892. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  893. sb->s_dirt = 1;
  894. unlock_super(sb);
  895. ext3_debug("freeing blocks %lu through %lu\n", o_blocks_count,
  896. o_blocks_count + add);
  897. ext3_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
  898. ext3_debug("freed blocks %lu through %lu\n", o_blocks_count,
  899. o_blocks_count + add);
  900. if ((err = ext3_journal_stop(handle)))
  901. goto exit_put;
  902. if (test_opt(sb, DEBUG))
  903. printk(KERN_DEBUG "EXT3-fs: extended group to %u blocks\n",
  904. le32_to_cpu(es->s_blocks_count));
  905. update_backups(sb, EXT3_SB(sb)->s_sbh->b_blocknr, (char *)es,
  906. sizeof(struct ext3_super_block));
  907. exit_put:
  908. return err;
  909. } /* ext3_group_extend */