resize.c 32 KB

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