blob: c77a7ac753f379efff52fd2283d7592777d07f33 [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{
50 return jbd2_journal_begin_ordered_truncate(&EXT4_I(inode)->jinode,
51 new_size);
52}
53
Alex Tomas64769242008-07-11 19:27:31 -040054static void ext4_invalidatepage(struct page *page, unsigned long offset);
55
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056/*
57 * Test whether an inode is a fast symlink.
58 */
Mingming Cao617ba132006-10-11 01:20:53 -070059static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060{
Mingming Cao617ba132006-10-11 01:20:53 -070061 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 (inode->i_sb->s_blocksize >> 9) : 0;
63
64 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
65}
66
67/*
Mingming Cao617ba132006-10-11 01:20:53 -070068 * The ext4 forget function must perform a revoke if we are freeing data
Dave Kleikampac27a0e2006-10-11 01:20:50 -070069 * which has been journaled. Metadata (eg. indirect blocks) must be
70 * revoked in all cases.
71 *
72 * "bh" may be NULL: a metadata block may have been freed from memory
73 * but there may still be a record of it in the journal, and that record
74 * still needs to be revoked.
75 */
Mingming Cao617ba132006-10-11 01:20:53 -070076int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
77 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070078{
79 int err;
80
81 might_sleep();
82
83 BUFFER_TRACE(bh, "enter");
84
85 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
86 "data mode %lx\n",
87 bh, is_metadata, inode->i_mode,
88 test_opt(inode->i_sb, DATA_FLAGS));
89
90 /* Never use the revoke function if we are doing full data
91 * journaling: there is no need to, and a V1 superblock won't
92 * support it. Otherwise, only skip the revoke on un-journaled
93 * data blocks. */
94
Mingming Cao617ba132006-10-11 01:20:53 -070095 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
96 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -070098 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -070099 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100 }
101 return 0;
102 }
103
104 /*
105 * data!=journal && (is_metadata || should_journal_data(inode))
106 */
Mingming Cao617ba132006-10-11 01:20:53 -0700107 BUFFER_TRACE(bh, "call ext4_journal_revoke");
108 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700109 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400110 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111 "error %d when attempting revoke", err);
112 BUFFER_TRACE(bh, "exit");
113 return err;
114}
115
116/*
117 * Work out how many blocks we need to proceed with the next chunk of a
118 * truncate transaction.
119 */
120static unsigned long blocks_for_truncate(struct inode *inode)
121{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500122 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123
124 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
125
126 /* Give ourselves just enough room to cope with inodes in which
127 * i_blocks is corrupt: we've seen disk corruptions in the past
128 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700129 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130 * will go a bit crazy if that happens, but at least we should
131 * try not to panic the whole kernel. */
132 if (needed < 2)
133 needed = 2;
134
135 /* But we need to bound the transaction so we don't overflow the
136 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700137 if (needed > EXT4_MAX_TRANS_DATA)
138 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139
Mingming Cao617ba132006-10-11 01:20:53 -0700140 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141}
142
143/*
144 * Truncate transactions can be complex and absolutely huge. So we need to
145 * be able to restart the transaction at a conventient checkpoint to make
146 * sure we don't overflow the journal.
147 *
148 * start_transaction gets us a new handle for a truncate transaction,
149 * and extend_transaction tries to extend the existing one a bit. If
150 * extend fails, we need to propagate the failure up and restart the
151 * transaction in the top-level truncate loop. --sct
152 */
153static handle_t *start_transaction(struct inode *inode)
154{
155 handle_t *result;
156
Mingming Cao617ba132006-10-11 01:20:53 -0700157 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158 if (!IS_ERR(result))
159 return result;
160
Mingming Cao617ba132006-10-11 01:20:53 -0700161 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162 return result;
163}
164
165/*
166 * Try to extend this transaction for the purposes of truncation.
167 *
168 * Returns 0 if we managed to create more room. If we can't create more
169 * room, and the transaction must be restarted we return 1.
170 */
171static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
172{
Mingming Cao617ba132006-10-11 01:20:53 -0700173 if (handle->h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700175 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 return 0;
177 return 1;
178}
179
180/*
181 * Restart the transaction associated with *handle. This does a commit,
182 * so before we call here everything must be consistently dirtied against
183 * this transaction.
184 */
Mingming Cao617ba132006-10-11 01:20:53 -0700185static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186{
187 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700188 return ext4_journal_restart(handle, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189}
190
191/*
192 * Called at the last iput() if i_nlink is zero.
193 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400194void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195{
196 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400197 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198
Jan Kara678aaf42008-07-11 19:27:31 -0400199 if (ext4_should_order_data(inode))
200 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201 truncate_inode_pages(&inode->i_data, 0);
202
203 if (is_bad_inode(inode))
204 goto no_delete;
205
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400206 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400208 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209 /*
210 * If we're going to skip the normal cleanup, we still need to
211 * make sure that the in-core orphan linked list is properly
212 * cleaned up.
213 */
Mingming Cao617ba132006-10-11 01:20:53 -0700214 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 goto no_delete;
216 }
217
218 if (IS_SYNC(inode))
219 handle->h_sync = 1;
220 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400221 err = ext4_mark_inode_dirty(handle, inode);
222 if (err) {
223 ext4_warning(inode->i_sb, __func__,
224 "couldn't mark inode dirty (err %d)", err);
225 goto stop_handle;
226 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700228 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400229
230 /*
231 * ext4_ext_truncate() doesn't reserve any slop when it
232 * restarts journal transactions; therefore there may not be
233 * enough credits left in the handle to remove the inode from
234 * the orphan list and set the dtime field.
235 */
236 if (handle->h_buffer_credits < 3) {
237 err = ext4_journal_extend(handle, 3);
238 if (err > 0)
239 err = ext4_journal_restart(handle, 3);
240 if (err != 0) {
241 ext4_warning(inode->i_sb, __func__,
242 "couldn't extend journal (err %d)", err);
243 stop_handle:
244 ext4_journal_stop(handle);
245 goto no_delete;
246 }
247 }
248
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700250 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700252 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700254 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255 * (Well, we could do this if we need to, but heck - it works)
256 */
Mingming Cao617ba132006-10-11 01:20:53 -0700257 ext4_orphan_del(handle, inode);
258 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259
260 /*
261 * One subtle ordering requirement: if anything has gone wrong
262 * (transaction abort, IO errors, whatever), then we can still
263 * do these next steps (the fs will already have been marked as
264 * having errors), but we can't free the inode if the mark_dirty
265 * fails.
266 */
Mingming Cao617ba132006-10-11 01:20:53 -0700267 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 /* If that failed, just do the required in-core inode clear. */
269 clear_inode(inode);
270 else
Mingming Cao617ba132006-10-11 01:20:53 -0700271 ext4_free_inode(handle, inode);
272 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 return;
274no_delete:
275 clear_inode(inode); /* We must guarantee clearing of inode... */
276}
277
278typedef struct {
279 __le32 *p;
280 __le32 key;
281 struct buffer_head *bh;
282} Indirect;
283
284static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
285{
286 p->key = *(p->p = v);
287 p->bh = bh;
288}
289
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290/**
Mingming Cao617ba132006-10-11 01:20:53 -0700291 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 * @inode: inode in question (we are only interested in its superblock)
293 * @i_block: block number to be parsed
294 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400295 * @boundary: set this non-zero if the referred-to block is likely to be
296 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 *
Mingming Cao617ba132006-10-11 01:20:53 -0700298 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 * for UNIX filesystems - tree of pointers anchored in the inode, with
300 * data blocks at leaves and indirect blocks in intermediate nodes.
301 * This function translates the block number into path in that tree -
302 * return value is the path length and @offsets[n] is the offset of
303 * pointer to (n+1)th node in the nth one. If @block is out of range
304 * (negative or too large) warning is printed and zero returned.
305 *
306 * Note: function doesn't find node addresses, so no IO is needed. All
307 * we need to know is the capacity of indirect blocks (taken from the
308 * inode->i_sb).
309 */
310
311/*
312 * Portability note: the last comparison (check that we fit into triple
313 * indirect block) is spelled differently, because otherwise on an
314 * architecture with 32-bit longs and 8Kb pages we might get into trouble
315 * if our filesystem had 8Kb blocks. We might use long long, but that would
316 * kill us on x86. Oh, well, at least the sign propagation does not matter -
317 * i_block would have to be negative in the very beginning, so we would not
318 * get there at all.
319 */
320
Mingming Cao617ba132006-10-11 01:20:53 -0700321static int ext4_block_to_path(struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500322 ext4_lblk_t i_block,
323 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324{
Mingming Cao617ba132006-10-11 01:20:53 -0700325 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
326 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
327 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 indirect_blocks = ptrs,
329 double_blocks = (1 << (ptrs_bits * 2));
330 int n = 0;
331 int final = 0;
332
333 if (i_block < 0) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400334 ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335 } else if (i_block < direct_blocks) {
336 offsets[n++] = i_block;
337 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400338 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700339 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 offsets[n++] = i_block;
341 final = ptrs;
342 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700343 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344 offsets[n++] = i_block >> ptrs_bits;
345 offsets[n++] = i_block & (ptrs - 1);
346 final = ptrs;
347 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700348 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 offsets[n++] = i_block >> (ptrs_bits * 2);
350 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
351 offsets[n++] = i_block & (ptrs - 1);
352 final = ptrs;
353 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500354 ext4_warning(inode->i_sb, "ext4_block_to_path",
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500355 "block %lu > max",
Eric Sandeene2b46572008-01-28 23:58:27 -0500356 i_block + direct_blocks +
357 indirect_blocks + double_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 }
359 if (boundary)
360 *boundary = final - 1 - (i_block & (ptrs - 1));
361 return n;
362}
363
364/**
Mingming Cao617ba132006-10-11 01:20:53 -0700365 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 * @inode: inode in question
367 * @depth: depth of the chain (1 - direct pointer, etc.)
368 * @offsets: offsets of pointers in inode/indirect blocks
369 * @chain: place to store the result
370 * @err: here we store the error value
371 *
372 * Function fills the array of triples <key, p, bh> and returns %NULL
373 * if everything went OK or the pointer to the last filled triple
374 * (incomplete one) otherwise. Upon the return chain[i].key contains
375 * the number of (i+1)-th block in the chain (as it is stored in memory,
376 * i.e. little-endian 32-bit), chain[i].p contains the address of that
377 * number (it points into struct inode for i==0 and into the bh->b_data
378 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
379 * block for i>0 and NULL for i==0. In other words, it holds the block
380 * numbers of the chain, addresses they were taken from (and where we can
381 * verify that chain did not change) and buffer_heads hosting these
382 * numbers.
383 *
384 * Function stops when it stumbles upon zero pointer (absent block)
385 * (pointer to last triple returned, *@err == 0)
386 * or when it gets an IO error reading an indirect block
387 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388 * or when it reads all @depth-1 indirect blocks successfully and finds
389 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500390 *
391 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500392 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700393 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500394static Indirect *ext4_get_branch(struct inode *inode, int depth,
395 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 Indirect chain[4], int *err)
397{
398 struct super_block *sb = inode->i_sb;
399 Indirect *p = chain;
400 struct buffer_head *bh;
401
402 *err = 0;
403 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400404 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 if (!p->key)
406 goto no_block;
407 while (--depth) {
408 bh = sb_bread(sb, le32_to_cpu(p->key));
409 if (!bh)
410 goto failure;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400411 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 /* Reader: end */
413 if (!p->key)
414 goto no_block;
415 }
416 return NULL;
417
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418failure:
419 *err = -EIO;
420no_block:
421 return p;
422}
423
424/**
Mingming Cao617ba132006-10-11 01:20:53 -0700425 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700426 * @inode: owner
427 * @ind: descriptor of indirect block.
428 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000429 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430 * It is used when heuristic for sequential allocation fails.
431 * Rules are:
432 * + if there is a block to the left of our position - allocate near it.
433 * + if pointer will live in indirect block - allocate near that block.
434 * + if pointer will live in inode - allocate in the same
435 * cylinder group.
436 *
437 * In the latter case we colour the starting block by the callers PID to
438 * prevent it from clashing with concurrent allocations for a different inode
439 * in the same block group. The PID is used here so that functionally related
440 * files will be close-by on-disk.
441 *
442 * Caller must make sure that @ind is valid and will stay that way.
443 */
Mingming Cao617ba132006-10-11 01:20:53 -0700444static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445{
Mingming Cao617ba132006-10-11 01:20:53 -0700446 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400447 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700448 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700449 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500450 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700451 ext4_grpblk_t colour;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700452
453 /* Try to find previous block */
454 for (p = ind->p - 1; p >= start; p--) {
455 if (*p)
456 return le32_to_cpu(*p);
457 }
458
459 /* No such thing, so let's try location of indirect block */
460 if (ind->bh)
461 return ind->bh->b_blocknr;
462
463 /*
464 * It is going to be referred to from the inode itself? OK, just put it
465 * into the same cylinder group then.
466 */
Mingming Cao617ba132006-10-11 01:20:53 -0700467 bg_start = ext4_group_first_block_no(inode->i_sb, ei->i_block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500468 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
469
470 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
471 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700472 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500473 else
474 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 return bg_start + colour;
476}
477
478/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000479 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 * @inode: owner
481 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000484 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800485 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500487static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800488 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400491 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493
Mingming Cao617ba132006-10-11 01:20:53 -0700494 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495}
496
497/**
Mingming Cao617ba132006-10-11 01:20:53 -0700498 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 * of direct blocks need to be allocated for the given branch.
500 *
501 * @branch: chain of indirect blocks
502 * @k: number of blocks need for indirect blocks
503 * @blks: number of data blocks to be mapped.
504 * @blocks_to_boundary: the offset in the indirect block
505 *
506 * return the total number of blocks to be allocate, including the
507 * direct and indirect blocks.
508 */
Mingming Cao617ba132006-10-11 01:20:53 -0700509static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 int blocks_to_boundary)
511{
512 unsigned long count = 0;
513
514 /*
515 * Simple case, [t,d]Indirect block(s) has not allocated yet
516 * then it's clear blocks on that path have not allocated
517 */
518 if (k > 0) {
519 /* right now we don't handle cross boundary allocation */
520 if (blks < blocks_to_boundary + 1)
521 count += blks;
522 else
523 count += blocks_to_boundary + 1;
524 return count;
525 }
526
527 count++;
528 while (count < blks && count <= blocks_to_boundary &&
529 le32_to_cpu(*(branch[0].p + count)) == 0) {
530 count++;
531 }
532 return count;
533}
534
535/**
Mingming Cao617ba132006-10-11 01:20:53 -0700536 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 * @indirect_blks: the number of blocks need to allocate for indirect
538 * blocks
539 *
540 * @new_blocks: on return it will store the new block numbers for
541 * the indirect blocks(if needed) and the first direct block,
542 * @blks: on return it will store the total number of allocated
543 * direct blocks
544 */
Mingming Cao617ba132006-10-11 01:20:53 -0700545static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400546 ext4_lblk_t iblock, ext4_fsblk_t goal,
547 int indirect_blks, int blks,
548 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500550 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400552 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700554 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 int ret = 0;
556
557 /*
558 * Here we try to allocate the requested multiple blocks at once,
559 * on a best-effort basis.
560 * To build a branch, we should allocate blocks for
561 * the indirect blocks(if not allocated yet), and at least
562 * the first direct block of this branch. That's the
563 * minimum number of blocks need to allocate(required)
564 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400565 /* first we try to allocate the indirect blocks */
566 target = indirect_blks;
567 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 count = target;
569 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400570 current_block = ext4_new_meta_blocks(handle, inode,
571 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 if (*err)
573 goto failed_out;
574
575 target -= count;
576 /* allocate blocks for indirect blocks */
577 while (index < indirect_blks && count) {
578 new_blocks[index++] = current_block++;
579 count--;
580 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400581 if (count > 0) {
582 /*
583 * save the new block number
584 * for the first direct block
585 */
586 new_blocks[index] = current_block;
587 printk(KERN_INFO "%s returned more blocks than "
588 "requested\n", __func__);
589 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400591 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592 }
593
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400594 target = blks - count ;
595 blk_allocated = count;
596 if (!target)
597 goto allocated;
598 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500599 memset(&ar, 0, sizeof(ar));
600 ar.inode = inode;
601 ar.goal = goal;
602 ar.len = target;
603 ar.logical = iblock;
604 if (S_ISREG(inode->i_mode))
605 /* enable in-core preallocation only for regular files */
606 ar.flags = EXT4_MB_HINT_DATA;
607
608 current_block = ext4_mb_new_blocks(handle, &ar, err);
609
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400610 if (*err && (target == blks)) {
611 /*
612 * if the allocation failed and we didn't allocate
613 * any blocks before
614 */
615 goto failed_out;
616 }
617 if (!*err) {
618 if (target == blks) {
619 /*
620 * save the new block number
621 * for the first direct block
622 */
623 new_blocks[index] = current_block;
624 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500625 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400626 }
627allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400629 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 *err = 0;
631 return ret;
632failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400633 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500634 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635 return ret;
636}
637
638/**
Mingming Cao617ba132006-10-11 01:20:53 -0700639 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 * @inode: owner
641 * @indirect_blks: number of allocated indirect blocks
642 * @blks: number of allocated direct blocks
643 * @offsets: offsets (in the blocks) to store the pointers to next.
644 * @branch: place to store the chain in.
645 *
646 * This function allocates blocks, zeroes out all but the last one,
647 * links them into chain and (if we are synchronous) writes them to disk.
648 * In other words, it prepares a branch that can be spliced onto the
649 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700650 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 * we had read the existing part of chain and partial points to the last
652 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700653 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654 * place chain is disconnected - *branch->p is still zero (we did not
655 * set the last link), but branch->key contains the number that should
656 * be placed into *branch->p to fill that gap.
657 *
658 * If allocation fails we free all blocks we've allocated (and forget
659 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700660 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 * as described above and return 0.
662 */
Mingming Cao617ba132006-10-11 01:20:53 -0700663static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400664 ext4_lblk_t iblock, int indirect_blks,
665 int *blks, ext4_fsblk_t goal,
666 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667{
668 int blocksize = inode->i_sb->s_blocksize;
669 int i, n = 0;
670 int err = 0;
671 struct buffer_head *bh;
672 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700673 ext4_fsblk_t new_blocks[4];
674 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400676 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 *blks, new_blocks, &err);
678 if (err)
679 return err;
680
681 branch[0].key = cpu_to_le32(new_blocks[0]);
682 /*
683 * metadata blocks and data blocks are allocated.
684 */
685 for (n = 1; n <= indirect_blks; n++) {
686 /*
687 * Get buffer_head for parent block, zero it out
688 * and set the pointer to new one, then send
689 * parent to disk.
690 */
691 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
692 branch[n].bh = bh;
693 lock_buffer(bh);
694 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700695 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 if (err) {
697 unlock_buffer(bh);
698 brelse(bh);
699 goto failed;
700 }
701
702 memset(bh->b_data, 0, blocksize);
703 branch[n].p = (__le32 *) bh->b_data + offsets[n];
704 branch[n].key = cpu_to_le32(new_blocks[n]);
705 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400706 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 current_block = new_blocks[n];
708 /*
709 * End of chain, update the last new metablock of
710 * the chain to point to the new allocated
711 * data blocks numbers
712 */
713 for (i=1; i < num; i++)
714 *(branch[n].p + i) = cpu_to_le32(++current_block);
715 }
716 BUFFER_TRACE(bh, "marking uptodate");
717 set_buffer_uptodate(bh);
718 unlock_buffer(bh);
719
Mingming Cao617ba132006-10-11 01:20:53 -0700720 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
721 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 if (err)
723 goto failed;
724 }
725 *blks = num;
726 return err;
727failed:
728 /* Allocation failed, free what we already allocated */
729 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700730 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700731 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400733 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500734 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735
Alex Tomasc9de5602008-01-29 00:19:52 -0500736 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737
738 return err;
739}
740
741/**
Mingming Cao617ba132006-10-11 01:20:53 -0700742 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743 * @inode: owner
744 * @block: (logical) number of block we are adding
745 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700746 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 * @where: location of missing link
748 * @num: number of indirect blocks we are adding
749 * @blks: number of direct blocks we are adding
750 *
751 * This function fills the missing link and does all housekeeping needed in
752 * inode (->i_blocks, etc.). In case of success we end up with the full
753 * chain to new block and return 0.
754 */
Mingming Cao617ba132006-10-11 01:20:53 -0700755static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500756 ext4_lblk_t block, Indirect *where, int num, int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757{
758 int i;
759 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700760 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 /*
763 * If we're splicing into a [td]indirect block (as opposed to the
764 * inode) then we need to get write access to the [td]indirect block
765 * before the splice.
766 */
767 if (where->bh) {
768 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700769 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 if (err)
771 goto err_out;
772 }
773 /* That's it */
774
775 *where->p = where->key;
776
777 /*
778 * Update the host buffer_head or inode to point to more just allocated
779 * direct blocks blocks
780 */
781 if (num == 0 && blks > 1) {
782 current_block = le32_to_cpu(where->key) + 1;
783 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400784 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 }
786
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 /* We are done with atomic stuff, now do the rest of housekeeping */
788
Kalpak Shahef7f3832007-07-18 09:15:20 -0400789 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700790 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791
792 /* had we spliced it onto indirect block? */
793 if (where->bh) {
794 /*
795 * If we spliced it onto an indirect block, we haven't
796 * altered the inode. Note however that if it is being spliced
797 * onto an indirect block at the very end of the file (the
798 * file is growing) then we *will* alter the inode to reflect
799 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700800 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 */
802 jbd_debug(5, "splicing indirect only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700803 BUFFER_TRACE(where->bh, "call ext4_journal_dirty_metadata");
804 err = ext4_journal_dirty_metadata(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805 if (err)
806 goto err_out;
807 } else {
808 /*
809 * OK, we spliced it into the inode itself on a direct block.
810 * Inode was dirtied above.
811 */
812 jbd_debug(5, "splicing direct\n");
813 }
814 return err;
815
816err_out:
817 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700818 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700819 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500820 ext4_free_blocks(handle, inode,
821 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824
825 return err;
826}
827
828/*
829 * Allocation strategy is simple: if we have to allocate something, we will
830 * have to go the whole way to leaf. So let's do it before attaching anything
831 * to tree, set linkage between the newborn blocks, write them if sync is
832 * required, recheck the path, free and repeat if check fails, otherwise
833 * set the last missing link (that will protect us from any truncate-generated
834 * removals - all blocks on the path are immune now) and possibly force the
835 * write on the parent block.
836 * That has a nice additional property: no special recovery from the failed
837 * allocations is needed - we simply release blocks and do not touch anything
838 * reachable from inode.
839 *
840 * `handle' can be NULL if create == 0.
841 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 * return > 0, # of blocks mapped or allocated.
843 * return = 0, if plain lookup failed.
844 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500845 *
846 *
847 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500848 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
849 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 */
Mingming Cao617ba132006-10-11 01:20:53 -0700851int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500852 ext4_lblk_t iblock, unsigned long maxblocks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 struct buffer_head *bh_result,
854 int create, int extend_disksize)
855{
856 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500857 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 Indirect chain[4];
859 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700860 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 int indirect_blks;
862 int blocks_to_boundary = 0;
863 int depth;
Mingming Cao617ba132006-10-11 01:20:53 -0700864 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700866 ext4_fsblk_t first_block = 0;
Mingming Cao61628a32008-07-11 19:27:31 -0400867 loff_t disksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868
869
Alex Tomasa86c6182006-10-11 01:21:03 -0700870 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 J_ASSERT(handle != NULL || create == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500872 depth = ext4_block_to_path(inode, iblock, offsets,
873 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700874
875 if (depth == 0)
876 goto out;
877
Mingming Cao617ba132006-10-11 01:20:53 -0700878 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879
880 /* Simplest case - block found, no allocation needed */
881 if (!partial) {
882 first_block = le32_to_cpu(chain[depth - 1].key);
883 clear_buffer_new(bh_result);
884 count++;
885 /*map more blocks*/
886 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700887 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 blk = le32_to_cpu(*(chain[depth-1].p + count));
890
891 if (blk == first_block + count)
892 count++;
893 else
894 break;
895 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500896 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 }
898
899 /* Next simple case - plain lookup or failed read of indirect block */
900 if (!create || err == -EIO)
901 goto cleanup;
902
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700903 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400904 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800906 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907
908 /* the number of blocks need to allocate for [d,t]indirect blocks */
909 indirect_blks = (chain + depth) - partial - 1;
910
911 /*
912 * Next look up the indirect map to count the totoal number of
913 * direct blocks to allocate for this branch.
914 */
Mingming Cao617ba132006-10-11 01:20:53 -0700915 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916 maxblocks, blocks_to_boundary);
917 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700918 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700919 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400920 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
921 &count, goal,
922 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700923
924 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700925 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926 * on the new chain if there is a failure, but that risks using
927 * up transaction credits, especially for bitmaps where the
928 * credits cannot be returned. Can we handle this somehow? We
929 * may need to return -EAGAIN upwards in the worst case. --sct
930 */
931 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -0700932 err = ext4_splice_branch(handle, inode, iblock,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 partial, indirect_blks, count);
934 /*
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500935 * i_disksize growing is protected by i_data_sem. Don't forget to
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 * protect it if you're about to implement concurrent
Mingming Cao617ba132006-10-11 01:20:53 -0700937 * ext4_get_block() -bzzz
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938 */
Mingming Cao61628a32008-07-11 19:27:31 -0400939 if (!err && extend_disksize) {
940 disksize = ((loff_t) iblock + count) << inode->i_blkbits;
941 if (disksize > i_size_read(inode))
942 disksize = i_size_read(inode);
943 if (disksize > ei->i_disksize)
944 ei->i_disksize = disksize;
945 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 if (err)
947 goto cleanup;
948
949 set_buffer_new(bh_result);
950got_it:
951 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
952 if (count > blocks_to_boundary)
953 set_buffer_boundary(bh_result);
954 err = count;
955 /* Clean up and exit */
956 partial = chain + depth - 1; /* the whole chain */
957cleanup:
958 while (partial > chain) {
959 BUFFER_TRACE(partial->bh, "call brelse");
960 brelse(partial->bh);
961 partial--;
962 }
963 BUFFER_TRACE(bh_result, "returned");
964out:
965 return err;
966}
967
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400968/*
969 * Calculate the number of metadata blocks need to reserve
970 * to allocate @blocks for non extent file based file
971 */
972static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
973{
974 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
975 int ind_blks, dind_blks, tind_blks;
976
977 /* number of new indirect blocks needed */
978 ind_blks = (blocks + icap - 1) / icap;
979
980 dind_blks = (ind_blks + icap - 1) / icap;
981
982 tind_blks = 1;
983
984 return ind_blks + dind_blks + tind_blks;
985}
986
987/*
988 * Calculate the number of metadata blocks need to reserve
989 * to allocate given number of blocks
990 */
991static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
992{
Mingming Caocd213222008-08-19 22:16:59 -0400993 if (!blocks)
994 return 0;
995
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400996 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
997 return ext4_ext_calc_metadata_amount(inode, blocks);
998
999 return ext4_indirect_calc_metadata_amount(inode, blocks);
1000}
1001
1002static void ext4_da_update_reserve_space(struct inode *inode, int used)
1003{
1004 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1005 int total, mdb, mdb_free;
1006
1007 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1008 /* recalculate the number of metablocks still need to be reserved */
1009 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1010 mdb = ext4_calc_metadata_amount(inode, total);
1011
1012 /* figure out how many metablocks to release */
1013 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1014 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1015
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001016 if (mdb_free) {
1017 /* Account for allocated meta_blocks */
1018 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001019
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001020 /* update fs dirty blocks counter */
1021 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1022 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1023 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1024 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001025
1026 /* update per-inode reservations */
1027 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1028 EXT4_I(inode)->i_reserved_data_blocks -= used;
1029
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001030 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1031}
1032
Mingming Caof5ab0d12008-02-25 15:29:55 -05001033/*
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001034 * The ext4_get_blocks_wrap() function try to look up the requested blocks,
1035 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001036 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001037 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1038 * and store the allocated blocks in the result buffer head and mark it
1039 * mapped.
1040 *
1041 * If file type is extents based, it will call ext4_ext_get_blocks(),
1042 * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping
1043 * based files
1044 *
1045 * On success, it returns the number of blocks being mapped or allocate.
1046 * if create==0 and the blocks are pre-allocated and uninitialized block,
1047 * the result buffer head is unmapped. If the create ==1, it will make sure
1048 * the buffer head is mapped.
1049 *
1050 * It returns 0 if plain look up failed (blocks have not been allocated), in
1051 * that casem, buffer head is unmapped
1052 *
1053 * It returns the error in case of allocation failure.
1054 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001055int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
1056 unsigned long max_blocks, struct buffer_head *bh,
Mingming Caod2a17632008-07-14 17:52:37 -04001057 int create, int extend_disksize, int flag)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001058{
1059 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001060
1061 clear_buffer_mapped(bh);
1062
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001063 /*
1064 * Try to see if we can get the block without requesting
1065 * for new file system block.
1066 */
1067 down_read((&EXT4_I(inode)->i_data_sem));
1068 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1069 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
1070 bh, 0, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001071 } else {
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001072 retval = ext4_get_blocks_handle(handle,
1073 inode, block, max_blocks, bh, 0, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001074 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001075 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001076
1077 /* If it is only a block(s) look up */
1078 if (!create)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001079 return retval;
1080
1081 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001082 * Returns if the blocks have already allocated
1083 *
1084 * Note that if blocks have been preallocated
1085 * ext4_ext_get_block() returns th create = 0
1086 * with buffer head unmapped.
1087 */
1088 if (retval > 0 && buffer_mapped(bh))
1089 return retval;
1090
1091 /*
1092 * New blocks allocate and/or writing to uninitialized extent
1093 * will possibly result in updating i_data, so we take
1094 * the write lock of i_data_sem, and call get_blocks()
1095 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001096 */
1097 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001098
1099 /*
1100 * if the caller is from delayed allocation writeout path
1101 * we have already reserved fs blocks for allocation
1102 * let the underlying get_block() function know to
1103 * avoid double accounting
1104 */
1105 if (flag)
1106 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001107 /*
1108 * We need to check for EXT4 here because migrate
1109 * could have changed the inode type in between
1110 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001111 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1112 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
1113 bh, create, extend_disksize);
1114 } else {
1115 retval = ext4_get_blocks_handle(handle, inode, block,
1116 max_blocks, bh, create, extend_disksize);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001117
1118 if (retval > 0 && buffer_new(bh)) {
1119 /*
1120 * We allocated new blocks which will result in
1121 * i_data's format changing. Force the migrate
1122 * to fail by clearing migrate flags
1123 */
1124 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1125 ~EXT4_EXT_MIGRATE;
1126 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001127 }
Mingming Caod2a17632008-07-14 17:52:37 -04001128
1129 if (flag) {
1130 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
1131 /*
1132 * Update reserved blocks/metadata blocks
1133 * after successful block allocation
1134 * which were deferred till now
1135 */
1136 if ((retval > 0) && buffer_delay(bh))
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001137 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001138 }
1139
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001140 up_write((&EXT4_I(inode)->i_data_sem));
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001141 return retval;
1142}
1143
Mingming Caof3bd1f32008-08-19 22:16:03 -04001144/* Maximum number of blocks we map for direct IO at once. */
1145#define DIO_MAX_BLOCKS 4096
1146
Eric Sandeen6873fa02008-10-07 00:46:36 -04001147int ext4_get_block(struct inode *inode, sector_t iblock,
1148 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001150 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001151 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001153 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001154
Jan Kara7fb54092008-02-10 01:08:38 -05001155 if (create && !handle) {
1156 /* Direct IO write... */
1157 if (max_blocks > DIO_MAX_BLOCKS)
1158 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001159 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1160 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001161 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001163 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 }
Jan Kara7fb54092008-02-10 01:08:38 -05001165 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 }
1167
Jan Kara7fb54092008-02-10 01:08:38 -05001168 ret = ext4_get_blocks_wrap(handle, inode, iblock,
Mingming Caod2a17632008-07-14 17:52:37 -04001169 max_blocks, bh_result, create, 0, 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001170 if (ret > 0) {
1171 bh_result->b_size = (ret << inode->i_blkbits);
1172 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 }
Jan Kara7fb54092008-02-10 01:08:38 -05001174 if (started)
1175 ext4_journal_stop(handle);
1176out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 return ret;
1178}
1179
1180/*
1181 * `handle' can be NULL if create is zero
1182 */
Mingming Cao617ba132006-10-11 01:20:53 -07001183struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001184 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185{
1186 struct buffer_head dummy;
1187 int fatal = 0, err;
1188
1189 J_ASSERT(handle != NULL || create == 0);
1190
1191 dummy.b_state = 0;
1192 dummy.b_blocknr = -1000;
1193 buffer_trace_init(&dummy.b_history);
Alex Tomasa86c6182006-10-11 01:21:03 -07001194 err = ext4_get_blocks_wrap(handle, inode, block, 1,
Mingming Caod2a17632008-07-14 17:52:37 -04001195 &dummy, create, 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001197 * ext4_get_blocks_handle() returns number of blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198 * mapped. 0 in case of a HOLE.
1199 */
1200 if (err > 0) {
1201 if (err > 1)
1202 WARN_ON(1);
1203 err = 0;
1204 }
1205 *errp = err;
1206 if (!err && buffer_mapped(&dummy)) {
1207 struct buffer_head *bh;
1208 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1209 if (!bh) {
1210 *errp = -EIO;
1211 goto err;
1212 }
1213 if (buffer_new(&dummy)) {
1214 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001215 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216
1217 /*
1218 * Now that we do not always journal data, we should
1219 * keep in mind whether this should always journal the
1220 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001221 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 * problem.
1223 */
1224 lock_buffer(bh);
1225 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001226 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001227 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001228 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 set_buffer_uptodate(bh);
1230 }
1231 unlock_buffer(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001232 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1233 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 if (!fatal)
1235 fatal = err;
1236 } else {
1237 BUFFER_TRACE(bh, "not a new buffer");
1238 }
1239 if (fatal) {
1240 *errp = fatal;
1241 brelse(bh);
1242 bh = NULL;
1243 }
1244 return bh;
1245 }
1246err:
1247 return NULL;
1248}
1249
Mingming Cao617ba132006-10-11 01:20:53 -07001250struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001251 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001253 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254
Mingming Cao617ba132006-10-11 01:20:53 -07001255 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 if (!bh)
1257 return bh;
1258 if (buffer_uptodate(bh))
1259 return bh;
1260 ll_rw_block(READ_META, 1, &bh);
1261 wait_on_buffer(bh);
1262 if (buffer_uptodate(bh))
1263 return bh;
1264 put_bh(bh);
1265 *err = -EIO;
1266 return NULL;
1267}
1268
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001269static int walk_page_buffers(handle_t *handle,
1270 struct buffer_head *head,
1271 unsigned from,
1272 unsigned to,
1273 int *partial,
1274 int (*fn)(handle_t *handle,
1275 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276{
1277 struct buffer_head *bh;
1278 unsigned block_start, block_end;
1279 unsigned blocksize = head->b_size;
1280 int err, ret = 0;
1281 struct buffer_head *next;
1282
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001283 for (bh = head, block_start = 0;
1284 ret == 0 && (bh != head || !block_start);
1285 block_start = block_end, bh = next)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 {
1287 next = bh->b_this_page;
1288 block_end = block_start + blocksize;
1289 if (block_end <= from || block_start >= to) {
1290 if (partial && !buffer_uptodate(bh))
1291 *partial = 1;
1292 continue;
1293 }
1294 err = (*fn)(handle, bh);
1295 if (!ret)
1296 ret = err;
1297 }
1298 return ret;
1299}
1300
1301/*
1302 * To preserve ordering, it is essential that the hole instantiation and
1303 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001304 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001305 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306 * prepare_write() is the right place.
1307 *
Mingming Cao617ba132006-10-11 01:20:53 -07001308 * Also, this function can nest inside ext4_writepage() ->
1309 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310 * has generated enough buffer credits to do the whole page. So we won't
1311 * block on the journal in that case, which is good, because the caller may
1312 * be PF_MEMALLOC.
1313 *
Mingming Cao617ba132006-10-11 01:20:53 -07001314 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001315 * quota file writes. If we were to commit the transaction while thus
1316 * reentered, there can be a deadlock - we would be holding a quota
1317 * lock, and the commit would never complete if another thread had a
1318 * transaction open and was blocking on the quota lock - a ranking
1319 * violation.
1320 *
Mingming Caodab291a2006-10-11 01:21:01 -07001321 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322 * will _not_ run commit under these circumstances because handle->h_ref
1323 * is elevated. We'll still have enough credits for the tiny quotafile
1324 * write.
1325 */
1326static int do_journal_get_write_access(handle_t *handle,
1327 struct buffer_head *bh)
1328{
1329 if (!buffer_mapped(bh) || buffer_freed(bh))
1330 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001331 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001332}
1333
Nick Pigginbfc1af62007-10-16 01:25:05 -07001334static int ext4_write_begin(struct file *file, struct address_space *mapping,
1335 loff_t pos, unsigned len, unsigned flags,
1336 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001338 struct inode *inode = mapping->host;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001339 int ret, needed_blocks = ext4_writepage_trans_blocks(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001340 handle_t *handle;
1341 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001342 struct page *page;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001343 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001344 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001345
1346 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001347 from = pos & (PAGE_CACHE_SIZE - 1);
1348 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349
1350retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001351 handle = ext4_journal_start(inode, needed_blocks);
1352 if (IS_ERR(handle)) {
1353 ret = PTR_ERR(handle);
1354 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001355 }
1356
Nick Piggin54566b22009-01-04 12:00:53 -08001357 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001358 if (!page) {
1359 ext4_journal_stop(handle);
1360 ret = -ENOMEM;
1361 goto out;
1362 }
1363 *pagep = page;
1364
Nick Pigginbfc1af62007-10-16 01:25:05 -07001365 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1366 ext4_get_block);
1367
1368 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 ret = walk_page_buffers(handle, page_buffers(page),
1370 from, to, NULL, do_journal_get_write_access);
1371 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001372
1373 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001374 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001375 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001376 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001377 /*
1378 * block_write_begin may have instantiated a few blocks
1379 * outside i_size. Trim these off again. Don't need
1380 * i_size_read because we hold i_mutex.
1381 */
1382 if (pos + len > inode->i_size)
1383 vmtruncate(inode, inode->i_size);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001384 }
1385
Mingming Cao617ba132006-10-11 01:20:53 -07001386 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001388out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389 return ret;
1390}
1391
Nick Pigginbfc1af62007-10-16 01:25:05 -07001392/* For write_end() in data=journal mode */
1393static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394{
1395 if (!buffer_mapped(bh) || buffer_freed(bh))
1396 return 0;
1397 set_buffer_uptodate(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001398 return ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001399}
1400
1401/*
1402 * We need to pick up the new inode size which generic_commit_write gave us
1403 * `file' can be NULL - eg, when called from page_symlink().
1404 *
Mingming Cao617ba132006-10-11 01:20:53 -07001405 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 * buffers are managed internally.
1407 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001408static int ext4_ordered_write_end(struct file *file,
1409 struct address_space *mapping,
1410 loff_t pos, unsigned len, unsigned copied,
1411 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001412{
Mingming Cao617ba132006-10-11 01:20:53 -07001413 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001414 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 int ret = 0, ret2;
1416
Jan Kara678aaf42008-07-11 19:27:31 -04001417 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418
1419 if (ret == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 loff_t new_i_size;
1421
Nick Pigginbfc1af62007-10-16 01:25:05 -07001422 new_i_size = pos + copied;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001423 if (new_i_size > EXT4_I(inode)->i_disksize) {
1424 ext4_update_i_disksize(inode, new_i_size);
1425 /* We need to mark inode dirty even if
1426 * new_i_size is less that inode->i_size
1427 * bu greater than i_disksize.(hint delalloc)
1428 */
1429 ext4_mark_inode_dirty(handle, inode);
1430 }
1431
Jan Karacf108bc2008-07-11 19:27:31 -04001432 ret2 = generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001433 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001434 copied = ret2;
1435 if (ret2 < 0)
1436 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001437 }
Mingming Cao617ba132006-10-11 01:20:53 -07001438 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 if (!ret)
1440 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001441
1442 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001443}
1444
Nick Pigginbfc1af62007-10-16 01:25:05 -07001445static int ext4_writeback_write_end(struct file *file,
1446 struct address_space *mapping,
1447 loff_t pos, unsigned len, unsigned copied,
1448 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449{
Mingming Cao617ba132006-10-11 01:20:53 -07001450 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001451 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 int ret = 0, ret2;
1453 loff_t new_i_size;
1454
Nick Pigginbfc1af62007-10-16 01:25:05 -07001455 new_i_size = pos + copied;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001456 if (new_i_size > EXT4_I(inode)->i_disksize) {
1457 ext4_update_i_disksize(inode, new_i_size);
1458 /* We need to mark inode dirty even if
1459 * new_i_size is less that inode->i_size
1460 * bu greater than i_disksize.(hint delalloc)
1461 */
1462 ext4_mark_inode_dirty(handle, inode);
1463 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464
Jan Karacf108bc2008-07-11 19:27:31 -04001465 ret2 = generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001466 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001467 copied = ret2;
1468 if (ret2 < 0)
1469 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001470
Mingming Cao617ba132006-10-11 01:20:53 -07001471 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001472 if (!ret)
1473 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001474
1475 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476}
1477
Nick Pigginbfc1af62007-10-16 01:25:05 -07001478static int ext4_journalled_write_end(struct file *file,
1479 struct address_space *mapping,
1480 loff_t pos, unsigned len, unsigned copied,
1481 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482{
Mingming Cao617ba132006-10-11 01:20:53 -07001483 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001484 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 int ret = 0, ret2;
1486 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001487 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001488 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489
Nick Pigginbfc1af62007-10-16 01:25:05 -07001490 from = pos & (PAGE_CACHE_SIZE - 1);
1491 to = from + len;
1492
1493 if (copied < len) {
1494 if (!PageUptodate(page))
1495 copied = 0;
1496 page_zero_new_buffers(page, from+copied, to);
1497 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498
1499 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001500 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 if (!partial)
1502 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001503 new_i_size = pos + copied;
1504 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001505 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001506 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001507 if (new_i_size > EXT4_I(inode)->i_disksize) {
1508 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001509 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 if (!ret)
1511 ret = ret2;
1512 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001513
Jan Karacf108bc2008-07-11 19:27:31 -04001514 unlock_page(page);
Mingming Cao617ba132006-10-11 01:20:53 -07001515 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 if (!ret)
1517 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001518 page_cache_release(page);
1519
1520 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001521}
Mingming Caod2a17632008-07-14 17:52:37 -04001522
1523static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1524{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001525 int retries = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001526 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1527 unsigned long md_needed, mdblocks, total = 0;
1528
1529 /*
1530 * recalculate the amount of metadata blocks to reserve
1531 * in order to allocate nrblocks
1532 * worse case is one extent per block
1533 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001534repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001535 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1536 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1537 mdblocks = ext4_calc_metadata_amount(inode, total);
1538 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1539
1540 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1541 total = md_needed + nrblocks;
1542
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001543 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001544 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001545 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1546 yield();
1547 goto repeat;
1548 }
Mingming Caod2a17632008-07-14 17:52:37 -04001549 return -ENOSPC;
1550 }
Mingming Caod2a17632008-07-14 17:52:37 -04001551 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1552 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1553
1554 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1555 return 0; /* success */
1556}
1557
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001558static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001559{
1560 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1561 int total, mdb, mdb_free, release;
1562
Mingming Caocd213222008-08-19 22:16:59 -04001563 if (!to_free)
1564 return; /* Nothing to release, exit */
1565
Mingming Caod2a17632008-07-14 17:52:37 -04001566 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001567
1568 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1569 /*
1570 * if there is no reserved blocks, but we try to free some
1571 * then the counter is messed up somewhere.
1572 * but since this function is called from invalidate
1573 * page, it's harmless to return without any action
1574 */
1575 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1576 "blocks for inode %lu, but there is no reserved "
1577 "data blocks\n", to_free, inode->i_ino);
1578 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1579 return;
1580 }
1581
Mingming Caod2a17632008-07-14 17:52:37 -04001582 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001583 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001584 mdb = ext4_calc_metadata_amount(inode, total);
1585
1586 /* figure out how many metablocks to release */
1587 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1588 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1589
Mingming Caod2a17632008-07-14 17:52:37 -04001590 release = to_free + mdb_free;
1591
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001592 /* update fs dirty blocks counter for truncate case */
1593 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001594
1595 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001596 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1597 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001598
1599 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1600 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001601 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1602}
1603
1604static void ext4_da_page_release_reservation(struct page *page,
1605 unsigned long offset)
1606{
1607 int to_release = 0;
1608 struct buffer_head *head, *bh;
1609 unsigned int curr_off = 0;
1610
1611 head = page_buffers(page);
1612 bh = head;
1613 do {
1614 unsigned int next_off = curr_off + bh->b_size;
1615
1616 if ((offset <= curr_off) && (buffer_delay(bh))) {
1617 to_release++;
1618 clear_buffer_delay(bh);
1619 }
1620 curr_off = next_off;
1621 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001622 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001623}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624
1625/*
Alex Tomas64769242008-07-11 19:27:31 -04001626 * Delayed allocation stuff
1627 */
1628
1629struct mpage_da_data {
1630 struct inode *inode;
1631 struct buffer_head lbh; /* extent of blocks */
1632 unsigned long first_page, next_page; /* extent of pages */
1633 get_block_t *get_block;
1634 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001635 int io_done;
1636 long pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001637 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001638};
1639
1640/*
1641 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001642 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001643 *
1644 * @mpd->inode: inode
1645 * @mpd->first_page: first page of the extent
1646 * @mpd->next_page: page after the last page of the extent
1647 * @mpd->get_block: the filesystem's block mapper function
1648 *
1649 * By the time mpage_da_submit_io() is called we expect all blocks
1650 * to be allocated. this may be wrong if allocation failed.
1651 *
1652 * As pages are already locked by write_cache_pages(), we can't use it
1653 */
1654static int mpage_da_submit_io(struct mpage_da_data *mpd)
1655{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001656 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001657 struct pagevec pvec;
1658 unsigned long index, end;
1659 int ret = 0, err, nr_pages, i;
1660 struct inode *inode = mpd->inode;
1661 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001662
1663 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001664 /*
1665 * We need to start from the first_page to the next_page - 1
1666 * to make sure we also write the mapped dirty buffer_heads.
1667 * If we look at mpd->lbh.b_blocknr we would only be looking
1668 * at the currently mapped buffer_heads.
1669 */
Alex Tomas64769242008-07-11 19:27:31 -04001670 index = mpd->first_page;
1671 end = mpd->next_page - 1;
1672
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001673 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001674 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001675 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001676 if (nr_pages == 0)
1677 break;
1678 for (i = 0; i < nr_pages; i++) {
1679 struct page *page = pvec.pages[i];
1680
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001681 index = page->index;
1682 if (index > end)
1683 break;
1684 index++;
1685
1686 BUG_ON(!PageLocked(page));
1687 BUG_ON(PageWriteback(page));
1688
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001689 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001690 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001691 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1692 /*
1693 * have successfully written the page
1694 * without skipping the same
1695 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001696 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001697 /*
1698 * In error case, we have to continue because
1699 * remaining pages are still locked
1700 * XXX: unlock and re-dirty them?
1701 */
1702 if (ret == 0)
1703 ret = err;
1704 }
1705 pagevec_release(&pvec);
1706 }
Alex Tomas64769242008-07-11 19:27:31 -04001707 return ret;
1708}
1709
1710/*
1711 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1712 *
1713 * @mpd->inode - inode to walk through
1714 * @exbh->b_blocknr - first block on a disk
1715 * @exbh->b_size - amount of space in bytes
1716 * @logical - first logical block to start assignment with
1717 *
1718 * the function goes through all passed space and put actual disk
1719 * block numbers into buffer heads, dropping BH_Delay
1720 */
1721static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1722 struct buffer_head *exbh)
1723{
1724 struct inode *inode = mpd->inode;
1725 struct address_space *mapping = inode->i_mapping;
1726 int blocks = exbh->b_size >> inode->i_blkbits;
1727 sector_t pblock = exbh->b_blocknr, cur_logical;
1728 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001729 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001730 struct pagevec pvec;
1731 int nr_pages, i;
1732
1733 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1734 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1735 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1736
1737 pagevec_init(&pvec, 0);
1738
1739 while (index <= end) {
1740 /* XXX: optimize tail */
1741 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1742 if (nr_pages == 0)
1743 break;
1744 for (i = 0; i < nr_pages; i++) {
1745 struct page *page = pvec.pages[i];
1746
1747 index = page->index;
1748 if (index > end)
1749 break;
1750 index++;
1751
1752 BUG_ON(!PageLocked(page));
1753 BUG_ON(PageWriteback(page));
1754 BUG_ON(!page_has_buffers(page));
1755
1756 bh = page_buffers(page);
1757 head = bh;
1758
1759 /* skip blocks out of the range */
1760 do {
1761 if (cur_logical >= logical)
1762 break;
1763 cur_logical++;
1764 } while ((bh = bh->b_this_page) != head);
1765
1766 do {
1767 if (cur_logical >= logical + blocks)
1768 break;
Alex Tomas64769242008-07-11 19:27:31 -04001769 if (buffer_delay(bh)) {
1770 bh->b_blocknr = pblock;
1771 clear_buffer_delay(bh);
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001772 bh->b_bdev = inode->i_sb->s_bdev;
1773 } else if (buffer_unwritten(bh)) {
1774 bh->b_blocknr = pblock;
1775 clear_buffer_unwritten(bh);
1776 set_buffer_mapped(bh);
1777 set_buffer_new(bh);
1778 bh->b_bdev = inode->i_sb->s_bdev;
Mingming Cao61628a32008-07-11 19:27:31 -04001779 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04001780 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04001781
1782 cur_logical++;
1783 pblock++;
1784 } while ((bh = bh->b_this_page) != head);
1785 }
1786 pagevec_release(&pvec);
1787 }
1788}
1789
1790
1791/*
1792 * __unmap_underlying_blocks - just a helper function to unmap
1793 * set of blocks described by @bh
1794 */
1795static inline void __unmap_underlying_blocks(struct inode *inode,
1796 struct buffer_head *bh)
1797{
1798 struct block_device *bdev = inode->i_sb->s_bdev;
1799 int blocks, i;
1800
1801 blocks = bh->b_size >> inode->i_blkbits;
1802 for (i = 0; i < blocks; i++)
1803 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
1804}
1805
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001806static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
1807 sector_t logical, long blk_cnt)
1808{
1809 int nr_pages, i;
1810 pgoff_t index, end;
1811 struct pagevec pvec;
1812 struct inode *inode = mpd->inode;
1813 struct address_space *mapping = inode->i_mapping;
1814
1815 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1816 end = (logical + blk_cnt - 1) >>
1817 (PAGE_CACHE_SHIFT - inode->i_blkbits);
1818 while (index <= end) {
1819 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1820 if (nr_pages == 0)
1821 break;
1822 for (i = 0; i < nr_pages; i++) {
1823 struct page *page = pvec.pages[i];
1824 index = page->index;
1825 if (index > end)
1826 break;
1827 index++;
1828
1829 BUG_ON(!PageLocked(page));
1830 BUG_ON(PageWriteback(page));
1831 block_invalidatepage(page, 0);
1832 ClearPageUptodate(page);
1833 unlock_page(page);
1834 }
1835 }
1836 return;
1837}
1838
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001839static void ext4_print_free_blocks(struct inode *inode)
1840{
1841 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1842 printk(KERN_EMERG "Total free blocks count %lld\n",
1843 ext4_count_free_blocks(inode->i_sb));
1844 printk(KERN_EMERG "Free/Dirty block details\n");
1845 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04001846 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001847 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04001848 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001849 printk(KERN_EMERG "Block reservation details\n");
1850 printk(KERN_EMERG "i_reserved_data_blocks=%lu\n",
1851 EXT4_I(inode)->i_reserved_data_blocks);
1852 printk(KERN_EMERG "i_reserved_meta_blocks=%lu\n",
1853 EXT4_I(inode)->i_reserved_meta_blocks);
1854 return;
1855}
1856
Alex Tomas64769242008-07-11 19:27:31 -04001857/*
1858 * mpage_da_map_blocks - go through given space
1859 *
1860 * @mpd->lbh - bh describing space
1861 * @mpd->get_block - the filesystem's block mapper function
1862 *
1863 * The function skips space we know is already mapped to disk blocks.
1864 *
Alex Tomas64769242008-07-11 19:27:31 -04001865 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001866static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04001867{
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001868 int err = 0;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001869 struct buffer_head new;
Alex Tomas64769242008-07-11 19:27:31 -04001870 struct buffer_head *lbh = &mpd->lbh;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001871 sector_t next;
Alex Tomas64769242008-07-11 19:27:31 -04001872
1873 /*
1874 * We consider only non-mapped and non-allocated blocks
1875 */
1876 if (buffer_mapped(lbh) && !buffer_delay(lbh))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001877 return 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001878 new.b_state = lbh->b_state;
1879 new.b_blocknr = 0;
1880 new.b_size = lbh->b_size;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001881 next = lbh->b_blocknr;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001882 /*
1883 * If we didn't accumulate anything
1884 * to write simply return
1885 */
1886 if (!new.b_size)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001887 return 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001888 err = mpd->get_block(mpd->inode, next, &new, 1);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001889 if (err) {
1890
1891 /* If get block returns with error
1892 * we simply return. Later writepage
1893 * will redirty the page and writepages
1894 * will find the dirty page again
1895 */
1896 if (err == -EAGAIN)
1897 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001898
1899 if (err == -ENOSPC &&
1900 ext4_count_free_blocks(mpd->inode->i_sb)) {
1901 mpd->retval = err;
1902 return 0;
1903 }
1904
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001905 /*
1906 * get block failure will cause us
1907 * to loop in writepages. Because
1908 * a_ops->writepage won't be able to
1909 * make progress. The page will be redirtied
1910 * by writepage and writepages will again
1911 * try to write the same.
1912 */
1913 printk(KERN_EMERG "%s block allocation failed for inode %lu "
1914 "at logical offset %llu with max blocks "
1915 "%zd with error %d\n",
1916 __func__, mpd->inode->i_ino,
1917 (unsigned long long)next,
1918 lbh->b_size >> mpd->inode->i_blkbits, err);
1919 printk(KERN_EMERG "This should not happen.!! "
1920 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001921 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001922 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001923 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001924 /* invlaidate all the pages */
1925 ext4_da_block_invalidatepages(mpd, next,
1926 lbh->b_size >> mpd->inode->i_blkbits);
1927 return err;
1928 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001929 BUG_ON(new.b_size == 0);
Alex Tomas64769242008-07-11 19:27:31 -04001930
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001931 if (buffer_new(&new))
1932 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04001933
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001934 /*
1935 * If blocks are delayed marked, we need to
1936 * put actual blocknr and drop delayed bit
1937 */
1938 if (buffer_delay(lbh) || buffer_unwritten(lbh))
1939 mpage_put_bnr_to_bhs(mpd, next, &new);
1940
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001941 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001942}
1943
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001944#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1945 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04001946
1947/*
1948 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1949 *
1950 * @mpd->lbh - extent of blocks
1951 * @logical - logical number of the block in the file
1952 * @bh - bh of the block (used to access block's state)
1953 *
1954 * the function is used to collect contig. blocks in same state
1955 */
1956static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
1957 sector_t logical, struct buffer_head *bh)
1958{
Alex Tomas64769242008-07-11 19:27:31 -04001959 sector_t next;
Mingming Cao525f4ed2008-08-19 22:15:58 -04001960 size_t b_size = bh->b_size;
1961 struct buffer_head *lbh = &mpd->lbh;
1962 int nrblocks = lbh->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04001963
Mingming Cao525f4ed2008-08-19 22:15:58 -04001964 /* check if thereserved journal credits might overflow */
1965 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
1966 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1967 /*
1968 * With non-extent format we are limited by the journal
1969 * credit available. Total credit needed to insert
1970 * nrblocks contiguous blocks is dependent on the
1971 * nrblocks. So limit nrblocks.
1972 */
1973 goto flush_it;
1974 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
1975 EXT4_MAX_TRANS_DATA) {
1976 /*
1977 * Adding the new buffer_head would make it cross the
1978 * allowed limit for which we have journal credit
1979 * reserved. So limit the new bh->b_size
1980 */
1981 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
1982 mpd->inode->i_blkbits;
1983 /* we will do mpage_da_submit_io in the next loop */
1984 }
1985 }
Alex Tomas64769242008-07-11 19:27:31 -04001986 /*
1987 * First block in the extent
1988 */
1989 if (lbh->b_size == 0) {
1990 lbh->b_blocknr = logical;
Mingming Cao525f4ed2008-08-19 22:15:58 -04001991 lbh->b_size = b_size;
Alex Tomas64769242008-07-11 19:27:31 -04001992 lbh->b_state = bh->b_state & BH_FLAGS;
1993 return;
1994 }
1995
Mingming Cao525f4ed2008-08-19 22:15:58 -04001996 next = lbh->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04001997 /*
1998 * Can we merge the block to our big extent?
1999 */
2000 if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04002001 lbh->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002002 return;
2003 }
2004
Mingming Cao525f4ed2008-08-19 22:15:58 -04002005flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002006 /*
2007 * We couldn't merge the block to our extent, so we
2008 * need to flush current extent and start new one
2009 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002010 if (mpage_da_map_blocks(mpd) == 0)
2011 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002012 mpd->io_done = 1;
2013 return;
Alex Tomas64769242008-07-11 19:27:31 -04002014}
2015
2016/*
2017 * __mpage_da_writepage - finds extent of pages and blocks
2018 *
2019 * @page: page to consider
2020 * @wbc: not used, we just follow rules
2021 * @data: context
2022 *
2023 * The function finds extents of pages and scan them for all blocks.
2024 */
2025static int __mpage_da_writepage(struct page *page,
2026 struct writeback_control *wbc, void *data)
2027{
2028 struct mpage_da_data *mpd = data;
2029 struct inode *inode = mpd->inode;
2030 struct buffer_head *bh, *head, fake;
2031 sector_t logical;
2032
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002033 if (mpd->io_done) {
2034 /*
2035 * Rest of the page in the page_vec
2036 * redirty then and skip then. We will
2037 * try to to write them again after
2038 * starting a new transaction
2039 */
2040 redirty_page_for_writepage(wbc, page);
2041 unlock_page(page);
2042 return MPAGE_DA_EXTENT_TAIL;
2043 }
Alex Tomas64769242008-07-11 19:27:31 -04002044 /*
2045 * Can we merge this page to current extent?
2046 */
2047 if (mpd->next_page != page->index) {
2048 /*
2049 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002050 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002051 */
2052 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002053 if (mpage_da_map_blocks(mpd) == 0)
2054 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002055 /*
2056 * skip rest of the page in the page_vec
2057 */
2058 mpd->io_done = 1;
2059 redirty_page_for_writepage(wbc, page);
2060 unlock_page(page);
2061 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002062 }
2063
2064 /*
2065 * Start next extent of pages ...
2066 */
2067 mpd->first_page = page->index;
2068
2069 /*
2070 * ... and blocks
2071 */
2072 mpd->lbh.b_size = 0;
2073 mpd->lbh.b_state = 0;
2074 mpd->lbh.b_blocknr = 0;
2075 }
2076
2077 mpd->next_page = page->index + 1;
2078 logical = (sector_t) page->index <<
2079 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2080
2081 if (!page_has_buffers(page)) {
2082 /*
2083 * There is no attached buffer heads yet (mmap?)
2084 * we treat the page asfull of dirty blocks
2085 */
2086 bh = &fake;
2087 bh->b_size = PAGE_CACHE_SIZE;
2088 bh->b_state = 0;
2089 set_buffer_dirty(bh);
2090 set_buffer_uptodate(bh);
2091 mpage_add_bh_to_extent(mpd, logical, bh);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002092 if (mpd->io_done)
2093 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002094 } else {
2095 /*
2096 * Page with regular buffer heads, just add all dirty ones
2097 */
2098 head = page_buffers(page);
2099 bh = head;
2100 do {
2101 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002102 /*
2103 * We need to try to allocate
2104 * unmapped blocks in the same page.
2105 * Otherwise we won't make progress
2106 * with the page in ext4_da_writepage
2107 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002108 if (buffer_dirty(bh) &&
2109 (!buffer_mapped(bh) || buffer_delay(bh))) {
Alex Tomas64769242008-07-11 19:27:31 -04002110 mpage_add_bh_to_extent(mpd, logical, bh);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002111 if (mpd->io_done)
2112 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002113 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2114 /*
2115 * mapped dirty buffer. We need to update
2116 * the b_state because we look at
2117 * b_state in mpage_da_map_blocks. We don't
2118 * update b_size because if we find an
2119 * unmapped buffer_head later we need to
2120 * use the b_state flag of that buffer_head.
2121 */
2122 if (mpd->lbh.b_size == 0)
2123 mpd->lbh.b_state =
2124 bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002125 }
Alex Tomas64769242008-07-11 19:27:31 -04002126 logical++;
2127 } while ((bh = bh->b_this_page) != head);
2128 }
2129
2130 return 0;
2131}
2132
2133/*
2134 * mpage_da_writepages - walk the list of dirty pages of the given
2135 * address space, allocates non-allocated blocks, maps newly-allocated
2136 * blocks to existing bhs and issue IO them
2137 *
2138 * @mapping: address space structure to write
2139 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2140 * @get_block: the filesystem's block mapper function.
2141 *
2142 * This is a library function, which implements the writepages()
2143 * address_space_operation.
Alex Tomas64769242008-07-11 19:27:31 -04002144 */
2145static int mpage_da_writepages(struct address_space *mapping,
2146 struct writeback_control *wbc,
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002147 struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002148{
Alex Tomas64769242008-07-11 19:27:31 -04002149 int ret;
2150
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002151 if (!mpd->get_block)
Alex Tomas64769242008-07-11 19:27:31 -04002152 return generic_writepages(mapping, wbc);
2153
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002154 mpd->lbh.b_size = 0;
2155 mpd->lbh.b_state = 0;
2156 mpd->lbh.b_blocknr = 0;
2157 mpd->first_page = 0;
2158 mpd->next_page = 0;
2159 mpd->io_done = 0;
2160 mpd->pages_written = 0;
2161 mpd->retval = 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002162
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002163 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, mpd);
Alex Tomas64769242008-07-11 19:27:31 -04002164 /*
2165 * Handle last extent of pages
2166 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002167 if (!mpd->io_done && mpd->next_page != mpd->first_page) {
2168 if (mpage_da_map_blocks(mpd) == 0)
2169 mpage_da_submit_io(mpd);
Alex Tomas64769242008-07-11 19:27:31 -04002170
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002171 mpd->io_done = 1;
2172 ret = MPAGE_DA_EXTENT_TAIL;
2173 }
2174 wbc->nr_to_write -= mpd->pages_written;
Alex Tomas64769242008-07-11 19:27:31 -04002175 return ret;
2176}
2177
2178/*
2179 * this is a special callback for ->write_begin() only
2180 * it's intention is to return mapped block or reserve space
2181 */
2182static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2183 struct buffer_head *bh_result, int create)
2184{
2185 int ret = 0;
2186
2187 BUG_ON(create == 0);
2188 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2189
2190 /*
2191 * first, we need to know whether the block is allocated already
2192 * preallocated blocks are unmapped but should treated
2193 * the same as allocated blocks.
2194 */
Mingming Caod2a17632008-07-14 17:52:37 -04002195 ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1, bh_result, 0, 0, 0);
2196 if ((ret == 0) && !buffer_delay(bh_result)) {
2197 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002198 /*
2199 * XXX: __block_prepare_write() unmaps passed block,
2200 * is it OK?
2201 */
Mingming Caod2a17632008-07-14 17:52:37 -04002202 ret = ext4_da_reserve_space(inode, 1);
2203 if (ret)
2204 /* not enough space to reserve */
2205 return ret;
2206
Alex Tomas64769242008-07-11 19:27:31 -04002207 map_bh(bh_result, inode->i_sb, 0);
2208 set_buffer_new(bh_result);
2209 set_buffer_delay(bh_result);
2210 } else if (ret > 0) {
2211 bh_result->b_size = (ret << inode->i_blkbits);
2212 ret = 0;
2213 }
2214
2215 return ret;
2216}
Mingming Caod2a17632008-07-14 17:52:37 -04002217#define EXT4_DELALLOC_RSVED 1
Alex Tomas64769242008-07-11 19:27:31 -04002218static int ext4_da_get_block_write(struct inode *inode, sector_t iblock,
2219 struct buffer_head *bh_result, int create)
2220{
Mingming Cao61628a32008-07-11 19:27:31 -04002221 int ret;
Alex Tomas64769242008-07-11 19:27:31 -04002222 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2223 loff_t disksize = EXT4_I(inode)->i_disksize;
2224 handle_t *handle = NULL;
2225
Mingming Cao61628a32008-07-11 19:27:31 -04002226 handle = ext4_journal_current_handle();
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002227 BUG_ON(!handle);
2228 ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
2229 bh_result, create, 0, EXT4_DELALLOC_RSVED);
Alex Tomas64769242008-07-11 19:27:31 -04002230 if (ret > 0) {
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002231
Alex Tomas64769242008-07-11 19:27:31 -04002232 bh_result->b_size = (ret << inode->i_blkbits);
2233
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002234 if (ext4_should_order_data(inode)) {
2235 int retval;
2236 retval = ext4_jbd2_file_inode(handle, inode);
2237 if (retval)
2238 /*
2239 * Failed to add inode for ordered
2240 * mode. Don't update file size
2241 */
2242 return retval;
2243 }
2244
Alex Tomas64769242008-07-11 19:27:31 -04002245 /*
2246 * Update on-disk size along with block allocation
2247 * we don't use 'extend_disksize' as size may change
2248 * within already allocated block -bzzz
2249 */
2250 disksize = ((loff_t) iblock + ret) << inode->i_blkbits;
2251 if (disksize > i_size_read(inode))
2252 disksize = i_size_read(inode);
2253 if (disksize > EXT4_I(inode)->i_disksize) {
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002254 ext4_update_i_disksize(inode, disksize);
2255 ret = ext4_mark_inode_dirty(handle, inode);
2256 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002257 }
Alex Tomas64769242008-07-11 19:27:31 -04002258 ret = 0;
2259 }
Alex Tomas64769242008-07-11 19:27:31 -04002260 return ret;
2261}
Mingming Cao61628a32008-07-11 19:27:31 -04002262
2263static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh)
2264{
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002265 /*
2266 * unmapped buffer is possible for holes.
2267 * delay buffer is possible with delayed allocation
2268 */
2269 return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh));
2270}
2271
2272static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock,
2273 struct buffer_head *bh_result, int create)
2274{
2275 int ret = 0;
2276 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2277
2278 /*
2279 * we don't want to do block allocation in writepage
2280 * so call get_block_wrap with create = 0
2281 */
2282 ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks,
2283 bh_result, 0, 0, 0);
2284 if (ret > 0) {
2285 bh_result->b_size = (ret << inode->i_blkbits);
2286 ret = 0;
2287 }
2288 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002289}
2290
Mingming Cao61628a32008-07-11 19:27:31 -04002291/*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002292 * get called vi ext4_da_writepages after taking page lock (have journal handle)
2293 * get called via journal_submit_inode_data_buffers (no journal handle)
2294 * get called via shrink_page_list via pdflush (no journal handle)
2295 * or grab_page_cache when doing write_begin (have journal handle)
Mingming Cao61628a32008-07-11 19:27:31 -04002296 */
Alex Tomas64769242008-07-11 19:27:31 -04002297static int ext4_da_writepage(struct page *page,
2298 struct writeback_control *wbc)
2299{
Alex Tomas64769242008-07-11 19:27:31 -04002300 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002301 loff_t size;
2302 unsigned long len;
Mingming Cao61628a32008-07-11 19:27:31 -04002303 struct buffer_head *page_bufs;
2304 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002305
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002306 size = i_size_read(inode);
2307 if (page->index == size >> PAGE_CACHE_SHIFT)
2308 len = size & ~PAGE_CACHE_MASK;
2309 else
2310 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002311
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002312 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002313 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002314 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2315 ext4_bh_unmapped_or_delay)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002316 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002317 * We don't want to do block allocation
2318 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002319 * We may reach here when we do a journal commit
2320 * via journal_submit_inode_data_buffers.
2321 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002322 * them. We can also reach here via shrink_page_list
2323 */
2324 redirty_page_for_writepage(wbc, page);
2325 unlock_page(page);
2326 return 0;
2327 }
2328 } else {
2329 /*
2330 * The test for page_has_buffers() is subtle:
2331 * We know the page is dirty but it lost buffers. That means
2332 * that at some moment in time after write_begin()/write_end()
2333 * has been called all buffers have been clean and thus they
2334 * must have been written at least once. So they are all
2335 * mapped and we can happily proceed with mapping them
2336 * and writing the page.
2337 *
2338 * Try to initialize the buffer_heads and check whether
2339 * all are mapped and non delay. We don't want to
2340 * do block allocation here.
2341 */
2342 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2343 ext4_normal_get_block_write);
2344 if (!ret) {
2345 page_bufs = page_buffers(page);
2346 /* check whether all are mapped and non delay */
2347 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2348 ext4_bh_unmapped_or_delay)) {
2349 redirty_page_for_writepage(wbc, page);
2350 unlock_page(page);
2351 return 0;
2352 }
2353 } else {
2354 /*
2355 * We can't do block allocation here
2356 * so just redity the page and unlock
2357 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002358 */
Mingming Cao61628a32008-07-11 19:27:31 -04002359 redirty_page_for_writepage(wbc, page);
2360 unlock_page(page);
2361 return 0;
2362 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002363 /* now mark the buffer_heads as dirty and uptodate */
2364 block_commit_write(page, 0, PAGE_CACHE_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04002365 }
2366
2367 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002368 ret = nobh_writepage(page, ext4_normal_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002369 else
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002370 ret = block_write_full_page(page,
2371 ext4_normal_get_block_write,
2372 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002373
Alex Tomas64769242008-07-11 19:27:31 -04002374 return ret;
2375}
2376
Mingming Cao61628a32008-07-11 19:27:31 -04002377/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002378 * This is called via ext4_da_writepages() to
2379 * calulate the total number of credits to reserve to fit
2380 * a single extent allocation into a single transaction,
2381 * ext4_da_writpeages() will loop calling this before
2382 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002383 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002384
2385static int ext4_da_writepages_trans_blocks(struct inode *inode)
2386{
2387 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2388
2389 /*
2390 * With non-extent format the journal credit needed to
2391 * insert nrblocks contiguous block is dependent on
2392 * number of contiguous block. So we will limit
2393 * number of contiguous block to a sane value
2394 */
2395 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2396 (max_blocks > EXT4_MAX_TRANS_DATA))
2397 max_blocks = EXT4_MAX_TRANS_DATA;
2398
2399 return ext4_chunk_trans_blocks(inode, max_blocks);
2400}
Mingming Cao61628a32008-07-11 19:27:31 -04002401
Alex Tomas64769242008-07-11 19:27:31 -04002402static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002403 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002404{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002405 pgoff_t index;
2406 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002407 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002408 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002409 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002410 int no_nrwrite_index_update;
2411 long pages_written = 0, pages_skipped;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002412 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002413 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002414
2415 /*
2416 * No pages to write? This is mainly a kludge to avoid starting
2417 * a transaction for special inodes like journal inode on last iput()
2418 * because that could violate lock ordering on umount
2419 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002420 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002421 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002422
2423 /*
2424 * If the filesystem has aborted, it is read-only, so return
2425 * right away instead of dumping stack traces later on that
2426 * will obscure the real source of the problem. We test
2427 * EXT4_MOUNT_ABORT instead of sb->s_flag's MS_RDONLY because
2428 * the latter could be true if the filesystem is mounted
2429 * read-only, and in that case, ext4_da_writepages should
2430 * *never* be called, so if that ever happens, we would want
2431 * the stack trace.
2432 */
2433 if (unlikely(sbi->s_mount_opt & EXT4_MOUNT_ABORT))
2434 return -EROFS;
2435
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002436 /*
2437 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2438 * This make sure small files blocks are allocated in
2439 * single attempt. This ensure that small files
2440 * get less fragmented.
2441 */
2442 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2443 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2444 wbc->nr_to_write = sbi->s_mb_stream_request;
2445 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002446 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2447 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002448
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002449 if (wbc->range_cyclic)
2450 index = mapping->writeback_index;
2451 else
2452 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002453
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002454 mpd.wbc = wbc;
2455 mpd.inode = mapping->host;
2456
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002457 /*
2458 * we don't want write_cache_pages to update
2459 * nr_to_write and writeback_index
2460 */
2461 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2462 wbc->no_nrwrite_index_update = 1;
2463 pages_skipped = wbc->pages_skipped;
2464
2465 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002466
2467 /*
2468 * we insert one extent at a time. So we need
2469 * credit needed for single extent allocation.
2470 * journalled mode is currently not supported
2471 * by delalloc
2472 */
2473 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002474 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002475
Mingming Cao61628a32008-07-11 19:27:31 -04002476 /* start a new transaction*/
2477 handle = ext4_journal_start(inode, needed_blocks);
2478 if (IS_ERR(handle)) {
2479 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002480 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002481 "%ld pages, ino %lu; err %d\n", __func__,
2482 wbc->nr_to_write, inode->i_ino, ret);
2483 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002484 goto out_writepages;
2485 }
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002486 mpd.get_block = ext4_da_get_block_write;
2487 ret = mpage_da_writepages(mapping, wbc, &mpd);
2488
Mingming Cao61628a32008-07-11 19:27:31 -04002489 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002490
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002491 if (mpd.retval == -ENOSPC) {
2492 /* commit the transaction which would
2493 * free blocks released in the transaction
2494 * and try again
2495 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002496 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002497 wbc->pages_skipped = pages_skipped;
2498 ret = 0;
2499 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002500 /*
2501 * got one extent now try with
2502 * rest of the pages
2503 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002504 pages_written += mpd.pages_written;
2505 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002506 ret = 0;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002507 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002508 /*
2509 * There is no more writeout needed
2510 * or we requested for a noblocking writeout
2511 * and we found the device congested
2512 */
Mingming Cao61628a32008-07-11 19:27:31 -04002513 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002514 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002515 if (pages_skipped != wbc->pages_skipped)
2516 printk(KERN_EMERG "This should not happen leaving %s "
2517 "with nr_to_write = %ld ret = %d\n",
2518 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002519
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002520 /* Update index */
2521 index += pages_written;
2522 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2523 /*
2524 * set the writeback_index so that range_cyclic
2525 * mode will write it back later
2526 */
2527 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002528
Mingming Cao61628a32008-07-11 19:27:31 -04002529out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002530 if (!no_nrwrite_index_update)
2531 wbc->no_nrwrite_index_update = 0;
2532 wbc->nr_to_write -= nr_to_writebump;
Mingming Cao61628a32008-07-11 19:27:31 -04002533 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002534}
2535
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002536#define FALL_BACK_TO_NONDELALLOC 1
2537static int ext4_nonda_switch(struct super_block *sb)
2538{
2539 s64 free_blocks, dirty_blocks;
2540 struct ext4_sb_info *sbi = EXT4_SB(sb);
2541
2542 /*
2543 * switch to non delalloc mode if we are running low
2544 * on free block. The free block accounting via percpu
2545 * counters can get slightly wrong with FBC_BATCH getting
2546 * accumulated on each CPU without updating global counters
2547 * Delalloc need an accurate free block accounting. So switch
2548 * to non delalloc when we are near to error range.
2549 */
2550 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2551 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2552 if (2 * free_blocks < 3 * dirty_blocks ||
2553 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2554 /*
2555 * free block count is less that 150% of dirty blocks
2556 * or free blocks is less that watermark
2557 */
2558 return 1;
2559 }
2560 return 0;
2561}
2562
Alex Tomas64769242008-07-11 19:27:31 -04002563static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
2564 loff_t pos, unsigned len, unsigned flags,
2565 struct page **pagep, void **fsdata)
2566{
Mingming Caod2a17632008-07-14 17:52:37 -04002567 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002568 struct page *page;
2569 pgoff_t index;
2570 unsigned from, to;
2571 struct inode *inode = mapping->host;
2572 handle_t *handle;
2573
2574 index = pos >> PAGE_CACHE_SHIFT;
2575 from = pos & (PAGE_CACHE_SIZE - 1);
2576 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002577
2578 if (ext4_nonda_switch(inode->i_sb)) {
2579 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2580 return ext4_write_begin(file, mapping, pos,
2581 len, flags, pagep, fsdata);
2582 }
2583 *fsdata = (void *)0;
Mingming Caod2a17632008-07-14 17:52:37 -04002584retry:
Alex Tomas64769242008-07-11 19:27:31 -04002585 /*
2586 * With delayed allocation, we don't log the i_disksize update
2587 * if there is delayed block allocation. But we still need
2588 * to journalling the i_disksize update if writes to the end
2589 * of file which has an already mapped buffer.
2590 */
2591 handle = ext4_journal_start(inode, 1);
2592 if (IS_ERR(handle)) {
2593 ret = PTR_ERR(handle);
2594 goto out;
2595 }
2596
Nick Piggin54566b22009-01-04 12:00:53 -08002597 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002598 if (!page) {
2599 ext4_journal_stop(handle);
2600 ret = -ENOMEM;
2601 goto out;
2602 }
Alex Tomas64769242008-07-11 19:27:31 -04002603 *pagep = page;
2604
2605 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
2606 ext4_da_get_block_prep);
2607 if (ret < 0) {
2608 unlock_page(page);
2609 ext4_journal_stop(handle);
2610 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002611 /*
2612 * block_write_begin may have instantiated a few blocks
2613 * outside i_size. Trim these off again. Don't need
2614 * i_size_read because we hold i_mutex.
2615 */
2616 if (pos + len > inode->i_size)
2617 vmtruncate(inode, inode->i_size);
Alex Tomas64769242008-07-11 19:27:31 -04002618 }
2619
Mingming Caod2a17632008-07-14 17:52:37 -04002620 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2621 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04002622out:
2623 return ret;
2624}
2625
Mingming Cao632eaea2008-07-11 19:27:31 -04002626/*
2627 * Check if we should update i_disksize
2628 * when write to the end of file but not require block allocation
2629 */
2630static int ext4_da_should_update_i_disksize(struct page *page,
2631 unsigned long offset)
2632{
2633 struct buffer_head *bh;
2634 struct inode *inode = page->mapping->host;
2635 unsigned int idx;
2636 int i;
2637
2638 bh = page_buffers(page);
2639 idx = offset >> inode->i_blkbits;
2640
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002641 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002642 bh = bh->b_this_page;
2643
2644 if (!buffer_mapped(bh) || (buffer_delay(bh)))
2645 return 0;
2646 return 1;
2647}
2648
Alex Tomas64769242008-07-11 19:27:31 -04002649static int ext4_da_write_end(struct file *file,
2650 struct address_space *mapping,
2651 loff_t pos, unsigned len, unsigned copied,
2652 struct page *page, void *fsdata)
2653{
2654 struct inode *inode = mapping->host;
2655 int ret = 0, ret2;
2656 handle_t *handle = ext4_journal_current_handle();
2657 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002658 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002659 int write_mode = (int)(unsigned long)fsdata;
2660
2661 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
2662 if (ext4_should_order_data(inode)) {
2663 return ext4_ordered_write_end(file, mapping, pos,
2664 len, copied, page, fsdata);
2665 } else if (ext4_should_writeback_data(inode)) {
2666 return ext4_writeback_write_end(file, mapping, pos,
2667 len, copied, page, fsdata);
2668 } else {
2669 BUG();
2670 }
2671 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002672
2673 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002674 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002675
2676 /*
2677 * generic_write_end() will run mark_inode_dirty() if i_size
2678 * changes. So let's piggyback the i_disksize mark_inode_dirty
2679 * into that.
2680 */
2681
2682 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04002683 if (new_i_size > EXT4_I(inode)->i_disksize) {
2684 if (ext4_da_should_update_i_disksize(page, end)) {
2685 down_write(&EXT4_I(inode)->i_data_sem);
2686 if (new_i_size > EXT4_I(inode)->i_disksize) {
2687 /*
2688 * Updating i_disksize when extending file
2689 * without needing block allocation
2690 */
2691 if (ext4_should_order_data(inode))
2692 ret = ext4_jbd2_file_inode(handle,
2693 inode);
Alex Tomas64769242008-07-11 19:27:31 -04002694
Mingming Cao632eaea2008-07-11 19:27:31 -04002695 EXT4_I(inode)->i_disksize = new_i_size;
2696 }
2697 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002698 /* We need to mark inode dirty even if
2699 * new_i_size is less that inode->i_size
2700 * bu greater than i_disksize.(hint delalloc)
2701 */
2702 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002703 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002704 }
Alex Tomas64769242008-07-11 19:27:31 -04002705 ret2 = generic_write_end(file, mapping, pos, len, copied,
2706 page, fsdata);
2707 copied = ret2;
2708 if (ret2 < 0)
2709 ret = ret2;
2710 ret2 = ext4_journal_stop(handle);
2711 if (!ret)
2712 ret = ret2;
2713
2714 return ret ? ret : copied;
2715}
2716
2717static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2718{
Alex Tomas64769242008-07-11 19:27:31 -04002719 /*
2720 * Drop reserved blocks
2721 */
2722 BUG_ON(!PageLocked(page));
2723 if (!page_has_buffers(page))
2724 goto out;
2725
Mingming Caod2a17632008-07-14 17:52:37 -04002726 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04002727
2728out:
2729 ext4_invalidatepage(page, offset);
2730
2731 return;
2732}
2733
2734
2735/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002736 * bmap() is special. It gets used by applications such as lilo and by
2737 * the swapper to find the on-disk block of a specific piece of data.
2738 *
2739 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002740 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002741 * filesystem and enables swap, then they may get a nasty shock when the
2742 * data getting swapped to that swapfile suddenly gets overwritten by
2743 * the original zero's written out previously to the journal and
2744 * awaiting writeback in the kernel's buffer cache.
2745 *
2746 * So, if we see any bmap calls here on a modified, data-journaled file,
2747 * take extra steps to flush any blocks which might be in the cache.
2748 */
Mingming Cao617ba132006-10-11 01:20:53 -07002749static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002750{
2751 struct inode *inode = mapping->host;
2752 journal_t *journal;
2753 int err;
2754
Alex Tomas64769242008-07-11 19:27:31 -04002755 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2756 test_opt(inode->i_sb, DELALLOC)) {
2757 /*
2758 * With delalloc we want to sync the file
2759 * so that we can make sure we allocate
2760 * blocks for file
2761 */
2762 filemap_write_and_wait(mapping);
2763 }
2764
Mingming Cao617ba132006-10-11 01:20:53 -07002765 if (EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002766 /*
2767 * This is a REALLY heavyweight approach, but the use of
2768 * bmap on dirty files is expected to be extremely rare:
2769 * only if we run lilo or swapon on a freshly made file
2770 * do we expect this to happen.
2771 *
2772 * (bmap requires CAP_SYS_RAWIO so this does not
2773 * represent an unprivileged user DOS attack --- we'd be
2774 * in trouble if mortal users could trigger this path at
2775 * will.)
2776 *
Mingming Cao617ba132006-10-11 01:20:53 -07002777 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002778 * regular files. If somebody wants to bmap a directory
2779 * or symlink and gets confused because the buffer
2780 * hasn't yet been flushed to disk, they deserve
2781 * everything they get.
2782 */
2783
Mingming Cao617ba132006-10-11 01:20:53 -07002784 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
2785 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002786 jbd2_journal_lock_updates(journal);
2787 err = jbd2_journal_flush(journal);
2788 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002789
2790 if (err)
2791 return 0;
2792 }
2793
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002794 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795}
2796
2797static int bget_one(handle_t *handle, struct buffer_head *bh)
2798{
2799 get_bh(bh);
2800 return 0;
2801}
2802
2803static int bput_one(handle_t *handle, struct buffer_head *bh)
2804{
2805 put_bh(bh);
2806 return 0;
2807}
2808
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002809/*
Jan Kara678aaf42008-07-11 19:27:31 -04002810 * Note that we don't need to start a transaction unless we're journaling data
2811 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2812 * need to file the inode to the transaction's list in ordered mode because if
2813 * we are writing back data added by write(), the inode is already there and if
2814 * we are writing back data modified via mmap(), noone guarantees in which
2815 * transaction the data will hit the disk. In case we are journaling data, we
2816 * cannot start transaction directly because transaction start ranks above page
2817 * lock so we have to do some magic.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002818 *
Jan Kara678aaf42008-07-11 19:27:31 -04002819 * In all journaling modes block_write_full_page() will start the I/O.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820 *
2821 * Problem:
2822 *
Mingming Cao617ba132006-10-11 01:20:53 -07002823 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2824 * ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825 *
2826 * Similar for:
2827 *
Mingming Cao617ba132006-10-11 01:20:53 -07002828 * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ...
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002829 *
Mingming Cao617ba132006-10-11 01:20:53 -07002830 * Same applies to ext4_get_block(). We will deadlock on various things like
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002831 * lock_journal and i_data_sem
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002832 *
2833 * Setting PF_MEMALLOC here doesn't work - too many internal memory
2834 * allocations fail.
2835 *
2836 * 16May01: If we're reentered then journal_current_handle() will be
2837 * non-zero. We simply *return*.
2838 *
2839 * 1 July 2001: @@@ FIXME:
2840 * In journalled data mode, a data buffer may be metadata against the
2841 * current transaction. But the same file is part of a shared mapping
2842 * and someone does a writepage() on it.
2843 *
2844 * We will move the buffer onto the async_data list, but *after* it has
2845 * been dirtied. So there's a small window where we have dirty data on
2846 * BJ_Metadata.
2847 *
2848 * Note that this only applies to the last partial page in the file. The
2849 * bit which block_write_full_page() uses prepare/commit for. (That's
2850 * broken code anyway: it's wrong for msync()).
2851 *
2852 * It's a rare case: affects the final partial page, for journalled data
2853 * where the file is subject to bith write() and writepage() in the same
2854 * transction. To fix it we'll need a custom block_write_full_page().
2855 * We'll probably need that anyway for journalling writepage() output.
2856 *
2857 * We don't honour synchronous mounts for writepage(). That would be
2858 * disastrous. Any write() or metadata operation will sync the fs for
2859 * us.
2860 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002861 */
Jan Kara678aaf42008-07-11 19:27:31 -04002862static int __ext4_normal_writepage(struct page *page,
Jan Karacf108bc2008-07-11 19:27:31 -04002863 struct writeback_control *wbc)
2864{
2865 struct inode *inode = page->mapping->host;
2866
2867 if (test_opt(inode->i_sb, NOBH))
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002868 return nobh_writepage(page,
2869 ext4_normal_get_block_write, wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002870 else
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002871 return block_write_full_page(page,
2872 ext4_normal_get_block_write,
2873 wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002874}
2875
Jan Kara678aaf42008-07-11 19:27:31 -04002876static int ext4_normal_writepage(struct page *page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002877 struct writeback_control *wbc)
2878{
2879 struct inode *inode = page->mapping->host;
Jan Karacf108bc2008-07-11 19:27:31 -04002880 loff_t size = i_size_read(inode);
2881 loff_t len;
2882
2883 J_ASSERT(PageLocked(page));
Jan Karacf108bc2008-07-11 19:27:31 -04002884 if (page->index == size >> PAGE_CACHE_SHIFT)
2885 len = size & ~PAGE_CACHE_MASK;
2886 else
2887 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002888
2889 if (page_has_buffers(page)) {
2890 /* if page has buffers it should all be mapped
2891 * and allocated. If there are not buffers attached
2892 * to the page we know the page is dirty but it lost
2893 * buffers. That means that at some moment in time
2894 * after write_begin() / write_end() has been called
2895 * all buffers have been clean and thus they must have been
2896 * written at least once. So they are all mapped and we can
2897 * happily proceed with mapping them and writing the page.
2898 */
2899 BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
2900 ext4_bh_unmapped_or_delay));
2901 }
Jan Karacf108bc2008-07-11 19:27:31 -04002902
2903 if (!ext4_journal_current_handle())
Jan Kara678aaf42008-07-11 19:27:31 -04002904 return __ext4_normal_writepage(page, wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002905
2906 redirty_page_for_writepage(wbc, page);
2907 unlock_page(page);
2908 return 0;
2909}
2910
2911static int __ext4_journalled_writepage(struct page *page,
2912 struct writeback_control *wbc)
2913{
2914 struct address_space *mapping = page->mapping;
2915 struct inode *inode = mapping->host;
2916 struct buffer_head *page_bufs;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002917 handle_t *handle = NULL;
2918 int ret = 0;
2919 int err;
2920
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002921 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2922 ext4_normal_get_block_write);
Jan Karacf108bc2008-07-11 19:27:31 -04002923 if (ret != 0)
2924 goto out_unlock;
2925
2926 page_bufs = page_buffers(page);
2927 walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL,
2928 bget_one);
2929 /* As soon as we unlock the page, it can go away, but we have
2930 * references to buffers so we are safe */
2931 unlock_page(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002932
Mingming Cao617ba132006-10-11 01:20:53 -07002933 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002934 if (IS_ERR(handle)) {
2935 ret = PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04002936 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937 }
2938
Jan Karacf108bc2008-07-11 19:27:31 -04002939 ret = walk_page_buffers(handle, page_bufs, 0,
2940 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002941
Jan Karacf108bc2008-07-11 19:27:31 -04002942 err = walk_page_buffers(handle, page_bufs, 0,
2943 PAGE_CACHE_SIZE, NULL, write_end_fn);
2944 if (ret == 0)
2945 ret = err;
Mingming Cao617ba132006-10-11 01:20:53 -07002946 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 if (!ret)
2948 ret = err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949
Jan Karacf108bc2008-07-11 19:27:31 -04002950 walk_page_buffers(handle, page_bufs, 0,
2951 PAGE_CACHE_SIZE, NULL, bput_one);
2952 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2953 goto out;
2954
2955out_unlock:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002956 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04002957out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958 return ret;
2959}
2960
Mingming Cao617ba132006-10-11 01:20:53 -07002961static int ext4_journalled_writepage(struct page *page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002962 struct writeback_control *wbc)
2963{
2964 struct inode *inode = page->mapping->host;
Jan Karacf108bc2008-07-11 19:27:31 -04002965 loff_t size = i_size_read(inode);
2966 loff_t len;
2967
2968 J_ASSERT(PageLocked(page));
Jan Karacf108bc2008-07-11 19:27:31 -04002969 if (page->index == size >> PAGE_CACHE_SHIFT)
2970 len = size & ~PAGE_CACHE_MASK;
2971 else
2972 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002973
2974 if (page_has_buffers(page)) {
2975 /* if page has buffers it should all be mapped
2976 * and allocated. If there are not buffers attached
2977 * to the page we know the page is dirty but it lost
2978 * buffers. That means that at some moment in time
2979 * after write_begin() / write_end() has been called
2980 * all buffers have been clean and thus they must have been
2981 * written at least once. So they are all mapped and we can
2982 * happily proceed with mapping them and writing the page.
2983 */
2984 BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
2985 ext4_bh_unmapped_or_delay));
2986 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002987
Mingming Cao617ba132006-10-11 01:20:53 -07002988 if (ext4_journal_current_handle())
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002989 goto no_write;
2990
Jan Karacf108bc2008-07-11 19:27:31 -04002991 if (PageChecked(page)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002992 /*
2993 * It's mmapped pagecache. Add buffers and journal it. There
2994 * doesn't seem much point in redirtying the page here.
2995 */
2996 ClearPageChecked(page);
Jan Karacf108bc2008-07-11 19:27:31 -04002997 return __ext4_journalled_writepage(page, wbc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 } else {
2999 /*
3000 * It may be a page full of checkpoint-mode buffers. We don't
3001 * really know unless we go poke around in the buffer_heads.
3002 * But block_write_full_page will do the right thing.
3003 */
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04003004 return block_write_full_page(page,
3005 ext4_normal_get_block_write,
3006 wbc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003007 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003008no_write:
3009 redirty_page_for_writepage(wbc, page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003011 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012}
3013
Mingming Cao617ba132006-10-11 01:20:53 -07003014static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003015{
Mingming Cao617ba132006-10-11 01:20:53 -07003016 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017}
3018
3019static int
Mingming Cao617ba132006-10-11 01:20:53 -07003020ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 struct list_head *pages, unsigned nr_pages)
3022{
Mingming Cao617ba132006-10-11 01:20:53 -07003023 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003024}
3025
Mingming Cao617ba132006-10-11 01:20:53 -07003026static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027{
Mingming Cao617ba132006-10-11 01:20:53 -07003028 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029
3030 /*
3031 * If it's a full truncate we just forget about the pending dirtying
3032 */
3033 if (offset == 0)
3034 ClearPageChecked(page);
3035
Mingming Caodab291a2006-10-11 01:21:01 -07003036 jbd2_journal_invalidatepage(journal, page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037}
3038
Mingming Cao617ba132006-10-11 01:20:53 -07003039static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003040{
Mingming Cao617ba132006-10-11 01:20:53 -07003041 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003042
3043 WARN_ON(PageChecked(page));
3044 if (!page_has_buffers(page))
3045 return 0;
Mingming Caodab291a2006-10-11 01:21:01 -07003046 return jbd2_journal_try_to_free_buffers(journal, page, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047}
3048
3049/*
3050 * If the O_DIRECT write will extend the file then add this inode to the
3051 * orphan list. So recovery will truncate it back to the original size
3052 * if the machine crashes during the write.
3053 *
3054 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003055 * crashes then stale disk data _may_ be exposed inside the file. But current
3056 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003057 */
Mingming Cao617ba132006-10-11 01:20:53 -07003058static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003059 const struct iovec *iov, loff_t offset,
3060 unsigned long nr_segs)
3061{
3062 struct file *file = iocb->ki_filp;
3063 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003064 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003065 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066 ssize_t ret;
3067 int orphan = 0;
3068 size_t count = iov_length(iov, nr_segs);
3069
3070 if (rw == WRITE) {
3071 loff_t final_size = offset + count;
3072
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003073 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003074 /* Credits for sb + inode write */
3075 handle = ext4_journal_start(inode, 2);
3076 if (IS_ERR(handle)) {
3077 ret = PTR_ERR(handle);
3078 goto out;
3079 }
Mingming Cao617ba132006-10-11 01:20:53 -07003080 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003081 if (ret) {
3082 ext4_journal_stop(handle);
3083 goto out;
3084 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 orphan = 1;
3086 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003087 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088 }
3089 }
3090
3091 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3092 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003093 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003094
Jan Kara7fb54092008-02-10 01:08:38 -05003095 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003096 int err;
3097
Jan Kara7fb54092008-02-10 01:08:38 -05003098 /* Credits for sb + inode write */
3099 handle = ext4_journal_start(inode, 2);
3100 if (IS_ERR(handle)) {
3101 /* This is really bad luck. We've written the data
3102 * but cannot extend i_size. Bail out and pretend
3103 * the write failed... */
3104 ret = PTR_ERR(handle);
3105 goto out;
3106 }
3107 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003108 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003109 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003110 loff_t end = offset + ret;
3111 if (end > inode->i_size) {
3112 ei->i_disksize = end;
3113 i_size_write(inode, end);
3114 /*
3115 * We're going to return a positive `ret'
3116 * here due to non-zero-length I/O, so there's
3117 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003118 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003119 * ignore it.
3120 */
Mingming Cao617ba132006-10-11 01:20:53 -07003121 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122 }
3123 }
Mingming Cao617ba132006-10-11 01:20:53 -07003124 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003125 if (ret == 0)
3126 ret = err;
3127 }
3128out:
3129 return ret;
3130}
3131
3132/*
Mingming Cao617ba132006-10-11 01:20:53 -07003133 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003134 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3135 * much here because ->set_page_dirty is called under VFS locks. The page is
3136 * not necessarily locked.
3137 *
3138 * We cannot just dirty the page and leave attached buffers clean, because the
3139 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3140 * or jbddirty because all the journalling code will explode.
3141 *
3142 * So what we do is to mark the page "pending dirty" and next time writepage
3143 * is called, propagate that into the buffers appropriately.
3144 */
Mingming Cao617ba132006-10-11 01:20:53 -07003145static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003146{
3147 SetPageChecked(page);
3148 return __set_page_dirty_nobuffers(page);
3149}
3150
Mingming Cao617ba132006-10-11 01:20:53 -07003151static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003152 .readpage = ext4_readpage,
3153 .readpages = ext4_readpages,
3154 .writepage = ext4_normal_writepage,
3155 .sync_page = block_sync_page,
3156 .write_begin = ext4_write_begin,
3157 .write_end = ext4_ordered_write_end,
3158 .bmap = ext4_bmap,
3159 .invalidatepage = ext4_invalidatepage,
3160 .releasepage = ext4_releasepage,
3161 .direct_IO = ext4_direct_IO,
3162 .migratepage = buffer_migrate_page,
3163 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003164};
3165
Mingming Cao617ba132006-10-11 01:20:53 -07003166static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003167 .readpage = ext4_readpage,
3168 .readpages = ext4_readpages,
3169 .writepage = ext4_normal_writepage,
3170 .sync_page = block_sync_page,
3171 .write_begin = ext4_write_begin,
3172 .write_end = ext4_writeback_write_end,
3173 .bmap = ext4_bmap,
3174 .invalidatepage = ext4_invalidatepage,
3175 .releasepage = ext4_releasepage,
3176 .direct_IO = ext4_direct_IO,
3177 .migratepage = buffer_migrate_page,
3178 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179};
3180
Mingming Cao617ba132006-10-11 01:20:53 -07003181static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003182 .readpage = ext4_readpage,
3183 .readpages = ext4_readpages,
3184 .writepage = ext4_journalled_writepage,
3185 .sync_page = block_sync_page,
3186 .write_begin = ext4_write_begin,
3187 .write_end = ext4_journalled_write_end,
3188 .set_page_dirty = ext4_journalled_set_page_dirty,
3189 .bmap = ext4_bmap,
3190 .invalidatepage = ext4_invalidatepage,
3191 .releasepage = ext4_releasepage,
3192 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003193};
3194
Alex Tomas64769242008-07-11 19:27:31 -04003195static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003196 .readpage = ext4_readpage,
3197 .readpages = ext4_readpages,
3198 .writepage = ext4_da_writepage,
3199 .writepages = ext4_da_writepages,
3200 .sync_page = block_sync_page,
3201 .write_begin = ext4_da_write_begin,
3202 .write_end = ext4_da_write_end,
3203 .bmap = ext4_bmap,
3204 .invalidatepage = ext4_da_invalidatepage,
3205 .releasepage = ext4_releasepage,
3206 .direct_IO = ext4_direct_IO,
3207 .migratepage = buffer_migrate_page,
3208 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003209};
3210
Mingming Cao617ba132006-10-11 01:20:53 -07003211void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003213 if (ext4_should_order_data(inode) &&
3214 test_opt(inode->i_sb, DELALLOC))
3215 inode->i_mapping->a_ops = &ext4_da_aops;
3216 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003217 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003218 else if (ext4_should_writeback_data(inode) &&
3219 test_opt(inode->i_sb, DELALLOC))
3220 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003221 else if (ext4_should_writeback_data(inode))
3222 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003223 else
Mingming Cao617ba132006-10-11 01:20:53 -07003224 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003225}
3226
3227/*
Mingming Cao617ba132006-10-11 01:20:53 -07003228 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229 * up to the end of the block which corresponds to `from'.
3230 * This required during truncate. We need to physically zero the tail end
3231 * of that block so it doesn't yield old data if the file is later grown.
3232 */
Jan Karacf108bc2008-07-11 19:27:31 -04003233int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003234 struct address_space *mapping, loff_t from)
3235{
Mingming Cao617ba132006-10-11 01:20:53 -07003236 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003237 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003238 unsigned blocksize, length, pos;
3239 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003240 struct inode *inode = mapping->host;
3241 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003242 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003243 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003244
Jan Karacf108bc2008-07-11 19:27:31 -04003245 page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT);
3246 if (!page)
3247 return -EINVAL;
3248
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003249 blocksize = inode->i_sb->s_blocksize;
3250 length = blocksize - (offset & (blocksize - 1));
3251 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3252
3253 /*
3254 * For "nobh" option, we can only work if we don't need to
3255 * read-in the page - otherwise we create buffers to do the IO.
3256 */
3257 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003258 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003259 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260 set_page_dirty(page);
3261 goto unlock;
3262 }
3263
3264 if (!page_has_buffers(page))
3265 create_empty_buffers(page, blocksize, 0);
3266
3267 /* Find the buffer that contains "offset" */
3268 bh = page_buffers(page);
3269 pos = blocksize;
3270 while (offset >= pos) {
3271 bh = bh->b_this_page;
3272 iblock++;
3273 pos += blocksize;
3274 }
3275
3276 err = 0;
3277 if (buffer_freed(bh)) {
3278 BUFFER_TRACE(bh, "freed: skip");
3279 goto unlock;
3280 }
3281
3282 if (!buffer_mapped(bh)) {
3283 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003284 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285 /* unmapped? It's a hole - nothing to do */
3286 if (!buffer_mapped(bh)) {
3287 BUFFER_TRACE(bh, "still unmapped");
3288 goto unlock;
3289 }
3290 }
3291
3292 /* Ok, it's mapped. Make sure it's up-to-date */
3293 if (PageUptodate(page))
3294 set_buffer_uptodate(bh);
3295
3296 if (!buffer_uptodate(bh)) {
3297 err = -EIO;
3298 ll_rw_block(READ, 1, &bh);
3299 wait_on_buffer(bh);
3300 /* Uhhuh. Read error. Complain and punt. */
3301 if (!buffer_uptodate(bh))
3302 goto unlock;
3303 }
3304
Mingming Cao617ba132006-10-11 01:20:53 -07003305 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003306 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003307 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308 if (err)
3309 goto unlock;
3310 }
3311
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003312 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003313
3314 BUFFER_TRACE(bh, "zeroed end of block");
3315
3316 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003317 if (ext4_should_journal_data(inode)) {
3318 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003319 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003320 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003321 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322 mark_buffer_dirty(bh);
3323 }
3324
3325unlock:
3326 unlock_page(page);
3327 page_cache_release(page);
3328 return err;
3329}
3330
3331/*
3332 * Probably it should be a library function... search for first non-zero word
3333 * or memcmp with zero_page, whatever is better for particular architecture.
3334 * Linus?
3335 */
3336static inline int all_zeroes(__le32 *p, __le32 *q)
3337{
3338 while (p < q)
3339 if (*p++)
3340 return 0;
3341 return 1;
3342}
3343
3344/**
Mingming Cao617ba132006-10-11 01:20:53 -07003345 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003346 * @inode: inode in question
3347 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003348 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003349 * @chain: place to store the pointers to partial indirect blocks
3350 * @top: place to the (detached) top of branch
3351 *
Mingming Cao617ba132006-10-11 01:20:53 -07003352 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003353 *
3354 * When we do truncate() we may have to clean the ends of several
3355 * indirect blocks but leave the blocks themselves alive. Block is
3356 * partially truncated if some data below the new i_size is refered
3357 * from it (and it is on the path to the first completely truncated
3358 * data block, indeed). We have to free the top of that path along
3359 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003360 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003361 * finishes, we may safely do the latter, but top of branch may
3362 * require special attention - pageout below the truncation point
3363 * might try to populate it.
3364 *
3365 * We atomically detach the top of branch from the tree, store the
3366 * block number of its root in *@top, pointers to buffer_heads of
3367 * partially truncated blocks - in @chain[].bh and pointers to
3368 * their last elements that should not be removed - in
3369 * @chain[].p. Return value is the pointer to last filled element
3370 * of @chain.
3371 *
3372 * The work left to caller to do the actual freeing of subtrees:
3373 * a) free the subtree starting from *@top
3374 * b) free the subtrees whose roots are stored in
3375 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3376 * c) free the subtrees growing from the inode past the @chain[0].
3377 * (no partially truncated stuff there). */
3378
Mingming Cao617ba132006-10-11 01:20:53 -07003379static Indirect *ext4_find_shared(struct inode *inode, int depth,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003380 ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003381{
3382 Indirect *partial, *p;
3383 int k, err;
3384
3385 *top = 0;
3386 /* Make k index the deepest non-null offest + 1 */
3387 for (k = depth; k > 1 && !offsets[k-1]; k--)
3388 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003389 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003390 /* Writer: pointers */
3391 if (!partial)
3392 partial = chain + k-1;
3393 /*
3394 * If the branch acquired continuation since we've looked at it -
3395 * fine, it should all survive and (new) top doesn't belong to us.
3396 */
3397 if (!partial->key && *partial->p)
3398 /* Writer: end */
3399 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003400 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003401 ;
3402 /*
3403 * OK, we've found the last block that must survive. The rest of our
3404 * branch should be detached before unlocking. However, if that rest
3405 * of branch is all ours and does not grow immediately from the inode
3406 * it's easier to cheat and just decrement partial->p.
3407 */
3408 if (p == chain + k - 1 && p > chain) {
3409 p->p--;
3410 } else {
3411 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003412 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003413#if 0
3414 *p->p = 0;
3415#endif
3416 }
3417 /* Writer: end */
3418
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003419 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003420 brelse(partial->bh);
3421 partial--;
3422 }
3423no_top:
3424 return partial;
3425}
3426
3427/*
3428 * Zero a number of block pointers in either an inode or an indirect block.
3429 * If we restart the transaction we must again get write access to the
3430 * indirect block for further modification.
3431 *
3432 * We release `count' blocks on disk, but (last - first) may be greater
3433 * than `count' because there can be holes in there.
3434 */
Mingming Cao617ba132006-10-11 01:20:53 -07003435static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
3436 struct buffer_head *bh, ext4_fsblk_t block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003437 unsigned long count, __le32 *first, __le32 *last)
3438{
3439 __le32 *p;
3440 if (try_to_extend_transaction(handle, inode)) {
3441 if (bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003442 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
3443 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003444 }
Mingming Cao617ba132006-10-11 01:20:53 -07003445 ext4_mark_inode_dirty(handle, inode);
3446 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447 if (bh) {
3448 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003449 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003450 }
3451 }
3452
3453 /*
3454 * Any buffers which are on the journal will be in memory. We find
Mingming Caodab291a2006-10-11 01:21:01 -07003455 * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003456 * on them. We've already detached each block from the file, so
Mingming Caodab291a2006-10-11 01:21:01 -07003457 * bforget() in jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458 *
Mingming Caodab291a2006-10-11 01:21:01 -07003459 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003460 */
3461 for (p = first; p < last; p++) {
3462 u32 nr = le32_to_cpu(*p);
3463 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003464 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003465
3466 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003467 tbh = sb_find_get_block(inode->i_sb, nr);
3468 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003469 }
3470 }
3471
Alex Tomasc9de5602008-01-29 00:19:52 -05003472 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003473}
3474
3475/**
Mingming Cao617ba132006-10-11 01:20:53 -07003476 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003477 * @handle: handle for this transaction
3478 * @inode: inode we are dealing with
3479 * @this_bh: indirect buffer_head which contains *@first and *@last
3480 * @first: array of block numbers
3481 * @last: points immediately past the end of array
3482 *
3483 * We are freeing all blocks refered from that array (numbers are stored as
3484 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3485 *
3486 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3487 * blocks are contiguous then releasing them at one time will only affect one
3488 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3489 * actually use a lot of journal space.
3490 *
3491 * @this_bh will be %NULL if @first and @last point into the inode's direct
3492 * block pointers.
3493 */
Mingming Cao617ba132006-10-11 01:20:53 -07003494static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003495 struct buffer_head *this_bh,
3496 __le32 *first, __le32 *last)
3497{
Mingming Cao617ba132006-10-11 01:20:53 -07003498 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003499 unsigned long count = 0; /* Number of blocks in the run */
3500 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3501 corresponding to
3502 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003503 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003504 __le32 *p; /* Pointer into inode/ind
3505 for current block */
3506 int err;
3507
3508 if (this_bh) { /* For indirect block */
3509 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003510 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003511 /* Important: if we can't update the indirect pointers
3512 * to the blocks, we can't free them. */
3513 if (err)
3514 return;
3515 }
3516
3517 for (p = first; p < last; p++) {
3518 nr = le32_to_cpu(*p);
3519 if (nr) {
3520 /* accumulate blocks to free if they're contiguous */
3521 if (count == 0) {
3522 block_to_free = nr;
3523 block_to_free_p = p;
3524 count = 1;
3525 } else if (nr == block_to_free + count) {
3526 count++;
3527 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003528 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003529 block_to_free,
3530 count, block_to_free_p, p);
3531 block_to_free = nr;
3532 block_to_free_p = p;
3533 count = 1;
3534 }
3535 }
3536 }
3537
3538 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003539 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003540 count, block_to_free_p, p);
3541
3542 if (this_bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003543 BUFFER_TRACE(this_bh, "call ext4_journal_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003544
3545 /*
3546 * The buffer head should have an attached journal head at this
3547 * point. However, if the data is corrupted and an indirect
3548 * block pointed to itself, it would have been detached when
3549 * the block was cleared. Check for this instead of OOPSing.
3550 */
3551 if (bh2jh(this_bh))
3552 ext4_journal_dirty_metadata(handle, this_bh);
3553 else
3554 ext4_error(inode->i_sb, __func__,
3555 "circular indirect block detected, "
3556 "inode=%lu, block=%llu",
3557 inode->i_ino,
3558 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003559 }
3560}
3561
3562/**
Mingming Cao617ba132006-10-11 01:20:53 -07003563 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003564 * @handle: JBD handle for this transaction
3565 * @inode: inode we are dealing with
3566 * @parent_bh: the buffer_head which contains *@first and *@last
3567 * @first: array of block numbers
3568 * @last: pointer immediately past the end of array
3569 * @depth: depth of the branches to free
3570 *
3571 * We are freeing all blocks refered from these branches (numbers are
3572 * stored as little-endian 32-bit) and updating @inode->i_blocks
3573 * appropriately.
3574 */
Mingming Cao617ba132006-10-11 01:20:53 -07003575static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003576 struct buffer_head *parent_bh,
3577 __le32 *first, __le32 *last, int depth)
3578{
Mingming Cao617ba132006-10-11 01:20:53 -07003579 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003580 __le32 *p;
3581
3582 if (is_handle_aborted(handle))
3583 return;
3584
3585 if (depth--) {
3586 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003587 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003588 p = last;
3589 while (--p >= first) {
3590 nr = le32_to_cpu(*p);
3591 if (!nr)
3592 continue; /* A hole */
3593
3594 /* Go read the buffer for the next level down */
3595 bh = sb_bread(inode->i_sb, nr);
3596
3597 /*
3598 * A read failure? Report error and clear slot
3599 * (should be rare).
3600 */
3601 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003602 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003603 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003604 inode->i_ino, nr);
3605 continue;
3606 }
3607
3608 /* This zaps the entire block. Bottom up. */
3609 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003610 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003611 (__le32 *) bh->b_data,
3612 (__le32 *) bh->b_data + addr_per_block,
3613 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003614
3615 /*
3616 * We've probably journalled the indirect block several
3617 * times during the truncate. But it's no longer
3618 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003619 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003620 *
3621 * That's easy if it's exclusively part of this
3622 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003623 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003624 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003625 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003626 * unmap_underlying_metadata() will find this block
3627 * and will try to get rid of it. damn, damn.
3628 *
3629 * If this block has already been committed to the
3630 * journal, a revoke record will be written. And
3631 * revoke records must be emitted *before* clearing
3632 * this block's bit in the bitmaps.
3633 */
Mingming Cao617ba132006-10-11 01:20:53 -07003634 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003635
3636 /*
3637 * Everything below this this pointer has been
3638 * released. Now let this top-of-subtree go.
3639 *
3640 * We want the freeing of this indirect block to be
3641 * atomic in the journal with the updating of the
3642 * bitmap block which owns it. So make some room in
3643 * the journal.
3644 *
3645 * We zero the parent pointer *after* freeing its
3646 * pointee in the bitmaps, so if extend_transaction()
3647 * for some reason fails to put the bitmap changes and
3648 * the release into the same transaction, recovery
3649 * will merely complain about releasing a free block,
3650 * rather than leaking blocks.
3651 */
3652 if (is_handle_aborted(handle))
3653 return;
3654 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003655 ext4_mark_inode_dirty(handle, inode);
3656 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003657 }
3658
Alex Tomasc9de5602008-01-29 00:19:52 -05003659 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003660
3661 if (parent_bh) {
3662 /*
3663 * The block which we have just freed is
3664 * pointed to by an indirect block: journal it
3665 */
3666 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003667 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003668 parent_bh)){
3669 *p = 0;
3670 BUFFER_TRACE(parent_bh,
Mingming Cao617ba132006-10-11 01:20:53 -07003671 "call ext4_journal_dirty_metadata");
3672 ext4_journal_dirty_metadata(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003673 parent_bh);
3674 }
3675 }
3676 }
3677 } else {
3678 /* We have reached the bottom of the tree. */
3679 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003680 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681 }
3682}
3683
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003684int ext4_can_truncate(struct inode *inode)
3685{
3686 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3687 return 0;
3688 if (S_ISREG(inode->i_mode))
3689 return 1;
3690 if (S_ISDIR(inode->i_mode))
3691 return 1;
3692 if (S_ISLNK(inode->i_mode))
3693 return !ext4_inode_is_fast_symlink(inode);
3694 return 0;
3695}
3696
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003697/*
Mingming Cao617ba132006-10-11 01:20:53 -07003698 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003699 *
Mingming Cao617ba132006-10-11 01:20:53 -07003700 * We block out ext4_get_block() block instantiations across the entire
3701 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003702 * simultaneously on behalf of the same inode.
3703 *
3704 * As we work through the truncate and commmit bits of it to the journal there
3705 * is one core, guiding principle: the file's tree must always be consistent on
3706 * disk. We must be able to restart the truncate after a crash.
3707 *
3708 * The file's tree may be transiently inconsistent in memory (although it
3709 * probably isn't), but whenever we close off and commit a journal transaction,
3710 * the contents of (the filesystem + the journal) must be consistent and
3711 * restartable. It's pretty simple, really: bottom up, right to left (although
3712 * left-to-right works OK too).
3713 *
3714 * Note that at recovery time, journal replay occurs *before* the restart of
3715 * truncate against the orphan inode list.
3716 *
3717 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003718 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003719 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003720 * ext4_truncate() to have another go. So there will be instantiated blocks
3721 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003722 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003723 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003724 */
Mingming Cao617ba132006-10-11 01:20:53 -07003725void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003726{
3727 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003728 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003729 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003730 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003731 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003732 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003733 Indirect chain[4];
3734 Indirect *partial;
3735 __le32 nr = 0;
3736 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003737 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003738 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003739
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003740 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003741 return;
3742
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003743 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003744 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003745 return;
3746 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003747
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003748 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003749 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003750 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003751
3752 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003753 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003754
Jan Karacf108bc2008-07-11 19:27:31 -04003755 if (inode->i_size & (blocksize - 1))
3756 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3757 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758
Mingming Cao617ba132006-10-11 01:20:53 -07003759 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003760 if (n == 0)
3761 goto out_stop; /* error */
3762
3763 /*
3764 * OK. This truncate is going to happen. We add the inode to the
3765 * orphan list, so that if this truncate spans multiple transactions,
3766 * and we crash, we will resume the truncate when the filesystem
3767 * recovers. It also marks the inode dirty, to catch the new size.
3768 *
3769 * Implication: the file must always be in a sane, consistent
3770 * truncatable state while each transaction commits.
3771 */
Mingming Cao617ba132006-10-11 01:20:53 -07003772 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003773 goto out_stop;
3774
3775 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04003776 * From here we block out all ext4_get_block() callers who want to
3777 * modify the block allocation tree.
3778 */
3779 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04003780
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003781 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04003782
Mingming Cao632eaea2008-07-11 19:27:31 -04003783 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003784 * The orphan list entry will now protect us from any crash which
3785 * occurs before the truncate completes, so it is now safe to propagate
3786 * the new, shorter inode size (held for now in i_size) into the
3787 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07003788 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003789 */
3790 ei->i_disksize = inode->i_size;
3791
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003792 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003793 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
3794 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003795 goto do_indirects;
3796 }
3797
Mingming Cao617ba132006-10-11 01:20:53 -07003798 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003799 /* Kill the top of shared branch (not detached) */
3800 if (nr) {
3801 if (partial == chain) {
3802 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07003803 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003804 &nr, &nr+1, (chain+n-1) - partial);
3805 *partial->p = 0;
3806 /*
3807 * We mark the inode dirty prior to restart,
3808 * and prior to stop. No need for it here.
3809 */
3810 } else {
3811 /* Shared branch grows from an indirect block */
3812 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003813 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003814 partial->p,
3815 partial->p+1, (chain+n-1) - partial);
3816 }
3817 }
3818 /* Clear the ends of indirect blocks on the shared branch */
3819 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07003820 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003821 (__le32*)partial->bh->b_data+addr_per_block,
3822 (chain+n-1) - partial);
3823 BUFFER_TRACE(partial->bh, "call brelse");
3824 brelse (partial->bh);
3825 partial--;
3826 }
3827do_indirects:
3828 /* Kill the remaining (whole) subtrees */
3829 switch (offsets[0]) {
3830 default:
Mingming Cao617ba132006-10-11 01:20:53 -07003831 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003832 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003833 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
3834 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003835 }
Mingming Cao617ba132006-10-11 01:20:53 -07003836 case EXT4_IND_BLOCK:
3837 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003838 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003839 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
3840 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003841 }
Mingming Cao617ba132006-10-11 01:20:53 -07003842 case EXT4_DIND_BLOCK:
3843 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003844 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003845 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
3846 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003847 }
Mingming Cao617ba132006-10-11 01:20:53 -07003848 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003849 ;
3850 }
3851
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003852 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003853 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003854 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003855
3856 /*
3857 * In a multi-transaction truncate, we only make the final transaction
3858 * synchronous
3859 */
3860 if (IS_SYNC(inode))
3861 handle->h_sync = 1;
3862out_stop:
3863 /*
3864 * If this was a simple ftruncate(), and the file will remain alive
3865 * then we need to clear up the orphan record which we created above.
3866 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07003867 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003868 * orphan info for us.
3869 */
3870 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003871 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003872
Mingming Cao617ba132006-10-11 01:20:53 -07003873 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003874}
3875
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876/*
Mingming Cao617ba132006-10-11 01:20:53 -07003877 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003878 * underlying buffer_head on success. If 'in_mem' is true, we have all
3879 * data in memory that is needed to recreate the on-disk version of this
3880 * inode.
3881 */
Mingming Cao617ba132006-10-11 01:20:53 -07003882static int __ext4_get_inode_loc(struct inode *inode,
3883 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003884{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003885 struct ext4_group_desc *gdp;
3886 struct buffer_head *bh;
3887 struct super_block *sb = inode->i_sb;
3888 ext4_fsblk_t block;
3889 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003890
Theodore Ts'o240799c2008-10-09 23:53:47 -04003891 iloc->bh = 0;
3892 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893 return -EIO;
3894
Theodore Ts'o240799c2008-10-09 23:53:47 -04003895 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3896 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3897 if (!gdp)
3898 return -EIO;
3899
3900 /*
3901 * Figure out the offset within the block group inode table
3902 */
3903 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
3904 inode_offset = ((inode->i_ino - 1) %
3905 EXT4_INODES_PER_GROUP(sb));
3906 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3907 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3908
3909 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003910 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04003911 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
3912 "inode block - inode=%lu, block=%llu",
3913 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003914 return -EIO;
3915 }
3916 if (!buffer_uptodate(bh)) {
3917 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003918
3919 /*
3920 * If the buffer has the write error flag, we have failed
3921 * to write out another inode in the same block. In this
3922 * case, we don't have to read the block because we may
3923 * read the old inode data successfully.
3924 */
3925 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
3926 set_buffer_uptodate(bh);
3927
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003928 if (buffer_uptodate(bh)) {
3929 /* someone brought it uptodate while we waited */
3930 unlock_buffer(bh);
3931 goto has_buffer;
3932 }
3933
3934 /*
3935 * If we have all information of the inode in memory and this
3936 * is the only valid inode in the block, we need not read the
3937 * block.
3938 */
3939 if (in_mem) {
3940 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003941 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003942
Theodore Ts'o240799c2008-10-09 23:53:47 -04003943 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003944
3945 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003946 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003947 if (!bitmap_bh)
3948 goto make_io;
3949
3950 /*
3951 * If the inode bitmap isn't in cache then the
3952 * optimisation may end up performing two reads instead
3953 * of one, so skip it.
3954 */
3955 if (!buffer_uptodate(bitmap_bh)) {
3956 brelse(bitmap_bh);
3957 goto make_io;
3958 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04003959 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003960 if (i == inode_offset)
3961 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07003962 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003963 break;
3964 }
3965 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003966 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003967 /* all other inodes are free, so skip I/O */
3968 memset(bh->b_data, 0, bh->b_size);
3969 set_buffer_uptodate(bh);
3970 unlock_buffer(bh);
3971 goto has_buffer;
3972 }
3973 }
3974
3975make_io:
3976 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04003977 * If we need to do any I/O, try to pre-readahead extra
3978 * blocks from the inode table.
3979 */
3980 if (EXT4_SB(sb)->s_inode_readahead_blks) {
3981 ext4_fsblk_t b, end, table;
3982 unsigned num;
3983
3984 table = ext4_inode_table(sb, gdp);
3985 /* Make sure s_inode_readahead_blks is a power of 2 */
3986 while (EXT4_SB(sb)->s_inode_readahead_blks &
3987 (EXT4_SB(sb)->s_inode_readahead_blks-1))
3988 EXT4_SB(sb)->s_inode_readahead_blks =
3989 (EXT4_SB(sb)->s_inode_readahead_blks &
3990 (EXT4_SB(sb)->s_inode_readahead_blks-1));
3991 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
3992 if (table > b)
3993 b = table;
3994 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
3995 num = EXT4_INODES_PER_GROUP(sb);
3996 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3997 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
3998 num -= le16_to_cpu(gdp->bg_itable_unused);
3999 table += num / inodes_per_block;
4000 if (end > table)
4001 end = table;
4002 while (b <= end)
4003 sb_breadahead(sb, b++);
4004 }
4005
4006 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004007 * There are other valid inodes in the buffer, this inode
4008 * has in-inode xattrs, or we don't have this inode in memory.
4009 * Read the block from disk.
4010 */
4011 get_bh(bh);
4012 bh->b_end_io = end_buffer_read_sync;
4013 submit_bh(READ_META, bh);
4014 wait_on_buffer(bh);
4015 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004016 ext4_error(sb, __func__,
4017 "unable to read inode block - inode=%lu, "
4018 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004019 brelse(bh);
4020 return -EIO;
4021 }
4022 }
4023has_buffer:
4024 iloc->bh = bh;
4025 return 0;
4026}
4027
Mingming Cao617ba132006-10-11 01:20:53 -07004028int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004029{
4030 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004031 return __ext4_get_inode_loc(inode, iloc,
4032 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004033}
4034
Mingming Cao617ba132006-10-11 01:20:53 -07004035void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004036{
Mingming Cao617ba132006-10-11 01:20:53 -07004037 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004038
4039 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004040 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004041 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004042 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004043 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004044 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004045 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004046 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004047 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004048 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004049 inode->i_flags |= S_DIRSYNC;
4050}
4051
Jan Karaff9ddf72007-07-18 09:24:20 -04004052/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4053void ext4_get_inode_flags(struct ext4_inode_info *ei)
4054{
4055 unsigned int flags = ei->vfs_inode.i_flags;
4056
4057 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4058 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4059 if (flags & S_SYNC)
4060 ei->i_flags |= EXT4_SYNC_FL;
4061 if (flags & S_APPEND)
4062 ei->i_flags |= EXT4_APPEND_FL;
4063 if (flags & S_IMMUTABLE)
4064 ei->i_flags |= EXT4_IMMUTABLE_FL;
4065 if (flags & S_NOATIME)
4066 ei->i_flags |= EXT4_NOATIME_FL;
4067 if (flags & S_DIRSYNC)
4068 ei->i_flags |= EXT4_DIRSYNC_FL;
4069}
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004070static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
4071 struct ext4_inode_info *ei)
4072{
4073 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004074 struct inode *inode = &(ei->vfs_inode);
4075 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004076
4077 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4078 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4079 /* we are using combined 48 bit field */
4080 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4081 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004082 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4083 /* i_blocks represent file system block size */
4084 return i_blocks << (inode->i_blkbits - 9);
4085 } else {
4086 return i_blocks;
4087 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004088 } else {
4089 return le32_to_cpu(raw_inode->i_blocks_lo);
4090 }
4091}
Jan Karaff9ddf72007-07-18 09:24:20 -04004092
David Howells1d1fe1e2008-02-07 00:15:37 -08004093struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004094{
Mingming Cao617ba132006-10-11 01:20:53 -07004095 struct ext4_iloc iloc;
4096 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004097 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004098 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004099 struct inode *inode;
4100 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004101 int block;
4102
David Howells1d1fe1e2008-02-07 00:15:37 -08004103 inode = iget_locked(sb, ino);
4104 if (!inode)
4105 return ERR_PTR(-ENOMEM);
4106 if (!(inode->i_state & I_NEW))
4107 return inode;
4108
4109 ei = EXT4_I(inode);
Theodore Ts'o03010a32008-10-10 20:02:48 -04004110#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07004111 ei->i_acl = EXT4_ACL_NOT_CACHED;
4112 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004113#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114
David Howells1d1fe1e2008-02-07 00:15:37 -08004115 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4116 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004117 goto bad_inode;
4118 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004119 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004120 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4121 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4122 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004123 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004124 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4125 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4126 }
4127 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004128
4129 ei->i_state = 0;
4130 ei->i_dir_start_lookup = 0;
4131 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4132 /* We now have enough fields to check if the inode was active or not.
4133 * This is needed because nfsd might try to access dead inodes
4134 * the test is that same one that e2fsck uses
4135 * NeilBrown 1999oct15
4136 */
4137 if (inode->i_nlink == 0) {
4138 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004139 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004140 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004141 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004142 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004143 goto bad_inode;
4144 }
4145 /* The only unlinked inodes we let through here have
4146 * valid i_mode and are being read by the orphan
4147 * recovery code: that's fine, we're about to complete
4148 * the process of deleting those. */
4149 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004150 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004151 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004152 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004153 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004154 cpu_to_le32(EXT4_OS_HURD)) {
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004155 ei->i_file_acl |=
4156 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004157 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004158 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004159 ei->i_disksize = inode->i_size;
4160 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4161 ei->i_block_group = iloc.block_group;
4162 /*
4163 * NOTE! The in-memory inode i_data array is in little-endian order
4164 * even on big-endian machines: we do NOT byteswap the block numbers!
4165 */
Mingming Cao617ba132006-10-11 01:20:53 -07004166 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004167 ei->i_data[block] = raw_inode->i_block[block];
4168 INIT_LIST_HEAD(&ei->i_orphan);
4169
Eric Sandeen0040d982008-02-05 22:36:43 -05004170 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004171 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004172 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004173 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004174 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004175 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004176 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004177 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004178 if (ei->i_extra_isize == 0) {
4179 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004180 ei->i_extra_isize = sizeof(struct ext4_inode) -
4181 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004182 } else {
4183 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004184 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004185 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004186 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
4187 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188 }
4189 } else
4190 ei->i_extra_isize = 0;
4191
Kalpak Shahef7f3832007-07-18 09:15:20 -04004192 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4193 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4194 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4195 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4196
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004197 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4198 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4199 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4200 inode->i_version |=
4201 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4202 }
4203
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004204 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004205 inode->i_op = &ext4_file_inode_operations;
4206 inode->i_fop = &ext4_file_operations;
4207 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004208 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004209 inode->i_op = &ext4_dir_inode_operations;
4210 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004211 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004212 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004213 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004214 nd_terminate_link(ei->i_data, inode->i_size,
4215 sizeof(ei->i_data) - 1);
4216 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004217 inode->i_op = &ext4_symlink_inode_operations;
4218 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004219 }
4220 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004221 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004222 if (raw_inode->i_block[0])
4223 init_special_inode(inode, inode->i_mode,
4224 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4225 else
4226 init_special_inode(inode, inode->i_mode,
4227 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4228 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004229 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004230 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004231 unlock_new_inode(inode);
4232 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004233
4234bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004235 iget_failed(inode);
4236 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004237}
4238
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004239static int ext4_inode_blocks_set(handle_t *handle,
4240 struct ext4_inode *raw_inode,
4241 struct ext4_inode_info *ei)
4242{
4243 struct inode *inode = &(ei->vfs_inode);
4244 u64 i_blocks = inode->i_blocks;
4245 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004246
4247 if (i_blocks <= ~0U) {
4248 /*
4249 * i_blocks can be represnted in a 32 bit variable
4250 * as multiple of 512 bytes
4251 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004252 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004253 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004254 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004255 return 0;
4256 }
4257 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4258 return -EFBIG;
4259
4260 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004261 /*
4262 * i_blocks can be represented in a 48 bit variable
4263 * as multiple of 512 bytes
4264 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004265 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004266 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004267 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004268 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004269 ei->i_flags |= EXT4_HUGE_FILE_FL;
4270 /* i_block is stored in file system block size */
4271 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4272 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4273 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004274 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004275 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004276}
4277
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004278/*
4279 * Post the struct inode info into an on-disk inode location in the
4280 * buffer-cache. This gobbles the caller's reference to the
4281 * buffer_head in the inode location struct.
4282 *
4283 * The caller must have write access to iloc->bh.
4284 */
Mingming Cao617ba132006-10-11 01:20:53 -07004285static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004286 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004287 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004288{
Mingming Cao617ba132006-10-11 01:20:53 -07004289 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4290 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004291 struct buffer_head *bh = iloc->bh;
4292 int err = 0, rc, block;
4293
4294 /* For fields not not tracking in the in-memory inode,
4295 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004296 if (ei->i_state & EXT4_STATE_NEW)
4297 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004298
Jan Karaff9ddf72007-07-18 09:24:20 -04004299 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004300 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004301 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004302 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4303 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4304/*
4305 * Fix up interoperability with old kernels. Otherwise, old inodes get
4306 * re-used with the upper 16 bits of the uid/gid intact
4307 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004308 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004309 raw_inode->i_uid_high =
4310 cpu_to_le16(high_16_bits(inode->i_uid));
4311 raw_inode->i_gid_high =
4312 cpu_to_le16(high_16_bits(inode->i_gid));
4313 } else {
4314 raw_inode->i_uid_high = 0;
4315 raw_inode->i_gid_high = 0;
4316 }
4317 } else {
4318 raw_inode->i_uid_low =
4319 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4320 raw_inode->i_gid_low =
4321 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4322 raw_inode->i_uid_high = 0;
4323 raw_inode->i_gid_high = 0;
4324 }
4325 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004326
4327 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4328 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4329 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4330 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4331
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004332 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4333 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004334 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004335 /* clear the migrate flag in the raw_inode */
4336 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004337 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4338 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004339 raw_inode->i_file_acl_high =
4340 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004341 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004342 ext4_isize_set(raw_inode, ei->i_disksize);
4343 if (ei->i_disksize > 0x7fffffffULL) {
4344 struct super_block *sb = inode->i_sb;
4345 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4346 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4347 EXT4_SB(sb)->s_es->s_rev_level ==
4348 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4349 /* If this is the first large file
4350 * created, add a flag to the superblock.
4351 */
4352 err = ext4_journal_get_write_access(handle,
4353 EXT4_SB(sb)->s_sbh);
4354 if (err)
4355 goto out_brelse;
4356 ext4_update_dynamic_rev(sb);
4357 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004358 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004359 sb->s_dirt = 1;
4360 handle->h_sync = 1;
4361 err = ext4_journal_dirty_metadata(handle,
4362 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004363 }
4364 }
4365 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4366 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4367 if (old_valid_dev(inode->i_rdev)) {
4368 raw_inode->i_block[0] =
4369 cpu_to_le32(old_encode_dev(inode->i_rdev));
4370 raw_inode->i_block[1] = 0;
4371 } else {
4372 raw_inode->i_block[0] = 0;
4373 raw_inode->i_block[1] =
4374 cpu_to_le32(new_encode_dev(inode->i_rdev));
4375 raw_inode->i_block[2] = 0;
4376 }
Mingming Cao617ba132006-10-11 01:20:53 -07004377 } else for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004378 raw_inode->i_block[block] = ei->i_data[block];
4379
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004380 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4381 if (ei->i_extra_isize) {
4382 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4383 raw_inode->i_version_hi =
4384 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004385 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004386 }
4387
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004388
Mingming Cao617ba132006-10-11 01:20:53 -07004389 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
4390 rc = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004391 if (!err)
4392 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004393 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004394
4395out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004396 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004397 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004398 return err;
4399}
4400
4401/*
Mingming Cao617ba132006-10-11 01:20:53 -07004402 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004403 *
4404 * We are called from a few places:
4405 *
4406 * - Within generic_file_write() for O_SYNC files.
4407 * Here, there will be no transaction running. We wait for any running
4408 * trasnaction to commit.
4409 *
4410 * - Within sys_sync(), kupdate and such.
4411 * We wait on commit, if tol to.
4412 *
4413 * - Within prune_icache() (PF_MEMALLOC == true)
4414 * Here we simply return. We can't afford to block kswapd on the
4415 * journal commit.
4416 *
4417 * In all cases it is actually safe for us to return without doing anything,
4418 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004419 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004420 * knfsd.
4421 *
4422 * Note that we are absolutely dependent upon all inode dirtiers doing the
4423 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4424 * which we are interested.
4425 *
4426 * It would be a bug for them to not do this. The code:
4427 *
4428 * mark_inode_dirty(inode)
4429 * stuff();
4430 * inode->i_size = expr;
4431 *
4432 * is in error because a kswapd-driven write_inode() could occur while
4433 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4434 * will no longer be on the superblock's dirty inode list.
4435 */
Mingming Cao617ba132006-10-11 01:20:53 -07004436int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004437{
4438 if (current->flags & PF_MEMALLOC)
4439 return 0;
4440
Mingming Cao617ba132006-10-11 01:20:53 -07004441 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004442 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004443 dump_stack();
4444 return -EIO;
4445 }
4446
4447 if (!wait)
4448 return 0;
4449
Mingming Cao617ba132006-10-11 01:20:53 -07004450 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004451}
4452
4453/*
Mingming Cao617ba132006-10-11 01:20:53 -07004454 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004455 *
4456 * Called from notify_change.
4457 *
4458 * We want to trap VFS attempts to truncate the file as soon as
4459 * possible. In particular, we want to make sure that when the VFS
4460 * shrinks i_size, we put the inode on the orphan list and modify
4461 * i_disksize immediately, so that during the subsequent flushing of
4462 * dirty pages and freeing of disk blocks, we can guarantee that any
4463 * commit will leave the blocks being flushed in an unused state on
4464 * disk. (On recovery, the inode will get truncated and the blocks will
4465 * be freed, so we have a strong guarantee that no future commit will
4466 * leave these blocks visible to the user.)
4467 *
Jan Kara678aaf42008-07-11 19:27:31 -04004468 * Another thing we have to assure is that if we are in ordered mode
4469 * and inode is still attached to the committing transaction, we must
4470 * we start writeout of all the dirty pages which are being truncated.
4471 * This way we are sure that all the data written in the previous
4472 * transaction are already on disk (truncate waits for pages under
4473 * writeback).
4474 *
4475 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004476 */
Mingming Cao617ba132006-10-11 01:20:53 -07004477int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004478{
4479 struct inode *inode = dentry->d_inode;
4480 int error, rc = 0;
4481 const unsigned int ia_valid = attr->ia_valid;
4482
4483 error = inode_change_ok(inode, attr);
4484 if (error)
4485 return error;
4486
4487 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4488 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4489 handle_t *handle;
4490
4491 /* (user+group)*(old+new) structure, inode write (sb,
4492 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004493 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4494 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004495 if (IS_ERR(handle)) {
4496 error = PTR_ERR(handle);
4497 goto err_out;
4498 }
4499 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
4500 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004501 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004502 return error;
4503 }
4504 /* Update corresponding info in inode so that everything is in
4505 * one transaction */
4506 if (attr->ia_valid & ATTR_UID)
4507 inode->i_uid = attr->ia_uid;
4508 if (attr->ia_valid & ATTR_GID)
4509 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004510 error = ext4_mark_inode_dirty(handle, inode);
4511 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004512 }
4513
Eric Sandeene2b46572008-01-28 23:58:27 -05004514 if (attr->ia_valid & ATTR_SIZE) {
4515 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4516 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4517
4518 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4519 error = -EFBIG;
4520 goto err_out;
4521 }
4522 }
4523 }
4524
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004525 if (S_ISREG(inode->i_mode) &&
4526 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4527 handle_t *handle;
4528
Mingming Cao617ba132006-10-11 01:20:53 -07004529 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004530 if (IS_ERR(handle)) {
4531 error = PTR_ERR(handle);
4532 goto err_out;
4533 }
4534
Mingming Cao617ba132006-10-11 01:20:53 -07004535 error = ext4_orphan_add(handle, inode);
4536 EXT4_I(inode)->i_disksize = attr->ia_size;
4537 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004538 if (!error)
4539 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004540 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004541
4542 if (ext4_should_order_data(inode)) {
4543 error = ext4_begin_ordered_truncate(inode,
4544 attr->ia_size);
4545 if (error) {
4546 /* Do as much error cleanup as possible */
4547 handle = ext4_journal_start(inode, 3);
4548 if (IS_ERR(handle)) {
4549 ext4_orphan_del(NULL, inode);
4550 goto err_out;
4551 }
4552 ext4_orphan_del(handle, inode);
4553 ext4_journal_stop(handle);
4554 goto err_out;
4555 }
4556 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004557 }
4558
4559 rc = inode_setattr(inode, attr);
4560
Mingming Cao617ba132006-10-11 01:20:53 -07004561 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004562 * transaction handle at all, we need to clean up the in-core
4563 * orphan list manually. */
4564 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004565 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004566
4567 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004568 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004569
4570err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004571 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004572 if (!error)
4573 error = rc;
4574 return error;
4575}
4576
Mingming Cao3e3398a2008-07-11 19:27:31 -04004577int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4578 struct kstat *stat)
4579{
4580 struct inode *inode;
4581 unsigned long delalloc_blocks;
4582
4583 inode = dentry->d_inode;
4584 generic_fillattr(inode, stat);
4585
4586 /*
4587 * We can't update i_blocks if the block allocation is delayed
4588 * otherwise in the case of system crash before the real block
4589 * allocation is done, we will have i_blocks inconsistent with
4590 * on-disk file blocks.
4591 * We always keep i_blocks updated together with real
4592 * allocation. But to not confuse with user, stat
4593 * will return the blocks that include the delayed allocation
4594 * blocks for this file.
4595 */
4596 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4597 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4598 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4599
4600 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4601 return 0;
4602}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004603
Mingming Caoa02908f2008-08-19 22:16:07 -04004604static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4605 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004606{
Mingming Caoa02908f2008-08-19 22:16:07 -04004607 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004608
Mingming Caoa02908f2008-08-19 22:16:07 -04004609 /* if nrblocks are contiguous */
4610 if (chunk) {
4611 /*
4612 * With N contiguous data blocks, it need at most
4613 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4614 * 2 dindirect blocks
4615 * 1 tindirect block
4616 */
4617 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4618 return indirects + 3;
4619 }
4620 /*
4621 * if nrblocks are not contiguous, worse case, each block touch
4622 * a indirect block, and each indirect block touch a double indirect
4623 * block, plus a triple indirect block
4624 */
4625 indirects = nrblocks * 2 + 1;
4626 return indirects;
4627}
Alex Tomasa86c6182006-10-11 01:21:03 -07004628
Mingming Caoa02908f2008-08-19 22:16:07 -04004629static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4630{
4631 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004632 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4633 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004634}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004635
Mingming Caoa02908f2008-08-19 22:16:07 -04004636/*
4637 * Account for index blocks, block groups bitmaps and block group
4638 * descriptor blocks if modify datablocks and index blocks
4639 * worse case, the indexs blocks spread over different block groups
4640 *
4641 * If datablocks are discontiguous, they are possible to spread over
4642 * different block groups too. If they are contiugous, with flexbg,
4643 * they could still across block group boundary.
4644 *
4645 * Also account for superblock, inode, quota and xattr blocks
4646 */
4647int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4648{
4649 int groups, gdpblocks;
4650 int idxblocks;
4651 int ret = 0;
4652
4653 /*
4654 * How many index blocks need to touch to modify nrblocks?
4655 * The "Chunk" flag indicating whether the nrblocks is
4656 * physically contiguous on disk
4657 *
4658 * For Direct IO and fallocate, they calls get_block to allocate
4659 * one single extent at a time, so they could set the "Chunk" flag
4660 */
4661 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4662
4663 ret = idxblocks;
4664
4665 /*
4666 * Now let's see how many group bitmaps and group descriptors need
4667 * to account
4668 */
4669 groups = idxblocks;
4670 if (chunk)
4671 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004672 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004673 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004674
Mingming Caoa02908f2008-08-19 22:16:07 -04004675 gdpblocks = groups;
4676 if (groups > EXT4_SB(inode->i_sb)->s_groups_count)
4677 groups = EXT4_SB(inode->i_sb)->s_groups_count;
4678 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4679 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4680
4681 /* bitmaps and block group descriptor blocks */
4682 ret += groups + gdpblocks;
4683
4684 /* Blocks for super block, inode, quota and xattr blocks */
4685 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004686
4687 return ret;
4688}
4689
4690/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004691 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004692 * the modification of a single pages into a single transaction,
4693 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004694 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004695 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004696 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004697 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004698 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004699 */
4700int ext4_writepage_trans_blocks(struct inode *inode)
4701{
4702 int bpp = ext4_journal_blocks_per_page(inode);
4703 int ret;
4704
4705 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4706
4707 /* Account for data blocks for journalled mode */
4708 if (ext4_should_journal_data(inode))
4709 ret += bpp;
4710 return ret;
4711}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004712
4713/*
4714 * Calculate the journal credits for a chunk of data modification.
4715 *
4716 * This is called from DIO, fallocate or whoever calling
4717 * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks.
4718 *
4719 * journal buffers for data blocks are not included here, as DIO
4720 * and fallocate do no need to journal data buffers.
4721 */
4722int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4723{
4724 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4725}
4726
Mingming Caoa02908f2008-08-19 22:16:07 -04004727/*
Mingming Cao617ba132006-10-11 01:20:53 -07004728 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004729 * Give this, we know that the caller already has write access to iloc->bh.
4730 */
Mingming Cao617ba132006-10-11 01:20:53 -07004731int ext4_mark_iloc_dirty(handle_t *handle,
4732 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004733{
4734 int err = 0;
4735
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004736 if (test_opt(inode->i_sb, I_VERSION))
4737 inode_inc_iversion(inode);
4738
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004739 /* the do_update_inode consumes one bh->b_count */
4740 get_bh(iloc->bh);
4741
Mingming Caodab291a2006-10-11 01:21:01 -07004742 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004743 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004744 put_bh(iloc->bh);
4745 return err;
4746}
4747
4748/*
4749 * On success, We end up with an outstanding reference count against
4750 * iloc->bh. This _must_ be cleaned up later.
4751 */
4752
4753int
Mingming Cao617ba132006-10-11 01:20:53 -07004754ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4755 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004756{
4757 int err = 0;
4758 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07004759 err = ext4_get_inode_loc(inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004760 if (!err) {
4761 BUFFER_TRACE(iloc->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004762 err = ext4_journal_get_write_access(handle, iloc->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004763 if (err) {
4764 brelse(iloc->bh);
4765 iloc->bh = NULL;
4766 }
4767 }
4768 }
Mingming Cao617ba132006-10-11 01:20:53 -07004769 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004770 return err;
4771}
4772
4773/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004774 * Expand an inode by new_extra_isize bytes.
4775 * Returns 0 on success or negative error number on failure.
4776 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004777static int ext4_expand_extra_isize(struct inode *inode,
4778 unsigned int new_extra_isize,
4779 struct ext4_iloc iloc,
4780 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004781{
4782 struct ext4_inode *raw_inode;
4783 struct ext4_xattr_ibody_header *header;
4784 struct ext4_xattr_entry *entry;
4785
4786 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4787 return 0;
4788
4789 raw_inode = ext4_raw_inode(&iloc);
4790
4791 header = IHDR(inode, raw_inode);
4792 entry = IFIRST(header);
4793
4794 /* No extended attributes present */
4795 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
4796 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
4797 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4798 new_extra_isize);
4799 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4800 return 0;
4801 }
4802
4803 /* try to expand with EAs present */
4804 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4805 raw_inode, handle);
4806}
4807
4808/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004809 * What we do here is to mark the in-core inode as clean with respect to inode
4810 * dirtiness (it may still be data-dirty).
4811 * This means that the in-core inode may be reaped by prune_icache
4812 * without having to perform any I/O. This is a very good thing,
4813 * because *any* task may call prune_icache - even ones which
4814 * have a transaction open against a different journal.
4815 *
4816 * Is this cheating? Not really. Sure, we haven't written the
4817 * inode out, but prune_icache isn't a user-visible syncing function.
4818 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4819 * we start and wait on commits.
4820 *
4821 * Is this efficient/effective? Well, we're being nice to the system
4822 * by cleaning up our inodes proactively so they can be reaped
4823 * without I/O. But we are potentially leaving up to five seconds'
4824 * worth of inodes floating about which prune_icache wants us to
4825 * write out. One way to fix that would be to get prune_icache()
4826 * to do a write_super() to free up some memory. It has the desired
4827 * effect.
4828 */
Mingming Cao617ba132006-10-11 01:20:53 -07004829int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004830{
Mingming Cao617ba132006-10-11 01:20:53 -07004831 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004832 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4833 static unsigned int mnt_count;
4834 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004835
4836 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07004837 err = ext4_reserve_inode_write(handle, inode, &iloc);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004838 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
4839 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
4840 /*
4841 * We need extra buffer credits since we may write into EA block
4842 * with this same handle. If journal_extend fails, then it will
4843 * only result in a minor loss of functionality for that inode.
4844 * If this is felt to be critical, then e2fsck should be run to
4845 * force a large enough s_min_extra_isize.
4846 */
4847 if ((jbd2_journal_extend(handle,
4848 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
4849 ret = ext4_expand_extra_isize(inode,
4850 sbi->s_want_extra_isize,
4851 iloc, handle);
4852 if (ret) {
4853 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004854 if (mnt_count !=
4855 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004856 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004857 "Unable to expand inode %lu. Delete"
4858 " some EAs or run e2fsck.",
4859 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004860 mnt_count =
4861 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004862 }
4863 }
4864 }
4865 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004866 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004867 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004868 return err;
4869}
4870
4871/*
Mingming Cao617ba132006-10-11 01:20:53 -07004872 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004873 *
4874 * We're really interested in the case where a file is being extended.
4875 * i_size has been changed by generic_commit_write() and we thus need
4876 * to include the updated inode in the current transaction.
4877 *
4878 * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
4879 * are allocated to the file.
4880 *
4881 * If the inode is marked synchronous, we don't honour that here - doing
4882 * so would cause a commit on atime updates, which we don't bother doing.
4883 * We handle synchronous inodes at the highest possible level.
4884 */
Mingming Cao617ba132006-10-11 01:20:53 -07004885void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004886{
Mingming Cao617ba132006-10-11 01:20:53 -07004887 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004888 handle_t *handle;
4889
Mingming Cao617ba132006-10-11 01:20:53 -07004890 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004891 if (IS_ERR(handle))
4892 goto out;
4893 if (current_handle &&
4894 current_handle->h_transaction != handle->h_transaction) {
4895 /* This task has a transaction open against a different fs */
4896 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04004897 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004898 } else {
4899 jbd_debug(5, "marking dirty. outer handle=%p\n",
4900 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07004901 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004902 }
Mingming Cao617ba132006-10-11 01:20:53 -07004903 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004904out:
4905 return;
4906}
4907
4908#if 0
4909/*
4910 * Bind an inode's backing buffer_head into this transaction, to prevent
4911 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07004912 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004913 * returns no iloc structure, so the caller needs to repeat the iloc
4914 * lookup to mark the inode dirty later.
4915 */
Mingming Cao617ba132006-10-11 01:20:53 -07004916static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004917{
Mingming Cao617ba132006-10-11 01:20:53 -07004918 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004919
4920 int err = 0;
4921 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07004922 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004923 if (!err) {
4924 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07004925 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004926 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004927 err = ext4_journal_dirty_metadata(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004928 iloc.bh);
4929 brelse(iloc.bh);
4930 }
4931 }
Mingming Cao617ba132006-10-11 01:20:53 -07004932 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004933 return err;
4934}
4935#endif
4936
Mingming Cao617ba132006-10-11 01:20:53 -07004937int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004938{
4939 journal_t *journal;
4940 handle_t *handle;
4941 int err;
4942
4943 /*
4944 * We have to be very careful here: changing a data block's
4945 * journaling status dynamically is dangerous. If we write a
4946 * data block to the journal, change the status and then delete
4947 * that block, we risk forgetting to revoke the old log record
4948 * from the journal and so a subsequent replay can corrupt data.
4949 * So, first we make sure that the journal is empty and that
4950 * nobody is changing anything.
4951 */
4952
Mingming Cao617ba132006-10-11 01:20:53 -07004953 journal = EXT4_JOURNAL(inode);
Dave Hansend6995942007-07-18 08:33:51 -04004954 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004955 return -EROFS;
4956
Mingming Caodab291a2006-10-11 01:21:01 -07004957 jbd2_journal_lock_updates(journal);
4958 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004959
4960 /*
4961 * OK, there are no updates running now, and all cached data is
4962 * synced to disk. We are now in a completely consistent state
4963 * which doesn't have anything in the journal, and we know that
4964 * no filesystem updates are running, so it is safe to modify
4965 * the inode's in-core data-journaling state flag now.
4966 */
4967
4968 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07004969 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004970 else
Mingming Cao617ba132006-10-11 01:20:53 -07004971 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
4972 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004973
Mingming Caodab291a2006-10-11 01:21:01 -07004974 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004975
4976 /* Finally we can mark the inode as dirty. */
4977
Mingming Cao617ba132006-10-11 01:20:53 -07004978 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004979 if (IS_ERR(handle))
4980 return PTR_ERR(handle);
4981
Mingming Cao617ba132006-10-11 01:20:53 -07004982 err = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004983 handle->h_sync = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07004984 ext4_journal_stop(handle);
4985 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004986
4987 return err;
4988}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004989
4990static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
4991{
4992 return !buffer_mapped(bh);
4993}
4994
4995int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4996{
4997 loff_t size;
4998 unsigned long len;
4999 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005000 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005001 struct file *file = vma->vm_file;
5002 struct inode *inode = file->f_path.dentry->d_inode;
5003 struct address_space *mapping = inode->i_mapping;
5004
5005 /*
5006 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5007 * get i_mutex because we are already holding mmap_sem.
5008 */
5009 down_read(&inode->i_alloc_sem);
5010 size = i_size_read(inode);
5011 if (page->mapping != mapping || size <= page_offset(page)
5012 || !PageUptodate(page)) {
5013 /* page got truncated from under us? */
5014 goto out_unlock;
5015 }
5016 ret = 0;
5017 if (PageMappedToDisk(page))
5018 goto out_unlock;
5019
5020 if (page->index == size >> PAGE_CACHE_SHIFT)
5021 len = size & ~PAGE_CACHE_MASK;
5022 else
5023 len = PAGE_CACHE_SIZE;
5024
5025 if (page_has_buffers(page)) {
5026 /* return if we have all the buffers mapped */
5027 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5028 ext4_bh_unmapped))
5029 goto out_unlock;
5030 }
5031 /*
5032 * OK, we need to fill the hole... Do write_begin write_end
5033 * to do block allocation/reservation.We are not holding
5034 * inode.i__mutex here. That allow * parallel write_begin,
5035 * write_end call. lock_page prevent this from happening
5036 * on the same page though
5037 */
5038 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005039 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005040 if (ret < 0)
5041 goto out_unlock;
5042 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005043 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005044 if (ret < 0)
5045 goto out_unlock;
5046 ret = 0;
5047out_unlock:
5048 up_read(&inode->i_alloc_sem);
5049 return ret;
5050}