1 ext4: Properly update i_disksize.
3 From: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 With delayed allocation we use i_data_sem to update i_disksize. We need
6 to update i_disksize only if the new size specified is greater than the
7 current value and we need to make sure we don't race with other
8 i_disksize update. With delayed allocation we will switch to the
9 write_begin function for non-delayed allocation if we are low on free
10 blocks. This means the write_begin function for non-delayed allocation
11 also needs to use the same locking.
13 We also need to check and update i_disksize even if the new size is less
14 that inode.i_size because of delayed allocation.
16 Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
17 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
19 fs/ext4/ext4.h | 11 +++++++++++
20 fs/ext4/extents.c | 9 +++++----
21 fs/ext4/inode.c | 54 +++++++++++++++++++++++++++++------------------------
22 3 files changed, 46 insertions(+), 28 deletions(-)
24 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
25 index 79bdcd8..9fb7796 100644
28 @@ -1219,6 +1219,17 @@ do { \
29 #define EXT4_FREEBLOCKS_WATERMARK 0
32 +static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
35 + * XXX: replace with spinlock if seen contended -bzzz
37 + down_write(&EXT4_I(inode)->i_data_sem);
38 + if (newsize > EXT4_I(inode)->i_disksize)
39 + EXT4_I(inode)->i_disksize = newsize;
40 + up_write(&EXT4_I(inode)->i_data_sem);
45 * Inodes and files operations
46 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
47 index b24d3c5..cbf388a 100644
48 --- a/fs/ext4/extents.c
49 +++ b/fs/ext4/extents.c
50 @@ -2877,10 +2877,11 @@ static void ext4_falloc_update_inode(struct inode *inode,
51 * Update only when preallocation was requested beyond
54 - if (!(mode & FALLOC_FL_KEEP_SIZE) &&
55 - new_size > i_size_read(inode)) {
56 - i_size_write(inode, new_size);
57 - EXT4_I(inode)->i_disksize = new_size;
58 + if (!(mode & FALLOC_FL_KEEP_SIZE)) {
59 + if (new_size > i_size_read(inode))
60 + i_size_write(inode, new_size);
61 + if (new_size > EXT4_I(inode)->i_disksize)
62 + ext4_update_i_disksize(inode, new_size);
66 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
67 index e7c84e4..a06a4a8 100644
70 @@ -1434,16 +1434,18 @@ static int ext4_ordered_write_end(struct file *file,
71 ret = ext4_jbd2_file_inode(handle, inode);
75 - * generic_write_end() will run mark_inode_dirty() if i_size
76 - * changes. So let's piggyback the i_disksize mark_inode_dirty
81 new_i_size = pos + copied;
82 - if (new_i_size > EXT4_I(inode)->i_disksize)
83 - EXT4_I(inode)->i_disksize = new_i_size;
84 + if (new_i_size > EXT4_I(inode)->i_disksize) {
85 + ext4_update_i_disksize(inode, new_i_size);
86 + /* We need to mark inode dirty even if
87 + * new_i_size is less that inode->i_size
88 + * bu greater than i_disksize.(hint delalloc)
90 + ext4_mark_inode_dirty(handle, inode);
93 ret2 = generic_write_end(file, mapping, pos, len, copied,
96 @@ -1468,8 +1470,14 @@ static int ext4_writeback_write_end(struct file *file,
99 new_i_size = pos + copied;
100 - if (new_i_size > EXT4_I(inode)->i_disksize)
101 - EXT4_I(inode)->i_disksize = new_i_size;
102 + if (new_i_size > EXT4_I(inode)->i_disksize) {
103 + ext4_update_i_disksize(inode, new_i_size);
104 + /* We need to mark inode dirty even if
105 + * new_i_size is less that inode->i_size
106 + * bu greater than i_disksize.(hint delalloc)
108 + ext4_mark_inode_dirty(handle, inode);
111 ret2 = generic_write_end(file, mapping, pos, len, copied,
113 @@ -1494,6 +1502,7 @@ static int ext4_journalled_write_end(struct file *file,
119 from = pos & (PAGE_CACHE_SIZE - 1);
121 @@ -1508,11 +1517,12 @@ static int ext4_journalled_write_end(struct file *file,
122 to, &partial, write_end_fn);
124 SetPageUptodate(page);
125 - if (pos+copied > inode->i_size)
126 + new_i_size = pos + copied;
127 + if (new_i_size > inode->i_size)
128 i_size_write(inode, pos+copied);
129 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
130 - if (inode->i_size > EXT4_I(inode)->i_disksize) {
131 - EXT4_I(inode)->i_disksize = inode->i_size;
132 + if (new_i_size > EXT4_I(inode)->i_disksize) {
133 + ext4_update_i_disksize(inode, new_i_size);
134 ret2 = ext4_mark_inode_dirty(handle, inode);
137 @@ -2227,18 +2237,9 @@ static int ext4_da_get_block_write(struct inode *inode, sector_t iblock,
138 if (disksize > i_size_read(inode))
139 disksize = i_size_read(inode);
140 if (disksize > EXT4_I(inode)->i_disksize) {
142 - * XXX: replace with spinlock if seen contended -bzzz
144 - down_write(&EXT4_I(inode)->i_data_sem);
145 - if (disksize > EXT4_I(inode)->i_disksize)
146 - EXT4_I(inode)->i_disksize = disksize;
147 - up_write(&EXT4_I(inode)->i_data_sem);
149 - if (EXT4_I(inode)->i_disksize == disksize) {
150 - ret = ext4_mark_inode_dirty(handle, inode);
153 + ext4_update_i_disksize(inode, disksize);
154 + ret = ext4_mark_inode_dirty(handle, inode);
159 @@ -2654,6 +2655,11 @@ static int ext4_da_write_end(struct file *file,
160 EXT4_I(inode)->i_disksize = new_i_size;
162 up_write(&EXT4_I(inode)->i_data_sem);
163 + /* We need to mark inode dirty even if
164 + * new_i_size is less that inode->i_size
165 + * bu greater than i_disksize.(hint delalloc)
167 + ext4_mark_inode_dirty(handle, inode);
170 ret2 = generic_write_end(file, mapping, pos, len, copied,