--- /dev/null
+From 15b49132fc972c63894592f218ea5a9a61b1a18f Mon Sep 17 00:00:00 2001
+From: Eryu Guan <guaneryu@gmail.com>
+Date: Sat, 12 Jan 2013 16:33:25 -0500
+Subject: ext4: check bh in ext4_read_block_bitmap()
+
+From: Eryu Guan <guaneryu@gmail.com>
+
+commit 15b49132fc972c63894592f218ea5a9a61b1a18f upstream.
+
+Validate the bh pointer before using it, since
+ext4_read_block_bitmap_nowait() might return NULL.
+
+I've seen this in fsfuzz testing.
+
+ EXT4-fs error (device loop0): ext4_read_block_bitmap_nowait:385: comm touch: Cannot get buffer for block bitmap - block_group = 0, block_bitmap = 3925999616
+ BUG: unable to handle kernel NULL pointer dereference at (null)
+ IP: [<ffffffff8121de25>] ext4_wait_block_bitmap+0x25/0xe0
+ ...
+ Call Trace:
+ [<ffffffff8121e1e5>] ext4_read_block_bitmap+0x35/0x60
+ [<ffffffff8125e9c6>] ext4_free_blocks+0x236/0xb80
+ [<ffffffff811d0d36>] ? __getblk+0x36/0x70
+ [<ffffffff811d0a5f>] ? __find_get_block+0x8f/0x210
+ [<ffffffff81191ef3>] ? kmem_cache_free+0x33/0x140
+ [<ffffffff812678e5>] ext4_xattr_release_block+0x1b5/0x1d0
+ [<ffffffff812679be>] ext4_xattr_delete_inode+0xbe/0x100
+ [<ffffffff81222a7c>] ext4_free_inode+0x7c/0x4d0
+ [<ffffffff812277b8>] ? ext4_mark_inode_dirty+0x88/0x230
+ [<ffffffff8122993c>] ext4_evict_inode+0x32c/0x490
+ [<ffffffff811b8cd7>] evict+0xa7/0x1c0
+ [<ffffffff811b8ed3>] iput_final+0xe3/0x170
+ [<ffffffff811b8f9e>] iput+0x3e/0x50
+ [<ffffffff812316fd>] ext4_add_nondir+0x4d/0x90
+ [<ffffffff81231d0b>] ext4_create+0xeb/0x170
+ [<ffffffff811aae9c>] vfs_create+0xac/0xd0
+ [<ffffffff811ac845>] lookup_open+0x185/0x1c0
+ [<ffffffff8129e3b9>] ? selinux_inode_permission+0xa9/0x170
+ [<ffffffff811acb54>] do_last+0x2d4/0x7a0
+ [<ffffffff811af743>] path_openat+0xb3/0x480
+ [<ffffffff8116a8a1>] ? handle_mm_fault+0x251/0x3b0
+ [<ffffffff811afc49>] do_filp_open+0x49/0xa0
+ [<ffffffff811bbaad>] ? __alloc_fd+0xdd/0x150
+ [<ffffffff8119da28>] do_sys_open+0x108/0x1f0
+ [<ffffffff8119db51>] sys_open+0x21/0x30
+ [<ffffffff81618959>] system_call_fastpath+0x16/0x1b
+
+Also fix comment for ext4_read_block_bitmap_nowait()
+
+Signed-off-by: Eryu Guan <guaneryu@gmail.com>
+Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ fs/ext4/balloc.c | 4 +++-
+ 1 file changed, 3 insertions(+), 1 deletion(-)
+
+--- a/fs/ext4/balloc.c
++++ b/fs/ext4/balloc.c
+@@ -326,7 +326,7 @@ err_out:
+ return 0;
+ }
+ /**
+- * ext4_read_block_bitmap()
++ * ext4_read_block_bitmap_nowait()
+ * @sb: super block
+ * @block_group: given block group
+ *
+@@ -422,6 +422,8 @@ ext4_read_block_bitmap(struct super_bloc
+ struct buffer_head *bh;
+
+ bh = ext4_read_block_bitmap_nowait(sb, block_group);
++ if (!bh)
++ return NULL;
+ if (ext4_wait_block_bitmap(sb, block_group, bh)) {
+ put_bh(bh);
+ return NULL;
--- /dev/null
+From 304e220f0879198b1f5309ad6f0be862b4009491 Mon Sep 17 00:00:00 2001
+From: Lukas Czerner <lczerner@redhat.com>
+Date: Fri, 22 Feb 2013 15:27:52 -0500
+Subject: ext4: fix free clusters calculation in bigalloc filesystem
+
+From: Lukas Czerner <lczerner@redhat.com>
+
+commit 304e220f0879198b1f5309ad6f0be862b4009491 upstream.
+
+ext4_has_free_clusters() should tell us whether there is enough free
+clusters to allocate, however number of free clusters in the file system
+is converted to blocks using EXT4_C2B() which is not only wrong use of
+the macro (we should have used EXT4_NUM_B2C) but it's also completely
+wrong concept since everything else is in cluster units.
+
+Moreover when calculating number of root clusters we should be using
+macro EXT4_NUM_B2C() instead of EXT4_B2C() otherwise the result might be
+off by one. However r_blocks_count should always be a multiple of the
+cluster ratio so doing a plain bit shift should be enough here. We
+avoid using EXT4_B2C() because it's confusing.
+
+As a result of the first problem number of free clusters is much bigger
+than it should have been and ext4_has_free_clusters() would return 1 even
+if there is really not enough free clusters available.
+
+Fix this by removing the EXT4_C2B() conversion of free clusters and
+using bit shift when calculating number of root clusters. This bug
+affects number of xfstests tests covering file system ENOSPC situation
+handling. With this patch most of the ENOSPC problems with bigalloc file
+system disappear, especially the errors caused by delayed allocation not
+having enough space when the actual allocation is finally requested.
+
+Signed-off-by: Lukas Czerner <lczerner@redhat.com>
+Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ fs/ext4/balloc.c | 9 +++++++--
+ 1 file changed, 7 insertions(+), 2 deletions(-)
+
+--- a/fs/ext4/balloc.c
++++ b/fs/ext4/balloc.c
+@@ -449,11 +449,16 @@ static int ext4_has_free_clusters(struct
+
+ free_clusters = percpu_counter_read_positive(fcc);
+ dirty_clusters = percpu_counter_read_positive(dcc);
+- root_clusters = EXT4_B2C(sbi, ext4_r_blocks_count(sbi->s_es));
++
++ /*
++ * r_blocks_count should always be multiple of the cluster ratio so
++ * we are safe to do a plane bit shift only.
++ */
++ root_clusters = ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits;
+
+ if (free_clusters - (nclusters + root_clusters + dirty_clusters) <
+ EXT4_FREECLUSTERS_WATERMARK) {
+- free_clusters = EXT4_C2B(sbi, percpu_counter_sum_positive(fcc));
++ free_clusters = percpu_counter_sum_positive(fcc);
+ dirty_clusters = percpu_counter_sum_positive(dcc);
+ }
+ /* Check whether we have space after accounting for current
--- /dev/null
+From f1167009711032b0d747ec89a632a626c901a1ad Mon Sep 17 00:00:00 2001
+From: Niu Yawei <yawei.niu@gmail.com>
+Date: Fri, 1 Feb 2013 21:31:27 -0500
+Subject: ext4: fix race in ext4_mb_add_n_trim()
+
+From: Niu Yawei <yawei.niu@gmail.com>
+
+commit f1167009711032b0d747ec89a632a626c901a1ad upstream.
+
+In ext4_mb_add_n_trim(), lg_prealloc_lock should be taken when
+changing the lg_prealloc_list.
+
+Signed-off-by: Niu Yawei <yawei.niu@intel.com>
+Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ fs/ext4/mballoc.c | 6 +++---
+ 1 file changed, 3 insertions(+), 3 deletions(-)
+
+--- a/fs/ext4/mballoc.c
++++ b/fs/ext4/mballoc.c
+@@ -4126,7 +4126,7 @@ static void ext4_mb_add_n_trim(struct ex
+ /* The max size of hash table is PREALLOC_TB_SIZE */
+ order = PREALLOC_TB_SIZE - 1;
+ /* Add the prealloc space to lg */
+- rcu_read_lock();
++ spin_lock(&lg->lg_prealloc_lock);
+ list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
+ pa_inode_list) {
+ spin_lock(&tmp_pa->pa_lock);
+@@ -4150,12 +4150,12 @@ static void ext4_mb_add_n_trim(struct ex
+ if (!added)
+ list_add_tail_rcu(&pa->pa_inode_list,
+ &lg->lg_prealloc_list[order]);
+- rcu_read_unlock();
++ spin_unlock(&lg->lg_prealloc_lock);
+
+ /* Now trim the list to be not more than 8 elements */
+ if (lg_prealloc_count > 8) {
+ ext4_mb_discard_lg_preallocations(sb, lg,
+- order, lg_prealloc_count);
++ order, lg_prealloc_count);
+ return;
+ }
+ return ;
--- /dev/null
+From 1231b3a1eb5740192aeebf5344dd6d6da000febf Mon Sep 17 00:00:00 2001
+From: Lukas Czerner <lczerner@redhat.com>
+Date: Mon, 18 Feb 2013 12:12:07 -0500
+Subject: ext4: fix xattr block allocation/release with bigalloc
+
+From: Lukas Czerner <lczerner@redhat.com>
+
+commit 1231b3a1eb5740192aeebf5344dd6d6da000febf upstream.
+
+Currently when new xattr block is created or released we we would call
+dquot_free_block() or dquot_alloc_block() respectively, among the else
+decrementing or incrementing the number of blocks assigned to the
+inode by one block.
+
+This however does not work for bigalloc file system because we always
+allocate/free the whole cluster so we have to count with that in
+dquot_free_block() and dquot_alloc_block() as well.
+
+Use the clusters-to-blocks conversion EXT4_C2B() when passing number of
+blocks to the dquot_alloc/free functions to fix the problem.
+
+The problem has been revealed by xfstests #117 (and possibly others).
+
+Signed-off-by: Lukas Czerner <lczerner@redhat.com>
+Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
+Reviewed-by: Eric Sandeen <sandeen@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ fs/ext4/xattr.c | 7 ++++---
+ 1 file changed, 4 insertions(+), 3 deletions(-)
+
+--- a/fs/ext4/xattr.c
++++ b/fs/ext4/xattr.c
+@@ -495,7 +495,7 @@ ext4_xattr_release_block(handle_t *handl
+ error = ext4_handle_dirty_metadata(handle, inode, bh);
+ if (IS_SYNC(inode))
+ ext4_handle_sync(handle);
+- dquot_free_block(inode, 1);
++ dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
+ ea_bdebug(bh, "refcount now=%d; releasing",
+ le32_to_cpu(BHDR(bh)->h_refcount));
+ }
+@@ -784,7 +784,8 @@ inserted:
+ else {
+ /* The old block is released after updating
+ the inode. */
+- error = dquot_alloc_block(inode, 1);
++ error = dquot_alloc_block(inode,
++ EXT4_C2B(EXT4_SB(sb), 1));
+ if (error)
+ goto cleanup;
+ error = ext4_journal_get_write_access(handle,
+@@ -880,7 +881,7 @@ cleanup:
+ return error;
+
+ cleanup_dquot:
+- dquot_free_block(inode, 1);
++ dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
+ goto cleanup;
+
+ bad_block:
sysctl-fix-null-checking-in-bin_dn_node_address.patch
fs-fix-possible-use-after-free-with-aio.patch
media-rc-unlock-on-error-in-show_protocols.patch
+ext4-check-bh-in-ext4_read_block_bitmap.patch
+ext4-fix-race-in-ext4_mb_add_n_trim.patch
+ext4-fix-xattr-block-allocation-release-with-bigalloc.patch
+ext4-fix-free-clusters-calculation-in-bigalloc-filesystem.patch