resize.c 58 KB

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