resize.c 33 KB

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