resize.c 59 KB

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