resize.c 33 KB

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