resize.c 33 KB

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