resize.c 34 KB

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