balloc.c 56 KB

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