Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/inode.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | |
5 | * Remy Card (card@masi.ibp.fr) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | * | |
9 | * from | |
10 | * | |
11 | * linux/fs/minix/inode.c | |
12 | * | |
13 | * Copyright (C) 1991, 1992 Linus Torvalds | |
14 | * | |
15 | * Goal-directed block allocation by Stephen Tweedie | |
16 | * (sct@redhat.com), 1993, 1998 | |
17 | * Big-endian to little-endian byte-swapping/bitmaps by | |
18 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
19 | * 64-bit file support on 64-bit platforms by Jakub Jelinek | |
20 | * (jj@sunsite.ms.mff.cuni.cz) | |
21 | * | |
617ba13b | 22 | * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 |
ac27a0ec DK |
23 | */ |
24 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/fs.h> | |
27 | #include <linux/time.h> | |
dab291af | 28 | #include <linux/jbd2.h> |
ac27a0ec DK |
29 | #include <linux/highuid.h> |
30 | #include <linux/pagemap.h> | |
31 | #include <linux/quotaops.h> | |
32 | #include <linux/string.h> | |
33 | #include <linux/buffer_head.h> | |
34 | #include <linux/writeback.h> | |
64769240 | 35 | #include <linux/pagevec.h> |
ac27a0ec | 36 | #include <linux/mpage.h> |
e83c1397 | 37 | #include <linux/namei.h> |
ac27a0ec DK |
38 | #include <linux/uio.h> |
39 | #include <linux/bio.h> | |
3dcf5451 | 40 | #include "ext4_jbd2.h" |
ac27a0ec DK |
41 | #include "xattr.h" |
42 | #include "acl.h" | |
d2a17637 | 43 | #include "ext4_extents.h" |
ac27a0ec | 44 | |
a1d6cc56 AK |
45 | #define MPAGE_DA_EXTENT_TAIL 0x01 |
46 | ||
678aaf48 JK |
47 | static inline int ext4_begin_ordered_truncate(struct inode *inode, |
48 | loff_t new_size) | |
49 | { | |
50 | return jbd2_journal_begin_ordered_truncate(&EXT4_I(inode)->jinode, | |
51 | new_size); | |
52 | } | |
53 | ||
64769240 AT |
54 | static void ext4_invalidatepage(struct page *page, unsigned long offset); |
55 | ||
ac27a0ec DK |
56 | /* |
57 | * Test whether an inode is a fast symlink. | |
58 | */ | |
617ba13b | 59 | static int ext4_inode_is_fast_symlink(struct inode *inode) |
ac27a0ec | 60 | { |
617ba13b | 61 | int ea_blocks = EXT4_I(inode)->i_file_acl ? |
ac27a0ec DK |
62 | (inode->i_sb->s_blocksize >> 9) : 0; |
63 | ||
64 | return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); | |
65 | } | |
66 | ||
67 | /* | |
617ba13b | 68 | * The ext4 forget function must perform a revoke if we are freeing data |
ac27a0ec DK |
69 | * which has been journaled. Metadata (eg. indirect blocks) must be |
70 | * revoked in all cases. | |
71 | * | |
72 | * "bh" may be NULL: a metadata block may have been freed from memory | |
73 | * but there may still be a record of it in the journal, and that record | |
74 | * still needs to be revoked. | |
75 | */ | |
617ba13b MC |
76 | int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, |
77 | struct buffer_head *bh, ext4_fsblk_t blocknr) | |
ac27a0ec DK |
78 | { |
79 | int err; | |
80 | ||
81 | might_sleep(); | |
82 | ||
83 | BUFFER_TRACE(bh, "enter"); | |
84 | ||
85 | jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " | |
86 | "data mode %lx\n", | |
87 | bh, is_metadata, inode->i_mode, | |
88 | test_opt(inode->i_sb, DATA_FLAGS)); | |
89 | ||
90 | /* Never use the revoke function if we are doing full data | |
91 | * journaling: there is no need to, and a V1 superblock won't | |
92 | * support it. Otherwise, only skip the revoke on un-journaled | |
93 | * data blocks. */ | |
94 | ||
617ba13b MC |
95 | if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || |
96 | (!is_metadata && !ext4_should_journal_data(inode))) { | |
ac27a0ec | 97 | if (bh) { |
dab291af | 98 | BUFFER_TRACE(bh, "call jbd2_journal_forget"); |
617ba13b | 99 | return ext4_journal_forget(handle, bh); |
ac27a0ec DK |
100 | } |
101 | return 0; | |
102 | } | |
103 | ||
104 | /* | |
105 | * data!=journal && (is_metadata || should_journal_data(inode)) | |
106 | */ | |
617ba13b MC |
107 | BUFFER_TRACE(bh, "call ext4_journal_revoke"); |
108 | err = ext4_journal_revoke(handle, blocknr, bh); | |
ac27a0ec | 109 | if (err) |
46e665e9 | 110 | ext4_abort(inode->i_sb, __func__, |
ac27a0ec DK |
111 | "error %d when attempting revoke", err); |
112 | BUFFER_TRACE(bh, "exit"); | |
113 | return err; | |
114 | } | |
115 | ||
116 | /* | |
117 | * Work out how many blocks we need to proceed with the next chunk of a | |
118 | * truncate transaction. | |
119 | */ | |
120 | static unsigned long blocks_for_truncate(struct inode *inode) | |
121 | { | |
725d26d3 | 122 | ext4_lblk_t needed; |
ac27a0ec DK |
123 | |
124 | needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); | |
125 | ||
126 | /* Give ourselves just enough room to cope with inodes in which | |
127 | * i_blocks is corrupt: we've seen disk corruptions in the past | |
128 | * which resulted in random data in an inode which looked enough | |
617ba13b | 129 | * like a regular file for ext4 to try to delete it. Things |
ac27a0ec DK |
130 | * will go a bit crazy if that happens, but at least we should |
131 | * try not to panic the whole kernel. */ | |
132 | if (needed < 2) | |
133 | needed = 2; | |
134 | ||
135 | /* But we need to bound the transaction so we don't overflow the | |
136 | * journal. */ | |
617ba13b MC |
137 | if (needed > EXT4_MAX_TRANS_DATA) |
138 | needed = EXT4_MAX_TRANS_DATA; | |
ac27a0ec | 139 | |
617ba13b | 140 | return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; |
ac27a0ec DK |
141 | } |
142 | ||
143 | /* | |
144 | * Truncate transactions can be complex and absolutely huge. So we need to | |
145 | * be able to restart the transaction at a conventient checkpoint to make | |
146 | * sure we don't overflow the journal. | |
147 | * | |
148 | * start_transaction gets us a new handle for a truncate transaction, | |
149 | * and extend_transaction tries to extend the existing one a bit. If | |
150 | * extend fails, we need to propagate the failure up and restart the | |
151 | * transaction in the top-level truncate loop. --sct | |
152 | */ | |
153 | static handle_t *start_transaction(struct inode *inode) | |
154 | { | |
155 | handle_t *result; | |
156 | ||
617ba13b | 157 | result = ext4_journal_start(inode, blocks_for_truncate(inode)); |
ac27a0ec DK |
158 | if (!IS_ERR(result)) |
159 | return result; | |
160 | ||
617ba13b | 161 | ext4_std_error(inode->i_sb, PTR_ERR(result)); |
ac27a0ec DK |
162 | return result; |
163 | } | |
164 | ||
165 | /* | |
166 | * Try to extend this transaction for the purposes of truncation. | |
167 | * | |
168 | * Returns 0 if we managed to create more room. If we can't create more | |
169 | * room, and the transaction must be restarted we return 1. | |
170 | */ | |
171 | static int try_to_extend_transaction(handle_t *handle, struct inode *inode) | |
172 | { | |
617ba13b | 173 | if (handle->h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS) |
ac27a0ec | 174 | return 0; |
617ba13b | 175 | if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) |
ac27a0ec DK |
176 | return 0; |
177 | return 1; | |
178 | } | |
179 | ||
180 | /* | |
181 | * Restart the transaction associated with *handle. This does a commit, | |
182 | * so before we call here everything must be consistently dirtied against | |
183 | * this transaction. | |
184 | */ | |
617ba13b | 185 | static int ext4_journal_test_restart(handle_t *handle, struct inode *inode) |
ac27a0ec DK |
186 | { |
187 | jbd_debug(2, "restarting handle %p\n", handle); | |
617ba13b | 188 | return ext4_journal_restart(handle, blocks_for_truncate(inode)); |
ac27a0ec DK |
189 | } |
190 | ||
191 | /* | |
192 | * Called at the last iput() if i_nlink is zero. | |
193 | */ | |
af5bc92d | 194 | void ext4_delete_inode(struct inode *inode) |
ac27a0ec DK |
195 | { |
196 | handle_t *handle; | |
bc965ab3 | 197 | int err; |
ac27a0ec | 198 | |
678aaf48 JK |
199 | if (ext4_should_order_data(inode)) |
200 | ext4_begin_ordered_truncate(inode, 0); | |
ac27a0ec DK |
201 | truncate_inode_pages(&inode->i_data, 0); |
202 | ||
203 | if (is_bad_inode(inode)) | |
204 | goto no_delete; | |
205 | ||
bc965ab3 | 206 | handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); |
ac27a0ec | 207 | if (IS_ERR(handle)) { |
bc965ab3 | 208 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); |
ac27a0ec DK |
209 | /* |
210 | * If we're going to skip the normal cleanup, we still need to | |
211 | * make sure that the in-core orphan linked list is properly | |
212 | * cleaned up. | |
213 | */ | |
617ba13b | 214 | ext4_orphan_del(NULL, inode); |
ac27a0ec DK |
215 | goto no_delete; |
216 | } | |
217 | ||
218 | if (IS_SYNC(inode)) | |
219 | handle->h_sync = 1; | |
220 | inode->i_size = 0; | |
bc965ab3 TT |
221 | err = ext4_mark_inode_dirty(handle, inode); |
222 | if (err) { | |
223 | ext4_warning(inode->i_sb, __func__, | |
224 | "couldn't mark inode dirty (err %d)", err); | |
225 | goto stop_handle; | |
226 | } | |
ac27a0ec | 227 | if (inode->i_blocks) |
617ba13b | 228 | ext4_truncate(inode); |
bc965ab3 TT |
229 | |
230 | /* | |
231 | * ext4_ext_truncate() doesn't reserve any slop when it | |
232 | * restarts journal transactions; therefore there may not be | |
233 | * enough credits left in the handle to remove the inode from | |
234 | * the orphan list and set the dtime field. | |
235 | */ | |
236 | if (handle->h_buffer_credits < 3) { | |
237 | err = ext4_journal_extend(handle, 3); | |
238 | if (err > 0) | |
239 | err = ext4_journal_restart(handle, 3); | |
240 | if (err != 0) { | |
241 | ext4_warning(inode->i_sb, __func__, | |
242 | "couldn't extend journal (err %d)", err); | |
243 | stop_handle: | |
244 | ext4_journal_stop(handle); | |
245 | goto no_delete; | |
246 | } | |
247 | } | |
248 | ||
ac27a0ec | 249 | /* |
617ba13b | 250 | * Kill off the orphan record which ext4_truncate created. |
ac27a0ec | 251 | * AKPM: I think this can be inside the above `if'. |
617ba13b | 252 | * Note that ext4_orphan_del() has to be able to cope with the |
ac27a0ec | 253 | * deletion of a non-existent orphan - this is because we don't |
617ba13b | 254 | * know if ext4_truncate() actually created an orphan record. |
ac27a0ec DK |
255 | * (Well, we could do this if we need to, but heck - it works) |
256 | */ | |
617ba13b MC |
257 | ext4_orphan_del(handle, inode); |
258 | EXT4_I(inode)->i_dtime = get_seconds(); | |
ac27a0ec DK |
259 | |
260 | /* | |
261 | * One subtle ordering requirement: if anything has gone wrong | |
262 | * (transaction abort, IO errors, whatever), then we can still | |
263 | * do these next steps (the fs will already have been marked as | |
264 | * having errors), but we can't free the inode if the mark_dirty | |
265 | * fails. | |
266 | */ | |
617ba13b | 267 | if (ext4_mark_inode_dirty(handle, inode)) |
ac27a0ec DK |
268 | /* If that failed, just do the required in-core inode clear. */ |
269 | clear_inode(inode); | |
270 | else | |
617ba13b MC |
271 | ext4_free_inode(handle, inode); |
272 | ext4_journal_stop(handle); | |
ac27a0ec DK |
273 | return; |
274 | no_delete: | |
275 | clear_inode(inode); /* We must guarantee clearing of inode... */ | |
276 | } | |
277 | ||
278 | typedef struct { | |
279 | __le32 *p; | |
280 | __le32 key; | |
281 | struct buffer_head *bh; | |
282 | } Indirect; | |
283 | ||
284 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) | |
285 | { | |
286 | p->key = *(p->p = v); | |
287 | p->bh = bh; | |
288 | } | |
289 | ||
ac27a0ec | 290 | /** |
617ba13b | 291 | * ext4_block_to_path - parse the block number into array of offsets |
ac27a0ec DK |
292 | * @inode: inode in question (we are only interested in its superblock) |
293 | * @i_block: block number to be parsed | |
294 | * @offsets: array to store the offsets in | |
8c55e204 DK |
295 | * @boundary: set this non-zero if the referred-to block is likely to be |
296 | * followed (on disk) by an indirect block. | |
ac27a0ec | 297 | * |
617ba13b | 298 | * To store the locations of file's data ext4 uses a data structure common |
ac27a0ec DK |
299 | * for UNIX filesystems - tree of pointers anchored in the inode, with |
300 | * data blocks at leaves and indirect blocks in intermediate nodes. | |
301 | * This function translates the block number into path in that tree - | |
302 | * return value is the path length and @offsets[n] is the offset of | |
303 | * pointer to (n+1)th node in the nth one. If @block is out of range | |
304 | * (negative or too large) warning is printed and zero returned. | |
305 | * | |
306 | * Note: function doesn't find node addresses, so no IO is needed. All | |
307 | * we need to know is the capacity of indirect blocks (taken from the | |
308 | * inode->i_sb). | |
309 | */ | |
310 | ||
311 | /* | |
312 | * Portability note: the last comparison (check that we fit into triple | |
313 | * indirect block) is spelled differently, because otherwise on an | |
314 | * architecture with 32-bit longs and 8Kb pages we might get into trouble | |
315 | * if our filesystem had 8Kb blocks. We might use long long, but that would | |
316 | * kill us on x86. Oh, well, at least the sign propagation does not matter - | |
317 | * i_block would have to be negative in the very beginning, so we would not | |
318 | * get there at all. | |
319 | */ | |
320 | ||
617ba13b | 321 | static int ext4_block_to_path(struct inode *inode, |
725d26d3 AK |
322 | ext4_lblk_t i_block, |
323 | ext4_lblk_t offsets[4], int *boundary) | |
ac27a0ec | 324 | { |
617ba13b MC |
325 | int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
326 | int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); | |
327 | const long direct_blocks = EXT4_NDIR_BLOCKS, | |
ac27a0ec DK |
328 | indirect_blocks = ptrs, |
329 | double_blocks = (1 << (ptrs_bits * 2)); | |
330 | int n = 0; | |
331 | int final = 0; | |
332 | ||
333 | if (i_block < 0) { | |
af5bc92d | 334 | ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0"); |
ac27a0ec DK |
335 | } else if (i_block < direct_blocks) { |
336 | offsets[n++] = i_block; | |
337 | final = direct_blocks; | |
af5bc92d | 338 | } else if ((i_block -= direct_blocks) < indirect_blocks) { |
617ba13b | 339 | offsets[n++] = EXT4_IND_BLOCK; |
ac27a0ec DK |
340 | offsets[n++] = i_block; |
341 | final = ptrs; | |
342 | } else if ((i_block -= indirect_blocks) < double_blocks) { | |
617ba13b | 343 | offsets[n++] = EXT4_DIND_BLOCK; |
ac27a0ec DK |
344 | offsets[n++] = i_block >> ptrs_bits; |
345 | offsets[n++] = i_block & (ptrs - 1); | |
346 | final = ptrs; | |
347 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { | |
617ba13b | 348 | offsets[n++] = EXT4_TIND_BLOCK; |
ac27a0ec DK |
349 | offsets[n++] = i_block >> (ptrs_bits * 2); |
350 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); | |
351 | offsets[n++] = i_block & (ptrs - 1); | |
352 | final = ptrs; | |
353 | } else { | |
e2b46574 | 354 | ext4_warning(inode->i_sb, "ext4_block_to_path", |
0e855ac8 | 355 | "block %lu > max", |
e2b46574 ES |
356 | i_block + direct_blocks + |
357 | indirect_blocks + double_blocks); | |
ac27a0ec DK |
358 | } |
359 | if (boundary) | |
360 | *boundary = final - 1 - (i_block & (ptrs - 1)); | |
361 | return n; | |
362 | } | |
363 | ||
364 | /** | |
617ba13b | 365 | * ext4_get_branch - read the chain of indirect blocks leading to data |
ac27a0ec DK |
366 | * @inode: inode in question |
367 | * @depth: depth of the chain (1 - direct pointer, etc.) | |
368 | * @offsets: offsets of pointers in inode/indirect blocks | |
369 | * @chain: place to store the result | |
370 | * @err: here we store the error value | |
371 | * | |
372 | * Function fills the array of triples <key, p, bh> and returns %NULL | |
373 | * if everything went OK or the pointer to the last filled triple | |
374 | * (incomplete one) otherwise. Upon the return chain[i].key contains | |
375 | * the number of (i+1)-th block in the chain (as it is stored in memory, | |
376 | * i.e. little-endian 32-bit), chain[i].p contains the address of that | |
377 | * number (it points into struct inode for i==0 and into the bh->b_data | |
378 | * for i>0) and chain[i].bh points to the buffer_head of i-th indirect | |
379 | * block for i>0 and NULL for i==0. In other words, it holds the block | |
380 | * numbers of the chain, addresses they were taken from (and where we can | |
381 | * verify that chain did not change) and buffer_heads hosting these | |
382 | * numbers. | |
383 | * | |
384 | * Function stops when it stumbles upon zero pointer (absent block) | |
385 | * (pointer to last triple returned, *@err == 0) | |
386 | * or when it gets an IO error reading an indirect block | |
387 | * (ditto, *@err == -EIO) | |
ac27a0ec DK |
388 | * or when it reads all @depth-1 indirect blocks successfully and finds |
389 | * the whole chain, all way to the data (returns %NULL, *err == 0). | |
c278bfec AK |
390 | * |
391 | * Need to be called with | |
0e855ac8 | 392 | * down_read(&EXT4_I(inode)->i_data_sem) |
ac27a0ec | 393 | */ |
725d26d3 AK |
394 | static Indirect *ext4_get_branch(struct inode *inode, int depth, |
395 | ext4_lblk_t *offsets, | |
ac27a0ec DK |
396 | Indirect chain[4], int *err) |
397 | { | |
398 | struct super_block *sb = inode->i_sb; | |
399 | Indirect *p = chain; | |
400 | struct buffer_head *bh; | |
401 | ||
402 | *err = 0; | |
403 | /* i_data is not going away, no lock needed */ | |
af5bc92d | 404 | add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); |
ac27a0ec DK |
405 | if (!p->key) |
406 | goto no_block; | |
407 | while (--depth) { | |
408 | bh = sb_bread(sb, le32_to_cpu(p->key)); | |
409 | if (!bh) | |
410 | goto failure; | |
af5bc92d | 411 | add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); |
ac27a0ec DK |
412 | /* Reader: end */ |
413 | if (!p->key) | |
414 | goto no_block; | |
415 | } | |
416 | return NULL; | |
417 | ||
ac27a0ec DK |
418 | failure: |
419 | *err = -EIO; | |
420 | no_block: | |
421 | return p; | |
422 | } | |
423 | ||
424 | /** | |
617ba13b | 425 | * ext4_find_near - find a place for allocation with sufficient locality |
ac27a0ec DK |
426 | * @inode: owner |
427 | * @ind: descriptor of indirect block. | |
428 | * | |
1cc8dcf5 | 429 | * This function returns the preferred place for block allocation. |
ac27a0ec DK |
430 | * It is used when heuristic for sequential allocation fails. |
431 | * Rules are: | |
432 | * + if there is a block to the left of our position - allocate near it. | |
433 | * + if pointer will live in indirect block - allocate near that block. | |
434 | * + if pointer will live in inode - allocate in the same | |
435 | * cylinder group. | |
436 | * | |
437 | * In the latter case we colour the starting block by the callers PID to | |
438 | * prevent it from clashing with concurrent allocations for a different inode | |
439 | * in the same block group. The PID is used here so that functionally related | |
440 | * files will be close-by on-disk. | |
441 | * | |
442 | * Caller must make sure that @ind is valid and will stay that way. | |
443 | */ | |
617ba13b | 444 | static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) |
ac27a0ec | 445 | { |
617ba13b | 446 | struct ext4_inode_info *ei = EXT4_I(inode); |
af5bc92d | 447 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; |
ac27a0ec | 448 | __le32 *p; |
617ba13b | 449 | ext4_fsblk_t bg_start; |
74d3487f | 450 | ext4_fsblk_t last_block; |
617ba13b | 451 | ext4_grpblk_t colour; |
ac27a0ec DK |
452 | |
453 | /* Try to find previous block */ | |
454 | for (p = ind->p - 1; p >= start; p--) { | |
455 | if (*p) | |
456 | return le32_to_cpu(*p); | |
457 | } | |
458 | ||
459 | /* No such thing, so let's try location of indirect block */ | |
460 | if (ind->bh) | |
461 | return ind->bh->b_blocknr; | |
462 | ||
463 | /* | |
464 | * It is going to be referred to from the inode itself? OK, just put it | |
465 | * into the same cylinder group then. | |
466 | */ | |
617ba13b | 467 | bg_start = ext4_group_first_block_no(inode->i_sb, ei->i_block_group); |
74d3487f VC |
468 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
469 | ||
470 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) | |
471 | colour = (current->pid % 16) * | |
617ba13b | 472 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
74d3487f VC |
473 | else |
474 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); | |
ac27a0ec DK |
475 | return bg_start + colour; |
476 | } | |
477 | ||
478 | /** | |
1cc8dcf5 | 479 | * ext4_find_goal - find a preferred place for allocation. |
ac27a0ec DK |
480 | * @inode: owner |
481 | * @block: block we want | |
ac27a0ec | 482 | * @partial: pointer to the last triple within a chain |
ac27a0ec | 483 | * |
1cc8dcf5 | 484 | * Normally this function find the preferred place for block allocation, |
fb01bfda | 485 | * returns it. |
ac27a0ec | 486 | */ |
725d26d3 | 487 | static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, |
fb01bfda | 488 | Indirect *partial) |
ac27a0ec | 489 | { |
ac27a0ec | 490 | /* |
c2ea3fde | 491 | * XXX need to get goal block from mballoc's data structures |
ac27a0ec | 492 | */ |
ac27a0ec | 493 | |
617ba13b | 494 | return ext4_find_near(inode, partial); |
ac27a0ec DK |
495 | } |
496 | ||
497 | /** | |
617ba13b | 498 | * ext4_blks_to_allocate: Look up the block map and count the number |
ac27a0ec DK |
499 | * of direct blocks need to be allocated for the given branch. |
500 | * | |
501 | * @branch: chain of indirect blocks | |
502 | * @k: number of blocks need for indirect blocks | |
503 | * @blks: number of data blocks to be mapped. | |
504 | * @blocks_to_boundary: the offset in the indirect block | |
505 | * | |
506 | * return the total number of blocks to be allocate, including the | |
507 | * direct and indirect blocks. | |
508 | */ | |
617ba13b | 509 | static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned long blks, |
ac27a0ec DK |
510 | int blocks_to_boundary) |
511 | { | |
512 | unsigned long count = 0; | |
513 | ||
514 | /* | |
515 | * Simple case, [t,d]Indirect block(s) has not allocated yet | |
516 | * then it's clear blocks on that path have not allocated | |
517 | */ | |
518 | if (k > 0) { | |
519 | /* right now we don't handle cross boundary allocation */ | |
520 | if (blks < blocks_to_boundary + 1) | |
521 | count += blks; | |
522 | else | |
523 | count += blocks_to_boundary + 1; | |
524 | return count; | |
525 | } | |
526 | ||
527 | count++; | |
528 | while (count < blks && count <= blocks_to_boundary && | |
529 | le32_to_cpu(*(branch[0].p + count)) == 0) { | |
530 | count++; | |
531 | } | |
532 | return count; | |
533 | } | |
534 | ||
535 | /** | |
617ba13b | 536 | * ext4_alloc_blocks: multiple allocate blocks needed for a branch |
ac27a0ec DK |
537 | * @indirect_blks: the number of blocks need to allocate for indirect |
538 | * blocks | |
539 | * | |
540 | * @new_blocks: on return it will store the new block numbers for | |
541 | * the indirect blocks(if needed) and the first direct block, | |
542 | * @blks: on return it will store the total number of allocated | |
543 | * direct blocks | |
544 | */ | |
617ba13b | 545 | static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, |
7061eba7 AK |
546 | ext4_lblk_t iblock, ext4_fsblk_t goal, |
547 | int indirect_blks, int blks, | |
548 | ext4_fsblk_t new_blocks[4], int *err) | |
ac27a0ec | 549 | { |
815a1130 | 550 | struct ext4_allocation_request ar; |
ac27a0ec | 551 | int target, i; |
7061eba7 | 552 | unsigned long count = 0, blk_allocated = 0; |
ac27a0ec | 553 | int index = 0; |
617ba13b | 554 | ext4_fsblk_t current_block = 0; |
ac27a0ec DK |
555 | int ret = 0; |
556 | ||
557 | /* | |
558 | * Here we try to allocate the requested multiple blocks at once, | |
559 | * on a best-effort basis. | |
560 | * To build a branch, we should allocate blocks for | |
561 | * the indirect blocks(if not allocated yet), and at least | |
562 | * the first direct block of this branch. That's the | |
563 | * minimum number of blocks need to allocate(required) | |
564 | */ | |
7061eba7 AK |
565 | /* first we try to allocate the indirect blocks */ |
566 | target = indirect_blks; | |
567 | while (target > 0) { | |
ac27a0ec DK |
568 | count = target; |
569 | /* allocating blocks for indirect blocks and direct blocks */ | |
7061eba7 AK |
570 | current_block = ext4_new_meta_blocks(handle, inode, |
571 | goal, &count, err); | |
ac27a0ec DK |
572 | if (*err) |
573 | goto failed_out; | |
574 | ||
575 | target -= count; | |
576 | /* allocate blocks for indirect blocks */ | |
577 | while (index < indirect_blks && count) { | |
578 | new_blocks[index++] = current_block++; | |
579 | count--; | |
580 | } | |
7061eba7 AK |
581 | if (count > 0) { |
582 | /* | |
583 | * save the new block number | |
584 | * for the first direct block | |
585 | */ | |
586 | new_blocks[index] = current_block; | |
587 | printk(KERN_INFO "%s returned more blocks than " | |
588 | "requested\n", __func__); | |
589 | WARN_ON(1); | |
ac27a0ec | 590 | break; |
7061eba7 | 591 | } |
ac27a0ec DK |
592 | } |
593 | ||
7061eba7 AK |
594 | target = blks - count ; |
595 | blk_allocated = count; | |
596 | if (!target) | |
597 | goto allocated; | |
598 | /* Now allocate data blocks */ | |
815a1130 TT |
599 | memset(&ar, 0, sizeof(ar)); |
600 | ar.inode = inode; | |
601 | ar.goal = goal; | |
602 | ar.len = target; | |
603 | ar.logical = iblock; | |
604 | if (S_ISREG(inode->i_mode)) | |
605 | /* enable in-core preallocation only for regular files */ | |
606 | ar.flags = EXT4_MB_HINT_DATA; | |
607 | ||
608 | current_block = ext4_mb_new_blocks(handle, &ar, err); | |
609 | ||
7061eba7 AK |
610 | if (*err && (target == blks)) { |
611 | /* | |
612 | * if the allocation failed and we didn't allocate | |
613 | * any blocks before | |
614 | */ | |
615 | goto failed_out; | |
616 | } | |
617 | if (!*err) { | |
618 | if (target == blks) { | |
619 | /* | |
620 | * save the new block number | |
621 | * for the first direct block | |
622 | */ | |
623 | new_blocks[index] = current_block; | |
624 | } | |
815a1130 | 625 | blk_allocated += ar.len; |
7061eba7 AK |
626 | } |
627 | allocated: | |
ac27a0ec | 628 | /* total number of blocks allocated for direct blocks */ |
7061eba7 | 629 | ret = blk_allocated; |
ac27a0ec DK |
630 | *err = 0; |
631 | return ret; | |
632 | failed_out: | |
af5bc92d | 633 | for (i = 0; i < index; i++) |
c9de560d | 634 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
ac27a0ec DK |
635 | return ret; |
636 | } | |
637 | ||
638 | /** | |
617ba13b | 639 | * ext4_alloc_branch - allocate and set up a chain of blocks. |
ac27a0ec DK |
640 | * @inode: owner |
641 | * @indirect_blks: number of allocated indirect blocks | |
642 | * @blks: number of allocated direct blocks | |
643 | * @offsets: offsets (in the blocks) to store the pointers to next. | |
644 | * @branch: place to store the chain in. | |
645 | * | |
646 | * This function allocates blocks, zeroes out all but the last one, | |
647 | * links them into chain and (if we are synchronous) writes them to disk. | |
648 | * In other words, it prepares a branch that can be spliced onto the | |
649 | * inode. It stores the information about that chain in the branch[], in | |
617ba13b | 650 | * the same format as ext4_get_branch() would do. We are calling it after |
ac27a0ec DK |
651 | * we had read the existing part of chain and partial points to the last |
652 | * triple of that (one with zero ->key). Upon the exit we have the same | |
617ba13b | 653 | * picture as after the successful ext4_get_block(), except that in one |
ac27a0ec DK |
654 | * place chain is disconnected - *branch->p is still zero (we did not |
655 | * set the last link), but branch->key contains the number that should | |
656 | * be placed into *branch->p to fill that gap. | |
657 | * | |
658 | * If allocation fails we free all blocks we've allocated (and forget | |
659 | * their buffer_heads) and return the error value the from failed | |
617ba13b | 660 | * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain |
ac27a0ec DK |
661 | * as described above and return 0. |
662 | */ | |
617ba13b | 663 | static int ext4_alloc_branch(handle_t *handle, struct inode *inode, |
7061eba7 AK |
664 | ext4_lblk_t iblock, int indirect_blks, |
665 | int *blks, ext4_fsblk_t goal, | |
666 | ext4_lblk_t *offsets, Indirect *branch) | |
ac27a0ec DK |
667 | { |
668 | int blocksize = inode->i_sb->s_blocksize; | |
669 | int i, n = 0; | |
670 | int err = 0; | |
671 | struct buffer_head *bh; | |
672 | int num; | |
617ba13b MC |
673 | ext4_fsblk_t new_blocks[4]; |
674 | ext4_fsblk_t current_block; | |
ac27a0ec | 675 | |
7061eba7 | 676 | num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, |
ac27a0ec DK |
677 | *blks, new_blocks, &err); |
678 | if (err) | |
679 | return err; | |
680 | ||
681 | branch[0].key = cpu_to_le32(new_blocks[0]); | |
682 | /* | |
683 | * metadata blocks and data blocks are allocated. | |
684 | */ | |
685 | for (n = 1; n <= indirect_blks; n++) { | |
686 | /* | |
687 | * Get buffer_head for parent block, zero it out | |
688 | * and set the pointer to new one, then send | |
689 | * parent to disk. | |
690 | */ | |
691 | bh = sb_getblk(inode->i_sb, new_blocks[n-1]); | |
692 | branch[n].bh = bh; | |
693 | lock_buffer(bh); | |
694 | BUFFER_TRACE(bh, "call get_create_access"); | |
617ba13b | 695 | err = ext4_journal_get_create_access(handle, bh); |
ac27a0ec DK |
696 | if (err) { |
697 | unlock_buffer(bh); | |
698 | brelse(bh); | |
699 | goto failed; | |
700 | } | |
701 | ||
702 | memset(bh->b_data, 0, blocksize); | |
703 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; | |
704 | branch[n].key = cpu_to_le32(new_blocks[n]); | |
705 | *branch[n].p = branch[n].key; | |
af5bc92d | 706 | if (n == indirect_blks) { |
ac27a0ec DK |
707 | current_block = new_blocks[n]; |
708 | /* | |
709 | * End of chain, update the last new metablock of | |
710 | * the chain to point to the new allocated | |
711 | * data blocks numbers | |
712 | */ | |
713 | for (i=1; i < num; i++) | |
714 | *(branch[n].p + i) = cpu_to_le32(++current_block); | |
715 | } | |
716 | BUFFER_TRACE(bh, "marking uptodate"); | |
717 | set_buffer_uptodate(bh); | |
718 | unlock_buffer(bh); | |
719 | ||
617ba13b MC |
720 | BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); |
721 | err = ext4_journal_dirty_metadata(handle, bh); | |
ac27a0ec DK |
722 | if (err) |
723 | goto failed; | |
724 | } | |
725 | *blks = num; | |
726 | return err; | |
727 | failed: | |
728 | /* Allocation failed, free what we already allocated */ | |
729 | for (i = 1; i <= n ; i++) { | |
dab291af | 730 | BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); |
617ba13b | 731 | ext4_journal_forget(handle, branch[i].bh); |
ac27a0ec | 732 | } |
af5bc92d | 733 | for (i = 0; i < indirect_blks; i++) |
c9de560d | 734 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
ac27a0ec | 735 | |
c9de560d | 736 | ext4_free_blocks(handle, inode, new_blocks[i], num, 0); |
ac27a0ec DK |
737 | |
738 | return err; | |
739 | } | |
740 | ||
741 | /** | |
617ba13b | 742 | * ext4_splice_branch - splice the allocated branch onto inode. |
ac27a0ec DK |
743 | * @inode: owner |
744 | * @block: (logical) number of block we are adding | |
745 | * @chain: chain of indirect blocks (with a missing link - see | |
617ba13b | 746 | * ext4_alloc_branch) |
ac27a0ec DK |
747 | * @where: location of missing link |
748 | * @num: number of indirect blocks we are adding | |
749 | * @blks: number of direct blocks we are adding | |
750 | * | |
751 | * This function fills the missing link and does all housekeeping needed in | |
752 | * inode (->i_blocks, etc.). In case of success we end up with the full | |
753 | * chain to new block and return 0. | |
754 | */ | |
617ba13b | 755 | static int ext4_splice_branch(handle_t *handle, struct inode *inode, |
725d26d3 | 756 | ext4_lblk_t block, Indirect *where, int num, int blks) |
ac27a0ec DK |
757 | { |
758 | int i; | |
759 | int err = 0; | |
617ba13b | 760 | ext4_fsblk_t current_block; |
ac27a0ec | 761 | |
ac27a0ec DK |
762 | /* |
763 | * If we're splicing into a [td]indirect block (as opposed to the | |
764 | * inode) then we need to get write access to the [td]indirect block | |
765 | * before the splice. | |
766 | */ | |
767 | if (where->bh) { | |
768 | BUFFER_TRACE(where->bh, "get_write_access"); | |
617ba13b | 769 | err = ext4_journal_get_write_access(handle, where->bh); |
ac27a0ec DK |
770 | if (err) |
771 | goto err_out; | |
772 | } | |
773 | /* That's it */ | |
774 | ||
775 | *where->p = where->key; | |
776 | ||
777 | /* | |
778 | * Update the host buffer_head or inode to point to more just allocated | |
779 | * direct blocks blocks | |
780 | */ | |
781 | if (num == 0 && blks > 1) { | |
782 | current_block = le32_to_cpu(where->key) + 1; | |
783 | for (i = 1; i < blks; i++) | |
af5bc92d | 784 | *(where->p + i) = cpu_to_le32(current_block++); |
ac27a0ec DK |
785 | } |
786 | ||
ac27a0ec DK |
787 | /* We are done with atomic stuff, now do the rest of housekeeping */ |
788 | ||
ef7f3835 | 789 | inode->i_ctime = ext4_current_time(inode); |
617ba13b | 790 | ext4_mark_inode_dirty(handle, inode); |
ac27a0ec DK |
791 | |
792 | /* had we spliced it onto indirect block? */ | |
793 | if (where->bh) { | |
794 | /* | |
795 | * If we spliced it onto an indirect block, we haven't | |
796 | * altered the inode. Note however that if it is being spliced | |
797 | * onto an indirect block at the very end of the file (the | |
798 | * file is growing) then we *will* alter the inode to reflect | |
799 | * the new i_size. But that is not done here - it is done in | |
617ba13b | 800 | * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. |
ac27a0ec DK |
801 | */ |
802 | jbd_debug(5, "splicing indirect only\n"); | |
617ba13b MC |
803 | BUFFER_TRACE(where->bh, "call ext4_journal_dirty_metadata"); |
804 | err = ext4_journal_dirty_metadata(handle, where->bh); | |
ac27a0ec DK |
805 | if (err) |
806 | goto err_out; | |
807 | } else { | |
808 | /* | |
809 | * OK, we spliced it into the inode itself on a direct block. | |
810 | * Inode was dirtied above. | |
811 | */ | |
812 | jbd_debug(5, "splicing direct\n"); | |
813 | } | |
814 | return err; | |
815 | ||
816 | err_out: | |
817 | for (i = 1; i <= num; i++) { | |
dab291af | 818 | BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); |
617ba13b | 819 | ext4_journal_forget(handle, where[i].bh); |
c9de560d AT |
820 | ext4_free_blocks(handle, inode, |
821 | le32_to_cpu(where[i-1].key), 1, 0); | |
ac27a0ec | 822 | } |
c9de560d | 823 | ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); |
ac27a0ec DK |
824 | |
825 | return err; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Allocation strategy is simple: if we have to allocate something, we will | |
830 | * have to go the whole way to leaf. So let's do it before attaching anything | |
831 | * to tree, set linkage between the newborn blocks, write them if sync is | |
832 | * required, recheck the path, free and repeat if check fails, otherwise | |
833 | * set the last missing link (that will protect us from any truncate-generated | |
834 | * removals - all blocks on the path are immune now) and possibly force the | |
835 | * write on the parent block. | |
836 | * That has a nice additional property: no special recovery from the failed | |
837 | * allocations is needed - we simply release blocks and do not touch anything | |
838 | * reachable from inode. | |
839 | * | |
840 | * `handle' can be NULL if create == 0. | |
841 | * | |
ac27a0ec DK |
842 | * return > 0, # of blocks mapped or allocated. |
843 | * return = 0, if plain lookup failed. | |
844 | * return < 0, error case. | |
c278bfec AK |
845 | * |
846 | * | |
847 | * Need to be called with | |
0e855ac8 AK |
848 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
849 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) | |
ac27a0ec | 850 | */ |
617ba13b | 851 | int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, |
725d26d3 | 852 | ext4_lblk_t iblock, unsigned long maxblocks, |
ac27a0ec DK |
853 | struct buffer_head *bh_result, |
854 | int create, int extend_disksize) | |
855 | { | |
856 | int err = -EIO; | |
725d26d3 | 857 | ext4_lblk_t offsets[4]; |
ac27a0ec DK |
858 | Indirect chain[4]; |
859 | Indirect *partial; | |
617ba13b | 860 | ext4_fsblk_t goal; |
ac27a0ec DK |
861 | int indirect_blks; |
862 | int blocks_to_boundary = 0; | |
863 | int depth; | |
617ba13b | 864 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 865 | int count = 0; |
617ba13b | 866 | ext4_fsblk_t first_block = 0; |
61628a3f | 867 | loff_t disksize; |
ac27a0ec DK |
868 | |
869 | ||
a86c6181 | 870 | J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); |
ac27a0ec | 871 | J_ASSERT(handle != NULL || create == 0); |
725d26d3 AK |
872 | depth = ext4_block_to_path(inode, iblock, offsets, |
873 | &blocks_to_boundary); | |
ac27a0ec DK |
874 | |
875 | if (depth == 0) | |
876 | goto out; | |
877 | ||
617ba13b | 878 | partial = ext4_get_branch(inode, depth, offsets, chain, &err); |
ac27a0ec DK |
879 | |
880 | /* Simplest case - block found, no allocation needed */ | |
881 | if (!partial) { | |
882 | first_block = le32_to_cpu(chain[depth - 1].key); | |
883 | clear_buffer_new(bh_result); | |
884 | count++; | |
885 | /*map more blocks*/ | |
886 | while (count < maxblocks && count <= blocks_to_boundary) { | |
617ba13b | 887 | ext4_fsblk_t blk; |
ac27a0ec | 888 | |
ac27a0ec DK |
889 | blk = le32_to_cpu(*(chain[depth-1].p + count)); |
890 | ||
891 | if (blk == first_block + count) | |
892 | count++; | |
893 | else | |
894 | break; | |
895 | } | |
c278bfec | 896 | goto got_it; |
ac27a0ec DK |
897 | } |
898 | ||
899 | /* Next simple case - plain lookup or failed read of indirect block */ | |
900 | if (!create || err == -EIO) | |
901 | goto cleanup; | |
902 | ||
ac27a0ec | 903 | /* |
c2ea3fde | 904 | * Okay, we need to do block allocation. |
ac27a0ec | 905 | */ |
fb01bfda | 906 | goal = ext4_find_goal(inode, iblock, partial); |
ac27a0ec DK |
907 | |
908 | /* the number of blocks need to allocate for [d,t]indirect blocks */ | |
909 | indirect_blks = (chain + depth) - partial - 1; | |
910 | ||
911 | /* | |
912 | * Next look up the indirect map to count the totoal number of | |
913 | * direct blocks to allocate for this branch. | |
914 | */ | |
617ba13b | 915 | count = ext4_blks_to_allocate(partial, indirect_blks, |
ac27a0ec DK |
916 | maxblocks, blocks_to_boundary); |
917 | /* | |
617ba13b | 918 | * Block out ext4_truncate while we alter the tree |
ac27a0ec | 919 | */ |
7061eba7 AK |
920 | err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, |
921 | &count, goal, | |
922 | offsets + (partial - chain), partial); | |
ac27a0ec DK |
923 | |
924 | /* | |
617ba13b | 925 | * The ext4_splice_branch call will free and forget any buffers |
ac27a0ec DK |
926 | * on the new chain if there is a failure, but that risks using |
927 | * up transaction credits, especially for bitmaps where the | |
928 | * credits cannot be returned. Can we handle this somehow? We | |
929 | * may need to return -EAGAIN upwards in the worst case. --sct | |
930 | */ | |
931 | if (!err) | |
617ba13b | 932 | err = ext4_splice_branch(handle, inode, iblock, |
ac27a0ec DK |
933 | partial, indirect_blks, count); |
934 | /* | |
0e855ac8 | 935 | * i_disksize growing is protected by i_data_sem. Don't forget to |
ac27a0ec | 936 | * protect it if you're about to implement concurrent |
617ba13b | 937 | * ext4_get_block() -bzzz |
ac27a0ec | 938 | */ |
61628a3f MC |
939 | if (!err && extend_disksize) { |
940 | disksize = ((loff_t) iblock + count) << inode->i_blkbits; | |
941 | if (disksize > i_size_read(inode)) | |
942 | disksize = i_size_read(inode); | |
943 | if (disksize > ei->i_disksize) | |
944 | ei->i_disksize = disksize; | |
945 | } | |
ac27a0ec DK |
946 | if (err) |
947 | goto cleanup; | |
948 | ||
949 | set_buffer_new(bh_result); | |
950 | got_it: | |
951 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); | |
952 | if (count > blocks_to_boundary) | |
953 | set_buffer_boundary(bh_result); | |
954 | err = count; | |
955 | /* Clean up and exit */ | |
956 | partial = chain + depth - 1; /* the whole chain */ | |
957 | cleanup: | |
958 | while (partial > chain) { | |
959 | BUFFER_TRACE(partial->bh, "call brelse"); | |
960 | brelse(partial->bh); | |
961 | partial--; | |
962 | } | |
963 | BUFFER_TRACE(bh_result, "returned"); | |
964 | out: | |
965 | return err; | |
966 | } | |
967 | ||
12219aea AK |
968 | /* |
969 | * Calculate the number of metadata blocks need to reserve | |
970 | * to allocate @blocks for non extent file based file | |
971 | */ | |
972 | static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) | |
973 | { | |
974 | int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); | |
975 | int ind_blks, dind_blks, tind_blks; | |
976 | ||
977 | /* number of new indirect blocks needed */ | |
978 | ind_blks = (blocks + icap - 1) / icap; | |
979 | ||
980 | dind_blks = (ind_blks + icap - 1) / icap; | |
981 | ||
982 | tind_blks = 1; | |
983 | ||
984 | return ind_blks + dind_blks + tind_blks; | |
985 | } | |
986 | ||
987 | /* | |
988 | * Calculate the number of metadata blocks need to reserve | |
989 | * to allocate given number of blocks | |
990 | */ | |
991 | static int ext4_calc_metadata_amount(struct inode *inode, int blocks) | |
992 | { | |
cd213226 MC |
993 | if (!blocks) |
994 | return 0; | |
995 | ||
12219aea AK |
996 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) |
997 | return ext4_ext_calc_metadata_amount(inode, blocks); | |
998 | ||
999 | return ext4_indirect_calc_metadata_amount(inode, blocks); | |
1000 | } | |
1001 | ||
1002 | static void ext4_da_update_reserve_space(struct inode *inode, int used) | |
1003 | { | |
1004 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1005 | int total, mdb, mdb_free; | |
1006 | ||
1007 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | |
1008 | /* recalculate the number of metablocks still need to be reserved */ | |
1009 | total = EXT4_I(inode)->i_reserved_data_blocks - used; | |
1010 | mdb = ext4_calc_metadata_amount(inode, total); | |
1011 | ||
1012 | /* figure out how many metablocks to release */ | |
1013 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); | |
1014 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; | |
1015 | ||
6bc6e63f AK |
1016 | if (mdb_free) { |
1017 | /* Account for allocated meta_blocks */ | |
1018 | mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; | |
1019 | ||
1020 | /* update fs dirty blocks counter */ | |
1021 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); | |
1022 | EXT4_I(inode)->i_allocated_meta_blocks = 0; | |
1023 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; | |
1024 | } | |
12219aea AK |
1025 | |
1026 | /* update per-inode reservations */ | |
1027 | BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); | |
1028 | EXT4_I(inode)->i_reserved_data_blocks -= used; | |
1029 | ||
12219aea AK |
1030 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
1031 | } | |
1032 | ||
f5ab0d1f | 1033 | /* |
2b2d6d01 TT |
1034 | * The ext4_get_blocks_wrap() function try to look up the requested blocks, |
1035 | * and returns if the blocks are already mapped. | |
f5ab0d1f | 1036 | * |
f5ab0d1f MC |
1037 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks |
1038 | * and store the allocated blocks in the result buffer head and mark it | |
1039 | * mapped. | |
1040 | * | |
1041 | * If file type is extents based, it will call ext4_ext_get_blocks(), | |
1042 | * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping | |
1043 | * based files | |
1044 | * | |
1045 | * On success, it returns the number of blocks being mapped or allocate. | |
1046 | * if create==0 and the blocks are pre-allocated and uninitialized block, | |
1047 | * the result buffer head is unmapped. If the create ==1, it will make sure | |
1048 | * the buffer head is mapped. | |
1049 | * | |
1050 | * It returns 0 if plain look up failed (blocks have not been allocated), in | |
1051 | * that casem, buffer head is unmapped | |
1052 | * | |
1053 | * It returns the error in case of allocation failure. | |
1054 | */ | |
0e855ac8 AK |
1055 | int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block, |
1056 | unsigned long max_blocks, struct buffer_head *bh, | |
d2a17637 | 1057 | int create, int extend_disksize, int flag) |
0e855ac8 AK |
1058 | { |
1059 | int retval; | |
f5ab0d1f MC |
1060 | |
1061 | clear_buffer_mapped(bh); | |
1062 | ||
4df3d265 AK |
1063 | /* |
1064 | * Try to see if we can get the block without requesting | |
1065 | * for new file system block. | |
1066 | */ | |
1067 | down_read((&EXT4_I(inode)->i_data_sem)); | |
1068 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { | |
1069 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, | |
1070 | bh, 0, 0); | |
0e855ac8 | 1071 | } else { |
4df3d265 AK |
1072 | retval = ext4_get_blocks_handle(handle, |
1073 | inode, block, max_blocks, bh, 0, 0); | |
0e855ac8 | 1074 | } |
4df3d265 | 1075 | up_read((&EXT4_I(inode)->i_data_sem)); |
f5ab0d1f MC |
1076 | |
1077 | /* If it is only a block(s) look up */ | |
1078 | if (!create) | |
1079 | return retval; | |
1080 | ||
1081 | /* | |
1082 | * Returns if the blocks have already allocated | |
1083 | * | |
1084 | * Note that if blocks have been preallocated | |
1085 | * ext4_ext_get_block() returns th create = 0 | |
1086 | * with buffer head unmapped. | |
1087 | */ | |
1088 | if (retval > 0 && buffer_mapped(bh)) | |
4df3d265 AK |
1089 | return retval; |
1090 | ||
1091 | /* | |
f5ab0d1f MC |
1092 | * New blocks allocate and/or writing to uninitialized extent |
1093 | * will possibly result in updating i_data, so we take | |
1094 | * the write lock of i_data_sem, and call get_blocks() | |
1095 | * with create == 1 flag. | |
4df3d265 AK |
1096 | */ |
1097 | down_write((&EXT4_I(inode)->i_data_sem)); | |
d2a17637 MC |
1098 | |
1099 | /* | |
1100 | * if the caller is from delayed allocation writeout path | |
1101 | * we have already reserved fs blocks for allocation | |
1102 | * let the underlying get_block() function know to | |
1103 | * avoid double accounting | |
1104 | */ | |
1105 | if (flag) | |
1106 | EXT4_I(inode)->i_delalloc_reserved_flag = 1; | |
4df3d265 AK |
1107 | /* |
1108 | * We need to check for EXT4 here because migrate | |
1109 | * could have changed the inode type in between | |
1110 | */ | |
0e855ac8 AK |
1111 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
1112 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, | |
1113 | bh, create, extend_disksize); | |
1114 | } else { | |
1115 | retval = ext4_get_blocks_handle(handle, inode, block, | |
1116 | max_blocks, bh, create, extend_disksize); | |
267e4db9 AK |
1117 | |
1118 | if (retval > 0 && buffer_new(bh)) { | |
1119 | /* | |
1120 | * We allocated new blocks which will result in | |
1121 | * i_data's format changing. Force the migrate | |
1122 | * to fail by clearing migrate flags | |
1123 | */ | |
1124 | EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags & | |
1125 | ~EXT4_EXT_MIGRATE; | |
1126 | } | |
0e855ac8 | 1127 | } |
d2a17637 MC |
1128 | |
1129 | if (flag) { | |
1130 | EXT4_I(inode)->i_delalloc_reserved_flag = 0; | |
1131 | /* | |
1132 | * Update reserved blocks/metadata blocks | |
1133 | * after successful block allocation | |
1134 | * which were deferred till now | |
1135 | */ | |
1136 | if ((retval > 0) && buffer_delay(bh)) | |
12219aea | 1137 | ext4_da_update_reserve_space(inode, retval); |
d2a17637 MC |
1138 | } |
1139 | ||
4df3d265 | 1140 | up_write((&EXT4_I(inode)->i_data_sem)); |
0e855ac8 AK |
1141 | return retval; |
1142 | } | |
1143 | ||
f3bd1f3f MC |
1144 | /* Maximum number of blocks we map for direct IO at once. */ |
1145 | #define DIO_MAX_BLOCKS 4096 | |
1146 | ||
6873fa0d ES |
1147 | int ext4_get_block(struct inode *inode, sector_t iblock, |
1148 | struct buffer_head *bh_result, int create) | |
ac27a0ec | 1149 | { |
3e4fdaf8 | 1150 | handle_t *handle = ext4_journal_current_handle(); |
7fb5409d | 1151 | int ret = 0, started = 0; |
ac27a0ec | 1152 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
f3bd1f3f | 1153 | int dio_credits; |
ac27a0ec | 1154 | |
7fb5409d JK |
1155 | if (create && !handle) { |
1156 | /* Direct IO write... */ | |
1157 | if (max_blocks > DIO_MAX_BLOCKS) | |
1158 | max_blocks = DIO_MAX_BLOCKS; | |
f3bd1f3f MC |
1159 | dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); |
1160 | handle = ext4_journal_start(inode, dio_credits); | |
7fb5409d | 1161 | if (IS_ERR(handle)) { |
ac27a0ec | 1162 | ret = PTR_ERR(handle); |
7fb5409d | 1163 | goto out; |
ac27a0ec | 1164 | } |
7fb5409d | 1165 | started = 1; |
ac27a0ec DK |
1166 | } |
1167 | ||
7fb5409d | 1168 | ret = ext4_get_blocks_wrap(handle, inode, iblock, |
d2a17637 | 1169 | max_blocks, bh_result, create, 0, 0); |
7fb5409d JK |
1170 | if (ret > 0) { |
1171 | bh_result->b_size = (ret << inode->i_blkbits); | |
1172 | ret = 0; | |
ac27a0ec | 1173 | } |
7fb5409d JK |
1174 | if (started) |
1175 | ext4_journal_stop(handle); | |
1176 | out: | |
ac27a0ec DK |
1177 | return ret; |
1178 | } | |
1179 | ||
1180 | /* | |
1181 | * `handle' can be NULL if create is zero | |
1182 | */ | |
617ba13b | 1183 | struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, |
725d26d3 | 1184 | ext4_lblk_t block, int create, int *errp) |
ac27a0ec DK |
1185 | { |
1186 | struct buffer_head dummy; | |
1187 | int fatal = 0, err; | |
1188 | ||
1189 | J_ASSERT(handle != NULL || create == 0); | |
1190 | ||
1191 | dummy.b_state = 0; | |
1192 | dummy.b_blocknr = -1000; | |
1193 | buffer_trace_init(&dummy.b_history); | |
a86c6181 | 1194 | err = ext4_get_blocks_wrap(handle, inode, block, 1, |
d2a17637 | 1195 | &dummy, create, 1, 0); |
ac27a0ec | 1196 | /* |
617ba13b | 1197 | * ext4_get_blocks_handle() returns number of blocks |
ac27a0ec DK |
1198 | * mapped. 0 in case of a HOLE. |
1199 | */ | |
1200 | if (err > 0) { | |
1201 | if (err > 1) | |
1202 | WARN_ON(1); | |
1203 | err = 0; | |
1204 | } | |
1205 | *errp = err; | |
1206 | if (!err && buffer_mapped(&dummy)) { | |
1207 | struct buffer_head *bh; | |
1208 | bh = sb_getblk(inode->i_sb, dummy.b_blocknr); | |
1209 | if (!bh) { | |
1210 | *errp = -EIO; | |
1211 | goto err; | |
1212 | } | |
1213 | if (buffer_new(&dummy)) { | |
1214 | J_ASSERT(create != 0); | |
ac39849d | 1215 | J_ASSERT(handle != NULL); |
ac27a0ec DK |
1216 | |
1217 | /* | |
1218 | * Now that we do not always journal data, we should | |
1219 | * keep in mind whether this should always journal the | |
1220 | * new buffer as metadata. For now, regular file | |
617ba13b | 1221 | * writes use ext4_get_block instead, so it's not a |
ac27a0ec DK |
1222 | * problem. |
1223 | */ | |
1224 | lock_buffer(bh); | |
1225 | BUFFER_TRACE(bh, "call get_create_access"); | |
617ba13b | 1226 | fatal = ext4_journal_get_create_access(handle, bh); |
ac27a0ec | 1227 | if (!fatal && !buffer_uptodate(bh)) { |
af5bc92d | 1228 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); |
ac27a0ec DK |
1229 | set_buffer_uptodate(bh); |
1230 | } | |
1231 | unlock_buffer(bh); | |
617ba13b MC |
1232 | BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); |
1233 | err = ext4_journal_dirty_metadata(handle, bh); | |
ac27a0ec DK |
1234 | if (!fatal) |
1235 | fatal = err; | |
1236 | } else { | |
1237 | BUFFER_TRACE(bh, "not a new buffer"); | |
1238 | } | |
1239 | if (fatal) { | |
1240 | *errp = fatal; | |
1241 | brelse(bh); | |
1242 | bh = NULL; | |
1243 | } | |
1244 | return bh; | |
1245 | } | |
1246 | err: | |
1247 | return NULL; | |
1248 | } | |
1249 | ||
617ba13b | 1250 | struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, |
725d26d3 | 1251 | ext4_lblk_t block, int create, int *err) |
ac27a0ec | 1252 | { |
af5bc92d | 1253 | struct buffer_head *bh; |
ac27a0ec | 1254 | |
617ba13b | 1255 | bh = ext4_getblk(handle, inode, block, create, err); |
ac27a0ec DK |
1256 | if (!bh) |
1257 | return bh; | |
1258 | if (buffer_uptodate(bh)) | |
1259 | return bh; | |
1260 | ll_rw_block(READ_META, 1, &bh); | |
1261 | wait_on_buffer(bh); | |
1262 | if (buffer_uptodate(bh)) | |
1263 | return bh; | |
1264 | put_bh(bh); | |
1265 | *err = -EIO; | |
1266 | return NULL; | |
1267 | } | |
1268 | ||
af5bc92d TT |
1269 | static int walk_page_buffers(handle_t *handle, |
1270 | struct buffer_head *head, | |
1271 | unsigned from, | |
1272 | unsigned to, | |
1273 | int *partial, | |
1274 | int (*fn)(handle_t *handle, | |
1275 | struct buffer_head *bh)) | |
ac27a0ec DK |
1276 | { |
1277 | struct buffer_head *bh; | |
1278 | unsigned block_start, block_end; | |
1279 | unsigned blocksize = head->b_size; | |
1280 | int err, ret = 0; | |
1281 | struct buffer_head *next; | |
1282 | ||
af5bc92d TT |
1283 | for (bh = head, block_start = 0; |
1284 | ret == 0 && (bh != head || !block_start); | |
1285 | block_start = block_end, bh = next) | |
ac27a0ec DK |
1286 | { |
1287 | next = bh->b_this_page; | |
1288 | block_end = block_start + blocksize; | |
1289 | if (block_end <= from || block_start >= to) { | |
1290 | if (partial && !buffer_uptodate(bh)) | |
1291 | *partial = 1; | |
1292 | continue; | |
1293 | } | |
1294 | err = (*fn)(handle, bh); | |
1295 | if (!ret) | |
1296 | ret = err; | |
1297 | } | |
1298 | return ret; | |
1299 | } | |
1300 | ||
1301 | /* | |
1302 | * To preserve ordering, it is essential that the hole instantiation and | |
1303 | * the data write be encapsulated in a single transaction. We cannot | |
617ba13b | 1304 | * close off a transaction and start a new one between the ext4_get_block() |
dab291af | 1305 | * and the commit_write(). So doing the jbd2_journal_start at the start of |
ac27a0ec DK |
1306 | * prepare_write() is the right place. |
1307 | * | |
617ba13b MC |
1308 | * Also, this function can nest inside ext4_writepage() -> |
1309 | * block_write_full_page(). In that case, we *know* that ext4_writepage() | |
ac27a0ec DK |
1310 | * has generated enough buffer credits to do the whole page. So we won't |
1311 | * block on the journal in that case, which is good, because the caller may | |
1312 | * be PF_MEMALLOC. | |
1313 | * | |
617ba13b | 1314 | * By accident, ext4 can be reentered when a transaction is open via |
ac27a0ec DK |
1315 | * quota file writes. If we were to commit the transaction while thus |
1316 | * reentered, there can be a deadlock - we would be holding a quota | |
1317 | * lock, and the commit would never complete if another thread had a | |
1318 | * transaction open and was blocking on the quota lock - a ranking | |
1319 | * violation. | |
1320 | * | |
dab291af | 1321 | * So what we do is to rely on the fact that jbd2_journal_stop/journal_start |
ac27a0ec DK |
1322 | * will _not_ run commit under these circumstances because handle->h_ref |
1323 | * is elevated. We'll still have enough credits for the tiny quotafile | |
1324 | * write. | |
1325 | */ | |
1326 | static int do_journal_get_write_access(handle_t *handle, | |
1327 | struct buffer_head *bh) | |
1328 | { | |
1329 | if (!buffer_mapped(bh) || buffer_freed(bh)) | |
1330 | return 0; | |
617ba13b | 1331 | return ext4_journal_get_write_access(handle, bh); |
ac27a0ec DK |
1332 | } |
1333 | ||
bfc1af65 NP |
1334 | static int ext4_write_begin(struct file *file, struct address_space *mapping, |
1335 | loff_t pos, unsigned len, unsigned flags, | |
1336 | struct page **pagep, void **fsdata) | |
ac27a0ec | 1337 | { |
af5bc92d | 1338 | struct inode *inode = mapping->host; |
7479d2b9 | 1339 | int ret, needed_blocks = ext4_writepage_trans_blocks(inode); |
ac27a0ec DK |
1340 | handle_t *handle; |
1341 | int retries = 0; | |
af5bc92d | 1342 | struct page *page; |
bfc1af65 | 1343 | pgoff_t index; |
af5bc92d | 1344 | unsigned from, to; |
bfc1af65 NP |
1345 | |
1346 | index = pos >> PAGE_CACHE_SHIFT; | |
af5bc92d TT |
1347 | from = pos & (PAGE_CACHE_SIZE - 1); |
1348 | to = from + len; | |
ac27a0ec DK |
1349 | |
1350 | retry: | |
af5bc92d TT |
1351 | handle = ext4_journal_start(inode, needed_blocks); |
1352 | if (IS_ERR(handle)) { | |
1353 | ret = PTR_ERR(handle); | |
1354 | goto out; | |
7479d2b9 | 1355 | } |
ac27a0ec | 1356 | |
54566b2c | 1357 | page = grab_cache_page_write_begin(mapping, index, flags); |
cf108bca JK |
1358 | if (!page) { |
1359 | ext4_journal_stop(handle); | |
1360 | ret = -ENOMEM; | |
1361 | goto out; | |
1362 | } | |
1363 | *pagep = page; | |
1364 | ||
bfc1af65 NP |
1365 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
1366 | ext4_get_block); | |
1367 | ||
1368 | if (!ret && ext4_should_journal_data(inode)) { | |
ac27a0ec DK |
1369 | ret = walk_page_buffers(handle, page_buffers(page), |
1370 | from, to, NULL, do_journal_get_write_access); | |
1371 | } | |
bfc1af65 NP |
1372 | |
1373 | if (ret) { | |
af5bc92d | 1374 | unlock_page(page); |
cf108bca | 1375 | ext4_journal_stop(handle); |
af5bc92d | 1376 | page_cache_release(page); |
ae4d5372 AK |
1377 | /* |
1378 | * block_write_begin may have instantiated a few blocks | |
1379 | * outside i_size. Trim these off again. Don't need | |
1380 | * i_size_read because we hold i_mutex. | |
1381 | */ | |
1382 | if (pos + len > inode->i_size) | |
1383 | vmtruncate(inode, inode->i_size); | |
bfc1af65 NP |
1384 | } |
1385 | ||
617ba13b | 1386 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
ac27a0ec | 1387 | goto retry; |
7479d2b9 | 1388 | out: |
ac27a0ec DK |
1389 | return ret; |
1390 | } | |
1391 | ||
bfc1af65 NP |
1392 | /* For write_end() in data=journal mode */ |
1393 | static int write_end_fn(handle_t *handle, struct buffer_head *bh) | |
ac27a0ec DK |
1394 | { |
1395 | if (!buffer_mapped(bh) || buffer_freed(bh)) | |
1396 | return 0; | |
1397 | set_buffer_uptodate(bh); | |
617ba13b | 1398 | return ext4_journal_dirty_metadata(handle, bh); |
ac27a0ec DK |
1399 | } |
1400 | ||
1401 | /* | |
1402 | * We need to pick up the new inode size which generic_commit_write gave us | |
1403 | * `file' can be NULL - eg, when called from page_symlink(). | |
1404 | * | |
617ba13b | 1405 | * ext4 never places buffers on inode->i_mapping->private_list. metadata |
ac27a0ec DK |
1406 | * buffers are managed internally. |
1407 | */ | |
bfc1af65 NP |
1408 | static int ext4_ordered_write_end(struct file *file, |
1409 | struct address_space *mapping, | |
1410 | loff_t pos, unsigned len, unsigned copied, | |
1411 | struct page *page, void *fsdata) | |
ac27a0ec | 1412 | { |
617ba13b | 1413 | handle_t *handle = ext4_journal_current_handle(); |
cf108bca | 1414 | struct inode *inode = mapping->host; |
ac27a0ec DK |
1415 | int ret = 0, ret2; |
1416 | ||
678aaf48 | 1417 | ret = ext4_jbd2_file_inode(handle, inode); |
ac27a0ec DK |
1418 | |
1419 | if (ret == 0) { | |
ac27a0ec DK |
1420 | loff_t new_i_size; |
1421 | ||
bfc1af65 | 1422 | new_i_size = pos + copied; |
cf17fea6 AK |
1423 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
1424 | ext4_update_i_disksize(inode, new_i_size); | |
1425 | /* We need to mark inode dirty even if | |
1426 | * new_i_size is less that inode->i_size | |
1427 | * bu greater than i_disksize.(hint delalloc) | |
1428 | */ | |
1429 | ext4_mark_inode_dirty(handle, inode); | |
1430 | } | |
1431 | ||
cf108bca | 1432 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
bfc1af65 | 1433 | page, fsdata); |
f8a87d89 RK |
1434 | copied = ret2; |
1435 | if (ret2 < 0) | |
1436 | ret = ret2; | |
ac27a0ec | 1437 | } |
617ba13b | 1438 | ret2 = ext4_journal_stop(handle); |
ac27a0ec DK |
1439 | if (!ret) |
1440 | ret = ret2; | |
bfc1af65 NP |
1441 | |
1442 | return ret ? ret : copied; | |
ac27a0ec DK |
1443 | } |
1444 | ||
bfc1af65 NP |
1445 | static int ext4_writeback_write_end(struct file *file, |
1446 | struct address_space *mapping, | |
1447 | loff_t pos, unsigned len, unsigned copied, | |
1448 | struct page *page, void *fsdata) | |
ac27a0ec | 1449 | { |
617ba13b | 1450 | handle_t *handle = ext4_journal_current_handle(); |
cf108bca | 1451 | struct inode *inode = mapping->host; |
ac27a0ec DK |
1452 | int ret = 0, ret2; |
1453 | loff_t new_i_size; | |
1454 | ||
bfc1af65 | 1455 | new_i_size = pos + copied; |
cf17fea6 AK |
1456 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
1457 | ext4_update_i_disksize(inode, new_i_size); | |
1458 | /* We need to mark inode dirty even if | |
1459 | * new_i_size is less that inode->i_size | |
1460 | * bu greater than i_disksize.(hint delalloc) | |
1461 | */ | |
1462 | ext4_mark_inode_dirty(handle, inode); | |
1463 | } | |
ac27a0ec | 1464 | |
cf108bca | 1465 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
bfc1af65 | 1466 | page, fsdata); |
f8a87d89 RK |
1467 | copied = ret2; |
1468 | if (ret2 < 0) | |
1469 | ret = ret2; | |
ac27a0ec | 1470 | |
617ba13b | 1471 | ret2 = ext4_journal_stop(handle); |
ac27a0ec DK |
1472 | if (!ret) |
1473 | ret = ret2; | |
bfc1af65 NP |
1474 | |
1475 | return ret ? ret : copied; | |
ac27a0ec DK |
1476 | } |
1477 | ||
bfc1af65 NP |
1478 | static int ext4_journalled_write_end(struct file *file, |
1479 | struct address_space *mapping, | |
1480 | loff_t pos, unsigned len, unsigned copied, | |
1481 | struct page *page, void *fsdata) | |
ac27a0ec | 1482 | { |
617ba13b | 1483 | handle_t *handle = ext4_journal_current_handle(); |
bfc1af65 | 1484 | struct inode *inode = mapping->host; |
ac27a0ec DK |
1485 | int ret = 0, ret2; |
1486 | int partial = 0; | |
bfc1af65 | 1487 | unsigned from, to; |
cf17fea6 | 1488 | loff_t new_i_size; |
ac27a0ec | 1489 | |
bfc1af65 NP |
1490 | from = pos & (PAGE_CACHE_SIZE - 1); |
1491 | to = from + len; | |
1492 | ||
1493 | if (copied < len) { | |
1494 | if (!PageUptodate(page)) | |
1495 | copied = 0; | |
1496 | page_zero_new_buffers(page, from+copied, to); | |
1497 | } | |
ac27a0ec DK |
1498 | |
1499 | ret = walk_page_buffers(handle, page_buffers(page), from, | |
bfc1af65 | 1500 | to, &partial, write_end_fn); |
ac27a0ec DK |
1501 | if (!partial) |
1502 | SetPageUptodate(page); | |
cf17fea6 AK |
1503 | new_i_size = pos + copied; |
1504 | if (new_i_size > inode->i_size) | |
bfc1af65 | 1505 | i_size_write(inode, pos+copied); |
617ba13b | 1506 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; |
cf17fea6 AK |
1507 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
1508 | ext4_update_i_disksize(inode, new_i_size); | |
617ba13b | 1509 | ret2 = ext4_mark_inode_dirty(handle, inode); |
ac27a0ec DK |
1510 | if (!ret) |
1511 | ret = ret2; | |
1512 | } | |
bfc1af65 | 1513 | |
cf108bca | 1514 | unlock_page(page); |
617ba13b | 1515 | ret2 = ext4_journal_stop(handle); |
ac27a0ec DK |
1516 | if (!ret) |
1517 | ret = ret2; | |
bfc1af65 NP |
1518 | page_cache_release(page); |
1519 | ||
1520 | return ret ? ret : copied; | |
ac27a0ec | 1521 | } |
d2a17637 MC |
1522 | |
1523 | static int ext4_da_reserve_space(struct inode *inode, int nrblocks) | |
1524 | { | |
030ba6bc | 1525 | int retries = 0; |
d2a17637 MC |
1526 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
1527 | unsigned long md_needed, mdblocks, total = 0; | |
1528 | ||
1529 | /* | |
1530 | * recalculate the amount of metadata blocks to reserve | |
1531 | * in order to allocate nrblocks | |
1532 | * worse case is one extent per block | |
1533 | */ | |
030ba6bc | 1534 | repeat: |
d2a17637 MC |
1535 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
1536 | total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; | |
1537 | mdblocks = ext4_calc_metadata_amount(inode, total); | |
1538 | BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); | |
1539 | ||
1540 | md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; | |
1541 | total = md_needed + nrblocks; | |
1542 | ||
a30d542a | 1543 | if (ext4_claim_free_blocks(sbi, total)) { |
d2a17637 | 1544 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
030ba6bc AK |
1545 | if (ext4_should_retry_alloc(inode->i_sb, &retries)) { |
1546 | yield(); | |
1547 | goto repeat; | |
1548 | } | |
d2a17637 MC |
1549 | return -ENOSPC; |
1550 | } | |
d2a17637 MC |
1551 | EXT4_I(inode)->i_reserved_data_blocks += nrblocks; |
1552 | EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; | |
1553 | ||
1554 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | |
1555 | return 0; /* success */ | |
1556 | } | |
1557 | ||
12219aea | 1558 | static void ext4_da_release_space(struct inode *inode, int to_free) |
d2a17637 MC |
1559 | { |
1560 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1561 | int total, mdb, mdb_free, release; | |
1562 | ||
cd213226 MC |
1563 | if (!to_free) |
1564 | return; /* Nothing to release, exit */ | |
1565 | ||
d2a17637 | 1566 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
cd213226 MC |
1567 | |
1568 | if (!EXT4_I(inode)->i_reserved_data_blocks) { | |
1569 | /* | |
1570 | * if there is no reserved blocks, but we try to free some | |
1571 | * then the counter is messed up somewhere. | |
1572 | * but since this function is called from invalidate | |
1573 | * page, it's harmless to return without any action | |
1574 | */ | |
1575 | printk(KERN_INFO "ext4 delalloc try to release %d reserved " | |
1576 | "blocks for inode %lu, but there is no reserved " | |
1577 | "data blocks\n", to_free, inode->i_ino); | |
1578 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | |
1579 | return; | |
1580 | } | |
1581 | ||
d2a17637 | 1582 | /* recalculate the number of metablocks still need to be reserved */ |
12219aea | 1583 | total = EXT4_I(inode)->i_reserved_data_blocks - to_free; |
d2a17637 MC |
1584 | mdb = ext4_calc_metadata_amount(inode, total); |
1585 | ||
1586 | /* figure out how many metablocks to release */ | |
1587 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); | |
1588 | mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; | |
1589 | ||
d2a17637 MC |
1590 | release = to_free + mdb_free; |
1591 | ||
6bc6e63f AK |
1592 | /* update fs dirty blocks counter for truncate case */ |
1593 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); | |
d2a17637 MC |
1594 | |
1595 | /* update per-inode reservations */ | |
12219aea AK |
1596 | BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); |
1597 | EXT4_I(inode)->i_reserved_data_blocks -= to_free; | |
d2a17637 MC |
1598 | |
1599 | BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); | |
1600 | EXT4_I(inode)->i_reserved_meta_blocks = mdb; | |
d2a17637 MC |
1601 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
1602 | } | |
1603 | ||
1604 | static void ext4_da_page_release_reservation(struct page *page, | |
1605 | unsigned long offset) | |
1606 | { | |
1607 | int to_release = 0; | |
1608 | struct buffer_head *head, *bh; | |
1609 | unsigned int curr_off = 0; | |
1610 | ||
1611 | head = page_buffers(page); | |
1612 | bh = head; | |
1613 | do { | |
1614 | unsigned int next_off = curr_off + bh->b_size; | |
1615 | ||
1616 | if ((offset <= curr_off) && (buffer_delay(bh))) { | |
1617 | to_release++; | |
1618 | clear_buffer_delay(bh); | |
1619 | } | |
1620 | curr_off = next_off; | |
1621 | } while ((bh = bh->b_this_page) != head); | |
12219aea | 1622 | ext4_da_release_space(page->mapping->host, to_release); |
d2a17637 | 1623 | } |
ac27a0ec | 1624 | |
64769240 AT |
1625 | /* |
1626 | * Delayed allocation stuff | |
1627 | */ | |
1628 | ||
1629 | struct mpage_da_data { | |
1630 | struct inode *inode; | |
1631 | struct buffer_head lbh; /* extent of blocks */ | |
1632 | unsigned long first_page, next_page; /* extent of pages */ | |
1633 | get_block_t *get_block; | |
1634 | struct writeback_control *wbc; | |
a1d6cc56 AK |
1635 | int io_done; |
1636 | long pages_written; | |
df22291f | 1637 | int retval; |
64769240 AT |
1638 | }; |
1639 | ||
1640 | /* | |
1641 | * mpage_da_submit_io - walks through extent of pages and try to write | |
a1d6cc56 | 1642 | * them with writepage() call back |
64769240 AT |
1643 | * |
1644 | * @mpd->inode: inode | |
1645 | * @mpd->first_page: first page of the extent | |
1646 | * @mpd->next_page: page after the last page of the extent | |
1647 | * @mpd->get_block: the filesystem's block mapper function | |
1648 | * | |
1649 | * By the time mpage_da_submit_io() is called we expect all blocks | |
1650 | * to be allocated. this may be wrong if allocation failed. | |
1651 | * | |
1652 | * As pages are already locked by write_cache_pages(), we can't use it | |
1653 | */ | |
1654 | static int mpage_da_submit_io(struct mpage_da_data *mpd) | |
1655 | { | |
1656 | struct address_space *mapping = mpd->inode->i_mapping; | |
64769240 AT |
1657 | int ret = 0, err, nr_pages, i; |
1658 | unsigned long index, end; | |
1659 | struct pagevec pvec; | |
22208ded | 1660 | long pages_skipped; |
64769240 AT |
1661 | |
1662 | BUG_ON(mpd->next_page <= mpd->first_page); | |
64769240 AT |
1663 | pagevec_init(&pvec, 0); |
1664 | index = mpd->first_page; | |
1665 | end = mpd->next_page - 1; | |
1666 | ||
1667 | while (index <= end) { | |
af6f029d AK |
1668 | /* |
1669 | * We can use PAGECACHE_TAG_DIRTY lookup here because | |
1670 | * even though we have cleared the dirty flag on the page | |
1671 | * We still keep the page in the radix tree with tag | |
1672 | * PAGECACHE_TAG_DIRTY. See clear_page_dirty_for_io. | |
1673 | * The PAGECACHE_TAG_DIRTY is cleared in set_page_writeback | |
1674 | * which is called via the below writepage callback. | |
1675 | */ | |
1676 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
1677 | PAGECACHE_TAG_DIRTY, | |
1678 | min(end - index, | |
1679 | (pgoff_t)PAGEVEC_SIZE-1) + 1); | |
64769240 AT |
1680 | if (nr_pages == 0) |
1681 | break; | |
1682 | for (i = 0; i < nr_pages; i++) { | |
1683 | struct page *page = pvec.pages[i]; | |
1684 | ||
22208ded | 1685 | pages_skipped = mpd->wbc->pages_skipped; |
a1d6cc56 | 1686 | err = mapping->a_ops->writepage(page, mpd->wbc); |
22208ded AK |
1687 | if (!err && (pages_skipped == mpd->wbc->pages_skipped)) |
1688 | /* | |
1689 | * have successfully written the page | |
1690 | * without skipping the same | |
1691 | */ | |
a1d6cc56 | 1692 | mpd->pages_written++; |
64769240 AT |
1693 | /* |
1694 | * In error case, we have to continue because | |
1695 | * remaining pages are still locked | |
1696 | * XXX: unlock and re-dirty them? | |
1697 | */ | |
1698 | if (ret == 0) | |
1699 | ret = err; | |
1700 | } | |
1701 | pagevec_release(&pvec); | |
1702 | } | |
64769240 AT |
1703 | return ret; |
1704 | } | |
1705 | ||
1706 | /* | |
1707 | * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers | |
1708 | * | |
1709 | * @mpd->inode - inode to walk through | |
1710 | * @exbh->b_blocknr - first block on a disk | |
1711 | * @exbh->b_size - amount of space in bytes | |
1712 | * @logical - first logical block to start assignment with | |
1713 | * | |
1714 | * the function goes through all passed space and put actual disk | |
1715 | * block numbers into buffer heads, dropping BH_Delay | |
1716 | */ | |
1717 | static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, | |
1718 | struct buffer_head *exbh) | |
1719 | { | |
1720 | struct inode *inode = mpd->inode; | |
1721 | struct address_space *mapping = inode->i_mapping; | |
1722 | int blocks = exbh->b_size >> inode->i_blkbits; | |
1723 | sector_t pblock = exbh->b_blocknr, cur_logical; | |
1724 | struct buffer_head *head, *bh; | |
a1d6cc56 | 1725 | pgoff_t index, end; |
64769240 AT |
1726 | struct pagevec pvec; |
1727 | int nr_pages, i; | |
1728 | ||
1729 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1730 | end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1731 | cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1732 | ||
1733 | pagevec_init(&pvec, 0); | |
1734 | ||
1735 | while (index <= end) { | |
1736 | /* XXX: optimize tail */ | |
1737 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); | |
1738 | if (nr_pages == 0) | |
1739 | break; | |
1740 | for (i = 0; i < nr_pages; i++) { | |
1741 | struct page *page = pvec.pages[i]; | |
1742 | ||
1743 | index = page->index; | |
1744 | if (index > end) | |
1745 | break; | |
1746 | index++; | |
1747 | ||
1748 | BUG_ON(!PageLocked(page)); | |
1749 | BUG_ON(PageWriteback(page)); | |
1750 | BUG_ON(!page_has_buffers(page)); | |
1751 | ||
1752 | bh = page_buffers(page); | |
1753 | head = bh; | |
1754 | ||
1755 | /* skip blocks out of the range */ | |
1756 | do { | |
1757 | if (cur_logical >= logical) | |
1758 | break; | |
1759 | cur_logical++; | |
1760 | } while ((bh = bh->b_this_page) != head); | |
1761 | ||
1762 | do { | |
1763 | if (cur_logical >= logical + blocks) | |
1764 | break; | |
64769240 AT |
1765 | if (buffer_delay(bh)) { |
1766 | bh->b_blocknr = pblock; | |
1767 | clear_buffer_delay(bh); | |
bf068ee2 AK |
1768 | bh->b_bdev = inode->i_sb->s_bdev; |
1769 | } else if (buffer_unwritten(bh)) { | |
1770 | bh->b_blocknr = pblock; | |
1771 | clear_buffer_unwritten(bh); | |
1772 | set_buffer_mapped(bh); | |
1773 | set_buffer_new(bh); | |
1774 | bh->b_bdev = inode->i_sb->s_bdev; | |
61628a3f | 1775 | } else if (buffer_mapped(bh)) |
64769240 | 1776 | BUG_ON(bh->b_blocknr != pblock); |
64769240 AT |
1777 | |
1778 | cur_logical++; | |
1779 | pblock++; | |
1780 | } while ((bh = bh->b_this_page) != head); | |
1781 | } | |
1782 | pagevec_release(&pvec); | |
1783 | } | |
1784 | } | |
1785 | ||
1786 | ||
1787 | /* | |
1788 | * __unmap_underlying_blocks - just a helper function to unmap | |
1789 | * set of blocks described by @bh | |
1790 | */ | |
1791 | static inline void __unmap_underlying_blocks(struct inode *inode, | |
1792 | struct buffer_head *bh) | |
1793 | { | |
1794 | struct block_device *bdev = inode->i_sb->s_bdev; | |
1795 | int blocks, i; | |
1796 | ||
1797 | blocks = bh->b_size >> inode->i_blkbits; | |
1798 | for (i = 0; i < blocks; i++) | |
1799 | unmap_underlying_metadata(bdev, bh->b_blocknr + i); | |
1800 | } | |
1801 | ||
c4a0c46e AK |
1802 | static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, |
1803 | sector_t logical, long blk_cnt) | |
1804 | { | |
1805 | int nr_pages, i; | |
1806 | pgoff_t index, end; | |
1807 | struct pagevec pvec; | |
1808 | struct inode *inode = mpd->inode; | |
1809 | struct address_space *mapping = inode->i_mapping; | |
1810 | ||
1811 | index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1812 | end = (logical + blk_cnt - 1) >> | |
1813 | (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1814 | while (index <= end) { | |
1815 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); | |
1816 | if (nr_pages == 0) | |
1817 | break; | |
1818 | for (i = 0; i < nr_pages; i++) { | |
1819 | struct page *page = pvec.pages[i]; | |
1820 | index = page->index; | |
1821 | if (index > end) | |
1822 | break; | |
1823 | index++; | |
1824 | ||
1825 | BUG_ON(!PageLocked(page)); | |
1826 | BUG_ON(PageWriteback(page)); | |
1827 | block_invalidatepage(page, 0); | |
1828 | ClearPageUptodate(page); | |
1829 | unlock_page(page); | |
1830 | } | |
1831 | } | |
1832 | return; | |
1833 | } | |
1834 | ||
df22291f AK |
1835 | static void ext4_print_free_blocks(struct inode *inode) |
1836 | { | |
1837 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1838 | printk(KERN_EMERG "Total free blocks count %lld\n", | |
1839 | ext4_count_free_blocks(inode->i_sb)); | |
1840 | printk(KERN_EMERG "Free/Dirty block details\n"); | |
1841 | printk(KERN_EMERG "free_blocks=%lld\n", | |
8f72fbdf | 1842 | (long long)percpu_counter_sum(&sbi->s_freeblocks_counter)); |
df22291f | 1843 | printk(KERN_EMERG "dirty_blocks=%lld\n", |
8f72fbdf | 1844 | (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter)); |
df22291f AK |
1845 | printk(KERN_EMERG "Block reservation details\n"); |
1846 | printk(KERN_EMERG "i_reserved_data_blocks=%lu\n", | |
1847 | EXT4_I(inode)->i_reserved_data_blocks); | |
1848 | printk(KERN_EMERG "i_reserved_meta_blocks=%lu\n", | |
1849 | EXT4_I(inode)->i_reserved_meta_blocks); | |
1850 | return; | |
1851 | } | |
1852 | ||
64769240 AT |
1853 | /* |
1854 | * mpage_da_map_blocks - go through given space | |
1855 | * | |
1856 | * @mpd->lbh - bh describing space | |
1857 | * @mpd->get_block - the filesystem's block mapper function | |
1858 | * | |
1859 | * The function skips space we know is already mapped to disk blocks. | |
1860 | * | |
64769240 | 1861 | */ |
c4a0c46e | 1862 | static int mpage_da_map_blocks(struct mpage_da_data *mpd) |
64769240 | 1863 | { |
a1d6cc56 | 1864 | int err = 0; |
030ba6bc | 1865 | struct buffer_head new; |
64769240 | 1866 | struct buffer_head *lbh = &mpd->lbh; |
df22291f | 1867 | sector_t next; |
64769240 AT |
1868 | |
1869 | /* | |
1870 | * We consider only non-mapped and non-allocated blocks | |
1871 | */ | |
1872 | if (buffer_mapped(lbh) && !buffer_delay(lbh)) | |
c4a0c46e | 1873 | return 0; |
a1d6cc56 AK |
1874 | new.b_state = lbh->b_state; |
1875 | new.b_blocknr = 0; | |
1876 | new.b_size = lbh->b_size; | |
df22291f | 1877 | next = lbh->b_blocknr; |
a1d6cc56 AK |
1878 | /* |
1879 | * If we didn't accumulate anything | |
1880 | * to write simply return | |
1881 | */ | |
1882 | if (!new.b_size) | |
c4a0c46e | 1883 | return 0; |
a1d6cc56 | 1884 | err = mpd->get_block(mpd->inode, next, &new, 1); |
c4a0c46e AK |
1885 | if (err) { |
1886 | ||
1887 | /* If get block returns with error | |
1888 | * we simply return. Later writepage | |
1889 | * will redirty the page and writepages | |
1890 | * will find the dirty page again | |
1891 | */ | |
1892 | if (err == -EAGAIN) | |
1893 | return 0; | |
df22291f AK |
1894 | |
1895 | if (err == -ENOSPC && | |
1896 | ext4_count_free_blocks(mpd->inode->i_sb)) { | |
1897 | mpd->retval = err; | |
1898 | return 0; | |
1899 | } | |
1900 | ||
c4a0c46e AK |
1901 | /* |
1902 | * get block failure will cause us | |
1903 | * to loop in writepages. Because | |
1904 | * a_ops->writepage won't be able to | |
1905 | * make progress. The page will be redirtied | |
1906 | * by writepage and writepages will again | |
1907 | * try to write the same. | |
1908 | */ | |
1909 | printk(KERN_EMERG "%s block allocation failed for inode %lu " | |
1910 | "at logical offset %llu with max blocks " | |
1911 | "%zd with error %d\n", | |
1912 | __func__, mpd->inode->i_ino, | |
1913 | (unsigned long long)next, | |
1914 | lbh->b_size >> mpd->inode->i_blkbits, err); | |
1915 | printk(KERN_EMERG "This should not happen.!! " | |
1916 | "Data will be lost\n"); | |
030ba6bc | 1917 | if (err == -ENOSPC) { |
df22291f | 1918 | ext4_print_free_blocks(mpd->inode); |
030ba6bc | 1919 | } |
c4a0c46e AK |
1920 | /* invlaidate all the pages */ |
1921 | ext4_da_block_invalidatepages(mpd, next, | |
1922 | lbh->b_size >> mpd->inode->i_blkbits); | |
1923 | return err; | |
1924 | } | |
a1d6cc56 | 1925 | BUG_ON(new.b_size == 0); |
64769240 | 1926 | |
a1d6cc56 AK |
1927 | if (buffer_new(&new)) |
1928 | __unmap_underlying_blocks(mpd->inode, &new); | |
64769240 | 1929 | |
a1d6cc56 AK |
1930 | /* |
1931 | * If blocks are delayed marked, we need to | |
1932 | * put actual blocknr and drop delayed bit | |
1933 | */ | |
1934 | if (buffer_delay(lbh) || buffer_unwritten(lbh)) | |
1935 | mpage_put_bnr_to_bhs(mpd, next, &new); | |
64769240 | 1936 | |
c4a0c46e | 1937 | return 0; |
64769240 AT |
1938 | } |
1939 | ||
bf068ee2 AK |
1940 | #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ |
1941 | (1 << BH_Delay) | (1 << BH_Unwritten)) | |
64769240 AT |
1942 | |
1943 | /* | |
1944 | * mpage_add_bh_to_extent - try to add one more block to extent of blocks | |
1945 | * | |
1946 | * @mpd->lbh - extent of blocks | |
1947 | * @logical - logical number of the block in the file | |
1948 | * @bh - bh of the block (used to access block's state) | |
1949 | * | |
1950 | * the function is used to collect contig. blocks in same state | |
1951 | */ | |
1952 | static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, | |
1953 | sector_t logical, struct buffer_head *bh) | |
1954 | { | |
64769240 | 1955 | sector_t next; |
525f4ed8 MC |
1956 | size_t b_size = bh->b_size; |
1957 | struct buffer_head *lbh = &mpd->lbh; | |
1958 | int nrblocks = lbh->b_size >> mpd->inode->i_blkbits; | |
64769240 | 1959 | |
525f4ed8 MC |
1960 | /* check if thereserved journal credits might overflow */ |
1961 | if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { | |
1962 | if (nrblocks >= EXT4_MAX_TRANS_DATA) { | |
1963 | /* | |
1964 | * With non-extent format we are limited by the journal | |
1965 | * credit available. Total credit needed to insert | |
1966 | * nrblocks contiguous blocks is dependent on the | |
1967 | * nrblocks. So limit nrblocks. | |
1968 | */ | |
1969 | goto flush_it; | |
1970 | } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > | |
1971 | EXT4_MAX_TRANS_DATA) { | |
1972 | /* | |
1973 | * Adding the new buffer_head would make it cross the | |
1974 | * allowed limit for which we have journal credit | |
1975 | * reserved. So limit the new bh->b_size | |
1976 | */ | |
1977 | b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << | |
1978 | mpd->inode->i_blkbits; | |
1979 | /* we will do mpage_da_submit_io in the next loop */ | |
1980 | } | |
1981 | } | |
64769240 AT |
1982 | /* |
1983 | * First block in the extent | |
1984 | */ | |
1985 | if (lbh->b_size == 0) { | |
1986 | lbh->b_blocknr = logical; | |
525f4ed8 | 1987 | lbh->b_size = b_size; |
64769240 AT |
1988 | lbh->b_state = bh->b_state & BH_FLAGS; |
1989 | return; | |
1990 | } | |
1991 | ||
525f4ed8 | 1992 | next = lbh->b_blocknr + nrblocks; |
64769240 AT |
1993 | /* |
1994 | * Can we merge the block to our big extent? | |
1995 | */ | |
1996 | if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) { | |
525f4ed8 | 1997 | lbh->b_size += b_size; |
64769240 AT |
1998 | return; |
1999 | } | |
2000 | ||
525f4ed8 | 2001 | flush_it: |
64769240 AT |
2002 | /* |
2003 | * We couldn't merge the block to our extent, so we | |
2004 | * need to flush current extent and start new one | |
2005 | */ | |
c4a0c46e AK |
2006 | if (mpage_da_map_blocks(mpd) == 0) |
2007 | mpage_da_submit_io(mpd); | |
a1d6cc56 AK |
2008 | mpd->io_done = 1; |
2009 | return; | |
64769240 AT |
2010 | } |
2011 | ||
2012 | /* | |
2013 | * __mpage_da_writepage - finds extent of pages and blocks | |
2014 | * | |
2015 | * @page: page to consider | |
2016 | * @wbc: not used, we just follow rules | |
2017 | * @data: context | |
2018 | * | |
2019 | * The function finds extents of pages and scan them for all blocks. | |
2020 | */ | |
2021 | static int __mpage_da_writepage(struct page *page, | |
2022 | struct writeback_control *wbc, void *data) | |
2023 | { | |
2024 | struct mpage_da_data *mpd = data; | |
2025 | struct inode *inode = mpd->inode; | |
2026 | struct buffer_head *bh, *head, fake; | |
2027 | sector_t logical; | |
2028 | ||
a1d6cc56 AK |
2029 | if (mpd->io_done) { |
2030 | /* | |
2031 | * Rest of the page in the page_vec | |
2032 | * redirty then and skip then. We will | |
2033 | * try to to write them again after | |
2034 | * starting a new transaction | |
2035 | */ | |
2036 | redirty_page_for_writepage(wbc, page); | |
2037 | unlock_page(page); | |
2038 | return MPAGE_DA_EXTENT_TAIL; | |
2039 | } | |
64769240 AT |
2040 | /* |
2041 | * Can we merge this page to current extent? | |
2042 | */ | |
2043 | if (mpd->next_page != page->index) { | |
2044 | /* | |
2045 | * Nope, we can't. So, we map non-allocated blocks | |
a1d6cc56 | 2046 | * and start IO on them using writepage() |
64769240 AT |
2047 | */ |
2048 | if (mpd->next_page != mpd->first_page) { | |
c4a0c46e AK |
2049 | if (mpage_da_map_blocks(mpd) == 0) |
2050 | mpage_da_submit_io(mpd); | |
a1d6cc56 AK |
2051 | /* |
2052 | * skip rest of the page in the page_vec | |
2053 | */ | |
2054 | mpd->io_done = 1; | |
2055 | redirty_page_for_writepage(wbc, page); | |
2056 | unlock_page(page); | |
2057 | return MPAGE_DA_EXTENT_TAIL; | |
64769240 AT |
2058 | } |
2059 | ||
2060 | /* | |
2061 | * Start next extent of pages ... | |
2062 | */ | |
2063 | mpd->first_page = page->index; | |
2064 | ||
2065 | /* | |
2066 | * ... and blocks | |
2067 | */ | |
2068 | mpd->lbh.b_size = 0; | |
2069 | mpd->lbh.b_state = 0; | |
2070 | mpd->lbh.b_blocknr = 0; | |
2071 | } | |
2072 | ||
2073 | mpd->next_page = page->index + 1; | |
2074 | logical = (sector_t) page->index << | |
2075 | (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
2076 | ||
2077 | if (!page_has_buffers(page)) { | |
2078 | /* | |
2079 | * There is no attached buffer heads yet (mmap?) | |
2080 | * we treat the page asfull of dirty blocks | |
2081 | */ | |
2082 | bh = &fake; | |
2083 | bh->b_size = PAGE_CACHE_SIZE; | |
2084 | bh->b_state = 0; | |
2085 | set_buffer_dirty(bh); | |
2086 | set_buffer_uptodate(bh); | |
2087 | mpage_add_bh_to_extent(mpd, logical, bh); | |
a1d6cc56 AK |
2088 | if (mpd->io_done) |
2089 | return MPAGE_DA_EXTENT_TAIL; | |
64769240 AT |
2090 | } else { |
2091 | /* | |
2092 | * Page with regular buffer heads, just add all dirty ones | |
2093 | */ | |
2094 | head = page_buffers(page); | |
2095 | bh = head; | |
2096 | do { | |
2097 | BUG_ON(buffer_locked(bh)); | |
a1d6cc56 AK |
2098 | if (buffer_dirty(bh) && |
2099 | (!buffer_mapped(bh) || buffer_delay(bh))) { | |
64769240 | 2100 | mpage_add_bh_to_extent(mpd, logical, bh); |
a1d6cc56 AK |
2101 | if (mpd->io_done) |
2102 | return MPAGE_DA_EXTENT_TAIL; | |
2103 | } | |
64769240 AT |
2104 | logical++; |
2105 | } while ((bh = bh->b_this_page) != head); | |
2106 | } | |
2107 | ||
2108 | return 0; | |
2109 | } | |
2110 | ||
2111 | /* | |
2112 | * mpage_da_writepages - walk the list of dirty pages of the given | |
2113 | * address space, allocates non-allocated blocks, maps newly-allocated | |
2114 | * blocks to existing bhs and issue IO them | |
2115 | * | |
2116 | * @mapping: address space structure to write | |
2117 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write | |
2118 | * @get_block: the filesystem's block mapper function. | |
2119 | * | |
2120 | * This is a library function, which implements the writepages() | |
2121 | * address_space_operation. | |
64769240 AT |
2122 | */ |
2123 | static int mpage_da_writepages(struct address_space *mapping, | |
2124 | struct writeback_control *wbc, | |
df22291f | 2125 | struct mpage_da_data *mpd) |
64769240 | 2126 | { |
64769240 AT |
2127 | int ret; |
2128 | ||
df22291f | 2129 | if (!mpd->get_block) |
64769240 AT |
2130 | return generic_writepages(mapping, wbc); |
2131 | ||
df22291f AK |
2132 | mpd->lbh.b_size = 0; |
2133 | mpd->lbh.b_state = 0; | |
2134 | mpd->lbh.b_blocknr = 0; | |
2135 | mpd->first_page = 0; | |
2136 | mpd->next_page = 0; | |
2137 | mpd->io_done = 0; | |
2138 | mpd->pages_written = 0; | |
2139 | mpd->retval = 0; | |
a1d6cc56 | 2140 | |
df22291f | 2141 | ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, mpd); |
64769240 AT |
2142 | /* |
2143 | * Handle last extent of pages | |
2144 | */ | |
df22291f AK |
2145 | if (!mpd->io_done && mpd->next_page != mpd->first_page) { |
2146 | if (mpage_da_map_blocks(mpd) == 0) | |
2147 | mpage_da_submit_io(mpd); | |
64769240 | 2148 | |
22208ded AK |
2149 | mpd->io_done = 1; |
2150 | ret = MPAGE_DA_EXTENT_TAIL; | |
2151 | } | |
2152 | wbc->nr_to_write -= mpd->pages_written; | |
64769240 AT |
2153 | return ret; |
2154 | } | |
2155 | ||
2156 | /* | |
2157 | * this is a special callback for ->write_begin() only | |
2158 | * it's intention is to return mapped block or reserve space | |
2159 | */ | |
2160 | static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, | |
2161 | struct buffer_head *bh_result, int create) | |
2162 | { | |
2163 | int ret = 0; | |
2164 | ||
2165 | BUG_ON(create == 0); | |
2166 | BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); | |
2167 | ||
2168 | /* | |
2169 | * first, we need to know whether the block is allocated already | |
2170 | * preallocated blocks are unmapped but should treated | |
2171 | * the same as allocated blocks. | |
2172 | */ | |
d2a17637 MC |
2173 | ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1, bh_result, 0, 0, 0); |
2174 | if ((ret == 0) && !buffer_delay(bh_result)) { | |
2175 | /* the block isn't (pre)allocated yet, let's reserve space */ | |
64769240 AT |
2176 | /* |
2177 | * XXX: __block_prepare_write() unmaps passed block, | |
2178 | * is it OK? | |
2179 | */ | |
d2a17637 MC |
2180 | ret = ext4_da_reserve_space(inode, 1); |
2181 | if (ret) | |
2182 | /* not enough space to reserve */ | |
2183 | return ret; | |
2184 | ||
64769240 AT |
2185 | map_bh(bh_result, inode->i_sb, 0); |
2186 | set_buffer_new(bh_result); | |
2187 | set_buffer_delay(bh_result); | |
2188 | } else if (ret > 0) { | |
2189 | bh_result->b_size = (ret << inode->i_blkbits); | |
2190 | ret = 0; | |
2191 | } | |
2192 | ||
2193 | return ret; | |
2194 | } | |
d2a17637 | 2195 | #define EXT4_DELALLOC_RSVED 1 |
64769240 AT |
2196 | static int ext4_da_get_block_write(struct inode *inode, sector_t iblock, |
2197 | struct buffer_head *bh_result, int create) | |
2198 | { | |
61628a3f | 2199 | int ret; |
64769240 AT |
2200 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; |
2201 | loff_t disksize = EXT4_I(inode)->i_disksize; | |
2202 | handle_t *handle = NULL; | |
2203 | ||
61628a3f | 2204 | handle = ext4_journal_current_handle(); |
166348dd AK |
2205 | BUG_ON(!handle); |
2206 | ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks, | |
2207 | bh_result, create, 0, EXT4_DELALLOC_RSVED); | |
64769240 | 2208 | if (ret > 0) { |
166348dd | 2209 | |
64769240 AT |
2210 | bh_result->b_size = (ret << inode->i_blkbits); |
2211 | ||
166348dd AK |
2212 | if (ext4_should_order_data(inode)) { |
2213 | int retval; | |
2214 | retval = ext4_jbd2_file_inode(handle, inode); | |
2215 | if (retval) | |
2216 | /* | |
2217 | * Failed to add inode for ordered | |
2218 | * mode. Don't update file size | |
2219 | */ | |
2220 | return retval; | |
2221 | } | |
2222 | ||
64769240 AT |
2223 | /* |
2224 | * Update on-disk size along with block allocation | |
2225 | * we don't use 'extend_disksize' as size may change | |
2226 | * within already allocated block -bzzz | |
2227 | */ | |
2228 | disksize = ((loff_t) iblock + ret) << inode->i_blkbits; | |
2229 | if (disksize > i_size_read(inode)) | |
2230 | disksize = i_size_read(inode); | |
2231 | if (disksize > EXT4_I(inode)->i_disksize) { | |
cf17fea6 AK |
2232 | ext4_update_i_disksize(inode, disksize); |
2233 | ret = ext4_mark_inode_dirty(handle, inode); | |
2234 | return ret; | |
64769240 | 2235 | } |
64769240 AT |
2236 | ret = 0; |
2237 | } | |
64769240 AT |
2238 | return ret; |
2239 | } | |
61628a3f MC |
2240 | |
2241 | static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh) | |
2242 | { | |
f0e6c985 AK |
2243 | /* |
2244 | * unmapped buffer is possible for holes. | |
2245 | * delay buffer is possible with delayed allocation | |
2246 | */ | |
2247 | return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh)); | |
2248 | } | |
2249 | ||
2250 | static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock, | |
2251 | struct buffer_head *bh_result, int create) | |
2252 | { | |
2253 | int ret = 0; | |
2254 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; | |
2255 | ||
2256 | /* | |
2257 | * we don't want to do block allocation in writepage | |
2258 | * so call get_block_wrap with create = 0 | |
2259 | */ | |
2260 | ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks, | |
2261 | bh_result, 0, 0, 0); | |
2262 | if (ret > 0) { | |
2263 | bh_result->b_size = (ret << inode->i_blkbits); | |
2264 | ret = 0; | |
2265 | } | |
2266 | return ret; | |
61628a3f MC |
2267 | } |
2268 | ||
61628a3f | 2269 | /* |
f0e6c985 AK |
2270 | * get called vi ext4_da_writepages after taking page lock (have journal handle) |
2271 | * get called via journal_submit_inode_data_buffers (no journal handle) | |
2272 | * get called via shrink_page_list via pdflush (no journal handle) | |
2273 | * or grab_page_cache when doing write_begin (have journal handle) | |
61628a3f | 2274 | */ |
64769240 AT |
2275 | static int ext4_da_writepage(struct page *page, |
2276 | struct writeback_control *wbc) | |
2277 | { | |
64769240 | 2278 | int ret = 0; |
61628a3f MC |
2279 | loff_t size; |
2280 | unsigned long len; | |
61628a3f MC |
2281 | struct buffer_head *page_bufs; |
2282 | struct inode *inode = page->mapping->host; | |
2283 | ||
f0e6c985 AK |
2284 | size = i_size_read(inode); |
2285 | if (page->index == size >> PAGE_CACHE_SHIFT) | |
2286 | len = size & ~PAGE_CACHE_MASK; | |
2287 | else | |
2288 | len = PAGE_CACHE_SIZE; | |
64769240 | 2289 | |
f0e6c985 | 2290 | if (page_has_buffers(page)) { |
61628a3f | 2291 | page_bufs = page_buffers(page); |
f0e6c985 AK |
2292 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
2293 | ext4_bh_unmapped_or_delay)) { | |
61628a3f | 2294 | /* |
f0e6c985 AK |
2295 | * We don't want to do block allocation |
2296 | * So redirty the page and return | |
cd1aac32 AK |
2297 | * We may reach here when we do a journal commit |
2298 | * via journal_submit_inode_data_buffers. | |
2299 | * If we don't have mapping block we just ignore | |
f0e6c985 AK |
2300 | * them. We can also reach here via shrink_page_list |
2301 | */ | |
2302 | redirty_page_for_writepage(wbc, page); | |
2303 | unlock_page(page); | |
2304 | return 0; | |
2305 | } | |
2306 | } else { | |
2307 | /* | |
2308 | * The test for page_has_buffers() is subtle: | |
2309 | * We know the page is dirty but it lost buffers. That means | |
2310 | * that at some moment in time after write_begin()/write_end() | |
2311 | * has been called all buffers have been clean and thus they | |
2312 | * must have been written at least once. So they are all | |
2313 | * mapped and we can happily proceed with mapping them | |
2314 | * and writing the page. | |
2315 | * | |
2316 | * Try to initialize the buffer_heads and check whether | |
2317 | * all are mapped and non delay. We don't want to | |
2318 | * do block allocation here. | |
2319 | */ | |
2320 | ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, | |
2321 | ext4_normal_get_block_write); | |
2322 | if (!ret) { | |
2323 | page_bufs = page_buffers(page); | |
2324 | /* check whether all are mapped and non delay */ | |
2325 | if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, | |
2326 | ext4_bh_unmapped_or_delay)) { | |
2327 | redirty_page_for_writepage(wbc, page); | |
2328 | unlock_page(page); | |
2329 | return 0; | |
2330 | } | |
2331 | } else { | |
2332 | /* | |
2333 | * We can't do block allocation here | |
2334 | * so just redity the page and unlock | |
2335 | * and return | |
61628a3f | 2336 | */ |
61628a3f MC |
2337 | redirty_page_for_writepage(wbc, page); |
2338 | unlock_page(page); | |
2339 | return 0; | |
2340 | } | |
ed9b3e33 AK |
2341 | /* now mark the buffer_heads as dirty and uptodate */ |
2342 | block_commit_write(page, 0, PAGE_CACHE_SIZE); | |
64769240 AT |
2343 | } |
2344 | ||
2345 | if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) | |
f0e6c985 | 2346 | ret = nobh_writepage(page, ext4_normal_get_block_write, wbc); |
64769240 | 2347 | else |
f0e6c985 AK |
2348 | ret = block_write_full_page(page, |
2349 | ext4_normal_get_block_write, | |
2350 | wbc); | |
64769240 | 2351 | |
64769240 AT |
2352 | return ret; |
2353 | } | |
2354 | ||
61628a3f | 2355 | /* |
525f4ed8 MC |
2356 | * This is called via ext4_da_writepages() to |
2357 | * calulate the total number of credits to reserve to fit | |
2358 | * a single extent allocation into a single transaction, | |
2359 | * ext4_da_writpeages() will loop calling this before | |
2360 | * the block allocation. | |
61628a3f | 2361 | */ |
525f4ed8 MC |
2362 | |
2363 | static int ext4_da_writepages_trans_blocks(struct inode *inode) | |
2364 | { | |
2365 | int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; | |
2366 | ||
2367 | /* | |
2368 | * With non-extent format the journal credit needed to | |
2369 | * insert nrblocks contiguous block is dependent on | |
2370 | * number of contiguous block. So we will limit | |
2371 | * number of contiguous block to a sane value | |
2372 | */ | |
2373 | if (!(inode->i_flags & EXT4_EXTENTS_FL) && | |
2374 | (max_blocks > EXT4_MAX_TRANS_DATA)) | |
2375 | max_blocks = EXT4_MAX_TRANS_DATA; | |
2376 | ||
2377 | return ext4_chunk_trans_blocks(inode, max_blocks); | |
2378 | } | |
61628a3f | 2379 | |
64769240 | 2380 | static int ext4_da_writepages(struct address_space *mapping, |
a1d6cc56 | 2381 | struct writeback_control *wbc) |
64769240 | 2382 | { |
22208ded AK |
2383 | pgoff_t index; |
2384 | int range_whole = 0; | |
61628a3f | 2385 | handle_t *handle = NULL; |
df22291f | 2386 | struct mpage_da_data mpd; |
5e745b04 | 2387 | struct inode *inode = mapping->host; |
22208ded AK |
2388 | int no_nrwrite_index_update; |
2389 | long pages_written = 0, pages_skipped; | |
5e745b04 | 2390 | int needed_blocks, ret = 0, nr_to_writebump = 0; |
5e745b04 | 2391 | struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); |
61628a3f MC |
2392 | |
2393 | /* | |
2394 | * No pages to write? This is mainly a kludge to avoid starting | |
2395 | * a transaction for special inodes like journal inode on last iput() | |
2396 | * because that could violate lock ordering on umount | |
2397 | */ | |
a1d6cc56 | 2398 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
61628a3f | 2399 | return 0; |
5e745b04 AK |
2400 | /* |
2401 | * Make sure nr_to_write is >= sbi->s_mb_stream_request | |
2402 | * This make sure small files blocks are allocated in | |
2403 | * single attempt. This ensure that small files | |
2404 | * get less fragmented. | |
2405 | */ | |
2406 | if (wbc->nr_to_write < sbi->s_mb_stream_request) { | |
2407 | nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write; | |
2408 | wbc->nr_to_write = sbi->s_mb_stream_request; | |
2409 | } | |
22208ded AK |
2410 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
2411 | range_whole = 1; | |
61628a3f | 2412 | |
22208ded AK |
2413 | if (wbc->range_cyclic) |
2414 | index = mapping->writeback_index; | |
2415 | else | |
2416 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | |
a1d6cc56 | 2417 | |
df22291f AK |
2418 | mpd.wbc = wbc; |
2419 | mpd.inode = mapping->host; | |
2420 | ||
22208ded AK |
2421 | /* |
2422 | * we don't want write_cache_pages to update | |
2423 | * nr_to_write and writeback_index | |
2424 | */ | |
2425 | no_nrwrite_index_update = wbc->no_nrwrite_index_update; | |
2426 | wbc->no_nrwrite_index_update = 1; | |
2427 | pages_skipped = wbc->pages_skipped; | |
2428 | ||
2429 | while (!ret && wbc->nr_to_write > 0) { | |
a1d6cc56 AK |
2430 | |
2431 | /* | |
2432 | * we insert one extent at a time. So we need | |
2433 | * credit needed for single extent allocation. | |
2434 | * journalled mode is currently not supported | |
2435 | * by delalloc | |
2436 | */ | |
2437 | BUG_ON(ext4_should_journal_data(inode)); | |
525f4ed8 | 2438 | needed_blocks = ext4_da_writepages_trans_blocks(inode); |
a1d6cc56 | 2439 | |
61628a3f MC |
2440 | /* start a new transaction*/ |
2441 | handle = ext4_journal_start(inode, needed_blocks); | |
2442 | if (IS_ERR(handle)) { | |
2443 | ret = PTR_ERR(handle); | |
a1d6cc56 AK |
2444 | printk(KERN_EMERG "%s: jbd2_start: " |
2445 | "%ld pages, ino %lu; err %d\n", __func__, | |
2446 | wbc->nr_to_write, inode->i_ino, ret); | |
2447 | dump_stack(); | |
61628a3f MC |
2448 | goto out_writepages; |
2449 | } | |
df22291f AK |
2450 | mpd.get_block = ext4_da_get_block_write; |
2451 | ret = mpage_da_writepages(mapping, wbc, &mpd); | |
2452 | ||
61628a3f | 2453 | ext4_journal_stop(handle); |
df22291f | 2454 | |
22208ded AK |
2455 | if (mpd.retval == -ENOSPC) { |
2456 | /* commit the transaction which would | |
2457 | * free blocks released in the transaction | |
2458 | * and try again | |
2459 | */ | |
df22291f | 2460 | jbd2_journal_force_commit_nested(sbi->s_journal); |
22208ded AK |
2461 | wbc->pages_skipped = pages_skipped; |
2462 | ret = 0; | |
2463 | } else if (ret == MPAGE_DA_EXTENT_TAIL) { | |
a1d6cc56 AK |
2464 | /* |
2465 | * got one extent now try with | |
2466 | * rest of the pages | |
2467 | */ | |
22208ded AK |
2468 | pages_written += mpd.pages_written; |
2469 | wbc->pages_skipped = pages_skipped; | |
a1d6cc56 | 2470 | ret = 0; |
22208ded | 2471 | } else if (wbc->nr_to_write) |
61628a3f MC |
2472 | /* |
2473 | * There is no more writeout needed | |
2474 | * or we requested for a noblocking writeout | |
2475 | * and we found the device congested | |
2476 | */ | |
61628a3f | 2477 | break; |
a1d6cc56 | 2478 | } |
22208ded AK |
2479 | if (pages_skipped != wbc->pages_skipped) |
2480 | printk(KERN_EMERG "This should not happen leaving %s " | |
2481 | "with nr_to_write = %ld ret = %d\n", | |
2482 | __func__, wbc->nr_to_write, ret); | |
2483 | ||
2484 | /* Update index */ | |
2485 | index += pages_written; | |
2486 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | |
2487 | /* | |
2488 | * set the writeback_index so that range_cyclic | |
2489 | * mode will write it back later | |
2490 | */ | |
2491 | mapping->writeback_index = index; | |
a1d6cc56 | 2492 | |
61628a3f | 2493 | out_writepages: |
22208ded AK |
2494 | if (!no_nrwrite_index_update) |
2495 | wbc->no_nrwrite_index_update = 0; | |
2496 | wbc->nr_to_write -= nr_to_writebump; | |
61628a3f | 2497 | return ret; |
64769240 AT |
2498 | } |
2499 | ||
79f0be8d AK |
2500 | #define FALL_BACK_TO_NONDELALLOC 1 |
2501 | static int ext4_nonda_switch(struct super_block *sb) | |
2502 | { | |
2503 | s64 free_blocks, dirty_blocks; | |
2504 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
2505 | ||
2506 | /* | |
2507 | * switch to non delalloc mode if we are running low | |
2508 | * on free block. The free block accounting via percpu | |
2509 | * counters can get slightly wrong with FBC_BATCH getting | |
2510 | * accumulated on each CPU without updating global counters | |
2511 | * Delalloc need an accurate free block accounting. So switch | |
2512 | * to non delalloc when we are near to error range. | |
2513 | */ | |
2514 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | |
2515 | dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); | |
2516 | if (2 * free_blocks < 3 * dirty_blocks || | |
2517 | free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { | |
2518 | /* | |
2519 | * free block count is less that 150% of dirty blocks | |
2520 | * or free blocks is less that watermark | |
2521 | */ | |
2522 | return 1; | |
2523 | } | |
2524 | return 0; | |
2525 | } | |
2526 | ||
64769240 AT |
2527 | static int ext4_da_write_begin(struct file *file, struct address_space *mapping, |
2528 | loff_t pos, unsigned len, unsigned flags, | |
2529 | struct page **pagep, void **fsdata) | |
2530 | { | |
d2a17637 | 2531 | int ret, retries = 0; |
64769240 AT |
2532 | struct page *page; |
2533 | pgoff_t index; | |
2534 | unsigned from, to; | |
2535 | struct inode *inode = mapping->host; | |
2536 | handle_t *handle; | |
2537 | ||
2538 | index = pos >> PAGE_CACHE_SHIFT; | |
2539 | from = pos & (PAGE_CACHE_SIZE - 1); | |
2540 | to = from + len; | |
79f0be8d AK |
2541 | |
2542 | if (ext4_nonda_switch(inode->i_sb)) { | |
2543 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; | |
2544 | return ext4_write_begin(file, mapping, pos, | |
2545 | len, flags, pagep, fsdata); | |
2546 | } | |
2547 | *fsdata = (void *)0; | |
d2a17637 | 2548 | retry: |
64769240 AT |
2549 | /* |
2550 | * With delayed allocation, we don't log the i_disksize update | |
2551 | * if there is delayed block allocation. But we still need | |
2552 | * to journalling the i_disksize update if writes to the end | |
2553 | * of file which has an already mapped buffer. | |
2554 | */ | |
2555 | handle = ext4_journal_start(inode, 1); | |
2556 | if (IS_ERR(handle)) { | |
2557 | ret = PTR_ERR(handle); | |
2558 | goto out; | |
2559 | } | |
2560 | ||
54566b2c | 2561 | page = grab_cache_page_write_begin(mapping, index, flags); |
d5a0d4f7 ES |
2562 | if (!page) { |
2563 | ext4_journal_stop(handle); | |
2564 | ret = -ENOMEM; | |
2565 | goto out; | |
2566 | } | |
64769240 AT |
2567 | *pagep = page; |
2568 | ||
2569 | ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, | |
2570 | ext4_da_get_block_prep); | |
2571 | if (ret < 0) { | |
2572 | unlock_page(page); | |
2573 | ext4_journal_stop(handle); | |
2574 | page_cache_release(page); | |
ae4d5372 AK |
2575 | /* |
2576 | * block_write_begin may have instantiated a few blocks | |
2577 | * outside i_size. Trim these off again. Don't need | |
2578 | * i_size_read because we hold i_mutex. | |
2579 | */ | |
2580 | if (pos + len > inode->i_size) | |
2581 | vmtruncate(inode, inode->i_size); | |
64769240 AT |
2582 | } |
2583 | ||
d2a17637 MC |
2584 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
2585 | goto retry; | |
64769240 AT |
2586 | out: |
2587 | return ret; | |
2588 | } | |
2589 | ||
632eaeab MC |
2590 | /* |
2591 | * Check if we should update i_disksize | |
2592 | * when write to the end of file but not require block allocation | |
2593 | */ | |
2594 | static int ext4_da_should_update_i_disksize(struct page *page, | |
2595 | unsigned long offset) | |
2596 | { | |
2597 | struct buffer_head *bh; | |
2598 | struct inode *inode = page->mapping->host; | |
2599 | unsigned int idx; | |
2600 | int i; | |
2601 | ||
2602 | bh = page_buffers(page); | |
2603 | idx = offset >> inode->i_blkbits; | |
2604 | ||
af5bc92d | 2605 | for (i = 0; i < idx; i++) |
632eaeab MC |
2606 | bh = bh->b_this_page; |
2607 | ||
2608 | if (!buffer_mapped(bh) || (buffer_delay(bh))) | |
2609 | return 0; | |
2610 | return 1; | |
2611 | } | |
2612 | ||
64769240 AT |
2613 | static int ext4_da_write_end(struct file *file, |
2614 | struct address_space *mapping, | |
2615 | loff_t pos, unsigned len, unsigned copied, | |
2616 | struct page *page, void *fsdata) | |
2617 | { | |
2618 | struct inode *inode = mapping->host; | |
2619 | int ret = 0, ret2; | |
2620 | handle_t *handle = ext4_journal_current_handle(); | |
2621 | loff_t new_i_size; | |
632eaeab | 2622 | unsigned long start, end; |
79f0be8d AK |
2623 | int write_mode = (int)(unsigned long)fsdata; |
2624 | ||
2625 | if (write_mode == FALL_BACK_TO_NONDELALLOC) { | |
2626 | if (ext4_should_order_data(inode)) { | |
2627 | return ext4_ordered_write_end(file, mapping, pos, | |
2628 | len, copied, page, fsdata); | |
2629 | } else if (ext4_should_writeback_data(inode)) { | |
2630 | return ext4_writeback_write_end(file, mapping, pos, | |
2631 | len, copied, page, fsdata); | |
2632 | } else { | |
2633 | BUG(); | |
2634 | } | |
2635 | } | |
632eaeab MC |
2636 | |
2637 | start = pos & (PAGE_CACHE_SIZE - 1); | |
af5bc92d | 2638 | end = start + copied - 1; |
64769240 AT |
2639 | |
2640 | /* | |
2641 | * generic_write_end() will run mark_inode_dirty() if i_size | |
2642 | * changes. So let's piggyback the i_disksize mark_inode_dirty | |
2643 | * into that. | |
2644 | */ | |
2645 | ||
2646 | new_i_size = pos + copied; | |
632eaeab MC |
2647 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
2648 | if (ext4_da_should_update_i_disksize(page, end)) { | |
2649 | down_write(&EXT4_I(inode)->i_data_sem); | |
2650 | if (new_i_size > EXT4_I(inode)->i_disksize) { | |
2651 | /* | |
2652 | * Updating i_disksize when extending file | |
2653 | * without needing block allocation | |
2654 | */ | |
2655 | if (ext4_should_order_data(inode)) | |
2656 | ret = ext4_jbd2_file_inode(handle, | |
2657 | inode); | |
64769240 | 2658 | |
632eaeab MC |
2659 | EXT4_I(inode)->i_disksize = new_i_size; |
2660 | } | |
2661 | up_write(&EXT4_I(inode)->i_data_sem); | |
cf17fea6 AK |
2662 | /* We need to mark inode dirty even if |
2663 | * new_i_size is less that inode->i_size | |
2664 | * bu greater than i_disksize.(hint delalloc) | |
2665 | */ | |
2666 | ext4_mark_inode_dirty(handle, inode); | |
64769240 | 2667 | } |
632eaeab | 2668 | } |
64769240 AT |
2669 | ret2 = generic_write_end(file, mapping, pos, len, copied, |
2670 | page, fsdata); | |
2671 | copied = ret2; | |
2672 | if (ret2 < 0) | |
2673 | ret = ret2; | |
2674 | ret2 = ext4_journal_stop(handle); | |
2675 | if (!ret) | |
2676 | ret = ret2; | |
2677 | ||
2678 | return ret ? ret : copied; | |
2679 | } | |
2680 | ||
2681 | static void ext4_da_invalidatepage(struct page *page, unsigned long offset) | |
2682 | { | |
64769240 AT |
2683 | /* |
2684 | * Drop reserved blocks | |
2685 | */ | |
2686 | BUG_ON(!PageLocked(page)); | |
2687 | if (!page_has_buffers(page)) | |
2688 | goto out; | |
2689 | ||
d2a17637 | 2690 | ext4_da_page_release_reservation(page, offset); |
64769240 AT |
2691 | |
2692 | out: | |
2693 | ext4_invalidatepage(page, offset); | |
2694 | ||
2695 | return; | |
2696 | } | |
2697 | ||
2698 | ||
ac27a0ec DK |
2699 | /* |
2700 | * bmap() is special. It gets used by applications such as lilo and by | |
2701 | * the swapper to find the on-disk block of a specific piece of data. | |
2702 | * | |
2703 | * Naturally, this is dangerous if the block concerned is still in the | |
617ba13b | 2704 | * journal. If somebody makes a swapfile on an ext4 data-journaling |
ac27a0ec DK |
2705 | * filesystem and enables swap, then they may get a nasty shock when the |
2706 | * data getting swapped to that swapfile suddenly gets overwritten by | |
2707 | * the original zero's written out previously to the journal and | |
2708 | * awaiting writeback in the kernel's buffer cache. | |
2709 | * | |
2710 | * So, if we see any bmap calls here on a modified, data-journaled file, | |
2711 | * take extra steps to flush any blocks which might be in the cache. | |
2712 | */ | |
617ba13b | 2713 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) |
ac27a0ec DK |
2714 | { |
2715 | struct inode *inode = mapping->host; | |
2716 | journal_t *journal; | |
2717 | int err; | |
2718 | ||
64769240 AT |
2719 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && |
2720 | test_opt(inode->i_sb, DELALLOC)) { | |
2721 | /* | |
2722 | * With delalloc we want to sync the file | |
2723 | * so that we can make sure we allocate | |
2724 | * blocks for file | |
2725 | */ | |
2726 | filemap_write_and_wait(mapping); | |
2727 | } | |
2728 | ||
617ba13b | 2729 | if (EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { |
ac27a0ec DK |
2730 | /* |
2731 | * This is a REALLY heavyweight approach, but the use of | |
2732 | * bmap on dirty files is expected to be extremely rare: | |
2733 | * only if we run lilo or swapon on a freshly made file | |
2734 | * do we expect this to happen. | |
2735 | * | |
2736 | * (bmap requires CAP_SYS_RAWIO so this does not | |
2737 | * represent an unprivileged user DOS attack --- we'd be | |
2738 | * in trouble if mortal users could trigger this path at | |
2739 | * will.) | |
2740 | * | |
617ba13b | 2741 | * NB. EXT4_STATE_JDATA is not set on files other than |
ac27a0ec DK |
2742 | * regular files. If somebody wants to bmap a directory |
2743 | * or symlink and gets confused because the buffer | |
2744 | * hasn't yet been flushed to disk, they deserve | |
2745 | * everything they get. | |
2746 | */ | |
2747 | ||
617ba13b MC |
2748 | EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; |
2749 | journal = EXT4_JOURNAL(inode); | |
dab291af MC |
2750 | jbd2_journal_lock_updates(journal); |
2751 | err = jbd2_journal_flush(journal); | |
2752 | jbd2_journal_unlock_updates(journal); | |
ac27a0ec DK |
2753 | |
2754 | if (err) | |
2755 | return 0; | |
2756 | } | |
2757 | ||
af5bc92d | 2758 | return generic_block_bmap(mapping, block, ext4_get_block); |
ac27a0ec DK |
2759 | } |
2760 | ||
2761 | static int bget_one(handle_t *handle, struct buffer_head *bh) | |
2762 | { | |
2763 | get_bh(bh); | |
2764 | return 0; | |
2765 | } | |
2766 | ||
2767 | static int bput_one(handle_t *handle, struct buffer_head *bh) | |
2768 | { | |
2769 | put_bh(bh); | |
2770 | return 0; | |
2771 | } | |
2772 | ||
ac27a0ec | 2773 | /* |
678aaf48 JK |
2774 | * Note that we don't need to start a transaction unless we're journaling data |
2775 | * because we should have holes filled from ext4_page_mkwrite(). We even don't | |
2776 | * need to file the inode to the transaction's list in ordered mode because if | |
2777 | * we are writing back data added by write(), the inode is already there and if | |
2778 | * we are writing back data modified via mmap(), noone guarantees in which | |
2779 | * transaction the data will hit the disk. In case we are journaling data, we | |
2780 | * cannot start transaction directly because transaction start ranks above page | |
2781 | * lock so we have to do some magic. | |
ac27a0ec | 2782 | * |
678aaf48 | 2783 | * In all journaling modes block_write_full_page() will start the I/O. |
ac27a0ec DK |
2784 | * |
2785 | * Problem: | |
2786 | * | |
617ba13b MC |
2787 | * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> |
2788 | * ext4_writepage() | |
ac27a0ec DK |
2789 | * |
2790 | * Similar for: | |
2791 | * | |
617ba13b | 2792 | * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ... |
ac27a0ec | 2793 | * |
617ba13b | 2794 | * Same applies to ext4_get_block(). We will deadlock on various things like |
0e855ac8 | 2795 | * lock_journal and i_data_sem |
ac27a0ec DK |
2796 | * |
2797 | * Setting PF_MEMALLOC here doesn't work - too many internal memory | |
2798 | * allocations fail. | |
2799 | * | |
2800 | * 16May01: If we're reentered then journal_current_handle() will be | |
2801 | * non-zero. We simply *return*. | |
2802 | * | |
2803 | * 1 July 2001: @@@ FIXME: | |
2804 | * In journalled data mode, a data buffer may be metadata against the | |
2805 | * current transaction. But the same file is part of a shared mapping | |
2806 | * and someone does a writepage() on it. | |
2807 | * | |
2808 | * We will move the buffer onto the async_data list, but *after* it has | |
2809 | * been dirtied. So there's a small window where we have dirty data on | |
2810 | * BJ_Metadata. | |
2811 | * | |
2812 | * Note that this only applies to the last partial page in the file. The | |
2813 | * bit which block_write_full_page() uses prepare/commit for. (That's | |
2814 | * broken code anyway: it's wrong for msync()). | |
2815 | * | |
2816 | * It's a rare case: affects the final partial page, for journalled data | |
2817 | * where the file is subject to bith write() and writepage() in the same | |
2818 | * transction. To fix it we'll need a custom block_write_full_page(). | |
2819 | * We'll probably need that anyway for journalling writepage() output. | |
2820 | * | |
2821 | * We don't honour synchronous mounts for writepage(). That would be | |
2822 | * disastrous. Any write() or metadata operation will sync the fs for | |
2823 | * us. | |
2824 | * | |
ac27a0ec | 2825 | */ |
678aaf48 | 2826 | static int __ext4_normal_writepage(struct page *page, |
cf108bca JK |
2827 | struct writeback_control *wbc) |
2828 | { | |
2829 | struct inode *inode = page->mapping->host; | |
2830 | ||
2831 | if (test_opt(inode->i_sb, NOBH)) | |
f0e6c985 AK |
2832 | return nobh_writepage(page, |
2833 | ext4_normal_get_block_write, wbc); | |
cf108bca | 2834 | else |
f0e6c985 AK |
2835 | return block_write_full_page(page, |
2836 | ext4_normal_get_block_write, | |
2837 | wbc); | |
cf108bca JK |
2838 | } |
2839 | ||
678aaf48 | 2840 | static int ext4_normal_writepage(struct page *page, |
ac27a0ec DK |
2841 | struct writeback_control *wbc) |
2842 | { | |
2843 | struct inode *inode = page->mapping->host; | |
cf108bca JK |
2844 | loff_t size = i_size_read(inode); |
2845 | loff_t len; | |
2846 | ||
2847 | J_ASSERT(PageLocked(page)); | |
cf108bca JK |
2848 | if (page->index == size >> PAGE_CACHE_SHIFT) |
2849 | len = size & ~PAGE_CACHE_MASK; | |
2850 | else | |
2851 | len = PAGE_CACHE_SIZE; | |
f0e6c985 AK |
2852 | |
2853 | if (page_has_buffers(page)) { | |
2854 | /* if page has buffers it should all be mapped | |
2855 | * and allocated. If there are not buffers attached | |
2856 | * to the page we know the page is dirty but it lost | |
2857 | * buffers. That means that at some moment in time | |
2858 | * after write_begin() / write_end() has been called | |
2859 | * all buffers have been clean and thus they must have been | |
2860 | * written at least once. So they are all mapped and we can | |
2861 | * happily proceed with mapping them and writing the page. | |
2862 | */ | |
2863 | BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, | |
2864 | ext4_bh_unmapped_or_delay)); | |
2865 | } | |
cf108bca JK |
2866 | |
2867 | if (!ext4_journal_current_handle()) | |
678aaf48 | 2868 | return __ext4_normal_writepage(page, wbc); |
cf108bca JK |
2869 | |
2870 | redirty_page_for_writepage(wbc, page); | |
2871 | unlock_page(page); | |
2872 | return 0; | |
2873 | } | |
2874 | ||
2875 | static int __ext4_journalled_writepage(struct page *page, | |
2876 | struct writeback_control *wbc) | |
2877 | { | |
2878 | struct address_space *mapping = page->mapping; | |
2879 | struct inode *inode = mapping->host; | |
2880 | struct buffer_head *page_bufs; | |
ac27a0ec DK |
2881 | handle_t *handle = NULL; |
2882 | int ret = 0; | |
2883 | int err; | |
2884 | ||
f0e6c985 AK |
2885 | ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, |
2886 | ext4_normal_get_block_write); | |
cf108bca JK |
2887 | if (ret != 0) |
2888 | goto out_unlock; | |
2889 | ||
2890 | page_bufs = page_buffers(page); | |
2891 | walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL, | |
2892 | bget_one); | |
2893 | /* As soon as we unlock the page, it can go away, but we have | |
2894 | * references to buffers so we are safe */ | |
2895 | unlock_page(page); | |
ac27a0ec | 2896 | |
617ba13b | 2897 | handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); |
ac27a0ec DK |
2898 | if (IS_ERR(handle)) { |
2899 | ret = PTR_ERR(handle); | |
cf108bca | 2900 | goto out; |
ac27a0ec DK |
2901 | } |
2902 | ||
cf108bca JK |
2903 | ret = walk_page_buffers(handle, page_bufs, 0, |
2904 | PAGE_CACHE_SIZE, NULL, do_journal_get_write_access); | |
ac27a0ec | 2905 | |
cf108bca JK |
2906 | err = walk_page_buffers(handle, page_bufs, 0, |
2907 | PAGE_CACHE_SIZE, NULL, write_end_fn); | |
2908 | if (ret == 0) | |
2909 | ret = err; | |
617ba13b | 2910 | err = ext4_journal_stop(handle); |
ac27a0ec DK |
2911 | if (!ret) |
2912 | ret = err; | |
ac27a0ec | 2913 | |
cf108bca JK |
2914 | walk_page_buffers(handle, page_bufs, 0, |
2915 | PAGE_CACHE_SIZE, NULL, bput_one); | |
2916 | EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; | |
2917 | goto out; | |
2918 | ||
2919 | out_unlock: | |
ac27a0ec | 2920 | unlock_page(page); |
cf108bca | 2921 | out: |
ac27a0ec DK |
2922 | return ret; |
2923 | } | |
2924 | ||
617ba13b | 2925 | static int ext4_journalled_writepage(struct page *page, |
ac27a0ec DK |
2926 | struct writeback_control *wbc) |
2927 | { | |
2928 | struct inode *inode = page->mapping->host; | |
cf108bca JK |
2929 | loff_t size = i_size_read(inode); |
2930 | loff_t len; | |
ac27a0ec | 2931 | |
cf108bca | 2932 | J_ASSERT(PageLocked(page)); |
cf108bca JK |
2933 | if (page->index == size >> PAGE_CACHE_SHIFT) |
2934 | len = size & ~PAGE_CACHE_MASK; | |
2935 | else | |
2936 | len = PAGE_CACHE_SIZE; | |
f0e6c985 AK |
2937 | |
2938 | if (page_has_buffers(page)) { | |
2939 | /* if page has buffers it should all be mapped | |
2940 | * and allocated. If there are not buffers attached | |
2941 | * to the page we know the page is dirty but it lost | |
2942 | * buffers. That means that at some moment in time | |
2943 | * after write_begin() / write_end() has been called | |
2944 | * all buffers have been clean and thus they must have been | |
2945 | * written at least once. So they are all mapped and we can | |
2946 | * happily proceed with mapping them and writing the page. | |
2947 | */ | |
2948 | BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, | |
2949 | ext4_bh_unmapped_or_delay)); | |
2950 | } | |
ac27a0ec | 2951 | |
cf108bca | 2952 | if (ext4_journal_current_handle()) |
ac27a0ec | 2953 | goto no_write; |
ac27a0ec | 2954 | |
cf108bca | 2955 | if (PageChecked(page)) { |
ac27a0ec DK |
2956 | /* |
2957 | * It's mmapped pagecache. Add buffers and journal it. There | |
2958 | * doesn't seem much point in redirtying the page here. | |
2959 | */ | |
2960 | ClearPageChecked(page); | |
cf108bca | 2961 | return __ext4_journalled_writepage(page, wbc); |
ac27a0ec DK |
2962 | } else { |
2963 | /* | |
2964 | * It may be a page full of checkpoint-mode buffers. We don't | |
2965 | * really know unless we go poke around in the buffer_heads. | |
2966 | * But block_write_full_page will do the right thing. | |
2967 | */ | |
f0e6c985 AK |
2968 | return block_write_full_page(page, |
2969 | ext4_normal_get_block_write, | |
2970 | wbc); | |
ac27a0ec | 2971 | } |
ac27a0ec DK |
2972 | no_write: |
2973 | redirty_page_for_writepage(wbc, page); | |
ac27a0ec | 2974 | unlock_page(page); |
cf108bca | 2975 | return 0; |
ac27a0ec DK |
2976 | } |
2977 | ||
617ba13b | 2978 | static int ext4_readpage(struct file *file, struct page *page) |
ac27a0ec | 2979 | { |
617ba13b | 2980 | return mpage_readpage(page, ext4_get_block); |
ac27a0ec DK |
2981 | } |
2982 | ||
2983 | static int | |
617ba13b | 2984 | ext4_readpages(struct file *file, struct address_space *mapping, |
ac27a0ec DK |
2985 | struct list_head *pages, unsigned nr_pages) |
2986 | { | |
617ba13b | 2987 | return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); |
ac27a0ec DK |
2988 | } |
2989 | ||
617ba13b | 2990 | static void ext4_invalidatepage(struct page *page, unsigned long offset) |
ac27a0ec | 2991 | { |
617ba13b | 2992 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
ac27a0ec DK |
2993 | |
2994 | /* | |
2995 | * If it's a full truncate we just forget about the pending dirtying | |
2996 | */ | |
2997 | if (offset == 0) | |
2998 | ClearPageChecked(page); | |
2999 | ||
dab291af | 3000 | jbd2_journal_invalidatepage(journal, page, offset); |
ac27a0ec DK |
3001 | } |
3002 | ||
617ba13b | 3003 | static int ext4_releasepage(struct page *page, gfp_t wait) |
ac27a0ec | 3004 | { |
617ba13b | 3005 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
ac27a0ec DK |
3006 | |
3007 | WARN_ON(PageChecked(page)); | |
3008 | if (!page_has_buffers(page)) | |
3009 | return 0; | |
dab291af | 3010 | return jbd2_journal_try_to_free_buffers(journal, page, wait); |
ac27a0ec DK |
3011 | } |
3012 | ||
3013 | /* | |
3014 | * If the O_DIRECT write will extend the file then add this inode to the | |
3015 | * orphan list. So recovery will truncate it back to the original size | |
3016 | * if the machine crashes during the write. | |
3017 | * | |
3018 | * If the O_DIRECT write is intantiating holes inside i_size and the machine | |
7fb5409d JK |
3019 | * crashes then stale disk data _may_ be exposed inside the file. But current |
3020 | * VFS code falls back into buffered path in that case so we are safe. | |
ac27a0ec | 3021 | */ |
617ba13b | 3022 | static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, |
ac27a0ec DK |
3023 | const struct iovec *iov, loff_t offset, |
3024 | unsigned long nr_segs) | |
3025 | { | |
3026 | struct file *file = iocb->ki_filp; | |
3027 | struct inode *inode = file->f_mapping->host; | |
617ba13b | 3028 | struct ext4_inode_info *ei = EXT4_I(inode); |
7fb5409d | 3029 | handle_t *handle; |
ac27a0ec DK |
3030 | ssize_t ret; |
3031 | int orphan = 0; | |
3032 | size_t count = iov_length(iov, nr_segs); | |
3033 | ||
3034 | if (rw == WRITE) { | |
3035 | loff_t final_size = offset + count; | |
3036 | ||
ac27a0ec | 3037 | if (final_size > inode->i_size) { |
7fb5409d JK |
3038 | /* Credits for sb + inode write */ |
3039 | handle = ext4_journal_start(inode, 2); | |
3040 | if (IS_ERR(handle)) { | |
3041 | ret = PTR_ERR(handle); | |
3042 | goto out; | |
3043 | } | |
617ba13b | 3044 | ret = ext4_orphan_add(handle, inode); |
7fb5409d JK |
3045 | if (ret) { |
3046 | ext4_journal_stop(handle); | |
3047 | goto out; | |
3048 | } | |
ac27a0ec DK |
3049 | orphan = 1; |
3050 | ei->i_disksize = inode->i_size; | |
7fb5409d | 3051 | ext4_journal_stop(handle); |
ac27a0ec DK |
3052 | } |
3053 | } | |
3054 | ||
3055 | ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | |
3056 | offset, nr_segs, | |
617ba13b | 3057 | ext4_get_block, NULL); |
ac27a0ec | 3058 | |
7fb5409d | 3059 | if (orphan) { |
ac27a0ec DK |
3060 | int err; |
3061 | ||
7fb5409d JK |
3062 | /* Credits for sb + inode write */ |
3063 | handle = ext4_journal_start(inode, 2); | |
3064 | if (IS_ERR(handle)) { | |
3065 | /* This is really bad luck. We've written the data | |
3066 | * but cannot extend i_size. Bail out and pretend | |
3067 | * the write failed... */ | |
3068 | ret = PTR_ERR(handle); | |
3069 | goto out; | |
3070 | } | |
3071 | if (inode->i_nlink) | |
617ba13b | 3072 | ext4_orphan_del(handle, inode); |
7fb5409d | 3073 | if (ret > 0) { |
ac27a0ec DK |
3074 | loff_t end = offset + ret; |
3075 | if (end > inode->i_size) { | |
3076 | ei->i_disksize = end; | |
3077 | i_size_write(inode, end); | |
3078 | /* | |
3079 | * We're going to return a positive `ret' | |
3080 | * here due to non-zero-length I/O, so there's | |
3081 | * no way of reporting error returns from | |
617ba13b | 3082 | * ext4_mark_inode_dirty() to userspace. So |
ac27a0ec DK |
3083 | * ignore it. |
3084 | */ | |
617ba13b | 3085 | ext4_mark_inode_dirty(handle, inode); |
ac27a0ec DK |
3086 | } |
3087 | } | |
617ba13b | 3088 | err = ext4_journal_stop(handle); |
ac27a0ec DK |
3089 | if (ret == 0) |
3090 | ret = err; | |
3091 | } | |
3092 | out: | |
3093 | return ret; | |
3094 | } | |
3095 | ||
3096 | /* | |
617ba13b | 3097 | * Pages can be marked dirty completely asynchronously from ext4's journalling |
ac27a0ec DK |
3098 | * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do |
3099 | * much here because ->set_page_dirty is called under VFS locks. The page is | |
3100 | * not necessarily locked. | |
3101 | * | |
3102 | * We cannot just dirty the page and leave attached buffers clean, because the | |
3103 | * buffers' dirty state is "definitive". We cannot just set the buffers dirty | |
3104 | * or jbddirty because all the journalling code will explode. | |
3105 | * | |
3106 | * So what we do is to mark the page "pending dirty" and next time writepage | |
3107 | * is called, propagate that into the buffers appropriately. | |
3108 | */ | |
617ba13b | 3109 | static int ext4_journalled_set_page_dirty(struct page *page) |
ac27a0ec DK |
3110 | { |
3111 | SetPageChecked(page); | |
3112 | return __set_page_dirty_nobuffers(page); | |
3113 | } | |
3114 | ||
617ba13b | 3115 | static const struct address_space_operations ext4_ordered_aops = { |
8ab22b9a HH |
3116 | .readpage = ext4_readpage, |
3117 | .readpages = ext4_readpages, | |
3118 | .writepage = ext4_normal_writepage, | |
3119 | .sync_page = block_sync_page, | |
3120 | .write_begin = ext4_write_begin, | |
3121 | .write_end = ext4_ordered_write_end, | |
3122 | .bmap = ext4_bmap, | |
3123 | .invalidatepage = ext4_invalidatepage, | |
3124 | .releasepage = ext4_releasepage, | |
3125 | .direct_IO = ext4_direct_IO, | |
3126 | .migratepage = buffer_migrate_page, | |
3127 | .is_partially_uptodate = block_is_partially_uptodate, | |
ac27a0ec DK |
3128 | }; |
3129 | ||
617ba13b | 3130 | static const struct address_space_operations ext4_writeback_aops = { |
8ab22b9a HH |
3131 | .readpage = ext4_readpage, |
3132 | .readpages = ext4_readpages, | |
3133 | .writepage = ext4_normal_writepage, | |
3134 | .sync_page = block_sync_page, | |
3135 | .write_begin = ext4_write_begin, | |
3136 | .write_end = ext4_writeback_write_end, | |
3137 | .bmap = ext4_bmap, | |
3138 | .invalidatepage = ext4_invalidatepage, | |
3139 | .releasepage = ext4_releasepage, | |
3140 | .direct_IO = ext4_direct_IO, | |
3141 | .migratepage = buffer_migrate_page, | |
3142 | .is_partially_uptodate = block_is_partially_uptodate, | |
ac27a0ec DK |
3143 | }; |
3144 | ||
617ba13b | 3145 | static const struct address_space_operations ext4_journalled_aops = { |
8ab22b9a HH |
3146 | .readpage = ext4_readpage, |
3147 | .readpages = ext4_readpages, | |
3148 | .writepage = ext4_journalled_writepage, | |
3149 | .sync_page = block_sync_page, | |
3150 | .write_begin = ext4_write_begin, | |
3151 | .write_end = ext4_journalled_write_end, | |
3152 | .set_page_dirty = ext4_journalled_set_page_dirty, | |
3153 | .bmap = ext4_bmap, | |
3154 | .invalidatepage = ext4_invalidatepage, | |
3155 | .releasepage = ext4_releasepage, | |
3156 | .is_partially_uptodate = block_is_partially_uptodate, | |
ac27a0ec DK |
3157 | }; |
3158 | ||
64769240 | 3159 | static const struct address_space_operations ext4_da_aops = { |
8ab22b9a HH |
3160 | .readpage = ext4_readpage, |
3161 | .readpages = ext4_readpages, | |
3162 | .writepage = ext4_da_writepage, | |
3163 | .writepages = ext4_da_writepages, | |
3164 | .sync_page = block_sync_page, | |
3165 | .write_begin = ext4_da_write_begin, | |
3166 | .write_end = ext4_da_write_end, | |
3167 | .bmap = ext4_bmap, | |
3168 | .invalidatepage = ext4_da_invalidatepage, | |
3169 | .releasepage = ext4_releasepage, | |
3170 | .direct_IO = ext4_direct_IO, | |
3171 | .migratepage = buffer_migrate_page, | |
3172 | .is_partially_uptodate = block_is_partially_uptodate, | |
64769240 AT |
3173 | }; |
3174 | ||
617ba13b | 3175 | void ext4_set_aops(struct inode *inode) |
ac27a0ec | 3176 | { |
cd1aac32 AK |
3177 | if (ext4_should_order_data(inode) && |
3178 | test_opt(inode->i_sb, DELALLOC)) | |
3179 | inode->i_mapping->a_ops = &ext4_da_aops; | |
3180 | else if (ext4_should_order_data(inode)) | |
617ba13b | 3181 | inode->i_mapping->a_ops = &ext4_ordered_aops; |
64769240 AT |
3182 | else if (ext4_should_writeback_data(inode) && |
3183 | test_opt(inode->i_sb, DELALLOC)) | |
3184 | inode->i_mapping->a_ops = &ext4_da_aops; | |
617ba13b MC |
3185 | else if (ext4_should_writeback_data(inode)) |
3186 | inode->i_mapping->a_ops = &ext4_writeback_aops; | |
ac27a0ec | 3187 | else |
617ba13b | 3188 | inode->i_mapping->a_ops = &ext4_journalled_aops; |
ac27a0ec DK |
3189 | } |
3190 | ||
3191 | /* | |
617ba13b | 3192 | * ext4_block_truncate_page() zeroes out a mapping from file offset `from' |
ac27a0ec DK |
3193 | * up to the end of the block which corresponds to `from'. |
3194 | * This required during truncate. We need to physically zero the tail end | |
3195 | * of that block so it doesn't yield old data if the file is later grown. | |
3196 | */ | |
cf108bca | 3197 | int ext4_block_truncate_page(handle_t *handle, |
ac27a0ec DK |
3198 | struct address_space *mapping, loff_t from) |
3199 | { | |
617ba13b | 3200 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; |
ac27a0ec | 3201 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
725d26d3 AK |
3202 | unsigned blocksize, length, pos; |
3203 | ext4_lblk_t iblock; | |
ac27a0ec DK |
3204 | struct inode *inode = mapping->host; |
3205 | struct buffer_head *bh; | |
cf108bca | 3206 | struct page *page; |
ac27a0ec | 3207 | int err = 0; |
ac27a0ec | 3208 | |
cf108bca JK |
3209 | page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT); |
3210 | if (!page) | |
3211 | return -EINVAL; | |
3212 | ||
ac27a0ec DK |
3213 | blocksize = inode->i_sb->s_blocksize; |
3214 | length = blocksize - (offset & (blocksize - 1)); | |
3215 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | |
3216 | ||
3217 | /* | |
3218 | * For "nobh" option, we can only work if we don't need to | |
3219 | * read-in the page - otherwise we create buffers to do the IO. | |
3220 | */ | |
3221 | if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && | |
617ba13b | 3222 | ext4_should_writeback_data(inode) && PageUptodate(page)) { |
eebd2aa3 | 3223 | zero_user(page, offset, length); |
ac27a0ec DK |
3224 | set_page_dirty(page); |
3225 | goto unlock; | |
3226 | } | |
3227 | ||
3228 | if (!page_has_buffers(page)) | |
3229 | create_empty_buffers(page, blocksize, 0); | |
3230 | ||
3231 | /* Find the buffer that contains "offset" */ | |
3232 | bh = page_buffers(page); | |
3233 | pos = blocksize; | |
3234 | while (offset >= pos) { | |
3235 | bh = bh->b_this_page; | |
3236 | iblock++; | |
3237 | pos += blocksize; | |
3238 | } | |
3239 | ||
3240 | err = 0; | |
3241 | if (buffer_freed(bh)) { | |
3242 | BUFFER_TRACE(bh, "freed: skip"); | |
3243 | goto unlock; | |
3244 | } | |
3245 | ||
3246 | if (!buffer_mapped(bh)) { | |
3247 | BUFFER_TRACE(bh, "unmapped"); | |
617ba13b | 3248 | ext4_get_block(inode, iblock, bh, 0); |
ac27a0ec DK |
3249 | /* unmapped? It's a hole - nothing to do */ |
3250 | if (!buffer_mapped(bh)) { | |
3251 | BUFFER_TRACE(bh, "still unmapped"); | |
3252 | goto unlock; | |
3253 | } | |
3254 | } | |
3255 | ||
3256 | /* Ok, it's mapped. Make sure it's up-to-date */ | |
3257 | if (PageUptodate(page)) | |
3258 | set_buffer_uptodate(bh); | |
3259 | ||
3260 | if (!buffer_uptodate(bh)) { | |
3261 | err = -EIO; | |
3262 | ll_rw_block(READ, 1, &bh); | |
3263 | wait_on_buffer(bh); | |
3264 | /* Uhhuh. Read error. Complain and punt. */ | |
3265 | if (!buffer_uptodate(bh)) | |
3266 | goto unlock; | |
3267 | } | |
3268 | ||
617ba13b | 3269 | if (ext4_should_journal_data(inode)) { |
ac27a0ec | 3270 | BUFFER_TRACE(bh, "get write access"); |
617ba13b | 3271 | err = ext4_journal_get_write_access(handle, bh); |
ac27a0ec DK |
3272 | if (err) |
3273 | goto unlock; | |
3274 | } | |
3275 | ||
eebd2aa3 | 3276 | zero_user(page, offset, length); |
ac27a0ec DK |
3277 | |
3278 | BUFFER_TRACE(bh, "zeroed end of block"); | |
3279 | ||
3280 | err = 0; | |
617ba13b MC |
3281 | if (ext4_should_journal_data(inode)) { |
3282 | err = ext4_journal_dirty_metadata(handle, bh); | |
ac27a0ec | 3283 | } else { |
617ba13b | 3284 | if (ext4_should_order_data(inode)) |
678aaf48 | 3285 | err = ext4_jbd2_file_inode(handle, inode); |
ac27a0ec DK |
3286 | mark_buffer_dirty(bh); |
3287 | } | |
3288 | ||
3289 | unlock: | |
3290 | unlock_page(page); | |
3291 | page_cache_release(page); | |
3292 | return err; | |
3293 | } | |
3294 | ||
3295 | /* | |
3296 | * Probably it should be a library function... search for first non-zero word | |
3297 | * or memcmp with zero_page, whatever is better for particular architecture. | |
3298 | * Linus? | |
3299 | */ | |
3300 | static inline int all_zeroes(__le32 *p, __le32 *q) | |
3301 | { | |
3302 | while (p < q) | |
3303 | if (*p++) | |
3304 | return 0; | |
3305 | return 1; | |
3306 | } | |
3307 | ||
3308 | /** | |
617ba13b | 3309 | * ext4_find_shared - find the indirect blocks for partial truncation. |
ac27a0ec DK |
3310 | * @inode: inode in question |
3311 | * @depth: depth of the affected branch | |
617ba13b | 3312 | * @offsets: offsets of pointers in that branch (see ext4_block_to_path) |
ac27a0ec DK |
3313 | * @chain: place to store the pointers to partial indirect blocks |
3314 | * @top: place to the (detached) top of branch | |
3315 | * | |
617ba13b | 3316 | * This is a helper function used by ext4_truncate(). |
ac27a0ec DK |
3317 | * |
3318 | * When we do truncate() we may have to clean the ends of several | |
3319 | * indirect blocks but leave the blocks themselves alive. Block is | |
3320 | * partially truncated if some data below the new i_size is refered | |
3321 | * from it (and it is on the path to the first completely truncated | |
3322 | * data block, indeed). We have to free the top of that path along | |
3323 | * with everything to the right of the path. Since no allocation | |
617ba13b | 3324 | * past the truncation point is possible until ext4_truncate() |
ac27a0ec DK |
3325 | * finishes, we may safely do the latter, but top of branch may |
3326 | * require special attention - pageout below the truncation point | |
3327 | * might try to populate it. | |
3328 | * | |
3329 | * We atomically detach the top of branch from the tree, store the | |
3330 | * block number of its root in *@top, pointers to buffer_heads of | |
3331 | * partially truncated blocks - in @chain[].bh and pointers to | |
3332 | * their last elements that should not be removed - in | |
3333 | * @chain[].p. Return value is the pointer to last filled element | |
3334 | * of @chain. | |
3335 | * | |
3336 | * The work left to caller to do the actual freeing of subtrees: | |
3337 | * a) free the subtree starting from *@top | |
3338 | * b) free the subtrees whose roots are stored in | |
3339 | * (@chain[i].p+1 .. end of @chain[i].bh->b_data) | |
3340 | * c) free the subtrees growing from the inode past the @chain[0]. | |
3341 | * (no partially truncated stuff there). */ | |
3342 | ||
617ba13b | 3343 | static Indirect *ext4_find_shared(struct inode *inode, int depth, |
725d26d3 | 3344 | ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top) |
ac27a0ec DK |
3345 | { |
3346 | Indirect *partial, *p; | |
3347 | int k, err; | |
3348 | ||
3349 | *top = 0; | |
3350 | /* Make k index the deepest non-null offest + 1 */ | |
3351 | for (k = depth; k > 1 && !offsets[k-1]; k--) | |
3352 | ; | |
617ba13b | 3353 | partial = ext4_get_branch(inode, k, offsets, chain, &err); |
ac27a0ec DK |
3354 | /* Writer: pointers */ |
3355 | if (!partial) | |
3356 | partial = chain + k-1; | |
3357 | /* | |
3358 | * If the branch acquired continuation since we've looked at it - | |
3359 | * fine, it should all survive and (new) top doesn't belong to us. | |
3360 | */ | |
3361 | if (!partial->key && *partial->p) | |
3362 | /* Writer: end */ | |
3363 | goto no_top; | |
af5bc92d | 3364 | for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) |
ac27a0ec DK |
3365 | ; |
3366 | /* | |
3367 | * OK, we've found the last block that must survive. The rest of our | |
3368 | * branch should be detached before unlocking. However, if that rest | |
3369 | * of branch is all ours and does not grow immediately from the inode | |
3370 | * it's easier to cheat and just decrement partial->p. | |
3371 | */ | |
3372 | if (p == chain + k - 1 && p > chain) { | |
3373 | p->p--; | |
3374 | } else { | |
3375 | *top = *p->p; | |
617ba13b | 3376 | /* Nope, don't do this in ext4. Must leave the tree intact */ |
ac27a0ec DK |
3377 | #if 0 |
3378 | *p->p = 0; | |
3379 | #endif | |
3380 | } | |
3381 | /* Writer: end */ | |
3382 | ||
af5bc92d | 3383 | while (partial > p) { |
ac27a0ec DK |
3384 | brelse(partial->bh); |
3385 | partial--; | |
3386 | } | |
3387 | no_top: | |
3388 | return partial; | |
3389 | } | |
3390 | ||
3391 | /* | |
3392 | * Zero a number of block pointers in either an inode or an indirect block. | |
3393 | * If we restart the transaction we must again get write access to the | |
3394 | * indirect block for further modification. | |
3395 | * | |
3396 | * We release `count' blocks on disk, but (last - first) may be greater | |
3397 | * than `count' because there can be holes in there. | |
3398 | */ | |
617ba13b MC |
3399 | static void ext4_clear_blocks(handle_t *handle, struct inode *inode, |
3400 | struct buffer_head *bh, ext4_fsblk_t block_to_free, | |
ac27a0ec DK |
3401 | unsigned long count, __le32 *first, __le32 *last) |
3402 | { | |
3403 | __le32 *p; | |
3404 | if (try_to_extend_transaction(handle, inode)) { | |
3405 | if (bh) { | |
617ba13b MC |
3406 | BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); |
3407 | ext4_journal_dirty_metadata(handle, bh); | |
ac27a0ec | 3408 | } |
617ba13b MC |
3409 | ext4_mark_inode_dirty(handle, inode); |
3410 | ext4_journal_test_restart(handle, inode); | |
ac27a0ec DK |
3411 | if (bh) { |
3412 | BUFFER_TRACE(bh, "retaking write access"); | |
617ba13b | 3413 | ext4_journal_get_write_access(handle, bh); |
ac27a0ec DK |
3414 | } |
3415 | } | |
3416 | ||
3417 | /* | |
3418 | * Any buffers which are on the journal will be in memory. We find | |
dab291af | 3419 | * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget() |
ac27a0ec | 3420 | * on them. We've already detached each block from the file, so |
dab291af | 3421 | * bforget() in jbd2_journal_forget() should be safe. |
ac27a0ec | 3422 | * |
dab291af | 3423 | * AKPM: turn on bforget in jbd2_journal_forget()!!! |
ac27a0ec DK |
3424 | */ |
3425 | for (p = first; p < last; p++) { | |
3426 | u32 nr = le32_to_cpu(*p); | |
3427 | if (nr) { | |
1d03ec98 | 3428 | struct buffer_head *tbh; |
ac27a0ec DK |
3429 | |
3430 | *p = 0; | |
1d03ec98 AK |
3431 | tbh = sb_find_get_block(inode->i_sb, nr); |
3432 | ext4_forget(handle, 0, inode, tbh, nr); | |
ac27a0ec DK |
3433 | } |
3434 | } | |
3435 | ||
c9de560d | 3436 | ext4_free_blocks(handle, inode, block_to_free, count, 0); |
ac27a0ec DK |
3437 | } |
3438 | ||
3439 | /** | |
617ba13b | 3440 | * ext4_free_data - free a list of data blocks |
ac27a0ec DK |
3441 | * @handle: handle for this transaction |
3442 | * @inode: inode we are dealing with | |
3443 | * @this_bh: indirect buffer_head which contains *@first and *@last | |
3444 | * @first: array of block numbers | |
3445 | * @last: points immediately past the end of array | |
3446 | * | |
3447 | * We are freeing all blocks refered from that array (numbers are stored as | |
3448 | * little-endian 32-bit) and updating @inode->i_blocks appropriately. | |
3449 | * | |
3450 | * We accumulate contiguous runs of blocks to free. Conveniently, if these | |
3451 | * blocks are contiguous then releasing them at one time will only affect one | |
3452 | * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't | |
3453 | * actually use a lot of journal space. | |
3454 | * | |
3455 | * @this_bh will be %NULL if @first and @last point into the inode's direct | |
3456 | * block pointers. | |
3457 | */ | |
617ba13b | 3458 | static void ext4_free_data(handle_t *handle, struct inode *inode, |
ac27a0ec DK |
3459 | struct buffer_head *this_bh, |
3460 | __le32 *first, __le32 *last) | |
3461 | { | |
617ba13b | 3462 | ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ |
ac27a0ec DK |
3463 | unsigned long count = 0; /* Number of blocks in the run */ |
3464 | __le32 *block_to_free_p = NULL; /* Pointer into inode/ind | |
3465 | corresponding to | |
3466 | block_to_free */ | |
617ba13b | 3467 | ext4_fsblk_t nr; /* Current block # */ |
ac27a0ec DK |
3468 | __le32 *p; /* Pointer into inode/ind |
3469 | for current block */ | |
3470 | int err; | |
3471 | ||
3472 | if (this_bh) { /* For indirect block */ | |
3473 | BUFFER_TRACE(this_bh, "get_write_access"); | |
617ba13b | 3474 | err = ext4_journal_get_write_access(handle, this_bh); |
ac27a0ec DK |
3475 | /* Important: if we can't update the indirect pointers |
3476 | * to the blocks, we can't free them. */ | |
3477 | if (err) | |
3478 | return; | |
3479 | } | |
3480 | ||
3481 | for (p = first; p < last; p++) { | |
3482 | nr = le32_to_cpu(*p); | |
3483 | if (nr) { | |
3484 | /* accumulate blocks to free if they're contiguous */ | |
3485 | if (count == 0) { | |
3486 | block_to_free = nr; | |
3487 | block_to_free_p = p; | |
3488 | count = 1; | |
3489 | } else if (nr == block_to_free + count) { | |
3490 | count++; | |
3491 | } else { | |
617ba13b | 3492 | ext4_clear_blocks(handle, inode, this_bh, |
ac27a0ec DK |
3493 | block_to_free, |
3494 | count, block_to_free_p, p); | |
3495 | block_to_free = nr; | |
3496 | block_to_free_p = p; | |
3497 | count = 1; | |
3498 | } | |
3499 | } | |
3500 | } | |
3501 | ||
3502 | if (count > 0) | |
617ba13b | 3503 | ext4_clear_blocks(handle, inode, this_bh, block_to_free, |
ac27a0ec DK |
3504 | count, block_to_free_p, p); |
3505 | ||
3506 | if (this_bh) { | |
617ba13b | 3507 | BUFFER_TRACE(this_bh, "call ext4_journal_dirty_metadata"); |
71dc8fbc DG |
3508 | |
3509 | /* | |
3510 | * The buffer head should have an attached journal head at this | |
3511 | * point. However, if the data is corrupted and an indirect | |
3512 | * block pointed to itself, it would have been detached when | |
3513 | * the block was cleared. Check for this instead of OOPSing. | |
3514 | */ | |
3515 | if (bh2jh(this_bh)) | |
3516 | ext4_journal_dirty_metadata(handle, this_bh); | |
3517 | else | |
3518 | ext4_error(inode->i_sb, __func__, | |
3519 | "circular indirect block detected, " | |
3520 | "inode=%lu, block=%llu", | |
3521 | inode->i_ino, | |
3522 | (unsigned long long) this_bh->b_blocknr); | |
ac27a0ec DK |
3523 | } |
3524 | } | |
3525 | ||
3526 | /** | |
617ba13b | 3527 | * ext4_free_branches - free an array of branches |
ac27a0ec DK |
3528 | * @handle: JBD handle for this transaction |
3529 | * @inode: inode we are dealing with | |
3530 | * @parent_bh: the buffer_head which contains *@first and *@last | |
3531 | * @first: array of block numbers | |
3532 | * @last: pointer immediately past the end of array | |
3533 | * @depth: depth of the branches to free | |
3534 | * | |
3535 | * We are freeing all blocks refered from these branches (numbers are | |
3536 | * stored as little-endian 32-bit) and updating @inode->i_blocks | |
3537 | * appropriately. | |
3538 | */ | |
617ba13b | 3539 | static void ext4_free_branches(handle_t *handle, struct inode *inode, |
ac27a0ec DK |
3540 | struct buffer_head *parent_bh, |
3541 | __le32 *first, __le32 *last, int depth) | |
3542 | { | |
617ba13b | 3543 | ext4_fsblk_t nr; |
ac27a0ec DK |
3544 | __le32 *p; |
3545 | ||
3546 | if (is_handle_aborted(handle)) | |
3547 | return; | |
3548 | ||
3549 | if (depth--) { | |
3550 | struct buffer_head *bh; | |
617ba13b | 3551 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
ac27a0ec DK |
3552 | p = last; |
3553 | while (--p >= first) { | |
3554 | nr = le32_to_cpu(*p); | |
3555 | if (!nr) | |
3556 | continue; /* A hole */ | |
3557 | ||
3558 | /* Go read the buffer for the next level down */ | |
3559 | bh = sb_bread(inode->i_sb, nr); | |
3560 | ||
3561 | /* | |
3562 | * A read failure? Report error and clear slot | |
3563 | * (should be rare). | |
3564 | */ | |
3565 | if (!bh) { | |
617ba13b | 3566 | ext4_error(inode->i_sb, "ext4_free_branches", |
2ae02107 | 3567 | "Read failure, inode=%lu, block=%llu", |
ac27a0ec DK |
3568 | inode->i_ino, nr); |
3569 | continue; | |
3570 | } | |
3571 | ||
3572 | /* This zaps the entire block. Bottom up. */ | |
3573 | BUFFER_TRACE(bh, "free child branches"); | |
617ba13b | 3574 | ext4_free_branches(handle, inode, bh, |
af5bc92d TT |
3575 | (__le32 *) bh->b_data, |
3576 | (__le32 *) bh->b_data + addr_per_block, | |
3577 | depth); | |
ac27a0ec DK |
3578 | |
3579 | /* | |
3580 | * We've probably journalled the indirect block several | |
3581 | * times during the truncate. But it's no longer | |
3582 | * needed and we now drop it from the transaction via | |
dab291af | 3583 | * jbd2_journal_revoke(). |
ac27a0ec DK |
3584 | * |
3585 | * That's easy if it's exclusively part of this | |
3586 | * transaction. But if it's part of the committing | |
dab291af | 3587 | * transaction then jbd2_journal_forget() will simply |
ac27a0ec | 3588 | * brelse() it. That means that if the underlying |
617ba13b | 3589 | * block is reallocated in ext4_get_block(), |
ac27a0ec DK |
3590 | * unmap_underlying_metadata() will find this block |
3591 | * and will try to get rid of it. damn, damn. | |
3592 | * | |
3593 | * If this block has already been committed to the | |
3594 | * journal, a revoke record will be written. And | |
3595 | * revoke records must be emitted *before* clearing | |
3596 | * this block's bit in the bitmaps. | |
3597 | */ | |
617ba13b | 3598 | ext4_forget(handle, 1, inode, bh, bh->b_blocknr); |
ac27a0ec DK |
3599 | |
3600 | /* | |
3601 | * Everything below this this pointer has been | |
3602 | * released. Now let this top-of-subtree go. | |
3603 | * | |
3604 | * We want the freeing of this indirect block to be | |
3605 | * atomic in the journal with the updating of the | |
3606 | * bitmap block which owns it. So make some room in | |
3607 | * the journal. | |
3608 | * | |
3609 | * We zero the parent pointer *after* freeing its | |
3610 | * pointee in the bitmaps, so if extend_transaction() | |
3611 | * for some reason fails to put the bitmap changes and | |
3612 | * the release into the same transaction, recovery | |
3613 | * will merely complain about releasing a free block, | |
3614 | * rather than leaking blocks. | |
3615 | */ | |
3616 | if (is_handle_aborted(handle)) | |
3617 | return; | |
3618 | if (try_to_extend_transaction(handle, inode)) { | |
617ba13b MC |
3619 | ext4_mark_inode_dirty(handle, inode); |
3620 | ext4_journal_test_restart(handle, inode); | |
ac27a0ec DK |
3621 | } |
3622 | ||
c9de560d | 3623 | ext4_free_blocks(handle, inode, nr, 1, 1); |
ac27a0ec DK |
3624 | |
3625 | if (parent_bh) { | |
3626 | /* | |
3627 | * The block which we have just freed is | |
3628 | * pointed to by an indirect block: journal it | |
3629 | */ | |
3630 | BUFFER_TRACE(parent_bh, "get_write_access"); | |
617ba13b | 3631 | if (!ext4_journal_get_write_access(handle, |
ac27a0ec DK |
3632 | parent_bh)){ |
3633 | *p = 0; | |
3634 | BUFFER_TRACE(parent_bh, | |
617ba13b MC |
3635 | "call ext4_journal_dirty_metadata"); |
3636 | ext4_journal_dirty_metadata(handle, | |
ac27a0ec DK |
3637 | parent_bh); |
3638 | } | |
3639 | } | |
3640 | } | |
3641 | } else { | |
3642 | /* We have reached the bottom of the tree. */ | |
3643 | BUFFER_TRACE(parent_bh, "free data blocks"); | |
617ba13b | 3644 | ext4_free_data(handle, inode, parent_bh, first, last); |
ac27a0ec DK |
3645 | } |
3646 | } | |
3647 | ||
91ef4caf DG |
3648 | int ext4_can_truncate(struct inode *inode) |
3649 | { | |
3650 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
3651 | return 0; | |
3652 | if (S_ISREG(inode->i_mode)) | |
3653 | return 1; | |
3654 | if (S_ISDIR(inode->i_mode)) | |
3655 | return 1; | |
3656 | if (S_ISLNK(inode->i_mode)) | |
3657 | return !ext4_inode_is_fast_symlink(inode); | |
3658 | return 0; | |
3659 | } | |
3660 | ||
ac27a0ec | 3661 | /* |
617ba13b | 3662 | * ext4_truncate() |
ac27a0ec | 3663 | * |
617ba13b MC |
3664 | * We block out ext4_get_block() block instantiations across the entire |
3665 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run | |
ac27a0ec DK |
3666 | * simultaneously on behalf of the same inode. |
3667 | * | |
3668 | * As we work through the truncate and commmit bits of it to the journal there | |
3669 | * is one core, guiding principle: the file's tree must always be consistent on | |
3670 | * disk. We must be able to restart the truncate after a crash. | |
3671 | * | |
3672 | * The file's tree may be transiently inconsistent in memory (although it | |
3673 | * probably isn't), but whenever we close off and commit a journal transaction, | |
3674 | * the contents of (the filesystem + the journal) must be consistent and | |
3675 | * restartable. It's pretty simple, really: bottom up, right to left (although | |
3676 | * left-to-right works OK too). | |
3677 | * | |
3678 | * Note that at recovery time, journal replay occurs *before* the restart of | |
3679 | * truncate against the orphan inode list. | |
3680 | * | |
3681 | * The committed inode has the new, desired i_size (which is the same as | |
617ba13b | 3682 | * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see |
ac27a0ec | 3683 | * that this inode's truncate did not complete and it will again call |
617ba13b MC |
3684 | * ext4_truncate() to have another go. So there will be instantiated blocks |
3685 | * to the right of the truncation point in a crashed ext4 filesystem. But | |
ac27a0ec | 3686 | * that's fine - as long as they are linked from the inode, the post-crash |
617ba13b | 3687 | * ext4_truncate() run will find them and release them. |
ac27a0ec | 3688 | */ |
617ba13b | 3689 | void ext4_truncate(struct inode *inode) |
ac27a0ec DK |
3690 | { |
3691 | handle_t *handle; | |
617ba13b | 3692 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 3693 | __le32 *i_data = ei->i_data; |
617ba13b | 3694 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
ac27a0ec | 3695 | struct address_space *mapping = inode->i_mapping; |
725d26d3 | 3696 | ext4_lblk_t offsets[4]; |
ac27a0ec DK |
3697 | Indirect chain[4]; |
3698 | Indirect *partial; | |
3699 | __le32 nr = 0; | |
3700 | int n; | |
725d26d3 | 3701 | ext4_lblk_t last_block; |
ac27a0ec | 3702 | unsigned blocksize = inode->i_sb->s_blocksize; |
ac27a0ec | 3703 | |
91ef4caf | 3704 | if (!ext4_can_truncate(inode)) |
ac27a0ec DK |
3705 | return; |
3706 | ||
1d03ec98 | 3707 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
cf108bca | 3708 | ext4_ext_truncate(inode); |
1d03ec98 AK |
3709 | return; |
3710 | } | |
a86c6181 | 3711 | |
ac27a0ec | 3712 | handle = start_transaction(inode); |
cf108bca | 3713 | if (IS_ERR(handle)) |
ac27a0ec | 3714 | return; /* AKPM: return what? */ |
ac27a0ec DK |
3715 | |
3716 | last_block = (inode->i_size + blocksize-1) | |
617ba13b | 3717 | >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); |
ac27a0ec | 3718 | |
cf108bca JK |
3719 | if (inode->i_size & (blocksize - 1)) |
3720 | if (ext4_block_truncate_page(handle, mapping, inode->i_size)) | |
3721 | goto out_stop; | |
ac27a0ec | 3722 | |
617ba13b | 3723 | n = ext4_block_to_path(inode, last_block, offsets, NULL); |
ac27a0ec DK |
3724 | if (n == 0) |
3725 | goto out_stop; /* error */ | |
3726 | ||
3727 | /* | |
3728 | * OK. This truncate is going to happen. We add the inode to the | |
3729 | * orphan list, so that if this truncate spans multiple transactions, | |
3730 | * and we crash, we will resume the truncate when the filesystem | |
3731 | * recovers. It also marks the inode dirty, to catch the new size. | |
3732 | * | |
3733 | * Implication: the file must always be in a sane, consistent | |
3734 | * truncatable state while each transaction commits. | |
3735 | */ | |
617ba13b | 3736 | if (ext4_orphan_add(handle, inode)) |
ac27a0ec DK |
3737 | goto out_stop; |
3738 | ||
632eaeab MC |
3739 | /* |
3740 | * From here we block out all ext4_get_block() callers who want to | |
3741 | * modify the block allocation tree. | |
3742 | */ | |
3743 | down_write(&ei->i_data_sem); | |
b4df2030 | 3744 | |
c2ea3fde | 3745 | ext4_discard_preallocations(inode); |
b4df2030 | 3746 | |
ac27a0ec DK |
3747 | /* |
3748 | * The orphan list entry will now protect us from any crash which | |
3749 | * occurs before the truncate completes, so it is now safe to propagate | |
3750 | * the new, shorter inode size (held for now in i_size) into the | |
3751 | * on-disk inode. We do this via i_disksize, which is the value which | |
617ba13b | 3752 | * ext4 *really* writes onto the disk inode. |
ac27a0ec DK |
3753 | */ |
3754 | ei->i_disksize = inode->i_size; | |
3755 | ||
ac27a0ec | 3756 | if (n == 1) { /* direct blocks */ |
617ba13b MC |
3757 | ext4_free_data(handle, inode, NULL, i_data+offsets[0], |
3758 | i_data + EXT4_NDIR_BLOCKS); | |
ac27a0ec DK |
3759 | goto do_indirects; |
3760 | } | |
3761 | ||
617ba13b | 3762 | partial = ext4_find_shared(inode, n, offsets, chain, &nr); |
ac27a0ec DK |
3763 | /* Kill the top of shared branch (not detached) */ |
3764 | if (nr) { | |
3765 | if (partial == chain) { | |
3766 | /* Shared branch grows from the inode */ | |
617ba13b | 3767 | ext4_free_branches(handle, inode, NULL, |
ac27a0ec DK |
3768 | &nr, &nr+1, (chain+n-1) - partial); |
3769 | *partial->p = 0; | |
3770 | /* | |
3771 | * We mark the inode dirty prior to restart, | |
3772 | * and prior to stop. No need for it here. | |
3773 | */ | |
3774 | } else { | |
3775 | /* Shared branch grows from an indirect block */ | |
3776 | BUFFER_TRACE(partial->bh, "get_write_access"); | |
617ba13b | 3777 | ext4_free_branches(handle, inode, partial->bh, |
ac27a0ec DK |
3778 | partial->p, |
3779 | partial->p+1, (chain+n-1) - partial); | |
3780 | } | |
3781 | } | |
3782 | /* Clear the ends of indirect blocks on the shared branch */ | |
3783 | while (partial > chain) { | |
617ba13b | 3784 | ext4_free_branches(handle, inode, partial->bh, partial->p + 1, |
ac27a0ec DK |
3785 | (__le32*)partial->bh->b_data+addr_per_block, |
3786 | (chain+n-1) - partial); | |
3787 | BUFFER_TRACE(partial->bh, "call brelse"); | |
3788 | brelse (partial->bh); | |
3789 | partial--; | |
3790 | } | |
3791 | do_indirects: | |
3792 | /* Kill the remaining (whole) subtrees */ | |
3793 | switch (offsets[0]) { | |
3794 | default: | |
617ba13b | 3795 | nr = i_data[EXT4_IND_BLOCK]; |
ac27a0ec | 3796 | if (nr) { |
617ba13b MC |
3797 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); |
3798 | i_data[EXT4_IND_BLOCK] = 0; | |
ac27a0ec | 3799 | } |
617ba13b MC |
3800 | case EXT4_IND_BLOCK: |
3801 | nr = i_data[EXT4_DIND_BLOCK]; | |
ac27a0ec | 3802 | if (nr) { |
617ba13b MC |
3803 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); |
3804 | i_data[EXT4_DIND_BLOCK] = 0; | |
ac27a0ec | 3805 | } |
617ba13b MC |
3806 | case EXT4_DIND_BLOCK: |
3807 | nr = i_data[EXT4_TIND_BLOCK]; | |
ac27a0ec | 3808 | if (nr) { |
617ba13b MC |
3809 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); |
3810 | i_data[EXT4_TIND_BLOCK] = 0; | |
ac27a0ec | 3811 | } |
617ba13b | 3812 | case EXT4_TIND_BLOCK: |
ac27a0ec DK |
3813 | ; |
3814 | } | |
3815 | ||
0e855ac8 | 3816 | up_write(&ei->i_data_sem); |
ef7f3835 | 3817 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
617ba13b | 3818 | ext4_mark_inode_dirty(handle, inode); |
ac27a0ec DK |
3819 | |
3820 | /* | |
3821 | * In a multi-transaction truncate, we only make the final transaction | |
3822 | * synchronous | |
3823 | */ | |
3824 | if (IS_SYNC(inode)) | |
3825 | handle->h_sync = 1; | |
3826 | out_stop: | |
3827 | /* | |
3828 | * If this was a simple ftruncate(), and the file will remain alive | |
3829 | * then we need to clear up the orphan record which we created above. | |
3830 | * However, if this was a real unlink then we were called by | |
617ba13b | 3831 | * ext4_delete_inode(), and we allow that function to clean up the |
ac27a0ec DK |
3832 | * orphan info for us. |
3833 | */ | |
3834 | if (inode->i_nlink) | |
617ba13b | 3835 | ext4_orphan_del(handle, inode); |
ac27a0ec | 3836 | |
617ba13b | 3837 | ext4_journal_stop(handle); |
ac27a0ec DK |
3838 | } |
3839 | ||
ac27a0ec | 3840 | /* |
617ba13b | 3841 | * ext4_get_inode_loc returns with an extra refcount against the inode's |
ac27a0ec DK |
3842 | * underlying buffer_head on success. If 'in_mem' is true, we have all |
3843 | * data in memory that is needed to recreate the on-disk version of this | |
3844 | * inode. | |
3845 | */ | |
617ba13b MC |
3846 | static int __ext4_get_inode_loc(struct inode *inode, |
3847 | struct ext4_iloc *iloc, int in_mem) | |
ac27a0ec | 3848 | { |
240799cd TT |
3849 | struct ext4_group_desc *gdp; |
3850 | struct buffer_head *bh; | |
3851 | struct super_block *sb = inode->i_sb; | |
3852 | ext4_fsblk_t block; | |
3853 | int inodes_per_block, inode_offset; | |
3854 | ||
3855 | iloc->bh = 0; | |
3856 | if (!ext4_valid_inum(sb, inode->i_ino)) | |
3857 | return -EIO; | |
ac27a0ec | 3858 | |
240799cd TT |
3859 | iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); |
3860 | gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); | |
3861 | if (!gdp) | |
ac27a0ec DK |
3862 | return -EIO; |
3863 | ||
240799cd TT |
3864 | /* |
3865 | * Figure out the offset within the block group inode table | |
3866 | */ | |
3867 | inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); | |
3868 | inode_offset = ((inode->i_ino - 1) % | |
3869 | EXT4_INODES_PER_GROUP(sb)); | |
3870 | block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); | |
3871 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); | |
3872 | ||
3873 | bh = sb_getblk(sb, block); | |
ac27a0ec | 3874 | if (!bh) { |
240799cd TT |
3875 | ext4_error(sb, "ext4_get_inode_loc", "unable to read " |
3876 | "inode block - inode=%lu, block=%llu", | |
3877 | inode->i_ino, block); | |
ac27a0ec DK |
3878 | return -EIO; |
3879 | } | |
3880 | if (!buffer_uptodate(bh)) { | |
3881 | lock_buffer(bh); | |
9c83a923 HK |
3882 | |
3883 | /* | |
3884 | * If the buffer has the write error flag, we have failed | |
3885 | * to write out another inode in the same block. In this | |
3886 | * case, we don't have to read the block because we may | |
3887 | * read the old inode data successfully. | |
3888 | */ | |
3889 | if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) | |
3890 | set_buffer_uptodate(bh); | |
3891 | ||
ac27a0ec DK |
3892 | if (buffer_uptodate(bh)) { |
3893 | /* someone brought it uptodate while we waited */ | |
3894 | unlock_buffer(bh); | |
3895 | goto has_buffer; | |
3896 | } | |
3897 | ||
3898 | /* | |
3899 | * If we have all information of the inode in memory and this | |
3900 | * is the only valid inode in the block, we need not read the | |
3901 | * block. | |
3902 | */ | |
3903 | if (in_mem) { | |
3904 | struct buffer_head *bitmap_bh; | |
240799cd | 3905 | int i, start; |
ac27a0ec | 3906 | |
240799cd | 3907 | start = inode_offset & ~(inodes_per_block - 1); |
ac27a0ec | 3908 | |
240799cd TT |
3909 | /* Is the inode bitmap in cache? */ |
3910 | bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); | |
ac27a0ec DK |
3911 | if (!bitmap_bh) |
3912 | goto make_io; | |
3913 | ||
3914 | /* | |
3915 | * If the inode bitmap isn't in cache then the | |
3916 | * optimisation may end up performing two reads instead | |
3917 | * of one, so skip it. | |
3918 | */ | |
3919 | if (!buffer_uptodate(bitmap_bh)) { | |
3920 | brelse(bitmap_bh); | |
3921 | goto make_io; | |
3922 | } | |
240799cd | 3923 | for (i = start; i < start + inodes_per_block; i++) { |
ac27a0ec DK |
3924 | if (i == inode_offset) |
3925 | continue; | |
617ba13b | 3926 | if (ext4_test_bit(i, bitmap_bh->b_data)) |
ac27a0ec DK |
3927 | break; |
3928 | } | |
3929 | brelse(bitmap_bh); | |
240799cd | 3930 | if (i == start + inodes_per_block) { |
ac27a0ec DK |
3931 | /* all other inodes are free, so skip I/O */ |
3932 | memset(bh->b_data, 0, bh->b_size); | |
3933 | set_buffer_uptodate(bh); | |
3934 | unlock_buffer(bh); | |
3935 | goto has_buffer; | |
3936 | } | |
3937 | } | |
3938 | ||
3939 | make_io: | |
240799cd TT |
3940 | /* |
3941 | * If we need to do any I/O, try to pre-readahead extra | |
3942 | * blocks from the inode table. | |
3943 | */ | |
3944 | if (EXT4_SB(sb)->s_inode_readahead_blks) { | |
3945 | ext4_fsblk_t b, end, table; | |
3946 | unsigned num; | |
3947 | ||
3948 | table = ext4_inode_table(sb, gdp); | |
3949 | /* Make sure s_inode_readahead_blks is a power of 2 */ | |
3950 | while (EXT4_SB(sb)->s_inode_readahead_blks & | |
3951 | (EXT4_SB(sb)->s_inode_readahead_blks-1)) | |
3952 | EXT4_SB(sb)->s_inode_readahead_blks = | |
3953 | (EXT4_SB(sb)->s_inode_readahead_blks & | |
3954 | (EXT4_SB(sb)->s_inode_readahead_blks-1)); | |
3955 | b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); | |
3956 | if (table > b) | |
3957 | b = table; | |
3958 | end = b + EXT4_SB(sb)->s_inode_readahead_blks; | |
3959 | num = EXT4_INODES_PER_GROUP(sb); | |
3960 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
3961 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) | |
3962 | num -= le16_to_cpu(gdp->bg_itable_unused); | |
3963 | table += num / inodes_per_block; | |
3964 | if (end > table) | |
3965 | end = table; | |
3966 | while (b <= end) | |
3967 | sb_breadahead(sb, b++); | |
3968 | } | |
3969 | ||
ac27a0ec DK |
3970 | /* |
3971 | * There are other valid inodes in the buffer, this inode | |
3972 | * has in-inode xattrs, or we don't have this inode in memory. | |
3973 | * Read the block from disk. | |
3974 | */ | |
3975 | get_bh(bh); | |
3976 | bh->b_end_io = end_buffer_read_sync; | |
3977 | submit_bh(READ_META, bh); | |
3978 | wait_on_buffer(bh); | |
3979 | if (!buffer_uptodate(bh)) { | |
240799cd TT |
3980 | ext4_error(sb, __func__, |
3981 | "unable to read inode block - inode=%lu, " | |
3982 | "block=%llu", inode->i_ino, block); | |
ac27a0ec DK |
3983 | brelse(bh); |
3984 | return -EIO; | |
3985 | } | |
3986 | } | |
3987 | has_buffer: | |
3988 | iloc->bh = bh; | |
3989 | return 0; | |
3990 | } | |
3991 | ||
617ba13b | 3992 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) |
ac27a0ec DK |
3993 | { |
3994 | /* We have all inode data except xattrs in memory here. */ | |
617ba13b MC |
3995 | return __ext4_get_inode_loc(inode, iloc, |
3996 | !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); | |
ac27a0ec DK |
3997 | } |
3998 | ||
617ba13b | 3999 | void ext4_set_inode_flags(struct inode *inode) |
ac27a0ec | 4000 | { |
617ba13b | 4001 | unsigned int flags = EXT4_I(inode)->i_flags; |
ac27a0ec DK |
4002 | |
4003 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | |
617ba13b | 4004 | if (flags & EXT4_SYNC_FL) |
ac27a0ec | 4005 | inode->i_flags |= S_SYNC; |
617ba13b | 4006 | if (flags & EXT4_APPEND_FL) |
ac27a0ec | 4007 | inode->i_flags |= S_APPEND; |
617ba13b | 4008 | if (flags & EXT4_IMMUTABLE_FL) |
ac27a0ec | 4009 | inode->i_flags |= S_IMMUTABLE; |
617ba13b | 4010 | if (flags & EXT4_NOATIME_FL) |
ac27a0ec | 4011 | inode->i_flags |= S_NOATIME; |
617ba13b | 4012 | if (flags & EXT4_DIRSYNC_FL) |
ac27a0ec DK |
4013 | inode->i_flags |= S_DIRSYNC; |
4014 | } | |
4015 | ||
ff9ddf7e JK |
4016 | /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ |
4017 | void ext4_get_inode_flags(struct ext4_inode_info *ei) | |
4018 | { | |
4019 | unsigned int flags = ei->vfs_inode.i_flags; | |
4020 | ||
4021 | ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| | |
4022 | EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); | |
4023 | if (flags & S_SYNC) | |
4024 | ei->i_flags |= EXT4_SYNC_FL; | |
4025 | if (flags & S_APPEND) | |
4026 | ei->i_flags |= EXT4_APPEND_FL; | |
4027 | if (flags & S_IMMUTABLE) | |
4028 | ei->i_flags |= EXT4_IMMUTABLE_FL; | |
4029 | if (flags & S_NOATIME) | |
4030 | ei->i_flags |= EXT4_NOATIME_FL; | |
4031 | if (flags & S_DIRSYNC) | |
4032 | ei->i_flags |= EXT4_DIRSYNC_FL; | |
4033 | } | |
0fc1b451 AK |
4034 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, |
4035 | struct ext4_inode_info *ei) | |
4036 | { | |
4037 | blkcnt_t i_blocks ; | |
8180a562 AK |
4038 | struct inode *inode = &(ei->vfs_inode); |
4039 | struct super_block *sb = inode->i_sb; | |
0fc1b451 AK |
4040 | |
4041 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
4042 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { | |
4043 | /* we are using combined 48 bit field */ | |
4044 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | | |
4045 | le32_to_cpu(raw_inode->i_blocks_lo); | |
8180a562 AK |
4046 | if (ei->i_flags & EXT4_HUGE_FILE_FL) { |
4047 | /* i_blocks represent file system block size */ | |
4048 | return i_blocks << (inode->i_blkbits - 9); | |
4049 | } else { | |
4050 | return i_blocks; | |
4051 | } | |
0fc1b451 AK |
4052 | } else { |
4053 | return le32_to_cpu(raw_inode->i_blocks_lo); | |
4054 | } | |
4055 | } | |
ff9ddf7e | 4056 | |
1d1fe1ee | 4057 | struct inode *ext4_iget(struct super_block *sb, unsigned long ino) |
ac27a0ec | 4058 | { |
617ba13b MC |
4059 | struct ext4_iloc iloc; |
4060 | struct ext4_inode *raw_inode; | |
1d1fe1ee | 4061 | struct ext4_inode_info *ei; |
ac27a0ec | 4062 | struct buffer_head *bh; |
1d1fe1ee DH |
4063 | struct inode *inode; |
4064 | long ret; | |
ac27a0ec DK |
4065 | int block; |
4066 | ||
1d1fe1ee DH |
4067 | inode = iget_locked(sb, ino); |
4068 | if (!inode) | |
4069 | return ERR_PTR(-ENOMEM); | |
4070 | if (!(inode->i_state & I_NEW)) | |
4071 | return inode; | |
4072 | ||
4073 | ei = EXT4_I(inode); | |
03010a33 | 4074 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
617ba13b MC |
4075 | ei->i_acl = EXT4_ACL_NOT_CACHED; |
4076 | ei->i_default_acl = EXT4_ACL_NOT_CACHED; | |
ac27a0ec | 4077 | #endif |
ac27a0ec | 4078 | |
1d1fe1ee DH |
4079 | ret = __ext4_get_inode_loc(inode, &iloc, 0); |
4080 | if (ret < 0) | |
ac27a0ec DK |
4081 | goto bad_inode; |
4082 | bh = iloc.bh; | |
617ba13b | 4083 | raw_inode = ext4_raw_inode(&iloc); |
ac27a0ec DK |
4084 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
4085 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); | |
4086 | inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); | |
af5bc92d | 4087 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
ac27a0ec DK |
4088 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; |
4089 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | |
4090 | } | |
4091 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | |
ac27a0ec DK |
4092 | |
4093 | ei->i_state = 0; | |
4094 | ei->i_dir_start_lookup = 0; | |
4095 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); | |
4096 | /* We now have enough fields to check if the inode was active or not. | |
4097 | * This is needed because nfsd might try to access dead inodes | |
4098 | * the test is that same one that e2fsck uses | |
4099 | * NeilBrown 1999oct15 | |
4100 | */ | |
4101 | if (inode->i_nlink == 0) { | |
4102 | if (inode->i_mode == 0 || | |
617ba13b | 4103 | !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { |
ac27a0ec | 4104 | /* this inode is deleted */ |
af5bc92d | 4105 | brelse(bh); |
1d1fe1ee | 4106 | ret = -ESTALE; |
ac27a0ec DK |
4107 | goto bad_inode; |
4108 | } | |
4109 | /* The only unlinked inodes we let through here have | |
4110 | * valid i_mode and are being read by the orphan | |
4111 | * recovery code: that's fine, we're about to complete | |
4112 | * the process of deleting those. */ | |
4113 | } | |
ac27a0ec | 4114 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
0fc1b451 | 4115 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); |
7973c0c1 | 4116 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); |
9b8f1f01 | 4117 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
a48380f7 | 4118 | cpu_to_le32(EXT4_OS_HURD)) { |
a1ddeb7e BP |
4119 | ei->i_file_acl |= |
4120 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; | |
ac27a0ec | 4121 | } |
a48380f7 | 4122 | inode->i_size = ext4_isize(raw_inode); |
ac27a0ec DK |
4123 | ei->i_disksize = inode->i_size; |
4124 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | |
4125 | ei->i_block_group = iloc.block_group; | |
4126 | /* | |
4127 | * NOTE! The in-memory inode i_data array is in little-endian order | |
4128 | * even on big-endian machines: we do NOT byteswap the block numbers! | |
4129 | */ | |
617ba13b | 4130 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
ac27a0ec DK |
4131 | ei->i_data[block] = raw_inode->i_block[block]; |
4132 | INIT_LIST_HEAD(&ei->i_orphan); | |
4133 | ||
0040d987 | 4134 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
ac27a0ec | 4135 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); |
617ba13b | 4136 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > |
e5d2861f | 4137 | EXT4_INODE_SIZE(inode->i_sb)) { |
af5bc92d | 4138 | brelse(bh); |
1d1fe1ee | 4139 | ret = -EIO; |
ac27a0ec | 4140 | goto bad_inode; |
e5d2861f | 4141 | } |
ac27a0ec DK |
4142 | if (ei->i_extra_isize == 0) { |
4143 | /* The extra space is currently unused. Use it. */ | |
617ba13b MC |
4144 | ei->i_extra_isize = sizeof(struct ext4_inode) - |
4145 | EXT4_GOOD_OLD_INODE_SIZE; | |
ac27a0ec DK |
4146 | } else { |
4147 | __le32 *magic = (void *)raw_inode + | |
617ba13b | 4148 | EXT4_GOOD_OLD_INODE_SIZE + |
ac27a0ec | 4149 | ei->i_extra_isize; |
617ba13b MC |
4150 | if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) |
4151 | ei->i_state |= EXT4_STATE_XATTR; | |
ac27a0ec DK |
4152 | } |
4153 | } else | |
4154 | ei->i_extra_isize = 0; | |
4155 | ||
ef7f3835 KS |
4156 | EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); |
4157 | EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); | |
4158 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); | |
4159 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); | |
4160 | ||
25ec56b5 JNC |
4161 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); |
4162 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | |
4163 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | |
4164 | inode->i_version |= | |
4165 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; | |
4166 | } | |
4167 | ||
ac27a0ec | 4168 | if (S_ISREG(inode->i_mode)) { |
617ba13b MC |
4169 | inode->i_op = &ext4_file_inode_operations; |
4170 | inode->i_fop = &ext4_file_operations; | |
4171 | ext4_set_aops(inode); | |
ac27a0ec | 4172 | } else if (S_ISDIR(inode->i_mode)) { |
617ba13b MC |
4173 | inode->i_op = &ext4_dir_inode_operations; |
4174 | inode->i_fop = &ext4_dir_operations; | |
ac27a0ec | 4175 | } else if (S_ISLNK(inode->i_mode)) { |
e83c1397 | 4176 | if (ext4_inode_is_fast_symlink(inode)) { |
617ba13b | 4177 | inode->i_op = &ext4_fast_symlink_inode_operations; |
e83c1397 DG |
4178 | nd_terminate_link(ei->i_data, inode->i_size, |
4179 | sizeof(ei->i_data) - 1); | |
4180 | } else { | |
617ba13b MC |
4181 | inode->i_op = &ext4_symlink_inode_operations; |
4182 | ext4_set_aops(inode); | |
ac27a0ec DK |
4183 | } |
4184 | } else { | |
617ba13b | 4185 | inode->i_op = &ext4_special_inode_operations; |
ac27a0ec DK |
4186 | if (raw_inode->i_block[0]) |
4187 | init_special_inode(inode, inode->i_mode, | |
4188 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); | |
4189 | else | |
4190 | init_special_inode(inode, inode->i_mode, | |
4191 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); | |
4192 | } | |
af5bc92d | 4193 | brelse(iloc.bh); |
617ba13b | 4194 | ext4_set_inode_flags(inode); |
1d1fe1ee DH |
4195 | unlock_new_inode(inode); |
4196 | return inode; | |
ac27a0ec DK |
4197 | |
4198 | bad_inode: | |
1d1fe1ee DH |
4199 | iget_failed(inode); |
4200 | return ERR_PTR(ret); | |
ac27a0ec DK |
4201 | } |
4202 | ||
0fc1b451 AK |
4203 | static int ext4_inode_blocks_set(handle_t *handle, |
4204 | struct ext4_inode *raw_inode, | |
4205 | struct ext4_inode_info *ei) | |
4206 | { | |
4207 | struct inode *inode = &(ei->vfs_inode); | |
4208 | u64 i_blocks = inode->i_blocks; | |
4209 | struct super_block *sb = inode->i_sb; | |
0fc1b451 AK |
4210 | |
4211 | if (i_blocks <= ~0U) { | |
4212 | /* | |
4213 | * i_blocks can be represnted in a 32 bit variable | |
4214 | * as multiple of 512 bytes | |
4215 | */ | |
8180a562 | 4216 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
0fc1b451 | 4217 | raw_inode->i_blocks_high = 0; |
8180a562 | 4218 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; |
f287a1a5 TT |
4219 | return 0; |
4220 | } | |
4221 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) | |
4222 | return -EFBIG; | |
4223 | ||
4224 | if (i_blocks <= 0xffffffffffffULL) { | |
0fc1b451 AK |
4225 | /* |
4226 | * i_blocks can be represented in a 48 bit variable | |
4227 | * as multiple of 512 bytes | |
4228 | */ | |
8180a562 | 4229 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
0fc1b451 | 4230 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); |
8180a562 | 4231 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; |
0fc1b451 | 4232 | } else { |
8180a562 AK |
4233 | ei->i_flags |= EXT4_HUGE_FILE_FL; |
4234 | /* i_block is stored in file system block size */ | |
4235 | i_blocks = i_blocks >> (inode->i_blkbits - 9); | |
4236 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); | |
4237 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); | |
0fc1b451 | 4238 | } |
f287a1a5 | 4239 | return 0; |
0fc1b451 AK |
4240 | } |
4241 | ||
ac27a0ec DK |
4242 | /* |
4243 | * Post the struct inode info into an on-disk inode location in the | |
4244 | * buffer-cache. This gobbles the caller's reference to the | |
4245 | * buffer_head in the inode location struct. | |
4246 | * | |
4247 | * The caller must have write access to iloc->bh. | |
4248 | */ | |
617ba13b | 4249 | static int ext4_do_update_inode(handle_t *handle, |
ac27a0ec | 4250 | struct inode *inode, |
617ba13b | 4251 | struct ext4_iloc *iloc) |
ac27a0ec | 4252 | { |
617ba13b MC |
4253 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); |
4254 | struct ext4_inode_info *ei = EXT4_I(inode); | |
ac27a0ec DK |
4255 | struct buffer_head *bh = iloc->bh; |
4256 | int err = 0, rc, block; | |
4257 | ||
4258 | /* For fields not not tracking in the in-memory inode, | |
4259 | * initialise them to zero for new inodes. */ | |
617ba13b MC |
4260 | if (ei->i_state & EXT4_STATE_NEW) |
4261 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); | |
ac27a0ec | 4262 | |
ff9ddf7e | 4263 | ext4_get_inode_flags(ei); |
ac27a0ec | 4264 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
af5bc92d | 4265 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
ac27a0ec DK |
4266 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); |
4267 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); | |
4268 | /* | |
4269 | * Fix up interoperability with old kernels. Otherwise, old inodes get | |
4270 | * re-used with the upper 16 bits of the uid/gid intact | |
4271 | */ | |
af5bc92d | 4272 | if (!ei->i_dtime) { |
ac27a0ec DK |
4273 | raw_inode->i_uid_high = |
4274 | cpu_to_le16(high_16_bits(inode->i_uid)); | |
4275 | raw_inode->i_gid_high = | |
4276 | cpu_to_le16(high_16_bits(inode->i_gid)); | |
4277 | } else { | |
4278 | raw_inode->i_uid_high = 0; | |
4279 | raw_inode->i_gid_high = 0; | |
4280 | } | |
4281 | } else { | |
4282 | raw_inode->i_uid_low = | |
4283 | cpu_to_le16(fs_high2lowuid(inode->i_uid)); | |
4284 | raw_inode->i_gid_low = | |
4285 | cpu_to_le16(fs_high2lowgid(inode->i_gid)); | |
4286 | raw_inode->i_uid_high = 0; | |
4287 | raw_inode->i_gid_high = 0; | |
4288 | } | |
4289 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | |
ef7f3835 KS |
4290 | |
4291 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); | |
4292 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); | |
4293 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); | |
4294 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); | |
4295 | ||
0fc1b451 AK |
4296 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) |
4297 | goto out_brelse; | |
ac27a0ec | 4298 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
267e4db9 AK |
4299 | /* clear the migrate flag in the raw_inode */ |
4300 | raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE); | |
9b8f1f01 MC |
4301 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
4302 | cpu_to_le32(EXT4_OS_HURD)) | |
a1ddeb7e BP |
4303 | raw_inode->i_file_acl_high = |
4304 | cpu_to_le16(ei->i_file_acl >> 32); | |
7973c0c1 | 4305 | raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); |
a48380f7 AK |
4306 | ext4_isize_set(raw_inode, ei->i_disksize); |
4307 | if (ei->i_disksize > 0x7fffffffULL) { | |
4308 | struct super_block *sb = inode->i_sb; | |
4309 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
4310 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || | |
4311 | EXT4_SB(sb)->s_es->s_rev_level == | |
4312 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { | |
4313 | /* If this is the first large file | |
4314 | * created, add a flag to the superblock. | |
4315 | */ | |
4316 | err = ext4_journal_get_write_access(handle, | |
4317 | EXT4_SB(sb)->s_sbh); | |
4318 | if (err) | |
4319 | goto out_brelse; | |
4320 | ext4_update_dynamic_rev(sb); | |
4321 | EXT4_SET_RO_COMPAT_FEATURE(sb, | |
617ba13b | 4322 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
a48380f7 AK |
4323 | sb->s_dirt = 1; |
4324 | handle->h_sync = 1; | |
4325 | err = ext4_journal_dirty_metadata(handle, | |
4326 | EXT4_SB(sb)->s_sbh); | |
ac27a0ec DK |
4327 | } |
4328 | } | |
4329 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | |
4330 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
4331 | if (old_valid_dev(inode->i_rdev)) { | |
4332 | raw_inode->i_block[0] = | |
4333 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | |
4334 | raw_inode->i_block[1] = 0; | |
4335 | } else { | |
4336 | raw_inode->i_block[0] = 0; | |
4337 | raw_inode->i_block[1] = | |
4338 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | |
4339 | raw_inode->i_block[2] = 0; | |
4340 | } | |
617ba13b | 4341 | } else for (block = 0; block < EXT4_N_BLOCKS; block++) |
ac27a0ec DK |
4342 | raw_inode->i_block[block] = ei->i_data[block]; |
4343 | ||
25ec56b5 JNC |
4344 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); |
4345 | if (ei->i_extra_isize) { | |
4346 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | |
4347 | raw_inode->i_version_hi = | |
4348 | cpu_to_le32(inode->i_version >> 32); | |
ac27a0ec | 4349 | raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); |
25ec56b5 JNC |
4350 | } |
4351 | ||
ac27a0ec | 4352 | |
617ba13b MC |
4353 | BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); |
4354 | rc = ext4_journal_dirty_metadata(handle, bh); | |
ac27a0ec DK |
4355 | if (!err) |
4356 | err = rc; | |
617ba13b | 4357 | ei->i_state &= ~EXT4_STATE_NEW; |
ac27a0ec DK |
4358 | |
4359 | out_brelse: | |
af5bc92d | 4360 | brelse(bh); |
617ba13b | 4361 | ext4_std_error(inode->i_sb, err); |
ac27a0ec DK |
4362 | return err; |
4363 | } | |
4364 | ||
4365 | /* | |
617ba13b | 4366 | * ext4_write_inode() |
ac27a0ec DK |
4367 | * |
4368 | * We are called from a few places: | |
4369 | * | |
4370 | * - Within generic_file_write() for O_SYNC files. | |
4371 | * Here, there will be no transaction running. We wait for any running | |
4372 | * trasnaction to commit. | |
4373 | * | |
4374 | * - Within sys_sync(), kupdate and such. | |
4375 | * We wait on commit, if tol to. | |
4376 | * | |
4377 | * - Within prune_icache() (PF_MEMALLOC == true) | |
4378 | * Here we simply return. We can't afford to block kswapd on the | |
4379 | * journal commit. | |
4380 | * | |
4381 | * In all cases it is actually safe for us to return without doing anything, | |
4382 | * because the inode has been copied into a raw inode buffer in | |
617ba13b | 4383 | * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for |
ac27a0ec DK |
4384 | * knfsd. |
4385 | * | |
4386 | * Note that we are absolutely dependent upon all inode dirtiers doing the | |
4387 | * right thing: they *must* call mark_inode_dirty() after dirtying info in | |
4388 | * which we are interested. | |
4389 | * | |
4390 | * It would be a bug for them to not do this. The code: | |
4391 | * | |
4392 | * mark_inode_dirty(inode) | |
4393 | * stuff(); | |
4394 | * inode->i_size = expr; | |
4395 | * | |
4396 | * is in error because a kswapd-driven write_inode() could occur while | |
4397 | * `stuff()' is running, and the new i_size will be lost. Plus the inode | |
4398 | * will no longer be on the superblock's dirty inode list. | |
4399 | */ | |
617ba13b | 4400 | int ext4_write_inode(struct inode *inode, int wait) |
ac27a0ec DK |
4401 | { |
4402 | if (current->flags & PF_MEMALLOC) | |
4403 | return 0; | |
4404 | ||
617ba13b | 4405 | if (ext4_journal_current_handle()) { |
b38bd33a | 4406 | jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); |
ac27a0ec DK |
4407 | dump_stack(); |
4408 | return -EIO; | |
4409 | } | |
4410 | ||
4411 | if (!wait) | |
4412 | return 0; | |
4413 | ||
617ba13b | 4414 | return ext4_force_commit(inode->i_sb); |
ac27a0ec DK |
4415 | } |
4416 | ||
4417 | /* | |
617ba13b | 4418 | * ext4_setattr() |
ac27a0ec DK |
4419 | * |
4420 | * Called from notify_change. | |
4421 | * | |
4422 | * We want to trap VFS attempts to truncate the file as soon as | |
4423 | * possible. In particular, we want to make sure that when the VFS | |
4424 | * shrinks i_size, we put the inode on the orphan list and modify | |
4425 | * i_disksize immediately, so that during the subsequent flushing of | |
4426 | * dirty pages and freeing of disk blocks, we can guarantee that any | |
4427 | * commit will leave the blocks being flushed in an unused state on | |
4428 | * disk. (On recovery, the inode will get truncated and the blocks will | |
4429 | * be freed, so we have a strong guarantee that no future commit will | |
4430 | * leave these blocks visible to the user.) | |
4431 | * | |
678aaf48 JK |
4432 | * Another thing we have to assure is that if we are in ordered mode |
4433 | * and inode is still attached to the committing transaction, we must | |
4434 | * we start writeout of all the dirty pages which are being truncated. | |
4435 | * This way we are sure that all the data written in the previous | |
4436 | * transaction are already on disk (truncate waits for pages under | |
4437 | * writeback). | |
4438 | * | |
4439 | * Called with inode->i_mutex down. | |
ac27a0ec | 4440 | */ |
617ba13b | 4441 | int ext4_setattr(struct dentry *dentry, struct iattr *attr) |
ac27a0ec DK |
4442 | { |
4443 | struct inode *inode = dentry->d_inode; | |
4444 | int error, rc = 0; | |
4445 | const unsigned int ia_valid = attr->ia_valid; | |
4446 | ||
4447 | error = inode_change_ok(inode, attr); | |
4448 | if (error) | |
4449 | return error; | |
4450 | ||
4451 | if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || | |
4452 | (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { | |
4453 | handle_t *handle; | |
4454 | ||
4455 | /* (user+group)*(old+new) structure, inode write (sb, | |
4456 | * inode block, ? - but truncate inode update has it) */ | |
617ba13b MC |
4457 | handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ |
4458 | EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); | |
ac27a0ec DK |
4459 | if (IS_ERR(handle)) { |
4460 | error = PTR_ERR(handle); | |
4461 | goto err_out; | |
4462 | } | |
4463 | error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0; | |
4464 | if (error) { | |
617ba13b | 4465 | ext4_journal_stop(handle); |
ac27a0ec DK |
4466 | return error; |
4467 | } | |
4468 | /* Update corresponding info in inode so that everything is in | |
4469 | * one transaction */ | |
4470 | if (attr->ia_valid & ATTR_UID) | |
4471 | inode->i_uid = attr->ia_uid; | |
4472 | if (attr->ia_valid & ATTR_GID) | |
4473 | inode->i_gid = attr->ia_gid; | |
617ba13b MC |
4474 | error = ext4_mark_inode_dirty(handle, inode); |
4475 | ext4_journal_stop(handle); | |
ac27a0ec DK |
4476 | } |
4477 | ||
e2b46574 ES |
4478 | if (attr->ia_valid & ATTR_SIZE) { |
4479 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { | |
4480 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
4481 | ||
4482 | if (attr->ia_size > sbi->s_bitmap_maxbytes) { | |
4483 | error = -EFBIG; | |
4484 | goto err_out; | |
4485 | } | |
4486 | } | |
4487 | } | |
4488 | ||
ac27a0ec DK |
4489 | if (S_ISREG(inode->i_mode) && |
4490 | attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { | |
4491 | handle_t *handle; | |
4492 | ||
617ba13b | 4493 | handle = ext4_journal_start(inode, 3); |
ac27a0ec DK |
4494 | if (IS_ERR(handle)) { |
4495 | error = PTR_ERR(handle); | |
4496 | goto err_out; | |
4497 | } | |
4498 | ||
617ba13b MC |
4499 | error = ext4_orphan_add(handle, inode); |
4500 | EXT4_I(inode)->i_disksize = attr->ia_size; | |
4501 | rc = ext4_mark_inode_dirty(handle, inode); | |
ac27a0ec DK |
4502 | if (!error) |
4503 | error = rc; | |
617ba13b | 4504 | ext4_journal_stop(handle); |
678aaf48 JK |
4505 | |
4506 | if (ext4_should_order_data(inode)) { | |
4507 | error = ext4_begin_ordered_truncate(inode, | |
4508 | attr->ia_size); | |
4509 | if (error) { | |
4510 | /* Do as much error cleanup as possible */ | |
4511 | handle = ext4_journal_start(inode, 3); | |
4512 | if (IS_ERR(handle)) { | |
4513 | ext4_orphan_del(NULL, inode); | |
4514 | goto err_out; | |
4515 | } | |
4516 | ext4_orphan_del(handle, inode); | |
4517 | ext4_journal_stop(handle); | |
4518 | goto err_out; | |
4519 | } | |
4520 | } | |
ac27a0ec DK |
4521 | } |
4522 | ||
4523 | rc = inode_setattr(inode, attr); | |
4524 | ||
617ba13b | 4525 | /* If inode_setattr's call to ext4_truncate failed to get a |
ac27a0ec DK |
4526 | * transaction handle at all, we need to clean up the in-core |
4527 | * orphan list manually. */ | |
4528 | if (inode->i_nlink) | |
617ba13b | 4529 | ext4_orphan_del(NULL, inode); |
ac27a0ec DK |
4530 | |
4531 | if (!rc && (ia_valid & ATTR_MODE)) | |
617ba13b | 4532 | rc = ext4_acl_chmod(inode); |
ac27a0ec DK |
4533 | |
4534 | err_out: | |
617ba13b | 4535 | ext4_std_error(inode->i_sb, error); |
ac27a0ec DK |
4536 | if (!error) |
4537 | error = rc; | |
4538 | return error; | |
4539 | } | |
4540 | ||
3e3398a0 MC |
4541 | int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, |
4542 | struct kstat *stat) | |
4543 | { | |
4544 | struct inode *inode; | |
4545 | unsigned long delalloc_blocks; | |
4546 | ||
4547 | inode = dentry->d_inode; | |
4548 | generic_fillattr(inode, stat); | |
4549 | ||
4550 | /* | |
4551 | * We can't update i_blocks if the block allocation is delayed | |
4552 | * otherwise in the case of system crash before the real block | |
4553 | * allocation is done, we will have i_blocks inconsistent with | |
4554 | * on-disk file blocks. | |
4555 | * We always keep i_blocks updated together with real | |
4556 | * allocation. But to not confuse with user, stat | |
4557 | * will return the blocks that include the delayed allocation | |
4558 | * blocks for this file. | |
4559 | */ | |
4560 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | |
4561 | delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; | |
4562 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | |
4563 | ||
4564 | stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; | |
4565 | return 0; | |
4566 | } | |
ac27a0ec | 4567 | |
a02908f1 MC |
4568 | static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, |
4569 | int chunk) | |
4570 | { | |
4571 | int indirects; | |
4572 | ||
4573 | /* if nrblocks are contiguous */ | |
4574 | if (chunk) { | |
4575 | /* | |
4576 | * With N contiguous data blocks, it need at most | |
4577 | * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks | |
4578 | * 2 dindirect blocks | |
4579 | * 1 tindirect block | |
4580 | */ | |
4581 | indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); | |
4582 | return indirects + 3; | |
4583 | } | |
4584 | /* | |
4585 | * if nrblocks are not contiguous, worse case, each block touch | |
4586 | * a indirect block, and each indirect block touch a double indirect | |
4587 | * block, plus a triple indirect block | |
4588 | */ | |
4589 | indirects = nrblocks * 2 + 1; | |
4590 | return indirects; | |
4591 | } | |
4592 | ||
4593 | static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) | |
4594 | { | |
4595 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) | |
ac51d837 TT |
4596 | return ext4_indirect_trans_blocks(inode, nrblocks, chunk); |
4597 | return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); | |
a02908f1 | 4598 | } |
ac51d837 | 4599 | |
ac27a0ec | 4600 | /* |
a02908f1 MC |
4601 | * Account for index blocks, block groups bitmaps and block group |
4602 | * descriptor blocks if modify datablocks and index blocks | |
4603 | * worse case, the indexs blocks spread over different block groups | |
ac27a0ec | 4604 | * |
a02908f1 MC |
4605 | * If datablocks are discontiguous, they are possible to spread over |
4606 | * different block groups too. If they are contiugous, with flexbg, | |
4607 | * they could still across block group boundary. | |
ac27a0ec | 4608 | * |
a02908f1 MC |
4609 | * Also account for superblock, inode, quota and xattr blocks |
4610 | */ | |
4611 | int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) | |
4612 | { | |
4613 | int groups, gdpblocks; | |
4614 | int idxblocks; | |
4615 | int ret = 0; | |
4616 | ||
4617 | /* | |
4618 | * How many index blocks need to touch to modify nrblocks? | |
4619 | * The "Chunk" flag indicating whether the nrblocks is | |
4620 | * physically contiguous on disk | |
4621 | * | |
4622 | * For Direct IO and fallocate, they calls get_block to allocate | |
4623 | * one single extent at a time, so they could set the "Chunk" flag | |
4624 | */ | |
4625 | idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); | |
4626 | ||
4627 | ret = idxblocks; | |
4628 | ||
4629 | /* | |
4630 | * Now let's see how many group bitmaps and group descriptors need | |
4631 | * to account | |
4632 | */ | |
4633 | groups = idxblocks; | |
4634 | if (chunk) | |
4635 | groups += 1; | |
4636 | else | |
4637 | groups += nrblocks; | |
4638 | ||
4639 | gdpblocks = groups; | |
4640 | if (groups > EXT4_SB(inode->i_sb)->s_groups_count) | |
4641 | groups = EXT4_SB(inode->i_sb)->s_groups_count; | |
4642 | if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) | |
4643 | gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; | |
4644 | ||
4645 | /* bitmaps and block group descriptor blocks */ | |
4646 | ret += groups + gdpblocks; | |
4647 | ||
4648 | /* Blocks for super block, inode, quota and xattr blocks */ | |
4649 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); | |
4650 | ||
4651 | return ret; | |
4652 | } | |
4653 | ||
4654 | /* | |
4655 | * Calulate the total number of credits to reserve to fit | |
f3bd1f3f MC |
4656 | * the modification of a single pages into a single transaction, |
4657 | * which may include multiple chunks of block allocations. | |
ac27a0ec | 4658 | * |
525f4ed8 | 4659 | * This could be called via ext4_write_begin() |
ac27a0ec | 4660 | * |
525f4ed8 | 4661 | * We need to consider the worse case, when |
a02908f1 | 4662 | * one new block per extent. |
ac27a0ec | 4663 | */ |
a86c6181 | 4664 | int ext4_writepage_trans_blocks(struct inode *inode) |
ac27a0ec | 4665 | { |
617ba13b | 4666 | int bpp = ext4_journal_blocks_per_page(inode); |
ac27a0ec DK |
4667 | int ret; |
4668 | ||
a02908f1 | 4669 | ret = ext4_meta_trans_blocks(inode, bpp, 0); |
a86c6181 | 4670 | |
a02908f1 | 4671 | /* Account for data blocks for journalled mode */ |
617ba13b | 4672 | if (ext4_should_journal_data(inode)) |
a02908f1 | 4673 | ret += bpp; |
ac27a0ec DK |
4674 | return ret; |
4675 | } | |
f3bd1f3f MC |
4676 | |
4677 | /* | |
4678 | * Calculate the journal credits for a chunk of data modification. | |
4679 | * | |
4680 | * This is called from DIO, fallocate or whoever calling | |
4681 | * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks. | |
4682 | * | |
4683 | * journal buffers for data blocks are not included here, as DIO | |
4684 | * and fallocate do no need to journal data buffers. | |
4685 | */ | |
4686 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) | |
4687 | { | |
4688 | return ext4_meta_trans_blocks(inode, nrblocks, 1); | |
4689 | } | |
4690 | ||
ac27a0ec | 4691 | /* |
617ba13b | 4692 | * The caller must have previously called ext4_reserve_inode_write(). |
ac27a0ec DK |
4693 | * Give this, we know that the caller already has write access to iloc->bh. |
4694 | */ | |
617ba13b MC |
4695 | int ext4_mark_iloc_dirty(handle_t *handle, |
4696 | struct inode *inode, struct ext4_iloc *iloc) | |
ac27a0ec DK |
4697 | { |
4698 | int err = 0; | |
4699 | ||
25ec56b5 JNC |
4700 | if (test_opt(inode->i_sb, I_VERSION)) |
4701 | inode_inc_iversion(inode); | |
4702 | ||
ac27a0ec DK |
4703 | /* the do_update_inode consumes one bh->b_count */ |
4704 | get_bh(iloc->bh); | |
4705 | ||
dab291af | 4706 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ |
617ba13b | 4707 | err = ext4_do_update_inode(handle, inode, iloc); |
ac27a0ec DK |
4708 | put_bh(iloc->bh); |
4709 | return err; | |
4710 | } | |
4711 | ||
4712 | /* | |
4713 | * On success, We end up with an outstanding reference count against | |
4714 | * iloc->bh. This _must_ be cleaned up later. | |
4715 | */ | |
4716 | ||
4717 | int | |
617ba13b MC |
4718 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, |
4719 | struct ext4_iloc *iloc) | |
ac27a0ec DK |
4720 | { |
4721 | int err = 0; | |
4722 | if (handle) { | |
617ba13b | 4723 | err = ext4_get_inode_loc(inode, iloc); |
ac27a0ec DK |
4724 | if (!err) { |
4725 | BUFFER_TRACE(iloc->bh, "get_write_access"); | |
617ba13b | 4726 | err = ext4_journal_get_write_access(handle, iloc->bh); |
ac27a0ec DK |
4727 | if (err) { |
4728 | brelse(iloc->bh); | |
4729 | iloc->bh = NULL; | |
4730 | } | |
4731 | } | |
4732 | } | |
617ba13b | 4733 | ext4_std_error(inode->i_sb, err); |
ac27a0ec DK |
4734 | return err; |
4735 | } | |
4736 | ||
6dd4ee7c KS |
4737 | /* |
4738 | * Expand an inode by new_extra_isize bytes. | |
4739 | * Returns 0 on success or negative error number on failure. | |
4740 | */ | |
1d03ec98 AK |
4741 | static int ext4_expand_extra_isize(struct inode *inode, |
4742 | unsigned int new_extra_isize, | |
4743 | struct ext4_iloc iloc, | |
4744 | handle_t *handle) | |
6dd4ee7c KS |
4745 | { |
4746 | struct ext4_inode *raw_inode; | |
4747 | struct ext4_xattr_ibody_header *header; | |
4748 | struct ext4_xattr_entry *entry; | |
4749 | ||
4750 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) | |
4751 | return 0; | |
4752 | ||
4753 | raw_inode = ext4_raw_inode(&iloc); | |
4754 | ||
4755 | header = IHDR(inode, raw_inode); | |
4756 | entry = IFIRST(header); | |
4757 | ||
4758 | /* No extended attributes present */ | |
4759 | if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || | |
4760 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { | |
4761 | memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, | |
4762 | new_extra_isize); | |
4763 | EXT4_I(inode)->i_extra_isize = new_extra_isize; | |
4764 | return 0; | |
4765 | } | |
4766 | ||
4767 | /* try to expand with EAs present */ | |
4768 | return ext4_expand_extra_isize_ea(inode, new_extra_isize, | |
4769 | raw_inode, handle); | |
4770 | } | |
4771 | ||
ac27a0ec DK |
4772 | /* |
4773 | * What we do here is to mark the in-core inode as clean with respect to inode | |
4774 | * dirtiness (it may still be data-dirty). | |
4775 | * This means that the in-core inode may be reaped by prune_icache | |
4776 | * without having to perform any I/O. This is a very good thing, | |
4777 | * because *any* task may call prune_icache - even ones which | |
4778 | * have a transaction open against a different journal. | |
4779 | * | |
4780 | * Is this cheating? Not really. Sure, we haven't written the | |
4781 | * inode out, but prune_icache isn't a user-visible syncing function. | |
4782 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) | |
4783 | * we start and wait on commits. | |
4784 | * | |
4785 | * Is this efficient/effective? Well, we're being nice to the system | |
4786 | * by cleaning up our inodes proactively so they can be reaped | |
4787 | * without I/O. But we are potentially leaving up to five seconds' | |
4788 | * worth of inodes floating about which prune_icache wants us to | |
4789 | * write out. One way to fix that would be to get prune_icache() | |
4790 | * to do a write_super() to free up some memory. It has the desired | |
4791 | * effect. | |
4792 | */ | |
617ba13b | 4793 | int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) |
ac27a0ec | 4794 | { |
617ba13b | 4795 | struct ext4_iloc iloc; |
6dd4ee7c KS |
4796 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
4797 | static unsigned int mnt_count; | |
4798 | int err, ret; | |
ac27a0ec DK |
4799 | |
4800 | might_sleep(); | |
617ba13b | 4801 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
6dd4ee7c KS |
4802 | if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && |
4803 | !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { | |
4804 | /* | |
4805 | * We need extra buffer credits since we may write into EA block | |
4806 | * with this same handle. If journal_extend fails, then it will | |
4807 | * only result in a minor loss of functionality for that inode. | |
4808 | * If this is felt to be critical, then e2fsck should be run to | |
4809 | * force a large enough s_min_extra_isize. | |
4810 | */ | |
4811 | if ((jbd2_journal_extend(handle, | |
4812 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { | |
4813 | ret = ext4_expand_extra_isize(inode, | |
4814 | sbi->s_want_extra_isize, | |
4815 | iloc, handle); | |
4816 | if (ret) { | |
4817 | EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; | |
c1bddad9 AK |
4818 | if (mnt_count != |
4819 | le16_to_cpu(sbi->s_es->s_mnt_count)) { | |
46e665e9 | 4820 | ext4_warning(inode->i_sb, __func__, |
6dd4ee7c KS |
4821 | "Unable to expand inode %lu. Delete" |
4822 | " some EAs or run e2fsck.", | |
4823 | inode->i_ino); | |
c1bddad9 AK |
4824 | mnt_count = |
4825 | le16_to_cpu(sbi->s_es->s_mnt_count); | |
6dd4ee7c KS |
4826 | } |
4827 | } | |
4828 | } | |
4829 | } | |
ac27a0ec | 4830 | if (!err) |
617ba13b | 4831 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec DK |
4832 | return err; |
4833 | } | |
4834 | ||
4835 | /* | |
617ba13b | 4836 | * ext4_dirty_inode() is called from __mark_inode_dirty() |
ac27a0ec DK |
4837 | * |
4838 | * We're really interested in the case where a file is being extended. | |
4839 | * i_size has been changed by generic_commit_write() and we thus need | |
4840 | * to include the updated inode in the current transaction. | |
4841 | * | |
4842 | * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks | |
4843 | * are allocated to the file. | |
4844 | * | |
4845 | * If the inode is marked synchronous, we don't honour that here - doing | |
4846 | * so would cause a commit on atime updates, which we don't bother doing. | |
4847 | * We handle synchronous inodes at the highest possible level. | |
4848 | */ | |
617ba13b | 4849 | void ext4_dirty_inode(struct inode *inode) |
ac27a0ec | 4850 | { |
617ba13b | 4851 | handle_t *current_handle = ext4_journal_current_handle(); |
ac27a0ec DK |
4852 | handle_t *handle; |
4853 | ||
617ba13b | 4854 | handle = ext4_journal_start(inode, 2); |
ac27a0ec DK |
4855 | if (IS_ERR(handle)) |
4856 | goto out; | |
4857 | if (current_handle && | |
4858 | current_handle->h_transaction != handle->h_transaction) { | |
4859 | /* This task has a transaction open against a different fs */ | |
4860 | printk(KERN_EMERG "%s: transactions do not match!\n", | |
46e665e9 | 4861 | __func__); |
ac27a0ec DK |
4862 | } else { |
4863 | jbd_debug(5, "marking dirty. outer handle=%p\n", | |
4864 | current_handle); | |
617ba13b | 4865 | ext4_mark_inode_dirty(handle, inode); |
ac27a0ec | 4866 | } |
617ba13b | 4867 | ext4_journal_stop(handle); |
ac27a0ec DK |
4868 | out: |
4869 | return; | |
4870 | } | |
4871 | ||
4872 | #if 0 | |
4873 | /* | |
4874 | * Bind an inode's backing buffer_head into this transaction, to prevent | |
4875 | * it from being flushed to disk early. Unlike | |
617ba13b | 4876 | * ext4_reserve_inode_write, this leaves behind no bh reference and |
ac27a0ec DK |
4877 | * returns no iloc structure, so the caller needs to repeat the iloc |
4878 | * lookup to mark the inode dirty later. | |
4879 | */ | |
617ba13b | 4880 | static int ext4_pin_inode(handle_t *handle, struct inode *inode) |
ac27a0ec | 4881 | { |
617ba13b | 4882 | struct ext4_iloc iloc; |
ac27a0ec DK |
4883 | |
4884 | int err = 0; | |
4885 | if (handle) { | |
617ba13b | 4886 | err = ext4_get_inode_loc(inode, &iloc); |
ac27a0ec DK |
4887 | if (!err) { |
4888 | BUFFER_TRACE(iloc.bh, "get_write_access"); | |
dab291af | 4889 | err = jbd2_journal_get_write_access(handle, iloc.bh); |
ac27a0ec | 4890 | if (!err) |
617ba13b | 4891 | err = ext4_journal_dirty_metadata(handle, |
ac27a0ec DK |
4892 | iloc.bh); |
4893 | brelse(iloc.bh); | |
4894 | } | |
4895 | } | |
617ba13b | 4896 | ext4_std_error(inode->i_sb, err); |
ac27a0ec DK |
4897 | return err; |
4898 | } | |
4899 | #endif | |
4900 | ||
617ba13b | 4901 | int ext4_change_inode_journal_flag(struct inode *inode, int val) |
ac27a0ec DK |
4902 | { |
4903 | journal_t *journal; | |
4904 | handle_t *handle; | |
4905 | int err; | |
4906 | ||
4907 | /* | |
4908 | * We have to be very careful here: changing a data block's | |
4909 | * journaling status dynamically is dangerous. If we write a | |
4910 | * data block to the journal, change the status and then delete | |
4911 | * that block, we risk forgetting to revoke the old log record | |
4912 | * from the journal and so a subsequent replay can corrupt data. | |
4913 | * So, first we make sure that the journal is empty and that | |
4914 | * nobody is changing anything. | |
4915 | */ | |
4916 | ||
617ba13b | 4917 | journal = EXT4_JOURNAL(inode); |
d699594d | 4918 | if (is_journal_aborted(journal)) |
ac27a0ec DK |
4919 | return -EROFS; |
4920 | ||
dab291af MC |
4921 | jbd2_journal_lock_updates(journal); |
4922 | jbd2_journal_flush(journal); | |
ac27a0ec DK |
4923 | |
4924 | /* | |
4925 | * OK, there are no updates running now, and all cached data is | |
4926 | * synced to disk. We are now in a completely consistent state | |
4927 | * which doesn't have anything in the journal, and we know that | |
4928 | * no filesystem updates are running, so it is safe to modify | |
4929 | * the inode's in-core data-journaling state flag now. | |
4930 | */ | |
4931 | ||
4932 | if (val) | |
617ba13b | 4933 | EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; |
ac27a0ec | 4934 | else |
617ba13b MC |
4935 | EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; |
4936 | ext4_set_aops(inode); | |
ac27a0ec | 4937 | |
dab291af | 4938 | jbd2_journal_unlock_updates(journal); |
ac27a0ec DK |
4939 | |
4940 | /* Finally we can mark the inode as dirty. */ | |
4941 | ||
617ba13b | 4942 | handle = ext4_journal_start(inode, 1); |
ac27a0ec DK |
4943 | if (IS_ERR(handle)) |
4944 | return PTR_ERR(handle); | |
4945 | ||
617ba13b | 4946 | err = ext4_mark_inode_dirty(handle, inode); |
ac27a0ec | 4947 | handle->h_sync = 1; |
617ba13b MC |
4948 | ext4_journal_stop(handle); |
4949 | ext4_std_error(inode->i_sb, err); | |
ac27a0ec DK |
4950 | |
4951 | return err; | |
4952 | } | |
2e9ee850 AK |
4953 | |
4954 | static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) | |
4955 | { | |
4956 | return !buffer_mapped(bh); | |
4957 | } | |
4958 | ||
4959 | int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
4960 | { | |
4961 | loff_t size; | |
4962 | unsigned long len; | |
4963 | int ret = -EINVAL; | |
79f0be8d | 4964 | void *fsdata; |
2e9ee850 AK |
4965 | struct file *file = vma->vm_file; |
4966 | struct inode *inode = file->f_path.dentry->d_inode; | |
4967 | struct address_space *mapping = inode->i_mapping; | |
4968 | ||
4969 | /* | |
4970 | * Get i_alloc_sem to stop truncates messing with the inode. We cannot | |
4971 | * get i_mutex because we are already holding mmap_sem. | |
4972 | */ | |
4973 | down_read(&inode->i_alloc_sem); | |
4974 | size = i_size_read(inode); | |
4975 | if (page->mapping != mapping || size <= page_offset(page) | |
4976 | || !PageUptodate(page)) { | |
4977 | /* page got truncated from under us? */ | |
4978 | goto out_unlock; | |
4979 | } | |
4980 | ret = 0; | |
4981 | if (PageMappedToDisk(page)) | |
4982 | goto out_unlock; | |
4983 | ||
4984 | if (page->index == size >> PAGE_CACHE_SHIFT) | |
4985 | len = size & ~PAGE_CACHE_MASK; | |
4986 | else | |
4987 | len = PAGE_CACHE_SIZE; | |
4988 | ||
4989 | if (page_has_buffers(page)) { | |
4990 | /* return if we have all the buffers mapped */ | |
4991 | if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, | |
4992 | ext4_bh_unmapped)) | |
4993 | goto out_unlock; | |
4994 | } | |
4995 | /* | |
4996 | * OK, we need to fill the hole... Do write_begin write_end | |
4997 | * to do block allocation/reservation.We are not holding | |
4998 | * inode.i__mutex here. That allow * parallel write_begin, | |
4999 | * write_end call. lock_page prevent this from happening | |
5000 | * on the same page though | |
5001 | */ | |
5002 | ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), | |
79f0be8d | 5003 | len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); |
2e9ee850 AK |
5004 | if (ret < 0) |
5005 | goto out_unlock; | |
5006 | ret = mapping->a_ops->write_end(file, mapping, page_offset(page), | |
79f0be8d | 5007 | len, len, page, fsdata); |
2e9ee850 AK |
5008 | if (ret < 0) |
5009 | goto out_unlock; | |
5010 | ret = 0; | |
5011 | out_unlock: | |
5012 | up_read(&inode->i_alloc_sem); | |
5013 | return ret; | |
5014 | } |