resize.c 64 KB

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