Fix ext3-4-migrate.patch and mballoc-core.patches
[ext4-patch-queue.git] / make-ext4_get_blocks_wrap-take-the-truncate_mutex_early.patch
blobcad900fc3488ce58ad749a5da0b50bdecee70f26
1 ext4: Make ext4_get_blocks_wrap take the truncate_mutex early.
3 From: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 When doing a migrate from ext3 to ext4 inode we need to make sure the test
6 for inode type and walking inode data happens inside lock. To make this
7 happen move truncate_mutex early before checking the i_flags.
10 This actually should enable us to remove the verify_chain().
12 Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
13 ---
15 fs/ext4/extents.c | 9 ++++--
16 fs/ext4/inode.c | 69 +++++-------------------------------------------
17 include/linux/ext4_fs.h | 2 +
18 3 files changed, 16 insertions(+), 64 deletions(-)
21 Index: linux-2.6.24-rc7/fs/ext4/extents.c
22 ===================================================================
23 --- linux-2.6.24-rc7.orig/fs/ext4/extents.c 2008-01-16 13:49:50.000000000 -0800
24 +++ linux-2.6.24-rc7/fs/ext4/extents.c 2008-01-16 13:50:04.000000000 -0800
25 @@ -2129,6 +2129,10 @@ out:
26 return err ? err : allocated;
29 +/*
30 + * Need to be called with
31 + * mutex_lock(&EXT4_I(inode)->truncate_mutex);
32 + */
33 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
34 ext4_lblk_t iblock,
35 unsigned long max_blocks, struct buffer_head *bh_result,
36 @@ -2144,7 +2148,6 @@ int ext4_ext_get_blocks(handle_t *handle
37 __clear_bit(BH_New, &bh_result->b_state);
38 ext_debug("blocks %u/%lu requested for inode %u\n",
39 iblock, max_blocks, inode->i_ino);
40 - mutex_lock(&EXT4_I(inode)->truncate_mutex);
42 /* check in cache */
43 goal = ext4_ext_in_cache(inode, iblock, &newex);
44 @@ -2318,8 +2321,6 @@ out2:
45 ext4_ext_drop_refs(path);
46 kfree(path);
48 - mutex_unlock(&EXT4_I(inode)->truncate_mutex);
50 return err ? err : allocated;
53 @@ -2449,6 +2450,7 @@ long ext4_fallocate(struct inode *inode,
54 * modify 1 super block, 1 block bitmap and 1 group descriptor.
56 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
57 + mutex_lock(&EXT4_I(inode)->truncate_mutex)
58 retry:
59 while (ret >= 0 && ret < max_blocks) {
60 block = block + ret;
61 @@ -2505,6 +2507,7 @@ retry:
62 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
63 goto retry;
65 + mutex_unlock(&EXT4_I(inode)->truncate_mutex)
67 * Time to update the file size.
68 * Update only when preallocation was requested beyond the file size.
69 Index: linux-2.6.24-rc7/fs/ext4/inode.c
70 ===================================================================
71 --- linux-2.6.24-rc7.orig/fs/ext4/inode.c 2008-01-16 13:49:03.000000000 -0800
72 +++ linux-2.6.24-rc7/fs/ext4/inode.c 2008-01-16 13:50:04.000000000 -0800
73 @@ -243,13 +243,6 @@ static inline void add_chain(Indirect *p
74 p->bh = bh;
77 -static int verify_chain(Indirect *from, Indirect *to)
79 - while (from <= to && from->key == *from->p)
80 - from++;
81 - return (from > to);
84 /**
85 * ext4_block_to_path - parse the block number into array of offsets
86 * @inode: inode in question (we are only interested in its superblock)
87 @@ -348,10 +341,11 @@ static int ext4_block_to_path(struct ino
88 * (pointer to last triple returned, *@err == 0)
89 * or when it gets an IO error reading an indirect block
90 * (ditto, *@err == -EIO)
91 - * or when it notices that chain had been changed while it was reading
92 - * (ditto, *@err == -EAGAIN)
93 * or when it reads all @depth-1 indirect blocks successfully and finds
94 * the whole chain, all way to the data (returns %NULL, *err == 0).
95 + *
96 + * Need to be called with
97 + * mutex_lock(&EXT4_I(inode)->truncate_mutex)
99 static Indirect *ext4_get_branch(struct inode *inode, int depth,
100 ext4_lblk_t *offsets,
101 @@ -370,9 +364,6 @@ static Indirect *ext4_get_branch(struct
102 bh = sb_bread(sb, le32_to_cpu(p->key));
103 if (!bh)
104 goto failure;
105 - /* Reader: pointers */
106 - if (!verify_chain(chain, p))
107 - goto changed;
108 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
109 /* Reader: end */
110 if (!p->key)
111 @@ -380,10 +371,6 @@ static Indirect *ext4_get_branch(struct
113 return NULL;
115 -changed:
116 - brelse(bh);
117 - *err = -EAGAIN;
118 - goto no_block;
119 failure:
120 *err = -EIO;
121 no_block:
122 @@ -787,6 +774,10 @@ err_out:
123 * return > 0, # of blocks mapped or allocated.
124 * return = 0, if plain lookup failed.
125 * return < 0, error case.
128 + * Need to be called with
129 + * mutex_lock(&EXT4_I(inode)->truncate_mutex)
131 int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
132 ext4_lblk_t iblock, unsigned long maxblocks,
133 @@ -825,18 +816,6 @@ int ext4_get_blocks_handle(handle_t *han
134 while (count < maxblocks && count <= blocks_to_boundary) {
135 ext4_fsblk_t blk;
137 - if (!verify_chain(chain, partial)) {
138 - /*
139 - * Indirect block might be removed by
140 - * truncate while we were reading it.
141 - * Handling of that case: forget what we've
142 - * got now. Flag the err as EAGAIN, so it
143 - * will reread.
144 - */
145 - err = -EAGAIN;
146 - count = 0;
147 - break;
149 blk = le32_to_cpu(*(chain[depth-1].p + count));
151 if (blk == first_block + count)
152 @@ -844,44 +823,13 @@ int ext4_get_blocks_handle(handle_t *han
153 else
154 break;
156 - if (err != -EAGAIN)
157 - goto got_it;
158 + goto got_it;
161 /* Next simple case - plain lookup or failed read of indirect block */
162 if (!create || err == -EIO)
163 goto cleanup;
165 - mutex_lock(&ei->truncate_mutex);
167 - /*
168 - * If the indirect block is missing while we are reading
169 - * the chain(ext4_get_branch() returns -EAGAIN err), or
170 - * if the chain has been changed after we grab the semaphore,
171 - * (either because another process truncated this branch, or
172 - * another get_block allocated this branch) re-grab the chain to see if
173 - * the request block has been allocated or not.
175 - * Since we already block the truncate/other get_block
176 - * at this point, we will have the current copy of the chain when we
177 - * splice the branch into the tree.
178 - */
179 - if (err == -EAGAIN || !verify_chain(chain, partial)) {
180 - while (partial > chain) {
181 - brelse(partial->bh);
182 - partial--;
184 - partial = ext4_get_branch(inode, depth, offsets, chain, &err);
185 - if (!partial) {
186 - count++;
187 - mutex_unlock(&ei->truncate_mutex);
188 - if (err)
189 - goto cleanup;
190 - clear_buffer_new(bh_result);
191 - goto got_it;
196 * Okay, we need to do block allocation. Lazily initialize the block
197 * allocation info here if necessary
198 @@ -923,7 +871,6 @@ int ext4_get_blocks_handle(handle_t *han
200 if (!err && extend_disksize && inode->i_size > ei->i_disksize)
201 ei->i_disksize = inode->i_size;
202 - mutex_unlock(&ei->truncate_mutex);
203 if (err)
204 goto cleanup;
206 Index: linux-2.6.24-rc7/include/linux/ext4_fs.h
207 ===================================================================
208 --- linux-2.6.24-rc7.orig/include/linux/ext4_fs.h 2008-01-16 13:49:41.000000000 -0800
209 +++ linux-2.6.24-rc7/include/linux/ext4_fs.h 2008-01-16 13:50:04.000000000 -0800
210 @@ -1113,6 +1113,7 @@ ext4_get_blocks_wrap(handle_t *handle, s
211 int create, int extend_disksize)
213 int retval;
214 + mutex_lock(&EXT4_I(inode)->truncate_mutex);
215 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
216 retval = ext4_ext_get_blocks(handle, inode,
217 (ext4_lblk_t)block, max_blocks,
218 @@ -1122,6 +1123,7 @@ ext4_get_blocks_wrap(handle_t *handle, s
219 (ext4_lblk_t)block, max_blocks,
220 bh, create, extend_disksize);
222 + mutex_unlock(&EXT4_I(inode)->truncate_mutex);
223 return retval;