resize.c 33 KB

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