resize.c 32 KB

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