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