resize.c 34 KB

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