resize.c 58 KB

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