resize.c 57 KB

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