blob: 658c4a7f2578cd61b5b673166957711323a2af18 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/inode.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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 *
Mingming Cao617ba132006-10-11 01:20:53 -070022 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070023 */
24
25#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/time.h>
Mingming Caodab291a2006-10-11 01:21:01 -070028#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#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>
Alex Tomas64769242008-07-11 19:27:31 -040035#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000037#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038#include <linux/uio.h>
39#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040040#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041#include "xattr.h"
42#include "acl.h"
Mingming Caod2a17632008-07-14 17:52:37 -040043#include "ext4_extents.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040045#define MPAGE_DA_EXTENT_TAIL 0x01
46
Jan Kara678aaf42008-07-11 19:27:31 -040047static inline int ext4_begin_ordered_truncate(struct inode *inode,
48 loff_t new_size)
49{
Jan Kara7f5aa212009-02-10 11:15:34 -050050 return jbd2_journal_begin_ordered_truncate(
51 EXT4_SB(inode->i_sb)->s_journal,
52 &EXT4_I(inode)->jinode,
53 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040054}
55
Alex Tomas64769242008-07-11 19:27:31 -040056static void ext4_invalidatepage(struct page *page, unsigned long offset);
57
Dave Kleikampac27a0e2006-10-11 01:20:50 -070058/*
59 * Test whether an inode is a fast symlink.
60 */
Mingming Cao617ba132006-10-11 01:20:53 -070061static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062{
Mingming Cao617ba132006-10-11 01:20:53 -070063 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064 (inode->i_sb->s_blocksize >> 9) : 0;
65
66 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
67}
68
69/*
Mingming Cao617ba132006-10-11 01:20:53 -070070 * The ext4 forget function must perform a revoke if we are freeing data
Dave Kleikampac27a0e2006-10-11 01:20:50 -070071 * which has been journaled. Metadata (eg. indirect blocks) must be
72 * revoked in all cases.
73 *
74 * "bh" may be NULL: a metadata block may have been freed from memory
75 * but there may still be a record of it in the journal, and that record
76 * still needs to be revoked.
Frank Mayhar03901312009-01-07 00:06:22 -050077 *
78 * If the handle isn't valid we're not journaling so there's nothing to do.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070079 */
Mingming Cao617ba132006-10-11 01:20:53 -070080int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
81 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070082{
83 int err;
84
Frank Mayhar03901312009-01-07 00:06:22 -050085 if (!ext4_handle_valid(handle))
86 return 0;
87
Dave Kleikampac27a0e2006-10-11 01:20:50 -070088 might_sleep();
89
90 BUFFER_TRACE(bh, "enter");
91
92 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
93 "data mode %lx\n",
94 bh, is_metadata, inode->i_mode,
95 test_opt(inode->i_sb, DATA_FLAGS));
96
97 /* Never use the revoke function if we are doing full data
98 * journaling: there is no need to, and a V1 superblock won't
99 * support it. Otherwise, only skip the revoke on un-journaled
100 * data blocks. */
101
Mingming Cao617ba132006-10-11 01:20:53 -0700102 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
103 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -0700105 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700106 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107 }
108 return 0;
109 }
110
111 /*
112 * data!=journal && (is_metadata || should_journal_data(inode))
113 */
Mingming Cao617ba132006-10-11 01:20:53 -0700114 BUFFER_TRACE(bh, "call ext4_journal_revoke");
115 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400117 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700118 "error %d when attempting revoke", err);
119 BUFFER_TRACE(bh, "exit");
120 return err;
121}
122
123/*
124 * Work out how many blocks we need to proceed with the next chunk of a
125 * truncate transaction.
126 */
127static unsigned long blocks_for_truncate(struct inode *inode)
128{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500129 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130
131 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
132
133 /* Give ourselves just enough room to cope with inodes in which
134 * i_blocks is corrupt: we've seen disk corruptions in the past
135 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700136 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137 * will go a bit crazy if that happens, but at least we should
138 * try not to panic the whole kernel. */
139 if (needed < 2)
140 needed = 2;
141
142 /* But we need to bound the transaction so we don't overflow the
143 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700144 if (needed > EXT4_MAX_TRANS_DATA)
145 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146
Mingming Cao617ba132006-10-11 01:20:53 -0700147 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148}
149
150/*
151 * Truncate transactions can be complex and absolutely huge. So we need to
152 * be able to restart the transaction at a conventient checkpoint to make
153 * sure we don't overflow the journal.
154 *
155 * start_transaction gets us a new handle for a truncate transaction,
156 * and extend_transaction tries to extend the existing one a bit. If
157 * extend fails, we need to propagate the failure up and restart the
158 * transaction in the top-level truncate loop. --sct
159 */
160static handle_t *start_transaction(struct inode *inode)
161{
162 handle_t *result;
163
Mingming Cao617ba132006-10-11 01:20:53 -0700164 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 if (!IS_ERR(result))
166 return result;
167
Mingming Cao617ba132006-10-11 01:20:53 -0700168 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 return result;
170}
171
172/*
173 * Try to extend this transaction for the purposes of truncation.
174 *
175 * Returns 0 if we managed to create more room. If we can't create more
176 * room, and the transaction must be restarted we return 1.
177 */
178static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
179{
Frank Mayhar03901312009-01-07 00:06:22 -0500180 if (!ext4_handle_valid(handle))
181 return 0;
182 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700184 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 return 0;
186 return 1;
187}
188
189/*
190 * Restart the transaction associated with *handle. This does a commit,
191 * so before we call here everything must be consistently dirtied against
192 * this transaction.
193 */
Mingming Cao617ba132006-10-11 01:20:53 -0700194static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195{
Frank Mayhar03901312009-01-07 00:06:22 -0500196 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700198 return ext4_journal_restart(handle, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700199}
200
201/*
202 * Called at the last iput() if i_nlink is zero.
203 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400204void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205{
206 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400207 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208
Jan Kara678aaf42008-07-11 19:27:31 -0400209 if (ext4_should_order_data(inode))
210 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211 truncate_inode_pages(&inode->i_data, 0);
212
213 if (is_bad_inode(inode))
214 goto no_delete;
215
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400216 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400218 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 /*
220 * If we're going to skip the normal cleanup, we still need to
221 * make sure that the in-core orphan linked list is properly
222 * cleaned up.
223 */
Mingming Cao617ba132006-10-11 01:20:53 -0700224 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 goto no_delete;
226 }
227
228 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500229 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400231 err = ext4_mark_inode_dirty(handle, inode);
232 if (err) {
233 ext4_warning(inode->i_sb, __func__,
234 "couldn't mark inode dirty (err %d)", err);
235 goto stop_handle;
236 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700238 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400239
240 /*
241 * ext4_ext_truncate() doesn't reserve any slop when it
242 * restarts journal transactions; therefore there may not be
243 * enough credits left in the handle to remove the inode from
244 * the orphan list and set the dtime field.
245 */
Frank Mayhar03901312009-01-07 00:06:22 -0500246 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400247 err = ext4_journal_extend(handle, 3);
248 if (err > 0)
249 err = ext4_journal_restart(handle, 3);
250 if (err != 0) {
251 ext4_warning(inode->i_sb, __func__,
252 "couldn't extend journal (err %d)", err);
253 stop_handle:
254 ext4_journal_stop(handle);
255 goto no_delete;
256 }
257 }
258
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700260 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700262 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700264 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265 * (Well, we could do this if we need to, but heck - it works)
266 */
Mingming Cao617ba132006-10-11 01:20:53 -0700267 ext4_orphan_del(handle, inode);
268 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269
270 /*
271 * One subtle ordering requirement: if anything has gone wrong
272 * (transaction abort, IO errors, whatever), then we can still
273 * do these next steps (the fs will already have been marked as
274 * having errors), but we can't free the inode if the mark_dirty
275 * fails.
276 */
Mingming Cao617ba132006-10-11 01:20:53 -0700277 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 /* If that failed, just do the required in-core inode clear. */
279 clear_inode(inode);
280 else
Mingming Cao617ba132006-10-11 01:20:53 -0700281 ext4_free_inode(handle, inode);
282 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 return;
284no_delete:
285 clear_inode(inode); /* We must guarantee clearing of inode... */
286}
287
288typedef struct {
289 __le32 *p;
290 __le32 key;
291 struct buffer_head *bh;
292} Indirect;
293
294static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
295{
296 p->key = *(p->p = v);
297 p->bh = bh;
298}
299
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300/**
Mingming Cao617ba132006-10-11 01:20:53 -0700301 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 * @inode: inode in question (we are only interested in its superblock)
303 * @i_block: block number to be parsed
304 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400305 * @boundary: set this non-zero if the referred-to block is likely to be
306 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307 *
Mingming Cao617ba132006-10-11 01:20:53 -0700308 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 * for UNIX filesystems - tree of pointers anchored in the inode, with
310 * data blocks at leaves and indirect blocks in intermediate nodes.
311 * This function translates the block number into path in that tree -
312 * return value is the path length and @offsets[n] is the offset of
313 * pointer to (n+1)th node in the nth one. If @block is out of range
314 * (negative or too large) warning is printed and zero returned.
315 *
316 * Note: function doesn't find node addresses, so no IO is needed. All
317 * we need to know is the capacity of indirect blocks (taken from the
318 * inode->i_sb).
319 */
320
321/*
322 * Portability note: the last comparison (check that we fit into triple
323 * indirect block) is spelled differently, because otherwise on an
324 * architecture with 32-bit longs and 8Kb pages we might get into trouble
325 * if our filesystem had 8Kb blocks. We might use long long, but that would
326 * kill us on x86. Oh, well, at least the sign propagation does not matter -
327 * i_block would have to be negative in the very beginning, so we would not
328 * get there at all.
329 */
330
Mingming Cao617ba132006-10-11 01:20:53 -0700331static int ext4_block_to_path(struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500332 ext4_lblk_t i_block,
333 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700334{
Mingming Cao617ba132006-10-11 01:20:53 -0700335 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
336 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
337 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 indirect_blocks = ptrs,
339 double_blocks = (1 << (ptrs_bits * 2));
340 int n = 0;
341 int final = 0;
342
343 if (i_block < 0) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400344 ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700345 } else if (i_block < direct_blocks) {
346 offsets[n++] = i_block;
347 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400348 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700349 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350 offsets[n++] = i_block;
351 final = ptrs;
352 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700353 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354 offsets[n++] = i_block >> ptrs_bits;
355 offsets[n++] = i_block & (ptrs - 1);
356 final = ptrs;
357 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700358 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359 offsets[n++] = i_block >> (ptrs_bits * 2);
360 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
361 offsets[n++] = i_block & (ptrs - 1);
362 final = ptrs;
363 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500364 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'o06a279d2009-01-17 18:41:37 -0500365 "block %lu > max in inode %lu",
Eric Sandeene2b46572008-01-28 23:58:27 -0500366 i_block + direct_blocks +
Theodore Ts'o06a279d2009-01-17 18:41:37 -0500367 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 }
369 if (boundary)
370 *boundary = final - 1 - (i_block & (ptrs - 1));
371 return n;
372}
373
374/**
Mingming Cao617ba132006-10-11 01:20:53 -0700375 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700376 * @inode: inode in question
377 * @depth: depth of the chain (1 - direct pointer, etc.)
378 * @offsets: offsets of pointers in inode/indirect blocks
379 * @chain: place to store the result
380 * @err: here we store the error value
381 *
382 * Function fills the array of triples <key, p, bh> and returns %NULL
383 * if everything went OK or the pointer to the last filled triple
384 * (incomplete one) otherwise. Upon the return chain[i].key contains
385 * the number of (i+1)-th block in the chain (as it is stored in memory,
386 * i.e. little-endian 32-bit), chain[i].p contains the address of that
387 * number (it points into struct inode for i==0 and into the bh->b_data
388 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
389 * block for i>0 and NULL for i==0. In other words, it holds the block
390 * numbers of the chain, addresses they were taken from (and where we can
391 * verify that chain did not change) and buffer_heads hosting these
392 * numbers.
393 *
394 * Function stops when it stumbles upon zero pointer (absent block)
395 * (pointer to last triple returned, *@err == 0)
396 * or when it gets an IO error reading an indirect block
397 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398 * or when it reads all @depth-1 indirect blocks successfully and finds
399 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500400 *
401 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500402 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700403 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500404static Indirect *ext4_get_branch(struct inode *inode, int depth,
405 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406 Indirect chain[4], int *err)
407{
408 struct super_block *sb = inode->i_sb;
409 Indirect *p = chain;
410 struct buffer_head *bh;
411
412 *err = 0;
413 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400414 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415 if (!p->key)
416 goto no_block;
417 while (--depth) {
418 bh = sb_bread(sb, le32_to_cpu(p->key));
419 if (!bh)
420 goto failure;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400421 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422 /* Reader: end */
423 if (!p->key)
424 goto no_block;
425 }
426 return NULL;
427
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428failure:
429 *err = -EIO;
430no_block:
431 return p;
432}
433
434/**
Mingming Cao617ba132006-10-11 01:20:53 -0700435 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 * @inode: owner
437 * @ind: descriptor of indirect block.
438 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000439 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 * It is used when heuristic for sequential allocation fails.
441 * Rules are:
442 * + if there is a block to the left of our position - allocate near it.
443 * + if pointer will live in indirect block - allocate near that block.
444 * + if pointer will live in inode - allocate in the same
445 * cylinder group.
446 *
447 * In the latter case we colour the starting block by the callers PID to
448 * prevent it from clashing with concurrent allocations for a different inode
449 * in the same block group. The PID is used here so that functionally related
450 * files will be close-by on-disk.
451 *
452 * Caller must make sure that @ind is valid and will stay that way.
453 */
Mingming Cao617ba132006-10-11 01:20:53 -0700454static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455{
Mingming Cao617ba132006-10-11 01:20:53 -0700456 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400457 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700459 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500460 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700461 ext4_grpblk_t colour;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462
463 /* Try to find previous block */
464 for (p = ind->p - 1; p >= start; p--) {
465 if (*p)
466 return le32_to_cpu(*p);
467 }
468
469 /* No such thing, so let's try location of indirect block */
470 if (ind->bh)
471 return ind->bh->b_blocknr;
472
473 /*
474 * It is going to be referred to from the inode itself? OK, just put it
475 * into the same cylinder group then.
476 */
Mingming Cao617ba132006-10-11 01:20:53 -0700477 bg_start = ext4_group_first_block_no(inode->i_sb, ei->i_block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500478 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
479
480 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
481 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700482 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500483 else
484 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 return bg_start + colour;
486}
487
488/**
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000489 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 * @inode: owner
491 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000494 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800495 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500497static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800498 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400501 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503
Mingming Cao617ba132006-10-11 01:20:53 -0700504 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505}
506
507/**
Mingming Cao617ba132006-10-11 01:20:53 -0700508 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 * of direct blocks need to be allocated for the given branch.
510 *
511 * @branch: chain of indirect blocks
512 * @k: number of blocks need for indirect blocks
513 * @blks: number of data blocks to be mapped.
514 * @blocks_to_boundary: the offset in the indirect block
515 *
516 * return the total number of blocks to be allocate, including the
517 * direct and indirect blocks.
518 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500519static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 int blocks_to_boundary)
521{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500522 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523
524 /*
525 * Simple case, [t,d]Indirect block(s) has not allocated yet
526 * then it's clear blocks on that path have not allocated
527 */
528 if (k > 0) {
529 /* right now we don't handle cross boundary allocation */
530 if (blks < blocks_to_boundary + 1)
531 count += blks;
532 else
533 count += blocks_to_boundary + 1;
534 return count;
535 }
536
537 count++;
538 while (count < blks && count <= blocks_to_boundary &&
539 le32_to_cpu(*(branch[0].p + count)) == 0) {
540 count++;
541 }
542 return count;
543}
544
545/**
Mingming Cao617ba132006-10-11 01:20:53 -0700546 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 * @indirect_blks: the number of blocks need to allocate for indirect
548 * blocks
549 *
550 * @new_blocks: on return it will store the new block numbers for
551 * the indirect blocks(if needed) and the first direct block,
552 * @blks: on return it will store the total number of allocated
553 * direct blocks
554 */
Mingming Cao617ba132006-10-11 01:20:53 -0700555static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400556 ext4_lblk_t iblock, ext4_fsblk_t goal,
557 int indirect_blks, int blks,
558 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500560 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400562 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700564 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 int ret = 0;
566
567 /*
568 * Here we try to allocate the requested multiple blocks at once,
569 * on a best-effort basis.
570 * To build a branch, we should allocate blocks for
571 * the indirect blocks(if not allocated yet), and at least
572 * the first direct block of this branch. That's the
573 * minimum number of blocks need to allocate(required)
574 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400575 /* first we try to allocate the indirect blocks */
576 target = indirect_blks;
577 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 count = target;
579 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400580 current_block = ext4_new_meta_blocks(handle, inode,
581 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 if (*err)
583 goto failed_out;
584
585 target -= count;
586 /* allocate blocks for indirect blocks */
587 while (index < indirect_blks && count) {
588 new_blocks[index++] = current_block++;
589 count--;
590 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400591 if (count > 0) {
592 /*
593 * save the new block number
594 * for the first direct block
595 */
596 new_blocks[index] = current_block;
597 printk(KERN_INFO "%s returned more blocks than "
598 "requested\n", __func__);
599 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400601 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700602 }
603
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400604 target = blks - count ;
605 blk_allocated = count;
606 if (!target)
607 goto allocated;
608 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500609 memset(&ar, 0, sizeof(ar));
610 ar.inode = inode;
611 ar.goal = goal;
612 ar.len = target;
613 ar.logical = iblock;
614 if (S_ISREG(inode->i_mode))
615 /* enable in-core preallocation only for regular files */
616 ar.flags = EXT4_MB_HINT_DATA;
617
618 current_block = ext4_mb_new_blocks(handle, &ar, err);
619
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400620 if (*err && (target == blks)) {
621 /*
622 * if the allocation failed and we didn't allocate
623 * any blocks before
624 */
625 goto failed_out;
626 }
627 if (!*err) {
628 if (target == blks) {
629 /*
630 * save the new block number
631 * for the first direct block
632 */
633 new_blocks[index] = current_block;
634 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500635 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400636 }
637allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400639 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 *err = 0;
641 return ret;
642failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400643 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500644 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645 return ret;
646}
647
648/**
Mingming Cao617ba132006-10-11 01:20:53 -0700649 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 * @inode: owner
651 * @indirect_blks: number of allocated indirect blocks
652 * @blks: number of allocated direct blocks
653 * @offsets: offsets (in the blocks) to store the pointers to next.
654 * @branch: place to store the chain in.
655 *
656 * This function allocates blocks, zeroes out all but the last one,
657 * links them into chain and (if we are synchronous) writes them to disk.
658 * In other words, it prepares a branch that can be spliced onto the
659 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700660 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 * we had read the existing part of chain and partial points to the last
662 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700663 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 * place chain is disconnected - *branch->p is still zero (we did not
665 * set the last link), but branch->key contains the number that should
666 * be placed into *branch->p to fill that gap.
667 *
668 * If allocation fails we free all blocks we've allocated (and forget
669 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700670 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 * as described above and return 0.
672 */
Mingming Cao617ba132006-10-11 01:20:53 -0700673static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400674 ext4_lblk_t iblock, int indirect_blks,
675 int *blks, ext4_fsblk_t goal,
676 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677{
678 int blocksize = inode->i_sb->s_blocksize;
679 int i, n = 0;
680 int err = 0;
681 struct buffer_head *bh;
682 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700683 ext4_fsblk_t new_blocks[4];
684 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400686 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 *blks, new_blocks, &err);
688 if (err)
689 return err;
690
691 branch[0].key = cpu_to_le32(new_blocks[0]);
692 /*
693 * metadata blocks and data blocks are allocated.
694 */
695 for (n = 1; n <= indirect_blks; n++) {
696 /*
697 * Get buffer_head for parent block, zero it out
698 * and set the pointer to new one, then send
699 * parent to disk.
700 */
701 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
702 branch[n].bh = bh;
703 lock_buffer(bh);
704 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700705 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 if (err) {
707 unlock_buffer(bh);
708 brelse(bh);
709 goto failed;
710 }
711
712 memset(bh->b_data, 0, blocksize);
713 branch[n].p = (__le32 *) bh->b_data + offsets[n];
714 branch[n].key = cpu_to_le32(new_blocks[n]);
715 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400716 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 current_block = new_blocks[n];
718 /*
719 * End of chain, update the last new metablock of
720 * the chain to point to the new allocated
721 * data blocks numbers
722 */
723 for (i=1; i < num; i++)
724 *(branch[n].p + i) = cpu_to_le32(++current_block);
725 }
726 BUFFER_TRACE(bh, "marking uptodate");
727 set_buffer_uptodate(bh);
728 unlock_buffer(bh);
729
Frank Mayhar03901312009-01-07 00:06:22 -0500730 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
731 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732 if (err)
733 goto failed;
734 }
735 *blks = num;
736 return err;
737failed:
738 /* Allocation failed, free what we already allocated */
739 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700740 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700741 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400743 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500744 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745
Alex Tomasc9de5602008-01-29 00:19:52 -0500746 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747
748 return err;
749}
750
751/**
Mingming Cao617ba132006-10-11 01:20:53 -0700752 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753 * @inode: owner
754 * @block: (logical) number of block we are adding
755 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700756 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757 * @where: location of missing link
758 * @num: number of indirect blocks we are adding
759 * @blks: number of direct blocks we are adding
760 *
761 * This function fills the missing link and does all housekeeping needed in
762 * inode (->i_blocks, etc.). In case of success we end up with the full
763 * chain to new block and return 0.
764 */
Mingming Cao617ba132006-10-11 01:20:53 -0700765static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500766 ext4_lblk_t block, Indirect *where, int num, int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767{
768 int i;
769 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700770 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 /*
773 * If we're splicing into a [td]indirect block (as opposed to the
774 * inode) then we need to get write access to the [td]indirect block
775 * before the splice.
776 */
777 if (where->bh) {
778 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700779 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 if (err)
781 goto err_out;
782 }
783 /* That's it */
784
785 *where->p = where->key;
786
787 /*
788 * Update the host buffer_head or inode to point to more just allocated
789 * direct blocks blocks
790 */
791 if (num == 0 && blks > 1) {
792 current_block = le32_to_cpu(where->key) + 1;
793 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400794 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 }
796
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797 /* We are done with atomic stuff, now do the rest of housekeeping */
798
Kalpak Shahef7f3832007-07-18 09:15:20 -0400799 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700800 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801
802 /* had we spliced it onto indirect block? */
803 if (where->bh) {
804 /*
805 * If we spliced it onto an indirect block, we haven't
806 * altered the inode. Note however that if it is being spliced
807 * onto an indirect block at the very end of the file (the
808 * file is growing) then we *will* alter the inode to reflect
809 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700810 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 */
812 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500813 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
814 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815 if (err)
816 goto err_out;
817 } else {
818 /*
819 * OK, we spliced it into the inode itself on a direct block.
820 * Inode was dirtied above.
821 */
822 jbd_debug(5, "splicing direct\n");
823 }
824 return err;
825
826err_out:
827 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700828 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700829 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500830 ext4_free_blocks(handle, inode,
831 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500833 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834
835 return err;
836}
837
838/*
839 * Allocation strategy is simple: if we have to allocate something, we will
840 * have to go the whole way to leaf. So let's do it before attaching anything
841 * to tree, set linkage between the newborn blocks, write them if sync is
842 * required, recheck the path, free and repeat if check fails, otherwise
843 * set the last missing link (that will protect us from any truncate-generated
844 * removals - all blocks on the path are immune now) and possibly force the
845 * write on the parent block.
846 * That has a nice additional property: no special recovery from the failed
847 * allocations is needed - we simply release blocks and do not touch anything
848 * reachable from inode.
849 *
850 * `handle' can be NULL if create == 0.
851 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852 * return > 0, # of blocks mapped or allocated.
853 * return = 0, if plain lookup failed.
854 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500855 *
856 *
857 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500858 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
859 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500861static int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
862 ext4_lblk_t iblock, unsigned int maxblocks,
863 struct buffer_head *bh_result,
864 int create, int extend_disksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865{
866 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500867 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868 Indirect chain[4];
869 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700870 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 int indirect_blks;
872 int blocks_to_boundary = 0;
873 int depth;
Mingming Cao617ba132006-10-11 01:20:53 -0700874 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700876 ext4_fsblk_t first_block = 0;
Mingming Cao61628a32008-07-11 19:27:31 -0400877 loff_t disksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878
879
Alex Tomasa86c6182006-10-11 01:21:03 -0700880 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 J_ASSERT(handle != NULL || create == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500882 depth = ext4_block_to_path(inode, iblock, offsets,
883 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884
885 if (depth == 0)
886 goto out;
887
Mingming Cao617ba132006-10-11 01:20:53 -0700888 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889
890 /* Simplest case - block found, no allocation needed */
891 if (!partial) {
892 first_block = le32_to_cpu(chain[depth - 1].key);
893 clear_buffer_new(bh_result);
894 count++;
895 /*map more blocks*/
896 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700897 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 blk = le32_to_cpu(*(chain[depth-1].p + count));
900
901 if (blk == first_block + count)
902 count++;
903 else
904 break;
905 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500906 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 }
908
909 /* Next simple case - plain lookup or failed read of indirect block */
910 if (!create || err == -EIO)
911 goto cleanup;
912
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400914 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800916 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917
918 /* the number of blocks need to allocate for [d,t]indirect blocks */
919 indirect_blks = (chain + depth) - partial - 1;
920
921 /*
922 * Next look up the indirect map to count the totoal number of
923 * direct blocks to allocate for this branch.
924 */
Mingming Cao617ba132006-10-11 01:20:53 -0700925 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926 maxblocks, blocks_to_boundary);
927 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700928 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400930 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
931 &count, goal,
932 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933
934 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700935 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 * on the new chain if there is a failure, but that risks using
937 * up transaction credits, especially for bitmaps where the
938 * credits cannot be returned. Can we handle this somehow? We
939 * may need to return -EAGAIN upwards in the worst case. --sct
940 */
941 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -0700942 err = ext4_splice_branch(handle, inode, iblock,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 partial, indirect_blks, count);
944 /*
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500945 * i_disksize growing is protected by i_data_sem. Don't forget to
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 * protect it if you're about to implement concurrent
Mingming Cao617ba132006-10-11 01:20:53 -0700947 * ext4_get_block() -bzzz
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 */
Mingming Cao61628a32008-07-11 19:27:31 -0400949 if (!err && extend_disksize) {
950 disksize = ((loff_t) iblock + count) << inode->i_blkbits;
951 if (disksize > i_size_read(inode))
952 disksize = i_size_read(inode);
953 if (disksize > ei->i_disksize)
954 ei->i_disksize = disksize;
955 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956 if (err)
957 goto cleanup;
958
959 set_buffer_new(bh_result);
960got_it:
961 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
962 if (count > blocks_to_boundary)
963 set_buffer_boundary(bh_result);
964 err = count;
965 /* Clean up and exit */
966 partial = chain + depth - 1; /* the whole chain */
967cleanup:
968 while (partial > chain) {
969 BUFFER_TRACE(partial->bh, "call brelse");
970 brelse(partial->bh);
971 partial--;
972 }
973 BUFFER_TRACE(bh_result, "returned");
974out:
975 return err;
976}
977
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400978/*
979 * Calculate the number of metadata blocks need to reserve
980 * to allocate @blocks for non extent file based file
981 */
982static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
983{
984 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
985 int ind_blks, dind_blks, tind_blks;
986
987 /* number of new indirect blocks needed */
988 ind_blks = (blocks + icap - 1) / icap;
989
990 dind_blks = (ind_blks + icap - 1) / icap;
991
992 tind_blks = 1;
993
994 return ind_blks + dind_blks + tind_blks;
995}
996
997/*
998 * Calculate the number of metadata blocks need to reserve
999 * to allocate given number of blocks
1000 */
1001static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1002{
Mingming Caocd213222008-08-19 22:16:59 -04001003 if (!blocks)
1004 return 0;
1005
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001006 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1007 return ext4_ext_calc_metadata_amount(inode, blocks);
1008
1009 return ext4_indirect_calc_metadata_amount(inode, blocks);
1010}
1011
1012static void ext4_da_update_reserve_space(struct inode *inode, int used)
1013{
1014 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1015 int total, mdb, mdb_free;
1016
1017 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1018 /* recalculate the number of metablocks still need to be reserved */
1019 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1020 mdb = ext4_calc_metadata_amount(inode, total);
1021
1022 /* figure out how many metablocks to release */
1023 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1024 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1025
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001026 if (mdb_free) {
1027 /* Account for allocated meta_blocks */
1028 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001029
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001030 /* update fs dirty blocks counter */
1031 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1032 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1033 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1034 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001035
1036 /* update per-inode reservations */
1037 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1038 EXT4_I(inode)->i_reserved_data_blocks -= used;
1039
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001040 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1041}
1042
Mingming Caof5ab0d12008-02-25 15:29:55 -05001043/*
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001044 * The ext4_get_blocks_wrap() function try to look up the requested blocks,
1045 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001046 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001047 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1048 * and store the allocated blocks in the result buffer head and mark it
1049 * mapped.
1050 *
1051 * If file type is extents based, it will call ext4_ext_get_blocks(),
1052 * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping
1053 * based files
1054 *
1055 * On success, it returns the number of blocks being mapped or allocate.
1056 * if create==0 and the blocks are pre-allocated and uninitialized block,
1057 * the result buffer head is unmapped. If the create ==1, it will make sure
1058 * the buffer head is mapped.
1059 *
1060 * It returns 0 if plain look up failed (blocks have not been allocated), in
1061 * that casem, buffer head is unmapped
1062 *
1063 * It returns the error in case of allocation failure.
1064 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001065int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001066 unsigned int max_blocks, struct buffer_head *bh,
Mingming Caod2a17632008-07-14 17:52:37 -04001067 int create, int extend_disksize, int flag)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001068{
1069 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001070
1071 clear_buffer_mapped(bh);
1072
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001073 /*
1074 * Try to see if we can get the block without requesting
1075 * for new file system block.
1076 */
1077 down_read((&EXT4_I(inode)->i_data_sem));
1078 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1079 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
1080 bh, 0, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001081 } else {
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001082 retval = ext4_get_blocks_handle(handle,
1083 inode, block, max_blocks, bh, 0, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001084 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001085 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001086
1087 /* If it is only a block(s) look up */
1088 if (!create)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001089 return retval;
1090
1091 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001092 * Returns if the blocks have already allocated
1093 *
1094 * Note that if blocks have been preallocated
1095 * ext4_ext_get_block() returns th create = 0
1096 * with buffer head unmapped.
1097 */
1098 if (retval > 0 && buffer_mapped(bh))
1099 return retval;
1100
1101 /*
1102 * New blocks allocate and/or writing to uninitialized extent
1103 * will possibly result in updating i_data, so we take
1104 * the write lock of i_data_sem, and call get_blocks()
1105 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001106 */
1107 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001108
1109 /*
1110 * if the caller is from delayed allocation writeout path
1111 * we have already reserved fs blocks for allocation
1112 * let the underlying get_block() function know to
1113 * avoid double accounting
1114 */
1115 if (flag)
1116 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001117 /*
1118 * We need to check for EXT4 here because migrate
1119 * could have changed the inode type in between
1120 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001121 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1122 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
1123 bh, create, extend_disksize);
1124 } else {
1125 retval = ext4_get_blocks_handle(handle, inode, block,
1126 max_blocks, bh, create, extend_disksize);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001127
1128 if (retval > 0 && buffer_new(bh)) {
1129 /*
1130 * We allocated new blocks which will result in
1131 * i_data's format changing. Force the migrate
1132 * to fail by clearing migrate flags
1133 */
1134 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1135 ~EXT4_EXT_MIGRATE;
1136 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001137 }
Mingming Caod2a17632008-07-14 17:52:37 -04001138
1139 if (flag) {
1140 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
1141 /*
1142 * Update reserved blocks/metadata blocks
1143 * after successful block allocation
1144 * which were deferred till now
1145 */
1146 if ((retval > 0) && buffer_delay(bh))
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001147 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001148 }
1149
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001150 up_write((&EXT4_I(inode)->i_data_sem));
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001151 return retval;
1152}
1153
Mingming Caof3bd1f32008-08-19 22:16:03 -04001154/* Maximum number of blocks we map for direct IO at once. */
1155#define DIO_MAX_BLOCKS 4096
1156
Eric Sandeen6873fa02008-10-07 00:46:36 -04001157int ext4_get_block(struct inode *inode, sector_t iblock,
1158 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001160 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001161 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001163 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164
Jan Kara7fb54092008-02-10 01:08:38 -05001165 if (create && !handle) {
1166 /* Direct IO write... */
1167 if (max_blocks > DIO_MAX_BLOCKS)
1168 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001169 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1170 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001171 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001173 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 }
Jan Kara7fb54092008-02-10 01:08:38 -05001175 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 }
1177
Jan Kara7fb54092008-02-10 01:08:38 -05001178 ret = ext4_get_blocks_wrap(handle, inode, iblock,
Mingming Caod2a17632008-07-14 17:52:37 -04001179 max_blocks, bh_result, create, 0, 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001180 if (ret > 0) {
1181 bh_result->b_size = (ret << inode->i_blkbits);
1182 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 }
Jan Kara7fb54092008-02-10 01:08:38 -05001184 if (started)
1185 ext4_journal_stop(handle);
1186out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001187 return ret;
1188}
1189
1190/*
1191 * `handle' can be NULL if create is zero
1192 */
Mingming Cao617ba132006-10-11 01:20:53 -07001193struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001194 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001195{
1196 struct buffer_head dummy;
1197 int fatal = 0, err;
1198
1199 J_ASSERT(handle != NULL || create == 0);
1200
1201 dummy.b_state = 0;
1202 dummy.b_blocknr = -1000;
1203 buffer_trace_init(&dummy.b_history);
Alex Tomasa86c6182006-10-11 01:21:03 -07001204 err = ext4_get_blocks_wrap(handle, inode, block, 1,
Mingming Caod2a17632008-07-14 17:52:37 -04001205 &dummy, create, 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001207 * ext4_get_blocks_handle() returns number of blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 * mapped. 0 in case of a HOLE.
1209 */
1210 if (err > 0) {
1211 if (err > 1)
1212 WARN_ON(1);
1213 err = 0;
1214 }
1215 *errp = err;
1216 if (!err && buffer_mapped(&dummy)) {
1217 struct buffer_head *bh;
1218 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1219 if (!bh) {
1220 *errp = -EIO;
1221 goto err;
1222 }
1223 if (buffer_new(&dummy)) {
1224 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001225 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226
1227 /*
1228 * Now that we do not always journal data, we should
1229 * keep in mind whether this should always journal the
1230 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001231 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 * problem.
1233 */
1234 lock_buffer(bh);
1235 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001236 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001238 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 set_buffer_uptodate(bh);
1240 }
1241 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001242 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1243 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001244 if (!fatal)
1245 fatal = err;
1246 } else {
1247 BUFFER_TRACE(bh, "not a new buffer");
1248 }
1249 if (fatal) {
1250 *errp = fatal;
1251 brelse(bh);
1252 bh = NULL;
1253 }
1254 return bh;
1255 }
1256err:
1257 return NULL;
1258}
1259
Mingming Cao617ba132006-10-11 01:20:53 -07001260struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001261 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001263 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264
Mingming Cao617ba132006-10-11 01:20:53 -07001265 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 if (!bh)
1267 return bh;
1268 if (buffer_uptodate(bh))
1269 return bh;
1270 ll_rw_block(READ_META, 1, &bh);
1271 wait_on_buffer(bh);
1272 if (buffer_uptodate(bh))
1273 return bh;
1274 put_bh(bh);
1275 *err = -EIO;
1276 return NULL;
1277}
1278
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001279static int walk_page_buffers(handle_t *handle,
1280 struct buffer_head *head,
1281 unsigned from,
1282 unsigned to,
1283 int *partial,
1284 int (*fn)(handle_t *handle,
1285 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286{
1287 struct buffer_head *bh;
1288 unsigned block_start, block_end;
1289 unsigned blocksize = head->b_size;
1290 int err, ret = 0;
1291 struct buffer_head *next;
1292
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001293 for (bh = head, block_start = 0;
1294 ret == 0 && (bh != head || !block_start);
1295 block_start = block_end, bh = next)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 {
1297 next = bh->b_this_page;
1298 block_end = block_start + blocksize;
1299 if (block_end <= from || block_start >= to) {
1300 if (partial && !buffer_uptodate(bh))
1301 *partial = 1;
1302 continue;
1303 }
1304 err = (*fn)(handle, bh);
1305 if (!ret)
1306 ret = err;
1307 }
1308 return ret;
1309}
1310
1311/*
1312 * To preserve ordering, it is essential that the hole instantiation and
1313 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001314 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001315 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 * prepare_write() is the right place.
1317 *
Mingming Cao617ba132006-10-11 01:20:53 -07001318 * Also, this function can nest inside ext4_writepage() ->
1319 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320 * has generated enough buffer credits to do the whole page. So we won't
1321 * block on the journal in that case, which is good, because the caller may
1322 * be PF_MEMALLOC.
1323 *
Mingming Cao617ba132006-10-11 01:20:53 -07001324 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001325 * quota file writes. If we were to commit the transaction while thus
1326 * reentered, there can be a deadlock - we would be holding a quota
1327 * lock, and the commit would never complete if another thread had a
1328 * transaction open and was blocking on the quota lock - a ranking
1329 * violation.
1330 *
Mingming Caodab291a2006-10-11 01:21:01 -07001331 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001332 * will _not_ run commit under these circumstances because handle->h_ref
1333 * is elevated. We'll still have enough credits for the tiny quotafile
1334 * write.
1335 */
1336static int do_journal_get_write_access(handle_t *handle,
1337 struct buffer_head *bh)
1338{
1339 if (!buffer_mapped(bh) || buffer_freed(bh))
1340 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001341 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342}
1343
Nick Pigginbfc1af62007-10-16 01:25:05 -07001344static int ext4_write_begin(struct file *file, struct address_space *mapping,
1345 loff_t pos, unsigned len, unsigned flags,
1346 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001348 struct inode *inode = mapping->host;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001349 int ret, needed_blocks = ext4_writepage_trans_blocks(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 handle_t *handle;
1351 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001352 struct page *page;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001353 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001354 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001355
Theodore Ts'oba80b102009-01-03 20:03:21 -05001356 trace_mark(ext4_write_begin,
1357 "dev %s ino %lu pos %llu len %u flags %u",
1358 inode->i_sb->s_id, inode->i_ino,
1359 (unsigned long long) pos, len, flags);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001360 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001361 from = pos & (PAGE_CACHE_SIZE - 1);
1362 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001363
1364retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001365 handle = ext4_journal_start(inode, needed_blocks);
1366 if (IS_ERR(handle)) {
1367 ret = PTR_ERR(handle);
1368 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001369 }
1370
Nick Piggin54566b22009-01-04 12:00:53 -08001371 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001372 if (!page) {
1373 ext4_journal_stop(handle);
1374 ret = -ENOMEM;
1375 goto out;
1376 }
1377 *pagep = page;
1378
Nick Pigginbfc1af62007-10-16 01:25:05 -07001379 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1380 ext4_get_block);
1381
1382 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 ret = walk_page_buffers(handle, page_buffers(page),
1384 from, to, NULL, do_journal_get_write_access);
1385 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001386
1387 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001388 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001389 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001390 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001391 /*
1392 * block_write_begin may have instantiated a few blocks
1393 * outside i_size. Trim these off again. Don't need
1394 * i_size_read because we hold i_mutex.
1395 */
1396 if (pos + len > inode->i_size)
1397 vmtruncate(inode, inode->i_size);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001398 }
1399
Mingming Cao617ba132006-10-11 01:20:53 -07001400 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001402out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 return ret;
1404}
1405
Nick Pigginbfc1af62007-10-16 01:25:05 -07001406/* For write_end() in data=journal mode */
1407static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408{
1409 if (!buffer_mapped(bh) || buffer_freed(bh))
1410 return 0;
1411 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001412 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413}
1414
1415/*
1416 * We need to pick up the new inode size which generic_commit_write gave us
1417 * `file' can be NULL - eg, when called from page_symlink().
1418 *
Mingming Cao617ba132006-10-11 01:20:53 -07001419 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 * buffers are managed internally.
1421 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001422static int ext4_ordered_write_end(struct file *file,
1423 struct address_space *mapping,
1424 loff_t pos, unsigned len, unsigned copied,
1425 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001426{
Mingming Cao617ba132006-10-11 01:20:53 -07001427 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001428 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429 int ret = 0, ret2;
1430
Theodore Ts'oba80b102009-01-03 20:03:21 -05001431 trace_mark(ext4_ordered_write_end,
1432 "dev %s ino %lu pos %llu len %u copied %u",
1433 inode->i_sb->s_id, inode->i_ino,
1434 (unsigned long long) pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001435 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436
1437 if (ret == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 loff_t new_i_size;
1439
Nick Pigginbfc1af62007-10-16 01:25:05 -07001440 new_i_size = pos + copied;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001441 if (new_i_size > EXT4_I(inode)->i_disksize) {
1442 ext4_update_i_disksize(inode, new_i_size);
1443 /* We need to mark inode dirty even if
1444 * new_i_size is less that inode->i_size
1445 * bu greater than i_disksize.(hint delalloc)
1446 */
1447 ext4_mark_inode_dirty(handle, inode);
1448 }
1449
Jan Karacf108bc2008-07-11 19:27:31 -04001450 ret2 = generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001451 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001452 copied = ret2;
1453 if (ret2 < 0)
1454 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455 }
Mingming Cao617ba132006-10-11 01:20:53 -07001456 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001457 if (!ret)
1458 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001459
1460 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461}
1462
Nick Pigginbfc1af62007-10-16 01:25:05 -07001463static int ext4_writeback_write_end(struct file *file,
1464 struct address_space *mapping,
1465 loff_t pos, unsigned len, unsigned copied,
1466 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467{
Mingming Cao617ba132006-10-11 01:20:53 -07001468 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001469 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001470 int ret = 0, ret2;
1471 loff_t new_i_size;
1472
Theodore Ts'oba80b102009-01-03 20:03:21 -05001473 trace_mark(ext4_writeback_write_end,
1474 "dev %s ino %lu pos %llu len %u copied %u",
1475 inode->i_sb->s_id, inode->i_ino,
1476 (unsigned long long) pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001477 new_i_size = pos + copied;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001478 if (new_i_size > EXT4_I(inode)->i_disksize) {
1479 ext4_update_i_disksize(inode, new_i_size);
1480 /* We need to mark inode dirty even if
1481 * new_i_size is less that inode->i_size
1482 * bu greater than i_disksize.(hint delalloc)
1483 */
1484 ext4_mark_inode_dirty(handle, inode);
1485 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486
Jan Karacf108bc2008-07-11 19:27:31 -04001487 ret2 = generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001488 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001489 copied = ret2;
1490 if (ret2 < 0)
1491 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492
Mingming Cao617ba132006-10-11 01:20:53 -07001493 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494 if (!ret)
1495 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001496
1497 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498}
1499
Nick Pigginbfc1af62007-10-16 01:25:05 -07001500static int ext4_journalled_write_end(struct file *file,
1501 struct address_space *mapping,
1502 loff_t pos, unsigned len, unsigned copied,
1503 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504{
Mingming Cao617ba132006-10-11 01:20:53 -07001505 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001506 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 int ret = 0, ret2;
1508 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001509 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001510 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511
Theodore Ts'oba80b102009-01-03 20:03:21 -05001512 trace_mark(ext4_journalled_write_end,
1513 "dev %s ino %lu pos %llu len %u copied %u",
1514 inode->i_sb->s_id, inode->i_ino,
1515 (unsigned long long) pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001516 from = pos & (PAGE_CACHE_SIZE - 1);
1517 to = from + len;
1518
1519 if (copied < len) {
1520 if (!PageUptodate(page))
1521 copied = 0;
1522 page_zero_new_buffers(page, from+copied, to);
1523 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524
1525 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001526 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001527 if (!partial)
1528 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001529 new_i_size = pos + copied;
1530 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001531 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001532 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001533 if (new_i_size > EXT4_I(inode)->i_disksize) {
1534 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001535 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 if (!ret)
1537 ret = ret2;
1538 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001539
Jan Karacf108bc2008-07-11 19:27:31 -04001540 unlock_page(page);
Mingming Cao617ba132006-10-11 01:20:53 -07001541 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001542 if (!ret)
1543 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001544 page_cache_release(page);
1545
1546 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547}
Mingming Caod2a17632008-07-14 17:52:37 -04001548
1549static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1550{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001551 int retries = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001552 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1553 unsigned long md_needed, mdblocks, total = 0;
1554
1555 /*
1556 * recalculate the amount of metadata blocks to reserve
1557 * in order to allocate nrblocks
1558 * worse case is one extent per block
1559 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001560repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001561 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1562 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1563 mdblocks = ext4_calc_metadata_amount(inode, total);
1564 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1565
1566 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1567 total = md_needed + nrblocks;
1568
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001569 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001570 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001571 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1572 yield();
1573 goto repeat;
1574 }
Mingming Caod2a17632008-07-14 17:52:37 -04001575 return -ENOSPC;
1576 }
Mingming Caod2a17632008-07-14 17:52:37 -04001577 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1578 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1579
1580 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1581 return 0; /* success */
1582}
1583
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001584static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001585{
1586 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1587 int total, mdb, mdb_free, release;
1588
Mingming Caocd213222008-08-19 22:16:59 -04001589 if (!to_free)
1590 return; /* Nothing to release, exit */
1591
Mingming Caod2a17632008-07-14 17:52:37 -04001592 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001593
1594 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1595 /*
1596 * if there is no reserved blocks, but we try to free some
1597 * then the counter is messed up somewhere.
1598 * but since this function is called from invalidate
1599 * page, it's harmless to return without any action
1600 */
1601 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1602 "blocks for inode %lu, but there is no reserved "
1603 "data blocks\n", to_free, inode->i_ino);
1604 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1605 return;
1606 }
1607
Mingming Caod2a17632008-07-14 17:52:37 -04001608 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001609 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001610 mdb = ext4_calc_metadata_amount(inode, total);
1611
1612 /* figure out how many metablocks to release */
1613 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1614 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1615
Mingming Caod2a17632008-07-14 17:52:37 -04001616 release = to_free + mdb_free;
1617
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001618 /* update fs dirty blocks counter for truncate case */
1619 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001620
1621 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001622 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1623 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001624
1625 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1626 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001627 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1628}
1629
1630static void ext4_da_page_release_reservation(struct page *page,
1631 unsigned long offset)
1632{
1633 int to_release = 0;
1634 struct buffer_head *head, *bh;
1635 unsigned int curr_off = 0;
1636
1637 head = page_buffers(page);
1638 bh = head;
1639 do {
1640 unsigned int next_off = curr_off + bh->b_size;
1641
1642 if ((offset <= curr_off) && (buffer_delay(bh))) {
1643 to_release++;
1644 clear_buffer_delay(bh);
1645 }
1646 curr_off = next_off;
1647 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001648 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001649}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650
1651/*
Alex Tomas64769242008-07-11 19:27:31 -04001652 * Delayed allocation stuff
1653 */
1654
1655struct mpage_da_data {
1656 struct inode *inode;
1657 struct buffer_head lbh; /* extent of blocks */
1658 unsigned long first_page, next_page; /* extent of pages */
1659 get_block_t *get_block;
1660 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001661 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001662 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001663 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001664};
1665
1666/*
1667 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001668 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001669 *
1670 * @mpd->inode: inode
1671 * @mpd->first_page: first page of the extent
1672 * @mpd->next_page: page after the last page of the extent
1673 * @mpd->get_block: the filesystem's block mapper function
1674 *
1675 * By the time mpage_da_submit_io() is called we expect all blocks
1676 * to be allocated. this may be wrong if allocation failed.
1677 *
1678 * As pages are already locked by write_cache_pages(), we can't use it
1679 */
1680static int mpage_da_submit_io(struct mpage_da_data *mpd)
1681{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001682 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001683 struct pagevec pvec;
1684 unsigned long index, end;
1685 int ret = 0, err, nr_pages, i;
1686 struct inode *inode = mpd->inode;
1687 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001688
1689 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001690 /*
1691 * We need to start from the first_page to the next_page - 1
1692 * to make sure we also write the mapped dirty buffer_heads.
1693 * If we look at mpd->lbh.b_blocknr we would only be looking
1694 * at the currently mapped buffer_heads.
1695 */
Alex Tomas64769242008-07-11 19:27:31 -04001696 index = mpd->first_page;
1697 end = mpd->next_page - 1;
1698
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001699 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001700 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001701 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001702 if (nr_pages == 0)
1703 break;
1704 for (i = 0; i < nr_pages; i++) {
1705 struct page *page = pvec.pages[i];
1706
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001707 index = page->index;
1708 if (index > end)
1709 break;
1710 index++;
1711
1712 BUG_ON(!PageLocked(page));
1713 BUG_ON(PageWriteback(page));
1714
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001715 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001716 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001717 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1718 /*
1719 * have successfully written the page
1720 * without skipping the same
1721 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001722 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001723 /*
1724 * In error case, we have to continue because
1725 * remaining pages are still locked
1726 * XXX: unlock and re-dirty them?
1727 */
1728 if (ret == 0)
1729 ret = err;
1730 }
1731 pagevec_release(&pvec);
1732 }
Alex Tomas64769242008-07-11 19:27:31 -04001733 return ret;
1734}
1735
1736/*
1737 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1738 *
1739 * @mpd->inode - inode to walk through
1740 * @exbh->b_blocknr - first block on a disk
1741 * @exbh->b_size - amount of space in bytes
1742 * @logical - first logical block to start assignment with
1743 *
1744 * the function goes through all passed space and put actual disk
1745 * block numbers into buffer heads, dropping BH_Delay
1746 */
1747static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1748 struct buffer_head *exbh)
1749{
1750 struct inode *inode = mpd->inode;
1751 struct address_space *mapping = inode->i_mapping;
1752 int blocks = exbh->b_size >> inode->i_blkbits;
1753 sector_t pblock = exbh->b_blocknr, cur_logical;
1754 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001755 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001756 struct pagevec pvec;
1757 int nr_pages, i;
1758
1759 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1760 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1761 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1762
1763 pagevec_init(&pvec, 0);
1764
1765 while (index <= end) {
1766 /* XXX: optimize tail */
1767 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1768 if (nr_pages == 0)
1769 break;
1770 for (i = 0; i < nr_pages; i++) {
1771 struct page *page = pvec.pages[i];
1772
1773 index = page->index;
1774 if (index > end)
1775 break;
1776 index++;
1777
1778 BUG_ON(!PageLocked(page));
1779 BUG_ON(PageWriteback(page));
1780 BUG_ON(!page_has_buffers(page));
1781
1782 bh = page_buffers(page);
1783 head = bh;
1784
1785 /* skip blocks out of the range */
1786 do {
1787 if (cur_logical >= logical)
1788 break;
1789 cur_logical++;
1790 } while ((bh = bh->b_this_page) != head);
1791
1792 do {
1793 if (cur_logical >= logical + blocks)
1794 break;
Alex Tomas64769242008-07-11 19:27:31 -04001795 if (buffer_delay(bh)) {
1796 bh->b_blocknr = pblock;
1797 clear_buffer_delay(bh);
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001798 bh->b_bdev = inode->i_sb->s_bdev;
1799 } else if (buffer_unwritten(bh)) {
1800 bh->b_blocknr = pblock;
1801 clear_buffer_unwritten(bh);
1802 set_buffer_mapped(bh);
1803 set_buffer_new(bh);
1804 bh->b_bdev = inode->i_sb->s_bdev;
Mingming Cao61628a32008-07-11 19:27:31 -04001805 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04001806 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04001807
1808 cur_logical++;
1809 pblock++;
1810 } while ((bh = bh->b_this_page) != head);
1811 }
1812 pagevec_release(&pvec);
1813 }
1814}
1815
1816
1817/*
1818 * __unmap_underlying_blocks - just a helper function to unmap
1819 * set of blocks described by @bh
1820 */
1821static inline void __unmap_underlying_blocks(struct inode *inode,
1822 struct buffer_head *bh)
1823{
1824 struct block_device *bdev = inode->i_sb->s_bdev;
1825 int blocks, i;
1826
1827 blocks = bh->b_size >> inode->i_blkbits;
1828 for (i = 0; i < blocks; i++)
1829 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
1830}
1831
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001832static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
1833 sector_t logical, long blk_cnt)
1834{
1835 int nr_pages, i;
1836 pgoff_t index, end;
1837 struct pagevec pvec;
1838 struct inode *inode = mpd->inode;
1839 struct address_space *mapping = inode->i_mapping;
1840
1841 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1842 end = (logical + blk_cnt - 1) >>
1843 (PAGE_CACHE_SHIFT - inode->i_blkbits);
1844 while (index <= end) {
1845 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1846 if (nr_pages == 0)
1847 break;
1848 for (i = 0; i < nr_pages; i++) {
1849 struct page *page = pvec.pages[i];
1850 index = page->index;
1851 if (index > end)
1852 break;
1853 index++;
1854
1855 BUG_ON(!PageLocked(page));
1856 BUG_ON(PageWriteback(page));
1857 block_invalidatepage(page, 0);
1858 ClearPageUptodate(page);
1859 unlock_page(page);
1860 }
1861 }
1862 return;
1863}
1864
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001865static void ext4_print_free_blocks(struct inode *inode)
1866{
1867 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1868 printk(KERN_EMERG "Total free blocks count %lld\n",
1869 ext4_count_free_blocks(inode->i_sb));
1870 printk(KERN_EMERG "Free/Dirty block details\n");
1871 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04001872 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001873 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04001874 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001875 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001876 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001877 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001878 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001879 EXT4_I(inode)->i_reserved_meta_blocks);
1880 return;
1881}
1882
Alex Tomas64769242008-07-11 19:27:31 -04001883/*
1884 * mpage_da_map_blocks - go through given space
1885 *
1886 * @mpd->lbh - bh describing space
1887 * @mpd->get_block - the filesystem's block mapper function
1888 *
1889 * The function skips space we know is already mapped to disk blocks.
1890 *
Alex Tomas64769242008-07-11 19:27:31 -04001891 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001892static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04001893{
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001894 int err = 0;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001895 struct buffer_head new;
Alex Tomas64769242008-07-11 19:27:31 -04001896 struct buffer_head *lbh = &mpd->lbh;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001897 sector_t next;
Alex Tomas64769242008-07-11 19:27:31 -04001898
1899 /*
1900 * We consider only non-mapped and non-allocated blocks
1901 */
1902 if (buffer_mapped(lbh) && !buffer_delay(lbh))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001903 return 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001904 new.b_state = lbh->b_state;
1905 new.b_blocknr = 0;
1906 new.b_size = lbh->b_size;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001907 next = lbh->b_blocknr;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001908 /*
1909 * If we didn't accumulate anything
1910 * to write simply return
1911 */
1912 if (!new.b_size)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001913 return 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001914 err = mpd->get_block(mpd->inode, next, &new, 1);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001915 if (err) {
1916
1917 /* If get block returns with error
1918 * we simply return. Later writepage
1919 * will redirty the page and writepages
1920 * will find the dirty page again
1921 */
1922 if (err == -EAGAIN)
1923 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001924
1925 if (err == -ENOSPC &&
1926 ext4_count_free_blocks(mpd->inode->i_sb)) {
1927 mpd->retval = err;
1928 return 0;
1929 }
1930
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001931 /*
1932 * get block failure will cause us
1933 * to loop in writepages. Because
1934 * a_ops->writepage won't be able to
1935 * make progress. The page will be redirtied
1936 * by writepage and writepages will again
1937 * try to write the same.
1938 */
1939 printk(KERN_EMERG "%s block allocation failed for inode %lu "
1940 "at logical offset %llu with max blocks "
1941 "%zd with error %d\n",
1942 __func__, mpd->inode->i_ino,
1943 (unsigned long long)next,
1944 lbh->b_size >> mpd->inode->i_blkbits, err);
1945 printk(KERN_EMERG "This should not happen.!! "
1946 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001947 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001948 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001949 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001950 /* invlaidate all the pages */
1951 ext4_da_block_invalidatepages(mpd, next,
1952 lbh->b_size >> mpd->inode->i_blkbits);
1953 return err;
1954 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001955 BUG_ON(new.b_size == 0);
Alex Tomas64769242008-07-11 19:27:31 -04001956
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001957 if (buffer_new(&new))
1958 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04001959
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001960 /*
1961 * If blocks are delayed marked, we need to
1962 * put actual blocknr and drop delayed bit
1963 */
1964 if (buffer_delay(lbh) || buffer_unwritten(lbh))
1965 mpage_put_bnr_to_bhs(mpd, next, &new);
1966
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001967 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001968}
1969
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001970#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1971 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04001972
1973/*
1974 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1975 *
1976 * @mpd->lbh - extent of blocks
1977 * @logical - logical number of the block in the file
1978 * @bh - bh of the block (used to access block's state)
1979 *
1980 * the function is used to collect contig. blocks in same state
1981 */
1982static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
1983 sector_t logical, struct buffer_head *bh)
1984{
Alex Tomas64769242008-07-11 19:27:31 -04001985 sector_t next;
Mingming Cao525f4ed2008-08-19 22:15:58 -04001986 size_t b_size = bh->b_size;
1987 struct buffer_head *lbh = &mpd->lbh;
1988 int nrblocks = lbh->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04001989
Mingming Cao525f4ed2008-08-19 22:15:58 -04001990 /* check if thereserved journal credits might overflow */
1991 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
1992 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1993 /*
1994 * With non-extent format we are limited by the journal
1995 * credit available. Total credit needed to insert
1996 * nrblocks contiguous blocks is dependent on the
1997 * nrblocks. So limit nrblocks.
1998 */
1999 goto flush_it;
2000 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2001 EXT4_MAX_TRANS_DATA) {
2002 /*
2003 * Adding the new buffer_head would make it cross the
2004 * allowed limit for which we have journal credit
2005 * reserved. So limit the new bh->b_size
2006 */
2007 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2008 mpd->inode->i_blkbits;
2009 /* we will do mpage_da_submit_io in the next loop */
2010 }
2011 }
Alex Tomas64769242008-07-11 19:27:31 -04002012 /*
2013 * First block in the extent
2014 */
2015 if (lbh->b_size == 0) {
2016 lbh->b_blocknr = logical;
Mingming Cao525f4ed2008-08-19 22:15:58 -04002017 lbh->b_size = b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002018 lbh->b_state = bh->b_state & BH_FLAGS;
2019 return;
2020 }
2021
Mingming Cao525f4ed2008-08-19 22:15:58 -04002022 next = lbh->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002023 /*
2024 * Can we merge the block to our big extent?
2025 */
2026 if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04002027 lbh->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002028 return;
2029 }
2030
Mingming Cao525f4ed2008-08-19 22:15:58 -04002031flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002032 /*
2033 * We couldn't merge the block to our extent, so we
2034 * need to flush current extent and start new one
2035 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002036 if (mpage_da_map_blocks(mpd) == 0)
2037 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002038 mpd->io_done = 1;
2039 return;
Alex Tomas64769242008-07-11 19:27:31 -04002040}
2041
2042/*
2043 * __mpage_da_writepage - finds extent of pages and blocks
2044 *
2045 * @page: page to consider
2046 * @wbc: not used, we just follow rules
2047 * @data: context
2048 *
2049 * The function finds extents of pages and scan them for all blocks.
2050 */
2051static int __mpage_da_writepage(struct page *page,
2052 struct writeback_control *wbc, void *data)
2053{
2054 struct mpage_da_data *mpd = data;
2055 struct inode *inode = mpd->inode;
2056 struct buffer_head *bh, *head, fake;
2057 sector_t logical;
2058
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002059 if (mpd->io_done) {
2060 /*
2061 * Rest of the page in the page_vec
2062 * redirty then and skip then. We will
2063 * try to to write them again after
2064 * starting a new transaction
2065 */
2066 redirty_page_for_writepage(wbc, page);
2067 unlock_page(page);
2068 return MPAGE_DA_EXTENT_TAIL;
2069 }
Alex Tomas64769242008-07-11 19:27:31 -04002070 /*
2071 * Can we merge this page to current extent?
2072 */
2073 if (mpd->next_page != page->index) {
2074 /*
2075 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002076 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002077 */
2078 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002079 if (mpage_da_map_blocks(mpd) == 0)
2080 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002081 /*
2082 * skip rest of the page in the page_vec
2083 */
2084 mpd->io_done = 1;
2085 redirty_page_for_writepage(wbc, page);
2086 unlock_page(page);
2087 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002088 }
2089
2090 /*
2091 * Start next extent of pages ...
2092 */
2093 mpd->first_page = page->index;
2094
2095 /*
2096 * ... and blocks
2097 */
2098 mpd->lbh.b_size = 0;
2099 mpd->lbh.b_state = 0;
2100 mpd->lbh.b_blocknr = 0;
2101 }
2102
2103 mpd->next_page = page->index + 1;
2104 logical = (sector_t) page->index <<
2105 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2106
2107 if (!page_has_buffers(page)) {
2108 /*
2109 * There is no attached buffer heads yet (mmap?)
2110 * we treat the page asfull of dirty blocks
2111 */
2112 bh = &fake;
2113 bh->b_size = PAGE_CACHE_SIZE;
2114 bh->b_state = 0;
2115 set_buffer_dirty(bh);
2116 set_buffer_uptodate(bh);
2117 mpage_add_bh_to_extent(mpd, logical, bh);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002118 if (mpd->io_done)
2119 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002120 } else {
2121 /*
2122 * Page with regular buffer heads, just add all dirty ones
2123 */
2124 head = page_buffers(page);
2125 bh = head;
2126 do {
2127 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002128 /*
2129 * We need to try to allocate
2130 * unmapped blocks in the same page.
2131 * Otherwise we won't make progress
2132 * with the page in ext4_da_writepage
2133 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002134 if (buffer_dirty(bh) &&
2135 (!buffer_mapped(bh) || buffer_delay(bh))) {
Alex Tomas64769242008-07-11 19:27:31 -04002136 mpage_add_bh_to_extent(mpd, logical, bh);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002137 if (mpd->io_done)
2138 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002139 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2140 /*
2141 * mapped dirty buffer. We need to update
2142 * the b_state because we look at
2143 * b_state in mpage_da_map_blocks. We don't
2144 * update b_size because if we find an
2145 * unmapped buffer_head later we need to
2146 * use the b_state flag of that buffer_head.
2147 */
2148 if (mpd->lbh.b_size == 0)
2149 mpd->lbh.b_state =
2150 bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002151 }
Alex Tomas64769242008-07-11 19:27:31 -04002152 logical++;
2153 } while ((bh = bh->b_this_page) != head);
2154 }
2155
2156 return 0;
2157}
2158
2159/*
2160 * mpage_da_writepages - walk the list of dirty pages of the given
2161 * address space, allocates non-allocated blocks, maps newly-allocated
2162 * blocks to existing bhs and issue IO them
2163 *
2164 * @mapping: address space structure to write
2165 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2166 * @get_block: the filesystem's block mapper function.
2167 *
2168 * This is a library function, which implements the writepages()
2169 * address_space_operation.
Alex Tomas64769242008-07-11 19:27:31 -04002170 */
2171static int mpage_da_writepages(struct address_space *mapping,
2172 struct writeback_control *wbc,
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002173 struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002174{
Alex Tomas64769242008-07-11 19:27:31 -04002175 int ret;
2176
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002177 if (!mpd->get_block)
Alex Tomas64769242008-07-11 19:27:31 -04002178 return generic_writepages(mapping, wbc);
2179
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002180 mpd->lbh.b_size = 0;
2181 mpd->lbh.b_state = 0;
2182 mpd->lbh.b_blocknr = 0;
2183 mpd->first_page = 0;
2184 mpd->next_page = 0;
2185 mpd->io_done = 0;
2186 mpd->pages_written = 0;
2187 mpd->retval = 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002188
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002189 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, mpd);
Alex Tomas64769242008-07-11 19:27:31 -04002190 /*
2191 * Handle last extent of pages
2192 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002193 if (!mpd->io_done && mpd->next_page != mpd->first_page) {
2194 if (mpage_da_map_blocks(mpd) == 0)
2195 mpage_da_submit_io(mpd);
Alex Tomas64769242008-07-11 19:27:31 -04002196
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002197 mpd->io_done = 1;
2198 ret = MPAGE_DA_EXTENT_TAIL;
2199 }
2200 wbc->nr_to_write -= mpd->pages_written;
Alex Tomas64769242008-07-11 19:27:31 -04002201 return ret;
2202}
2203
2204/*
2205 * this is a special callback for ->write_begin() only
2206 * it's intention is to return mapped block or reserve space
2207 */
2208static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2209 struct buffer_head *bh_result, int create)
2210{
2211 int ret = 0;
2212
2213 BUG_ON(create == 0);
2214 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2215
2216 /*
2217 * first, we need to know whether the block is allocated already
2218 * preallocated blocks are unmapped but should treated
2219 * the same as allocated blocks.
2220 */
Mingming Caod2a17632008-07-14 17:52:37 -04002221 ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1, bh_result, 0, 0, 0);
2222 if ((ret == 0) && !buffer_delay(bh_result)) {
2223 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002224 /*
2225 * XXX: __block_prepare_write() unmaps passed block,
2226 * is it OK?
2227 */
Mingming Caod2a17632008-07-14 17:52:37 -04002228 ret = ext4_da_reserve_space(inode, 1);
2229 if (ret)
2230 /* not enough space to reserve */
2231 return ret;
2232
Alex Tomas64769242008-07-11 19:27:31 -04002233 map_bh(bh_result, inode->i_sb, 0);
2234 set_buffer_new(bh_result);
2235 set_buffer_delay(bh_result);
2236 } else if (ret > 0) {
2237 bh_result->b_size = (ret << inode->i_blkbits);
2238 ret = 0;
2239 }
2240
2241 return ret;
2242}
Mingming Caod2a17632008-07-14 17:52:37 -04002243#define EXT4_DELALLOC_RSVED 1
Alex Tomas64769242008-07-11 19:27:31 -04002244static int ext4_da_get_block_write(struct inode *inode, sector_t iblock,
2245 struct buffer_head *bh_result, int create)
2246{
Mingming Cao61628a32008-07-11 19:27:31 -04002247 int ret;
Alex Tomas64769242008-07-11 19:27:31 -04002248 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2249 loff_t disksize = EXT4_I(inode)->i_disksize;
2250 handle_t *handle = NULL;
2251
Mingming Cao61628a32008-07-11 19:27:31 -04002252 handle = ext4_journal_current_handle();
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002253 BUG_ON(!handle);
2254 ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
2255 bh_result, create, 0, EXT4_DELALLOC_RSVED);
Alex Tomas64769242008-07-11 19:27:31 -04002256 if (ret > 0) {
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002257
Alex Tomas64769242008-07-11 19:27:31 -04002258 bh_result->b_size = (ret << inode->i_blkbits);
2259
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002260 if (ext4_should_order_data(inode)) {
2261 int retval;
2262 retval = ext4_jbd2_file_inode(handle, inode);
2263 if (retval)
2264 /*
2265 * Failed to add inode for ordered
2266 * mode. Don't update file size
2267 */
2268 return retval;
2269 }
2270
Alex Tomas64769242008-07-11 19:27:31 -04002271 /*
2272 * Update on-disk size along with block allocation
2273 * we don't use 'extend_disksize' as size may change
2274 * within already allocated block -bzzz
2275 */
2276 disksize = ((loff_t) iblock + ret) << inode->i_blkbits;
2277 if (disksize > i_size_read(inode))
2278 disksize = i_size_read(inode);
2279 if (disksize > EXT4_I(inode)->i_disksize) {
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002280 ext4_update_i_disksize(inode, disksize);
2281 ret = ext4_mark_inode_dirty(handle, inode);
2282 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002283 }
Alex Tomas64769242008-07-11 19:27:31 -04002284 ret = 0;
2285 }
Alex Tomas64769242008-07-11 19:27:31 -04002286 return ret;
2287}
Mingming Cao61628a32008-07-11 19:27:31 -04002288
2289static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh)
2290{
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002291 /*
2292 * unmapped buffer is possible for holes.
2293 * delay buffer is possible with delayed allocation
2294 */
2295 return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh));
2296}
2297
2298static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock,
2299 struct buffer_head *bh_result, int create)
2300{
2301 int ret = 0;
2302 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2303
2304 /*
2305 * we don't want to do block allocation in writepage
2306 * so call get_block_wrap with create = 0
2307 */
2308 ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks,
2309 bh_result, 0, 0, 0);
2310 if (ret > 0) {
2311 bh_result->b_size = (ret << inode->i_blkbits);
2312 ret = 0;
2313 }
2314 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002315}
2316
Mingming Cao61628a32008-07-11 19:27:31 -04002317/*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002318 * get called vi ext4_da_writepages after taking page lock (have journal handle)
2319 * get called via journal_submit_inode_data_buffers (no journal handle)
2320 * get called via shrink_page_list via pdflush (no journal handle)
2321 * or grab_page_cache when doing write_begin (have journal handle)
Mingming Cao61628a32008-07-11 19:27:31 -04002322 */
Alex Tomas64769242008-07-11 19:27:31 -04002323static int ext4_da_writepage(struct page *page,
2324 struct writeback_control *wbc)
2325{
Alex Tomas64769242008-07-11 19:27:31 -04002326 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002327 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002328 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002329 struct buffer_head *page_bufs;
2330 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002331
Theodore Ts'oba80b102009-01-03 20:03:21 -05002332 trace_mark(ext4_da_writepage,
2333 "dev %s ino %lu page_index %lu",
2334 inode->i_sb->s_id, inode->i_ino, page->index);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002335 size = i_size_read(inode);
2336 if (page->index == size >> PAGE_CACHE_SHIFT)
2337 len = size & ~PAGE_CACHE_MASK;
2338 else
2339 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002340
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002341 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002342 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002343 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2344 ext4_bh_unmapped_or_delay)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002345 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002346 * We don't want to do block allocation
2347 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002348 * We may reach here when we do a journal commit
2349 * via journal_submit_inode_data_buffers.
2350 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002351 * them. We can also reach here via shrink_page_list
2352 */
2353 redirty_page_for_writepage(wbc, page);
2354 unlock_page(page);
2355 return 0;
2356 }
2357 } else {
2358 /*
2359 * The test for page_has_buffers() is subtle:
2360 * We know the page is dirty but it lost buffers. That means
2361 * that at some moment in time after write_begin()/write_end()
2362 * has been called all buffers have been clean and thus they
2363 * must have been written at least once. So they are all
2364 * mapped and we can happily proceed with mapping them
2365 * and writing the page.
2366 *
2367 * Try to initialize the buffer_heads and check whether
2368 * all are mapped and non delay. We don't want to
2369 * do block allocation here.
2370 */
2371 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2372 ext4_normal_get_block_write);
2373 if (!ret) {
2374 page_bufs = page_buffers(page);
2375 /* check whether all are mapped and non delay */
2376 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2377 ext4_bh_unmapped_or_delay)) {
2378 redirty_page_for_writepage(wbc, page);
2379 unlock_page(page);
2380 return 0;
2381 }
2382 } else {
2383 /*
2384 * We can't do block allocation here
2385 * so just redity the page and unlock
2386 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002387 */
Mingming Cao61628a32008-07-11 19:27:31 -04002388 redirty_page_for_writepage(wbc, page);
2389 unlock_page(page);
2390 return 0;
2391 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002392 /* now mark the buffer_heads as dirty and uptodate */
2393 block_commit_write(page, 0, PAGE_CACHE_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04002394 }
2395
2396 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002397 ret = nobh_writepage(page, ext4_normal_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002398 else
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002399 ret = block_write_full_page(page,
2400 ext4_normal_get_block_write,
2401 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002402
Alex Tomas64769242008-07-11 19:27:31 -04002403 return ret;
2404}
2405
Mingming Cao61628a32008-07-11 19:27:31 -04002406/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002407 * This is called via ext4_da_writepages() to
2408 * calulate the total number of credits to reserve to fit
2409 * a single extent allocation into a single transaction,
2410 * ext4_da_writpeages() will loop calling this before
2411 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002412 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002413
2414static int ext4_da_writepages_trans_blocks(struct inode *inode)
2415{
2416 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2417
2418 /*
2419 * With non-extent format the journal credit needed to
2420 * insert nrblocks contiguous block is dependent on
2421 * number of contiguous block. So we will limit
2422 * number of contiguous block to a sane value
2423 */
2424 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2425 (max_blocks > EXT4_MAX_TRANS_DATA))
2426 max_blocks = EXT4_MAX_TRANS_DATA;
2427
2428 return ext4_chunk_trans_blocks(inode, max_blocks);
2429}
Mingming Cao61628a32008-07-11 19:27:31 -04002430
Alex Tomas64769242008-07-11 19:27:31 -04002431static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002432 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002433{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002434 pgoff_t index;
2435 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002436 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002437 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002438 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002439 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002440 int pages_written = 0;
2441 long pages_skipped;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002442 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002443 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002444
Theodore Ts'oba80b102009-01-03 20:03:21 -05002445 trace_mark(ext4_da_writepages,
2446 "dev %s ino %lu nr_t_write %ld "
2447 "pages_skipped %ld range_start %llu "
2448 "range_end %llu nonblocking %d "
2449 "for_kupdate %d for_reclaim %d "
2450 "for_writepages %d range_cyclic %d",
2451 inode->i_sb->s_id, inode->i_ino,
2452 wbc->nr_to_write, wbc->pages_skipped,
2453 (unsigned long long) wbc->range_start,
2454 (unsigned long long) wbc->range_end,
2455 wbc->nonblocking, wbc->for_kupdate,
2456 wbc->for_reclaim, wbc->for_writepages,
2457 wbc->range_cyclic);
2458
Mingming Cao61628a32008-07-11 19:27:31 -04002459 /*
2460 * No pages to write? This is mainly a kludge to avoid starting
2461 * a transaction for special inodes like journal inode on last iput()
2462 * because that could violate lock ordering on umount
2463 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002464 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002465 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002466
2467 /*
2468 * If the filesystem has aborted, it is read-only, so return
2469 * right away instead of dumping stack traces later on that
2470 * will obscure the real source of the problem. We test
2471 * EXT4_MOUNT_ABORT instead of sb->s_flag's MS_RDONLY because
2472 * the latter could be true if the filesystem is mounted
2473 * read-only, and in that case, ext4_da_writepages should
2474 * *never* be called, so if that ever happens, we would want
2475 * the stack trace.
2476 */
2477 if (unlikely(sbi->s_mount_opt & EXT4_MOUNT_ABORT))
2478 return -EROFS;
2479
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002480 /*
2481 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2482 * This make sure small files blocks are allocated in
2483 * single attempt. This ensure that small files
2484 * get less fragmented.
2485 */
2486 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2487 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2488 wbc->nr_to_write = sbi->s_mb_stream_request;
2489 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002490 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2491 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002492
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002493 if (wbc->range_cyclic)
2494 index = mapping->writeback_index;
2495 else
2496 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002497
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002498 mpd.wbc = wbc;
2499 mpd.inode = mapping->host;
2500
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002501 /*
2502 * we don't want write_cache_pages to update
2503 * nr_to_write and writeback_index
2504 */
2505 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2506 wbc->no_nrwrite_index_update = 1;
2507 pages_skipped = wbc->pages_skipped;
2508
2509 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002510
2511 /*
2512 * we insert one extent at a time. So we need
2513 * credit needed for single extent allocation.
2514 * journalled mode is currently not supported
2515 * by delalloc
2516 */
2517 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002518 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002519
Mingming Cao61628a32008-07-11 19:27:31 -04002520 /* start a new transaction*/
2521 handle = ext4_journal_start(inode, needed_blocks);
2522 if (IS_ERR(handle)) {
2523 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002524 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002525 "%ld pages, ino %lu; err %d\n", __func__,
2526 wbc->nr_to_write, inode->i_ino, ret);
2527 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002528 goto out_writepages;
2529 }
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002530 mpd.get_block = ext4_da_get_block_write;
2531 ret = mpage_da_writepages(mapping, wbc, &mpd);
2532
Mingming Cao61628a32008-07-11 19:27:31 -04002533 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002534
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002535 if (mpd.retval == -ENOSPC) {
2536 /* commit the transaction which would
2537 * free blocks released in the transaction
2538 * and try again
2539 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002540 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002541 wbc->pages_skipped = pages_skipped;
2542 ret = 0;
2543 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002544 /*
2545 * got one extent now try with
2546 * rest of the pages
2547 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002548 pages_written += mpd.pages_written;
2549 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002550 ret = 0;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002551 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002552 /*
2553 * There is no more writeout needed
2554 * or we requested for a noblocking writeout
2555 * and we found the device congested
2556 */
Mingming Cao61628a32008-07-11 19:27:31 -04002557 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002558 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002559 if (pages_skipped != wbc->pages_skipped)
2560 printk(KERN_EMERG "This should not happen leaving %s "
2561 "with nr_to_write = %ld ret = %d\n",
2562 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002563
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002564 /* Update index */
2565 index += pages_written;
2566 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2567 /*
2568 * set the writeback_index so that range_cyclic
2569 * mode will write it back later
2570 */
2571 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002572
Mingming Cao61628a32008-07-11 19:27:31 -04002573out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002574 if (!no_nrwrite_index_update)
2575 wbc->no_nrwrite_index_update = 0;
2576 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'oba80b102009-01-03 20:03:21 -05002577 trace_mark(ext4_da_writepage_result,
2578 "dev %s ino %lu ret %d pages_written %d "
2579 "pages_skipped %ld congestion %d "
2580 "more_io %d no_nrwrite_index_update %d",
2581 inode->i_sb->s_id, inode->i_ino, ret,
2582 pages_written, wbc->pages_skipped,
2583 wbc->encountered_congestion, wbc->more_io,
2584 wbc->no_nrwrite_index_update);
Mingming Cao61628a32008-07-11 19:27:31 -04002585 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002586}
2587
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002588#define FALL_BACK_TO_NONDELALLOC 1
2589static int ext4_nonda_switch(struct super_block *sb)
2590{
2591 s64 free_blocks, dirty_blocks;
2592 struct ext4_sb_info *sbi = EXT4_SB(sb);
2593
2594 /*
2595 * switch to non delalloc mode if we are running low
2596 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002597 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002598 * accumulated on each CPU without updating global counters
2599 * Delalloc need an accurate free block accounting. So switch
2600 * to non delalloc when we are near to error range.
2601 */
2602 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2603 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2604 if (2 * free_blocks < 3 * dirty_blocks ||
2605 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2606 /*
2607 * free block count is less that 150% of dirty blocks
2608 * or free blocks is less that watermark
2609 */
2610 return 1;
2611 }
2612 return 0;
2613}
2614
Alex Tomas64769242008-07-11 19:27:31 -04002615static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
2616 loff_t pos, unsigned len, unsigned flags,
2617 struct page **pagep, void **fsdata)
2618{
Mingming Caod2a17632008-07-14 17:52:37 -04002619 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002620 struct page *page;
2621 pgoff_t index;
2622 unsigned from, to;
2623 struct inode *inode = mapping->host;
2624 handle_t *handle;
2625
2626 index = pos >> PAGE_CACHE_SHIFT;
2627 from = pos & (PAGE_CACHE_SIZE - 1);
2628 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002629
2630 if (ext4_nonda_switch(inode->i_sb)) {
2631 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2632 return ext4_write_begin(file, mapping, pos,
2633 len, flags, pagep, fsdata);
2634 }
2635 *fsdata = (void *)0;
Theodore Ts'oba80b102009-01-03 20:03:21 -05002636
2637 trace_mark(ext4_da_write_begin,
2638 "dev %s ino %lu pos %llu len %u flags %u",
2639 inode->i_sb->s_id, inode->i_ino,
2640 (unsigned long long) pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002641retry:
Alex Tomas64769242008-07-11 19:27:31 -04002642 /*
2643 * With delayed allocation, we don't log the i_disksize update
2644 * if there is delayed block allocation. But we still need
2645 * to journalling the i_disksize update if writes to the end
2646 * of file which has an already mapped buffer.
2647 */
2648 handle = ext4_journal_start(inode, 1);
2649 if (IS_ERR(handle)) {
2650 ret = PTR_ERR(handle);
2651 goto out;
2652 }
2653
Nick Piggin54566b22009-01-04 12:00:53 -08002654 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002655 if (!page) {
2656 ext4_journal_stop(handle);
2657 ret = -ENOMEM;
2658 goto out;
2659 }
Alex Tomas64769242008-07-11 19:27:31 -04002660 *pagep = page;
2661
2662 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
2663 ext4_da_get_block_prep);
2664 if (ret < 0) {
2665 unlock_page(page);
2666 ext4_journal_stop(handle);
2667 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002668 /*
2669 * block_write_begin may have instantiated a few blocks
2670 * outside i_size. Trim these off again. Don't need
2671 * i_size_read because we hold i_mutex.
2672 */
2673 if (pos + len > inode->i_size)
2674 vmtruncate(inode, inode->i_size);
Alex Tomas64769242008-07-11 19:27:31 -04002675 }
2676
Mingming Caod2a17632008-07-14 17:52:37 -04002677 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2678 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04002679out:
2680 return ret;
2681}
2682
Mingming Cao632eaea2008-07-11 19:27:31 -04002683/*
2684 * Check if we should update i_disksize
2685 * when write to the end of file but not require block allocation
2686 */
2687static int ext4_da_should_update_i_disksize(struct page *page,
2688 unsigned long offset)
2689{
2690 struct buffer_head *bh;
2691 struct inode *inode = page->mapping->host;
2692 unsigned int idx;
2693 int i;
2694
2695 bh = page_buffers(page);
2696 idx = offset >> inode->i_blkbits;
2697
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002698 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002699 bh = bh->b_this_page;
2700
2701 if (!buffer_mapped(bh) || (buffer_delay(bh)))
2702 return 0;
2703 return 1;
2704}
2705
Alex Tomas64769242008-07-11 19:27:31 -04002706static int ext4_da_write_end(struct file *file,
2707 struct address_space *mapping,
2708 loff_t pos, unsigned len, unsigned copied,
2709 struct page *page, void *fsdata)
2710{
2711 struct inode *inode = mapping->host;
2712 int ret = 0, ret2;
2713 handle_t *handle = ext4_journal_current_handle();
2714 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002715 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002716 int write_mode = (int)(unsigned long)fsdata;
2717
2718 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
2719 if (ext4_should_order_data(inode)) {
2720 return ext4_ordered_write_end(file, mapping, pos,
2721 len, copied, page, fsdata);
2722 } else if (ext4_should_writeback_data(inode)) {
2723 return ext4_writeback_write_end(file, mapping, pos,
2724 len, copied, page, fsdata);
2725 } else {
2726 BUG();
2727 }
2728 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002729
Theodore Ts'oba80b102009-01-03 20:03:21 -05002730 trace_mark(ext4_da_write_end,
2731 "dev %s ino %lu pos %llu len %u copied %u",
2732 inode->i_sb->s_id, inode->i_ino,
2733 (unsigned long long) pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002734 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002735 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002736
2737 /*
2738 * generic_write_end() will run mark_inode_dirty() if i_size
2739 * changes. So let's piggyback the i_disksize mark_inode_dirty
2740 * into that.
2741 */
2742
2743 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04002744 if (new_i_size > EXT4_I(inode)->i_disksize) {
2745 if (ext4_da_should_update_i_disksize(page, end)) {
2746 down_write(&EXT4_I(inode)->i_data_sem);
2747 if (new_i_size > EXT4_I(inode)->i_disksize) {
2748 /*
2749 * Updating i_disksize when extending file
2750 * without needing block allocation
2751 */
2752 if (ext4_should_order_data(inode))
2753 ret = ext4_jbd2_file_inode(handle,
2754 inode);
Alex Tomas64769242008-07-11 19:27:31 -04002755
Mingming Cao632eaea2008-07-11 19:27:31 -04002756 EXT4_I(inode)->i_disksize = new_i_size;
2757 }
2758 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002759 /* We need to mark inode dirty even if
2760 * new_i_size is less that inode->i_size
2761 * bu greater than i_disksize.(hint delalloc)
2762 */
2763 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002764 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002765 }
Alex Tomas64769242008-07-11 19:27:31 -04002766 ret2 = generic_write_end(file, mapping, pos, len, copied,
2767 page, fsdata);
2768 copied = ret2;
2769 if (ret2 < 0)
2770 ret = ret2;
2771 ret2 = ext4_journal_stop(handle);
2772 if (!ret)
2773 ret = ret2;
2774
2775 return ret ? ret : copied;
2776}
2777
2778static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2779{
Alex Tomas64769242008-07-11 19:27:31 -04002780 /*
2781 * Drop reserved blocks
2782 */
2783 BUG_ON(!PageLocked(page));
2784 if (!page_has_buffers(page))
2785 goto out;
2786
Mingming Caod2a17632008-07-14 17:52:37 -04002787 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04002788
2789out:
2790 ext4_invalidatepage(page, offset);
2791
2792 return;
2793}
2794
2795
2796/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002797 * bmap() is special. It gets used by applications such as lilo and by
2798 * the swapper to find the on-disk block of a specific piece of data.
2799 *
2800 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002801 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002802 * filesystem and enables swap, then they may get a nasty shock when the
2803 * data getting swapped to that swapfile suddenly gets overwritten by
2804 * the original zero's written out previously to the journal and
2805 * awaiting writeback in the kernel's buffer cache.
2806 *
2807 * So, if we see any bmap calls here on a modified, data-journaled file,
2808 * take extra steps to flush any blocks which might be in the cache.
2809 */
Mingming Cao617ba132006-10-11 01:20:53 -07002810static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002811{
2812 struct inode *inode = mapping->host;
2813 journal_t *journal;
2814 int err;
2815
Alex Tomas64769242008-07-11 19:27:31 -04002816 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2817 test_opt(inode->i_sb, DELALLOC)) {
2818 /*
2819 * With delalloc we want to sync the file
2820 * so that we can make sure we allocate
2821 * blocks for file
2822 */
2823 filemap_write_and_wait(mapping);
2824 }
2825
Frank Mayhar03901312009-01-07 00:06:22 -05002826 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002827 /*
2828 * This is a REALLY heavyweight approach, but the use of
2829 * bmap on dirty files is expected to be extremely rare:
2830 * only if we run lilo or swapon on a freshly made file
2831 * do we expect this to happen.
2832 *
2833 * (bmap requires CAP_SYS_RAWIO so this does not
2834 * represent an unprivileged user DOS attack --- we'd be
2835 * in trouble if mortal users could trigger this path at
2836 * will.)
2837 *
Mingming Cao617ba132006-10-11 01:20:53 -07002838 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002839 * regular files. If somebody wants to bmap a directory
2840 * or symlink and gets confused because the buffer
2841 * hasn't yet been flushed to disk, they deserve
2842 * everything they get.
2843 */
2844
Mingming Cao617ba132006-10-11 01:20:53 -07002845 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
2846 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002847 jbd2_journal_lock_updates(journal);
2848 err = jbd2_journal_flush(journal);
2849 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850
2851 if (err)
2852 return 0;
2853 }
2854
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002855 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002856}
2857
2858static int bget_one(handle_t *handle, struct buffer_head *bh)
2859{
2860 get_bh(bh);
2861 return 0;
2862}
2863
2864static int bput_one(handle_t *handle, struct buffer_head *bh)
2865{
2866 put_bh(bh);
2867 return 0;
2868}
2869
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002870/*
Jan Kara678aaf42008-07-11 19:27:31 -04002871 * Note that we don't need to start a transaction unless we're journaling data
2872 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2873 * need to file the inode to the transaction's list in ordered mode because if
2874 * we are writing back data added by write(), the inode is already there and if
2875 * we are writing back data modified via mmap(), noone guarantees in which
2876 * transaction the data will hit the disk. In case we are journaling data, we
2877 * cannot start transaction directly because transaction start ranks above page
2878 * lock so we have to do some magic.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002879 *
Jan Kara678aaf42008-07-11 19:27:31 -04002880 * In all journaling modes block_write_full_page() will start the I/O.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002881 *
2882 * Problem:
2883 *
Mingming Cao617ba132006-10-11 01:20:53 -07002884 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2885 * ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002886 *
2887 * Similar for:
2888 *
Mingming Cao617ba132006-10-11 01:20:53 -07002889 * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ...
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002890 *
Mingming Cao617ba132006-10-11 01:20:53 -07002891 * Same applies to ext4_get_block(). We will deadlock on various things like
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002892 * lock_journal and i_data_sem
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893 *
2894 * Setting PF_MEMALLOC here doesn't work - too many internal memory
2895 * allocations fail.
2896 *
2897 * 16May01: If we're reentered then journal_current_handle() will be
2898 * non-zero. We simply *return*.
2899 *
2900 * 1 July 2001: @@@ FIXME:
2901 * In journalled data mode, a data buffer may be metadata against the
2902 * current transaction. But the same file is part of a shared mapping
2903 * and someone does a writepage() on it.
2904 *
2905 * We will move the buffer onto the async_data list, but *after* it has
2906 * been dirtied. So there's a small window where we have dirty data on
2907 * BJ_Metadata.
2908 *
2909 * Note that this only applies to the last partial page in the file. The
2910 * bit which block_write_full_page() uses prepare/commit for. (That's
2911 * broken code anyway: it's wrong for msync()).
2912 *
2913 * It's a rare case: affects the final partial page, for journalled data
2914 * where the file is subject to bith write() and writepage() in the same
2915 * transction. To fix it we'll need a custom block_write_full_page().
2916 * We'll probably need that anyway for journalling writepage() output.
2917 *
2918 * We don't honour synchronous mounts for writepage(). That would be
2919 * disastrous. Any write() or metadata operation will sync the fs for
2920 * us.
2921 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002922 */
Jan Kara678aaf42008-07-11 19:27:31 -04002923static int __ext4_normal_writepage(struct page *page,
Jan Karacf108bc2008-07-11 19:27:31 -04002924 struct writeback_control *wbc)
2925{
2926 struct inode *inode = page->mapping->host;
2927
2928 if (test_opt(inode->i_sb, NOBH))
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002929 return nobh_writepage(page,
2930 ext4_normal_get_block_write, wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002931 else
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002932 return block_write_full_page(page,
2933 ext4_normal_get_block_write,
2934 wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002935}
2936
Jan Kara678aaf42008-07-11 19:27:31 -04002937static int ext4_normal_writepage(struct page *page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002938 struct writeback_control *wbc)
2939{
2940 struct inode *inode = page->mapping->host;
Jan Karacf108bc2008-07-11 19:27:31 -04002941 loff_t size = i_size_read(inode);
2942 loff_t len;
2943
Theodore Ts'oba80b102009-01-03 20:03:21 -05002944 trace_mark(ext4_normal_writepage,
2945 "dev %s ino %lu page_index %lu",
2946 inode->i_sb->s_id, inode->i_ino, page->index);
Jan Karacf108bc2008-07-11 19:27:31 -04002947 J_ASSERT(PageLocked(page));
Jan Karacf108bc2008-07-11 19:27:31 -04002948 if (page->index == size >> PAGE_CACHE_SHIFT)
2949 len = size & ~PAGE_CACHE_MASK;
2950 else
2951 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002952
2953 if (page_has_buffers(page)) {
2954 /* if page has buffers it should all be mapped
2955 * and allocated. If there are not buffers attached
2956 * to the page we know the page is dirty but it lost
2957 * buffers. That means that at some moment in time
2958 * after write_begin() / write_end() has been called
2959 * all buffers have been clean and thus they must have been
2960 * written at least once. So they are all mapped and we can
2961 * happily proceed with mapping them and writing the page.
2962 */
2963 BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
2964 ext4_bh_unmapped_or_delay));
2965 }
Jan Karacf108bc2008-07-11 19:27:31 -04002966
2967 if (!ext4_journal_current_handle())
Jan Kara678aaf42008-07-11 19:27:31 -04002968 return __ext4_normal_writepage(page, wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002969
2970 redirty_page_for_writepage(wbc, page);
2971 unlock_page(page);
2972 return 0;
2973}
2974
2975static int __ext4_journalled_writepage(struct page *page,
2976 struct writeback_control *wbc)
2977{
2978 struct address_space *mapping = page->mapping;
2979 struct inode *inode = mapping->host;
2980 struct buffer_head *page_bufs;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981 handle_t *handle = NULL;
2982 int ret = 0;
2983 int err;
2984
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002985 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2986 ext4_normal_get_block_write);
Jan Karacf108bc2008-07-11 19:27:31 -04002987 if (ret != 0)
2988 goto out_unlock;
2989
2990 page_bufs = page_buffers(page);
2991 walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL,
2992 bget_one);
2993 /* As soon as we unlock the page, it can go away, but we have
2994 * references to buffers so we are safe */
2995 unlock_page(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996
Mingming Cao617ba132006-10-11 01:20:53 -07002997 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 if (IS_ERR(handle)) {
2999 ret = PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04003000 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003001 }
3002
Jan Karacf108bc2008-07-11 19:27:31 -04003003 ret = walk_page_buffers(handle, page_bufs, 0,
3004 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003005
Jan Karacf108bc2008-07-11 19:27:31 -04003006 err = walk_page_buffers(handle, page_bufs, 0,
3007 PAGE_CACHE_SIZE, NULL, write_end_fn);
3008 if (ret == 0)
3009 ret = err;
Mingming Cao617ba132006-10-11 01:20:53 -07003010 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003011 if (!ret)
3012 ret = err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003013
Jan Karacf108bc2008-07-11 19:27:31 -04003014 walk_page_buffers(handle, page_bufs, 0,
3015 PAGE_CACHE_SIZE, NULL, bput_one);
3016 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
3017 goto out;
3018
3019out_unlock:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003020 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003021out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022 return ret;
3023}
3024
Mingming Cao617ba132006-10-11 01:20:53 -07003025static int ext4_journalled_writepage(struct page *page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003026 struct writeback_control *wbc)
3027{
3028 struct inode *inode = page->mapping->host;
Jan Karacf108bc2008-07-11 19:27:31 -04003029 loff_t size = i_size_read(inode);
3030 loff_t len;
3031
Theodore Ts'oba80b102009-01-03 20:03:21 -05003032 trace_mark(ext4_journalled_writepage,
3033 "dev %s ino %lu page_index %lu",
3034 inode->i_sb->s_id, inode->i_ino, page->index);
Jan Karacf108bc2008-07-11 19:27:31 -04003035 J_ASSERT(PageLocked(page));
Jan Karacf108bc2008-07-11 19:27:31 -04003036 if (page->index == size >> PAGE_CACHE_SHIFT)
3037 len = size & ~PAGE_CACHE_MASK;
3038 else
3039 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04003040
3041 if (page_has_buffers(page)) {
3042 /* if page has buffers it should all be mapped
3043 * and allocated. If there are not buffers attached
3044 * to the page we know the page is dirty but it lost
3045 * buffers. That means that at some moment in time
3046 * after write_begin() / write_end() has been called
3047 * all buffers have been clean and thus they must have been
3048 * written at least once. So they are all mapped and we can
3049 * happily proceed with mapping them and writing the page.
3050 */
3051 BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
3052 ext4_bh_unmapped_or_delay));
3053 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054
Mingming Cao617ba132006-10-11 01:20:53 -07003055 if (ext4_journal_current_handle())
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056 goto no_write;
3057
Jan Karacf108bc2008-07-11 19:27:31 -04003058 if (PageChecked(page)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003059 /*
3060 * It's mmapped pagecache. Add buffers and journal it. There
3061 * doesn't seem much point in redirtying the page here.
3062 */
3063 ClearPageChecked(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003064 return __ext4_journalled_writepage(page, wbc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 } else {
3066 /*
3067 * It may be a page full of checkpoint-mode buffers. We don't
3068 * really know unless we go poke around in the buffer_heads.
3069 * But block_write_full_page will do the right thing.
3070 */
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04003071 return block_write_full_page(page,
3072 ext4_normal_get_block_write,
3073 wbc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003074 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075no_write:
3076 redirty_page_for_writepage(wbc, page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003077 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003078 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079}
3080
Mingming Cao617ba132006-10-11 01:20:53 -07003081static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003082{
Mingming Cao617ba132006-10-11 01:20:53 -07003083 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003084}
3085
3086static int
Mingming Cao617ba132006-10-11 01:20:53 -07003087ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088 struct list_head *pages, unsigned nr_pages)
3089{
Mingming Cao617ba132006-10-11 01:20:53 -07003090 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003091}
3092
Mingming Cao617ba132006-10-11 01:20:53 -07003093static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003094{
Mingming Cao617ba132006-10-11 01:20:53 -07003095 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003096
3097 /*
3098 * If it's a full truncate we just forget about the pending dirtying
3099 */
3100 if (offset == 0)
3101 ClearPageChecked(page);
3102
Frank Mayhar03901312009-01-07 00:06:22 -05003103 if (journal)
3104 jbd2_journal_invalidatepage(journal, page, offset);
3105 else
3106 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003107}
3108
Mingming Cao617ba132006-10-11 01:20:53 -07003109static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003110{
Mingming Cao617ba132006-10-11 01:20:53 -07003111 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112
3113 WARN_ON(PageChecked(page));
3114 if (!page_has_buffers(page))
3115 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003116 if (journal)
3117 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3118 else
3119 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120}
3121
3122/*
3123 * If the O_DIRECT write will extend the file then add this inode to the
3124 * orphan list. So recovery will truncate it back to the original size
3125 * if the machine crashes during the write.
3126 *
3127 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003128 * crashes then stale disk data _may_ be exposed inside the file. But current
3129 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 */
Mingming Cao617ba132006-10-11 01:20:53 -07003131static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003132 const struct iovec *iov, loff_t offset,
3133 unsigned long nr_segs)
3134{
3135 struct file *file = iocb->ki_filp;
3136 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003137 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003138 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003139 ssize_t ret;
3140 int orphan = 0;
3141 size_t count = iov_length(iov, nr_segs);
3142
3143 if (rw == WRITE) {
3144 loff_t final_size = offset + count;
3145
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003146 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003147 /* Credits for sb + inode write */
3148 handle = ext4_journal_start(inode, 2);
3149 if (IS_ERR(handle)) {
3150 ret = PTR_ERR(handle);
3151 goto out;
3152 }
Mingming Cao617ba132006-10-11 01:20:53 -07003153 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003154 if (ret) {
3155 ext4_journal_stop(handle);
3156 goto out;
3157 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003158 orphan = 1;
3159 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003160 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 }
3162 }
3163
3164 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3165 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003166 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167
Jan Kara7fb54092008-02-10 01:08:38 -05003168 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003169 int err;
3170
Jan Kara7fb54092008-02-10 01:08:38 -05003171 /* Credits for sb + inode write */
3172 handle = ext4_journal_start(inode, 2);
3173 if (IS_ERR(handle)) {
3174 /* This is really bad luck. We've written the data
3175 * but cannot extend i_size. Bail out and pretend
3176 * the write failed... */
3177 ret = PTR_ERR(handle);
3178 goto out;
3179 }
3180 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003181 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003182 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183 loff_t end = offset + ret;
3184 if (end > inode->i_size) {
3185 ei->i_disksize = end;
3186 i_size_write(inode, end);
3187 /*
3188 * We're going to return a positive `ret'
3189 * here due to non-zero-length I/O, so there's
3190 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003191 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003192 * ignore it.
3193 */
Mingming Cao617ba132006-10-11 01:20:53 -07003194 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 }
3196 }
Mingming Cao617ba132006-10-11 01:20:53 -07003197 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003198 if (ret == 0)
3199 ret = err;
3200 }
3201out:
3202 return ret;
3203}
3204
3205/*
Mingming Cao617ba132006-10-11 01:20:53 -07003206 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003207 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3208 * much here because ->set_page_dirty is called under VFS locks. The page is
3209 * not necessarily locked.
3210 *
3211 * We cannot just dirty the page and leave attached buffers clean, because the
3212 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3213 * or jbddirty because all the journalling code will explode.
3214 *
3215 * So what we do is to mark the page "pending dirty" and next time writepage
3216 * is called, propagate that into the buffers appropriately.
3217 */
Mingming Cao617ba132006-10-11 01:20:53 -07003218static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003219{
3220 SetPageChecked(page);
3221 return __set_page_dirty_nobuffers(page);
3222}
3223
Mingming Cao617ba132006-10-11 01:20:53 -07003224static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003225 .readpage = ext4_readpage,
3226 .readpages = ext4_readpages,
3227 .writepage = ext4_normal_writepage,
3228 .sync_page = block_sync_page,
3229 .write_begin = ext4_write_begin,
3230 .write_end = ext4_ordered_write_end,
3231 .bmap = ext4_bmap,
3232 .invalidatepage = ext4_invalidatepage,
3233 .releasepage = ext4_releasepage,
3234 .direct_IO = ext4_direct_IO,
3235 .migratepage = buffer_migrate_page,
3236 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003237};
3238
Mingming Cao617ba132006-10-11 01:20:53 -07003239static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003240 .readpage = ext4_readpage,
3241 .readpages = ext4_readpages,
3242 .writepage = ext4_normal_writepage,
3243 .sync_page = block_sync_page,
3244 .write_begin = ext4_write_begin,
3245 .write_end = ext4_writeback_write_end,
3246 .bmap = ext4_bmap,
3247 .invalidatepage = ext4_invalidatepage,
3248 .releasepage = ext4_releasepage,
3249 .direct_IO = ext4_direct_IO,
3250 .migratepage = buffer_migrate_page,
3251 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252};
3253
Mingming Cao617ba132006-10-11 01:20:53 -07003254static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003255 .readpage = ext4_readpage,
3256 .readpages = ext4_readpages,
3257 .writepage = ext4_journalled_writepage,
3258 .sync_page = block_sync_page,
3259 .write_begin = ext4_write_begin,
3260 .write_end = ext4_journalled_write_end,
3261 .set_page_dirty = ext4_journalled_set_page_dirty,
3262 .bmap = ext4_bmap,
3263 .invalidatepage = ext4_invalidatepage,
3264 .releasepage = ext4_releasepage,
3265 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003266};
3267
Alex Tomas64769242008-07-11 19:27:31 -04003268static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003269 .readpage = ext4_readpage,
3270 .readpages = ext4_readpages,
3271 .writepage = ext4_da_writepage,
3272 .writepages = ext4_da_writepages,
3273 .sync_page = block_sync_page,
3274 .write_begin = ext4_da_write_begin,
3275 .write_end = ext4_da_write_end,
3276 .bmap = ext4_bmap,
3277 .invalidatepage = ext4_da_invalidatepage,
3278 .releasepage = ext4_releasepage,
3279 .direct_IO = ext4_direct_IO,
3280 .migratepage = buffer_migrate_page,
3281 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003282};
3283
Mingming Cao617ba132006-10-11 01:20:53 -07003284void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003286 if (ext4_should_order_data(inode) &&
3287 test_opt(inode->i_sb, DELALLOC))
3288 inode->i_mapping->a_ops = &ext4_da_aops;
3289 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003290 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003291 else if (ext4_should_writeback_data(inode) &&
3292 test_opt(inode->i_sb, DELALLOC))
3293 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003294 else if (ext4_should_writeback_data(inode))
3295 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296 else
Mingming Cao617ba132006-10-11 01:20:53 -07003297 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298}
3299
3300/*
Mingming Cao617ba132006-10-11 01:20:53 -07003301 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003302 * up to the end of the block which corresponds to `from'.
3303 * This required during truncate. We need to physically zero the tail end
3304 * of that block so it doesn't yield old data if the file is later grown.
3305 */
Jan Karacf108bc2008-07-11 19:27:31 -04003306int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003307 struct address_space *mapping, loff_t from)
3308{
Mingming Cao617ba132006-10-11 01:20:53 -07003309 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003310 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003311 unsigned blocksize, length, pos;
3312 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003313 struct inode *inode = mapping->host;
3314 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003315 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003316 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003317
Jan Karacf108bc2008-07-11 19:27:31 -04003318 page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT);
3319 if (!page)
3320 return -EINVAL;
3321
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322 blocksize = inode->i_sb->s_blocksize;
3323 length = blocksize - (offset & (blocksize - 1));
3324 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3325
3326 /*
3327 * For "nobh" option, we can only work if we don't need to
3328 * read-in the page - otherwise we create buffers to do the IO.
3329 */
3330 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003331 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003332 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003333 set_page_dirty(page);
3334 goto unlock;
3335 }
3336
3337 if (!page_has_buffers(page))
3338 create_empty_buffers(page, blocksize, 0);
3339
3340 /* Find the buffer that contains "offset" */
3341 bh = page_buffers(page);
3342 pos = blocksize;
3343 while (offset >= pos) {
3344 bh = bh->b_this_page;
3345 iblock++;
3346 pos += blocksize;
3347 }
3348
3349 err = 0;
3350 if (buffer_freed(bh)) {
3351 BUFFER_TRACE(bh, "freed: skip");
3352 goto unlock;
3353 }
3354
3355 if (!buffer_mapped(bh)) {
3356 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003357 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003358 /* unmapped? It's a hole - nothing to do */
3359 if (!buffer_mapped(bh)) {
3360 BUFFER_TRACE(bh, "still unmapped");
3361 goto unlock;
3362 }
3363 }
3364
3365 /* Ok, it's mapped. Make sure it's up-to-date */
3366 if (PageUptodate(page))
3367 set_buffer_uptodate(bh);
3368
3369 if (!buffer_uptodate(bh)) {
3370 err = -EIO;
3371 ll_rw_block(READ, 1, &bh);
3372 wait_on_buffer(bh);
3373 /* Uhhuh. Read error. Complain and punt. */
3374 if (!buffer_uptodate(bh))
3375 goto unlock;
3376 }
3377
Mingming Cao617ba132006-10-11 01:20:53 -07003378 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003379 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003380 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003381 if (err)
3382 goto unlock;
3383 }
3384
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003385 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003386
3387 BUFFER_TRACE(bh, "zeroed end of block");
3388
3389 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003390 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003391 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003392 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003393 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003394 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003395 mark_buffer_dirty(bh);
3396 }
3397
3398unlock:
3399 unlock_page(page);
3400 page_cache_release(page);
3401 return err;
3402}
3403
3404/*
3405 * Probably it should be a library function... search for first non-zero word
3406 * or memcmp with zero_page, whatever is better for particular architecture.
3407 * Linus?
3408 */
3409static inline int all_zeroes(__le32 *p, __le32 *q)
3410{
3411 while (p < q)
3412 if (*p++)
3413 return 0;
3414 return 1;
3415}
3416
3417/**
Mingming Cao617ba132006-10-11 01:20:53 -07003418 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003419 * @inode: inode in question
3420 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003421 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003422 * @chain: place to store the pointers to partial indirect blocks
3423 * @top: place to the (detached) top of branch
3424 *
Mingming Cao617ba132006-10-11 01:20:53 -07003425 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426 *
3427 * When we do truncate() we may have to clean the ends of several
3428 * indirect blocks but leave the blocks themselves alive. Block is
3429 * partially truncated if some data below the new i_size is refered
3430 * from it (and it is on the path to the first completely truncated
3431 * data block, indeed). We have to free the top of that path along
3432 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003433 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003434 * finishes, we may safely do the latter, but top of branch may
3435 * require special attention - pageout below the truncation point
3436 * might try to populate it.
3437 *
3438 * We atomically detach the top of branch from the tree, store the
3439 * block number of its root in *@top, pointers to buffer_heads of
3440 * partially truncated blocks - in @chain[].bh and pointers to
3441 * their last elements that should not be removed - in
3442 * @chain[].p. Return value is the pointer to last filled element
3443 * of @chain.
3444 *
3445 * The work left to caller to do the actual freeing of subtrees:
3446 * a) free the subtree starting from *@top
3447 * b) free the subtrees whose roots are stored in
3448 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3449 * c) free the subtrees growing from the inode past the @chain[0].
3450 * (no partially truncated stuff there). */
3451
Mingming Cao617ba132006-10-11 01:20:53 -07003452static Indirect *ext4_find_shared(struct inode *inode, int depth,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003453 ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003454{
3455 Indirect *partial, *p;
3456 int k, err;
3457
3458 *top = 0;
3459 /* Make k index the deepest non-null offest + 1 */
3460 for (k = depth; k > 1 && !offsets[k-1]; k--)
3461 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003462 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003463 /* Writer: pointers */
3464 if (!partial)
3465 partial = chain + k-1;
3466 /*
3467 * If the branch acquired continuation since we've looked at it -
3468 * fine, it should all survive and (new) top doesn't belong to us.
3469 */
3470 if (!partial->key && *partial->p)
3471 /* Writer: end */
3472 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003473 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003474 ;
3475 /*
3476 * OK, we've found the last block that must survive. The rest of our
3477 * branch should be detached before unlocking. However, if that rest
3478 * of branch is all ours and does not grow immediately from the inode
3479 * it's easier to cheat and just decrement partial->p.
3480 */
3481 if (p == chain + k - 1 && p > chain) {
3482 p->p--;
3483 } else {
3484 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003485 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003486#if 0
3487 *p->p = 0;
3488#endif
3489 }
3490 /* Writer: end */
3491
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003492 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003493 brelse(partial->bh);
3494 partial--;
3495 }
3496no_top:
3497 return partial;
3498}
3499
3500/*
3501 * Zero a number of block pointers in either an inode or an indirect block.
3502 * If we restart the transaction we must again get write access to the
3503 * indirect block for further modification.
3504 *
3505 * We release `count' blocks on disk, but (last - first) may be greater
3506 * than `count' because there can be holes in there.
3507 */
Mingming Cao617ba132006-10-11 01:20:53 -07003508static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
3509 struct buffer_head *bh, ext4_fsblk_t block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003510 unsigned long count, __le32 *first, __le32 *last)
3511{
3512 __le32 *p;
3513 if (try_to_extend_transaction(handle, inode)) {
3514 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003515 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3516 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003517 }
Mingming Cao617ba132006-10-11 01:20:53 -07003518 ext4_mark_inode_dirty(handle, inode);
3519 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003520 if (bh) {
3521 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003522 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003523 }
3524 }
3525
3526 /*
3527 * Any buffers which are on the journal will be in memory. We find
Mingming Caodab291a2006-10-11 01:21:01 -07003528 * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003529 * on them. We've already detached each block from the file, so
Mingming Caodab291a2006-10-11 01:21:01 -07003530 * bforget() in jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531 *
Mingming Caodab291a2006-10-11 01:21:01 -07003532 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003533 */
3534 for (p = first; p < last; p++) {
3535 u32 nr = le32_to_cpu(*p);
3536 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003537 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538
3539 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003540 tbh = sb_find_get_block(inode->i_sb, nr);
3541 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542 }
3543 }
3544
Alex Tomasc9de5602008-01-29 00:19:52 -05003545 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003546}
3547
3548/**
Mingming Cao617ba132006-10-11 01:20:53 -07003549 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003550 * @handle: handle for this transaction
3551 * @inode: inode we are dealing with
3552 * @this_bh: indirect buffer_head which contains *@first and *@last
3553 * @first: array of block numbers
3554 * @last: points immediately past the end of array
3555 *
3556 * We are freeing all blocks refered from that array (numbers are stored as
3557 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3558 *
3559 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3560 * blocks are contiguous then releasing them at one time will only affect one
3561 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3562 * actually use a lot of journal space.
3563 *
3564 * @this_bh will be %NULL if @first and @last point into the inode's direct
3565 * block pointers.
3566 */
Mingming Cao617ba132006-10-11 01:20:53 -07003567static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003568 struct buffer_head *this_bh,
3569 __le32 *first, __le32 *last)
3570{
Mingming Cao617ba132006-10-11 01:20:53 -07003571 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003572 unsigned long count = 0; /* Number of blocks in the run */
3573 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3574 corresponding to
3575 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003576 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003577 __le32 *p; /* Pointer into inode/ind
3578 for current block */
3579 int err;
3580
3581 if (this_bh) { /* For indirect block */
3582 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003583 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003584 /* Important: if we can't update the indirect pointers
3585 * to the blocks, we can't free them. */
3586 if (err)
3587 return;
3588 }
3589
3590 for (p = first; p < last; p++) {
3591 nr = le32_to_cpu(*p);
3592 if (nr) {
3593 /* accumulate blocks to free if they're contiguous */
3594 if (count == 0) {
3595 block_to_free = nr;
3596 block_to_free_p = p;
3597 count = 1;
3598 } else if (nr == block_to_free + count) {
3599 count++;
3600 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003601 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003602 block_to_free,
3603 count, block_to_free_p, p);
3604 block_to_free = nr;
3605 block_to_free_p = p;
3606 count = 1;
3607 }
3608 }
3609 }
3610
3611 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003612 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003613 count, block_to_free_p, p);
3614
3615 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003616 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003617
3618 /*
3619 * The buffer head should have an attached journal head at this
3620 * point. However, if the data is corrupted and an indirect
3621 * block pointed to itself, it would have been detached when
3622 * the block was cleared. Check for this instead of OOPSing.
3623 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05003624 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003625 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003626 else
3627 ext4_error(inode->i_sb, __func__,
3628 "circular indirect block detected, "
3629 "inode=%lu, block=%llu",
3630 inode->i_ino,
3631 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003632 }
3633}
3634
3635/**
Mingming Cao617ba132006-10-11 01:20:53 -07003636 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003637 * @handle: JBD handle for this transaction
3638 * @inode: inode we are dealing with
3639 * @parent_bh: the buffer_head which contains *@first and *@last
3640 * @first: array of block numbers
3641 * @last: pointer immediately past the end of array
3642 * @depth: depth of the branches to free
3643 *
3644 * We are freeing all blocks refered from these branches (numbers are
3645 * stored as little-endian 32-bit) and updating @inode->i_blocks
3646 * appropriately.
3647 */
Mingming Cao617ba132006-10-11 01:20:53 -07003648static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003649 struct buffer_head *parent_bh,
3650 __le32 *first, __le32 *last, int depth)
3651{
Mingming Cao617ba132006-10-11 01:20:53 -07003652 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003653 __le32 *p;
3654
Frank Mayhar03901312009-01-07 00:06:22 -05003655 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656 return;
3657
3658 if (depth--) {
3659 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003660 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003661 p = last;
3662 while (--p >= first) {
3663 nr = le32_to_cpu(*p);
3664 if (!nr)
3665 continue; /* A hole */
3666
3667 /* Go read the buffer for the next level down */
3668 bh = sb_bread(inode->i_sb, nr);
3669
3670 /*
3671 * A read failure? Report error and clear slot
3672 * (should be rare).
3673 */
3674 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003675 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003676 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003677 inode->i_ino, nr);
3678 continue;
3679 }
3680
3681 /* This zaps the entire block. Bottom up. */
3682 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003683 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003684 (__le32 *) bh->b_data,
3685 (__le32 *) bh->b_data + addr_per_block,
3686 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003687
3688 /*
3689 * We've probably journalled the indirect block several
3690 * times during the truncate. But it's no longer
3691 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003692 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003693 *
3694 * That's easy if it's exclusively part of this
3695 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003696 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003697 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003698 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003699 * unmap_underlying_metadata() will find this block
3700 * and will try to get rid of it. damn, damn.
3701 *
3702 * If this block has already been committed to the
3703 * journal, a revoke record will be written. And
3704 * revoke records must be emitted *before* clearing
3705 * this block's bit in the bitmaps.
3706 */
Mingming Cao617ba132006-10-11 01:20:53 -07003707 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003708
3709 /*
3710 * Everything below this this pointer has been
3711 * released. Now let this top-of-subtree go.
3712 *
3713 * We want the freeing of this indirect block to be
3714 * atomic in the journal with the updating of the
3715 * bitmap block which owns it. So make some room in
3716 * the journal.
3717 *
3718 * We zero the parent pointer *after* freeing its
3719 * pointee in the bitmaps, so if extend_transaction()
3720 * for some reason fails to put the bitmap changes and
3721 * the release into the same transaction, recovery
3722 * will merely complain about releasing a free block,
3723 * rather than leaking blocks.
3724 */
Frank Mayhar03901312009-01-07 00:06:22 -05003725 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003726 return;
3727 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003728 ext4_mark_inode_dirty(handle, inode);
3729 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003730 }
3731
Alex Tomasc9de5602008-01-29 00:19:52 -05003732 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003733
3734 if (parent_bh) {
3735 /*
3736 * The block which we have just freed is
3737 * pointed to by an indirect block: journal it
3738 */
3739 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003740 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003741 parent_bh)){
3742 *p = 0;
3743 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003744 "call ext4_handle_dirty_metadata");
3745 ext4_handle_dirty_metadata(handle,
3746 inode,
3747 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003748 }
3749 }
3750 }
3751 } else {
3752 /* We have reached the bottom of the tree. */
3753 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003754 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003755 }
3756}
3757
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003758int ext4_can_truncate(struct inode *inode)
3759{
3760 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3761 return 0;
3762 if (S_ISREG(inode->i_mode))
3763 return 1;
3764 if (S_ISDIR(inode->i_mode))
3765 return 1;
3766 if (S_ISLNK(inode->i_mode))
3767 return !ext4_inode_is_fast_symlink(inode);
3768 return 0;
3769}
3770
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003771/*
Mingming Cao617ba132006-10-11 01:20:53 -07003772 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003773 *
Mingming Cao617ba132006-10-11 01:20:53 -07003774 * We block out ext4_get_block() block instantiations across the entire
3775 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003776 * simultaneously on behalf of the same inode.
3777 *
3778 * As we work through the truncate and commmit bits of it to the journal there
3779 * is one core, guiding principle: the file's tree must always be consistent on
3780 * disk. We must be able to restart the truncate after a crash.
3781 *
3782 * The file's tree may be transiently inconsistent in memory (although it
3783 * probably isn't), but whenever we close off and commit a journal transaction,
3784 * the contents of (the filesystem + the journal) must be consistent and
3785 * restartable. It's pretty simple, really: bottom up, right to left (although
3786 * left-to-right works OK too).
3787 *
3788 * Note that at recovery time, journal replay occurs *before* the restart of
3789 * truncate against the orphan inode list.
3790 *
3791 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003792 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003793 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003794 * ext4_truncate() to have another go. So there will be instantiated blocks
3795 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003796 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003797 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003798 */
Mingming Cao617ba132006-10-11 01:20:53 -07003799void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003800{
3801 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003802 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003803 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003804 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003805 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003806 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003807 Indirect chain[4];
3808 Indirect *partial;
3809 __le32 nr = 0;
3810 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003811 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003812 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003813
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003814 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003815 return;
3816
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003817 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003818 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003819 return;
3820 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003821
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003822 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003823 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003824 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003825
3826 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003827 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003828
Jan Karacf108bc2008-07-11 19:27:31 -04003829 if (inode->i_size & (blocksize - 1))
3830 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3831 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003832
Mingming Cao617ba132006-10-11 01:20:53 -07003833 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834 if (n == 0)
3835 goto out_stop; /* error */
3836
3837 /*
3838 * OK. This truncate is going to happen. We add the inode to the
3839 * orphan list, so that if this truncate spans multiple transactions,
3840 * and we crash, we will resume the truncate when the filesystem
3841 * recovers. It also marks the inode dirty, to catch the new size.
3842 *
3843 * Implication: the file must always be in a sane, consistent
3844 * truncatable state while each transaction commits.
3845 */
Mingming Cao617ba132006-10-11 01:20:53 -07003846 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003847 goto out_stop;
3848
3849 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04003850 * From here we block out all ext4_get_block() callers who want to
3851 * modify the block allocation tree.
3852 */
3853 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04003854
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003855 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04003856
Mingming Cao632eaea2008-07-11 19:27:31 -04003857 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003858 * The orphan list entry will now protect us from any crash which
3859 * occurs before the truncate completes, so it is now safe to propagate
3860 * the new, shorter inode size (held for now in i_size) into the
3861 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07003862 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003863 */
3864 ei->i_disksize = inode->i_size;
3865
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003866 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003867 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
3868 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003869 goto do_indirects;
3870 }
3871
Mingming Cao617ba132006-10-11 01:20:53 -07003872 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003873 /* Kill the top of shared branch (not detached) */
3874 if (nr) {
3875 if (partial == chain) {
3876 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07003877 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003878 &nr, &nr+1, (chain+n-1) - partial);
3879 *partial->p = 0;
3880 /*
3881 * We mark the inode dirty prior to restart,
3882 * and prior to stop. No need for it here.
3883 */
3884 } else {
3885 /* Shared branch grows from an indirect block */
3886 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003887 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003888 partial->p,
3889 partial->p+1, (chain+n-1) - partial);
3890 }
3891 }
3892 /* Clear the ends of indirect blocks on the shared branch */
3893 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07003894 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003895 (__le32*)partial->bh->b_data+addr_per_block,
3896 (chain+n-1) - partial);
3897 BUFFER_TRACE(partial->bh, "call brelse");
3898 brelse (partial->bh);
3899 partial--;
3900 }
3901do_indirects:
3902 /* Kill the remaining (whole) subtrees */
3903 switch (offsets[0]) {
3904 default:
Mingming Cao617ba132006-10-11 01:20:53 -07003905 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003906 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003907 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
3908 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003909 }
Mingming Cao617ba132006-10-11 01:20:53 -07003910 case EXT4_IND_BLOCK:
3911 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003912 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003913 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
3914 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003915 }
Mingming Cao617ba132006-10-11 01:20:53 -07003916 case EXT4_DIND_BLOCK:
3917 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003918 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003919 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
3920 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003921 }
Mingming Cao617ba132006-10-11 01:20:53 -07003922 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003923 ;
3924 }
3925
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003926 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003927 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003928 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003929
3930 /*
3931 * In a multi-transaction truncate, we only make the final transaction
3932 * synchronous
3933 */
3934 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003935 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003936out_stop:
3937 /*
3938 * If this was a simple ftruncate(), and the file will remain alive
3939 * then we need to clear up the orphan record which we created above.
3940 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07003941 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003942 * orphan info for us.
3943 */
3944 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003945 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946
Mingming Cao617ba132006-10-11 01:20:53 -07003947 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003948}
3949
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950/*
Mingming Cao617ba132006-10-11 01:20:53 -07003951 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003952 * underlying buffer_head on success. If 'in_mem' is true, we have all
3953 * data in memory that is needed to recreate the on-disk version of this
3954 * inode.
3955 */
Mingming Cao617ba132006-10-11 01:20:53 -07003956static int __ext4_get_inode_loc(struct inode *inode,
3957 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003959 struct ext4_group_desc *gdp;
3960 struct buffer_head *bh;
3961 struct super_block *sb = inode->i_sb;
3962 ext4_fsblk_t block;
3963 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003964
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003965 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003966 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003967 return -EIO;
3968
Theodore Ts'o240799c2008-10-09 23:53:47 -04003969 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3970 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3971 if (!gdp)
3972 return -EIO;
3973
3974 /*
3975 * Figure out the offset within the block group inode table
3976 */
3977 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
3978 inode_offset = ((inode->i_ino - 1) %
3979 EXT4_INODES_PER_GROUP(sb));
3980 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3981 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3982
3983 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04003985 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
3986 "inode block - inode=%lu, block=%llu",
3987 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003988 return -EIO;
3989 }
3990 if (!buffer_uptodate(bh)) {
3991 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003992
3993 /*
3994 * If the buffer has the write error flag, we have failed
3995 * to write out another inode in the same block. In this
3996 * case, we don't have to read the block because we may
3997 * read the old inode data successfully.
3998 */
3999 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4000 set_buffer_uptodate(bh);
4001
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004002 if (buffer_uptodate(bh)) {
4003 /* someone brought it uptodate while we waited */
4004 unlock_buffer(bh);
4005 goto has_buffer;
4006 }
4007
4008 /*
4009 * If we have all information of the inode in memory and this
4010 * is the only valid inode in the block, we need not read the
4011 * block.
4012 */
4013 if (in_mem) {
4014 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004015 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004016
Theodore Ts'o240799c2008-10-09 23:53:47 -04004017 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004018
4019 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004020 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004021 if (!bitmap_bh)
4022 goto make_io;
4023
4024 /*
4025 * If the inode bitmap isn't in cache then the
4026 * optimisation may end up performing two reads instead
4027 * of one, so skip it.
4028 */
4029 if (!buffer_uptodate(bitmap_bh)) {
4030 brelse(bitmap_bh);
4031 goto make_io;
4032 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004033 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004034 if (i == inode_offset)
4035 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004036 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004037 break;
4038 }
4039 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004040 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004041 /* all other inodes are free, so skip I/O */
4042 memset(bh->b_data, 0, bh->b_size);
4043 set_buffer_uptodate(bh);
4044 unlock_buffer(bh);
4045 goto has_buffer;
4046 }
4047 }
4048
4049make_io:
4050 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004051 * If we need to do any I/O, try to pre-readahead extra
4052 * blocks from the inode table.
4053 */
4054 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4055 ext4_fsblk_t b, end, table;
4056 unsigned num;
4057
4058 table = ext4_inode_table(sb, gdp);
4059 /* Make sure s_inode_readahead_blks is a power of 2 */
4060 while (EXT4_SB(sb)->s_inode_readahead_blks &
4061 (EXT4_SB(sb)->s_inode_readahead_blks-1))
4062 EXT4_SB(sb)->s_inode_readahead_blks =
4063 (EXT4_SB(sb)->s_inode_readahead_blks &
4064 (EXT4_SB(sb)->s_inode_readahead_blks-1));
4065 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4066 if (table > b)
4067 b = table;
4068 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4069 num = EXT4_INODES_PER_GROUP(sb);
4070 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4071 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004072 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004073 table += num / inodes_per_block;
4074 if (end > table)
4075 end = table;
4076 while (b <= end)
4077 sb_breadahead(sb, b++);
4078 }
4079
4080 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004081 * There are other valid inodes in the buffer, this inode
4082 * has in-inode xattrs, or we don't have this inode in memory.
4083 * Read the block from disk.
4084 */
4085 get_bh(bh);
4086 bh->b_end_io = end_buffer_read_sync;
4087 submit_bh(READ_META, bh);
4088 wait_on_buffer(bh);
4089 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004090 ext4_error(sb, __func__,
4091 "unable to read inode block - inode=%lu, "
4092 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004093 brelse(bh);
4094 return -EIO;
4095 }
4096 }
4097has_buffer:
4098 iloc->bh = bh;
4099 return 0;
4100}
4101
Mingming Cao617ba132006-10-11 01:20:53 -07004102int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004103{
4104 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004105 return __ext4_get_inode_loc(inode, iloc,
4106 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004107}
4108
Mingming Cao617ba132006-10-11 01:20:53 -07004109void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004110{
Mingming Cao617ba132006-10-11 01:20:53 -07004111 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004112
4113 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004114 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004115 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004116 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004117 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004118 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004119 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004120 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004121 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004122 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004123 inode->i_flags |= S_DIRSYNC;
4124}
4125
Jan Karaff9ddf72007-07-18 09:24:20 -04004126/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4127void ext4_get_inode_flags(struct ext4_inode_info *ei)
4128{
4129 unsigned int flags = ei->vfs_inode.i_flags;
4130
4131 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4132 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4133 if (flags & S_SYNC)
4134 ei->i_flags |= EXT4_SYNC_FL;
4135 if (flags & S_APPEND)
4136 ei->i_flags |= EXT4_APPEND_FL;
4137 if (flags & S_IMMUTABLE)
4138 ei->i_flags |= EXT4_IMMUTABLE_FL;
4139 if (flags & S_NOATIME)
4140 ei->i_flags |= EXT4_NOATIME_FL;
4141 if (flags & S_DIRSYNC)
4142 ei->i_flags |= EXT4_DIRSYNC_FL;
4143}
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004144static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
4145 struct ext4_inode_info *ei)
4146{
4147 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004148 struct inode *inode = &(ei->vfs_inode);
4149 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004150
4151 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4152 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4153 /* we are using combined 48 bit field */
4154 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4155 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004156 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4157 /* i_blocks represent file system block size */
4158 return i_blocks << (inode->i_blkbits - 9);
4159 } else {
4160 return i_blocks;
4161 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004162 } else {
4163 return le32_to_cpu(raw_inode->i_blocks_lo);
4164 }
4165}
Jan Karaff9ddf72007-07-18 09:24:20 -04004166
David Howells1d1fe1e2008-02-07 00:15:37 -08004167struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004168{
Mingming Cao617ba132006-10-11 01:20:53 -07004169 struct ext4_iloc iloc;
4170 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004171 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004172 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004173 struct inode *inode;
4174 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004175 int block;
4176
David Howells1d1fe1e2008-02-07 00:15:37 -08004177 inode = iget_locked(sb, ino);
4178 if (!inode)
4179 return ERR_PTR(-ENOMEM);
4180 if (!(inode->i_state & I_NEW))
4181 return inode;
4182
4183 ei = EXT4_I(inode);
Theodore Ts'o03010a32008-10-10 20:02:48 -04004184#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07004185 ei->i_acl = EXT4_ACL_NOT_CACHED;
4186 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004187#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188
David Howells1d1fe1e2008-02-07 00:15:37 -08004189 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4190 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004191 goto bad_inode;
4192 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004193 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004194 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4195 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4196 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004197 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004198 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4199 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4200 }
4201 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004202
4203 ei->i_state = 0;
4204 ei->i_dir_start_lookup = 0;
4205 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4206 /* We now have enough fields to check if the inode was active or not.
4207 * This is needed because nfsd might try to access dead inodes
4208 * the test is that same one that e2fsck uses
4209 * NeilBrown 1999oct15
4210 */
4211 if (inode->i_nlink == 0) {
4212 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004213 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004214 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004215 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004216 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004217 goto bad_inode;
4218 }
4219 /* The only unlinked inodes we let through here have
4220 * valid i_mode and are being read by the orphan
4221 * recovery code: that's fine, we're about to complete
4222 * the process of deleting those. */
4223 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004224 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004225 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004226 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004227 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004228 cpu_to_le32(EXT4_OS_HURD)) {
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004229 ei->i_file_acl |=
4230 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004231 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004232 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004233 ei->i_disksize = inode->i_size;
4234 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4235 ei->i_block_group = iloc.block_group;
4236 /*
4237 * NOTE! The in-memory inode i_data array is in little-endian order
4238 * even on big-endian machines: we do NOT byteswap the block numbers!
4239 */
Mingming Cao617ba132006-10-11 01:20:53 -07004240 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004241 ei->i_data[block] = raw_inode->i_block[block];
4242 INIT_LIST_HEAD(&ei->i_orphan);
4243
Eric Sandeen0040d982008-02-05 22:36:43 -05004244 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004245 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004246 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004247 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004248 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004249 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004250 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004251 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252 if (ei->i_extra_isize == 0) {
4253 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004254 ei->i_extra_isize = sizeof(struct ext4_inode) -
4255 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004256 } else {
4257 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004258 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004259 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004260 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
4261 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004262 }
4263 } else
4264 ei->i_extra_isize = 0;
4265
Kalpak Shahef7f3832007-07-18 09:15:20 -04004266 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4267 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4268 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4269 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4270
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004271 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4272 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4273 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4274 inode->i_version |=
4275 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4276 }
4277
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004278 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004279 inode->i_op = &ext4_file_inode_operations;
4280 inode->i_fop = &ext4_file_operations;
4281 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004282 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004283 inode->i_op = &ext4_dir_inode_operations;
4284 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004285 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004286 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004287 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004288 nd_terminate_link(ei->i_data, inode->i_size,
4289 sizeof(ei->i_data) - 1);
4290 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004291 inode->i_op = &ext4_symlink_inode_operations;
4292 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004293 }
4294 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004295 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004296 if (raw_inode->i_block[0])
4297 init_special_inode(inode, inode->i_mode,
4298 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4299 else
4300 init_special_inode(inode, inode->i_mode,
4301 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4302 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004303 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004304 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004305 unlock_new_inode(inode);
4306 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004307
4308bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004309 iget_failed(inode);
4310 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004311}
4312
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004313static int ext4_inode_blocks_set(handle_t *handle,
4314 struct ext4_inode *raw_inode,
4315 struct ext4_inode_info *ei)
4316{
4317 struct inode *inode = &(ei->vfs_inode);
4318 u64 i_blocks = inode->i_blocks;
4319 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004320
4321 if (i_blocks <= ~0U) {
4322 /*
4323 * i_blocks can be represnted in a 32 bit variable
4324 * as multiple of 512 bytes
4325 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004326 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004327 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004328 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004329 return 0;
4330 }
4331 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4332 return -EFBIG;
4333
4334 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004335 /*
4336 * i_blocks can be represented in a 48 bit variable
4337 * as multiple of 512 bytes
4338 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004339 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004340 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004341 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004342 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004343 ei->i_flags |= EXT4_HUGE_FILE_FL;
4344 /* i_block is stored in file system block size */
4345 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4346 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4347 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004348 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004349 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004350}
4351
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004352/*
4353 * Post the struct inode info into an on-disk inode location in the
4354 * buffer-cache. This gobbles the caller's reference to the
4355 * buffer_head in the inode location struct.
4356 *
4357 * The caller must have write access to iloc->bh.
4358 */
Mingming Cao617ba132006-10-11 01:20:53 -07004359static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004360 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004361 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004362{
Mingming Cao617ba132006-10-11 01:20:53 -07004363 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4364 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365 struct buffer_head *bh = iloc->bh;
4366 int err = 0, rc, block;
4367
4368 /* For fields not not tracking in the in-memory inode,
4369 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004370 if (ei->i_state & EXT4_STATE_NEW)
4371 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004372
Jan Karaff9ddf72007-07-18 09:24:20 -04004373 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004374 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004375 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004376 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4377 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4378/*
4379 * Fix up interoperability with old kernels. Otherwise, old inodes get
4380 * re-used with the upper 16 bits of the uid/gid intact
4381 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004382 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004383 raw_inode->i_uid_high =
4384 cpu_to_le16(high_16_bits(inode->i_uid));
4385 raw_inode->i_gid_high =
4386 cpu_to_le16(high_16_bits(inode->i_gid));
4387 } else {
4388 raw_inode->i_uid_high = 0;
4389 raw_inode->i_gid_high = 0;
4390 }
4391 } else {
4392 raw_inode->i_uid_low =
4393 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4394 raw_inode->i_gid_low =
4395 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4396 raw_inode->i_uid_high = 0;
4397 raw_inode->i_gid_high = 0;
4398 }
4399 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004400
4401 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4402 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4403 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4404 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4405
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004406 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4407 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004408 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004409 /* clear the migrate flag in the raw_inode */
4410 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004411 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4412 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004413 raw_inode->i_file_acl_high =
4414 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004415 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004416 ext4_isize_set(raw_inode, ei->i_disksize);
4417 if (ei->i_disksize > 0x7fffffffULL) {
4418 struct super_block *sb = inode->i_sb;
4419 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4420 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4421 EXT4_SB(sb)->s_es->s_rev_level ==
4422 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4423 /* If this is the first large file
4424 * created, add a flag to the superblock.
4425 */
4426 err = ext4_journal_get_write_access(handle,
4427 EXT4_SB(sb)->s_sbh);
4428 if (err)
4429 goto out_brelse;
4430 ext4_update_dynamic_rev(sb);
4431 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004432 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004433 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004434 ext4_handle_sync(handle);
4435 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004436 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004437 }
4438 }
4439 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4440 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4441 if (old_valid_dev(inode->i_rdev)) {
4442 raw_inode->i_block[0] =
4443 cpu_to_le32(old_encode_dev(inode->i_rdev));
4444 raw_inode->i_block[1] = 0;
4445 } else {
4446 raw_inode->i_block[0] = 0;
4447 raw_inode->i_block[1] =
4448 cpu_to_le32(new_encode_dev(inode->i_rdev));
4449 raw_inode->i_block[2] = 0;
4450 }
Mingming Cao617ba132006-10-11 01:20:53 -07004451 } else for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004452 raw_inode->i_block[block] = ei->i_data[block];
4453
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004454 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4455 if (ei->i_extra_isize) {
4456 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4457 raw_inode->i_version_hi =
4458 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004459 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004460 }
4461
Frank Mayhar03901312009-01-07 00:06:22 -05004462 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4463 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004464 if (!err)
4465 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004466 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004467
4468out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004469 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004470 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004471 return err;
4472}
4473
4474/*
Mingming Cao617ba132006-10-11 01:20:53 -07004475 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004476 *
4477 * We are called from a few places:
4478 *
4479 * - Within generic_file_write() for O_SYNC files.
4480 * Here, there will be no transaction running. We wait for any running
4481 * trasnaction to commit.
4482 *
4483 * - Within sys_sync(), kupdate and such.
4484 * We wait on commit, if tol to.
4485 *
4486 * - Within prune_icache() (PF_MEMALLOC == true)
4487 * Here we simply return. We can't afford to block kswapd on the
4488 * journal commit.
4489 *
4490 * In all cases it is actually safe for us to return without doing anything,
4491 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004492 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004493 * knfsd.
4494 *
4495 * Note that we are absolutely dependent upon all inode dirtiers doing the
4496 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4497 * which we are interested.
4498 *
4499 * It would be a bug for them to not do this. The code:
4500 *
4501 * mark_inode_dirty(inode)
4502 * stuff();
4503 * inode->i_size = expr;
4504 *
4505 * is in error because a kswapd-driven write_inode() could occur while
4506 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4507 * will no longer be on the superblock's dirty inode list.
4508 */
Mingming Cao617ba132006-10-11 01:20:53 -07004509int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004510{
4511 if (current->flags & PF_MEMALLOC)
4512 return 0;
4513
Mingming Cao617ba132006-10-11 01:20:53 -07004514 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004515 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004516 dump_stack();
4517 return -EIO;
4518 }
4519
4520 if (!wait)
4521 return 0;
4522
Mingming Cao617ba132006-10-11 01:20:53 -07004523 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004524}
4525
Frank Mayhar03901312009-01-07 00:06:22 -05004526int __ext4_write_dirty_metadata(struct inode *inode, struct buffer_head *bh)
4527{
4528 int err = 0;
4529
4530 mark_buffer_dirty(bh);
4531 if (inode && inode_needs_sync(inode)) {
4532 sync_dirty_buffer(bh);
4533 if (buffer_req(bh) && !buffer_uptodate(bh)) {
4534 ext4_error(inode->i_sb, __func__,
4535 "IO error syncing inode, "
4536 "inode=%lu, block=%llu",
4537 inode->i_ino,
4538 (unsigned long long)bh->b_blocknr);
4539 err = -EIO;
4540 }
4541 }
4542 return err;
4543}
4544
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004545/*
Mingming Cao617ba132006-10-11 01:20:53 -07004546 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004547 *
4548 * Called from notify_change.
4549 *
4550 * We want to trap VFS attempts to truncate the file as soon as
4551 * possible. In particular, we want to make sure that when the VFS
4552 * shrinks i_size, we put the inode on the orphan list and modify
4553 * i_disksize immediately, so that during the subsequent flushing of
4554 * dirty pages and freeing of disk blocks, we can guarantee that any
4555 * commit will leave the blocks being flushed in an unused state on
4556 * disk. (On recovery, the inode will get truncated and the blocks will
4557 * be freed, so we have a strong guarantee that no future commit will
4558 * leave these blocks visible to the user.)
4559 *
Jan Kara678aaf42008-07-11 19:27:31 -04004560 * Another thing we have to assure is that if we are in ordered mode
4561 * and inode is still attached to the committing transaction, we must
4562 * we start writeout of all the dirty pages which are being truncated.
4563 * This way we are sure that all the data written in the previous
4564 * transaction are already on disk (truncate waits for pages under
4565 * writeback).
4566 *
4567 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004568 */
Mingming Cao617ba132006-10-11 01:20:53 -07004569int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004570{
4571 struct inode *inode = dentry->d_inode;
4572 int error, rc = 0;
4573 const unsigned int ia_valid = attr->ia_valid;
4574
4575 error = inode_change_ok(inode, attr);
4576 if (error)
4577 return error;
4578
4579 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4580 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4581 handle_t *handle;
4582
4583 /* (user+group)*(old+new) structure, inode write (sb,
4584 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004585 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4586 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004587 if (IS_ERR(handle)) {
4588 error = PTR_ERR(handle);
4589 goto err_out;
4590 }
4591 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
4592 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004593 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004594 return error;
4595 }
4596 /* Update corresponding info in inode so that everything is in
4597 * one transaction */
4598 if (attr->ia_valid & ATTR_UID)
4599 inode->i_uid = attr->ia_uid;
4600 if (attr->ia_valid & ATTR_GID)
4601 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004602 error = ext4_mark_inode_dirty(handle, inode);
4603 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004604 }
4605
Eric Sandeene2b46572008-01-28 23:58:27 -05004606 if (attr->ia_valid & ATTR_SIZE) {
4607 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4608 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4609
4610 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4611 error = -EFBIG;
4612 goto err_out;
4613 }
4614 }
4615 }
4616
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004617 if (S_ISREG(inode->i_mode) &&
4618 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4619 handle_t *handle;
4620
Mingming Cao617ba132006-10-11 01:20:53 -07004621 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004622 if (IS_ERR(handle)) {
4623 error = PTR_ERR(handle);
4624 goto err_out;
4625 }
4626
Mingming Cao617ba132006-10-11 01:20:53 -07004627 error = ext4_orphan_add(handle, inode);
4628 EXT4_I(inode)->i_disksize = attr->ia_size;
4629 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004630 if (!error)
4631 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004632 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004633
4634 if (ext4_should_order_data(inode)) {
4635 error = ext4_begin_ordered_truncate(inode,
4636 attr->ia_size);
4637 if (error) {
4638 /* Do as much error cleanup as possible */
4639 handle = ext4_journal_start(inode, 3);
4640 if (IS_ERR(handle)) {
4641 ext4_orphan_del(NULL, inode);
4642 goto err_out;
4643 }
4644 ext4_orphan_del(handle, inode);
4645 ext4_journal_stop(handle);
4646 goto err_out;
4647 }
4648 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004649 }
4650
4651 rc = inode_setattr(inode, attr);
4652
Mingming Cao617ba132006-10-11 01:20:53 -07004653 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004654 * transaction handle at all, we need to clean up the in-core
4655 * orphan list manually. */
4656 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004657 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004658
4659 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004660 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004661
4662err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004663 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004664 if (!error)
4665 error = rc;
4666 return error;
4667}
4668
Mingming Cao3e3398a2008-07-11 19:27:31 -04004669int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4670 struct kstat *stat)
4671{
4672 struct inode *inode;
4673 unsigned long delalloc_blocks;
4674
4675 inode = dentry->d_inode;
4676 generic_fillattr(inode, stat);
4677
4678 /*
4679 * We can't update i_blocks if the block allocation is delayed
4680 * otherwise in the case of system crash before the real block
4681 * allocation is done, we will have i_blocks inconsistent with
4682 * on-disk file blocks.
4683 * We always keep i_blocks updated together with real
4684 * allocation. But to not confuse with user, stat
4685 * will return the blocks that include the delayed allocation
4686 * blocks for this file.
4687 */
4688 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4689 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4690 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4691
4692 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4693 return 0;
4694}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004695
Mingming Caoa02908f2008-08-19 22:16:07 -04004696static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4697 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004698{
Mingming Caoa02908f2008-08-19 22:16:07 -04004699 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004700
Mingming Caoa02908f2008-08-19 22:16:07 -04004701 /* if nrblocks are contiguous */
4702 if (chunk) {
4703 /*
4704 * With N contiguous data blocks, it need at most
4705 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4706 * 2 dindirect blocks
4707 * 1 tindirect block
4708 */
4709 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4710 return indirects + 3;
4711 }
4712 /*
4713 * if nrblocks are not contiguous, worse case, each block touch
4714 * a indirect block, and each indirect block touch a double indirect
4715 * block, plus a triple indirect block
4716 */
4717 indirects = nrblocks * 2 + 1;
4718 return indirects;
4719}
Alex Tomasa86c6182006-10-11 01:21:03 -07004720
Mingming Caoa02908f2008-08-19 22:16:07 -04004721static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4722{
4723 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004724 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4725 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004726}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004727
Mingming Caoa02908f2008-08-19 22:16:07 -04004728/*
4729 * Account for index blocks, block groups bitmaps and block group
4730 * descriptor blocks if modify datablocks and index blocks
4731 * worse case, the indexs blocks spread over different block groups
4732 *
4733 * If datablocks are discontiguous, they are possible to spread over
4734 * different block groups too. If they are contiugous, with flexbg,
4735 * they could still across block group boundary.
4736 *
4737 * Also account for superblock, inode, quota and xattr blocks
4738 */
4739int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4740{
4741 int groups, gdpblocks;
4742 int idxblocks;
4743 int ret = 0;
4744
4745 /*
4746 * How many index blocks need to touch to modify nrblocks?
4747 * The "Chunk" flag indicating whether the nrblocks is
4748 * physically contiguous on disk
4749 *
4750 * For Direct IO and fallocate, they calls get_block to allocate
4751 * one single extent at a time, so they could set the "Chunk" flag
4752 */
4753 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4754
4755 ret = idxblocks;
4756
4757 /*
4758 * Now let's see how many group bitmaps and group descriptors need
4759 * to account
4760 */
4761 groups = idxblocks;
4762 if (chunk)
4763 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004764 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004765 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004766
Mingming Caoa02908f2008-08-19 22:16:07 -04004767 gdpblocks = groups;
4768 if (groups > EXT4_SB(inode->i_sb)->s_groups_count)
4769 groups = EXT4_SB(inode->i_sb)->s_groups_count;
4770 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4771 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4772
4773 /* bitmaps and block group descriptor blocks */
4774 ret += groups + gdpblocks;
4775
4776 /* Blocks for super block, inode, quota and xattr blocks */
4777 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004778
4779 return ret;
4780}
4781
4782/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004783 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004784 * the modification of a single pages into a single transaction,
4785 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004786 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004787 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004788 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004789 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004790 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004791 */
4792int ext4_writepage_trans_blocks(struct inode *inode)
4793{
4794 int bpp = ext4_journal_blocks_per_page(inode);
4795 int ret;
4796
4797 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4798
4799 /* Account for data blocks for journalled mode */
4800 if (ext4_should_journal_data(inode))
4801 ret += bpp;
4802 return ret;
4803}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004804
4805/*
4806 * Calculate the journal credits for a chunk of data modification.
4807 *
4808 * This is called from DIO, fallocate or whoever calling
4809 * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks.
4810 *
4811 * journal buffers for data blocks are not included here, as DIO
4812 * and fallocate do no need to journal data buffers.
4813 */
4814int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4815{
4816 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4817}
4818
Mingming Caoa02908f2008-08-19 22:16:07 -04004819/*
Mingming Cao617ba132006-10-11 01:20:53 -07004820 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004821 * Give this, we know that the caller already has write access to iloc->bh.
4822 */
Mingming Cao617ba132006-10-11 01:20:53 -07004823int ext4_mark_iloc_dirty(handle_t *handle,
4824 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004825{
4826 int err = 0;
4827
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004828 if (test_opt(inode->i_sb, I_VERSION))
4829 inode_inc_iversion(inode);
4830
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004831 /* the do_update_inode consumes one bh->b_count */
4832 get_bh(iloc->bh);
4833
Mingming Caodab291a2006-10-11 01:21:01 -07004834 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004835 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004836 put_bh(iloc->bh);
4837 return err;
4838}
4839
4840/*
4841 * On success, We end up with an outstanding reference count against
4842 * iloc->bh. This _must_ be cleaned up later.
4843 */
4844
4845int
Mingming Cao617ba132006-10-11 01:20:53 -07004846ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4847 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004848{
Frank Mayhar03901312009-01-07 00:06:22 -05004849 int err;
4850
4851 err = ext4_get_inode_loc(inode, iloc);
4852 if (!err) {
4853 BUFFER_TRACE(iloc->bh, "get_write_access");
4854 err = ext4_journal_get_write_access(handle, iloc->bh);
4855 if (err) {
4856 brelse(iloc->bh);
4857 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004858 }
4859 }
Mingming Cao617ba132006-10-11 01:20:53 -07004860 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861 return err;
4862}
4863
4864/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004865 * Expand an inode by new_extra_isize bytes.
4866 * Returns 0 on success or negative error number on failure.
4867 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004868static int ext4_expand_extra_isize(struct inode *inode,
4869 unsigned int new_extra_isize,
4870 struct ext4_iloc iloc,
4871 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004872{
4873 struct ext4_inode *raw_inode;
4874 struct ext4_xattr_ibody_header *header;
4875 struct ext4_xattr_entry *entry;
4876
4877 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4878 return 0;
4879
4880 raw_inode = ext4_raw_inode(&iloc);
4881
4882 header = IHDR(inode, raw_inode);
4883 entry = IFIRST(header);
4884
4885 /* No extended attributes present */
4886 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
4887 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
4888 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4889 new_extra_isize);
4890 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4891 return 0;
4892 }
4893
4894 /* try to expand with EAs present */
4895 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4896 raw_inode, handle);
4897}
4898
4899/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004900 * What we do here is to mark the in-core inode as clean with respect to inode
4901 * dirtiness (it may still be data-dirty).
4902 * This means that the in-core inode may be reaped by prune_icache
4903 * without having to perform any I/O. This is a very good thing,
4904 * because *any* task may call prune_icache - even ones which
4905 * have a transaction open against a different journal.
4906 *
4907 * Is this cheating? Not really. Sure, we haven't written the
4908 * inode out, but prune_icache isn't a user-visible syncing function.
4909 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4910 * we start and wait on commits.
4911 *
4912 * Is this efficient/effective? Well, we're being nice to the system
4913 * by cleaning up our inodes proactively so they can be reaped
4914 * without I/O. But we are potentially leaving up to five seconds'
4915 * worth of inodes floating about which prune_icache wants us to
4916 * write out. One way to fix that would be to get prune_icache()
4917 * to do a write_super() to free up some memory. It has the desired
4918 * effect.
4919 */
Mingming Cao617ba132006-10-11 01:20:53 -07004920int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004921{
Mingming Cao617ba132006-10-11 01:20:53 -07004922 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004923 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4924 static unsigned int mnt_count;
4925 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004926
4927 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07004928 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05004929 if (ext4_handle_valid(handle) &&
4930 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004931 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
4932 /*
4933 * We need extra buffer credits since we may write into EA block
4934 * with this same handle. If journal_extend fails, then it will
4935 * only result in a minor loss of functionality for that inode.
4936 * If this is felt to be critical, then e2fsck should be run to
4937 * force a large enough s_min_extra_isize.
4938 */
4939 if ((jbd2_journal_extend(handle,
4940 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
4941 ret = ext4_expand_extra_isize(inode,
4942 sbi->s_want_extra_isize,
4943 iloc, handle);
4944 if (ret) {
4945 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004946 if (mnt_count !=
4947 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004948 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004949 "Unable to expand inode %lu. Delete"
4950 " some EAs or run e2fsck.",
4951 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004952 mnt_count =
4953 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004954 }
4955 }
4956 }
4957 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004958 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004959 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004960 return err;
4961}
4962
4963/*
Mingming Cao617ba132006-10-11 01:20:53 -07004964 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004965 *
4966 * We're really interested in the case where a file is being extended.
4967 * i_size has been changed by generic_commit_write() and we thus need
4968 * to include the updated inode in the current transaction.
4969 *
4970 * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
4971 * are allocated to the file.
4972 *
4973 * If the inode is marked synchronous, we don't honour that here - doing
4974 * so would cause a commit on atime updates, which we don't bother doing.
4975 * We handle synchronous inodes at the highest possible level.
4976 */
Mingming Cao617ba132006-10-11 01:20:53 -07004977void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004978{
Mingming Cao617ba132006-10-11 01:20:53 -07004979 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004980 handle_t *handle;
4981
Frank Mayhar03901312009-01-07 00:06:22 -05004982 if (!ext4_handle_valid(current_handle)) {
4983 ext4_mark_inode_dirty(current_handle, inode);
4984 return;
4985 }
4986
Mingming Cao617ba132006-10-11 01:20:53 -07004987 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004988 if (IS_ERR(handle))
4989 goto out;
4990 if (current_handle &&
4991 current_handle->h_transaction != handle->h_transaction) {
4992 /* This task has a transaction open against a different fs */
4993 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04004994 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004995 } else {
4996 jbd_debug(5, "marking dirty. outer handle=%p\n",
4997 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07004998 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004999 }
Mingming Cao617ba132006-10-11 01:20:53 -07005000 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005001out:
5002 return;
5003}
5004
5005#if 0
5006/*
5007 * Bind an inode's backing buffer_head into this transaction, to prevent
5008 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005009 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005010 * returns no iloc structure, so the caller needs to repeat the iloc
5011 * lookup to mark the inode dirty later.
5012 */
Mingming Cao617ba132006-10-11 01:20:53 -07005013static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005014{
Mingming Cao617ba132006-10-11 01:20:53 -07005015 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005016
5017 int err = 0;
5018 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005019 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005020 if (!err) {
5021 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005022 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005023 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005024 err = ext4_handle_dirty_metadata(handle,
5025 inode,
5026 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005027 brelse(iloc.bh);
5028 }
5029 }
Mingming Cao617ba132006-10-11 01:20:53 -07005030 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005031 return err;
5032}
5033#endif
5034
Mingming Cao617ba132006-10-11 01:20:53 -07005035int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005036{
5037 journal_t *journal;
5038 handle_t *handle;
5039 int err;
5040
5041 /*
5042 * We have to be very careful here: changing a data block's
5043 * journaling status dynamically is dangerous. If we write a
5044 * data block to the journal, change the status and then delete
5045 * that block, we risk forgetting to revoke the old log record
5046 * from the journal and so a subsequent replay can corrupt data.
5047 * So, first we make sure that the journal is empty and that
5048 * nobody is changing anything.
5049 */
5050
Mingming Cao617ba132006-10-11 01:20:53 -07005051 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005052 if (!journal)
5053 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005054 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005055 return -EROFS;
5056
Mingming Caodab291a2006-10-11 01:21:01 -07005057 jbd2_journal_lock_updates(journal);
5058 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005059
5060 /*
5061 * OK, there are no updates running now, and all cached data is
5062 * synced to disk. We are now in a completely consistent state
5063 * which doesn't have anything in the journal, and we know that
5064 * no filesystem updates are running, so it is safe to modify
5065 * the inode's in-core data-journaling state flag now.
5066 */
5067
5068 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005069 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005070 else
Mingming Cao617ba132006-10-11 01:20:53 -07005071 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5072 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005073
Mingming Caodab291a2006-10-11 01:21:01 -07005074 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005075
5076 /* Finally we can mark the inode as dirty. */
5077
Mingming Cao617ba132006-10-11 01:20:53 -07005078 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005079 if (IS_ERR(handle))
5080 return PTR_ERR(handle);
5081
Mingming Cao617ba132006-10-11 01:20:53 -07005082 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005083 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005084 ext4_journal_stop(handle);
5085 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005086
5087 return err;
5088}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005089
5090static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5091{
5092 return !buffer_mapped(bh);
5093}
5094
5095int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page)
5096{
5097 loff_t size;
5098 unsigned long len;
5099 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005100 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005101 struct file *file = vma->vm_file;
5102 struct inode *inode = file->f_path.dentry->d_inode;
5103 struct address_space *mapping = inode->i_mapping;
5104
5105 /*
5106 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5107 * get i_mutex because we are already holding mmap_sem.
5108 */
5109 down_read(&inode->i_alloc_sem);
5110 size = i_size_read(inode);
5111 if (page->mapping != mapping || size <= page_offset(page)
5112 || !PageUptodate(page)) {
5113 /* page got truncated from under us? */
5114 goto out_unlock;
5115 }
5116 ret = 0;
5117 if (PageMappedToDisk(page))
5118 goto out_unlock;
5119
5120 if (page->index == size >> PAGE_CACHE_SHIFT)
5121 len = size & ~PAGE_CACHE_MASK;
5122 else
5123 len = PAGE_CACHE_SIZE;
5124
5125 if (page_has_buffers(page)) {
5126 /* return if we have all the buffers mapped */
5127 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5128 ext4_bh_unmapped))
5129 goto out_unlock;
5130 }
5131 /*
5132 * OK, we need to fill the hole... Do write_begin write_end
5133 * to do block allocation/reservation.We are not holding
5134 * inode.i__mutex here. That allow * parallel write_begin,
5135 * write_end call. lock_page prevent this from happening
5136 * on the same page though
5137 */
5138 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005139 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005140 if (ret < 0)
5141 goto out_unlock;
5142 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005143 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005144 if (ret < 0)
5145 goto out_unlock;
5146 ret = 0;
5147out_unlock:
5148 up_read(&inode->i_alloc_sem);
5149 return ret;
5150}