resize.c 34 KB

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