Skip to content

Commit 40c73ab

Browse files
committed
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs-2.6
* 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs-2.6: ext2: Resolve 'dereferencing pointer to incomplete type' when enabling EXT2_XATTR_DEBUG ext3: Remove redundant unlikely() ext2: Remove redundant unlikely() ext3: speed up file creates by optimizing rec_len functions ext2: speed up file creates by optimizing rec_len functions ext3: Add more journal error check ext3: Add journal error check in resize.c quota: Use %pV and __attribute__((format (printf in __quota_error and fix fallout ext3: Add FITRIM handling ext3: Add batched discard support for ext3 ext3: Add journal error check into ext3_rename() ext3: Use search_dirblock() in ext3_dx_find_entry() ext3: Avoid uninitialized memory references with a corrupted htree directory ext3: Return error code from generic_check_addressable ext3: Add journal error check into ext3_delete_entry() ext3: Add error check in ext3_mkdir() fs/ext3/super.c: Use printf extension %pV fs/ext2/super.c: Use printf extension %pV ext3: don't update sb journal_devnum when RO dev
2 parents 0945f35 + d96336b commit 40c73ab

16 files changed

Lines changed: 545 additions & 142 deletions

File tree

‎fs/ext2/dir.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,30 @@
2828

2929
typedef struct ext2_dir_entry_2 ext2_dirent;
3030

31+
/*
32+
* Tests against MAX_REC_LEN etc were put in place for 64k block
33+
* sizes; if that is not possible on this arch, we can skip
34+
* those tests and speed things up.
35+
*/
3136
static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
3237
{
3338
unsigned len = le16_to_cpu(dlen);
3439

40+
#if (PAGE_CACHE_SIZE >= 65536)
3541
if (len == EXT2_MAX_REC_LEN)
3642
return 1 << 16;
43+
#endif
3744
return len;
3845
}
3946

4047
static inline __le16 ext2_rec_len_to_disk(unsigned len)
4148
{
49+
#if (PAGE_CACHE_SIZE >= 65536)
4250
if (len == (1 << 16))
4351
return cpu_to_le16(EXT2_MAX_REC_LEN);
4452
else
4553
BUG_ON(len > (1 << 16));
54+
#endif
4655
return cpu_to_le16(len);
4756
}
4857

@@ -129,15 +138,15 @@ static void ext2_check_page(struct page *page, int quiet)
129138
p = (ext2_dirent *)(kaddr + offs);
130139
rec_len = ext2_rec_len_from_disk(p->rec_len);
131140

132-
if (rec_len < EXT2_DIR_REC_LEN(1))
141+
if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
133142
goto Eshort;
134-
if (rec_len & 3)
143+
if (unlikely(rec_len & 3))
135144
goto Ealign;
136-
if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
145+
if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
137146
goto Enamelen;
138-
if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
147+
if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
139148
goto Espan;
140-
if (le32_to_cpu(p->inode) > max_inumber)
149+
if (unlikely(le32_to_cpu(p->inode) > max_inumber))
141150
goto Einumber;
142151
}
143152
if (offs != limit)

‎fs/ext2/namei.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, str
6767
inode = NULL;
6868
if (ino) {
6969
inode = ext2_iget(dir->i_sb, ino);
70-
if (unlikely(IS_ERR(inode))) {
70+
if (IS_ERR(inode)) {
7171
if (PTR_ERR(inode) == -ESTALE) {
7272
ext2_error(dir->i_sb, __func__,
7373
"deleted inode referenced: %lu",

‎fs/ext2/super.c‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data);
4343
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
4444
static int ext2_sync_fs(struct super_block *sb, int wait);
4545

46-
void ext2_error (struct super_block * sb, const char * function,
47-
const char * fmt, ...)
46+
void ext2_error(struct super_block *sb, const char *function,
47+
const char *fmt, ...)
4848
{
49+
struct va_format vaf;
4950
va_list args;
5051
struct ext2_sb_info *sbi = EXT2_SB(sb);
5152
struct ext2_super_block *es = sbi->s_es;
@@ -59,9 +60,13 @@ void ext2_error (struct super_block * sb, const char * function,
5960
}
6061

6162
va_start(args, fmt);
62-
printk(KERN_CRIT "EXT2-fs (%s): error: %s: ", sb->s_id, function);
63-
vprintk(fmt, args);
64-
printk("\n");
63+
64+
vaf.fmt = fmt;
65+
vaf.va = &args;
66+
67+
printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
68+
sb->s_id, function, &vaf);
69+
6570
va_end(args);
6671

6772
if (test_opt(sb, ERRORS_PANIC))
@@ -76,12 +81,16 @@ void ext2_error (struct super_block * sb, const char * function,
7681
void ext2_msg(struct super_block *sb, const char *prefix,
7782
const char *fmt, ...)
7883
{
84+
struct va_format vaf;
7985
va_list args;
8086

8187
va_start(args, fmt);
82-
printk("%sEXT2-fs (%s): ", prefix, sb->s_id);
83-
vprintk(fmt, args);
84-
printk("\n");
88+
89+
vaf.fmt = fmt;
90+
vaf.va = &args;
91+
92+
printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
93+
8594
va_end(args);
8695
}
8796

‎fs/ext3/balloc.c‎

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/ext3_jbd.h>
2121
#include <linux/quotaops.h>
2222
#include <linux/buffer_head.h>
23+
#include <linux/blkdev.h>
2324

2425
/*
2526
* balloc.c contains the blocks allocation and deallocation routines
@@ -39,6 +40,21 @@
3940

4041
#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
4142

43+
/*
44+
* Calculate the block group number and offset, given a block number
45+
*/
46+
static 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+
4258
/**
4359
* ext3_get_group_desc() -- load group descriptor from disk
4460
* @sb: super block
@@ -1885,3 +1901,253 @@ unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
18851901
return ext3_bg_num_gdb_meta(sb,group);
18861902

18871903
}
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+
*/
1920+
ext3_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+
/* Do not issue a TRIM on extents smaller than minblocks */
1995+
if ((next - start) < minblocks)
1996+
goto free_extent;
1997+
1998+
/* Send the TRIM command down to the device */
1999+
err = sb_issue_discard(sb, discard_block, next - start,
2000+
GFP_NOFS, 0);
2001+
count += (next - start);
2002+
free_extent:
2003+
freed = 0;
2004+
2005+
/*
2006+
* Clear bits in the bitmap
2007+
*/
2008+
for (bit = start; bit < next; bit++) {
2009+
BUFFER_TRACE(bitmap_bh, "clear bit");
2010+
if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
2011+
bit, bitmap_bh->b_data)) {
2012+
ext3_error(sb, __func__,
2013+
"bit already cleared for block "E3FSBLK,
2014+
(unsigned long)bit);
2015+
BUFFER_TRACE(bitmap_bh, "bit already cleared");
2016+
} else {
2017+
freed++;
2018+
}
2019+
}
2020+
2021+
/* Update couters */
2022+
spin_lock(sb_bgl_lock(sbi, group));
2023+
le16_add_cpu(&gdp->bg_free_blocks_count, freed);
2024+
spin_unlock(sb_bgl_lock(sbi, group));
2025+
percpu_counter_add(&sbi->s_freeblocks_counter, freed);
2026+
2027+
start = next;
2028+
if (err < 0) {
2029+
if (err != -EOPNOTSUPP)
2030+
ext3_warning(sb, __func__, "Discard command "
2031+
"returned error %d\n", err);
2032+
break;
2033+
}
2034+
2035+
if (fatal_signal_pending(current)) {
2036+
err = -ERESTARTSYS;
2037+
break;
2038+
}
2039+
2040+
cond_resched();
2041+
2042+
/* No more suitable extents */
2043+
if ((free_blocks - count) < minblocks)
2044+
break;
2045+
}
2046+
2047+
/* We dirtied the bitmap block */
2048+
BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2049+
ret = ext3_journal_dirty_metadata(handle, bitmap_bh);
2050+
if (!err)
2051+
err = ret;
2052+
2053+
/* And the group descriptor block */
2054+
BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
2055+
ret = ext3_journal_dirty_metadata(handle, gdp_bh);
2056+
if (!err)
2057+
err = ret;
2058+
2059+
ext3_debug("trimmed %d blocks in the group %d\n",
2060+
count, group);
2061+
2062+
err_out:
2063+
if (err)
2064+
count = err;
2065+
ext3_journal_stop(handle);
2066+
brelse(bitmap_bh);
2067+
2068+
return count;
2069+
}
2070+
2071+
/**
2072+
* ext3_trim_fs() -- trim ioctl handle function
2073+
* @sb: superblock for filesystem
2074+
* @start: First Byte to trim
2075+
* @len: number of Bytes to trim from start
2076+
* @minlen: minimum extent length in Bytes
2077+
*
2078+
* ext3_trim_fs goes through all allocation groups containing Bytes from
2079+
* start to start+len. For each such a group ext3_trim_all_free function
2080+
* is invoked to trim all free space.
2081+
*/
2082+
int ext3_trim_fs(struct super_block *sb, struct fstrim_range *range)
2083+
{
2084+
ext3_grpblk_t last_block, first_block, free_blocks;
2085+
unsigned long first_group, last_group;
2086+
unsigned long group, ngroups;
2087+
struct ext3_group_desc *gdp;
2088+
struct ext3_super_block *es = EXT3_SB(sb)->s_es;
2089+
uint64_t start, len, minlen, trimmed;
2090+
ext3_fsblk_t max_blks = le32_to_cpu(es->s_blocks_count);
2091+
int ret = 0;
2092+
2093+
start = range->start >> sb->s_blocksize_bits;
2094+
len = range->len >> sb->s_blocksize_bits;
2095+
minlen = range->minlen >> sb->s_blocksize_bits;
2096+
trimmed = 0;
2097+
2098+
if (unlikely(minlen > EXT3_BLOCKS_PER_GROUP(sb)))
2099+
return -EINVAL;
2100+
if (start >= max_blks)
2101+
goto out;
2102+
if (start < le32_to_cpu(es->s_first_data_block)) {
2103+
len -= le32_to_cpu(es->s_first_data_block) - start;
2104+
start = le32_to_cpu(es->s_first_data_block);
2105+
}
2106+
if (start + len > max_blks)
2107+
len = max_blks - start;
2108+
2109+
ngroups = EXT3_SB(sb)->s_groups_count;
2110+
smp_rmb();
2111+
2112+
/* Determine first and last group to examine based on start and len */
2113+
ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) start,
2114+
&first_group, &first_block);
2115+
ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) (start + len),
2116+
&last_group, &last_block);
2117+
last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
2118+
last_block = EXT3_BLOCKS_PER_GROUP(sb);
2119+
2120+
if (first_group > last_group)
2121+
return -EINVAL;
2122+
2123+
for (group = first_group; group <= last_group; group++) {
2124+
gdp = ext3_get_group_desc(sb, group, NULL);
2125+
if (!gdp)
2126+
break;
2127+
2128+
free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
2129+
if (free_blocks < minlen)
2130+
continue;
2131+
2132+
if (len >= EXT3_BLOCKS_PER_GROUP(sb))
2133+
len -= (EXT3_BLOCKS_PER_GROUP(sb) - first_block);
2134+
else
2135+
last_block = first_block + len;
2136+
2137+
ret = ext3_trim_all_free(sb, group, first_block,
2138+
last_block, minlen);
2139+
if (ret < 0)
2140+
break;
2141+
2142+
trimmed += ret;
2143+
first_block = 0;
2144+
}
2145+
2146+
if (ret >= 0)
2147+
ret = 0;
2148+
2149+
out:
2150+
range->len = trimmed * sb->s_blocksize;
2151+
2152+
return ret;
2153+
}

0 commit comments

Comments
 (0)