balloc.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. /*
  2. * linux/fs/ext4/balloc.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
  10. * Big-endian to little-endian byte-swapping/bitmaps by
  11. * David S. Miller (davem@caip.rutgers.edu), 1995
  12. */
  13. #include <linux/time.h>
  14. #include <linux/capability.h>
  15. #include <linux/fs.h>
  16. #include <linux/jbd2.h>
  17. #include <linux/quotaops.h>
  18. #include <linux/buffer_head.h>
  19. #include "ext4.h"
  20. #include "ext4_jbd2.h"
  21. #include "group.h"
  22. /*
  23. * balloc.c contains the blocks allocation and deallocation routines
  24. */
  25. /*
  26. * Calculate the block group number and offset, given a block number
  27. */
  28. void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
  29. ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
  30. {
  31. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  32. ext4_grpblk_t offset;
  33. blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
  34. offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
  35. if (offsetp)
  36. *offsetp = offset;
  37. if (blockgrpp)
  38. *blockgrpp = blocknr;
  39. }
  40. static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
  41. ext4_group_t block_group)
  42. {
  43. ext4_group_t actual_group;
  44. ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
  45. if (actual_group == block_group)
  46. return 1;
  47. return 0;
  48. }
  49. static int ext4_group_used_meta_blocks(struct super_block *sb,
  50. ext4_group_t block_group)
  51. {
  52. ext4_fsblk_t tmp;
  53. struct ext4_sb_info *sbi = EXT4_SB(sb);
  54. /* block bitmap, inode bitmap, and inode table blocks */
  55. int used_blocks = sbi->s_itb_per_group + 2;
  56. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
  57. struct ext4_group_desc *gdp;
  58. struct buffer_head *bh;
  59. gdp = ext4_get_group_desc(sb, block_group, &bh);
  60. if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
  61. block_group))
  62. used_blocks--;
  63. if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
  64. block_group))
  65. used_blocks--;
  66. tmp = ext4_inode_table(sb, gdp);
  67. for (; tmp < ext4_inode_table(sb, gdp) +
  68. sbi->s_itb_per_group; tmp++) {
  69. if (!ext4_block_in_group(sb, tmp, block_group))
  70. used_blocks -= 1;
  71. }
  72. }
  73. return used_blocks;
  74. }
  75. /* Initializes an uninitialized block bitmap if given, and returns the
  76. * number of blocks free in the group. */
  77. unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
  78. ext4_group_t block_group, struct ext4_group_desc *gdp)
  79. {
  80. int bit, bit_max;
  81. unsigned free_blocks, group_blocks;
  82. struct ext4_sb_info *sbi = EXT4_SB(sb);
  83. if (bh) {
  84. J_ASSERT_BH(bh, buffer_locked(bh));
  85. /* If checksum is bad mark all blocks used to prevent allocation
  86. * essentially implementing a per-group read-only flag. */
  87. if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
  88. ext4_error(sb, __func__,
  89. "Checksum bad for group %lu\n", block_group);
  90. gdp->bg_free_blocks_count = 0;
  91. gdp->bg_free_inodes_count = 0;
  92. gdp->bg_itable_unused = 0;
  93. memset(bh->b_data, 0xff, sb->s_blocksize);
  94. return 0;
  95. }
  96. memset(bh->b_data, 0, sb->s_blocksize);
  97. }
  98. /* Check for superblock and gdt backups in this group */
  99. bit_max = ext4_bg_has_super(sb, block_group);
  100. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  101. block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
  102. sbi->s_desc_per_block) {
  103. if (bit_max) {
  104. bit_max += ext4_bg_num_gdb(sb, block_group);
  105. bit_max +=
  106. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
  107. }
  108. } else { /* For META_BG_BLOCK_GROUPS */
  109. bit_max += ext4_bg_num_gdb(sb, block_group);
  110. }
  111. if (block_group == sbi->s_groups_count - 1) {
  112. /*
  113. * Even though mke2fs always initialize first and last group
  114. * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
  115. * to make sure we calculate the right free blocks
  116. */
  117. group_blocks = ext4_blocks_count(sbi->s_es) -
  118. le32_to_cpu(sbi->s_es->s_first_data_block) -
  119. (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count - 1));
  120. } else {
  121. group_blocks = EXT4_BLOCKS_PER_GROUP(sb);
  122. }
  123. free_blocks = group_blocks - bit_max;
  124. if (bh) {
  125. ext4_fsblk_t start, tmp;
  126. int flex_bg = 0;
  127. for (bit = 0; bit < bit_max; bit++)
  128. ext4_set_bit(bit, bh->b_data);
  129. start = ext4_group_first_block_no(sb, block_group);
  130. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  131. EXT4_FEATURE_INCOMPAT_FLEX_BG))
  132. flex_bg = 1;
  133. /* Set bits for block and inode bitmaps, and inode table */
  134. tmp = ext4_block_bitmap(sb, gdp);
  135. if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
  136. ext4_set_bit(tmp - start, bh->b_data);
  137. tmp = ext4_inode_bitmap(sb, gdp);
  138. if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
  139. ext4_set_bit(tmp - start, bh->b_data);
  140. tmp = ext4_inode_table(sb, gdp);
  141. for (; tmp < ext4_inode_table(sb, gdp) +
  142. sbi->s_itb_per_group; tmp++) {
  143. if (!flex_bg ||
  144. ext4_block_in_group(sb, tmp, block_group))
  145. ext4_set_bit(tmp - start, bh->b_data);
  146. }
  147. /*
  148. * Also if the number of blocks within the group is
  149. * less than the blocksize * 8 ( which is the size
  150. * of bitmap ), set rest of the block bitmap to 1
  151. */
  152. mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data);
  153. }
  154. return free_blocks - ext4_group_used_meta_blocks(sb, block_group);
  155. }
  156. /*
  157. * The free blocks are managed by bitmaps. A file system contains several
  158. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  159. * block for inodes, N blocks for the inode table and data blocks.
  160. *
  161. * The file system contains group descriptors which are located after the
  162. * super block. Each descriptor contains the number of the bitmap block and
  163. * the free blocks count in the block. The descriptors are loaded in memory
  164. * when a file system is mounted (see ext4_fill_super).
  165. */
  166. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  167. /**
  168. * ext4_get_group_desc() -- load group descriptor from disk
  169. * @sb: super block
  170. * @block_group: given block group
  171. * @bh: pointer to the buffer head to store the block
  172. * group descriptor
  173. */
  174. struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
  175. ext4_group_t block_group,
  176. struct buffer_head **bh)
  177. {
  178. unsigned long group_desc;
  179. unsigned long offset;
  180. struct ext4_group_desc *desc;
  181. struct ext4_sb_info *sbi = EXT4_SB(sb);
  182. if (block_group >= sbi->s_groups_count) {
  183. ext4_error(sb, "ext4_get_group_desc",
  184. "block_group >= groups_count - "
  185. "block_group = %lu, groups_count = %lu",
  186. block_group, sbi->s_groups_count);
  187. return NULL;
  188. }
  189. smp_rmb();
  190. group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
  191. offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  192. if (!sbi->s_group_desc[group_desc]) {
  193. ext4_error(sb, "ext4_get_group_desc",
  194. "Group descriptor not loaded - "
  195. "block_group = %lu, group_desc = %lu, desc = %lu",
  196. block_group, group_desc, offset);
  197. return NULL;
  198. }
  199. desc = (struct ext4_group_desc *)(
  200. (__u8 *)sbi->s_group_desc[group_desc]->b_data +
  201. offset * EXT4_DESC_SIZE(sb));
  202. if (bh)
  203. *bh = sbi->s_group_desc[group_desc];
  204. return desc;
  205. }
  206. static int ext4_valid_block_bitmap(struct super_block *sb,
  207. struct ext4_group_desc *desc,
  208. unsigned int block_group,
  209. struct buffer_head *bh)
  210. {
  211. ext4_grpblk_t offset;
  212. ext4_grpblk_t next_zero_bit;
  213. ext4_fsblk_t bitmap_blk;
  214. ext4_fsblk_t group_first_block;
  215. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
  216. /* with FLEX_BG, the inode/block bitmaps and itable
  217. * blocks may not be in the group at all
  218. * so the bitmap validation will be skipped for those groups
  219. * or it has to also read the block group where the bitmaps
  220. * are located to verify they are set.
  221. */
  222. return 1;
  223. }
  224. group_first_block = ext4_group_first_block_no(sb, block_group);
  225. /* check whether block bitmap block number is set */
  226. bitmap_blk = ext4_block_bitmap(sb, desc);
  227. offset = bitmap_blk - group_first_block;
  228. if (!ext4_test_bit(offset, bh->b_data))
  229. /* bad block bitmap */
  230. goto err_out;
  231. /* check whether the inode bitmap block number is set */
  232. bitmap_blk = ext4_inode_bitmap(sb, desc);
  233. offset = bitmap_blk - group_first_block;
  234. if (!ext4_test_bit(offset, bh->b_data))
  235. /* bad block bitmap */
  236. goto err_out;
  237. /* check whether the inode table block number is set */
  238. bitmap_blk = ext4_inode_table(sb, desc);
  239. offset = bitmap_blk - group_first_block;
  240. next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
  241. offset + EXT4_SB(sb)->s_itb_per_group,
  242. offset);
  243. if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
  244. /* good bitmap for inode tables */
  245. return 1;
  246. err_out:
  247. ext4_error(sb, __func__,
  248. "Invalid block bitmap - "
  249. "block_group = %d, block = %llu",
  250. block_group, bitmap_blk);
  251. return 0;
  252. }
  253. /**
  254. * ext4_read_block_bitmap()
  255. * @sb: super block
  256. * @block_group: given block group
  257. *
  258. * Read the bitmap for a given block_group,and validate the
  259. * bits for block/inode/inode tables are set in the bitmaps
  260. *
  261. * Return buffer_head on success or NULL in case of failure.
  262. */
  263. struct buffer_head *
  264. ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
  265. {
  266. struct ext4_group_desc *desc;
  267. struct buffer_head *bh = NULL;
  268. ext4_fsblk_t bitmap_blk;
  269. desc = ext4_get_group_desc(sb, block_group, NULL);
  270. if (!desc)
  271. return NULL;
  272. bitmap_blk = ext4_block_bitmap(sb, desc);
  273. bh = sb_getblk(sb, bitmap_blk);
  274. if (unlikely(!bh)) {
  275. ext4_error(sb, __func__,
  276. "Cannot read block bitmap - "
  277. "block_group = %lu, block_bitmap = %llu",
  278. block_group, bitmap_blk);
  279. return NULL;
  280. }
  281. if (bh_uptodate_or_lock(bh))
  282. return bh;
  283. spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
  284. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  285. ext4_init_block_bitmap(sb, bh, block_group, desc);
  286. set_buffer_uptodate(bh);
  287. unlock_buffer(bh);
  288. spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
  289. return bh;
  290. }
  291. spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
  292. if (bh_submit_read(bh) < 0) {
  293. put_bh(bh);
  294. ext4_error(sb, __func__,
  295. "Cannot read block bitmap - "
  296. "block_group = %lu, block_bitmap = %llu",
  297. block_group, bitmap_blk);
  298. return NULL;
  299. }
  300. ext4_valid_block_bitmap(sb, desc, block_group, bh);
  301. /*
  302. * file system mounted not to panic on error,
  303. * continue with corrupt bitmap
  304. */
  305. return bh;
  306. }
  307. /*
  308. * The reservation window structure operations
  309. * --------------------------------------------
  310. * Operations include:
  311. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  312. *
  313. * We use a red-black tree to represent per-filesystem reservation
  314. * windows.
  315. *
  316. */
  317. /**
  318. * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
  319. * @rb_root: root of per-filesystem reservation rb tree
  320. * @verbose: verbose mode
  321. * @fn: function which wishes to dump the reservation map
  322. *
  323. * If verbose is turned on, it will print the whole block reservation
  324. * windows(start, end). Otherwise, it will only print out the "bad" windows,
  325. * those windows that overlap with their immediate neighbors.
  326. */
  327. #if 1
  328. static void __rsv_window_dump(struct rb_root *root, int verbose,
  329. const char *fn)
  330. {
  331. struct rb_node *n;
  332. struct ext4_reserve_window_node *rsv, *prev;
  333. int bad;
  334. restart:
  335. n = rb_first(root);
  336. bad = 0;
  337. prev = NULL;
  338. printk(KERN_DEBUG "Block Allocation Reservation "
  339. "Windows Map (%s):\n", fn);
  340. while (n) {
  341. rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
  342. if (verbose)
  343. printk(KERN_DEBUG "reservation window 0x%p "
  344. "start: %llu, end: %llu\n",
  345. rsv, rsv->rsv_start, rsv->rsv_end);
  346. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  347. printk(KERN_DEBUG "Bad reservation %p (start >= end)\n",
  348. rsv);
  349. bad = 1;
  350. }
  351. if (prev && prev->rsv_end >= rsv->rsv_start) {
  352. printk(KERN_DEBUG "Bad reservation %p "
  353. "(prev->end >= start)\n", rsv);
  354. bad = 1;
  355. }
  356. if (bad) {
  357. if (!verbose) {
  358. printk(KERN_DEBUG "Restarting reservation "
  359. "walk in verbose mode\n");
  360. verbose = 1;
  361. goto restart;
  362. }
  363. }
  364. n = rb_next(n);
  365. prev = rsv;
  366. }
  367. printk(KERN_DEBUG "Window map complete.\n");
  368. BUG_ON(bad);
  369. }
  370. #define rsv_window_dump(root, verbose) \
  371. __rsv_window_dump((root), (verbose), __func__)
  372. #else
  373. #define rsv_window_dump(root, verbose) do {} while (0)
  374. #endif
  375. /**
  376. * goal_in_my_reservation()
  377. * @rsv: inode's reservation window
  378. * @grp_goal: given goal block relative to the allocation block group
  379. * @group: the current allocation block group
  380. * @sb: filesystem super block
  381. *
  382. * Test if the given goal block (group relative) is within the file's
  383. * own block reservation window range.
  384. *
  385. * If the reservation window is outside the goal allocation group, return 0;
  386. * grp_goal (given goal block) could be -1, which means no specific
  387. * goal block. In this case, always return 1.
  388. * If the goal block is within the reservation window, return 1;
  389. * otherwise, return 0;
  390. */
  391. static int
  392. goal_in_my_reservation(struct ext4_reserve_window *rsv, ext4_grpblk_t grp_goal,
  393. ext4_group_t group, struct super_block *sb)
  394. {
  395. ext4_fsblk_t group_first_block, group_last_block;
  396. group_first_block = ext4_group_first_block_no(sb, group);
  397. group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  398. if ((rsv->_rsv_start > group_last_block) ||
  399. (rsv->_rsv_end < group_first_block))
  400. return 0;
  401. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  402. || (grp_goal + group_first_block > rsv->_rsv_end)))
  403. return 0;
  404. return 1;
  405. }
  406. /**
  407. * search_reserve_window()
  408. * @rb_root: root of reservation tree
  409. * @goal: target allocation block
  410. *
  411. * Find the reserved window which includes the goal, or the previous one
  412. * if the goal is not in any window.
  413. * Returns NULL if there are no windows or if all windows start after the goal.
  414. */
  415. static struct ext4_reserve_window_node *
  416. search_reserve_window(struct rb_root *root, ext4_fsblk_t goal)
  417. {
  418. struct rb_node *n = root->rb_node;
  419. struct ext4_reserve_window_node *rsv;
  420. if (!n)
  421. return NULL;
  422. do {
  423. rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
  424. if (goal < rsv->rsv_start)
  425. n = n->rb_left;
  426. else if (goal > rsv->rsv_end)
  427. n = n->rb_right;
  428. else
  429. return rsv;
  430. } while (n);
  431. /*
  432. * We've fallen off the end of the tree: the goal wasn't inside
  433. * any particular node. OK, the previous node must be to one
  434. * side of the interval containing the goal. If it's the RHS,
  435. * we need to back up one.
  436. */
  437. if (rsv->rsv_start > goal) {
  438. n = rb_prev(&rsv->rsv_node);
  439. rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
  440. }
  441. return rsv;
  442. }
  443. /**
  444. * ext4_rsv_window_add() -- Insert a window to the block reservation rb tree.
  445. * @sb: super block
  446. * @rsv: reservation window to add
  447. *
  448. * Must be called with rsv_lock hold.
  449. */
  450. void ext4_rsv_window_add(struct super_block *sb,
  451. struct ext4_reserve_window_node *rsv)
  452. {
  453. struct rb_root *root = &EXT4_SB(sb)->s_rsv_window_root;
  454. struct rb_node *node = &rsv->rsv_node;
  455. ext4_fsblk_t start = rsv->rsv_start;
  456. struct rb_node **p = &root->rb_node;
  457. struct rb_node *parent = NULL;
  458. struct ext4_reserve_window_node *this;
  459. while (*p)
  460. {
  461. parent = *p;
  462. this = rb_entry(parent, struct ext4_reserve_window_node, rsv_node);
  463. if (start < this->rsv_start)
  464. p = &(*p)->rb_left;
  465. else if (start > this->rsv_end)
  466. p = &(*p)->rb_right;
  467. else {
  468. rsv_window_dump(root, 1);
  469. BUG();
  470. }
  471. }
  472. rb_link_node(node, parent, p);
  473. rb_insert_color(node, root);
  474. }
  475. /**
  476. * ext4_rsv_window_remove() -- unlink a window from the reservation rb tree
  477. * @sb: super block
  478. * @rsv: reservation window to remove
  479. *
  480. * Mark the block reservation window as not allocated, and unlink it
  481. * from the filesystem reservation window rb tree. Must be called with
  482. * rsv_lock hold.
  483. */
  484. static void rsv_window_remove(struct super_block *sb,
  485. struct ext4_reserve_window_node *rsv)
  486. {
  487. rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  488. rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  489. rsv->rsv_alloc_hit = 0;
  490. rb_erase(&rsv->rsv_node, &EXT4_SB(sb)->s_rsv_window_root);
  491. }
  492. /*
  493. * rsv_is_empty() -- Check if the reservation window is allocated.
  494. * @rsv: given reservation window to check
  495. *
  496. * returns 1 if the end block is EXT4_RESERVE_WINDOW_NOT_ALLOCATED.
  497. */
  498. static inline int rsv_is_empty(struct ext4_reserve_window *rsv)
  499. {
  500. /* a valid reservation end block could not be 0 */
  501. return rsv->_rsv_end == EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  502. }
  503. /**
  504. * ext4_init_block_alloc_info()
  505. * @inode: file inode structure
  506. *
  507. * Allocate and initialize the reservation window structure, and
  508. * link the window to the ext4 inode structure at last
  509. *
  510. * The reservation window structure is only dynamically allocated
  511. * and linked to ext4 inode the first time the open file
  512. * needs a new block. So, before every ext4_new_block(s) call, for
  513. * regular files, we should check whether the reservation window
  514. * structure exists or not. In the latter case, this function is called.
  515. * Fail to do so will result in block reservation being turned off for that
  516. * open file.
  517. *
  518. * This function is called from ext4_get_blocks_handle(), also called
  519. * when setting the reservation window size through ioctl before the file
  520. * is open for write (needs block allocation).
  521. *
  522. * Needs down_write(i_data_sem) protection prior to call this function.
  523. */
  524. void ext4_init_block_alloc_info(struct inode *inode)
  525. {
  526. struct ext4_inode_info *ei = EXT4_I(inode);
  527. struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info;
  528. struct super_block *sb = inode->i_sb;
  529. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  530. if (block_i) {
  531. struct ext4_reserve_window_node *rsv = &block_i->rsv_window_node;
  532. rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  533. rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  534. /*
  535. * if filesystem is mounted with NORESERVATION, the goal
  536. * reservation window size is set to zero to indicate
  537. * block reservation is off
  538. */
  539. if (!test_opt(sb, RESERVATION))
  540. rsv->rsv_goal_size = 0;
  541. else
  542. rsv->rsv_goal_size = EXT4_DEFAULT_RESERVE_BLOCKS;
  543. rsv->rsv_alloc_hit = 0;
  544. block_i->last_alloc_logical_block = 0;
  545. block_i->last_alloc_physical_block = 0;
  546. }
  547. ei->i_block_alloc_info = block_i;
  548. }
  549. /**
  550. * ext4_discard_reservation()
  551. * @inode: inode
  552. *
  553. * Discard(free) block reservation window on last file close, or truncate
  554. * or at last iput().
  555. *
  556. * It is being called in three cases:
  557. * ext4_release_file(): last writer close the file
  558. * ext4_clear_inode(): last iput(), when nobody link to this file.
  559. * ext4_truncate(): when the block indirect map is about to change.
  560. *
  561. */
  562. void ext4_discard_reservation(struct inode *inode)
  563. {
  564. struct ext4_inode_info *ei = EXT4_I(inode);
  565. struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info;
  566. struct ext4_reserve_window_node *rsv;
  567. spinlock_t *rsv_lock = &EXT4_SB(inode->i_sb)->s_rsv_window_lock;
  568. ext4_mb_discard_inode_preallocations(inode);
  569. if (!block_i)
  570. return;
  571. rsv = &block_i->rsv_window_node;
  572. if (!rsv_is_empty(&rsv->rsv_window)) {
  573. spin_lock(rsv_lock);
  574. if (!rsv_is_empty(&rsv->rsv_window))
  575. rsv_window_remove(inode->i_sb, rsv);
  576. spin_unlock(rsv_lock);
  577. }
  578. }
  579. /**
  580. * ext4_free_blocks_sb() -- Free given blocks and update quota
  581. * @handle: handle to this transaction
  582. * @sb: super block
  583. * @block: start physcial block to free
  584. * @count: number of blocks to free
  585. * @pdquot_freed_blocks: pointer to quota
  586. */
  587. void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb,
  588. ext4_fsblk_t block, unsigned long count,
  589. unsigned long *pdquot_freed_blocks)
  590. {
  591. struct buffer_head *bitmap_bh = NULL;
  592. struct buffer_head *gd_bh;
  593. ext4_group_t block_group;
  594. ext4_grpblk_t bit;
  595. unsigned long i;
  596. unsigned long overflow;
  597. struct ext4_group_desc *desc;
  598. struct ext4_super_block *es;
  599. struct ext4_sb_info *sbi;
  600. int err = 0, ret;
  601. ext4_grpblk_t group_freed;
  602. *pdquot_freed_blocks = 0;
  603. sbi = EXT4_SB(sb);
  604. es = sbi->s_es;
  605. if (block < le32_to_cpu(es->s_first_data_block) ||
  606. block + count < block ||
  607. block + count > ext4_blocks_count(es)) {
  608. ext4_error(sb, "ext4_free_blocks",
  609. "Freeing blocks not in datazone - "
  610. "block = %llu, count = %lu", block, count);
  611. goto error_return;
  612. }
  613. ext4_debug("freeing block(s) %llu-%llu\n", block, block + count - 1);
  614. do_more:
  615. overflow = 0;
  616. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  617. /*
  618. * Check to see if we are freeing blocks across a group
  619. * boundary.
  620. */
  621. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  622. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  623. count -= overflow;
  624. }
  625. brelse(bitmap_bh);
  626. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  627. if (!bitmap_bh)
  628. goto error_return;
  629. desc = ext4_get_group_desc(sb, block_group, &gd_bh);
  630. if (!desc)
  631. goto error_return;
  632. if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
  633. in_range(ext4_inode_bitmap(sb, desc), block, count) ||
  634. in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
  635. in_range(block + count - 1, ext4_inode_table(sb, desc),
  636. sbi->s_itb_per_group)) {
  637. ext4_error(sb, "ext4_free_blocks",
  638. "Freeing blocks in system zones - "
  639. "Block = %llu, count = %lu",
  640. block, count);
  641. goto error_return;
  642. }
  643. /*
  644. * We are about to start releasing blocks in the bitmap,
  645. * so we need undo access.
  646. */
  647. /* @@@ check errors */
  648. BUFFER_TRACE(bitmap_bh, "getting undo access");
  649. err = ext4_journal_get_undo_access(handle, bitmap_bh);
  650. if (err)
  651. goto error_return;
  652. /*
  653. * We are about to modify some metadata. Call the journal APIs
  654. * to unshare ->b_data if a currently-committing transaction is
  655. * using it
  656. */
  657. BUFFER_TRACE(gd_bh, "get_write_access");
  658. err = ext4_journal_get_write_access(handle, gd_bh);
  659. if (err)
  660. goto error_return;
  661. jbd_lock_bh_state(bitmap_bh);
  662. for (i = 0, group_freed = 0; i < count; i++) {
  663. /*
  664. * An HJ special. This is expensive...
  665. */
  666. #ifdef CONFIG_JBD2_DEBUG
  667. jbd_unlock_bh_state(bitmap_bh);
  668. {
  669. struct buffer_head *debug_bh;
  670. debug_bh = sb_find_get_block(sb, block + i);
  671. if (debug_bh) {
  672. BUFFER_TRACE(debug_bh, "Deleted!");
  673. if (!bh2jh(bitmap_bh)->b_committed_data)
  674. BUFFER_TRACE(debug_bh,
  675. "No commited data in bitmap");
  676. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
  677. __brelse(debug_bh);
  678. }
  679. }
  680. jbd_lock_bh_state(bitmap_bh);
  681. #endif
  682. if (need_resched()) {
  683. jbd_unlock_bh_state(bitmap_bh);
  684. cond_resched();
  685. jbd_lock_bh_state(bitmap_bh);
  686. }
  687. /* @@@ This prevents newly-allocated data from being
  688. * freed and then reallocated within the same
  689. * transaction.
  690. *
  691. * Ideally we would want to allow that to happen, but to
  692. * do so requires making jbd2_journal_forget() capable of
  693. * revoking the queued write of a data block, which
  694. * implies blocking on the journal lock. *forget()
  695. * cannot block due to truncate races.
  696. *
  697. * Eventually we can fix this by making jbd2_journal_forget()
  698. * return a status indicating whether or not it was able
  699. * to revoke the buffer. On successful revoke, it is
  700. * safe not to set the allocation bit in the committed
  701. * bitmap, because we know that there is no outstanding
  702. * activity on the buffer any more and so it is safe to
  703. * reallocate it.
  704. */
  705. BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
  706. J_ASSERT_BH(bitmap_bh,
  707. bh2jh(bitmap_bh)->b_committed_data != NULL);
  708. ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
  709. bh2jh(bitmap_bh)->b_committed_data);
  710. /*
  711. * We clear the bit in the bitmap after setting the committed
  712. * data bit, because this is the reverse order to that which
  713. * the allocator uses.
  714. */
  715. BUFFER_TRACE(bitmap_bh, "clear bit");
  716. if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  717. bit + i, bitmap_bh->b_data)) {
  718. jbd_unlock_bh_state(bitmap_bh);
  719. ext4_error(sb, __func__,
  720. "bit already cleared for block %llu",
  721. (ext4_fsblk_t)(block + i));
  722. jbd_lock_bh_state(bitmap_bh);
  723. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  724. } else {
  725. group_freed++;
  726. }
  727. }
  728. jbd_unlock_bh_state(bitmap_bh);
  729. spin_lock(sb_bgl_lock(sbi, block_group));
  730. le16_add_cpu(&desc->bg_free_blocks_count, group_freed);
  731. desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
  732. spin_unlock(sb_bgl_lock(sbi, block_group));
  733. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  734. if (sbi->s_log_groups_per_flex) {
  735. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  736. spin_lock(sb_bgl_lock(sbi, flex_group));
  737. sbi->s_flex_groups[flex_group].free_blocks += count;
  738. spin_unlock(sb_bgl_lock(sbi, flex_group));
  739. }
  740. /* We dirtied the bitmap block */
  741. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  742. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  743. /* And the group descriptor block */
  744. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  745. ret = ext4_journal_dirty_metadata(handle, gd_bh);
  746. if (!err) err = ret;
  747. *pdquot_freed_blocks += group_freed;
  748. if (overflow && !err) {
  749. block += count;
  750. count = overflow;
  751. goto do_more;
  752. }
  753. sb->s_dirt = 1;
  754. error_return:
  755. brelse(bitmap_bh);
  756. ext4_std_error(sb, err);
  757. return;
  758. }
  759. /**
  760. * ext4_free_blocks() -- Free given blocks and update quota
  761. * @handle: handle for this transaction
  762. * @inode: inode
  763. * @block: start physical block to free
  764. * @count: number of blocks to count
  765. * @metadata: Are these metadata blocks
  766. */
  767. void ext4_free_blocks(handle_t *handle, struct inode *inode,
  768. ext4_fsblk_t block, unsigned long count,
  769. int metadata)
  770. {
  771. struct super_block *sb;
  772. unsigned long dquot_freed_blocks;
  773. /* this isn't the right place to decide whether block is metadata
  774. * inode.c/extents.c knows better, but for safety ... */
  775. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
  776. ext4_should_journal_data(inode))
  777. metadata = 1;
  778. sb = inode->i_sb;
  779. if (!test_opt(sb, MBALLOC) || !EXT4_SB(sb)->s_group_info)
  780. ext4_free_blocks_sb(handle, sb, block, count,
  781. &dquot_freed_blocks);
  782. else
  783. ext4_mb_free_blocks(handle, inode, block, count,
  784. metadata, &dquot_freed_blocks);
  785. if (dquot_freed_blocks)
  786. DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
  787. return;
  788. }
  789. /**
  790. * ext4_test_allocatable()
  791. * @nr: given allocation block group
  792. * @bh: bufferhead contains the bitmap of the given block group
  793. *
  794. * For ext4 allocations, we must not reuse any blocks which are
  795. * allocated in the bitmap buffer's "last committed data" copy. This
  796. * prevents deletes from freeing up the page for reuse until we have
  797. * committed the delete transaction.
  798. *
  799. * If we didn't do this, then deleting something and reallocating it as
  800. * data would allow the old block to be overwritten before the
  801. * transaction committed (because we force data to disk before commit).
  802. * This would lead to corruption if we crashed between overwriting the
  803. * data and committing the delete.
  804. *
  805. * @@@ We may want to make this allocation behaviour conditional on
  806. * data-writes at some point, and disable it for metadata allocations or
  807. * sync-data inodes.
  808. */
  809. static int ext4_test_allocatable(ext4_grpblk_t nr, struct buffer_head *bh)
  810. {
  811. int ret;
  812. struct journal_head *jh = bh2jh(bh);
  813. if (ext4_test_bit(nr, bh->b_data))
  814. return 0;
  815. jbd_lock_bh_state(bh);
  816. if (!jh->b_committed_data)
  817. ret = 1;
  818. else
  819. ret = !ext4_test_bit(nr, jh->b_committed_data);
  820. jbd_unlock_bh_state(bh);
  821. return ret;
  822. }
  823. /**
  824. * bitmap_search_next_usable_block()
  825. * @start: the starting block (group relative) of the search
  826. * @bh: bufferhead contains the block group bitmap
  827. * @maxblocks: the ending block (group relative) of the reservation
  828. *
  829. * The bitmap search --- search forward alternately through the actual
  830. * bitmap on disk and the last-committed copy in journal, until we find a
  831. * bit free in both bitmaps.
  832. */
  833. static ext4_grpblk_t
  834. bitmap_search_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh,
  835. ext4_grpblk_t maxblocks)
  836. {
  837. ext4_grpblk_t next;
  838. struct journal_head *jh = bh2jh(bh);
  839. while (start < maxblocks) {
  840. next = ext4_find_next_zero_bit(bh->b_data, maxblocks, start);
  841. if (next >= maxblocks)
  842. return -1;
  843. if (ext4_test_allocatable(next, bh))
  844. return next;
  845. jbd_lock_bh_state(bh);
  846. if (jh->b_committed_data)
  847. start = ext4_find_next_zero_bit(jh->b_committed_data,
  848. maxblocks, next);
  849. jbd_unlock_bh_state(bh);
  850. }
  851. return -1;
  852. }
  853. /**
  854. * find_next_usable_block()
  855. * @start: the starting block (group relative) to find next
  856. * allocatable block in bitmap.
  857. * @bh: bufferhead contains the block group bitmap
  858. * @maxblocks: the ending block (group relative) for the search
  859. *
  860. * Find an allocatable block in a bitmap. We honor both the bitmap and
  861. * its last-committed copy (if that exists), and perform the "most
  862. * appropriate allocation" algorithm of looking for a free block near
  863. * the initial goal; then for a free byte somewhere in the bitmap; then
  864. * for any free bit in the bitmap.
  865. */
  866. static ext4_grpblk_t
  867. find_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh,
  868. ext4_grpblk_t maxblocks)
  869. {
  870. ext4_grpblk_t here, next;
  871. char *p, *r;
  872. if (start > 0) {
  873. /*
  874. * The goal was occupied; search forward for a free
  875. * block within the next XX blocks.
  876. *
  877. * end_goal is more or less random, but it has to be
  878. * less than EXT4_BLOCKS_PER_GROUP. Aligning up to the
  879. * next 64-bit boundary is simple..
  880. */
  881. ext4_grpblk_t end_goal = (start + 63) & ~63;
  882. if (end_goal > maxblocks)
  883. end_goal = maxblocks;
  884. here = ext4_find_next_zero_bit(bh->b_data, end_goal, start);
  885. if (here < end_goal && ext4_test_allocatable(here, bh))
  886. return here;
  887. ext4_debug("Bit not found near goal\n");
  888. }
  889. here = start;
  890. if (here < 0)
  891. here = 0;
  892. p = ((char *)bh->b_data) + (here >> 3);
  893. r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
  894. next = (r - ((char *)bh->b_data)) << 3;
  895. if (next < maxblocks && next >= start && ext4_test_allocatable(next, bh))
  896. return next;
  897. /*
  898. * The bitmap search --- search forward alternately through the actual
  899. * bitmap and the last-committed copy until we find a bit free in
  900. * both
  901. */
  902. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  903. return here;
  904. }
  905. /**
  906. * claim_block()
  907. * @block: the free block (group relative) to allocate
  908. * @bh: the bufferhead containts the block group bitmap
  909. *
  910. * We think we can allocate this block in this bitmap. Try to set the bit.
  911. * If that succeeds then check that nobody has allocated and then freed the
  912. * block since we saw that is was not marked in b_committed_data. If it _was_
  913. * allocated and freed then clear the bit in the bitmap again and return
  914. * zero (failure).
  915. */
  916. static inline int
  917. claim_block(spinlock_t *lock, ext4_grpblk_t block, struct buffer_head *bh)
  918. {
  919. struct journal_head *jh = bh2jh(bh);
  920. int ret;
  921. if (ext4_set_bit_atomic(lock, block, bh->b_data))
  922. return 0;
  923. jbd_lock_bh_state(bh);
  924. if (jh->b_committed_data && ext4_test_bit(block, jh->b_committed_data)) {
  925. ext4_clear_bit_atomic(lock, block, bh->b_data);
  926. ret = 0;
  927. } else {
  928. ret = 1;
  929. }
  930. jbd_unlock_bh_state(bh);
  931. return ret;
  932. }
  933. /**
  934. * ext4_try_to_allocate()
  935. * @sb: superblock
  936. * @handle: handle to this transaction
  937. * @group: given allocation block group
  938. * @bitmap_bh: bufferhead holds the block bitmap
  939. * @grp_goal: given target block within the group
  940. * @count: target number of blocks to allocate
  941. * @my_rsv: reservation window
  942. *
  943. * Attempt to allocate blocks within a give range. Set the range of allocation
  944. * first, then find the first free bit(s) from the bitmap (within the range),
  945. * and at last, allocate the blocks by claiming the found free bit as allocated.
  946. *
  947. * To set the range of this allocation:
  948. * if there is a reservation window, only try to allocate block(s) from the
  949. * file's own reservation window;
  950. * Otherwise, the allocation range starts from the give goal block, ends at
  951. * the block group's last block.
  952. *
  953. * If we failed to allocate the desired block then we may end up crossing to a
  954. * new bitmap. In that case we must release write access to the old one via
  955. * ext4_journal_release_buffer(), else we'll run out of credits.
  956. */
  957. static ext4_grpblk_t
  958. ext4_try_to_allocate(struct super_block *sb, handle_t *handle,
  959. ext4_group_t group, struct buffer_head *bitmap_bh,
  960. ext4_grpblk_t grp_goal, unsigned long *count,
  961. struct ext4_reserve_window *my_rsv)
  962. {
  963. ext4_fsblk_t group_first_block;
  964. ext4_grpblk_t start, end;
  965. unsigned long num = 0;
  966. /* we do allocation within the reservation window if we have a window */
  967. if (my_rsv) {
  968. group_first_block = ext4_group_first_block_no(sb, group);
  969. if (my_rsv->_rsv_start >= group_first_block)
  970. start = my_rsv->_rsv_start - group_first_block;
  971. else
  972. /* reservation window cross group boundary */
  973. start = 0;
  974. end = my_rsv->_rsv_end - group_first_block + 1;
  975. if (end > EXT4_BLOCKS_PER_GROUP(sb))
  976. /* reservation window crosses group boundary */
  977. end = EXT4_BLOCKS_PER_GROUP(sb);
  978. if ((start <= grp_goal) && (grp_goal < end))
  979. start = grp_goal;
  980. else
  981. grp_goal = -1;
  982. } else {
  983. if (grp_goal > 0)
  984. start = grp_goal;
  985. else
  986. start = 0;
  987. end = EXT4_BLOCKS_PER_GROUP(sb);
  988. }
  989. BUG_ON(start > EXT4_BLOCKS_PER_GROUP(sb));
  990. repeat:
  991. if (grp_goal < 0 || !ext4_test_allocatable(grp_goal, bitmap_bh)) {
  992. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  993. if (grp_goal < 0)
  994. goto fail_access;
  995. if (!my_rsv) {
  996. int i;
  997. for (i = 0; i < 7 && grp_goal > start &&
  998. ext4_test_allocatable(grp_goal - 1,
  999. bitmap_bh);
  1000. i++, grp_goal--)
  1001. ;
  1002. }
  1003. }
  1004. start = grp_goal;
  1005. if (!claim_block(sb_bgl_lock(EXT4_SB(sb), group),
  1006. grp_goal, bitmap_bh)) {
  1007. /*
  1008. * The block was allocated by another thread, or it was
  1009. * allocated and then freed by another thread
  1010. */
  1011. start++;
  1012. grp_goal++;
  1013. if (start >= end)
  1014. goto fail_access;
  1015. goto repeat;
  1016. }
  1017. num++;
  1018. grp_goal++;
  1019. while (num < *count && grp_goal < end
  1020. && ext4_test_allocatable(grp_goal, bitmap_bh)
  1021. && claim_block(sb_bgl_lock(EXT4_SB(sb), group),
  1022. grp_goal, bitmap_bh)) {
  1023. num++;
  1024. grp_goal++;
  1025. }
  1026. *count = num;
  1027. return grp_goal - num;
  1028. fail_access:
  1029. *count = num;
  1030. return -1;
  1031. }
  1032. /**
  1033. * find_next_reservable_window():
  1034. * find a reservable space within the given range.
  1035. * It does not allocate the reservation window for now:
  1036. * alloc_new_reservation() will do the work later.
  1037. *
  1038. * @search_head: the head of the searching list;
  1039. * This is not necessarily the list head of the whole filesystem
  1040. *
  1041. * We have both head and start_block to assist the search
  1042. * for the reservable space. The list starts from head,
  1043. * but we will shift to the place where start_block is,
  1044. * then start from there, when looking for a reservable space.
  1045. *
  1046. * @size: the target new reservation window size
  1047. *
  1048. * @group_first_block: the first block we consider to start
  1049. * the real search from
  1050. *
  1051. * @last_block:
  1052. * the maximum block number that our goal reservable space
  1053. * could start from. This is normally the last block in this
  1054. * group. The search will end when we found the start of next
  1055. * possible reservable space is out of this boundary.
  1056. * This could handle the cross boundary reservation window
  1057. * request.
  1058. *
  1059. * basically we search from the given range, rather than the whole
  1060. * reservation double linked list, (start_block, last_block)
  1061. * to find a free region that is of my size and has not
  1062. * been reserved.
  1063. *
  1064. */
  1065. static int find_next_reservable_window(
  1066. struct ext4_reserve_window_node *search_head,
  1067. struct ext4_reserve_window_node *my_rsv,
  1068. struct super_block *sb,
  1069. ext4_fsblk_t start_block,
  1070. ext4_fsblk_t last_block)
  1071. {
  1072. struct rb_node *next;
  1073. struct ext4_reserve_window_node *rsv, *prev;
  1074. ext4_fsblk_t cur;
  1075. int size = my_rsv->rsv_goal_size;
  1076. /* TODO: make the start of the reservation window byte-aligned */
  1077. /* cur = *start_block & ~7;*/
  1078. cur = start_block;
  1079. rsv = search_head;
  1080. if (!rsv)
  1081. return -1;
  1082. while (1) {
  1083. if (cur <= rsv->rsv_end)
  1084. cur = rsv->rsv_end + 1;
  1085. /* TODO?
  1086. * in the case we could not find a reservable space
  1087. * that is what is expected, during the re-search, we could
  1088. * remember what's the largest reservable space we could have
  1089. * and return that one.
  1090. *
  1091. * For now it will fail if we could not find the reservable
  1092. * space with expected-size (or more)...
  1093. */
  1094. if (cur > last_block)
  1095. return -1; /* fail */
  1096. prev = rsv;
  1097. next = rb_next(&rsv->rsv_node);
  1098. rsv = rb_entry(next, struct ext4_reserve_window_node, rsv_node);
  1099. /*
  1100. * Reached the last reservation, we can just append to the
  1101. * previous one.
  1102. */
  1103. if (!next)
  1104. break;
  1105. if (cur + size <= rsv->rsv_start) {
  1106. /*
  1107. * Found a reserveable space big enough. We could
  1108. * have a reservation across the group boundary here
  1109. */
  1110. break;
  1111. }
  1112. }
  1113. /*
  1114. * we come here either :
  1115. * when we reach the end of the whole list,
  1116. * and there is empty reservable space after last entry in the list.
  1117. * append it to the end of the list.
  1118. *
  1119. * or we found one reservable space in the middle of the list,
  1120. * return the reservation window that we could append to.
  1121. * succeed.
  1122. */
  1123. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  1124. rsv_window_remove(sb, my_rsv);
  1125. /*
  1126. * Let's book the whole avaliable window for now. We will check the
  1127. * disk bitmap later and then, if there are free blocks then we adjust
  1128. * the window size if it's larger than requested.
  1129. * Otherwise, we will remove this node from the tree next time
  1130. * call find_next_reservable_window.
  1131. */
  1132. my_rsv->rsv_start = cur;
  1133. my_rsv->rsv_end = cur + size - 1;
  1134. my_rsv->rsv_alloc_hit = 0;
  1135. if (prev != my_rsv)
  1136. ext4_rsv_window_add(sb, my_rsv);
  1137. return 0;
  1138. }
  1139. /**
  1140. * alloc_new_reservation()--allocate a new reservation window
  1141. *
  1142. * To make a new reservation, we search part of the filesystem
  1143. * reservation list (the list that inside the group). We try to
  1144. * allocate a new reservation window near the allocation goal,
  1145. * or the beginning of the group, if there is no goal.
  1146. *
  1147. * We first find a reservable space after the goal, then from
  1148. * there, we check the bitmap for the first free block after
  1149. * it. If there is no free block until the end of group, then the
  1150. * whole group is full, we failed. Otherwise, check if the free
  1151. * block is inside the expected reservable space, if so, we
  1152. * succeed.
  1153. * If the first free block is outside the reservable space, then
  1154. * start from the first free block, we search for next available
  1155. * space, and go on.
  1156. *
  1157. * on succeed, a new reservation will be found and inserted into the list
  1158. * It contains at least one free block, and it does not overlap with other
  1159. * reservation windows.
  1160. *
  1161. * failed: we failed to find a reservation window in this group
  1162. *
  1163. * @rsv: the reservation
  1164. *
  1165. * @grp_goal: The goal (group-relative). It is where the search for a
  1166. * free reservable space should start from.
  1167. * if we have a grp_goal(grp_goal >0 ), then start from there,
  1168. * no grp_goal(grp_goal = -1), we start from the first block
  1169. * of the group.
  1170. *
  1171. * @sb: the super block
  1172. * @group: the group we are trying to allocate in
  1173. * @bitmap_bh: the block group block bitmap
  1174. *
  1175. */
  1176. static int alloc_new_reservation(struct ext4_reserve_window_node *my_rsv,
  1177. ext4_grpblk_t grp_goal, struct super_block *sb,
  1178. ext4_group_t group, struct buffer_head *bitmap_bh)
  1179. {
  1180. struct ext4_reserve_window_node *search_head;
  1181. ext4_fsblk_t group_first_block, group_end_block, start_block;
  1182. ext4_grpblk_t first_free_block;
  1183. struct rb_root *fs_rsv_root = &EXT4_SB(sb)->s_rsv_window_root;
  1184. unsigned long size;
  1185. int ret;
  1186. spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock;
  1187. group_first_block = ext4_group_first_block_no(sb, group);
  1188. group_end_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1189. if (grp_goal < 0)
  1190. start_block = group_first_block;
  1191. else
  1192. start_block = grp_goal + group_first_block;
  1193. size = my_rsv->rsv_goal_size;
  1194. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  1195. /*
  1196. * if the old reservation is cross group boundary
  1197. * and if the goal is inside the old reservation window,
  1198. * we will come here when we just failed to allocate from
  1199. * the first part of the window. We still have another part
  1200. * that belongs to the next group. In this case, there is no
  1201. * point to discard our window and try to allocate a new one
  1202. * in this group(which will fail). we should
  1203. * keep the reservation window, just simply move on.
  1204. *
  1205. * Maybe we could shift the start block of the reservation
  1206. * window to the first block of next group.
  1207. */
  1208. if ((my_rsv->rsv_start <= group_end_block) &&
  1209. (my_rsv->rsv_end > group_end_block) &&
  1210. (start_block >= my_rsv->rsv_start))
  1211. return -1;
  1212. if ((my_rsv->rsv_alloc_hit >
  1213. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  1214. /*
  1215. * if the previously allocation hit ratio is
  1216. * greater than 1/2, then we double the size of
  1217. * the reservation window the next time,
  1218. * otherwise we keep the same size window
  1219. */
  1220. size = size * 2;
  1221. if (size > EXT4_MAX_RESERVE_BLOCKS)
  1222. size = EXT4_MAX_RESERVE_BLOCKS;
  1223. my_rsv->rsv_goal_size = size;
  1224. }
  1225. }
  1226. spin_lock(rsv_lock);
  1227. /*
  1228. * shift the search start to the window near the goal block
  1229. */
  1230. search_head = search_reserve_window(fs_rsv_root, start_block);
  1231. /*
  1232. * find_next_reservable_window() simply finds a reservable window
  1233. * inside the given range(start_block, group_end_block).
  1234. *
  1235. * To make sure the reservation window has a free bit inside it, we
  1236. * need to check the bitmap after we found a reservable window.
  1237. */
  1238. retry:
  1239. ret = find_next_reservable_window(search_head, my_rsv, sb,
  1240. start_block, group_end_block);
  1241. if (ret == -1) {
  1242. if (!rsv_is_empty(&my_rsv->rsv_window))
  1243. rsv_window_remove(sb, my_rsv);
  1244. spin_unlock(rsv_lock);
  1245. return -1;
  1246. }
  1247. /*
  1248. * On success, find_next_reservable_window() returns the
  1249. * reservation window where there is a reservable space after it.
  1250. * Before we reserve this reservable space, we need
  1251. * to make sure there is at least a free block inside this region.
  1252. *
  1253. * searching the first free bit on the block bitmap and copy of
  1254. * last committed bitmap alternatively, until we found a allocatable
  1255. * block. Search start from the start block of the reservable space
  1256. * we just found.
  1257. */
  1258. spin_unlock(rsv_lock);
  1259. first_free_block = bitmap_search_next_usable_block(
  1260. my_rsv->rsv_start - group_first_block,
  1261. bitmap_bh, group_end_block - group_first_block + 1);
  1262. if (first_free_block < 0) {
  1263. /*
  1264. * no free block left on the bitmap, no point
  1265. * to reserve the space. return failed.
  1266. */
  1267. spin_lock(rsv_lock);
  1268. if (!rsv_is_empty(&my_rsv->rsv_window))
  1269. rsv_window_remove(sb, my_rsv);
  1270. spin_unlock(rsv_lock);
  1271. return -1; /* failed */
  1272. }
  1273. start_block = first_free_block + group_first_block;
  1274. /*
  1275. * check if the first free block is within the
  1276. * free space we just reserved
  1277. */
  1278. if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
  1279. return 0; /* success */
  1280. /*
  1281. * if the first free bit we found is out of the reservable space
  1282. * continue search for next reservable space,
  1283. * start from where the free block is,
  1284. * we also shift the list head to where we stopped last time
  1285. */
  1286. search_head = my_rsv;
  1287. spin_lock(rsv_lock);
  1288. goto retry;
  1289. }
  1290. /**
  1291. * try_to_extend_reservation()
  1292. * @my_rsv: given reservation window
  1293. * @sb: super block
  1294. * @size: the delta to extend
  1295. *
  1296. * Attempt to expand the reservation window large enough to have
  1297. * required number of free blocks
  1298. *
  1299. * Since ext4_try_to_allocate() will always allocate blocks within
  1300. * the reservation window range, if the window size is too small,
  1301. * multiple blocks allocation has to stop at the end of the reservation
  1302. * window. To make this more efficient, given the total number of
  1303. * blocks needed and the current size of the window, we try to
  1304. * expand the reservation window size if necessary on a best-effort
  1305. * basis before ext4_new_blocks() tries to allocate blocks,
  1306. */
  1307. static void try_to_extend_reservation(struct ext4_reserve_window_node *my_rsv,
  1308. struct super_block *sb, int size)
  1309. {
  1310. struct ext4_reserve_window_node *next_rsv;
  1311. struct rb_node *next;
  1312. spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock;
  1313. if (!spin_trylock(rsv_lock))
  1314. return;
  1315. next = rb_next(&my_rsv->rsv_node);
  1316. if (!next)
  1317. my_rsv->rsv_end += size;
  1318. else {
  1319. next_rsv = rb_entry(next, struct ext4_reserve_window_node, rsv_node);
  1320. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  1321. my_rsv->rsv_end += size;
  1322. else
  1323. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  1324. }
  1325. spin_unlock(rsv_lock);
  1326. }
  1327. /**
  1328. * ext4_try_to_allocate_with_rsv()
  1329. * @sb: superblock
  1330. * @handle: handle to this transaction
  1331. * @group: given allocation block group
  1332. * @bitmap_bh: bufferhead holds the block bitmap
  1333. * @grp_goal: given target block within the group
  1334. * @count: target number of blocks to allocate
  1335. * @my_rsv: reservation window
  1336. * @errp: pointer to store the error code
  1337. *
  1338. * This is the main function used to allocate a new block and its reservation
  1339. * window.
  1340. *
  1341. * Each time when a new block allocation is need, first try to allocate from
  1342. * its own reservation. If it does not have a reservation window, instead of
  1343. * looking for a free bit on bitmap first, then look up the reservation list to
  1344. * see if it is inside somebody else's reservation window, we try to allocate a
  1345. * reservation window for it starting from the goal first. Then do the block
  1346. * allocation within the reservation window.
  1347. *
  1348. * This will avoid keeping on searching the reservation list again and
  1349. * again when somebody is looking for a free block (without
  1350. * reservation), and there are lots of free blocks, but they are all
  1351. * being reserved.
  1352. *
  1353. * We use a red-black tree for the per-filesystem reservation list.
  1354. *
  1355. */
  1356. static ext4_grpblk_t
  1357. ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
  1358. ext4_group_t group, struct buffer_head *bitmap_bh,
  1359. ext4_grpblk_t grp_goal,
  1360. struct ext4_reserve_window_node *my_rsv,
  1361. unsigned long *count, int *errp)
  1362. {
  1363. ext4_fsblk_t group_first_block, group_last_block;
  1364. ext4_grpblk_t ret = 0;
  1365. int fatal;
  1366. unsigned long num = *count;
  1367. *errp = 0;
  1368. /*
  1369. * Make sure we use undo access for the bitmap, because it is critical
  1370. * that we do the frozen_data COW on bitmap buffers in all cases even
  1371. * if the buffer is in BJ_Forget state in the committing transaction.
  1372. */
  1373. BUFFER_TRACE(bitmap_bh, "get undo access for new block");
  1374. fatal = ext4_journal_get_undo_access(handle, bitmap_bh);
  1375. if (fatal) {
  1376. *errp = fatal;
  1377. return -1;
  1378. }
  1379. /*
  1380. * we don't deal with reservation when
  1381. * filesystem is mounted without reservation
  1382. * or the file is not a regular file
  1383. * or last attempt to allocate a block with reservation turned on failed
  1384. */
  1385. if (my_rsv == NULL) {
  1386. ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh,
  1387. grp_goal, count, NULL);
  1388. goto out;
  1389. }
  1390. /*
  1391. * grp_goal is a group relative block number (if there is a goal)
  1392. * 0 <= grp_goal < EXT4_BLOCKS_PER_GROUP(sb)
  1393. * first block is a filesystem wide block number
  1394. * first block is the block number of the first block in this group
  1395. */
  1396. group_first_block = ext4_group_first_block_no(sb, group);
  1397. group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1398. /*
  1399. * Basically we will allocate a new block from inode's reservation
  1400. * window.
  1401. *
  1402. * We need to allocate a new reservation window, if:
  1403. * a) inode does not have a reservation window; or
  1404. * b) last attempt to allocate a block from existing reservation
  1405. * failed; or
  1406. * c) we come here with a goal and with a reservation window
  1407. *
  1408. * We do not need to allocate a new reservation window if we come here
  1409. * at the beginning with a goal and the goal is inside the window, or
  1410. * we don't have a goal but already have a reservation window.
  1411. * then we could go to allocate from the reservation window directly.
  1412. */
  1413. while (1) {
  1414. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  1415. !goal_in_my_reservation(&my_rsv->rsv_window,
  1416. grp_goal, group, sb)) {
  1417. if (my_rsv->rsv_goal_size < *count)
  1418. my_rsv->rsv_goal_size = *count;
  1419. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  1420. group, bitmap_bh);
  1421. if (ret < 0)
  1422. break; /* failed */
  1423. if (!goal_in_my_reservation(&my_rsv->rsv_window,
  1424. grp_goal, group, sb))
  1425. grp_goal = -1;
  1426. } else if (grp_goal >= 0) {
  1427. int curr = my_rsv->rsv_end -
  1428. (grp_goal + group_first_block) + 1;
  1429. if (curr < *count)
  1430. try_to_extend_reservation(my_rsv, sb,
  1431. *count - curr);
  1432. }
  1433. if ((my_rsv->rsv_start > group_last_block) ||
  1434. (my_rsv->rsv_end < group_first_block)) {
  1435. rsv_window_dump(&EXT4_SB(sb)->s_rsv_window_root, 1);
  1436. BUG();
  1437. }
  1438. ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh,
  1439. grp_goal, &num, &my_rsv->rsv_window);
  1440. if (ret >= 0) {
  1441. my_rsv->rsv_alloc_hit += num;
  1442. *count = num;
  1443. break; /* succeed */
  1444. }
  1445. num = *count;
  1446. }
  1447. out:
  1448. if (ret >= 0) {
  1449. BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for "
  1450. "bitmap block");
  1451. fatal = ext4_journal_dirty_metadata(handle, bitmap_bh);
  1452. if (fatal) {
  1453. *errp = fatal;
  1454. return -1;
  1455. }
  1456. return ret;
  1457. }
  1458. BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
  1459. ext4_journal_release_buffer(handle, bitmap_bh);
  1460. return ret;
  1461. }
  1462. int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
  1463. s64 nblocks)
  1464. {
  1465. s64 free_blocks, dirty_blocks;
  1466. s64 root_blocks = 0;
  1467. struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
  1468. struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
  1469. free_blocks = percpu_counter_read_positive(fbc);
  1470. dirty_blocks = percpu_counter_read_positive(dbc);
  1471. if (!capable(CAP_SYS_RESOURCE) &&
  1472. sbi->s_resuid != current->fsuid &&
  1473. (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid)))
  1474. root_blocks = ext4_r_blocks_count(sbi->s_es);
  1475. if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
  1476. EXT4_FREEBLOCKS_WATERMARK) {
  1477. free_blocks = percpu_counter_sum(fbc);
  1478. dirty_blocks = percpu_counter_sum(dbc);
  1479. if (dirty_blocks < 0) {
  1480. printk(KERN_CRIT "Dirty block accounting "
  1481. "went wrong %lld\n",
  1482. dirty_blocks);
  1483. }
  1484. }
  1485. /* Check whether we have space after
  1486. * accounting for current dirty blocks
  1487. */
  1488. if (free_blocks < ((root_blocks + nblocks) + dirty_blocks))
  1489. /* we don't have free space */
  1490. return -ENOSPC;
  1491. /* Add the blocks to nblocks */
  1492. percpu_counter_add(dbc, nblocks);
  1493. return 0;
  1494. }
  1495. /**
  1496. * ext4_has_free_blocks()
  1497. * @sbi: in-core super block structure.
  1498. * @nblocks: number of neeed blocks
  1499. *
  1500. * Check if filesystem has free blocks available for allocation.
  1501. * Return the number of blocks avaible for allocation for this request
  1502. * On success, return nblocks
  1503. */
  1504. ext4_fsblk_t ext4_has_free_blocks(struct ext4_sb_info *sbi,
  1505. s64 nblocks)
  1506. {
  1507. s64 free_blocks, dirty_blocks;
  1508. s64 root_blocks = 0;
  1509. struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
  1510. struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
  1511. free_blocks = percpu_counter_read_positive(fbc);
  1512. dirty_blocks = percpu_counter_read_positive(dbc);
  1513. if (!capable(CAP_SYS_RESOURCE) &&
  1514. sbi->s_resuid != current->fsuid &&
  1515. (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid)))
  1516. root_blocks = ext4_r_blocks_count(sbi->s_es);
  1517. if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
  1518. EXT4_FREEBLOCKS_WATERMARK) {
  1519. free_blocks = percpu_counter_sum(fbc);
  1520. dirty_blocks = percpu_counter_sum(dbc);
  1521. }
  1522. if (free_blocks <= (root_blocks + dirty_blocks))
  1523. /* we don't have free space */
  1524. return 0;
  1525. if (free_blocks - (root_blocks + dirty_blocks) < nblocks)
  1526. return free_blocks - (root_blocks + dirty_blocks);
  1527. return nblocks;
  1528. }
  1529. /**
  1530. * ext4_should_retry_alloc()
  1531. * @sb: super block
  1532. * @retries number of attemps has been made
  1533. *
  1534. * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
  1535. * it is profitable to retry the operation, this function will wait
  1536. * for the current or commiting transaction to complete, and then
  1537. * return TRUE.
  1538. *
  1539. * if the total number of retries exceed three times, return FALSE.
  1540. */
  1541. int ext4_should_retry_alloc(struct super_block *sb, int *retries)
  1542. {
  1543. if (!ext4_has_free_blocks(EXT4_SB(sb), 1) || (*retries)++ > 3)
  1544. return 0;
  1545. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  1546. return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
  1547. }
  1548. /**
  1549. * ext4_old_new_blocks() -- core block bitmap based block allocation function
  1550. *
  1551. * @handle: handle to this transaction
  1552. * @inode: file inode
  1553. * @goal: given target block(filesystem wide)
  1554. * @count: target number of blocks to allocate
  1555. * @errp: error code
  1556. *
  1557. * ext4_old_new_blocks uses a goal block to assist allocation and look up
  1558. * the block bitmap directly to do block allocation. It tries to
  1559. * allocate block(s) from the block group contains the goal block first. If
  1560. * that fails, it will try to allocate block(s) from other block groups
  1561. * without any specific goal block.
  1562. *
  1563. * This function is called when -o nomballoc mount option is enabled
  1564. *
  1565. */
  1566. ext4_fsblk_t ext4_old_new_blocks(handle_t *handle, struct inode *inode,
  1567. ext4_fsblk_t goal, unsigned long *count, int *errp)
  1568. {
  1569. struct buffer_head *bitmap_bh = NULL;
  1570. struct buffer_head *gdp_bh;
  1571. ext4_group_t group_no;
  1572. ext4_group_t goal_group;
  1573. ext4_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1574. ext4_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1575. ext4_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1576. ext4_group_t bgi; /* blockgroup iteration index */
  1577. int fatal = 0, err;
  1578. int performed_allocation = 0;
  1579. ext4_grpblk_t free_blocks; /* number of free blocks in a group */
  1580. struct super_block *sb;
  1581. struct ext4_group_desc *gdp;
  1582. struct ext4_super_block *es;
  1583. struct ext4_sb_info *sbi;
  1584. struct ext4_reserve_window_node *my_rsv = NULL;
  1585. struct ext4_block_alloc_info *block_i;
  1586. unsigned short windowsz = 0;
  1587. ext4_group_t ngroups;
  1588. unsigned long num = *count;
  1589. sb = inode->i_sb;
  1590. if (!sb) {
  1591. *errp = -ENODEV;
  1592. printk(KERN_ERR "ext4_new_block: nonexistent superblock");
  1593. return 0;
  1594. }
  1595. sbi = EXT4_SB(sb);
  1596. if (!EXT4_I(inode)->i_delalloc_reserved_flag) {
  1597. /*
  1598. * With delalloc we already reserved the blocks
  1599. */
  1600. while (*count && ext4_claim_free_blocks(sbi, *count)) {
  1601. /* let others to free the space */
  1602. yield();
  1603. *count = *count >> 1;
  1604. }
  1605. if (!*count) {
  1606. *errp = -ENOSPC;
  1607. return 0; /*return with ENOSPC error */
  1608. }
  1609. num = *count;
  1610. }
  1611. /*
  1612. * Check quota for allocation of this block.
  1613. */
  1614. if (DQUOT_ALLOC_BLOCK(inode, num)) {
  1615. *errp = -EDQUOT;
  1616. return 0;
  1617. }
  1618. sbi = EXT4_SB(sb);
  1619. es = EXT4_SB(sb)->s_es;
  1620. ext4_debug("goal=%llu.\n", goal);
  1621. /*
  1622. * Allocate a block from reservation only when
  1623. * filesystem is mounted with reservation(default,-o reservation), and
  1624. * it's a regular file, and
  1625. * the desired window size is greater than 0 (One could use ioctl
  1626. * command EXT4_IOC_SETRSVSZ to set the window size to 0 to turn off
  1627. * reservation on that particular file)
  1628. */
  1629. block_i = EXT4_I(inode)->i_block_alloc_info;
  1630. if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
  1631. my_rsv = &block_i->rsv_window_node;
  1632. /*
  1633. * First, test whether the goal block is free.
  1634. */
  1635. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1636. goal >= ext4_blocks_count(es))
  1637. goal = le32_to_cpu(es->s_first_data_block);
  1638. ext4_get_group_no_and_offset(sb, goal, &group_no, &grp_target_blk);
  1639. goal_group = group_no;
  1640. retry_alloc:
  1641. gdp = ext4_get_group_desc(sb, group_no, &gdp_bh);
  1642. if (!gdp)
  1643. goto io_error;
  1644. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1645. /*
  1646. * if there is not enough free blocks to make a new resevation
  1647. * turn off reservation for this allocation
  1648. */
  1649. if (my_rsv && (free_blocks < windowsz)
  1650. && (rsv_is_empty(&my_rsv->rsv_window)))
  1651. my_rsv = NULL;
  1652. if (free_blocks > 0) {
  1653. bitmap_bh = ext4_read_block_bitmap(sb, group_no);
  1654. if (!bitmap_bh)
  1655. goto io_error;
  1656. grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle,
  1657. group_no, bitmap_bh, grp_target_blk,
  1658. my_rsv, &num, &fatal);
  1659. if (fatal)
  1660. goto out;
  1661. if (grp_alloc_blk >= 0)
  1662. goto allocated;
  1663. }
  1664. ngroups = EXT4_SB(sb)->s_groups_count;
  1665. smp_rmb();
  1666. /*
  1667. * Now search the rest of the groups. We assume that
  1668. * group_no and gdp correctly point to the last group visited.
  1669. */
  1670. for (bgi = 0; bgi < ngroups; bgi++) {
  1671. group_no++;
  1672. if (group_no >= ngroups)
  1673. group_no = 0;
  1674. gdp = ext4_get_group_desc(sb, group_no, &gdp_bh);
  1675. if (!gdp)
  1676. goto io_error;
  1677. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1678. /*
  1679. * skip this group if the number of
  1680. * free blocks is less than half of the reservation
  1681. * window size.
  1682. */
  1683. if (free_blocks <= (windowsz/2))
  1684. continue;
  1685. brelse(bitmap_bh);
  1686. bitmap_bh = ext4_read_block_bitmap(sb, group_no);
  1687. if (!bitmap_bh)
  1688. goto io_error;
  1689. /*
  1690. * try to allocate block(s) from this group, without a goal(-1).
  1691. */
  1692. grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle,
  1693. group_no, bitmap_bh, -1, my_rsv,
  1694. &num, &fatal);
  1695. if (fatal)
  1696. goto out;
  1697. if (grp_alloc_blk >= 0)
  1698. goto allocated;
  1699. }
  1700. /*
  1701. * We may end up a bogus ealier ENOSPC error due to
  1702. * filesystem is "full" of reservations, but
  1703. * there maybe indeed free blocks avaliable on disk
  1704. * In this case, we just forget about the reservations
  1705. * just do block allocation as without reservations.
  1706. */
  1707. if (my_rsv) {
  1708. my_rsv = NULL;
  1709. windowsz = 0;
  1710. group_no = goal_group;
  1711. goto retry_alloc;
  1712. }
  1713. /* No space left on the device */
  1714. *errp = -ENOSPC;
  1715. goto out;
  1716. allocated:
  1717. ext4_debug("using block group %lu(%d)\n",
  1718. group_no, gdp->bg_free_blocks_count);
  1719. BUFFER_TRACE(gdp_bh, "get_write_access");
  1720. fatal = ext4_journal_get_write_access(handle, gdp_bh);
  1721. if (fatal)
  1722. goto out;
  1723. ret_block = grp_alloc_blk + ext4_group_first_block_no(sb, group_no);
  1724. if (in_range(ext4_block_bitmap(sb, gdp), ret_block, num) ||
  1725. in_range(ext4_inode_bitmap(sb, gdp), ret_block, num) ||
  1726. in_range(ret_block, ext4_inode_table(sb, gdp),
  1727. EXT4_SB(sb)->s_itb_per_group) ||
  1728. in_range(ret_block + num - 1, ext4_inode_table(sb, gdp),
  1729. EXT4_SB(sb)->s_itb_per_group)) {
  1730. ext4_error(sb, "ext4_new_block",
  1731. "Allocating block in system zone - "
  1732. "blocks from %llu, length %lu",
  1733. ret_block, num);
  1734. /*
  1735. * claim_block marked the blocks we allocated
  1736. * as in use. So we may want to selectively
  1737. * mark some of the blocks as free
  1738. */
  1739. goto retry_alloc;
  1740. }
  1741. performed_allocation = 1;
  1742. #ifdef CONFIG_JBD2_DEBUG
  1743. {
  1744. struct buffer_head *debug_bh;
  1745. /* Record bitmap buffer state in the newly allocated block */
  1746. debug_bh = sb_find_get_block(sb, ret_block);
  1747. if (debug_bh) {
  1748. BUFFER_TRACE(debug_bh, "state when allocated");
  1749. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
  1750. brelse(debug_bh);
  1751. }
  1752. }
  1753. jbd_lock_bh_state(bitmap_bh);
  1754. spin_lock(sb_bgl_lock(sbi, group_no));
  1755. if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
  1756. int i;
  1757. for (i = 0; i < num; i++) {
  1758. if (ext4_test_bit(grp_alloc_blk+i,
  1759. bh2jh(bitmap_bh)->b_committed_data)) {
  1760. printk(KERN_ERR "%s: block was unexpectedly "
  1761. "set in b_committed_data\n", __func__);
  1762. }
  1763. }
  1764. }
  1765. ext4_debug("found bit %d\n", grp_alloc_blk);
  1766. spin_unlock(sb_bgl_lock(sbi, group_no));
  1767. jbd_unlock_bh_state(bitmap_bh);
  1768. #endif
  1769. if (ret_block + num - 1 >= ext4_blocks_count(es)) {
  1770. ext4_error(sb, "ext4_new_block",
  1771. "block(%llu) >= blocks count(%llu) - "
  1772. "block_group = %lu, es == %p ", ret_block,
  1773. ext4_blocks_count(es), group_no, es);
  1774. goto out;
  1775. }
  1776. /*
  1777. * It is up to the caller to add the new buffer to a journal
  1778. * list of some description. We don't know in advance whether
  1779. * the caller wants to use it as metadata or data.
  1780. */
  1781. spin_lock(sb_bgl_lock(sbi, group_no));
  1782. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
  1783. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  1784. le16_add_cpu(&gdp->bg_free_blocks_count, -num);
  1785. gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp);
  1786. spin_unlock(sb_bgl_lock(sbi, group_no));
  1787. percpu_counter_sub(&sbi->s_freeblocks_counter, num);
  1788. /*
  1789. * Now reduce the dirty block count also. Should not go negative
  1790. */
  1791. if (!EXT4_I(inode)->i_delalloc_reserved_flag)
  1792. percpu_counter_sub(&sbi->s_dirtyblocks_counter, *count);
  1793. else
  1794. percpu_counter_sub(&sbi->s_dirtyblocks_counter, num);
  1795. if (sbi->s_log_groups_per_flex) {
  1796. ext4_group_t flex_group = ext4_flex_group(sbi, group_no);
  1797. spin_lock(sb_bgl_lock(sbi, flex_group));
  1798. sbi->s_flex_groups[flex_group].free_blocks -= num;
  1799. spin_unlock(sb_bgl_lock(sbi, flex_group));
  1800. }
  1801. BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
  1802. err = ext4_journal_dirty_metadata(handle, gdp_bh);
  1803. if (!fatal)
  1804. fatal = err;
  1805. sb->s_dirt = 1;
  1806. if (fatal)
  1807. goto out;
  1808. *errp = 0;
  1809. brelse(bitmap_bh);
  1810. DQUOT_FREE_BLOCK(inode, *count-num);
  1811. *count = num;
  1812. return ret_block;
  1813. io_error:
  1814. *errp = -EIO;
  1815. out:
  1816. if (fatal) {
  1817. *errp = fatal;
  1818. ext4_std_error(sb, fatal);
  1819. }
  1820. /*
  1821. * Undo the block allocation
  1822. */
  1823. if (!performed_allocation)
  1824. DQUOT_FREE_BLOCK(inode, *count);
  1825. brelse(bitmap_bh);
  1826. return 0;
  1827. }
  1828. #define EXT4_META_BLOCK 0x1
  1829. static ext4_fsblk_t do_blk_alloc(handle_t *handle, struct inode *inode,
  1830. ext4_lblk_t iblock, ext4_fsblk_t goal,
  1831. unsigned long *count, int *errp, int flags)
  1832. {
  1833. struct ext4_allocation_request ar;
  1834. ext4_fsblk_t ret;
  1835. if (!test_opt(inode->i_sb, MBALLOC)) {
  1836. return ext4_old_new_blocks(handle, inode, goal, count, errp);
  1837. }
  1838. memset(&ar, 0, sizeof(ar));
  1839. /* Fill with neighbour allocated blocks */
  1840. ar.inode = inode;
  1841. ar.goal = goal;
  1842. ar.len = *count;
  1843. ar.logical = iblock;
  1844. if (S_ISREG(inode->i_mode) && !(flags & EXT4_META_BLOCK))
  1845. /* enable in-core preallocation for data block allocation */
  1846. ar.flags = EXT4_MB_HINT_DATA;
  1847. else
  1848. /* disable in-core preallocation for non-regular files */
  1849. ar.flags = 0;
  1850. ret = ext4_mb_new_blocks(handle, &ar, errp);
  1851. *count = ar.len;
  1852. return ret;
  1853. }
  1854. /*
  1855. * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
  1856. *
  1857. * @handle: handle to this transaction
  1858. * @inode: file inode
  1859. * @goal: given target block(filesystem wide)
  1860. * @count: total number of blocks need
  1861. * @errp: error code
  1862. *
  1863. * Return 1st allocated block numberon success, *count stores total account
  1864. * error stores in errp pointer
  1865. */
  1866. ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
  1867. ext4_fsblk_t goal, unsigned long *count, int *errp)
  1868. {
  1869. ext4_fsblk_t ret;
  1870. ret = do_blk_alloc(handle, inode, 0, goal,
  1871. count, errp, EXT4_META_BLOCK);
  1872. /*
  1873. * Account for the allocated meta blocks
  1874. */
  1875. if (!(*errp)) {
  1876. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1877. EXT4_I(inode)->i_allocated_meta_blocks += *count;
  1878. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1879. }
  1880. return ret;
  1881. }
  1882. /*
  1883. * ext4_new_meta_block() -- allocate block for meta data (indexing) blocks
  1884. *
  1885. * @handle: handle to this transaction
  1886. * @inode: file inode
  1887. * @goal: given target block(filesystem wide)
  1888. * @errp: error code
  1889. *
  1890. * Return allocated block number on success
  1891. */
  1892. ext4_fsblk_t ext4_new_meta_block(handle_t *handle, struct inode *inode,
  1893. ext4_fsblk_t goal, int *errp)
  1894. {
  1895. unsigned long count = 1;
  1896. return ext4_new_meta_blocks(handle, inode, goal, &count, errp);
  1897. }
  1898. /*
  1899. * ext4_new_blocks() -- allocate data blocks
  1900. *
  1901. * @handle: handle to this transaction
  1902. * @inode: file inode
  1903. * @goal: given target block(filesystem wide)
  1904. * @count: total number of blocks need
  1905. * @errp: error code
  1906. *
  1907. * Return 1st allocated block numberon success, *count stores total account
  1908. * error stores in errp pointer
  1909. */
  1910. ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
  1911. ext4_lblk_t iblock, ext4_fsblk_t goal,
  1912. unsigned long *count, int *errp)
  1913. {
  1914. return do_blk_alloc(handle, inode, iblock, goal, count, errp, 0);
  1915. }
  1916. /**
  1917. * ext4_count_free_blocks() -- count filesystem free blocks
  1918. * @sb: superblock
  1919. *
  1920. * Adds up the number of free blocks from each block group.
  1921. */
  1922. ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
  1923. {
  1924. ext4_fsblk_t desc_count;
  1925. struct ext4_group_desc *gdp;
  1926. ext4_group_t i;
  1927. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  1928. #ifdef EXT4FS_DEBUG
  1929. struct ext4_super_block *es;
  1930. ext4_fsblk_t bitmap_count;
  1931. unsigned long x;
  1932. struct buffer_head *bitmap_bh = NULL;
  1933. es = EXT4_SB(sb)->s_es;
  1934. desc_count = 0;
  1935. bitmap_count = 0;
  1936. gdp = NULL;
  1937. smp_rmb();
  1938. for (i = 0; i < ngroups; i++) {
  1939. gdp = ext4_get_group_desc(sb, i, NULL);
  1940. if (!gdp)
  1941. continue;
  1942. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1943. brelse(bitmap_bh);
  1944. bitmap_bh = ext4_read_block_bitmap(sb, i);
  1945. if (bitmap_bh == NULL)
  1946. continue;
  1947. x = ext4_count_free(bitmap_bh, sb->s_blocksize);
  1948. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1949. i, le16_to_cpu(gdp->bg_free_blocks_count), x);
  1950. bitmap_count += x;
  1951. }
  1952. brelse(bitmap_bh);
  1953. printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
  1954. ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
  1955. desc_count, bitmap_count);
  1956. return bitmap_count;
  1957. #else
  1958. desc_count = 0;
  1959. smp_rmb();
  1960. for (i = 0; i < ngroups; i++) {
  1961. gdp = ext4_get_group_desc(sb, i, NULL);
  1962. if (!gdp)
  1963. continue;
  1964. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1965. }
  1966. return desc_count;
  1967. #endif
  1968. }
  1969. static inline int test_root(ext4_group_t a, int b)
  1970. {
  1971. int num = b;
  1972. while (a > num)
  1973. num *= b;
  1974. return num == a;
  1975. }
  1976. static int ext4_group_sparse(ext4_group_t group)
  1977. {
  1978. if (group <= 1)
  1979. return 1;
  1980. if (!(group & 1))
  1981. return 0;
  1982. return (test_root(group, 7) || test_root(group, 5) ||
  1983. test_root(group, 3));
  1984. }
  1985. /**
  1986. * ext4_bg_has_super - number of blocks used by the superblock in group
  1987. * @sb: superblock for filesystem
  1988. * @group: group number to check
  1989. *
  1990. * Return the number of blocks used by the superblock (primary or backup)
  1991. * in this group. Currently this will be only 0 or 1.
  1992. */
  1993. int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
  1994. {
  1995. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1996. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1997. !ext4_group_sparse(group))
  1998. return 0;
  1999. return 1;
  2000. }
  2001. static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
  2002. ext4_group_t group)
  2003. {
  2004. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  2005. ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
  2006. ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
  2007. if (group == first || group == first + 1 || group == last)
  2008. return 1;
  2009. return 0;
  2010. }
  2011. static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
  2012. ext4_group_t group)
  2013. {
  2014. return ext4_bg_has_super(sb, group) ? EXT4_SB(sb)->s_gdb_count : 0;
  2015. }
  2016. /**
  2017. * ext4_bg_num_gdb - number of blocks used by the group table in group
  2018. * @sb: superblock for filesystem
  2019. * @group: group number to check
  2020. *
  2021. * Return the number of blocks used by the group descriptor table
  2022. * (primary or backup) in this group. In the future there may be a
  2023. * different number of descriptor blocks in each group.
  2024. */
  2025. unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
  2026. {
  2027. unsigned long first_meta_bg =
  2028. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
  2029. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  2030. if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
  2031. metagroup < first_meta_bg)
  2032. return ext4_bg_num_gdb_nometa(sb, group);
  2033. return ext4_bg_num_gdb_meta(sb,group);
  2034. }