Commit 40c73abb authored by Linus Torvalds's avatar Linus Torvalds
Browse files

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
parents 0945f352 d96336b0
......@@ -28,21 +28,30 @@
typedef struct ext2_dir_entry_2 ext2_dirent;
/*
* Tests against MAX_REC_LEN etc were put in place for 64k block
* sizes; if that is not possible on this arch, we can skip
* those tests and speed things up.
*/
static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
{
unsigned len = le16_to_cpu(dlen);
#if (PAGE_CACHE_SIZE >= 65536)
if (len == EXT2_MAX_REC_LEN)
return 1 << 16;
#endif
return len;
}
static inline __le16 ext2_rec_len_to_disk(unsigned len)
{
#if (PAGE_CACHE_SIZE >= 65536)
if (len == (1 << 16))
return cpu_to_le16(EXT2_MAX_REC_LEN);
else
BUG_ON(len > (1 << 16));
#endif
return cpu_to_le16(len);
}
......@@ -129,15 +138,15 @@ static void ext2_check_page(struct page *page, int quiet)
p = (ext2_dirent *)(kaddr + offs);
rec_len = ext2_rec_len_from_disk(p->rec_len);
if (rec_len < EXT2_DIR_REC_LEN(1))
if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
goto Eshort;
if (rec_len & 3)
if (unlikely(rec_len & 3))
goto Ealign;
if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
goto Enamelen;
if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
goto Espan;
if (le32_to_cpu(p->inode) > max_inumber)
if (unlikely(le32_to_cpu(p->inode) > max_inumber))
goto Einumber;
}
if (offs != limit)
......
......@@ -67,7 +67,7 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, str
inode = NULL;
if (ino) {
inode = ext2_iget(dir->i_sb, ino);
if (unlikely(IS_ERR(inode))) {
if (IS_ERR(inode)) {
if (PTR_ERR(inode) == -ESTALE) {
ext2_error(dir->i_sb, __func__,
"deleted inode referenced: %lu",
......
......@@ -43,9 +43,10 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data);
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
static int ext2_sync_fs(struct super_block *sb, int wait);
void ext2_error (struct super_block * sb, const char * function,
const char * fmt, ...)
void ext2_error(struct super_block *sb, const char *function,
const char *fmt, ...)
{
struct va_format vaf;
va_list args;
struct ext2_sb_info *sbi = EXT2_SB(sb);
struct ext2_super_block *es = sbi->s_es;
......@@ -59,9 +60,13 @@ void ext2_error (struct super_block * sb, const char * function,
}
va_start(args, fmt);
printk(KERN_CRIT "EXT2-fs (%s): error: %s: ", sb->s_id, function);
vprintk(fmt, args);
printk("\n");
vaf.fmt = fmt;
vaf.va = &args;
printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
sb->s_id, function, &vaf);
va_end(args);
if (test_opt(sb, ERRORS_PANIC))
......@@ -76,12 +81,16 @@ void ext2_error (struct super_block * sb, const char * function,
void ext2_msg(struct super_block *sb, const char *prefix,
const char *fmt, ...)
{
struct va_format vaf;
va_list args;
va_start(args, fmt);
printk("%sEXT2-fs (%s): ", prefix, sb->s_id);
vprintk(fmt, args);
printk("\n");
vaf.fmt = fmt;
vaf.va = &args;
printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
va_end(args);
}
......
......@@ -20,6 +20,7 @@
#include <linux/ext3_jbd.h>
#include <linux/quotaops.h>
#include <linux/buffer_head.h>
#include <linux/blkdev.h>
/*
* balloc.c contains the blocks allocation and deallocation routines
......@@ -39,6 +40,21 @@
#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
/*
* Calculate the block group number and offset, given a block number
*/
static void ext3_get_group_no_and_offset(struct super_block *sb,
ext3_fsblk_t blocknr, unsigned long *blockgrpp, ext3_grpblk_t *offsetp)
{
struct ext3_super_block *es = EXT3_SB(sb)->s_es;
blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
if (offsetp)
*offsetp = blocknr % EXT3_BLOCKS_PER_GROUP(sb);
if (blockgrpp)
*blockgrpp = blocknr / EXT3_BLOCKS_PER_GROUP(sb);
}
/**
* ext3_get_group_desc() -- load group descriptor from disk
* @sb: super block
......@@ -1885,3 +1901,253 @@ unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
return ext3_bg_num_gdb_meta(sb,group);
}
/**
* ext3_trim_all_free -- function to trim all free space in alloc. group
* @sb: super block for file system
* @group: allocation group to trim
* @start: first group block to examine
* @max: last group block to examine
* @gdp: allocation group description structure
* @minblocks: minimum extent block count
*
* ext3_trim_all_free walks through group's block bitmap searching for free
* blocks. When the free block is found, it tries to allocate this block and
* consequent free block to get the biggest free extent possible, until it
* reaches any used block. Then issue a TRIM command on this extent and free
* the extent in the block bitmap. This is done until whole group is scanned.
*/
ext3_grpblk_t ext3_trim_all_free(struct super_block *sb, unsigned int group,
ext3_grpblk_t start, ext3_grpblk_t max,
ext3_grpblk_t minblocks)
{
handle_t *handle;
ext3_grpblk_t next, free_blocks, bit, freed, count = 0;
ext3_fsblk_t discard_block;
struct ext3_sb_info *sbi;
struct buffer_head *gdp_bh, *bitmap_bh = NULL;
struct ext3_group_desc *gdp;
int err = 0, ret = 0;
/*
* We will update one block bitmap, and one group descriptor
*/
handle = ext3_journal_start_sb(sb, 2);
if (IS_ERR(handle))
return PTR_ERR(handle);
bitmap_bh = read_block_bitmap(sb, group);
if (!bitmap_bh) {
err = -EIO;
goto err_out;
}
BUFFER_TRACE(bitmap_bh, "getting undo access");
err = ext3_journal_get_undo_access(handle, bitmap_bh);
if (err)
goto err_out;
gdp = ext3_get_group_desc(sb, group, &gdp_bh);
if (!gdp) {
err = -EIO;
goto err_out;
}
BUFFER_TRACE(gdp_bh, "get_write_access");
err = ext3_journal_get_write_access(handle, gdp_bh);
if (err)
goto err_out;
free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
sbi = EXT3_SB(sb);
/* Walk through the whole group */
while (start < max) {
start = bitmap_search_next_usable_block(start, bitmap_bh, max);
if (start < 0)
break;
next = start;
/*
* Allocate contiguous free extents by setting bits in the
* block bitmap
*/
while (next < max
&& claim_block(sb_bgl_lock(sbi, group),
next, bitmap_bh)) {
next++;
}
/* We did not claim any blocks */
if (next == start)
continue;
discard_block = (ext3_fsblk_t)start +
ext3_group_first_block_no(sb, group);
/* Update counters */
spin_lock(sb_bgl_lock(sbi, group));
le16_add_cpu(&gdp->bg_free_blocks_count, start - next);
spin_unlock(sb_bgl_lock(sbi, group));
percpu_counter_sub(&sbi->s_freeblocks_counter, next - start);
/* Do not issue a TRIM on extents smaller than minblocks */
if ((next - start) < minblocks)
goto free_extent;
/* Send the TRIM command down to the device */
err = sb_issue_discard(sb, discard_block, next - start,
GFP_NOFS, 0);
count += (next - start);
free_extent:
freed = 0;
/*
* Clear bits in the bitmap
*/
for (bit = start; bit < next; bit++) {
BUFFER_TRACE(bitmap_bh, "clear bit");
if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
bit, bitmap_bh->b_data)) {
ext3_error(sb, __func__,
"bit already cleared for block "E3FSBLK,
(unsigned long)bit);
BUFFER_TRACE(bitmap_bh, "bit already cleared");
} else {
freed++;
}
}
/* Update couters */
spin_lock(sb_bgl_lock(sbi, group));
le16_add_cpu(&gdp->bg_free_blocks_count, freed);
spin_unlock(sb_bgl_lock(sbi, group));
percpu_counter_add(&sbi->s_freeblocks_counter, freed);
start = next;
if (err < 0) {
if (err != -EOPNOTSUPP)
ext3_warning(sb, __func__, "Discard command "
"returned error %d\n", err);
break;
}
if (fatal_signal_pending(current)) {
err = -ERESTARTSYS;
break;
}
cond_resched();
/* No more suitable extents */
if ((free_blocks - count) < minblocks)
break;
}
/* We dirtied the bitmap block */
BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
ret = ext3_journal_dirty_metadata(handle, bitmap_bh);
if (!err)
err = ret;
/* And the group descriptor block */
BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
ret = ext3_journal_dirty_metadata(handle, gdp_bh);
if (!err)
err = ret;
ext3_debug("trimmed %d blocks in the group %d\n",
count, group);
err_out:
if (err)
count = err;
ext3_journal_stop(handle);
brelse(bitmap_bh);
return count;
}
/**
* ext3_trim_fs() -- trim ioctl handle function
* @sb: superblock for filesystem
* @start: First Byte to trim
* @len: number of Bytes to trim from start
* @minlen: minimum extent length in Bytes
*
* ext3_trim_fs goes through all allocation groups containing Bytes from
* start to start+len. For each such a group ext3_trim_all_free function
* is invoked to trim all free space.
*/
int ext3_trim_fs(struct super_block *sb, struct fstrim_range *range)
{
ext3_grpblk_t last_block, first_block, free_blocks;
unsigned long first_group, last_group;
unsigned long group, ngroups;
struct ext3_group_desc *gdp;
struct ext3_super_block *es = EXT3_SB(sb)->s_es;
uint64_t start, len, minlen, trimmed;
ext3_fsblk_t max_blks = le32_to_cpu(es->s_blocks_count);
int ret = 0;
start = range->start >> sb->s_blocksize_bits;
len = range->len >> sb->s_blocksize_bits;
minlen = range->minlen >> sb->s_blocksize_bits;
trimmed = 0;
if (unlikely(minlen > EXT3_BLOCKS_PER_GROUP(sb)))
return -EINVAL;
if (start >= max_blks)
goto out;
if (start < le32_to_cpu(es->s_first_data_block)) {
len -= le32_to_cpu(es->s_first_data_block) - start;
start = le32_to_cpu(es->s_first_data_block);
}
if (start + len > max_blks)
len = max_blks - start;
ngroups = EXT3_SB(sb)->s_groups_count;
smp_rmb();
/* Determine first and last group to examine based on start and len */
ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) start,
&first_group, &first_block);
ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) (start + len),
&last_group, &last_block);
last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
last_block = EXT3_BLOCKS_PER_GROUP(sb);
if (first_group > last_group)
return -EINVAL;
for (group = first_group; group <= last_group; group++) {
gdp = ext3_get_group_desc(sb, group, NULL);
if (!gdp)
break;
free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
if (free_blocks < minlen)
continue;
if (len >= EXT3_BLOCKS_PER_GROUP(sb))
len -= (EXT3_BLOCKS_PER_GROUP(sb) - first_block);
else
last_block = first_block + len;
ret = ext3_trim_all_free(sb, group, first_block,
last_block, minlen);
if (ret < 0)
break;
trimmed += ret;
first_block = 0;
}
if (ret >= 0)
ret = 0;
out:
range->len = trimmed * sb->s_blocksize;
return ret;
}
......@@ -69,25 +69,26 @@ int ext3_check_dir_entry (const char * function, struct inode * dir,
const char * error_msg = NULL;
const int rlen = ext3_rec_len_from_disk(de->rec_len);
if (rlen < EXT3_DIR_REC_LEN(1))
if (unlikely(rlen < EXT3_DIR_REC_LEN(1)))
error_msg = "rec_len is smaller than minimal";
else if (rlen % 4 != 0)
else if (unlikely(rlen % 4 != 0))
error_msg = "rec_len % 4 != 0";
else if (rlen < EXT3_DIR_REC_LEN(de->name_len))
else if (unlikely(rlen < EXT3_DIR_REC_LEN(de->name_len)))
error_msg = "rec_len is too small for name_len";
else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
else if (unlikely((((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)))
error_msg = "directory entry across blocks";
else if (le32_to_cpu(de->inode) >
le32_to_cpu(EXT3_SB(dir->i_sb)->s_es->s_inodes_count))
else if (unlikely(le32_to_cpu(de->inode) >
le32_to_cpu(EXT3_SB(dir->i_sb)->s_es->s_inodes_count)))
error_msg = "inode out of bounds";
if (error_msg != NULL)
if (unlikely(error_msg != NULL))
ext3_error (dir->i_sb, function,
"bad entry in directory #%lu: %s - "
"offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
dir->i_ino, error_msg, offset,
(unsigned long) le32_to_cpu(de->inode),
rlen, de->name_len);
return error_msg == NULL ? 1 : 0;
}
......
......@@ -2145,13 +2145,15 @@ static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
if (try_to_extend_transaction(handle, inode)) {
if (bh) {
BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
ext3_journal_dirty_metadata(handle, bh);
if (ext3_journal_dirty_metadata(handle, bh))
return;
}
ext3_mark_inode_dirty(handle, inode);
truncate_restart_transaction(handle, inode);
if (bh) {
BUFFER_TRACE(bh, "retaking write access");
ext3_journal_get_write_access(handle, bh);
if (ext3_journal_get_write_access(handle, bh))
return;
}
}
......
......@@ -276,7 +276,29 @@ group_add_out:
mnt_drop_write(filp->f_path.mnt);
return err;
}
case FITRIM: {
struct super_block *sb = inode->i_sb;
struct fstrim_range range;
int ret = 0;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (copy_from_user(&range, (struct fstrim_range *)arg,
sizeof(range)))
return -EFAULT;
ret = ext3_trim_fs(sb, &range);
if (ret < 0)
return ret;
if (copy_to_user((struct fstrim_range *)arg, &range,
sizeof(range)))
return -EFAULT;
return 0;
}
default:
return -ENOTTY;
......
......@@ -858,6 +858,7 @@ static struct buffer_head *ext3_find_entry(struct inode *dir,
struct buffer_head * bh_use[NAMEI_RA_SIZE];
struct buffer_head * bh, *ret = NULL;
unsigned long start, block, b;
const u8 *name = entry->name;
int ra_max = 0; /* Number of bh's in the readahead
buffer, bh_use[] */
int ra_ptr = 0; /* Current index into readahead
......@@ -871,6 +872,16 @@ static struct buffer_head *ext3_find_entry(struct inode *dir,
namelen = entry->len;
if (namelen > EXT3_NAME_LEN)
return NULL;
if ((namelen <= 2) && (name[0] == '.') &&
(name[1] == '.' || name[1] == 0)) {
/*
* "." or ".." will only be in the first block
* NFS may look up ".."; "." should be handled by the VFS
*/
block = start = 0;
nblocks = 1;
goto restart;
}
if (is_dx(dir)) {
bh = ext3_dx_find_entry(dir, entry, res_dir, &err);
/*
......@@ -961,55 +972,35 @@ static struct buffer_head * ext3_dx_find_entry(struct inode *dir,
struct qstr *entry, struct ext3_dir_entry_2 **res_dir,
int *err)
{
struct super_block * sb;
struct super_block *sb = dir->i_sb;
struct dx_hash_info hinfo;
u32 hash;
struct dx_frame frames[2], *frame;
struct ext3_dir_entry_2 *de, *top;
struct buffer_head *bh;
unsigned long block;
int retval;
int namelen = entry->len;
const u8 *name = entry->name;
sb = dir->i_sb;
/* NFS may look up ".." - look at dx_root directory block */
if (namelen > 2 || name[0] != '.'|| (namelen == 2 && name[1] != '.')) {
if (!(frame = dx_probe(entry, dir, &hinfo, frames, err)))
return NULL;
} else {
frame = frames;
frame->bh = NULL; /* for dx_release() */
frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
dx_set_block(frame->at, 0); /* dx_root block is 0 */
}
hash = hinfo.hash;
if (!(frame = dx_probe(entry, dir, &hinfo, frames, err)))
return NULL;
do {
block = dx_get_block(frame->at);
if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
goto errout;
de = (struct ext3_dir_entry_2 *) bh->b_data;
top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
EXT3_DIR_REC_LEN(0));
for (; de < top; de = ext3_next_entry(de)) {
int off = (block << EXT3_BLOCK_SIZE_BITS(sb))
+ ((char *) de - bh->b_data);
if (!ext3_check_dir_entry(__func__, dir, de, bh, off)) {
brelse(bh);
*err = ERR_BAD_DX_DIR;
goto errout;
}
if (ext3_match(namelen, name, de)) {
*res_dir = de;
dx_release(frames);
return bh;
}
retval = search_dirblock(bh, dir, entry,
block << EXT3_BLOCK_SIZE_BITS(sb),
res_dir);
if (retval == 1) {
dx_release(frames);
return bh;
}
brelse (bh);
brelse(bh);
if (retval == -1) {
*err = ERR_BAD_DX_DIR;
goto errout;
}
/* Check to see if we should continue to search */
retval = ext3_htree_next_block(dir, hash, frame,
retval = ext3_htree_next_block(dir, hinfo.hash, frame,
frames, NULL);
if (retval < 0) {
ext3_warning(sb, __func__,
......@@ -1047,7 +1038,7 @@ static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, str
return ERR_PTR(-EIO);
}
inode = ext3_iget(dir->i_sb, ino);
if (unlikely(IS_ERR(inode))) {
if (IS_ERR(inode)) {
if (PTR_ERR(inode) == -ESTALE) {
ext3_error(dir->i_sb, __func__,
"deleted inode referenced: %lu",
......@@ -1607,7 +1598,9 @@ static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
if (err)
goto journal_error;
}
ext3_journal_dirty_metadata(handle, frames[0].bh);
err = ext3_journal_dirty_metadata(handle, frames[0].bh);
if (err)
goto journal_error;
}
de = do_split(handle, dir, &bh, frame, &hinfo, &err);
if (!de)
......@@ -1644,8 +1637,13 @@ static int ext3_delete_entry (handle_t *handle,
if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
return -EIO;
if (de == de_del) {
int err;
BUFFER_TRACE(bh, "get_write_access");
ext3_journal_get_write_access(handle, bh);
err = ext3_journal_get_write_access(handle, bh);
if (err)
goto journal_error;
if (pde)
pde->rec_len = ext3_rec_len_to_disk(
ext3_rec_len_from_disk(pde->rec_len) +
......@@ -1654,7 +1652,12 @@ static int ext3_delete_entry (handle_t *handle,
de->inode = 0;
dir->i_version++;
BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
ext3_journal_dirty_metadata(handle, bh);
err = ext3_journal_dirty_metadata(handle, bh);