--- /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
+@@ -358,7 +358,7 @@ void ext4_validate_block_bitmap(struct s
+ }
+
+ /**
+- * ext4_read_block_bitmap()
++ * ext4_read_block_bitmap_nowait()
+ * @sb: super block
+ * @block_group: given block group
+ *
+@@ -457,6 +457,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
+@@ -484,11 +484,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
+@@ -4136,7 +4136,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);
+@@ -4160,12 +4160,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
+@@ -549,7 +549,7 @@ ext4_xattr_release_block(handle_t *handl
+ error = ext4_handle_dirty_xattr_block(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));
+ }
+@@ -832,7 +832,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,
+@@ -929,7 +930,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:
--- /dev/null
+From 72ba74508b2857e71d65fc93f0d6b684492fc740 Mon Sep 17 00:00:00 2001
+From: Theodore Ts'o <tytso@mit.edu>
+Date: Thu, 24 Jan 2013 23:24:54 -0500
+Subject: ext4: release sysfs kobject when failing to enable quotas on mount
+
+From: Theodore Ts'o <tytso@mit.edu>
+
+commit 72ba74508b2857e71d65fc93f0d6b684492fc740 upstream.
+
+In addition, print the error returned from ext4_enable_quotas()
+
+Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
+Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ fs/ext4/super.c | 12 ++++++++----
+ 1 file changed, 8 insertions(+), 4 deletions(-)
+
+--- a/fs/ext4/super.c
++++ b/fs/ext4/super.c
+@@ -4008,7 +4008,7 @@ no_journal:
+ !(sb->s_flags & MS_RDONLY)) {
+ err = ext4_enable_quotas(sb);
+ if (err)
+- goto failed_mount7;
++ goto failed_mount8;
+ }
+ #endif /* CONFIG_QUOTA */
+
+@@ -4035,6 +4035,10 @@ cantfind_ext4:
+ ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
+ goto failed_mount;
+
++#ifdef CONFIG_QUOTA
++failed_mount8:
++ kobject_del(&sbi->s_kobj);
++#endif
+ failed_mount7:
+ ext4_unregister_li_request(sb);
+ failed_mount6:
+@@ -5005,9 +5009,9 @@ static int ext4_enable_quotas(struct sup
+ DQUOT_USAGE_ENABLED);
+ if (err) {
+ ext4_warning(sb,
+- "Failed to enable quota (type=%d) "
+- "tracking. Please run e2fsck to fix.",
+- type);
++ "Failed to enable quota tracking "
++ "(type=%d, err=%d). Please run "
++ "e2fsck to fix.", type, err);
+ return err;
+ }
+ }
--- /dev/null
+From 860d21e2c585f7ee8a4ecc06f474fdc33c9474f4 Mon Sep 17 00:00:00 2001
+From: Theodore Ts'o <tytso@mit.edu>
+Date: Sat, 12 Jan 2013 16:19:36 -0500
+Subject: ext4: return ENOMEM if sb_getblk() fails
+
+From: Theodore Ts'o <tytso@mit.edu>
+
+commit 860d21e2c585f7ee8a4ecc06f474fdc33c9474f4 upstream.
+
+The only reason for sb_getblk() failing is if it can't allocate the
+buffer_head. So ENOMEM is more appropriate than EIO. In addition,
+make sure that the file system is marked as being inconsistent if
+sb_getblk() fails.
+
+Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ fs/ext4/extents.c | 25 ++++++++++++++-----------
+ fs/ext4/indirect.c | 9 ++++++---
+ fs/ext4/inline.c | 2 +-
+ fs/ext4/inode.c | 9 +++------
+ fs/ext4/mmp.c | 2 ++
+ fs/ext4/resize.c | 8 ++++----
+ fs/ext4/xattr.c | 3 ++-
+ 7 files changed, 32 insertions(+), 26 deletions(-)
+
+--- a/fs/ext4/extents.c
++++ b/fs/ext4/extents.c
+@@ -725,6 +725,7 @@ ext4_ext_find_extent(struct inode *inode
+ struct ext4_extent_header *eh;
+ struct buffer_head *bh;
+ short int depth, i, ppos = 0, alloc = 0;
++ int ret;
+
+ eh = ext_inode_hdr(inode);
+ depth = ext_depth(inode);
+@@ -752,12 +753,15 @@ ext4_ext_find_extent(struct inode *inode
+ path[ppos].p_ext = NULL;
+
+ bh = sb_getblk(inode->i_sb, path[ppos].p_block);
+- if (unlikely(!bh))
++ if (unlikely(!bh)) {
++ ret = -ENOMEM;
+ goto err;
++ }
+ if (!bh_uptodate_or_lock(bh)) {
+ trace_ext4_ext_load_extent(inode, block,
+ path[ppos].p_block);
+- if (bh_submit_read(bh) < 0) {
++ ret = bh_submit_read(bh);
++ if (ret < 0) {
+ put_bh(bh);
+ goto err;
+ }
+@@ -768,13 +772,15 @@ ext4_ext_find_extent(struct inode *inode
+ put_bh(bh);
+ EXT4_ERROR_INODE(inode,
+ "ppos %d > depth %d", ppos, depth);
++ ret = -EIO;
+ goto err;
+ }
+ path[ppos].p_bh = bh;
+ path[ppos].p_hdr = eh;
+ i--;
+
+- if (ext4_ext_check_block(inode, eh, i, bh))
++ ret = ext4_ext_check_block(inode, eh, i, bh);
++ if (ret < 0)
+ goto err;
+ }
+
+@@ -796,7 +802,7 @@ err:
+ ext4_ext_drop_refs(path);
+ if (alloc)
+ kfree(path);
+- return ERR_PTR(-EIO);
++ return ERR_PTR(ret);
+ }
+
+ /*
+@@ -951,7 +957,7 @@ static int ext4_ext_split(handle_t *hand
+ }
+ bh = sb_getblk(inode->i_sb, newblock);
+ if (!bh) {
+- err = -EIO;
++ err = -ENOMEM;
+ goto cleanup;
+ }
+ lock_buffer(bh);
+@@ -1024,7 +1030,7 @@ static int ext4_ext_split(handle_t *hand
+ newblock = ablocks[--a];
+ bh = sb_getblk(inode->i_sb, newblock);
+ if (!bh) {
+- err = -EIO;
++ err = -ENOMEM;
+ goto cleanup;
+ }
+ lock_buffer(bh);
+@@ -1136,11 +1142,8 @@ static int ext4_ext_grow_indepth(handle_
+ return err;
+
+ bh = sb_getblk(inode->i_sb, newblock);
+- if (!bh) {
+- err = -EIO;
+- ext4_std_error(inode->i_sb, err);
+- return err;
+- }
++ if (!bh)
++ return -ENOMEM;
+ lock_buffer(bh);
+
+ err = ext4_journal_get_create_access(handle, bh);
+--- a/fs/ext4/indirect.c
++++ b/fs/ext4/indirect.c
+@@ -146,6 +146,7 @@ static Indirect *ext4_get_branch(struct
+ struct super_block *sb = inode->i_sb;
+ Indirect *p = chain;
+ struct buffer_head *bh;
++ int ret = -EIO;
+
+ *err = 0;
+ /* i_data is not going away, no lock needed */
+@@ -154,8 +155,10 @@ static Indirect *ext4_get_branch(struct
+ goto no_block;
+ while (--depth) {
+ bh = sb_getblk(sb, le32_to_cpu(p->key));
+- if (unlikely(!bh))
++ if (unlikely(!bh)) {
++ ret = -ENOMEM;
+ goto failure;
++ }
+
+ if (!bh_uptodate_or_lock(bh)) {
+ if (bh_submit_read(bh) < 0) {
+@@ -177,7 +180,7 @@ static Indirect *ext4_get_branch(struct
+ return NULL;
+
+ failure:
+- *err = -EIO;
++ *err = ret;
+ no_block:
+ return p;
+ }
+@@ -471,7 +474,7 @@ static int ext4_alloc_branch(handle_t *h
+ */
+ bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
+ if (unlikely(!bh)) {
+- err = -EIO;
++ err = -ENOMEM;
+ goto failed;
+ }
+
+--- a/fs/ext4/inline.c
++++ b/fs/ext4/inline.c
+@@ -1188,7 +1188,7 @@ static int ext4_convert_inline_data_nolo
+
+ data_bh = sb_getblk(inode->i_sb, map.m_pblk);
+ if (!data_bh) {
+- error = -EIO;
++ error = -ENOMEM;
+ goto out_restore;
+ }
+
+--- a/fs/ext4/inode.c
++++ b/fs/ext4/inode.c
+@@ -714,7 +714,7 @@ struct buffer_head *ext4_getblk(handle_t
+
+ bh = sb_getblk(inode->i_sb, map.m_pblk);
+ if (!bh) {
+- *errp = -EIO;
++ *errp = -ENOMEM;
+ return NULL;
+ }
+ if (map.m_flags & EXT4_MAP_NEW) {
+@@ -3660,11 +3660,8 @@ static int __ext4_get_inode_loc(struct i
+ iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
+
+ bh = sb_getblk(sb, block);
+- if (!bh) {
+- EXT4_ERROR_INODE_BLOCK(inode, block,
+- "unable to read itable block");
+- return -EIO;
+- }
++ if (!bh)
++ return -ENOMEM;
+ if (!buffer_uptodate(bh)) {
+ lock_buffer(bh);
+
+--- a/fs/ext4/mmp.c
++++ b/fs/ext4/mmp.c
+@@ -80,6 +80,8 @@ static int read_mmp_block(struct super_b
+ * is not blocked in the elevator. */
+ if (!*bh)
+ *bh = sb_getblk(sb, mmp_block);
++ if (!*bh)
++ return -ENOMEM;
+ if (*bh) {
+ get_bh(*bh);
+ lock_buffer(*bh);
+--- a/fs/ext4/resize.c
++++ b/fs/ext4/resize.c
+@@ -334,7 +334,7 @@ static struct buffer_head *bclean(handle
+
+ bh = sb_getblk(sb, blk);
+ if (!bh)
+- return ERR_PTR(-EIO);
++ return ERR_PTR(-ENOMEM);
+ if ((err = ext4_journal_get_write_access(handle, bh))) {
+ brelse(bh);
+ bh = ERR_PTR(err);
+@@ -411,7 +411,7 @@ static int set_flexbg_block_bitmap(struc
+
+ bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
+ if (!bh)
+- return -EIO;
++ return -ENOMEM;
+
+ err = ext4_journal_get_write_access(handle, bh);
+ if (err)
+@@ -501,7 +501,7 @@ static int setup_new_flex_group_blocks(s
+
+ gdb = sb_getblk(sb, block);
+ if (!gdb) {
+- err = -EIO;
++ err = -ENOMEM;
+ goto out;
+ }
+
+@@ -1065,7 +1065,7 @@ static void update_backups(struct super_
+
+ bh = sb_getblk(sb, backup_block);
+ if (!bh) {
+- err = -EIO;
++ err = -ENOMEM;
+ break;
+ }
+ ext4_debug("update metadata backup %llu(+%llu)\n",
+--- a/fs/ext4/xattr.c
++++ b/fs/ext4/xattr.c
+@@ -887,16 +887,17 @@ inserted:
+
+ new_bh = sb_getblk(sb, block);
+ if (!new_bh) {
++ error = -ENOMEM;
+ getblk_failed:
+ ext4_free_blocks(handle, inode, NULL, block, 1,
+ EXT4_FREE_BLOCKS_METADATA);
+- error = -EIO;
+ goto cleanup;
+ }
+ lock_buffer(new_bh);
+ error = ext4_journal_get_create_access(handle, new_bh);
+ if (error) {
+ unlock_buffer(new_bh);
++ error = -EIO;
+ goto getblk_failed;
+ }
+ memcpy(new_bh->b_data, s->base, new_bh->b_size);
media-v4l-reset-subdev-v4l2_dev-field-to-null-if-registration-fails.patch
media-omap_vout-find_vma-needs-mmap_sem-held.patch
media-rc-unlock-on-error-in-show_protocols.patch
+ext4-return-enomem-if-sb_getblk-fails.patch
+ext4-check-bh-in-ext4_read_block_bitmap.patch
+ext4-release-sysfs-kobject-when-failing-to-enable-quotas-on-mount.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