aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-07-27 23:52:25 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-07-27 23:52:25 -0400
commit173f8654746c138a08f51a8a0db7747763a896a2 (patch)
tree08e0fcd13c49e09fa8a15d7f4fb46535f13e454e /fs/ext4
parentcea8f46c36c3f82860b038aa23a46e16757666ba (diff)
parent03179fe92318e7934c180d96f12eff2cb36ef7b6 (diff)
Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
Pull ext4 updates from Ted Ts'o: "The usual collection of bug fixes and optimizations. Perhaps of greatest note is a speed up for parallel, non-allocating DIO writes, since we no longer take the i_mutex lock in that case. For bug fixes, we fix an incorrect overhead calculation which caused slightly incorrect results for df(1) and statfs(2). We also fixed bugs in the metadata checksum feature." * tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: (23 commits) ext4: undo ext4_calc_metadata_amount if we fail to claim space ext4: don't let i_reserved_meta_blocks go negative ext4: fix hole punch failure when depth is greater than 0 ext4: remove unnecessary argument from __ext4_handle_dirty_metadata() ext4: weed out ext4_write_super ext4: remove unnecessary superblock dirtying ext4: convert last user of ext4_mark_super_dirty() to ext4_handle_dirty_super() ext4: remove useless marking of superblock dirty ext4: fix ext4 mismerge back in January ext4: remove dynamic array size in ext4_chksum() ext4: remove unused variable in ext4_update_super() ext4: make quota as first class supported feature ext4: don't take the i_mutex lock when doing DIO overwrites ext4: add a new nolock flag in ext4_map_blocks ext4: split ext4_file_write into buffered IO and direct IO ext4: remove an unused statement in ext4_mb_get_buddy_page_lock() ext4: fix out-of-date comments in extents.c ext4: use s_csum_seed instead of i_csum_seed for xattr block ext4: use proper csum calculation in ext4_rename ext4: fix overhead calculation used by ext4_statfs() ...
Diffstat (limited to 'fs/ext4')
-rw-r--r--fs/ext4/balloc.c3
-rw-r--r--fs/ext4/bitmap.c12
-rw-r--r--fs/ext4/ext4.h26
-rw-r--r--fs/ext4/ext4_jbd2.c8
-rw-r--r--fs/ext4/ext4_jbd2.h25
-rw-r--r--fs/ext4/extents.c51
-rw-r--r--fs/ext4/file.c121
-rw-r--r--fs/ext4/ialloc.c5
-rw-r--r--fs/ext4/inode.c126
-rw-r--r--fs/ext4/mballoc.c26
-rw-r--r--fs/ext4/namei.c15
-rw-r--r--fs/ext4/resize.c7
-rw-r--r--fs/ext4/super.c323
-rw-r--r--fs/ext4/xattr.c11
14 files changed, 563 insertions, 196 deletions
diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
index cee7812cc3cf..d23b31ca9d7a 100644
--- a/fs/ext4/balloc.c
+++ b/fs/ext4/balloc.c
@@ -609,7 +609,8 @@ ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
609 if (bitmap_bh == NULL) 609 if (bitmap_bh == NULL)
610 continue; 610 continue;
611 611
612 x = ext4_count_free(bitmap_bh, sb->s_blocksize); 612 x = ext4_count_free(bitmap_bh->b_data,
613 EXT4_BLOCKS_PER_GROUP(sb) / 8);
613 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n", 614 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
614 i, ext4_free_group_clusters(sb, gdp), x); 615 i, ext4_free_group_clusters(sb, gdp), x);
615 bitmap_count += x; 616 bitmap_count += x;
diff --git a/fs/ext4/bitmap.c b/fs/ext4/bitmap.c
index b319721da26a..a94b9c63ee5c 100644
--- a/fs/ext4/bitmap.c
+++ b/fs/ext4/bitmap.c
@@ -11,24 +11,18 @@
11#include <linux/jbd2.h> 11#include <linux/jbd2.h>
12#include "ext4.h" 12#include "ext4.h"
13 13
14#ifdef EXT4FS_DEBUG
15
16static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0}; 14static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
17 15
18unsigned int ext4_count_free(struct buffer_head *map, unsigned int numchars) 16unsigned int ext4_count_free(char *bitmap, unsigned int numchars)
19{ 17{
20 unsigned int i, sum = 0; 18 unsigned int i, sum = 0;
21 19
22 if (!map)
23 return 0;
24 for (i = 0; i < numchars; i++) 20 for (i = 0; i < numchars; i++)
25 sum += nibblemap[map->b_data[i] & 0xf] + 21 sum += nibblemap[bitmap[i] & 0xf] +
26 nibblemap[(map->b_data[i] >> 4) & 0xf]; 22 nibblemap[(bitmap[i] >> 4) & 0xf];
27 return sum; 23 return sum;
28} 24}
29 25
30#endif /* EXT4FS_DEBUG */
31
32int ext4_inode_bitmap_csum_verify(struct super_block *sb, ext4_group_t group, 26int ext4_inode_bitmap_csum_verify(struct super_block *sb, ext4_group_t group,
33 struct ext4_group_desc *gdp, 27 struct ext4_group_desc *gdp,
34 struct buffer_head *bh, int sz) 28 struct buffer_head *bh, int sz)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index cfc4e01b3c83..c3411d4ce2da 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -571,6 +571,8 @@ enum {
571#define EXT4_GET_BLOCKS_NO_NORMALIZE 0x0040 571#define EXT4_GET_BLOCKS_NO_NORMALIZE 0x0040
572 /* Request will not result in inode size update (user for fallocate) */ 572 /* Request will not result in inode size update (user for fallocate) */
573#define EXT4_GET_BLOCKS_KEEP_SIZE 0x0080 573#define EXT4_GET_BLOCKS_KEEP_SIZE 0x0080
574 /* Do not take i_data_sem locking in ext4_map_blocks */
575#define EXT4_GET_BLOCKS_NO_LOCK 0x0100
574 576
575/* 577/*
576 * Flags used by ext4_free_blocks 578 * Flags used by ext4_free_blocks
@@ -1161,8 +1163,7 @@ struct ext4_sb_info {
1161 unsigned long s_desc_per_block; /* Number of group descriptors per block */ 1163 unsigned long s_desc_per_block; /* Number of group descriptors per block */
1162 ext4_group_t s_groups_count; /* Number of groups in the fs */ 1164 ext4_group_t s_groups_count; /* Number of groups in the fs */
1163 ext4_group_t s_blockfile_groups;/* Groups acceptable for non-extent files */ 1165 ext4_group_t s_blockfile_groups;/* Groups acceptable for non-extent files */
1164 unsigned long s_overhead_last; /* Last calculated overhead */ 1166 unsigned long s_overhead; /* # of fs overhead clusters */
1165 unsigned long s_blocks_last; /* Last seen block count */
1166 unsigned int s_cluster_ratio; /* Number of blocks per cluster */ 1167 unsigned int s_cluster_ratio; /* Number of blocks per cluster */
1167 unsigned int s_cluster_bits; /* log2 of s_cluster_ratio */ 1168 unsigned int s_cluster_bits; /* log2 of s_cluster_ratio */
1168 loff_t s_bitmap_maxbytes; /* max bytes for bitmap files */ 1169 loff_t s_bitmap_maxbytes; /* max bytes for bitmap files */
@@ -1314,6 +1315,8 @@ static inline struct timespec ext4_current_time(struct inode *inode)
1314static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino) 1315static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
1315{ 1316{
1316 return ino == EXT4_ROOT_INO || 1317 return ino == EXT4_ROOT_INO ||
1318 ino == EXT4_USR_QUOTA_INO ||
1319 ino == EXT4_GRP_QUOTA_INO ||
1317 ino == EXT4_JOURNAL_INO || 1320 ino == EXT4_JOURNAL_INO ||
1318 ino == EXT4_RESIZE_INO || 1321 ino == EXT4_RESIZE_INO ||
1319 (ino >= EXT4_FIRST_INO(sb) && 1322 (ino >= EXT4_FIRST_INO(sb) &&
@@ -1496,7 +1499,8 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1496 EXT4_FEATURE_RO_COMPAT_BTREE_DIR |\ 1499 EXT4_FEATURE_RO_COMPAT_BTREE_DIR |\
1497 EXT4_FEATURE_RO_COMPAT_HUGE_FILE |\ 1500 EXT4_FEATURE_RO_COMPAT_HUGE_FILE |\
1498 EXT4_FEATURE_RO_COMPAT_BIGALLOC |\ 1501 EXT4_FEATURE_RO_COMPAT_BIGALLOC |\
1499 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) 1502 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
1503 EXT4_FEATURE_RO_COMPAT_QUOTA)
1500 1504
1501/* 1505/*
1502 * Default values for user and/or group using reserved blocks 1506 * Default values for user and/or group using reserved blocks
@@ -1663,10 +1667,12 @@ static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
1663{ 1667{
1664 struct { 1668 struct {
1665 struct shash_desc shash; 1669 struct shash_desc shash;
1666 char ctx[crypto_shash_descsize(sbi->s_chksum_driver)]; 1670 char ctx[4];
1667 } desc; 1671 } desc;
1668 int err; 1672 int err;
1669 1673
1674 BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver)!=sizeof(desc.ctx));
1675
1670 desc.shash.tfm = sbi->s_chksum_driver; 1676 desc.shash.tfm = sbi->s_chksum_driver;
1671 desc.shash.flags = 0; 1677 desc.shash.flags = 0;
1672 *(u32 *)desc.ctx = crc; 1678 *(u32 *)desc.ctx = crc;
@@ -1852,7 +1858,7 @@ struct mmpd_data {
1852# define NORET_AND noreturn, 1858# define NORET_AND noreturn,
1853 1859
1854/* bitmap.c */ 1860/* bitmap.c */
1855extern unsigned int ext4_count_free(struct buffer_head *, unsigned); 1861extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
1856void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group, 1862void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
1857 struct ext4_group_desc *gdp, 1863 struct ext4_group_desc *gdp,
1858 struct buffer_head *bh, int sz); 1864 struct buffer_head *bh, int sz);
@@ -2037,6 +2043,7 @@ extern int ext4_group_extend(struct super_block *sb,
2037extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count); 2043extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
2038 2044
2039/* super.c */ 2045/* super.c */
2046extern int ext4_calculate_overhead(struct super_block *sb);
2040extern int ext4_superblock_csum_verify(struct super_block *sb, 2047extern int ext4_superblock_csum_verify(struct super_block *sb,
2041 struct ext4_super_block *es); 2048 struct ext4_super_block *es);
2042extern void ext4_superblock_csum_set(struct super_block *sb, 2049extern void ext4_superblock_csum_set(struct super_block *sb,
@@ -2321,15 +2328,6 @@ static inline void ext4_unlock_group(struct super_block *sb,
2321 spin_unlock(ext4_group_lock_ptr(sb, group)); 2328 spin_unlock(ext4_group_lock_ptr(sb, group));
2322} 2329}
2323 2330
2324static inline void ext4_mark_super_dirty(struct super_block *sb)
2325{
2326 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2327
2328 ext4_superblock_csum_set(sb, es);
2329 if (EXT4_SB(sb)->s_journal == NULL)
2330 sb->s_dirt =1;
2331}
2332
2333/* 2331/*
2334 * Block validity checking 2332 * Block validity checking
2335 */ 2333 */
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 90f7c2e84db1..bfa65b49d424 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -138,8 +138,7 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
138} 138}
139 139
140int __ext4_handle_dirty_super(const char *where, unsigned int line, 140int __ext4_handle_dirty_super(const char *where, unsigned int line,
141 handle_t *handle, struct super_block *sb, 141 handle_t *handle, struct super_block *sb)
142 int now)
143{ 142{
144 struct buffer_head *bh = EXT4_SB(sb)->s_sbh; 143 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
145 int err = 0; 144 int err = 0;
@@ -151,11 +150,10 @@ int __ext4_handle_dirty_super(const char *where, unsigned int line,
151 if (err) 150 if (err)
152 ext4_journal_abort_handle(where, line, __func__, 151 ext4_journal_abort_handle(where, line, __func__,
153 bh, handle, err); 152 bh, handle, err);
154 } else if (now) { 153 } else {
155 ext4_superblock_csum_set(sb, 154 ext4_superblock_csum_set(sb,
156 (struct ext4_super_block *)bh->b_data); 155 (struct ext4_super_block *)bh->b_data);
157 mark_buffer_dirty(bh); 156 mark_buffer_dirty(bh);
158 } else 157 }
159 sb->s_dirt = 1;
160 return err; 158 return err;
161} 159}
diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index f440e8f1841f..56d258c18303 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -87,14 +87,20 @@
87#ifdef CONFIG_QUOTA 87#ifdef CONFIG_QUOTA
88/* Amount of blocks needed for quota update - we know that the structure was 88/* Amount of blocks needed for quota update - we know that the structure was
89 * allocated so we need to update only data block */ 89 * allocated so we need to update only data block */
90#define EXT4_QUOTA_TRANS_BLOCKS(sb) (test_opt(sb, QUOTA) ? 1 : 0) 90#define EXT4_QUOTA_TRANS_BLOCKS(sb) ((test_opt(sb, QUOTA) ||\
91 EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) ?\
92 1 : 0)
91/* Amount of blocks needed for quota insert/delete - we do some block writes 93/* Amount of blocks needed for quota insert/delete - we do some block writes
92 * but inode, sb and group updates are done only once */ 94 * but inode, sb and group updates are done only once */
93#define EXT4_QUOTA_INIT_BLOCKS(sb) (test_opt(sb, QUOTA) ? (DQUOT_INIT_ALLOC*\ 95#define EXT4_QUOTA_INIT_BLOCKS(sb) ((test_opt(sb, QUOTA) ||\
94 (EXT4_SINGLEDATA_TRANS_BLOCKS(sb)-3)+3+DQUOT_INIT_REWRITE) : 0) 96 EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) ?\
95 97 (DQUOT_INIT_ALLOC*(EXT4_SINGLEDATA_TRANS_BLOCKS(sb)-3)\
96#define EXT4_QUOTA_DEL_BLOCKS(sb) (test_opt(sb, QUOTA) ? (DQUOT_DEL_ALLOC*\ 98 +3+DQUOT_INIT_REWRITE) : 0)
97 (EXT4_SINGLEDATA_TRANS_BLOCKS(sb)-3)+3+DQUOT_DEL_REWRITE) : 0) 99
100#define EXT4_QUOTA_DEL_BLOCKS(sb) ((test_opt(sb, QUOTA) ||\
101 EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) ?\
102 (DQUOT_DEL_ALLOC*(EXT4_SINGLEDATA_TRANS_BLOCKS(sb)-3)\
103 +3+DQUOT_DEL_REWRITE) : 0)
98#else 104#else
99#define EXT4_QUOTA_TRANS_BLOCKS(sb) 0 105#define EXT4_QUOTA_TRANS_BLOCKS(sb) 0
100#define EXT4_QUOTA_INIT_BLOCKS(sb) 0 106#define EXT4_QUOTA_INIT_BLOCKS(sb) 0
@@ -213,8 +219,7 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
213 struct buffer_head *bh); 219 struct buffer_head *bh);
214 220
215int __ext4_handle_dirty_super(const char *where, unsigned int line, 221int __ext4_handle_dirty_super(const char *where, unsigned int line,
216 handle_t *handle, struct super_block *sb, 222 handle_t *handle, struct super_block *sb);
217 int now);
218 223
219#define ext4_journal_get_write_access(handle, bh) \ 224#define ext4_journal_get_write_access(handle, bh) \
220 __ext4_journal_get_write_access(__func__, __LINE__, (handle), (bh)) 225 __ext4_journal_get_write_access(__func__, __LINE__, (handle), (bh))
@@ -226,10 +231,8 @@ int __ext4_handle_dirty_super(const char *where, unsigned int line,
226#define ext4_handle_dirty_metadata(handle, inode, bh) \ 231#define ext4_handle_dirty_metadata(handle, inode, bh) \
227 __ext4_handle_dirty_metadata(__func__, __LINE__, (handle), (inode), \ 232 __ext4_handle_dirty_metadata(__func__, __LINE__, (handle), (inode), \
228 (bh)) 233 (bh))
229#define ext4_handle_dirty_super_now(handle, sb) \
230 __ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb), 1)
231#define ext4_handle_dirty_super(handle, sb) \ 234#define ext4_handle_dirty_super(handle, sb) \
232 __ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb), 0) 235 __ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb))
233 236
234handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks); 237handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks);
235int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle); 238int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle);
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 91341ec6e06a..cd0c7ed06772 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -1891,11 +1891,10 @@ has_space:
1891 nearex->ee_len = newext->ee_len; 1891 nearex->ee_len = newext->ee_len;
1892 1892
1893merge: 1893merge:
1894 /* try to merge extents to the right */ 1894 /* try to merge extents */
1895 if (!(flag & EXT4_GET_BLOCKS_PRE_IO)) 1895 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
1896 ext4_ext_try_to_merge(inode, path, nearex); 1896 ext4_ext_try_to_merge(inode, path, nearex);
1897 1897
1898 /* try to merge extents to the left */
1899 1898
1900 /* time to correct all indexes above */ 1899 /* time to correct all indexes above */
1901 err = ext4_ext_correct_indexes(handle, inode, path); 1900 err = ext4_ext_correct_indexes(handle, inode, path);
@@ -2570,10 +2569,10 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2570{ 2569{
2571 struct super_block *sb = inode->i_sb; 2570 struct super_block *sb = inode->i_sb;
2572 int depth = ext_depth(inode); 2571 int depth = ext_depth(inode);
2573 struct ext4_ext_path *path; 2572 struct ext4_ext_path *path = NULL;
2574 ext4_fsblk_t partial_cluster = 0; 2573 ext4_fsblk_t partial_cluster = 0;
2575 handle_t *handle; 2574 handle_t *handle;
2576 int i, err; 2575 int i = 0, err;
2577 2576
2578 ext_debug("truncate since %u to %u\n", start, end); 2577 ext_debug("truncate since %u to %u\n", start, end);
2579 2578
@@ -2606,8 +2605,12 @@ again:
2606 } 2605 }
2607 depth = ext_depth(inode); 2606 depth = ext_depth(inode);
2608 ex = path[depth].p_ext; 2607 ex = path[depth].p_ext;
2609 if (!ex) 2608 if (!ex) {
2609 ext4_ext_drop_refs(path);
2610 kfree(path);
2611 path = NULL;
2610 goto cont; 2612 goto cont;
2613 }
2611 2614
2612 ee_block = le32_to_cpu(ex->ee_block); 2615 ee_block = le32_to_cpu(ex->ee_block);
2613 2616
@@ -2637,8 +2640,6 @@ again:
2637 if (err < 0) 2640 if (err < 0)
2638 goto out; 2641 goto out;
2639 } 2642 }
2640 ext4_ext_drop_refs(path);
2641 kfree(path);
2642 } 2643 }
2643cont: 2644cont:
2644 2645
@@ -2647,19 +2648,27 @@ cont:
2647 * after i_size and walking into the tree depth-wise. 2648 * after i_size and walking into the tree depth-wise.
2648 */ 2649 */
2649 depth = ext_depth(inode); 2650 depth = ext_depth(inode);
2650 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2651 if (path) {
2651 if (path == NULL) { 2652 int k = i = depth;
2652 ext4_journal_stop(handle); 2653 while (--k > 0)
2653 return -ENOMEM; 2654 path[k].p_block =
2654 } 2655 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2655 path[0].p_depth = depth; 2656 } else {
2656 path[0].p_hdr = ext_inode_hdr(inode); 2657 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2658 GFP_NOFS);
2659 if (path == NULL) {
2660 ext4_journal_stop(handle);
2661 return -ENOMEM;
2662 }
2663 path[0].p_depth = depth;
2664 path[0].p_hdr = ext_inode_hdr(inode);
2657 2665
2658 if (ext4_ext_check(inode, path[0].p_hdr, depth)) { 2666 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2659 err = -EIO; 2667 err = -EIO;
2660 goto out; 2668 goto out;
2669 }
2661 } 2670 }
2662 i = err = 0; 2671 err = 0;
2663 2672
2664 while (i >= 0 && err == 0) { 2673 while (i >= 0 && err == 0) {
2665 if (i == depth) { 2674 if (i == depth) {
@@ -2773,8 +2782,10 @@ cont:
2773out: 2782out:
2774 ext4_ext_drop_refs(path); 2783 ext4_ext_drop_refs(path);
2775 kfree(path); 2784 kfree(path);
2776 if (err == -EAGAIN) 2785 if (err == -EAGAIN) {
2786 path = NULL;
2777 goto again; 2787 goto again;
2788 }
2778 ext4_journal_stop(handle); 2789 ext4_journal_stop(handle);
2779 2790
2780 return err; 2791 return err;
@@ -4420,6 +4431,8 @@ retry:
4420 ext4_falloc_update_inode(inode, mode, new_size, 4431 ext4_falloc_update_inode(inode, mode, new_size,
4421 (map.m_flags & EXT4_MAP_NEW)); 4432 (map.m_flags & EXT4_MAP_NEW));
4422 ext4_mark_inode_dirty(handle, inode); 4433 ext4_mark_inode_dirty(handle, inode);
4434 if ((file->f_flags & O_SYNC) && ret >= max_blocks)
4435 ext4_handle_sync(handle);
4423 ret2 = ext4_journal_stop(handle); 4436 ret2 = ext4_journal_stop(handle);
4424 if (ret2) 4437 if (ret2)
4425 break; 4438 break;
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 782eecb57e43..3b0e3bdaabfc 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -90,11 +90,91 @@ ext4_unaligned_aio(struct inode *inode, const struct iovec *iov,
90} 90}
91 91
92static ssize_t 92static ssize_t
93ext4_file_dio_write(struct kiocb *iocb, const struct iovec *iov,
94 unsigned long nr_segs, loff_t pos)
95{
96 struct file *file = iocb->ki_filp;
97 struct inode *inode = file->f_mapping->host;
98 struct blk_plug plug;
99 int unaligned_aio = 0;
100 ssize_t ret;
101 int overwrite = 0;
102 size_t length = iov_length(iov, nr_segs);
103
104 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
105 !is_sync_kiocb(iocb))
106 unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
107
108 /* Unaligned direct AIO must be serialized; see comment above */
109 if (unaligned_aio) {
110 static unsigned long unaligned_warn_time;
111
112 /* Warn about this once per day */
113 if (printk_timed_ratelimit(&unaligned_warn_time, 60*60*24*HZ))
114 ext4_msg(inode->i_sb, KERN_WARNING,
115 "Unaligned AIO/DIO on inode %ld by %s; "
116 "performance will be poor.",
117 inode->i_ino, current->comm);
118 mutex_lock(ext4_aio_mutex(inode));
119 ext4_aiodio_wait(inode);
120 }
121
122 BUG_ON(iocb->ki_pos != pos);
123
124 mutex_lock(&inode->i_mutex);
125 blk_start_plug(&plug);
126
127 iocb->private = &overwrite;
128
129 /* check whether we do a DIO overwrite or not */
130 if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
131 !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
132 struct ext4_map_blocks map;
133 unsigned int blkbits = inode->i_blkbits;
134 int err, len;
135
136 map.m_lblk = pos >> blkbits;
137 map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
138 - map.m_lblk;
139 len = map.m_len;
140
141 err = ext4_map_blocks(NULL, inode, &map, 0);
142 /*
143 * 'err==len' means that all of blocks has been preallocated no
144 * matter they are initialized or not. For excluding
145 * uninitialized extents, we need to check m_flags. There are
146 * two conditions that indicate for initialized extents.
147 * 1) If we hit extent cache, EXT4_MAP_MAPPED flag is returned;
148 * 2) If we do a real lookup, non-flags are returned.
149 * So we should check these two conditions.
150 */
151 if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
152 overwrite = 1;
153 }
154
155 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
156 mutex_unlock(&inode->i_mutex);
157
158 if (ret > 0 || ret == -EIOCBQUEUED) {
159 ssize_t err;
160
161 err = generic_write_sync(file, pos, ret);
162 if (err < 0 && ret > 0)
163 ret = err;
164 }
165 blk_finish_plug(&plug);
166
167 if (unaligned_aio)
168 mutex_unlock(ext4_aio_mutex(inode));
169
170 return ret;
171}
172
173static ssize_t
93ext4_file_write(struct kiocb *iocb, const struct iovec *iov, 174ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
94 unsigned long nr_segs, loff_t pos) 175 unsigned long nr_segs, loff_t pos)
95{ 176{
96 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; 177 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
97 int unaligned_aio = 0;
98 ssize_t ret; 178 ssize_t ret;
99 179
100 /* 180 /*
@@ -114,29 +194,12 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
114 nr_segs = iov_shorten((struct iovec *)iov, nr_segs, 194 nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
115 sbi->s_bitmap_maxbytes - pos); 195 sbi->s_bitmap_maxbytes - pos);
116 } 196 }
117 } else if (unlikely((iocb->ki_filp->f_flags & O_DIRECT) &&
118 !is_sync_kiocb(iocb))) {
119 unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
120 } 197 }
121 198
122 /* Unaligned direct AIO must be serialized; see comment above */ 199 if (unlikely(iocb->ki_filp->f_flags & O_DIRECT))
123 if (unaligned_aio) { 200 ret = ext4_file_dio_write(iocb, iov, nr_segs, pos);
124 static unsigned long unaligned_warn_time; 201 else
125 202 ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
126 /* Warn about this once per day */
127 if (printk_timed_ratelimit(&unaligned_warn_time, 60*60*24*HZ))
128 ext4_msg(inode->i_sb, KERN_WARNING,
129 "Unaligned AIO/DIO on inode %ld by %s; "
130 "performance will be poor.",
131 inode->i_ino, current->comm);
132 mutex_lock(ext4_aio_mutex(inode));
133 ext4_aiodio_wait(inode);
134 }
135
136 ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
137
138 if (unaligned_aio)
139 mutex_unlock(ext4_aio_mutex(inode));
140 203
141 return ret; 204 return ret;
142} 205}
@@ -181,9 +244,21 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
181 path.dentry = mnt->mnt_root; 244 path.dentry = mnt->mnt_root;
182 cp = d_path(&path, buf, sizeof(buf)); 245 cp = d_path(&path, buf, sizeof(buf));
183 if (!IS_ERR(cp)) { 246 if (!IS_ERR(cp)) {
247 handle_t *handle;
248 int err;
249
250 handle = ext4_journal_start_sb(sb, 1);
251 if (IS_ERR(handle))
252 return PTR_ERR(handle);
253 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
254 if (err) {
255 ext4_journal_stop(handle);
256 return err;
257 }
184 strlcpy(sbi->s_es->s_last_mounted, cp, 258 strlcpy(sbi->s_es->s_last_mounted, cp,
185 sizeof(sbi->s_es->s_last_mounted)); 259 sizeof(sbi->s_es->s_last_mounted));
186 ext4_mark_super_dirty(sb); 260 ext4_handle_dirty_super(handle, sb);
261 ext4_journal_stop(handle);
187 } 262 }
188 } 263 }
189 /* 264 /*
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index d48e8b14928c..26154b81b836 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -315,7 +315,6 @@ out:
315 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 315 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
316 if (!fatal) 316 if (!fatal)
317 fatal = err; 317 fatal = err;
318 ext4_mark_super_dirty(sb);
319 } else 318 } else
320 ext4_error(sb, "bit already cleared for inode %lu", ino); 319 ext4_error(sb, "bit already cleared for inode %lu", ino);
321 320
@@ -830,7 +829,6 @@ got:
830 percpu_counter_dec(&sbi->s_freeinodes_counter); 829 percpu_counter_dec(&sbi->s_freeinodes_counter);
831 if (S_ISDIR(mode)) 830 if (S_ISDIR(mode))
832 percpu_counter_inc(&sbi->s_dirs_counter); 831 percpu_counter_inc(&sbi->s_dirs_counter);
833 ext4_mark_super_dirty(sb);
834 832
835 if (sbi->s_log_groups_per_flex) { 833 if (sbi->s_log_groups_per_flex) {
836 flex_group = ext4_flex_group(sbi, group); 834 flex_group = ext4_flex_group(sbi, group);
@@ -1054,7 +1052,8 @@ unsigned long ext4_count_free_inodes(struct super_block *sb)
1054 if (!bitmap_bh) 1052 if (!bitmap_bh)
1055 continue; 1053 continue;
1056 1054
1057 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); 1055 x = ext4_count_free(bitmap_bh->b_data,
1056 EXT4_INODES_PER_GROUP(sb) / 8);
1058 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1057 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1059 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1058 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1060 bitmap_count += x; 1059 bitmap_count += x;
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 02bc8cbe7281..89b59cb7f9b8 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -346,6 +346,15 @@ void ext4_da_update_reserve_space(struct inode *inode,
346 used = ei->i_reserved_data_blocks; 346 used = ei->i_reserved_data_blocks;
347 } 347 }
348 348
349 if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
350 ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, allocated %d "
351 "with only %d reserved metadata blocks\n", __func__,
352 inode->i_ino, ei->i_allocated_meta_blocks,
353 ei->i_reserved_meta_blocks);
354 WARN_ON(1);
355 ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
356 }
357
349 /* Update per-inode reservations */ 358 /* Update per-inode reservations */
350 ei->i_reserved_data_blocks -= used; 359 ei->i_reserved_data_blocks -= used;
351 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 360 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
@@ -544,7 +553,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
544 * Try to see if we can get the block without requesting a new 553 * Try to see if we can get the block without requesting a new
545 * file system block. 554 * file system block.
546 */ 555 */
547 down_read((&EXT4_I(inode)->i_data_sem)); 556 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
557 down_read((&EXT4_I(inode)->i_data_sem));
548 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 558 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
549 retval = ext4_ext_map_blocks(handle, inode, map, flags & 559 retval = ext4_ext_map_blocks(handle, inode, map, flags &
550 EXT4_GET_BLOCKS_KEEP_SIZE); 560 EXT4_GET_BLOCKS_KEEP_SIZE);
@@ -552,7 +562,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
552 retval = ext4_ind_map_blocks(handle, inode, map, flags & 562 retval = ext4_ind_map_blocks(handle, inode, map, flags &
553 EXT4_GET_BLOCKS_KEEP_SIZE); 563 EXT4_GET_BLOCKS_KEEP_SIZE);
554 } 564 }
555 up_read((&EXT4_I(inode)->i_data_sem)); 565 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
566 up_read((&EXT4_I(inode)->i_data_sem));
556 567
557 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 568 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
558 int ret = check_block_validity(inode, map); 569 int ret = check_block_validity(inode, map);
@@ -1171,6 +1182,17 @@ static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
1171 struct ext4_inode_info *ei = EXT4_I(inode); 1182 struct ext4_inode_info *ei = EXT4_I(inode);
1172 unsigned int md_needed; 1183 unsigned int md_needed;
1173 int ret; 1184 int ret;
1185 ext4_lblk_t save_last_lblock;
1186 int save_len;
1187
1188 /*
1189 * We will charge metadata quota at writeout time; this saves
1190 * us from metadata over-estimation, though we may go over by
1191 * a small amount in the end. Here we just reserve for data.
1192 */
1193 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
1194 if (ret)
1195 return ret;
1174 1196
1175 /* 1197 /*
1176 * recalculate the amount of metadata blocks to reserve 1198 * recalculate the amount of metadata blocks to reserve
@@ -1179,32 +1201,31 @@ static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
1179 */ 1201 */
1180repeat: 1202repeat:
1181 spin_lock(&ei->i_block_reservation_lock); 1203 spin_lock(&ei->i_block_reservation_lock);
1204 /*
1205 * ext4_calc_metadata_amount() has side effects, which we have
1206 * to be prepared undo if we fail to claim space.
1207 */
1208 save_len = ei->i_da_metadata_calc_len;
1209 save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1182 md_needed = EXT4_NUM_B2C(sbi, 1210 md_needed = EXT4_NUM_B2C(sbi,
1183 ext4_calc_metadata_amount(inode, lblock)); 1211 ext4_calc_metadata_amount(inode, lblock));
1184 trace_ext4_da_reserve_space(inode, md_needed); 1212 trace_ext4_da_reserve_space(inode, md_needed);
1185 spin_unlock(&ei->i_block_reservation_lock);
1186 1213
1187 /* 1214 /*
1188 * We will charge metadata quota at writeout time; this saves
1189 * us from metadata over-estimation, though we may go over by
1190 * a small amount in the end. Here we just reserve for data.
1191 */
1192 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
1193 if (ret)
1194 return ret;
1195 /*
1196 * We do still charge estimated metadata to the sb though; 1215 * We do still charge estimated metadata to the sb though;
1197 * we cannot afford to run out of free blocks. 1216 * we cannot afford to run out of free blocks.
1198 */ 1217 */
1199 if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 1218 if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
1200 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1219 ei->i_da_metadata_calc_len = save_len;
1220 ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1221 spin_unlock(&ei->i_block_reservation_lock);
1201 if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1222 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1202 yield(); 1223 yield();
1203 goto repeat; 1224 goto repeat;
1204 } 1225 }
1226 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
1205 return -ENOSPC; 1227 return -ENOSPC;
1206 } 1228 }
1207 spin_lock(&ei->i_block_reservation_lock);
1208 ei->i_reserved_data_blocks++; 1229 ei->i_reserved_data_blocks++;
1209 ei->i_reserved_meta_blocks += md_needed; 1230 ei->i_reserved_meta_blocks += md_needed;
1210 spin_unlock(&ei->i_block_reservation_lock); 1231 spin_unlock(&ei->i_block_reservation_lock);
@@ -2818,6 +2839,32 @@ static int ext4_get_block_write(struct inode *inode, sector_t iblock,
2818 EXT4_GET_BLOCKS_IO_CREATE_EXT); 2839 EXT4_GET_BLOCKS_IO_CREATE_EXT);
2819} 2840}
2820 2841
2842static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
2843 struct buffer_head *bh_result, int flags)
2844{
2845 handle_t *handle = ext4_journal_current_handle();
2846 struct ext4_map_blocks map;
2847 int ret = 0;
2848
2849 ext4_debug("ext4_get_block_write_nolock: inode %lu, flag %d\n",
2850 inode->i_ino, flags);
2851
2852 flags = EXT4_GET_BLOCKS_NO_LOCK;
2853
2854 map.m_lblk = iblock;
2855 map.m_len = bh_result->b_size >> inode->i_blkbits;
2856
2857 ret = ext4_map_blocks(handle, inode, &map, flags);
2858 if (ret > 0) {
2859 map_bh(bh_result, inode->i_sb, map.m_pblk);
2860 bh_result->b_state = (bh_result->b_state & ~EXT4_MAP_FLAGS) |
2861 map.m_flags;
2862 bh_result->b_size = inode->i_sb->s_blocksize * map.m_len;
2863 ret = 0;
2864 }
2865 return ret;
2866}
2867
2821static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 2868static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
2822 ssize_t size, void *private, int ret, 2869 ssize_t size, void *private, int ret,
2823 bool is_async) 2870 bool is_async)
@@ -2966,6 +3013,18 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
2966 3013
2967 loff_t final_size = offset + count; 3014 loff_t final_size = offset + count;
2968 if (rw == WRITE && final_size <= inode->i_size) { 3015 if (rw == WRITE && final_size <= inode->i_size) {
3016 int overwrite = 0;
3017
3018 BUG_ON(iocb->private == NULL);
3019
3020 /* If we do a overwrite dio, i_mutex locking can be released */
3021 overwrite = *((int *)iocb->private);
3022
3023 if (overwrite) {
3024 down_read(&EXT4_I(inode)->i_data_sem);
3025 mutex_unlock(&inode->i_mutex);
3026 }
3027
2969 /* 3028 /*
2970 * We could direct write to holes and fallocate. 3029 * We could direct write to holes and fallocate.
2971 * 3030 *
@@ -2991,8 +3050,10 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
2991 if (!is_sync_kiocb(iocb)) { 3050 if (!is_sync_kiocb(iocb)) {
2992 ext4_io_end_t *io_end = 3051 ext4_io_end_t *io_end =
2993 ext4_init_io_end(inode, GFP_NOFS); 3052 ext4_init_io_end(inode, GFP_NOFS);
2994 if (!io_end) 3053 if (!io_end) {
2995 return -ENOMEM; 3054 ret = -ENOMEM;
3055 goto retake_lock;
3056 }
2996 io_end->flag |= EXT4_IO_END_DIRECT; 3057 io_end->flag |= EXT4_IO_END_DIRECT;
2997 iocb->private = io_end; 3058 iocb->private = io_end;
2998 /* 3059 /*
@@ -3005,13 +3066,22 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3005 EXT4_I(inode)->cur_aio_dio = iocb->private; 3066 EXT4_I(inode)->cur_aio_dio = iocb->private;
3006 } 3067 }
3007 3068
3008 ret = __blockdev_direct_IO(rw, iocb, inode, 3069 if (overwrite)
3009 inode->i_sb->s_bdev, iov, 3070 ret = __blockdev_direct_IO(rw, iocb, inode,
3010 offset, nr_segs, 3071 inode->i_sb->s_bdev, iov,
3011 ext4_get_block_write, 3072 offset, nr_segs,
3012 ext4_end_io_dio, 3073 ext4_get_block_write_nolock,
3013 NULL, 3074 ext4_end_io_dio,
3014 DIO_LOCKING); 3075 NULL,
3076 0);
3077 else
3078 ret = __blockdev_direct_IO(rw, iocb, inode,
3079 inode->i_sb->s_bdev, iov,
3080 offset, nr_segs,
3081 ext4_get_block_write,
3082 ext4_end_io_dio,
3083 NULL,
3084 DIO_LOCKING);
3015 if (iocb->private) 3085 if (iocb->private)
3016 EXT4_I(inode)->cur_aio_dio = NULL; 3086 EXT4_I(inode)->cur_aio_dio = NULL;
3017 /* 3087 /*
@@ -3031,7 +3101,7 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3031 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 3101 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3032 ext4_free_io_end(iocb->private); 3102 ext4_free_io_end(iocb->private);
3033 iocb->private = NULL; 3103 iocb->private = NULL;
3034 } else if (ret > 0 && ext4_test_inode_state(inode, 3104 } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
3035 EXT4_STATE_DIO_UNWRITTEN)) { 3105 EXT4_STATE_DIO_UNWRITTEN)) {
3036 int err; 3106 int err;
3037 /* 3107 /*
@@ -3044,6 +3114,14 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3044 ret = err; 3114 ret = err;
3045 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3115 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3046 } 3116 }
3117
3118 retake_lock:
3119 /* take i_mutex locking again if we do a ovewrite dio */
3120 if (overwrite) {
3121 up_read(&EXT4_I(inode)->i_data_sem);
3122 mutex_lock(&inode->i_mutex);
3123 }
3124
3047 return ret; 3125 return ret;
3048 } 3126 }
3049 3127
@@ -4034,7 +4112,7 @@ static int ext4_do_update_inode(handle_t *handle,
4034 EXT4_SET_RO_COMPAT_FEATURE(sb, 4112 EXT4_SET_RO_COMPAT_FEATURE(sb,
4035 EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 4113 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
4036 ext4_handle_sync(handle); 4114 ext4_handle_sync(handle);
4037 err = ext4_handle_dirty_super_now(handle, sb); 4115 err = ext4_handle_dirty_super(handle, sb);
4038 } 4116 }
4039 } 4117 }
4040 raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4118 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 1cd6994fc446..8eae94771c45 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -969,7 +969,6 @@ static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
969 969
970 block++; 970 block++;
971 pnum = block / blocks_per_page; 971 pnum = block / blocks_per_page;
972 poff = block % blocks_per_page;
973 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); 972 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
974 if (!page) 973 if (!page)
975 return -EIO; 974 return -EIO;
@@ -2077,8 +2076,9 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2077 struct super_block *sb = seq->private; 2076 struct super_block *sb = seq->private;
2078 ext4_group_t group = (ext4_group_t) ((unsigned long) v); 2077 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
2079 int i; 2078 int i;
2080 int err; 2079 int err, buddy_loaded = 0;
2081 struct ext4_buddy e4b; 2080 struct ext4_buddy e4b;
2081 struct ext4_group_info *grinfo;
2082 struct sg { 2082 struct sg {
2083 struct ext4_group_info info; 2083 struct ext4_group_info info;
2084 ext4_grpblk_t counters[16]; 2084 ext4_grpblk_t counters[16];
@@ -2095,15 +2095,21 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2095 2095
2096 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) + 2096 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2097 sizeof(struct ext4_group_info); 2097 sizeof(struct ext4_group_info);
2098 err = ext4_mb_load_buddy(sb, group, &e4b); 2098 grinfo = ext4_get_group_info(sb, group);
2099 if (err) { 2099 /* Load the group info in memory only if not already loaded. */
2100 seq_printf(seq, "#%-5u: I/O error\n", group); 2100 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2101 return 0; 2101 err = ext4_mb_load_buddy(sb, group, &e4b);
2102 if (err) {
2103 seq_printf(seq, "#%-5u: I/O error\n", group);
2104 return 0;
2105 }
2106 buddy_loaded = 1;
2102 } 2107 }
2103 ext4_lock_group(sb, group); 2108
2104 memcpy(&sg, ext4_get_group_info(sb, group), i); 2109 memcpy(&sg, ext4_get_group_info(sb, group), i);
2105 ext4_unlock_group(sb, group); 2110
2106 ext4_mb_unload_buddy(&e4b); 2111 if (buddy_loaded)
2112 ext4_mb_unload_buddy(&e4b);
2107 2113
2108 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free, 2114 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
2109 sg.info.bb_fragments, sg.info.bb_first_free); 2115 sg.info.bb_fragments, sg.info.bb_first_free);
@@ -2825,7 +2831,6 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
2825 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); 2831 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
2826 2832
2827out_err: 2833out_err:
2828 ext4_mark_super_dirty(sb);
2829 brelse(bitmap_bh); 2834 brelse(bitmap_bh);
2830 return err; 2835 return err;
2831} 2836}
@@ -4694,7 +4699,6 @@ do_more:
4694 put_bh(bitmap_bh); 4699 put_bh(bitmap_bh);
4695 goto do_more; 4700 goto do_more;
4696 } 4701 }
4697 ext4_mark_super_dirty(sb);
4698error_return: 4702error_return:
4699 brelse(bitmap_bh); 4703 brelse(bitmap_bh);
4700 ext4_std_error(sb, err); 4704 ext4_std_error(sb, err);
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index d0d3f0e87f99..2a42cc04466f 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2397,7 +2397,7 @@ int ext4_orphan_add(handle_t *handle, struct inode *inode)
2397 /* Insert this inode at the head of the on-disk orphan list... */ 2397 /* Insert this inode at the head of the on-disk orphan list... */
2398 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan); 2398 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2399 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); 2399 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2400 err = ext4_handle_dirty_super_now(handle, sb); 2400 err = ext4_handle_dirty_super(handle, sb);
2401 rc = ext4_mark_iloc_dirty(handle, inode, &iloc); 2401 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2402 if (!err) 2402 if (!err)
2403 err = rc; 2403 err = rc;
@@ -2470,7 +2470,7 @@ int ext4_orphan_del(handle_t *handle, struct inode *inode)
2470 if (err) 2470 if (err)
2471 goto out_brelse; 2471 goto out_brelse;
2472 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); 2472 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2473 err = ext4_handle_dirty_super_now(handle, inode->i_sb); 2473 err = ext4_handle_dirty_super(handle, inode->i_sb);
2474 } else { 2474 } else {
2475 struct ext4_iloc iloc2; 2475 struct ext4_iloc iloc2;
2476 struct inode *i_prev = 2476 struct inode *i_prev =
@@ -2918,8 +2918,15 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2918 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) = 2918 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
2919 cpu_to_le32(new_dir->i_ino); 2919 cpu_to_le32(new_dir->i_ino);
2920 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata"); 2920 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
2921 retval = ext4_handle_dirty_dirent_node(handle, old_inode, 2921 if (is_dx(old_inode)) {
2922 dir_bh); 2922 retval = ext4_handle_dirty_dx_node(handle,
2923 old_inode,
2924 dir_bh);
2925 } else {
2926 retval = ext4_handle_dirty_dirent_node(handle,
2927 old_inode,
2928 dir_bh);
2929 }
2923 if (retval) { 2930 if (retval) {
2924 ext4_std_error(old_dir->i_sb, retval); 2931 ext4_std_error(old_dir->i_sb, retval);
2925 goto end_rename; 2932 goto end_rename;
diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c
index 7ea6cbb44121..41f6ef68e2e1 100644
--- a/fs/ext4/resize.c
+++ b/fs/ext4/resize.c
@@ -798,7 +798,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
798 ext4_kvfree(o_group_desc); 798 ext4_kvfree(o_group_desc);
799 799
800 le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 800 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
801 err = ext4_handle_dirty_super_now(handle, sb); 801 err = ext4_handle_dirty_super(handle, sb);
802 if (err) 802 if (err)
803 ext4_std_error(sb, err); 803 ext4_std_error(sb, err);
804 804
@@ -1272,6 +1272,11 @@ static void ext4_update_super(struct super_block *sb,
1272 &sbi->s_flex_groups[flex_group].free_inodes); 1272 &sbi->s_flex_groups[flex_group].free_inodes);
1273 } 1273 }
1274 1274
1275 /*
1276 * Update the fs overhead information
1277 */
1278 ext4_calculate_overhead(sb);
1279
1275 if (test_opt(sb, DEBUG)) 1280 if (test_opt(sb, DEBUG))
1276 printk(KERN_DEBUG "EXT4-fs: added group %u:" 1281 printk(KERN_DEBUG "EXT4-fs: added group %u:"
1277 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count, 1282 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index d8759401ecae..2d51cd9af225 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -74,7 +74,6 @@ static const char *ext4_decode_error(struct super_block *sb, int errno,
74static int ext4_remount(struct super_block *sb, int *flags, char *data); 74static int ext4_remount(struct super_block *sb, int *flags, char *data);
75static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf); 75static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
76static int ext4_unfreeze(struct super_block *sb); 76static int ext4_unfreeze(struct super_block *sb);
77static void ext4_write_super(struct super_block *sb);
78static int ext4_freeze(struct super_block *sb); 77static int ext4_freeze(struct super_block *sb);
79static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags, 78static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
80 const char *dev_name, void *data); 79 const char *dev_name, void *data);
@@ -896,7 +895,7 @@ static void ext4_put_super(struct super_block *sb)
896 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); 895 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
897 es->s_state = cpu_to_le16(sbi->s_mount_state); 896 es->s_state = cpu_to_le16(sbi->s_mount_state);
898 } 897 }
899 if (sb->s_dirt || !(sb->s_flags & MS_RDONLY)) 898 if (!(sb->s_flags & MS_RDONLY))
900 ext4_commit_super(sb, 1); 899 ext4_commit_super(sb, 1);
901 900
902 if (sbi->s_proc) { 901 if (sbi->s_proc) {
@@ -1137,12 +1136,18 @@ static int ext4_mark_dquot_dirty(struct dquot *dquot);
1137static int ext4_write_info(struct super_block *sb, int type); 1136static int ext4_write_info(struct super_block *sb, int type);
1138static int ext4_quota_on(struct super_block *sb, int type, int format_id, 1137static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1139 struct path *path); 1138 struct path *path);
1139static int ext4_quota_on_sysfile(struct super_block *sb, int type,
1140 int format_id);
1140static int ext4_quota_off(struct super_block *sb, int type); 1141static int ext4_quota_off(struct super_block *sb, int type);
1142static int ext4_quota_off_sysfile(struct super_block *sb, int type);
1141static int ext4_quota_on_mount(struct super_block *sb, int type); 1143static int ext4_quota_on_mount(struct super_block *sb, int type);
1142static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 1144static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1143 size_t len, loff_t off); 1145 size_t len, loff_t off);
1144static ssize_t ext4_quota_write(struct super_block *sb, int type, 1146static ssize_t ext4_quota_write(struct super_block *sb, int type,
1145 const char *data, size_t len, loff_t off); 1147 const char *data, size_t len, loff_t off);
1148static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1149 unsigned int flags);
1150static int ext4_enable_quotas(struct super_block *sb);
1146 1151
1147static const struct dquot_operations ext4_quota_operations = { 1152static const struct dquot_operations ext4_quota_operations = {
1148 .get_reserved_space = ext4_get_reserved_space, 1153 .get_reserved_space = ext4_get_reserved_space,
@@ -1164,6 +1169,16 @@ static const struct quotactl_ops ext4_qctl_operations = {
1164 .get_dqblk = dquot_get_dqblk, 1169 .get_dqblk = dquot_get_dqblk,
1165 .set_dqblk = dquot_set_dqblk 1170 .set_dqblk = dquot_set_dqblk
1166}; 1171};
1172
1173static const struct quotactl_ops ext4_qctl_sysfile_operations = {
1174 .quota_on_meta = ext4_quota_on_sysfile,
1175 .quota_off = ext4_quota_off_sysfile,
1176 .quota_sync = dquot_quota_sync,
1177 .get_info = dquot_get_dqinfo,
1178 .set_info = dquot_set_dqinfo,
1179 .get_dqblk = dquot_get_dqblk,
1180 .set_dqblk = dquot_set_dqblk
1181};
1167#endif 1182#endif
1168 1183
1169static const struct super_operations ext4_sops = { 1184static const struct super_operations ext4_sops = {
@@ -1194,7 +1209,6 @@ static const struct super_operations ext4_nojournal_sops = {
1194 .dirty_inode = ext4_dirty_inode, 1209 .dirty_inode = ext4_dirty_inode,
1195 .drop_inode = ext4_drop_inode, 1210 .drop_inode = ext4_drop_inode,
1196 .evict_inode = ext4_evict_inode, 1211 .evict_inode = ext4_evict_inode,
1197 .write_super = ext4_write_super,
1198 .put_super = ext4_put_super, 1212 .put_super = ext4_put_super,
1199 .statfs = ext4_statfs, 1213 .statfs = ext4_statfs,
1200 .remount_fs = ext4_remount, 1214 .remount_fs = ext4_remount,
@@ -2661,6 +2675,16 @@ static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2661 "extents feature\n"); 2675 "extents feature\n");
2662 return 0; 2676 return 0;
2663 } 2677 }
2678
2679#ifndef CONFIG_QUOTA
2680 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
2681 !readonly) {
2682 ext4_msg(sb, KERN_ERR,
2683 "Filesystem with quota feature cannot be mounted RDWR "
2684 "without CONFIG_QUOTA");
2685 return 0;
2686 }
2687#endif /* CONFIG_QUOTA */
2664 return 1; 2688 return 1;
2665} 2689}
2666 2690
@@ -3085,6 +3109,114 @@ static int set_journal_csum_feature_set(struct super_block *sb)
3085 return ret; 3109 return ret;
3086} 3110}
3087 3111
3112/*
3113 * Note: calculating the overhead so we can be compatible with
3114 * historical BSD practice is quite difficult in the face of
3115 * clusters/bigalloc. This is because multiple metadata blocks from
3116 * different block group can end up in the same allocation cluster.
3117 * Calculating the exact overhead in the face of clustered allocation
3118 * requires either O(all block bitmaps) in memory or O(number of block
3119 * groups**2) in time. We will still calculate the superblock for
3120 * older file systems --- and if we come across with a bigalloc file
3121 * system with zero in s_overhead_clusters the estimate will be close to
3122 * correct especially for very large cluster sizes --- but for newer
3123 * file systems, it's better to calculate this figure once at mkfs
3124 * time, and store it in the superblock. If the superblock value is
3125 * present (even for non-bigalloc file systems), we will use it.
3126 */
3127static int count_overhead(struct super_block *sb, ext4_group_t grp,
3128 char *buf)
3129{
3130 struct ext4_sb_info *sbi = EXT4_SB(sb);
3131 struct ext4_group_desc *gdp;
3132 ext4_fsblk_t first_block, last_block, b;
3133 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3134 int s, j, count = 0;
3135
3136 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3137 (grp * EXT4_BLOCKS_PER_GROUP(sb));
3138 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3139 for (i = 0; i < ngroups; i++) {
3140 gdp = ext4_get_group_desc(sb, i, NULL);
3141 b = ext4_block_bitmap(sb, gdp);
3142 if (b >= first_block && b <= last_block) {
3143 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3144 count++;
3145 }
3146 b = ext4_inode_bitmap(sb, gdp);
3147 if (b >= first_block && b <= last_block) {
3148 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3149 count++;
3150 }
3151 b = ext4_inode_table(sb, gdp);
3152 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3153 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3154 int c = EXT4_B2C(sbi, b - first_block);
3155 ext4_set_bit(c, buf);
3156 count++;
3157 }
3158 if (i != grp)
3159 continue;
3160 s = 0;
3161 if (ext4_bg_has_super(sb, grp)) {
3162 ext4_set_bit(s++, buf);
3163 count++;
3164 }
3165 for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3166 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3167 count++;
3168 }
3169 }
3170 if (!count)
3171 return 0;
3172 return EXT4_CLUSTERS_PER_GROUP(sb) -
3173 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3174}
3175
3176/*
3177 * Compute the overhead and stash it in sbi->s_overhead
3178 */
3179int ext4_calculate_overhead(struct super_block *sb)
3180{
3181 struct ext4_sb_info *sbi = EXT4_SB(sb);
3182 struct ext4_super_block *es = sbi->s_es;
3183 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3184 ext4_fsblk_t overhead = 0;
3185 char *buf = (char *) get_zeroed_page(GFP_KERNEL);
3186
3187 memset(buf, 0, PAGE_SIZE);
3188 if (!buf)
3189 return -ENOMEM;
3190
3191 /*
3192 * Compute the overhead (FS structures). This is constant
3193 * for a given filesystem unless the number of block groups
3194 * changes so we cache the previous value until it does.
3195 */
3196
3197 /*
3198 * All of the blocks before first_data_block are overhead
3199 */
3200 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3201
3202 /*
3203 * Add the overhead found in each block group
3204 */
3205 for (i = 0; i < ngroups; i++) {
3206 int blks;
3207
3208 blks = count_overhead(sb, i, buf);
3209 overhead += blks;
3210 if (blks)
3211 memset(buf, 0, PAGE_SIZE);
3212 cond_resched();
3213 }
3214 sbi->s_overhead = overhead;
3215 smp_wmb();
3216 free_page((unsigned long) buf);
3217 return 0;
3218}
3219
3088static int ext4_fill_super(struct super_block *sb, void *data, int silent) 3220static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3089{ 3221{
3090 char *orig_data = kstrdup(data, GFP_KERNEL); 3222 char *orig_data = kstrdup(data, GFP_KERNEL);
@@ -3640,6 +3772,11 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3640#ifdef CONFIG_QUOTA 3772#ifdef CONFIG_QUOTA
3641 sb->s_qcop = &ext4_qctl_operations; 3773 sb->s_qcop = &ext4_qctl_operations;
3642 sb->dq_op = &ext4_quota_operations; 3774 sb->dq_op = &ext4_quota_operations;
3775
3776 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) {
3777 /* Use qctl operations for hidden quota files. */
3778 sb->s_qcop = &ext4_qctl_sysfile_operations;
3779 }
3643#endif 3780#endif
3644 memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid)); 3781 memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3645 3782
@@ -3735,6 +3872,18 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3735 3872
3736no_journal: 3873no_journal:
3737 /* 3874 /*
3875 * Get the # of file system overhead blocks from the
3876 * superblock if present.
3877 */
3878 if (es->s_overhead_clusters)
3879 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
3880 else {
3881 ret = ext4_calculate_overhead(sb);
3882 if (ret)
3883 goto failed_mount_wq;
3884 }
3885
3886 /*
3738 * The maximum number of concurrent works can be high and 3887 * The maximum number of concurrent works can be high and
3739 * concurrency isn't really necessary. Limit it to 1. 3888 * concurrency isn't really necessary. Limit it to 1.
3740 */ 3889 */
@@ -3840,6 +3989,16 @@ no_journal:
3840 } else 3989 } else
3841 descr = "out journal"; 3990 descr = "out journal";
3842 3991
3992#ifdef CONFIG_QUOTA
3993 /* Enable quota usage during mount. */
3994 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
3995 !(sb->s_flags & MS_RDONLY)) {
3996 ret = ext4_enable_quotas(sb);
3997 if (ret)
3998 goto failed_mount7;
3999 }
4000#endif /* CONFIG_QUOTA */
4001
3843 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. " 4002 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
3844 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts, 4003 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
3845 *sbi->s_es->s_mount_opts ? "; " : "", orig_data); 4004 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
@@ -4203,7 +4362,6 @@ static int ext4_commit_super(struct super_block *sb, int sync)
4203 es->s_free_inodes_count = 4362 es->s_free_inodes_count =
4204 cpu_to_le32(percpu_counter_sum_positive( 4363 cpu_to_le32(percpu_counter_sum_positive(
4205 &EXT4_SB(sb)->s_freeinodes_counter)); 4364 &EXT4_SB(sb)->s_freeinodes_counter));
4206 sb->s_dirt = 0;
4207 BUFFER_TRACE(sbh, "marking dirty"); 4365 BUFFER_TRACE(sbh, "marking dirty");
4208 ext4_superblock_csum_set(sb, es); 4366 ext4_superblock_csum_set(sb, es);
4209 mark_buffer_dirty(sbh); 4367 mark_buffer_dirty(sbh);
@@ -4310,13 +4468,6 @@ int ext4_force_commit(struct super_block *sb)
4310 return ret; 4468 return ret;
4311} 4469}
4312 4470
4313static void ext4_write_super(struct super_block *sb)
4314{
4315 lock_super(sb);
4316 ext4_commit_super(sb, 1);
4317 unlock_super(sb);
4318}
4319
4320static int ext4_sync_fs(struct super_block *sb, int wait) 4471static int ext4_sync_fs(struct super_block *sb, int wait)
4321{ 4472{
4322 int ret = 0; 4473 int ret = 0;
@@ -4567,16 +4718,26 @@ static int ext4_remount(struct super_block *sb, int *flags, char *data)
4567 if (sbi->s_journal == NULL) 4718 if (sbi->s_journal == NULL)
4568 ext4_commit_super(sb, 1); 4719 ext4_commit_super(sb, 1);
4569 4720
4721 unlock_super(sb);
4570#ifdef CONFIG_QUOTA 4722#ifdef CONFIG_QUOTA
4571 /* Release old quota file names */ 4723 /* Release old quota file names */
4572 for (i = 0; i < MAXQUOTAS; i++) 4724 for (i = 0; i < MAXQUOTAS; i++)
4573 if (old_opts.s_qf_names[i] && 4725 if (old_opts.s_qf_names[i] &&
4574 old_opts.s_qf_names[i] != sbi->s_qf_names[i]) 4726 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4575 kfree(old_opts.s_qf_names[i]); 4727 kfree(old_opts.s_qf_names[i]);
4728 if (enable_quota) {
4729 if (sb_any_quota_suspended(sb))
4730 dquot_resume(sb, -1);
4731 else if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4732 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
4733 err = ext4_enable_quotas(sb);
4734 if (err) {
4735 lock_super(sb);
4736 goto restore_opts;
4737 }
4738 }
4739 }
4576#endif 4740#endif
4577 unlock_super(sb);
4578 if (enable_quota)
4579 dquot_resume(sb, -1);
4580 4741
4581 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data); 4742 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4582 kfree(orig_data); 4743 kfree(orig_data);
@@ -4605,67 +4766,21 @@ restore_opts:
4605 return err; 4766 return err;
4606} 4767}
4607 4768
4608/*
4609 * Note: calculating the overhead so we can be compatible with
4610 * historical BSD practice is quite difficult in the face of
4611 * clusters/bigalloc. This is because multiple metadata blocks from
4612 * different block group can end up in the same allocation cluster.
4613 * Calculating the exact overhead in the face of clustered allocation
4614 * requires either O(all block bitmaps) in memory or O(number of block
4615 * groups**2) in time. We will still calculate the superblock for
4616 * older file systems --- and if we come across with a bigalloc file
4617 * system with zero in s_overhead_clusters the estimate will be close to
4618 * correct especially for very large cluster sizes --- but for newer
4619 * file systems, it's better to calculate this figure once at mkfs
4620 * time, and store it in the superblock. If the superblock value is
4621 * present (even for non-bigalloc file systems), we will use it.
4622 */
4623static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) 4769static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
4624{ 4770{
4625 struct super_block *sb = dentry->d_sb; 4771 struct super_block *sb = dentry->d_sb;
4626 struct ext4_sb_info *sbi = EXT4_SB(sb); 4772 struct ext4_sb_info *sbi = EXT4_SB(sb);
4627 struct ext4_super_block *es = sbi->s_es; 4773 struct ext4_super_block *es = sbi->s_es;
4628 struct ext4_group_desc *gdp; 4774 ext4_fsblk_t overhead = 0;
4629 u64 fsid; 4775 u64 fsid;
4630 s64 bfree; 4776 s64 bfree;
4631 4777
4632 if (test_opt(sb, MINIX_DF)) { 4778 if (!test_opt(sb, MINIX_DF))
4633 sbi->s_overhead_last = 0; 4779 overhead = sbi->s_overhead;
4634 } else if (es->s_overhead_clusters) {
4635 sbi->s_overhead_last = le32_to_cpu(es->s_overhead_clusters);
4636 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
4637 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4638 ext4_fsblk_t overhead = 0;
4639
4640 /*
4641 * Compute the overhead (FS structures). This is constant
4642 * for a given filesystem unless the number of block groups
4643 * changes so we cache the previous value until it does.
4644 */
4645
4646 /*
4647 * All of the blocks before first_data_block are
4648 * overhead
4649 */
4650 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
4651
4652 /*
4653 * Add the overhead found in each block group
4654 */
4655 for (i = 0; i < ngroups; i++) {
4656 gdp = ext4_get_group_desc(sb, i, NULL);
4657 overhead += ext4_num_overhead_clusters(sb, i, gdp);
4658 cond_resched();
4659 }
4660 sbi->s_overhead_last = overhead;
4661 smp_wmb();
4662 sbi->s_blocks_last = ext4_blocks_count(es);
4663 }
4664 4780
4665 buf->f_type = EXT4_SUPER_MAGIC; 4781 buf->f_type = EXT4_SUPER_MAGIC;
4666 buf->f_bsize = sb->s_blocksize; 4782 buf->f_bsize = sb->s_blocksize;
4667 buf->f_blocks = (ext4_blocks_count(es) - 4783 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, sbi->s_overhead);
4668 EXT4_C2B(sbi, sbi->s_overhead_last));
4669 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) - 4784 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
4670 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter); 4785 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
4671 /* prevent underflow in case that few free space is available */ 4786 /* prevent underflow in case that few free space is available */
@@ -4835,6 +4950,74 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
4835 return dquot_quota_on(sb, type, format_id, path); 4950 return dquot_quota_on(sb, type, format_id, path);
4836} 4951}
4837 4952
4953static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
4954 unsigned int flags)
4955{
4956 int err;
4957 struct inode *qf_inode;
4958 unsigned long qf_inums[MAXQUOTAS] = {
4959 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
4960 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
4961 };
4962
4963 BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA));
4964
4965 if (!qf_inums[type])
4966 return -EPERM;
4967
4968 qf_inode = ext4_iget(sb, qf_inums[type]);
4969 if (IS_ERR(qf_inode)) {
4970 ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
4971 return PTR_ERR(qf_inode);
4972 }
4973
4974 err = dquot_enable(qf_inode, type, format_id, flags);
4975 iput(qf_inode);
4976
4977 return err;
4978}
4979
4980/* Enable usage tracking for all quota types. */
4981static int ext4_enable_quotas(struct super_block *sb)
4982{
4983 int type, err = 0;
4984 unsigned long qf_inums[MAXQUOTAS] = {
4985 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
4986 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
4987 };
4988
4989 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
4990 for (type = 0; type < MAXQUOTAS; type++) {
4991 if (qf_inums[type]) {
4992 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
4993 DQUOT_USAGE_ENABLED);
4994 if (err) {
4995 ext4_warning(sb,
4996 "Failed to enable quota (type=%d) "
4997 "tracking. Please run e2fsck to fix.",
4998 type);
4999 return err;
5000 }
5001 }
5002 }
5003 return 0;
5004}
5005
5006/*
5007 * quota_on function that is used when QUOTA feature is set.
5008 */
5009static int ext4_quota_on_sysfile(struct super_block *sb, int type,
5010 int format_id)
5011{
5012 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
5013 return -EINVAL;
5014
5015 /*
5016 * USAGE was enabled at mount time. Only need to enable LIMITS now.
5017 */
5018 return ext4_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
5019}
5020
4838static int ext4_quota_off(struct super_block *sb, int type) 5021static int ext4_quota_off(struct super_block *sb, int type)
4839{ 5022{
4840 struct inode *inode = sb_dqopt(sb)->files[type]; 5023 struct inode *inode = sb_dqopt(sb)->files[type];
@@ -4861,6 +5044,18 @@ out:
4861 return dquot_quota_off(sb, type); 5044 return dquot_quota_off(sb, type);
4862} 5045}
4863 5046
5047/*
5048 * quota_off function that is used when QUOTA feature is set.
5049 */
5050static int ext4_quota_off_sysfile(struct super_block *sb, int type)
5051{
5052 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
5053 return -EINVAL;
5054
5055 /* Disable only the limits. */
5056 return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
5057}
5058
4864/* Read data from quotafile - avoid pagecache and such because we cannot afford 5059/* Read data from quotafile - avoid pagecache and such because we cannot afford
4865 * acquiring the locks... As quota files are never truncated and quota code 5060 * acquiring the locks... As quota files are never truncated and quota code
4866 * itself serializes the operations (and no one else should touch the files) 5061 * itself serializes the operations (and no one else should touch the files)
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index e56c9ed7d6e3..2cdb98d62980 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -127,19 +127,16 @@ static __le32 ext4_xattr_block_csum(struct inode *inode,
127 struct ext4_xattr_header *hdr) 127 struct ext4_xattr_header *hdr)
128{ 128{
129 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 129 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
130 struct ext4_inode_info *ei = EXT4_I(inode);
131 __u32 csum, old; 130 __u32 csum, old;
132 131
133 old = hdr->h_checksum; 132 old = hdr->h_checksum;
134 hdr->h_checksum = 0; 133 hdr->h_checksum = 0;
135 if (le32_to_cpu(hdr->h_refcount) != 1) { 134 block_nr = cpu_to_le64(block_nr);
136 block_nr = cpu_to_le64(block_nr); 135 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&block_nr,
137 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&block_nr, 136 sizeof(block_nr));
138 sizeof(block_nr));
139 } else
140 csum = ei->i_csum_seed;
141 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, 137 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
142 EXT4_BLOCK_SIZE(inode->i_sb)); 138 EXT4_BLOCK_SIZE(inode->i_sb));
139
143 hdr->h_checksum = old; 140 hdr->h_checksum = old;
144 return cpu_to_le32(csum); 141 return cpu_to_le32(csum);
145} 142}