resize.c 33 KB

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