resize.c 33 KB

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