resize.c 32 KB

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