]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ext4/inode.c
convert ext4 to ->evict_inode()
[net-next-2.6.git] / fs / ext4 / inode.c
CommitLineData
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>
4c0425ff 40#include <linux/workqueue.h>
744692dc 41#include <linux/kernel.h>
5a0e3ad6 42#include <linux/slab.h>
9bffad1e 43
3dcf5451 44#include "ext4_jbd2.h"
ac27a0ec
DK
45#include "xattr.h"
46#include "acl.h"
d2a17637 47#include "ext4_extents.h"
ac27a0ec 48
9bffad1e
TT
49#include <trace/events/ext4.h>
50
a1d6cc56
AK
51#define MPAGE_DA_EXTENT_TAIL 0x01
52
678aaf48
JK
53static inline int ext4_begin_ordered_truncate(struct inode *inode,
54 loff_t new_size)
55{
7f5aa215
JK
56 return jbd2_journal_begin_ordered_truncate(
57 EXT4_SB(inode->i_sb)->s_journal,
58 &EXT4_I(inode)->jinode,
59 new_size);
678aaf48
JK
60}
61
64769240
AT
62static void ext4_invalidatepage(struct page *page, unsigned long offset);
63
ac27a0ec
DK
64/*
65 * Test whether an inode is a fast symlink.
66 */
617ba13b 67static int ext4_inode_is_fast_symlink(struct inode *inode)
ac27a0ec 68{
617ba13b 69 int ea_blocks = EXT4_I(inode)->i_file_acl ?
ac27a0ec
DK
70 (inode->i_sb->s_blocksize >> 9) : 0;
71
72 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
73}
74
ac27a0ec
DK
75/*
76 * Work out how many blocks we need to proceed with the next chunk of a
77 * truncate transaction.
78 */
79static unsigned long blocks_for_truncate(struct inode *inode)
80{
725d26d3 81 ext4_lblk_t needed;
ac27a0ec
DK
82
83 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
84
85 /* Give ourselves just enough room to cope with inodes in which
86 * i_blocks is corrupt: we've seen disk corruptions in the past
87 * which resulted in random data in an inode which looked enough
617ba13b 88 * like a regular file for ext4 to try to delete it. Things
ac27a0ec
DK
89 * will go a bit crazy if that happens, but at least we should
90 * try not to panic the whole kernel. */
91 if (needed < 2)
92 needed = 2;
93
94 /* But we need to bound the transaction so we don't overflow the
95 * journal. */
617ba13b
MC
96 if (needed > EXT4_MAX_TRANS_DATA)
97 needed = EXT4_MAX_TRANS_DATA;
ac27a0ec 98
617ba13b 99 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
ac27a0ec
DK
100}
101
102/*
103 * Truncate transactions can be complex and absolutely huge. So we need to
104 * be able to restart the transaction at a conventient checkpoint to make
105 * sure we don't overflow the journal.
106 *
107 * start_transaction gets us a new handle for a truncate transaction,
108 * and extend_transaction tries to extend the existing one a bit. If
109 * extend fails, we need to propagate the failure up and restart the
110 * transaction in the top-level truncate loop. --sct
111 */
112static handle_t *start_transaction(struct inode *inode)
113{
114 handle_t *result;
115
617ba13b 116 result = ext4_journal_start(inode, blocks_for_truncate(inode));
ac27a0ec
DK
117 if (!IS_ERR(result))
118 return result;
119
617ba13b 120 ext4_std_error(inode->i_sb, PTR_ERR(result));
ac27a0ec
DK
121 return result;
122}
123
124/*
125 * Try to extend this transaction for the purposes of truncation.
126 *
127 * Returns 0 if we managed to create more room. If we can't create more
128 * room, and the transaction must be restarted we return 1.
129 */
130static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
131{
0390131b
FM
132 if (!ext4_handle_valid(handle))
133 return 0;
134 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
ac27a0ec 135 return 0;
617ba13b 136 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
ac27a0ec
DK
137 return 0;
138 return 1;
139}
140
141/*
142 * Restart the transaction associated with *handle. This does a commit,
143 * so before we call here everything must be consistently dirtied against
144 * this transaction.
145 */
fa5d1113 146int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
487caeef 147 int nblocks)
ac27a0ec 148{
487caeef
JK
149 int ret;
150
151 /*
e35fd660 152 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
487caeef
JK
153 * moment, get_block can be called only for blocks inside i_size since
154 * page cache has been already dropped and writes are blocked by
155 * i_mutex. So we can safely drop the i_data_sem here.
156 */
0390131b 157 BUG_ON(EXT4_JOURNAL(inode) == NULL);
ac27a0ec 158 jbd_debug(2, "restarting handle %p\n", handle);
487caeef
JK
159 up_write(&EXT4_I(inode)->i_data_sem);
160 ret = ext4_journal_restart(handle, blocks_for_truncate(inode));
161 down_write(&EXT4_I(inode)->i_data_sem);
fa5d1113 162 ext4_discard_preallocations(inode);
487caeef
JK
163
164 return ret;
ac27a0ec
DK
165}
166
167/*
168 * Called at the last iput() if i_nlink is zero.
169 */
0930fcc1 170void ext4_evict_inode(struct inode *inode)
ac27a0ec
DK
171{
172 handle_t *handle;
bc965ab3 173 int err;
ac27a0ec 174
0930fcc1
AV
175 if (inode->i_nlink) {
176 truncate_inode_pages(&inode->i_data, 0);
177 goto no_delete;
178 }
179
907f4554 180 if (!is_bad_inode(inode))
871a2931 181 dquot_initialize(inode);
907f4554 182
678aaf48
JK
183 if (ext4_should_order_data(inode))
184 ext4_begin_ordered_truncate(inode, 0);
ac27a0ec
DK
185 truncate_inode_pages(&inode->i_data, 0);
186
187 if (is_bad_inode(inode))
188 goto no_delete;
189
bc965ab3 190 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
ac27a0ec 191 if (IS_ERR(handle)) {
bc965ab3 192 ext4_std_error(inode->i_sb, PTR_ERR(handle));
ac27a0ec
DK
193 /*
194 * If we're going to skip the normal cleanup, we still need to
195 * make sure that the in-core orphan linked list is properly
196 * cleaned up.
197 */
617ba13b 198 ext4_orphan_del(NULL, inode);
ac27a0ec
DK
199 goto no_delete;
200 }
201
202 if (IS_SYNC(inode))
0390131b 203 ext4_handle_sync(handle);
ac27a0ec 204 inode->i_size = 0;
bc965ab3
TT
205 err = ext4_mark_inode_dirty(handle, inode);
206 if (err) {
12062ddd 207 ext4_warning(inode->i_sb,
bc965ab3
TT
208 "couldn't mark inode dirty (err %d)", err);
209 goto stop_handle;
210 }
ac27a0ec 211 if (inode->i_blocks)
617ba13b 212 ext4_truncate(inode);
bc965ab3
TT
213
214 /*
215 * ext4_ext_truncate() doesn't reserve any slop when it
216 * restarts journal transactions; therefore there may not be
217 * enough credits left in the handle to remove the inode from
218 * the orphan list and set the dtime field.
219 */
0390131b 220 if (!ext4_handle_has_enough_credits(handle, 3)) {
bc965ab3
TT
221 err = ext4_journal_extend(handle, 3);
222 if (err > 0)
223 err = ext4_journal_restart(handle, 3);
224 if (err != 0) {
12062ddd 225 ext4_warning(inode->i_sb,
bc965ab3
TT
226 "couldn't extend journal (err %d)", err);
227 stop_handle:
228 ext4_journal_stop(handle);
229 goto no_delete;
230 }
231 }
232
ac27a0ec 233 /*
617ba13b 234 * Kill off the orphan record which ext4_truncate created.
ac27a0ec 235 * AKPM: I think this can be inside the above `if'.
617ba13b 236 * Note that ext4_orphan_del() has to be able to cope with the
ac27a0ec 237 * deletion of a non-existent orphan - this is because we don't
617ba13b 238 * know if ext4_truncate() actually created an orphan record.
ac27a0ec
DK
239 * (Well, we could do this if we need to, but heck - it works)
240 */
617ba13b
MC
241 ext4_orphan_del(handle, inode);
242 EXT4_I(inode)->i_dtime = get_seconds();
ac27a0ec
DK
243
244 /*
245 * One subtle ordering requirement: if anything has gone wrong
246 * (transaction abort, IO errors, whatever), then we can still
247 * do these next steps (the fs will already have been marked as
248 * having errors), but we can't free the inode if the mark_dirty
249 * fails.
250 */
617ba13b 251 if (ext4_mark_inode_dirty(handle, inode))
ac27a0ec 252 /* If that failed, just do the required in-core inode clear. */
0930fcc1 253 ext4_clear_inode(inode);
ac27a0ec 254 else
617ba13b
MC
255 ext4_free_inode(handle, inode);
256 ext4_journal_stop(handle);
ac27a0ec
DK
257 return;
258no_delete:
0930fcc1 259 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
ac27a0ec
DK
260}
261
262typedef struct {
263 __le32 *p;
264 __le32 key;
265 struct buffer_head *bh;
266} Indirect;
267
268static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
269{
270 p->key = *(p->p = v);
271 p->bh = bh;
272}
273
ac27a0ec 274/**
617ba13b 275 * ext4_block_to_path - parse the block number into array of offsets
ac27a0ec
DK
276 * @inode: inode in question (we are only interested in its superblock)
277 * @i_block: block number to be parsed
278 * @offsets: array to store the offsets in
8c55e204
DK
279 * @boundary: set this non-zero if the referred-to block is likely to be
280 * followed (on disk) by an indirect block.
ac27a0ec 281 *
617ba13b 282 * To store the locations of file's data ext4 uses a data structure common
ac27a0ec
DK
283 * for UNIX filesystems - tree of pointers anchored in the inode, with
284 * data blocks at leaves and indirect blocks in intermediate nodes.
285 * This function translates the block number into path in that tree -
286 * return value is the path length and @offsets[n] is the offset of
287 * pointer to (n+1)th node in the nth one. If @block is out of range
288 * (negative or too large) warning is printed and zero returned.
289 *
290 * Note: function doesn't find node addresses, so no IO is needed. All
291 * we need to know is the capacity of indirect blocks (taken from the
292 * inode->i_sb).
293 */
294
295/*
296 * Portability note: the last comparison (check that we fit into triple
297 * indirect block) is spelled differently, because otherwise on an
298 * architecture with 32-bit longs and 8Kb pages we might get into trouble
299 * if our filesystem had 8Kb blocks. We might use long long, but that would
300 * kill us on x86. Oh, well, at least the sign propagation does not matter -
301 * i_block would have to be negative in the very beginning, so we would not
302 * get there at all.
303 */
304
617ba13b 305static int ext4_block_to_path(struct inode *inode,
de9a55b8
TT
306 ext4_lblk_t i_block,
307 ext4_lblk_t offsets[4], int *boundary)
ac27a0ec 308{
617ba13b
MC
309 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
310 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
311 const long direct_blocks = EXT4_NDIR_BLOCKS,
ac27a0ec
DK
312 indirect_blocks = ptrs,
313 double_blocks = (1 << (ptrs_bits * 2));
314 int n = 0;
315 int final = 0;
316
c333e073 317 if (i_block < direct_blocks) {
ac27a0ec
DK
318 offsets[n++] = i_block;
319 final = direct_blocks;
af5bc92d 320 } else if ((i_block -= direct_blocks) < indirect_blocks) {
617ba13b 321 offsets[n++] = EXT4_IND_BLOCK;
ac27a0ec
DK
322 offsets[n++] = i_block;
323 final = ptrs;
324 } else if ((i_block -= indirect_blocks) < double_blocks) {
617ba13b 325 offsets[n++] = EXT4_DIND_BLOCK;
ac27a0ec
DK
326 offsets[n++] = i_block >> ptrs_bits;
327 offsets[n++] = i_block & (ptrs - 1);
328 final = ptrs;
329 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
617ba13b 330 offsets[n++] = EXT4_TIND_BLOCK;
ac27a0ec
DK
331 offsets[n++] = i_block >> (ptrs_bits * 2);
332 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
333 offsets[n++] = i_block & (ptrs - 1);
334 final = ptrs;
335 } else {
12062ddd 336 ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
de9a55b8
TT
337 i_block + direct_blocks +
338 indirect_blocks + double_blocks, inode->i_ino);
ac27a0ec
DK
339 }
340 if (boundary)
341 *boundary = final - 1 - (i_block & (ptrs - 1));
342 return n;
343}
344
fe2c8191 345static int __ext4_check_blockref(const char *function, struct inode *inode,
6fd058f7
TT
346 __le32 *p, unsigned int max)
347{
f73953c0 348 __le32 *bref = p;
6fd058f7
TT
349 unsigned int blk;
350
fe2c8191 351 while (bref < p+max) {
6fd058f7 352 blk = le32_to_cpu(*bref++);
de9a55b8
TT
353 if (blk &&
354 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
6fd058f7 355 blk, 1))) {
24676da4
TT
356 ext4_error_inode(function, inode,
357 "invalid block reference %u", blk);
de9a55b8
TT
358 return -EIO;
359 }
360 }
361 return 0;
fe2c8191
TN
362}
363
364
365#define ext4_check_indirect_blockref(inode, bh) \
de9a55b8 366 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
fe2c8191
TN
367 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
368
369#define ext4_check_inode_blockref(inode) \
de9a55b8 370 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
fe2c8191
TN
371 EXT4_NDIR_BLOCKS)
372
ac27a0ec 373/**
617ba13b 374 * ext4_get_branch - read the chain of indirect blocks leading to data
ac27a0ec
DK
375 * @inode: inode in question
376 * @depth: depth of the chain (1 - direct pointer, etc.)
377 * @offsets: offsets of pointers in inode/indirect blocks
378 * @chain: place to store the result
379 * @err: here we store the error value
380 *
381 * Function fills the array of triples <key, p, bh> and returns %NULL
382 * if everything went OK or the pointer to the last filled triple
383 * (incomplete one) otherwise. Upon the return chain[i].key contains
384 * the number of (i+1)-th block in the chain (as it is stored in memory,
385 * i.e. little-endian 32-bit), chain[i].p contains the address of that
386 * number (it points into struct inode for i==0 and into the bh->b_data
387 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
388 * block for i>0 and NULL for i==0. In other words, it holds the block
389 * numbers of the chain, addresses they were taken from (and where we can
390 * verify that chain did not change) and buffer_heads hosting these
391 * numbers.
392 *
393 * Function stops when it stumbles upon zero pointer (absent block)
394 * (pointer to last triple returned, *@err == 0)
395 * or when it gets an IO error reading an indirect block
396 * (ditto, *@err == -EIO)
ac27a0ec
DK
397 * or when it reads all @depth-1 indirect blocks successfully and finds
398 * the whole chain, all way to the data (returns %NULL, *err == 0).
c278bfec
AK
399 *
400 * Need to be called with
0e855ac8 401 * down_read(&EXT4_I(inode)->i_data_sem)
ac27a0ec 402 */
725d26d3
AK
403static Indirect *ext4_get_branch(struct inode *inode, int depth,
404 ext4_lblk_t *offsets,
ac27a0ec
DK
405 Indirect chain[4], int *err)
406{
407 struct super_block *sb = inode->i_sb;
408 Indirect *p = chain;
409 struct buffer_head *bh;
410
411 *err = 0;
412 /* i_data is not going away, no lock needed */
af5bc92d 413 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
ac27a0ec
DK
414 if (!p->key)
415 goto no_block;
416 while (--depth) {
fe2c8191
TN
417 bh = sb_getblk(sb, le32_to_cpu(p->key));
418 if (unlikely(!bh))
ac27a0ec 419 goto failure;
de9a55b8 420
fe2c8191
TN
421 if (!bh_uptodate_or_lock(bh)) {
422 if (bh_submit_read(bh) < 0) {
423 put_bh(bh);
424 goto failure;
425 }
426 /* validate block references */
427 if (ext4_check_indirect_blockref(inode, bh)) {
428 put_bh(bh);
429 goto failure;
430 }
431 }
de9a55b8 432
af5bc92d 433 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
ac27a0ec
DK
434 /* Reader: end */
435 if (!p->key)
436 goto no_block;
437 }
438 return NULL;
439
ac27a0ec
DK
440failure:
441 *err = -EIO;
442no_block:
443 return p;
444}
445
446/**
617ba13b 447 * ext4_find_near - find a place for allocation with sufficient locality
ac27a0ec
DK
448 * @inode: owner
449 * @ind: descriptor of indirect block.
450 *
1cc8dcf5 451 * This function returns the preferred place for block allocation.
ac27a0ec
DK
452 * It is used when heuristic for sequential allocation fails.
453 * Rules are:
454 * + if there is a block to the left of our position - allocate near it.
455 * + if pointer will live in indirect block - allocate near that block.
456 * + if pointer will live in inode - allocate in the same
457 * cylinder group.
458 *
459 * In the latter case we colour the starting block by the callers PID to
460 * prevent it from clashing with concurrent allocations for a different inode
461 * in the same block group. The PID is used here so that functionally related
462 * files will be close-by on-disk.
463 *
464 * Caller must make sure that @ind is valid and will stay that way.
465 */
617ba13b 466static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
ac27a0ec 467{
617ba13b 468 struct ext4_inode_info *ei = EXT4_I(inode);
af5bc92d 469 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
ac27a0ec 470 __le32 *p;
617ba13b 471 ext4_fsblk_t bg_start;
74d3487f 472 ext4_fsblk_t last_block;
617ba13b 473 ext4_grpblk_t colour;
a4912123
TT
474 ext4_group_t block_group;
475 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
ac27a0ec
DK
476
477 /* Try to find previous block */
478 for (p = ind->p - 1; p >= start; p--) {
479 if (*p)
480 return le32_to_cpu(*p);
481 }
482
483 /* No such thing, so let's try location of indirect block */
484 if (ind->bh)
485 return ind->bh->b_blocknr;
486
487 /*
488 * It is going to be referred to from the inode itself? OK, just put it
489 * into the same cylinder group then.
490 */
a4912123
TT
491 block_group = ei->i_block_group;
492 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
493 block_group &= ~(flex_size-1);
494 if (S_ISREG(inode->i_mode))
495 block_group++;
496 }
497 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
74d3487f
VC
498 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
499
a4912123
TT
500 /*
501 * If we are doing delayed allocation, we don't need take
502 * colour into account.
503 */
504 if (test_opt(inode->i_sb, DELALLOC))
505 return bg_start;
506
74d3487f
VC
507 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
508 colour = (current->pid % 16) *
617ba13b 509 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
74d3487f
VC
510 else
511 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
ac27a0ec
DK
512 return bg_start + colour;
513}
514
515/**
1cc8dcf5 516 * ext4_find_goal - find a preferred place for allocation.
ac27a0ec
DK
517 * @inode: owner
518 * @block: block we want
ac27a0ec 519 * @partial: pointer to the last triple within a chain
ac27a0ec 520 *
1cc8dcf5 521 * Normally this function find the preferred place for block allocation,
fb01bfda 522 * returns it.
fb0a387d
ES
523 * Because this is only used for non-extent files, we limit the block nr
524 * to 32 bits.
ac27a0ec 525 */
725d26d3 526static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
de9a55b8 527 Indirect *partial)
ac27a0ec 528{
fb0a387d
ES
529 ext4_fsblk_t goal;
530
ac27a0ec 531 /*
c2ea3fde 532 * XXX need to get goal block from mballoc's data structures
ac27a0ec 533 */
ac27a0ec 534
fb0a387d
ES
535 goal = ext4_find_near(inode, partial);
536 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
537 return goal;
ac27a0ec
DK
538}
539
540/**
617ba13b 541 * ext4_blks_to_allocate: Look up the block map and count the number
ac27a0ec
DK
542 * of direct blocks need to be allocated for the given branch.
543 *
544 * @branch: chain of indirect blocks
545 * @k: number of blocks need for indirect blocks
546 * @blks: number of data blocks to be mapped.
547 * @blocks_to_boundary: the offset in the indirect block
548 *
549 * return the total number of blocks to be allocate, including the
550 * direct and indirect blocks.
551 */
498e5f24 552static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
de9a55b8 553 int blocks_to_boundary)
ac27a0ec 554{
498e5f24 555 unsigned int count = 0;
ac27a0ec
DK
556
557 /*
558 * Simple case, [t,d]Indirect block(s) has not allocated yet
559 * then it's clear blocks on that path have not allocated
560 */
561 if (k > 0) {
562 /* right now we don't handle cross boundary allocation */
563 if (blks < blocks_to_boundary + 1)
564 count += blks;
565 else
566 count += blocks_to_boundary + 1;
567 return count;
568 }
569
570 count++;
571 while (count < blks && count <= blocks_to_boundary &&
572 le32_to_cpu(*(branch[0].p + count)) == 0) {
573 count++;
574 }
575 return count;
576}
577
578/**
617ba13b 579 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
ac27a0ec
DK
580 * @indirect_blks: the number of blocks need to allocate for indirect
581 * blocks
582 *
583 * @new_blocks: on return it will store the new block numbers for
584 * the indirect blocks(if needed) and the first direct block,
585 * @blks: on return it will store the total number of allocated
586 * direct blocks
587 */
617ba13b 588static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
de9a55b8
TT
589 ext4_lblk_t iblock, ext4_fsblk_t goal,
590 int indirect_blks, int blks,
591 ext4_fsblk_t new_blocks[4], int *err)
ac27a0ec 592{
815a1130 593 struct ext4_allocation_request ar;
ac27a0ec 594 int target, i;
7061eba7 595 unsigned long count = 0, blk_allocated = 0;
ac27a0ec 596 int index = 0;
617ba13b 597 ext4_fsblk_t current_block = 0;
ac27a0ec
DK
598 int ret = 0;
599
600 /*
601 * Here we try to allocate the requested multiple blocks at once,
602 * on a best-effort basis.
603 * To build a branch, we should allocate blocks for
604 * the indirect blocks(if not allocated yet), and at least
605 * the first direct block of this branch. That's the
606 * minimum number of blocks need to allocate(required)
607 */
7061eba7
AK
608 /* first we try to allocate the indirect blocks */
609 target = indirect_blks;
610 while (target > 0) {
ac27a0ec
DK
611 count = target;
612 /* allocating blocks for indirect blocks and direct blocks */
7061eba7
AK
613 current_block = ext4_new_meta_blocks(handle, inode,
614 goal, &count, err);
ac27a0ec
DK
615 if (*err)
616 goto failed_out;
617
273df556
FM
618 if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) {
619 EXT4_ERROR_INODE(inode,
620 "current_block %llu + count %lu > %d!",
621 current_block, count,
622 EXT4_MAX_BLOCK_FILE_PHYS);
623 *err = -EIO;
624 goto failed_out;
625 }
fb0a387d 626
ac27a0ec
DK
627 target -= count;
628 /* allocate blocks for indirect blocks */
629 while (index < indirect_blks && count) {
630 new_blocks[index++] = current_block++;
631 count--;
632 }
7061eba7
AK
633 if (count > 0) {
634 /*
635 * save the new block number
636 * for the first direct block
637 */
638 new_blocks[index] = current_block;
639 printk(KERN_INFO "%s returned more blocks than "
640 "requested\n", __func__);
641 WARN_ON(1);
ac27a0ec 642 break;
7061eba7 643 }
ac27a0ec
DK
644 }
645
7061eba7
AK
646 target = blks - count ;
647 blk_allocated = count;
648 if (!target)
649 goto allocated;
650 /* Now allocate data blocks */
815a1130
TT
651 memset(&ar, 0, sizeof(ar));
652 ar.inode = inode;
653 ar.goal = goal;
654 ar.len = target;
655 ar.logical = iblock;
656 if (S_ISREG(inode->i_mode))
657 /* enable in-core preallocation only for regular files */
658 ar.flags = EXT4_MB_HINT_DATA;
659
660 current_block = ext4_mb_new_blocks(handle, &ar, err);
273df556
FM
661 if (unlikely(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS)) {
662 EXT4_ERROR_INODE(inode,
663 "current_block %llu + ar.len %d > %d!",
664 current_block, ar.len,
665 EXT4_MAX_BLOCK_FILE_PHYS);
666 *err = -EIO;
667 goto failed_out;
668 }
815a1130 669
7061eba7
AK
670 if (*err && (target == blks)) {
671 /*
672 * if the allocation failed and we didn't allocate
673 * any blocks before
674 */
675 goto failed_out;
676 }
677 if (!*err) {
678 if (target == blks) {
de9a55b8
TT
679 /*
680 * save the new block number
681 * for the first direct block
682 */
7061eba7
AK
683 new_blocks[index] = current_block;
684 }
815a1130 685 blk_allocated += ar.len;
7061eba7
AK
686 }
687allocated:
ac27a0ec 688 /* total number of blocks allocated for direct blocks */
7061eba7 689 ret = blk_allocated;
ac27a0ec
DK
690 *err = 0;
691 return ret;
692failed_out:
af5bc92d 693 for (i = 0; i < index; i++)
e6362609 694 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
ac27a0ec
DK
695 return ret;
696}
697
698/**
617ba13b 699 * ext4_alloc_branch - allocate and set up a chain of blocks.
ac27a0ec
DK
700 * @inode: owner
701 * @indirect_blks: number of allocated indirect blocks
702 * @blks: number of allocated direct blocks
703 * @offsets: offsets (in the blocks) to store the pointers to next.
704 * @branch: place to store the chain in.
705 *
706 * This function allocates blocks, zeroes out all but the last one,
707 * links them into chain and (if we are synchronous) writes them to disk.
708 * In other words, it prepares a branch that can be spliced onto the
709 * inode. It stores the information about that chain in the branch[], in
617ba13b 710 * the same format as ext4_get_branch() would do. We are calling it after
ac27a0ec
DK
711 * we had read the existing part of chain and partial points to the last
712 * triple of that (one with zero ->key). Upon the exit we have the same
617ba13b 713 * picture as after the successful ext4_get_block(), except that in one
ac27a0ec
DK
714 * place chain is disconnected - *branch->p is still zero (we did not
715 * set the last link), but branch->key contains the number that should
716 * be placed into *branch->p to fill that gap.
717 *
718 * If allocation fails we free all blocks we've allocated (and forget
719 * their buffer_heads) and return the error value the from failed
617ba13b 720 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
ac27a0ec
DK
721 * as described above and return 0.
722 */
617ba13b 723static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
de9a55b8
TT
724 ext4_lblk_t iblock, int indirect_blks,
725 int *blks, ext4_fsblk_t goal,
726 ext4_lblk_t *offsets, Indirect *branch)
ac27a0ec
DK
727{
728 int blocksize = inode->i_sb->s_blocksize;
729 int i, n = 0;
730 int err = 0;
731 struct buffer_head *bh;
732 int num;
617ba13b
MC
733 ext4_fsblk_t new_blocks[4];
734 ext4_fsblk_t current_block;
ac27a0ec 735
7061eba7 736 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
ac27a0ec
DK
737 *blks, new_blocks, &err);
738 if (err)
739 return err;
740
741 branch[0].key = cpu_to_le32(new_blocks[0]);
742 /*
743 * metadata blocks and data blocks are allocated.
744 */
745 for (n = 1; n <= indirect_blks; n++) {
746 /*
747 * Get buffer_head for parent block, zero it out
748 * and set the pointer to new one, then send
749 * parent to disk.
750 */
751 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
752 branch[n].bh = bh;
753 lock_buffer(bh);
754 BUFFER_TRACE(bh, "call get_create_access");
617ba13b 755 err = ext4_journal_get_create_access(handle, bh);
ac27a0ec 756 if (err) {
6487a9d3
CW
757 /* Don't brelse(bh) here; it's done in
758 * ext4_journal_forget() below */
ac27a0ec 759 unlock_buffer(bh);
ac27a0ec
DK
760 goto failed;
761 }
762
763 memset(bh->b_data, 0, blocksize);
764 branch[n].p = (__le32 *) bh->b_data + offsets[n];
765 branch[n].key = cpu_to_le32(new_blocks[n]);
766 *branch[n].p = branch[n].key;
af5bc92d 767 if (n == indirect_blks) {
ac27a0ec
DK
768 current_block = new_blocks[n];
769 /*
770 * End of chain, update the last new metablock of
771 * the chain to point to the new allocated
772 * data blocks numbers
773 */
de9a55b8 774 for (i = 1; i < num; i++)
ac27a0ec
DK
775 *(branch[n].p + i) = cpu_to_le32(++current_block);
776 }
777 BUFFER_TRACE(bh, "marking uptodate");
778 set_buffer_uptodate(bh);
779 unlock_buffer(bh);
780
0390131b
FM
781 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
782 err = ext4_handle_dirty_metadata(handle, inode, bh);
ac27a0ec
DK
783 if (err)
784 goto failed;
785 }
786 *blks = num;
787 return err;
788failed:
789 /* Allocation failed, free what we already allocated */
e6362609 790 ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0);
ac27a0ec 791 for (i = 1; i <= n ; i++) {
60e6679e 792 /*
e6362609
TT
793 * branch[i].bh is newly allocated, so there is no
794 * need to revoke the block, which is why we don't
795 * need to set EXT4_FREE_BLOCKS_METADATA.
b7e57e7c 796 */
e6362609
TT
797 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1,
798 EXT4_FREE_BLOCKS_FORGET);
ac27a0ec 799 }
e6362609
TT
800 for (i = n+1; i < indirect_blks; i++)
801 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
ac27a0ec 802
e6362609 803 ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0);
ac27a0ec
DK
804
805 return err;
806}
807
808/**
617ba13b 809 * ext4_splice_branch - splice the allocated branch onto inode.
ac27a0ec
DK
810 * @inode: owner
811 * @block: (logical) number of block we are adding
812 * @chain: chain of indirect blocks (with a missing link - see
617ba13b 813 * ext4_alloc_branch)
ac27a0ec
DK
814 * @where: location of missing link
815 * @num: number of indirect blocks we are adding
816 * @blks: number of direct blocks we are adding
817 *
818 * This function fills the missing link and does all housekeeping needed in
819 * inode (->i_blocks, etc.). In case of success we end up with the full
820 * chain to new block and return 0.
821 */
617ba13b 822static int ext4_splice_branch(handle_t *handle, struct inode *inode,
de9a55b8
TT
823 ext4_lblk_t block, Indirect *where, int num,
824 int blks)
ac27a0ec
DK
825{
826 int i;
827 int err = 0;
617ba13b 828 ext4_fsblk_t current_block;
ac27a0ec 829
ac27a0ec
DK
830 /*
831 * If we're splicing into a [td]indirect block (as opposed to the
832 * inode) then we need to get write access to the [td]indirect block
833 * before the splice.
834 */
835 if (where->bh) {
836 BUFFER_TRACE(where->bh, "get_write_access");
617ba13b 837 err = ext4_journal_get_write_access(handle, where->bh);
ac27a0ec
DK
838 if (err)
839 goto err_out;
840 }
841 /* That's it */
842
843 *where->p = where->key;
844
845 /*
846 * Update the host buffer_head or inode to point to more just allocated
847 * direct blocks blocks
848 */
849 if (num == 0 && blks > 1) {
850 current_block = le32_to_cpu(where->key) + 1;
851 for (i = 1; i < blks; i++)
af5bc92d 852 *(where->p + i) = cpu_to_le32(current_block++);
ac27a0ec
DK
853 }
854
ac27a0ec 855 /* We are done with atomic stuff, now do the rest of housekeeping */
ac27a0ec
DK
856 /* had we spliced it onto indirect block? */
857 if (where->bh) {
858 /*
859 * If we spliced it onto an indirect block, we haven't
860 * altered the inode. Note however that if it is being spliced
861 * onto an indirect block at the very end of the file (the
862 * file is growing) then we *will* alter the inode to reflect
863 * the new i_size. But that is not done here - it is done in
617ba13b 864 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
ac27a0ec
DK
865 */
866 jbd_debug(5, "splicing indirect only\n");
0390131b
FM
867 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
868 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
ac27a0ec
DK
869 if (err)
870 goto err_out;
871 } else {
872 /*
873 * OK, we spliced it into the inode itself on a direct block.
ac27a0ec 874 */
41591750 875 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
876 jbd_debug(5, "splicing direct\n");
877 }
878 return err;
879
880err_out:
881 for (i = 1; i <= num; i++) {
60e6679e 882 /*
e6362609
TT
883 * branch[i].bh is newly allocated, so there is no
884 * need to revoke the block, which is why we don't
885 * need to set EXT4_FREE_BLOCKS_METADATA.
b7e57e7c 886 */
e6362609
TT
887 ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
888 EXT4_FREE_BLOCKS_FORGET);
ac27a0ec 889 }
e6362609
TT
890 ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key),
891 blks, 0);
ac27a0ec
DK
892
893 return err;
894}
895
896/*
e35fd660 897 * The ext4_ind_map_blocks() function handles non-extents inodes
b920c755 898 * (i.e., using the traditional indirect/double-indirect i_blocks
e35fd660 899 * scheme) for ext4_map_blocks().
b920c755 900 *
ac27a0ec
DK
901 * Allocation strategy is simple: if we have to allocate something, we will
902 * have to go the whole way to leaf. So let's do it before attaching anything
903 * to tree, set linkage between the newborn blocks, write them if sync is
904 * required, recheck the path, free and repeat if check fails, otherwise
905 * set the last missing link (that will protect us from any truncate-generated
906 * removals - all blocks on the path are immune now) and possibly force the
907 * write on the parent block.
908 * That has a nice additional property: no special recovery from the failed
909 * allocations is needed - we simply release blocks and do not touch anything
910 * reachable from inode.
911 *
912 * `handle' can be NULL if create == 0.
913 *
ac27a0ec
DK
914 * return > 0, # of blocks mapped or allocated.
915 * return = 0, if plain lookup failed.
916 * return < 0, error case.
c278bfec 917 *
b920c755
TT
918 * The ext4_ind_get_blocks() function should be called with
919 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
920 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
921 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
922 * blocks.
ac27a0ec 923 */
e35fd660
TT
924static int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
925 struct ext4_map_blocks *map,
de9a55b8 926 int flags)
ac27a0ec
DK
927{
928 int err = -EIO;
725d26d3 929 ext4_lblk_t offsets[4];
ac27a0ec
DK
930 Indirect chain[4];
931 Indirect *partial;
617ba13b 932 ext4_fsblk_t goal;
ac27a0ec
DK
933 int indirect_blks;
934 int blocks_to_boundary = 0;
935 int depth;
ac27a0ec 936 int count = 0;
617ba13b 937 ext4_fsblk_t first_block = 0;
ac27a0ec 938
12e9b892 939 J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
c2177057 940 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
e35fd660 941 depth = ext4_block_to_path(inode, map->m_lblk, offsets,
de9a55b8 942 &blocks_to_boundary);
ac27a0ec
DK
943
944 if (depth == 0)
945 goto out;
946
617ba13b 947 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
ac27a0ec
DK
948
949 /* Simplest case - block found, no allocation needed */
950 if (!partial) {
951 first_block = le32_to_cpu(chain[depth - 1].key);
ac27a0ec
DK
952 count++;
953 /*map more blocks*/
e35fd660 954 while (count < map->m_len && count <= blocks_to_boundary) {
617ba13b 955 ext4_fsblk_t blk;
ac27a0ec 956
ac27a0ec
DK
957 blk = le32_to_cpu(*(chain[depth-1].p + count));
958
959 if (blk == first_block + count)
960 count++;
961 else
962 break;
963 }
c278bfec 964 goto got_it;
ac27a0ec
DK
965 }
966
967 /* Next simple case - plain lookup or failed read of indirect block */
c2177057 968 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
ac27a0ec
DK
969 goto cleanup;
970
ac27a0ec 971 /*
c2ea3fde 972 * Okay, we need to do block allocation.
ac27a0ec 973 */
e35fd660 974 goal = ext4_find_goal(inode, map->m_lblk, partial);
ac27a0ec
DK
975
976 /* the number of blocks need to allocate for [d,t]indirect blocks */
977 indirect_blks = (chain + depth) - partial - 1;
978
979 /*
980 * Next look up the indirect map to count the totoal number of
981 * direct blocks to allocate for this branch.
982 */
617ba13b 983 count = ext4_blks_to_allocate(partial, indirect_blks,
e35fd660 984 map->m_len, blocks_to_boundary);
ac27a0ec 985 /*
617ba13b 986 * Block out ext4_truncate while we alter the tree
ac27a0ec 987 */
e35fd660 988 err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks,
de9a55b8
TT
989 &count, goal,
990 offsets + (partial - chain), partial);
ac27a0ec
DK
991
992 /*
617ba13b 993 * The ext4_splice_branch call will free and forget any buffers
ac27a0ec
DK
994 * on the new chain if there is a failure, but that risks using
995 * up transaction credits, especially for bitmaps where the
996 * credits cannot be returned. Can we handle this somehow? We
997 * may need to return -EAGAIN upwards in the worst case. --sct
998 */
999 if (!err)
e35fd660 1000 err = ext4_splice_branch(handle, inode, map->m_lblk,
de9a55b8 1001 partial, indirect_blks, count);
2bba702d 1002 if (err)
ac27a0ec
DK
1003 goto cleanup;
1004
e35fd660 1005 map->m_flags |= EXT4_MAP_NEW;
b436b9be
JK
1006
1007 ext4_update_inode_fsync_trans(handle, inode, 1);
ac27a0ec 1008got_it:
e35fd660
TT
1009 map->m_flags |= EXT4_MAP_MAPPED;
1010 map->m_pblk = le32_to_cpu(chain[depth-1].key);
1011 map->m_len = count;
ac27a0ec 1012 if (count > blocks_to_boundary)
e35fd660 1013 map->m_flags |= EXT4_MAP_BOUNDARY;
ac27a0ec
DK
1014 err = count;
1015 /* Clean up and exit */
1016 partial = chain + depth - 1; /* the whole chain */
1017cleanup:
1018 while (partial > chain) {
1019 BUFFER_TRACE(partial->bh, "call brelse");
1020 brelse(partial->bh);
1021 partial--;
1022 }
ac27a0ec
DK
1023out:
1024 return err;
1025}
1026
a9e7f447
DM
1027#ifdef CONFIG_QUOTA
1028qsize_t *ext4_get_reserved_space(struct inode *inode)
60e58e0f 1029{
a9e7f447 1030 return &EXT4_I(inode)->i_reserved_quota;
60e58e0f 1031}
a9e7f447 1032#endif
9d0be502 1033
12219aea
AK
1034/*
1035 * Calculate the number of metadata blocks need to reserve
9d0be502 1036 * to allocate a new block at @lblocks for non extent file based file
12219aea 1037 */
9d0be502
TT
1038static int ext4_indirect_calc_metadata_amount(struct inode *inode,
1039 sector_t lblock)
12219aea 1040{
9d0be502 1041 struct ext4_inode_info *ei = EXT4_I(inode);
d330a5be 1042 sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1);
9d0be502 1043 int blk_bits;
12219aea 1044
9d0be502
TT
1045 if (lblock < EXT4_NDIR_BLOCKS)
1046 return 0;
12219aea 1047
9d0be502 1048 lblock -= EXT4_NDIR_BLOCKS;
12219aea 1049
9d0be502
TT
1050 if (ei->i_da_metadata_calc_len &&
1051 (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
1052 ei->i_da_metadata_calc_len++;
1053 return 0;
1054 }
1055 ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
1056 ei->i_da_metadata_calc_len = 1;
d330a5be 1057 blk_bits = order_base_2(lblock);
9d0be502 1058 return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
12219aea
AK
1059}
1060
1061/*
1062 * Calculate the number of metadata blocks need to reserve
9d0be502 1063 * to allocate a block located at @lblock
12219aea 1064 */
9d0be502 1065static int ext4_calc_metadata_amount(struct inode *inode, sector_t lblock)
12219aea 1066{
12e9b892 1067 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
9d0be502 1068 return ext4_ext_calc_metadata_amount(inode, lblock);
12219aea 1069
9d0be502 1070 return ext4_indirect_calc_metadata_amount(inode, lblock);
12219aea
AK
1071}
1072
0637c6f4
TT
1073/*
1074 * Called with i_data_sem down, which is important since we can call
1075 * ext4_discard_preallocations() from here.
1076 */
5f634d06
AK
1077void ext4_da_update_reserve_space(struct inode *inode,
1078 int used, int quota_claim)
12219aea
AK
1079{
1080 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
0637c6f4 1081 struct ext4_inode_info *ei = EXT4_I(inode);
0637c6f4
TT
1082
1083 spin_lock(&ei->i_block_reservation_lock);
f8ec9d68 1084 trace_ext4_da_update_reserve_space(inode, used);
0637c6f4
TT
1085 if (unlikely(used > ei->i_reserved_data_blocks)) {
1086 ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
1087 "with only %d reserved data blocks\n",
1088 __func__, inode->i_ino, used,
1089 ei->i_reserved_data_blocks);
1090 WARN_ON(1);
1091 used = ei->i_reserved_data_blocks;
1092 }
12219aea 1093
0637c6f4
TT
1094 /* Update per-inode reservations */
1095 ei->i_reserved_data_blocks -= used;
0637c6f4 1096 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
72b8ab9d
ES
1097 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1098 used + ei->i_allocated_meta_blocks);
0637c6f4 1099 ei->i_allocated_meta_blocks = 0;
6bc6e63f 1100
0637c6f4
TT
1101 if (ei->i_reserved_data_blocks == 0) {
1102 /*
1103 * We can release all of the reserved metadata blocks
1104 * only when we have written all of the delayed
1105 * allocation blocks.
1106 */
72b8ab9d
ES
1107 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1108 ei->i_reserved_meta_blocks);
ee5f4d9c 1109 ei->i_reserved_meta_blocks = 0;
9d0be502 1110 ei->i_da_metadata_calc_len = 0;
6bc6e63f 1111 }
12219aea 1112 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
60e58e0f 1113
72b8ab9d
ES
1114 /* Update quota subsystem for data blocks */
1115 if (quota_claim)
5dd4056d 1116 dquot_claim_block(inode, used);
72b8ab9d 1117 else {
5f634d06
AK
1118 /*
1119 * We did fallocate with an offset that is already delayed
1120 * allocated. So on delayed allocated writeback we should
72b8ab9d 1121 * not re-claim the quota for fallocated blocks.
5f634d06 1122 */
72b8ab9d 1123 dquot_release_reservation_block(inode, used);
5f634d06 1124 }
d6014301
AK
1125
1126 /*
1127 * If we have done all the pending block allocations and if
1128 * there aren't any writers on the inode, we can discard the
1129 * inode's preallocations.
1130 */
0637c6f4
TT
1131 if ((ei->i_reserved_data_blocks == 0) &&
1132 (atomic_read(&inode->i_writecount) == 0))
d6014301 1133 ext4_discard_preallocations(inode);
12219aea
AK
1134}
1135
24676da4
TT
1136static int check_block_validity(struct inode *inode, const char *func,
1137 struct ext4_map_blocks *map)
6fd058f7 1138{
24676da4
TT
1139 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
1140 map->m_len)) {
1141 ext4_error_inode(func, inode,
1142 "lblock %lu mapped to illegal pblock %llu "
1143 "(length %d)", (unsigned long) map->m_lblk,
1144 map->m_pblk, map->m_len);
6fd058f7
TT
1145 return -EIO;
1146 }
1147 return 0;
1148}
1149
55138e0b 1150/*
1f94533d
TT
1151 * Return the number of contiguous dirty pages in a given inode
1152 * starting at page frame idx.
55138e0b
TT
1153 */
1154static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
1155 unsigned int max_pages)
1156{
1157 struct address_space *mapping = inode->i_mapping;
1158 pgoff_t index;
1159 struct pagevec pvec;
1160 pgoff_t num = 0;
1161 int i, nr_pages, done = 0;
1162
1163 if (max_pages == 0)
1164 return 0;
1165 pagevec_init(&pvec, 0);
1166 while (!done) {
1167 index = idx;
1168 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1169 PAGECACHE_TAG_DIRTY,
1170 (pgoff_t)PAGEVEC_SIZE);
1171 if (nr_pages == 0)
1172 break;
1173 for (i = 0; i < nr_pages; i++) {
1174 struct page *page = pvec.pages[i];
1175 struct buffer_head *bh, *head;
1176
1177 lock_page(page);
1178 if (unlikely(page->mapping != mapping) ||
1179 !PageDirty(page) ||
1180 PageWriteback(page) ||
1181 page->index != idx) {
1182 done = 1;
1183 unlock_page(page);
1184 break;
1185 }
1f94533d
TT
1186 if (page_has_buffers(page)) {
1187 bh = head = page_buffers(page);
1188 do {
1189 if (!buffer_delay(bh) &&
1190 !buffer_unwritten(bh))
1191 done = 1;
1192 bh = bh->b_this_page;
1193 } while (!done && (bh != head));
1194 }
55138e0b
TT
1195 unlock_page(page);
1196 if (done)
1197 break;
1198 idx++;
1199 num++;
1200 if (num >= max_pages)
1201 break;
1202 }
1203 pagevec_release(&pvec);
1204 }
1205 return num;
1206}
1207
f5ab0d1f 1208/*
e35fd660 1209 * The ext4_map_blocks() function tries to look up the requested blocks,
2b2d6d01 1210 * and returns if the blocks are already mapped.
f5ab0d1f 1211 *
f5ab0d1f
MC
1212 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1213 * and store the allocated blocks in the result buffer head and mark it
1214 * mapped.
1215 *
e35fd660
TT
1216 * If file type is extents based, it will call ext4_ext_map_blocks(),
1217 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
f5ab0d1f
MC
1218 * based files
1219 *
1220 * On success, it returns the number of blocks being mapped or allocate.
1221 * if create==0 and the blocks are pre-allocated and uninitialized block,
1222 * the result buffer head is unmapped. If the create ==1, it will make sure
1223 * the buffer head is mapped.
1224 *
1225 * It returns 0 if plain look up failed (blocks have not been allocated), in
1226 * that casem, buffer head is unmapped
1227 *
1228 * It returns the error in case of allocation failure.
1229 */
e35fd660
TT
1230int ext4_map_blocks(handle_t *handle, struct inode *inode,
1231 struct ext4_map_blocks *map, int flags)
0e855ac8
AK
1232{
1233 int retval;
f5ab0d1f 1234
e35fd660
TT
1235 map->m_flags = 0;
1236 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
1237 "logical block %lu\n", inode->i_ino, flags, map->m_len,
1238 (unsigned long) map->m_lblk);
4df3d265 1239 /*
b920c755
TT
1240 * Try to see if we can get the block without requesting a new
1241 * file system block.
4df3d265
AK
1242 */
1243 down_read((&EXT4_I(inode)->i_data_sem));
12e9b892 1244 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
e35fd660 1245 retval = ext4_ext_map_blocks(handle, inode, map, 0);
0e855ac8 1246 } else {
e35fd660 1247 retval = ext4_ind_map_blocks(handle, inode, map, 0);
0e855ac8 1248 }
4df3d265 1249 up_read((&EXT4_I(inode)->i_data_sem));
f5ab0d1f 1250
e35fd660 1251 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
24676da4 1252 int ret = check_block_validity(inode, __func__, map);
6fd058f7
TT
1253 if (ret != 0)
1254 return ret;
1255 }
1256
f5ab0d1f 1257 /* If it is only a block(s) look up */
c2177057 1258 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
f5ab0d1f
MC
1259 return retval;
1260
1261 /*
1262 * Returns if the blocks have already allocated
1263 *
1264 * Note that if blocks have been preallocated
1265 * ext4_ext_get_block() returns th create = 0
1266 * with buffer head unmapped.
1267 */
e35fd660 1268 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
4df3d265
AK
1269 return retval;
1270
2a8964d6
AK
1271 /*
1272 * When we call get_blocks without the create flag, the
1273 * BH_Unwritten flag could have gotten set if the blocks
1274 * requested were part of a uninitialized extent. We need to
1275 * clear this flag now that we are committed to convert all or
1276 * part of the uninitialized extent to be an initialized
1277 * extent. This is because we need to avoid the combination
1278 * of BH_Unwritten and BH_Mapped flags being simultaneously
1279 * set on the buffer_head.
1280 */
e35fd660 1281 map->m_flags &= ~EXT4_MAP_UNWRITTEN;
2a8964d6 1282
4df3d265 1283 /*
f5ab0d1f
MC
1284 * New blocks allocate and/or writing to uninitialized extent
1285 * will possibly result in updating i_data, so we take
1286 * the write lock of i_data_sem, and call get_blocks()
1287 * with create == 1 flag.
4df3d265
AK
1288 */
1289 down_write((&EXT4_I(inode)->i_data_sem));
d2a17637
MC
1290
1291 /*
1292 * if the caller is from delayed allocation writeout path
1293 * we have already reserved fs blocks for allocation
1294 * let the underlying get_block() function know to
1295 * avoid double accounting
1296 */
c2177057 1297 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
d2a17637 1298 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
4df3d265
AK
1299 /*
1300 * We need to check for EXT4 here because migrate
1301 * could have changed the inode type in between
1302 */
12e9b892 1303 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
e35fd660 1304 retval = ext4_ext_map_blocks(handle, inode, map, flags);
0e855ac8 1305 } else {
e35fd660 1306 retval = ext4_ind_map_blocks(handle, inode, map, flags);
267e4db9 1307
e35fd660 1308 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
267e4db9
AK
1309 /*
1310 * We allocated new blocks which will result in
1311 * i_data's format changing. Force the migrate
1312 * to fail by clearing migrate flags
1313 */
19f5fb7a 1314 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
267e4db9 1315 }
d2a17637 1316
5f634d06
AK
1317 /*
1318 * Update reserved blocks/metadata blocks after successful
1319 * block allocation which had been deferred till now. We don't
1320 * support fallocate for non extent files. So we can update
1321 * reserve space here.
1322 */
1323 if ((retval > 0) &&
1296cc85 1324 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
5f634d06
AK
1325 ext4_da_update_reserve_space(inode, retval, 1);
1326 }
2ac3b6e0 1327 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
d2a17637 1328 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
2ac3b6e0 1329
4df3d265 1330 up_write((&EXT4_I(inode)->i_data_sem));
e35fd660 1331 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
24676da4
TT
1332 int ret = check_block_validity(inode,
1333 "ext4_map_blocks_after_alloc",
1334 map);
6fd058f7
TT
1335 if (ret != 0)
1336 return ret;
1337 }
0e855ac8
AK
1338 return retval;
1339}
1340
f3bd1f3f
MC
1341/* Maximum number of blocks we map for direct IO at once. */
1342#define DIO_MAX_BLOCKS 4096
1343
2ed88685
TT
1344static int _ext4_get_block(struct inode *inode, sector_t iblock,
1345 struct buffer_head *bh, int flags)
ac27a0ec 1346{
3e4fdaf8 1347 handle_t *handle = ext4_journal_current_handle();
2ed88685 1348 struct ext4_map_blocks map;
7fb5409d 1349 int ret = 0, started = 0;
f3bd1f3f 1350 int dio_credits;
ac27a0ec 1351
2ed88685
TT
1352 map.m_lblk = iblock;
1353 map.m_len = bh->b_size >> inode->i_blkbits;
1354
1355 if (flags && !handle) {
7fb5409d 1356 /* Direct IO write... */
2ed88685
TT
1357 if (map.m_len > DIO_MAX_BLOCKS)
1358 map.m_len = DIO_MAX_BLOCKS;
1359 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
f3bd1f3f 1360 handle = ext4_journal_start(inode, dio_credits);
7fb5409d 1361 if (IS_ERR(handle)) {
ac27a0ec 1362 ret = PTR_ERR(handle);
2ed88685 1363 return ret;
ac27a0ec 1364 }
7fb5409d 1365 started = 1;
ac27a0ec
DK
1366 }
1367
2ed88685 1368 ret = ext4_map_blocks(handle, inode, &map, flags);
7fb5409d 1369 if (ret > 0) {
2ed88685
TT
1370 map_bh(bh, inode->i_sb, map.m_pblk);
1371 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1372 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
7fb5409d 1373 ret = 0;
ac27a0ec 1374 }
7fb5409d
JK
1375 if (started)
1376 ext4_journal_stop(handle);
ac27a0ec
DK
1377 return ret;
1378}
1379
2ed88685
TT
1380int ext4_get_block(struct inode *inode, sector_t iblock,
1381 struct buffer_head *bh, int create)
1382{
1383 return _ext4_get_block(inode, iblock, bh,
1384 create ? EXT4_GET_BLOCKS_CREATE : 0);
1385}
1386
ac27a0ec
DK
1387/*
1388 * `handle' can be NULL if create is zero
1389 */
617ba13b 1390struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
725d26d3 1391 ext4_lblk_t block, int create, int *errp)
ac27a0ec 1392{
2ed88685
TT
1393 struct ext4_map_blocks map;
1394 struct buffer_head *bh;
ac27a0ec
DK
1395 int fatal = 0, err;
1396
1397 J_ASSERT(handle != NULL || create == 0);
1398
2ed88685
TT
1399 map.m_lblk = block;
1400 map.m_len = 1;
1401 err = ext4_map_blocks(handle, inode, &map,
1402 create ? EXT4_GET_BLOCKS_CREATE : 0);
ac27a0ec 1403
2ed88685
TT
1404 if (err < 0)
1405 *errp = err;
1406 if (err <= 0)
1407 return NULL;
1408 *errp = 0;
1409
1410 bh = sb_getblk(inode->i_sb, map.m_pblk);
1411 if (!bh) {
1412 *errp = -EIO;
1413 return NULL;
ac27a0ec 1414 }
2ed88685
TT
1415 if (map.m_flags & EXT4_MAP_NEW) {
1416 J_ASSERT(create != 0);
1417 J_ASSERT(handle != NULL);
ac27a0ec 1418
2ed88685
TT
1419 /*
1420 * Now that we do not always journal data, we should
1421 * keep in mind whether this should always journal the
1422 * new buffer as metadata. For now, regular file
1423 * writes use ext4_get_block instead, so it's not a
1424 * problem.
1425 */
1426 lock_buffer(bh);
1427 BUFFER_TRACE(bh, "call get_create_access");
1428 fatal = ext4_journal_get_create_access(handle, bh);
1429 if (!fatal && !buffer_uptodate(bh)) {
1430 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1431 set_buffer_uptodate(bh);
ac27a0ec 1432 }
2ed88685
TT
1433 unlock_buffer(bh);
1434 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1435 err = ext4_handle_dirty_metadata(handle, inode, bh);
1436 if (!fatal)
1437 fatal = err;
1438 } else {
1439 BUFFER_TRACE(bh, "not a new buffer");
ac27a0ec 1440 }
2ed88685
TT
1441 if (fatal) {
1442 *errp = fatal;
1443 brelse(bh);
1444 bh = NULL;
1445 }
1446 return bh;
ac27a0ec
DK
1447}
1448
617ba13b 1449struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
725d26d3 1450 ext4_lblk_t block, int create, int *err)
ac27a0ec 1451{
af5bc92d 1452 struct buffer_head *bh;
ac27a0ec 1453
617ba13b 1454 bh = ext4_getblk(handle, inode, block, create, err);
ac27a0ec
DK
1455 if (!bh)
1456 return bh;
1457 if (buffer_uptodate(bh))
1458 return bh;
1459 ll_rw_block(READ_META, 1, &bh);
1460 wait_on_buffer(bh);
1461 if (buffer_uptodate(bh))
1462 return bh;
1463 put_bh(bh);
1464 *err = -EIO;
1465 return NULL;
1466}
1467
af5bc92d
TT
1468static int walk_page_buffers(handle_t *handle,
1469 struct buffer_head *head,
1470 unsigned from,
1471 unsigned to,
1472 int *partial,
1473 int (*fn)(handle_t *handle,
1474 struct buffer_head *bh))
ac27a0ec
DK
1475{
1476 struct buffer_head *bh;
1477 unsigned block_start, block_end;
1478 unsigned blocksize = head->b_size;
1479 int err, ret = 0;
1480 struct buffer_head *next;
1481
af5bc92d
TT
1482 for (bh = head, block_start = 0;
1483 ret == 0 && (bh != head || !block_start);
de9a55b8 1484 block_start = block_end, bh = next) {
ac27a0ec
DK
1485 next = bh->b_this_page;
1486 block_end = block_start + blocksize;
1487 if (block_end <= from || block_start >= to) {
1488 if (partial && !buffer_uptodate(bh))
1489 *partial = 1;
1490 continue;
1491 }
1492 err = (*fn)(handle, bh);
1493 if (!ret)
1494 ret = err;
1495 }
1496 return ret;
1497}
1498
1499/*
1500 * To preserve ordering, it is essential that the hole instantiation and
1501 * the data write be encapsulated in a single transaction. We cannot
617ba13b 1502 * close off a transaction and start a new one between the ext4_get_block()
dab291af 1503 * and the commit_write(). So doing the jbd2_journal_start at the start of
ac27a0ec
DK
1504 * prepare_write() is the right place.
1505 *
617ba13b
MC
1506 * Also, this function can nest inside ext4_writepage() ->
1507 * block_write_full_page(). In that case, we *know* that ext4_writepage()
ac27a0ec
DK
1508 * has generated enough buffer credits to do the whole page. So we won't
1509 * block on the journal in that case, which is good, because the caller may
1510 * be PF_MEMALLOC.
1511 *
617ba13b 1512 * By accident, ext4 can be reentered when a transaction is open via
ac27a0ec
DK
1513 * quota file writes. If we were to commit the transaction while thus
1514 * reentered, there can be a deadlock - we would be holding a quota
1515 * lock, and the commit would never complete if another thread had a
1516 * transaction open and was blocking on the quota lock - a ranking
1517 * violation.
1518 *
dab291af 1519 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
ac27a0ec
DK
1520 * will _not_ run commit under these circumstances because handle->h_ref
1521 * is elevated. We'll still have enough credits for the tiny quotafile
1522 * write.
1523 */
1524static int do_journal_get_write_access(handle_t *handle,
de9a55b8 1525 struct buffer_head *bh)
ac27a0ec
DK
1526{
1527 if (!buffer_mapped(bh) || buffer_freed(bh))
1528 return 0;
617ba13b 1529 return ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
1530}
1531
b9a4207d
JK
1532/*
1533 * Truncate blocks that were not used by write. We have to truncate the
1534 * pagecache as well so that corresponding buffers get properly unmapped.
1535 */
1536static void ext4_truncate_failed_write(struct inode *inode)
1537{
1538 truncate_inode_pages(inode->i_mapping, inode->i_size);
1539 ext4_truncate(inode);
1540}
1541
744692dc
JZ
1542static int ext4_get_block_write(struct inode *inode, sector_t iblock,
1543 struct buffer_head *bh_result, int create);
bfc1af65 1544static int ext4_write_begin(struct file *file, struct address_space *mapping,
de9a55b8
TT
1545 loff_t pos, unsigned len, unsigned flags,
1546 struct page **pagep, void **fsdata)
ac27a0ec 1547{
af5bc92d 1548 struct inode *inode = mapping->host;
1938a150 1549 int ret, needed_blocks;
ac27a0ec
DK
1550 handle_t *handle;
1551 int retries = 0;
af5bc92d 1552 struct page *page;
de9a55b8 1553 pgoff_t index;
af5bc92d 1554 unsigned from, to;
bfc1af65 1555
9bffad1e 1556 trace_ext4_write_begin(inode, pos, len, flags);
1938a150
AK
1557 /*
1558 * Reserve one block more for addition to orphan list in case
1559 * we allocate blocks but write fails for some reason
1560 */
1561 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
de9a55b8 1562 index = pos >> PAGE_CACHE_SHIFT;
af5bc92d
TT
1563 from = pos & (PAGE_CACHE_SIZE - 1);
1564 to = from + len;
ac27a0ec
DK
1565
1566retry:
af5bc92d
TT
1567 handle = ext4_journal_start(inode, needed_blocks);
1568 if (IS_ERR(handle)) {
1569 ret = PTR_ERR(handle);
1570 goto out;
7479d2b9 1571 }
ac27a0ec 1572
ebd3610b
JK
1573 /* We cannot recurse into the filesystem as the transaction is already
1574 * started */
1575 flags |= AOP_FLAG_NOFS;
1576
54566b2c 1577 page = grab_cache_page_write_begin(mapping, index, flags);
cf108bca
JK
1578 if (!page) {
1579 ext4_journal_stop(handle);
1580 ret = -ENOMEM;
1581 goto out;
1582 }
1583 *pagep = page;
1584
744692dc 1585 if (ext4_should_dioread_nolock(inode))
6e1db88d 1586 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
744692dc 1587 else
6e1db88d 1588 ret = __block_write_begin(page, pos, len, ext4_get_block);
bfc1af65
NP
1589
1590 if (!ret && ext4_should_journal_data(inode)) {
ac27a0ec
DK
1591 ret = walk_page_buffers(handle, page_buffers(page),
1592 from, to, NULL, do_journal_get_write_access);
1593 }
bfc1af65
NP
1594
1595 if (ret) {
af5bc92d 1596 unlock_page(page);
af5bc92d 1597 page_cache_release(page);
ae4d5372 1598 /*
6e1db88d 1599 * __block_write_begin may have instantiated a few blocks
ae4d5372
AK
1600 * outside i_size. Trim these off again. Don't need
1601 * i_size_read because we hold i_mutex.
1938a150
AK
1602 *
1603 * Add inode to orphan list in case we crash before
1604 * truncate finishes
ae4d5372 1605 */
ffacfa7a 1606 if (pos + len > inode->i_size && ext4_can_truncate(inode))
1938a150
AK
1607 ext4_orphan_add(handle, inode);
1608
1609 ext4_journal_stop(handle);
1610 if (pos + len > inode->i_size) {
b9a4207d 1611 ext4_truncate_failed_write(inode);
de9a55b8 1612 /*
ffacfa7a 1613 * If truncate failed early the inode might
1938a150
AK
1614 * still be on the orphan list; we need to
1615 * make sure the inode is removed from the
1616 * orphan list in that case.
1617 */
1618 if (inode->i_nlink)
1619 ext4_orphan_del(NULL, inode);
1620 }
bfc1af65
NP
1621 }
1622
617ba13b 1623 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
ac27a0ec 1624 goto retry;
7479d2b9 1625out:
ac27a0ec
DK
1626 return ret;
1627}
1628
bfc1af65
NP
1629/* For write_end() in data=journal mode */
1630static int write_end_fn(handle_t *handle, struct buffer_head *bh)
ac27a0ec
DK
1631{
1632 if (!buffer_mapped(bh) || buffer_freed(bh))
1633 return 0;
1634 set_buffer_uptodate(bh);
0390131b 1635 return ext4_handle_dirty_metadata(handle, NULL, bh);
ac27a0ec
DK
1636}
1637
f8514083 1638static int ext4_generic_write_end(struct file *file,
de9a55b8
TT
1639 struct address_space *mapping,
1640 loff_t pos, unsigned len, unsigned copied,
1641 struct page *page, void *fsdata)
f8514083
AK
1642{
1643 int i_size_changed = 0;
1644 struct inode *inode = mapping->host;
1645 handle_t *handle = ext4_journal_current_handle();
1646
1647 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1648
1649 /*
1650 * No need to use i_size_read() here, the i_size
1651 * cannot change under us because we hold i_mutex.
1652 *
1653 * But it's important to update i_size while still holding page lock:
1654 * page writeout could otherwise come in and zero beyond i_size.
1655 */
1656 if (pos + copied > inode->i_size) {
1657 i_size_write(inode, pos + copied);
1658 i_size_changed = 1;
1659 }
1660
1661 if (pos + copied > EXT4_I(inode)->i_disksize) {
1662 /* We need to mark inode dirty even if
1663 * new_i_size is less that inode->i_size
1664 * bu greater than i_disksize.(hint delalloc)
1665 */
1666 ext4_update_i_disksize(inode, (pos + copied));
1667 i_size_changed = 1;
1668 }
1669 unlock_page(page);
1670 page_cache_release(page);
1671
1672 /*
1673 * Don't mark the inode dirty under page lock. First, it unnecessarily
1674 * makes the holding time of page lock longer. Second, it forces lock
1675 * ordering of page lock and transaction start for journaling
1676 * filesystems.
1677 */
1678 if (i_size_changed)
1679 ext4_mark_inode_dirty(handle, inode);
1680
1681 return copied;
1682}
1683
ac27a0ec
DK
1684/*
1685 * We need to pick up the new inode size which generic_commit_write gave us
1686 * `file' can be NULL - eg, when called from page_symlink().
1687 *
617ba13b 1688 * ext4 never places buffers on inode->i_mapping->private_list. metadata
ac27a0ec
DK
1689 * buffers are managed internally.
1690 */
bfc1af65 1691static int ext4_ordered_write_end(struct file *file,
de9a55b8
TT
1692 struct address_space *mapping,
1693 loff_t pos, unsigned len, unsigned copied,
1694 struct page *page, void *fsdata)
ac27a0ec 1695{
617ba13b 1696 handle_t *handle = ext4_journal_current_handle();
cf108bca 1697 struct inode *inode = mapping->host;
ac27a0ec
DK
1698 int ret = 0, ret2;
1699
9bffad1e 1700 trace_ext4_ordered_write_end(inode, pos, len, copied);
678aaf48 1701 ret = ext4_jbd2_file_inode(handle, inode);
ac27a0ec
DK
1702
1703 if (ret == 0) {
f8514083 1704 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
bfc1af65 1705 page, fsdata);
f8a87d89 1706 copied = ret2;
ffacfa7a 1707 if (pos + len > inode->i_size && ext4_can_truncate(inode))
f8514083
AK
1708 /* if we have allocated more blocks and copied
1709 * less. We will have blocks allocated outside
1710 * inode->i_size. So truncate them
1711 */
1712 ext4_orphan_add(handle, inode);
f8a87d89
RK
1713 if (ret2 < 0)
1714 ret = ret2;
ac27a0ec 1715 }
617ba13b 1716 ret2 = ext4_journal_stop(handle);
ac27a0ec
DK
1717 if (!ret)
1718 ret = ret2;
bfc1af65 1719
f8514083 1720 if (pos + len > inode->i_size) {
b9a4207d 1721 ext4_truncate_failed_write(inode);
de9a55b8 1722 /*
ffacfa7a 1723 * If truncate failed early the inode might still be
f8514083
AK
1724 * on the orphan list; we need to make sure the inode
1725 * is removed from the orphan list in that case.
1726 */
1727 if (inode->i_nlink)
1728 ext4_orphan_del(NULL, inode);
1729 }
1730
1731
bfc1af65 1732 return ret ? ret : copied;
ac27a0ec
DK
1733}
1734
bfc1af65 1735static int ext4_writeback_write_end(struct file *file,
de9a55b8
TT
1736 struct address_space *mapping,
1737 loff_t pos, unsigned len, unsigned copied,
1738 struct page *page, void *fsdata)
ac27a0ec 1739{
617ba13b 1740 handle_t *handle = ext4_journal_current_handle();
cf108bca 1741 struct inode *inode = mapping->host;
ac27a0ec 1742 int ret = 0, ret2;
ac27a0ec 1743
9bffad1e 1744 trace_ext4_writeback_write_end(inode, pos, len, copied);
f8514083 1745 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
bfc1af65 1746 page, fsdata);
f8a87d89 1747 copied = ret2;
ffacfa7a 1748 if (pos + len > inode->i_size && ext4_can_truncate(inode))
f8514083
AK
1749 /* if we have allocated more blocks and copied
1750 * less. We will have blocks allocated outside
1751 * inode->i_size. So truncate them
1752 */
1753 ext4_orphan_add(handle, inode);
1754
f8a87d89
RK
1755 if (ret2 < 0)
1756 ret = ret2;
ac27a0ec 1757
617ba13b 1758 ret2 = ext4_journal_stop(handle);
ac27a0ec
DK
1759 if (!ret)
1760 ret = ret2;
bfc1af65 1761
f8514083 1762 if (pos + len > inode->i_size) {
b9a4207d 1763 ext4_truncate_failed_write(inode);
de9a55b8 1764 /*
ffacfa7a 1765 * If truncate failed early the inode might still be
f8514083
AK
1766 * on the orphan list; we need to make sure the inode
1767 * is removed from the orphan list in that case.
1768 */
1769 if (inode->i_nlink)
1770 ext4_orphan_del(NULL, inode);
1771 }
1772
bfc1af65 1773 return ret ? ret : copied;
ac27a0ec
DK
1774}
1775
bfc1af65 1776static int ext4_journalled_write_end(struct file *file,
de9a55b8
TT
1777 struct address_space *mapping,
1778 loff_t pos, unsigned len, unsigned copied,
1779 struct page *page, void *fsdata)
ac27a0ec 1780{
617ba13b 1781 handle_t *handle = ext4_journal_current_handle();
bfc1af65 1782 struct inode *inode = mapping->host;
ac27a0ec
DK
1783 int ret = 0, ret2;
1784 int partial = 0;
bfc1af65 1785 unsigned from, to;
cf17fea6 1786 loff_t new_i_size;
ac27a0ec 1787
9bffad1e 1788 trace_ext4_journalled_write_end(inode, pos, len, copied);
bfc1af65
NP
1789 from = pos & (PAGE_CACHE_SIZE - 1);
1790 to = from + len;
1791
1792 if (copied < len) {
1793 if (!PageUptodate(page))
1794 copied = 0;
1795 page_zero_new_buffers(page, from+copied, to);
1796 }
ac27a0ec
DK
1797
1798 ret = walk_page_buffers(handle, page_buffers(page), from,
bfc1af65 1799 to, &partial, write_end_fn);
ac27a0ec
DK
1800 if (!partial)
1801 SetPageUptodate(page);
cf17fea6
AK
1802 new_i_size = pos + copied;
1803 if (new_i_size > inode->i_size)
bfc1af65 1804 i_size_write(inode, pos+copied);
19f5fb7a 1805 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
cf17fea6
AK
1806 if (new_i_size > EXT4_I(inode)->i_disksize) {
1807 ext4_update_i_disksize(inode, new_i_size);
617ba13b 1808 ret2 = ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
1809 if (!ret)
1810 ret = ret2;
1811 }
bfc1af65 1812
cf108bca 1813 unlock_page(page);
f8514083 1814 page_cache_release(page);
ffacfa7a 1815 if (pos + len > inode->i_size && ext4_can_truncate(inode))
f8514083
AK
1816 /* if we have allocated more blocks and copied
1817 * less. We will have blocks allocated outside
1818 * inode->i_size. So truncate them
1819 */
1820 ext4_orphan_add(handle, inode);
1821
617ba13b 1822 ret2 = ext4_journal_stop(handle);
ac27a0ec
DK
1823 if (!ret)
1824 ret = ret2;
f8514083 1825 if (pos + len > inode->i_size) {
b9a4207d 1826 ext4_truncate_failed_write(inode);
de9a55b8 1827 /*
ffacfa7a 1828 * If truncate failed early the inode might still be
f8514083
AK
1829 * on the orphan list; we need to make sure the inode
1830 * is removed from the orphan list in that case.
1831 */
1832 if (inode->i_nlink)
1833 ext4_orphan_del(NULL, inode);
1834 }
bfc1af65
NP
1835
1836 return ret ? ret : copied;
ac27a0ec 1837}
d2a17637 1838
9d0be502
TT
1839/*
1840 * Reserve a single block located at lblock
1841 */
1842static int ext4_da_reserve_space(struct inode *inode, sector_t lblock)
d2a17637 1843{
030ba6bc 1844 int retries = 0;
60e58e0f 1845 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
0637c6f4 1846 struct ext4_inode_info *ei = EXT4_I(inode);
72b8ab9d 1847 unsigned long md_needed;
5dd4056d 1848 int ret;
d2a17637
MC
1849
1850 /*
1851 * recalculate the amount of metadata blocks to reserve
1852 * in order to allocate nrblocks
1853 * worse case is one extent per block
1854 */
030ba6bc 1855repeat:
0637c6f4 1856 spin_lock(&ei->i_block_reservation_lock);
9d0be502 1857 md_needed = ext4_calc_metadata_amount(inode, lblock);
f8ec9d68 1858 trace_ext4_da_reserve_space(inode, md_needed);
0637c6f4 1859 spin_unlock(&ei->i_block_reservation_lock);
d2a17637 1860
60e58e0f 1861 /*
72b8ab9d
ES
1862 * We will charge metadata quota at writeout time; this saves
1863 * us from metadata over-estimation, though we may go over by
1864 * a small amount in the end. Here we just reserve for data.
60e58e0f 1865 */
72b8ab9d 1866 ret = dquot_reserve_block(inode, 1);
5dd4056d
CH
1867 if (ret)
1868 return ret;
72b8ab9d
ES
1869 /*
1870 * We do still charge estimated metadata to the sb though;
1871 * we cannot afford to run out of free blocks.
1872 */
9d0be502 1873 if (ext4_claim_free_blocks(sbi, md_needed + 1)) {
72b8ab9d 1874 dquot_release_reservation_block(inode, 1);
030ba6bc
AK
1875 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1876 yield();
1877 goto repeat;
1878 }
d2a17637
MC
1879 return -ENOSPC;
1880 }
0637c6f4 1881 spin_lock(&ei->i_block_reservation_lock);
9d0be502 1882 ei->i_reserved_data_blocks++;
0637c6f4
TT
1883 ei->i_reserved_meta_blocks += md_needed;
1884 spin_unlock(&ei->i_block_reservation_lock);
39bc680a 1885
d2a17637
MC
1886 return 0; /* success */
1887}
1888
12219aea 1889static void ext4_da_release_space(struct inode *inode, int to_free)
d2a17637
MC
1890{
1891 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
0637c6f4 1892 struct ext4_inode_info *ei = EXT4_I(inode);
d2a17637 1893
cd213226
MC
1894 if (!to_free)
1895 return; /* Nothing to release, exit */
1896
d2a17637 1897 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
cd213226 1898
5a58ec87 1899 trace_ext4_da_release_space(inode, to_free);
0637c6f4 1900 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
cd213226 1901 /*
0637c6f4
TT
1902 * if there aren't enough reserved blocks, then the
1903 * counter is messed up somewhere. Since this
1904 * function is called from invalidate page, it's
1905 * harmless to return without any action.
cd213226 1906 */
0637c6f4
TT
1907 ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
1908 "ino %lu, to_free %d with only %d reserved "
1909 "data blocks\n", inode->i_ino, to_free,
1910 ei->i_reserved_data_blocks);
1911 WARN_ON(1);
1912 to_free = ei->i_reserved_data_blocks;
cd213226 1913 }
0637c6f4 1914 ei->i_reserved_data_blocks -= to_free;
cd213226 1915
0637c6f4
TT
1916 if (ei->i_reserved_data_blocks == 0) {
1917 /*
1918 * We can release all of the reserved metadata blocks
1919 * only when we have written all of the delayed
1920 * allocation blocks.
1921 */
72b8ab9d
ES
1922 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1923 ei->i_reserved_meta_blocks);
ee5f4d9c 1924 ei->i_reserved_meta_blocks = 0;
9d0be502 1925 ei->i_da_metadata_calc_len = 0;
0637c6f4 1926 }
d2a17637 1927
72b8ab9d 1928 /* update fs dirty data blocks counter */
0637c6f4 1929 percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free);
d2a17637 1930
d2a17637 1931 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
60e58e0f 1932
5dd4056d 1933 dquot_release_reservation_block(inode, to_free);
d2a17637
MC
1934}
1935
1936static void ext4_da_page_release_reservation(struct page *page,
de9a55b8 1937 unsigned long offset)
d2a17637
MC
1938{
1939 int to_release = 0;
1940 struct buffer_head *head, *bh;
1941 unsigned int curr_off = 0;
1942
1943 head = page_buffers(page);
1944 bh = head;
1945 do {
1946 unsigned int next_off = curr_off + bh->b_size;
1947
1948 if ((offset <= curr_off) && (buffer_delay(bh))) {
1949 to_release++;
1950 clear_buffer_delay(bh);
1951 }
1952 curr_off = next_off;
1953 } while ((bh = bh->b_this_page) != head);
12219aea 1954 ext4_da_release_space(page->mapping->host, to_release);
d2a17637 1955}
ac27a0ec 1956
64769240
AT
1957/*
1958 * Delayed allocation stuff
1959 */
1960
64769240
AT
1961/*
1962 * mpage_da_submit_io - walks through extent of pages and try to write
a1d6cc56 1963 * them with writepage() call back
64769240
AT
1964 *
1965 * @mpd->inode: inode
1966 * @mpd->first_page: first page of the extent
1967 * @mpd->next_page: page after the last page of the extent
64769240
AT
1968 *
1969 * By the time mpage_da_submit_io() is called we expect all blocks
1970 * to be allocated. this may be wrong if allocation failed.
1971 *
1972 * As pages are already locked by write_cache_pages(), we can't use it
1973 */
1974static int mpage_da_submit_io(struct mpage_da_data *mpd)
1975{
22208ded 1976 long pages_skipped;
791b7f08
AK
1977 struct pagevec pvec;
1978 unsigned long index, end;
1979 int ret = 0, err, nr_pages, i;
1980 struct inode *inode = mpd->inode;
1981 struct address_space *mapping = inode->i_mapping;
64769240
AT
1982
1983 BUG_ON(mpd->next_page <= mpd->first_page);
791b7f08
AK
1984 /*
1985 * We need to start from the first_page to the next_page - 1
1986 * to make sure we also write the mapped dirty buffer_heads.
8dc207c0 1987 * If we look at mpd->b_blocknr we would only be looking
791b7f08
AK
1988 * at the currently mapped buffer_heads.
1989 */
64769240
AT
1990 index = mpd->first_page;
1991 end = mpd->next_page - 1;
1992
791b7f08 1993 pagevec_init(&pvec, 0);
64769240 1994 while (index <= end) {
791b7f08 1995 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
64769240
AT
1996 if (nr_pages == 0)
1997 break;
1998 for (i = 0; i < nr_pages; i++) {
1999 struct page *page = pvec.pages[i];
2000
791b7f08
AK
2001 index = page->index;
2002 if (index > end)
2003 break;
2004 index++;
2005
2006 BUG_ON(!PageLocked(page));
2007 BUG_ON(PageWriteback(page));
2008
22208ded 2009 pages_skipped = mpd->wbc->pages_skipped;
a1d6cc56 2010 err = mapping->a_ops->writepage(page, mpd->wbc);
22208ded
AK
2011 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
2012 /*
2013 * have successfully written the page
2014 * without skipping the same
2015 */
a1d6cc56 2016 mpd->pages_written++;
64769240
AT
2017 /*
2018 * In error case, we have to continue because
2019 * remaining pages are still locked
2020 * XXX: unlock and re-dirty them?
2021 */
2022 if (ret == 0)
2023 ret = err;
2024 }
2025 pagevec_release(&pvec);
2026 }
64769240
AT
2027 return ret;
2028}
2029
2030/*
2031 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
2032 *
64769240 2033 * the function goes through all passed space and put actual disk
29fa89d0 2034 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
64769240 2035 */
2ed88685
TT
2036static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd,
2037 struct ext4_map_blocks *map)
64769240
AT
2038{
2039 struct inode *inode = mpd->inode;
2040 struct address_space *mapping = inode->i_mapping;
2ed88685
TT
2041 int blocks = map->m_len;
2042 sector_t pblock = map->m_pblk, cur_logical;
64769240 2043 struct buffer_head *head, *bh;
a1d6cc56 2044 pgoff_t index, end;
64769240
AT
2045 struct pagevec pvec;
2046 int nr_pages, i;
2047
2ed88685
TT
2048 index = map->m_lblk >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2049 end = (map->m_lblk + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
64769240
AT
2050 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2051
2052 pagevec_init(&pvec, 0);
2053
2054 while (index <= end) {
2055 /* XXX: optimize tail */
2056 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2057 if (nr_pages == 0)
2058 break;
2059 for (i = 0; i < nr_pages; i++) {
2060 struct page *page = pvec.pages[i];
2061
2062 index = page->index;
2063 if (index > end)
2064 break;
2065 index++;
2066
2067 BUG_ON(!PageLocked(page));
2068 BUG_ON(PageWriteback(page));
2069 BUG_ON(!page_has_buffers(page));
2070
2071 bh = page_buffers(page);
2072 head = bh;
2073
2074 /* skip blocks out of the range */
2075 do {
2ed88685 2076 if (cur_logical >= map->m_lblk)
64769240
AT
2077 break;
2078 cur_logical++;
2079 } while ((bh = bh->b_this_page) != head);
2080
2081 do {
2ed88685 2082 if (cur_logical >= map->m_lblk + blocks)
64769240 2083 break;
29fa89d0 2084
2ed88685 2085 if (buffer_delay(bh) || buffer_unwritten(bh)) {
29fa89d0
AK
2086
2087 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2088
2089 if (buffer_delay(bh)) {
2090 clear_buffer_delay(bh);
2091 bh->b_blocknr = pblock;
2092 } else {
2093 /*
2094 * unwritten already should have
2095 * blocknr assigned. Verify that
2096 */
2097 clear_buffer_unwritten(bh);
2098 BUG_ON(bh->b_blocknr != pblock);
2099 }
2100
61628a3f 2101 } else if (buffer_mapped(bh))
64769240 2102 BUG_ON(bh->b_blocknr != pblock);
64769240 2103
2ed88685 2104 if (map->m_flags & EXT4_MAP_UNINIT)
744692dc 2105 set_buffer_uninit(bh);
64769240
AT
2106 cur_logical++;
2107 pblock++;
2108 } while ((bh = bh->b_this_page) != head);
2109 }
2110 pagevec_release(&pvec);
2111 }
2112}
2113
2114
c4a0c46e
AK
2115static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2116 sector_t logical, long blk_cnt)
2117{
2118 int nr_pages, i;
2119 pgoff_t index, end;
2120 struct pagevec pvec;
2121 struct inode *inode = mpd->inode;
2122 struct address_space *mapping = inode->i_mapping;
2123
2124 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2125 end = (logical + blk_cnt - 1) >>
2126 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2127 while (index <= end) {
2128 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2129 if (nr_pages == 0)
2130 break;
2131 for (i = 0; i < nr_pages; i++) {
2132 struct page *page = pvec.pages[i];
9b1d0998 2133 if (page->index > end)
c4a0c46e 2134 break;
c4a0c46e
AK
2135 BUG_ON(!PageLocked(page));
2136 BUG_ON(PageWriteback(page));
2137 block_invalidatepage(page, 0);
2138 ClearPageUptodate(page);
2139 unlock_page(page);
2140 }
9b1d0998
JK
2141 index = pvec.pages[nr_pages - 1]->index + 1;
2142 pagevec_release(&pvec);
c4a0c46e
AK
2143 }
2144 return;
2145}
2146
df22291f
AK
2147static void ext4_print_free_blocks(struct inode *inode)
2148{
2149 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1693918e
TT
2150 printk(KERN_CRIT "Total free blocks count %lld\n",
2151 ext4_count_free_blocks(inode->i_sb));
2152 printk(KERN_CRIT "Free/Dirty block details\n");
2153 printk(KERN_CRIT "free_blocks=%lld\n",
2154 (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
2155 printk(KERN_CRIT "dirty_blocks=%lld\n",
2156 (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2157 printk(KERN_CRIT "Block reservation details\n");
2158 printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
2159 EXT4_I(inode)->i_reserved_data_blocks);
2160 printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
2161 EXT4_I(inode)->i_reserved_meta_blocks);
df22291f
AK
2162 return;
2163}
2164
64769240
AT
2165/*
2166 * mpage_da_map_blocks - go through given space
2167 *
8dc207c0 2168 * @mpd - bh describing space
64769240
AT
2169 *
2170 * The function skips space we know is already mapped to disk blocks.
2171 *
64769240 2172 */
ed5bde0b 2173static int mpage_da_map_blocks(struct mpage_da_data *mpd)
64769240 2174{
2ac3b6e0 2175 int err, blks, get_blocks_flags;
2ed88685 2176 struct ext4_map_blocks map;
2fa3cdfb
TT
2177 sector_t next = mpd->b_blocknr;
2178 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2179 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2180 handle_t *handle = NULL;
64769240
AT
2181
2182 /*
2183 * We consider only non-mapped and non-allocated blocks
2184 */
8dc207c0 2185 if ((mpd->b_state & (1 << BH_Mapped)) &&
29fa89d0
AK
2186 !(mpd->b_state & (1 << BH_Delay)) &&
2187 !(mpd->b_state & (1 << BH_Unwritten)))
c4a0c46e 2188 return 0;
2fa3cdfb
TT
2189
2190 /*
2191 * If we didn't accumulate anything to write simply return
2192 */
2193 if (!mpd->b_size)
2194 return 0;
2195
2196 handle = ext4_journal_current_handle();
2197 BUG_ON(!handle);
2198
79ffab34 2199 /*
2ac3b6e0
TT
2200 * Call ext4_get_blocks() to allocate any delayed allocation
2201 * blocks, or to convert an uninitialized extent to be
2202 * initialized (in the case where we have written into
2203 * one or more preallocated blocks).
2204 *
2205 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2206 * indicate that we are on the delayed allocation path. This
2207 * affects functions in many different parts of the allocation
2208 * call path. This flag exists primarily because we don't
2209 * want to change *many* call functions, so ext4_get_blocks()
2210 * will set the magic i_delalloc_reserved_flag once the
2211 * inode's allocation semaphore is taken.
2212 *
2213 * If the blocks in questions were delalloc blocks, set
2214 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2215 * variables are updated after the blocks have been allocated.
79ffab34 2216 */
2ed88685
TT
2217 map.m_lblk = next;
2218 map.m_len = max_blocks;
1296cc85 2219 get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
744692dc
JZ
2220 if (ext4_should_dioread_nolock(mpd->inode))
2221 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2ac3b6e0 2222 if (mpd->b_state & (1 << BH_Delay))
1296cc85
AK
2223 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2224
2ed88685 2225 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
2fa3cdfb
TT
2226 if (blks < 0) {
2227 err = blks;
ed5bde0b
TT
2228 /*
2229 * If get block returns with error we simply
2230 * return. Later writepage will redirty the page and
2231 * writepages will find the dirty page again
c4a0c46e
AK
2232 */
2233 if (err == -EAGAIN)
2234 return 0;
df22291f
AK
2235
2236 if (err == -ENOSPC &&
ed5bde0b 2237 ext4_count_free_blocks(mpd->inode->i_sb)) {
df22291f
AK
2238 mpd->retval = err;
2239 return 0;
2240 }
2241
c4a0c46e 2242 /*
ed5bde0b
TT
2243 * get block failure will cause us to loop in
2244 * writepages, because a_ops->writepage won't be able
2245 * to make progress. The page will be redirtied by
2246 * writepage and writepages will again try to write
2247 * the same.
c4a0c46e 2248 */
1693918e
TT
2249 ext4_msg(mpd->inode->i_sb, KERN_CRIT,
2250 "delayed block allocation failed for inode %lu at "
2251 "logical offset %llu with max blocks %zd with "
fbe845dd 2252 "error %d", mpd->inode->i_ino,
1693918e
TT
2253 (unsigned long long) next,
2254 mpd->b_size >> mpd->inode->i_blkbits, err);
2255 printk(KERN_CRIT "This should not happen!! "
2256 "Data will be lost\n");
030ba6bc 2257 if (err == -ENOSPC) {
df22291f 2258 ext4_print_free_blocks(mpd->inode);
030ba6bc 2259 }
2fa3cdfb 2260 /* invalidate all the pages */
c4a0c46e 2261 ext4_da_block_invalidatepages(mpd, next,
8dc207c0 2262 mpd->b_size >> mpd->inode->i_blkbits);
c4a0c46e
AK
2263 return err;
2264 }
2fa3cdfb
TT
2265 BUG_ON(blks == 0);
2266
2ed88685
TT
2267 if (map.m_flags & EXT4_MAP_NEW) {
2268 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
2269 int i;
64769240 2270
2ed88685
TT
2271 for (i = 0; i < map.m_len; i++)
2272 unmap_underlying_metadata(bdev, map.m_pblk + i);
2273 }
64769240 2274
a1d6cc56
AK
2275 /*
2276 * If blocks are delayed marked, we need to
2277 * put actual blocknr and drop delayed bit
2278 */
8dc207c0
TT
2279 if ((mpd->b_state & (1 << BH_Delay)) ||
2280 (mpd->b_state & (1 << BH_Unwritten)))
2ed88685 2281 mpage_put_bnr_to_bhs(mpd, &map);
64769240 2282
2fa3cdfb
TT
2283 if (ext4_should_order_data(mpd->inode)) {
2284 err = ext4_jbd2_file_inode(handle, mpd->inode);
2285 if (err)
2286 return err;
2287 }
2288
2289 /*
03f5d8bc 2290 * Update on-disk size along with block allocation.
2fa3cdfb
TT
2291 */
2292 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2293 if (disksize > i_size_read(mpd->inode))
2294 disksize = i_size_read(mpd->inode);
2295 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2296 ext4_update_i_disksize(mpd->inode, disksize);
2297 return ext4_mark_inode_dirty(handle, mpd->inode);
2298 }
2299
c4a0c46e 2300 return 0;
64769240
AT
2301}
2302
bf068ee2
AK
2303#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2304 (1 << BH_Delay) | (1 << BH_Unwritten))
64769240
AT
2305
2306/*
2307 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2308 *
2309 * @mpd->lbh - extent of blocks
2310 * @logical - logical number of the block in the file
2311 * @bh - bh of the block (used to access block's state)
2312 *
2313 * the function is used to collect contig. blocks in same state
2314 */
2315static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
8dc207c0
TT
2316 sector_t logical, size_t b_size,
2317 unsigned long b_state)
64769240 2318{
64769240 2319 sector_t next;
8dc207c0 2320 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
64769240 2321
c445e3e0
ES
2322 /*
2323 * XXX Don't go larger than mballoc is willing to allocate
2324 * This is a stopgap solution. We eventually need to fold
2325 * mpage_da_submit_io() into this function and then call
2326 * ext4_get_blocks() multiple times in a loop
2327 */
2328 if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
2329 goto flush_it;
2330
525f4ed8 2331 /* check if thereserved journal credits might overflow */
12e9b892 2332 if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
525f4ed8
MC
2333 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2334 /*
2335 * With non-extent format we are limited by the journal
2336 * credit available. Total credit needed to insert
2337 * nrblocks contiguous blocks is dependent on the
2338 * nrblocks. So limit nrblocks.
2339 */
2340 goto flush_it;
2341 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2342 EXT4_MAX_TRANS_DATA) {
2343 /*
2344 * Adding the new buffer_head would make it cross the
2345 * allowed limit for which we have journal credit
2346 * reserved. So limit the new bh->b_size
2347 */
2348 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2349 mpd->inode->i_blkbits;
2350 /* we will do mpage_da_submit_io in the next loop */
2351 }
2352 }
64769240
AT
2353 /*
2354 * First block in the extent
2355 */
8dc207c0
TT
2356 if (mpd->b_size == 0) {
2357 mpd->b_blocknr = logical;
2358 mpd->b_size = b_size;
2359 mpd->b_state = b_state & BH_FLAGS;
64769240
AT
2360 return;
2361 }
2362
8dc207c0 2363 next = mpd->b_blocknr + nrblocks;
64769240
AT
2364 /*
2365 * Can we merge the block to our big extent?
2366 */
8dc207c0
TT
2367 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2368 mpd->b_size += b_size;
64769240
AT
2369 return;
2370 }
2371
525f4ed8 2372flush_it:
64769240
AT
2373 /*
2374 * We couldn't merge the block to our extent, so we
2375 * need to flush current extent and start new one
2376 */
c4a0c46e
AK
2377 if (mpage_da_map_blocks(mpd) == 0)
2378 mpage_da_submit_io(mpd);
a1d6cc56
AK
2379 mpd->io_done = 1;
2380 return;
64769240
AT
2381}
2382
c364b22c 2383static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
29fa89d0 2384{
c364b22c 2385 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
29fa89d0
AK
2386}
2387
64769240
AT
2388/*
2389 * __mpage_da_writepage - finds extent of pages and blocks
2390 *
2391 * @page: page to consider
2392 * @wbc: not used, we just follow rules
2393 * @data: context
2394 *
2395 * The function finds extents of pages and scan them for all blocks.
2396 */
2397static int __mpage_da_writepage(struct page *page,
2398 struct writeback_control *wbc, void *data)
2399{
2400 struct mpage_da_data *mpd = data;
2401 struct inode *inode = mpd->inode;
8dc207c0 2402 struct buffer_head *bh, *head;
64769240
AT
2403 sector_t logical;
2404
2405 /*
2406 * Can we merge this page to current extent?
2407 */
2408 if (mpd->next_page != page->index) {
2409 /*
2410 * Nope, we can't. So, we map non-allocated blocks
a1d6cc56 2411 * and start IO on them using writepage()
64769240
AT
2412 */
2413 if (mpd->next_page != mpd->first_page) {
c4a0c46e
AK
2414 if (mpage_da_map_blocks(mpd) == 0)
2415 mpage_da_submit_io(mpd);
a1d6cc56
AK
2416 /*
2417 * skip rest of the page in the page_vec
2418 */
2419 mpd->io_done = 1;
2420 redirty_page_for_writepage(wbc, page);
2421 unlock_page(page);
2422 return MPAGE_DA_EXTENT_TAIL;
64769240
AT
2423 }
2424
2425 /*
2426 * Start next extent of pages ...
2427 */
2428 mpd->first_page = page->index;
2429
2430 /*
2431 * ... and blocks
2432 */
8dc207c0
TT
2433 mpd->b_size = 0;
2434 mpd->b_state = 0;
2435 mpd->b_blocknr = 0;
64769240
AT
2436 }
2437
2438 mpd->next_page = page->index + 1;
2439 logical = (sector_t) page->index <<
2440 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2441
2442 if (!page_has_buffers(page)) {
8dc207c0
TT
2443 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2444 (1 << BH_Dirty) | (1 << BH_Uptodate));
a1d6cc56
AK
2445 if (mpd->io_done)
2446 return MPAGE_DA_EXTENT_TAIL;
64769240
AT
2447 } else {
2448 /*
2449 * Page with regular buffer heads, just add all dirty ones
2450 */
2451 head = page_buffers(page);
2452 bh = head;
2453 do {
2454 BUG_ON(buffer_locked(bh));
791b7f08
AK
2455 /*
2456 * We need to try to allocate
2457 * unmapped blocks in the same page.
2458 * Otherwise we won't make progress
43ce1d23 2459 * with the page in ext4_writepage
791b7f08 2460 */
c364b22c 2461 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
8dc207c0
TT
2462 mpage_add_bh_to_extent(mpd, logical,
2463 bh->b_size,
2464 bh->b_state);
a1d6cc56
AK
2465 if (mpd->io_done)
2466 return MPAGE_DA_EXTENT_TAIL;
791b7f08
AK
2467 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2468 /*
2469 * mapped dirty buffer. We need to update
2470 * the b_state because we look at
2471 * b_state in mpage_da_map_blocks. We don't
2472 * update b_size because if we find an
2473 * unmapped buffer_head later we need to
2474 * use the b_state flag of that buffer_head.
2475 */
8dc207c0
TT
2476 if (mpd->b_size == 0)
2477 mpd->b_state = bh->b_state & BH_FLAGS;
a1d6cc56 2478 }
64769240
AT
2479 logical++;
2480 } while ((bh = bh->b_this_page) != head);
2481 }
2482
2483 return 0;
2484}
2485
64769240 2486/*
b920c755
TT
2487 * This is a special get_blocks_t callback which is used by
2488 * ext4_da_write_begin(). It will either return mapped block or
2489 * reserve space for a single block.
29fa89d0
AK
2490 *
2491 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2492 * We also have b_blocknr = -1 and b_bdev initialized properly
2493 *
2494 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2495 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2496 * initialized properly.
64769240
AT
2497 */
2498static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2ed88685 2499 struct buffer_head *bh, int create)
64769240 2500{
2ed88685 2501 struct ext4_map_blocks map;
64769240 2502 int ret = 0;
33b9817e
AK
2503 sector_t invalid_block = ~((sector_t) 0xffff);
2504
2505 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2506 invalid_block = ~0;
64769240
AT
2507
2508 BUG_ON(create == 0);
2ed88685
TT
2509 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
2510
2511 map.m_lblk = iblock;
2512 map.m_len = 1;
64769240
AT
2513
2514 /*
2515 * first, we need to know whether the block is allocated already
2516 * preallocated blocks are unmapped but should treated
2517 * the same as allocated blocks.
2518 */
2ed88685
TT
2519 ret = ext4_map_blocks(NULL, inode, &map, 0);
2520 if (ret < 0)
2521 return ret;
2522 if (ret == 0) {
2523 if (buffer_delay(bh))
2524 return 0; /* Not sure this could or should happen */
64769240
AT
2525 /*
2526 * XXX: __block_prepare_write() unmaps passed block,
2527 * is it OK?
2528 */
9d0be502 2529 ret = ext4_da_reserve_space(inode, iblock);
d2a17637
MC
2530 if (ret)
2531 /* not enough space to reserve */
2532 return ret;
2533
2ed88685
TT
2534 map_bh(bh, inode->i_sb, invalid_block);
2535 set_buffer_new(bh);
2536 set_buffer_delay(bh);
2537 return 0;
64769240
AT
2538 }
2539
2ed88685
TT
2540 map_bh(bh, inode->i_sb, map.m_pblk);
2541 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
2542
2543 if (buffer_unwritten(bh)) {
2544 /* A delayed write to unwritten bh should be marked
2545 * new and mapped. Mapped ensures that we don't do
2546 * get_block multiple times when we write to the same
2547 * offset and new ensures that we do proper zero out
2548 * for partial write.
2549 */
2550 set_buffer_new(bh);
2551 set_buffer_mapped(bh);
2552 }
2553 return 0;
64769240 2554}
61628a3f 2555
b920c755
TT
2556/*
2557 * This function is used as a standard get_block_t calback function
2558 * when there is no desire to allocate any blocks. It is used as a
2559 * callback function for block_prepare_write(), nobh_writepage(), and
2560 * block_write_full_page(). These functions should only try to map a
2561 * single block at a time.
2562 *
2563 * Since this function doesn't do block allocations even if the caller
2564 * requests it by passing in create=1, it is critically important that
2565 * any caller checks to make sure that any buffer heads are returned
2566 * by this function are either all already mapped or marked for
2567 * delayed allocation before calling nobh_writepage() or
2568 * block_write_full_page(). Otherwise, b_blocknr could be left
2569 * unitialized, and the page write functions will be taken by
2570 * surprise.
2571 */
2572static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
f0e6c985
AK
2573 struct buffer_head *bh_result, int create)
2574{
a2dc52b5 2575 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2ed88685 2576 return _ext4_get_block(inode, iblock, bh_result, 0);
61628a3f
MC
2577}
2578
62e086be
AK
2579static int bget_one(handle_t *handle, struct buffer_head *bh)
2580{
2581 get_bh(bh);
2582 return 0;
2583}
2584
2585static int bput_one(handle_t *handle, struct buffer_head *bh)
2586{
2587 put_bh(bh);
2588 return 0;
2589}
2590
2591static int __ext4_journalled_writepage(struct page *page,
62e086be
AK
2592 unsigned int len)
2593{
2594 struct address_space *mapping = page->mapping;
2595 struct inode *inode = mapping->host;
2596 struct buffer_head *page_bufs;
2597 handle_t *handle = NULL;
2598 int ret = 0;
2599 int err;
2600
2601 page_bufs = page_buffers(page);
2602 BUG_ON(!page_bufs);
2603 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2604 /* As soon as we unlock the page, it can go away, but we have
2605 * references to buffers so we are safe */
2606 unlock_page(page);
2607
2608 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2609 if (IS_ERR(handle)) {
2610 ret = PTR_ERR(handle);
2611 goto out;
2612 }
2613
2614 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2615 do_journal_get_write_access);
2616
2617 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2618 write_end_fn);
2619 if (ret == 0)
2620 ret = err;
2621 err = ext4_journal_stop(handle);
2622 if (!ret)
2623 ret = err;
2624
2625 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
19f5fb7a 2626 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
62e086be
AK
2627out:
2628 return ret;
2629}
2630
744692dc
JZ
2631static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
2632static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
2633
61628a3f 2634/*
43ce1d23
AK
2635 * Note that we don't need to start a transaction unless we're journaling data
2636 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2637 * need to file the inode to the transaction's list in ordered mode because if
2638 * we are writing back data added by write(), the inode is already there and if
2639 * we are writing back data modified via mmap(), noone guarantees in which
2640 * transaction the data will hit the disk. In case we are journaling data, we
2641 * cannot start transaction directly because transaction start ranks above page
2642 * lock so we have to do some magic.
2643 *
b920c755
TT
2644 * This function can get called via...
2645 * - ext4_da_writepages after taking page lock (have journal handle)
2646 * - journal_submit_inode_data_buffers (no journal handle)
2647 * - shrink_page_list via pdflush (no journal handle)
2648 * - grab_page_cache when doing write_begin (have journal handle)
43ce1d23
AK
2649 *
2650 * We don't do any block allocation in this function. If we have page with
2651 * multiple blocks we need to write those buffer_heads that are mapped. This
2652 * is important for mmaped based write. So if we do with blocksize 1K
2653 * truncate(f, 1024);
2654 * a = mmap(f, 0, 4096);
2655 * a[0] = 'a';
2656 * truncate(f, 4096);
2657 * we have in the page first buffer_head mapped via page_mkwrite call back
2658 * but other bufer_heads would be unmapped but dirty(dirty done via the
2659 * do_wp_page). So writepage should write the first block. If we modify
2660 * the mmap area beyond 1024 we will again get a page_fault and the
2661 * page_mkwrite callback will do the block allocation and mark the
2662 * buffer_heads mapped.
2663 *
2664 * We redirty the page if we have any buffer_heads that is either delay or
2665 * unwritten in the page.
2666 *
2667 * We can get recursively called as show below.
2668 *
2669 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2670 * ext4_writepage()
2671 *
2672 * But since we don't do any block allocation we should not deadlock.
2673 * Page also have the dirty flag cleared so we don't get recurive page_lock.
61628a3f 2674 */
43ce1d23 2675static int ext4_writepage(struct page *page,
62e086be 2676 struct writeback_control *wbc)
64769240 2677{
64769240 2678 int ret = 0;
61628a3f 2679 loff_t size;
498e5f24 2680 unsigned int len;
744692dc 2681 struct buffer_head *page_bufs = NULL;
61628a3f
MC
2682 struct inode *inode = page->mapping->host;
2683
43ce1d23 2684 trace_ext4_writepage(inode, page);
f0e6c985
AK
2685 size = i_size_read(inode);
2686 if (page->index == size >> PAGE_CACHE_SHIFT)
2687 len = size & ~PAGE_CACHE_MASK;
2688 else
2689 len = PAGE_CACHE_SIZE;
64769240 2690
f0e6c985 2691 if (page_has_buffers(page)) {
61628a3f 2692 page_bufs = page_buffers(page);
f0e6c985 2693 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
c364b22c 2694 ext4_bh_delay_or_unwritten)) {
61628a3f 2695 /*
f0e6c985
AK
2696 * We don't want to do block allocation
2697 * So redirty the page and return
cd1aac32
AK
2698 * We may reach here when we do a journal commit
2699 * via journal_submit_inode_data_buffers.
2700 * If we don't have mapping block we just ignore
f0e6c985
AK
2701 * them. We can also reach here via shrink_page_list
2702 */
2703 redirty_page_for_writepage(wbc, page);
2704 unlock_page(page);
2705 return 0;
2706 }
2707 } else {
2708 /*
2709 * The test for page_has_buffers() is subtle:
2710 * We know the page is dirty but it lost buffers. That means
2711 * that at some moment in time after write_begin()/write_end()
2712 * has been called all buffers have been clean and thus they
2713 * must have been written at least once. So they are all
2714 * mapped and we can happily proceed with mapping them
2715 * and writing the page.
2716 *
2717 * Try to initialize the buffer_heads and check whether
2718 * all are mapped and non delay. We don't want to
2719 * do block allocation here.
2720 */
b767e78a 2721 ret = block_prepare_write(page, 0, len,
b920c755 2722 noalloc_get_block_write);
f0e6c985
AK
2723 if (!ret) {
2724 page_bufs = page_buffers(page);
2725 /* check whether all are mapped and non delay */
2726 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
c364b22c 2727 ext4_bh_delay_or_unwritten)) {
f0e6c985
AK
2728 redirty_page_for_writepage(wbc, page);
2729 unlock_page(page);
2730 return 0;
2731 }
2732 } else {
2733 /*
2734 * We can't do block allocation here
2735 * so just redity the page and unlock
2736 * and return
61628a3f 2737 */
61628a3f
MC
2738 redirty_page_for_writepage(wbc, page);
2739 unlock_page(page);
2740 return 0;
2741 }
ed9b3e33 2742 /* now mark the buffer_heads as dirty and uptodate */
b767e78a 2743 block_commit_write(page, 0, len);
64769240
AT
2744 }
2745
43ce1d23
AK
2746 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2747 /*
2748 * It's mmapped pagecache. Add buffers and journal it. There
2749 * doesn't seem much point in redirtying the page here.
2750 */
2751 ClearPageChecked(page);
3f0ca309 2752 return __ext4_journalled_writepage(page, len);
43ce1d23
AK
2753 }
2754
64769240 2755 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
b920c755 2756 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
744692dc
JZ
2757 else if (page_bufs && buffer_uninit(page_bufs)) {
2758 ext4_set_bh_endio(page_bufs, inode);
2759 ret = block_write_full_page_endio(page, noalloc_get_block_write,
2760 wbc, ext4_end_io_buffer_write);
2761 } else
b920c755
TT
2762 ret = block_write_full_page(page, noalloc_get_block_write,
2763 wbc);
64769240 2764
64769240
AT
2765 return ret;
2766}
2767
61628a3f 2768/*
525f4ed8
MC
2769 * This is called via ext4_da_writepages() to
2770 * calulate the total number of credits to reserve to fit
2771 * a single extent allocation into a single transaction,
2772 * ext4_da_writpeages() will loop calling this before
2773 * the block allocation.
61628a3f 2774 */
525f4ed8
MC
2775
2776static int ext4_da_writepages_trans_blocks(struct inode *inode)
2777{
2778 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2779
2780 /*
2781 * With non-extent format the journal credit needed to
2782 * insert nrblocks contiguous block is dependent on
2783 * number of contiguous block. So we will limit
2784 * number of contiguous block to a sane value
2785 */
12e9b892 2786 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
525f4ed8
MC
2787 (max_blocks > EXT4_MAX_TRANS_DATA))
2788 max_blocks = EXT4_MAX_TRANS_DATA;
2789
2790 return ext4_chunk_trans_blocks(inode, max_blocks);
2791}
61628a3f 2792
8e48dcfb
TT
2793/*
2794 * write_cache_pages_da - walk the list of dirty pages of the given
2795 * address space and call the callback function (which usually writes
2796 * the pages).
2797 *
2798 * This is a forked version of write_cache_pages(). Differences:
2799 * Range cyclic is ignored.
2800 * no_nrwrite_index_update is always presumed true
2801 */
2802static int write_cache_pages_da(struct address_space *mapping,
2803 struct writeback_control *wbc,
2804 struct mpage_da_data *mpd)
2805{
2806 int ret = 0;
2807 int done = 0;
2808 struct pagevec pvec;
2809 int nr_pages;
2810 pgoff_t index;
2811 pgoff_t end; /* Inclusive */
2812 long nr_to_write = wbc->nr_to_write;
2813
2814 pagevec_init(&pvec, 0);
2815 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2816 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2817
2818 while (!done && (index <= end)) {
2819 int i;
2820
2821 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2822 PAGECACHE_TAG_DIRTY,
2823 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2824 if (nr_pages == 0)
2825 break;
2826
2827 for (i = 0; i < nr_pages; i++) {
2828 struct page *page = pvec.pages[i];
2829
2830 /*
2831 * At this point, the page may be truncated or
2832 * invalidated (changing page->mapping to NULL), or
2833 * even swizzled back from swapper_space to tmpfs file
2834 * mapping. However, page->index will not change
2835 * because we have a reference on the page.
2836 */
2837 if (page->index > end) {
2838 done = 1;
2839 break;
2840 }
2841
2842 lock_page(page);
2843
2844 /*
2845 * Page truncated or invalidated. We can freely skip it
2846 * then, even for data integrity operations: the page
2847 * has disappeared concurrently, so there could be no
2848 * real expectation of this data interity operation
2849 * even if there is now a new, dirty page at the same
2850 * pagecache address.
2851 */
2852 if (unlikely(page->mapping != mapping)) {
2853continue_unlock:
2854 unlock_page(page);
2855 continue;
2856 }
2857
2858 if (!PageDirty(page)) {
2859 /* someone wrote it for us */
2860 goto continue_unlock;
2861 }
2862
2863 if (PageWriteback(page)) {
2864 if (wbc->sync_mode != WB_SYNC_NONE)
2865 wait_on_page_writeback(page);
2866 else
2867 goto continue_unlock;
2868 }
2869
2870 BUG_ON(PageWriteback(page));
2871 if (!clear_page_dirty_for_io(page))
2872 goto continue_unlock;
2873
2874 ret = __mpage_da_writepage(page, wbc, mpd);
2875 if (unlikely(ret)) {
2876 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2877 unlock_page(page);
2878 ret = 0;
2879 } else {
2880 done = 1;
2881 break;
2882 }
2883 }
2884
2885 if (nr_to_write > 0) {
2886 nr_to_write--;
2887 if (nr_to_write == 0 &&
2888 wbc->sync_mode == WB_SYNC_NONE) {
2889 /*
2890 * We stop writing back only if we are
2891 * not doing integrity sync. In case of
2892 * integrity sync we have to keep going
2893 * because someone may be concurrently
2894 * dirtying pages, and we might have
2895 * synced a lot of newly appeared dirty
2896 * pages, but have not synced all of the
2897 * old dirty pages.
2898 */
2899 done = 1;
2900 break;
2901 }
2902 }
2903 }
2904 pagevec_release(&pvec);
2905 cond_resched();
2906 }
2907 return ret;
2908}
2909
2910
64769240 2911static int ext4_da_writepages(struct address_space *mapping,
a1d6cc56 2912 struct writeback_control *wbc)
64769240 2913{
22208ded
AK
2914 pgoff_t index;
2915 int range_whole = 0;
61628a3f 2916 handle_t *handle = NULL;
df22291f 2917 struct mpage_da_data mpd;
5e745b04 2918 struct inode *inode = mapping->host;
498e5f24
TT
2919 int pages_written = 0;
2920 long pages_skipped;
55138e0b 2921 unsigned int max_pages;
2acf2c26 2922 int range_cyclic, cycled = 1, io_done = 0;
55138e0b
TT
2923 int needed_blocks, ret = 0;
2924 long desired_nr_to_write, nr_to_writebump = 0;
de89de6e 2925 loff_t range_start = wbc->range_start;
5e745b04 2926 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
61628a3f 2927
9bffad1e 2928 trace_ext4_da_writepages(inode, wbc);
ba80b101 2929
61628a3f
MC
2930 /*
2931 * No pages to write? This is mainly a kludge to avoid starting
2932 * a transaction for special inodes like journal inode on last iput()
2933 * because that could violate lock ordering on umount
2934 */
a1d6cc56 2935 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
61628a3f 2936 return 0;
2a21e37e
TT
2937
2938 /*
2939 * If the filesystem has aborted, it is read-only, so return
2940 * right away instead of dumping stack traces later on that
2941 * will obscure the real source of the problem. We test
4ab2f15b 2942 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
2a21e37e
TT
2943 * the latter could be true if the filesystem is mounted
2944 * read-only, and in that case, ext4_da_writepages should
2945 * *never* be called, so if that ever happens, we would want
2946 * the stack trace.
2947 */
4ab2f15b 2948 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
2a21e37e
TT
2949 return -EROFS;
2950
22208ded
AK
2951 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2952 range_whole = 1;
61628a3f 2953
2acf2c26
AK
2954 range_cyclic = wbc->range_cyclic;
2955 if (wbc->range_cyclic) {
22208ded 2956 index = mapping->writeback_index;
2acf2c26
AK
2957 if (index)
2958 cycled = 0;
2959 wbc->range_start = index << PAGE_CACHE_SHIFT;
2960 wbc->range_end = LLONG_MAX;
2961 wbc->range_cyclic = 0;
2962 } else
22208ded 2963 index = wbc->range_start >> PAGE_CACHE_SHIFT;
a1d6cc56 2964
55138e0b
TT
2965 /*
2966 * This works around two forms of stupidity. The first is in
2967 * the writeback code, which caps the maximum number of pages
2968 * written to be 1024 pages. This is wrong on multiple
2969 * levels; different architectues have a different page size,
2970 * which changes the maximum amount of data which gets
2971 * written. Secondly, 4 megabytes is way too small. XFS
2972 * forces this value to be 16 megabytes by multiplying
2973 * nr_to_write parameter by four, and then relies on its
2974 * allocator to allocate larger extents to make them
2975 * contiguous. Unfortunately this brings us to the second
2976 * stupidity, which is that ext4's mballoc code only allocates
2977 * at most 2048 blocks. So we force contiguous writes up to
2978 * the number of dirty blocks in the inode, or
2979 * sbi->max_writeback_mb_bump whichever is smaller.
2980 */
2981 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2982 if (!range_cyclic && range_whole)
2983 desired_nr_to_write = wbc->nr_to_write * 8;
2984 else
2985 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2986 max_pages);
2987 if (desired_nr_to_write > max_pages)
2988 desired_nr_to_write = max_pages;
2989
2990 if (wbc->nr_to_write < desired_nr_to_write) {
2991 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2992 wbc->nr_to_write = desired_nr_to_write;
2993 }
2994
df22291f
AK
2995 mpd.wbc = wbc;
2996 mpd.inode = mapping->host;
2997
22208ded
AK
2998 pages_skipped = wbc->pages_skipped;
2999
2acf2c26 3000retry:
22208ded 3001 while (!ret && wbc->nr_to_write > 0) {
a1d6cc56
AK
3002
3003 /*
3004 * we insert one extent at a time. So we need
3005 * credit needed for single extent allocation.
3006 * journalled mode is currently not supported
3007 * by delalloc
3008 */
3009 BUG_ON(ext4_should_journal_data(inode));
525f4ed8 3010 needed_blocks = ext4_da_writepages_trans_blocks(inode);
a1d6cc56 3011
61628a3f
MC
3012 /* start a new transaction*/
3013 handle = ext4_journal_start(inode, needed_blocks);
3014 if (IS_ERR(handle)) {
3015 ret = PTR_ERR(handle);
1693918e 3016 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
fbe845dd 3017 "%ld pages, ino %lu; err %d", __func__,
a1d6cc56 3018 wbc->nr_to_write, inode->i_ino, ret);
61628a3f
MC
3019 goto out_writepages;
3020 }
f63e6005
TT
3021
3022 /*
3023 * Now call __mpage_da_writepage to find the next
3024 * contiguous region of logical blocks that need
3025 * blocks to be allocated by ext4. We don't actually
3026 * submit the blocks for I/O here, even though
3027 * write_cache_pages thinks it will, and will set the
3028 * pages as clean for write before calling
3029 * __mpage_da_writepage().
3030 */
3031 mpd.b_size = 0;
3032 mpd.b_state = 0;
3033 mpd.b_blocknr = 0;
3034 mpd.first_page = 0;
3035 mpd.next_page = 0;
3036 mpd.io_done = 0;
3037 mpd.pages_written = 0;
3038 mpd.retval = 0;
8e48dcfb 3039 ret = write_cache_pages_da(mapping, wbc, &mpd);
f63e6005 3040 /*
af901ca1 3041 * If we have a contiguous extent of pages and we
f63e6005
TT
3042 * haven't done the I/O yet, map the blocks and submit
3043 * them for I/O.
3044 */
3045 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
3046 if (mpage_da_map_blocks(&mpd) == 0)
3047 mpage_da_submit_io(&mpd);
3048 mpd.io_done = 1;
3049 ret = MPAGE_DA_EXTENT_TAIL;
3050 }
b3a3ca8c 3051 trace_ext4_da_write_pages(inode, &mpd);
f63e6005 3052 wbc->nr_to_write -= mpd.pages_written;
df22291f 3053
61628a3f 3054 ext4_journal_stop(handle);
df22291f 3055
8f64b32e 3056 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
22208ded
AK
3057 /* commit the transaction which would
3058 * free blocks released in the transaction
3059 * and try again
3060 */
df22291f 3061 jbd2_journal_force_commit_nested(sbi->s_journal);
22208ded
AK
3062 wbc->pages_skipped = pages_skipped;
3063 ret = 0;
3064 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
a1d6cc56
AK
3065 /*
3066 * got one extent now try with
3067 * rest of the pages
3068 */
22208ded
AK
3069 pages_written += mpd.pages_written;
3070 wbc->pages_skipped = pages_skipped;
a1d6cc56 3071 ret = 0;
2acf2c26 3072 io_done = 1;
22208ded 3073 } else if (wbc->nr_to_write)
61628a3f
MC
3074 /*
3075 * There is no more writeout needed
3076 * or we requested for a noblocking writeout
3077 * and we found the device congested
3078 */
61628a3f 3079 break;
a1d6cc56 3080 }
2acf2c26
AK
3081 if (!io_done && !cycled) {
3082 cycled = 1;
3083 index = 0;
3084 wbc->range_start = index << PAGE_CACHE_SHIFT;
3085 wbc->range_end = mapping->writeback_index - 1;
3086 goto retry;
3087 }
22208ded 3088 if (pages_skipped != wbc->pages_skipped)
1693918e
TT
3089 ext4_msg(inode->i_sb, KERN_CRIT,
3090 "This should not happen leaving %s "
fbe845dd 3091 "with nr_to_write = %ld ret = %d",
1693918e 3092 __func__, wbc->nr_to_write, ret);
22208ded
AK
3093
3094 /* Update index */
3095 index += pages_written;
2acf2c26 3096 wbc->range_cyclic = range_cyclic;
22208ded
AK
3097 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3098 /*
3099 * set the writeback_index so that range_cyclic
3100 * mode will write it back later
3101 */
3102 mapping->writeback_index = index;
a1d6cc56 3103
61628a3f 3104out_writepages:
2faf2e19 3105 wbc->nr_to_write -= nr_to_writebump;
de89de6e 3106 wbc->range_start = range_start;
9bffad1e 3107 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
61628a3f 3108 return ret;
64769240
AT
3109}
3110
79f0be8d
AK
3111#define FALL_BACK_TO_NONDELALLOC 1
3112static int ext4_nonda_switch(struct super_block *sb)
3113{
3114 s64 free_blocks, dirty_blocks;
3115 struct ext4_sb_info *sbi = EXT4_SB(sb);
3116
3117 /*
3118 * switch to non delalloc mode if we are running low
3119 * on free block. The free block accounting via percpu
179f7ebf 3120 * counters can get slightly wrong with percpu_counter_batch getting
79f0be8d
AK
3121 * accumulated on each CPU without updating global counters
3122 * Delalloc need an accurate free block accounting. So switch
3123 * to non delalloc when we are near to error range.
3124 */
3125 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3126 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
3127 if (2 * free_blocks < 3 * dirty_blocks ||
3128 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
3129 /*
c8afb446
ES
3130 * free block count is less than 150% of dirty blocks
3131 * or free blocks is less than watermark
79f0be8d
AK
3132 */
3133 return 1;
3134 }
c8afb446
ES
3135 /*
3136 * Even if we don't switch but are nearing capacity,
3137 * start pushing delalloc when 1/2 of free blocks are dirty.
3138 */
3139 if (free_blocks < 2 * dirty_blocks)
3140 writeback_inodes_sb_if_idle(sb);
3141
79f0be8d
AK
3142 return 0;
3143}
3144
64769240 3145static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
de9a55b8
TT
3146 loff_t pos, unsigned len, unsigned flags,
3147 struct page **pagep, void **fsdata)
64769240 3148{
72b8ab9d 3149 int ret, retries = 0;
64769240
AT
3150 struct page *page;
3151 pgoff_t index;
3152 unsigned from, to;
3153 struct inode *inode = mapping->host;
3154 handle_t *handle;
3155
3156 index = pos >> PAGE_CACHE_SHIFT;
3157 from = pos & (PAGE_CACHE_SIZE - 1);
3158 to = from + len;
79f0be8d
AK
3159
3160 if (ext4_nonda_switch(inode->i_sb)) {
3161 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
3162 return ext4_write_begin(file, mapping, pos,
3163 len, flags, pagep, fsdata);
3164 }
3165 *fsdata = (void *)0;
9bffad1e 3166 trace_ext4_da_write_begin(inode, pos, len, flags);
d2a17637 3167retry:
64769240
AT
3168 /*
3169 * With delayed allocation, we don't log the i_disksize update
3170 * if there is delayed block allocation. But we still need
3171 * to journalling the i_disksize update if writes to the end
3172 * of file which has an already mapped buffer.
3173 */
3174 handle = ext4_journal_start(inode, 1);
3175 if (IS_ERR(handle)) {
3176 ret = PTR_ERR(handle);
3177 goto out;
3178 }
ebd3610b
JK
3179 /* We cannot recurse into the filesystem as the transaction is already
3180 * started */
3181 flags |= AOP_FLAG_NOFS;
64769240 3182
54566b2c 3183 page = grab_cache_page_write_begin(mapping, index, flags);
d5a0d4f7
ES
3184 if (!page) {
3185 ext4_journal_stop(handle);
3186 ret = -ENOMEM;
3187 goto out;
3188 }
64769240
AT
3189 *pagep = page;
3190
6e1db88d 3191 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
64769240
AT
3192 if (ret < 0) {
3193 unlock_page(page);
3194 ext4_journal_stop(handle);
3195 page_cache_release(page);
ae4d5372
AK
3196 /*
3197 * block_write_begin may have instantiated a few blocks
3198 * outside i_size. Trim these off again. Don't need
3199 * i_size_read because we hold i_mutex.
3200 */
3201 if (pos + len > inode->i_size)
b9a4207d 3202 ext4_truncate_failed_write(inode);
64769240
AT
3203 }
3204
d2a17637
MC
3205 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3206 goto retry;
64769240
AT
3207out:
3208 return ret;
3209}
3210
632eaeab
MC
3211/*
3212 * Check if we should update i_disksize
3213 * when write to the end of file but not require block allocation
3214 */
3215static int ext4_da_should_update_i_disksize(struct page *page,
de9a55b8 3216 unsigned long offset)
632eaeab
MC
3217{
3218 struct buffer_head *bh;
3219 struct inode *inode = page->mapping->host;
3220 unsigned int idx;
3221 int i;
3222
3223 bh = page_buffers(page);
3224 idx = offset >> inode->i_blkbits;
3225
af5bc92d 3226 for (i = 0; i < idx; i++)
632eaeab
MC
3227 bh = bh->b_this_page;
3228
29fa89d0 3229 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
632eaeab
MC
3230 return 0;
3231 return 1;
3232}
3233
64769240 3234static int ext4_da_write_end(struct file *file,
de9a55b8
TT
3235 struct address_space *mapping,
3236 loff_t pos, unsigned len, unsigned copied,
3237 struct page *page, void *fsdata)
64769240
AT
3238{
3239 struct inode *inode = mapping->host;
3240 int ret = 0, ret2;
3241 handle_t *handle = ext4_journal_current_handle();
3242 loff_t new_i_size;
632eaeab 3243 unsigned long start, end;
79f0be8d
AK
3244 int write_mode = (int)(unsigned long)fsdata;
3245
3246 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3247 if (ext4_should_order_data(inode)) {
3248 return ext4_ordered_write_end(file, mapping, pos,
3249 len, copied, page, fsdata);
3250 } else if (ext4_should_writeback_data(inode)) {
3251 return ext4_writeback_write_end(file, mapping, pos,
3252 len, copied, page, fsdata);
3253 } else {
3254 BUG();
3255 }
3256 }
632eaeab 3257
9bffad1e 3258 trace_ext4_da_write_end(inode, pos, len, copied);
632eaeab 3259 start = pos & (PAGE_CACHE_SIZE - 1);
af5bc92d 3260 end = start + copied - 1;
64769240
AT
3261
3262 /*
3263 * generic_write_end() will run mark_inode_dirty() if i_size
3264 * changes. So let's piggyback the i_disksize mark_inode_dirty
3265 * into that.
3266 */
3267
3268 new_i_size = pos + copied;
632eaeab
MC
3269 if (new_i_size > EXT4_I(inode)->i_disksize) {
3270 if (ext4_da_should_update_i_disksize(page, end)) {
3271 down_write(&EXT4_I(inode)->i_data_sem);
3272 if (new_i_size > EXT4_I(inode)->i_disksize) {
3273 /*
3274 * Updating i_disksize when extending file
3275 * without needing block allocation
3276 */
3277 if (ext4_should_order_data(inode))
3278 ret = ext4_jbd2_file_inode(handle,
3279 inode);
64769240 3280
632eaeab
MC
3281 EXT4_I(inode)->i_disksize = new_i_size;
3282 }
3283 up_write(&EXT4_I(inode)->i_data_sem);
cf17fea6
AK
3284 /* We need to mark inode dirty even if
3285 * new_i_size is less that inode->i_size
3286 * bu greater than i_disksize.(hint delalloc)
3287 */
3288 ext4_mark_inode_dirty(handle, inode);
64769240 3289 }
632eaeab 3290 }
64769240
AT
3291 ret2 = generic_write_end(file, mapping, pos, len, copied,
3292 page, fsdata);
3293 copied = ret2;
3294 if (ret2 < 0)
3295 ret = ret2;
3296 ret2 = ext4_journal_stop(handle);
3297 if (!ret)
3298 ret = ret2;
3299
3300 return ret ? ret : copied;
3301}
3302
3303static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3304{
64769240
AT
3305 /*
3306 * Drop reserved blocks
3307 */
3308 BUG_ON(!PageLocked(page));
3309 if (!page_has_buffers(page))
3310 goto out;
3311
d2a17637 3312 ext4_da_page_release_reservation(page, offset);
64769240
AT
3313
3314out:
3315 ext4_invalidatepage(page, offset);
3316
3317 return;
3318}
3319
ccd2506b
TT
3320/*
3321 * Force all delayed allocation blocks to be allocated for a given inode.
3322 */
3323int ext4_alloc_da_blocks(struct inode *inode)
3324{
fb40ba0d
TT
3325 trace_ext4_alloc_da_blocks(inode);
3326
ccd2506b
TT
3327 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3328 !EXT4_I(inode)->i_reserved_meta_blocks)
3329 return 0;
3330
3331 /*
3332 * We do something simple for now. The filemap_flush() will
3333 * also start triggering a write of the data blocks, which is
3334 * not strictly speaking necessary (and for users of
3335 * laptop_mode, not even desirable). However, to do otherwise
3336 * would require replicating code paths in:
de9a55b8 3337 *
ccd2506b
TT
3338 * ext4_da_writepages() ->
3339 * write_cache_pages() ---> (via passed in callback function)
3340 * __mpage_da_writepage() -->
3341 * mpage_add_bh_to_extent()
3342 * mpage_da_map_blocks()
3343 *
3344 * The problem is that write_cache_pages(), located in
3345 * mm/page-writeback.c, marks pages clean in preparation for
3346 * doing I/O, which is not desirable if we're not planning on
3347 * doing I/O at all.
3348 *
3349 * We could call write_cache_pages(), and then redirty all of
3350 * the pages by calling redirty_page_for_writeback() but that
3351 * would be ugly in the extreme. So instead we would need to
3352 * replicate parts of the code in the above functions,
3353 * simplifying them becuase we wouldn't actually intend to
3354 * write out the pages, but rather only collect contiguous
3355 * logical block extents, call the multi-block allocator, and
3356 * then update the buffer heads with the block allocations.
de9a55b8 3357 *
ccd2506b
TT
3358 * For now, though, we'll cheat by calling filemap_flush(),
3359 * which will map the blocks, and start the I/O, but not
3360 * actually wait for the I/O to complete.
3361 */
3362 return filemap_flush(inode->i_mapping);
3363}
64769240 3364
ac27a0ec
DK
3365/*
3366 * bmap() is special. It gets used by applications such as lilo and by
3367 * the swapper to find the on-disk block of a specific piece of data.
3368 *
3369 * Naturally, this is dangerous if the block concerned is still in the
617ba13b 3370 * journal. If somebody makes a swapfile on an ext4 data-journaling
ac27a0ec
DK
3371 * filesystem and enables swap, then they may get a nasty shock when the
3372 * data getting swapped to that swapfile suddenly gets overwritten by
3373 * the original zero's written out previously to the journal and
3374 * awaiting writeback in the kernel's buffer cache.
3375 *
3376 * So, if we see any bmap calls here on a modified, data-journaled file,
3377 * take extra steps to flush any blocks which might be in the cache.
3378 */
617ba13b 3379static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
ac27a0ec
DK
3380{
3381 struct inode *inode = mapping->host;
3382 journal_t *journal;
3383 int err;
3384
64769240
AT
3385 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3386 test_opt(inode->i_sb, DELALLOC)) {
3387 /*
3388 * With delalloc we want to sync the file
3389 * so that we can make sure we allocate
3390 * blocks for file
3391 */
3392 filemap_write_and_wait(mapping);
3393 }
3394
19f5fb7a
TT
3395 if (EXT4_JOURNAL(inode) &&
3396 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
ac27a0ec
DK
3397 /*
3398 * This is a REALLY heavyweight approach, but the use of
3399 * bmap on dirty files is expected to be extremely rare:
3400 * only if we run lilo or swapon on a freshly made file
3401 * do we expect this to happen.
3402 *
3403 * (bmap requires CAP_SYS_RAWIO so this does not
3404 * represent an unprivileged user DOS attack --- we'd be
3405 * in trouble if mortal users could trigger this path at
3406 * will.)
3407 *
617ba13b 3408 * NB. EXT4_STATE_JDATA is not set on files other than
ac27a0ec
DK
3409 * regular files. If somebody wants to bmap a directory
3410 * or symlink and gets confused because the buffer
3411 * hasn't yet been flushed to disk, they deserve
3412 * everything they get.
3413 */
3414
19f5fb7a 3415 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
617ba13b 3416 journal = EXT4_JOURNAL(inode);
dab291af
MC
3417 jbd2_journal_lock_updates(journal);
3418 err = jbd2_journal_flush(journal);
3419 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
3420
3421 if (err)
3422 return 0;
3423 }
3424
af5bc92d 3425 return generic_block_bmap(mapping, block, ext4_get_block);
ac27a0ec
DK
3426}
3427
617ba13b 3428static int ext4_readpage(struct file *file, struct page *page)
ac27a0ec 3429{
617ba13b 3430 return mpage_readpage(page, ext4_get_block);
ac27a0ec
DK
3431}
3432
3433static int
617ba13b 3434ext4_readpages(struct file *file, struct address_space *mapping,
ac27a0ec
DK
3435 struct list_head *pages, unsigned nr_pages)
3436{
617ba13b 3437 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
ac27a0ec
DK
3438}
3439
744692dc
JZ
3440static void ext4_free_io_end(ext4_io_end_t *io)
3441{
3442 BUG_ON(!io);
3443 if (io->page)
3444 put_page(io->page);
3445 iput(io->inode);
3446 kfree(io);
3447}
3448
3449static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset)
3450{
3451 struct buffer_head *head, *bh;
3452 unsigned int curr_off = 0;
3453
3454 if (!page_has_buffers(page))
3455 return;
3456 head = bh = page_buffers(page);
3457 do {
3458 if (offset <= curr_off && test_clear_buffer_uninit(bh)
3459 && bh->b_private) {
3460 ext4_free_io_end(bh->b_private);
3461 bh->b_private = NULL;
3462 bh->b_end_io = NULL;
3463 }
3464 curr_off = curr_off + bh->b_size;
3465 bh = bh->b_this_page;
3466 } while (bh != head);
3467}
3468
617ba13b 3469static void ext4_invalidatepage(struct page *page, unsigned long offset)
ac27a0ec 3470{
617ba13b 3471 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
ac27a0ec 3472
744692dc
JZ
3473 /*
3474 * free any io_end structure allocated for buffers to be discarded
3475 */
3476 if (ext4_should_dioread_nolock(page->mapping->host))
3477 ext4_invalidatepage_free_endio(page, offset);
ac27a0ec
DK
3478 /*
3479 * If it's a full truncate we just forget about the pending dirtying
3480 */
3481 if (offset == 0)
3482 ClearPageChecked(page);
3483
0390131b
FM
3484 if (journal)
3485 jbd2_journal_invalidatepage(journal, page, offset);
3486 else
3487 block_invalidatepage(page, offset);
ac27a0ec
DK
3488}
3489
617ba13b 3490static int ext4_releasepage(struct page *page, gfp_t wait)
ac27a0ec 3491{
617ba13b 3492 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
ac27a0ec
DK
3493
3494 WARN_ON(PageChecked(page));
3495 if (!page_has_buffers(page))
3496 return 0;
0390131b
FM
3497 if (journal)
3498 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3499 else
3500 return try_to_free_buffers(page);
ac27a0ec
DK
3501}
3502
3503/*
4c0425ff
MC
3504 * O_DIRECT for ext3 (or indirect map) based files
3505 *
ac27a0ec
DK
3506 * If the O_DIRECT write will extend the file then add this inode to the
3507 * orphan list. So recovery will truncate it back to the original size
3508 * if the machine crashes during the write.
3509 *
3510 * If the O_DIRECT write is intantiating holes inside i_size and the machine
7fb5409d
JK
3511 * crashes then stale disk data _may_ be exposed inside the file. But current
3512 * VFS code falls back into buffered path in that case so we are safe.
ac27a0ec 3513 */
4c0425ff 3514static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
de9a55b8
TT
3515 const struct iovec *iov, loff_t offset,
3516 unsigned long nr_segs)
ac27a0ec
DK
3517{
3518 struct file *file = iocb->ki_filp;
3519 struct inode *inode = file->f_mapping->host;
617ba13b 3520 struct ext4_inode_info *ei = EXT4_I(inode);
7fb5409d 3521 handle_t *handle;
ac27a0ec
DK
3522 ssize_t ret;
3523 int orphan = 0;
3524 size_t count = iov_length(iov, nr_segs);
fbbf6945 3525 int retries = 0;
ac27a0ec
DK
3526
3527 if (rw == WRITE) {
3528 loff_t final_size = offset + count;
3529
ac27a0ec 3530 if (final_size > inode->i_size) {
7fb5409d
JK
3531 /* Credits for sb + inode write */
3532 handle = ext4_journal_start(inode, 2);
3533 if (IS_ERR(handle)) {
3534 ret = PTR_ERR(handle);
3535 goto out;
3536 }
617ba13b 3537 ret = ext4_orphan_add(handle, inode);
7fb5409d
JK
3538 if (ret) {
3539 ext4_journal_stop(handle);
3540 goto out;
3541 }
ac27a0ec
DK
3542 orphan = 1;
3543 ei->i_disksize = inode->i_size;
7fb5409d 3544 ext4_journal_stop(handle);
ac27a0ec
DK
3545 }
3546 }
3547
fbbf6945 3548retry:
b7adc1f3 3549 if (rw == READ && ext4_should_dioread_nolock(inode))
eafdc7d1 3550 ret = __blockdev_direct_IO(rw, iocb, inode,
b7adc1f3
JZ
3551 inode->i_sb->s_bdev, iov,
3552 offset, nr_segs,
eafdc7d1
CH
3553 ext4_get_block, NULL, NULL, 0);
3554 else {
b7adc1f3
JZ
3555 ret = blockdev_direct_IO(rw, iocb, inode,
3556 inode->i_sb->s_bdev, iov,
ac27a0ec 3557 offset, nr_segs,
617ba13b 3558 ext4_get_block, NULL);
eafdc7d1
CH
3559
3560 if (unlikely((rw & WRITE) && ret < 0)) {
3561 loff_t isize = i_size_read(inode);
3562 loff_t end = offset + iov_length(iov, nr_segs);
3563
3564 if (end > isize)
3565 vmtruncate(inode, isize);
3566 }
3567 }
fbbf6945
ES
3568 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3569 goto retry;
ac27a0ec 3570
7fb5409d 3571 if (orphan) {
ac27a0ec
DK
3572 int err;
3573
7fb5409d
JK
3574 /* Credits for sb + inode write */
3575 handle = ext4_journal_start(inode, 2);
3576 if (IS_ERR(handle)) {
3577 /* This is really bad luck. We've written the data
3578 * but cannot extend i_size. Bail out and pretend
3579 * the write failed... */
3580 ret = PTR_ERR(handle);
da1dafca
DM
3581 if (inode->i_nlink)
3582 ext4_orphan_del(NULL, inode);
3583
7fb5409d
JK
3584 goto out;
3585 }
3586 if (inode->i_nlink)
617ba13b 3587 ext4_orphan_del(handle, inode);
7fb5409d 3588 if (ret > 0) {
ac27a0ec
DK
3589 loff_t end = offset + ret;
3590 if (end > inode->i_size) {
3591 ei->i_disksize = end;
3592 i_size_write(inode, end);
3593 /*
3594 * We're going to return a positive `ret'
3595 * here due to non-zero-length I/O, so there's
3596 * no way of reporting error returns from
617ba13b 3597 * ext4_mark_inode_dirty() to userspace. So
ac27a0ec
DK
3598 * ignore it.
3599 */
617ba13b 3600 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
3601 }
3602 }
617ba13b 3603 err = ext4_journal_stop(handle);
ac27a0ec
DK
3604 if (ret == 0)
3605 ret = err;
3606 }
3607out:
3608 return ret;
3609}
3610
2ed88685
TT
3611/*
3612 * ext4_get_block used when preparing for a DIO write or buffer write.
3613 * We allocate an uinitialized extent if blocks haven't been allocated.
3614 * The extent will be converted to initialized after the IO is complete.
3615 */
c7064ef1 3616static int ext4_get_block_write(struct inode *inode, sector_t iblock,
4c0425ff
MC
3617 struct buffer_head *bh_result, int create)
3618{
c7064ef1 3619 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
8d5d02e6 3620 inode->i_ino, create);
2ed88685
TT
3621 return _ext4_get_block(inode, iblock, bh_result,
3622 EXT4_GET_BLOCKS_IO_CREATE_EXT);
4c0425ff
MC
3623}
3624
c7064ef1 3625static void dump_completed_IO(struct inode * inode)
8d5d02e6
MC
3626{
3627#ifdef EXT4_DEBUG
3628 struct list_head *cur, *before, *after;
3629 ext4_io_end_t *io, *io0, *io1;
744692dc 3630 unsigned long flags;
8d5d02e6 3631
c7064ef1
JZ
3632 if (list_empty(&EXT4_I(inode)->i_completed_io_list)){
3633 ext4_debug("inode %lu completed_io list is empty\n", inode->i_ino);
8d5d02e6
MC
3634 return;
3635 }
3636
c7064ef1 3637 ext4_debug("Dump inode %lu completed_io list \n", inode->i_ino);
744692dc 3638 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
c7064ef1 3639 list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list){
8d5d02e6
MC
3640 cur = &io->list;
3641 before = cur->prev;
3642 io0 = container_of(before, ext4_io_end_t, list);
3643 after = cur->next;
3644 io1 = container_of(after, ext4_io_end_t, list);
3645
3646 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
3647 io, inode->i_ino, io0, io1);
3648 }
744692dc 3649 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
8d5d02e6
MC
3650#endif
3651}
4c0425ff
MC
3652
3653/*
4c0425ff
MC
3654 * check a range of space and convert unwritten extents to written.
3655 */
c7064ef1 3656static int ext4_end_io_nolock(ext4_io_end_t *io)
4c0425ff 3657{
4c0425ff
MC
3658 struct inode *inode = io->inode;
3659 loff_t offset = io->offset;
a1de02dc 3660 ssize_t size = io->size;
4c0425ff 3661 int ret = 0;
4c0425ff 3662
c7064ef1 3663 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
8d5d02e6
MC
3664 "list->prev 0x%p\n",
3665 io, inode->i_ino, io->list.next, io->list.prev);
3666
3667 if (list_empty(&io->list))
3668 return ret;
3669
c7064ef1 3670 if (io->flag != EXT4_IO_UNWRITTEN)
8d5d02e6
MC
3671 return ret;
3672
744692dc 3673 ret = ext4_convert_unwritten_extents(inode, offset, size);
8d5d02e6 3674 if (ret < 0) {
4c0425ff 3675 printk(KERN_EMERG "%s: failed to convert unwritten"
8d5d02e6
MC
3676 "extents to written extents, error is %d"
3677 " io is still on inode %lu aio dio list\n",
3678 __func__, ret, inode->i_ino);
3679 return ret;
3680 }
4c0425ff 3681
8d5d02e6
MC
3682 /* clear the DIO AIO unwritten flag */
3683 io->flag = 0;
3684 return ret;
4c0425ff 3685}
c7064ef1 3686
8d5d02e6
MC
3687/*
3688 * work on completed aio dio IO, to convert unwritten extents to extents
3689 */
c7064ef1 3690static void ext4_end_io_work(struct work_struct *work)
8d5d02e6 3691{
744692dc
JZ
3692 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
3693 struct inode *inode = io->inode;
3694 struct ext4_inode_info *ei = EXT4_I(inode);
3695 unsigned long flags;
3696 int ret;
4c0425ff 3697
8d5d02e6 3698 mutex_lock(&inode->i_mutex);
c7064ef1 3699 ret = ext4_end_io_nolock(io);
744692dc
JZ
3700 if (ret < 0) {
3701 mutex_unlock(&inode->i_mutex);
3702 return;
8d5d02e6 3703 }
744692dc
JZ
3704
3705 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
3706 if (!list_empty(&io->list))
3707 list_del_init(&io->list);
3708 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
8d5d02e6 3709 mutex_unlock(&inode->i_mutex);
744692dc 3710 ext4_free_io_end(io);
8d5d02e6 3711}
c7064ef1 3712
8d5d02e6
MC
3713/*
3714 * This function is called from ext4_sync_file().
3715 *
c7064ef1
JZ
3716 * When IO is completed, the work to convert unwritten extents to
3717 * written is queued on workqueue but may not get immediately
8d5d02e6
MC
3718 * scheduled. When fsync is called, we need to ensure the
3719 * conversion is complete before fsync returns.
c7064ef1
JZ
3720 * The inode keeps track of a list of pending/completed IO that
3721 * might needs to do the conversion. This function walks through
3722 * the list and convert the related unwritten extents for completed IO
3723 * to written.
3724 * The function return the number of pending IOs on success.
8d5d02e6 3725 */
c7064ef1 3726int flush_completed_IO(struct inode *inode)
8d5d02e6
MC
3727{
3728 ext4_io_end_t *io;
744692dc
JZ
3729 struct ext4_inode_info *ei = EXT4_I(inode);
3730 unsigned long flags;
8d5d02e6
MC
3731 int ret = 0;
3732 int ret2 = 0;
3733
744692dc 3734 if (list_empty(&ei->i_completed_io_list))
8d5d02e6
MC
3735 return ret;
3736
c7064ef1 3737 dump_completed_IO(inode);
744692dc
JZ
3738 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
3739 while (!list_empty(&ei->i_completed_io_list)){
3740 io = list_entry(ei->i_completed_io_list.next,
8d5d02e6
MC
3741 ext4_io_end_t, list);
3742 /*
c7064ef1 3743 * Calling ext4_end_io_nolock() to convert completed
8d5d02e6
MC
3744 * IO to written.
3745 *
3746 * When ext4_sync_file() is called, run_queue() may already
3747 * about to flush the work corresponding to this io structure.
3748 * It will be upset if it founds the io structure related
3749 * to the work-to-be schedule is freed.
3750 *
3751 * Thus we need to keep the io structure still valid here after
3752 * convertion finished. The io structure has a flag to
3753 * avoid double converting from both fsync and background work
3754 * queue work.
3755 */
744692dc 3756 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
c7064ef1 3757 ret = ext4_end_io_nolock(io);
744692dc 3758 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
8d5d02e6
MC
3759 if (ret < 0)
3760 ret2 = ret;
3761 else
3762 list_del_init(&io->list);
3763 }
744692dc 3764 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
8d5d02e6
MC
3765 return (ret2 < 0) ? ret2 : 0;
3766}
3767
744692dc 3768static ext4_io_end_t *ext4_init_io_end (struct inode *inode, gfp_t flags)
4c0425ff
MC
3769{
3770 ext4_io_end_t *io = NULL;
3771
744692dc 3772 io = kmalloc(sizeof(*io), flags);
4c0425ff
MC
3773
3774 if (io) {
8d5d02e6 3775 igrab(inode);
4c0425ff 3776 io->inode = inode;
8d5d02e6 3777 io->flag = 0;
4c0425ff
MC
3778 io->offset = 0;
3779 io->size = 0;
744692dc 3780 io->page = NULL;
c7064ef1 3781 INIT_WORK(&io->work, ext4_end_io_work);
8d5d02e6 3782 INIT_LIST_HEAD(&io->list);
4c0425ff
MC
3783 }
3784
3785 return io;
3786}
3787
3788static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
40e2e973
CH
3789 ssize_t size, void *private, int ret,
3790 bool is_async)
4c0425ff
MC
3791{
3792 ext4_io_end_t *io_end = iocb->private;
3793 struct workqueue_struct *wq;
744692dc
JZ
3794 unsigned long flags;
3795 struct ext4_inode_info *ei;
4c0425ff 3796
4b70df18
M
3797 /* if not async direct IO or dio with 0 bytes write, just return */
3798 if (!io_end || !size)
40e2e973 3799 goto out;
4b70df18 3800
8d5d02e6
MC
3801 ext_debug("ext4_end_io_dio(): io_end 0x%p"
3802 "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
3803 iocb->private, io_end->inode->i_ino, iocb, offset,
3804 size);
8d5d02e6
MC
3805
3806 /* if not aio dio with unwritten extents, just free io and return */
c7064ef1 3807 if (io_end->flag != EXT4_IO_UNWRITTEN){
8d5d02e6
MC
3808 ext4_free_io_end(io_end);
3809 iocb->private = NULL;
40e2e973 3810 goto out;
8d5d02e6
MC
3811 }
3812
4c0425ff
MC
3813 io_end->offset = offset;
3814 io_end->size = size;
744692dc 3815 io_end->flag = EXT4_IO_UNWRITTEN;
4c0425ff
MC
3816 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
3817
8d5d02e6 3818 /* queue the work to convert unwritten extents to written */
4c0425ff
MC
3819 queue_work(wq, &io_end->work);
3820
8d5d02e6 3821 /* Add the io_end to per-inode completed aio dio list*/
744692dc
JZ
3822 ei = EXT4_I(io_end->inode);
3823 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
3824 list_add_tail(&io_end->list, &ei->i_completed_io_list);
3825 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
4c0425ff 3826 iocb->private = NULL;
40e2e973
CH
3827out:
3828 if (is_async)
3829 aio_complete(iocb, ret, 0);
4c0425ff 3830}
c7064ef1 3831
744692dc
JZ
3832static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate)
3833{
3834 ext4_io_end_t *io_end = bh->b_private;
3835 struct workqueue_struct *wq;
3836 struct inode *inode;
3837 unsigned long flags;
3838
3839 if (!test_clear_buffer_uninit(bh) || !io_end)
3840 goto out;
3841
3842 if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) {
3843 printk("sb umounted, discard end_io request for inode %lu\n",
3844 io_end->inode->i_ino);
3845 ext4_free_io_end(io_end);
3846 goto out;
3847 }
3848
3849 io_end->flag = EXT4_IO_UNWRITTEN;
3850 inode = io_end->inode;
3851
3852 /* Add the io_end to per-inode completed io list*/
3853 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
3854 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
3855 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
3856
3857 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
3858 /* queue the work to convert unwritten extents to written */
3859 queue_work(wq, &io_end->work);
3860out:
3861 bh->b_private = NULL;
3862 bh->b_end_io = NULL;
3863 clear_buffer_uninit(bh);
3864 end_buffer_async_write(bh, uptodate);
3865}
3866
3867static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode)
3868{
3869 ext4_io_end_t *io_end;
3870 struct page *page = bh->b_page;
3871 loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT;
3872 size_t size = bh->b_size;
3873
3874retry:
3875 io_end = ext4_init_io_end(inode, GFP_ATOMIC);
3876 if (!io_end) {
3877 if (printk_ratelimit())
3878 printk(KERN_WARNING "%s: allocation fail\n", __func__);
3879 schedule();
3880 goto retry;
3881 }
3882 io_end->offset = offset;
3883 io_end->size = size;
3884 /*
3885 * We need to hold a reference to the page to make sure it
3886 * doesn't get evicted before ext4_end_io_work() has a chance
3887 * to convert the extent from written to unwritten.
3888 */
3889 io_end->page = page;
3890 get_page(io_end->page);
3891
3892 bh->b_private = io_end;
3893 bh->b_end_io = ext4_end_io_buffer_write;
3894 return 0;
3895}
3896
4c0425ff
MC
3897/*
3898 * For ext4 extent files, ext4 will do direct-io write to holes,
3899 * preallocated extents, and those write extend the file, no need to
3900 * fall back to buffered IO.
3901 *
3902 * For holes, we fallocate those blocks, mark them as unintialized
3903 * If those blocks were preallocated, we mark sure they are splited, but
3904 * still keep the range to write as unintialized.
3905 *
8d5d02e6
MC
3906 * The unwrritten extents will be converted to written when DIO is completed.
3907 * For async direct IO, since the IO may still pending when return, we
3908 * set up an end_io call back function, which will do the convertion
3909 * when async direct IO completed.
4c0425ff
MC
3910 *
3911 * If the O_DIRECT write will extend the file then add this inode to the
3912 * orphan list. So recovery will truncate it back to the original size
3913 * if the machine crashes during the write.
3914 *
3915 */
3916static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3917 const struct iovec *iov, loff_t offset,
3918 unsigned long nr_segs)
3919{
3920 struct file *file = iocb->ki_filp;
3921 struct inode *inode = file->f_mapping->host;
3922 ssize_t ret;
3923 size_t count = iov_length(iov, nr_segs);
3924
3925 loff_t final_size = offset + count;
3926 if (rw == WRITE && final_size <= inode->i_size) {
3927 /*
8d5d02e6
MC
3928 * We could direct write to holes and fallocate.
3929 *
3930 * Allocated blocks to fill the hole are marked as uninitialized
4c0425ff
MC
3931 * to prevent paralel buffered read to expose the stale data
3932 * before DIO complete the data IO.
8d5d02e6
MC
3933 *
3934 * As to previously fallocated extents, ext4 get_block
4c0425ff
MC
3935 * will just simply mark the buffer mapped but still
3936 * keep the extents uninitialized.
3937 *
8d5d02e6
MC
3938 * for non AIO case, we will convert those unwritten extents
3939 * to written after return back from blockdev_direct_IO.
3940 *
3941 * for async DIO, the conversion needs to be defered when
3942 * the IO is completed. The ext4 end_io callback function
3943 * will be called to take care of the conversion work.
3944 * Here for async case, we allocate an io_end structure to
3945 * hook to the iocb.
4c0425ff 3946 */
8d5d02e6
MC
3947 iocb->private = NULL;
3948 EXT4_I(inode)->cur_aio_dio = NULL;
3949 if (!is_sync_kiocb(iocb)) {
744692dc 3950 iocb->private = ext4_init_io_end(inode, GFP_NOFS);
8d5d02e6
MC
3951 if (!iocb->private)
3952 return -ENOMEM;
3953 /*
3954 * we save the io structure for current async
3955 * direct IO, so that later ext4_get_blocks()
3956 * could flag the io structure whether there
3957 * is a unwritten extents needs to be converted
3958 * when IO is completed.
3959 */
3960 EXT4_I(inode)->cur_aio_dio = iocb->private;
3961 }
3962
4c0425ff
MC
3963 ret = blockdev_direct_IO(rw, iocb, inode,
3964 inode->i_sb->s_bdev, iov,
3965 offset, nr_segs,
c7064ef1 3966 ext4_get_block_write,
4c0425ff 3967 ext4_end_io_dio);
8d5d02e6
MC
3968 if (iocb->private)
3969 EXT4_I(inode)->cur_aio_dio = NULL;
3970 /*
3971 * The io_end structure takes a reference to the inode,
3972 * that structure needs to be destroyed and the
3973 * reference to the inode need to be dropped, when IO is
3974 * complete, even with 0 byte write, or failed.
3975 *
3976 * In the successful AIO DIO case, the io_end structure will be
3977 * desctroyed and the reference to the inode will be dropped
3978 * after the end_io call back function is called.
3979 *
3980 * In the case there is 0 byte write, or error case, since
3981 * VFS direct IO won't invoke the end_io call back function,
3982 * we need to free the end_io structure here.
3983 */
3984 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3985 ext4_free_io_end(iocb->private);
3986 iocb->private = NULL;
19f5fb7a
TT
3987 } else if (ret > 0 && ext4_test_inode_state(inode,
3988 EXT4_STATE_DIO_UNWRITTEN)) {
109f5565 3989 int err;
8d5d02e6
MC
3990 /*
3991 * for non AIO case, since the IO is already
3992 * completed, we could do the convertion right here
3993 */
109f5565
M
3994 err = ext4_convert_unwritten_extents(inode,
3995 offset, ret);
3996 if (err < 0)
3997 ret = err;
19f5fb7a 3998 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
109f5565 3999 }
4c0425ff
MC
4000 return ret;
4001 }
8d5d02e6
MC
4002
4003 /* for write the the end of file case, we fall back to old way */
4c0425ff
MC
4004 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
4005}
4006
4007static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
4008 const struct iovec *iov, loff_t offset,
4009 unsigned long nr_segs)
4010{
4011 struct file *file = iocb->ki_filp;
4012 struct inode *inode = file->f_mapping->host;
4013
12e9b892 4014 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4c0425ff
MC
4015 return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
4016
4017 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
4018}
4019
ac27a0ec 4020/*
617ba13b 4021 * Pages can be marked dirty completely asynchronously from ext4's journalling
ac27a0ec
DK
4022 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
4023 * much here because ->set_page_dirty is called under VFS locks. The page is
4024 * not necessarily locked.
4025 *
4026 * We cannot just dirty the page and leave attached buffers clean, because the
4027 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
4028 * or jbddirty because all the journalling code will explode.
4029 *
4030 * So what we do is to mark the page "pending dirty" and next time writepage
4031 * is called, propagate that into the buffers appropriately.
4032 */
617ba13b 4033static int ext4_journalled_set_page_dirty(struct page *page)
ac27a0ec
DK
4034{
4035 SetPageChecked(page);
4036 return __set_page_dirty_nobuffers(page);
4037}
4038
617ba13b 4039static const struct address_space_operations ext4_ordered_aops = {
8ab22b9a
HH
4040 .readpage = ext4_readpage,
4041 .readpages = ext4_readpages,
43ce1d23 4042 .writepage = ext4_writepage,
8ab22b9a
HH
4043 .sync_page = block_sync_page,
4044 .write_begin = ext4_write_begin,
4045 .write_end = ext4_ordered_write_end,
4046 .bmap = ext4_bmap,
4047 .invalidatepage = ext4_invalidatepage,
4048 .releasepage = ext4_releasepage,
4049 .direct_IO = ext4_direct_IO,
4050 .migratepage = buffer_migrate_page,
4051 .is_partially_uptodate = block_is_partially_uptodate,
aa261f54 4052 .error_remove_page = generic_error_remove_page,
ac27a0ec
DK
4053};
4054
617ba13b 4055static const struct address_space_operations ext4_writeback_aops = {
8ab22b9a
HH
4056 .readpage = ext4_readpage,
4057 .readpages = ext4_readpages,
43ce1d23 4058 .writepage = ext4_writepage,
8ab22b9a
HH
4059 .sync_page = block_sync_page,
4060 .write_begin = ext4_write_begin,
4061 .write_end = ext4_writeback_write_end,
4062 .bmap = ext4_bmap,
4063 .invalidatepage = ext4_invalidatepage,
4064 .releasepage = ext4_releasepage,
4065 .direct_IO = ext4_direct_IO,
4066 .migratepage = buffer_migrate_page,
4067 .is_partially_uptodate = block_is_partially_uptodate,
aa261f54 4068 .error_remove_page = generic_error_remove_page,
ac27a0ec
DK
4069};
4070
617ba13b 4071static const struct address_space_operations ext4_journalled_aops = {
8ab22b9a
HH
4072 .readpage = ext4_readpage,
4073 .readpages = ext4_readpages,
43ce1d23 4074 .writepage = ext4_writepage,
8ab22b9a
HH
4075 .sync_page = block_sync_page,
4076 .write_begin = ext4_write_begin,
4077 .write_end = ext4_journalled_write_end,
4078 .set_page_dirty = ext4_journalled_set_page_dirty,
4079 .bmap = ext4_bmap,
4080 .invalidatepage = ext4_invalidatepage,
4081 .releasepage = ext4_releasepage,
4082 .is_partially_uptodate = block_is_partially_uptodate,
aa261f54 4083 .error_remove_page = generic_error_remove_page,
ac27a0ec
DK
4084};
4085
64769240 4086static const struct address_space_operations ext4_da_aops = {
8ab22b9a
HH
4087 .readpage = ext4_readpage,
4088 .readpages = ext4_readpages,
43ce1d23 4089 .writepage = ext4_writepage,
8ab22b9a
HH
4090 .writepages = ext4_da_writepages,
4091 .sync_page = block_sync_page,
4092 .write_begin = ext4_da_write_begin,
4093 .write_end = ext4_da_write_end,
4094 .bmap = ext4_bmap,
4095 .invalidatepage = ext4_da_invalidatepage,
4096 .releasepage = ext4_releasepage,
4097 .direct_IO = ext4_direct_IO,
4098 .migratepage = buffer_migrate_page,
4099 .is_partially_uptodate = block_is_partially_uptodate,
aa261f54 4100 .error_remove_page = generic_error_remove_page,
64769240
AT
4101};
4102
617ba13b 4103void ext4_set_aops(struct inode *inode)
ac27a0ec 4104{
cd1aac32
AK
4105 if (ext4_should_order_data(inode) &&
4106 test_opt(inode->i_sb, DELALLOC))
4107 inode->i_mapping->a_ops = &ext4_da_aops;
4108 else if (ext4_should_order_data(inode))
617ba13b 4109 inode->i_mapping->a_ops = &ext4_ordered_aops;
64769240
AT
4110 else if (ext4_should_writeback_data(inode) &&
4111 test_opt(inode->i_sb, DELALLOC))
4112 inode->i_mapping->a_ops = &ext4_da_aops;
617ba13b
MC
4113 else if (ext4_should_writeback_data(inode))
4114 inode->i_mapping->a_ops = &ext4_writeback_aops;
ac27a0ec 4115 else
617ba13b 4116 inode->i_mapping->a_ops = &ext4_journalled_aops;
ac27a0ec
DK
4117}
4118
4119/*
617ba13b 4120 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
ac27a0ec
DK
4121 * up to the end of the block which corresponds to `from'.
4122 * This required during truncate. We need to physically zero the tail end
4123 * of that block so it doesn't yield old data if the file is later grown.
4124 */
cf108bca 4125int ext4_block_truncate_page(handle_t *handle,
ac27a0ec
DK
4126 struct address_space *mapping, loff_t from)
4127{
617ba13b 4128 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
ac27a0ec 4129 unsigned offset = from & (PAGE_CACHE_SIZE-1);
725d26d3
AK
4130 unsigned blocksize, length, pos;
4131 ext4_lblk_t iblock;
ac27a0ec
DK
4132 struct inode *inode = mapping->host;
4133 struct buffer_head *bh;
cf108bca 4134 struct page *page;
ac27a0ec 4135 int err = 0;
ac27a0ec 4136
f4a01017
TT
4137 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
4138 mapping_gfp_mask(mapping) & ~__GFP_FS);
cf108bca
JK
4139 if (!page)
4140 return -EINVAL;
4141
ac27a0ec
DK
4142 blocksize = inode->i_sb->s_blocksize;
4143 length = blocksize - (offset & (blocksize - 1));
4144 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
4145
4146 /*
4147 * For "nobh" option, we can only work if we don't need to
4148 * read-in the page - otherwise we create buffers to do the IO.
4149 */
4150 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
617ba13b 4151 ext4_should_writeback_data(inode) && PageUptodate(page)) {
eebd2aa3 4152 zero_user(page, offset, length);
ac27a0ec
DK
4153 set_page_dirty(page);
4154 goto unlock;
4155 }
4156
4157 if (!page_has_buffers(page))
4158 create_empty_buffers(page, blocksize, 0);
4159
4160 /* Find the buffer that contains "offset" */
4161 bh = page_buffers(page);
4162 pos = blocksize;
4163 while (offset >= pos) {
4164 bh = bh->b_this_page;
4165 iblock++;
4166 pos += blocksize;
4167 }
4168
4169 err = 0;
4170 if (buffer_freed(bh)) {
4171 BUFFER_TRACE(bh, "freed: skip");
4172 goto unlock;
4173 }
4174
4175 if (!buffer_mapped(bh)) {
4176 BUFFER_TRACE(bh, "unmapped");
617ba13b 4177 ext4_get_block(inode, iblock, bh, 0);
ac27a0ec
DK
4178 /* unmapped? It's a hole - nothing to do */
4179 if (!buffer_mapped(bh)) {
4180 BUFFER_TRACE(bh, "still unmapped");
4181 goto unlock;
4182 }
4183 }
4184
4185 /* Ok, it's mapped. Make sure it's up-to-date */
4186 if (PageUptodate(page))
4187 set_buffer_uptodate(bh);
4188
4189 if (!buffer_uptodate(bh)) {
4190 err = -EIO;
4191 ll_rw_block(READ, 1, &bh);
4192 wait_on_buffer(bh);
4193 /* Uhhuh. Read error. Complain and punt. */
4194 if (!buffer_uptodate(bh))
4195 goto unlock;
4196 }
4197
617ba13b 4198 if (ext4_should_journal_data(inode)) {
ac27a0ec 4199 BUFFER_TRACE(bh, "get write access");
617ba13b 4200 err = ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
4201 if (err)
4202 goto unlock;
4203 }
4204
eebd2aa3 4205 zero_user(page, offset, length);
ac27a0ec
DK
4206
4207 BUFFER_TRACE(bh, "zeroed end of block");
4208
4209 err = 0;
617ba13b 4210 if (ext4_should_journal_data(inode)) {
0390131b 4211 err = ext4_handle_dirty_metadata(handle, inode, bh);
ac27a0ec 4212 } else {
617ba13b 4213 if (ext4_should_order_data(inode))
678aaf48 4214 err = ext4_jbd2_file_inode(handle, inode);
ac27a0ec
DK
4215 mark_buffer_dirty(bh);
4216 }
4217
4218unlock:
4219 unlock_page(page);
4220 page_cache_release(page);
4221 return err;
4222}
4223
4224/*
4225 * Probably it should be a library function... search for first non-zero word
4226 * or memcmp with zero_page, whatever is better for particular architecture.
4227 * Linus?
4228 */
4229static inline int all_zeroes(__le32 *p, __le32 *q)
4230{
4231 while (p < q)
4232 if (*p++)
4233 return 0;
4234 return 1;
4235}
4236
4237/**
617ba13b 4238 * ext4_find_shared - find the indirect blocks for partial truncation.
ac27a0ec
DK
4239 * @inode: inode in question
4240 * @depth: depth of the affected branch
617ba13b 4241 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
ac27a0ec
DK
4242 * @chain: place to store the pointers to partial indirect blocks
4243 * @top: place to the (detached) top of branch
4244 *
617ba13b 4245 * This is a helper function used by ext4_truncate().
ac27a0ec
DK
4246 *
4247 * When we do truncate() we may have to clean the ends of several
4248 * indirect blocks but leave the blocks themselves alive. Block is
4249 * partially truncated if some data below the new i_size is refered
4250 * from it (and it is on the path to the first completely truncated
4251 * data block, indeed). We have to free the top of that path along
4252 * with everything to the right of the path. Since no allocation
617ba13b 4253 * past the truncation point is possible until ext4_truncate()
ac27a0ec
DK
4254 * finishes, we may safely do the latter, but top of branch may
4255 * require special attention - pageout below the truncation point
4256 * might try to populate it.
4257 *
4258 * We atomically detach the top of branch from the tree, store the
4259 * block number of its root in *@top, pointers to buffer_heads of
4260 * partially truncated blocks - in @chain[].bh and pointers to
4261 * their last elements that should not be removed - in
4262 * @chain[].p. Return value is the pointer to last filled element
4263 * of @chain.
4264 *
4265 * The work left to caller to do the actual freeing of subtrees:
4266 * a) free the subtree starting from *@top
4267 * b) free the subtrees whose roots are stored in
4268 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
4269 * c) free the subtrees growing from the inode past the @chain[0].
4270 * (no partially truncated stuff there). */
4271
617ba13b 4272static Indirect *ext4_find_shared(struct inode *inode, int depth,
de9a55b8
TT
4273 ext4_lblk_t offsets[4], Indirect chain[4],
4274 __le32 *top)
ac27a0ec
DK
4275{
4276 Indirect *partial, *p;
4277 int k, err;
4278
4279 *top = 0;
bf48aabb 4280 /* Make k index the deepest non-null offset + 1 */
ac27a0ec
DK
4281 for (k = depth; k > 1 && !offsets[k-1]; k--)
4282 ;
617ba13b 4283 partial = ext4_get_branch(inode, k, offsets, chain, &err);
ac27a0ec
DK
4284 /* Writer: pointers */
4285 if (!partial)
4286 partial = chain + k-1;
4287 /*
4288 * If the branch acquired continuation since we've looked at it -
4289 * fine, it should all survive and (new) top doesn't belong to us.
4290 */
4291 if (!partial->key && *partial->p)
4292 /* Writer: end */
4293 goto no_top;
af5bc92d 4294 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
ac27a0ec
DK
4295 ;
4296 /*
4297 * OK, we've found the last block that must survive. The rest of our
4298 * branch should be detached before unlocking. However, if that rest
4299 * of branch is all ours and does not grow immediately from the inode
4300 * it's easier to cheat and just decrement partial->p.
4301 */
4302 if (p == chain + k - 1 && p > chain) {
4303 p->p--;
4304 } else {
4305 *top = *p->p;
617ba13b 4306 /* Nope, don't do this in ext4. Must leave the tree intact */
ac27a0ec
DK
4307#if 0
4308 *p->p = 0;
4309#endif
4310 }
4311 /* Writer: end */
4312
af5bc92d 4313 while (partial > p) {
ac27a0ec
DK
4314 brelse(partial->bh);
4315 partial--;
4316 }
4317no_top:
4318 return partial;
4319}
4320
4321/*
4322 * Zero a number of block pointers in either an inode or an indirect block.
4323 * If we restart the transaction we must again get write access to the
4324 * indirect block for further modification.
4325 *
4326 * We release `count' blocks on disk, but (last - first) may be greater
4327 * than `count' because there can be holes in there.
4328 */
1f2acb60
TT
4329static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
4330 struct buffer_head *bh,
4331 ext4_fsblk_t block_to_free,
4332 unsigned long count, __le32 *first,
4333 __le32 *last)
ac27a0ec
DK
4334{
4335 __le32 *p;
1f2acb60 4336 int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED;
e6362609
TT
4337
4338 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4339 flags |= EXT4_FREE_BLOCKS_METADATA;
50689696 4340
1f2acb60
TT
4341 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
4342 count)) {
24676da4
TT
4343 EXT4_ERROR_INODE(inode, "attempt to clear invalid "
4344 "blocks %llu len %lu",
4345 (unsigned long long) block_to_free, count);
1f2acb60
TT
4346 return 1;
4347 }
4348
ac27a0ec
DK
4349 if (try_to_extend_transaction(handle, inode)) {
4350 if (bh) {
0390131b
FM
4351 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4352 ext4_handle_dirty_metadata(handle, inode, bh);
ac27a0ec 4353 }
617ba13b 4354 ext4_mark_inode_dirty(handle, inode);
487caeef
JK
4355 ext4_truncate_restart_trans(handle, inode,
4356 blocks_for_truncate(inode));
ac27a0ec
DK
4357 if (bh) {
4358 BUFFER_TRACE(bh, "retaking write access");
617ba13b 4359 ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
4360 }
4361 }
4362
e6362609
TT
4363 for (p = first; p < last; p++)
4364 *p = 0;
ac27a0ec 4365
e6362609 4366 ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
1f2acb60 4367 return 0;
ac27a0ec
DK
4368}
4369
4370/**
617ba13b 4371 * ext4_free_data - free a list of data blocks
ac27a0ec
DK
4372 * @handle: handle for this transaction
4373 * @inode: inode we are dealing with
4374 * @this_bh: indirect buffer_head which contains *@first and *@last
4375 * @first: array of block numbers
4376 * @last: points immediately past the end of array
4377 *
4378 * We are freeing all blocks refered from that array (numbers are stored as
4379 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
4380 *
4381 * We accumulate contiguous runs of blocks to free. Conveniently, if these
4382 * blocks are contiguous then releasing them at one time will only affect one
4383 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
4384 * actually use a lot of journal space.
4385 *
4386 * @this_bh will be %NULL if @first and @last point into the inode's direct
4387 * block pointers.
4388 */
617ba13b 4389static void ext4_free_data(handle_t *handle, struct inode *inode,
ac27a0ec
DK
4390 struct buffer_head *this_bh,
4391 __le32 *first, __le32 *last)
4392{
617ba13b 4393 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
ac27a0ec
DK
4394 unsigned long count = 0; /* Number of blocks in the run */
4395 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
4396 corresponding to
4397 block_to_free */
617ba13b 4398 ext4_fsblk_t nr; /* Current block # */
ac27a0ec
DK
4399 __le32 *p; /* Pointer into inode/ind
4400 for current block */
4401 int err;
4402
4403 if (this_bh) { /* For indirect block */
4404 BUFFER_TRACE(this_bh, "get_write_access");
617ba13b 4405 err = ext4_journal_get_write_access(handle, this_bh);
ac27a0ec
DK
4406 /* Important: if we can't update the indirect pointers
4407 * to the blocks, we can't free them. */
4408 if (err)
4409 return;
4410 }
4411
4412 for (p = first; p < last; p++) {
4413 nr = le32_to_cpu(*p);
4414 if (nr) {
4415 /* accumulate blocks to free if they're contiguous */
4416 if (count == 0) {
4417 block_to_free = nr;
4418 block_to_free_p = p;
4419 count = 1;
4420 } else if (nr == block_to_free + count) {
4421 count++;
4422 } else {
1f2acb60
TT
4423 if (ext4_clear_blocks(handle, inode, this_bh,
4424 block_to_free, count,
4425 block_to_free_p, p))
4426 break;
ac27a0ec
DK
4427 block_to_free = nr;
4428 block_to_free_p = p;
4429 count = 1;
4430 }
4431 }
4432 }
4433
4434 if (count > 0)
617ba13b 4435 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
ac27a0ec
DK
4436 count, block_to_free_p, p);
4437
4438 if (this_bh) {
0390131b 4439 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
71dc8fbc
DG
4440
4441 /*
4442 * The buffer head should have an attached journal head at this
4443 * point. However, if the data is corrupted and an indirect
4444 * block pointed to itself, it would have been detached when
4445 * the block was cleared. Check for this instead of OOPSing.
4446 */
e7f07968 4447 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
0390131b 4448 ext4_handle_dirty_metadata(handle, inode, this_bh);
71dc8fbc 4449 else
24676da4
TT
4450 EXT4_ERROR_INODE(inode,
4451 "circular indirect block detected at "
4452 "block %llu",
4453 (unsigned long long) this_bh->b_blocknr);
ac27a0ec
DK
4454 }
4455}
4456
4457/**
617ba13b 4458 * ext4_free_branches - free an array of branches
ac27a0ec
DK
4459 * @handle: JBD handle for this transaction
4460 * @inode: inode we are dealing with
4461 * @parent_bh: the buffer_head which contains *@first and *@last
4462 * @first: array of block numbers
4463 * @last: pointer immediately past the end of array
4464 * @depth: depth of the branches to free
4465 *
4466 * We are freeing all blocks refered from these branches (numbers are
4467 * stored as little-endian 32-bit) and updating @inode->i_blocks
4468 * appropriately.
4469 */
617ba13b 4470static void ext4_free_branches(handle_t *handle, struct inode *inode,
ac27a0ec
DK
4471 struct buffer_head *parent_bh,
4472 __le32 *first, __le32 *last, int depth)
4473{
617ba13b 4474 ext4_fsblk_t nr;
ac27a0ec
DK
4475 __le32 *p;
4476
0390131b 4477 if (ext4_handle_is_aborted(handle))
ac27a0ec
DK
4478 return;
4479
4480 if (depth--) {
4481 struct buffer_head *bh;
617ba13b 4482 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
ac27a0ec
DK
4483 p = last;
4484 while (--p >= first) {
4485 nr = le32_to_cpu(*p);
4486 if (!nr)
4487 continue; /* A hole */
4488
1f2acb60
TT
4489 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
4490 nr, 1)) {
24676da4
TT
4491 EXT4_ERROR_INODE(inode,
4492 "invalid indirect mapped "
4493 "block %lu (level %d)",
4494 (unsigned long) nr, depth);
1f2acb60
TT
4495 break;
4496 }
4497
ac27a0ec
DK
4498 /* Go read the buffer for the next level down */
4499 bh = sb_bread(inode->i_sb, nr);
4500
4501 /*
4502 * A read failure? Report error and clear slot
4503 * (should be rare).
4504 */
4505 if (!bh) {
24676da4
TT
4506 EXT4_ERROR_INODE(inode,
4507 "Read failure block=%llu",
4508 (unsigned long long) nr);
ac27a0ec
DK
4509 continue;
4510 }
4511
4512 /* This zaps the entire block. Bottom up. */
4513 BUFFER_TRACE(bh, "free child branches");
617ba13b 4514 ext4_free_branches(handle, inode, bh,
af5bc92d
TT
4515 (__le32 *) bh->b_data,
4516 (__le32 *) bh->b_data + addr_per_block,
4517 depth);
ac27a0ec
DK
4518
4519 /*
4520 * We've probably journalled the indirect block several
4521 * times during the truncate. But it's no longer
4522 * needed and we now drop it from the transaction via
dab291af 4523 * jbd2_journal_revoke().
ac27a0ec
DK
4524 *
4525 * That's easy if it's exclusively part of this
4526 * transaction. But if it's part of the committing
dab291af 4527 * transaction then jbd2_journal_forget() will simply
ac27a0ec 4528 * brelse() it. That means that if the underlying
617ba13b 4529 * block is reallocated in ext4_get_block(),
ac27a0ec
DK
4530 * unmap_underlying_metadata() will find this block
4531 * and will try to get rid of it. damn, damn.
4532 *
4533 * If this block has already been committed to the
4534 * journal, a revoke record will be written. And
4535 * revoke records must be emitted *before* clearing
4536 * this block's bit in the bitmaps.
4537 */
617ba13b 4538 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
ac27a0ec
DK
4539
4540 /*
4541 * Everything below this this pointer has been
4542 * released. Now let this top-of-subtree go.
4543 *
4544 * We want the freeing of this indirect block to be
4545 * atomic in the journal with the updating of the
4546 * bitmap block which owns it. So make some room in
4547 * the journal.
4548 *
4549 * We zero the parent pointer *after* freeing its
4550 * pointee in the bitmaps, so if extend_transaction()
4551 * for some reason fails to put the bitmap changes and
4552 * the release into the same transaction, recovery
4553 * will merely complain about releasing a free block,
4554 * rather than leaking blocks.
4555 */
0390131b 4556 if (ext4_handle_is_aborted(handle))
ac27a0ec
DK
4557 return;
4558 if (try_to_extend_transaction(handle, inode)) {
617ba13b 4559 ext4_mark_inode_dirty(handle, inode);
487caeef
JK
4560 ext4_truncate_restart_trans(handle, inode,
4561 blocks_for_truncate(inode));
ac27a0ec
DK
4562 }
4563
e6362609
TT
4564 ext4_free_blocks(handle, inode, 0, nr, 1,
4565 EXT4_FREE_BLOCKS_METADATA);
ac27a0ec
DK
4566
4567 if (parent_bh) {
4568 /*
4569 * The block which we have just freed is
4570 * pointed to by an indirect block: journal it
4571 */
4572 BUFFER_TRACE(parent_bh, "get_write_access");
617ba13b 4573 if (!ext4_journal_get_write_access(handle,
ac27a0ec
DK
4574 parent_bh)){
4575 *p = 0;
4576 BUFFER_TRACE(parent_bh,
0390131b
FM
4577 "call ext4_handle_dirty_metadata");
4578 ext4_handle_dirty_metadata(handle,
4579 inode,
4580 parent_bh);
ac27a0ec
DK
4581 }
4582 }
4583 }
4584 } else {
4585 /* We have reached the bottom of the tree. */
4586 BUFFER_TRACE(parent_bh, "free data blocks");
617ba13b 4587 ext4_free_data(handle, inode, parent_bh, first, last);
ac27a0ec
DK
4588 }
4589}
4590
91ef4caf
DG
4591int ext4_can_truncate(struct inode *inode)
4592{
4593 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4594 return 0;
4595 if (S_ISREG(inode->i_mode))
4596 return 1;
4597 if (S_ISDIR(inode->i_mode))
4598 return 1;
4599 if (S_ISLNK(inode->i_mode))
4600 return !ext4_inode_is_fast_symlink(inode);
4601 return 0;
4602}
4603
ac27a0ec 4604/*
617ba13b 4605 * ext4_truncate()
ac27a0ec 4606 *
617ba13b
MC
4607 * We block out ext4_get_block() block instantiations across the entire
4608 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
ac27a0ec
DK
4609 * simultaneously on behalf of the same inode.
4610 *
4611 * As we work through the truncate and commmit bits of it to the journal there
4612 * is one core, guiding principle: the file's tree must always be consistent on
4613 * disk. We must be able to restart the truncate after a crash.
4614 *
4615 * The file's tree may be transiently inconsistent in memory (although it
4616 * probably isn't), but whenever we close off and commit a journal transaction,
4617 * the contents of (the filesystem + the journal) must be consistent and
4618 * restartable. It's pretty simple, really: bottom up, right to left (although
4619 * left-to-right works OK too).
4620 *
4621 * Note that at recovery time, journal replay occurs *before* the restart of
4622 * truncate against the orphan inode list.
4623 *
4624 * The committed inode has the new, desired i_size (which is the same as
617ba13b 4625 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
ac27a0ec 4626 * that this inode's truncate did not complete and it will again call
617ba13b
MC
4627 * ext4_truncate() to have another go. So there will be instantiated blocks
4628 * to the right of the truncation point in a crashed ext4 filesystem. But
ac27a0ec 4629 * that's fine - as long as they are linked from the inode, the post-crash
617ba13b 4630 * ext4_truncate() run will find them and release them.
ac27a0ec 4631 */
617ba13b 4632void ext4_truncate(struct inode *inode)
ac27a0ec
DK
4633{
4634 handle_t *handle;
617ba13b 4635 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec 4636 __le32 *i_data = ei->i_data;
617ba13b 4637 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
ac27a0ec 4638 struct address_space *mapping = inode->i_mapping;
725d26d3 4639 ext4_lblk_t offsets[4];
ac27a0ec
DK
4640 Indirect chain[4];
4641 Indirect *partial;
4642 __le32 nr = 0;
4643 int n;
725d26d3 4644 ext4_lblk_t last_block;
ac27a0ec 4645 unsigned blocksize = inode->i_sb->s_blocksize;
ac27a0ec 4646
91ef4caf 4647 if (!ext4_can_truncate(inode))
ac27a0ec
DK
4648 return;
4649
12e9b892 4650 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
c8d46e41 4651
5534fb5b 4652 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
19f5fb7a 4653 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
7d8f9f7d 4654
12e9b892 4655 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
cf108bca 4656 ext4_ext_truncate(inode);
1d03ec98
AK
4657 return;
4658 }
a86c6181 4659
ac27a0ec 4660 handle = start_transaction(inode);
cf108bca 4661 if (IS_ERR(handle))
ac27a0ec 4662 return; /* AKPM: return what? */
ac27a0ec
DK
4663
4664 last_block = (inode->i_size + blocksize-1)
617ba13b 4665 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
ac27a0ec 4666
cf108bca
JK
4667 if (inode->i_size & (blocksize - 1))
4668 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
4669 goto out_stop;
ac27a0ec 4670
617ba13b 4671 n = ext4_block_to_path(inode, last_block, offsets, NULL);
ac27a0ec
DK
4672 if (n == 0)
4673 goto out_stop; /* error */
4674
4675 /*
4676 * OK. This truncate is going to happen. We add the inode to the
4677 * orphan list, so that if this truncate spans multiple transactions,
4678 * and we crash, we will resume the truncate when the filesystem
4679 * recovers. It also marks the inode dirty, to catch the new size.
4680 *
4681 * Implication: the file must always be in a sane, consistent
4682 * truncatable state while each transaction commits.
4683 */
617ba13b 4684 if (ext4_orphan_add(handle, inode))
ac27a0ec
DK
4685 goto out_stop;
4686
632eaeab
MC
4687 /*
4688 * From here we block out all ext4_get_block() callers who want to
4689 * modify the block allocation tree.
4690 */
4691 down_write(&ei->i_data_sem);
b4df2030 4692
c2ea3fde 4693 ext4_discard_preallocations(inode);
b4df2030 4694
ac27a0ec
DK
4695 /*
4696 * The orphan list entry will now protect us from any crash which
4697 * occurs before the truncate completes, so it is now safe to propagate
4698 * the new, shorter inode size (held for now in i_size) into the
4699 * on-disk inode. We do this via i_disksize, which is the value which
617ba13b 4700 * ext4 *really* writes onto the disk inode.
ac27a0ec
DK
4701 */
4702 ei->i_disksize = inode->i_size;
4703
ac27a0ec 4704 if (n == 1) { /* direct blocks */
617ba13b
MC
4705 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4706 i_data + EXT4_NDIR_BLOCKS);
ac27a0ec
DK
4707 goto do_indirects;
4708 }
4709
617ba13b 4710 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
ac27a0ec
DK
4711 /* Kill the top of shared branch (not detached) */
4712 if (nr) {
4713 if (partial == chain) {
4714 /* Shared branch grows from the inode */
617ba13b 4715 ext4_free_branches(handle, inode, NULL,
ac27a0ec
DK
4716 &nr, &nr+1, (chain+n-1) - partial);
4717 *partial->p = 0;
4718 /*
4719 * We mark the inode dirty prior to restart,
4720 * and prior to stop. No need for it here.
4721 */
4722 } else {
4723 /* Shared branch grows from an indirect block */
4724 BUFFER_TRACE(partial->bh, "get_write_access");
617ba13b 4725 ext4_free_branches(handle, inode, partial->bh,
ac27a0ec
DK
4726 partial->p,
4727 partial->p+1, (chain+n-1) - partial);
4728 }
4729 }
4730 /* Clear the ends of indirect blocks on the shared branch */
4731 while (partial > chain) {
617ba13b 4732 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
ac27a0ec
DK
4733 (__le32*)partial->bh->b_data+addr_per_block,
4734 (chain+n-1) - partial);
4735 BUFFER_TRACE(partial->bh, "call brelse");
de9a55b8 4736 brelse(partial->bh);
ac27a0ec
DK
4737 partial--;
4738 }
4739do_indirects:
4740 /* Kill the remaining (whole) subtrees */
4741 switch (offsets[0]) {
4742 default:
617ba13b 4743 nr = i_data[EXT4_IND_BLOCK];
ac27a0ec 4744 if (nr) {
617ba13b
MC
4745 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4746 i_data[EXT4_IND_BLOCK] = 0;
ac27a0ec 4747 }
617ba13b
MC
4748 case EXT4_IND_BLOCK:
4749 nr = i_data[EXT4_DIND_BLOCK];
ac27a0ec 4750 if (nr) {
617ba13b
MC
4751 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4752 i_data[EXT4_DIND_BLOCK] = 0;
ac27a0ec 4753 }
617ba13b
MC
4754 case EXT4_DIND_BLOCK:
4755 nr = i_data[EXT4_TIND_BLOCK];
ac27a0ec 4756 if (nr) {
617ba13b
MC
4757 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4758 i_data[EXT4_TIND_BLOCK] = 0;
ac27a0ec 4759 }
617ba13b 4760 case EXT4_TIND_BLOCK:
ac27a0ec
DK
4761 ;
4762 }
4763
0e855ac8 4764 up_write(&ei->i_data_sem);
ef7f3835 4765 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
617ba13b 4766 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
4767
4768 /*
4769 * In a multi-transaction truncate, we only make the final transaction
4770 * synchronous
4771 */
4772 if (IS_SYNC(inode))
0390131b 4773 ext4_handle_sync(handle);
ac27a0ec
DK
4774out_stop:
4775 /*
4776 * If this was a simple ftruncate(), and the file will remain alive
4777 * then we need to clear up the orphan record which we created above.
4778 * However, if this was a real unlink then we were called by
617ba13b 4779 * ext4_delete_inode(), and we allow that function to clean up the
ac27a0ec
DK
4780 * orphan info for us.
4781 */
4782 if (inode->i_nlink)
617ba13b 4783 ext4_orphan_del(handle, inode);
ac27a0ec 4784
617ba13b 4785 ext4_journal_stop(handle);
ac27a0ec
DK
4786}
4787
ac27a0ec 4788/*
617ba13b 4789 * ext4_get_inode_loc returns with an extra refcount against the inode's
ac27a0ec
DK
4790 * underlying buffer_head on success. If 'in_mem' is true, we have all
4791 * data in memory that is needed to recreate the on-disk version of this
4792 * inode.
4793 */
617ba13b
MC
4794static int __ext4_get_inode_loc(struct inode *inode,
4795 struct ext4_iloc *iloc, int in_mem)
ac27a0ec 4796{
240799cd
TT
4797 struct ext4_group_desc *gdp;
4798 struct buffer_head *bh;
4799 struct super_block *sb = inode->i_sb;
4800 ext4_fsblk_t block;
4801 int inodes_per_block, inode_offset;
4802
3a06d778 4803 iloc->bh = NULL;
240799cd
TT
4804 if (!ext4_valid_inum(sb, inode->i_ino))
4805 return -EIO;
ac27a0ec 4806
240799cd
TT
4807 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4808 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4809 if (!gdp)
ac27a0ec
DK
4810 return -EIO;
4811
240799cd
TT
4812 /*
4813 * Figure out the offset within the block group inode table
4814 */
4815 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4816 inode_offset = ((inode->i_ino - 1) %
4817 EXT4_INODES_PER_GROUP(sb));
4818 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4819 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4820
4821 bh = sb_getblk(sb, block);
ac27a0ec 4822 if (!bh) {
24676da4
TT
4823 EXT4_ERROR_INODE(inode, "unable to read inode block - "
4824 "block %llu", block);
ac27a0ec
DK
4825 return -EIO;
4826 }
4827 if (!buffer_uptodate(bh)) {
4828 lock_buffer(bh);
9c83a923
HK
4829
4830 /*
4831 * If the buffer has the write error flag, we have failed
4832 * to write out another inode in the same block. In this
4833 * case, we don't have to read the block because we may
4834 * read the old inode data successfully.
4835 */
4836 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4837 set_buffer_uptodate(bh);
4838
ac27a0ec
DK
4839 if (buffer_uptodate(bh)) {
4840 /* someone brought it uptodate while we waited */
4841 unlock_buffer(bh);
4842 goto has_buffer;
4843 }
4844
4845 /*
4846 * If we have all information of the inode in memory and this
4847 * is the only valid inode in the block, we need not read the
4848 * block.
4849 */
4850 if (in_mem) {
4851 struct buffer_head *bitmap_bh;
240799cd 4852 int i, start;
ac27a0ec 4853
240799cd 4854 start = inode_offset & ~(inodes_per_block - 1);
ac27a0ec 4855
240799cd
TT
4856 /* Is the inode bitmap in cache? */
4857 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
ac27a0ec
DK
4858 if (!bitmap_bh)
4859 goto make_io;
4860
4861 /*
4862 * If the inode bitmap isn't in cache then the
4863 * optimisation may end up performing two reads instead
4864 * of one, so skip it.
4865 */
4866 if (!buffer_uptodate(bitmap_bh)) {
4867 brelse(bitmap_bh);
4868 goto make_io;
4869 }
240799cd 4870 for (i = start; i < start + inodes_per_block; i++) {
ac27a0ec
DK
4871 if (i == inode_offset)
4872 continue;
617ba13b 4873 if (ext4_test_bit(i, bitmap_bh->b_data))
ac27a0ec
DK
4874 break;
4875 }
4876 brelse(bitmap_bh);
240799cd 4877 if (i == start + inodes_per_block) {
ac27a0ec
DK
4878 /* all other inodes are free, so skip I/O */
4879 memset(bh->b_data, 0, bh->b_size);
4880 set_buffer_uptodate(bh);
4881 unlock_buffer(bh);
4882 goto has_buffer;
4883 }
4884 }
4885
4886make_io:
240799cd
TT
4887 /*
4888 * If we need to do any I/O, try to pre-readahead extra
4889 * blocks from the inode table.
4890 */
4891 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4892 ext4_fsblk_t b, end, table;
4893 unsigned num;
4894
4895 table = ext4_inode_table(sb, gdp);
b713a5ec 4896 /* s_inode_readahead_blks is always a power of 2 */
240799cd
TT
4897 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4898 if (table > b)
4899 b = table;
4900 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4901 num = EXT4_INODES_PER_GROUP(sb);
4902 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4903 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
560671a0 4904 num -= ext4_itable_unused_count(sb, gdp);
240799cd
TT
4905 table += num / inodes_per_block;
4906 if (end > table)
4907 end = table;
4908 while (b <= end)
4909 sb_breadahead(sb, b++);
4910 }
4911
ac27a0ec
DK
4912 /*
4913 * There are other valid inodes in the buffer, this inode
4914 * has in-inode xattrs, or we don't have this inode in memory.
4915 * Read the block from disk.
4916 */
4917 get_bh(bh);
4918 bh->b_end_io = end_buffer_read_sync;
4919 submit_bh(READ_META, bh);
4920 wait_on_buffer(bh);
4921 if (!buffer_uptodate(bh)) {
24676da4
TT
4922 EXT4_ERROR_INODE(inode, "unable to read inode "
4923 "block %llu", block);
ac27a0ec
DK
4924 brelse(bh);
4925 return -EIO;
4926 }
4927 }
4928has_buffer:
4929 iloc->bh = bh;
4930 return 0;
4931}
4932
617ba13b 4933int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
ac27a0ec
DK
4934{
4935 /* We have all inode data except xattrs in memory here. */
617ba13b 4936 return __ext4_get_inode_loc(inode, iloc,
19f5fb7a 4937 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
ac27a0ec
DK
4938}
4939
617ba13b 4940void ext4_set_inode_flags(struct inode *inode)
ac27a0ec 4941{
617ba13b 4942 unsigned int flags = EXT4_I(inode)->i_flags;
ac27a0ec
DK
4943
4944 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
617ba13b 4945 if (flags & EXT4_SYNC_FL)
ac27a0ec 4946 inode->i_flags |= S_SYNC;
617ba13b 4947 if (flags & EXT4_APPEND_FL)
ac27a0ec 4948 inode->i_flags |= S_APPEND;
617ba13b 4949 if (flags & EXT4_IMMUTABLE_FL)
ac27a0ec 4950 inode->i_flags |= S_IMMUTABLE;
617ba13b 4951 if (flags & EXT4_NOATIME_FL)
ac27a0ec 4952 inode->i_flags |= S_NOATIME;
617ba13b 4953 if (flags & EXT4_DIRSYNC_FL)
ac27a0ec
DK
4954 inode->i_flags |= S_DIRSYNC;
4955}
4956
ff9ddf7e
JK
4957/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4958void ext4_get_inode_flags(struct ext4_inode_info *ei)
4959{
84a8dce2
DM
4960 unsigned int vfs_fl;
4961 unsigned long old_fl, new_fl;
4962
4963 do {
4964 vfs_fl = ei->vfs_inode.i_flags;
4965 old_fl = ei->i_flags;
4966 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4967 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
4968 EXT4_DIRSYNC_FL);
4969 if (vfs_fl & S_SYNC)
4970 new_fl |= EXT4_SYNC_FL;
4971 if (vfs_fl & S_APPEND)
4972 new_fl |= EXT4_APPEND_FL;
4973 if (vfs_fl & S_IMMUTABLE)
4974 new_fl |= EXT4_IMMUTABLE_FL;
4975 if (vfs_fl & S_NOATIME)
4976 new_fl |= EXT4_NOATIME_FL;
4977 if (vfs_fl & S_DIRSYNC)
4978 new_fl |= EXT4_DIRSYNC_FL;
4979 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
ff9ddf7e 4980}
de9a55b8 4981
0fc1b451 4982static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
de9a55b8 4983 struct ext4_inode_info *ei)
0fc1b451
AK
4984{
4985 blkcnt_t i_blocks ;
8180a562
AK
4986 struct inode *inode = &(ei->vfs_inode);
4987 struct super_block *sb = inode->i_sb;
0fc1b451
AK
4988
4989 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4990 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4991 /* we are using combined 48 bit field */
4992 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4993 le32_to_cpu(raw_inode->i_blocks_lo);
8180a562
AK
4994 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4995 /* i_blocks represent file system block size */
4996 return i_blocks << (inode->i_blkbits - 9);
4997 } else {
4998 return i_blocks;
4999 }
0fc1b451
AK
5000 } else {
5001 return le32_to_cpu(raw_inode->i_blocks_lo);
5002 }
5003}
ff9ddf7e 5004
1d1fe1ee 5005struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
ac27a0ec 5006{
617ba13b
MC
5007 struct ext4_iloc iloc;
5008 struct ext4_inode *raw_inode;
1d1fe1ee 5009 struct ext4_inode_info *ei;
1d1fe1ee 5010 struct inode *inode;
b436b9be 5011 journal_t *journal = EXT4_SB(sb)->s_journal;
1d1fe1ee 5012 long ret;
ac27a0ec
DK
5013 int block;
5014
1d1fe1ee
DH
5015 inode = iget_locked(sb, ino);
5016 if (!inode)
5017 return ERR_PTR(-ENOMEM);
5018 if (!(inode->i_state & I_NEW))
5019 return inode;
5020
5021 ei = EXT4_I(inode);
567f3e9a 5022 iloc.bh = 0;
ac27a0ec 5023
1d1fe1ee
DH
5024 ret = __ext4_get_inode_loc(inode, &iloc, 0);
5025 if (ret < 0)
ac27a0ec 5026 goto bad_inode;
617ba13b 5027 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec
DK
5028 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
5029 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
5030 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
af5bc92d 5031 if (!(test_opt(inode->i_sb, NO_UID32))) {
ac27a0ec
DK
5032 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
5033 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
5034 }
5035 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
ac27a0ec 5036
19f5fb7a 5037 ei->i_state_flags = 0;
ac27a0ec
DK
5038 ei->i_dir_start_lookup = 0;
5039 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
5040 /* We now have enough fields to check if the inode was active or not.
5041 * This is needed because nfsd might try to access dead inodes
5042 * the test is that same one that e2fsck uses
5043 * NeilBrown 1999oct15
5044 */
5045 if (inode->i_nlink == 0) {
5046 if (inode->i_mode == 0 ||
617ba13b 5047 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
ac27a0ec 5048 /* this inode is deleted */
1d1fe1ee 5049 ret = -ESTALE;
ac27a0ec
DK
5050 goto bad_inode;
5051 }
5052 /* The only unlinked inodes we let through here have
5053 * valid i_mode and are being read by the orphan
5054 * recovery code: that's fine, we're about to complete
5055 * the process of deleting those. */
5056 }
ac27a0ec 5057 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
0fc1b451 5058 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
7973c0c1 5059 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
a9e81742 5060 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
a1ddeb7e
BP
5061 ei->i_file_acl |=
5062 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
a48380f7 5063 inode->i_size = ext4_isize(raw_inode);
ac27a0ec 5064 ei->i_disksize = inode->i_size;
a9e7f447
DM
5065#ifdef CONFIG_QUOTA
5066 ei->i_reserved_quota = 0;
5067#endif
ac27a0ec
DK
5068 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
5069 ei->i_block_group = iloc.block_group;
a4912123 5070 ei->i_last_alloc_group = ~0;
ac27a0ec
DK
5071 /*
5072 * NOTE! The in-memory inode i_data array is in little-endian order
5073 * even on big-endian machines: we do NOT byteswap the block numbers!
5074 */
617ba13b 5075 for (block = 0; block < EXT4_N_BLOCKS; block++)
ac27a0ec
DK
5076 ei->i_data[block] = raw_inode->i_block[block];
5077 INIT_LIST_HEAD(&ei->i_orphan);
5078
b436b9be
JK
5079 /*
5080 * Set transaction id's of transactions that have to be committed
5081 * to finish f[data]sync. We set them to currently running transaction
5082 * as we cannot be sure that the inode or some of its metadata isn't
5083 * part of the transaction - the inode could have been reclaimed and
5084 * now it is reread from disk.
5085 */
5086 if (journal) {
5087 transaction_t *transaction;
5088 tid_t tid;
5089
5090 spin_lock(&journal->j_state_lock);
5091 if (journal->j_running_transaction)
5092 transaction = journal->j_running_transaction;
5093 else
5094 transaction = journal->j_committing_transaction;
5095 if (transaction)
5096 tid = transaction->t_tid;
5097 else
5098 tid = journal->j_commit_sequence;
5099 spin_unlock(&journal->j_state_lock);
5100 ei->i_sync_tid = tid;
5101 ei->i_datasync_tid = tid;
5102 }
5103
0040d987 5104 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
ac27a0ec 5105 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
617ba13b 5106 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
e5d2861f 5107 EXT4_INODE_SIZE(inode->i_sb)) {
1d1fe1ee 5108 ret = -EIO;
ac27a0ec 5109 goto bad_inode;
e5d2861f 5110 }
ac27a0ec
DK
5111 if (ei->i_extra_isize == 0) {
5112 /* The extra space is currently unused. Use it. */
617ba13b
MC
5113 ei->i_extra_isize = sizeof(struct ext4_inode) -
5114 EXT4_GOOD_OLD_INODE_SIZE;
ac27a0ec
DK
5115 } else {
5116 __le32 *magic = (void *)raw_inode +
617ba13b 5117 EXT4_GOOD_OLD_INODE_SIZE +
ac27a0ec 5118 ei->i_extra_isize;
617ba13b 5119 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
19f5fb7a 5120 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
5121 }
5122 } else
5123 ei->i_extra_isize = 0;
5124
ef7f3835
KS
5125 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
5126 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
5127 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
5128 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
5129
25ec56b5
JNC
5130 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
5131 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
5132 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5133 inode->i_version |=
5134 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
5135 }
5136
c4b5a614 5137 ret = 0;
485c26ec 5138 if (ei->i_file_acl &&
1032988c 5139 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
24676da4
TT
5140 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
5141 ei->i_file_acl);
485c26ec
TT
5142 ret = -EIO;
5143 goto bad_inode;
5144 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
c4b5a614
TT
5145 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
5146 (S_ISLNK(inode->i_mode) &&
5147 !ext4_inode_is_fast_symlink(inode)))
5148 /* Validate extent which is part of inode */
5149 ret = ext4_ext_check_inode(inode);
de9a55b8 5150 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
fe2c8191
TN
5151 (S_ISLNK(inode->i_mode) &&
5152 !ext4_inode_is_fast_symlink(inode))) {
de9a55b8 5153 /* Validate block references which are part of inode */
fe2c8191
TN
5154 ret = ext4_check_inode_blockref(inode);
5155 }
567f3e9a 5156 if (ret)
de9a55b8 5157 goto bad_inode;
7a262f7c 5158
ac27a0ec 5159 if (S_ISREG(inode->i_mode)) {
617ba13b
MC
5160 inode->i_op = &ext4_file_inode_operations;
5161 inode->i_fop = &ext4_file_operations;
5162 ext4_set_aops(inode);
ac27a0ec 5163 } else if (S_ISDIR(inode->i_mode)) {
617ba13b
MC
5164 inode->i_op = &ext4_dir_inode_operations;
5165 inode->i_fop = &ext4_dir_operations;
ac27a0ec 5166 } else if (S_ISLNK(inode->i_mode)) {
e83c1397 5167 if (ext4_inode_is_fast_symlink(inode)) {
617ba13b 5168 inode->i_op = &ext4_fast_symlink_inode_operations;
e83c1397
DG
5169 nd_terminate_link(ei->i_data, inode->i_size,
5170 sizeof(ei->i_data) - 1);
5171 } else {
617ba13b
MC
5172 inode->i_op = &ext4_symlink_inode_operations;
5173 ext4_set_aops(inode);
ac27a0ec 5174 }
563bdd61
TT
5175 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
5176 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
617ba13b 5177 inode->i_op = &ext4_special_inode_operations;
ac27a0ec
DK
5178 if (raw_inode->i_block[0])
5179 init_special_inode(inode, inode->i_mode,
5180 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
5181 else
5182 init_special_inode(inode, inode->i_mode,
5183 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
563bdd61 5184 } else {
563bdd61 5185 ret = -EIO;
24676da4 5186 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
563bdd61 5187 goto bad_inode;
ac27a0ec 5188 }
af5bc92d 5189 brelse(iloc.bh);
617ba13b 5190 ext4_set_inode_flags(inode);
1d1fe1ee
DH
5191 unlock_new_inode(inode);
5192 return inode;
ac27a0ec
DK
5193
5194bad_inode:
567f3e9a 5195 brelse(iloc.bh);
1d1fe1ee
DH
5196 iget_failed(inode);
5197 return ERR_PTR(ret);
ac27a0ec
DK
5198}
5199
0fc1b451
AK
5200static int ext4_inode_blocks_set(handle_t *handle,
5201 struct ext4_inode *raw_inode,
5202 struct ext4_inode_info *ei)
5203{
5204 struct inode *inode = &(ei->vfs_inode);
5205 u64 i_blocks = inode->i_blocks;
5206 struct super_block *sb = inode->i_sb;
0fc1b451
AK
5207
5208 if (i_blocks <= ~0U) {
5209 /*
5210 * i_blocks can be represnted in a 32 bit variable
5211 * as multiple of 512 bytes
5212 */
8180a562 5213 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
0fc1b451 5214 raw_inode->i_blocks_high = 0;
84a8dce2 5215 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
f287a1a5
TT
5216 return 0;
5217 }
5218 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
5219 return -EFBIG;
5220
5221 if (i_blocks <= 0xffffffffffffULL) {
0fc1b451
AK
5222 /*
5223 * i_blocks can be represented in a 48 bit variable
5224 * as multiple of 512 bytes
5225 */
8180a562 5226 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
0fc1b451 5227 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
84a8dce2 5228 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
0fc1b451 5229 } else {
84a8dce2 5230 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
8180a562
AK
5231 /* i_block is stored in file system block size */
5232 i_blocks = i_blocks >> (inode->i_blkbits - 9);
5233 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
5234 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
0fc1b451 5235 }
f287a1a5 5236 return 0;
0fc1b451
AK
5237}
5238
ac27a0ec
DK
5239/*
5240 * Post the struct inode info into an on-disk inode location in the
5241 * buffer-cache. This gobbles the caller's reference to the
5242 * buffer_head in the inode location struct.
5243 *
5244 * The caller must have write access to iloc->bh.
5245 */
617ba13b 5246static int ext4_do_update_inode(handle_t *handle,
ac27a0ec 5247 struct inode *inode,
830156c7 5248 struct ext4_iloc *iloc)
ac27a0ec 5249{
617ba13b
MC
5250 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
5251 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec
DK
5252 struct buffer_head *bh = iloc->bh;
5253 int err = 0, rc, block;
5254
5255 /* For fields not not tracking in the in-memory inode,
5256 * initialise them to zero for new inodes. */
19f5fb7a 5257 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
617ba13b 5258 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
ac27a0ec 5259
ff9ddf7e 5260 ext4_get_inode_flags(ei);
ac27a0ec 5261 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
af5bc92d 5262 if (!(test_opt(inode->i_sb, NO_UID32))) {
ac27a0ec
DK
5263 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
5264 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
5265/*
5266 * Fix up interoperability with old kernels. Otherwise, old inodes get
5267 * re-used with the upper 16 bits of the uid/gid intact
5268 */
af5bc92d 5269 if (!ei->i_dtime) {
ac27a0ec
DK
5270 raw_inode->i_uid_high =
5271 cpu_to_le16(high_16_bits(inode->i_uid));
5272 raw_inode->i_gid_high =
5273 cpu_to_le16(high_16_bits(inode->i_gid));
5274 } else {
5275 raw_inode->i_uid_high = 0;
5276 raw_inode->i_gid_high = 0;
5277 }
5278 } else {
5279 raw_inode->i_uid_low =
5280 cpu_to_le16(fs_high2lowuid(inode->i_uid));
5281 raw_inode->i_gid_low =
5282 cpu_to_le16(fs_high2lowgid(inode->i_gid));
5283 raw_inode->i_uid_high = 0;
5284 raw_inode->i_gid_high = 0;
5285 }
5286 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
ef7f3835
KS
5287
5288 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5289 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5290 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5291 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5292
0fc1b451
AK
5293 if (ext4_inode_blocks_set(handle, raw_inode, ei))
5294 goto out_brelse;
ac27a0ec 5295 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
1b9c12f4 5296 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
9b8f1f01
MC
5297 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
5298 cpu_to_le32(EXT4_OS_HURD))
a1ddeb7e
BP
5299 raw_inode->i_file_acl_high =
5300 cpu_to_le16(ei->i_file_acl >> 32);
7973c0c1 5301 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
a48380f7
AK
5302 ext4_isize_set(raw_inode, ei->i_disksize);
5303 if (ei->i_disksize > 0x7fffffffULL) {
5304 struct super_block *sb = inode->i_sb;
5305 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
5306 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
5307 EXT4_SB(sb)->s_es->s_rev_level ==
5308 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
5309 /* If this is the first large file
5310 * created, add a flag to the superblock.
5311 */
5312 err = ext4_journal_get_write_access(handle,
5313 EXT4_SB(sb)->s_sbh);
5314 if (err)
5315 goto out_brelse;
5316 ext4_update_dynamic_rev(sb);
5317 EXT4_SET_RO_COMPAT_FEATURE(sb,
617ba13b 5318 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
a48380f7 5319 sb->s_dirt = 1;
0390131b 5320 ext4_handle_sync(handle);
73b50c1c 5321 err = ext4_handle_dirty_metadata(handle, NULL,
a48380f7 5322 EXT4_SB(sb)->s_sbh);
ac27a0ec
DK
5323 }
5324 }
5325 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5326 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5327 if (old_valid_dev(inode->i_rdev)) {
5328 raw_inode->i_block[0] =
5329 cpu_to_le32(old_encode_dev(inode->i_rdev));
5330 raw_inode->i_block[1] = 0;
5331 } else {
5332 raw_inode->i_block[0] = 0;
5333 raw_inode->i_block[1] =
5334 cpu_to_le32(new_encode_dev(inode->i_rdev));
5335 raw_inode->i_block[2] = 0;
5336 }
de9a55b8
TT
5337 } else
5338 for (block = 0; block < EXT4_N_BLOCKS; block++)
5339 raw_inode->i_block[block] = ei->i_data[block];
ac27a0ec 5340
25ec56b5
JNC
5341 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
5342 if (ei->i_extra_isize) {
5343 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5344 raw_inode->i_version_hi =
5345 cpu_to_le32(inode->i_version >> 32);
ac27a0ec 5346 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
25ec56b5
JNC
5347 }
5348
830156c7 5349 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
73b50c1c 5350 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
830156c7
FM
5351 if (!err)
5352 err = rc;
19f5fb7a 5353 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
ac27a0ec 5354
b436b9be 5355 ext4_update_inode_fsync_trans(handle, inode, 0);
ac27a0ec 5356out_brelse:
af5bc92d 5357 brelse(bh);
617ba13b 5358 ext4_std_error(inode->i_sb, err);
ac27a0ec
DK
5359 return err;
5360}
5361
5362/*
617ba13b 5363 * ext4_write_inode()
ac27a0ec
DK
5364 *
5365 * We are called from a few places:
5366 *
5367 * - Within generic_file_write() for O_SYNC files.
5368 * Here, there will be no transaction running. We wait for any running
5369 * trasnaction to commit.
5370 *
5371 * - Within sys_sync(), kupdate and such.
5372 * We wait on commit, if tol to.
5373 *
5374 * - Within prune_icache() (PF_MEMALLOC == true)
5375 * Here we simply return. We can't afford to block kswapd on the
5376 * journal commit.
5377 *
5378 * In all cases it is actually safe for us to return without doing anything,
5379 * because the inode has been copied into a raw inode buffer in
617ba13b 5380 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
ac27a0ec
DK
5381 * knfsd.
5382 *
5383 * Note that we are absolutely dependent upon all inode dirtiers doing the
5384 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5385 * which we are interested.
5386 *
5387 * It would be a bug for them to not do this. The code:
5388 *
5389 * mark_inode_dirty(inode)
5390 * stuff();
5391 * inode->i_size = expr;
5392 *
5393 * is in error because a kswapd-driven write_inode() could occur while
5394 * `stuff()' is running, and the new i_size will be lost. Plus the inode
5395 * will no longer be on the superblock's dirty inode list.
5396 */
a9185b41 5397int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
ac27a0ec 5398{
91ac6f43
FM
5399 int err;
5400
ac27a0ec
DK
5401 if (current->flags & PF_MEMALLOC)
5402 return 0;
5403
91ac6f43
FM
5404 if (EXT4_SB(inode->i_sb)->s_journal) {
5405 if (ext4_journal_current_handle()) {
5406 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5407 dump_stack();
5408 return -EIO;
5409 }
ac27a0ec 5410
a9185b41 5411 if (wbc->sync_mode != WB_SYNC_ALL)
91ac6f43
FM
5412 return 0;
5413
5414 err = ext4_force_commit(inode->i_sb);
5415 } else {
5416 struct ext4_iloc iloc;
ac27a0ec 5417
8b472d73 5418 err = __ext4_get_inode_loc(inode, &iloc, 0);
91ac6f43
FM
5419 if (err)
5420 return err;
a9185b41 5421 if (wbc->sync_mode == WB_SYNC_ALL)
830156c7
FM
5422 sync_dirty_buffer(iloc.bh);
5423 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
24676da4
TT
5424 EXT4_ERROR_INODE(inode,
5425 "IO error syncing inode (block=%llu)",
5426 (unsigned long long) iloc.bh->b_blocknr);
830156c7
FM
5427 err = -EIO;
5428 }
fd2dd9fb 5429 brelse(iloc.bh);
91ac6f43
FM
5430 }
5431 return err;
ac27a0ec
DK
5432}
5433
5434/*
617ba13b 5435 * ext4_setattr()
ac27a0ec
DK
5436 *
5437 * Called from notify_change.
5438 *
5439 * We want to trap VFS attempts to truncate the file as soon as
5440 * possible. In particular, we want to make sure that when the VFS
5441 * shrinks i_size, we put the inode on the orphan list and modify
5442 * i_disksize immediately, so that during the subsequent flushing of
5443 * dirty pages and freeing of disk blocks, we can guarantee that any
5444 * commit will leave the blocks being flushed in an unused state on
5445 * disk. (On recovery, the inode will get truncated and the blocks will
5446 * be freed, so we have a strong guarantee that no future commit will
5447 * leave these blocks visible to the user.)
5448 *
678aaf48
JK
5449 * Another thing we have to assure is that if we are in ordered mode
5450 * and inode is still attached to the committing transaction, we must
5451 * we start writeout of all the dirty pages which are being truncated.
5452 * This way we are sure that all the data written in the previous
5453 * transaction are already on disk (truncate waits for pages under
5454 * writeback).
5455 *
5456 * Called with inode->i_mutex down.
ac27a0ec 5457 */
617ba13b 5458int ext4_setattr(struct dentry *dentry, struct iattr *attr)
ac27a0ec
DK
5459{
5460 struct inode *inode = dentry->d_inode;
5461 int error, rc = 0;
5462 const unsigned int ia_valid = attr->ia_valid;
5463
5464 error = inode_change_ok(inode, attr);
5465 if (error)
5466 return error;
5467
12755627 5468 if (is_quota_modification(inode, attr))
871a2931 5469 dquot_initialize(inode);
ac27a0ec
DK
5470 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
5471 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
5472 handle_t *handle;
5473
5474 /* (user+group)*(old+new) structure, inode write (sb,
5475 * inode block, ? - but truncate inode update has it) */
5aca07eb 5476 handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
194074ac 5477 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
ac27a0ec
DK
5478 if (IS_ERR(handle)) {
5479 error = PTR_ERR(handle);
5480 goto err_out;
5481 }
b43fa828 5482 error = dquot_transfer(inode, attr);
ac27a0ec 5483 if (error) {
617ba13b 5484 ext4_journal_stop(handle);
ac27a0ec
DK
5485 return error;
5486 }
5487 /* Update corresponding info in inode so that everything is in
5488 * one transaction */
5489 if (attr->ia_valid & ATTR_UID)
5490 inode->i_uid = attr->ia_uid;
5491 if (attr->ia_valid & ATTR_GID)
5492 inode->i_gid = attr->ia_gid;
617ba13b
MC
5493 error = ext4_mark_inode_dirty(handle, inode);
5494 ext4_journal_stop(handle);
ac27a0ec
DK
5495 }
5496
e2b46574 5497 if (attr->ia_valid & ATTR_SIZE) {
12e9b892 5498 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
e2b46574
ES
5499 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5500
5501 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
5502 error = -EFBIG;
5503 goto err_out;
5504 }
5505 }
5506 }
5507
ac27a0ec 5508 if (S_ISREG(inode->i_mode) &&
c8d46e41
JZ
5509 attr->ia_valid & ATTR_SIZE &&
5510 (attr->ia_size < inode->i_size ||
12e9b892 5511 (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)))) {
ac27a0ec
DK
5512 handle_t *handle;
5513
617ba13b 5514 handle = ext4_journal_start(inode, 3);
ac27a0ec
DK
5515 if (IS_ERR(handle)) {
5516 error = PTR_ERR(handle);
5517 goto err_out;
5518 }
5519
617ba13b
MC
5520 error = ext4_orphan_add(handle, inode);
5521 EXT4_I(inode)->i_disksize = attr->ia_size;
5522 rc = ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
5523 if (!error)
5524 error = rc;
617ba13b 5525 ext4_journal_stop(handle);
678aaf48
JK
5526
5527 if (ext4_should_order_data(inode)) {
5528 error = ext4_begin_ordered_truncate(inode,
5529 attr->ia_size);
5530 if (error) {
5531 /* Do as much error cleanup as possible */
5532 handle = ext4_journal_start(inode, 3);
5533 if (IS_ERR(handle)) {
5534 ext4_orphan_del(NULL, inode);
5535 goto err_out;
5536 }
5537 ext4_orphan_del(handle, inode);
5538 ext4_journal_stop(handle);
5539 goto err_out;
5540 }
5541 }
c8d46e41 5542 /* ext4_truncate will clear the flag */
12e9b892 5543 if ((ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)))
c8d46e41 5544 ext4_truncate(inode);
ac27a0ec
DK
5545 }
5546
1025774c
CH
5547 if ((attr->ia_valid & ATTR_SIZE) &&
5548 attr->ia_size != i_size_read(inode))
5549 rc = vmtruncate(inode, attr->ia_size);
ac27a0ec 5550
1025774c
CH
5551 if (!rc) {
5552 setattr_copy(inode, attr);
5553 mark_inode_dirty(inode);
5554 }
5555
5556 /*
5557 * If the call to ext4_truncate failed to get a transaction handle at
5558 * all, we need to clean up the in-core orphan list manually.
5559 */
ac27a0ec 5560 if (inode->i_nlink)
617ba13b 5561 ext4_orphan_del(NULL, inode);
ac27a0ec
DK
5562
5563 if (!rc && (ia_valid & ATTR_MODE))
617ba13b 5564 rc = ext4_acl_chmod(inode);
ac27a0ec
DK
5565
5566err_out:
617ba13b 5567 ext4_std_error(inode->i_sb, error);
ac27a0ec
DK
5568 if (!error)
5569 error = rc;
5570 return error;
5571}
5572
3e3398a0
MC
5573int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5574 struct kstat *stat)
5575{
5576 struct inode *inode;
5577 unsigned long delalloc_blocks;
5578
5579 inode = dentry->d_inode;
5580 generic_fillattr(inode, stat);
5581
5582 /*
5583 * We can't update i_blocks if the block allocation is delayed
5584 * otherwise in the case of system crash before the real block
5585 * allocation is done, we will have i_blocks inconsistent with
5586 * on-disk file blocks.
5587 * We always keep i_blocks updated together with real
5588 * allocation. But to not confuse with user, stat
5589 * will return the blocks that include the delayed allocation
5590 * blocks for this file.
5591 */
5592 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
5593 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
5594 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
5595
5596 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
5597 return 0;
5598}
ac27a0ec 5599
a02908f1
MC
5600static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
5601 int chunk)
5602{
5603 int indirects;
5604
5605 /* if nrblocks are contiguous */
5606 if (chunk) {
5607 /*
5608 * With N contiguous data blocks, it need at most
5609 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
5610 * 2 dindirect blocks
5611 * 1 tindirect block
5612 */
5613 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
5614 return indirects + 3;
5615 }
5616 /*
5617 * if nrblocks are not contiguous, worse case, each block touch
5618 * a indirect block, and each indirect block touch a double indirect
5619 * block, plus a triple indirect block
5620 */
5621 indirects = nrblocks * 2 + 1;
5622 return indirects;
5623}
5624
5625static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5626{
12e9b892 5627 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
ac51d837
TT
5628 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
5629 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
a02908f1 5630}
ac51d837 5631
ac27a0ec 5632/*
a02908f1
MC
5633 * Account for index blocks, block groups bitmaps and block group
5634 * descriptor blocks if modify datablocks and index blocks
5635 * worse case, the indexs blocks spread over different block groups
ac27a0ec 5636 *
a02908f1 5637 * If datablocks are discontiguous, they are possible to spread over
af901ca1 5638 * different block groups too. If they are contiuguous, with flexbg,
a02908f1 5639 * they could still across block group boundary.
ac27a0ec 5640 *
a02908f1
MC
5641 * Also account for superblock, inode, quota and xattr blocks
5642 */
5643int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5644{
8df9675f
TT
5645 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5646 int gdpblocks;
a02908f1
MC
5647 int idxblocks;
5648 int ret = 0;
5649
5650 /*
5651 * How many index blocks need to touch to modify nrblocks?
5652 * The "Chunk" flag indicating whether the nrblocks is
5653 * physically contiguous on disk
5654 *
5655 * For Direct IO and fallocate, they calls get_block to allocate
5656 * one single extent at a time, so they could set the "Chunk" flag
5657 */
5658 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
5659
5660 ret = idxblocks;
5661
5662 /*
5663 * Now let's see how many group bitmaps and group descriptors need
5664 * to account
5665 */
5666 groups = idxblocks;
5667 if (chunk)
5668 groups += 1;
5669 else
5670 groups += nrblocks;
5671
5672 gdpblocks = groups;
8df9675f
TT
5673 if (groups > ngroups)
5674 groups = ngroups;
a02908f1
MC
5675 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5676 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5677
5678 /* bitmaps and block group descriptor blocks */
5679 ret += groups + gdpblocks;
5680
5681 /* Blocks for super block, inode, quota and xattr blocks */
5682 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
5683
5684 return ret;
5685}
5686
5687/*
5688 * Calulate the total number of credits to reserve to fit
f3bd1f3f
MC
5689 * the modification of a single pages into a single transaction,
5690 * which may include multiple chunks of block allocations.
ac27a0ec 5691 *
525f4ed8 5692 * This could be called via ext4_write_begin()
ac27a0ec 5693 *
525f4ed8 5694 * We need to consider the worse case, when
a02908f1 5695 * one new block per extent.
ac27a0ec 5696 */
a86c6181 5697int ext4_writepage_trans_blocks(struct inode *inode)
ac27a0ec 5698{
617ba13b 5699 int bpp = ext4_journal_blocks_per_page(inode);
ac27a0ec
DK
5700 int ret;
5701
a02908f1 5702 ret = ext4_meta_trans_blocks(inode, bpp, 0);
a86c6181 5703
a02908f1 5704 /* Account for data blocks for journalled mode */
617ba13b 5705 if (ext4_should_journal_data(inode))
a02908f1 5706 ret += bpp;
ac27a0ec
DK
5707 return ret;
5708}
f3bd1f3f
MC
5709
5710/*
5711 * Calculate the journal credits for a chunk of data modification.
5712 *
5713 * This is called from DIO, fallocate or whoever calling
af901ca1 5714 * ext4_get_blocks() to map/allocate a chunk of contiguous disk blocks.
f3bd1f3f
MC
5715 *
5716 * journal buffers for data blocks are not included here, as DIO
5717 * and fallocate do no need to journal data buffers.
5718 */
5719int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5720{
5721 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5722}
5723
ac27a0ec 5724/*
617ba13b 5725 * The caller must have previously called ext4_reserve_inode_write().
ac27a0ec
DK
5726 * Give this, we know that the caller already has write access to iloc->bh.
5727 */
617ba13b 5728int ext4_mark_iloc_dirty(handle_t *handle,
de9a55b8 5729 struct inode *inode, struct ext4_iloc *iloc)
ac27a0ec
DK
5730{
5731 int err = 0;
5732
25ec56b5
JNC
5733 if (test_opt(inode->i_sb, I_VERSION))
5734 inode_inc_iversion(inode);
5735
ac27a0ec
DK
5736 /* the do_update_inode consumes one bh->b_count */
5737 get_bh(iloc->bh);
5738
dab291af 5739 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
830156c7 5740 err = ext4_do_update_inode(handle, inode, iloc);
ac27a0ec
DK
5741 put_bh(iloc->bh);
5742 return err;
5743}
5744
5745/*
5746 * On success, We end up with an outstanding reference count against
5747 * iloc->bh. This _must_ be cleaned up later.
5748 */
5749
5750int
617ba13b
MC
5751ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5752 struct ext4_iloc *iloc)
ac27a0ec 5753{
0390131b
FM
5754 int err;
5755
5756 err = ext4_get_inode_loc(inode, iloc);
5757 if (!err) {
5758 BUFFER_TRACE(iloc->bh, "get_write_access");
5759 err = ext4_journal_get_write_access(handle, iloc->bh);
5760 if (err) {
5761 brelse(iloc->bh);
5762 iloc->bh = NULL;
ac27a0ec
DK
5763 }
5764 }
617ba13b 5765 ext4_std_error(inode->i_sb, err);
ac27a0ec
DK
5766 return err;
5767}
5768
6dd4ee7c
KS
5769/*
5770 * Expand an inode by new_extra_isize bytes.
5771 * Returns 0 on success or negative error number on failure.
5772 */
1d03ec98
AK
5773static int ext4_expand_extra_isize(struct inode *inode,
5774 unsigned int new_extra_isize,
5775 struct ext4_iloc iloc,
5776 handle_t *handle)
6dd4ee7c
KS
5777{
5778 struct ext4_inode *raw_inode;
5779 struct ext4_xattr_ibody_header *header;
5780 struct ext4_xattr_entry *entry;
5781
5782 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5783 return 0;
5784
5785 raw_inode = ext4_raw_inode(&iloc);
5786
5787 header = IHDR(inode, raw_inode);
5788 entry = IFIRST(header);
5789
5790 /* No extended attributes present */
19f5fb7a
TT
5791 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5792 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
6dd4ee7c
KS
5793 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5794 new_extra_isize);
5795 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5796 return 0;
5797 }
5798
5799 /* try to expand with EAs present */
5800 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5801 raw_inode, handle);
5802}
5803
ac27a0ec
DK
5804/*
5805 * What we do here is to mark the in-core inode as clean with respect to inode
5806 * dirtiness (it may still be data-dirty).
5807 * This means that the in-core inode may be reaped by prune_icache
5808 * without having to perform any I/O. This is a very good thing,
5809 * because *any* task may call prune_icache - even ones which
5810 * have a transaction open against a different journal.
5811 *
5812 * Is this cheating? Not really. Sure, we haven't written the
5813 * inode out, but prune_icache isn't a user-visible syncing function.
5814 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5815 * we start and wait on commits.
5816 *
5817 * Is this efficient/effective? Well, we're being nice to the system
5818 * by cleaning up our inodes proactively so they can be reaped
5819 * without I/O. But we are potentially leaving up to five seconds'
5820 * worth of inodes floating about which prune_icache wants us to
5821 * write out. One way to fix that would be to get prune_icache()
5822 * to do a write_super() to free up some memory. It has the desired
5823 * effect.
5824 */
617ba13b 5825int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
ac27a0ec 5826{
617ba13b 5827 struct ext4_iloc iloc;
6dd4ee7c
KS
5828 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5829 static unsigned int mnt_count;
5830 int err, ret;
ac27a0ec
DK
5831
5832 might_sleep();
617ba13b 5833 err = ext4_reserve_inode_write(handle, inode, &iloc);
0390131b
FM
5834 if (ext4_handle_valid(handle) &&
5835 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
19f5fb7a 5836 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
6dd4ee7c
KS
5837 /*
5838 * We need extra buffer credits since we may write into EA block
5839 * with this same handle. If journal_extend fails, then it will
5840 * only result in a minor loss of functionality for that inode.
5841 * If this is felt to be critical, then e2fsck should be run to
5842 * force a large enough s_min_extra_isize.
5843 */
5844 if ((jbd2_journal_extend(handle,
5845 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5846 ret = ext4_expand_extra_isize(inode,
5847 sbi->s_want_extra_isize,
5848 iloc, handle);
5849 if (ret) {
19f5fb7a
TT
5850 ext4_set_inode_state(inode,
5851 EXT4_STATE_NO_EXPAND);
c1bddad9
AK
5852 if (mnt_count !=
5853 le16_to_cpu(sbi->s_es->s_mnt_count)) {
12062ddd 5854 ext4_warning(inode->i_sb,
6dd4ee7c
KS
5855 "Unable to expand inode %lu. Delete"
5856 " some EAs or run e2fsck.",
5857 inode->i_ino);
c1bddad9
AK
5858 mnt_count =
5859 le16_to_cpu(sbi->s_es->s_mnt_count);
6dd4ee7c
KS
5860 }
5861 }
5862 }
5863 }
ac27a0ec 5864 if (!err)
617ba13b 5865 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec
DK
5866 return err;
5867}
5868
5869/*
617ba13b 5870 * ext4_dirty_inode() is called from __mark_inode_dirty()
ac27a0ec
DK
5871 *
5872 * We're really interested in the case where a file is being extended.
5873 * i_size has been changed by generic_commit_write() and we thus need
5874 * to include the updated inode in the current transaction.
5875 *
5dd4056d 5876 * Also, dquot_alloc_block() will always dirty the inode when blocks
ac27a0ec
DK
5877 * are allocated to the file.
5878 *
5879 * If the inode is marked synchronous, we don't honour that here - doing
5880 * so would cause a commit on atime updates, which we don't bother doing.
5881 * We handle synchronous inodes at the highest possible level.
5882 */
617ba13b 5883void ext4_dirty_inode(struct inode *inode)
ac27a0ec 5884{
ac27a0ec
DK
5885 handle_t *handle;
5886
617ba13b 5887 handle = ext4_journal_start(inode, 2);
ac27a0ec
DK
5888 if (IS_ERR(handle))
5889 goto out;
f3dc272f 5890
f3dc272f
CW
5891 ext4_mark_inode_dirty(handle, inode);
5892
617ba13b 5893 ext4_journal_stop(handle);
ac27a0ec
DK
5894out:
5895 return;
5896}
5897
5898#if 0
5899/*
5900 * Bind an inode's backing buffer_head into this transaction, to prevent
5901 * it from being flushed to disk early. Unlike
617ba13b 5902 * ext4_reserve_inode_write, this leaves behind no bh reference and
ac27a0ec
DK
5903 * returns no iloc structure, so the caller needs to repeat the iloc
5904 * lookup to mark the inode dirty later.
5905 */
617ba13b 5906static int ext4_pin_inode(handle_t *handle, struct inode *inode)
ac27a0ec 5907{
617ba13b 5908 struct ext4_iloc iloc;
ac27a0ec
DK
5909
5910 int err = 0;
5911 if (handle) {
617ba13b 5912 err = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
5913 if (!err) {
5914 BUFFER_TRACE(iloc.bh, "get_write_access");
dab291af 5915 err = jbd2_journal_get_write_access(handle, iloc.bh);
ac27a0ec 5916 if (!err)
0390131b 5917 err = ext4_handle_dirty_metadata(handle,
73b50c1c 5918 NULL,
0390131b 5919 iloc.bh);
ac27a0ec
DK
5920 brelse(iloc.bh);
5921 }
5922 }
617ba13b 5923 ext4_std_error(inode->i_sb, err);
ac27a0ec
DK
5924 return err;
5925}
5926#endif
5927
617ba13b 5928int ext4_change_inode_journal_flag(struct inode *inode, int val)
ac27a0ec
DK
5929{
5930 journal_t *journal;
5931 handle_t *handle;
5932 int err;
5933
5934 /*
5935 * We have to be very careful here: changing a data block's
5936 * journaling status dynamically is dangerous. If we write a
5937 * data block to the journal, change the status and then delete
5938 * that block, we risk forgetting to revoke the old log record
5939 * from the journal and so a subsequent replay can corrupt data.
5940 * So, first we make sure that the journal is empty and that
5941 * nobody is changing anything.
5942 */
5943
617ba13b 5944 journal = EXT4_JOURNAL(inode);
0390131b
FM
5945 if (!journal)
5946 return 0;
d699594d 5947 if (is_journal_aborted(journal))
ac27a0ec
DK
5948 return -EROFS;
5949
dab291af
MC
5950 jbd2_journal_lock_updates(journal);
5951 jbd2_journal_flush(journal);
ac27a0ec
DK
5952
5953 /*
5954 * OK, there are no updates running now, and all cached data is
5955 * synced to disk. We are now in a completely consistent state
5956 * which doesn't have anything in the journal, and we know that
5957 * no filesystem updates are running, so it is safe to modify
5958 * the inode's in-core data-journaling state flag now.
5959 */
5960
5961 if (val)
12e9b892 5962 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
ac27a0ec 5963 else
12e9b892 5964 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
617ba13b 5965 ext4_set_aops(inode);
ac27a0ec 5966
dab291af 5967 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
5968
5969 /* Finally we can mark the inode as dirty. */
5970
617ba13b 5971 handle = ext4_journal_start(inode, 1);
ac27a0ec
DK
5972 if (IS_ERR(handle))
5973 return PTR_ERR(handle);
5974
617ba13b 5975 err = ext4_mark_inode_dirty(handle, inode);
0390131b 5976 ext4_handle_sync(handle);
617ba13b
MC
5977 ext4_journal_stop(handle);
5978 ext4_std_error(inode->i_sb, err);
ac27a0ec
DK
5979
5980 return err;
5981}
2e9ee850
AK
5982
5983static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5984{
5985 return !buffer_mapped(bh);
5986}
5987
c2ec175c 5988int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2e9ee850 5989{
c2ec175c 5990 struct page *page = vmf->page;
2e9ee850
AK
5991 loff_t size;
5992 unsigned long len;
5993 int ret = -EINVAL;
79f0be8d 5994 void *fsdata;
2e9ee850
AK
5995 struct file *file = vma->vm_file;
5996 struct inode *inode = file->f_path.dentry->d_inode;
5997 struct address_space *mapping = inode->i_mapping;
5998
5999 /*
6000 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
6001 * get i_mutex because we are already holding mmap_sem.
6002 */
6003 down_read(&inode->i_alloc_sem);
6004 size = i_size_read(inode);
6005 if (page->mapping != mapping || size <= page_offset(page)
6006 || !PageUptodate(page)) {
6007 /* page got truncated from under us? */
6008 goto out_unlock;
6009 }
6010 ret = 0;
6011 if (PageMappedToDisk(page))
6012 goto out_unlock;
6013
6014 if (page->index == size >> PAGE_CACHE_SHIFT)
6015 len = size & ~PAGE_CACHE_MASK;
6016 else
6017 len = PAGE_CACHE_SIZE;
6018
a827eaff
AK
6019 lock_page(page);
6020 /*
6021 * return if we have all the buffers mapped. This avoid
6022 * the need to call write_begin/write_end which does a
6023 * journal_start/journal_stop which can block and take
6024 * long time
6025 */
2e9ee850 6026 if (page_has_buffers(page)) {
2e9ee850 6027 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
a827eaff
AK
6028 ext4_bh_unmapped)) {
6029 unlock_page(page);
2e9ee850 6030 goto out_unlock;
a827eaff 6031 }
2e9ee850 6032 }
a827eaff 6033 unlock_page(page);
2e9ee850
AK
6034 /*
6035 * OK, we need to fill the hole... Do write_begin write_end
6036 * to do block allocation/reservation.We are not holding
6037 * inode.i__mutex here. That allow * parallel write_begin,
6038 * write_end call. lock_page prevent this from happening
6039 * on the same page though
6040 */
6041 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
79f0be8d 6042 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
2e9ee850
AK
6043 if (ret < 0)
6044 goto out_unlock;
6045 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
79f0be8d 6046 len, len, page, fsdata);
2e9ee850
AK
6047 if (ret < 0)
6048 goto out_unlock;
6049 ret = 0;
6050out_unlock:
c2ec175c
NP
6051 if (ret)
6052 ret = VM_FAULT_SIGBUS;
2e9ee850
AK
6053 up_read(&inode->i_alloc_sem);
6054 return ret;
6055}