resize.c 61 KB

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