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