resize.c 59 KB

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