resize.c 33 KB

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