// SPDX-License-Identifier: GPL-2.01/*2* linux/fs/ext2/balloc.c3*4* Copyright (C) 1992, 1993, 1994, 19955* Remy Card ([email protected])6* Laboratoire MASI - Institut Blaise Pascal7* Universite Pierre et Marie Curie (Paris VI)8*9* Enhanced block allocation by Stephen Tweedie ([email protected]), 199310* Big-endian to little-endian byte-swapping/bitmaps by11* David S. Miller ([email protected]), 199512*/1314#include "ext2.h"15#include <linux/quotaops.h>16#include <linux/slab.h>17#include <linux/sched.h>18#include <linux/cred.h>19#include <linux/buffer_head.h>20#include <linux/capability.h>2122/*23* balloc.c contains the blocks allocation and deallocation routines24*/2526/*27* The free blocks are managed by bitmaps. A file system contains several28* blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap29* block for inodes, N blocks for the inode table and data blocks.30*31* The file system contains group descriptors which are located after the32* super block. Each descriptor contains the number of the bitmap block and33* the free blocks count in the block. The descriptors are loaded in memory34* when a file system is mounted (see ext2_fill_super).35*/363738struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,39unsigned int block_group,40struct buffer_head ** bh)41{42unsigned long group_desc;43unsigned long offset;44struct ext2_group_desc * desc;45struct ext2_sb_info *sbi = EXT2_SB(sb);4647if (block_group >= sbi->s_groups_count) {48WARN(1, "block_group >= groups_count - "49"block_group = %d, groups_count = %lu",50block_group, sbi->s_groups_count);5152return NULL;53}5455group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);56offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);57if (!sbi->s_group_desc[group_desc]) {58WARN(1, "Group descriptor not loaded - "59"block_group = %d, group_desc = %lu, desc = %lu",60block_group, group_desc, offset);61return NULL;62}6364desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;65if (bh)66*bh = sbi->s_group_desc[group_desc];67return desc + offset;68}6970static int ext2_valid_block_bitmap(struct super_block *sb,71struct ext2_group_desc *desc,72unsigned int block_group,73struct buffer_head *bh)74{75ext2_grpblk_t offset;76ext2_grpblk_t next_zero_bit;77ext2_fsblk_t bitmap_blk;78ext2_fsblk_t group_first_block;79ext2_grpblk_t max_bit;8081group_first_block = ext2_group_first_block_no(sb, block_group);82max_bit = ext2_group_last_block_no(sb, block_group) - group_first_block;8384/* check whether block bitmap block number is set */85bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);86offset = bitmap_blk - group_first_block;87if (offset < 0 || offset > max_bit ||88!ext2_test_bit(offset, bh->b_data))89/* bad block bitmap */90goto err_out;9192/* check whether the inode bitmap block number is set */93bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);94offset = bitmap_blk - group_first_block;95if (offset < 0 || offset > max_bit ||96!ext2_test_bit(offset, bh->b_data))97/* bad block bitmap */98goto err_out;99100/* check whether the inode table block number is set */101bitmap_blk = le32_to_cpu(desc->bg_inode_table);102offset = bitmap_blk - group_first_block;103if (offset < 0 || offset > max_bit ||104offset + EXT2_SB(sb)->s_itb_per_group - 1 > max_bit)105goto err_out;106next_zero_bit = ext2_find_next_zero_bit(bh->b_data,107offset + EXT2_SB(sb)->s_itb_per_group,108offset);109if (next_zero_bit >= offset + EXT2_SB(sb)->s_itb_per_group)110/* good bitmap for inode tables */111return 1;112113err_out:114ext2_error(sb, __func__,115"Invalid block bitmap - "116"block_group = %d, block = %lu",117block_group, bitmap_blk);118return 0;119}120121/*122* Read the bitmap for a given block_group,and validate the123* bits for block/inode/inode tables are set in the bitmaps124*125* Return buffer_head on success or NULL in case of failure.126*/127static struct buffer_head *128read_block_bitmap(struct super_block *sb, unsigned int block_group)129{130struct ext2_group_desc * desc;131struct buffer_head * bh = NULL;132ext2_fsblk_t bitmap_blk;133int ret;134135desc = ext2_get_group_desc(sb, block_group, NULL);136if (!desc)137return NULL;138bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);139bh = sb_getblk(sb, bitmap_blk);140if (unlikely(!bh)) {141ext2_error(sb, __func__,142"Cannot read block bitmap - "143"block_group = %d, block_bitmap = %u",144block_group, le32_to_cpu(desc->bg_block_bitmap));145return NULL;146}147ret = bh_read(bh, 0);148if (ret > 0)149return bh;150if (ret < 0) {151brelse(bh);152ext2_error(sb, __func__,153"Cannot read block bitmap - "154"block_group = %d, block_bitmap = %u",155block_group, le32_to_cpu(desc->bg_block_bitmap));156return NULL;157}158159ext2_valid_block_bitmap(sb, desc, block_group, bh);160/*161* file system mounted not to panic on error, continue with corrupt162* bitmap163*/164return bh;165}166167static void group_adjust_blocks(struct super_block *sb, int group_no,168struct ext2_group_desc *desc, struct buffer_head *bh, int count)169{170if (count) {171struct ext2_sb_info *sbi = EXT2_SB(sb);172unsigned free_blocks;173174spin_lock(sb_bgl_lock(sbi, group_no));175free_blocks = le16_to_cpu(desc->bg_free_blocks_count);176desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);177spin_unlock(sb_bgl_lock(sbi, group_no));178mark_buffer_dirty(bh);179}180}181182/*183* The reservation window structure operations184* --------------------------------------------185* Operations include:186* dump, find, add, remove, is_empty, find_next_reservable_window, etc.187*188* We use a red-black tree to represent per-filesystem reservation189* windows.190*191*/192193/**194* __rsv_window_dump() -- Dump the filesystem block allocation reservation map195* @root: root of per-filesystem reservation rb tree196* @verbose: verbose mode197* @fn: function which wishes to dump the reservation map198*199* If verbose is turned on, it will print the whole block reservation200* windows(start, end). Otherwise, it will only print out the "bad" windows,201* those windows that overlap with their immediate neighbors.202*/203#if 1204static void __rsv_window_dump(struct rb_root *root, int verbose,205const char *fn)206{207struct rb_node *n;208struct ext2_reserve_window_node *rsv, *prev;209int bad;210211restart:212n = rb_first(root);213bad = 0;214prev = NULL;215216printk("Block Allocation Reservation Windows Map (%s):\n", fn);217while (n) {218rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);219if (verbose)220printk("reservation window 0x%p "221"start: %lu, end: %lu\n",222rsv, rsv->rsv_start, rsv->rsv_end);223if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {224printk("Bad reservation %p (start >= end)\n",225rsv);226bad = 1;227}228if (prev && prev->rsv_end >= rsv->rsv_start) {229printk("Bad reservation %p (prev->end >= start)\n",230rsv);231bad = 1;232}233if (bad) {234if (!verbose) {235printk("Restarting reservation walk in verbose mode\n");236verbose = 1;237goto restart;238}239}240n = rb_next(n);241prev = rsv;242}243printk("Window map complete.\n");244BUG_ON(bad);245}246#define rsv_window_dump(root, verbose) \247__rsv_window_dump((root), (verbose), __func__)248#else249#define rsv_window_dump(root, verbose) do {} while (0)250#endif251252/**253* goal_in_my_reservation()254* @rsv: inode's reservation window255* @grp_goal: given goal block relative to the allocation block group256* @group: the current allocation block group257* @sb: filesystem super block258*259* Test if the given goal block (group relative) is within the file's260* own block reservation window range.261*262* If the reservation window is outside the goal allocation group, return 0;263* grp_goal (given goal block) could be -1, which means no specific264* goal block. In this case, always return 1.265* If the goal block is within the reservation window, return 1;266* otherwise, return 0;267*/268static int269goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,270unsigned int group, struct super_block * sb)271{272ext2_fsblk_t group_first_block, group_last_block;273274group_first_block = ext2_group_first_block_no(sb, group);275group_last_block = ext2_group_last_block_no(sb, group);276277if ((rsv->_rsv_start > group_last_block) ||278(rsv->_rsv_end < group_first_block))279return 0;280if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)281|| (grp_goal + group_first_block > rsv->_rsv_end)))282return 0;283return 1;284}285286/**287* search_reserve_window()288* @root: root of reservation tree289* @goal: target allocation block290*291* Find the reserved window which includes the goal, or the previous one292* if the goal is not in any window.293* Returns NULL if there are no windows or if all windows start after the goal.294*/295static struct ext2_reserve_window_node *296search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)297{298struct rb_node *n = root->rb_node;299struct ext2_reserve_window_node *rsv;300301if (!n)302return NULL;303304do {305rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);306307if (goal < rsv->rsv_start)308n = n->rb_left;309else if (goal > rsv->rsv_end)310n = n->rb_right;311else312return rsv;313} while (n);314/*315* We've fallen off the end of the tree: the goal wasn't inside316* any particular node. OK, the previous node must be to one317* side of the interval containing the goal. If it's the RHS,318* we need to back up one.319*/320if (rsv->rsv_start > goal) {321n = rb_prev(&rsv->rsv_node);322rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);323}324return rsv;325}326327/*328* ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.329* @sb: super block330* @rsv: reservation window to add331*332* Must be called with rsv_lock held.333*/334void ext2_rsv_window_add(struct super_block *sb,335struct ext2_reserve_window_node *rsv)336{337struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;338struct rb_node *node = &rsv->rsv_node;339ext2_fsblk_t start = rsv->rsv_start;340341struct rb_node ** p = &root->rb_node;342struct rb_node * parent = NULL;343struct ext2_reserve_window_node *this;344345while (*p)346{347parent = *p;348this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);349350if (start < this->rsv_start)351p = &(*p)->rb_left;352else if (start > this->rsv_end)353p = &(*p)->rb_right;354else {355rsv_window_dump(root, 1);356BUG();357}358}359360rb_link_node(node, parent, p);361rb_insert_color(node, root);362}363364/**365* rsv_window_remove() -- unlink a window from the reservation rb tree366* @sb: super block367* @rsv: reservation window to remove368*369* Mark the block reservation window as not allocated, and unlink it370* from the filesystem reservation window rb tree. Must be called with371* rsv_lock held.372*/373static void rsv_window_remove(struct super_block *sb,374struct ext2_reserve_window_node *rsv)375{376rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;377rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;378rsv->rsv_alloc_hit = 0;379rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);380}381382/*383* rsv_is_empty() -- Check if the reservation window is allocated.384* @rsv: given reservation window to check385*386* returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.387*/388static inline int rsv_is_empty(struct ext2_reserve_window *rsv)389{390/* a valid reservation end block could not be 0 */391return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);392}393394/**395* ext2_init_block_alloc_info()396* @inode: file inode structure397*398* Allocate and initialize the reservation window structure, and399* link the window to the ext2 inode structure at last400*401* The reservation window structure is only dynamically allocated402* and linked to ext2 inode the first time the open file403* needs a new block. So, before every ext2_new_block(s) call, for404* regular files, we should check whether the reservation window405* structure exists or not. In the latter case, this function is called.406* Fail to do so will result in block reservation being turned off for that407* open file.408*409* This function is called from ext2_get_blocks_handle(), also called410* when setting the reservation window size through ioctl before the file411* is open for write (needs block allocation).412*413* Needs truncate_mutex protection prior to calling this function.414*/415void ext2_init_block_alloc_info(struct inode *inode)416{417struct ext2_inode_info *ei = EXT2_I(inode);418struct ext2_block_alloc_info *block_i;419struct super_block *sb = inode->i_sb;420421block_i = kmalloc(sizeof(*block_i), GFP_KERNEL);422if (block_i) {423struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;424425rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;426rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;427428/*429* if filesystem is mounted with NORESERVATION, the goal430* reservation window size is set to zero to indicate431* block reservation is off432*/433if (!test_opt(sb, RESERVATION))434rsv->rsv_goal_size = 0;435else436rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;437rsv->rsv_alloc_hit = 0;438block_i->last_alloc_logical_block = 0;439block_i->last_alloc_physical_block = 0;440}441ei->i_block_alloc_info = block_i;442}443444/**445* ext2_discard_reservation()446* @inode: inode447*448* Discard(free) block reservation window on last file close, or truncate449* or at last iput().450*451* It is being called in three cases:452* ext2_release_file(): last writer closes the file453* ext2_clear_inode(): last iput(), when nobody links to this file.454* ext2_truncate(): when the block indirect map is about to change.455*/456void ext2_discard_reservation(struct inode *inode)457{458struct ext2_inode_info *ei = EXT2_I(inode);459struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;460struct ext2_reserve_window_node *rsv;461spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;462463if (!block_i)464return;465466rsv = &block_i->rsv_window_node;467if (!rsv_is_empty(&rsv->rsv_window)) {468spin_lock(rsv_lock);469if (!rsv_is_empty(&rsv->rsv_window))470rsv_window_remove(inode->i_sb, rsv);471spin_unlock(rsv_lock);472}473}474475/**476* ext2_free_blocks() -- Free given blocks and update quota and i_blocks477* @inode: inode478* @block: start physical block to free479* @count: number of blocks to free480*/481void ext2_free_blocks(struct inode * inode, ext2_fsblk_t block,482unsigned long count)483{484struct buffer_head *bitmap_bh = NULL;485struct buffer_head * bh2;486unsigned long block_group;487unsigned long bit;488unsigned long i;489unsigned long overflow;490struct super_block * sb = inode->i_sb;491struct ext2_sb_info * sbi = EXT2_SB(sb);492struct ext2_group_desc * desc;493struct ext2_super_block * es = sbi->s_es;494unsigned freed = 0, group_freed;495496if (!ext2_data_block_valid(sbi, block, count)) {497ext2_error (sb, "ext2_free_blocks",498"Freeing blocks not in datazone - "499"block = %lu, count = %lu", block, count);500goto error_return;501}502503ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);504505do_more:506overflow = 0;507block_group = (block - le32_to_cpu(es->s_first_data_block)) /508EXT2_BLOCKS_PER_GROUP(sb);509bit = (block - le32_to_cpu(es->s_first_data_block)) %510EXT2_BLOCKS_PER_GROUP(sb);511/*512* Check to see if we are freeing blocks across a group513* boundary.514*/515if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {516overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);517count -= overflow;518}519brelse(bitmap_bh);520bitmap_bh = read_block_bitmap(sb, block_group);521if (!bitmap_bh)522goto error_return;523524desc = ext2_get_group_desc (sb, block_group, &bh2);525if (!desc)526goto error_return;527528if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||529in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||530in_range (block, le32_to_cpu(desc->bg_inode_table),531sbi->s_itb_per_group) ||532in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),533sbi->s_itb_per_group)) {534ext2_error (sb, "ext2_free_blocks",535"Freeing blocks in system zones - "536"Block = %lu, count = %lu",537block, count);538goto error_return;539}540541for (i = 0, group_freed = 0; i < count; i++) {542if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),543bit + i, bitmap_bh->b_data)) {544ext2_error(sb, __func__,545"bit already cleared for block %lu", block + i);546} else {547group_freed++;548}549}550551mark_buffer_dirty(bitmap_bh);552if (sb->s_flags & SB_SYNCHRONOUS)553sync_dirty_buffer(bitmap_bh);554555group_adjust_blocks(sb, block_group, desc, bh2, group_freed);556freed += group_freed;557558if (overflow) {559block += count;560count = overflow;561goto do_more;562}563error_return:564brelse(bitmap_bh);565if (freed) {566percpu_counter_add(&sbi->s_freeblocks_counter, freed);567dquot_free_block_nodirty(inode, freed);568mark_inode_dirty(inode);569}570}571572/**573* bitmap_search_next_usable_block()574* @start: the starting block (group relative) of the search575* @bh: bufferhead contains the block group bitmap576* @maxblocks: the ending block (group relative) of the reservation577*578* The bitmap search --- search forward through the actual bitmap on disk until579* we find a bit free.580*/581static ext2_grpblk_t582bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,583ext2_grpblk_t maxblocks)584{585ext2_grpblk_t next;586587next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);588if (next >= maxblocks)589return -1;590return next;591}592593/**594* find_next_usable_block()595* @start: the starting block (group relative) to find next596* allocatable block in bitmap.597* @bh: bufferhead contains the block group bitmap598* @maxblocks: the ending block (group relative) for the search599*600* Find an allocatable block in a bitmap. We perform the "most601* appropriate allocation" algorithm of looking for a free block near602* the initial goal; then for a free byte somewhere in the bitmap;603* then for any free bit in the bitmap.604*/605static ext2_grpblk_t606find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)607{608ext2_grpblk_t here, next;609char *p, *r;610611if (start > 0) {612/*613* The goal was occupied; search forward for a free614* block within the next XX blocks.615*616* end_goal is more or less random, but it has to be617* less than EXT2_BLOCKS_PER_GROUP. Aligning up to the618* next 64-bit boundary is simple..619*/620ext2_grpblk_t end_goal = (start + 63) & ~63;621if (end_goal > maxblocks)622end_goal = maxblocks;623here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);624if (here < end_goal)625return here;626ext2_debug("Bit not found near goal\n");627}628629here = start;630if (here < 0)631here = 0;632633p = ((char *)bh->b_data) + (here >> 3);634r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));635next = (r - ((char *)bh->b_data)) << 3;636637if (next < maxblocks && next >= here)638return next;639640here = bitmap_search_next_usable_block(here, bh, maxblocks);641return here;642}643644/**645* ext2_try_to_allocate()646* @sb: superblock647* @group: given allocation block group648* @bitmap_bh: bufferhead holds the block bitmap649* @grp_goal: given target block within the group650* @count: target number of blocks to allocate651* @my_rsv: reservation window652*653* Attempt to allocate blocks within a give range. Set the range of allocation654* first, then find the first free bit(s) from the bitmap (within the range),655* and at last, allocate the blocks by claiming the found free bit as allocated.656*657* To set the range of this allocation:658* if there is a reservation window, only try to allocate block(s)659* from the file's own reservation window;660* Otherwise, the allocation range starts from the give goal block,661* ends at the block group's last block.662*663* If we failed to allocate the desired block then we may end up crossing to a664* new bitmap.665*/666static int667ext2_try_to_allocate(struct super_block *sb, int group,668struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,669unsigned long *count,670struct ext2_reserve_window *my_rsv)671{672ext2_fsblk_t group_first_block = ext2_group_first_block_no(sb, group);673ext2_fsblk_t group_last_block = ext2_group_last_block_no(sb, group);674ext2_grpblk_t start, end;675unsigned long num = 0;676677start = 0;678end = group_last_block - group_first_block + 1;679/* we do allocation within the reservation window if we have a window */680if (my_rsv) {681if (my_rsv->_rsv_start >= group_first_block)682start = my_rsv->_rsv_start - group_first_block;683if (my_rsv->_rsv_end < group_last_block)684end = my_rsv->_rsv_end - group_first_block + 1;685if (grp_goal < start || grp_goal >= end)686grp_goal = -1;687}688BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));689690if (grp_goal < 0) {691grp_goal = find_next_usable_block(start, bitmap_bh, end);692if (grp_goal < 0)693goto fail_access;694if (!my_rsv) {695int i;696697for (i = 0; i < 7 && grp_goal > start &&698!ext2_test_bit(grp_goal - 1,699bitmap_bh->b_data);700i++, grp_goal--)701;702}703}704705for (; num < *count && grp_goal < end; grp_goal++) {706if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),707grp_goal, bitmap_bh->b_data)) {708if (num == 0)709continue;710break;711}712num++;713}714715if (num == 0)716goto fail_access;717718*count = num;719return grp_goal - num;720fail_access:721return -1;722}723724/**725* find_next_reservable_window - Find a reservable space within the given range.726* @search_head: The list to search.727* @my_rsv: The reservation we're currently using.728* @sb: The super block.729* @start_block: The first block we consider to start the real search from730* @last_block: The maximum block number that our goal reservable space731* could start from.732*733* It does not allocate the reservation window: alloc_new_reservation()734* will do the work later.735*736* We search the given range, rather than the whole reservation double737* linked list, (start_block, last_block) to find a free region that is738* of my size and has not been reserved.739*740* @search_head is not necessarily the list head of the whole filesystem.741* We have both head and @start_block to assist the search for the742* reservable space. The list starts from head, but we will shift to743* the place where start_block is, then start from there, when looking744* for a reservable space.745*746* @last_block is normally the last block in this group. The search will end747* when we found the start of next possible reservable space is out748* of this boundary. This could handle the cross boundary reservation749* window request.750*751* Return: -1 if we could not find a range of sufficient size. If we could,752* return 0 and fill in @my_rsv with the range information.753*/754static int find_next_reservable_window(755struct ext2_reserve_window_node *search_head,756struct ext2_reserve_window_node *my_rsv,757struct super_block * sb,758ext2_fsblk_t start_block,759ext2_fsblk_t last_block)760{761struct rb_node *next;762struct ext2_reserve_window_node *rsv, *prev;763ext2_fsblk_t cur;764int size = my_rsv->rsv_goal_size;765766/* TODO: make the start of the reservation window byte-aligned */767/* cur = *start_block & ~7;*/768cur = start_block;769rsv = search_head;770if (!rsv)771return -1;772773while (1) {774if (cur <= rsv->rsv_end)775cur = rsv->rsv_end + 1;776777/* TODO?778* in the case we could not find a reservable space779* that is what is expected, during the re-search, we could780* remember what's the largest reservable space we could have781* and return that one.782*783* For now it will fail if we could not find the reservable784* space with expected-size (or more)...785*/786if (cur > last_block)787return -1; /* fail */788789prev = rsv;790next = rb_next(&rsv->rsv_node);791rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);792793/*794* Reached the last reservation, we can just append to the795* previous one.796*/797if (!next)798break;799800if (cur + size <= rsv->rsv_start) {801/*802* Found a reserveable space big enough. We could803* have a reservation across the group boundary here804*/805break;806}807}808/*809* we come here either :810* when we reach the end of the whole list,811* and there is empty reservable space after last entry in the list.812* append it to the end of the list.813*814* or we found one reservable space in the middle of the list,815* return the reservation window that we could append to.816* succeed.817*/818819if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))820rsv_window_remove(sb, my_rsv);821822/*823* Let's book the whole available window for now. We will check the824* disk bitmap later and then, if there are free blocks then we adjust825* the window size if it's larger than requested.826* Otherwise, we will remove this node from the tree next time827* call find_next_reservable_window.828*/829my_rsv->rsv_start = cur;830my_rsv->rsv_end = cur + size - 1;831my_rsv->rsv_alloc_hit = 0;832833if (prev != my_rsv)834ext2_rsv_window_add(sb, my_rsv);835836return 0;837}838839/**840* alloc_new_reservation - Allocate a new reservation window.841* @my_rsv: The reservation we're currently using.842* @grp_goal: The goal block relative to the start of the group.843* @sb: The super block.844* @group: The group we are trying to allocate in.845* @bitmap_bh: The block group block bitmap.846*847* To make a new reservation, we search part of the filesystem reservation848* list (the list inside the group). We try to allocate a new849* reservation window near @grp_goal, or the beginning of the850* group, if @grp_goal is negative.851*852* We first find a reservable space after the goal, then from there,853* we check the bitmap for the first free block after it. If there is854* no free block until the end of group, then the whole group is full,855* we failed. Otherwise, check if the free block is inside the expected856* reservable space, if so, we succeed.857*858* If the first free block is outside the reservable space, then start859* from the first free block, we search for next available space, and860* go on.861*862* on succeed, a new reservation will be found and inserted into the863* list. It contains at least one free block, and it does not overlap864* with other reservation windows.865*866* Return: 0 on success, -1 if we failed to find a reservation window867* in this group868*/869static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,870ext2_grpblk_t grp_goal, struct super_block *sb,871unsigned int group, struct buffer_head *bitmap_bh)872{873struct ext2_reserve_window_node *search_head;874ext2_fsblk_t group_first_block, group_end_block, start_block;875ext2_grpblk_t first_free_block;876struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;877unsigned long size;878int ret;879spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;880881group_first_block = ext2_group_first_block_no(sb, group);882group_end_block = ext2_group_last_block_no(sb, group);883884if (grp_goal < 0)885start_block = group_first_block;886else887start_block = grp_goal + group_first_block;888889size = my_rsv->rsv_goal_size;890891if (!rsv_is_empty(&my_rsv->rsv_window)) {892/*893* if the old reservation is cross group boundary894* and if the goal is inside the old reservation window,895* we will come here when we just failed to allocate from896* the first part of the window. We still have another part897* that belongs to the next group. In this case, there is no898* point to discard our window and try to allocate a new one899* in this group(which will fail). we should900* keep the reservation window, just simply move on.901*902* Maybe we could shift the start block of the reservation903* window to the first block of next group.904*/905906if ((my_rsv->rsv_start <= group_end_block) &&907(my_rsv->rsv_end > group_end_block) &&908(start_block >= my_rsv->rsv_start))909return -1;910911if ((my_rsv->rsv_alloc_hit >912(my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {913/*914* if the previously allocation hit ratio is915* greater than 1/2, then we double the size of916* the reservation window the next time,917* otherwise we keep the same size window918*/919size = size * 2;920if (size > EXT2_MAX_RESERVE_BLOCKS)921size = EXT2_MAX_RESERVE_BLOCKS;922my_rsv->rsv_goal_size= size;923}924}925926spin_lock(rsv_lock);927/*928* shift the search start to the window near the goal block929*/930search_head = search_reserve_window(fs_rsv_root, start_block);931932/*933* find_next_reservable_window() simply finds a reservable window934* inside the given range(start_block, group_end_block).935*936* To make sure the reservation window has a free bit inside it, we937* need to check the bitmap after we found a reservable window.938*/939retry:940ret = find_next_reservable_window(search_head, my_rsv, sb,941start_block, group_end_block);942943if (ret == -1) {944if (!rsv_is_empty(&my_rsv->rsv_window))945rsv_window_remove(sb, my_rsv);946spin_unlock(rsv_lock);947return -1;948}949950/*951* On success, find_next_reservable_window() returns the952* reservation window where there is a reservable space after it.953* Before we reserve this reservable space, we need954* to make sure there is at least a free block inside this region.955*956* Search the first free bit on the block bitmap. Search starts from957* the start block of the reservable space we just found.958*/959spin_unlock(rsv_lock);960first_free_block = bitmap_search_next_usable_block(961my_rsv->rsv_start - group_first_block,962bitmap_bh, group_end_block - group_first_block + 1);963964if (first_free_block < 0) {965/*966* no free block left on the bitmap, no point967* to reserve the space. return failed.968*/969spin_lock(rsv_lock);970if (!rsv_is_empty(&my_rsv->rsv_window))971rsv_window_remove(sb, my_rsv);972spin_unlock(rsv_lock);973return -1; /* failed */974}975976start_block = first_free_block + group_first_block;977/*978* check if the first free block is within the979* free space we just reserved980*/981if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)982return 0; /* success */983/*984* if the first free bit we found is out of the reservable space985* continue search for next reservable space,986* start from where the free block is,987* we also shift the list head to where we stopped last time988*/989search_head = my_rsv;990spin_lock(rsv_lock);991goto retry;992}993994/**995* try_to_extend_reservation()996* @my_rsv: given reservation window997* @sb: super block998* @size: the delta to extend999*1000* Attempt to expand the reservation window large enough to have1001* required number of free blocks1002*1003* Since ext2_try_to_allocate() will always allocate blocks within1004* the reservation window range, if the window size is too small,1005* multiple blocks allocation has to stop at the end of the reservation1006* window. To make this more efficient, given the total number of1007* blocks needed and the current size of the window, we try to1008* expand the reservation window size if necessary on a best-effort1009* basis before ext2_new_blocks() tries to allocate blocks.1010*/1011static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,1012struct super_block *sb, int size)1013{1014struct ext2_reserve_window_node *next_rsv;1015struct rb_node *next;1016spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;10171018if (!spin_trylock(rsv_lock))1019return;10201021next = rb_next(&my_rsv->rsv_node);10221023if (!next)1024my_rsv->rsv_end += size;1025else {1026next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);10271028if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)1029my_rsv->rsv_end += size;1030else1031my_rsv->rsv_end = next_rsv->rsv_start - 1;1032}1033spin_unlock(rsv_lock);1034}10351036/**1037* ext2_try_to_allocate_with_rsv()1038* @sb: superblock1039* @group: given allocation block group1040* @bitmap_bh: bufferhead holds the block bitmap1041* @grp_goal: given target block within the group1042* @count: target number of blocks to allocate1043* @my_rsv: reservation window1044*1045* This is the main function used to allocate a new block and its reservation1046* window.1047*1048* Each time when a new block allocation is need, first try to allocate from1049* its own reservation. If it does not have a reservation window, instead of1050* looking for a free bit on bitmap first, then look up the reservation list to1051* see if it is inside somebody else's reservation window, we try to allocate a1052* reservation window for it starting from the goal first. Then do the block1053* allocation within the reservation window.1054*1055* This will avoid keeping on searching the reservation list again and1056* again when somebody is looking for a free block (without1057* reservation), and there are lots of free blocks, but they are all1058* being reserved.1059*1060* We use a red-black tree for the per-filesystem reservation list.1061*/1062static ext2_grpblk_t1063ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,1064struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,1065struct ext2_reserve_window_node * my_rsv,1066unsigned long *count)1067{1068ext2_fsblk_t group_first_block, group_last_block;1069ext2_grpblk_t ret = 0;1070unsigned long num = *count;10711072/*1073* we don't deal with reservation when1074* filesystem is mounted without reservation1075* or the file is not a regular file1076* or last attempt to allocate a block with reservation turned on failed1077*/1078if (my_rsv == NULL) {1079return ext2_try_to_allocate(sb, group, bitmap_bh,1080grp_goal, count, NULL);1081}1082/*1083* grp_goal is a group relative block number (if there is a goal)1084* 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)1085* first block is a filesystem wide block number1086* first block is the block number of the first block in this group1087*/1088group_first_block = ext2_group_first_block_no(sb, group);1089group_last_block = ext2_group_last_block_no(sb, group);10901091/*1092* Basically we will allocate a new block from inode's reservation1093* window.1094*1095* We need to allocate a new reservation window, if:1096* a) inode does not have a reservation window; or1097* b) last attempt to allocate a block from existing reservation1098* failed; or1099* c) we come here with a goal and with a reservation window1100*1101* We do not need to allocate a new reservation window if we come here1102* at the beginning with a goal and the goal is inside the window, or1103* we don't have a goal but already have a reservation window.1104* then we could go to allocate from the reservation window directly.1105*/1106while (1) {1107if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||1108!goal_in_my_reservation(&my_rsv->rsv_window,1109grp_goal, group, sb)) {1110if (my_rsv->rsv_goal_size < *count)1111my_rsv->rsv_goal_size = *count;1112ret = alloc_new_reservation(my_rsv, grp_goal, sb,1113group, bitmap_bh);1114if (ret < 0)1115break; /* failed */11161117if (!goal_in_my_reservation(&my_rsv->rsv_window,1118grp_goal, group, sb))1119grp_goal = -1;1120} else if (grp_goal >= 0) {1121int curr = my_rsv->rsv_end -1122(grp_goal + group_first_block) + 1;11231124if (curr < *count)1125try_to_extend_reservation(my_rsv, sb,1126*count - curr);1127}11281129if ((my_rsv->rsv_start > group_last_block) ||1130(my_rsv->rsv_end < group_first_block)) {1131ext2_error(sb, __func__,1132"Reservation out of group %u range goal %d fsb[%lu,%lu] rsv[%lu, %lu]",1133group, grp_goal, group_first_block,1134group_last_block, my_rsv->rsv_start,1135my_rsv->rsv_end);1136rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);1137return -1;1138}1139ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,1140&num, &my_rsv->rsv_window);1141if (ret >= 0) {1142my_rsv->rsv_alloc_hit += num;1143*count = num;1144break; /* succeed */1145}1146num = *count;1147}1148return ret;1149}11501151/**1152* ext2_has_free_blocks()1153* @sbi: in-core super block structure.1154*1155* Check if filesystem has at least 1 free block available for allocation.1156*/1157static int ext2_has_free_blocks(struct ext2_sb_info *sbi)1158{1159ext2_fsblk_t free_blocks, root_blocks;11601161free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);1162root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);1163if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&1164!uid_eq(sbi->s_resuid, current_fsuid()) &&1165(gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) ||1166!in_group_p (sbi->s_resgid))) {1167return 0;1168}1169return 1;1170}11711172/*1173* Returns 1 if the passed-in block region is valid; 0 if some part overlaps1174* with filesystem metadata blocks.1175*/1176int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,1177unsigned int count)1178{1179if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||1180(start_blk + count - 1 < start_blk) ||1181(start_blk + count - 1 >= le32_to_cpu(sbi->s_es->s_blocks_count)))1182return 0;11831184/* Ensure we do not step over superblock */1185if ((start_blk <= sbi->s_sb_block) &&1186(start_blk + count - 1 >= sbi->s_sb_block))1187return 0;11881189return 1;1190}11911192/*1193* ext2_new_blocks() -- core block(s) allocation function1194* @inode: file inode1195* @goal: given target block(filesystem wide)1196* @count: target number of blocks to allocate1197* @errp: error code1198* @flags: allocate flags1199*1200* ext2_new_blocks uses a goal block to assist allocation. If the goal is1201* free, or there is a free block within 32 blocks of the goal, that block1202* is allocated. Otherwise a forward search is made for a free block; within1203* each block group the search first looks for an entire free byte in the block1204* bitmap, and then for any free bit if that fails.1205* This function also updates quota and i_blocks field.1206*/1207ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,1208unsigned long *count, int *errp, unsigned int flags)1209{1210struct buffer_head *bitmap_bh = NULL;1211struct buffer_head *gdp_bh;1212int group_no;1213int goal_group;1214ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */1215ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/1216ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */1217int bgi; /* blockgroup iteration index */1218int performed_allocation = 0;1219ext2_grpblk_t free_blocks; /* number of free blocks in a group */1220struct super_block *sb;1221struct ext2_group_desc *gdp;1222struct ext2_super_block *es;1223struct ext2_sb_info *sbi;1224struct ext2_reserve_window_node *my_rsv = NULL;1225struct ext2_block_alloc_info *block_i;1226unsigned short windowsz = 0;1227unsigned long ngroups;1228unsigned long num = *count;1229int ret;12301231*errp = -ENOSPC;1232sb = inode->i_sb;12331234/*1235* Check quota for allocation of this block.1236*/1237ret = dquot_alloc_block(inode, num);1238if (ret) {1239*errp = ret;1240return 0;1241}12421243sbi = EXT2_SB(sb);1244es = EXT2_SB(sb)->s_es;1245ext2_debug("goal=%lu.\n", goal);1246/*1247* Allocate a block from reservation only when the filesystem is1248* mounted with reservation(default,-o reservation), and it's a regular1249* file, and the desired window size is greater than 0 (One could use1250* ioctl command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn1251* off reservation on that particular file). Also do not use the1252* reservation window if the caller asked us not to do it.1253*/1254block_i = EXT2_I(inode)->i_block_alloc_info;1255if (!(flags & EXT2_ALLOC_NORESERVE) && block_i) {1256windowsz = block_i->rsv_window_node.rsv_goal_size;1257if (windowsz > 0)1258my_rsv = &block_i->rsv_window_node;1259}12601261if (!ext2_has_free_blocks(sbi)) {1262*errp = -ENOSPC;1263goto out;1264}12651266/*1267* First, test whether the goal block is free.1268*/1269if (goal < le32_to_cpu(es->s_first_data_block) ||1270goal >= le32_to_cpu(es->s_blocks_count))1271goal = le32_to_cpu(es->s_first_data_block);1272group_no = (goal - le32_to_cpu(es->s_first_data_block)) /1273EXT2_BLOCKS_PER_GROUP(sb);1274goal_group = group_no;1275retry_alloc:1276gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);1277if (!gdp)1278goto io_error;12791280free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);1281/*1282* if there is not enough free blocks to make a new resevation1283* turn off reservation for this allocation1284*/1285if (my_rsv && (free_blocks < windowsz)1286&& (free_blocks > 0)1287&& (rsv_is_empty(&my_rsv->rsv_window)))1288my_rsv = NULL;12891290if (free_blocks > 0) {1291grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %1292EXT2_BLOCKS_PER_GROUP(sb));1293/*1294* In case we retry allocation (due to fs reservation not1295* working out or fs corruption), the bitmap_bh is non-null1296* pointer and we have to release it before calling1297* read_block_bitmap().1298*/1299brelse(bitmap_bh);1300bitmap_bh = read_block_bitmap(sb, group_no);1301if (!bitmap_bh)1302goto io_error;1303grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,1304bitmap_bh, grp_target_blk,1305my_rsv, &num);1306if (grp_alloc_blk >= 0)1307goto allocated;1308}13091310ngroups = EXT2_SB(sb)->s_groups_count;1311smp_rmb();13121313/*1314* Now search the rest of the groups. We assume that1315* group_no and gdp correctly point to the last group visited.1316*/1317for (bgi = 0; bgi < ngroups; bgi++) {1318group_no++;1319if (group_no >= ngroups)1320group_no = 0;1321gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);1322if (!gdp)1323goto io_error;13241325free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);1326/*1327* skip this group (and avoid loading bitmap) if there1328* are no free blocks1329*/1330if (!free_blocks)1331continue;1332/*1333* skip this group if the number of1334* free blocks is less than half of the reservation1335* window size.1336*/1337if (my_rsv && (free_blocks <= (windowsz/2)))1338continue;13391340brelse(bitmap_bh);1341bitmap_bh = read_block_bitmap(sb, group_no);1342if (!bitmap_bh)1343goto io_error;1344/*1345* try to allocate block(s) from this group, without a goal(-1).1346*/1347grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,1348bitmap_bh, -1, my_rsv, &num);1349if (grp_alloc_blk >= 0)1350goto allocated;1351}1352/*1353* We may end up a bogus earlier ENOSPC error due to1354* filesystem is "full" of reservations, but1355* there maybe indeed free blocks available on disk1356* In this case, we just forget about the reservations1357* just do block allocation as without reservations.1358*/1359if (my_rsv) {1360my_rsv = NULL;1361windowsz = 0;1362group_no = goal_group;1363goto retry_alloc;1364}1365/* No space left on the device */1366*errp = -ENOSPC;1367goto out;13681369allocated:13701371ext2_debug("using block group %d(%d)\n",1372group_no, gdp->bg_free_blocks_count);13731374ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);13751376if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||1377in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||1378in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),1379EXT2_SB(sb)->s_itb_per_group) ||1380in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),1381EXT2_SB(sb)->s_itb_per_group)) {1382ext2_error(sb, "ext2_new_blocks",1383"Allocating block in system zone - "1384"blocks from "E2FSBLK", length %lu",1385ret_block, num);1386/*1387* ext2_try_to_allocate marked the blocks we allocated as in1388* use. So we may want to selectively mark some of the blocks1389* as free1390*/1391num = *count;1392goto retry_alloc;1393}13941395performed_allocation = 1;13961397if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {1398ext2_error(sb, "ext2_new_blocks",1399"block("E2FSBLK") >= blocks count(%d) - "1400"block_group = %d, es == %p ", ret_block,1401le32_to_cpu(es->s_blocks_count), group_no, es);1402goto out;1403}14041405group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);1406percpu_counter_sub(&sbi->s_freeblocks_counter, num);14071408mark_buffer_dirty(bitmap_bh);1409if (sb->s_flags & SB_SYNCHRONOUS)1410sync_dirty_buffer(bitmap_bh);14111412*errp = 0;1413brelse(bitmap_bh);1414if (num < *count) {1415dquot_free_block_nodirty(inode, *count-num);1416mark_inode_dirty(inode);1417*count = num;1418}1419return ret_block;14201421io_error:1422*errp = -EIO;1423out:1424/*1425* Undo the block allocation1426*/1427if (!performed_allocation) {1428dquot_free_block_nodirty(inode, *count);1429mark_inode_dirty(inode);1430}1431brelse(bitmap_bh);1432return 0;1433}14341435#ifdef EXT2FS_DEBUG14361437unsigned long ext2_count_free(struct buffer_head *map, unsigned int numchars)1438{1439return numchars * BITS_PER_BYTE - memweight(map->b_data, numchars);1440}14411442#endif /* EXT2FS_DEBUG */14431444unsigned long ext2_count_free_blocks (struct super_block * sb)1445{1446struct ext2_group_desc * desc;1447unsigned long desc_count = 0;1448int i;1449#ifdef EXT2FS_DEBUG1450unsigned long bitmap_count, x;1451struct ext2_super_block *es;14521453es = EXT2_SB(sb)->s_es;1454desc_count = 0;1455bitmap_count = 0;1456desc = NULL;1457for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {1458struct buffer_head *bitmap_bh;1459desc = ext2_get_group_desc (sb, i, NULL);1460if (!desc)1461continue;1462desc_count += le16_to_cpu(desc->bg_free_blocks_count);1463bitmap_bh = read_block_bitmap(sb, i);1464if (!bitmap_bh)1465continue;14661467x = ext2_count_free(bitmap_bh, sb->s_blocksize);1468printk ("group %d: stored = %d, counted = %lu\n",1469i, le16_to_cpu(desc->bg_free_blocks_count), x);1470bitmap_count += x;1471brelse(bitmap_bh);1472}1473printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",1474(long)le32_to_cpu(es->s_free_blocks_count),1475desc_count, bitmap_count);1476return bitmap_count;1477#else1478for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {1479desc = ext2_get_group_desc(sb, i, NULL);1480if (!desc)1481continue;1482desc_count += le16_to_cpu(desc->bg_free_blocks_count);1483}1484return desc_count;1485#endif1486}14871488static inline int test_root(int a, int b)1489{1490int num = b;14911492while (a > num)1493num *= b;1494return num == a;1495}14961497static int ext2_group_sparse(int group)1498{1499if (group <= 1)1500return 1;1501return (test_root(group, 3) || test_root(group, 5) ||1502test_root(group, 7));1503}15041505/**1506* ext2_bg_has_super - number of blocks used by the superblock in group1507* @sb: superblock for filesystem1508* @group: group number to check1509*1510* Return the number of blocks used by the superblock (primary or backup)1511* in this group. Currently this will be only 0 or 1.1512*/1513int ext2_bg_has_super(struct super_block *sb, int group)1514{1515if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&1516!ext2_group_sparse(group))1517return 0;1518return 1;1519}15201521/**1522* ext2_bg_num_gdb - number of blocks used by the group table in group1523* @sb: superblock for filesystem1524* @group: group number to check1525*1526* Return the number of blocks used by the group descriptor table1527* (primary or backup) in this group. In the future there may be a1528* different number of descriptor blocks in each group.1529*/1530unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)1531{1532return ext2_bg_has_super(sb, group) ? EXT2_SB(sb)->s_gdb_count : 0;1533}1534153515361537