resize.c 33 KB

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