resize.c 33 KB

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