resize.c 49 KB

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