blob: a31980dd9b86ff7336393559b12011667dea6589 [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>
Mingming Cao4c0425f2009-09-28 15:48:41 -040040#include <linux/workqueue.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040041
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040042#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043#include "xattr.h"
44#include "acl.h"
Mingming Caod2a17632008-07-14 17:52:37 -040045#include "ext4_extents.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046
Theodore Ts'o9bffad12009-06-17 11:48:11 -040047#include <trace/events/ext4.h>
48
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040049#define MPAGE_DA_EXTENT_TAIL 0x01
50
Jan Kara678aaf42008-07-11 19:27:31 -040051static inline int ext4_begin_ordered_truncate(struct inode *inode,
52 loff_t new_size)
53{
Jan Kara7f5aa212009-02-10 11:15:34 -050054 return jbd2_journal_begin_ordered_truncate(
55 EXT4_SB(inode->i_sb)->s_journal,
56 &EXT4_I(inode)->jinode,
57 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040058}
59
Alex Tomas64769242008-07-11 19:27:31 -040060static void ext4_invalidatepage(struct page *page, unsigned long offset);
61
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062/*
63 * Test whether an inode is a fast symlink.
64 */
Mingming Cao617ba132006-10-11 01:20:53 -070065static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066{
Mingming Cao617ba132006-10-11 01:20:53 -070067 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070068 (inode->i_sb->s_blocksize >> 9) : 0;
69
70 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
71}
72
73/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -070074 * Work out how many blocks we need to proceed with the next chunk of a
75 * truncate transaction.
76 */
77static unsigned long blocks_for_truncate(struct inode *inode)
78{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -050079 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070080
81 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
82
83 /* Give ourselves just enough room to cope with inodes in which
84 * i_blocks is corrupt: we've seen disk corruptions in the past
85 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -070086 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087 * will go a bit crazy if that happens, but at least we should
88 * try not to panic the whole kernel. */
89 if (needed < 2)
90 needed = 2;
91
92 /* But we need to bound the transaction so we don't overflow the
93 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -070094 if (needed > EXT4_MAX_TRANS_DATA)
95 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070096
Mingming Cao617ba132006-10-11 01:20:53 -070097 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070098}
99
100/*
101 * Truncate transactions can be complex and absolutely huge. So we need to
102 * be able to restart the transaction at a conventient checkpoint to make
103 * sure we don't overflow the journal.
104 *
105 * start_transaction gets us a new handle for a truncate transaction,
106 * and extend_transaction tries to extend the existing one a bit. If
107 * extend fails, we need to propagate the failure up and restart the
108 * transaction in the top-level truncate loop. --sct
109 */
110static handle_t *start_transaction(struct inode *inode)
111{
112 handle_t *result;
113
Mingming Cao617ba132006-10-11 01:20:53 -0700114 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700115 if (!IS_ERR(result))
116 return result;
117
Mingming Cao617ba132006-10-11 01:20:53 -0700118 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119 return result;
120}
121
122/*
123 * Try to extend this transaction for the purposes of truncation.
124 *
125 * Returns 0 if we managed to create more room. If we can't create more
126 * room, and the transaction must be restarted we return 1.
127 */
128static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
129{
Frank Mayhar03901312009-01-07 00:06:22 -0500130 if (!ext4_handle_valid(handle))
131 return 0;
132 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700134 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700135 return 0;
136 return 1;
137}
138
139/*
140 * Restart the transaction associated with *handle. This does a commit,
141 * so before we call here everything must be consistently dirtied against
142 * this transaction.
143 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500144int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400145 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146{
Jan Kara487caee2009-08-17 22:17:20 -0400147 int ret;
148
149 /*
150 * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this
151 * moment, get_block can be called only for blocks inside i_size since
152 * page cache has been already dropped and writes are blocked by
153 * i_mutex. So we can safely drop the i_data_sem here.
154 */
Frank Mayhar03901312009-01-07 00:06:22 -0500155 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700156 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400157 up_write(&EXT4_I(inode)->i_data_sem);
158 ret = ext4_journal_restart(handle, blocks_for_truncate(inode));
159 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500160 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400161
162 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163}
164
165/*
166 * Called at the last iput() if i_nlink is zero.
167 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400168void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169{
170 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400171 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172
Jan Kara678aaf42008-07-11 19:27:31 -0400173 if (ext4_should_order_data(inode))
174 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175 truncate_inode_pages(&inode->i_data, 0);
176
177 if (is_bad_inode(inode))
178 goto no_delete;
179
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400180 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400182 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 /*
184 * If we're going to skip the normal cleanup, we still need to
185 * make sure that the in-core orphan linked list is properly
186 * cleaned up.
187 */
Mingming Cao617ba132006-10-11 01:20:53 -0700188 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189 goto no_delete;
190 }
191
192 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500193 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400195 err = ext4_mark_inode_dirty(handle, inode);
196 if (err) {
197 ext4_warning(inode->i_sb, __func__,
198 "couldn't mark inode dirty (err %d)", err);
199 goto stop_handle;
200 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700202 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400203
204 /*
205 * ext4_ext_truncate() doesn't reserve any slop when it
206 * restarts journal transactions; therefore there may not be
207 * enough credits left in the handle to remove the inode from
208 * the orphan list and set the dtime field.
209 */
Frank Mayhar03901312009-01-07 00:06:22 -0500210 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400211 err = ext4_journal_extend(handle, 3);
212 if (err > 0)
213 err = ext4_journal_restart(handle, 3);
214 if (err != 0) {
215 ext4_warning(inode->i_sb, __func__,
216 "couldn't extend journal (err %d)", err);
217 stop_handle:
218 ext4_journal_stop(handle);
219 goto no_delete;
220 }
221 }
222
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700224 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700226 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700228 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 * (Well, we could do this if we need to, but heck - it works)
230 */
Mingming Cao617ba132006-10-11 01:20:53 -0700231 ext4_orphan_del(handle, inode);
232 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233
234 /*
235 * One subtle ordering requirement: if anything has gone wrong
236 * (transaction abort, IO errors, whatever), then we can still
237 * do these next steps (the fs will already have been marked as
238 * having errors), but we can't free the inode if the mark_dirty
239 * fails.
240 */
Mingming Cao617ba132006-10-11 01:20:53 -0700241 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 /* If that failed, just do the required in-core inode clear. */
243 clear_inode(inode);
244 else
Mingming Cao617ba132006-10-11 01:20:53 -0700245 ext4_free_inode(handle, inode);
246 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 return;
248no_delete:
249 clear_inode(inode); /* We must guarantee clearing of inode... */
250}
251
252typedef struct {
253 __le32 *p;
254 __le32 key;
255 struct buffer_head *bh;
256} Indirect;
257
258static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
259{
260 p->key = *(p->p = v);
261 p->bh = bh;
262}
263
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264/**
Mingming Cao617ba132006-10-11 01:20:53 -0700265 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 * @inode: inode in question (we are only interested in its superblock)
267 * @i_block: block number to be parsed
268 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400269 * @boundary: set this non-zero if the referred-to block is likely to be
270 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700271 *
Mingming Cao617ba132006-10-11 01:20:53 -0700272 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 * for UNIX filesystems - tree of pointers anchored in the inode, with
274 * data blocks at leaves and indirect blocks in intermediate nodes.
275 * This function translates the block number into path in that tree -
276 * return value is the path length and @offsets[n] is the offset of
277 * pointer to (n+1)th node in the nth one. If @block is out of range
278 * (negative or too large) warning is printed and zero returned.
279 *
280 * Note: function doesn't find node addresses, so no IO is needed. All
281 * we need to know is the capacity of indirect blocks (taken from the
282 * inode->i_sb).
283 */
284
285/*
286 * Portability note: the last comparison (check that we fit into triple
287 * indirect block) is spelled differently, because otherwise on an
288 * architecture with 32-bit longs and 8Kb pages we might get into trouble
289 * if our filesystem had 8Kb blocks. We might use long long, but that would
290 * kill us on x86. Oh, well, at least the sign propagation does not matter -
291 * i_block would have to be negative in the very beginning, so we would not
292 * get there at all.
293 */
294
Mingming Cao617ba132006-10-11 01:20:53 -0700295static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400296 ext4_lblk_t i_block,
297 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298{
Mingming Cao617ba132006-10-11 01:20:53 -0700299 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
300 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
301 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 indirect_blocks = ptrs,
303 double_blocks = (1 << (ptrs_bits * 2));
304 int n = 0;
305 int final = 0;
306
Roel Kluinc333e072009-08-10 22:47:22 -0400307 if (i_block < direct_blocks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 offsets[n++] = i_block;
309 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400310 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700311 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 offsets[n++] = i_block;
313 final = ptrs;
314 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700315 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 offsets[n++] = i_block >> ptrs_bits;
317 offsets[n++] = i_block & (ptrs - 1);
318 final = ptrs;
319 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700320 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 offsets[n++] = i_block >> (ptrs_bits * 2);
322 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
323 offsets[n++] = i_block & (ptrs - 1);
324 final = ptrs;
325 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500326 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400327 "block %lu > max in inode %lu",
328 i_block + direct_blocks +
329 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700330 }
331 if (boundary)
332 *boundary = final - 1 - (i_block & (ptrs - 1));
333 return n;
334}
335
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400336static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400337 __le32 *p, unsigned int max)
338{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400339 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400340 unsigned int blk;
341
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400342 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400343 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400344 if (blk &&
345 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400346 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400347 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400348 "invalid block reference %u "
349 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400350 return -EIO;
351 }
352 }
353 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400354}
355
356
357#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400358 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400359 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
360
361#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400362 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400363 EXT4_NDIR_BLOCKS)
364
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365/**
Mingming Cao617ba132006-10-11 01:20:53 -0700366 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 * @inode: inode in question
368 * @depth: depth of the chain (1 - direct pointer, etc.)
369 * @offsets: offsets of pointers in inode/indirect blocks
370 * @chain: place to store the result
371 * @err: here we store the error value
372 *
373 * Function fills the array of triples <key, p, bh> and returns %NULL
374 * if everything went OK or the pointer to the last filled triple
375 * (incomplete one) otherwise. Upon the return chain[i].key contains
376 * the number of (i+1)-th block in the chain (as it is stored in memory,
377 * i.e. little-endian 32-bit), chain[i].p contains the address of that
378 * number (it points into struct inode for i==0 and into the bh->b_data
379 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
380 * block for i>0 and NULL for i==0. In other words, it holds the block
381 * numbers of the chain, addresses they were taken from (and where we can
382 * verify that chain did not change) and buffer_heads hosting these
383 * numbers.
384 *
385 * Function stops when it stumbles upon zero pointer (absent block)
386 * (pointer to last triple returned, *@err == 0)
387 * or when it gets an IO error reading an indirect block
388 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700389 * or when it reads all @depth-1 indirect blocks successfully and finds
390 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500391 *
392 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500393 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700394 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500395static Indirect *ext4_get_branch(struct inode *inode, int depth,
396 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700397 Indirect chain[4], int *err)
398{
399 struct super_block *sb = inode->i_sb;
400 Indirect *p = chain;
401 struct buffer_head *bh;
402
403 *err = 0;
404 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400405 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406 if (!p->key)
407 goto no_block;
408 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400409 bh = sb_getblk(sb, le32_to_cpu(p->key));
410 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700411 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400412
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400413 if (!bh_uptodate_or_lock(bh)) {
414 if (bh_submit_read(bh) < 0) {
415 put_bh(bh);
416 goto failure;
417 }
418 /* validate block references */
419 if (ext4_check_indirect_blockref(inode, bh)) {
420 put_bh(bh);
421 goto failure;
422 }
423 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400424
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400425 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700426 /* Reader: end */
427 if (!p->key)
428 goto no_block;
429 }
430 return NULL;
431
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700432failure:
433 *err = -EIO;
434no_block:
435 return p;
436}
437
438/**
Mingming Cao617ba132006-10-11 01:20:53 -0700439 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 * @inode: owner
441 * @ind: descriptor of indirect block.
442 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000443 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 * It is used when heuristic for sequential allocation fails.
445 * Rules are:
446 * + if there is a block to the left of our position - allocate near it.
447 * + if pointer will live in indirect block - allocate near that block.
448 * + if pointer will live in inode - allocate in the same
449 * cylinder group.
450 *
451 * In the latter case we colour the starting block by the callers PID to
452 * prevent it from clashing with concurrent allocations for a different inode
453 * in the same block group. The PID is used here so that functionally related
454 * files will be close-by on-disk.
455 *
456 * Caller must make sure that @ind is valid and will stay that way.
457 */
Mingming Cao617ba132006-10-11 01:20:53 -0700458static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459{
Mingming Cao617ba132006-10-11 01:20:53 -0700460 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400461 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700463 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500464 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700465 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400466 ext4_group_t block_group;
467 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468
469 /* Try to find previous block */
470 for (p = ind->p - 1; p >= start; p--) {
471 if (*p)
472 return le32_to_cpu(*p);
473 }
474
475 /* No such thing, so let's try location of indirect block */
476 if (ind->bh)
477 return ind->bh->b_blocknr;
478
479 /*
480 * It is going to be referred to from the inode itself? OK, just put it
481 * into the same cylinder group then.
482 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400483 block_group = ei->i_block_group;
484 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
485 block_group &= ~(flex_size-1);
486 if (S_ISREG(inode->i_mode))
487 block_group++;
488 }
489 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500490 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
491
Theodore Ts'oa4912122009-03-12 12:18:34 -0400492 /*
493 * If we are doing delayed allocation, we don't need take
494 * colour into account.
495 */
496 if (test_opt(inode->i_sb, DELALLOC))
497 return bg_start;
498
Valerie Clement74d34872008-02-15 13:43:07 -0500499 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
500 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700501 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500502 else
503 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 return bg_start + colour;
505}
506
507/**
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000508 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 * @inode: owner
510 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000513 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800514 * returns it.
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400515 * Because this is only used for non-extent files, we limit the block nr
516 * to 32 bits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500518static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400519 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520{
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400521 ext4_fsblk_t goal;
522
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400524 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400527 goal = ext4_find_near(inode, partial);
528 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
529 return goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530}
531
532/**
Mingming Cao617ba132006-10-11 01:20:53 -0700533 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534 * of direct blocks need to be allocated for the given branch.
535 *
536 * @branch: chain of indirect blocks
537 * @k: number of blocks need for indirect blocks
538 * @blks: number of data blocks to be mapped.
539 * @blocks_to_boundary: the offset in the indirect block
540 *
541 * return the total number of blocks to be allocate, including the
542 * direct and indirect blocks.
543 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500544static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400545 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500547 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548
549 /*
550 * Simple case, [t,d]Indirect block(s) has not allocated yet
551 * then it's clear blocks on that path have not allocated
552 */
553 if (k > 0) {
554 /* right now we don't handle cross boundary allocation */
555 if (blks < blocks_to_boundary + 1)
556 count += blks;
557 else
558 count += blocks_to_boundary + 1;
559 return count;
560 }
561
562 count++;
563 while (count < blks && count <= blocks_to_boundary &&
564 le32_to_cpu(*(branch[0].p + count)) == 0) {
565 count++;
566 }
567 return count;
568}
569
570/**
Mingming Cao617ba132006-10-11 01:20:53 -0700571 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 * @indirect_blks: the number of blocks need to allocate for indirect
573 * blocks
574 *
575 * @new_blocks: on return it will store the new block numbers for
576 * the indirect blocks(if needed) and the first direct block,
577 * @blks: on return it will store the total number of allocated
578 * direct blocks
579 */
Mingming Cao617ba132006-10-11 01:20:53 -0700580static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400581 ext4_lblk_t iblock, ext4_fsblk_t goal,
582 int indirect_blks, int blks,
583 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500585 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400587 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700589 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590 int ret = 0;
591
592 /*
593 * Here we try to allocate the requested multiple blocks at once,
594 * on a best-effort basis.
595 * To build a branch, we should allocate blocks for
596 * the indirect blocks(if not allocated yet), and at least
597 * the first direct block of this branch. That's the
598 * minimum number of blocks need to allocate(required)
599 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400600 /* first we try to allocate the indirect blocks */
601 target = indirect_blks;
602 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 count = target;
604 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400605 current_block = ext4_new_meta_blocks(handle, inode,
606 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 if (*err)
608 goto failed_out;
609
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400610 BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS);
611
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 target -= count;
613 /* allocate blocks for indirect blocks */
614 while (index < indirect_blks && count) {
615 new_blocks[index++] = current_block++;
616 count--;
617 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400618 if (count > 0) {
619 /*
620 * save the new block number
621 * for the first direct block
622 */
623 new_blocks[index] = current_block;
624 printk(KERN_INFO "%s returned more blocks than "
625 "requested\n", __func__);
626 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400628 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629 }
630
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400631 target = blks - count ;
632 blk_allocated = count;
633 if (!target)
634 goto allocated;
635 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500636 memset(&ar, 0, sizeof(ar));
637 ar.inode = inode;
638 ar.goal = goal;
639 ar.len = target;
640 ar.logical = iblock;
641 if (S_ISREG(inode->i_mode))
642 /* enable in-core preallocation only for regular files */
643 ar.flags = EXT4_MB_HINT_DATA;
644
645 current_block = ext4_mb_new_blocks(handle, &ar, err);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400646 BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS);
Theodore Ts'o815a1132009-01-01 23:59:43 -0500647
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400648 if (*err && (target == blks)) {
649 /*
650 * if the allocation failed and we didn't allocate
651 * any blocks before
652 */
653 goto failed_out;
654 }
655 if (!*err) {
656 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400657 /*
658 * save the new block number
659 * for the first direct block
660 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400661 new_blocks[index] = current_block;
662 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500663 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400664 }
665allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400667 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 *err = 0;
669 return ret;
670failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400671 for (i = 0; i < index; i++)
Theodore Ts'oe6362602009-11-23 07:17:05 -0500672 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 return ret;
674}
675
676/**
Mingming Cao617ba132006-10-11 01:20:53 -0700677 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678 * @inode: owner
679 * @indirect_blks: number of allocated indirect blocks
680 * @blks: number of allocated direct blocks
681 * @offsets: offsets (in the blocks) to store the pointers to next.
682 * @branch: place to store the chain in.
683 *
684 * This function allocates blocks, zeroes out all but the last one,
685 * links them into chain and (if we are synchronous) writes them to disk.
686 * In other words, it prepares a branch that can be spliced onto the
687 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700688 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 * we had read the existing part of chain and partial points to the last
690 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700691 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 * place chain is disconnected - *branch->p is still zero (we did not
693 * set the last link), but branch->key contains the number that should
694 * be placed into *branch->p to fill that gap.
695 *
696 * If allocation fails we free all blocks we've allocated (and forget
697 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700698 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 * as described above and return 0.
700 */
Mingming Cao617ba132006-10-11 01:20:53 -0700701static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400702 ext4_lblk_t iblock, int indirect_blks,
703 int *blks, ext4_fsblk_t goal,
704 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705{
706 int blocksize = inode->i_sb->s_blocksize;
707 int i, n = 0;
708 int err = 0;
709 struct buffer_head *bh;
710 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700711 ext4_fsblk_t new_blocks[4];
712 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400714 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 *blks, new_blocks, &err);
716 if (err)
717 return err;
718
719 branch[0].key = cpu_to_le32(new_blocks[0]);
720 /*
721 * metadata blocks and data blocks are allocated.
722 */
723 for (n = 1; n <= indirect_blks; n++) {
724 /*
725 * Get buffer_head for parent block, zero it out
726 * and set the pointer to new one, then send
727 * parent to disk.
728 */
729 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
730 branch[n].bh = bh;
731 lock_buffer(bh);
732 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700733 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400735 /* Don't brelse(bh) here; it's done in
736 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 goto failed;
739 }
740
741 memset(bh->b_data, 0, blocksize);
742 branch[n].p = (__le32 *) bh->b_data + offsets[n];
743 branch[n].key = cpu_to_le32(new_blocks[n]);
744 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400745 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 current_block = new_blocks[n];
747 /*
748 * End of chain, update the last new metablock of
749 * the chain to point to the new allocated
750 * data blocks numbers
751 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400752 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753 *(branch[n].p + i) = cpu_to_le32(++current_block);
754 }
755 BUFFER_TRACE(bh, "marking uptodate");
756 set_buffer_uptodate(bh);
757 unlock_buffer(bh);
758
Frank Mayhar03901312009-01-07 00:06:22 -0500759 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
760 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 if (err)
762 goto failed;
763 }
764 *blks = num;
765 return err;
766failed:
767 /* Allocation failed, free what we already allocated */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500768 ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769 for (i = 1; i <= n ; i++) {
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500770 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500771 * branch[i].bh is newly allocated, so there is no
772 * need to revoke the block, which is why we don't
773 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500774 */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500775 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1,
776 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777 }
Theodore Ts'oe6362602009-11-23 07:17:05 -0500778 for (i = n+1; i < indirect_blks; i++)
779 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780
Theodore Ts'oe6362602009-11-23 07:17:05 -0500781 ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782
783 return err;
784}
785
786/**
Mingming Cao617ba132006-10-11 01:20:53 -0700787 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 * @inode: owner
789 * @block: (logical) number of block we are adding
790 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700791 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 * @where: location of missing link
793 * @num: number of indirect blocks we are adding
794 * @blks: number of direct blocks we are adding
795 *
796 * This function fills the missing link and does all housekeeping needed in
797 * inode (->i_blocks, etc.). In case of success we end up with the full
798 * chain to new block and return 0.
799 */
Mingming Cao617ba132006-10-11 01:20:53 -0700800static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400801 ext4_lblk_t block, Indirect *where, int num,
802 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803{
804 int i;
805 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700806 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700808 /*
809 * If we're splicing into a [td]indirect block (as opposed to the
810 * inode) then we need to get write access to the [td]indirect block
811 * before the splice.
812 */
813 if (where->bh) {
814 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700815 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 if (err)
817 goto err_out;
818 }
819 /* That's it */
820
821 *where->p = where->key;
822
823 /*
824 * Update the host buffer_head or inode to point to more just allocated
825 * direct blocks blocks
826 */
827 if (num == 0 && blks > 1) {
828 current_block = le32_to_cpu(where->key) + 1;
829 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400830 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831 }
832
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834 /* had we spliced it onto indirect block? */
835 if (where->bh) {
836 /*
837 * If we spliced it onto an indirect block, we haven't
838 * altered the inode. Note however that if it is being spliced
839 * onto an indirect block at the very end of the file (the
840 * file is growing) then we *will* alter the inode to reflect
841 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700842 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 */
844 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500845 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
846 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847 if (err)
848 goto err_out;
849 } else {
850 /*
851 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400853 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 jbd_debug(5, "splicing direct\n");
855 }
856 return err;
857
858err_out:
859 for (i = 1; i <= num; i++) {
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500860 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500861 * branch[i].bh is newly allocated, so there is no
862 * need to revoke the block, which is why we don't
863 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500864 */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500865 ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
866 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 }
Theodore Ts'oe6362602009-11-23 07:17:05 -0500868 ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key),
869 blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870
871 return err;
872}
873
874/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400875 * The ext4_ind_get_blocks() function handles non-extents inodes
876 * (i.e., using the traditional indirect/double-indirect i_blocks
877 * scheme) for ext4_get_blocks().
878 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 * Allocation strategy is simple: if we have to allocate something, we will
880 * have to go the whole way to leaf. So let's do it before attaching anything
881 * to tree, set linkage between the newborn blocks, write them if sync is
882 * required, recheck the path, free and repeat if check fails, otherwise
883 * set the last missing link (that will protect us from any truncate-generated
884 * removals - all blocks on the path are immune now) and possibly force the
885 * write on the parent block.
886 * That has a nice additional property: no special recovery from the failed
887 * allocations is needed - we simply release blocks and do not touch anything
888 * reachable from inode.
889 *
890 * `handle' can be NULL if create == 0.
891 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 * return > 0, # of blocks mapped or allocated.
893 * return = 0, if plain lookup failed.
894 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500895 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400896 * The ext4_ind_get_blocks() function should be called with
897 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
898 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
899 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
900 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400902static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400903 ext4_lblk_t iblock, unsigned int maxblocks,
904 struct buffer_head *bh_result,
905 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906{
907 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500908 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 Indirect chain[4];
910 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700911 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 int indirect_blks;
913 int blocks_to_boundary = 0;
914 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700916 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917
Alex Tomasa86c6182006-10-11 01:21:03 -0700918 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400919 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500920 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400921 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922
923 if (depth == 0)
924 goto out;
925
Mingming Cao617ba132006-10-11 01:20:53 -0700926 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927
928 /* Simplest case - block found, no allocation needed */
929 if (!partial) {
930 first_block = le32_to_cpu(chain[depth - 1].key);
931 clear_buffer_new(bh_result);
932 count++;
933 /*map more blocks*/
934 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700935 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937 blk = le32_to_cpu(*(chain[depth-1].p + count));
938
939 if (blk == first_block + count)
940 count++;
941 else
942 break;
943 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500944 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945 }
946
947 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400948 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 goto cleanup;
950
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400952 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800954 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955
956 /* the number of blocks need to allocate for [d,t]indirect blocks */
957 indirect_blks = (chain + depth) - partial - 1;
958
959 /*
960 * Next look up the indirect map to count the totoal number of
961 * direct blocks to allocate for this branch.
962 */
Mingming Cao617ba132006-10-11 01:20:53 -0700963 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 maxblocks, blocks_to_boundary);
965 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700966 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400968 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400969 &count, goal,
970 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971
972 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700973 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 * on the new chain if there is a failure, but that risks using
975 * up transaction credits, especially for bitmaps where the
976 * credits cannot be returned. Can we handle this somehow? We
977 * may need to return -EAGAIN upwards in the worst case. --sct
978 */
979 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -0700980 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400981 partial, indirect_blks, count);
Jan Kara2bba7022009-11-23 07:24:48 -0500982 if (err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 goto cleanup;
984
985 set_buffer_new(bh_result);
Jan Karab436b9b2009-12-08 23:51:10 -0500986
987 ext4_update_inode_fsync_trans(handle, inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988got_it:
989 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
990 if (count > blocks_to_boundary)
991 set_buffer_boundary(bh_result);
992 err = count;
993 /* Clean up and exit */
994 partial = chain + depth - 1; /* the whole chain */
995cleanup:
996 while (partial > chain) {
997 BUFFER_TRACE(partial->bh, "call brelse");
998 brelse(partial->bh);
999 partial--;
1000 }
1001 BUFFER_TRACE(bh_result, "returned");
1002out:
1003 return err;
1004}
1005
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001006#ifdef CONFIG_QUOTA
1007qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +01001008{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001009 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +01001010}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001011#endif
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001012/*
1013 * Calculate the number of metadata blocks need to reserve
1014 * to allocate @blocks for non extent file based file
1015 */
1016static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
1017{
1018 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1019 int ind_blks, dind_blks, tind_blks;
1020
1021 /* number of new indirect blocks needed */
1022 ind_blks = (blocks + icap - 1) / icap;
1023
1024 dind_blks = (ind_blks + icap - 1) / icap;
1025
1026 tind_blks = 1;
1027
1028 return ind_blks + dind_blks + tind_blks;
1029}
1030
1031/*
1032 * Calculate the number of metadata blocks need to reserve
1033 * to allocate given number of blocks
1034 */
1035static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1036{
Mingming Caocd213222008-08-19 22:16:59 -04001037 if (!blocks)
1038 return 0;
1039
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001040 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1041 return ext4_ext_calc_metadata_amount(inode, blocks);
1042
1043 return ext4_indirect_calc_metadata_amount(inode, blocks);
1044}
1045
1046static void ext4_da_update_reserve_space(struct inode *inode, int used)
1047{
1048 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1049 int total, mdb, mdb_free;
1050
1051 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1052 /* recalculate the number of metablocks still need to be reserved */
1053 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1054 mdb = ext4_calc_metadata_amount(inode, total);
1055
1056 /* figure out how many metablocks to release */
1057 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1058 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1059
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001060 if (mdb_free) {
1061 /* Account for allocated meta_blocks */
1062 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001063
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001064 /* update fs dirty blocks counter */
1065 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1066 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1067 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1068 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001069
1070 /* update per-inode reservations */
1071 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1072 EXT4_I(inode)->i_reserved_data_blocks -= used;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001073 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001074
1075 /*
1076 * free those over-booking quota for metadata blocks
1077 */
Mingming Cao60e58e02009-01-22 18:13:05 +01001078 if (mdb_free)
1079 vfs_dq_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001080
1081 /*
1082 * If we have done all the pending block allocations and if
1083 * there aren't any writers on the inode, we can discard the
1084 * inode's preallocations.
1085 */
1086 if (!total && (atomic_read(&inode->i_writecount) == 0))
1087 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001088}
1089
Theodore Ts'o80e42462009-09-08 08:21:26 -04001090static int check_block_validity(struct inode *inode, const char *msg,
1091 sector_t logical, sector_t phys, int len)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001092{
1093 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
Theodore Ts'o80e42462009-09-08 08:21:26 -04001094 ext4_error(inode->i_sb, msg,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001095 "inode #%lu logical block %llu mapped to %llu "
1096 "(size %d)", inode->i_ino,
1097 (unsigned long long) logical,
1098 (unsigned long long) phys, len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001099 return -EIO;
1100 }
1101 return 0;
1102}
1103
Mingming Caof5ab0d12008-02-25 15:29:55 -05001104/*
Theodore Ts'o1f945332009-09-30 22:57:41 -04001105 * Return the number of contiguous dirty pages in a given inode
1106 * starting at page frame idx.
Theodore Ts'o55138e02009-09-29 13:31:31 -04001107 */
1108static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
1109 unsigned int max_pages)
1110{
1111 struct address_space *mapping = inode->i_mapping;
1112 pgoff_t index;
1113 struct pagevec pvec;
1114 pgoff_t num = 0;
1115 int i, nr_pages, done = 0;
1116
1117 if (max_pages == 0)
1118 return 0;
1119 pagevec_init(&pvec, 0);
1120 while (!done) {
1121 index = idx;
1122 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1123 PAGECACHE_TAG_DIRTY,
1124 (pgoff_t)PAGEVEC_SIZE);
1125 if (nr_pages == 0)
1126 break;
1127 for (i = 0; i < nr_pages; i++) {
1128 struct page *page = pvec.pages[i];
1129 struct buffer_head *bh, *head;
1130
1131 lock_page(page);
1132 if (unlikely(page->mapping != mapping) ||
1133 !PageDirty(page) ||
1134 PageWriteback(page) ||
1135 page->index != idx) {
1136 done = 1;
1137 unlock_page(page);
1138 break;
1139 }
Theodore Ts'o1f945332009-09-30 22:57:41 -04001140 if (page_has_buffers(page)) {
1141 bh = head = page_buffers(page);
1142 do {
1143 if (!buffer_delay(bh) &&
1144 !buffer_unwritten(bh))
1145 done = 1;
1146 bh = bh->b_this_page;
1147 } while (!done && (bh != head));
1148 }
Theodore Ts'o55138e02009-09-29 13:31:31 -04001149 unlock_page(page);
1150 if (done)
1151 break;
1152 idx++;
1153 num++;
1154 if (num >= max_pages)
1155 break;
1156 }
1157 pagevec_release(&pvec);
1158 }
1159 return num;
1160}
1161
1162/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001163 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001164 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001165 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001166 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1167 * and store the allocated blocks in the result buffer head and mark it
1168 * mapped.
1169 *
1170 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001171 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001172 * based files
1173 *
1174 * On success, it returns the number of blocks being mapped or allocate.
1175 * if create==0 and the blocks are pre-allocated and uninitialized block,
1176 * the result buffer head is unmapped. If the create ==1, it will make sure
1177 * the buffer head is mapped.
1178 *
1179 * It returns 0 if plain look up failed (blocks have not been allocated), in
1180 * that casem, buffer head is unmapped
1181 *
1182 * It returns the error in case of allocation failure.
1183 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001184int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1185 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001186 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001187{
1188 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001189
1190 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001191 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001192
Mingming Cao00314622009-09-28 15:49:08 -04001193 ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u,"
1194 "logical block %lu\n", inode->i_ino, flags, max_blocks,
1195 (unsigned long)block);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001196 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001197 * Try to see if we can get the block without requesting a new
1198 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001199 */
1200 down_read((&EXT4_I(inode)->i_data_sem));
1201 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1202 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001203 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001204 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001205 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001206 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001207 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001208 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001209
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001210 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'o80e42462009-09-08 08:21:26 -04001211 int ret = check_block_validity(inode, "file system corruption",
1212 block, bh->b_blocknr, retval);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001213 if (ret != 0)
1214 return ret;
1215 }
1216
Mingming Caof5ab0d12008-02-25 15:29:55 -05001217 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001218 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001219 return retval;
1220
1221 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001222 * Returns if the blocks have already allocated
1223 *
1224 * Note that if blocks have been preallocated
1225 * ext4_ext_get_block() returns th create = 0
1226 * with buffer head unmapped.
1227 */
1228 if (retval > 0 && buffer_mapped(bh))
1229 return retval;
1230
1231 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001232 * When we call get_blocks without the create flag, the
1233 * BH_Unwritten flag could have gotten set if the blocks
1234 * requested were part of a uninitialized extent. We need to
1235 * clear this flag now that we are committed to convert all or
1236 * part of the uninitialized extent to be an initialized
1237 * extent. This is because we need to avoid the combination
1238 * of BH_Unwritten and BH_Mapped flags being simultaneously
1239 * set on the buffer_head.
1240 */
1241 clear_buffer_unwritten(bh);
1242
1243 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001244 * New blocks allocate and/or writing to uninitialized extent
1245 * will possibly result in updating i_data, so we take
1246 * the write lock of i_data_sem, and call get_blocks()
1247 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001248 */
1249 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001250
1251 /*
1252 * if the caller is from delayed allocation writeout path
1253 * we have already reserved fs blocks for allocation
1254 * let the underlying get_block() function know to
1255 * avoid double accounting
1256 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001257 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001258 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001259 /*
1260 * We need to check for EXT4 here because migrate
1261 * could have changed the inode type in between
1262 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001263 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1264 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001265 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001266 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001267 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001268 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001269
1270 if (retval > 0 && buffer_new(bh)) {
1271 /*
1272 * We allocated new blocks which will result in
1273 * i_data's format changing. Force the migrate
1274 * to fail by clearing migrate flags
1275 */
Theodore Ts'o1b9c12f2009-09-17 08:32:22 -04001276 EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE;
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001277 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001278 }
Mingming Caod2a17632008-07-14 17:52:37 -04001279
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001280 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001281 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001282
1283 /*
1284 * Update reserved blocks/metadata blocks after successful
1285 * block allocation which had been deferred till now.
1286 */
1287 if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
1288 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001289
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001290 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001291 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'o80e42462009-09-08 08:21:26 -04001292 int ret = check_block_validity(inode, "file system "
1293 "corruption after allocation",
1294 block, bh->b_blocknr, retval);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001295 if (ret != 0)
1296 return ret;
1297 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001298 return retval;
1299}
1300
Mingming Caof3bd1f32008-08-19 22:16:03 -04001301/* Maximum number of blocks we map for direct IO at once. */
1302#define DIO_MAX_BLOCKS 4096
1303
Eric Sandeen6873fa02008-10-07 00:46:36 -04001304int ext4_get_block(struct inode *inode, sector_t iblock,
1305 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001307 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001308 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001310 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311
Jan Kara7fb54092008-02-10 01:08:38 -05001312 if (create && !handle) {
1313 /* Direct IO write... */
1314 if (max_blocks > DIO_MAX_BLOCKS)
1315 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001316 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1317 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001318 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001319 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001320 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 }
Jan Kara7fb54092008-02-10 01:08:38 -05001322 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 }
1324
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001325 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001326 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001327 if (ret > 0) {
1328 bh_result->b_size = (ret << inode->i_blkbits);
1329 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 }
Jan Kara7fb54092008-02-10 01:08:38 -05001331 if (started)
1332 ext4_journal_stop(handle);
1333out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001334 return ret;
1335}
1336
1337/*
1338 * `handle' can be NULL if create is zero
1339 */
Mingming Cao617ba132006-10-11 01:20:53 -07001340struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001341 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342{
1343 struct buffer_head dummy;
1344 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001345 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001346
1347 J_ASSERT(handle != NULL || create == 0);
1348
1349 dummy.b_state = 0;
1350 dummy.b_blocknr = -1000;
1351 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001352 if (create)
1353 flags |= EXT4_GET_BLOCKS_CREATE;
1354 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001355 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001356 * ext4_get_blocks() returns number of blocks mapped. 0 in
1357 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001358 */
1359 if (err > 0) {
1360 if (err > 1)
1361 WARN_ON(1);
1362 err = 0;
1363 }
1364 *errp = err;
1365 if (!err && buffer_mapped(&dummy)) {
1366 struct buffer_head *bh;
1367 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1368 if (!bh) {
1369 *errp = -EIO;
1370 goto err;
1371 }
1372 if (buffer_new(&dummy)) {
1373 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001374 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001375
1376 /*
1377 * Now that we do not always journal data, we should
1378 * keep in mind whether this should always journal the
1379 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001380 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 * problem.
1382 */
1383 lock_buffer(bh);
1384 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001385 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001386 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001387 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001388 set_buffer_uptodate(bh);
1389 }
1390 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001391 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1392 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393 if (!fatal)
1394 fatal = err;
1395 } else {
1396 BUFFER_TRACE(bh, "not a new buffer");
1397 }
1398 if (fatal) {
1399 *errp = fatal;
1400 brelse(bh);
1401 bh = NULL;
1402 }
1403 return bh;
1404 }
1405err:
1406 return NULL;
1407}
1408
Mingming Cao617ba132006-10-11 01:20:53 -07001409struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001410 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001412 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413
Mingming Cao617ba132006-10-11 01:20:53 -07001414 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 if (!bh)
1416 return bh;
1417 if (buffer_uptodate(bh))
1418 return bh;
1419 ll_rw_block(READ_META, 1, &bh);
1420 wait_on_buffer(bh);
1421 if (buffer_uptodate(bh))
1422 return bh;
1423 put_bh(bh);
1424 *err = -EIO;
1425 return NULL;
1426}
1427
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001428static int walk_page_buffers(handle_t *handle,
1429 struct buffer_head *head,
1430 unsigned from,
1431 unsigned to,
1432 int *partial,
1433 int (*fn)(handle_t *handle,
1434 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001435{
1436 struct buffer_head *bh;
1437 unsigned block_start, block_end;
1438 unsigned blocksize = head->b_size;
1439 int err, ret = 0;
1440 struct buffer_head *next;
1441
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001442 for (bh = head, block_start = 0;
1443 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001444 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445 next = bh->b_this_page;
1446 block_end = block_start + blocksize;
1447 if (block_end <= from || block_start >= to) {
1448 if (partial && !buffer_uptodate(bh))
1449 *partial = 1;
1450 continue;
1451 }
1452 err = (*fn)(handle, bh);
1453 if (!ret)
1454 ret = err;
1455 }
1456 return ret;
1457}
1458
1459/*
1460 * To preserve ordering, it is essential that the hole instantiation and
1461 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001462 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001463 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464 * prepare_write() is the right place.
1465 *
Mingming Cao617ba132006-10-11 01:20:53 -07001466 * Also, this function can nest inside ext4_writepage() ->
1467 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 * has generated enough buffer credits to do the whole page. So we won't
1469 * block on the journal in that case, which is good, because the caller may
1470 * be PF_MEMALLOC.
1471 *
Mingming Cao617ba132006-10-11 01:20:53 -07001472 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473 * quota file writes. If we were to commit the transaction while thus
1474 * reentered, there can be a deadlock - we would be holding a quota
1475 * lock, and the commit would never complete if another thread had a
1476 * transaction open and was blocking on the quota lock - a ranking
1477 * violation.
1478 *
Mingming Caodab291a2006-10-11 01:21:01 -07001479 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 * will _not_ run commit under these circumstances because handle->h_ref
1481 * is elevated. We'll still have enough credits for the tiny quotafile
1482 * write.
1483 */
1484static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001485 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486{
1487 if (!buffer_mapped(bh) || buffer_freed(bh))
1488 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001489 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490}
1491
Jan Karab9a42072009-12-08 21:24:33 -05001492/*
1493 * Truncate blocks that were not used by write. We have to truncate the
1494 * pagecache as well so that corresponding buffers get properly unmapped.
1495 */
1496static void ext4_truncate_failed_write(struct inode *inode)
1497{
1498 truncate_inode_pages(inode->i_mapping, inode->i_size);
1499 ext4_truncate(inode);
1500}
1501
Nick Pigginbfc1af62007-10-16 01:25:05 -07001502static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001503 loff_t pos, unsigned len, unsigned flags,
1504 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001506 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001507 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508 handle_t *handle;
1509 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001510 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001511 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001512 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001513
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001514 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001515 /*
1516 * Reserve one block more for addition to orphan list in case
1517 * we allocate blocks but write fails for some reason
1518 */
1519 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001520 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001521 from = pos & (PAGE_CACHE_SIZE - 1);
1522 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523
1524retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001525 handle = ext4_journal_start(inode, needed_blocks);
1526 if (IS_ERR(handle)) {
1527 ret = PTR_ERR(handle);
1528 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001529 }
1530
Jan Karaebd36102009-02-22 21:09:59 -05001531 /* We cannot recurse into the filesystem as the transaction is already
1532 * started */
1533 flags |= AOP_FLAG_NOFS;
1534
Nick Piggin54566b22009-01-04 12:00:53 -08001535 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001536 if (!page) {
1537 ext4_journal_stop(handle);
1538 ret = -ENOMEM;
1539 goto out;
1540 }
1541 *pagep = page;
1542
Nick Pigginbfc1af62007-10-16 01:25:05 -07001543 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001544 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001545
1546 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547 ret = walk_page_buffers(handle, page_buffers(page),
1548 from, to, NULL, do_journal_get_write_access);
1549 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001550
1551 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001552 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001553 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001554 /*
1555 * block_write_begin may have instantiated a few blocks
1556 * outside i_size. Trim these off again. Don't need
1557 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001558 *
1559 * Add inode to orphan list in case we crash before
1560 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001561 */
Jan Karaffacfa72009-07-13 16:22:22 -04001562 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001563 ext4_orphan_add(handle, inode);
1564
1565 ext4_journal_stop(handle);
1566 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001567 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001568 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001569 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001570 * still be on the orphan list; we need to
1571 * make sure the inode is removed from the
1572 * orphan list in that case.
1573 */
1574 if (inode->i_nlink)
1575 ext4_orphan_del(NULL, inode);
1576 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001577 }
1578
Mingming Cao617ba132006-10-11 01:20:53 -07001579 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001581out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 return ret;
1583}
1584
Nick Pigginbfc1af62007-10-16 01:25:05 -07001585/* For write_end() in data=journal mode */
1586static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587{
1588 if (!buffer_mapped(bh) || buffer_freed(bh))
1589 return 0;
1590 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001591 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592}
1593
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001594static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001595 struct address_space *mapping,
1596 loff_t pos, unsigned len, unsigned copied,
1597 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001598{
1599 int i_size_changed = 0;
1600 struct inode *inode = mapping->host;
1601 handle_t *handle = ext4_journal_current_handle();
1602
1603 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1604
1605 /*
1606 * No need to use i_size_read() here, the i_size
1607 * cannot change under us because we hold i_mutex.
1608 *
1609 * But it's important to update i_size while still holding page lock:
1610 * page writeout could otherwise come in and zero beyond i_size.
1611 */
1612 if (pos + copied > inode->i_size) {
1613 i_size_write(inode, pos + copied);
1614 i_size_changed = 1;
1615 }
1616
1617 if (pos + copied > EXT4_I(inode)->i_disksize) {
1618 /* We need to mark inode dirty even if
1619 * new_i_size is less that inode->i_size
1620 * bu greater than i_disksize.(hint delalloc)
1621 */
1622 ext4_update_i_disksize(inode, (pos + copied));
1623 i_size_changed = 1;
1624 }
1625 unlock_page(page);
1626 page_cache_release(page);
1627
1628 /*
1629 * Don't mark the inode dirty under page lock. First, it unnecessarily
1630 * makes the holding time of page lock longer. Second, it forces lock
1631 * ordering of page lock and transaction start for journaling
1632 * filesystems.
1633 */
1634 if (i_size_changed)
1635 ext4_mark_inode_dirty(handle, inode);
1636
1637 return copied;
1638}
1639
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640/*
1641 * We need to pick up the new inode size which generic_commit_write gave us
1642 * `file' can be NULL - eg, when called from page_symlink().
1643 *
Mingming Cao617ba132006-10-11 01:20:53 -07001644 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645 * buffers are managed internally.
1646 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001647static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001648 struct address_space *mapping,
1649 loff_t pos, unsigned len, unsigned copied,
1650 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651{
Mingming Cao617ba132006-10-11 01:20:53 -07001652 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001653 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654 int ret = 0, ret2;
1655
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001656 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001657 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658
1659 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001660 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001661 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001662 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001663 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001664 /* if we have allocated more blocks and copied
1665 * less. We will have blocks allocated outside
1666 * inode->i_size. So truncate them
1667 */
1668 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001669 if (ret2 < 0)
1670 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001671 }
Mingming Cao617ba132006-10-11 01:20:53 -07001672 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 if (!ret)
1674 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001675
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001676 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001677 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001678 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001679 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001680 * on the orphan list; we need to make sure the inode
1681 * is removed from the orphan list in that case.
1682 */
1683 if (inode->i_nlink)
1684 ext4_orphan_del(NULL, inode);
1685 }
1686
1687
Nick Pigginbfc1af62007-10-16 01:25:05 -07001688 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689}
1690
Nick Pigginbfc1af62007-10-16 01:25:05 -07001691static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001692 struct address_space *mapping,
1693 loff_t pos, unsigned len, unsigned copied,
1694 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695{
Mingming Cao617ba132006-10-11 01:20:53 -07001696 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001697 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001699
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001700 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001701 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001702 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001703 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001704 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001705 /* if we have allocated more blocks and copied
1706 * less. We will have blocks allocated outside
1707 * inode->i_size. So truncate them
1708 */
1709 ext4_orphan_add(handle, inode);
1710
Roel Kluinf8a87d82008-04-29 22:01:18 -04001711 if (ret2 < 0)
1712 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713
Mingming Cao617ba132006-10-11 01:20:53 -07001714 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001715 if (!ret)
1716 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001717
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001718 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001719 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001720 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001721 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001722 * on the orphan list; we need to make sure the inode
1723 * is removed from the orphan list in that case.
1724 */
1725 if (inode->i_nlink)
1726 ext4_orphan_del(NULL, inode);
1727 }
1728
Nick Pigginbfc1af62007-10-16 01:25:05 -07001729 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730}
1731
Nick Pigginbfc1af62007-10-16 01:25:05 -07001732static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001733 struct address_space *mapping,
1734 loff_t pos, unsigned len, unsigned copied,
1735 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736{
Mingming Cao617ba132006-10-11 01:20:53 -07001737 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001738 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001739 int ret = 0, ret2;
1740 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001741 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001742 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001744 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001745 from = pos & (PAGE_CACHE_SIZE - 1);
1746 to = from + len;
1747
1748 if (copied < len) {
1749 if (!PageUptodate(page))
1750 copied = 0;
1751 page_zero_new_buffers(page, from+copied, to);
1752 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001753
1754 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001755 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001756 if (!partial)
1757 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001758 new_i_size = pos + copied;
1759 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001760 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001761 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001762 if (new_i_size > EXT4_I(inode)->i_disksize) {
1763 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001764 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765 if (!ret)
1766 ret = ret2;
1767 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001768
Jan Karacf108bc2008-07-11 19:27:31 -04001769 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001770 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001771 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001772 /* if we have allocated more blocks and copied
1773 * less. We will have blocks allocated outside
1774 * inode->i_size. So truncate them
1775 */
1776 ext4_orphan_add(handle, inode);
1777
Mingming Cao617ba132006-10-11 01:20:53 -07001778 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779 if (!ret)
1780 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001781 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001782 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001783 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001784 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001785 * on the orphan list; we need to make sure the inode
1786 * is removed from the orphan list in that case.
1787 */
1788 if (inode->i_nlink)
1789 ext4_orphan_del(NULL, inode);
1790 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001791
1792 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793}
Mingming Caod2a17632008-07-14 17:52:37 -04001794
1795static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1796{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001797 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001798 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1799 unsigned long md_needed, mdblocks, total = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001800
1801 /*
1802 * recalculate the amount of metadata blocks to reserve
1803 * in order to allocate nrblocks
1804 * worse case is one extent per block
1805 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001806repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001807 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1808 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1809 mdblocks = ext4_calc_metadata_amount(inode, total);
1810 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1811
1812 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1813 total = md_needed + nrblocks;
1814
Mingming Cao60e58e02009-01-22 18:13:05 +01001815 /*
1816 * Make quota reservation here to prevent quota overflow
1817 * later. Real quota accounting is done at pages writeout
1818 * time.
1819 */
1820 if (vfs_dq_reserve_block(inode, total)) {
1821 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1822 return -EDQUOT;
1823 }
1824
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001825 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001826 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao9f0ccfd2009-09-28 15:49:52 -04001827 vfs_dq_release_reservation_block(inode, total);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001828 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1829 yield();
1830 goto repeat;
1831 }
Mingming Caod2a17632008-07-14 17:52:37 -04001832 return -ENOSPC;
1833 }
Mingming Caod2a17632008-07-14 17:52:37 -04001834 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1835 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1836
1837 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1838 return 0; /* success */
1839}
1840
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001841static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001842{
1843 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1844 int total, mdb, mdb_free, release;
1845
Mingming Caocd213222008-08-19 22:16:59 -04001846 if (!to_free)
1847 return; /* Nothing to release, exit */
1848
Mingming Caod2a17632008-07-14 17:52:37 -04001849 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001850
1851 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1852 /*
1853 * if there is no reserved blocks, but we try to free some
1854 * then the counter is messed up somewhere.
1855 * but since this function is called from invalidate
1856 * page, it's harmless to return without any action
1857 */
1858 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1859 "blocks for inode %lu, but there is no reserved "
1860 "data blocks\n", to_free, inode->i_ino);
1861 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1862 return;
1863 }
1864
Mingming Caod2a17632008-07-14 17:52:37 -04001865 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001866 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001867 mdb = ext4_calc_metadata_amount(inode, total);
1868
1869 /* figure out how many metablocks to release */
1870 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1871 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1872
Mingming Caod2a17632008-07-14 17:52:37 -04001873 release = to_free + mdb_free;
1874
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001875 /* update fs dirty blocks counter for truncate case */
1876 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001877
1878 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001879 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1880 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001881
1882 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1883 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001884 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001885
1886 vfs_dq_release_reservation_block(inode, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001887}
1888
1889static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001890 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001891{
1892 int to_release = 0;
1893 struct buffer_head *head, *bh;
1894 unsigned int curr_off = 0;
1895
1896 head = page_buffers(page);
1897 bh = head;
1898 do {
1899 unsigned int next_off = curr_off + bh->b_size;
1900
1901 if ((offset <= curr_off) && (buffer_delay(bh))) {
1902 to_release++;
1903 clear_buffer_delay(bh);
1904 }
1905 curr_off = next_off;
1906 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001907 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001908}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001909
1910/*
Alex Tomas64769242008-07-11 19:27:31 -04001911 * Delayed allocation stuff
1912 */
1913
Alex Tomas64769242008-07-11 19:27:31 -04001914/*
1915 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001916 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001917 *
1918 * @mpd->inode: inode
1919 * @mpd->first_page: first page of the extent
1920 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001921 *
1922 * By the time mpage_da_submit_io() is called we expect all blocks
1923 * to be allocated. this may be wrong if allocation failed.
1924 *
1925 * As pages are already locked by write_cache_pages(), we can't use it
1926 */
1927static int mpage_da_submit_io(struct mpage_da_data *mpd)
1928{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001929 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001930 struct pagevec pvec;
1931 unsigned long index, end;
1932 int ret = 0, err, nr_pages, i;
1933 struct inode *inode = mpd->inode;
1934 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001935
1936 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001937 /*
1938 * We need to start from the first_page to the next_page - 1
1939 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001940 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001941 * at the currently mapped buffer_heads.
1942 */
Alex Tomas64769242008-07-11 19:27:31 -04001943 index = mpd->first_page;
1944 end = mpd->next_page - 1;
1945
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001946 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001947 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001948 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001949 if (nr_pages == 0)
1950 break;
1951 for (i = 0; i < nr_pages; i++) {
1952 struct page *page = pvec.pages[i];
1953
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001954 index = page->index;
1955 if (index > end)
1956 break;
1957 index++;
1958
1959 BUG_ON(!PageLocked(page));
1960 BUG_ON(PageWriteback(page));
1961
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001962 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001963 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001964 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1965 /*
1966 * have successfully written the page
1967 * without skipping the same
1968 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001969 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001970 /*
1971 * In error case, we have to continue because
1972 * remaining pages are still locked
1973 * XXX: unlock and re-dirty them?
1974 */
1975 if (ret == 0)
1976 ret = err;
1977 }
1978 pagevec_release(&pvec);
1979 }
Alex Tomas64769242008-07-11 19:27:31 -04001980 return ret;
1981}
1982
1983/*
1984 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1985 *
1986 * @mpd->inode - inode to walk through
1987 * @exbh->b_blocknr - first block on a disk
1988 * @exbh->b_size - amount of space in bytes
1989 * @logical - first logical block to start assignment with
1990 *
1991 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001992 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04001993 */
1994static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1995 struct buffer_head *exbh)
1996{
1997 struct inode *inode = mpd->inode;
1998 struct address_space *mapping = inode->i_mapping;
1999 int blocks = exbh->b_size >> inode->i_blkbits;
2000 sector_t pblock = exbh->b_blocknr, cur_logical;
2001 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002002 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04002003 struct pagevec pvec;
2004 int nr_pages, i;
2005
2006 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2007 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2008 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2009
2010 pagevec_init(&pvec, 0);
2011
2012 while (index <= end) {
2013 /* XXX: optimize tail */
2014 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2015 if (nr_pages == 0)
2016 break;
2017 for (i = 0; i < nr_pages; i++) {
2018 struct page *page = pvec.pages[i];
2019
2020 index = page->index;
2021 if (index > end)
2022 break;
2023 index++;
2024
2025 BUG_ON(!PageLocked(page));
2026 BUG_ON(PageWriteback(page));
2027 BUG_ON(!page_has_buffers(page));
2028
2029 bh = page_buffers(page);
2030 head = bh;
2031
2032 /* skip blocks out of the range */
2033 do {
2034 if (cur_logical >= logical)
2035 break;
2036 cur_logical++;
2037 } while ((bh = bh->b_this_page) != head);
2038
2039 do {
2040 if (cur_logical >= logical + blocks)
2041 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002042
2043 if (buffer_delay(bh) ||
2044 buffer_unwritten(bh)) {
2045
2046 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2047
2048 if (buffer_delay(bh)) {
2049 clear_buffer_delay(bh);
2050 bh->b_blocknr = pblock;
2051 } else {
2052 /*
2053 * unwritten already should have
2054 * blocknr assigned. Verify that
2055 */
2056 clear_buffer_unwritten(bh);
2057 BUG_ON(bh->b_blocknr != pblock);
2058 }
2059
Mingming Cao61628a32008-07-11 19:27:31 -04002060 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002061 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002062
2063 cur_logical++;
2064 pblock++;
2065 } while ((bh = bh->b_this_page) != head);
2066 }
2067 pagevec_release(&pvec);
2068 }
2069}
2070
2071
2072/*
2073 * __unmap_underlying_blocks - just a helper function to unmap
2074 * set of blocks described by @bh
2075 */
2076static inline void __unmap_underlying_blocks(struct inode *inode,
2077 struct buffer_head *bh)
2078{
2079 struct block_device *bdev = inode->i_sb->s_bdev;
2080 int blocks, i;
2081
2082 blocks = bh->b_size >> inode->i_blkbits;
2083 for (i = 0; i < blocks; i++)
2084 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2085}
2086
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002087static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2088 sector_t logical, long blk_cnt)
2089{
2090 int nr_pages, i;
2091 pgoff_t index, end;
2092 struct pagevec pvec;
2093 struct inode *inode = mpd->inode;
2094 struct address_space *mapping = inode->i_mapping;
2095
2096 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2097 end = (logical + blk_cnt - 1) >>
2098 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2099 while (index <= end) {
2100 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2101 if (nr_pages == 0)
2102 break;
2103 for (i = 0; i < nr_pages; i++) {
2104 struct page *page = pvec.pages[i];
2105 index = page->index;
2106 if (index > end)
2107 break;
2108 index++;
2109
2110 BUG_ON(!PageLocked(page));
2111 BUG_ON(PageWriteback(page));
2112 block_invalidatepage(page, 0);
2113 ClearPageUptodate(page);
2114 unlock_page(page);
2115 }
2116 }
2117 return;
2118}
2119
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002120static void ext4_print_free_blocks(struct inode *inode)
2121{
2122 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o16939182009-09-26 17:43:59 -04002123 printk(KERN_CRIT "Total free blocks count %lld\n",
2124 ext4_count_free_blocks(inode->i_sb));
2125 printk(KERN_CRIT "Free/Dirty block details\n");
2126 printk(KERN_CRIT "free_blocks=%lld\n",
2127 (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
2128 printk(KERN_CRIT "dirty_blocks=%lld\n",
2129 (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2130 printk(KERN_CRIT "Block reservation details\n");
2131 printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
2132 EXT4_I(inode)->i_reserved_data_blocks);
2133 printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
2134 EXT4_I(inode)->i_reserved_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002135 return;
2136}
2137
Theodore Ts'ob920c752009-05-14 00:54:29 -04002138/*
Alex Tomas64769242008-07-11 19:27:31 -04002139 * mpage_da_map_blocks - go through given space
2140 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002141 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002142 *
2143 * The function skips space we know is already mapped to disk blocks.
2144 *
Alex Tomas64769242008-07-11 19:27:31 -04002145 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002146static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002147{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002148 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002149 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002150 sector_t next = mpd->b_blocknr;
2151 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2152 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2153 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002154
2155 /*
2156 * We consider only non-mapped and non-allocated blocks
2157 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002158 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002159 !(mpd->b_state & (1 << BH_Delay)) &&
2160 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002161 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002162
2163 /*
2164 * If we didn't accumulate anything to write simply return
2165 */
2166 if (!mpd->b_size)
2167 return 0;
2168
2169 handle = ext4_journal_current_handle();
2170 BUG_ON(!handle);
2171
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002172 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002173 * Call ext4_get_blocks() to allocate any delayed allocation
2174 * blocks, or to convert an uninitialized extent to be
2175 * initialized (in the case where we have written into
2176 * one or more preallocated blocks).
2177 *
2178 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2179 * indicate that we are on the delayed allocation path. This
2180 * affects functions in many different parts of the allocation
2181 * call path. This flag exists primarily because we don't
2182 * want to change *many* call functions, so ext4_get_blocks()
2183 * will set the magic i_delalloc_reserved_flag once the
2184 * inode's allocation semaphore is taken.
2185 *
2186 * If the blocks in questions were delalloc blocks, set
2187 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2188 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002189 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002190 new.b_state = 0;
2191 get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
2192 EXT4_GET_BLOCKS_DELALLOC_RESERVE);
2193 if (mpd->b_state & (1 << BH_Delay))
2194 get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002195 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002196 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002197 if (blks < 0) {
2198 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002199 /*
2200 * If get block returns with error we simply
2201 * return. Later writepage will redirty the page and
2202 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002203 */
2204 if (err == -EAGAIN)
2205 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002206
2207 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002208 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002209 mpd->retval = err;
2210 return 0;
2211 }
2212
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002213 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002214 * get block failure will cause us to loop in
2215 * writepages, because a_ops->writepage won't be able
2216 * to make progress. The page will be redirtied by
2217 * writepage and writepages will again try to write
2218 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002219 */
Theodore Ts'o16939182009-09-26 17:43:59 -04002220 ext4_msg(mpd->inode->i_sb, KERN_CRIT,
2221 "delayed block allocation failed for inode %lu at "
2222 "logical offset %llu with max blocks %zd with "
2223 "error %d\n", mpd->inode->i_ino,
2224 (unsigned long long) next,
2225 mpd->b_size >> mpd->inode->i_blkbits, err);
2226 printk(KERN_CRIT "This should not happen!! "
2227 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002228 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002229 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002230 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002231 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002232 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002233 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002234 return err;
2235 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002236 BUG_ON(blks == 0);
2237
2238 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002239
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002240 if (buffer_new(&new))
2241 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002242
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002243 /*
2244 * If blocks are delayed marked, we need to
2245 * put actual blocknr and drop delayed bit
2246 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002247 if ((mpd->b_state & (1 << BH_Delay)) ||
2248 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002249 mpage_put_bnr_to_bhs(mpd, next, &new);
2250
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002251 if (ext4_should_order_data(mpd->inode)) {
2252 err = ext4_jbd2_file_inode(handle, mpd->inode);
2253 if (err)
2254 return err;
2255 }
2256
2257 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002258 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002259 */
2260 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2261 if (disksize > i_size_read(mpd->inode))
2262 disksize = i_size_read(mpd->inode);
2263 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2264 ext4_update_i_disksize(mpd->inode, disksize);
2265 return ext4_mark_inode_dirty(handle, mpd->inode);
2266 }
2267
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002268 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002269}
2270
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002271#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2272 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002273
2274/*
2275 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2276 *
2277 * @mpd->lbh - extent of blocks
2278 * @logical - logical number of the block in the file
2279 * @bh - bh of the block (used to access block's state)
2280 *
2281 * the function is used to collect contig. blocks in same state
2282 */
2283static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002284 sector_t logical, size_t b_size,
2285 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002286{
Alex Tomas64769242008-07-11 19:27:31 -04002287 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002288 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002289
Mingming Cao525f4ed2008-08-19 22:15:58 -04002290 /* check if thereserved journal credits might overflow */
2291 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2292 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2293 /*
2294 * With non-extent format we are limited by the journal
2295 * credit available. Total credit needed to insert
2296 * nrblocks contiguous blocks is dependent on the
2297 * nrblocks. So limit nrblocks.
2298 */
2299 goto flush_it;
2300 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2301 EXT4_MAX_TRANS_DATA) {
2302 /*
2303 * Adding the new buffer_head would make it cross the
2304 * allowed limit for which we have journal credit
2305 * reserved. So limit the new bh->b_size
2306 */
2307 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2308 mpd->inode->i_blkbits;
2309 /* we will do mpage_da_submit_io in the next loop */
2310 }
2311 }
Alex Tomas64769242008-07-11 19:27:31 -04002312 /*
2313 * First block in the extent
2314 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002315 if (mpd->b_size == 0) {
2316 mpd->b_blocknr = logical;
2317 mpd->b_size = b_size;
2318 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002319 return;
2320 }
2321
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002322 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002323 /*
2324 * Can we merge the block to our big extent?
2325 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002326 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2327 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002328 return;
2329 }
2330
Mingming Cao525f4ed2008-08-19 22:15:58 -04002331flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002332 /*
2333 * We couldn't merge the block to our extent, so we
2334 * need to flush current extent and start new one
2335 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002336 if (mpage_da_map_blocks(mpd) == 0)
2337 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002338 mpd->io_done = 1;
2339 return;
Alex Tomas64769242008-07-11 19:27:31 -04002340}
2341
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002342static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002343{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002344 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002345}
2346
Alex Tomas64769242008-07-11 19:27:31 -04002347/*
2348 * __mpage_da_writepage - finds extent of pages and blocks
2349 *
2350 * @page: page to consider
2351 * @wbc: not used, we just follow rules
2352 * @data: context
2353 *
2354 * The function finds extents of pages and scan them for all blocks.
2355 */
2356static int __mpage_da_writepage(struct page *page,
2357 struct writeback_control *wbc, void *data)
2358{
2359 struct mpage_da_data *mpd = data;
2360 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002361 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002362 sector_t logical;
2363
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002364 if (mpd->io_done) {
2365 /*
2366 * Rest of the page in the page_vec
2367 * redirty then and skip then. We will
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002368 * try to write them again after
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002369 * starting a new transaction
2370 */
2371 redirty_page_for_writepage(wbc, page);
2372 unlock_page(page);
2373 return MPAGE_DA_EXTENT_TAIL;
2374 }
Alex Tomas64769242008-07-11 19:27:31 -04002375 /*
2376 * Can we merge this page to current extent?
2377 */
2378 if (mpd->next_page != page->index) {
2379 /*
2380 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002381 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002382 */
2383 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002384 if (mpage_da_map_blocks(mpd) == 0)
2385 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002386 /*
2387 * skip rest of the page in the page_vec
2388 */
2389 mpd->io_done = 1;
2390 redirty_page_for_writepage(wbc, page);
2391 unlock_page(page);
2392 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002393 }
2394
2395 /*
2396 * Start next extent of pages ...
2397 */
2398 mpd->first_page = page->index;
2399
2400 /*
2401 * ... and blocks
2402 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002403 mpd->b_size = 0;
2404 mpd->b_state = 0;
2405 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002406 }
2407
2408 mpd->next_page = page->index + 1;
2409 logical = (sector_t) page->index <<
2410 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2411
2412 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002413 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2414 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002415 if (mpd->io_done)
2416 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002417 } else {
2418 /*
2419 * Page with regular buffer heads, just add all dirty ones
2420 */
2421 head = page_buffers(page);
2422 bh = head;
2423 do {
2424 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002425 /*
2426 * We need to try to allocate
2427 * unmapped blocks in the same page.
2428 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002429 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002430 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002431 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002432 mpage_add_bh_to_extent(mpd, logical,
2433 bh->b_size,
2434 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002435 if (mpd->io_done)
2436 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002437 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2438 /*
2439 * mapped dirty buffer. We need to update
2440 * the b_state because we look at
2441 * b_state in mpage_da_map_blocks. We don't
2442 * update b_size because if we find an
2443 * unmapped buffer_head later we need to
2444 * use the b_state flag of that buffer_head.
2445 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002446 if (mpd->b_size == 0)
2447 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002448 }
Alex Tomas64769242008-07-11 19:27:31 -04002449 logical++;
2450 } while ((bh = bh->b_this_page) != head);
2451 }
2452
2453 return 0;
2454}
2455
2456/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002457 * This is a special get_blocks_t callback which is used by
2458 * ext4_da_write_begin(). It will either return mapped block or
2459 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002460 *
2461 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2462 * We also have b_blocknr = -1 and b_bdev initialized properly
2463 *
2464 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2465 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2466 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002467 */
2468static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2469 struct buffer_head *bh_result, int create)
2470{
2471 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002472 sector_t invalid_block = ~((sector_t) 0xffff);
2473
2474 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2475 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002476
2477 BUG_ON(create == 0);
2478 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2479
2480 /*
2481 * first, we need to know whether the block is allocated already
2482 * preallocated blocks are unmapped but should treated
2483 * the same as allocated blocks.
2484 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002485 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002486 if ((ret == 0) && !buffer_delay(bh_result)) {
2487 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002488 /*
2489 * XXX: __block_prepare_write() unmaps passed block,
2490 * is it OK?
2491 */
Mingming Caod2a17632008-07-14 17:52:37 -04002492 ret = ext4_da_reserve_space(inode, 1);
2493 if (ret)
2494 /* not enough space to reserve */
2495 return ret;
2496
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002497 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002498 set_buffer_new(bh_result);
2499 set_buffer_delay(bh_result);
2500 } else if (ret > 0) {
2501 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002502 if (buffer_unwritten(bh_result)) {
2503 /* A delayed write to unwritten bh should
2504 * be marked new and mapped. Mapped ensures
2505 * that we don't do get_block multiple times
2506 * when we write to the same offset and new
2507 * ensures that we do proper zero out for
2508 * partial write.
2509 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002510 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002511 set_buffer_mapped(bh_result);
2512 }
Alex Tomas64769242008-07-11 19:27:31 -04002513 ret = 0;
2514 }
2515
2516 return ret;
2517}
Mingming Cao61628a32008-07-11 19:27:31 -04002518
Theodore Ts'ob920c752009-05-14 00:54:29 -04002519/*
2520 * This function is used as a standard get_block_t calback function
2521 * when there is no desire to allocate any blocks. It is used as a
2522 * callback function for block_prepare_write(), nobh_writepage(), and
2523 * block_write_full_page(). These functions should only try to map a
2524 * single block at a time.
2525 *
2526 * Since this function doesn't do block allocations even if the caller
2527 * requests it by passing in create=1, it is critically important that
2528 * any caller checks to make sure that any buffer heads are returned
2529 * by this function are either all already mapped or marked for
2530 * delayed allocation before calling nobh_writepage() or
2531 * block_write_full_page(). Otherwise, b_blocknr could be left
2532 * unitialized, and the page write functions will be taken by
2533 * surprise.
2534 */
2535static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002536 struct buffer_head *bh_result, int create)
2537{
2538 int ret = 0;
2539 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2540
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002541 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2542
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002543 /*
2544 * we don't want to do block allocation in writepage
2545 * so call get_block_wrap with create = 0
2546 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002547 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002548 if (ret > 0) {
2549 bh_result->b_size = (ret << inode->i_blkbits);
2550 ret = 0;
2551 }
2552 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002553}
2554
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002555static int bget_one(handle_t *handle, struct buffer_head *bh)
2556{
2557 get_bh(bh);
2558 return 0;
2559}
2560
2561static int bput_one(handle_t *handle, struct buffer_head *bh)
2562{
2563 put_bh(bh);
2564 return 0;
2565}
2566
2567static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002568 unsigned int len)
2569{
2570 struct address_space *mapping = page->mapping;
2571 struct inode *inode = mapping->host;
2572 struct buffer_head *page_bufs;
2573 handle_t *handle = NULL;
2574 int ret = 0;
2575 int err;
2576
2577 page_bufs = page_buffers(page);
2578 BUG_ON(!page_bufs);
2579 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2580 /* As soon as we unlock the page, it can go away, but we have
2581 * references to buffers so we are safe */
2582 unlock_page(page);
2583
2584 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2585 if (IS_ERR(handle)) {
2586 ret = PTR_ERR(handle);
2587 goto out;
2588 }
2589
2590 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2591 do_journal_get_write_access);
2592
2593 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2594 write_end_fn);
2595 if (ret == 0)
2596 ret = err;
2597 err = ext4_journal_stop(handle);
2598 if (!ret)
2599 ret = err;
2600
2601 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
2602 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2603out:
2604 return ret;
2605}
2606
Mingming Cao61628a32008-07-11 19:27:31 -04002607/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002608 * Note that we don't need to start a transaction unless we're journaling data
2609 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2610 * need to file the inode to the transaction's list in ordered mode because if
2611 * we are writing back data added by write(), the inode is already there and if
2612 * we are writing back data modified via mmap(), noone guarantees in which
2613 * transaction the data will hit the disk. In case we are journaling data, we
2614 * cannot start transaction directly because transaction start ranks above page
2615 * lock so we have to do some magic.
2616 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002617 * This function can get called via...
2618 * - ext4_da_writepages after taking page lock (have journal handle)
2619 * - journal_submit_inode_data_buffers (no journal handle)
2620 * - shrink_page_list via pdflush (no journal handle)
2621 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002622 *
2623 * We don't do any block allocation in this function. If we have page with
2624 * multiple blocks we need to write those buffer_heads that are mapped. This
2625 * is important for mmaped based write. So if we do with blocksize 1K
2626 * truncate(f, 1024);
2627 * a = mmap(f, 0, 4096);
2628 * a[0] = 'a';
2629 * truncate(f, 4096);
2630 * we have in the page first buffer_head mapped via page_mkwrite call back
2631 * but other bufer_heads would be unmapped but dirty(dirty done via the
2632 * do_wp_page). So writepage should write the first block. If we modify
2633 * the mmap area beyond 1024 we will again get a page_fault and the
2634 * page_mkwrite callback will do the block allocation and mark the
2635 * buffer_heads mapped.
2636 *
2637 * We redirty the page if we have any buffer_heads that is either delay or
2638 * unwritten in the page.
2639 *
2640 * We can get recursively called as show below.
2641 *
2642 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2643 * ext4_writepage()
2644 *
2645 * But since we don't do any block allocation we should not deadlock.
2646 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002647 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002648static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002649 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002650{
Alex Tomas64769242008-07-11 19:27:31 -04002651 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002652 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002653 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002654 struct buffer_head *page_bufs;
2655 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002656
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002657 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002658 size = i_size_read(inode);
2659 if (page->index == size >> PAGE_CACHE_SHIFT)
2660 len = size & ~PAGE_CACHE_MASK;
2661 else
2662 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002663
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002664 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002665 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002666 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002667 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002668 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002669 * We don't want to do block allocation
2670 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002671 * We may reach here when we do a journal commit
2672 * via journal_submit_inode_data_buffers.
2673 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002674 * them. We can also reach here via shrink_page_list
2675 */
2676 redirty_page_for_writepage(wbc, page);
2677 unlock_page(page);
2678 return 0;
2679 }
2680 } else {
2681 /*
2682 * The test for page_has_buffers() is subtle:
2683 * We know the page is dirty but it lost buffers. That means
2684 * that at some moment in time after write_begin()/write_end()
2685 * has been called all buffers have been clean and thus they
2686 * must have been written at least once. So they are all
2687 * mapped and we can happily proceed with mapping them
2688 * and writing the page.
2689 *
2690 * Try to initialize the buffer_heads and check whether
2691 * all are mapped and non delay. We don't want to
2692 * do block allocation here.
2693 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002694 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002695 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002696 if (!ret) {
2697 page_bufs = page_buffers(page);
2698 /* check whether all are mapped and non delay */
2699 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002700 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002701 redirty_page_for_writepage(wbc, page);
2702 unlock_page(page);
2703 return 0;
2704 }
2705 } else {
2706 /*
2707 * We can't do block allocation here
2708 * so just redity the page and unlock
2709 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002710 */
Mingming Cao61628a32008-07-11 19:27:31 -04002711 redirty_page_for_writepage(wbc, page);
2712 unlock_page(page);
2713 return 0;
2714 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002715 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002716 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002717 }
2718
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002719 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2720 /*
2721 * It's mmapped pagecache. Add buffers and journal it. There
2722 * doesn't seem much point in redirtying the page here.
2723 */
2724 ClearPageChecked(page);
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002725 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002726 }
2727
Alex Tomas64769242008-07-11 19:27:31 -04002728 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002729 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002730 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002731 ret = block_write_full_page(page, noalloc_get_block_write,
2732 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002733
Alex Tomas64769242008-07-11 19:27:31 -04002734 return ret;
2735}
2736
Mingming Cao61628a32008-07-11 19:27:31 -04002737/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002738 * This is called via ext4_da_writepages() to
2739 * calulate the total number of credits to reserve to fit
2740 * a single extent allocation into a single transaction,
2741 * ext4_da_writpeages() will loop calling this before
2742 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002743 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002744
2745static int ext4_da_writepages_trans_blocks(struct inode *inode)
2746{
2747 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2748
2749 /*
2750 * With non-extent format the journal credit needed to
2751 * insert nrblocks contiguous block is dependent on
2752 * number of contiguous block. So we will limit
2753 * number of contiguous block to a sane value
2754 */
Julia Lawall30c6e07a2009-11-15 15:30:58 -05002755 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) &&
Mingming Cao525f4ed2008-08-19 22:15:58 -04002756 (max_blocks > EXT4_MAX_TRANS_DATA))
2757 max_blocks = EXT4_MAX_TRANS_DATA;
2758
2759 return ext4_chunk_trans_blocks(inode, max_blocks);
2760}
Mingming Cao61628a32008-07-11 19:27:31 -04002761
Alex Tomas64769242008-07-11 19:27:31 -04002762static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002763 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002764{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002765 pgoff_t index;
2766 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002767 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002768 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002769 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002770 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002771 int pages_written = 0;
2772 long pages_skipped;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002773 unsigned int max_pages;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002774 int range_cyclic, cycled = 1, io_done = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002775 int needed_blocks, ret = 0;
2776 long desired_nr_to_write, nr_to_writebump = 0;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002777 loff_t range_start = wbc->range_start;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002778 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002779
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002780 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002781
Mingming Cao61628a32008-07-11 19:27:31 -04002782 /*
2783 * No pages to write? This is mainly a kludge to avoid starting
2784 * a transaction for special inodes like journal inode on last iput()
2785 * because that could violate lock ordering on umount
2786 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002787 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002788 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002789
2790 /*
2791 * If the filesystem has aborted, it is read-only, so return
2792 * right away instead of dumping stack traces later on that
2793 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002794 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002795 * the latter could be true if the filesystem is mounted
2796 * read-only, and in that case, ext4_da_writepages should
2797 * *never* be called, so if that ever happens, we would want
2798 * the stack trace.
2799 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002800 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002801 return -EROFS;
2802
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002803 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2804 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002805
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002806 range_cyclic = wbc->range_cyclic;
2807 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002808 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002809 if (index)
2810 cycled = 0;
2811 wbc->range_start = index << PAGE_CACHE_SHIFT;
2812 wbc->range_end = LLONG_MAX;
2813 wbc->range_cyclic = 0;
2814 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002815 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002816
Theodore Ts'o55138e02009-09-29 13:31:31 -04002817 /*
2818 * This works around two forms of stupidity. The first is in
2819 * the writeback code, which caps the maximum number of pages
2820 * written to be 1024 pages. This is wrong on multiple
2821 * levels; different architectues have a different page size,
2822 * which changes the maximum amount of data which gets
2823 * written. Secondly, 4 megabytes is way too small. XFS
2824 * forces this value to be 16 megabytes by multiplying
2825 * nr_to_write parameter by four, and then relies on its
2826 * allocator to allocate larger extents to make them
2827 * contiguous. Unfortunately this brings us to the second
2828 * stupidity, which is that ext4's mballoc code only allocates
2829 * at most 2048 blocks. So we force contiguous writes up to
2830 * the number of dirty blocks in the inode, or
2831 * sbi->max_writeback_mb_bump whichever is smaller.
2832 */
2833 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2834 if (!range_cyclic && range_whole)
2835 desired_nr_to_write = wbc->nr_to_write * 8;
2836 else
2837 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2838 max_pages);
2839 if (desired_nr_to_write > max_pages)
2840 desired_nr_to_write = max_pages;
2841
2842 if (wbc->nr_to_write < desired_nr_to_write) {
2843 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2844 wbc->nr_to_write = desired_nr_to_write;
2845 }
2846
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002847 mpd.wbc = wbc;
2848 mpd.inode = mapping->host;
2849
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002850 /*
2851 * we don't want write_cache_pages to update
2852 * nr_to_write and writeback_index
2853 */
2854 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2855 wbc->no_nrwrite_index_update = 1;
2856 pages_skipped = wbc->pages_skipped;
2857
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002858retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002859 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002860
2861 /*
2862 * we insert one extent at a time. So we need
2863 * credit needed for single extent allocation.
2864 * journalled mode is currently not supported
2865 * by delalloc
2866 */
2867 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002868 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002869
Mingming Cao61628a32008-07-11 19:27:31 -04002870 /* start a new transaction*/
2871 handle = ext4_journal_start(inode, needed_blocks);
2872 if (IS_ERR(handle)) {
2873 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002874 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002875 "%ld pages, ino %lu; err %d\n", __func__,
2876 wbc->nr_to_write, inode->i_ino, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002877 goto out_writepages;
2878 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002879
2880 /*
2881 * Now call __mpage_da_writepage to find the next
2882 * contiguous region of logical blocks that need
2883 * blocks to be allocated by ext4. We don't actually
2884 * submit the blocks for I/O here, even though
2885 * write_cache_pages thinks it will, and will set the
2886 * pages as clean for write before calling
2887 * __mpage_da_writepage().
2888 */
2889 mpd.b_size = 0;
2890 mpd.b_state = 0;
2891 mpd.b_blocknr = 0;
2892 mpd.first_page = 0;
2893 mpd.next_page = 0;
2894 mpd.io_done = 0;
2895 mpd.pages_written = 0;
2896 mpd.retval = 0;
2897 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2898 &mpd);
2899 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002900 * If we have a contiguous extent of pages and we
Theodore Ts'of63e6002009-02-23 16:42:39 -05002901 * haven't done the I/O yet, map the blocks and submit
2902 * them for I/O.
2903 */
2904 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2905 if (mpage_da_map_blocks(&mpd) == 0)
2906 mpage_da_submit_io(&mpd);
2907 mpd.io_done = 1;
2908 ret = MPAGE_DA_EXTENT_TAIL;
2909 }
Theodore Ts'ob3a3ca82009-08-31 23:13:11 -04002910 trace_ext4_da_write_pages(inode, &mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002911 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002912
Mingming Cao61628a32008-07-11 19:27:31 -04002913 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002914
Eric Sandeen8f64b322009-02-26 00:57:35 -05002915 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002916 /* commit the transaction which would
2917 * free blocks released in the transaction
2918 * and try again
2919 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002920 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002921 wbc->pages_skipped = pages_skipped;
2922 ret = 0;
2923 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002924 /*
2925 * got one extent now try with
2926 * rest of the pages
2927 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002928 pages_written += mpd.pages_written;
2929 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002930 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002931 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002932 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002933 /*
2934 * There is no more writeout needed
2935 * or we requested for a noblocking writeout
2936 * and we found the device congested
2937 */
Mingming Cao61628a32008-07-11 19:27:31 -04002938 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002939 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002940 if (!io_done && !cycled) {
2941 cycled = 1;
2942 index = 0;
2943 wbc->range_start = index << PAGE_CACHE_SHIFT;
2944 wbc->range_end = mapping->writeback_index - 1;
2945 goto retry;
2946 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002947 if (pages_skipped != wbc->pages_skipped)
Theodore Ts'o16939182009-09-26 17:43:59 -04002948 ext4_msg(inode->i_sb, KERN_CRIT,
2949 "This should not happen leaving %s "
2950 "with nr_to_write = %ld ret = %d\n",
2951 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002952
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002953 /* Update index */
2954 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002955 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002956 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2957 /*
2958 * set the writeback_index so that range_cyclic
2959 * mode will write it back later
2960 */
2961 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002962
Mingming Cao61628a32008-07-11 19:27:31 -04002963out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002964 if (!no_nrwrite_index_update)
2965 wbc->no_nrwrite_index_update = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002966 if (wbc->nr_to_write > nr_to_writebump)
2967 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002968 wbc->range_start = range_start;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002969 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002970 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002971}
2972
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002973#define FALL_BACK_TO_NONDELALLOC 1
2974static int ext4_nonda_switch(struct super_block *sb)
2975{
2976 s64 free_blocks, dirty_blocks;
2977 struct ext4_sb_info *sbi = EXT4_SB(sb);
2978
2979 /*
2980 * switch to non delalloc mode if we are running low
2981 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002982 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002983 * accumulated on each CPU without updating global counters
2984 * Delalloc need an accurate free block accounting. So switch
2985 * to non delalloc when we are near to error range.
2986 */
2987 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2988 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2989 if (2 * free_blocks < 3 * dirty_blocks ||
2990 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2991 /*
2992 * free block count is less that 150% of dirty blocks
2993 * or free blocks is less that watermark
2994 */
2995 return 1;
2996 }
2997 return 0;
2998}
2999
Alex Tomas64769242008-07-11 19:27:31 -04003000static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003001 loff_t pos, unsigned len, unsigned flags,
3002 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003003{
Mingming Caod2a17632008-07-14 17:52:37 -04003004 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04003005 struct page *page;
3006 pgoff_t index;
3007 unsigned from, to;
3008 struct inode *inode = mapping->host;
3009 handle_t *handle;
3010
3011 index = pos >> PAGE_CACHE_SHIFT;
3012 from = pos & (PAGE_CACHE_SIZE - 1);
3013 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003014
3015 if (ext4_nonda_switch(inode->i_sb)) {
3016 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
3017 return ext4_write_begin(file, mapping, pos,
3018 len, flags, pagep, fsdata);
3019 }
3020 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003021 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04003022retry:
Alex Tomas64769242008-07-11 19:27:31 -04003023 /*
3024 * With delayed allocation, we don't log the i_disksize update
3025 * if there is delayed block allocation. But we still need
3026 * to journalling the i_disksize update if writes to the end
3027 * of file which has an already mapped buffer.
3028 */
3029 handle = ext4_journal_start(inode, 1);
3030 if (IS_ERR(handle)) {
3031 ret = PTR_ERR(handle);
3032 goto out;
3033 }
Jan Karaebd36102009-02-22 21:09:59 -05003034 /* We cannot recurse into the filesystem as the transaction is already
3035 * started */
3036 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04003037
Nick Piggin54566b22009-01-04 12:00:53 -08003038 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003039 if (!page) {
3040 ext4_journal_stop(handle);
3041 ret = -ENOMEM;
3042 goto out;
3043 }
Alex Tomas64769242008-07-11 19:27:31 -04003044 *pagep = page;
3045
3046 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04003047 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04003048 if (ret < 0) {
3049 unlock_page(page);
3050 ext4_journal_stop(handle);
3051 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003052 /*
3053 * block_write_begin may have instantiated a few blocks
3054 * outside i_size. Trim these off again. Don't need
3055 * i_size_read because we hold i_mutex.
3056 */
3057 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003058 ext4_truncate_failed_write(inode);
Alex Tomas64769242008-07-11 19:27:31 -04003059 }
3060
Mingming Caod2a17632008-07-14 17:52:37 -04003061 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3062 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003063out:
3064 return ret;
3065}
3066
Mingming Cao632eaea2008-07-11 19:27:31 -04003067/*
3068 * Check if we should update i_disksize
3069 * when write to the end of file but not require block allocation
3070 */
3071static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003072 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003073{
3074 struct buffer_head *bh;
3075 struct inode *inode = page->mapping->host;
3076 unsigned int idx;
3077 int i;
3078
3079 bh = page_buffers(page);
3080 idx = offset >> inode->i_blkbits;
3081
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003082 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003083 bh = bh->b_this_page;
3084
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003085 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003086 return 0;
3087 return 1;
3088}
3089
Alex Tomas64769242008-07-11 19:27:31 -04003090static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003091 struct address_space *mapping,
3092 loff_t pos, unsigned len, unsigned copied,
3093 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003094{
3095 struct inode *inode = mapping->host;
3096 int ret = 0, ret2;
3097 handle_t *handle = ext4_journal_current_handle();
3098 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003099 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003100 int write_mode = (int)(unsigned long)fsdata;
3101
3102 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3103 if (ext4_should_order_data(inode)) {
3104 return ext4_ordered_write_end(file, mapping, pos,
3105 len, copied, page, fsdata);
3106 } else if (ext4_should_writeback_data(inode)) {
3107 return ext4_writeback_write_end(file, mapping, pos,
3108 len, copied, page, fsdata);
3109 } else {
3110 BUG();
3111 }
3112 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003113
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003114 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003115 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003116 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003117
3118 /*
3119 * generic_write_end() will run mark_inode_dirty() if i_size
3120 * changes. So let's piggyback the i_disksize mark_inode_dirty
3121 * into that.
3122 */
3123
3124 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003125 if (new_i_size > EXT4_I(inode)->i_disksize) {
3126 if (ext4_da_should_update_i_disksize(page, end)) {
3127 down_write(&EXT4_I(inode)->i_data_sem);
3128 if (new_i_size > EXT4_I(inode)->i_disksize) {
3129 /*
3130 * Updating i_disksize when extending file
3131 * without needing block allocation
3132 */
3133 if (ext4_should_order_data(inode))
3134 ret = ext4_jbd2_file_inode(handle,
3135 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003136
Mingming Cao632eaea2008-07-11 19:27:31 -04003137 EXT4_I(inode)->i_disksize = new_i_size;
3138 }
3139 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003140 /* We need to mark inode dirty even if
3141 * new_i_size is less that inode->i_size
3142 * bu greater than i_disksize.(hint delalloc)
3143 */
3144 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003145 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003146 }
Alex Tomas64769242008-07-11 19:27:31 -04003147 ret2 = generic_write_end(file, mapping, pos, len, copied,
3148 page, fsdata);
3149 copied = ret2;
3150 if (ret2 < 0)
3151 ret = ret2;
3152 ret2 = ext4_journal_stop(handle);
3153 if (!ret)
3154 ret = ret2;
3155
3156 return ret ? ret : copied;
3157}
3158
3159static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3160{
Alex Tomas64769242008-07-11 19:27:31 -04003161 /*
3162 * Drop reserved blocks
3163 */
3164 BUG_ON(!PageLocked(page));
3165 if (!page_has_buffers(page))
3166 goto out;
3167
Mingming Caod2a17632008-07-14 17:52:37 -04003168 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003169
3170out:
3171 ext4_invalidatepage(page, offset);
3172
3173 return;
3174}
3175
Theodore Ts'occd25062009-02-26 01:04:07 -05003176/*
3177 * Force all delayed allocation blocks to be allocated for a given inode.
3178 */
3179int ext4_alloc_da_blocks(struct inode *inode)
3180{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003181 trace_ext4_alloc_da_blocks(inode);
3182
Theodore Ts'occd25062009-02-26 01:04:07 -05003183 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3184 !EXT4_I(inode)->i_reserved_meta_blocks)
3185 return 0;
3186
3187 /*
3188 * We do something simple for now. The filemap_flush() will
3189 * also start triggering a write of the data blocks, which is
3190 * not strictly speaking necessary (and for users of
3191 * laptop_mode, not even desirable). However, to do otherwise
3192 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003193 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003194 * ext4_da_writepages() ->
3195 * write_cache_pages() ---> (via passed in callback function)
3196 * __mpage_da_writepage() -->
3197 * mpage_add_bh_to_extent()
3198 * mpage_da_map_blocks()
3199 *
3200 * The problem is that write_cache_pages(), located in
3201 * mm/page-writeback.c, marks pages clean in preparation for
3202 * doing I/O, which is not desirable if we're not planning on
3203 * doing I/O at all.
3204 *
3205 * We could call write_cache_pages(), and then redirty all of
3206 * the pages by calling redirty_page_for_writeback() but that
3207 * would be ugly in the extreme. So instead we would need to
3208 * replicate parts of the code in the above functions,
3209 * simplifying them becuase we wouldn't actually intend to
3210 * write out the pages, but rather only collect contiguous
3211 * logical block extents, call the multi-block allocator, and
3212 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003213 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003214 * For now, though, we'll cheat by calling filemap_flush(),
3215 * which will map the blocks, and start the I/O, but not
3216 * actually wait for the I/O to complete.
3217 */
3218 return filemap_flush(inode->i_mapping);
3219}
Alex Tomas64769242008-07-11 19:27:31 -04003220
3221/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003222 * bmap() is special. It gets used by applications such as lilo and by
3223 * the swapper to find the on-disk block of a specific piece of data.
3224 *
3225 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003226 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227 * filesystem and enables swap, then they may get a nasty shock when the
3228 * data getting swapped to that swapfile suddenly gets overwritten by
3229 * the original zero's written out previously to the journal and
3230 * awaiting writeback in the kernel's buffer cache.
3231 *
3232 * So, if we see any bmap calls here on a modified, data-journaled file,
3233 * take extra steps to flush any blocks which might be in the cache.
3234 */
Mingming Cao617ba132006-10-11 01:20:53 -07003235static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236{
3237 struct inode *inode = mapping->host;
3238 journal_t *journal;
3239 int err;
3240
Alex Tomas64769242008-07-11 19:27:31 -04003241 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3242 test_opt(inode->i_sb, DELALLOC)) {
3243 /*
3244 * With delalloc we want to sync the file
3245 * so that we can make sure we allocate
3246 * blocks for file
3247 */
3248 filemap_write_and_wait(mapping);
3249 }
3250
Frank Mayhar03901312009-01-07 00:06:22 -05003251 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252 /*
3253 * This is a REALLY heavyweight approach, but the use of
3254 * bmap on dirty files is expected to be extremely rare:
3255 * only if we run lilo or swapon on a freshly made file
3256 * do we expect this to happen.
3257 *
3258 * (bmap requires CAP_SYS_RAWIO so this does not
3259 * represent an unprivileged user DOS attack --- we'd be
3260 * in trouble if mortal users could trigger this path at
3261 * will.)
3262 *
Mingming Cao617ba132006-10-11 01:20:53 -07003263 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003264 * regular files. If somebody wants to bmap a directory
3265 * or symlink and gets confused because the buffer
3266 * hasn't yet been flushed to disk, they deserve
3267 * everything they get.
3268 */
3269
Mingming Cao617ba132006-10-11 01:20:53 -07003270 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3271 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003272 jbd2_journal_lock_updates(journal);
3273 err = jbd2_journal_flush(journal);
3274 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003275
3276 if (err)
3277 return 0;
3278 }
3279
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003280 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003281}
3282
Mingming Cao617ba132006-10-11 01:20:53 -07003283static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284{
Mingming Cao617ba132006-10-11 01:20:53 -07003285 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003286}
3287
3288static int
Mingming Cao617ba132006-10-11 01:20:53 -07003289ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 struct list_head *pages, unsigned nr_pages)
3291{
Mingming Cao617ba132006-10-11 01:20:53 -07003292 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003293}
3294
Mingming Cao617ba132006-10-11 01:20:53 -07003295static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296{
Mingming Cao617ba132006-10-11 01:20:53 -07003297 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298
3299 /*
3300 * If it's a full truncate we just forget about the pending dirtying
3301 */
3302 if (offset == 0)
3303 ClearPageChecked(page);
3304
Frank Mayhar03901312009-01-07 00:06:22 -05003305 if (journal)
3306 jbd2_journal_invalidatepage(journal, page, offset);
3307 else
3308 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309}
3310
Mingming Cao617ba132006-10-11 01:20:53 -07003311static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312{
Mingming Cao617ba132006-10-11 01:20:53 -07003313 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003314
3315 WARN_ON(PageChecked(page));
3316 if (!page_has_buffers(page))
3317 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003318 if (journal)
3319 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3320 else
3321 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322}
3323
3324/*
Mingming Cao4c0425f2009-09-28 15:48:41 -04003325 * O_DIRECT for ext3 (or indirect map) based files
3326 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003327 * If the O_DIRECT write will extend the file then add this inode to the
3328 * orphan list. So recovery will truncate it back to the original size
3329 * if the machine crashes during the write.
3330 *
3331 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003332 * crashes then stale disk data _may_ be exposed inside the file. But current
3333 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334 */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003335static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003336 const struct iovec *iov, loff_t offset,
3337 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003338{
3339 struct file *file = iocb->ki_filp;
3340 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003341 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003342 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343 ssize_t ret;
3344 int orphan = 0;
3345 size_t count = iov_length(iov, nr_segs);
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003346 int retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003347
3348 if (rw == WRITE) {
3349 loff_t final_size = offset + count;
3350
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003351 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003352 /* Credits for sb + inode write */
3353 handle = ext4_journal_start(inode, 2);
3354 if (IS_ERR(handle)) {
3355 ret = PTR_ERR(handle);
3356 goto out;
3357 }
Mingming Cao617ba132006-10-11 01:20:53 -07003358 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003359 if (ret) {
3360 ext4_journal_stop(handle);
3361 goto out;
3362 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003363 orphan = 1;
3364 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003365 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003366 }
3367 }
3368
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003369retry:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003370 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3371 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003372 ext4_get_block, NULL);
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003373 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3374 goto retry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003375
Jan Kara7fb54092008-02-10 01:08:38 -05003376 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003377 int err;
3378
Jan Kara7fb54092008-02-10 01:08:38 -05003379 /* Credits for sb + inode write */
3380 handle = ext4_journal_start(inode, 2);
3381 if (IS_ERR(handle)) {
3382 /* This is really bad luck. We've written the data
3383 * but cannot extend i_size. Bail out and pretend
3384 * the write failed... */
3385 ret = PTR_ERR(handle);
3386 goto out;
3387 }
3388 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003389 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003390 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003391 loff_t end = offset + ret;
3392 if (end > inode->i_size) {
3393 ei->i_disksize = end;
3394 i_size_write(inode, end);
3395 /*
3396 * We're going to return a positive `ret'
3397 * here due to non-zero-length I/O, so there's
3398 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003399 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003400 * ignore it.
3401 */
Mingming Cao617ba132006-10-11 01:20:53 -07003402 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003403 }
3404 }
Mingming Cao617ba132006-10-11 01:20:53 -07003405 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003406 if (ret == 0)
3407 ret = err;
3408 }
3409out:
3410 return ret;
3411}
3412
Mingming Cao4c0425f2009-09-28 15:48:41 -04003413static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock,
3414 struct buffer_head *bh_result, int create)
3415{
3416 handle_t *handle = NULL;
3417 int ret = 0;
3418 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
3419 int dio_credits;
3420
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003421 ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n",
3422 inode->i_ino, create);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003423 /*
3424 * DIO VFS code passes create = 0 flag for write to
3425 * the middle of file. It does this to avoid block
3426 * allocation for holes, to prevent expose stale data
3427 * out when there is parallel buffered read (which does
3428 * not hold the i_mutex lock) while direct IO write has
3429 * not completed. DIO request on holes finally falls back
3430 * to buffered IO for this reason.
3431 *
3432 * For ext4 extent based file, since we support fallocate,
3433 * new allocated extent as uninitialized, for holes, we
3434 * could fallocate blocks for holes, thus parallel
3435 * buffered IO read will zero out the page when read on
3436 * a hole while parallel DIO write to the hole has not completed.
3437 *
3438 * when we come here, we know it's a direct IO write to
3439 * to the middle of file (<i_size)
3440 * so it's safe to override the create flag from VFS.
3441 */
3442 create = EXT4_GET_BLOCKS_DIO_CREATE_EXT;
3443
3444 if (max_blocks > DIO_MAX_BLOCKS)
3445 max_blocks = DIO_MAX_BLOCKS;
3446 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
3447 handle = ext4_journal_start(inode, dio_credits);
3448 if (IS_ERR(handle)) {
3449 ret = PTR_ERR(handle);
3450 goto out;
3451 }
3452 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
3453 create);
3454 if (ret > 0) {
3455 bh_result->b_size = (ret << inode->i_blkbits);
3456 ret = 0;
3457 }
3458 ext4_journal_stop(handle);
3459out:
3460 return ret;
3461}
3462
Mingming Cao4c0425f2009-09-28 15:48:41 -04003463static void ext4_free_io_end(ext4_io_end_t *io)
3464{
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003465 BUG_ON(!io);
3466 iput(io->inode);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003467 kfree(io);
3468}
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003469static void dump_aio_dio_list(struct inode * inode)
3470{
3471#ifdef EXT4_DEBUG
3472 struct list_head *cur, *before, *after;
3473 ext4_io_end_t *io, *io0, *io1;
3474
3475 if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
3476 ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino);
3477 return;
3478 }
3479
3480 ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino);
3481 list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){
3482 cur = &io->list;
3483 before = cur->prev;
3484 io0 = container_of(before, ext4_io_end_t, list);
3485 after = cur->next;
3486 io1 = container_of(after, ext4_io_end_t, list);
3487
3488 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
3489 io, inode->i_ino, io0, io1);
3490 }
3491#endif
3492}
Mingming Cao4c0425f2009-09-28 15:48:41 -04003493
3494/*
Mingming Cao4c0425f2009-09-28 15:48:41 -04003495 * check a range of space and convert unwritten extents to written.
3496 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003497static int ext4_end_aio_dio_nolock(ext4_io_end_t *io)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003498{
Mingming Cao4c0425f2009-09-28 15:48:41 -04003499 struct inode *inode = io->inode;
3500 loff_t offset = io->offset;
3501 size_t size = io->size;
3502 int ret = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003503
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003504 ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p,"
3505 "list->prev 0x%p\n",
3506 io, inode->i_ino, io->list.next, io->list.prev);
3507
3508 if (list_empty(&io->list))
3509 return ret;
3510
3511 if (io->flag != DIO_AIO_UNWRITTEN)
3512 return ret;
3513
Mingming Cao4c0425f2009-09-28 15:48:41 -04003514 if (offset + size <= i_size_read(inode))
3515 ret = ext4_convert_unwritten_extents(inode, offset, size);
3516
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003517 if (ret < 0) {
Mingming Cao4c0425f2009-09-28 15:48:41 -04003518 printk(KERN_EMERG "%s: failed to convert unwritten"
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003519 "extents to written extents, error is %d"
3520 " io is still on inode %lu aio dio list\n",
3521 __func__, ret, inode->i_ino);
3522 return ret;
3523 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003524
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003525 /* clear the DIO AIO unwritten flag */
3526 io->flag = 0;
3527 return ret;
3528}
3529/*
3530 * work on completed aio dio IO, to convert unwritten extents to extents
3531 */
3532static void ext4_end_aio_dio_work(struct work_struct *work)
3533{
3534 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
3535 struct inode *inode = io->inode;
3536 int ret = 0;
3537
3538 mutex_lock(&inode->i_mutex);
3539 ret = ext4_end_aio_dio_nolock(io);
3540 if (ret >= 0) {
3541 if (!list_empty(&io->list))
3542 list_del_init(&io->list);
3543 ext4_free_io_end(io);
3544 }
3545 mutex_unlock(&inode->i_mutex);
3546}
3547/*
3548 * This function is called from ext4_sync_file().
3549 *
3550 * When AIO DIO IO is completed, the work to convert unwritten
3551 * extents to written is queued on workqueue but may not get immediately
3552 * scheduled. When fsync is called, we need to ensure the
3553 * conversion is complete before fsync returns.
3554 * The inode keeps track of a list of completed AIO from DIO path
3555 * that might needs to do the conversion. This function walks through
3556 * the list and convert the related unwritten extents to written.
3557 */
3558int flush_aio_dio_completed_IO(struct inode *inode)
3559{
3560 ext4_io_end_t *io;
3561 int ret = 0;
3562 int ret2 = 0;
3563
3564 if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list))
3565 return ret;
3566
3567 dump_aio_dio_list(inode);
3568 while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
3569 io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next,
3570 ext4_io_end_t, list);
3571 /*
3572 * Calling ext4_end_aio_dio_nolock() to convert completed
3573 * IO to written.
3574 *
3575 * When ext4_sync_file() is called, run_queue() may already
3576 * about to flush the work corresponding to this io structure.
3577 * It will be upset if it founds the io structure related
3578 * to the work-to-be schedule is freed.
3579 *
3580 * Thus we need to keep the io structure still valid here after
3581 * convertion finished. The io structure has a flag to
3582 * avoid double converting from both fsync and background work
3583 * queue work.
3584 */
3585 ret = ext4_end_aio_dio_nolock(io);
3586 if (ret < 0)
3587 ret2 = ret;
3588 else
3589 list_del_init(&io->list);
3590 }
3591 return (ret2 < 0) ? ret2 : 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003592}
3593
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003594static ext4_io_end_t *ext4_init_io_end (struct inode *inode)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003595{
3596 ext4_io_end_t *io = NULL;
3597
3598 io = kmalloc(sizeof(*io), GFP_NOFS);
3599
3600 if (io) {
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003601 igrab(inode);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003602 io->inode = inode;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003603 io->flag = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003604 io->offset = 0;
3605 io->size = 0;
3606 io->error = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003607 INIT_WORK(&io->work, ext4_end_aio_dio_work);
3608 INIT_LIST_HEAD(&io->list);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003609 }
3610
3611 return io;
3612}
3613
3614static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3615 ssize_t size, void *private)
3616{
3617 ext4_io_end_t *io_end = iocb->private;
3618 struct workqueue_struct *wq;
3619
Mingming4b70df12009-11-03 14:44:54 -05003620 /* if not async direct IO or dio with 0 bytes write, just return */
3621 if (!io_end || !size)
3622 return;
3623
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003624 ext_debug("ext4_end_io_dio(): io_end 0x%p"
3625 "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
3626 iocb->private, io_end->inode->i_ino, iocb, offset,
3627 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003628
3629 /* if not aio dio with unwritten extents, just free io and return */
3630 if (io_end->flag != DIO_AIO_UNWRITTEN){
3631 ext4_free_io_end(io_end);
3632 iocb->private = NULL;
3633 return;
3634 }
3635
Mingming Cao4c0425f2009-09-28 15:48:41 -04003636 io_end->offset = offset;
3637 io_end->size = size;
3638 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
3639
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003640 /* queue the work to convert unwritten extents to written */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003641 queue_work(wq, &io_end->work);
3642
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003643 /* Add the io_end to per-inode completed aio dio list*/
3644 list_add_tail(&io_end->list,
3645 &EXT4_I(io_end->inode)->i_aio_dio_complete_list);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003646 iocb->private = NULL;
3647}
3648/*
3649 * For ext4 extent files, ext4 will do direct-io write to holes,
3650 * preallocated extents, and those write extend the file, no need to
3651 * fall back to buffered IO.
3652 *
3653 * For holes, we fallocate those blocks, mark them as unintialized
3654 * If those blocks were preallocated, we mark sure they are splited, but
3655 * still keep the range to write as unintialized.
3656 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003657 * The unwrritten extents will be converted to written when DIO is completed.
3658 * For async direct IO, since the IO may still pending when return, we
3659 * set up an end_io call back function, which will do the convertion
3660 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003661 *
3662 * If the O_DIRECT write will extend the file then add this inode to the
3663 * orphan list. So recovery will truncate it back to the original size
3664 * if the machine crashes during the write.
3665 *
3666 */
3667static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3668 const struct iovec *iov, loff_t offset,
3669 unsigned long nr_segs)
3670{
3671 struct file *file = iocb->ki_filp;
3672 struct inode *inode = file->f_mapping->host;
3673 ssize_t ret;
3674 size_t count = iov_length(iov, nr_segs);
3675
3676 loff_t final_size = offset + count;
3677 if (rw == WRITE && final_size <= inode->i_size) {
3678 /*
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003679 * We could direct write to holes and fallocate.
3680 *
3681 * Allocated blocks to fill the hole are marked as uninitialized
Mingming Cao4c0425f2009-09-28 15:48:41 -04003682 * to prevent paralel buffered read to expose the stale data
3683 * before DIO complete the data IO.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003684 *
3685 * As to previously fallocated extents, ext4 get_block
Mingming Cao4c0425f2009-09-28 15:48:41 -04003686 * will just simply mark the buffer mapped but still
3687 * keep the extents uninitialized.
3688 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003689 * for non AIO case, we will convert those unwritten extents
3690 * to written after return back from blockdev_direct_IO.
3691 *
3692 * for async DIO, the conversion needs to be defered when
3693 * the IO is completed. The ext4 end_io callback function
3694 * will be called to take care of the conversion work.
3695 * Here for async case, we allocate an io_end structure to
3696 * hook to the iocb.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003697 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003698 iocb->private = NULL;
3699 EXT4_I(inode)->cur_aio_dio = NULL;
3700 if (!is_sync_kiocb(iocb)) {
3701 iocb->private = ext4_init_io_end(inode);
3702 if (!iocb->private)
3703 return -ENOMEM;
3704 /*
3705 * we save the io structure for current async
3706 * direct IO, so that later ext4_get_blocks()
3707 * could flag the io structure whether there
3708 * is a unwritten extents needs to be converted
3709 * when IO is completed.
3710 */
3711 EXT4_I(inode)->cur_aio_dio = iocb->private;
3712 }
3713
Mingming Cao4c0425f2009-09-28 15:48:41 -04003714 ret = blockdev_direct_IO(rw, iocb, inode,
3715 inode->i_sb->s_bdev, iov,
3716 offset, nr_segs,
3717 ext4_get_block_dio_write,
3718 ext4_end_io_dio);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003719 if (iocb->private)
3720 EXT4_I(inode)->cur_aio_dio = NULL;
3721 /*
3722 * The io_end structure takes a reference to the inode,
3723 * that structure needs to be destroyed and the
3724 * reference to the inode need to be dropped, when IO is
3725 * complete, even with 0 byte write, or failed.
3726 *
3727 * In the successful AIO DIO case, the io_end structure will be
3728 * desctroyed and the reference to the inode will be dropped
3729 * after the end_io call back function is called.
3730 *
3731 * In the case there is 0 byte write, or error case, since
3732 * VFS direct IO won't invoke the end_io call back function,
3733 * we need to free the end_io structure here.
3734 */
3735 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3736 ext4_free_io_end(iocb->private);
3737 iocb->private = NULL;
Mingming5f524952009-11-10 10:48:04 -05003738 } else if (ret > 0 && (EXT4_I(inode)->i_state &
3739 EXT4_STATE_DIO_UNWRITTEN)) {
Mingming109f5562009-11-10 10:48:08 -05003740 int err;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003741 /*
3742 * for non AIO case, since the IO is already
3743 * completed, we could do the convertion right here
3744 */
Mingming109f5562009-11-10 10:48:08 -05003745 err = ext4_convert_unwritten_extents(inode,
3746 offset, ret);
3747 if (err < 0)
3748 ret = err;
Mingming5f524952009-11-10 10:48:04 -05003749 EXT4_I(inode)->i_state &= ~EXT4_STATE_DIO_UNWRITTEN;
Mingming109f5562009-11-10 10:48:08 -05003750 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003751 return ret;
3752 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003753
3754 /* for write the the end of file case, we fall back to old way */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003755 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3756}
3757
3758static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
3759 const struct iovec *iov, loff_t offset,
3760 unsigned long nr_segs)
3761{
3762 struct file *file = iocb->ki_filp;
3763 struct inode *inode = file->f_mapping->host;
3764
3765 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
3766 return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3767
3768 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3769}
3770
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003771/*
Mingming Cao617ba132006-10-11 01:20:53 -07003772 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003773 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3774 * much here because ->set_page_dirty is called under VFS locks. The page is
3775 * not necessarily locked.
3776 *
3777 * We cannot just dirty the page and leave attached buffers clean, because the
3778 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3779 * or jbddirty because all the journalling code will explode.
3780 *
3781 * So what we do is to mark the page "pending dirty" and next time writepage
3782 * is called, propagate that into the buffers appropriately.
3783 */
Mingming Cao617ba132006-10-11 01:20:53 -07003784static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003785{
3786 SetPageChecked(page);
3787 return __set_page_dirty_nobuffers(page);
3788}
3789
Mingming Cao617ba132006-10-11 01:20:53 -07003790static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003791 .readpage = ext4_readpage,
3792 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003793 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003794 .sync_page = block_sync_page,
3795 .write_begin = ext4_write_begin,
3796 .write_end = ext4_ordered_write_end,
3797 .bmap = ext4_bmap,
3798 .invalidatepage = ext4_invalidatepage,
3799 .releasepage = ext4_releasepage,
3800 .direct_IO = ext4_direct_IO,
3801 .migratepage = buffer_migrate_page,
3802 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003803 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003804};
3805
Mingming Cao617ba132006-10-11 01:20:53 -07003806static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003807 .readpage = ext4_readpage,
3808 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003809 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003810 .sync_page = block_sync_page,
3811 .write_begin = ext4_write_begin,
3812 .write_end = ext4_writeback_write_end,
3813 .bmap = ext4_bmap,
3814 .invalidatepage = ext4_invalidatepage,
3815 .releasepage = ext4_releasepage,
3816 .direct_IO = ext4_direct_IO,
3817 .migratepage = buffer_migrate_page,
3818 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003819 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003820};
3821
Mingming Cao617ba132006-10-11 01:20:53 -07003822static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003823 .readpage = ext4_readpage,
3824 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003825 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003826 .sync_page = block_sync_page,
3827 .write_begin = ext4_write_begin,
3828 .write_end = ext4_journalled_write_end,
3829 .set_page_dirty = ext4_journalled_set_page_dirty,
3830 .bmap = ext4_bmap,
3831 .invalidatepage = ext4_invalidatepage,
3832 .releasepage = ext4_releasepage,
3833 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003834 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003835};
3836
Alex Tomas64769242008-07-11 19:27:31 -04003837static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003838 .readpage = ext4_readpage,
3839 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003840 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003841 .writepages = ext4_da_writepages,
3842 .sync_page = block_sync_page,
3843 .write_begin = ext4_da_write_begin,
3844 .write_end = ext4_da_write_end,
3845 .bmap = ext4_bmap,
3846 .invalidatepage = ext4_da_invalidatepage,
3847 .releasepage = ext4_releasepage,
3848 .direct_IO = ext4_direct_IO,
3849 .migratepage = buffer_migrate_page,
3850 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003851 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003852};
3853
Mingming Cao617ba132006-10-11 01:20:53 -07003854void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003855{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003856 if (ext4_should_order_data(inode) &&
3857 test_opt(inode->i_sb, DELALLOC))
3858 inode->i_mapping->a_ops = &ext4_da_aops;
3859 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003860 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003861 else if (ext4_should_writeback_data(inode) &&
3862 test_opt(inode->i_sb, DELALLOC))
3863 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003864 else if (ext4_should_writeback_data(inode))
3865 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003866 else
Mingming Cao617ba132006-10-11 01:20:53 -07003867 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003868}
3869
3870/*
Mingming Cao617ba132006-10-11 01:20:53 -07003871 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003872 * up to the end of the block which corresponds to `from'.
3873 * This required during truncate. We need to physically zero the tail end
3874 * of that block so it doesn't yield old data if the file is later grown.
3875 */
Jan Karacf108bc2008-07-11 19:27:31 -04003876int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003877 struct address_space *mapping, loff_t from)
3878{
Mingming Cao617ba132006-10-11 01:20:53 -07003879 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003880 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003881 unsigned blocksize, length, pos;
3882 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003883 struct inode *inode = mapping->host;
3884 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003885 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003886 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003887
Theodore Ts'of4a01012009-07-05 22:08:16 -04003888 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3889 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003890 if (!page)
3891 return -EINVAL;
3892
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893 blocksize = inode->i_sb->s_blocksize;
3894 length = blocksize - (offset & (blocksize - 1));
3895 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3896
3897 /*
3898 * For "nobh" option, we can only work if we don't need to
3899 * read-in the page - otherwise we create buffers to do the IO.
3900 */
3901 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003902 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003903 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003904 set_page_dirty(page);
3905 goto unlock;
3906 }
3907
3908 if (!page_has_buffers(page))
3909 create_empty_buffers(page, blocksize, 0);
3910
3911 /* Find the buffer that contains "offset" */
3912 bh = page_buffers(page);
3913 pos = blocksize;
3914 while (offset >= pos) {
3915 bh = bh->b_this_page;
3916 iblock++;
3917 pos += blocksize;
3918 }
3919
3920 err = 0;
3921 if (buffer_freed(bh)) {
3922 BUFFER_TRACE(bh, "freed: skip");
3923 goto unlock;
3924 }
3925
3926 if (!buffer_mapped(bh)) {
3927 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003928 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003929 /* unmapped? It's a hole - nothing to do */
3930 if (!buffer_mapped(bh)) {
3931 BUFFER_TRACE(bh, "still unmapped");
3932 goto unlock;
3933 }
3934 }
3935
3936 /* Ok, it's mapped. Make sure it's up-to-date */
3937 if (PageUptodate(page))
3938 set_buffer_uptodate(bh);
3939
3940 if (!buffer_uptodate(bh)) {
3941 err = -EIO;
3942 ll_rw_block(READ, 1, &bh);
3943 wait_on_buffer(bh);
3944 /* Uhhuh. Read error. Complain and punt. */
3945 if (!buffer_uptodate(bh))
3946 goto unlock;
3947 }
3948
Mingming Cao617ba132006-10-11 01:20:53 -07003949 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003951 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003952 if (err)
3953 goto unlock;
3954 }
3955
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003956 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003957
3958 BUFFER_TRACE(bh, "zeroed end of block");
3959
3960 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003961 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003962 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003963 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003964 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003965 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003966 mark_buffer_dirty(bh);
3967 }
3968
3969unlock:
3970 unlock_page(page);
3971 page_cache_release(page);
3972 return err;
3973}
3974
3975/*
3976 * Probably it should be a library function... search for first non-zero word
3977 * or memcmp with zero_page, whatever is better for particular architecture.
3978 * Linus?
3979 */
3980static inline int all_zeroes(__le32 *p, __le32 *q)
3981{
3982 while (p < q)
3983 if (*p++)
3984 return 0;
3985 return 1;
3986}
3987
3988/**
Mingming Cao617ba132006-10-11 01:20:53 -07003989 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003990 * @inode: inode in question
3991 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003992 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003993 * @chain: place to store the pointers to partial indirect blocks
3994 * @top: place to the (detached) top of branch
3995 *
Mingming Cao617ba132006-10-11 01:20:53 -07003996 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003997 *
3998 * When we do truncate() we may have to clean the ends of several
3999 * indirect blocks but leave the blocks themselves alive. Block is
4000 * partially truncated if some data below the new i_size is refered
4001 * from it (and it is on the path to the first completely truncated
4002 * data block, indeed). We have to free the top of that path along
4003 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07004004 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004005 * finishes, we may safely do the latter, but top of branch may
4006 * require special attention - pageout below the truncation point
4007 * might try to populate it.
4008 *
4009 * We atomically detach the top of branch from the tree, store the
4010 * block number of its root in *@top, pointers to buffer_heads of
4011 * partially truncated blocks - in @chain[].bh and pointers to
4012 * their last elements that should not be removed - in
4013 * @chain[].p. Return value is the pointer to last filled element
4014 * of @chain.
4015 *
4016 * The work left to caller to do the actual freeing of subtrees:
4017 * a) free the subtree starting from *@top
4018 * b) free the subtrees whose roots are stored in
4019 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
4020 * c) free the subtrees growing from the inode past the @chain[0].
4021 * (no partially truncated stuff there). */
4022
Mingming Cao617ba132006-10-11 01:20:53 -07004023static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004024 ext4_lblk_t offsets[4], Indirect chain[4],
4025 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004026{
4027 Indirect *partial, *p;
4028 int k, err;
4029
4030 *top = 0;
Uwe Kleine-Königbf48aab2009-10-28 20:11:03 +01004031 /* Make k index the deepest non-null offset + 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004032 for (k = depth; k > 1 && !offsets[k-1]; k--)
4033 ;
Mingming Cao617ba132006-10-11 01:20:53 -07004034 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004035 /* Writer: pointers */
4036 if (!partial)
4037 partial = chain + k-1;
4038 /*
4039 * If the branch acquired continuation since we've looked at it -
4040 * fine, it should all survive and (new) top doesn't belong to us.
4041 */
4042 if (!partial->key && *partial->p)
4043 /* Writer: end */
4044 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004045 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004046 ;
4047 /*
4048 * OK, we've found the last block that must survive. The rest of our
4049 * branch should be detached before unlocking. However, if that rest
4050 * of branch is all ours and does not grow immediately from the inode
4051 * it's easier to cheat and just decrement partial->p.
4052 */
4053 if (p == chain + k - 1 && p > chain) {
4054 p->p--;
4055 } else {
4056 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07004057 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004058#if 0
4059 *p->p = 0;
4060#endif
4061 }
4062 /* Writer: end */
4063
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004064 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004065 brelse(partial->bh);
4066 partial--;
4067 }
4068no_top:
4069 return partial;
4070}
4071
4072/*
4073 * Zero a number of block pointers in either an inode or an indirect block.
4074 * If we restart the transaction we must again get write access to the
4075 * indirect block for further modification.
4076 *
4077 * We release `count' blocks on disk, but (last - first) may be greater
4078 * than `count' because there can be holes in there.
4079 */
Mingming Cao617ba132006-10-11 01:20:53 -07004080static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004081 struct buffer_head *bh,
4082 ext4_fsblk_t block_to_free,
4083 unsigned long count, __le32 *first,
4084 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004085{
4086 __le32 *p;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004087 int flags = EXT4_FREE_BLOCKS_FORGET;
4088
4089 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4090 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o50689692009-11-23 07:17:34 -05004091
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004092 if (try_to_extend_transaction(handle, inode)) {
4093 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004094 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4095 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004096 }
Mingming Cao617ba132006-10-11 01:20:53 -07004097 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04004098 ext4_truncate_restart_trans(handle, inode,
4099 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004100 if (bh) {
4101 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07004102 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004103 }
4104 }
4105
Theodore Ts'oe6362602009-11-23 07:17:05 -05004106 for (p = first; p < last; p++)
4107 *p = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108
Theodore Ts'oe6362602009-11-23 07:17:05 -05004109 ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004110}
4111
4112/**
Mingming Cao617ba132006-10-11 01:20:53 -07004113 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114 * @handle: handle for this transaction
4115 * @inode: inode we are dealing with
4116 * @this_bh: indirect buffer_head which contains *@first and *@last
4117 * @first: array of block numbers
4118 * @last: points immediately past the end of array
4119 *
4120 * We are freeing all blocks refered from that array (numbers are stored as
4121 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
4122 *
4123 * We accumulate contiguous runs of blocks to free. Conveniently, if these
4124 * blocks are contiguous then releasing them at one time will only affect one
4125 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
4126 * actually use a lot of journal space.
4127 *
4128 * @this_bh will be %NULL if @first and @last point into the inode's direct
4129 * block pointers.
4130 */
Mingming Cao617ba132006-10-11 01:20:53 -07004131static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004132 struct buffer_head *this_bh,
4133 __le32 *first, __le32 *last)
4134{
Mingming Cao617ba132006-10-11 01:20:53 -07004135 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004136 unsigned long count = 0; /* Number of blocks in the run */
4137 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
4138 corresponding to
4139 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07004140 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004141 __le32 *p; /* Pointer into inode/ind
4142 for current block */
4143 int err;
4144
4145 if (this_bh) { /* For indirect block */
4146 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004147 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004148 /* Important: if we can't update the indirect pointers
4149 * to the blocks, we can't free them. */
4150 if (err)
4151 return;
4152 }
4153
4154 for (p = first; p < last; p++) {
4155 nr = le32_to_cpu(*p);
4156 if (nr) {
4157 /* accumulate blocks to free if they're contiguous */
4158 if (count == 0) {
4159 block_to_free = nr;
4160 block_to_free_p = p;
4161 count = 1;
4162 } else if (nr == block_to_free + count) {
4163 count++;
4164 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004165 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004166 block_to_free,
4167 count, block_to_free_p, p);
4168 block_to_free = nr;
4169 block_to_free_p = p;
4170 count = 1;
4171 }
4172 }
4173 }
4174
4175 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07004176 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004177 count, block_to_free_p, p);
4178
4179 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004180 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004181
4182 /*
4183 * The buffer head should have an attached journal head at this
4184 * point. However, if the data is corrupted and an indirect
4185 * block pointed to itself, it would have been detached when
4186 * the block was cleared. Check for this instead of OOPSing.
4187 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05004188 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05004189 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004190 else
4191 ext4_error(inode->i_sb, __func__,
4192 "circular indirect block detected, "
4193 "inode=%lu, block=%llu",
4194 inode->i_ino,
4195 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004196 }
4197}
4198
4199/**
Mingming Cao617ba132006-10-11 01:20:53 -07004200 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004201 * @handle: JBD handle for this transaction
4202 * @inode: inode we are dealing with
4203 * @parent_bh: the buffer_head which contains *@first and *@last
4204 * @first: array of block numbers
4205 * @last: pointer immediately past the end of array
4206 * @depth: depth of the branches to free
4207 *
4208 * We are freeing all blocks refered from these branches (numbers are
4209 * stored as little-endian 32-bit) and updating @inode->i_blocks
4210 * appropriately.
4211 */
Mingming Cao617ba132006-10-11 01:20:53 -07004212static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004213 struct buffer_head *parent_bh,
4214 __le32 *first, __le32 *last, int depth)
4215{
Mingming Cao617ba132006-10-11 01:20:53 -07004216 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004217 __le32 *p;
4218
Frank Mayhar03901312009-01-07 00:06:22 -05004219 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004220 return;
4221
4222 if (depth--) {
4223 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004224 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004225 p = last;
4226 while (--p >= first) {
4227 nr = le32_to_cpu(*p);
4228 if (!nr)
4229 continue; /* A hole */
4230
4231 /* Go read the buffer for the next level down */
4232 bh = sb_bread(inode->i_sb, nr);
4233
4234 /*
4235 * A read failure? Report error and clear slot
4236 * (should be rare).
4237 */
4238 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07004239 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07004240 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004241 inode->i_ino, nr);
4242 continue;
4243 }
4244
4245 /* This zaps the entire block. Bottom up. */
4246 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07004247 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004248 (__le32 *) bh->b_data,
4249 (__le32 *) bh->b_data + addr_per_block,
4250 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004251
4252 /*
4253 * We've probably journalled the indirect block several
4254 * times during the truncate. But it's no longer
4255 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07004256 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004257 *
4258 * That's easy if it's exclusively part of this
4259 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07004260 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004261 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07004262 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004263 * unmap_underlying_metadata() will find this block
4264 * and will try to get rid of it. damn, damn.
4265 *
4266 * If this block has already been committed to the
4267 * journal, a revoke record will be written. And
4268 * revoke records must be emitted *before* clearing
4269 * this block's bit in the bitmaps.
4270 */
Mingming Cao617ba132006-10-11 01:20:53 -07004271 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004272
4273 /*
4274 * Everything below this this pointer has been
4275 * released. Now let this top-of-subtree go.
4276 *
4277 * We want the freeing of this indirect block to be
4278 * atomic in the journal with the updating of the
4279 * bitmap block which owns it. So make some room in
4280 * the journal.
4281 *
4282 * We zero the parent pointer *after* freeing its
4283 * pointee in the bitmaps, so if extend_transaction()
4284 * for some reason fails to put the bitmap changes and
4285 * the release into the same transaction, recovery
4286 * will merely complain about releasing a free block,
4287 * rather than leaking blocks.
4288 */
Frank Mayhar03901312009-01-07 00:06:22 -05004289 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004290 return;
4291 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004292 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04004293 ext4_truncate_restart_trans(handle, inode,
4294 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004295 }
4296
Theodore Ts'oe6362602009-11-23 07:17:05 -05004297 ext4_free_blocks(handle, inode, 0, nr, 1,
4298 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004299
4300 if (parent_bh) {
4301 /*
4302 * The block which we have just freed is
4303 * pointed to by an indirect block: journal it
4304 */
4305 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004306 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004307 parent_bh)){
4308 *p = 0;
4309 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05004310 "call ext4_handle_dirty_metadata");
4311 ext4_handle_dirty_metadata(handle,
4312 inode,
4313 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004314 }
4315 }
4316 }
4317 } else {
4318 /* We have reached the bottom of the tree. */
4319 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07004320 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004321 }
4322}
4323
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004324int ext4_can_truncate(struct inode *inode)
4325{
4326 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4327 return 0;
4328 if (S_ISREG(inode->i_mode))
4329 return 1;
4330 if (S_ISDIR(inode->i_mode))
4331 return 1;
4332 if (S_ISLNK(inode->i_mode))
4333 return !ext4_inode_is_fast_symlink(inode);
4334 return 0;
4335}
4336
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004337/*
Mingming Cao617ba132006-10-11 01:20:53 -07004338 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004339 *
Mingming Cao617ba132006-10-11 01:20:53 -07004340 * We block out ext4_get_block() block instantiations across the entire
4341 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004342 * simultaneously on behalf of the same inode.
4343 *
4344 * As we work through the truncate and commmit bits of it to the journal there
4345 * is one core, guiding principle: the file's tree must always be consistent on
4346 * disk. We must be able to restart the truncate after a crash.
4347 *
4348 * The file's tree may be transiently inconsistent in memory (although it
4349 * probably isn't), but whenever we close off and commit a journal transaction,
4350 * the contents of (the filesystem + the journal) must be consistent and
4351 * restartable. It's pretty simple, really: bottom up, right to left (although
4352 * left-to-right works OK too).
4353 *
4354 * Note that at recovery time, journal replay occurs *before* the restart of
4355 * truncate against the orphan inode list.
4356 *
4357 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004358 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004359 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004360 * ext4_truncate() to have another go. So there will be instantiated blocks
4361 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004362 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004363 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004364 */
Mingming Cao617ba132006-10-11 01:20:53 -07004365void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004366{
4367 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07004368 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004369 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004370 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004371 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004372 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004373 Indirect chain[4];
4374 Indirect *partial;
4375 __le32 nr = 0;
4376 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004377 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004378 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004379
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004380 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381 return;
4382
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004383 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004384 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
4385
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004386 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04004387 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004388 return;
4389 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004390
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004391 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04004392 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004393 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004394
4395 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07004396 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004397
Jan Karacf108bc2008-07-11 19:27:31 -04004398 if (inode->i_size & (blocksize - 1))
4399 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
4400 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004401
Mingming Cao617ba132006-10-11 01:20:53 -07004402 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004403 if (n == 0)
4404 goto out_stop; /* error */
4405
4406 /*
4407 * OK. This truncate is going to happen. We add the inode to the
4408 * orphan list, so that if this truncate spans multiple transactions,
4409 * and we crash, we will resume the truncate when the filesystem
4410 * recovers. It also marks the inode dirty, to catch the new size.
4411 *
4412 * Implication: the file must always be in a sane, consistent
4413 * truncatable state while each transaction commits.
4414 */
Mingming Cao617ba132006-10-11 01:20:53 -07004415 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004416 goto out_stop;
4417
4418 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004419 * From here we block out all ext4_get_block() callers who want to
4420 * modify the block allocation tree.
4421 */
4422 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004423
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004424 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004425
Mingming Cao632eaea2008-07-11 19:27:31 -04004426 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004427 * The orphan list entry will now protect us from any crash which
4428 * occurs before the truncate completes, so it is now safe to propagate
4429 * the new, shorter inode size (held for now in i_size) into the
4430 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004431 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004432 */
4433 ei->i_disksize = inode->i_size;
4434
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004435 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004436 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4437 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004438 goto do_indirects;
4439 }
4440
Mingming Cao617ba132006-10-11 01:20:53 -07004441 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004442 /* Kill the top of shared branch (not detached) */
4443 if (nr) {
4444 if (partial == chain) {
4445 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004446 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004447 &nr, &nr+1, (chain+n-1) - partial);
4448 *partial->p = 0;
4449 /*
4450 * We mark the inode dirty prior to restart,
4451 * and prior to stop. No need for it here.
4452 */
4453 } else {
4454 /* Shared branch grows from an indirect block */
4455 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004456 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004457 partial->p,
4458 partial->p+1, (chain+n-1) - partial);
4459 }
4460 }
4461 /* Clear the ends of indirect blocks on the shared branch */
4462 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004463 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004464 (__le32*)partial->bh->b_data+addr_per_block,
4465 (chain+n-1) - partial);
4466 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004467 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004468 partial--;
4469 }
4470do_indirects:
4471 /* Kill the remaining (whole) subtrees */
4472 switch (offsets[0]) {
4473 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004474 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004475 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004476 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4477 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004478 }
Mingming Cao617ba132006-10-11 01:20:53 -07004479 case EXT4_IND_BLOCK:
4480 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004481 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004482 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4483 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004484 }
Mingming Cao617ba132006-10-11 01:20:53 -07004485 case EXT4_DIND_BLOCK:
4486 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004487 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004488 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4489 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004490 }
Mingming Cao617ba132006-10-11 01:20:53 -07004491 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004492 ;
4493 }
4494
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004495 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004496 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004497 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004498
4499 /*
4500 * In a multi-transaction truncate, we only make the final transaction
4501 * synchronous
4502 */
4503 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004504 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004505out_stop:
4506 /*
4507 * If this was a simple ftruncate(), and the file will remain alive
4508 * then we need to clear up the orphan record which we created above.
4509 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004510 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004511 * orphan info for us.
4512 */
4513 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004514 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004515
Mingming Cao617ba132006-10-11 01:20:53 -07004516 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004517}
4518
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004519/*
Mingming Cao617ba132006-10-11 01:20:53 -07004520 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004521 * underlying buffer_head on success. If 'in_mem' is true, we have all
4522 * data in memory that is needed to recreate the on-disk version of this
4523 * inode.
4524 */
Mingming Cao617ba132006-10-11 01:20:53 -07004525static int __ext4_get_inode_loc(struct inode *inode,
4526 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004527{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004528 struct ext4_group_desc *gdp;
4529 struct buffer_head *bh;
4530 struct super_block *sb = inode->i_sb;
4531 ext4_fsblk_t block;
4532 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004533
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004534 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004535 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004536 return -EIO;
4537
Theodore Ts'o240799c2008-10-09 23:53:47 -04004538 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4539 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4540 if (!gdp)
4541 return -EIO;
4542
4543 /*
4544 * Figure out the offset within the block group inode table
4545 */
4546 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4547 inode_offset = ((inode->i_ino - 1) %
4548 EXT4_INODES_PER_GROUP(sb));
4549 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4550 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4551
4552 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004553 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004554 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4555 "inode block - inode=%lu, block=%llu",
4556 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004557 return -EIO;
4558 }
4559 if (!buffer_uptodate(bh)) {
4560 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004561
4562 /*
4563 * If the buffer has the write error flag, we have failed
4564 * to write out another inode in the same block. In this
4565 * case, we don't have to read the block because we may
4566 * read the old inode data successfully.
4567 */
4568 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4569 set_buffer_uptodate(bh);
4570
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004571 if (buffer_uptodate(bh)) {
4572 /* someone brought it uptodate while we waited */
4573 unlock_buffer(bh);
4574 goto has_buffer;
4575 }
4576
4577 /*
4578 * If we have all information of the inode in memory and this
4579 * is the only valid inode in the block, we need not read the
4580 * block.
4581 */
4582 if (in_mem) {
4583 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004584 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004585
Theodore Ts'o240799c2008-10-09 23:53:47 -04004586 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004587
4588 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004589 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004590 if (!bitmap_bh)
4591 goto make_io;
4592
4593 /*
4594 * If the inode bitmap isn't in cache then the
4595 * optimisation may end up performing two reads instead
4596 * of one, so skip it.
4597 */
4598 if (!buffer_uptodate(bitmap_bh)) {
4599 brelse(bitmap_bh);
4600 goto make_io;
4601 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004602 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004603 if (i == inode_offset)
4604 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004605 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004606 break;
4607 }
4608 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004609 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004610 /* all other inodes are free, so skip I/O */
4611 memset(bh->b_data, 0, bh->b_size);
4612 set_buffer_uptodate(bh);
4613 unlock_buffer(bh);
4614 goto has_buffer;
4615 }
4616 }
4617
4618make_io:
4619 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004620 * If we need to do any I/O, try to pre-readahead extra
4621 * blocks from the inode table.
4622 */
4623 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4624 ext4_fsblk_t b, end, table;
4625 unsigned num;
4626
4627 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004628 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004629 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4630 if (table > b)
4631 b = table;
4632 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4633 num = EXT4_INODES_PER_GROUP(sb);
4634 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4635 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004636 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004637 table += num / inodes_per_block;
4638 if (end > table)
4639 end = table;
4640 while (b <= end)
4641 sb_breadahead(sb, b++);
4642 }
4643
4644 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004645 * There are other valid inodes in the buffer, this inode
4646 * has in-inode xattrs, or we don't have this inode in memory.
4647 * Read the block from disk.
4648 */
4649 get_bh(bh);
4650 bh->b_end_io = end_buffer_read_sync;
4651 submit_bh(READ_META, bh);
4652 wait_on_buffer(bh);
4653 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004654 ext4_error(sb, __func__,
4655 "unable to read inode block - inode=%lu, "
4656 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004657 brelse(bh);
4658 return -EIO;
4659 }
4660 }
4661has_buffer:
4662 iloc->bh = bh;
4663 return 0;
4664}
4665
Mingming Cao617ba132006-10-11 01:20:53 -07004666int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004667{
4668 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004669 return __ext4_get_inode_loc(inode, iloc,
4670 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004671}
4672
Mingming Cao617ba132006-10-11 01:20:53 -07004673void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004674{
Mingming Cao617ba132006-10-11 01:20:53 -07004675 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004676
4677 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004678 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004679 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004680 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004681 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004682 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004683 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004684 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004685 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004686 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004687 inode->i_flags |= S_DIRSYNC;
4688}
4689
Jan Karaff9ddf72007-07-18 09:24:20 -04004690/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4691void ext4_get_inode_flags(struct ext4_inode_info *ei)
4692{
4693 unsigned int flags = ei->vfs_inode.i_flags;
4694
4695 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4696 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4697 if (flags & S_SYNC)
4698 ei->i_flags |= EXT4_SYNC_FL;
4699 if (flags & S_APPEND)
4700 ei->i_flags |= EXT4_APPEND_FL;
4701 if (flags & S_IMMUTABLE)
4702 ei->i_flags |= EXT4_IMMUTABLE_FL;
4703 if (flags & S_NOATIME)
4704 ei->i_flags |= EXT4_NOATIME_FL;
4705 if (flags & S_DIRSYNC)
4706 ei->i_flags |= EXT4_DIRSYNC_FL;
4707}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004708
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004709static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004710 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004711{
4712 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004713 struct inode *inode = &(ei->vfs_inode);
4714 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004715
4716 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4717 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4718 /* we are using combined 48 bit field */
4719 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4720 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004721 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4722 /* i_blocks represent file system block size */
4723 return i_blocks << (inode->i_blkbits - 9);
4724 } else {
4725 return i_blocks;
4726 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004727 } else {
4728 return le32_to_cpu(raw_inode->i_blocks_lo);
4729 }
4730}
Jan Karaff9ddf72007-07-18 09:24:20 -04004731
David Howells1d1fe1e2008-02-07 00:15:37 -08004732struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004733{
Mingming Cao617ba132006-10-11 01:20:53 -07004734 struct ext4_iloc iloc;
4735 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004736 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004737 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004738 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004739 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004740 int block;
4741
David Howells1d1fe1e2008-02-07 00:15:37 -08004742 inode = iget_locked(sb, ino);
4743 if (!inode)
4744 return ERR_PTR(-ENOMEM);
4745 if (!(inode->i_state & I_NEW))
4746 return inode;
4747
4748 ei = EXT4_I(inode);
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004749 iloc.bh = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004750
David Howells1d1fe1e2008-02-07 00:15:37 -08004751 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4752 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004753 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004754 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004755 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4756 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4757 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004758 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004759 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4760 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4761 }
4762 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004763
4764 ei->i_state = 0;
4765 ei->i_dir_start_lookup = 0;
4766 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4767 /* We now have enough fields to check if the inode was active or not.
4768 * This is needed because nfsd might try to access dead inodes
4769 * the test is that same one that e2fsck uses
4770 * NeilBrown 1999oct15
4771 */
4772 if (inode->i_nlink == 0) {
4773 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004774 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004775 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004776 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004777 goto bad_inode;
4778 }
4779 /* The only unlinked inodes we let through here have
4780 * valid i_mode and are being read by the orphan
4781 * recovery code: that's fine, we're about to complete
4782 * the process of deleting those. */
4783 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004784 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004785 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004786 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004787 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004788 ei->i_file_acl |=
4789 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004790 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004791 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004792#ifdef CONFIG_QUOTA
4793 ei->i_reserved_quota = 0;
4794#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004795 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4796 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004797 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004798 /*
4799 * NOTE! The in-memory inode i_data array is in little-endian order
4800 * even on big-endian machines: we do NOT byteswap the block numbers!
4801 */
Mingming Cao617ba132006-10-11 01:20:53 -07004802 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004803 ei->i_data[block] = raw_inode->i_block[block];
4804 INIT_LIST_HEAD(&ei->i_orphan);
4805
Jan Karab436b9b2009-12-08 23:51:10 -05004806 /*
4807 * Set transaction id's of transactions that have to be committed
4808 * to finish f[data]sync. We set them to currently running transaction
4809 * as we cannot be sure that the inode or some of its metadata isn't
4810 * part of the transaction - the inode could have been reclaimed and
4811 * now it is reread from disk.
4812 */
4813 if (journal) {
4814 transaction_t *transaction;
4815 tid_t tid;
4816
4817 spin_lock(&journal->j_state_lock);
4818 if (journal->j_running_transaction)
4819 transaction = journal->j_running_transaction;
4820 else
4821 transaction = journal->j_committing_transaction;
4822 if (transaction)
4823 tid = transaction->t_tid;
4824 else
4825 tid = journal->j_commit_sequence;
4826 spin_unlock(&journal->j_state_lock);
4827 ei->i_sync_tid = tid;
4828 ei->i_datasync_tid = tid;
4829 }
4830
Eric Sandeen0040d982008-02-05 22:36:43 -05004831 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004832 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004833 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004834 EXT4_INODE_SIZE(inode->i_sb)) {
David Howells1d1fe1e2008-02-07 00:15:37 -08004835 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004836 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004837 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004838 if (ei->i_extra_isize == 0) {
4839 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004840 ei->i_extra_isize = sizeof(struct ext4_inode) -
4841 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004842 } else {
4843 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004844 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004845 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004846 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004847 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004848 }
4849 } else
4850 ei->i_extra_isize = 0;
4851
Kalpak Shahef7f3832007-07-18 09:15:20 -04004852 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4853 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4854 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4855 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4856
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004857 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4858 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4859 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4860 inode->i_version |=
4861 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4862 }
4863
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004864 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004865 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004866 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004867 ext4_error(sb, __func__,
4868 "bad extended attribute block %llu in inode #%lu",
4869 ei->i_file_acl, inode->i_ino);
4870 ret = -EIO;
4871 goto bad_inode;
4872 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004873 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4874 (S_ISLNK(inode->i_mode) &&
4875 !ext4_inode_is_fast_symlink(inode)))
4876 /* Validate extent which is part of inode */
4877 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004878 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004879 (S_ISLNK(inode->i_mode) &&
4880 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004881 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004882 ret = ext4_check_inode_blockref(inode);
4883 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004884 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004885 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004886
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004887 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004888 inode->i_op = &ext4_file_inode_operations;
4889 inode->i_fop = &ext4_file_operations;
4890 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004891 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004892 inode->i_op = &ext4_dir_inode_operations;
4893 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004894 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004895 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004896 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004897 nd_terminate_link(ei->i_data, inode->i_size,
4898 sizeof(ei->i_data) - 1);
4899 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004900 inode->i_op = &ext4_symlink_inode_operations;
4901 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004902 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004903 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4904 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004905 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004906 if (raw_inode->i_block[0])
4907 init_special_inode(inode, inode->i_mode,
4908 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4909 else
4910 init_special_inode(inode, inode->i_mode,
4911 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004912 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004913 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004914 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004915 "bogus i_mode (%o) for inode=%lu",
4916 inode->i_mode, inode->i_ino);
4917 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004918 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004919 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004920 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004921 unlock_new_inode(inode);
4922 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004923
4924bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004925 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004926 iget_failed(inode);
4927 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004928}
4929
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004930static int ext4_inode_blocks_set(handle_t *handle,
4931 struct ext4_inode *raw_inode,
4932 struct ext4_inode_info *ei)
4933{
4934 struct inode *inode = &(ei->vfs_inode);
4935 u64 i_blocks = inode->i_blocks;
4936 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004937
4938 if (i_blocks <= ~0U) {
4939 /*
4940 * i_blocks can be represnted in a 32 bit variable
4941 * as multiple of 512 bytes
4942 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004943 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004944 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004945 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004946 return 0;
4947 }
4948 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4949 return -EFBIG;
4950
4951 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004952 /*
4953 * i_blocks can be represented in a 48 bit variable
4954 * as multiple of 512 bytes
4955 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004956 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004957 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004958 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004959 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004960 ei->i_flags |= EXT4_HUGE_FILE_FL;
4961 /* i_block is stored in file system block size */
4962 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4963 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4964 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004965 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004966 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004967}
4968
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004969/*
4970 * Post the struct inode info into an on-disk inode location in the
4971 * buffer-cache. This gobbles the caller's reference to the
4972 * buffer_head in the inode location struct.
4973 *
4974 * The caller must have write access to iloc->bh.
4975 */
Mingming Cao617ba132006-10-11 01:20:53 -07004976static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004977 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004978 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004979{
Mingming Cao617ba132006-10-11 01:20:53 -07004980 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4981 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004982 struct buffer_head *bh = iloc->bh;
4983 int err = 0, rc, block;
4984
4985 /* For fields not not tracking in the in-memory inode,
4986 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004987 if (ei->i_state & EXT4_STATE_NEW)
4988 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004989
Jan Karaff9ddf72007-07-18 09:24:20 -04004990 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004991 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004992 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004993 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4994 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4995/*
4996 * Fix up interoperability with old kernels. Otherwise, old inodes get
4997 * re-used with the upper 16 bits of the uid/gid intact
4998 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004999 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005000 raw_inode->i_uid_high =
5001 cpu_to_le16(high_16_bits(inode->i_uid));
5002 raw_inode->i_gid_high =
5003 cpu_to_le16(high_16_bits(inode->i_gid));
5004 } else {
5005 raw_inode->i_uid_high = 0;
5006 raw_inode->i_gid_high = 0;
5007 }
5008 } else {
5009 raw_inode->i_uid_low =
5010 cpu_to_le16(fs_high2lowuid(inode->i_uid));
5011 raw_inode->i_gid_low =
5012 cpu_to_le16(fs_high2lowgid(inode->i_gid));
5013 raw_inode->i_uid_high = 0;
5014 raw_inode->i_gid_high = 0;
5015 }
5016 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04005017
5018 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5019 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5020 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5021 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5022
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005023 if (ext4_inode_blocks_set(handle, raw_inode, ei))
5024 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005025 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o1b9c12f2009-09-17 08:32:22 -04005026 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07005027 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
5028 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005029 raw_inode->i_file_acl_high =
5030 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005031 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005032 ext4_isize_set(raw_inode, ei->i_disksize);
5033 if (ei->i_disksize > 0x7fffffffULL) {
5034 struct super_block *sb = inode->i_sb;
5035 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
5036 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
5037 EXT4_SB(sb)->s_es->s_rev_level ==
5038 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
5039 /* If this is the first large file
5040 * created, add a flag to the superblock.
5041 */
5042 err = ext4_journal_get_write_access(handle,
5043 EXT4_SB(sb)->s_sbh);
5044 if (err)
5045 goto out_brelse;
5046 ext4_update_dynamic_rev(sb);
5047 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07005048 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005049 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05005050 ext4_handle_sync(handle);
5051 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005052 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005053 }
5054 }
5055 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5056 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5057 if (old_valid_dev(inode->i_rdev)) {
5058 raw_inode->i_block[0] =
5059 cpu_to_le32(old_encode_dev(inode->i_rdev));
5060 raw_inode->i_block[1] = 0;
5061 } else {
5062 raw_inode->i_block[0] = 0;
5063 raw_inode->i_block[1] =
5064 cpu_to_le32(new_encode_dev(inode->i_rdev));
5065 raw_inode->i_block[2] = 0;
5066 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005067 } else
5068 for (block = 0; block < EXT4_N_BLOCKS; block++)
5069 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005070
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005071 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
5072 if (ei->i_extra_isize) {
5073 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5074 raw_inode->i_version_hi =
5075 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005076 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005077 }
5078
Frank Mayhar830156c2009-09-29 10:07:47 -04005079 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
5080 rc = ext4_handle_dirty_metadata(handle, inode, bh);
5081 if (!err)
5082 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07005083 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005084
Jan Karab436b9b2009-12-08 23:51:10 -05005085 ext4_update_inode_fsync_trans(handle, inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005086out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005087 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005088 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005089 return err;
5090}
5091
5092/*
Mingming Cao617ba132006-10-11 01:20:53 -07005093 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005094 *
5095 * We are called from a few places:
5096 *
5097 * - Within generic_file_write() for O_SYNC files.
5098 * Here, there will be no transaction running. We wait for any running
5099 * trasnaction to commit.
5100 *
5101 * - Within sys_sync(), kupdate and such.
5102 * We wait on commit, if tol to.
5103 *
5104 * - Within prune_icache() (PF_MEMALLOC == true)
5105 * Here we simply return. We can't afford to block kswapd on the
5106 * journal commit.
5107 *
5108 * In all cases it is actually safe for us to return without doing anything,
5109 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07005110 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005111 * knfsd.
5112 *
5113 * Note that we are absolutely dependent upon all inode dirtiers doing the
5114 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5115 * which we are interested.
5116 *
5117 * It would be a bug for them to not do this. The code:
5118 *
5119 * mark_inode_dirty(inode)
5120 * stuff();
5121 * inode->i_size = expr;
5122 *
5123 * is in error because a kswapd-driven write_inode() could occur while
5124 * `stuff()' is running, and the new i_size will be lost. Plus the inode
5125 * will no longer be on the superblock's dirty inode list.
5126 */
Mingming Cao617ba132006-10-11 01:20:53 -07005127int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005128{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005129 int err;
5130
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005131 if (current->flags & PF_MEMALLOC)
5132 return 0;
5133
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005134 if (EXT4_SB(inode->i_sb)->s_journal) {
5135 if (ext4_journal_current_handle()) {
5136 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5137 dump_stack();
5138 return -EIO;
5139 }
5140
5141 if (!wait)
5142 return 0;
5143
5144 err = ext4_force_commit(inode->i_sb);
5145 } else {
5146 struct ext4_iloc iloc;
5147
5148 err = ext4_get_inode_loc(inode, &iloc);
5149 if (err)
5150 return err;
Frank Mayhar830156c2009-09-29 10:07:47 -04005151 if (wait)
5152 sync_dirty_buffer(iloc.bh);
5153 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
5154 ext4_error(inode->i_sb, __func__,
5155 "IO error syncing inode, "
5156 "inode=%lu, block=%llu",
5157 inode->i_ino,
5158 (unsigned long long)iloc.bh->b_blocknr);
5159 err = -EIO;
5160 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005161 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005162 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005163}
5164
5165/*
Mingming Cao617ba132006-10-11 01:20:53 -07005166 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005167 *
5168 * Called from notify_change.
5169 *
5170 * We want to trap VFS attempts to truncate the file as soon as
5171 * possible. In particular, we want to make sure that when the VFS
5172 * shrinks i_size, we put the inode on the orphan list and modify
5173 * i_disksize immediately, so that during the subsequent flushing of
5174 * dirty pages and freeing of disk blocks, we can guarantee that any
5175 * commit will leave the blocks being flushed in an unused state on
5176 * disk. (On recovery, the inode will get truncated and the blocks will
5177 * be freed, so we have a strong guarantee that no future commit will
5178 * leave these blocks visible to the user.)
5179 *
Jan Kara678aaf42008-07-11 19:27:31 -04005180 * Another thing we have to assure is that if we are in ordered mode
5181 * and inode is still attached to the committing transaction, we must
5182 * we start writeout of all the dirty pages which are being truncated.
5183 * This way we are sure that all the data written in the previous
5184 * transaction are already on disk (truncate waits for pages under
5185 * writeback).
5186 *
5187 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005188 */
Mingming Cao617ba132006-10-11 01:20:53 -07005189int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005190{
5191 struct inode *inode = dentry->d_inode;
5192 int error, rc = 0;
5193 const unsigned int ia_valid = attr->ia_valid;
5194
5195 error = inode_change_ok(inode, attr);
5196 if (error)
5197 return error;
5198
5199 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
5200 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
5201 handle_t *handle;
5202
5203 /* (user+group)*(old+new) structure, inode write (sb,
5204 * inode block, ? - but truncate inode update has it) */
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05005205 handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
Dmitry Monakhov194074a2009-12-08 22:42:28 -05005206 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005207 if (IS_ERR(handle)) {
5208 error = PTR_ERR(handle);
5209 goto err_out;
5210 }
Jan Karaa269eb12009-01-26 17:04:39 +01005211 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005212 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005213 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005214 return error;
5215 }
5216 /* Update corresponding info in inode so that everything is in
5217 * one transaction */
5218 if (attr->ia_valid & ATTR_UID)
5219 inode->i_uid = attr->ia_uid;
5220 if (attr->ia_valid & ATTR_GID)
5221 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005222 error = ext4_mark_inode_dirty(handle, inode);
5223 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005224 }
5225
Eric Sandeene2b46572008-01-28 23:58:27 -05005226 if (attr->ia_valid & ATTR_SIZE) {
5227 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
5228 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5229
5230 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
5231 error = -EFBIG;
5232 goto err_out;
5233 }
5234 }
5235 }
5236
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005237 if (S_ISREG(inode->i_mode) &&
5238 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
5239 handle_t *handle;
5240
Mingming Cao617ba132006-10-11 01:20:53 -07005241 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005242 if (IS_ERR(handle)) {
5243 error = PTR_ERR(handle);
5244 goto err_out;
5245 }
5246
Mingming Cao617ba132006-10-11 01:20:53 -07005247 error = ext4_orphan_add(handle, inode);
5248 EXT4_I(inode)->i_disksize = attr->ia_size;
5249 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005250 if (!error)
5251 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07005252 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04005253
5254 if (ext4_should_order_data(inode)) {
5255 error = ext4_begin_ordered_truncate(inode,
5256 attr->ia_size);
5257 if (error) {
5258 /* Do as much error cleanup as possible */
5259 handle = ext4_journal_start(inode, 3);
5260 if (IS_ERR(handle)) {
5261 ext4_orphan_del(NULL, inode);
5262 goto err_out;
5263 }
5264 ext4_orphan_del(handle, inode);
5265 ext4_journal_stop(handle);
5266 goto err_out;
5267 }
5268 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005269 }
5270
5271 rc = inode_setattr(inode, attr);
5272
Mingming Cao617ba132006-10-11 01:20:53 -07005273 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005274 * transaction handle at all, we need to clean up the in-core
5275 * orphan list manually. */
5276 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005277 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005278
5279 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07005280 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005281
5282err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005283 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005284 if (!error)
5285 error = rc;
5286 return error;
5287}
5288
Mingming Cao3e3398a2008-07-11 19:27:31 -04005289int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5290 struct kstat *stat)
5291{
5292 struct inode *inode;
5293 unsigned long delalloc_blocks;
5294
5295 inode = dentry->d_inode;
5296 generic_fillattr(inode, stat);
5297
5298 /*
5299 * We can't update i_blocks if the block allocation is delayed
5300 * otherwise in the case of system crash before the real block
5301 * allocation is done, we will have i_blocks inconsistent with
5302 * on-disk file blocks.
5303 * We always keep i_blocks updated together with real
5304 * allocation. But to not confuse with user, stat
5305 * will return the blocks that include the delayed allocation
5306 * blocks for this file.
5307 */
5308 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
5309 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
5310 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
5311
5312 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
5313 return 0;
5314}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005315
Mingming Caoa02908f2008-08-19 22:16:07 -04005316static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
5317 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005318{
Mingming Caoa02908f2008-08-19 22:16:07 -04005319 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005320
Mingming Caoa02908f2008-08-19 22:16:07 -04005321 /* if nrblocks are contiguous */
5322 if (chunk) {
5323 /*
5324 * With N contiguous data blocks, it need at most
5325 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
5326 * 2 dindirect blocks
5327 * 1 tindirect block
5328 */
5329 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
5330 return indirects + 3;
5331 }
5332 /*
5333 * if nrblocks are not contiguous, worse case, each block touch
5334 * a indirect block, and each indirect block touch a double indirect
5335 * block, plus a triple indirect block
5336 */
5337 indirects = nrblocks * 2 + 1;
5338 return indirects;
5339}
Alex Tomasa86c6182006-10-11 01:21:03 -07005340
Mingming Caoa02908f2008-08-19 22:16:07 -04005341static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5342{
5343 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05005344 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
5345 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04005346}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005347
Mingming Caoa02908f2008-08-19 22:16:07 -04005348/*
5349 * Account for index blocks, block groups bitmaps and block group
5350 * descriptor blocks if modify datablocks and index blocks
5351 * worse case, the indexs blocks spread over different block groups
5352 *
5353 * If datablocks are discontiguous, they are possible to spread over
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005354 * different block groups too. If they are contiuguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005355 * they could still across block group boundary.
5356 *
5357 * Also account for superblock, inode, quota and xattr blocks
5358 */
5359int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5360{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005361 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5362 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005363 int idxblocks;
5364 int ret = 0;
5365
5366 /*
5367 * How many index blocks need to touch to modify nrblocks?
5368 * The "Chunk" flag indicating whether the nrblocks is
5369 * physically contiguous on disk
5370 *
5371 * For Direct IO and fallocate, they calls get_block to allocate
5372 * one single extent at a time, so they could set the "Chunk" flag
5373 */
5374 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
5375
5376 ret = idxblocks;
5377
5378 /*
5379 * Now let's see how many group bitmaps and group descriptors need
5380 * to account
5381 */
5382 groups = idxblocks;
5383 if (chunk)
5384 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005385 else
Mingming Caoa02908f2008-08-19 22:16:07 -04005386 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005387
Mingming Caoa02908f2008-08-19 22:16:07 -04005388 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005389 if (groups > ngroups)
5390 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005391 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5392 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5393
5394 /* bitmaps and block group descriptor blocks */
5395 ret += groups + gdpblocks;
5396
5397 /* Blocks for super block, inode, quota and xattr blocks */
5398 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005399
5400 return ret;
5401}
5402
5403/*
Mingming Caoa02908f2008-08-19 22:16:07 -04005404 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005405 * the modification of a single pages into a single transaction,
5406 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005407 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005408 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005409 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005410 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005411 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005412 */
5413int ext4_writepage_trans_blocks(struct inode *inode)
5414{
5415 int bpp = ext4_journal_blocks_per_page(inode);
5416 int ret;
5417
5418 ret = ext4_meta_trans_blocks(inode, bpp, 0);
5419
5420 /* Account for data blocks for journalled mode */
5421 if (ext4_should_journal_data(inode))
5422 ret += bpp;
5423 return ret;
5424}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005425
5426/*
5427 * Calculate the journal credits for a chunk of data modification.
5428 *
5429 * This is called from DIO, fallocate or whoever calling
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005430 * ext4_get_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005431 *
5432 * journal buffers for data blocks are not included here, as DIO
5433 * and fallocate do no need to journal data buffers.
5434 */
5435int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5436{
5437 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5438}
5439
Mingming Caoa02908f2008-08-19 22:16:07 -04005440/*
Mingming Cao617ba132006-10-11 01:20:53 -07005441 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005442 * Give this, we know that the caller already has write access to iloc->bh.
5443 */
Mingming Cao617ba132006-10-11 01:20:53 -07005444int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005445 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005446{
5447 int err = 0;
5448
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005449 if (test_opt(inode->i_sb, I_VERSION))
5450 inode_inc_iversion(inode);
5451
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005452 /* the do_update_inode consumes one bh->b_count */
5453 get_bh(iloc->bh);
5454
Mingming Caodab291a2006-10-11 01:21:01 -07005455 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005456 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005457 put_bh(iloc->bh);
5458 return err;
5459}
5460
5461/*
5462 * On success, We end up with an outstanding reference count against
5463 * iloc->bh. This _must_ be cleaned up later.
5464 */
5465
5466int
Mingming Cao617ba132006-10-11 01:20:53 -07005467ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5468 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005469{
Frank Mayhar03901312009-01-07 00:06:22 -05005470 int err;
5471
5472 err = ext4_get_inode_loc(inode, iloc);
5473 if (!err) {
5474 BUFFER_TRACE(iloc->bh, "get_write_access");
5475 err = ext4_journal_get_write_access(handle, iloc->bh);
5476 if (err) {
5477 brelse(iloc->bh);
5478 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005479 }
5480 }
Mingming Cao617ba132006-10-11 01:20:53 -07005481 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005482 return err;
5483}
5484
5485/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005486 * Expand an inode by new_extra_isize bytes.
5487 * Returns 0 on success or negative error number on failure.
5488 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005489static int ext4_expand_extra_isize(struct inode *inode,
5490 unsigned int new_extra_isize,
5491 struct ext4_iloc iloc,
5492 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005493{
5494 struct ext4_inode *raw_inode;
5495 struct ext4_xattr_ibody_header *header;
5496 struct ext4_xattr_entry *entry;
5497
5498 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5499 return 0;
5500
5501 raw_inode = ext4_raw_inode(&iloc);
5502
5503 header = IHDR(inode, raw_inode);
5504 entry = IFIRST(header);
5505
5506 /* No extended attributes present */
5507 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5508 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5509 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5510 new_extra_isize);
5511 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5512 return 0;
5513 }
5514
5515 /* try to expand with EAs present */
5516 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5517 raw_inode, handle);
5518}
5519
5520/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005521 * What we do here is to mark the in-core inode as clean with respect to inode
5522 * dirtiness (it may still be data-dirty).
5523 * This means that the in-core inode may be reaped by prune_icache
5524 * without having to perform any I/O. This is a very good thing,
5525 * because *any* task may call prune_icache - even ones which
5526 * have a transaction open against a different journal.
5527 *
5528 * Is this cheating? Not really. Sure, we haven't written the
5529 * inode out, but prune_icache isn't a user-visible syncing function.
5530 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5531 * we start and wait on commits.
5532 *
5533 * Is this efficient/effective? Well, we're being nice to the system
5534 * by cleaning up our inodes proactively so they can be reaped
5535 * without I/O. But we are potentially leaving up to five seconds'
5536 * worth of inodes floating about which prune_icache wants us to
5537 * write out. One way to fix that would be to get prune_icache()
5538 * to do a write_super() to free up some memory. It has the desired
5539 * effect.
5540 */
Mingming Cao617ba132006-10-11 01:20:53 -07005541int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005542{
Mingming Cao617ba132006-10-11 01:20:53 -07005543 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005544 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5545 static unsigned int mnt_count;
5546 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005547
5548 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005549 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005550 if (ext4_handle_valid(handle) &&
5551 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005552 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5553 /*
5554 * We need extra buffer credits since we may write into EA block
5555 * with this same handle. If journal_extend fails, then it will
5556 * only result in a minor loss of functionality for that inode.
5557 * If this is felt to be critical, then e2fsck should be run to
5558 * force a large enough s_min_extra_isize.
5559 */
5560 if ((jbd2_journal_extend(handle,
5561 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5562 ret = ext4_expand_extra_isize(inode,
5563 sbi->s_want_extra_isize,
5564 iloc, handle);
5565 if (ret) {
5566 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005567 if (mnt_count !=
5568 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005569 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005570 "Unable to expand inode %lu. Delete"
5571 " some EAs or run e2fsck.",
5572 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005573 mnt_count =
5574 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005575 }
5576 }
5577 }
5578 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005579 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005580 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005581 return err;
5582}
5583
5584/*
Mingming Cao617ba132006-10-11 01:20:53 -07005585 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005586 *
5587 * We're really interested in the case where a file is being extended.
5588 * i_size has been changed by generic_commit_write() and we thus need
5589 * to include the updated inode in the current transaction.
5590 *
Jan Karaa269eb12009-01-26 17:04:39 +01005591 * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005592 * are allocated to the file.
5593 *
5594 * If the inode is marked synchronous, we don't honour that here - doing
5595 * so would cause a commit on atime updates, which we don't bother doing.
5596 * We handle synchronous inodes at the highest possible level.
5597 */
Mingming Cao617ba132006-10-11 01:20:53 -07005598void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005599{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005600 handle_t *handle;
5601
Mingming Cao617ba132006-10-11 01:20:53 -07005602 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005603 if (IS_ERR(handle))
5604 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005605
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005606 ext4_mark_inode_dirty(handle, inode);
5607
Mingming Cao617ba132006-10-11 01:20:53 -07005608 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005609out:
5610 return;
5611}
5612
5613#if 0
5614/*
5615 * Bind an inode's backing buffer_head into this transaction, to prevent
5616 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005617 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005618 * returns no iloc structure, so the caller needs to repeat the iloc
5619 * lookup to mark the inode dirty later.
5620 */
Mingming Cao617ba132006-10-11 01:20:53 -07005621static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005622{
Mingming Cao617ba132006-10-11 01:20:53 -07005623 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005624
5625 int err = 0;
5626 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005627 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005628 if (!err) {
5629 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005630 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005631 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005632 err = ext4_handle_dirty_metadata(handle,
5633 inode,
5634 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005635 brelse(iloc.bh);
5636 }
5637 }
Mingming Cao617ba132006-10-11 01:20:53 -07005638 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005639 return err;
5640}
5641#endif
5642
Mingming Cao617ba132006-10-11 01:20:53 -07005643int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005644{
5645 journal_t *journal;
5646 handle_t *handle;
5647 int err;
5648
5649 /*
5650 * We have to be very careful here: changing a data block's
5651 * journaling status dynamically is dangerous. If we write a
5652 * data block to the journal, change the status and then delete
5653 * that block, we risk forgetting to revoke the old log record
5654 * from the journal and so a subsequent replay can corrupt data.
5655 * So, first we make sure that the journal is empty and that
5656 * nobody is changing anything.
5657 */
5658
Mingming Cao617ba132006-10-11 01:20:53 -07005659 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005660 if (!journal)
5661 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005662 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005663 return -EROFS;
5664
Mingming Caodab291a2006-10-11 01:21:01 -07005665 jbd2_journal_lock_updates(journal);
5666 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005667
5668 /*
5669 * OK, there are no updates running now, and all cached data is
5670 * synced to disk. We are now in a completely consistent state
5671 * which doesn't have anything in the journal, and we know that
5672 * no filesystem updates are running, so it is safe to modify
5673 * the inode's in-core data-journaling state flag now.
5674 */
5675
5676 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005677 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005678 else
Mingming Cao617ba132006-10-11 01:20:53 -07005679 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5680 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005681
Mingming Caodab291a2006-10-11 01:21:01 -07005682 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005683
5684 /* Finally we can mark the inode as dirty. */
5685
Mingming Cao617ba132006-10-11 01:20:53 -07005686 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005687 if (IS_ERR(handle))
5688 return PTR_ERR(handle);
5689
Mingming Cao617ba132006-10-11 01:20:53 -07005690 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005691 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005692 ext4_journal_stop(handle);
5693 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005694
5695 return err;
5696}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005697
5698static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5699{
5700 return !buffer_mapped(bh);
5701}
5702
Nick Pigginc2ec1752009-03-31 15:23:21 -07005703int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005704{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005705 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005706 loff_t size;
5707 unsigned long len;
5708 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005709 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005710 struct file *file = vma->vm_file;
5711 struct inode *inode = file->f_path.dentry->d_inode;
5712 struct address_space *mapping = inode->i_mapping;
5713
5714 /*
5715 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5716 * get i_mutex because we are already holding mmap_sem.
5717 */
5718 down_read(&inode->i_alloc_sem);
5719 size = i_size_read(inode);
5720 if (page->mapping != mapping || size <= page_offset(page)
5721 || !PageUptodate(page)) {
5722 /* page got truncated from under us? */
5723 goto out_unlock;
5724 }
5725 ret = 0;
5726 if (PageMappedToDisk(page))
5727 goto out_unlock;
5728
5729 if (page->index == size >> PAGE_CACHE_SHIFT)
5730 len = size & ~PAGE_CACHE_MASK;
5731 else
5732 len = PAGE_CACHE_SIZE;
5733
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005734 lock_page(page);
5735 /*
5736 * return if we have all the buffers mapped. This avoid
5737 * the need to call write_begin/write_end which does a
5738 * journal_start/journal_stop which can block and take
5739 * long time
5740 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005741 if (page_has_buffers(page)) {
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005742 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005743 ext4_bh_unmapped)) {
5744 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005745 goto out_unlock;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005746 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005747 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005748 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005749 /*
5750 * OK, we need to fill the hole... Do write_begin write_end
5751 * to do block allocation/reservation.We are not holding
5752 * inode.i__mutex here. That allow * parallel write_begin,
5753 * write_end call. lock_page prevent this from happening
5754 * on the same page though
5755 */
5756 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005757 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005758 if (ret < 0)
5759 goto out_unlock;
5760 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005761 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005762 if (ret < 0)
5763 goto out_unlock;
5764 ret = 0;
5765out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005766 if (ret)
5767 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005768 up_read(&inode->i_alloc_sem);
5769 return ret;
5770}