blob: 49484ba801c930c5100957b76cf01b012f0ba31b [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.
Frank Mayhar03901312009-01-07 00:06:22 -050075 *
76 * If the handle isn't valid we're not journaling so there's nothing to do.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070077 */
Mingming Cao617ba132006-10-11 01:20:53 -070078int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
79 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070080{
81 int err;
82
Frank Mayhar03901312009-01-07 00:06:22 -050083 if (!ext4_handle_valid(handle))
84 return 0;
85
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086 might_sleep();
87
88 BUFFER_TRACE(bh, "enter");
89
90 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
91 "data mode %lx\n",
92 bh, is_metadata, inode->i_mode,
93 test_opt(inode->i_sb, DATA_FLAGS));
94
95 /* Never use the revoke function if we are doing full data
96 * journaling: there is no need to, and a V1 superblock won't
97 * support it. Otherwise, only skip the revoke on un-journaled
98 * data blocks. */
99
Mingming Cao617ba132006-10-11 01:20:53 -0700100 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
101 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -0700103 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700104 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 }
106 return 0;
107 }
108
109 /*
110 * data!=journal && (is_metadata || should_journal_data(inode))
111 */
Mingming Cao617ba132006-10-11 01:20:53 -0700112 BUFFER_TRACE(bh, "call ext4_journal_revoke");
113 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400115 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116 "error %d when attempting revoke", err);
117 BUFFER_TRACE(bh, "exit");
118 return err;
119}
120
121/*
122 * Work out how many blocks we need to proceed with the next chunk of a
123 * truncate transaction.
124 */
125static unsigned long blocks_for_truncate(struct inode *inode)
126{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500127 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700128
129 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
130
131 /* Give ourselves just enough room to cope with inodes in which
132 * i_blocks is corrupt: we've seen disk corruptions in the past
133 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700134 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700135 * will go a bit crazy if that happens, but at least we should
136 * try not to panic the whole kernel. */
137 if (needed < 2)
138 needed = 2;
139
140 /* But we need to bound the transaction so we don't overflow the
141 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700142 if (needed > EXT4_MAX_TRANS_DATA)
143 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700144
Mingming Cao617ba132006-10-11 01:20:53 -0700145 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146}
147
148/*
149 * Truncate transactions can be complex and absolutely huge. So we need to
150 * be able to restart the transaction at a conventient checkpoint to make
151 * sure we don't overflow the journal.
152 *
153 * start_transaction gets us a new handle for a truncate transaction,
154 * and extend_transaction tries to extend the existing one a bit. If
155 * extend fails, we need to propagate the failure up and restart the
156 * transaction in the top-level truncate loop. --sct
157 */
158static handle_t *start_transaction(struct inode *inode)
159{
160 handle_t *result;
161
Mingming Cao617ba132006-10-11 01:20:53 -0700162 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 if (!IS_ERR(result))
164 return result;
165
Mingming Cao617ba132006-10-11 01:20:53 -0700166 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167 return result;
168}
169
170/*
171 * Try to extend this transaction for the purposes of truncation.
172 *
173 * Returns 0 if we managed to create more room. If we can't create more
174 * room, and the transaction must be restarted we return 1.
175 */
176static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
177{
Frank Mayhar03901312009-01-07 00:06:22 -0500178 if (!ext4_handle_valid(handle))
179 return 0;
180 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700182 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 return 0;
184 return 1;
185}
186
187/*
188 * Restart the transaction associated with *handle. This does a commit,
189 * so before we call here everything must be consistently dirtied against
190 * this transaction.
191 */
Mingming Cao617ba132006-10-11 01:20:53 -0700192static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193{
Frank Mayhar03901312009-01-07 00:06:22 -0500194 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700196 return ext4_journal_restart(handle, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197}
198
199/*
200 * Called at the last iput() if i_nlink is zero.
201 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400202void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203{
204 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400205 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206
Jan Kara678aaf42008-07-11 19:27:31 -0400207 if (ext4_should_order_data(inode))
208 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209 truncate_inode_pages(&inode->i_data, 0);
210
211 if (is_bad_inode(inode))
212 goto no_delete;
213
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400214 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400216 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217 /*
218 * If we're going to skip the normal cleanup, we still need to
219 * make sure that the in-core orphan linked list is properly
220 * cleaned up.
221 */
Mingming Cao617ba132006-10-11 01:20:53 -0700222 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 goto no_delete;
224 }
225
226 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500227 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400229 err = ext4_mark_inode_dirty(handle, inode);
230 if (err) {
231 ext4_warning(inode->i_sb, __func__,
232 "couldn't mark inode dirty (err %d)", err);
233 goto stop_handle;
234 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700236 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400237
238 /*
239 * ext4_ext_truncate() doesn't reserve any slop when it
240 * restarts journal transactions; therefore there may not be
241 * enough credits left in the handle to remove the inode from
242 * the orphan list and set the dtime field.
243 */
Frank Mayhar03901312009-01-07 00:06:22 -0500244 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400245 err = ext4_journal_extend(handle, 3);
246 if (err > 0)
247 err = ext4_journal_restart(handle, 3);
248 if (err != 0) {
249 ext4_warning(inode->i_sb, __func__,
250 "couldn't extend journal (err %d)", err);
251 stop_handle:
252 ext4_journal_stop(handle);
253 goto no_delete;
254 }
255 }
256
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700258 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700260 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700262 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 * (Well, we could do this if we need to, but heck - it works)
264 */
Mingming Cao617ba132006-10-11 01:20:53 -0700265 ext4_orphan_del(handle, inode);
266 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267
268 /*
269 * One subtle ordering requirement: if anything has gone wrong
270 * (transaction abort, IO errors, whatever), then we can still
271 * do these next steps (the fs will already have been marked as
272 * having errors), but we can't free the inode if the mark_dirty
273 * fails.
274 */
Mingming Cao617ba132006-10-11 01:20:53 -0700275 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 /* If that failed, just do the required in-core inode clear. */
277 clear_inode(inode);
278 else
Mingming Cao617ba132006-10-11 01:20:53 -0700279 ext4_free_inode(handle, inode);
280 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 return;
282no_delete:
283 clear_inode(inode); /* We must guarantee clearing of inode... */
284}
285
286typedef struct {
287 __le32 *p;
288 __le32 key;
289 struct buffer_head *bh;
290} Indirect;
291
292static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
293{
294 p->key = *(p->p = v);
295 p->bh = bh;
296}
297
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298/**
Mingming Cao617ba132006-10-11 01:20:53 -0700299 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 * @inode: inode in question (we are only interested in its superblock)
301 * @i_block: block number to be parsed
302 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400303 * @boundary: set this non-zero if the referred-to block is likely to be
304 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 *
Mingming Cao617ba132006-10-11 01:20:53 -0700306 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307 * for UNIX filesystems - tree of pointers anchored in the inode, with
308 * data blocks at leaves and indirect blocks in intermediate nodes.
309 * This function translates the block number into path in that tree -
310 * return value is the path length and @offsets[n] is the offset of
311 * pointer to (n+1)th node in the nth one. If @block is out of range
312 * (negative or too large) warning is printed and zero returned.
313 *
314 * Note: function doesn't find node addresses, so no IO is needed. All
315 * we need to know is the capacity of indirect blocks (taken from the
316 * inode->i_sb).
317 */
318
319/*
320 * Portability note: the last comparison (check that we fit into triple
321 * indirect block) is spelled differently, because otherwise on an
322 * architecture with 32-bit longs and 8Kb pages we might get into trouble
323 * if our filesystem had 8Kb blocks. We might use long long, but that would
324 * kill us on x86. Oh, well, at least the sign propagation does not matter -
325 * i_block would have to be negative in the very beginning, so we would not
326 * get there at all.
327 */
328
Mingming Cao617ba132006-10-11 01:20:53 -0700329static int ext4_block_to_path(struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500330 ext4_lblk_t i_block,
331 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332{
Mingming Cao617ba132006-10-11 01:20:53 -0700333 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
334 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
335 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 indirect_blocks = ptrs,
337 double_blocks = (1 << (ptrs_bits * 2));
338 int n = 0;
339 int final = 0;
340
341 if (i_block < 0) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400342 ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 } else if (i_block < direct_blocks) {
344 offsets[n++] = i_block;
345 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400346 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700347 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348 offsets[n++] = i_block;
349 final = ptrs;
350 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700351 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 offsets[n++] = i_block >> ptrs_bits;
353 offsets[n++] = i_block & (ptrs - 1);
354 final = ptrs;
355 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700356 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357 offsets[n++] = i_block >> (ptrs_bits * 2);
358 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
359 offsets[n++] = i_block & (ptrs - 1);
360 final = ptrs;
361 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500362 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'o06a279d2009-01-17 18:41:37 -0500363 "block %lu > max in inode %lu",
Eric Sandeene2b46572008-01-28 23:58:27 -0500364 i_block + direct_blocks +
Theodore Ts'o06a279d2009-01-17 18:41:37 -0500365 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 }
367 if (boundary)
368 *boundary = final - 1 - (i_block & (ptrs - 1));
369 return n;
370}
371
372/**
Mingming Cao617ba132006-10-11 01:20:53 -0700373 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700374 * @inode: inode in question
375 * @depth: depth of the chain (1 - direct pointer, etc.)
376 * @offsets: offsets of pointers in inode/indirect blocks
377 * @chain: place to store the result
378 * @err: here we store the error value
379 *
380 * Function fills the array of triples <key, p, bh> and returns %NULL
381 * if everything went OK or the pointer to the last filled triple
382 * (incomplete one) otherwise. Upon the return chain[i].key contains
383 * the number of (i+1)-th block in the chain (as it is stored in memory,
384 * i.e. little-endian 32-bit), chain[i].p contains the address of that
385 * number (it points into struct inode for i==0 and into the bh->b_data
386 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
387 * block for i>0 and NULL for i==0. In other words, it holds the block
388 * numbers of the chain, addresses they were taken from (and where we can
389 * verify that chain did not change) and buffer_heads hosting these
390 * numbers.
391 *
392 * Function stops when it stumbles upon zero pointer (absent block)
393 * (pointer to last triple returned, *@err == 0)
394 * or when it gets an IO error reading an indirect block
395 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 * or when it reads all @depth-1 indirect blocks successfully and finds
397 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500398 *
399 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500400 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500402static Indirect *ext4_get_branch(struct inode *inode, int depth,
403 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404 Indirect chain[4], int *err)
405{
406 struct super_block *sb = inode->i_sb;
407 Indirect *p = chain;
408 struct buffer_head *bh;
409
410 *err = 0;
411 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400412 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413 if (!p->key)
414 goto no_block;
415 while (--depth) {
416 bh = sb_bread(sb, le32_to_cpu(p->key));
417 if (!bh)
418 goto failure;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400419 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420 /* Reader: end */
421 if (!p->key)
422 goto no_block;
423 }
424 return NULL;
425
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700426failure:
427 *err = -EIO;
428no_block:
429 return p;
430}
431
432/**
Mingming Cao617ba132006-10-11 01:20:53 -0700433 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 * @inode: owner
435 * @ind: descriptor of indirect block.
436 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000437 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438 * It is used when heuristic for sequential allocation fails.
439 * Rules are:
440 * + if there is a block to the left of our position - allocate near it.
441 * + if pointer will live in indirect block - allocate near that block.
442 * + if pointer will live in inode - allocate in the same
443 * cylinder group.
444 *
445 * In the latter case we colour the starting block by the callers PID to
446 * prevent it from clashing with concurrent allocations for a different inode
447 * in the same block group. The PID is used here so that functionally related
448 * files will be close-by on-disk.
449 *
450 * Caller must make sure that @ind is valid and will stay that way.
451 */
Mingming Cao617ba132006-10-11 01:20:53 -0700452static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453{
Mingming Cao617ba132006-10-11 01:20:53 -0700454 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400455 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700457 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500458 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700459 ext4_grpblk_t colour;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460
461 /* Try to find previous block */
462 for (p = ind->p - 1; p >= start; p--) {
463 if (*p)
464 return le32_to_cpu(*p);
465 }
466
467 /* No such thing, so let's try location of indirect block */
468 if (ind->bh)
469 return ind->bh->b_blocknr;
470
471 /*
472 * It is going to be referred to from the inode itself? OK, just put it
473 * into the same cylinder group then.
474 */
Mingming Cao617ba132006-10-11 01:20:53 -0700475 bg_start = ext4_group_first_block_no(inode->i_sb, ei->i_block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500476 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
477
478 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
479 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700480 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500481 else
482 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 return bg_start + colour;
484}
485
486/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000487 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 * @inode: owner
489 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000492 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800493 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500495static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800496 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400499 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501
Mingming Cao617ba132006-10-11 01:20:53 -0700502 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503}
504
505/**
Mingming Cao617ba132006-10-11 01:20:53 -0700506 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 * of direct blocks need to be allocated for the given branch.
508 *
509 * @branch: chain of indirect blocks
510 * @k: number of blocks need for indirect blocks
511 * @blks: number of data blocks to be mapped.
512 * @blocks_to_boundary: the offset in the indirect block
513 *
514 * return the total number of blocks to be allocate, including the
515 * direct and indirect blocks.
516 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500517static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 int blocks_to_boundary)
519{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500520 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521
522 /*
523 * Simple case, [t,d]Indirect block(s) has not allocated yet
524 * then it's clear blocks on that path have not allocated
525 */
526 if (k > 0) {
527 /* right now we don't handle cross boundary allocation */
528 if (blks < blocks_to_boundary + 1)
529 count += blks;
530 else
531 count += blocks_to_boundary + 1;
532 return count;
533 }
534
535 count++;
536 while (count < blks && count <= blocks_to_boundary &&
537 le32_to_cpu(*(branch[0].p + count)) == 0) {
538 count++;
539 }
540 return count;
541}
542
543/**
Mingming Cao617ba132006-10-11 01:20:53 -0700544 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 * @indirect_blks: the number of blocks need to allocate for indirect
546 * blocks
547 *
548 * @new_blocks: on return it will store the new block numbers for
549 * the indirect blocks(if needed) and the first direct block,
550 * @blks: on return it will store the total number of allocated
551 * direct blocks
552 */
Mingming Cao617ba132006-10-11 01:20:53 -0700553static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400554 ext4_lblk_t iblock, ext4_fsblk_t goal,
555 int indirect_blks, int blks,
556 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500558 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400560 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700562 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 int ret = 0;
564
565 /*
566 * Here we try to allocate the requested multiple blocks at once,
567 * on a best-effort basis.
568 * To build a branch, we should allocate blocks for
569 * the indirect blocks(if not allocated yet), and at least
570 * the first direct block of this branch. That's the
571 * minimum number of blocks need to allocate(required)
572 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400573 /* first we try to allocate the indirect blocks */
574 target = indirect_blks;
575 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 count = target;
577 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400578 current_block = ext4_new_meta_blocks(handle, inode,
579 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580 if (*err)
581 goto failed_out;
582
583 target -= count;
584 /* allocate blocks for indirect blocks */
585 while (index < indirect_blks && count) {
586 new_blocks[index++] = current_block++;
587 count--;
588 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400589 if (count > 0) {
590 /*
591 * save the new block number
592 * for the first direct block
593 */
594 new_blocks[index] = current_block;
595 printk(KERN_INFO "%s returned more blocks than "
596 "requested\n", __func__);
597 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700598 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400599 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 }
601
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400602 target = blks - count ;
603 blk_allocated = count;
604 if (!target)
605 goto allocated;
606 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500607 memset(&ar, 0, sizeof(ar));
608 ar.inode = inode;
609 ar.goal = goal;
610 ar.len = target;
611 ar.logical = iblock;
612 if (S_ISREG(inode->i_mode))
613 /* enable in-core preallocation only for regular files */
614 ar.flags = EXT4_MB_HINT_DATA;
615
616 current_block = ext4_mb_new_blocks(handle, &ar, err);
617
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400618 if (*err && (target == blks)) {
619 /*
620 * if the allocation failed and we didn't allocate
621 * any blocks before
622 */
623 goto failed_out;
624 }
625 if (!*err) {
626 if (target == blks) {
627 /*
628 * save the new block number
629 * for the first direct block
630 */
631 new_blocks[index] = current_block;
632 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500633 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400634 }
635allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400637 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 *err = 0;
639 return ret;
640failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400641 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500642 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 return ret;
644}
645
646/**
Mingming Cao617ba132006-10-11 01:20:53 -0700647 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700648 * @inode: owner
649 * @indirect_blks: number of allocated indirect blocks
650 * @blks: number of allocated direct blocks
651 * @offsets: offsets (in the blocks) to store the pointers to next.
652 * @branch: place to store the chain in.
653 *
654 * This function allocates blocks, zeroes out all but the last one,
655 * links them into chain and (if we are synchronous) writes them to disk.
656 * In other words, it prepares a branch that can be spliced onto the
657 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700658 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 * we had read the existing part of chain and partial points to the last
660 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700661 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662 * place chain is disconnected - *branch->p is still zero (we did not
663 * set the last link), but branch->key contains the number that should
664 * be placed into *branch->p to fill that gap.
665 *
666 * If allocation fails we free all blocks we've allocated (and forget
667 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700668 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 * as described above and return 0.
670 */
Mingming Cao617ba132006-10-11 01:20:53 -0700671static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400672 ext4_lblk_t iblock, int indirect_blks,
673 int *blks, ext4_fsblk_t goal,
674 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675{
676 int blocksize = inode->i_sb->s_blocksize;
677 int i, n = 0;
678 int err = 0;
679 struct buffer_head *bh;
680 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700681 ext4_fsblk_t new_blocks[4];
682 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400684 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 *blks, new_blocks, &err);
686 if (err)
687 return err;
688
689 branch[0].key = cpu_to_le32(new_blocks[0]);
690 /*
691 * metadata blocks and data blocks are allocated.
692 */
693 for (n = 1; n <= indirect_blks; n++) {
694 /*
695 * Get buffer_head for parent block, zero it out
696 * and set the pointer to new one, then send
697 * parent to disk.
698 */
699 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
700 branch[n].bh = bh;
701 lock_buffer(bh);
702 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700703 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 if (err) {
705 unlock_buffer(bh);
706 brelse(bh);
707 goto failed;
708 }
709
710 memset(bh->b_data, 0, blocksize);
711 branch[n].p = (__le32 *) bh->b_data + offsets[n];
712 branch[n].key = cpu_to_le32(new_blocks[n]);
713 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400714 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 current_block = new_blocks[n];
716 /*
717 * End of chain, update the last new metablock of
718 * the chain to point to the new allocated
719 * data blocks numbers
720 */
721 for (i=1; i < num; i++)
722 *(branch[n].p + i) = cpu_to_le32(++current_block);
723 }
724 BUFFER_TRACE(bh, "marking uptodate");
725 set_buffer_uptodate(bh);
726 unlock_buffer(bh);
727
Frank Mayhar03901312009-01-07 00:06:22 -0500728 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
729 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 if (err)
731 goto failed;
732 }
733 *blks = num;
734 return err;
735failed:
736 /* Allocation failed, free what we already allocated */
737 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700738 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700739 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400741 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500742 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743
Alex Tomasc9de5602008-01-29 00:19:52 -0500744 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745
746 return err;
747}
748
749/**
Mingming Cao617ba132006-10-11 01:20:53 -0700750 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 * @inode: owner
752 * @block: (logical) number of block we are adding
753 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700754 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755 * @where: location of missing link
756 * @num: number of indirect blocks we are adding
757 * @blks: number of direct blocks we are adding
758 *
759 * This function fills the missing link and does all housekeeping needed in
760 * inode (->i_blocks, etc.). In case of success we end up with the full
761 * chain to new block and return 0.
762 */
Mingming Cao617ba132006-10-11 01:20:53 -0700763static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500764 ext4_lblk_t block, Indirect *where, int num, int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765{
766 int i;
767 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700768 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 /*
771 * If we're splicing into a [td]indirect block (as opposed to the
772 * inode) then we need to get write access to the [td]indirect block
773 * before the splice.
774 */
775 if (where->bh) {
776 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700777 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778 if (err)
779 goto err_out;
780 }
781 /* That's it */
782
783 *where->p = where->key;
784
785 /*
786 * Update the host buffer_head or inode to point to more just allocated
787 * direct blocks blocks
788 */
789 if (num == 0 && blks > 1) {
790 current_block = le32_to_cpu(where->key) + 1;
791 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400792 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 }
794
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 /* We are done with atomic stuff, now do the rest of housekeeping */
796
Kalpak Shahef7f3832007-07-18 09:15:20 -0400797 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700798 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799
800 /* had we spliced it onto indirect block? */
801 if (where->bh) {
802 /*
803 * If we spliced it onto an indirect block, we haven't
804 * altered the inode. Note however that if it is being spliced
805 * onto an indirect block at the very end of the file (the
806 * file is growing) then we *will* alter the inode to reflect
807 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700808 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 */
810 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500811 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
812 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 if (err)
814 goto err_out;
815 } else {
816 /*
817 * OK, we spliced it into the inode itself on a direct block.
818 * Inode was dirtied above.
819 */
820 jbd_debug(5, "splicing direct\n");
821 }
822 return err;
823
824err_out:
825 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700826 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700827 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500828 ext4_free_blocks(handle, inode,
829 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500831 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832
833 return err;
834}
835
836/*
837 * Allocation strategy is simple: if we have to allocate something, we will
838 * have to go the whole way to leaf. So let's do it before attaching anything
839 * to tree, set linkage between the newborn blocks, write them if sync is
840 * required, recheck the path, free and repeat if check fails, otherwise
841 * set the last missing link (that will protect us from any truncate-generated
842 * removals - all blocks on the path are immune now) and possibly force the
843 * write on the parent block.
844 * That has a nice additional property: no special recovery from the failed
845 * allocations is needed - we simply release blocks and do not touch anything
846 * reachable from inode.
847 *
848 * `handle' can be NULL if create == 0.
849 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 * return > 0, # of blocks mapped or allocated.
851 * return = 0, if plain lookup failed.
852 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500853 *
854 *
855 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500856 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
857 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500859static int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
860 ext4_lblk_t iblock, unsigned int maxblocks,
861 struct buffer_head *bh_result,
862 int create, int extend_disksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863{
864 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500865 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866 Indirect chain[4];
867 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700868 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869 int indirect_blks;
870 int blocks_to_boundary = 0;
871 int depth;
Mingming Cao617ba132006-10-11 01:20:53 -0700872 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700874 ext4_fsblk_t first_block = 0;
Mingming Cao61628a32008-07-11 19:27:31 -0400875 loff_t disksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876
877
Alex Tomasa86c6182006-10-11 01:21:03 -0700878 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 J_ASSERT(handle != NULL || create == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500880 depth = ext4_block_to_path(inode, iblock, offsets,
881 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882
883 if (depth == 0)
884 goto out;
885
Mingming Cao617ba132006-10-11 01:20:53 -0700886 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887
888 /* Simplest case - block found, no allocation needed */
889 if (!partial) {
890 first_block = le32_to_cpu(chain[depth - 1].key);
891 clear_buffer_new(bh_result);
892 count++;
893 /*map more blocks*/
894 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700895 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 blk = le32_to_cpu(*(chain[depth-1].p + count));
898
899 if (blk == first_block + count)
900 count++;
901 else
902 break;
903 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500904 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 }
906
907 /* Next simple case - plain lookup or failed read of indirect block */
908 if (!create || err == -EIO)
909 goto cleanup;
910
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400912 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800914 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915
916 /* the number of blocks need to allocate for [d,t]indirect blocks */
917 indirect_blks = (chain + depth) - partial - 1;
918
919 /*
920 * Next look up the indirect map to count the totoal number of
921 * direct blocks to allocate for this branch.
922 */
Mingming Cao617ba132006-10-11 01:20:53 -0700923 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924 maxblocks, blocks_to_boundary);
925 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700926 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400928 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
929 &count, goal,
930 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931
932 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700933 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 * on the new chain if there is a failure, but that risks using
935 * up transaction credits, especially for bitmaps where the
936 * credits cannot be returned. Can we handle this somehow? We
937 * may need to return -EAGAIN upwards in the worst case. --sct
938 */
939 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -0700940 err = ext4_splice_branch(handle, inode, iblock,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941 partial, indirect_blks, count);
942 /*
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500943 * i_disksize growing is protected by i_data_sem. Don't forget to
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944 * protect it if you're about to implement concurrent
Mingming Cao617ba132006-10-11 01:20:53 -0700945 * ext4_get_block() -bzzz
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 */
Mingming Cao61628a32008-07-11 19:27:31 -0400947 if (!err && extend_disksize) {
948 disksize = ((loff_t) iblock + count) << inode->i_blkbits;
949 if (disksize > i_size_read(inode))
950 disksize = i_size_read(inode);
951 if (disksize > ei->i_disksize)
952 ei->i_disksize = disksize;
953 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 if (err)
955 goto cleanup;
956
957 set_buffer_new(bh_result);
958got_it:
959 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
960 if (count > blocks_to_boundary)
961 set_buffer_boundary(bh_result);
962 err = count;
963 /* Clean up and exit */
964 partial = chain + depth - 1; /* the whole chain */
965cleanup:
966 while (partial > chain) {
967 BUFFER_TRACE(partial->bh, "call brelse");
968 brelse(partial->bh);
969 partial--;
970 }
971 BUFFER_TRACE(bh_result, "returned");
972out:
973 return err;
974}
975
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400976/*
977 * Calculate the number of metadata blocks need to reserve
978 * to allocate @blocks for non extent file based file
979 */
980static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
981{
982 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
983 int ind_blks, dind_blks, tind_blks;
984
985 /* number of new indirect blocks needed */
986 ind_blks = (blocks + icap - 1) / icap;
987
988 dind_blks = (ind_blks + icap - 1) / icap;
989
990 tind_blks = 1;
991
992 return ind_blks + dind_blks + tind_blks;
993}
994
995/*
996 * Calculate the number of metadata blocks need to reserve
997 * to allocate given number of blocks
998 */
999static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1000{
Mingming Caocd213222008-08-19 22:16:59 -04001001 if (!blocks)
1002 return 0;
1003
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001004 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1005 return ext4_ext_calc_metadata_amount(inode, blocks);
1006
1007 return ext4_indirect_calc_metadata_amount(inode, blocks);
1008}
1009
1010static void ext4_da_update_reserve_space(struct inode *inode, int used)
1011{
1012 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1013 int total, mdb, mdb_free;
1014
1015 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1016 /* recalculate the number of metablocks still need to be reserved */
1017 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1018 mdb = ext4_calc_metadata_amount(inode, total);
1019
1020 /* figure out how many metablocks to release */
1021 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1022 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1023
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001024 if (mdb_free) {
1025 /* Account for allocated meta_blocks */
1026 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001027
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001028 /* update fs dirty blocks counter */
1029 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1030 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1031 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1032 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001033
1034 /* update per-inode reservations */
1035 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1036 EXT4_I(inode)->i_reserved_data_blocks -= used;
1037
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001038 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1039}
1040
Mingming Caof5ab0d12008-02-25 15:29:55 -05001041/*
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001042 * The ext4_get_blocks_wrap() function try to look up the requested blocks,
1043 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001044 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001045 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1046 * and store the allocated blocks in the result buffer head and mark it
1047 * mapped.
1048 *
1049 * If file type is extents based, it will call ext4_ext_get_blocks(),
1050 * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping
1051 * based files
1052 *
1053 * On success, it returns the number of blocks being mapped or allocate.
1054 * if create==0 and the blocks are pre-allocated and uninitialized block,
1055 * the result buffer head is unmapped. If the create ==1, it will make sure
1056 * the buffer head is mapped.
1057 *
1058 * It returns 0 if plain look up failed (blocks have not been allocated), in
1059 * that casem, buffer head is unmapped
1060 *
1061 * It returns the error in case of allocation failure.
1062 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001063int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001064 unsigned int max_blocks, struct buffer_head *bh,
Mingming Caod2a17632008-07-14 17:52:37 -04001065 int create, int extend_disksize, int flag)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001066{
1067 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001068
1069 clear_buffer_mapped(bh);
1070
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001071 /*
1072 * Try to see if we can get the block without requesting
1073 * for new file system block.
1074 */
1075 down_read((&EXT4_I(inode)->i_data_sem));
1076 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1077 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
1078 bh, 0, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001079 } else {
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001080 retval = ext4_get_blocks_handle(handle,
1081 inode, block, max_blocks, bh, 0, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001082 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001083 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001084
1085 /* If it is only a block(s) look up */
1086 if (!create)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001087 return retval;
1088
1089 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001090 * Returns if the blocks have already allocated
1091 *
1092 * Note that if blocks have been preallocated
1093 * ext4_ext_get_block() returns th create = 0
1094 * with buffer head unmapped.
1095 */
1096 if (retval > 0 && buffer_mapped(bh))
1097 return retval;
1098
1099 /*
1100 * New blocks allocate and/or writing to uninitialized extent
1101 * will possibly result in updating i_data, so we take
1102 * the write lock of i_data_sem, and call get_blocks()
1103 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001104 */
1105 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001106
1107 /*
1108 * if the caller is from delayed allocation writeout path
1109 * we have already reserved fs blocks for allocation
1110 * let the underlying get_block() function know to
1111 * avoid double accounting
1112 */
1113 if (flag)
1114 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001115 /*
1116 * We need to check for EXT4 here because migrate
1117 * could have changed the inode type in between
1118 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001119 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1120 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
1121 bh, create, extend_disksize);
1122 } else {
1123 retval = ext4_get_blocks_handle(handle, inode, block,
1124 max_blocks, bh, create, extend_disksize);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001125
1126 if (retval > 0 && buffer_new(bh)) {
1127 /*
1128 * We allocated new blocks which will result in
1129 * i_data's format changing. Force the migrate
1130 * to fail by clearing migrate flags
1131 */
1132 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1133 ~EXT4_EXT_MIGRATE;
1134 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001135 }
Mingming Caod2a17632008-07-14 17:52:37 -04001136
1137 if (flag) {
1138 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
1139 /*
1140 * Update reserved blocks/metadata blocks
1141 * after successful block allocation
1142 * which were deferred till now
1143 */
1144 if ((retval > 0) && buffer_delay(bh))
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001145 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001146 }
1147
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001148 up_write((&EXT4_I(inode)->i_data_sem));
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001149 return retval;
1150}
1151
Mingming Caof3bd1f32008-08-19 22:16:03 -04001152/* Maximum number of blocks we map for direct IO at once. */
1153#define DIO_MAX_BLOCKS 4096
1154
Eric Sandeen6873fa02008-10-07 00:46:36 -04001155int ext4_get_block(struct inode *inode, sector_t iblock,
1156 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001158 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001159 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001161 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162
Jan Kara7fb54092008-02-10 01:08:38 -05001163 if (create && !handle) {
1164 /* Direct IO write... */
1165 if (max_blocks > DIO_MAX_BLOCKS)
1166 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001167 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1168 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001169 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001171 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 }
Jan Kara7fb54092008-02-10 01:08:38 -05001173 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 }
1175
Jan Kara7fb54092008-02-10 01:08:38 -05001176 ret = ext4_get_blocks_wrap(handle, inode, iblock,
Mingming Caod2a17632008-07-14 17:52:37 -04001177 max_blocks, bh_result, create, 0, 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001178 if (ret > 0) {
1179 bh_result->b_size = (ret << inode->i_blkbits);
1180 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 }
Jan Kara7fb54092008-02-10 01:08:38 -05001182 if (started)
1183 ext4_journal_stop(handle);
1184out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 return ret;
1186}
1187
1188/*
1189 * `handle' can be NULL if create is zero
1190 */
Mingming Cao617ba132006-10-11 01:20:53 -07001191struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001192 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193{
1194 struct buffer_head dummy;
1195 int fatal = 0, err;
1196
1197 J_ASSERT(handle != NULL || create == 0);
1198
1199 dummy.b_state = 0;
1200 dummy.b_blocknr = -1000;
1201 buffer_trace_init(&dummy.b_history);
Alex Tomasa86c6182006-10-11 01:21:03 -07001202 err = ext4_get_blocks_wrap(handle, inode, block, 1,
Mingming Caod2a17632008-07-14 17:52:37 -04001203 &dummy, create, 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001205 * ext4_get_blocks_handle() returns number of blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 * mapped. 0 in case of a HOLE.
1207 */
1208 if (err > 0) {
1209 if (err > 1)
1210 WARN_ON(1);
1211 err = 0;
1212 }
1213 *errp = err;
1214 if (!err && buffer_mapped(&dummy)) {
1215 struct buffer_head *bh;
1216 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1217 if (!bh) {
1218 *errp = -EIO;
1219 goto err;
1220 }
1221 if (buffer_new(&dummy)) {
1222 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001223 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001224
1225 /*
1226 * Now that we do not always journal data, we should
1227 * keep in mind whether this should always journal the
1228 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001229 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230 * problem.
1231 */
1232 lock_buffer(bh);
1233 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001234 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001236 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237 set_buffer_uptodate(bh);
1238 }
1239 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001240 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1241 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 if (!fatal)
1243 fatal = err;
1244 } else {
1245 BUFFER_TRACE(bh, "not a new buffer");
1246 }
1247 if (fatal) {
1248 *errp = fatal;
1249 brelse(bh);
1250 bh = NULL;
1251 }
1252 return bh;
1253 }
1254err:
1255 return NULL;
1256}
1257
Mingming Cao617ba132006-10-11 01:20:53 -07001258struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001259 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001261 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262
Mingming Cao617ba132006-10-11 01:20:53 -07001263 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264 if (!bh)
1265 return bh;
1266 if (buffer_uptodate(bh))
1267 return bh;
1268 ll_rw_block(READ_META, 1, &bh);
1269 wait_on_buffer(bh);
1270 if (buffer_uptodate(bh))
1271 return bh;
1272 put_bh(bh);
1273 *err = -EIO;
1274 return NULL;
1275}
1276
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001277static int walk_page_buffers(handle_t *handle,
1278 struct buffer_head *head,
1279 unsigned from,
1280 unsigned to,
1281 int *partial,
1282 int (*fn)(handle_t *handle,
1283 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284{
1285 struct buffer_head *bh;
1286 unsigned block_start, block_end;
1287 unsigned blocksize = head->b_size;
1288 int err, ret = 0;
1289 struct buffer_head *next;
1290
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001291 for (bh = head, block_start = 0;
1292 ret == 0 && (bh != head || !block_start);
1293 block_start = block_end, bh = next)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 {
1295 next = bh->b_this_page;
1296 block_end = block_start + blocksize;
1297 if (block_end <= from || block_start >= to) {
1298 if (partial && !buffer_uptodate(bh))
1299 *partial = 1;
1300 continue;
1301 }
1302 err = (*fn)(handle, bh);
1303 if (!ret)
1304 ret = err;
1305 }
1306 return ret;
1307}
1308
1309/*
1310 * To preserve ordering, it is essential that the hole instantiation and
1311 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001312 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001313 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001314 * prepare_write() is the right place.
1315 *
Mingming Cao617ba132006-10-11 01:20:53 -07001316 * Also, this function can nest inside ext4_writepage() ->
1317 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 * has generated enough buffer credits to do the whole page. So we won't
1319 * block on the journal in that case, which is good, because the caller may
1320 * be PF_MEMALLOC.
1321 *
Mingming Cao617ba132006-10-11 01:20:53 -07001322 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 * quota file writes. If we were to commit the transaction while thus
1324 * reentered, there can be a deadlock - we would be holding a quota
1325 * lock, and the commit would never complete if another thread had a
1326 * transaction open and was blocking on the quota lock - a ranking
1327 * violation.
1328 *
Mingming Caodab291a2006-10-11 01:21:01 -07001329 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 * will _not_ run commit under these circumstances because handle->h_ref
1331 * is elevated. We'll still have enough credits for the tiny quotafile
1332 * write.
1333 */
1334static int do_journal_get_write_access(handle_t *handle,
1335 struct buffer_head *bh)
1336{
1337 if (!buffer_mapped(bh) || buffer_freed(bh))
1338 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001339 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001340}
1341
Nick Pigginbfc1af62007-10-16 01:25:05 -07001342static int ext4_write_begin(struct file *file, struct address_space *mapping,
1343 loff_t pos, unsigned len, unsigned flags,
1344 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001346 struct inode *inode = mapping->host;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001347 int ret, needed_blocks = ext4_writepage_trans_blocks(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 handle_t *handle;
1349 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001350 struct page *page;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001351 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001352 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001353
Theodore Ts'oba80b102009-01-03 20:03:21 -05001354 trace_mark(ext4_write_begin,
1355 "dev %s ino %lu pos %llu len %u flags %u",
1356 inode->i_sb->s_id, inode->i_ino,
1357 (unsigned long long) pos, len, flags);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001358 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001359 from = pos & (PAGE_CACHE_SIZE - 1);
1360 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361
1362retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001363 handle = ext4_journal_start(inode, needed_blocks);
1364 if (IS_ERR(handle)) {
1365 ret = PTR_ERR(handle);
1366 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001367 }
1368
Nick Piggin54566b22009-01-04 12:00:53 -08001369 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001370 if (!page) {
1371 ext4_journal_stop(handle);
1372 ret = -ENOMEM;
1373 goto out;
1374 }
1375 *pagep = page;
1376
Nick Pigginbfc1af62007-10-16 01:25:05 -07001377 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1378 ext4_get_block);
1379
1380 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 ret = walk_page_buffers(handle, page_buffers(page),
1382 from, to, NULL, do_journal_get_write_access);
1383 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001384
1385 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001386 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001387 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001388 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001389 /*
1390 * block_write_begin may have instantiated a few blocks
1391 * outside i_size. Trim these off again. Don't need
1392 * i_size_read because we hold i_mutex.
1393 */
1394 if (pos + len > inode->i_size)
1395 vmtruncate(inode, inode->i_size);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001396 }
1397
Mingming Cao617ba132006-10-11 01:20:53 -07001398 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001399 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001400out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 return ret;
1402}
1403
Nick Pigginbfc1af62007-10-16 01:25:05 -07001404/* For write_end() in data=journal mode */
1405static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406{
1407 if (!buffer_mapped(bh) || buffer_freed(bh))
1408 return 0;
1409 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001410 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411}
1412
1413/*
1414 * We need to pick up the new inode size which generic_commit_write gave us
1415 * `file' can be NULL - eg, when called from page_symlink().
1416 *
Mingming Cao617ba132006-10-11 01:20:53 -07001417 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 * buffers are managed internally.
1419 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001420static int ext4_ordered_write_end(struct file *file,
1421 struct address_space *mapping,
1422 loff_t pos, unsigned len, unsigned copied,
1423 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424{
Mingming Cao617ba132006-10-11 01:20:53 -07001425 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001426 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001427 int ret = 0, ret2;
1428
Theodore Ts'oba80b102009-01-03 20:03:21 -05001429 trace_mark(ext4_ordered_write_end,
1430 "dev %s ino %lu pos %llu len %u copied %u",
1431 inode->i_sb->s_id, inode->i_ino,
1432 (unsigned long long) pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001433 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434
1435 if (ret == 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 loff_t new_i_size;
1437
Nick Pigginbfc1af62007-10-16 01:25:05 -07001438 new_i_size = pos + copied;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001439 if (new_i_size > EXT4_I(inode)->i_disksize) {
1440 ext4_update_i_disksize(inode, new_i_size);
1441 /* We need to mark inode dirty even if
1442 * new_i_size is less that inode->i_size
1443 * bu greater than i_disksize.(hint delalloc)
1444 */
1445 ext4_mark_inode_dirty(handle, inode);
1446 }
1447
Jan Karacf108bc2008-07-11 19:27:31 -04001448 ret2 = generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001449 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001450 copied = ret2;
1451 if (ret2 < 0)
1452 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 }
Mingming Cao617ba132006-10-11 01:20:53 -07001454 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455 if (!ret)
1456 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001457
1458 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459}
1460
Nick Pigginbfc1af62007-10-16 01:25:05 -07001461static int ext4_writeback_write_end(struct file *file,
1462 struct address_space *mapping,
1463 loff_t pos, unsigned len, unsigned copied,
1464 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465{
Mingming Cao617ba132006-10-11 01:20:53 -07001466 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001467 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 int ret = 0, ret2;
1469 loff_t new_i_size;
1470
Theodore Ts'oba80b102009-01-03 20:03:21 -05001471 trace_mark(ext4_writeback_write_end,
1472 "dev %s ino %lu pos %llu len %u copied %u",
1473 inode->i_sb->s_id, inode->i_ino,
1474 (unsigned long long) pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001475 new_i_size = pos + copied;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001476 if (new_i_size > EXT4_I(inode)->i_disksize) {
1477 ext4_update_i_disksize(inode, new_i_size);
1478 /* We need to mark inode dirty even if
1479 * new_i_size is less that inode->i_size
1480 * bu greater than i_disksize.(hint delalloc)
1481 */
1482 ext4_mark_inode_dirty(handle, inode);
1483 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484
Jan Karacf108bc2008-07-11 19:27:31 -04001485 ret2 = generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001486 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001487 copied = ret2;
1488 if (ret2 < 0)
1489 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490
Mingming Cao617ba132006-10-11 01:20:53 -07001491 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 if (!ret)
1493 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001494
1495 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001496}
1497
Nick Pigginbfc1af62007-10-16 01:25:05 -07001498static int ext4_journalled_write_end(struct file *file,
1499 struct address_space *mapping,
1500 loff_t pos, unsigned len, unsigned copied,
1501 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001502{
Mingming Cao617ba132006-10-11 01:20:53 -07001503 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001504 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 int ret = 0, ret2;
1506 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001507 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001508 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509
Theodore Ts'oba80b102009-01-03 20:03:21 -05001510 trace_mark(ext4_journalled_write_end,
1511 "dev %s ino %lu pos %llu len %u copied %u",
1512 inode->i_sb->s_id, inode->i_ino,
1513 (unsigned long long) pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001514 from = pos & (PAGE_CACHE_SIZE - 1);
1515 to = from + len;
1516
1517 if (copied < len) {
1518 if (!PageUptodate(page))
1519 copied = 0;
1520 page_zero_new_buffers(page, from+copied, to);
1521 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522
1523 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001524 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001525 if (!partial)
1526 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001527 new_i_size = pos + copied;
1528 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001529 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001530 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001531 if (new_i_size > EXT4_I(inode)->i_disksize) {
1532 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001533 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534 if (!ret)
1535 ret = ret2;
1536 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001537
Jan Karacf108bc2008-07-11 19:27:31 -04001538 unlock_page(page);
Mingming Cao617ba132006-10-11 01:20:53 -07001539 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 if (!ret)
1541 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001542 page_cache_release(page);
1543
1544 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545}
Mingming Caod2a17632008-07-14 17:52:37 -04001546
1547static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1548{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001549 int retries = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001550 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1551 unsigned long md_needed, mdblocks, total = 0;
1552
1553 /*
1554 * recalculate the amount of metadata blocks to reserve
1555 * in order to allocate nrblocks
1556 * worse case is one extent per block
1557 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001558repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001559 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1560 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1561 mdblocks = ext4_calc_metadata_amount(inode, total);
1562 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1563
1564 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1565 total = md_needed + nrblocks;
1566
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001567 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001568 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001569 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1570 yield();
1571 goto repeat;
1572 }
Mingming Caod2a17632008-07-14 17:52:37 -04001573 return -ENOSPC;
1574 }
Mingming Caod2a17632008-07-14 17:52:37 -04001575 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1576 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1577
1578 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1579 return 0; /* success */
1580}
1581
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001582static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001583{
1584 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1585 int total, mdb, mdb_free, release;
1586
Mingming Caocd213222008-08-19 22:16:59 -04001587 if (!to_free)
1588 return; /* Nothing to release, exit */
1589
Mingming Caod2a17632008-07-14 17:52:37 -04001590 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001591
1592 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1593 /*
1594 * if there is no reserved blocks, but we try to free some
1595 * then the counter is messed up somewhere.
1596 * but since this function is called from invalidate
1597 * page, it's harmless to return without any action
1598 */
1599 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1600 "blocks for inode %lu, but there is no reserved "
1601 "data blocks\n", to_free, inode->i_ino);
1602 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1603 return;
1604 }
1605
Mingming Caod2a17632008-07-14 17:52:37 -04001606 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001607 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001608 mdb = ext4_calc_metadata_amount(inode, total);
1609
1610 /* figure out how many metablocks to release */
1611 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1612 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1613
Mingming Caod2a17632008-07-14 17:52:37 -04001614 release = to_free + mdb_free;
1615
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001616 /* update fs dirty blocks counter for truncate case */
1617 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001618
1619 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001620 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1621 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001622
1623 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1624 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001625 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1626}
1627
1628static void ext4_da_page_release_reservation(struct page *page,
1629 unsigned long offset)
1630{
1631 int to_release = 0;
1632 struct buffer_head *head, *bh;
1633 unsigned int curr_off = 0;
1634
1635 head = page_buffers(page);
1636 bh = head;
1637 do {
1638 unsigned int next_off = curr_off + bh->b_size;
1639
1640 if ((offset <= curr_off) && (buffer_delay(bh))) {
1641 to_release++;
1642 clear_buffer_delay(bh);
1643 }
1644 curr_off = next_off;
1645 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001646 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001647}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001648
1649/*
Alex Tomas64769242008-07-11 19:27:31 -04001650 * Delayed allocation stuff
1651 */
1652
1653struct mpage_da_data {
1654 struct inode *inode;
1655 struct buffer_head lbh; /* extent of blocks */
1656 unsigned long first_page, next_page; /* extent of pages */
1657 get_block_t *get_block;
1658 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001659 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001660 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001661 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001662};
1663
1664/*
1665 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001666 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001667 *
1668 * @mpd->inode: inode
1669 * @mpd->first_page: first page of the extent
1670 * @mpd->next_page: page after the last page of the extent
1671 * @mpd->get_block: the filesystem's block mapper function
1672 *
1673 * By the time mpage_da_submit_io() is called we expect all blocks
1674 * to be allocated. this may be wrong if allocation failed.
1675 *
1676 * As pages are already locked by write_cache_pages(), we can't use it
1677 */
1678static int mpage_da_submit_io(struct mpage_da_data *mpd)
1679{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001680 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001681 struct pagevec pvec;
1682 unsigned long index, end;
1683 int ret = 0, err, nr_pages, i;
1684 struct inode *inode = mpd->inode;
1685 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001686
1687 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001688 /*
1689 * We need to start from the first_page to the next_page - 1
1690 * to make sure we also write the mapped dirty buffer_heads.
1691 * If we look at mpd->lbh.b_blocknr we would only be looking
1692 * at the currently mapped buffer_heads.
1693 */
Alex Tomas64769242008-07-11 19:27:31 -04001694 index = mpd->first_page;
1695 end = mpd->next_page - 1;
1696
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001697 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001698 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001699 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001700 if (nr_pages == 0)
1701 break;
1702 for (i = 0; i < nr_pages; i++) {
1703 struct page *page = pvec.pages[i];
1704
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001705 index = page->index;
1706 if (index > end)
1707 break;
1708 index++;
1709
1710 BUG_ON(!PageLocked(page));
1711 BUG_ON(PageWriteback(page));
1712
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001713 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001714 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001715 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1716 /*
1717 * have successfully written the page
1718 * without skipping the same
1719 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001720 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001721 /*
1722 * In error case, we have to continue because
1723 * remaining pages are still locked
1724 * XXX: unlock and re-dirty them?
1725 */
1726 if (ret == 0)
1727 ret = err;
1728 }
1729 pagevec_release(&pvec);
1730 }
Alex Tomas64769242008-07-11 19:27:31 -04001731 return ret;
1732}
1733
1734/*
1735 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1736 *
1737 * @mpd->inode - inode to walk through
1738 * @exbh->b_blocknr - first block on a disk
1739 * @exbh->b_size - amount of space in bytes
1740 * @logical - first logical block to start assignment with
1741 *
1742 * the function goes through all passed space and put actual disk
1743 * block numbers into buffer heads, dropping BH_Delay
1744 */
1745static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1746 struct buffer_head *exbh)
1747{
1748 struct inode *inode = mpd->inode;
1749 struct address_space *mapping = inode->i_mapping;
1750 int blocks = exbh->b_size >> inode->i_blkbits;
1751 sector_t pblock = exbh->b_blocknr, cur_logical;
1752 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001753 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001754 struct pagevec pvec;
1755 int nr_pages, i;
1756
1757 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1758 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1759 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1760
1761 pagevec_init(&pvec, 0);
1762
1763 while (index <= end) {
1764 /* XXX: optimize tail */
1765 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1766 if (nr_pages == 0)
1767 break;
1768 for (i = 0; i < nr_pages; i++) {
1769 struct page *page = pvec.pages[i];
1770
1771 index = page->index;
1772 if (index > end)
1773 break;
1774 index++;
1775
1776 BUG_ON(!PageLocked(page));
1777 BUG_ON(PageWriteback(page));
1778 BUG_ON(!page_has_buffers(page));
1779
1780 bh = page_buffers(page);
1781 head = bh;
1782
1783 /* skip blocks out of the range */
1784 do {
1785 if (cur_logical >= logical)
1786 break;
1787 cur_logical++;
1788 } while ((bh = bh->b_this_page) != head);
1789
1790 do {
1791 if (cur_logical >= logical + blocks)
1792 break;
Alex Tomas64769242008-07-11 19:27:31 -04001793 if (buffer_delay(bh)) {
1794 bh->b_blocknr = pblock;
1795 clear_buffer_delay(bh);
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001796 bh->b_bdev = inode->i_sb->s_bdev;
1797 } else if (buffer_unwritten(bh)) {
1798 bh->b_blocknr = pblock;
1799 clear_buffer_unwritten(bh);
1800 set_buffer_mapped(bh);
1801 set_buffer_new(bh);
1802 bh->b_bdev = inode->i_sb->s_bdev;
Mingming Cao61628a32008-07-11 19:27:31 -04001803 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04001804 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04001805
1806 cur_logical++;
1807 pblock++;
1808 } while ((bh = bh->b_this_page) != head);
1809 }
1810 pagevec_release(&pvec);
1811 }
1812}
1813
1814
1815/*
1816 * __unmap_underlying_blocks - just a helper function to unmap
1817 * set of blocks described by @bh
1818 */
1819static inline void __unmap_underlying_blocks(struct inode *inode,
1820 struct buffer_head *bh)
1821{
1822 struct block_device *bdev = inode->i_sb->s_bdev;
1823 int blocks, i;
1824
1825 blocks = bh->b_size >> inode->i_blkbits;
1826 for (i = 0; i < blocks; i++)
1827 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
1828}
1829
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001830static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
1831 sector_t logical, long blk_cnt)
1832{
1833 int nr_pages, i;
1834 pgoff_t index, end;
1835 struct pagevec pvec;
1836 struct inode *inode = mpd->inode;
1837 struct address_space *mapping = inode->i_mapping;
1838
1839 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1840 end = (logical + blk_cnt - 1) >>
1841 (PAGE_CACHE_SHIFT - inode->i_blkbits);
1842 while (index <= end) {
1843 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1844 if (nr_pages == 0)
1845 break;
1846 for (i = 0; i < nr_pages; i++) {
1847 struct page *page = pvec.pages[i];
1848 index = page->index;
1849 if (index > end)
1850 break;
1851 index++;
1852
1853 BUG_ON(!PageLocked(page));
1854 BUG_ON(PageWriteback(page));
1855 block_invalidatepage(page, 0);
1856 ClearPageUptodate(page);
1857 unlock_page(page);
1858 }
1859 }
1860 return;
1861}
1862
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001863static void ext4_print_free_blocks(struct inode *inode)
1864{
1865 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1866 printk(KERN_EMERG "Total free blocks count %lld\n",
1867 ext4_count_free_blocks(inode->i_sb));
1868 printk(KERN_EMERG "Free/Dirty block details\n");
1869 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04001870 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001871 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04001872 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001873 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001874 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001875 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001876 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001877 EXT4_I(inode)->i_reserved_meta_blocks);
1878 return;
1879}
1880
Alex Tomas64769242008-07-11 19:27:31 -04001881/*
1882 * mpage_da_map_blocks - go through given space
1883 *
1884 * @mpd->lbh - bh describing space
1885 * @mpd->get_block - the filesystem's block mapper function
1886 *
1887 * The function skips space we know is already mapped to disk blocks.
1888 *
Alex Tomas64769242008-07-11 19:27:31 -04001889 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001890static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04001891{
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001892 int err = 0;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001893 struct buffer_head new;
Alex Tomas64769242008-07-11 19:27:31 -04001894 struct buffer_head *lbh = &mpd->lbh;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001895 sector_t next;
Alex Tomas64769242008-07-11 19:27:31 -04001896
1897 /*
1898 * We consider only non-mapped and non-allocated blocks
1899 */
1900 if (buffer_mapped(lbh) && !buffer_delay(lbh))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001901 return 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001902 new.b_state = lbh->b_state;
1903 new.b_blocknr = 0;
1904 new.b_size = lbh->b_size;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001905 next = lbh->b_blocknr;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001906 /*
1907 * If we didn't accumulate anything
1908 * to write simply return
1909 */
1910 if (!new.b_size)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001911 return 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001912 err = mpd->get_block(mpd->inode, next, &new, 1);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001913 if (err) {
1914
1915 /* If get block returns with error
1916 * we simply return. Later writepage
1917 * will redirty the page and writepages
1918 * will find the dirty page again
1919 */
1920 if (err == -EAGAIN)
1921 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001922
1923 if (err == -ENOSPC &&
1924 ext4_count_free_blocks(mpd->inode->i_sb)) {
1925 mpd->retval = err;
1926 return 0;
1927 }
1928
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001929 /*
1930 * get block failure will cause us
1931 * to loop in writepages. Because
1932 * a_ops->writepage won't be able to
1933 * make progress. The page will be redirtied
1934 * by writepage and writepages will again
1935 * try to write the same.
1936 */
1937 printk(KERN_EMERG "%s block allocation failed for inode %lu "
1938 "at logical offset %llu with max blocks "
1939 "%zd with error %d\n",
1940 __func__, mpd->inode->i_ino,
1941 (unsigned long long)next,
1942 lbh->b_size >> mpd->inode->i_blkbits, err);
1943 printk(KERN_EMERG "This should not happen.!! "
1944 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001945 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001946 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001947 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001948 /* invlaidate all the pages */
1949 ext4_da_block_invalidatepages(mpd, next,
1950 lbh->b_size >> mpd->inode->i_blkbits);
1951 return err;
1952 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001953 BUG_ON(new.b_size == 0);
Alex Tomas64769242008-07-11 19:27:31 -04001954
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001955 if (buffer_new(&new))
1956 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04001957
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001958 /*
1959 * If blocks are delayed marked, we need to
1960 * put actual blocknr and drop delayed bit
1961 */
1962 if (buffer_delay(lbh) || buffer_unwritten(lbh))
1963 mpage_put_bnr_to_bhs(mpd, next, &new);
1964
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001965 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001966}
1967
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001968#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1969 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04001970
1971/*
1972 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1973 *
1974 * @mpd->lbh - extent of blocks
1975 * @logical - logical number of the block in the file
1976 * @bh - bh of the block (used to access block's state)
1977 *
1978 * the function is used to collect contig. blocks in same state
1979 */
1980static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
1981 sector_t logical, struct buffer_head *bh)
1982{
Alex Tomas64769242008-07-11 19:27:31 -04001983 sector_t next;
Mingming Cao525f4ed2008-08-19 22:15:58 -04001984 size_t b_size = bh->b_size;
1985 struct buffer_head *lbh = &mpd->lbh;
1986 int nrblocks = lbh->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04001987
Mingming Cao525f4ed2008-08-19 22:15:58 -04001988 /* check if thereserved journal credits might overflow */
1989 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
1990 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1991 /*
1992 * With non-extent format we are limited by the journal
1993 * credit available. Total credit needed to insert
1994 * nrblocks contiguous blocks is dependent on the
1995 * nrblocks. So limit nrblocks.
1996 */
1997 goto flush_it;
1998 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
1999 EXT4_MAX_TRANS_DATA) {
2000 /*
2001 * Adding the new buffer_head would make it cross the
2002 * allowed limit for which we have journal credit
2003 * reserved. So limit the new bh->b_size
2004 */
2005 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2006 mpd->inode->i_blkbits;
2007 /* we will do mpage_da_submit_io in the next loop */
2008 }
2009 }
Alex Tomas64769242008-07-11 19:27:31 -04002010 /*
2011 * First block in the extent
2012 */
2013 if (lbh->b_size == 0) {
2014 lbh->b_blocknr = logical;
Mingming Cao525f4ed2008-08-19 22:15:58 -04002015 lbh->b_size = b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002016 lbh->b_state = bh->b_state & BH_FLAGS;
2017 return;
2018 }
2019
Mingming Cao525f4ed2008-08-19 22:15:58 -04002020 next = lbh->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002021 /*
2022 * Can we merge the block to our big extent?
2023 */
2024 if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04002025 lbh->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002026 return;
2027 }
2028
Mingming Cao525f4ed2008-08-19 22:15:58 -04002029flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002030 /*
2031 * We couldn't merge the block to our extent, so we
2032 * need to flush current extent and start new one
2033 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002034 if (mpage_da_map_blocks(mpd) == 0)
2035 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002036 mpd->io_done = 1;
2037 return;
Alex Tomas64769242008-07-11 19:27:31 -04002038}
2039
2040/*
2041 * __mpage_da_writepage - finds extent of pages and blocks
2042 *
2043 * @page: page to consider
2044 * @wbc: not used, we just follow rules
2045 * @data: context
2046 *
2047 * The function finds extents of pages and scan them for all blocks.
2048 */
2049static int __mpage_da_writepage(struct page *page,
2050 struct writeback_control *wbc, void *data)
2051{
2052 struct mpage_da_data *mpd = data;
2053 struct inode *inode = mpd->inode;
2054 struct buffer_head *bh, *head, fake;
2055 sector_t logical;
2056
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002057 if (mpd->io_done) {
2058 /*
2059 * Rest of the page in the page_vec
2060 * redirty then and skip then. We will
2061 * try to to write them again after
2062 * starting a new transaction
2063 */
2064 redirty_page_for_writepage(wbc, page);
2065 unlock_page(page);
2066 return MPAGE_DA_EXTENT_TAIL;
2067 }
Alex Tomas64769242008-07-11 19:27:31 -04002068 /*
2069 * Can we merge this page to current extent?
2070 */
2071 if (mpd->next_page != page->index) {
2072 /*
2073 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002074 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002075 */
2076 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002077 if (mpage_da_map_blocks(mpd) == 0)
2078 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002079 /*
2080 * skip rest of the page in the page_vec
2081 */
2082 mpd->io_done = 1;
2083 redirty_page_for_writepage(wbc, page);
2084 unlock_page(page);
2085 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002086 }
2087
2088 /*
2089 * Start next extent of pages ...
2090 */
2091 mpd->first_page = page->index;
2092
2093 /*
2094 * ... and blocks
2095 */
2096 mpd->lbh.b_size = 0;
2097 mpd->lbh.b_state = 0;
2098 mpd->lbh.b_blocknr = 0;
2099 }
2100
2101 mpd->next_page = page->index + 1;
2102 logical = (sector_t) page->index <<
2103 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2104
2105 if (!page_has_buffers(page)) {
2106 /*
2107 * There is no attached buffer heads yet (mmap?)
2108 * we treat the page asfull of dirty blocks
2109 */
2110 bh = &fake;
2111 bh->b_size = PAGE_CACHE_SIZE;
2112 bh->b_state = 0;
2113 set_buffer_dirty(bh);
2114 set_buffer_uptodate(bh);
2115 mpage_add_bh_to_extent(mpd, logical, bh);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002116 if (mpd->io_done)
2117 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002118 } else {
2119 /*
2120 * Page with regular buffer heads, just add all dirty ones
2121 */
2122 head = page_buffers(page);
2123 bh = head;
2124 do {
2125 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002126 /*
2127 * We need to try to allocate
2128 * unmapped blocks in the same page.
2129 * Otherwise we won't make progress
2130 * with the page in ext4_da_writepage
2131 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002132 if (buffer_dirty(bh) &&
2133 (!buffer_mapped(bh) || buffer_delay(bh))) {
Alex Tomas64769242008-07-11 19:27:31 -04002134 mpage_add_bh_to_extent(mpd, logical, bh);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002135 if (mpd->io_done)
2136 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002137 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2138 /*
2139 * mapped dirty buffer. We need to update
2140 * the b_state because we look at
2141 * b_state in mpage_da_map_blocks. We don't
2142 * update b_size because if we find an
2143 * unmapped buffer_head later we need to
2144 * use the b_state flag of that buffer_head.
2145 */
2146 if (mpd->lbh.b_size == 0)
2147 mpd->lbh.b_state =
2148 bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002149 }
Alex Tomas64769242008-07-11 19:27:31 -04002150 logical++;
2151 } while ((bh = bh->b_this_page) != head);
2152 }
2153
2154 return 0;
2155}
2156
2157/*
2158 * mpage_da_writepages - walk the list of dirty pages of the given
2159 * address space, allocates non-allocated blocks, maps newly-allocated
2160 * blocks to existing bhs and issue IO them
2161 *
2162 * @mapping: address space structure to write
2163 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2164 * @get_block: the filesystem's block mapper function.
2165 *
2166 * This is a library function, which implements the writepages()
2167 * address_space_operation.
Alex Tomas64769242008-07-11 19:27:31 -04002168 */
2169static int mpage_da_writepages(struct address_space *mapping,
2170 struct writeback_control *wbc,
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002171 struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002172{
Alex Tomas64769242008-07-11 19:27:31 -04002173 int ret;
2174
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002175 if (!mpd->get_block)
Alex Tomas64769242008-07-11 19:27:31 -04002176 return generic_writepages(mapping, wbc);
2177
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002178 mpd->lbh.b_size = 0;
2179 mpd->lbh.b_state = 0;
2180 mpd->lbh.b_blocknr = 0;
2181 mpd->first_page = 0;
2182 mpd->next_page = 0;
2183 mpd->io_done = 0;
2184 mpd->pages_written = 0;
2185 mpd->retval = 0;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002186
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002187 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, mpd);
Alex Tomas64769242008-07-11 19:27:31 -04002188 /*
2189 * Handle last extent of pages
2190 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002191 if (!mpd->io_done && mpd->next_page != mpd->first_page) {
2192 if (mpage_da_map_blocks(mpd) == 0)
2193 mpage_da_submit_io(mpd);
Alex Tomas64769242008-07-11 19:27:31 -04002194
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002195 mpd->io_done = 1;
2196 ret = MPAGE_DA_EXTENT_TAIL;
2197 }
2198 wbc->nr_to_write -= mpd->pages_written;
Alex Tomas64769242008-07-11 19:27:31 -04002199 return ret;
2200}
2201
2202/*
2203 * this is a special callback for ->write_begin() only
2204 * it's intention is to return mapped block or reserve space
2205 */
2206static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2207 struct buffer_head *bh_result, int create)
2208{
2209 int ret = 0;
2210
2211 BUG_ON(create == 0);
2212 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2213
2214 /*
2215 * first, we need to know whether the block is allocated already
2216 * preallocated blocks are unmapped but should treated
2217 * the same as allocated blocks.
2218 */
Mingming Caod2a17632008-07-14 17:52:37 -04002219 ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1, bh_result, 0, 0, 0);
2220 if ((ret == 0) && !buffer_delay(bh_result)) {
2221 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002222 /*
2223 * XXX: __block_prepare_write() unmaps passed block,
2224 * is it OK?
2225 */
Mingming Caod2a17632008-07-14 17:52:37 -04002226 ret = ext4_da_reserve_space(inode, 1);
2227 if (ret)
2228 /* not enough space to reserve */
2229 return ret;
2230
Alex Tomas64769242008-07-11 19:27:31 -04002231 map_bh(bh_result, inode->i_sb, 0);
2232 set_buffer_new(bh_result);
2233 set_buffer_delay(bh_result);
2234 } else if (ret > 0) {
2235 bh_result->b_size = (ret << inode->i_blkbits);
2236 ret = 0;
2237 }
2238
2239 return ret;
2240}
Mingming Caod2a17632008-07-14 17:52:37 -04002241#define EXT4_DELALLOC_RSVED 1
Alex Tomas64769242008-07-11 19:27:31 -04002242static int ext4_da_get_block_write(struct inode *inode, sector_t iblock,
2243 struct buffer_head *bh_result, int create)
2244{
Mingming Cao61628a32008-07-11 19:27:31 -04002245 int ret;
Alex Tomas64769242008-07-11 19:27:31 -04002246 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2247 loff_t disksize = EXT4_I(inode)->i_disksize;
2248 handle_t *handle = NULL;
2249
Mingming Cao61628a32008-07-11 19:27:31 -04002250 handle = ext4_journal_current_handle();
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002251 BUG_ON(!handle);
2252 ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
2253 bh_result, create, 0, EXT4_DELALLOC_RSVED);
Alex Tomas64769242008-07-11 19:27:31 -04002254 if (ret > 0) {
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002255
Alex Tomas64769242008-07-11 19:27:31 -04002256 bh_result->b_size = (ret << inode->i_blkbits);
2257
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -04002258 if (ext4_should_order_data(inode)) {
2259 int retval;
2260 retval = ext4_jbd2_file_inode(handle, inode);
2261 if (retval)
2262 /*
2263 * Failed to add inode for ordered
2264 * mode. Don't update file size
2265 */
2266 return retval;
2267 }
2268
Alex Tomas64769242008-07-11 19:27:31 -04002269 /*
2270 * Update on-disk size along with block allocation
2271 * we don't use 'extend_disksize' as size may change
2272 * within already allocated block -bzzz
2273 */
2274 disksize = ((loff_t) iblock + ret) << inode->i_blkbits;
2275 if (disksize > i_size_read(inode))
2276 disksize = i_size_read(inode);
2277 if (disksize > EXT4_I(inode)->i_disksize) {
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002278 ext4_update_i_disksize(inode, disksize);
2279 ret = ext4_mark_inode_dirty(handle, inode);
2280 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002281 }
Alex Tomas64769242008-07-11 19:27:31 -04002282 ret = 0;
2283 }
Alex Tomas64769242008-07-11 19:27:31 -04002284 return ret;
2285}
Mingming Cao61628a32008-07-11 19:27:31 -04002286
2287static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh)
2288{
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002289 /*
2290 * unmapped buffer is possible for holes.
2291 * delay buffer is possible with delayed allocation
2292 */
2293 return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh));
2294}
2295
2296static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock,
2297 struct buffer_head *bh_result, int create)
2298{
2299 int ret = 0;
2300 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2301
2302 /*
2303 * we don't want to do block allocation in writepage
2304 * so call get_block_wrap with create = 0
2305 */
2306 ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks,
2307 bh_result, 0, 0, 0);
2308 if (ret > 0) {
2309 bh_result->b_size = (ret << inode->i_blkbits);
2310 ret = 0;
2311 }
2312 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002313}
2314
Mingming Cao61628a32008-07-11 19:27:31 -04002315/*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002316 * get called vi ext4_da_writepages after taking page lock (have journal handle)
2317 * get called via journal_submit_inode_data_buffers (no journal handle)
2318 * get called via shrink_page_list via pdflush (no journal handle)
2319 * or grab_page_cache when doing write_begin (have journal handle)
Mingming Cao61628a32008-07-11 19:27:31 -04002320 */
Alex Tomas64769242008-07-11 19:27:31 -04002321static int ext4_da_writepage(struct page *page,
2322 struct writeback_control *wbc)
2323{
Alex Tomas64769242008-07-11 19:27:31 -04002324 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002325 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002326 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002327 struct buffer_head *page_bufs;
2328 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002329
Theodore Ts'oba80b102009-01-03 20:03:21 -05002330 trace_mark(ext4_da_writepage,
2331 "dev %s ino %lu page_index %lu",
2332 inode->i_sb->s_id, inode->i_ino, page->index);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002333 size = i_size_read(inode);
2334 if (page->index == size >> PAGE_CACHE_SHIFT)
2335 len = size & ~PAGE_CACHE_MASK;
2336 else
2337 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002338
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002339 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002340 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002341 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2342 ext4_bh_unmapped_or_delay)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002343 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002344 * We don't want to do block allocation
2345 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002346 * We may reach here when we do a journal commit
2347 * via journal_submit_inode_data_buffers.
2348 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002349 * them. We can also reach here via shrink_page_list
2350 */
2351 redirty_page_for_writepage(wbc, page);
2352 unlock_page(page);
2353 return 0;
2354 }
2355 } else {
2356 /*
2357 * The test for page_has_buffers() is subtle:
2358 * We know the page is dirty but it lost buffers. That means
2359 * that at some moment in time after write_begin()/write_end()
2360 * has been called all buffers have been clean and thus they
2361 * must have been written at least once. So they are all
2362 * mapped and we can happily proceed with mapping them
2363 * and writing the page.
2364 *
2365 * Try to initialize the buffer_heads and check whether
2366 * all are mapped and non delay. We don't want to
2367 * do block allocation here.
2368 */
2369 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2370 ext4_normal_get_block_write);
2371 if (!ret) {
2372 page_bufs = page_buffers(page);
2373 /* check whether all are mapped and non delay */
2374 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2375 ext4_bh_unmapped_or_delay)) {
2376 redirty_page_for_writepage(wbc, page);
2377 unlock_page(page);
2378 return 0;
2379 }
2380 } else {
2381 /*
2382 * We can't do block allocation here
2383 * so just redity the page and unlock
2384 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002385 */
Mingming Cao61628a32008-07-11 19:27:31 -04002386 redirty_page_for_writepage(wbc, page);
2387 unlock_page(page);
2388 return 0;
2389 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002390 /* now mark the buffer_heads as dirty and uptodate */
2391 block_commit_write(page, 0, PAGE_CACHE_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04002392 }
2393
2394 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002395 ret = nobh_writepage(page, ext4_normal_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002396 else
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002397 ret = block_write_full_page(page,
2398 ext4_normal_get_block_write,
2399 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002400
Alex Tomas64769242008-07-11 19:27:31 -04002401 return ret;
2402}
2403
Mingming Cao61628a32008-07-11 19:27:31 -04002404/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002405 * This is called via ext4_da_writepages() to
2406 * calulate the total number of credits to reserve to fit
2407 * a single extent allocation into a single transaction,
2408 * ext4_da_writpeages() will loop calling this before
2409 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002410 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002411
2412static int ext4_da_writepages_trans_blocks(struct inode *inode)
2413{
2414 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2415
2416 /*
2417 * With non-extent format the journal credit needed to
2418 * insert nrblocks contiguous block is dependent on
2419 * number of contiguous block. So we will limit
2420 * number of contiguous block to a sane value
2421 */
2422 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2423 (max_blocks > EXT4_MAX_TRANS_DATA))
2424 max_blocks = EXT4_MAX_TRANS_DATA;
2425
2426 return ext4_chunk_trans_blocks(inode, max_blocks);
2427}
Mingming Cao61628a32008-07-11 19:27:31 -04002428
Alex Tomas64769242008-07-11 19:27:31 -04002429static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002430 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002431{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002432 pgoff_t index;
2433 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002434 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002435 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002436 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002437 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002438 int pages_written = 0;
2439 long pages_skipped;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002440 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002441 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002442
Theodore Ts'oba80b102009-01-03 20:03:21 -05002443 trace_mark(ext4_da_writepages,
2444 "dev %s ino %lu nr_t_write %ld "
2445 "pages_skipped %ld range_start %llu "
2446 "range_end %llu nonblocking %d "
2447 "for_kupdate %d for_reclaim %d "
2448 "for_writepages %d range_cyclic %d",
2449 inode->i_sb->s_id, inode->i_ino,
2450 wbc->nr_to_write, wbc->pages_skipped,
2451 (unsigned long long) wbc->range_start,
2452 (unsigned long long) wbc->range_end,
2453 wbc->nonblocking, wbc->for_kupdate,
2454 wbc->for_reclaim, wbc->for_writepages,
2455 wbc->range_cyclic);
2456
Mingming Cao61628a32008-07-11 19:27:31 -04002457 /*
2458 * No pages to write? This is mainly a kludge to avoid starting
2459 * a transaction for special inodes like journal inode on last iput()
2460 * because that could violate lock ordering on umount
2461 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002462 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002463 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002464
2465 /*
2466 * If the filesystem has aborted, it is read-only, so return
2467 * right away instead of dumping stack traces later on that
2468 * will obscure the real source of the problem. We test
2469 * EXT4_MOUNT_ABORT instead of sb->s_flag's MS_RDONLY because
2470 * the latter could be true if the filesystem is mounted
2471 * read-only, and in that case, ext4_da_writepages should
2472 * *never* be called, so if that ever happens, we would want
2473 * the stack trace.
2474 */
2475 if (unlikely(sbi->s_mount_opt & EXT4_MOUNT_ABORT))
2476 return -EROFS;
2477
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002478 /*
2479 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2480 * This make sure small files blocks are allocated in
2481 * single attempt. This ensure that small files
2482 * get less fragmented.
2483 */
2484 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2485 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2486 wbc->nr_to_write = sbi->s_mb_stream_request;
2487 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002488 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2489 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002490
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002491 if (wbc->range_cyclic)
2492 index = mapping->writeback_index;
2493 else
2494 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002495
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002496 mpd.wbc = wbc;
2497 mpd.inode = mapping->host;
2498
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002499 /*
2500 * we don't want write_cache_pages to update
2501 * nr_to_write and writeback_index
2502 */
2503 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2504 wbc->no_nrwrite_index_update = 1;
2505 pages_skipped = wbc->pages_skipped;
2506
2507 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002508
2509 /*
2510 * we insert one extent at a time. So we need
2511 * credit needed for single extent allocation.
2512 * journalled mode is currently not supported
2513 * by delalloc
2514 */
2515 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002516 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002517
Mingming Cao61628a32008-07-11 19:27:31 -04002518 /* start a new transaction*/
2519 handle = ext4_journal_start(inode, needed_blocks);
2520 if (IS_ERR(handle)) {
2521 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002522 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002523 "%ld pages, ino %lu; err %d\n", __func__,
2524 wbc->nr_to_write, inode->i_ino, ret);
2525 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002526 goto out_writepages;
2527 }
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002528 mpd.get_block = ext4_da_get_block_write;
2529 ret = mpage_da_writepages(mapping, wbc, &mpd);
2530
Mingming Cao61628a32008-07-11 19:27:31 -04002531 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002532
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002533 if (mpd.retval == -ENOSPC) {
2534 /* commit the transaction which would
2535 * free blocks released in the transaction
2536 * and try again
2537 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002538 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002539 wbc->pages_skipped = pages_skipped;
2540 ret = 0;
2541 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002542 /*
2543 * got one extent now try with
2544 * rest of the pages
2545 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002546 pages_written += mpd.pages_written;
2547 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002548 ret = 0;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002549 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002550 /*
2551 * There is no more writeout needed
2552 * or we requested for a noblocking writeout
2553 * and we found the device congested
2554 */
Mingming Cao61628a32008-07-11 19:27:31 -04002555 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002556 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002557 if (pages_skipped != wbc->pages_skipped)
2558 printk(KERN_EMERG "This should not happen leaving %s "
2559 "with nr_to_write = %ld ret = %d\n",
2560 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002561
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002562 /* Update index */
2563 index += pages_written;
2564 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2565 /*
2566 * set the writeback_index so that range_cyclic
2567 * mode will write it back later
2568 */
2569 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002570
Mingming Cao61628a32008-07-11 19:27:31 -04002571out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002572 if (!no_nrwrite_index_update)
2573 wbc->no_nrwrite_index_update = 0;
2574 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'oba80b102009-01-03 20:03:21 -05002575 trace_mark(ext4_da_writepage_result,
2576 "dev %s ino %lu ret %d pages_written %d "
2577 "pages_skipped %ld congestion %d "
2578 "more_io %d no_nrwrite_index_update %d",
2579 inode->i_sb->s_id, inode->i_ino, ret,
2580 pages_written, wbc->pages_skipped,
2581 wbc->encountered_congestion, wbc->more_io,
2582 wbc->no_nrwrite_index_update);
Mingming Cao61628a32008-07-11 19:27:31 -04002583 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002584}
2585
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002586#define FALL_BACK_TO_NONDELALLOC 1
2587static int ext4_nonda_switch(struct super_block *sb)
2588{
2589 s64 free_blocks, dirty_blocks;
2590 struct ext4_sb_info *sbi = EXT4_SB(sb);
2591
2592 /*
2593 * switch to non delalloc mode if we are running low
2594 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002595 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002596 * accumulated on each CPU without updating global counters
2597 * Delalloc need an accurate free block accounting. So switch
2598 * to non delalloc when we are near to error range.
2599 */
2600 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2601 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2602 if (2 * free_blocks < 3 * dirty_blocks ||
2603 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2604 /*
2605 * free block count is less that 150% of dirty blocks
2606 * or free blocks is less that watermark
2607 */
2608 return 1;
2609 }
2610 return 0;
2611}
2612
Alex Tomas64769242008-07-11 19:27:31 -04002613static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
2614 loff_t pos, unsigned len, unsigned flags,
2615 struct page **pagep, void **fsdata)
2616{
Mingming Caod2a17632008-07-14 17:52:37 -04002617 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002618 struct page *page;
2619 pgoff_t index;
2620 unsigned from, to;
2621 struct inode *inode = mapping->host;
2622 handle_t *handle;
2623
2624 index = pos >> PAGE_CACHE_SHIFT;
2625 from = pos & (PAGE_CACHE_SIZE - 1);
2626 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002627
2628 if (ext4_nonda_switch(inode->i_sb)) {
2629 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2630 return ext4_write_begin(file, mapping, pos,
2631 len, flags, pagep, fsdata);
2632 }
2633 *fsdata = (void *)0;
Theodore Ts'oba80b102009-01-03 20:03:21 -05002634
2635 trace_mark(ext4_da_write_begin,
2636 "dev %s ino %lu pos %llu len %u flags %u",
2637 inode->i_sb->s_id, inode->i_ino,
2638 (unsigned long long) pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002639retry:
Alex Tomas64769242008-07-11 19:27:31 -04002640 /*
2641 * With delayed allocation, we don't log the i_disksize update
2642 * if there is delayed block allocation. But we still need
2643 * to journalling the i_disksize update if writes to the end
2644 * of file which has an already mapped buffer.
2645 */
2646 handle = ext4_journal_start(inode, 1);
2647 if (IS_ERR(handle)) {
2648 ret = PTR_ERR(handle);
2649 goto out;
2650 }
2651
Nick Piggin54566b22009-01-04 12:00:53 -08002652 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002653 if (!page) {
2654 ext4_journal_stop(handle);
2655 ret = -ENOMEM;
2656 goto out;
2657 }
Alex Tomas64769242008-07-11 19:27:31 -04002658 *pagep = page;
2659
2660 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
2661 ext4_da_get_block_prep);
2662 if (ret < 0) {
2663 unlock_page(page);
2664 ext4_journal_stop(handle);
2665 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002666 /*
2667 * block_write_begin may have instantiated a few blocks
2668 * outside i_size. Trim these off again. Don't need
2669 * i_size_read because we hold i_mutex.
2670 */
2671 if (pos + len > inode->i_size)
2672 vmtruncate(inode, inode->i_size);
Alex Tomas64769242008-07-11 19:27:31 -04002673 }
2674
Mingming Caod2a17632008-07-14 17:52:37 -04002675 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2676 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04002677out:
2678 return ret;
2679}
2680
Mingming Cao632eaea2008-07-11 19:27:31 -04002681/*
2682 * Check if we should update i_disksize
2683 * when write to the end of file but not require block allocation
2684 */
2685static int ext4_da_should_update_i_disksize(struct page *page,
2686 unsigned long offset)
2687{
2688 struct buffer_head *bh;
2689 struct inode *inode = page->mapping->host;
2690 unsigned int idx;
2691 int i;
2692
2693 bh = page_buffers(page);
2694 idx = offset >> inode->i_blkbits;
2695
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002696 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002697 bh = bh->b_this_page;
2698
2699 if (!buffer_mapped(bh) || (buffer_delay(bh)))
2700 return 0;
2701 return 1;
2702}
2703
Alex Tomas64769242008-07-11 19:27:31 -04002704static int ext4_da_write_end(struct file *file,
2705 struct address_space *mapping,
2706 loff_t pos, unsigned len, unsigned copied,
2707 struct page *page, void *fsdata)
2708{
2709 struct inode *inode = mapping->host;
2710 int ret = 0, ret2;
2711 handle_t *handle = ext4_journal_current_handle();
2712 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002713 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002714 int write_mode = (int)(unsigned long)fsdata;
2715
2716 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
2717 if (ext4_should_order_data(inode)) {
2718 return ext4_ordered_write_end(file, mapping, pos,
2719 len, copied, page, fsdata);
2720 } else if (ext4_should_writeback_data(inode)) {
2721 return ext4_writeback_write_end(file, mapping, pos,
2722 len, copied, page, fsdata);
2723 } else {
2724 BUG();
2725 }
2726 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002727
Theodore Ts'oba80b102009-01-03 20:03:21 -05002728 trace_mark(ext4_da_write_end,
2729 "dev %s ino %lu pos %llu len %u copied %u",
2730 inode->i_sb->s_id, inode->i_ino,
2731 (unsigned long long) pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002732 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002733 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002734
2735 /*
2736 * generic_write_end() will run mark_inode_dirty() if i_size
2737 * changes. So let's piggyback the i_disksize mark_inode_dirty
2738 * into that.
2739 */
2740
2741 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04002742 if (new_i_size > EXT4_I(inode)->i_disksize) {
2743 if (ext4_da_should_update_i_disksize(page, end)) {
2744 down_write(&EXT4_I(inode)->i_data_sem);
2745 if (new_i_size > EXT4_I(inode)->i_disksize) {
2746 /*
2747 * Updating i_disksize when extending file
2748 * without needing block allocation
2749 */
2750 if (ext4_should_order_data(inode))
2751 ret = ext4_jbd2_file_inode(handle,
2752 inode);
Alex Tomas64769242008-07-11 19:27:31 -04002753
Mingming Cao632eaea2008-07-11 19:27:31 -04002754 EXT4_I(inode)->i_disksize = new_i_size;
2755 }
2756 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002757 /* We need to mark inode dirty even if
2758 * new_i_size is less that inode->i_size
2759 * bu greater than i_disksize.(hint delalloc)
2760 */
2761 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002762 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002763 }
Alex Tomas64769242008-07-11 19:27:31 -04002764 ret2 = generic_write_end(file, mapping, pos, len, copied,
2765 page, fsdata);
2766 copied = ret2;
2767 if (ret2 < 0)
2768 ret = ret2;
2769 ret2 = ext4_journal_stop(handle);
2770 if (!ret)
2771 ret = ret2;
2772
2773 return ret ? ret : copied;
2774}
2775
2776static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2777{
Alex Tomas64769242008-07-11 19:27:31 -04002778 /*
2779 * Drop reserved blocks
2780 */
2781 BUG_ON(!PageLocked(page));
2782 if (!page_has_buffers(page))
2783 goto out;
2784
Mingming Caod2a17632008-07-14 17:52:37 -04002785 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04002786
2787out:
2788 ext4_invalidatepage(page, offset);
2789
2790 return;
2791}
2792
2793
2794/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795 * bmap() is special. It gets used by applications such as lilo and by
2796 * the swapper to find the on-disk block of a specific piece of data.
2797 *
2798 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002799 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002800 * filesystem and enables swap, then they may get a nasty shock when the
2801 * data getting swapped to that swapfile suddenly gets overwritten by
2802 * the original zero's written out previously to the journal and
2803 * awaiting writeback in the kernel's buffer cache.
2804 *
2805 * So, if we see any bmap calls here on a modified, data-journaled file,
2806 * take extra steps to flush any blocks which might be in the cache.
2807 */
Mingming Cao617ba132006-10-11 01:20:53 -07002808static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002809{
2810 struct inode *inode = mapping->host;
2811 journal_t *journal;
2812 int err;
2813
Alex Tomas64769242008-07-11 19:27:31 -04002814 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2815 test_opt(inode->i_sb, DELALLOC)) {
2816 /*
2817 * With delalloc we want to sync the file
2818 * so that we can make sure we allocate
2819 * blocks for file
2820 */
2821 filemap_write_and_wait(mapping);
2822 }
2823
Frank Mayhar03901312009-01-07 00:06:22 -05002824 BUG_ON(!EXT4_JOURNAL(inode) &&
2825 EXT4_I(inode)->i_state & EXT4_STATE_JDATA);
2826
2827 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002828 /*
2829 * This is a REALLY heavyweight approach, but the use of
2830 * bmap on dirty files is expected to be extremely rare:
2831 * only if we run lilo or swapon on a freshly made file
2832 * do we expect this to happen.
2833 *
2834 * (bmap requires CAP_SYS_RAWIO so this does not
2835 * represent an unprivileged user DOS attack --- we'd be
2836 * in trouble if mortal users could trigger this path at
2837 * will.)
2838 *
Mingming Cao617ba132006-10-11 01:20:53 -07002839 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002840 * regular files. If somebody wants to bmap a directory
2841 * or symlink and gets confused because the buffer
2842 * hasn't yet been flushed to disk, they deserve
2843 * everything they get.
2844 */
2845
Mingming Cao617ba132006-10-11 01:20:53 -07002846 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
2847 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002848 jbd2_journal_lock_updates(journal);
2849 err = jbd2_journal_flush(journal);
2850 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851
2852 if (err)
2853 return 0;
2854 }
2855
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002856 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857}
2858
2859static int bget_one(handle_t *handle, struct buffer_head *bh)
2860{
2861 get_bh(bh);
2862 return 0;
2863}
2864
2865static int bput_one(handle_t *handle, struct buffer_head *bh)
2866{
2867 put_bh(bh);
2868 return 0;
2869}
2870
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002871/*
Jan Kara678aaf42008-07-11 19:27:31 -04002872 * Note that we don't need to start a transaction unless we're journaling data
2873 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2874 * need to file the inode to the transaction's list in ordered mode because if
2875 * we are writing back data added by write(), the inode is already there and if
2876 * we are writing back data modified via mmap(), noone guarantees in which
2877 * transaction the data will hit the disk. In case we are journaling data, we
2878 * cannot start transaction directly because transaction start ranks above page
2879 * lock so we have to do some magic.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002880 *
Jan Kara678aaf42008-07-11 19:27:31 -04002881 * In all journaling modes block_write_full_page() will start the I/O.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002882 *
2883 * Problem:
2884 *
Mingming Cao617ba132006-10-11 01:20:53 -07002885 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2886 * ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002887 *
2888 * Similar for:
2889 *
Mingming Cao617ba132006-10-11 01:20:53 -07002890 * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ...
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 *
Mingming Cao617ba132006-10-11 01:20:53 -07002892 * Same applies to ext4_get_block(). We will deadlock on various things like
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002893 * lock_journal and i_data_sem
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894 *
2895 * Setting PF_MEMALLOC here doesn't work - too many internal memory
2896 * allocations fail.
2897 *
2898 * 16May01: If we're reentered then journal_current_handle() will be
2899 * non-zero. We simply *return*.
2900 *
2901 * 1 July 2001: @@@ FIXME:
2902 * In journalled data mode, a data buffer may be metadata against the
2903 * current transaction. But the same file is part of a shared mapping
2904 * and someone does a writepage() on it.
2905 *
2906 * We will move the buffer onto the async_data list, but *after* it has
2907 * been dirtied. So there's a small window where we have dirty data on
2908 * BJ_Metadata.
2909 *
2910 * Note that this only applies to the last partial page in the file. The
2911 * bit which block_write_full_page() uses prepare/commit for. (That's
2912 * broken code anyway: it's wrong for msync()).
2913 *
2914 * It's a rare case: affects the final partial page, for journalled data
2915 * where the file is subject to bith write() and writepage() in the same
2916 * transction. To fix it we'll need a custom block_write_full_page().
2917 * We'll probably need that anyway for journalling writepage() output.
2918 *
2919 * We don't honour synchronous mounts for writepage(). That would be
2920 * disastrous. Any write() or metadata operation will sync the fs for
2921 * us.
2922 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002923 */
Jan Kara678aaf42008-07-11 19:27:31 -04002924static int __ext4_normal_writepage(struct page *page,
Jan Karacf108bc2008-07-11 19:27:31 -04002925 struct writeback_control *wbc)
2926{
2927 struct inode *inode = page->mapping->host;
2928
2929 if (test_opt(inode->i_sb, NOBH))
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002930 return nobh_writepage(page,
2931 ext4_normal_get_block_write, wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002932 else
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002933 return block_write_full_page(page,
2934 ext4_normal_get_block_write,
2935 wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002936}
2937
Jan Kara678aaf42008-07-11 19:27:31 -04002938static int ext4_normal_writepage(struct page *page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939 struct writeback_control *wbc)
2940{
2941 struct inode *inode = page->mapping->host;
Jan Karacf108bc2008-07-11 19:27:31 -04002942 loff_t size = i_size_read(inode);
2943 loff_t len;
2944
Theodore Ts'oba80b102009-01-03 20:03:21 -05002945 trace_mark(ext4_normal_writepage,
2946 "dev %s ino %lu page_index %lu",
2947 inode->i_sb->s_id, inode->i_ino, page->index);
Jan Karacf108bc2008-07-11 19:27:31 -04002948 J_ASSERT(PageLocked(page));
Jan Karacf108bc2008-07-11 19:27:31 -04002949 if (page->index == size >> PAGE_CACHE_SHIFT)
2950 len = size & ~PAGE_CACHE_MASK;
2951 else
2952 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002953
2954 if (page_has_buffers(page)) {
2955 /* if page has buffers it should all be mapped
2956 * and allocated. If there are not buffers attached
2957 * to the page we know the page is dirty but it lost
2958 * buffers. That means that at some moment in time
2959 * after write_begin() / write_end() has been called
2960 * all buffers have been clean and thus they must have been
2961 * written at least once. So they are all mapped and we can
2962 * happily proceed with mapping them and writing the page.
2963 */
2964 BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
2965 ext4_bh_unmapped_or_delay));
2966 }
Jan Karacf108bc2008-07-11 19:27:31 -04002967
2968 if (!ext4_journal_current_handle())
Jan Kara678aaf42008-07-11 19:27:31 -04002969 return __ext4_normal_writepage(page, wbc);
Jan Karacf108bc2008-07-11 19:27:31 -04002970
2971 redirty_page_for_writepage(wbc, page);
2972 unlock_page(page);
2973 return 0;
2974}
2975
2976static int __ext4_journalled_writepage(struct page *page,
2977 struct writeback_control *wbc)
2978{
2979 struct address_space *mapping = page->mapping;
2980 struct inode *inode = mapping->host;
2981 struct buffer_head *page_bufs;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002982 handle_t *handle = NULL;
2983 int ret = 0;
2984 int err;
2985
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002986 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2987 ext4_normal_get_block_write);
Jan Karacf108bc2008-07-11 19:27:31 -04002988 if (ret != 0)
2989 goto out_unlock;
2990
2991 page_bufs = page_buffers(page);
2992 walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL,
2993 bget_one);
2994 /* As soon as we unlock the page, it can go away, but we have
2995 * references to buffers so we are safe */
2996 unlock_page(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002997
Mingming Cao617ba132006-10-11 01:20:53 -07002998 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002999 if (IS_ERR(handle)) {
3000 ret = PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04003001 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002 }
3003
Jan Karacf108bc2008-07-11 19:27:31 -04003004 ret = walk_page_buffers(handle, page_bufs, 0,
3005 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003006
Jan Karacf108bc2008-07-11 19:27:31 -04003007 err = walk_page_buffers(handle, page_bufs, 0,
3008 PAGE_CACHE_SIZE, NULL, write_end_fn);
3009 if (ret == 0)
3010 ret = err;
Mingming Cao617ba132006-10-11 01:20:53 -07003011 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 if (!ret)
3013 ret = err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003014
Jan Karacf108bc2008-07-11 19:27:31 -04003015 walk_page_buffers(handle, page_bufs, 0,
3016 PAGE_CACHE_SIZE, NULL, bput_one);
3017 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
3018 goto out;
3019
3020out_unlock:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003022out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003023 return ret;
3024}
3025
Mingming Cao617ba132006-10-11 01:20:53 -07003026static int ext4_journalled_writepage(struct page *page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027 struct writeback_control *wbc)
3028{
3029 struct inode *inode = page->mapping->host;
Jan Karacf108bc2008-07-11 19:27:31 -04003030 loff_t size = i_size_read(inode);
3031 loff_t len;
3032
Theodore Ts'oba80b102009-01-03 20:03:21 -05003033 trace_mark(ext4_journalled_writepage,
3034 "dev %s ino %lu page_index %lu",
3035 inode->i_sb->s_id, inode->i_ino, page->index);
Jan Karacf108bc2008-07-11 19:27:31 -04003036 J_ASSERT(PageLocked(page));
Jan Karacf108bc2008-07-11 19:27:31 -04003037 if (page->index == size >> PAGE_CACHE_SHIFT)
3038 len = size & ~PAGE_CACHE_MASK;
3039 else
3040 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04003041
3042 if (page_has_buffers(page)) {
3043 /* if page has buffers it should all be mapped
3044 * and allocated. If there are not buffers attached
3045 * to the page we know the page is dirty but it lost
3046 * buffers. That means that at some moment in time
3047 * after write_begin() / write_end() has been called
3048 * all buffers have been clean and thus they must have been
3049 * written at least once. So they are all mapped and we can
3050 * happily proceed with mapping them and writing the page.
3051 */
3052 BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
3053 ext4_bh_unmapped_or_delay));
3054 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003055
Mingming Cao617ba132006-10-11 01:20:53 -07003056 if (ext4_journal_current_handle())
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003057 goto no_write;
3058
Jan Karacf108bc2008-07-11 19:27:31 -04003059 if (PageChecked(page)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003060 /*
3061 * It's mmapped pagecache. Add buffers and journal it. There
3062 * doesn't seem much point in redirtying the page here.
3063 */
3064 ClearPageChecked(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003065 return __ext4_journalled_writepage(page, wbc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066 } else {
3067 /*
3068 * It may be a page full of checkpoint-mode buffers. We don't
3069 * really know unless we go poke around in the buffer_heads.
3070 * But block_write_full_page will do the right thing.
3071 */
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04003072 return block_write_full_page(page,
3073 ext4_normal_get_block_write,
3074 wbc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003076no_write:
3077 redirty_page_for_writepage(wbc, page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003078 unlock_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04003079 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003080}
3081
Mingming Cao617ba132006-10-11 01:20:53 -07003082static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003083{
Mingming Cao617ba132006-10-11 01:20:53 -07003084 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085}
3086
3087static int
Mingming Cao617ba132006-10-11 01:20:53 -07003088ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003089 struct list_head *pages, unsigned nr_pages)
3090{
Mingming Cao617ba132006-10-11 01:20:53 -07003091 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003092}
3093
Mingming Cao617ba132006-10-11 01:20:53 -07003094static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095{
Mingming Cao617ba132006-10-11 01:20:53 -07003096 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003097
3098 /*
3099 * If it's a full truncate we just forget about the pending dirtying
3100 */
3101 if (offset == 0)
3102 ClearPageChecked(page);
3103
Frank Mayhar03901312009-01-07 00:06:22 -05003104 if (journal)
3105 jbd2_journal_invalidatepage(journal, page, offset);
3106 else
3107 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003108}
3109
Mingming Cao617ba132006-10-11 01:20:53 -07003110static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003111{
Mingming Cao617ba132006-10-11 01:20:53 -07003112 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003113
3114 WARN_ON(PageChecked(page));
3115 if (!page_has_buffers(page))
3116 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003117 if (journal)
3118 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3119 else
3120 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003121}
3122
3123/*
3124 * If the O_DIRECT write will extend the file then add this inode to the
3125 * orphan list. So recovery will truncate it back to the original size
3126 * if the machine crashes during the write.
3127 *
3128 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003129 * crashes then stale disk data _may_ be exposed inside the file. But current
3130 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003131 */
Mingming Cao617ba132006-10-11 01:20:53 -07003132static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003133 const struct iovec *iov, loff_t offset,
3134 unsigned long nr_segs)
3135{
3136 struct file *file = iocb->ki_filp;
3137 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003138 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003139 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003140 ssize_t ret;
3141 int orphan = 0;
3142 size_t count = iov_length(iov, nr_segs);
3143
3144 if (rw == WRITE) {
3145 loff_t final_size = offset + count;
3146
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003147 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003148 /* Credits for sb + inode write */
3149 handle = ext4_journal_start(inode, 2);
3150 if (IS_ERR(handle)) {
3151 ret = PTR_ERR(handle);
3152 goto out;
3153 }
Mingming Cao617ba132006-10-11 01:20:53 -07003154 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003155 if (ret) {
3156 ext4_journal_stop(handle);
3157 goto out;
3158 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159 orphan = 1;
3160 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003161 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003162 }
3163 }
3164
3165 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3166 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003167 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003168
Jan Kara7fb54092008-02-10 01:08:38 -05003169 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003170 int err;
3171
Jan Kara7fb54092008-02-10 01:08:38 -05003172 /* Credits for sb + inode write */
3173 handle = ext4_journal_start(inode, 2);
3174 if (IS_ERR(handle)) {
3175 /* This is really bad luck. We've written the data
3176 * but cannot extend i_size. Bail out and pretend
3177 * the write failed... */
3178 ret = PTR_ERR(handle);
3179 goto out;
3180 }
3181 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003182 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003183 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184 loff_t end = offset + ret;
3185 if (end > inode->i_size) {
3186 ei->i_disksize = end;
3187 i_size_write(inode, end);
3188 /*
3189 * We're going to return a positive `ret'
3190 * here due to non-zero-length I/O, so there's
3191 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003192 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003193 * ignore it.
3194 */
Mingming Cao617ba132006-10-11 01:20:53 -07003195 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003196 }
3197 }
Mingming Cao617ba132006-10-11 01:20:53 -07003198 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003199 if (ret == 0)
3200 ret = err;
3201 }
3202out:
3203 return ret;
3204}
3205
3206/*
Mingming Cao617ba132006-10-11 01:20:53 -07003207 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003208 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3209 * much here because ->set_page_dirty is called under VFS locks. The page is
3210 * not necessarily locked.
3211 *
3212 * We cannot just dirty the page and leave attached buffers clean, because the
3213 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3214 * or jbddirty because all the journalling code will explode.
3215 *
3216 * So what we do is to mark the page "pending dirty" and next time writepage
3217 * is called, propagate that into the buffers appropriately.
3218 */
Mingming Cao617ba132006-10-11 01:20:53 -07003219static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220{
3221 SetPageChecked(page);
3222 return __set_page_dirty_nobuffers(page);
3223}
3224
Mingming Cao617ba132006-10-11 01:20:53 -07003225static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003226 .readpage = ext4_readpage,
3227 .readpages = ext4_readpages,
3228 .writepage = ext4_normal_writepage,
3229 .sync_page = block_sync_page,
3230 .write_begin = ext4_write_begin,
3231 .write_end = ext4_ordered_write_end,
3232 .bmap = ext4_bmap,
3233 .invalidatepage = ext4_invalidatepage,
3234 .releasepage = ext4_releasepage,
3235 .direct_IO = ext4_direct_IO,
3236 .migratepage = buffer_migrate_page,
3237 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003238};
3239
Mingming Cao617ba132006-10-11 01:20:53 -07003240static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003241 .readpage = ext4_readpage,
3242 .readpages = ext4_readpages,
3243 .writepage = ext4_normal_writepage,
3244 .sync_page = block_sync_page,
3245 .write_begin = ext4_write_begin,
3246 .write_end = ext4_writeback_write_end,
3247 .bmap = ext4_bmap,
3248 .invalidatepage = ext4_invalidatepage,
3249 .releasepage = ext4_releasepage,
3250 .direct_IO = ext4_direct_IO,
3251 .migratepage = buffer_migrate_page,
3252 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003253};
3254
Mingming Cao617ba132006-10-11 01:20:53 -07003255static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003256 .readpage = ext4_readpage,
3257 .readpages = ext4_readpages,
3258 .writepage = ext4_journalled_writepage,
3259 .sync_page = block_sync_page,
3260 .write_begin = ext4_write_begin,
3261 .write_end = ext4_journalled_write_end,
3262 .set_page_dirty = ext4_journalled_set_page_dirty,
3263 .bmap = ext4_bmap,
3264 .invalidatepage = ext4_invalidatepage,
3265 .releasepage = ext4_releasepage,
3266 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003267};
3268
Alex Tomas64769242008-07-11 19:27:31 -04003269static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003270 .readpage = ext4_readpage,
3271 .readpages = ext4_readpages,
3272 .writepage = ext4_da_writepage,
3273 .writepages = ext4_da_writepages,
3274 .sync_page = block_sync_page,
3275 .write_begin = ext4_da_write_begin,
3276 .write_end = ext4_da_write_end,
3277 .bmap = ext4_bmap,
3278 .invalidatepage = ext4_da_invalidatepage,
3279 .releasepage = ext4_releasepage,
3280 .direct_IO = ext4_direct_IO,
3281 .migratepage = buffer_migrate_page,
3282 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003283};
3284
Mingming Cao617ba132006-10-11 01:20:53 -07003285void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003286{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003287 if (ext4_should_order_data(inode) &&
3288 test_opt(inode->i_sb, DELALLOC))
3289 inode->i_mapping->a_ops = &ext4_da_aops;
3290 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003291 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003292 else if (ext4_should_writeback_data(inode) &&
3293 test_opt(inode->i_sb, DELALLOC))
3294 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003295 else if (ext4_should_writeback_data(inode))
3296 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003297 else
Mingming Cao617ba132006-10-11 01:20:53 -07003298 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003299}
3300
3301/*
Mingming Cao617ba132006-10-11 01:20:53 -07003302 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303 * up to the end of the block which corresponds to `from'.
3304 * This required during truncate. We need to physically zero the tail end
3305 * of that block so it doesn't yield old data if the file is later grown.
3306 */
Jan Karacf108bc2008-07-11 19:27:31 -04003307int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308 struct address_space *mapping, loff_t from)
3309{
Mingming Cao617ba132006-10-11 01:20:53 -07003310 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003311 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003312 unsigned blocksize, length, pos;
3313 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003314 struct inode *inode = mapping->host;
3315 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003316 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003317 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318
Jan Karacf108bc2008-07-11 19:27:31 -04003319 page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT);
3320 if (!page)
3321 return -EINVAL;
3322
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003323 blocksize = inode->i_sb->s_blocksize;
3324 length = blocksize - (offset & (blocksize - 1));
3325 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3326
3327 /*
3328 * For "nobh" option, we can only work if we don't need to
3329 * read-in the page - otherwise we create buffers to do the IO.
3330 */
3331 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003332 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003333 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334 set_page_dirty(page);
3335 goto unlock;
3336 }
3337
3338 if (!page_has_buffers(page))
3339 create_empty_buffers(page, blocksize, 0);
3340
3341 /* Find the buffer that contains "offset" */
3342 bh = page_buffers(page);
3343 pos = blocksize;
3344 while (offset >= pos) {
3345 bh = bh->b_this_page;
3346 iblock++;
3347 pos += blocksize;
3348 }
3349
3350 err = 0;
3351 if (buffer_freed(bh)) {
3352 BUFFER_TRACE(bh, "freed: skip");
3353 goto unlock;
3354 }
3355
3356 if (!buffer_mapped(bh)) {
3357 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003358 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003359 /* unmapped? It's a hole - nothing to do */
3360 if (!buffer_mapped(bh)) {
3361 BUFFER_TRACE(bh, "still unmapped");
3362 goto unlock;
3363 }
3364 }
3365
3366 /* Ok, it's mapped. Make sure it's up-to-date */
3367 if (PageUptodate(page))
3368 set_buffer_uptodate(bh);
3369
3370 if (!buffer_uptodate(bh)) {
3371 err = -EIO;
3372 ll_rw_block(READ, 1, &bh);
3373 wait_on_buffer(bh);
3374 /* Uhhuh. Read error. Complain and punt. */
3375 if (!buffer_uptodate(bh))
3376 goto unlock;
3377 }
3378
Mingming Cao617ba132006-10-11 01:20:53 -07003379 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003380 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003381 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003382 if (err)
3383 goto unlock;
3384 }
3385
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003386 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003387
3388 BUFFER_TRACE(bh, "zeroed end of block");
3389
3390 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003391 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003392 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003393 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003394 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003395 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003396 mark_buffer_dirty(bh);
3397 }
3398
3399unlock:
3400 unlock_page(page);
3401 page_cache_release(page);
3402 return err;
3403}
3404
3405/*
3406 * Probably it should be a library function... search for first non-zero word
3407 * or memcmp with zero_page, whatever is better for particular architecture.
3408 * Linus?
3409 */
3410static inline int all_zeroes(__le32 *p, __le32 *q)
3411{
3412 while (p < q)
3413 if (*p++)
3414 return 0;
3415 return 1;
3416}
3417
3418/**
Mingming Cao617ba132006-10-11 01:20:53 -07003419 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003420 * @inode: inode in question
3421 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003422 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003423 * @chain: place to store the pointers to partial indirect blocks
3424 * @top: place to the (detached) top of branch
3425 *
Mingming Cao617ba132006-10-11 01:20:53 -07003426 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003427 *
3428 * When we do truncate() we may have to clean the ends of several
3429 * indirect blocks but leave the blocks themselves alive. Block is
3430 * partially truncated if some data below the new i_size is refered
3431 * from it (and it is on the path to the first completely truncated
3432 * data block, indeed). We have to free the top of that path along
3433 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003434 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003435 * finishes, we may safely do the latter, but top of branch may
3436 * require special attention - pageout below the truncation point
3437 * might try to populate it.
3438 *
3439 * We atomically detach the top of branch from the tree, store the
3440 * block number of its root in *@top, pointers to buffer_heads of
3441 * partially truncated blocks - in @chain[].bh and pointers to
3442 * their last elements that should not be removed - in
3443 * @chain[].p. Return value is the pointer to last filled element
3444 * of @chain.
3445 *
3446 * The work left to caller to do the actual freeing of subtrees:
3447 * a) free the subtree starting from *@top
3448 * b) free the subtrees whose roots are stored in
3449 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3450 * c) free the subtrees growing from the inode past the @chain[0].
3451 * (no partially truncated stuff there). */
3452
Mingming Cao617ba132006-10-11 01:20:53 -07003453static Indirect *ext4_find_shared(struct inode *inode, int depth,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003454 ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003455{
3456 Indirect *partial, *p;
3457 int k, err;
3458
3459 *top = 0;
3460 /* Make k index the deepest non-null offest + 1 */
3461 for (k = depth; k > 1 && !offsets[k-1]; k--)
3462 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003463 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464 /* Writer: pointers */
3465 if (!partial)
3466 partial = chain + k-1;
3467 /*
3468 * If the branch acquired continuation since we've looked at it -
3469 * fine, it should all survive and (new) top doesn't belong to us.
3470 */
3471 if (!partial->key && *partial->p)
3472 /* Writer: end */
3473 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003474 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003475 ;
3476 /*
3477 * OK, we've found the last block that must survive. The rest of our
3478 * branch should be detached before unlocking. However, if that rest
3479 * of branch is all ours and does not grow immediately from the inode
3480 * it's easier to cheat and just decrement partial->p.
3481 */
3482 if (p == chain + k - 1 && p > chain) {
3483 p->p--;
3484 } else {
3485 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003486 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003487#if 0
3488 *p->p = 0;
3489#endif
3490 }
3491 /* Writer: end */
3492
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003493 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003494 brelse(partial->bh);
3495 partial--;
3496 }
3497no_top:
3498 return partial;
3499}
3500
3501/*
3502 * Zero a number of block pointers in either an inode or an indirect block.
3503 * If we restart the transaction we must again get write access to the
3504 * indirect block for further modification.
3505 *
3506 * We release `count' blocks on disk, but (last - first) may be greater
3507 * than `count' because there can be holes in there.
3508 */
Mingming Cao617ba132006-10-11 01:20:53 -07003509static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
3510 struct buffer_head *bh, ext4_fsblk_t block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003511 unsigned long count, __le32 *first, __le32 *last)
3512{
3513 __le32 *p;
3514 if (try_to_extend_transaction(handle, inode)) {
3515 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003516 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3517 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003518 }
Mingming Cao617ba132006-10-11 01:20:53 -07003519 ext4_mark_inode_dirty(handle, inode);
3520 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003521 if (bh) {
3522 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003523 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003524 }
3525 }
3526
3527 /*
3528 * Any buffers which are on the journal will be in memory. We find
Mingming Caodab291a2006-10-11 01:21:01 -07003529 * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003530 * on them. We've already detached each block from the file, so
Mingming Caodab291a2006-10-11 01:21:01 -07003531 * bforget() in jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003532 *
Mingming Caodab291a2006-10-11 01:21:01 -07003533 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534 */
3535 for (p = first; p < last; p++) {
3536 u32 nr = le32_to_cpu(*p);
3537 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003538 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003539
3540 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003541 tbh = sb_find_get_block(inode->i_sb, nr);
3542 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003543 }
3544 }
3545
Alex Tomasc9de5602008-01-29 00:19:52 -05003546 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003547}
3548
3549/**
Mingming Cao617ba132006-10-11 01:20:53 -07003550 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003551 * @handle: handle for this transaction
3552 * @inode: inode we are dealing with
3553 * @this_bh: indirect buffer_head which contains *@first and *@last
3554 * @first: array of block numbers
3555 * @last: points immediately past the end of array
3556 *
3557 * We are freeing all blocks refered from that array (numbers are stored as
3558 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3559 *
3560 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3561 * blocks are contiguous then releasing them at one time will only affect one
3562 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3563 * actually use a lot of journal space.
3564 *
3565 * @this_bh will be %NULL if @first and @last point into the inode's direct
3566 * block pointers.
3567 */
Mingming Cao617ba132006-10-11 01:20:53 -07003568static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003569 struct buffer_head *this_bh,
3570 __le32 *first, __le32 *last)
3571{
Mingming Cao617ba132006-10-11 01:20:53 -07003572 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003573 unsigned long count = 0; /* Number of blocks in the run */
3574 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3575 corresponding to
3576 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003577 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003578 __le32 *p; /* Pointer into inode/ind
3579 for current block */
3580 int err;
3581
3582 if (this_bh) { /* For indirect block */
3583 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003584 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003585 /* Important: if we can't update the indirect pointers
3586 * to the blocks, we can't free them. */
3587 if (err)
3588 return;
3589 }
3590
3591 for (p = first; p < last; p++) {
3592 nr = le32_to_cpu(*p);
3593 if (nr) {
3594 /* accumulate blocks to free if they're contiguous */
3595 if (count == 0) {
3596 block_to_free = nr;
3597 block_to_free_p = p;
3598 count = 1;
3599 } else if (nr == block_to_free + count) {
3600 count++;
3601 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003602 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003603 block_to_free,
3604 count, block_to_free_p, p);
3605 block_to_free = nr;
3606 block_to_free_p = p;
3607 count = 1;
3608 }
3609 }
3610 }
3611
3612 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003613 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003614 count, block_to_free_p, p);
3615
3616 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003617 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003618
3619 /*
3620 * The buffer head should have an attached journal head at this
3621 * point. However, if the data is corrupted and an indirect
3622 * block pointed to itself, it would have been detached when
3623 * the block was cleared. Check for this instead of OOPSing.
3624 */
3625 if (bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003626 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003627 else
3628 ext4_error(inode->i_sb, __func__,
3629 "circular indirect block detected, "
3630 "inode=%lu, block=%llu",
3631 inode->i_ino,
3632 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003633 }
3634}
3635
3636/**
Mingming Cao617ba132006-10-11 01:20:53 -07003637 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003638 * @handle: JBD handle for this transaction
3639 * @inode: inode we are dealing with
3640 * @parent_bh: the buffer_head which contains *@first and *@last
3641 * @first: array of block numbers
3642 * @last: pointer immediately past the end of array
3643 * @depth: depth of the branches to free
3644 *
3645 * We are freeing all blocks refered from these branches (numbers are
3646 * stored as little-endian 32-bit) and updating @inode->i_blocks
3647 * appropriately.
3648 */
Mingming Cao617ba132006-10-11 01:20:53 -07003649static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003650 struct buffer_head *parent_bh,
3651 __le32 *first, __le32 *last, int depth)
3652{
Mingming Cao617ba132006-10-11 01:20:53 -07003653 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003654 __le32 *p;
3655
Frank Mayhar03901312009-01-07 00:06:22 -05003656 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003657 return;
3658
3659 if (depth--) {
3660 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003661 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003662 p = last;
3663 while (--p >= first) {
3664 nr = le32_to_cpu(*p);
3665 if (!nr)
3666 continue; /* A hole */
3667
3668 /* Go read the buffer for the next level down */
3669 bh = sb_bread(inode->i_sb, nr);
3670
3671 /*
3672 * A read failure? Report error and clear slot
3673 * (should be rare).
3674 */
3675 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003676 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003677 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003678 inode->i_ino, nr);
3679 continue;
3680 }
3681
3682 /* This zaps the entire block. Bottom up. */
3683 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003684 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003685 (__le32 *) bh->b_data,
3686 (__le32 *) bh->b_data + addr_per_block,
3687 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688
3689 /*
3690 * We've probably journalled the indirect block several
3691 * times during the truncate. But it's no longer
3692 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003693 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003694 *
3695 * That's easy if it's exclusively part of this
3696 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003697 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003698 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003699 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003700 * unmap_underlying_metadata() will find this block
3701 * and will try to get rid of it. damn, damn.
3702 *
3703 * If this block has already been committed to the
3704 * journal, a revoke record will be written. And
3705 * revoke records must be emitted *before* clearing
3706 * this block's bit in the bitmaps.
3707 */
Mingming Cao617ba132006-10-11 01:20:53 -07003708 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003709
3710 /*
3711 * Everything below this this pointer has been
3712 * released. Now let this top-of-subtree go.
3713 *
3714 * We want the freeing of this indirect block to be
3715 * atomic in the journal with the updating of the
3716 * bitmap block which owns it. So make some room in
3717 * the journal.
3718 *
3719 * We zero the parent pointer *after* freeing its
3720 * pointee in the bitmaps, so if extend_transaction()
3721 * for some reason fails to put the bitmap changes and
3722 * the release into the same transaction, recovery
3723 * will merely complain about releasing a free block,
3724 * rather than leaking blocks.
3725 */
Frank Mayhar03901312009-01-07 00:06:22 -05003726 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003727 return;
3728 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003729 ext4_mark_inode_dirty(handle, inode);
3730 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003731 }
3732
Alex Tomasc9de5602008-01-29 00:19:52 -05003733 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003734
3735 if (parent_bh) {
3736 /*
3737 * The block which we have just freed is
3738 * pointed to by an indirect block: journal it
3739 */
3740 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003741 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003742 parent_bh)){
3743 *p = 0;
3744 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003745 "call ext4_handle_dirty_metadata");
3746 ext4_handle_dirty_metadata(handle,
3747 inode,
3748 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003749 }
3750 }
3751 }
3752 } else {
3753 /* We have reached the bottom of the tree. */
3754 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003755 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003756 }
3757}
3758
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003759int ext4_can_truncate(struct inode *inode)
3760{
3761 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3762 return 0;
3763 if (S_ISREG(inode->i_mode))
3764 return 1;
3765 if (S_ISDIR(inode->i_mode))
3766 return 1;
3767 if (S_ISLNK(inode->i_mode))
3768 return !ext4_inode_is_fast_symlink(inode);
3769 return 0;
3770}
3771
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003772/*
Mingming Cao617ba132006-10-11 01:20:53 -07003773 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003774 *
Mingming Cao617ba132006-10-11 01:20:53 -07003775 * We block out ext4_get_block() block instantiations across the entire
3776 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003777 * simultaneously on behalf of the same inode.
3778 *
3779 * As we work through the truncate and commmit bits of it to the journal there
3780 * is one core, guiding principle: the file's tree must always be consistent on
3781 * disk. We must be able to restart the truncate after a crash.
3782 *
3783 * The file's tree may be transiently inconsistent in memory (although it
3784 * probably isn't), but whenever we close off and commit a journal transaction,
3785 * the contents of (the filesystem + the journal) must be consistent and
3786 * restartable. It's pretty simple, really: bottom up, right to left (although
3787 * left-to-right works OK too).
3788 *
3789 * Note that at recovery time, journal replay occurs *before* the restart of
3790 * truncate against the orphan inode list.
3791 *
3792 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003793 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003794 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003795 * ext4_truncate() to have another go. So there will be instantiated blocks
3796 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003797 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003798 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003799 */
Mingming Cao617ba132006-10-11 01:20:53 -07003800void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003801{
3802 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003803 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003804 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003805 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003806 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003807 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003808 Indirect chain[4];
3809 Indirect *partial;
3810 __le32 nr = 0;
3811 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003812 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003813 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003814
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003815 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003816 return;
3817
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003818 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003819 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003820 return;
3821 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003822
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003823 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003824 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003825 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003826
3827 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003828 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003829
Jan Karacf108bc2008-07-11 19:27:31 -04003830 if (inode->i_size & (blocksize - 1))
3831 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3832 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003833
Mingming Cao617ba132006-10-11 01:20:53 -07003834 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003835 if (n == 0)
3836 goto out_stop; /* error */
3837
3838 /*
3839 * OK. This truncate is going to happen. We add the inode to the
3840 * orphan list, so that if this truncate spans multiple transactions,
3841 * and we crash, we will resume the truncate when the filesystem
3842 * recovers. It also marks the inode dirty, to catch the new size.
3843 *
3844 * Implication: the file must always be in a sane, consistent
3845 * truncatable state while each transaction commits.
3846 */
Mingming Cao617ba132006-10-11 01:20:53 -07003847 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003848 goto out_stop;
3849
3850 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04003851 * From here we block out all ext4_get_block() callers who want to
3852 * modify the block allocation tree.
3853 */
3854 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04003855
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003856 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04003857
Mingming Cao632eaea2008-07-11 19:27:31 -04003858 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003859 * The orphan list entry will now protect us from any crash which
3860 * occurs before the truncate completes, so it is now safe to propagate
3861 * the new, shorter inode size (held for now in i_size) into the
3862 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07003863 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003864 */
3865 ei->i_disksize = inode->i_size;
3866
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003867 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003868 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
3869 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003870 goto do_indirects;
3871 }
3872
Mingming Cao617ba132006-10-11 01:20:53 -07003873 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003874 /* Kill the top of shared branch (not detached) */
3875 if (nr) {
3876 if (partial == chain) {
3877 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07003878 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879 &nr, &nr+1, (chain+n-1) - partial);
3880 *partial->p = 0;
3881 /*
3882 * We mark the inode dirty prior to restart,
3883 * and prior to stop. No need for it here.
3884 */
3885 } else {
3886 /* Shared branch grows from an indirect block */
3887 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003888 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003889 partial->p,
3890 partial->p+1, (chain+n-1) - partial);
3891 }
3892 }
3893 /* Clear the ends of indirect blocks on the shared branch */
3894 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07003895 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003896 (__le32*)partial->bh->b_data+addr_per_block,
3897 (chain+n-1) - partial);
3898 BUFFER_TRACE(partial->bh, "call brelse");
3899 brelse (partial->bh);
3900 partial--;
3901 }
3902do_indirects:
3903 /* Kill the remaining (whole) subtrees */
3904 switch (offsets[0]) {
3905 default:
Mingming Cao617ba132006-10-11 01:20:53 -07003906 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003907 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003908 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
3909 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003910 }
Mingming Cao617ba132006-10-11 01:20:53 -07003911 case EXT4_IND_BLOCK:
3912 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003913 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003914 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
3915 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003916 }
Mingming Cao617ba132006-10-11 01:20:53 -07003917 case EXT4_DIND_BLOCK:
3918 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003919 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07003920 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
3921 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003922 }
Mingming Cao617ba132006-10-11 01:20:53 -07003923 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003924 ;
3925 }
3926
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003927 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003928 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003929 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003930
3931 /*
3932 * In a multi-transaction truncate, we only make the final transaction
3933 * synchronous
3934 */
3935 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003936 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003937out_stop:
3938 /*
3939 * If this was a simple ftruncate(), and the file will remain alive
3940 * then we need to clear up the orphan record which we created above.
3941 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07003942 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003943 * orphan info for us.
3944 */
3945 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003946 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003947
Mingming Cao617ba132006-10-11 01:20:53 -07003948 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003949}
3950
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003951/*
Mingming Cao617ba132006-10-11 01:20:53 -07003952 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003953 * underlying buffer_head on success. If 'in_mem' is true, we have all
3954 * data in memory that is needed to recreate the on-disk version of this
3955 * inode.
3956 */
Mingming Cao617ba132006-10-11 01:20:53 -07003957static int __ext4_get_inode_loc(struct inode *inode,
3958 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003959{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003960 struct ext4_group_desc *gdp;
3961 struct buffer_head *bh;
3962 struct super_block *sb = inode->i_sb;
3963 ext4_fsblk_t block;
3964 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003965
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003966 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003967 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003968 return -EIO;
3969
Theodore Ts'o240799c2008-10-09 23:53:47 -04003970 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3971 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3972 if (!gdp)
3973 return -EIO;
3974
3975 /*
3976 * Figure out the offset within the block group inode table
3977 */
3978 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
3979 inode_offset = ((inode->i_ino - 1) %
3980 EXT4_INODES_PER_GROUP(sb));
3981 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3982 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3983
3984 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003985 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04003986 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
3987 "inode block - inode=%lu, block=%llu",
3988 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003989 return -EIO;
3990 }
3991 if (!buffer_uptodate(bh)) {
3992 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003993
3994 /*
3995 * If the buffer has the write error flag, we have failed
3996 * to write out another inode in the same block. In this
3997 * case, we don't have to read the block because we may
3998 * read the old inode data successfully.
3999 */
4000 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4001 set_buffer_uptodate(bh);
4002
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004003 if (buffer_uptodate(bh)) {
4004 /* someone brought it uptodate while we waited */
4005 unlock_buffer(bh);
4006 goto has_buffer;
4007 }
4008
4009 /*
4010 * If we have all information of the inode in memory and this
4011 * is the only valid inode in the block, we need not read the
4012 * block.
4013 */
4014 if (in_mem) {
4015 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004016 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004017
Theodore Ts'o240799c2008-10-09 23:53:47 -04004018 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004019
4020 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004021 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004022 if (!bitmap_bh)
4023 goto make_io;
4024
4025 /*
4026 * If the inode bitmap isn't in cache then the
4027 * optimisation may end up performing two reads instead
4028 * of one, so skip it.
4029 */
4030 if (!buffer_uptodate(bitmap_bh)) {
4031 brelse(bitmap_bh);
4032 goto make_io;
4033 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004034 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004035 if (i == inode_offset)
4036 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004037 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004038 break;
4039 }
4040 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004041 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004042 /* all other inodes are free, so skip I/O */
4043 memset(bh->b_data, 0, bh->b_size);
4044 set_buffer_uptodate(bh);
4045 unlock_buffer(bh);
4046 goto has_buffer;
4047 }
4048 }
4049
4050make_io:
4051 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004052 * If we need to do any I/O, try to pre-readahead extra
4053 * blocks from the inode table.
4054 */
4055 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4056 ext4_fsblk_t b, end, table;
4057 unsigned num;
4058
4059 table = ext4_inode_table(sb, gdp);
4060 /* Make sure s_inode_readahead_blks is a power of 2 */
4061 while (EXT4_SB(sb)->s_inode_readahead_blks &
4062 (EXT4_SB(sb)->s_inode_readahead_blks-1))
4063 EXT4_SB(sb)->s_inode_readahead_blks =
4064 (EXT4_SB(sb)->s_inode_readahead_blks &
4065 (EXT4_SB(sb)->s_inode_readahead_blks-1));
4066 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4067 if (table > b)
4068 b = table;
4069 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4070 num = EXT4_INODES_PER_GROUP(sb);
4071 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4072 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004073 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004074 table += num / inodes_per_block;
4075 if (end > table)
4076 end = table;
4077 while (b <= end)
4078 sb_breadahead(sb, b++);
4079 }
4080
4081 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004082 * There are other valid inodes in the buffer, this inode
4083 * has in-inode xattrs, or we don't have this inode in memory.
4084 * Read the block from disk.
4085 */
4086 get_bh(bh);
4087 bh->b_end_io = end_buffer_read_sync;
4088 submit_bh(READ_META, bh);
4089 wait_on_buffer(bh);
4090 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004091 ext4_error(sb, __func__,
4092 "unable to read inode block - inode=%lu, "
4093 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004094 brelse(bh);
4095 return -EIO;
4096 }
4097 }
4098has_buffer:
4099 iloc->bh = bh;
4100 return 0;
4101}
4102
Mingming Cao617ba132006-10-11 01:20:53 -07004103int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104{
4105 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004106 return __ext4_get_inode_loc(inode, iloc,
4107 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108}
4109
Mingming Cao617ba132006-10-11 01:20:53 -07004110void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004111{
Mingming Cao617ba132006-10-11 01:20:53 -07004112 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004113
4114 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004115 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004116 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004117 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004118 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004119 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004120 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004121 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004122 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004123 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004124 inode->i_flags |= S_DIRSYNC;
4125}
4126
Jan Karaff9ddf72007-07-18 09:24:20 -04004127/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4128void ext4_get_inode_flags(struct ext4_inode_info *ei)
4129{
4130 unsigned int flags = ei->vfs_inode.i_flags;
4131
4132 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4133 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4134 if (flags & S_SYNC)
4135 ei->i_flags |= EXT4_SYNC_FL;
4136 if (flags & S_APPEND)
4137 ei->i_flags |= EXT4_APPEND_FL;
4138 if (flags & S_IMMUTABLE)
4139 ei->i_flags |= EXT4_IMMUTABLE_FL;
4140 if (flags & S_NOATIME)
4141 ei->i_flags |= EXT4_NOATIME_FL;
4142 if (flags & S_DIRSYNC)
4143 ei->i_flags |= EXT4_DIRSYNC_FL;
4144}
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004145static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
4146 struct ext4_inode_info *ei)
4147{
4148 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004149 struct inode *inode = &(ei->vfs_inode);
4150 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004151
4152 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4153 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4154 /* we are using combined 48 bit field */
4155 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4156 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004157 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4158 /* i_blocks represent file system block size */
4159 return i_blocks << (inode->i_blkbits - 9);
4160 } else {
4161 return i_blocks;
4162 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004163 } else {
4164 return le32_to_cpu(raw_inode->i_blocks_lo);
4165 }
4166}
Jan Karaff9ddf72007-07-18 09:24:20 -04004167
David Howells1d1fe1e2008-02-07 00:15:37 -08004168struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004169{
Mingming Cao617ba132006-10-11 01:20:53 -07004170 struct ext4_iloc iloc;
4171 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004172 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004173 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004174 struct inode *inode;
4175 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004176 int block;
4177
David Howells1d1fe1e2008-02-07 00:15:37 -08004178 inode = iget_locked(sb, ino);
4179 if (!inode)
4180 return ERR_PTR(-ENOMEM);
4181 if (!(inode->i_state & I_NEW))
4182 return inode;
4183
4184 ei = EXT4_I(inode);
Theodore Ts'o03010a32008-10-10 20:02:48 -04004185#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07004186 ei->i_acl = EXT4_ACL_NOT_CACHED;
4187 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004189
David Howells1d1fe1e2008-02-07 00:15:37 -08004190 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4191 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004192 goto bad_inode;
4193 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004194 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004195 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4196 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4197 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004198 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004199 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4200 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4201 }
4202 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004203
4204 ei->i_state = 0;
4205 ei->i_dir_start_lookup = 0;
4206 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4207 /* We now have enough fields to check if the inode was active or not.
4208 * This is needed because nfsd might try to access dead inodes
4209 * the test is that same one that e2fsck uses
4210 * NeilBrown 1999oct15
4211 */
4212 if (inode->i_nlink == 0) {
4213 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004214 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004215 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004216 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004217 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004218 goto bad_inode;
4219 }
4220 /* The only unlinked inodes we let through here have
4221 * valid i_mode and are being read by the orphan
4222 * recovery code: that's fine, we're about to complete
4223 * the process of deleting those. */
4224 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004225 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004226 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004227 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004228 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004229 cpu_to_le32(EXT4_OS_HURD)) {
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004230 ei->i_file_acl |=
4231 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004232 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004233 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004234 ei->i_disksize = inode->i_size;
4235 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4236 ei->i_block_group = iloc.block_group;
4237 /*
4238 * NOTE! The in-memory inode i_data array is in little-endian order
4239 * even on big-endian machines: we do NOT byteswap the block numbers!
4240 */
Mingming Cao617ba132006-10-11 01:20:53 -07004241 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004242 ei->i_data[block] = raw_inode->i_block[block];
4243 INIT_LIST_HEAD(&ei->i_orphan);
4244
Eric Sandeen0040d982008-02-05 22:36:43 -05004245 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004246 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004247 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004248 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004249 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004250 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004251 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004252 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004253 if (ei->i_extra_isize == 0) {
4254 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004255 ei->i_extra_isize = sizeof(struct ext4_inode) -
4256 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004257 } else {
4258 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004259 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004260 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004261 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
4262 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004263 }
4264 } else
4265 ei->i_extra_isize = 0;
4266
Kalpak Shahef7f3832007-07-18 09:15:20 -04004267 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4268 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4269 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4270 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4271
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004272 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4273 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4274 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4275 inode->i_version |=
4276 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4277 }
4278
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004279 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004280 inode->i_op = &ext4_file_inode_operations;
4281 inode->i_fop = &ext4_file_operations;
4282 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004283 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004284 inode->i_op = &ext4_dir_inode_operations;
4285 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004286 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004287 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004288 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004289 nd_terminate_link(ei->i_data, inode->i_size,
4290 sizeof(ei->i_data) - 1);
4291 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004292 inode->i_op = &ext4_symlink_inode_operations;
4293 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004294 }
4295 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004296 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004297 if (raw_inode->i_block[0])
4298 init_special_inode(inode, inode->i_mode,
4299 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4300 else
4301 init_special_inode(inode, inode->i_mode,
4302 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4303 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004304 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004305 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004306 unlock_new_inode(inode);
4307 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004308
4309bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004310 iget_failed(inode);
4311 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004312}
4313
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004314static int ext4_inode_blocks_set(handle_t *handle,
4315 struct ext4_inode *raw_inode,
4316 struct ext4_inode_info *ei)
4317{
4318 struct inode *inode = &(ei->vfs_inode);
4319 u64 i_blocks = inode->i_blocks;
4320 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004321
4322 if (i_blocks <= ~0U) {
4323 /*
4324 * i_blocks can be represnted in a 32 bit variable
4325 * as multiple of 512 bytes
4326 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004327 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004328 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004329 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004330 return 0;
4331 }
4332 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4333 return -EFBIG;
4334
4335 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004336 /*
4337 * i_blocks can be represented in a 48 bit variable
4338 * as multiple of 512 bytes
4339 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004340 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004341 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004342 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004343 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004344 ei->i_flags |= EXT4_HUGE_FILE_FL;
4345 /* i_block is stored in file system block size */
4346 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4347 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4348 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004349 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004350 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004351}
4352
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004353/*
4354 * Post the struct inode info into an on-disk inode location in the
4355 * buffer-cache. This gobbles the caller's reference to the
4356 * buffer_head in the inode location struct.
4357 *
4358 * The caller must have write access to iloc->bh.
4359 */
Mingming Cao617ba132006-10-11 01:20:53 -07004360static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004361 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004362 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004363{
Mingming Cao617ba132006-10-11 01:20:53 -07004364 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4365 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004366 struct buffer_head *bh = iloc->bh;
4367 int err = 0, rc, block;
4368
4369 /* For fields not not tracking in the in-memory inode,
4370 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004371 if (ei->i_state & EXT4_STATE_NEW)
4372 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004373
Jan Karaff9ddf72007-07-18 09:24:20 -04004374 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004375 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004376 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004377 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4378 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4379/*
4380 * Fix up interoperability with old kernels. Otherwise, old inodes get
4381 * re-used with the upper 16 bits of the uid/gid intact
4382 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004383 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004384 raw_inode->i_uid_high =
4385 cpu_to_le16(high_16_bits(inode->i_uid));
4386 raw_inode->i_gid_high =
4387 cpu_to_le16(high_16_bits(inode->i_gid));
4388 } else {
4389 raw_inode->i_uid_high = 0;
4390 raw_inode->i_gid_high = 0;
4391 }
4392 } else {
4393 raw_inode->i_uid_low =
4394 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4395 raw_inode->i_gid_low =
4396 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4397 raw_inode->i_uid_high = 0;
4398 raw_inode->i_gid_high = 0;
4399 }
4400 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004401
4402 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4403 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4404 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4405 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4406
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004407 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4408 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004409 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004410 /* clear the migrate flag in the raw_inode */
4411 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004412 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4413 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004414 raw_inode->i_file_acl_high =
4415 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004416 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004417 ext4_isize_set(raw_inode, ei->i_disksize);
4418 if (ei->i_disksize > 0x7fffffffULL) {
4419 struct super_block *sb = inode->i_sb;
4420 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4421 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4422 EXT4_SB(sb)->s_es->s_rev_level ==
4423 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4424 /* If this is the first large file
4425 * created, add a flag to the superblock.
4426 */
4427 err = ext4_journal_get_write_access(handle,
4428 EXT4_SB(sb)->s_sbh);
4429 if (err)
4430 goto out_brelse;
4431 ext4_update_dynamic_rev(sb);
4432 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004433 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004434 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004435 ext4_handle_sync(handle);
4436 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004437 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004438 }
4439 }
4440 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4441 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4442 if (old_valid_dev(inode->i_rdev)) {
4443 raw_inode->i_block[0] =
4444 cpu_to_le32(old_encode_dev(inode->i_rdev));
4445 raw_inode->i_block[1] = 0;
4446 } else {
4447 raw_inode->i_block[0] = 0;
4448 raw_inode->i_block[1] =
4449 cpu_to_le32(new_encode_dev(inode->i_rdev));
4450 raw_inode->i_block[2] = 0;
4451 }
Mingming Cao617ba132006-10-11 01:20:53 -07004452 } else for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004453 raw_inode->i_block[block] = ei->i_data[block];
4454
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004455 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4456 if (ei->i_extra_isize) {
4457 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4458 raw_inode->i_version_hi =
4459 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004460 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004461 }
4462
Frank Mayhar03901312009-01-07 00:06:22 -05004463 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4464 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004465 if (!err)
4466 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004467 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004468
4469out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004470 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004471 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004472 return err;
4473}
4474
4475/*
Mingming Cao617ba132006-10-11 01:20:53 -07004476 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004477 *
4478 * We are called from a few places:
4479 *
4480 * - Within generic_file_write() for O_SYNC files.
4481 * Here, there will be no transaction running. We wait for any running
4482 * trasnaction to commit.
4483 *
4484 * - Within sys_sync(), kupdate and such.
4485 * We wait on commit, if tol to.
4486 *
4487 * - Within prune_icache() (PF_MEMALLOC == true)
4488 * Here we simply return. We can't afford to block kswapd on the
4489 * journal commit.
4490 *
4491 * In all cases it is actually safe for us to return without doing anything,
4492 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004493 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004494 * knfsd.
4495 *
4496 * Note that we are absolutely dependent upon all inode dirtiers doing the
4497 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4498 * which we are interested.
4499 *
4500 * It would be a bug for them to not do this. The code:
4501 *
4502 * mark_inode_dirty(inode)
4503 * stuff();
4504 * inode->i_size = expr;
4505 *
4506 * is in error because a kswapd-driven write_inode() could occur while
4507 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4508 * will no longer be on the superblock's dirty inode list.
4509 */
Mingming Cao617ba132006-10-11 01:20:53 -07004510int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004511{
4512 if (current->flags & PF_MEMALLOC)
4513 return 0;
4514
Mingming Cao617ba132006-10-11 01:20:53 -07004515 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004516 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004517 dump_stack();
4518 return -EIO;
4519 }
4520
4521 if (!wait)
4522 return 0;
4523
Mingming Cao617ba132006-10-11 01:20:53 -07004524 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004525}
4526
Frank Mayhar03901312009-01-07 00:06:22 -05004527int __ext4_write_dirty_metadata(struct inode *inode, struct buffer_head *bh)
4528{
4529 int err = 0;
4530
4531 mark_buffer_dirty(bh);
4532 if (inode && inode_needs_sync(inode)) {
4533 sync_dirty_buffer(bh);
4534 if (buffer_req(bh) && !buffer_uptodate(bh)) {
4535 ext4_error(inode->i_sb, __func__,
4536 "IO error syncing inode, "
4537 "inode=%lu, block=%llu",
4538 inode->i_ino,
4539 (unsigned long long)bh->b_blocknr);
4540 err = -EIO;
4541 }
4542 }
4543 return err;
4544}
4545
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004546/*
Mingming Cao617ba132006-10-11 01:20:53 -07004547 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548 *
4549 * Called from notify_change.
4550 *
4551 * We want to trap VFS attempts to truncate the file as soon as
4552 * possible. In particular, we want to make sure that when the VFS
4553 * shrinks i_size, we put the inode on the orphan list and modify
4554 * i_disksize immediately, so that during the subsequent flushing of
4555 * dirty pages and freeing of disk blocks, we can guarantee that any
4556 * commit will leave the blocks being flushed in an unused state on
4557 * disk. (On recovery, the inode will get truncated and the blocks will
4558 * be freed, so we have a strong guarantee that no future commit will
4559 * leave these blocks visible to the user.)
4560 *
Jan Kara678aaf42008-07-11 19:27:31 -04004561 * Another thing we have to assure is that if we are in ordered mode
4562 * and inode is still attached to the committing transaction, we must
4563 * we start writeout of all the dirty pages which are being truncated.
4564 * This way we are sure that all the data written in the previous
4565 * transaction are already on disk (truncate waits for pages under
4566 * writeback).
4567 *
4568 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004569 */
Mingming Cao617ba132006-10-11 01:20:53 -07004570int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004571{
4572 struct inode *inode = dentry->d_inode;
4573 int error, rc = 0;
4574 const unsigned int ia_valid = attr->ia_valid;
4575
4576 error = inode_change_ok(inode, attr);
4577 if (error)
4578 return error;
4579
4580 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4581 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4582 handle_t *handle;
4583
4584 /* (user+group)*(old+new) structure, inode write (sb,
4585 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004586 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4587 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004588 if (IS_ERR(handle)) {
4589 error = PTR_ERR(handle);
4590 goto err_out;
4591 }
4592 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
4593 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004594 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004595 return error;
4596 }
4597 /* Update corresponding info in inode so that everything is in
4598 * one transaction */
4599 if (attr->ia_valid & ATTR_UID)
4600 inode->i_uid = attr->ia_uid;
4601 if (attr->ia_valid & ATTR_GID)
4602 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004603 error = ext4_mark_inode_dirty(handle, inode);
4604 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004605 }
4606
Eric Sandeene2b46572008-01-28 23:58:27 -05004607 if (attr->ia_valid & ATTR_SIZE) {
4608 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4609 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4610
4611 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4612 error = -EFBIG;
4613 goto err_out;
4614 }
4615 }
4616 }
4617
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004618 if (S_ISREG(inode->i_mode) &&
4619 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4620 handle_t *handle;
4621
Mingming Cao617ba132006-10-11 01:20:53 -07004622 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004623 if (IS_ERR(handle)) {
4624 error = PTR_ERR(handle);
4625 goto err_out;
4626 }
4627
Mingming Cao617ba132006-10-11 01:20:53 -07004628 error = ext4_orphan_add(handle, inode);
4629 EXT4_I(inode)->i_disksize = attr->ia_size;
4630 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004631 if (!error)
4632 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004633 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004634
4635 if (ext4_should_order_data(inode)) {
4636 error = ext4_begin_ordered_truncate(inode,
4637 attr->ia_size);
4638 if (error) {
4639 /* Do as much error cleanup as possible */
4640 handle = ext4_journal_start(inode, 3);
4641 if (IS_ERR(handle)) {
4642 ext4_orphan_del(NULL, inode);
4643 goto err_out;
4644 }
4645 ext4_orphan_del(handle, inode);
4646 ext4_journal_stop(handle);
4647 goto err_out;
4648 }
4649 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004650 }
4651
4652 rc = inode_setattr(inode, attr);
4653
Mingming Cao617ba132006-10-11 01:20:53 -07004654 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004655 * transaction handle at all, we need to clean up the in-core
4656 * orphan list manually. */
4657 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004658 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004659
4660 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004661 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004662
4663err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004664 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004665 if (!error)
4666 error = rc;
4667 return error;
4668}
4669
Mingming Cao3e3398a2008-07-11 19:27:31 -04004670int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4671 struct kstat *stat)
4672{
4673 struct inode *inode;
4674 unsigned long delalloc_blocks;
4675
4676 inode = dentry->d_inode;
4677 generic_fillattr(inode, stat);
4678
4679 /*
4680 * We can't update i_blocks if the block allocation is delayed
4681 * otherwise in the case of system crash before the real block
4682 * allocation is done, we will have i_blocks inconsistent with
4683 * on-disk file blocks.
4684 * We always keep i_blocks updated together with real
4685 * allocation. But to not confuse with user, stat
4686 * will return the blocks that include the delayed allocation
4687 * blocks for this file.
4688 */
4689 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4690 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4691 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4692
4693 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4694 return 0;
4695}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004696
Mingming Caoa02908f2008-08-19 22:16:07 -04004697static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4698 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004699{
Mingming Caoa02908f2008-08-19 22:16:07 -04004700 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004701
Mingming Caoa02908f2008-08-19 22:16:07 -04004702 /* if nrblocks are contiguous */
4703 if (chunk) {
4704 /*
4705 * With N contiguous data blocks, it need at most
4706 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4707 * 2 dindirect blocks
4708 * 1 tindirect block
4709 */
4710 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4711 return indirects + 3;
4712 }
4713 /*
4714 * if nrblocks are not contiguous, worse case, each block touch
4715 * a indirect block, and each indirect block touch a double indirect
4716 * block, plus a triple indirect block
4717 */
4718 indirects = nrblocks * 2 + 1;
4719 return indirects;
4720}
Alex Tomasa86c6182006-10-11 01:21:03 -07004721
Mingming Caoa02908f2008-08-19 22:16:07 -04004722static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4723{
4724 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004725 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4726 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004727}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004728
Mingming Caoa02908f2008-08-19 22:16:07 -04004729/*
4730 * Account for index blocks, block groups bitmaps and block group
4731 * descriptor blocks if modify datablocks and index blocks
4732 * worse case, the indexs blocks spread over different block groups
4733 *
4734 * If datablocks are discontiguous, they are possible to spread over
4735 * different block groups too. If they are contiugous, with flexbg,
4736 * they could still across block group boundary.
4737 *
4738 * Also account for superblock, inode, quota and xattr blocks
4739 */
4740int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4741{
4742 int groups, gdpblocks;
4743 int idxblocks;
4744 int ret = 0;
4745
4746 /*
4747 * How many index blocks need to touch to modify nrblocks?
4748 * The "Chunk" flag indicating whether the nrblocks is
4749 * physically contiguous on disk
4750 *
4751 * For Direct IO and fallocate, they calls get_block to allocate
4752 * one single extent at a time, so they could set the "Chunk" flag
4753 */
4754 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4755
4756 ret = idxblocks;
4757
4758 /*
4759 * Now let's see how many group bitmaps and group descriptors need
4760 * to account
4761 */
4762 groups = idxblocks;
4763 if (chunk)
4764 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004765 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004766 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004767
Mingming Caoa02908f2008-08-19 22:16:07 -04004768 gdpblocks = groups;
4769 if (groups > EXT4_SB(inode->i_sb)->s_groups_count)
4770 groups = EXT4_SB(inode->i_sb)->s_groups_count;
4771 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4772 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4773
4774 /* bitmaps and block group descriptor blocks */
4775 ret += groups + gdpblocks;
4776
4777 /* Blocks for super block, inode, quota and xattr blocks */
4778 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004779
4780 return ret;
4781}
4782
4783/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004784 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004785 * the modification of a single pages into a single transaction,
4786 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004787 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004788 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004789 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004790 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004791 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004792 */
4793int ext4_writepage_trans_blocks(struct inode *inode)
4794{
4795 int bpp = ext4_journal_blocks_per_page(inode);
4796 int ret;
4797
4798 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4799
4800 /* Account for data blocks for journalled mode */
4801 if (ext4_should_journal_data(inode))
4802 ret += bpp;
4803 return ret;
4804}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004805
4806/*
4807 * Calculate the journal credits for a chunk of data modification.
4808 *
4809 * This is called from DIO, fallocate or whoever calling
4810 * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks.
4811 *
4812 * journal buffers for data blocks are not included here, as DIO
4813 * and fallocate do no need to journal data buffers.
4814 */
4815int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4816{
4817 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4818}
4819
Mingming Caoa02908f2008-08-19 22:16:07 -04004820/*
Mingming Cao617ba132006-10-11 01:20:53 -07004821 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004822 * Give this, we know that the caller already has write access to iloc->bh.
4823 */
Mingming Cao617ba132006-10-11 01:20:53 -07004824int ext4_mark_iloc_dirty(handle_t *handle,
4825 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004826{
4827 int err = 0;
4828
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004829 if (test_opt(inode->i_sb, I_VERSION))
4830 inode_inc_iversion(inode);
4831
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004832 /* the do_update_inode consumes one bh->b_count */
4833 get_bh(iloc->bh);
4834
Mingming Caodab291a2006-10-11 01:21:01 -07004835 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004836 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004837 put_bh(iloc->bh);
4838 return err;
4839}
4840
4841/*
4842 * On success, We end up with an outstanding reference count against
4843 * iloc->bh. This _must_ be cleaned up later.
4844 */
4845
4846int
Mingming Cao617ba132006-10-11 01:20:53 -07004847ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4848 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004849{
Frank Mayhar03901312009-01-07 00:06:22 -05004850 int err;
4851
4852 err = ext4_get_inode_loc(inode, iloc);
4853 if (!err) {
4854 BUFFER_TRACE(iloc->bh, "get_write_access");
4855 err = ext4_journal_get_write_access(handle, iloc->bh);
4856 if (err) {
4857 brelse(iloc->bh);
4858 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004859 }
4860 }
Mingming Cao617ba132006-10-11 01:20:53 -07004861 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004862 return err;
4863}
4864
4865/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004866 * Expand an inode by new_extra_isize bytes.
4867 * Returns 0 on success or negative error number on failure.
4868 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004869static int ext4_expand_extra_isize(struct inode *inode,
4870 unsigned int new_extra_isize,
4871 struct ext4_iloc iloc,
4872 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004873{
4874 struct ext4_inode *raw_inode;
4875 struct ext4_xattr_ibody_header *header;
4876 struct ext4_xattr_entry *entry;
4877
4878 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4879 return 0;
4880
4881 raw_inode = ext4_raw_inode(&iloc);
4882
4883 header = IHDR(inode, raw_inode);
4884 entry = IFIRST(header);
4885
4886 /* No extended attributes present */
4887 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
4888 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
4889 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4890 new_extra_isize);
4891 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4892 return 0;
4893 }
4894
4895 /* try to expand with EAs present */
4896 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4897 raw_inode, handle);
4898}
4899
4900/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004901 * What we do here is to mark the in-core inode as clean with respect to inode
4902 * dirtiness (it may still be data-dirty).
4903 * This means that the in-core inode may be reaped by prune_icache
4904 * without having to perform any I/O. This is a very good thing,
4905 * because *any* task may call prune_icache - even ones which
4906 * have a transaction open against a different journal.
4907 *
4908 * Is this cheating? Not really. Sure, we haven't written the
4909 * inode out, but prune_icache isn't a user-visible syncing function.
4910 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4911 * we start and wait on commits.
4912 *
4913 * Is this efficient/effective? Well, we're being nice to the system
4914 * by cleaning up our inodes proactively so they can be reaped
4915 * without I/O. But we are potentially leaving up to five seconds'
4916 * worth of inodes floating about which prune_icache wants us to
4917 * write out. One way to fix that would be to get prune_icache()
4918 * to do a write_super() to free up some memory. It has the desired
4919 * effect.
4920 */
Mingming Cao617ba132006-10-11 01:20:53 -07004921int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004922{
Mingming Cao617ba132006-10-11 01:20:53 -07004923 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004924 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4925 static unsigned int mnt_count;
4926 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004927
4928 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07004929 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05004930 if (ext4_handle_valid(handle) &&
4931 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004932 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
4933 /*
4934 * We need extra buffer credits since we may write into EA block
4935 * with this same handle. If journal_extend fails, then it will
4936 * only result in a minor loss of functionality for that inode.
4937 * If this is felt to be critical, then e2fsck should be run to
4938 * force a large enough s_min_extra_isize.
4939 */
4940 if ((jbd2_journal_extend(handle,
4941 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
4942 ret = ext4_expand_extra_isize(inode,
4943 sbi->s_want_extra_isize,
4944 iloc, handle);
4945 if (ret) {
4946 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004947 if (mnt_count !=
4948 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004949 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004950 "Unable to expand inode %lu. Delete"
4951 " some EAs or run e2fsck.",
4952 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004953 mnt_count =
4954 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004955 }
4956 }
4957 }
4958 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004959 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004960 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004961 return err;
4962}
4963
4964/*
Mingming Cao617ba132006-10-11 01:20:53 -07004965 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004966 *
4967 * We're really interested in the case where a file is being extended.
4968 * i_size has been changed by generic_commit_write() and we thus need
4969 * to include the updated inode in the current transaction.
4970 *
4971 * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
4972 * are allocated to the file.
4973 *
4974 * If the inode is marked synchronous, we don't honour that here - doing
4975 * so would cause a commit on atime updates, which we don't bother doing.
4976 * We handle synchronous inodes at the highest possible level.
4977 */
Mingming Cao617ba132006-10-11 01:20:53 -07004978void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004979{
Mingming Cao617ba132006-10-11 01:20:53 -07004980 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004981 handle_t *handle;
4982
Frank Mayhar03901312009-01-07 00:06:22 -05004983 if (!ext4_handle_valid(current_handle)) {
4984 ext4_mark_inode_dirty(current_handle, inode);
4985 return;
4986 }
4987
Mingming Cao617ba132006-10-11 01:20:53 -07004988 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004989 if (IS_ERR(handle))
4990 goto out;
4991 if (current_handle &&
4992 current_handle->h_transaction != handle->h_transaction) {
4993 /* This task has a transaction open against a different fs */
4994 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04004995 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004996 } else {
4997 jbd_debug(5, "marking dirty. outer handle=%p\n",
4998 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07004999 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005000 }
Mingming Cao617ba132006-10-11 01:20:53 -07005001 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005002out:
5003 return;
5004}
5005
5006#if 0
5007/*
5008 * Bind an inode's backing buffer_head into this transaction, to prevent
5009 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005010 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005011 * returns no iloc structure, so the caller needs to repeat the iloc
5012 * lookup to mark the inode dirty later.
5013 */
Mingming Cao617ba132006-10-11 01:20:53 -07005014static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005015{
Mingming Cao617ba132006-10-11 01:20:53 -07005016 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005017
5018 int err = 0;
5019 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005020 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005021 if (!err) {
5022 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005023 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005024 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005025 err = ext4_handle_dirty_metadata(handle,
5026 inode,
5027 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005028 brelse(iloc.bh);
5029 }
5030 }
Mingming Cao617ba132006-10-11 01:20:53 -07005031 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005032 return err;
5033}
5034#endif
5035
Mingming Cao617ba132006-10-11 01:20:53 -07005036int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005037{
5038 journal_t *journal;
5039 handle_t *handle;
5040 int err;
5041
5042 /*
5043 * We have to be very careful here: changing a data block's
5044 * journaling status dynamically is dangerous. If we write a
5045 * data block to the journal, change the status and then delete
5046 * that block, we risk forgetting to revoke the old log record
5047 * from the journal and so a subsequent replay can corrupt data.
5048 * So, first we make sure that the journal is empty and that
5049 * nobody is changing anything.
5050 */
5051
Mingming Cao617ba132006-10-11 01:20:53 -07005052 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005053 if (!journal)
5054 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005055 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005056 return -EROFS;
5057
Mingming Caodab291a2006-10-11 01:21:01 -07005058 jbd2_journal_lock_updates(journal);
5059 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005060
5061 /*
5062 * OK, there are no updates running now, and all cached data is
5063 * synced to disk. We are now in a completely consistent state
5064 * which doesn't have anything in the journal, and we know that
5065 * no filesystem updates are running, so it is safe to modify
5066 * the inode's in-core data-journaling state flag now.
5067 */
5068
5069 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005070 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005071 else
Mingming Cao617ba132006-10-11 01:20:53 -07005072 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5073 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005074
Mingming Caodab291a2006-10-11 01:21:01 -07005075 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005076
5077 /* Finally we can mark the inode as dirty. */
5078
Mingming Cao617ba132006-10-11 01:20:53 -07005079 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005080 if (IS_ERR(handle))
5081 return PTR_ERR(handle);
5082
Mingming Cao617ba132006-10-11 01:20:53 -07005083 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005084 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005085 ext4_journal_stop(handle);
5086 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005087
5088 return err;
5089}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005090
5091static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5092{
5093 return !buffer_mapped(bh);
5094}
5095
5096int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page)
5097{
5098 loff_t size;
5099 unsigned long len;
5100 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005101 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005102 struct file *file = vma->vm_file;
5103 struct inode *inode = file->f_path.dentry->d_inode;
5104 struct address_space *mapping = inode->i_mapping;
5105
5106 /*
5107 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5108 * get i_mutex because we are already holding mmap_sem.
5109 */
5110 down_read(&inode->i_alloc_sem);
5111 size = i_size_read(inode);
5112 if (page->mapping != mapping || size <= page_offset(page)
5113 || !PageUptodate(page)) {
5114 /* page got truncated from under us? */
5115 goto out_unlock;
5116 }
5117 ret = 0;
5118 if (PageMappedToDisk(page))
5119 goto out_unlock;
5120
5121 if (page->index == size >> PAGE_CACHE_SHIFT)
5122 len = size & ~PAGE_CACHE_MASK;
5123 else
5124 len = PAGE_CACHE_SIZE;
5125
5126 if (page_has_buffers(page)) {
5127 /* return if we have all the buffers mapped */
5128 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5129 ext4_bh_unmapped))
5130 goto out_unlock;
5131 }
5132 /*
5133 * OK, we need to fill the hole... Do write_begin write_end
5134 * to do block allocation/reservation.We are not holding
5135 * inode.i__mutex here. That allow * parallel write_begin,
5136 * write_end call. lock_page prevent this from happening
5137 * on the same page though
5138 */
5139 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005140 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005141 if (ret < 0)
5142 goto out_unlock;
5143 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005144 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005145 if (ret < 0)
5146 goto out_unlock;
5147 ret = 0;
5148out_unlock:
5149 up_read(&inode->i_alloc_sem);
5150 return ret;
5151}