resize.c 33 KB

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