resize.c 57 KB

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