resize.c 32 KB

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