resize.c 49 KB

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