resize.c 35 KB

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