resize.c 33 KB

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