resize.c 57 KB

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