resize.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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. * Set up the block and inode bitmaps, and the inode table for the new group.
  518. * This doesn't need to be part of the main transaction, since we are only
  519. * changing blocks outside the actual filesystem. We still do journaling to
  520. * ensure the recovery is correct in case of a failure just after resize.
  521. * If any part of this fails, we simply abort the resize.
  522. */
  523. static int setup_new_group_blocks(struct super_block *sb,
  524. struct ext4_new_group_data *input)
  525. {
  526. struct ext4_sb_info *sbi = EXT4_SB(sb);
  527. ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
  528. int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
  529. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
  530. unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
  531. struct buffer_head *bh;
  532. handle_t *handle;
  533. ext4_fsblk_t block;
  534. ext4_grpblk_t bit;
  535. int i;
  536. int err = 0, err2;
  537. /* This transaction may be extended/restarted along the way */
  538. handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
  539. if (IS_ERR(handle))
  540. return PTR_ERR(handle);
  541. BUG_ON(input->group != sbi->s_groups_count);
  542. /* Copy all of the GDT blocks into the backup in this group */
  543. for (i = 0, bit = 1, block = start + 1;
  544. i < gdblocks; i++, block++, bit++) {
  545. struct buffer_head *gdb;
  546. ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
  547. err = extend_or_restart_transaction(handle, 1);
  548. if (err)
  549. goto exit_journal;
  550. gdb = sb_getblk(sb, block);
  551. if (!gdb) {
  552. err = -EIO;
  553. goto exit_journal;
  554. }
  555. if ((err = ext4_journal_get_write_access(handle, gdb))) {
  556. brelse(gdb);
  557. goto exit_journal;
  558. }
  559. memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
  560. set_buffer_uptodate(gdb);
  561. err = ext4_handle_dirty_metadata(handle, NULL, gdb);
  562. if (unlikely(err)) {
  563. brelse(gdb);
  564. goto exit_journal;
  565. }
  566. brelse(gdb);
  567. }
  568. /* Zero out all of the reserved backup group descriptor table blocks */
  569. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  570. block, sbi->s_itb_per_group);
  571. err = sb_issue_zeroout(sb, gdblocks + start + 1, reserved_gdb,
  572. GFP_NOFS);
  573. if (err)
  574. goto exit_journal;
  575. err = extend_or_restart_transaction(handle, 2);
  576. if (err)
  577. goto exit_journal;
  578. bh = bclean(handle, sb, input->block_bitmap);
  579. if (IS_ERR(bh)) {
  580. err = PTR_ERR(bh);
  581. goto exit_journal;
  582. }
  583. if (ext4_bg_has_super(sb, input->group)) {
  584. ext4_debug("mark backup group tables %#04llx (+0)\n", start);
  585. ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb + 1);
  586. }
  587. ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
  588. input->block_bitmap - start);
  589. ext4_set_bit(input->block_bitmap - start, bh->b_data);
  590. ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
  591. input->inode_bitmap - start);
  592. ext4_set_bit(input->inode_bitmap - start, bh->b_data);
  593. /* Zero out all of the inode table blocks */
  594. block = input->inode_table;
  595. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  596. block, sbi->s_itb_per_group);
  597. err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, GFP_NOFS);
  598. if (err)
  599. goto exit_bh;
  600. ext4_set_bits(bh->b_data, input->inode_table - start,
  601. sbi->s_itb_per_group);
  602. ext4_mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8,
  603. bh->b_data);
  604. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  605. if (unlikely(err)) {
  606. ext4_std_error(sb, err);
  607. goto exit_bh;
  608. }
  609. brelse(bh);
  610. /* Mark unused entries in inode bitmap used */
  611. ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
  612. input->inode_bitmap, input->inode_bitmap - start);
  613. if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
  614. err = PTR_ERR(bh);
  615. goto exit_journal;
  616. }
  617. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
  618. bh->b_data);
  619. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  620. if (unlikely(err))
  621. ext4_std_error(sb, err);
  622. exit_bh:
  623. brelse(bh);
  624. exit_journal:
  625. if ((err2 = ext4_journal_stop(handle)) && !err)
  626. err = err2;
  627. return err;
  628. }
  629. /*
  630. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  631. * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
  632. * calling this for the first time. In a sparse filesystem it will be the
  633. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  634. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  635. */
  636. static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
  637. unsigned *five, unsigned *seven)
  638. {
  639. unsigned *min = three;
  640. int mult = 3;
  641. unsigned ret;
  642. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  643. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  644. ret = *min;
  645. *min += 1;
  646. return ret;
  647. }
  648. if (*five < *min) {
  649. min = five;
  650. mult = 5;
  651. }
  652. if (*seven < *min) {
  653. min = seven;
  654. mult = 7;
  655. }
  656. ret = *min;
  657. *min *= mult;
  658. return ret;
  659. }
  660. /*
  661. * Check that all of the backup GDT blocks are held in the primary GDT block.
  662. * It is assumed that they are stored in group order. Returns the number of
  663. * groups in current filesystem that have BACKUPS, or -ve error code.
  664. */
  665. static int verify_reserved_gdb(struct super_block *sb,
  666. ext4_group_t end,
  667. struct buffer_head *primary)
  668. {
  669. const ext4_fsblk_t blk = primary->b_blocknr;
  670. unsigned three = 1;
  671. unsigned five = 5;
  672. unsigned seven = 7;
  673. unsigned grp;
  674. __le32 *p = (__le32 *)primary->b_data;
  675. int gdbackups = 0;
  676. while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
  677. if (le32_to_cpu(*p++) !=
  678. grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
  679. ext4_warning(sb, "reserved GDT %llu"
  680. " missing grp %d (%llu)",
  681. blk, grp,
  682. grp *
  683. (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
  684. blk);
  685. return -EINVAL;
  686. }
  687. if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
  688. return -EFBIG;
  689. }
  690. return gdbackups;
  691. }
  692. /*
  693. * Called when we need to bring a reserved group descriptor table block into
  694. * use from the resize inode. The primary copy of the new GDT block currently
  695. * is an indirect block (under the double indirect block in the resize inode).
  696. * The new backup GDT blocks will be stored as leaf blocks in this indirect
  697. * block, in group order. Even though we know all the block numbers we need,
  698. * we check to ensure that the resize inode has actually reserved these blocks.
  699. *
  700. * Don't need to update the block bitmaps because the blocks are still in use.
  701. *
  702. * We get all of the error cases out of the way, so that we are sure to not
  703. * fail once we start modifying the data on disk, because JBD has no rollback.
  704. */
  705. static int add_new_gdb(handle_t *handle, struct inode *inode,
  706. ext4_group_t group)
  707. {
  708. struct super_block *sb = inode->i_sb;
  709. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  710. unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  711. ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
  712. struct buffer_head **o_group_desc, **n_group_desc;
  713. struct buffer_head *dind;
  714. struct buffer_head *gdb_bh;
  715. int gdbackups;
  716. struct ext4_iloc iloc;
  717. __le32 *data;
  718. int err;
  719. if (test_opt(sb, DEBUG))
  720. printk(KERN_DEBUG
  721. "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
  722. gdb_num);
  723. /*
  724. * If we are not using the primary superblock/GDT copy don't resize,
  725. * because the user tools have no way of handling this. Probably a
  726. * bad time to do it anyways.
  727. */
  728. if (EXT4_SB(sb)->s_sbh->b_blocknr !=
  729. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
  730. ext4_warning(sb, "won't resize using backup superblock at %llu",
  731. (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
  732. return -EPERM;
  733. }
  734. gdb_bh = sb_bread(sb, gdblock);
  735. if (!gdb_bh)
  736. return -EIO;
  737. gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
  738. if (gdbackups < 0) {
  739. err = gdbackups;
  740. goto exit_bh;
  741. }
  742. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  743. dind = sb_bread(sb, le32_to_cpu(*data));
  744. if (!dind) {
  745. err = -EIO;
  746. goto exit_bh;
  747. }
  748. data = (__le32 *)dind->b_data;
  749. if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
  750. ext4_warning(sb, "new group %u GDT block %llu not reserved",
  751. group, gdblock);
  752. err = -EINVAL;
  753. goto exit_dind;
  754. }
  755. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  756. if (unlikely(err))
  757. goto exit_dind;
  758. err = ext4_journal_get_write_access(handle, gdb_bh);
  759. if (unlikely(err))
  760. goto exit_sbh;
  761. err = ext4_journal_get_write_access(handle, dind);
  762. if (unlikely(err))
  763. ext4_std_error(sb, err);
  764. /* ext4_reserve_inode_write() gets a reference on the iloc */
  765. err = ext4_reserve_inode_write(handle, inode, &iloc);
  766. if (unlikely(err))
  767. goto exit_dindj;
  768. n_group_desc = ext4_kvmalloc((gdb_num + 1) *
  769. sizeof(struct buffer_head *),
  770. GFP_NOFS);
  771. if (!n_group_desc) {
  772. err = -ENOMEM;
  773. ext4_warning(sb, "not enough memory for %lu groups",
  774. gdb_num + 1);
  775. goto exit_inode;
  776. }
  777. /*
  778. * Finally, we have all of the possible failures behind us...
  779. *
  780. * Remove new GDT block from inode double-indirect block and clear out
  781. * the new GDT block for use (which also "frees" the backup GDT blocks
  782. * from the reserved inode). We don't need to change the bitmaps for
  783. * these blocks, because they are marked as in-use from being in the
  784. * reserved inode, and will become GDT blocks (primary and backup).
  785. */
  786. data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
  787. err = ext4_handle_dirty_metadata(handle, NULL, dind);
  788. if (unlikely(err)) {
  789. ext4_std_error(sb, err);
  790. goto exit_inode;
  791. }
  792. inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
  793. ext4_mark_iloc_dirty(handle, inode, &iloc);
  794. memset(gdb_bh->b_data, 0, sb->s_blocksize);
  795. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  796. if (unlikely(err)) {
  797. ext4_std_error(sb, err);
  798. goto exit_inode;
  799. }
  800. brelse(dind);
  801. o_group_desc = EXT4_SB(sb)->s_group_desc;
  802. memcpy(n_group_desc, o_group_desc,
  803. EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
  804. n_group_desc[gdb_num] = gdb_bh;
  805. EXT4_SB(sb)->s_group_desc = n_group_desc;
  806. EXT4_SB(sb)->s_gdb_count++;
  807. ext4_kvfree(o_group_desc);
  808. le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
  809. err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
  810. if (err)
  811. ext4_std_error(sb, err);
  812. return err;
  813. exit_inode:
  814. ext4_kvfree(n_group_desc);
  815. /* ext4_handle_release_buffer(handle, iloc.bh); */
  816. brelse(iloc.bh);
  817. exit_dindj:
  818. /* ext4_handle_release_buffer(handle, dind); */
  819. exit_sbh:
  820. /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */
  821. exit_dind:
  822. brelse(dind);
  823. exit_bh:
  824. brelse(gdb_bh);
  825. ext4_debug("leaving with error %d\n", err);
  826. return err;
  827. }
  828. /*
  829. * Called when we are adding a new group which has a backup copy of each of
  830. * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
  831. * We need to add these reserved backup GDT blocks to the resize inode, so
  832. * that they are kept for future resizing and not allocated to files.
  833. *
  834. * Each reserved backup GDT block will go into a different indirect block.
  835. * The indirect blocks are actually the primary reserved GDT blocks,
  836. * so we know in advance what their block numbers are. We only get the
  837. * double-indirect block to verify it is pointing to the primary reserved
  838. * GDT blocks so we don't overwrite a data block by accident. The reserved
  839. * backup GDT blocks are stored in their reserved primary GDT block.
  840. */
  841. static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
  842. ext4_group_t group)
  843. {
  844. struct super_block *sb = inode->i_sb;
  845. int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
  846. struct buffer_head **primary;
  847. struct buffer_head *dind;
  848. struct ext4_iloc iloc;
  849. ext4_fsblk_t blk;
  850. __le32 *data, *end;
  851. int gdbackups = 0;
  852. int res, i;
  853. int err;
  854. primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
  855. if (!primary)
  856. return -ENOMEM;
  857. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  858. dind = sb_bread(sb, le32_to_cpu(*data));
  859. if (!dind) {
  860. err = -EIO;
  861. goto exit_free;
  862. }
  863. blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
  864. data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
  865. EXT4_ADDR_PER_BLOCK(sb));
  866. end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
  867. /* Get each reserved primary GDT block and verify it holds backups */
  868. for (res = 0; res < reserved_gdb; res++, blk++) {
  869. if (le32_to_cpu(*data) != blk) {
  870. ext4_warning(sb, "reserved block %llu"
  871. " not at offset %ld",
  872. blk,
  873. (long)(data - (__le32 *)dind->b_data));
  874. err = -EINVAL;
  875. goto exit_bh;
  876. }
  877. primary[res] = sb_bread(sb, blk);
  878. if (!primary[res]) {
  879. err = -EIO;
  880. goto exit_bh;
  881. }
  882. gdbackups = verify_reserved_gdb(sb, group, primary[res]);
  883. if (gdbackups < 0) {
  884. brelse(primary[res]);
  885. err = gdbackups;
  886. goto exit_bh;
  887. }
  888. if (++data >= end)
  889. data = (__le32 *)dind->b_data;
  890. }
  891. for (i = 0; i < reserved_gdb; i++) {
  892. if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
  893. /*
  894. int j;
  895. for (j = 0; j < i; j++)
  896. ext4_handle_release_buffer(handle, primary[j]);
  897. */
  898. goto exit_bh;
  899. }
  900. }
  901. if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
  902. goto exit_bh;
  903. /*
  904. * Finally we can add each of the reserved backup GDT blocks from
  905. * the new group to its reserved primary GDT block.
  906. */
  907. blk = group * EXT4_BLOCKS_PER_GROUP(sb);
  908. for (i = 0; i < reserved_gdb; i++) {
  909. int err2;
  910. data = (__le32 *)primary[i]->b_data;
  911. /* printk("reserving backup %lu[%u] = %lu\n",
  912. primary[i]->b_blocknr, gdbackups,
  913. blk + primary[i]->b_blocknr); */
  914. data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
  915. err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
  916. if (!err)
  917. err = err2;
  918. }
  919. inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
  920. ext4_mark_iloc_dirty(handle, inode, &iloc);
  921. exit_bh:
  922. while (--res >= 0)
  923. brelse(primary[res]);
  924. brelse(dind);
  925. exit_free:
  926. kfree(primary);
  927. return err;
  928. }
  929. /*
  930. * Update the backup copies of the ext4 metadata. These don't need to be part
  931. * of the main resize transaction, because e2fsck will re-write them if there
  932. * is a problem (basically only OOM will cause a problem). However, we
  933. * _should_ update the backups if possible, in case the primary gets trashed
  934. * for some reason and we need to run e2fsck from a backup superblock. The
  935. * important part is that the new block and inode counts are in the backup
  936. * superblocks, and the location of the new group metadata in the GDT backups.
  937. *
  938. * We do not need take the s_resize_lock for this, because these
  939. * blocks are not otherwise touched by the filesystem code when it is
  940. * mounted. We don't need to worry about last changing from
  941. * sbi->s_groups_count, because the worst that can happen is that we
  942. * do not copy the full number of backups at this time. The resize
  943. * which changed s_groups_count will backup again.
  944. */
  945. static void update_backups(struct super_block *sb,
  946. int blk_off, char *data, int size)
  947. {
  948. struct ext4_sb_info *sbi = EXT4_SB(sb);
  949. const ext4_group_t last = sbi->s_groups_count;
  950. const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
  951. unsigned three = 1;
  952. unsigned five = 5;
  953. unsigned seven = 7;
  954. ext4_group_t group;
  955. int rest = sb->s_blocksize - size;
  956. handle_t *handle;
  957. int err = 0, err2;
  958. handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
  959. if (IS_ERR(handle)) {
  960. group = 1;
  961. err = PTR_ERR(handle);
  962. goto exit_err;
  963. }
  964. while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
  965. struct buffer_head *bh;
  966. /* Out of journal space, and can't get more - abort - so sad */
  967. if (ext4_handle_valid(handle) &&
  968. handle->h_buffer_credits == 0 &&
  969. ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
  970. (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
  971. break;
  972. bh = sb_getblk(sb, group * bpg + blk_off);
  973. if (!bh) {
  974. err = -EIO;
  975. break;
  976. }
  977. ext4_debug("update metadata backup %#04lx\n",
  978. (unsigned long)bh->b_blocknr);
  979. if ((err = ext4_journal_get_write_access(handle, bh)))
  980. break;
  981. lock_buffer(bh);
  982. memcpy(bh->b_data, data, size);
  983. if (rest)
  984. memset(bh->b_data + size, 0, rest);
  985. set_buffer_uptodate(bh);
  986. unlock_buffer(bh);
  987. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  988. if (unlikely(err))
  989. ext4_std_error(sb, err);
  990. brelse(bh);
  991. }
  992. if ((err2 = ext4_journal_stop(handle)) && !err)
  993. err = err2;
  994. /*
  995. * Ugh! Need to have e2fsck write the backup copies. It is too
  996. * late to revert the resize, we shouldn't fail just because of
  997. * the backup copies (they are only needed in case of corruption).
  998. *
  999. * However, if we got here we have a journal problem too, so we
  1000. * can't really start a transaction to mark the superblock.
  1001. * Chicken out and just set the flag on the hope it will be written
  1002. * to disk, and if not - we will simply wait until next fsck.
  1003. */
  1004. exit_err:
  1005. if (err) {
  1006. ext4_warning(sb, "can't update backup for group %u (err %d), "
  1007. "forcing fsck on next reboot", group, err);
  1008. sbi->s_mount_state &= ~EXT4_VALID_FS;
  1009. sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  1010. mark_buffer_dirty(sbi->s_sbh);
  1011. }
  1012. }
  1013. /*
  1014. * ext4_add_new_descs() adds @count group descriptor of groups
  1015. * starting at @group
  1016. *
  1017. * @handle: journal handle
  1018. * @sb: super block
  1019. * @group: the group no. of the first group desc to be added
  1020. * @resize_inode: the resize inode
  1021. * @count: number of group descriptors to be added
  1022. */
  1023. static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
  1024. ext4_group_t group, struct inode *resize_inode,
  1025. ext4_group_t count)
  1026. {
  1027. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1028. struct ext4_super_block *es = sbi->s_es;
  1029. struct buffer_head *gdb_bh;
  1030. int i, gdb_off, gdb_num, err = 0;
  1031. for (i = 0; i < count; i++, group++) {
  1032. int reserved_gdb = ext4_bg_has_super(sb, group) ?
  1033. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  1034. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  1035. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1036. /*
  1037. * We will only either add reserved group blocks to a backup group
  1038. * or remove reserved blocks for the first group in a new group block.
  1039. * Doing both would be mean more complex code, and sane people don't
  1040. * use non-sparse filesystems anymore. This is already checked above.
  1041. */
  1042. if (gdb_off) {
  1043. gdb_bh = sbi->s_group_desc[gdb_num];
  1044. err = ext4_journal_get_write_access(handle, gdb_bh);
  1045. if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
  1046. err = reserve_backup_gdb(handle, resize_inode, group);
  1047. } else
  1048. err = add_new_gdb(handle, resize_inode, group);
  1049. if (err)
  1050. break;
  1051. }
  1052. return err;
  1053. }
  1054. /*
  1055. * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
  1056. */
  1057. static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
  1058. struct ext4_new_flex_group_data *flex_gd)
  1059. {
  1060. struct ext4_new_group_data *group_data = flex_gd->groups;
  1061. struct ext4_group_desc *gdp;
  1062. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1063. struct buffer_head *gdb_bh;
  1064. ext4_group_t group;
  1065. __u16 *bg_flags = flex_gd->bg_flags;
  1066. int i, gdb_off, gdb_num, err = 0;
  1067. for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
  1068. group = group_data->group;
  1069. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  1070. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1071. /*
  1072. * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
  1073. */
  1074. gdb_bh = sbi->s_group_desc[gdb_num];
  1075. /* Update group descriptor block for new group */
  1076. gdp = (struct ext4_group_desc *)((char *)gdb_bh->b_data +
  1077. gdb_off * EXT4_DESC_SIZE(sb));
  1078. memset(gdp, 0, EXT4_DESC_SIZE(sb));
  1079. ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
  1080. ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
  1081. ext4_inode_table_set(sb, gdp, group_data->inode_table);
  1082. ext4_free_group_clusters_set(sb, gdp,
  1083. EXT4_B2C(sbi, group_data->free_blocks_count));
  1084. ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
  1085. gdp->bg_flags = cpu_to_le16(*bg_flags);
  1086. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  1087. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  1088. if (unlikely(err)) {
  1089. ext4_std_error(sb, err);
  1090. break;
  1091. }
  1092. /*
  1093. * We can allocate memory for mb_alloc based on the new group
  1094. * descriptor
  1095. */
  1096. err = ext4_mb_add_groupinfo(sb, group, gdp);
  1097. if (err)
  1098. break;
  1099. }
  1100. return err;
  1101. }
  1102. /*
  1103. * ext4_update_super() updates the super block so that the newly added
  1104. * groups can be seen by the filesystem.
  1105. *
  1106. * @sb: super block
  1107. * @flex_gd: new added groups
  1108. */
  1109. static void ext4_update_super(struct super_block *sb,
  1110. struct ext4_new_flex_group_data *flex_gd)
  1111. {
  1112. ext4_fsblk_t blocks_count = 0;
  1113. ext4_fsblk_t free_blocks = 0;
  1114. ext4_fsblk_t reserved_blocks = 0;
  1115. struct ext4_new_group_data *group_data = flex_gd->groups;
  1116. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1117. struct ext4_super_block *es = sbi->s_es;
  1118. int i;
  1119. BUG_ON(flex_gd->count == 0 || group_data == NULL);
  1120. /*
  1121. * Make the new blocks and inodes valid next. We do this before
  1122. * increasing the group count so that once the group is enabled,
  1123. * all of its blocks and inodes are already valid.
  1124. *
  1125. * We always allocate group-by-group, then block-by-block or
  1126. * inode-by-inode within a group, so enabling these
  1127. * blocks/inodes before the group is live won't actually let us
  1128. * allocate the new space yet.
  1129. */
  1130. for (i = 0; i < flex_gd->count; i++) {
  1131. blocks_count += group_data[i].blocks_count;
  1132. free_blocks += group_data[i].free_blocks_count;
  1133. }
  1134. reserved_blocks = ext4_r_blocks_count(es) * 100;
  1135. do_div(reserved_blocks, ext4_blocks_count(es));
  1136. reserved_blocks *= blocks_count;
  1137. do_div(reserved_blocks, 100);
  1138. ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
  1139. le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
  1140. flex_gd->count);
  1141. /*
  1142. * We need to protect s_groups_count against other CPUs seeing
  1143. * inconsistent state in the superblock.
  1144. *
  1145. * The precise rules we use are:
  1146. *
  1147. * * Writers must perform a smp_wmb() after updating all
  1148. * dependent data and before modifying the groups count
  1149. *
  1150. * * Readers must perform an smp_rmb() after reading the groups
  1151. * count and before reading any dependent data.
  1152. *
  1153. * NB. These rules can be relaxed when checking the group count
  1154. * while freeing data, as we can only allocate from a block
  1155. * group after serialising against the group count, and we can
  1156. * only then free after serialising in turn against that
  1157. * allocation.
  1158. */
  1159. smp_wmb();
  1160. /* Update the global fs size fields */
  1161. sbi->s_groups_count += flex_gd->count;
  1162. /* Update the reserved block counts only once the new group is
  1163. * active. */
  1164. ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
  1165. reserved_blocks);
  1166. /* Update the free space counts */
  1167. percpu_counter_add(&sbi->s_freeclusters_counter,
  1168. EXT4_B2C(sbi, free_blocks));
  1169. percpu_counter_add(&sbi->s_freeinodes_counter,
  1170. EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
  1171. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  1172. EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1173. sbi->s_log_groups_per_flex) {
  1174. ext4_group_t flex_group;
  1175. flex_group = ext4_flex_group(sbi, group_data[0].group);
  1176. atomic_add(EXT4_B2C(sbi, free_blocks),
  1177. &sbi->s_flex_groups[flex_group].free_clusters);
  1178. atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
  1179. &sbi->s_flex_groups[flex_group].free_inodes);
  1180. }
  1181. if (test_opt(sb, DEBUG))
  1182. printk(KERN_DEBUG "EXT4-fs: added group %u:"
  1183. "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
  1184. blocks_count, free_blocks, reserved_blocks);
  1185. }
  1186. /* Add a flex group to an fs. Ensure we handle all possible error conditions
  1187. * _before_ we start modifying the filesystem, because we cannot abort the
  1188. * transaction and not have it write the data to disk.
  1189. */
  1190. static int ext4_flex_group_add(struct super_block *sb,
  1191. struct inode *resize_inode,
  1192. struct ext4_new_flex_group_data *flex_gd)
  1193. {
  1194. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1195. struct ext4_super_block *es = sbi->s_es;
  1196. ext4_fsblk_t o_blocks_count;
  1197. ext4_grpblk_t last;
  1198. ext4_group_t group;
  1199. handle_t *handle;
  1200. unsigned reserved_gdb;
  1201. int err = 0, err2 = 0, credit;
  1202. BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
  1203. reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
  1204. o_blocks_count = ext4_blocks_count(es);
  1205. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1206. BUG_ON(last);
  1207. err = setup_new_flex_group_blocks(sb, flex_gd);
  1208. if (err)
  1209. goto exit;
  1210. /*
  1211. * We will always be modifying at least the superblock and GDT
  1212. * block. If we are adding a group past the last current GDT block,
  1213. * we will also modify the inode and the dindirect block. If we
  1214. * are adding a group with superblock/GDT backups we will also
  1215. * modify each of the reserved GDT dindirect blocks.
  1216. */
  1217. credit = flex_gd->count * 4 + reserved_gdb;
  1218. handle = ext4_journal_start_sb(sb, credit);
  1219. if (IS_ERR(handle)) {
  1220. err = PTR_ERR(handle);
  1221. goto exit;
  1222. }
  1223. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1224. if (err)
  1225. goto exit_journal;
  1226. group = flex_gd->groups[0].group;
  1227. BUG_ON(group != EXT4_SB(sb)->s_groups_count);
  1228. err = ext4_add_new_descs(handle, sb, group,
  1229. resize_inode, flex_gd->count);
  1230. if (err)
  1231. goto exit_journal;
  1232. err = ext4_setup_new_descs(handle, sb, flex_gd);
  1233. if (err)
  1234. goto exit_journal;
  1235. ext4_update_super(sb, flex_gd);
  1236. err = ext4_handle_dirty_super(handle, sb);
  1237. exit_journal:
  1238. err2 = ext4_journal_stop(handle);
  1239. if (!err)
  1240. err = err2;
  1241. if (!err) {
  1242. int i;
  1243. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  1244. sizeof(struct ext4_super_block));
  1245. for (i = 0; i < flex_gd->count; i++, group++) {
  1246. struct buffer_head *gdb_bh;
  1247. int gdb_num;
  1248. gdb_num = group / EXT4_BLOCKS_PER_GROUP(sb);
  1249. gdb_bh = sbi->s_group_desc[gdb_num];
  1250. update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
  1251. gdb_bh->b_size);
  1252. }
  1253. }
  1254. exit:
  1255. return err;
  1256. }
  1257. static int ext4_setup_next_flex_gd(struct super_block *sb,
  1258. struct ext4_new_flex_group_data *flex_gd,
  1259. ext4_fsblk_t n_blocks_count,
  1260. unsigned long flexbg_size)
  1261. {
  1262. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  1263. struct ext4_new_group_data *group_data = flex_gd->groups;
  1264. ext4_fsblk_t o_blocks_count;
  1265. ext4_group_t n_group;
  1266. ext4_group_t group;
  1267. ext4_group_t last_group;
  1268. ext4_grpblk_t last;
  1269. ext4_grpblk_t blocks_per_group;
  1270. unsigned long i;
  1271. blocks_per_group = EXT4_BLOCKS_PER_GROUP(sb);
  1272. o_blocks_count = ext4_blocks_count(es);
  1273. if (o_blocks_count == n_blocks_count)
  1274. return 0;
  1275. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1276. BUG_ON(last);
  1277. ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
  1278. last_group = group | (flexbg_size - 1);
  1279. if (last_group > n_group)
  1280. last_group = n_group;
  1281. flex_gd->count = last_group - group + 1;
  1282. for (i = 0; i < flex_gd->count; i++) {
  1283. int overhead;
  1284. group_data[i].group = group + i;
  1285. group_data[i].blocks_count = blocks_per_group;
  1286. overhead = ext4_bg_has_super(sb, group + i) ?
  1287. (1 + ext4_bg_num_gdb(sb, group + i) +
  1288. le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
  1289. group_data[i].free_blocks_count = blocks_per_group - overhead;
  1290. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1291. EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
  1292. flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
  1293. EXT4_BG_INODE_UNINIT;
  1294. else
  1295. flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
  1296. }
  1297. if (last_group == n_group &&
  1298. EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1299. EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
  1300. /* We need to initialize block bitmap of last group. */
  1301. flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
  1302. if ((last_group == n_group) && (last != blocks_per_group - 1)) {
  1303. group_data[i - 1].blocks_count = last + 1;
  1304. group_data[i - 1].free_blocks_count -= blocks_per_group-
  1305. last - 1;
  1306. }
  1307. return 1;
  1308. }
  1309. /* Add group descriptor data to an existing or new group descriptor block.
  1310. * Ensure we handle all possible error conditions _before_ we start modifying
  1311. * the filesystem, because we cannot abort the transaction and not have it
  1312. * write the data to disk.
  1313. *
  1314. * If we are on a GDT block boundary, we need to get the reserved GDT block.
  1315. * Otherwise, we may need to add backup GDT blocks for a sparse group.
  1316. *
  1317. * We only need to hold the superblock lock while we are actually adding
  1318. * in the new group's counts to the superblock. Prior to that we have
  1319. * not really "added" the group at all. We re-check that we are still
  1320. * adding in the last group in case things have changed since verifying.
  1321. */
  1322. int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
  1323. {
  1324. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1325. struct ext4_super_block *es = sbi->s_es;
  1326. int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
  1327. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  1328. struct buffer_head *primary = NULL;
  1329. struct ext4_group_desc *gdp;
  1330. struct inode *inode = NULL;
  1331. handle_t *handle;
  1332. int gdb_off, gdb_num;
  1333. int err, err2;
  1334. gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
  1335. gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
  1336. if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1337. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  1338. ext4_warning(sb, "Can't resize non-sparse filesystem further");
  1339. return -EPERM;
  1340. }
  1341. if (ext4_blocks_count(es) + input->blocks_count <
  1342. ext4_blocks_count(es)) {
  1343. ext4_warning(sb, "blocks_count overflow");
  1344. return -EINVAL;
  1345. }
  1346. if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
  1347. le32_to_cpu(es->s_inodes_count)) {
  1348. ext4_warning(sb, "inodes_count overflow");
  1349. return -EINVAL;
  1350. }
  1351. if (reserved_gdb || gdb_off == 0) {
  1352. if (!EXT4_HAS_COMPAT_FEATURE(sb,
  1353. EXT4_FEATURE_COMPAT_RESIZE_INODE)
  1354. || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
  1355. ext4_warning(sb,
  1356. "No reserved GDT blocks, can't resize");
  1357. return -EPERM;
  1358. }
  1359. inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1360. if (IS_ERR(inode)) {
  1361. ext4_warning(sb, "Error opening resize inode");
  1362. return PTR_ERR(inode);
  1363. }
  1364. }
  1365. if ((err = verify_group_input(sb, input)))
  1366. goto exit_put;
  1367. if ((err = setup_new_group_blocks(sb, input)))
  1368. goto exit_put;
  1369. /*
  1370. * We will always be modifying at least the superblock and a GDT
  1371. * block. If we are adding a group past the last current GDT block,
  1372. * we will also modify the inode and the dindirect block. If we
  1373. * are adding a group with superblock/GDT backups we will also
  1374. * modify each of the reserved GDT dindirect blocks.
  1375. */
  1376. handle = ext4_journal_start_sb(sb,
  1377. ext4_bg_has_super(sb, input->group) ?
  1378. 3 + reserved_gdb : 4);
  1379. if (IS_ERR(handle)) {
  1380. err = PTR_ERR(handle);
  1381. goto exit_put;
  1382. }
  1383. if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
  1384. goto exit_journal;
  1385. /*
  1386. * We will only either add reserved group blocks to a backup group
  1387. * or remove reserved blocks for the first group in a new group block.
  1388. * Doing both would be mean more complex code, and sane people don't
  1389. * use non-sparse filesystems anymore. This is already checked above.
  1390. */
  1391. if (gdb_off) {
  1392. primary = sbi->s_group_desc[gdb_num];
  1393. if ((err = ext4_journal_get_write_access(handle, primary)))
  1394. goto exit_journal;
  1395. if (reserved_gdb && ext4_bg_num_gdb(sb, input->group)) {
  1396. err = reserve_backup_gdb(handle, inode, input->group);
  1397. if (err)
  1398. goto exit_journal;
  1399. }
  1400. } else {
  1401. /*
  1402. * Note that we can access new group descriptor block safely
  1403. * only if add_new_gdb() succeeds.
  1404. */
  1405. err = add_new_gdb(handle, inode, input->group);
  1406. if (err)
  1407. goto exit_journal;
  1408. primary = sbi->s_group_desc[gdb_num];
  1409. }
  1410. /*
  1411. * OK, now we've set up the new group. Time to make it active.
  1412. *
  1413. * so we have to be safe wrt. concurrent accesses the group
  1414. * data. So we need to be careful to set all of the relevant
  1415. * group descriptor data etc. *before* we enable the group.
  1416. *
  1417. * The key field here is sbi->s_groups_count: as long as
  1418. * that retains its old value, nobody is going to access the new
  1419. * group.
  1420. *
  1421. * So first we update all the descriptor metadata for the new
  1422. * group; then we update the total disk blocks count; then we
  1423. * update the groups count to enable the group; then finally we
  1424. * update the free space counts so that the system can start
  1425. * using the new disk blocks.
  1426. */
  1427. /* Update group descriptor block for new group */
  1428. gdp = (struct ext4_group_desc *)((char *)primary->b_data +
  1429. gdb_off * EXT4_DESC_SIZE(sb));
  1430. memset(gdp, 0, EXT4_DESC_SIZE(sb));
  1431. ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
  1432. ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
  1433. ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
  1434. ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count);
  1435. ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
  1436. gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1437. gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
  1438. /*
  1439. * We can allocate memory for mb_alloc based on the new group
  1440. * descriptor
  1441. */
  1442. err = ext4_mb_add_groupinfo(sb, input->group, gdp);
  1443. if (err)
  1444. goto exit_journal;
  1445. /*
  1446. * Make the new blocks and inodes valid next. We do this before
  1447. * increasing the group count so that once the group is enabled,
  1448. * all of its blocks and inodes are already valid.
  1449. *
  1450. * We always allocate group-by-group, then block-by-block or
  1451. * inode-by-inode within a group, so enabling these
  1452. * blocks/inodes before the group is live won't actually let us
  1453. * allocate the new space yet.
  1454. */
  1455. ext4_blocks_count_set(es, ext4_blocks_count(es) +
  1456. input->blocks_count);
  1457. le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
  1458. /*
  1459. * We need to protect s_groups_count against other CPUs seeing
  1460. * inconsistent state in the superblock.
  1461. *
  1462. * The precise rules we use are:
  1463. *
  1464. * * Writers must perform a smp_wmb() after updating all dependent
  1465. * data and before modifying the groups count
  1466. *
  1467. * * Readers must perform an smp_rmb() after reading the groups count
  1468. * and before reading any dependent data.
  1469. *
  1470. * NB. These rules can be relaxed when checking the group count
  1471. * while freeing data, as we can only allocate from a block
  1472. * group after serialising against the group count, and we can
  1473. * only then free after serialising in turn against that
  1474. * allocation.
  1475. */
  1476. smp_wmb();
  1477. /* Update the global fs size fields */
  1478. sbi->s_groups_count++;
  1479. err = ext4_handle_dirty_metadata(handle, NULL, primary);
  1480. if (unlikely(err)) {
  1481. ext4_std_error(sb, err);
  1482. goto exit_journal;
  1483. }
  1484. /* Update the reserved block counts only once the new group is
  1485. * active. */
  1486. ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
  1487. input->reserved_blocks);
  1488. /* Update the free space counts */
  1489. percpu_counter_add(&sbi->s_freeclusters_counter,
  1490. EXT4_B2C(sbi, input->free_blocks_count));
  1491. percpu_counter_add(&sbi->s_freeinodes_counter,
  1492. EXT4_INODES_PER_GROUP(sb));
  1493. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1494. sbi->s_log_groups_per_flex) {
  1495. ext4_group_t flex_group;
  1496. flex_group = ext4_flex_group(sbi, input->group);
  1497. atomic_add(EXT4_B2C(sbi, input->free_blocks_count),
  1498. &sbi->s_flex_groups[flex_group].free_clusters);
  1499. atomic_add(EXT4_INODES_PER_GROUP(sb),
  1500. &sbi->s_flex_groups[flex_group].free_inodes);
  1501. }
  1502. ext4_handle_dirty_super(handle, sb);
  1503. exit_journal:
  1504. if ((err2 = ext4_journal_stop(handle)) && !err)
  1505. err = err2;
  1506. if (!err && primary) {
  1507. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  1508. sizeof(struct ext4_super_block));
  1509. update_backups(sb, primary->b_blocknr, primary->b_data,
  1510. primary->b_size);
  1511. }
  1512. exit_put:
  1513. iput(inode);
  1514. return err;
  1515. } /* ext4_group_add */
  1516. /*
  1517. * extend a group without checking assuming that checking has been done.
  1518. */
  1519. static int ext4_group_extend_no_check(struct super_block *sb,
  1520. ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
  1521. {
  1522. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  1523. handle_t *handle;
  1524. int err = 0, err2;
  1525. /* We will update the superblock, one block bitmap, and
  1526. * one group descriptor via ext4_group_add_blocks().
  1527. */
  1528. handle = ext4_journal_start_sb(sb, 3);
  1529. if (IS_ERR(handle)) {
  1530. err = PTR_ERR(handle);
  1531. ext4_warning(sb, "error %d on journal start", err);
  1532. return err;
  1533. }
  1534. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1535. if (err) {
  1536. ext4_warning(sb, "error %d on journal write access", err);
  1537. goto errout;
  1538. }
  1539. ext4_blocks_count_set(es, o_blocks_count + add);
  1540. ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
  1541. o_blocks_count + add);
  1542. /* We add the blocks to the bitmap and set the group need init bit */
  1543. err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
  1544. if (err)
  1545. goto errout;
  1546. ext4_handle_dirty_super(handle, sb);
  1547. ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
  1548. o_blocks_count + add);
  1549. errout:
  1550. err2 = ext4_journal_stop(handle);
  1551. if (err2 && !err)
  1552. err = err2;
  1553. if (!err) {
  1554. if (test_opt(sb, DEBUG))
  1555. printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
  1556. "blocks\n", ext4_blocks_count(es));
  1557. update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
  1558. sizeof(struct ext4_super_block));
  1559. }
  1560. return err;
  1561. }
  1562. /*
  1563. * Extend the filesystem to the new number of blocks specified. This entry
  1564. * point is only used to extend the current filesystem to the end of the last
  1565. * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
  1566. * for emergencies (because it has no dependencies on reserved blocks).
  1567. *
  1568. * If we _really_ wanted, we could use default values to call ext4_group_add()
  1569. * allow the "remount" trick to work for arbitrary resizing, assuming enough
  1570. * GDT blocks are reserved to grow to the desired size.
  1571. */
  1572. int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
  1573. ext4_fsblk_t n_blocks_count)
  1574. {
  1575. ext4_fsblk_t o_blocks_count;
  1576. ext4_grpblk_t last;
  1577. ext4_grpblk_t add;
  1578. struct buffer_head *bh;
  1579. int err;
  1580. ext4_group_t group;
  1581. o_blocks_count = ext4_blocks_count(es);
  1582. if (test_opt(sb, DEBUG))
  1583. printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n",
  1584. o_blocks_count, n_blocks_count);
  1585. if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
  1586. return 0;
  1587. if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  1588. printk(KERN_ERR "EXT4-fs: filesystem on %s:"
  1589. " too large to resize to %llu blocks safely\n",
  1590. sb->s_id, n_blocks_count);
  1591. if (sizeof(sector_t) < 8)
  1592. ext4_warning(sb, "CONFIG_LBDAF not enabled");
  1593. return -EINVAL;
  1594. }
  1595. if (n_blocks_count < o_blocks_count) {
  1596. ext4_warning(sb, "can't shrink FS - resize aborted");
  1597. return -EINVAL;
  1598. }
  1599. /* Handle the remaining blocks in the last group only. */
  1600. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1601. if (last == 0) {
  1602. ext4_warning(sb, "need to use ext2online to resize further");
  1603. return -EPERM;
  1604. }
  1605. add = EXT4_BLOCKS_PER_GROUP(sb) - last;
  1606. if (o_blocks_count + add < o_blocks_count) {
  1607. ext4_warning(sb, "blocks_count overflow");
  1608. return -EINVAL;
  1609. }
  1610. if (o_blocks_count + add > n_blocks_count)
  1611. add = n_blocks_count - o_blocks_count;
  1612. if (o_blocks_count + add < n_blocks_count)
  1613. ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
  1614. o_blocks_count + add, add);
  1615. /* See if the device is actually as big as what was requested */
  1616. bh = sb_bread(sb, o_blocks_count + add - 1);
  1617. if (!bh) {
  1618. ext4_warning(sb, "can't read last block, resize aborted");
  1619. return -ENOSPC;
  1620. }
  1621. brelse(bh);
  1622. err = ext4_group_extend_no_check(sb, o_blocks_count, add);
  1623. return err;
  1624. } /* ext4_group_extend */
  1625. /*
  1626. * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
  1627. *
  1628. * @sb: super block of the fs to be resized
  1629. * @n_blocks_count: the number of blocks resides in the resized fs
  1630. */
  1631. int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
  1632. {
  1633. struct ext4_new_flex_group_data *flex_gd = NULL;
  1634. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1635. struct ext4_super_block *es = sbi->s_es;
  1636. struct buffer_head *bh;
  1637. struct inode *resize_inode;
  1638. ext4_fsblk_t o_blocks_count;
  1639. ext4_group_t o_group;
  1640. ext4_group_t n_group;
  1641. ext4_grpblk_t offset;
  1642. unsigned long n_desc_blocks;
  1643. unsigned long o_desc_blocks;
  1644. unsigned long desc_blocks;
  1645. int err = 0, flexbg_size = 1;
  1646. o_blocks_count = ext4_blocks_count(es);
  1647. if (test_opt(sb, DEBUG))
  1648. printk(KERN_DEBUG "EXT4-fs: resizing filesystem from %llu "
  1649. "upto %llu blocks\n", o_blocks_count, n_blocks_count);
  1650. if (n_blocks_count < o_blocks_count) {
  1651. /* On-line shrinking not supported */
  1652. ext4_warning(sb, "can't shrink FS - resize aborted");
  1653. return -EINVAL;
  1654. }
  1655. if (n_blocks_count == o_blocks_count)
  1656. /* Nothing need to do */
  1657. return 0;
  1658. ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &offset);
  1659. ext4_get_group_no_and_offset(sb, o_blocks_count, &o_group, &offset);
  1660. n_desc_blocks = (n_group + EXT4_DESC_PER_BLOCK(sb)) /
  1661. EXT4_DESC_PER_BLOCK(sb);
  1662. o_desc_blocks = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  1663. EXT4_DESC_PER_BLOCK(sb);
  1664. desc_blocks = n_desc_blocks - o_desc_blocks;
  1665. if (desc_blocks &&
  1666. (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_RESIZE_INODE) ||
  1667. le16_to_cpu(es->s_reserved_gdt_blocks) < desc_blocks)) {
  1668. ext4_warning(sb, "No reserved GDT blocks, can't resize");
  1669. return -EPERM;
  1670. }
  1671. resize_inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1672. if (IS_ERR(resize_inode)) {
  1673. ext4_warning(sb, "Error opening resize inode");
  1674. return PTR_ERR(resize_inode);
  1675. }
  1676. /* See if the device is actually as big as what was requested */
  1677. bh = sb_bread(sb, n_blocks_count - 1);
  1678. if (!bh) {
  1679. ext4_warning(sb, "can't read last block, resize aborted");
  1680. return -ENOSPC;
  1681. }
  1682. brelse(bh);
  1683. if (offset != 0) {
  1684. /* extend the last group */
  1685. ext4_grpblk_t add;
  1686. add = EXT4_BLOCKS_PER_GROUP(sb) - offset;
  1687. err = ext4_group_extend_no_check(sb, o_blocks_count, add);
  1688. if (err)
  1689. goto out;
  1690. }
  1691. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1692. es->s_log_groups_per_flex)
  1693. flexbg_size = 1 << es->s_log_groups_per_flex;
  1694. o_blocks_count = ext4_blocks_count(es);
  1695. if (o_blocks_count == n_blocks_count)
  1696. goto out;
  1697. flex_gd = alloc_flex_gd(flexbg_size);
  1698. if (flex_gd == NULL) {
  1699. err = -ENOMEM;
  1700. goto out;
  1701. }
  1702. /* Add flex groups. Note that a regular group is a
  1703. * flex group with 1 group.
  1704. */
  1705. while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
  1706. flexbg_size)) {
  1707. ext4_alloc_group_tables(sb, flex_gd, flexbg_size);
  1708. err = ext4_flex_group_add(sb, resize_inode, flex_gd);
  1709. if (unlikely(err))
  1710. break;
  1711. }
  1712. out:
  1713. if (flex_gd)
  1714. free_flex_gd(flex_gd);
  1715. iput(resize_inode);
  1716. if (test_opt(sb, DEBUG))
  1717. printk(KERN_DEBUG "EXT4-fs: resized filesystem from %llu "
  1718. "upto %llu blocks\n", o_blocks_count, n_blocks_count);
  1719. return err;
  1720. }