PageRenderTime 43ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext3/resize.c

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