resize.c 33 KB

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