resize.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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/errno.h>
  12. #include <linux/slab.h>
  13. #include "ext4_jbd2.h"
  14. int ext4_resize_begin(struct super_block *sb)
  15. {
  16. int ret = 0;
  17. if (!capable(CAP_SYS_RESOURCE))
  18. return -EPERM;
  19. /*
  20. * We are not allowed to do online-resizing on a filesystem mounted
  21. * with error, because it can destroy the filesystem easily.
  22. */
  23. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  24. ext4_warning(sb, "There are errors in the filesystem, "
  25. "so online resizing is not allowed\n");
  26. return -EPERM;
  27. }
  28. if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags))
  29. ret = -EBUSY;
  30. return ret;
  31. }
  32. void ext4_resize_end(struct super_block *sb)
  33. {
  34. clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags);
  35. smp_mb__after_clear_bit();
  36. }
  37. #define outside(b, first, last) ((b) < (first) || (b) >= (last))
  38. #define inside(b, first, last) ((b) >= (first) && (b) < (last))
  39. static int verify_group_input(struct super_block *sb,
  40. struct ext4_new_group_data *input)
  41. {
  42. struct ext4_sb_info *sbi = EXT4_SB(sb);
  43. struct ext4_super_block *es = sbi->s_es;
  44. ext4_fsblk_t start = ext4_blocks_count(es);
  45. ext4_fsblk_t end = start + input->blocks_count;
  46. ext4_group_t group = input->group;
  47. ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
  48. unsigned overhead = ext4_bg_has_super(sb, group) ?
  49. (1 + ext4_bg_num_gdb(sb, group) +
  50. le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
  51. ext4_fsblk_t metaend = start + overhead;
  52. struct buffer_head *bh = NULL;
  53. ext4_grpblk_t free_blocks_count, offset;
  54. int err = -EINVAL;
  55. input->free_blocks_count = free_blocks_count =
  56. input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
  57. if (test_opt(sb, DEBUG))
  58. printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
  59. "(%d free, %u reserved)\n",
  60. ext4_bg_has_super(sb, input->group) ? "normal" :
  61. "no-super", input->group, input->blocks_count,
  62. free_blocks_count, input->reserved_blocks);
  63. ext4_get_group_no_and_offset(sb, start, NULL, &offset);
  64. if (group != sbi->s_groups_count)
  65. ext4_warning(sb, "Cannot add at group %u (only %u groups)",
  66. input->group, sbi->s_groups_count);
  67. else if (offset != 0)
  68. ext4_warning(sb, "Last group not full");
  69. else if (input->reserved_blocks > input->blocks_count / 5)
  70. ext4_warning(sb, "Reserved blocks too high (%u)",
  71. input->reserved_blocks);
  72. else if (free_blocks_count < 0)
  73. ext4_warning(sb, "Bad blocks count %u",
  74. input->blocks_count);
  75. else if (!(bh = sb_bread(sb, end - 1)))
  76. ext4_warning(sb, "Cannot read last block (%llu)",
  77. end - 1);
  78. else if (outside(input->block_bitmap, start, end))
  79. ext4_warning(sb, "Block bitmap not in group (block %llu)",
  80. (unsigned long long)input->block_bitmap);
  81. else if (outside(input->inode_bitmap, start, end))
  82. ext4_warning(sb, "Inode bitmap not in group (block %llu)",
  83. (unsigned long long)input->inode_bitmap);
  84. else if (outside(input->inode_table, start, end) ||
  85. outside(itend - 1, start, end))
  86. ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
  87. (unsigned long long)input->inode_table, itend - 1);
  88. else if (input->inode_bitmap == input->block_bitmap)
  89. ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
  90. (unsigned long long)input->block_bitmap);
  91. else if (inside(input->block_bitmap, input->inode_table, itend))
  92. ext4_warning(sb, "Block bitmap (%llu) in inode table "
  93. "(%llu-%llu)",
  94. (unsigned long long)input->block_bitmap,
  95. (unsigned long long)input->inode_table, itend - 1);
  96. else if (inside(input->inode_bitmap, input->inode_table, itend))
  97. ext4_warning(sb, "Inode bitmap (%llu) in inode table "
  98. "(%llu-%llu)",
  99. (unsigned long long)input->inode_bitmap,
  100. (unsigned long long)input->inode_table, itend - 1);
  101. else if (inside(input->block_bitmap, start, metaend))
  102. ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
  103. (unsigned long long)input->block_bitmap,
  104. start, metaend - 1);
  105. else if (inside(input->inode_bitmap, start, metaend))
  106. ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
  107. (unsigned long long)input->inode_bitmap,
  108. start, metaend - 1);
  109. else if (inside(input->inode_table, start, metaend) ||
  110. inside(itend - 1, start, metaend))
  111. ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
  112. "(%llu-%llu)",
  113. (unsigned long long)input->inode_table,
  114. itend - 1, start, metaend - 1);
  115. else
  116. err = 0;
  117. brelse(bh);
  118. return err;
  119. }
  120. /*
  121. * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
  122. * group each time.
  123. */
  124. struct ext4_new_flex_group_data {
  125. struct ext4_new_group_data *groups; /* new_group_data for groups
  126. in the flex group */
  127. __u16 *bg_flags; /* block group flags of groups
  128. in @groups */
  129. ext4_group_t count; /* number of groups in @groups
  130. */
  131. };
  132. /*
  133. * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
  134. * @flexbg_size.
  135. *
  136. * Returns NULL on failure otherwise address of the allocated structure.
  137. */
  138. static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
  139. {
  140. struct ext4_new_flex_group_data *flex_gd;
  141. flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
  142. if (flex_gd == NULL)
  143. goto out3;
  144. flex_gd->count = flexbg_size;
  145. flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) *
  146. flexbg_size, GFP_NOFS);
  147. if (flex_gd->groups == NULL)
  148. goto out2;
  149. flex_gd->bg_flags = kmalloc(flexbg_size * sizeof(__u16), GFP_NOFS);
  150. if (flex_gd->bg_flags == NULL)
  151. goto out1;
  152. return flex_gd;
  153. out1:
  154. kfree(flex_gd->groups);
  155. out2:
  156. kfree(flex_gd);
  157. out3:
  158. return NULL;
  159. }
  160. static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
  161. {
  162. kfree(flex_gd->bg_flags);
  163. kfree(flex_gd->groups);
  164. kfree(flex_gd);
  165. }
  166. static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
  167. ext4_fsblk_t blk)
  168. {
  169. struct buffer_head *bh;
  170. int err;
  171. bh = sb_getblk(sb, blk);
  172. if (!bh)
  173. return ERR_PTR(-EIO);
  174. if ((err = ext4_journal_get_write_access(handle, bh))) {
  175. brelse(bh);
  176. bh = ERR_PTR(err);
  177. } else {
  178. memset(bh->b_data, 0, sb->s_blocksize);
  179. set_buffer_uptodate(bh);
  180. }
  181. return bh;
  182. }
  183. /*
  184. * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
  185. * If that fails, restart the transaction & regain write access for the
  186. * buffer head which is used for block_bitmap modifications.
  187. */
  188. static int extend_or_restart_transaction(handle_t *handle, int thresh)
  189. {
  190. int err;
  191. if (ext4_handle_has_enough_credits(handle, thresh))
  192. return 0;
  193. err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
  194. if (err < 0)
  195. return err;
  196. if (err) {
  197. err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
  198. if (err)
  199. return err;
  200. }
  201. return 0;
  202. }
  203. /*
  204. * set_flexbg_block_bitmap() mark @count blocks starting from @block used.
  205. *
  206. * Helper function for ext4_setup_new_group_blocks() which set .
  207. *
  208. * @sb: super block
  209. * @handle: journal handle
  210. * @flex_gd: flex group data
  211. */
  212. static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
  213. struct ext4_new_flex_group_data *flex_gd,
  214. ext4_fsblk_t block, ext4_group_t count)
  215. {
  216. ext4_group_t count2;
  217. ext4_debug("mark blocks [%llu/%u] used\n", block, count);
  218. for (count2 = count; count > 0; count -= count2, block += count2) {
  219. ext4_fsblk_t start;
  220. struct buffer_head *bh;
  221. ext4_group_t group;
  222. int err;
  223. ext4_get_group_no_and_offset(sb, block, &group, NULL);
  224. start = ext4_group_first_block_no(sb, group);
  225. group -= flex_gd->groups[0].group;
  226. count2 = sb->s_blocksize * 8 - (block - start);
  227. if (count2 > count)
  228. count2 = count;
  229. if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
  230. BUG_ON(flex_gd->count > 1);
  231. continue;
  232. }
  233. err = extend_or_restart_transaction(handle, 1);
  234. if (err)
  235. return err;
  236. bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
  237. if (!bh)
  238. return -EIO;
  239. err = ext4_journal_get_write_access(handle, bh);
  240. if (err)
  241. return err;
  242. ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", block,
  243. block - start, count2);
  244. ext4_set_bits(bh->b_data, block - start, count2);
  245. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  246. if (unlikely(err))
  247. return err;
  248. brelse(bh);
  249. }
  250. return 0;
  251. }
  252. /*
  253. * Set up the block and inode bitmaps, and the inode table for the new groups.
  254. * This doesn't need to be part of the main transaction, since we are only
  255. * changing blocks outside the actual filesystem. We still do journaling to
  256. * ensure the recovery is correct in case of a failure just after resize.
  257. * If any part of this fails, we simply abort the resize.
  258. *
  259. * setup_new_flex_group_blocks handles a flex group as follow:
  260. * 1. copy super block and GDT, and initialize group tables if necessary.
  261. * In this step, we only set bits in blocks bitmaps for blocks taken by
  262. * super block and GDT.
  263. * 2. allocate group tables in block bitmaps, that is, set bits in block
  264. * bitmap for blocks taken by group tables.
  265. */
  266. static int setup_new_flex_group_blocks(struct super_block *sb,
  267. struct ext4_new_flex_group_data *flex_gd)
  268. {
  269. int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
  270. ext4_fsblk_t start;
  271. ext4_fsblk_t block;
  272. struct ext4_sb_info *sbi = EXT4_SB(sb);
  273. struct ext4_super_block *es = sbi->s_es;
  274. struct ext4_new_group_data *group_data = flex_gd->groups;
  275. __u16 *bg_flags = flex_gd->bg_flags;
  276. handle_t *handle;
  277. ext4_group_t group, count;
  278. struct buffer_head *bh = NULL;
  279. int reserved_gdb, i, j, err = 0, err2;
  280. BUG_ON(!flex_gd->count || !group_data ||
  281. group_data[0].group != sbi->s_groups_count);
  282. reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
  283. /* This transaction may be extended/restarted along the way */
  284. handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
  285. if (IS_ERR(handle))
  286. return PTR_ERR(handle);
  287. group = group_data[0].group;
  288. for (i = 0; i < flex_gd->count; i++, group++) {
  289. unsigned long gdblocks;
  290. gdblocks = ext4_bg_num_gdb(sb, group);
  291. start = ext4_group_first_block_no(sb, group);
  292. /* Copy all of the GDT blocks into the backup in this group */
  293. for (j = 0, block = start + 1; j < gdblocks; j++, block++) {
  294. struct buffer_head *gdb;
  295. ext4_debug("update backup group %#04llx\n", block);
  296. err = extend_or_restart_transaction(handle, 1);
  297. if (err)
  298. goto out;
  299. gdb = sb_getblk(sb, block);
  300. if (!gdb) {
  301. err = -EIO;
  302. goto out;
  303. }
  304. err = ext4_journal_get_write_access(handle, gdb);
  305. if (err) {
  306. brelse(gdb);
  307. goto out;
  308. }
  309. memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data,
  310. gdb->b_size);
  311. set_buffer_uptodate(gdb);
  312. err = ext4_handle_dirty_metadata(handle, NULL, gdb);
  313. if (unlikely(err)) {
  314. brelse(gdb);
  315. goto out;
  316. }
  317. brelse(gdb);
  318. }
  319. /* Zero out all of the reserved backup group descriptor
  320. * table blocks
  321. */
  322. if (ext4_bg_has_super(sb, group)) {
  323. err = sb_issue_zeroout(sb, gdblocks + start + 1,
  324. reserved_gdb, GFP_NOFS);
  325. if (err)
  326. goto out;
  327. }
  328. /* Initialize group tables of the grop @group */
  329. if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
  330. goto handle_bb;
  331. /* Zero out all of the inode table blocks */
  332. block = group_data[i].inode_table;
  333. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  334. block, sbi->s_itb_per_group);
  335. err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
  336. GFP_NOFS);
  337. if (err)
  338. goto out;
  339. handle_bb:
  340. if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
  341. goto handle_ib;
  342. /* Initialize block bitmap of the @group */
  343. block = group_data[i].block_bitmap;
  344. err = extend_or_restart_transaction(handle, 1);
  345. if (err)
  346. goto out;
  347. bh = bclean(handle, sb, block);
  348. if (IS_ERR(bh)) {
  349. err = PTR_ERR(bh);
  350. goto out;
  351. }
  352. if (ext4_bg_has_super(sb, group)) {
  353. ext4_debug("mark backup superblock %#04llx (+0)\n",
  354. start);
  355. ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb +
  356. 1);
  357. }
  358. ext4_mark_bitmap_end(group_data[i].blocks_count,
  359. sb->s_blocksize * 8, bh->b_data);
  360. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  361. if (err)
  362. goto out;
  363. brelse(bh);
  364. handle_ib:
  365. if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
  366. continue;
  367. /* Initialize inode bitmap of the @group */
  368. block = group_data[i].inode_bitmap;
  369. err = extend_or_restart_transaction(handle, 1);
  370. if (err)
  371. goto out;
  372. /* Mark unused entries in inode bitmap used */
  373. bh = bclean(handle, sb, block);
  374. if (IS_ERR(bh)) {
  375. err = PTR_ERR(bh);
  376. goto out;
  377. }
  378. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
  379. sb->s_blocksize * 8, bh->b_data);
  380. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  381. if (err)
  382. goto out;
  383. brelse(bh);
  384. }
  385. bh = NULL;
  386. /* Mark group tables in block bitmap */
  387. for (j = 0; j < GROUP_TABLE_COUNT; j++) {
  388. count = group_table_count[j];
  389. start = (&group_data[0].block_bitmap)[j];
  390. block = start;
  391. for (i = 1; i < flex_gd->count; i++) {
  392. block += group_table_count[j];
  393. if (block == (&group_data[i].block_bitmap)[j]) {
  394. count += group_table_count[j];
  395. continue;
  396. }
  397. err = set_flexbg_block_bitmap(sb, handle,
  398. flex_gd, start, count);
  399. if (err)
  400. goto out;
  401. count = group_table_count[j];
  402. start = group_data[i].block_bitmap;
  403. block = start;
  404. }
  405. if (count) {
  406. err = set_flexbg_block_bitmap(sb, handle,
  407. flex_gd, start, count);
  408. if (err)
  409. goto out;
  410. }
  411. }
  412. out:
  413. brelse(bh);
  414. err2 = ext4_journal_stop(handle);
  415. if (err2 && !err)
  416. err = err2;
  417. return err;
  418. }
  419. /*
  420. * Set up the block and inode bitmaps, and the inode table for the new group.
  421. * This doesn't need to be part of the main transaction, since we are only
  422. * changing blocks outside the actual filesystem. We still do journaling to
  423. * ensure the recovery is correct in case of a failure just after resize.
  424. * If any part of this fails, we simply abort the resize.
  425. */
  426. static int setup_new_group_blocks(struct super_block *sb,
  427. struct ext4_new_group_data *input)
  428. {
  429. struct ext4_sb_info *sbi = EXT4_SB(sb);
  430. ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
  431. int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
  432. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
  433. unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
  434. struct buffer_head *bh;
  435. handle_t *handle;
  436. ext4_fsblk_t block;
  437. ext4_grpblk_t bit;
  438. int i;
  439. int err = 0, err2;
  440. /* This transaction may be extended/restarted along the way */
  441. handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
  442. if (IS_ERR(handle))
  443. return PTR_ERR(handle);
  444. BUG_ON(input->group != sbi->s_groups_count);
  445. /* Copy all of the GDT blocks into the backup in this group */
  446. for (i = 0, bit = 1, block = start + 1;
  447. i < gdblocks; i++, block++, bit++) {
  448. struct buffer_head *gdb;
  449. ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
  450. err = extend_or_restart_transaction(handle, 1);
  451. if (err)
  452. goto exit_journal;
  453. gdb = sb_getblk(sb, block);
  454. if (!gdb) {
  455. err = -EIO;
  456. goto exit_journal;
  457. }
  458. if ((err = ext4_journal_get_write_access(handle, gdb))) {
  459. brelse(gdb);
  460. goto exit_journal;
  461. }
  462. memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
  463. set_buffer_uptodate(gdb);
  464. err = ext4_handle_dirty_metadata(handle, NULL, gdb);
  465. if (unlikely(err)) {
  466. brelse(gdb);
  467. goto exit_journal;
  468. }
  469. brelse(gdb);
  470. }
  471. /* Zero out all of the reserved backup group descriptor table blocks */
  472. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  473. block, sbi->s_itb_per_group);
  474. err = sb_issue_zeroout(sb, gdblocks + start + 1, reserved_gdb,
  475. GFP_NOFS);
  476. if (err)
  477. goto exit_journal;
  478. err = extend_or_restart_transaction(handle, 2);
  479. if (err)
  480. goto exit_journal;
  481. bh = bclean(handle, sb, input->block_bitmap);
  482. if (IS_ERR(bh)) {
  483. err = PTR_ERR(bh);
  484. goto exit_journal;
  485. }
  486. if (ext4_bg_has_super(sb, input->group)) {
  487. ext4_debug("mark backup group tables %#04llx (+0)\n", start);
  488. ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb + 1);
  489. }
  490. ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
  491. input->block_bitmap - start);
  492. ext4_set_bit(input->block_bitmap - start, bh->b_data);
  493. ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
  494. input->inode_bitmap - start);
  495. ext4_set_bit(input->inode_bitmap - start, bh->b_data);
  496. /* Zero out all of the inode table blocks */
  497. block = input->inode_table;
  498. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  499. block, sbi->s_itb_per_group);
  500. err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, GFP_NOFS);
  501. if (err)
  502. goto exit_bh;
  503. ext4_set_bits(bh->b_data, input->inode_table - start,
  504. sbi->s_itb_per_group);
  505. ext4_mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8,
  506. bh->b_data);
  507. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  508. if (unlikely(err)) {
  509. ext4_std_error(sb, err);
  510. goto exit_bh;
  511. }
  512. brelse(bh);
  513. /* Mark unused entries in inode bitmap used */
  514. ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
  515. input->inode_bitmap, input->inode_bitmap - start);
  516. if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
  517. err = PTR_ERR(bh);
  518. goto exit_journal;
  519. }
  520. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
  521. bh->b_data);
  522. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  523. if (unlikely(err))
  524. ext4_std_error(sb, err);
  525. exit_bh:
  526. brelse(bh);
  527. exit_journal:
  528. if ((err2 = ext4_journal_stop(handle)) && !err)
  529. err = err2;
  530. return err;
  531. }
  532. /*
  533. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  534. * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
  535. * calling this for the first time. In a sparse filesystem it will be the
  536. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  537. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  538. */
  539. static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
  540. unsigned *five, unsigned *seven)
  541. {
  542. unsigned *min = three;
  543. int mult = 3;
  544. unsigned ret;
  545. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  546. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  547. ret = *min;
  548. *min += 1;
  549. return ret;
  550. }
  551. if (*five < *min) {
  552. min = five;
  553. mult = 5;
  554. }
  555. if (*seven < *min) {
  556. min = seven;
  557. mult = 7;
  558. }
  559. ret = *min;
  560. *min *= mult;
  561. return ret;
  562. }
  563. /*
  564. * Check that all of the backup GDT blocks are held in the primary GDT block.
  565. * It is assumed that they are stored in group order. Returns the number of
  566. * groups in current filesystem that have BACKUPS, or -ve error code.
  567. */
  568. static int verify_reserved_gdb(struct super_block *sb,
  569. ext4_group_t end,
  570. struct buffer_head *primary)
  571. {
  572. const ext4_fsblk_t blk = primary->b_blocknr;
  573. unsigned three = 1;
  574. unsigned five = 5;
  575. unsigned seven = 7;
  576. unsigned grp;
  577. __le32 *p = (__le32 *)primary->b_data;
  578. int gdbackups = 0;
  579. while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
  580. if (le32_to_cpu(*p++) !=
  581. grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
  582. ext4_warning(sb, "reserved GDT %llu"
  583. " missing grp %d (%llu)",
  584. blk, grp,
  585. grp *
  586. (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
  587. blk);
  588. return -EINVAL;
  589. }
  590. if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
  591. return -EFBIG;
  592. }
  593. return gdbackups;
  594. }
  595. /*
  596. * Called when we need to bring a reserved group descriptor table block into
  597. * use from the resize inode. The primary copy of the new GDT block currently
  598. * is an indirect block (under the double indirect block in the resize inode).
  599. * The new backup GDT blocks will be stored as leaf blocks in this indirect
  600. * block, in group order. Even though we know all the block numbers we need,
  601. * we check to ensure that the resize inode has actually reserved these blocks.
  602. *
  603. * Don't need to update the block bitmaps because the blocks are still in use.
  604. *
  605. * We get all of the error cases out of the way, so that we are sure to not
  606. * fail once we start modifying the data on disk, because JBD has no rollback.
  607. */
  608. static int add_new_gdb(handle_t *handle, struct inode *inode,
  609. ext4_group_t group)
  610. {
  611. struct super_block *sb = inode->i_sb;
  612. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  613. unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  614. ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
  615. struct buffer_head **o_group_desc, **n_group_desc;
  616. struct buffer_head *dind;
  617. struct buffer_head *gdb_bh;
  618. int gdbackups;
  619. struct ext4_iloc iloc;
  620. __le32 *data;
  621. int err;
  622. if (test_opt(sb, DEBUG))
  623. printk(KERN_DEBUG
  624. "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
  625. gdb_num);
  626. /*
  627. * If we are not using the primary superblock/GDT copy don't resize,
  628. * because the user tools have no way of handling this. Probably a
  629. * bad time to do it anyways.
  630. */
  631. if (EXT4_SB(sb)->s_sbh->b_blocknr !=
  632. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
  633. ext4_warning(sb, "won't resize using backup superblock at %llu",
  634. (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
  635. return -EPERM;
  636. }
  637. gdb_bh = sb_bread(sb, gdblock);
  638. if (!gdb_bh)
  639. return -EIO;
  640. gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
  641. if (gdbackups < 0) {
  642. err = gdbackups;
  643. goto exit_bh;
  644. }
  645. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  646. dind = sb_bread(sb, le32_to_cpu(*data));
  647. if (!dind) {
  648. err = -EIO;
  649. goto exit_bh;
  650. }
  651. data = (__le32 *)dind->b_data;
  652. if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
  653. ext4_warning(sb, "new group %u GDT block %llu not reserved",
  654. group, gdblock);
  655. err = -EINVAL;
  656. goto exit_dind;
  657. }
  658. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  659. if (unlikely(err))
  660. goto exit_dind;
  661. err = ext4_journal_get_write_access(handle, gdb_bh);
  662. if (unlikely(err))
  663. goto exit_sbh;
  664. err = ext4_journal_get_write_access(handle, dind);
  665. if (unlikely(err))
  666. ext4_std_error(sb, err);
  667. /* ext4_reserve_inode_write() gets a reference on the iloc */
  668. err = ext4_reserve_inode_write(handle, inode, &iloc);
  669. if (unlikely(err))
  670. goto exit_dindj;
  671. n_group_desc = ext4_kvmalloc((gdb_num + 1) *
  672. sizeof(struct buffer_head *),
  673. GFP_NOFS);
  674. if (!n_group_desc) {
  675. err = -ENOMEM;
  676. ext4_warning(sb, "not enough memory for %lu groups",
  677. gdb_num + 1);
  678. goto exit_inode;
  679. }
  680. /*
  681. * Finally, we have all of the possible failures behind us...
  682. *
  683. * Remove new GDT block from inode double-indirect block and clear out
  684. * the new GDT block for use (which also "frees" the backup GDT blocks
  685. * from the reserved inode). We don't need to change the bitmaps for
  686. * these blocks, because they are marked as in-use from being in the
  687. * reserved inode, and will become GDT blocks (primary and backup).
  688. */
  689. data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
  690. err = ext4_handle_dirty_metadata(handle, NULL, dind);
  691. if (unlikely(err)) {
  692. ext4_std_error(sb, err);
  693. goto exit_inode;
  694. }
  695. inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
  696. ext4_mark_iloc_dirty(handle, inode, &iloc);
  697. memset(gdb_bh->b_data, 0, sb->s_blocksize);
  698. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  699. if (unlikely(err)) {
  700. ext4_std_error(sb, err);
  701. goto exit_inode;
  702. }
  703. brelse(dind);
  704. o_group_desc = EXT4_SB(sb)->s_group_desc;
  705. memcpy(n_group_desc, o_group_desc,
  706. EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
  707. n_group_desc[gdb_num] = gdb_bh;
  708. EXT4_SB(sb)->s_group_desc = n_group_desc;
  709. EXT4_SB(sb)->s_gdb_count++;
  710. ext4_kvfree(o_group_desc);
  711. le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
  712. err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
  713. if (err)
  714. ext4_std_error(sb, err);
  715. return err;
  716. exit_inode:
  717. ext4_kvfree(n_group_desc);
  718. /* ext4_handle_release_buffer(handle, iloc.bh); */
  719. brelse(iloc.bh);
  720. exit_dindj:
  721. /* ext4_handle_release_buffer(handle, dind); */
  722. exit_sbh:
  723. /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */
  724. exit_dind:
  725. brelse(dind);
  726. exit_bh:
  727. brelse(gdb_bh);
  728. ext4_debug("leaving with error %d\n", err);
  729. return err;
  730. }
  731. /*
  732. * Called when we are adding a new group which has a backup copy of each of
  733. * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
  734. * We need to add these reserved backup GDT blocks to the resize inode, so
  735. * that they are kept for future resizing and not allocated to files.
  736. *
  737. * Each reserved backup GDT block will go into a different indirect block.
  738. * The indirect blocks are actually the primary reserved GDT blocks,
  739. * so we know in advance what their block numbers are. We only get the
  740. * double-indirect block to verify it is pointing to the primary reserved
  741. * GDT blocks so we don't overwrite a data block by accident. The reserved
  742. * backup GDT blocks are stored in their reserved primary GDT block.
  743. */
  744. static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
  745. ext4_group_t group)
  746. {
  747. struct super_block *sb = inode->i_sb;
  748. int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
  749. struct buffer_head **primary;
  750. struct buffer_head *dind;
  751. struct ext4_iloc iloc;
  752. ext4_fsblk_t blk;
  753. __le32 *data, *end;
  754. int gdbackups = 0;
  755. int res, i;
  756. int err;
  757. primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
  758. if (!primary)
  759. return -ENOMEM;
  760. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  761. dind = sb_bread(sb, le32_to_cpu(*data));
  762. if (!dind) {
  763. err = -EIO;
  764. goto exit_free;
  765. }
  766. blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
  767. data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
  768. EXT4_ADDR_PER_BLOCK(sb));
  769. end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
  770. /* Get each reserved primary GDT block and verify it holds backups */
  771. for (res = 0; res < reserved_gdb; res++, blk++) {
  772. if (le32_to_cpu(*data) != blk) {
  773. ext4_warning(sb, "reserved block %llu"
  774. " not at offset %ld",
  775. blk,
  776. (long)(data - (__le32 *)dind->b_data));
  777. err = -EINVAL;
  778. goto exit_bh;
  779. }
  780. primary[res] = sb_bread(sb, blk);
  781. if (!primary[res]) {
  782. err = -EIO;
  783. goto exit_bh;
  784. }
  785. gdbackups = verify_reserved_gdb(sb, group, primary[res]);
  786. if (gdbackups < 0) {
  787. brelse(primary[res]);
  788. err = gdbackups;
  789. goto exit_bh;
  790. }
  791. if (++data >= end)
  792. data = (__le32 *)dind->b_data;
  793. }
  794. for (i = 0; i < reserved_gdb; i++) {
  795. if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
  796. /*
  797. int j;
  798. for (j = 0; j < i; j++)
  799. ext4_handle_release_buffer(handle, primary[j]);
  800. */
  801. goto exit_bh;
  802. }
  803. }
  804. if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
  805. goto exit_bh;
  806. /*
  807. * Finally we can add each of the reserved backup GDT blocks from
  808. * the new group to its reserved primary GDT block.
  809. */
  810. blk = group * EXT4_BLOCKS_PER_GROUP(sb);
  811. for (i = 0; i < reserved_gdb; i++) {
  812. int err2;
  813. data = (__le32 *)primary[i]->b_data;
  814. /* printk("reserving backup %lu[%u] = %lu\n",
  815. primary[i]->b_blocknr, gdbackups,
  816. blk + primary[i]->b_blocknr); */
  817. data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
  818. err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
  819. if (!err)
  820. err = err2;
  821. }
  822. inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
  823. ext4_mark_iloc_dirty(handle, inode, &iloc);
  824. exit_bh:
  825. while (--res >= 0)
  826. brelse(primary[res]);
  827. brelse(dind);
  828. exit_free:
  829. kfree(primary);
  830. return err;
  831. }
  832. /*
  833. * Update the backup copies of the ext4 metadata. These don't need to be part
  834. * of the main resize transaction, because e2fsck will re-write them if there
  835. * is a problem (basically only OOM will cause a problem). However, we
  836. * _should_ update the backups if possible, in case the primary gets trashed
  837. * for some reason and we need to run e2fsck from a backup superblock. The
  838. * important part is that the new block and inode counts are in the backup
  839. * superblocks, and the location of the new group metadata in the GDT backups.
  840. *
  841. * We do not need take the s_resize_lock for this, because these
  842. * blocks are not otherwise touched by the filesystem code when it is
  843. * mounted. We don't need to worry about last changing from
  844. * sbi->s_groups_count, because the worst that can happen is that we
  845. * do not copy the full number of backups at this time. The resize
  846. * which changed s_groups_count will backup again.
  847. */
  848. static void update_backups(struct super_block *sb,
  849. int blk_off, char *data, int size)
  850. {
  851. struct ext4_sb_info *sbi = EXT4_SB(sb);
  852. const ext4_group_t last = sbi->s_groups_count;
  853. const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
  854. unsigned three = 1;
  855. unsigned five = 5;
  856. unsigned seven = 7;
  857. ext4_group_t group;
  858. int rest = sb->s_blocksize - size;
  859. handle_t *handle;
  860. int err = 0, err2;
  861. handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
  862. if (IS_ERR(handle)) {
  863. group = 1;
  864. err = PTR_ERR(handle);
  865. goto exit_err;
  866. }
  867. while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
  868. struct buffer_head *bh;
  869. /* Out of journal space, and can't get more - abort - so sad */
  870. if (ext4_handle_valid(handle) &&
  871. handle->h_buffer_credits == 0 &&
  872. ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
  873. (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
  874. break;
  875. bh = sb_getblk(sb, group * bpg + blk_off);
  876. if (!bh) {
  877. err = -EIO;
  878. break;
  879. }
  880. ext4_debug("update metadata backup %#04lx\n",
  881. (unsigned long)bh->b_blocknr);
  882. if ((err = ext4_journal_get_write_access(handle, bh)))
  883. break;
  884. lock_buffer(bh);
  885. memcpy(bh->b_data, data, size);
  886. if (rest)
  887. memset(bh->b_data + size, 0, rest);
  888. set_buffer_uptodate(bh);
  889. unlock_buffer(bh);
  890. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  891. if (unlikely(err))
  892. ext4_std_error(sb, err);
  893. brelse(bh);
  894. }
  895. if ((err2 = ext4_journal_stop(handle)) && !err)
  896. err = err2;
  897. /*
  898. * Ugh! Need to have e2fsck write the backup copies. It is too
  899. * late to revert the resize, we shouldn't fail just because of
  900. * the backup copies (they are only needed in case of corruption).
  901. *
  902. * However, if we got here we have a journal problem too, so we
  903. * can't really start a transaction to mark the superblock.
  904. * Chicken out and just set the flag on the hope it will be written
  905. * to disk, and if not - we will simply wait until next fsck.
  906. */
  907. exit_err:
  908. if (err) {
  909. ext4_warning(sb, "can't update backup for group %u (err %d), "
  910. "forcing fsck on next reboot", group, err);
  911. sbi->s_mount_state &= ~EXT4_VALID_FS;
  912. sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  913. mark_buffer_dirty(sbi->s_sbh);
  914. }
  915. }
  916. /*
  917. * ext4_add_new_descs() adds @count group descriptor of groups
  918. * starting at @group
  919. *
  920. * @handle: journal handle
  921. * @sb: super block
  922. * @group: the group no. of the first group desc to be added
  923. * @resize_inode: the resize inode
  924. * @count: number of group descriptors to be added
  925. */
  926. static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
  927. ext4_group_t group, struct inode *resize_inode,
  928. ext4_group_t count)
  929. {
  930. struct ext4_sb_info *sbi = EXT4_SB(sb);
  931. struct ext4_super_block *es = sbi->s_es;
  932. struct buffer_head *gdb_bh;
  933. int i, gdb_off, gdb_num, err = 0;
  934. for (i = 0; i < count; i++, group++) {
  935. int reserved_gdb = ext4_bg_has_super(sb, group) ?
  936. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  937. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  938. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  939. /*
  940. * We will only either add reserved group blocks to a backup group
  941. * or remove reserved blocks for the first group in a new group block.
  942. * Doing both would be mean more complex code, and sane people don't
  943. * use non-sparse filesystems anymore. This is already checked above.
  944. */
  945. if (gdb_off) {
  946. gdb_bh = sbi->s_group_desc[gdb_num];
  947. err = ext4_journal_get_write_access(handle, gdb_bh);
  948. if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
  949. err = reserve_backup_gdb(handle, resize_inode, group);
  950. } else
  951. err = add_new_gdb(handle, resize_inode, group);
  952. if (err)
  953. break;
  954. }
  955. return err;
  956. }
  957. /*
  958. * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
  959. */
  960. static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
  961. struct ext4_new_flex_group_data *flex_gd)
  962. {
  963. struct ext4_new_group_data *group_data = flex_gd->groups;
  964. struct ext4_group_desc *gdp;
  965. struct ext4_sb_info *sbi = EXT4_SB(sb);
  966. struct buffer_head *gdb_bh;
  967. ext4_group_t group;
  968. __u16 *bg_flags = flex_gd->bg_flags;
  969. int i, gdb_off, gdb_num, err = 0;
  970. for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
  971. group = group_data->group;
  972. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  973. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  974. /*
  975. * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
  976. */
  977. gdb_bh = sbi->s_group_desc[gdb_num];
  978. /* Update group descriptor block for new group */
  979. gdp = (struct ext4_group_desc *)((char *)gdb_bh->b_data +
  980. gdb_off * EXT4_DESC_SIZE(sb));
  981. memset(gdp, 0, EXT4_DESC_SIZE(sb));
  982. ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
  983. ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
  984. ext4_inode_table_set(sb, gdp, group_data->inode_table);
  985. ext4_free_group_clusters_set(sb, gdp,
  986. EXT4_B2C(sbi, group_data->free_blocks_count));
  987. ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
  988. gdp->bg_flags = cpu_to_le16(*bg_flags);
  989. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  990. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  991. if (unlikely(err)) {
  992. ext4_std_error(sb, err);
  993. break;
  994. }
  995. /*
  996. * We can allocate memory for mb_alloc based on the new group
  997. * descriptor
  998. */
  999. err = ext4_mb_add_groupinfo(sb, group, gdp);
  1000. if (err)
  1001. break;
  1002. }
  1003. return err;
  1004. }
  1005. /*
  1006. * ext4_update_super() updates the super block so that the newly added
  1007. * groups can be seen by the filesystem.
  1008. *
  1009. * @sb: super block
  1010. * @flex_gd: new added groups
  1011. */
  1012. static void ext4_update_super(struct super_block *sb,
  1013. struct ext4_new_flex_group_data *flex_gd)
  1014. {
  1015. ext4_fsblk_t blocks_count = 0;
  1016. ext4_fsblk_t free_blocks = 0;
  1017. ext4_fsblk_t reserved_blocks = 0;
  1018. struct ext4_new_group_data *group_data = flex_gd->groups;
  1019. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1020. struct ext4_super_block *es = sbi->s_es;
  1021. int i;
  1022. BUG_ON(flex_gd->count == 0 || group_data == NULL);
  1023. /*
  1024. * Make the new blocks and inodes valid next. We do this before
  1025. * increasing the group count so that once the group is enabled,
  1026. * all of its blocks and inodes are already valid.
  1027. *
  1028. * We always allocate group-by-group, then block-by-block or
  1029. * inode-by-inode within a group, so enabling these
  1030. * blocks/inodes before the group is live won't actually let us
  1031. * allocate the new space yet.
  1032. */
  1033. for (i = 0; i < flex_gd->count; i++) {
  1034. blocks_count += group_data[i].blocks_count;
  1035. free_blocks += group_data[i].free_blocks_count;
  1036. }
  1037. reserved_blocks = ext4_r_blocks_count(es) * 100;
  1038. do_div(reserved_blocks, ext4_blocks_count(es));
  1039. reserved_blocks *= blocks_count;
  1040. do_div(reserved_blocks, 100);
  1041. ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
  1042. le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
  1043. flex_gd->count);
  1044. /*
  1045. * We need to protect s_groups_count against other CPUs seeing
  1046. * inconsistent state in the superblock.
  1047. *
  1048. * The precise rules we use are:
  1049. *
  1050. * * Writers must perform a smp_wmb() after updating all
  1051. * dependent data and before modifying the groups count
  1052. *
  1053. * * Readers must perform an smp_rmb() after reading the groups
  1054. * count and before reading any dependent data.
  1055. *
  1056. * NB. These rules can be relaxed when checking the group count
  1057. * while freeing data, as we can only allocate from a block
  1058. * group after serialising against the group count, and we can
  1059. * only then free after serialising in turn against that
  1060. * allocation.
  1061. */
  1062. smp_wmb();
  1063. /* Update the global fs size fields */
  1064. sbi->s_groups_count += flex_gd->count;
  1065. /* Update the reserved block counts only once the new group is
  1066. * active. */
  1067. ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
  1068. reserved_blocks);
  1069. /* Update the free space counts */
  1070. percpu_counter_add(&sbi->s_freeclusters_counter,
  1071. EXT4_B2C(sbi, free_blocks));
  1072. percpu_counter_add(&sbi->s_freeinodes_counter,
  1073. EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
  1074. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  1075. EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1076. sbi->s_log_groups_per_flex) {
  1077. ext4_group_t flex_group;
  1078. flex_group = ext4_flex_group(sbi, group_data[0].group);
  1079. atomic_add(EXT4_B2C(sbi, free_blocks),
  1080. &sbi->s_flex_groups[flex_group].free_clusters);
  1081. atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
  1082. &sbi->s_flex_groups[flex_group].free_inodes);
  1083. }
  1084. if (test_opt(sb, DEBUG))
  1085. printk(KERN_DEBUG "EXT4-fs: added group %u:"
  1086. "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
  1087. blocks_count, free_blocks, reserved_blocks);
  1088. }
  1089. /* Add group descriptor data to an existing or new group descriptor block.
  1090. * Ensure we handle all possible error conditions _before_ we start modifying
  1091. * the filesystem, because we cannot abort the transaction and not have it
  1092. * write the data to disk.
  1093. *
  1094. * If we are on a GDT block boundary, we need to get the reserved GDT block.
  1095. * Otherwise, we may need to add backup GDT blocks for a sparse group.
  1096. *
  1097. * We only need to hold the superblock lock while we are actually adding
  1098. * in the new group's counts to the superblock. Prior to that we have
  1099. * not really "added" the group at all. We re-check that we are still
  1100. * adding in the last group in case things have changed since verifying.
  1101. */
  1102. int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
  1103. {
  1104. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1105. struct ext4_super_block *es = sbi->s_es;
  1106. int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
  1107. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  1108. struct buffer_head *primary = NULL;
  1109. struct ext4_group_desc *gdp;
  1110. struct inode *inode = NULL;
  1111. handle_t *handle;
  1112. int gdb_off, gdb_num;
  1113. int err, err2;
  1114. gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
  1115. gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
  1116. if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1117. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  1118. ext4_warning(sb, "Can't resize non-sparse filesystem further");
  1119. return -EPERM;
  1120. }
  1121. if (ext4_blocks_count(es) + input->blocks_count <
  1122. ext4_blocks_count(es)) {
  1123. ext4_warning(sb, "blocks_count overflow");
  1124. return -EINVAL;
  1125. }
  1126. if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
  1127. le32_to_cpu(es->s_inodes_count)) {
  1128. ext4_warning(sb, "inodes_count overflow");
  1129. return -EINVAL;
  1130. }
  1131. if (reserved_gdb || gdb_off == 0) {
  1132. if (!EXT4_HAS_COMPAT_FEATURE(sb,
  1133. EXT4_FEATURE_COMPAT_RESIZE_INODE)
  1134. || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
  1135. ext4_warning(sb,
  1136. "No reserved GDT blocks, can't resize");
  1137. return -EPERM;
  1138. }
  1139. inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1140. if (IS_ERR(inode)) {
  1141. ext4_warning(sb, "Error opening resize inode");
  1142. return PTR_ERR(inode);
  1143. }
  1144. }
  1145. if ((err = verify_group_input(sb, input)))
  1146. goto exit_put;
  1147. if ((err = setup_new_group_blocks(sb, input)))
  1148. goto exit_put;
  1149. /*
  1150. * We will always be modifying at least the superblock and a GDT
  1151. * block. If we are adding a group past the last current GDT block,
  1152. * we will also modify the inode and the dindirect block. If we
  1153. * are adding a group with superblock/GDT backups we will also
  1154. * modify each of the reserved GDT dindirect blocks.
  1155. */
  1156. handle = ext4_journal_start_sb(sb,
  1157. ext4_bg_has_super(sb, input->group) ?
  1158. 3 + reserved_gdb : 4);
  1159. if (IS_ERR(handle)) {
  1160. err = PTR_ERR(handle);
  1161. goto exit_put;
  1162. }
  1163. if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
  1164. goto exit_journal;
  1165. /*
  1166. * We will only either add reserved group blocks to a backup group
  1167. * or remove reserved blocks for the first group in a new group block.
  1168. * Doing both would be mean more complex code, and sane people don't
  1169. * use non-sparse filesystems anymore. This is already checked above.
  1170. */
  1171. if (gdb_off) {
  1172. primary = sbi->s_group_desc[gdb_num];
  1173. if ((err = ext4_journal_get_write_access(handle, primary)))
  1174. goto exit_journal;
  1175. if (reserved_gdb && ext4_bg_num_gdb(sb, input->group)) {
  1176. err = reserve_backup_gdb(handle, inode, input->group);
  1177. if (err)
  1178. goto exit_journal;
  1179. }
  1180. } else {
  1181. /*
  1182. * Note that we can access new group descriptor block safely
  1183. * only if add_new_gdb() succeeds.
  1184. */
  1185. err = add_new_gdb(handle, inode, input->group);
  1186. if (err)
  1187. goto exit_journal;
  1188. primary = sbi->s_group_desc[gdb_num];
  1189. }
  1190. /*
  1191. * OK, now we've set up the new group. Time to make it active.
  1192. *
  1193. * so we have to be safe wrt. concurrent accesses the group
  1194. * data. So we need to be careful to set all of the relevant
  1195. * group descriptor data etc. *before* we enable the group.
  1196. *
  1197. * The key field here is sbi->s_groups_count: as long as
  1198. * that retains its old value, nobody is going to access the new
  1199. * group.
  1200. *
  1201. * So first we update all the descriptor metadata for the new
  1202. * group; then we update the total disk blocks count; then we
  1203. * update the groups count to enable the group; then finally we
  1204. * update the free space counts so that the system can start
  1205. * using the new disk blocks.
  1206. */
  1207. /* Update group descriptor block for new group */
  1208. gdp = (struct ext4_group_desc *)((char *)primary->b_data +
  1209. gdb_off * EXT4_DESC_SIZE(sb));
  1210. memset(gdp, 0, EXT4_DESC_SIZE(sb));
  1211. ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
  1212. ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
  1213. ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
  1214. ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count);
  1215. ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
  1216. gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1217. gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
  1218. /*
  1219. * We can allocate memory for mb_alloc based on the new group
  1220. * descriptor
  1221. */
  1222. err = ext4_mb_add_groupinfo(sb, input->group, gdp);
  1223. if (err)
  1224. goto exit_journal;
  1225. /*
  1226. * Make the new blocks and inodes valid next. We do this before
  1227. * increasing the group count so that once the group is enabled,
  1228. * all of its blocks and inodes are already valid.
  1229. *
  1230. * We always allocate group-by-group, then block-by-block or
  1231. * inode-by-inode within a group, so enabling these
  1232. * blocks/inodes before the group is live won't actually let us
  1233. * allocate the new space yet.
  1234. */
  1235. ext4_blocks_count_set(es, ext4_blocks_count(es) +
  1236. input->blocks_count);
  1237. le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
  1238. /*
  1239. * We need to protect s_groups_count against other CPUs seeing
  1240. * inconsistent state in the superblock.
  1241. *
  1242. * The precise rules we use are:
  1243. *
  1244. * * Writers must perform a smp_wmb() after updating all dependent
  1245. * data and before modifying the groups count
  1246. *
  1247. * * Readers must perform an smp_rmb() after reading the groups count
  1248. * and before reading any dependent data.
  1249. *
  1250. * NB. These rules can be relaxed when checking the group count
  1251. * while freeing data, as we can only allocate from a block
  1252. * group after serialising against the group count, and we can
  1253. * only then free after serialising in turn against that
  1254. * allocation.
  1255. */
  1256. smp_wmb();
  1257. /* Update the global fs size fields */
  1258. sbi->s_groups_count++;
  1259. err = ext4_handle_dirty_metadata(handle, NULL, primary);
  1260. if (unlikely(err)) {
  1261. ext4_std_error(sb, err);
  1262. goto exit_journal;
  1263. }
  1264. /* Update the reserved block counts only once the new group is
  1265. * active. */
  1266. ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
  1267. input->reserved_blocks);
  1268. /* Update the free space counts */
  1269. percpu_counter_add(&sbi->s_freeclusters_counter,
  1270. EXT4_B2C(sbi, input->free_blocks_count));
  1271. percpu_counter_add(&sbi->s_freeinodes_counter,
  1272. EXT4_INODES_PER_GROUP(sb));
  1273. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1274. sbi->s_log_groups_per_flex) {
  1275. ext4_group_t flex_group;
  1276. flex_group = ext4_flex_group(sbi, input->group);
  1277. atomic_add(EXT4_B2C(sbi, input->free_blocks_count),
  1278. &sbi->s_flex_groups[flex_group].free_clusters);
  1279. atomic_add(EXT4_INODES_PER_GROUP(sb),
  1280. &sbi->s_flex_groups[flex_group].free_inodes);
  1281. }
  1282. ext4_handle_dirty_super(handle, sb);
  1283. exit_journal:
  1284. if ((err2 = ext4_journal_stop(handle)) && !err)
  1285. err = err2;
  1286. if (!err && primary) {
  1287. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  1288. sizeof(struct ext4_super_block));
  1289. update_backups(sb, primary->b_blocknr, primary->b_data,
  1290. primary->b_size);
  1291. }
  1292. exit_put:
  1293. iput(inode);
  1294. return err;
  1295. } /* ext4_group_add */
  1296. /*
  1297. * extend a group without checking assuming that checking has been done.
  1298. */
  1299. static int ext4_group_extend_no_check(struct super_block *sb,
  1300. ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
  1301. {
  1302. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  1303. handle_t *handle;
  1304. int err = 0, err2;
  1305. /* We will update the superblock, one block bitmap, and
  1306. * one group descriptor via ext4_group_add_blocks().
  1307. */
  1308. handle = ext4_journal_start_sb(sb, 3);
  1309. if (IS_ERR(handle)) {
  1310. err = PTR_ERR(handle);
  1311. ext4_warning(sb, "error %d on journal start", err);
  1312. return err;
  1313. }
  1314. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1315. if (err) {
  1316. ext4_warning(sb, "error %d on journal write access", err);
  1317. goto errout;
  1318. }
  1319. ext4_blocks_count_set(es, o_blocks_count + add);
  1320. ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
  1321. o_blocks_count + add);
  1322. /* We add the blocks to the bitmap and set the group need init bit */
  1323. err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
  1324. if (err)
  1325. goto errout;
  1326. ext4_handle_dirty_super(handle, sb);
  1327. ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
  1328. o_blocks_count + add);
  1329. errout:
  1330. err2 = ext4_journal_stop(handle);
  1331. if (err2 && !err)
  1332. err = err2;
  1333. if (!err) {
  1334. if (test_opt(sb, DEBUG))
  1335. printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
  1336. "blocks\n", ext4_blocks_count(es));
  1337. update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
  1338. sizeof(struct ext4_super_block));
  1339. }
  1340. return err;
  1341. }
  1342. /*
  1343. * Extend the filesystem to the new number of blocks specified. This entry
  1344. * point is only used to extend the current filesystem to the end of the last
  1345. * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
  1346. * for emergencies (because it has no dependencies on reserved blocks).
  1347. *
  1348. * If we _really_ wanted, we could use default values to call ext4_group_add()
  1349. * allow the "remount" trick to work for arbitrary resizing, assuming enough
  1350. * GDT blocks are reserved to grow to the desired size.
  1351. */
  1352. int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
  1353. ext4_fsblk_t n_blocks_count)
  1354. {
  1355. ext4_fsblk_t o_blocks_count;
  1356. ext4_grpblk_t last;
  1357. ext4_grpblk_t add;
  1358. struct buffer_head *bh;
  1359. handle_t *handle;
  1360. int err, err2;
  1361. ext4_group_t group;
  1362. o_blocks_count = ext4_blocks_count(es);
  1363. if (test_opt(sb, DEBUG))
  1364. printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n",
  1365. o_blocks_count, n_blocks_count);
  1366. if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
  1367. return 0;
  1368. if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  1369. printk(KERN_ERR "EXT4-fs: filesystem on %s:"
  1370. " too large to resize to %llu blocks safely\n",
  1371. sb->s_id, n_blocks_count);
  1372. if (sizeof(sector_t) < 8)
  1373. ext4_warning(sb, "CONFIG_LBDAF not enabled");
  1374. return -EINVAL;
  1375. }
  1376. if (n_blocks_count < o_blocks_count) {
  1377. ext4_warning(sb, "can't shrink FS - resize aborted");
  1378. return -EINVAL;
  1379. }
  1380. /* Handle the remaining blocks in the last group only. */
  1381. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1382. if (last == 0) {
  1383. ext4_warning(sb, "need to use ext2online to resize further");
  1384. return -EPERM;
  1385. }
  1386. add = EXT4_BLOCKS_PER_GROUP(sb) - last;
  1387. if (o_blocks_count + add < o_blocks_count) {
  1388. ext4_warning(sb, "blocks_count overflow");
  1389. return -EINVAL;
  1390. }
  1391. if (o_blocks_count + add > n_blocks_count)
  1392. add = n_blocks_count - o_blocks_count;
  1393. if (o_blocks_count + add < n_blocks_count)
  1394. ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
  1395. o_blocks_count + add, add);
  1396. /* See if the device is actually as big as what was requested */
  1397. bh = sb_bread(sb, o_blocks_count + add - 1);
  1398. if (!bh) {
  1399. ext4_warning(sb, "can't read last block, resize aborted");
  1400. return -ENOSPC;
  1401. }
  1402. brelse(bh);
  1403. /* We will update the superblock, one block bitmap, and
  1404. * one group descriptor via ext4_free_blocks().
  1405. */
  1406. handle = ext4_journal_start_sb(sb, 3);
  1407. if (IS_ERR(handle)) {
  1408. err = PTR_ERR(handle);
  1409. ext4_warning(sb, "error %d on journal start", err);
  1410. goto exit_put;
  1411. }
  1412. if ((err = ext4_journal_get_write_access(handle,
  1413. EXT4_SB(sb)->s_sbh))) {
  1414. ext4_warning(sb, "error %d on journal write access", err);
  1415. ext4_journal_stop(handle);
  1416. goto exit_put;
  1417. }
  1418. ext4_blocks_count_set(es, o_blocks_count + add);
  1419. ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
  1420. o_blocks_count + add);
  1421. /* We add the blocks to the bitmap and set the group need init bit */
  1422. err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
  1423. ext4_handle_dirty_super(handle, sb);
  1424. ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
  1425. o_blocks_count + add);
  1426. err2 = ext4_journal_stop(handle);
  1427. if (!err && err2)
  1428. err = err2;
  1429. if (err)
  1430. goto exit_put;
  1431. if (test_opt(sb, DEBUG))
  1432. printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
  1433. ext4_blocks_count(es));
  1434. update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
  1435. sizeof(struct ext4_super_block));
  1436. exit_put:
  1437. return err;
  1438. } /* ext4_group_extend */