aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext3/balloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext3/balloc.c')
-rw-r--r--fs/ext3/balloc.c296
1 files changed, 284 insertions, 12 deletions
diff --git a/fs/ext3/balloc.c b/fs/ext3/balloc.c
index 4a32511f4ded..fe52297e31ad 100644
--- a/fs/ext3/balloc.c
+++ b/fs/ext3/balloc.c
@@ -20,6 +20,7 @@
20#include <linux/ext3_jbd.h> 20#include <linux/ext3_jbd.h>
21#include <linux/quotaops.h> 21#include <linux/quotaops.h>
22#include <linux/buffer_head.h> 22#include <linux/buffer_head.h>
23#include <linux/blkdev.h>
23 24
24/* 25/*
25 * balloc.c contains the blocks allocation and deallocation routines 26 * balloc.c contains the blocks allocation and deallocation routines
@@ -39,6 +40,21 @@
39 40
40#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) 41#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
41 42
43/*
44 * Calculate the block group number and offset, given a block number
45 */
46static void ext3_get_group_no_and_offset(struct super_block *sb,
47 ext3_fsblk_t blocknr, unsigned long *blockgrpp, ext3_grpblk_t *offsetp)
48{
49 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
50
51 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
52 if (offsetp)
53 *offsetp = blocknr % EXT3_BLOCKS_PER_GROUP(sb);
54 if (blockgrpp)
55 *blockgrpp = blocknr / EXT3_BLOCKS_PER_GROUP(sb);
56}
57
42/** 58/**
43 * ext3_get_group_desc() -- load group descriptor from disk 59 * ext3_get_group_desc() -- load group descriptor from disk
44 * @sb: super block 60 * @sb: super block
@@ -574,7 +590,7 @@ do_more:
574 BUFFER_TRACE(debug_bh, "Deleted!"); 590 BUFFER_TRACE(debug_bh, "Deleted!");
575 if (!bh2jh(bitmap_bh)->b_committed_data) 591 if (!bh2jh(bitmap_bh)->b_committed_data)
576 BUFFER_TRACE(debug_bh, 592 BUFFER_TRACE(debug_bh,
577 "No commited data in bitmap"); 593 "No committed data in bitmap");
578 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); 594 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
579 __brelse(debug_bh); 595 __brelse(debug_bh);
580 } 596 }
@@ -792,9 +808,9 @@ find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
792 if (here < 0) 808 if (here < 0)
793 here = 0; 809 here = 0;
794 810
795 p = ((char *)bh->b_data) + (here >> 3); 811 p = bh->b_data + (here >> 3);
796 r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); 812 r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
797 next = (r - ((char *)bh->b_data)) << 3; 813 next = (r - bh->b_data) << 3;
798 814
799 if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh)) 815 if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh))
800 return next; 816 return next;
@@ -810,8 +826,9 @@ find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
810 826
811/** 827/**
812 * claim_block() 828 * claim_block()
829 * @lock: the spin lock for this block group
813 * @block: the free block (group relative) to allocate 830 * @block: the free block (group relative) to allocate
814 * @bh: the bufferhead containts the block group bitmap 831 * @bh: the buffer_head contains the block group bitmap
815 * 832 *
816 * We think we can allocate this block in this bitmap. Try to set the bit. 833 * We think we can allocate this block in this bitmap. Try to set the bit.
817 * If that succeeds then check that nobody has allocated and then freed the 834 * If that succeeds then check that nobody has allocated and then freed the
@@ -956,9 +973,11 @@ fail_access:
956 * but we will shift to the place where start_block is, 973 * but we will shift to the place where start_block is,
957 * then start from there, when looking for a reservable space. 974 * then start from there, when looking for a reservable space.
958 * 975 *
959 * @size: the target new reservation window size 976 * @my_rsv: the reservation window
977 *
978 * @sb: the super block
960 * 979 *
961 * @group_first_block: the first block we consider to start 980 * @start_block: the first block we consider to start
962 * the real search from 981 * the real search from
963 * 982 *
964 * @last_block: 983 * @last_block:
@@ -1044,7 +1063,7 @@ static int find_next_reservable_window(
1044 rsv_window_remove(sb, my_rsv); 1063 rsv_window_remove(sb, my_rsv);
1045 1064
1046 /* 1065 /*
1047 * Let's book the whole avaliable window for now. We will check the 1066 * Let's book the whole available window for now. We will check the
1048 * disk bitmap later and then, if there are free blocks then we adjust 1067 * disk bitmap later and then, if there are free blocks then we adjust
1049 * the window size if it's larger than requested. 1068 * the window size if it's larger than requested.
1050 * Otherwise, we will remove this node from the tree next time 1069 * Otherwise, we will remove this node from the tree next time
@@ -1084,7 +1103,7 @@ static int find_next_reservable_window(
1084 * 1103 *
1085 * failed: we failed to find a reservation window in this group 1104 * failed: we failed to find a reservation window in this group
1086 * 1105 *
1087 * @rsv: the reservation 1106 * @my_rsv: the reservation window
1088 * 1107 *
1089 * @grp_goal: The goal (group-relative). It is where the search for a 1108 * @grp_goal: The goal (group-relative). It is where the search for a
1090 * free reservable space should start from. 1109 * free reservable space should start from.
@@ -1273,8 +1292,8 @@ static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv,
1273 * @group: given allocation block group 1292 * @group: given allocation block group
1274 * @bitmap_bh: bufferhead holds the block bitmap 1293 * @bitmap_bh: bufferhead holds the block bitmap
1275 * @grp_goal: given target block within the group 1294 * @grp_goal: given target block within the group
1276 * @count: target number of blocks to allocate
1277 * @my_rsv: reservation window 1295 * @my_rsv: reservation window
1296 * @count: target number of blocks to allocate
1278 * @errp: pointer to store the error code 1297 * @errp: pointer to store the error code
1279 * 1298 *
1280 * This is the main function used to allocate a new block and its reservation 1299 * This is the main function used to allocate a new block and its reservation
@@ -1437,7 +1456,7 @@ static int ext3_has_free_blocks(struct ext3_sb_info *sbi)
1437 * 1456 *
1438 * ext3_should_retry_alloc() is called when ENOSPC is returned, and if 1457 * ext3_should_retry_alloc() is called when ENOSPC is returned, and if
1439 * it is profitable to retry the operation, this function will wait 1458 * it is profitable to retry the operation, this function will wait
1440 * for the current or commiting transaction to complete, and then 1459 * for the current or committing transaction to complete, and then
1441 * return TRUE. 1460 * return TRUE.
1442 * 1461 *
1443 * if the total number of retries exceed three times, return FALSE. 1462 * if the total number of retries exceed three times, return FALSE.
@@ -1613,9 +1632,9 @@ retry_alloc:
1613 goto allocated; 1632 goto allocated;
1614 } 1633 }
1615 /* 1634 /*
1616 * We may end up a bogus ealier ENOSPC error due to 1635 * We may end up a bogus earlier ENOSPC error due to
1617 * filesystem is "full" of reservations, but 1636 * filesystem is "full" of reservations, but
1618 * there maybe indeed free blocks avaliable on disk 1637 * there maybe indeed free blocks available on disk
1619 * In this case, we just forget about the reservations 1638 * In this case, we just forget about the reservations
1620 * just do block allocation as without reservations. 1639 * just do block allocation as without reservations.
1621 */ 1640 */
@@ -1882,3 +1901,256 @@ unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
1882 return ext3_bg_num_gdb_meta(sb,group); 1901 return ext3_bg_num_gdb_meta(sb,group);
1883 1902
1884} 1903}
1904
1905/**
1906 * ext3_trim_all_free -- function to trim all free space in alloc. group
1907 * @sb: super block for file system
1908 * @group: allocation group to trim
1909 * @start: first group block to examine
1910 * @max: last group block to examine
1911 * @gdp: allocation group description structure
1912 * @minblocks: minimum extent block count
1913 *
1914 * ext3_trim_all_free walks through group's block bitmap searching for free
1915 * blocks. When the free block is found, it tries to allocate this block and
1916 * consequent free block to get the biggest free extent possible, until it
1917 * reaches any used block. Then issue a TRIM command on this extent and free
1918 * the extent in the block bitmap. This is done until whole group is scanned.
1919 */
1920ext3_grpblk_t ext3_trim_all_free(struct super_block *sb, unsigned int group,
1921 ext3_grpblk_t start, ext3_grpblk_t max,
1922 ext3_grpblk_t minblocks)
1923{
1924 handle_t *handle;
1925 ext3_grpblk_t next, free_blocks, bit, freed, count = 0;
1926 ext3_fsblk_t discard_block;
1927 struct ext3_sb_info *sbi;
1928 struct buffer_head *gdp_bh, *bitmap_bh = NULL;
1929 struct ext3_group_desc *gdp;
1930 int err = 0, ret = 0;
1931
1932 /*
1933 * We will update one block bitmap, and one group descriptor
1934 */
1935 handle = ext3_journal_start_sb(sb, 2);
1936 if (IS_ERR(handle))
1937 return PTR_ERR(handle);
1938
1939 bitmap_bh = read_block_bitmap(sb, group);
1940 if (!bitmap_bh) {
1941 err = -EIO;
1942 goto err_out;
1943 }
1944
1945 BUFFER_TRACE(bitmap_bh, "getting undo access");
1946 err = ext3_journal_get_undo_access(handle, bitmap_bh);
1947 if (err)
1948 goto err_out;
1949
1950 gdp = ext3_get_group_desc(sb, group, &gdp_bh);
1951 if (!gdp) {
1952 err = -EIO;
1953 goto err_out;
1954 }
1955
1956 BUFFER_TRACE(gdp_bh, "get_write_access");
1957 err = ext3_journal_get_write_access(handle, gdp_bh);
1958 if (err)
1959 goto err_out;
1960
1961 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1962 sbi = EXT3_SB(sb);
1963
1964 /* Walk through the whole group */
1965 while (start < max) {
1966 start = bitmap_search_next_usable_block(start, bitmap_bh, max);
1967 if (start < 0)
1968 break;
1969 next = start;
1970
1971 /*
1972 * Allocate contiguous free extents by setting bits in the
1973 * block bitmap
1974 */
1975 while (next < max
1976 && claim_block(sb_bgl_lock(sbi, group),
1977 next, bitmap_bh)) {
1978 next++;
1979 }
1980
1981 /* We did not claim any blocks */
1982 if (next == start)
1983 continue;
1984
1985 discard_block = (ext3_fsblk_t)start +
1986 ext3_group_first_block_no(sb, group);
1987
1988 /* Update counters */
1989 spin_lock(sb_bgl_lock(sbi, group));
1990 le16_add_cpu(&gdp->bg_free_blocks_count, start - next);
1991 spin_unlock(sb_bgl_lock(sbi, group));
1992 percpu_counter_sub(&sbi->s_freeblocks_counter, next - start);
1993
1994 free_blocks -= next - start;
1995 /* Do not issue a TRIM on extents smaller than minblocks */
1996 if ((next - start) < minblocks)
1997 goto free_extent;
1998
1999 /* Send the TRIM command down to the device */
2000 err = sb_issue_discard(sb, discard_block, next - start,
2001 GFP_NOFS, 0);
2002 count += (next - start);
2003free_extent:
2004 freed = 0;
2005
2006 /*
2007 * Clear bits in the bitmap
2008 */
2009 for (bit = start; bit < next; bit++) {
2010 BUFFER_TRACE(bitmap_bh, "clear bit");
2011 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
2012 bit, bitmap_bh->b_data)) {
2013 ext3_error(sb, __func__,
2014 "bit already cleared for block "E3FSBLK,
2015 (unsigned long)bit);
2016 BUFFER_TRACE(bitmap_bh, "bit already cleared");
2017 } else {
2018 freed++;
2019 }
2020 }
2021
2022 /* Update couters */
2023 spin_lock(sb_bgl_lock(sbi, group));
2024 le16_add_cpu(&gdp->bg_free_blocks_count, freed);
2025 spin_unlock(sb_bgl_lock(sbi, group));
2026 percpu_counter_add(&sbi->s_freeblocks_counter, freed);
2027
2028 start = next;
2029 if (err < 0) {
2030 if (err != -EOPNOTSUPP)
2031 ext3_warning(sb, __func__, "Discard command "
2032 "returned error %d\n", err);
2033 break;
2034 }
2035
2036 if (fatal_signal_pending(current)) {
2037 err = -ERESTARTSYS;
2038 break;
2039 }
2040
2041 cond_resched();
2042
2043 /* No more suitable extents */
2044 if (free_blocks < minblocks)
2045 break;
2046 }
2047
2048 /* We dirtied the bitmap block */
2049 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2050 ret = ext3_journal_dirty_metadata(handle, bitmap_bh);
2051 if (!err)
2052 err = ret;
2053
2054 /* And the group descriptor block */
2055 BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
2056 ret = ext3_journal_dirty_metadata(handle, gdp_bh);
2057 if (!err)
2058 err = ret;
2059
2060 ext3_debug("trimmed %d blocks in the group %d\n",
2061 count, group);
2062
2063err_out:
2064 if (err)
2065 count = err;
2066 ext3_journal_stop(handle);
2067 brelse(bitmap_bh);
2068
2069 return count;
2070}
2071
2072/**
2073 * ext3_trim_fs() -- trim ioctl handle function
2074 * @sb: superblock for filesystem
2075 * @start: First Byte to trim
2076 * @len: number of Bytes to trim from start
2077 * @minlen: minimum extent length in Bytes
2078 *
2079 * ext3_trim_fs goes through all allocation groups containing Bytes from
2080 * start to start+len. For each such a group ext3_trim_all_free function
2081 * is invoked to trim all free space.
2082 */
2083int ext3_trim_fs(struct super_block *sb, struct fstrim_range *range)
2084{
2085 ext3_grpblk_t last_block, first_block, free_blocks;
2086 unsigned long first_group, last_group;
2087 unsigned long group, ngroups;
2088 struct ext3_group_desc *gdp;
2089 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
2090 uint64_t start, len, minlen, trimmed;
2091 ext3_fsblk_t max_blks = le32_to_cpu(es->s_blocks_count);
2092 int ret = 0;
2093
2094 start = (range->start >> sb->s_blocksize_bits) +
2095 le32_to_cpu(es->s_first_data_block);
2096 len = range->len >> sb->s_blocksize_bits;
2097 minlen = range->minlen >> sb->s_blocksize_bits;
2098 trimmed = 0;
2099
2100 if (unlikely(minlen > EXT3_BLOCKS_PER_GROUP(sb)))
2101 return -EINVAL;
2102 if (start >= max_blks)
2103 goto out;
2104 if (start + len > max_blks)
2105 len = max_blks - start;
2106
2107 ngroups = EXT3_SB(sb)->s_groups_count;
2108 smp_rmb();
2109
2110 /* Determine first and last group to examine based on start and len */
2111 ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) start,
2112 &first_group, &first_block);
2113 ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) (start + len),
2114 &last_group, &last_block);
2115 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
2116 last_block = EXT3_BLOCKS_PER_GROUP(sb);
2117
2118 if (first_group > last_group)
2119 return -EINVAL;
2120
2121 for (group = first_group; group <= last_group; group++) {
2122 gdp = ext3_get_group_desc(sb, group, NULL);
2123 if (!gdp)
2124 break;
2125
2126 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
2127 if (free_blocks < minlen)
2128 continue;
2129
2130 /*
2131 * For all the groups except the last one, last block will
2132 * always be EXT3_BLOCKS_PER_GROUP(sb), so we only need to
2133 * change it for the last group in which case first_block +
2134 * len < EXT3_BLOCKS_PER_GROUP(sb).
2135 */
2136 if (first_block + len < EXT3_BLOCKS_PER_GROUP(sb))
2137 last_block = first_block + len;
2138 len -= last_block - first_block;
2139
2140 ret = ext3_trim_all_free(sb, group, first_block,
2141 last_block, minlen);
2142 if (ret < 0)
2143 break;
2144
2145 trimmed += ret;
2146 first_block = 0;
2147 }
2148
2149 if (ret >= 0)
2150 ret = 0;
2151
2152out:
2153 range->len = trimmed * sb->s_blocksize;
2154
2155 return ret;
2156}