resize.c 34 KB

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