PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext4/resize.c

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