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