resize.c 34 KB

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