resize.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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, __FUNCTION__,
  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, __FUNCTION__, "Last group not full");
  47. else if (input->reserved_blocks > input->blocks_count / 5)
  48. ext3_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)",
  49. input->reserved_blocks);
  50. else if (free_blocks_count < 0)
  51. ext3_warning(sb, __FUNCTION__, "Bad blocks count %u",
  52. input->blocks_count);
  53. else if (!(bh = sb_bread(sb, end - 1)))
  54. ext3_warning(sb, __FUNCTION__,
  55. "Cannot read last block ("E3FSBLK")",
  56. end - 1);
  57. else if (outside(input->block_bitmap, start, end))
  58. ext3_warning(sb, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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_KERNEL);
  424. if (!n_group_desc) {
  425. err = -ENOMEM;
  426. ext3_warning (sb, __FUNCTION__,
  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. es->s_reserved_gdt_blocks =
  454. cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1);
  455. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  456. return 0;
  457. exit_inode:
  458. //ext3_journal_release_buffer(handle, iloc.bh);
  459. brelse(iloc.bh);
  460. exit_dindj:
  461. //ext3_journal_release_buffer(handle, dind);
  462. exit_primary:
  463. //ext3_journal_release_buffer(handle, *primary);
  464. exit_sbh:
  465. //ext3_journal_release_buffer(handle, *primary);
  466. exit_dind:
  467. brelse(dind);
  468. exit_bh:
  469. brelse(*primary);
  470. ext3_debug("leaving with error %d\n", err);
  471. return err;
  472. }
  473. /*
  474. * Called when we are adding a new group which has a backup copy of each of
  475. * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
  476. * We need to add these reserved backup GDT blocks to the resize inode, so
  477. * that they are kept for future resizing and not allocated to files.
  478. *
  479. * Each reserved backup GDT block will go into a different indirect block.
  480. * The indirect blocks are actually the primary reserved GDT blocks,
  481. * so we know in advance what their block numbers are. We only get the
  482. * double-indirect block to verify it is pointing to the primary reserved
  483. * GDT blocks so we don't overwrite a data block by accident. The reserved
  484. * backup GDT blocks are stored in their reserved primary GDT block.
  485. */
  486. static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
  487. struct ext3_new_group_data *input)
  488. {
  489. struct super_block *sb = inode->i_sb;
  490. int reserved_gdb =le16_to_cpu(EXT3_SB(sb)->s_es->s_reserved_gdt_blocks);
  491. struct buffer_head **primary;
  492. struct buffer_head *dind;
  493. struct ext3_iloc iloc;
  494. ext3_fsblk_t blk;
  495. __le32 *data, *end;
  496. int gdbackups = 0;
  497. int res, i;
  498. int err;
  499. primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL);
  500. if (!primary)
  501. return -ENOMEM;
  502. data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
  503. dind = sb_bread(sb, le32_to_cpu(*data));
  504. if (!dind) {
  505. err = -EIO;
  506. goto exit_free;
  507. }
  508. blk = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + EXT3_SB(sb)->s_gdb_count;
  509. data = (__le32 *)dind->b_data + EXT3_SB(sb)->s_gdb_count;
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__, "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, __FUNCTION__, "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. ext3_warning(sb, __FUNCTION__,
  701. "No reserved GDT blocks, can't resize");
  702. return -EPERM;
  703. }
  704. inode = iget(sb, EXT3_RESIZE_INO);
  705. if (!inode || is_bad_inode(inode)) {
  706. ext3_warning(sb, __FUNCTION__,
  707. "Error opening resize inode");
  708. iput(inode);
  709. return -ENOENT;
  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, __FUNCTION__,
  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. es->s_blocks_count = cpu_to_le32(le32_to_cpu(es->s_blocks_count) +
  790. input->blocks_count);
  791. es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) +
  792. EXT3_INODES_PER_GROUP(sb));
  793. /*
  794. * We need to protect s_groups_count against other CPUs seeing
  795. * inconsistent state in the superblock.
  796. *
  797. * The precise rules we use are:
  798. *
  799. * * Writers of s_groups_count *must* hold lock_super
  800. * AND
  801. * * Writers must perform a smp_wmb() after updating all dependent
  802. * data and before modifying the groups count
  803. *
  804. * * Readers must hold lock_super() over the access
  805. * OR
  806. * * Readers must perform an smp_rmb() after reading the groups count
  807. * and before reading any dependent data.
  808. *
  809. * NB. These rules can be relaxed when checking the group count
  810. * while freeing data, as we can only allocate from a block
  811. * group after serialising against the group count, and we can
  812. * only then free after serialising in turn against that
  813. * allocation.
  814. */
  815. smp_wmb();
  816. /* Update the global fs size fields */
  817. sbi->s_groups_count++;
  818. ext3_journal_dirty_metadata(handle, primary);
  819. /* Update the reserved block counts only once the new group is
  820. * active. */
  821. es->s_r_blocks_count = cpu_to_le32(le32_to_cpu(es->s_r_blocks_count) +
  822. input->reserved_blocks);
  823. /* Update the free space counts */
  824. percpu_counter_add(&sbi->s_freeblocks_counter,
  825. input->free_blocks_count);
  826. percpu_counter_add(&sbi->s_freeinodes_counter,
  827. EXT3_INODES_PER_GROUP(sb));
  828. ext3_journal_dirty_metadata(handle, sbi->s_sbh);
  829. sb->s_dirt = 1;
  830. exit_journal:
  831. unlock_super(sb);
  832. if ((err2 = ext3_journal_stop(handle)) && !err)
  833. err = err2;
  834. if (!err) {
  835. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  836. sizeof(struct ext3_super_block));
  837. update_backups(sb, primary->b_blocknr, primary->b_data,
  838. primary->b_size);
  839. }
  840. exit_put:
  841. iput(inode);
  842. return err;
  843. } /* ext3_group_add */
  844. /* Extend the filesystem to the new number of blocks specified. This entry
  845. * point is only used to extend the current filesystem to the end of the last
  846. * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
  847. * for emergencies (because it has no dependencies on reserved blocks).
  848. *
  849. * If we _really_ wanted, we could use default values to call ext3_group_add()
  850. * allow the "remount" trick to work for arbitrary resizing, assuming enough
  851. * GDT blocks are reserved to grow to the desired size.
  852. */
  853. int ext3_group_extend(struct super_block *sb, struct ext3_super_block *es,
  854. ext3_fsblk_t n_blocks_count)
  855. {
  856. ext3_fsblk_t o_blocks_count;
  857. unsigned long o_groups_count;
  858. ext3_grpblk_t last;
  859. ext3_grpblk_t add;
  860. struct buffer_head * bh;
  861. handle_t *handle;
  862. int err;
  863. unsigned long freed_blocks;
  864. /* We don't need to worry about locking wrt other resizers just
  865. * yet: we're going to revalidate es->s_blocks_count after
  866. * taking lock_super() below. */
  867. o_blocks_count = le32_to_cpu(es->s_blocks_count);
  868. o_groups_count = EXT3_SB(sb)->s_groups_count;
  869. if (test_opt(sb, DEBUG))
  870. printk(KERN_DEBUG "EXT3-fs: extending last group from "E3FSBLK" uto "E3FSBLK" blocks\n",
  871. o_blocks_count, n_blocks_count);
  872. if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
  873. return 0;
  874. if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  875. printk(KERN_ERR "EXT3-fs: filesystem on %s:"
  876. " too large to resize to %lu blocks safely\n",
  877. sb->s_id, n_blocks_count);
  878. if (sizeof(sector_t) < 8)
  879. ext3_warning(sb, __FUNCTION__,
  880. "CONFIG_LBD not enabled\n");
  881. return -EINVAL;
  882. }
  883. if (n_blocks_count < o_blocks_count) {
  884. ext3_warning(sb, __FUNCTION__,
  885. "can't shrink FS - resize aborted");
  886. return -EBUSY;
  887. }
  888. /* Handle the remaining blocks in the last group only. */
  889. last = (o_blocks_count - le32_to_cpu(es->s_first_data_block)) %
  890. EXT3_BLOCKS_PER_GROUP(sb);
  891. if (last == 0) {
  892. ext3_warning(sb, __FUNCTION__,
  893. "need to use ext2online to resize further");
  894. return -EPERM;
  895. }
  896. add = EXT3_BLOCKS_PER_GROUP(sb) - last;
  897. if (o_blocks_count + add < o_blocks_count) {
  898. ext3_warning(sb, __FUNCTION__, "blocks_count overflow");
  899. return -EINVAL;
  900. }
  901. if (o_blocks_count + add > n_blocks_count)
  902. add = n_blocks_count - o_blocks_count;
  903. if (o_blocks_count + add < n_blocks_count)
  904. ext3_warning(sb, __FUNCTION__,
  905. "will only finish group ("E3FSBLK
  906. " blocks, %u new)",
  907. o_blocks_count + add, add);
  908. /* See if the device is actually as big as what was requested */
  909. bh = sb_bread(sb, o_blocks_count + add -1);
  910. if (!bh) {
  911. ext3_warning(sb, __FUNCTION__,
  912. "can't read last block, resize aborted");
  913. return -ENOSPC;
  914. }
  915. brelse(bh);
  916. /* We will update the superblock, one block bitmap, and
  917. * one group descriptor via ext3_free_blocks().
  918. */
  919. handle = ext3_journal_start_sb(sb, 3);
  920. if (IS_ERR(handle)) {
  921. err = PTR_ERR(handle);
  922. ext3_warning(sb, __FUNCTION__, "error %d on journal start",err);
  923. goto exit_put;
  924. }
  925. lock_super(sb);
  926. if (o_blocks_count != le32_to_cpu(es->s_blocks_count)) {
  927. ext3_warning(sb, __FUNCTION__,
  928. "multiple resizers run on filesystem!");
  929. unlock_super(sb);
  930. err = -EBUSY;
  931. goto exit_put;
  932. }
  933. if ((err = ext3_journal_get_write_access(handle,
  934. EXT3_SB(sb)->s_sbh))) {
  935. ext3_warning(sb, __FUNCTION__,
  936. "error %d on journal write access", err);
  937. unlock_super(sb);
  938. ext3_journal_stop(handle);
  939. goto exit_put;
  940. }
  941. es->s_blocks_count = cpu_to_le32(o_blocks_count + add);
  942. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  943. sb->s_dirt = 1;
  944. unlock_super(sb);
  945. ext3_debug("freeing blocks %lu through "E3FSBLK"\n", o_blocks_count,
  946. o_blocks_count + add);
  947. ext3_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
  948. ext3_debug("freed blocks "E3FSBLK" through "E3FSBLK"\n", o_blocks_count,
  949. o_blocks_count + add);
  950. if ((err = ext3_journal_stop(handle)))
  951. goto exit_put;
  952. if (test_opt(sb, DEBUG))
  953. printk(KERN_DEBUG "EXT3-fs: extended group to %u blocks\n",
  954. le32_to_cpu(es->s_blocks_count));
  955. update_backups(sb, EXT3_SB(sb)->s_sbh->b_blocknr, (char *)es,
  956. sizeof(struct ext3_super_block));
  957. exit_put:
  958. return err;
  959. } /* ext3_group_extend */