blob: bec222ca9ba4c5c84d37760258b5be46a66e46db [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
Christoph Hellwig907f4552010-03-03 09:05:06 -0500173 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500174 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500175
Jan Kara678aaf42008-07-11 19:27:31 -0400176 if (ext4_should_order_data(inode))
177 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178 truncate_inode_pages(&inode->i_data, 0);
179
180 if (is_bad_inode(inode))
181 goto no_delete;
182
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400183 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400185 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 /*
187 * If we're going to skip the normal cleanup, we still need to
188 * make sure that the in-core orphan linked list is properly
189 * cleaned up.
190 */
Mingming Cao617ba132006-10-11 01:20:53 -0700191 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 goto no_delete;
193 }
194
195 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500196 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400198 err = ext4_mark_inode_dirty(handle, inode);
199 if (err) {
200 ext4_warning(inode->i_sb, __func__,
201 "couldn't mark inode dirty (err %d)", err);
202 goto stop_handle;
203 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700205 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400206
207 /*
208 * ext4_ext_truncate() doesn't reserve any slop when it
209 * restarts journal transactions; therefore there may not be
210 * enough credits left in the handle to remove the inode from
211 * the orphan list and set the dtime field.
212 */
Frank Mayhar03901312009-01-07 00:06:22 -0500213 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400214 err = ext4_journal_extend(handle, 3);
215 if (err > 0)
216 err = ext4_journal_restart(handle, 3);
217 if (err != 0) {
218 ext4_warning(inode->i_sb, __func__,
219 "couldn't extend journal (err %d)", err);
220 stop_handle:
221 ext4_journal_stop(handle);
222 goto no_delete;
223 }
224 }
225
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700227 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700229 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700231 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232 * (Well, we could do this if we need to, but heck - it works)
233 */
Mingming Cao617ba132006-10-11 01:20:53 -0700234 ext4_orphan_del(handle, inode);
235 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236
237 /*
238 * One subtle ordering requirement: if anything has gone wrong
239 * (transaction abort, IO errors, whatever), then we can still
240 * do these next steps (the fs will already have been marked as
241 * having errors), but we can't free the inode if the mark_dirty
242 * fails.
243 */
Mingming Cao617ba132006-10-11 01:20:53 -0700244 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 /* If that failed, just do the required in-core inode clear. */
246 clear_inode(inode);
247 else
Mingming Cao617ba132006-10-11 01:20:53 -0700248 ext4_free_inode(handle, inode);
249 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250 return;
251no_delete:
252 clear_inode(inode); /* We must guarantee clearing of inode... */
253}
254
255typedef struct {
256 __le32 *p;
257 __le32 key;
258 struct buffer_head *bh;
259} Indirect;
260
261static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
262{
263 p->key = *(p->p = v);
264 p->bh = bh;
265}
266
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267/**
Mingming Cao617ba132006-10-11 01:20:53 -0700268 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269 * @inode: inode in question (we are only interested in its superblock)
270 * @i_block: block number to be parsed
271 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400272 * @boundary: set this non-zero if the referred-to block is likely to be
273 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 *
Mingming Cao617ba132006-10-11 01:20:53 -0700275 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 * for UNIX filesystems - tree of pointers anchored in the inode, with
277 * data blocks at leaves and indirect blocks in intermediate nodes.
278 * This function translates the block number into path in that tree -
279 * return value is the path length and @offsets[n] is the offset of
280 * pointer to (n+1)th node in the nth one. If @block is out of range
281 * (negative or too large) warning is printed and zero returned.
282 *
283 * Note: function doesn't find node addresses, so no IO is needed. All
284 * we need to know is the capacity of indirect blocks (taken from the
285 * inode->i_sb).
286 */
287
288/*
289 * Portability note: the last comparison (check that we fit into triple
290 * indirect block) is spelled differently, because otherwise on an
291 * architecture with 32-bit longs and 8Kb pages we might get into trouble
292 * if our filesystem had 8Kb blocks. We might use long long, but that would
293 * kill us on x86. Oh, well, at least the sign propagation does not matter -
294 * i_block would have to be negative in the very beginning, so we would not
295 * get there at all.
296 */
297
Mingming Cao617ba132006-10-11 01:20:53 -0700298static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400299 ext4_lblk_t i_block,
300 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301{
Mingming Cao617ba132006-10-11 01:20:53 -0700302 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
303 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
304 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 indirect_blocks = ptrs,
306 double_blocks = (1 << (ptrs_bits * 2));
307 int n = 0;
308 int final = 0;
309
Roel Kluinc333e072009-08-10 22:47:22 -0400310 if (i_block < direct_blocks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 offsets[n++] = i_block;
312 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400313 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700314 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315 offsets[n++] = i_block;
316 final = ptrs;
317 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700318 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319 offsets[n++] = i_block >> ptrs_bits;
320 offsets[n++] = i_block & (ptrs - 1);
321 final = ptrs;
322 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700323 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324 offsets[n++] = i_block >> (ptrs_bits * 2);
325 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
326 offsets[n++] = i_block & (ptrs - 1);
327 final = ptrs;
328 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500329 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400330 "block %lu > max in inode %lu",
331 i_block + direct_blocks +
332 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 }
334 if (boundary)
335 *boundary = final - 1 - (i_block & (ptrs - 1));
336 return n;
337}
338
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400339static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400340 __le32 *p, unsigned int max)
341{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400342 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400343 unsigned int blk;
344
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400345 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400346 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400347 if (blk &&
348 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400349 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400350 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400351 "invalid block reference %u "
352 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400353 return -EIO;
354 }
355 }
356 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400357}
358
359
360#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400361 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400362 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
363
364#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400365 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400366 EXT4_NDIR_BLOCKS)
367
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368/**
Mingming Cao617ba132006-10-11 01:20:53 -0700369 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 * @inode: inode in question
371 * @depth: depth of the chain (1 - direct pointer, etc.)
372 * @offsets: offsets of pointers in inode/indirect blocks
373 * @chain: place to store the result
374 * @err: here we store the error value
375 *
376 * Function fills the array of triples <key, p, bh> and returns %NULL
377 * if everything went OK or the pointer to the last filled triple
378 * (incomplete one) otherwise. Upon the return chain[i].key contains
379 * the number of (i+1)-th block in the chain (as it is stored in memory,
380 * i.e. little-endian 32-bit), chain[i].p contains the address of that
381 * number (it points into struct inode for i==0 and into the bh->b_data
382 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
383 * block for i>0 and NULL for i==0. In other words, it holds the block
384 * numbers of the chain, addresses they were taken from (and where we can
385 * verify that chain did not change) and buffer_heads hosting these
386 * numbers.
387 *
388 * Function stops when it stumbles upon zero pointer (absent block)
389 * (pointer to last triple returned, *@err == 0)
390 * or when it gets an IO error reading an indirect block
391 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 * or when it reads all @depth-1 indirect blocks successfully and finds
393 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500394 *
395 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500396 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700397 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500398static Indirect *ext4_get_branch(struct inode *inode, int depth,
399 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 Indirect chain[4], int *err)
401{
402 struct super_block *sb = inode->i_sb;
403 Indirect *p = chain;
404 struct buffer_head *bh;
405
406 *err = 0;
407 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400408 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 if (!p->key)
410 goto no_block;
411 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400412 bh = sb_getblk(sb, le32_to_cpu(p->key));
413 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400415
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400416 if (!bh_uptodate_or_lock(bh)) {
417 if (bh_submit_read(bh) < 0) {
418 put_bh(bh);
419 goto failure;
420 }
421 /* validate block references */
422 if (ext4_check_indirect_blockref(inode, bh)) {
423 put_bh(bh);
424 goto failure;
425 }
426 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400427
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400428 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429 /* Reader: end */
430 if (!p->key)
431 goto no_block;
432 }
433 return NULL;
434
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435failure:
436 *err = -EIO;
437no_block:
438 return p;
439}
440
441/**
Mingming Cao617ba132006-10-11 01:20:53 -0700442 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 * @inode: owner
444 * @ind: descriptor of indirect block.
445 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000446 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 * It is used when heuristic for sequential allocation fails.
448 * Rules are:
449 * + if there is a block to the left of our position - allocate near it.
450 * + if pointer will live in indirect block - allocate near that block.
451 * + if pointer will live in inode - allocate in the same
452 * cylinder group.
453 *
454 * In the latter case we colour the starting block by the callers PID to
455 * prevent it from clashing with concurrent allocations for a different inode
456 * in the same block group. The PID is used here so that functionally related
457 * files will be close-by on-disk.
458 *
459 * Caller must make sure that @ind is valid and will stay that way.
460 */
Mingming Cao617ba132006-10-11 01:20:53 -0700461static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462{
Mingming Cao617ba132006-10-11 01:20:53 -0700463 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400464 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700466 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500467 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700468 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400469 ext4_group_t block_group;
470 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471
472 /* Try to find previous block */
473 for (p = ind->p - 1; p >= start; p--) {
474 if (*p)
475 return le32_to_cpu(*p);
476 }
477
478 /* No such thing, so let's try location of indirect block */
479 if (ind->bh)
480 return ind->bh->b_blocknr;
481
482 /*
483 * It is going to be referred to from the inode itself? OK, just put it
484 * into the same cylinder group then.
485 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400486 block_group = ei->i_block_group;
487 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
488 block_group &= ~(flex_size-1);
489 if (S_ISREG(inode->i_mode))
490 block_group++;
491 }
492 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500493 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
494
Theodore Ts'oa4912122009-03-12 12:18:34 -0400495 /*
496 * If we are doing delayed allocation, we don't need take
497 * colour into account.
498 */
499 if (test_opt(inode->i_sb, DELALLOC))
500 return bg_start;
501
Valerie Clement74d34872008-02-15 13:43:07 -0500502 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
503 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700504 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500505 else
506 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 return bg_start + colour;
508}
509
510/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000511 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 * @inode: owner
513 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000516 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800517 * returns it.
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400518 * Because this is only used for non-extent files, we limit the block nr
519 * to 32 bits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500521static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400522 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523{
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400524 ext4_fsblk_t goal;
525
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400527 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400530 goal = ext4_find_near(inode, partial);
531 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
532 return goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700533}
534
535/**
Mingming Cao617ba132006-10-11 01:20:53 -0700536 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 * of direct blocks need to be allocated for the given branch.
538 *
539 * @branch: chain of indirect blocks
540 * @k: number of blocks need for indirect blocks
541 * @blks: number of data blocks to be mapped.
542 * @blocks_to_boundary: the offset in the indirect block
543 *
544 * return the total number of blocks to be allocate, including the
545 * direct and indirect blocks.
546 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500547static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400548 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500550 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551
552 /*
553 * Simple case, [t,d]Indirect block(s) has not allocated yet
554 * then it's clear blocks on that path have not allocated
555 */
556 if (k > 0) {
557 /* right now we don't handle cross boundary allocation */
558 if (blks < blocks_to_boundary + 1)
559 count += blks;
560 else
561 count += blocks_to_boundary + 1;
562 return count;
563 }
564
565 count++;
566 while (count < blks && count <= blocks_to_boundary &&
567 le32_to_cpu(*(branch[0].p + count)) == 0) {
568 count++;
569 }
570 return count;
571}
572
573/**
Mingming Cao617ba132006-10-11 01:20:53 -0700574 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575 * @indirect_blks: the number of blocks need to allocate for indirect
576 * blocks
577 *
578 * @new_blocks: on return it will store the new block numbers for
579 * the indirect blocks(if needed) and the first direct block,
580 * @blks: on return it will store the total number of allocated
581 * direct blocks
582 */
Mingming Cao617ba132006-10-11 01:20:53 -0700583static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400584 ext4_lblk_t iblock, ext4_fsblk_t goal,
585 int indirect_blks, int blks,
586 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500588 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400590 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700592 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 int ret = 0;
594
595 /*
596 * Here we try to allocate the requested multiple blocks at once,
597 * on a best-effort basis.
598 * To build a branch, we should allocate blocks for
599 * the indirect blocks(if not allocated yet), and at least
600 * the first direct block of this branch. That's the
601 * minimum number of blocks need to allocate(required)
602 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400603 /* first we try to allocate the indirect blocks */
604 target = indirect_blks;
605 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 count = target;
607 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400608 current_block = ext4_new_meta_blocks(handle, inode,
609 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 if (*err)
611 goto failed_out;
612
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400613 BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS);
614
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615 target -= count;
616 /* allocate blocks for indirect blocks */
617 while (index < indirect_blks && count) {
618 new_blocks[index++] = current_block++;
619 count--;
620 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400621 if (count > 0) {
622 /*
623 * save the new block number
624 * for the first direct block
625 */
626 new_blocks[index] = current_block;
627 printk(KERN_INFO "%s returned more blocks than "
628 "requested\n", __func__);
629 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400631 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 }
633
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400634 target = blks - count ;
635 blk_allocated = count;
636 if (!target)
637 goto allocated;
638 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500639 memset(&ar, 0, sizeof(ar));
640 ar.inode = inode;
641 ar.goal = goal;
642 ar.len = target;
643 ar.logical = iblock;
644 if (S_ISREG(inode->i_mode))
645 /* enable in-core preallocation only for regular files */
646 ar.flags = EXT4_MB_HINT_DATA;
647
648 current_block = ext4_mb_new_blocks(handle, &ar, err);
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400649 BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS);
Theodore Ts'o815a1132009-01-01 23:59:43 -0500650
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400651 if (*err && (target == blks)) {
652 /*
653 * if the allocation failed and we didn't allocate
654 * any blocks before
655 */
656 goto failed_out;
657 }
658 if (!*err) {
659 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400660 /*
661 * save the new block number
662 * for the first direct block
663 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400664 new_blocks[index] = current_block;
665 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500666 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400667 }
668allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400670 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 *err = 0;
672 return ret;
673failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400674 for (i = 0; i < index; i++)
Theodore Ts'oe6362602009-11-23 07:17:05 -0500675 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 return ret;
677}
678
679/**
Mingming Cao617ba132006-10-11 01:20:53 -0700680 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 * @inode: owner
682 * @indirect_blks: number of allocated indirect blocks
683 * @blks: number of allocated direct blocks
684 * @offsets: offsets (in the blocks) to store the pointers to next.
685 * @branch: place to store the chain in.
686 *
687 * This function allocates blocks, zeroes out all but the last one,
688 * links them into chain and (if we are synchronous) writes them to disk.
689 * In other words, it prepares a branch that can be spliced onto the
690 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700691 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 * we had read the existing part of chain and partial points to the last
693 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700694 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695 * place chain is disconnected - *branch->p is still zero (we did not
696 * set the last link), but branch->key contains the number that should
697 * be placed into *branch->p to fill that gap.
698 *
699 * If allocation fails we free all blocks we've allocated (and forget
700 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700701 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702 * as described above and return 0.
703 */
Mingming Cao617ba132006-10-11 01:20:53 -0700704static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400705 ext4_lblk_t iblock, int indirect_blks,
706 int *blks, ext4_fsblk_t goal,
707 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708{
709 int blocksize = inode->i_sb->s_blocksize;
710 int i, n = 0;
711 int err = 0;
712 struct buffer_head *bh;
713 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700714 ext4_fsblk_t new_blocks[4];
715 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400717 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 *blks, new_blocks, &err);
719 if (err)
720 return err;
721
722 branch[0].key = cpu_to_le32(new_blocks[0]);
723 /*
724 * metadata blocks and data blocks are allocated.
725 */
726 for (n = 1; n <= indirect_blks; n++) {
727 /*
728 * Get buffer_head for parent block, zero it out
729 * and set the pointer to new one, then send
730 * parent to disk.
731 */
732 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
733 branch[n].bh = bh;
734 lock_buffer(bh);
735 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700736 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400738 /* Don't brelse(bh) here; it's done in
739 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 goto failed;
742 }
743
744 memset(bh->b_data, 0, blocksize);
745 branch[n].p = (__le32 *) bh->b_data + offsets[n];
746 branch[n].key = cpu_to_le32(new_blocks[n]);
747 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400748 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 current_block = new_blocks[n];
750 /*
751 * End of chain, update the last new metablock of
752 * the chain to point to the new allocated
753 * data blocks numbers
754 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400755 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700756 *(branch[n].p + i) = cpu_to_le32(++current_block);
757 }
758 BUFFER_TRACE(bh, "marking uptodate");
759 set_buffer_uptodate(bh);
760 unlock_buffer(bh);
761
Frank Mayhar03901312009-01-07 00:06:22 -0500762 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
763 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764 if (err)
765 goto failed;
766 }
767 *blks = num;
768 return err;
769failed:
770 /* Allocation failed, free what we already allocated */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500771 ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 for (i = 1; i <= n ; i++) {
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500773 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500774 * branch[i].bh is newly allocated, so there is no
775 * need to revoke the block, which is why we don't
776 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500777 */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500778 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1,
779 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 }
Theodore Ts'oe6362602009-11-23 07:17:05 -0500781 for (i = n+1; i < indirect_blks; i++)
782 ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783
Theodore Ts'oe6362602009-11-23 07:17:05 -0500784 ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785
786 return err;
787}
788
789/**
Mingming Cao617ba132006-10-11 01:20:53 -0700790 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 * @inode: owner
792 * @block: (logical) number of block we are adding
793 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700794 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 * @where: location of missing link
796 * @num: number of indirect blocks we are adding
797 * @blks: number of direct blocks we are adding
798 *
799 * This function fills the missing link and does all housekeeping needed in
800 * inode (->i_blocks, etc.). In case of success we end up with the full
801 * chain to new block and return 0.
802 */
Mingming Cao617ba132006-10-11 01:20:53 -0700803static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400804 ext4_lblk_t block, Indirect *where, int num,
805 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806{
807 int i;
808 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700809 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 /*
812 * If we're splicing into a [td]indirect block (as opposed to the
813 * inode) then we need to get write access to the [td]indirect block
814 * before the splice.
815 */
816 if (where->bh) {
817 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700818 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 if (err)
820 goto err_out;
821 }
822 /* That's it */
823
824 *where->p = where->key;
825
826 /*
827 * Update the host buffer_head or inode to point to more just allocated
828 * direct blocks blocks
829 */
830 if (num == 0 && blks > 1) {
831 current_block = le32_to_cpu(where->key) + 1;
832 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400833 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834 }
835
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 /* had we spliced it onto indirect block? */
838 if (where->bh) {
839 /*
840 * If we spliced it onto an indirect block, we haven't
841 * altered the inode. Note however that if it is being spliced
842 * onto an indirect block at the very end of the file (the
843 * file is growing) then we *will* alter the inode to reflect
844 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700845 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 */
847 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500848 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
849 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 if (err)
851 goto err_out;
852 } else {
853 /*
854 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400856 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 jbd_debug(5, "splicing direct\n");
858 }
859 return err;
860
861err_out:
862 for (i = 1; i <= num; i++) {
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500863 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500864 * branch[i].bh is newly allocated, so there is no
865 * need to revoke the block, which is why we don't
866 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500867 */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500868 ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
869 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 }
Theodore Ts'oe6362602009-11-23 07:17:05 -0500871 ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key),
872 blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873
874 return err;
875}
876
877/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400878 * The ext4_ind_get_blocks() function handles non-extents inodes
879 * (i.e., using the traditional indirect/double-indirect i_blocks
880 * scheme) for ext4_get_blocks().
881 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 * Allocation strategy is simple: if we have to allocate something, we will
883 * have to go the whole way to leaf. So let's do it before attaching anything
884 * to tree, set linkage between the newborn blocks, write them if sync is
885 * required, recheck the path, free and repeat if check fails, otherwise
886 * set the last missing link (that will protect us from any truncate-generated
887 * removals - all blocks on the path are immune now) and possibly force the
888 * write on the parent block.
889 * That has a nice additional property: no special recovery from the failed
890 * allocations is needed - we simply release blocks and do not touch anything
891 * reachable from inode.
892 *
893 * `handle' can be NULL if create == 0.
894 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 * return > 0, # of blocks mapped or allocated.
896 * return = 0, if plain lookup failed.
897 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500898 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400899 * The ext4_ind_get_blocks() function should be called with
900 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
901 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
902 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
903 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700904 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400905static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400906 ext4_lblk_t iblock, unsigned int maxblocks,
907 struct buffer_head *bh_result,
908 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909{
910 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500911 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 Indirect chain[4];
913 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700914 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 int indirect_blks;
916 int blocks_to_boundary = 0;
917 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700919 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920
Alex Tomasa86c6182006-10-11 01:21:03 -0700921 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400922 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500923 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400924 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925
926 if (depth == 0)
927 goto out;
928
Mingming Cao617ba132006-10-11 01:20:53 -0700929 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930
931 /* Simplest case - block found, no allocation needed */
932 if (!partial) {
933 first_block = le32_to_cpu(chain[depth - 1].key);
934 clear_buffer_new(bh_result);
935 count++;
936 /*map more blocks*/
937 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700938 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700939
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940 blk = le32_to_cpu(*(chain[depth-1].p + count));
941
942 if (blk == first_block + count)
943 count++;
944 else
945 break;
946 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500947 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 }
949
950 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400951 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 goto cleanup;
953
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400955 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800957 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958
959 /* the number of blocks need to allocate for [d,t]indirect blocks */
960 indirect_blks = (chain + depth) - partial - 1;
961
962 /*
963 * Next look up the indirect map to count the totoal number of
964 * direct blocks to allocate for this branch.
965 */
Mingming Cao617ba132006-10-11 01:20:53 -0700966 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 maxblocks, blocks_to_boundary);
968 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700969 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400971 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400972 &count, goal,
973 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974
975 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700976 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 * on the new chain if there is a failure, but that risks using
978 * up transaction credits, especially for bitmaps where the
979 * credits cannot be returned. Can we handle this somehow? We
980 * may need to return -EAGAIN upwards in the worst case. --sct
981 */
982 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -0700983 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400984 partial, indirect_blks, count);
Jan Kara2bba7022009-11-23 07:24:48 -0500985 if (err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 goto cleanup;
987
988 set_buffer_new(bh_result);
Jan Karab436b9b2009-12-08 23:51:10 -0500989
990 ext4_update_inode_fsync_trans(handle, inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991got_it:
992 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
993 if (count > blocks_to_boundary)
994 set_buffer_boundary(bh_result);
995 err = count;
996 /* Clean up and exit */
997 partial = chain + depth - 1; /* the whole chain */
998cleanup:
999 while (partial > chain) {
1000 BUFFER_TRACE(partial->bh, "call brelse");
1001 brelse(partial->bh);
1002 partial--;
1003 }
1004 BUFFER_TRACE(bh_result, "returned");
1005out:
1006 return err;
1007}
1008
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001009#ifdef CONFIG_QUOTA
1010qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +01001011{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001012 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +01001013}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001014#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001015
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001016/*
1017 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001018 * to allocate a new block at @lblocks for non extent file based file
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001019 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001020static int ext4_indirect_calc_metadata_amount(struct inode *inode,
1021 sector_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001022{
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001023 struct ext4_inode_info *ei = EXT4_I(inode);
1024 int dind_mask = EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1;
1025 int blk_bits;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001026
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001027 if (lblock < EXT4_NDIR_BLOCKS)
1028 return 0;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001029
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001030 lblock -= EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001031
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001032 if (ei->i_da_metadata_calc_len &&
1033 (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
1034 ei->i_da_metadata_calc_len++;
1035 return 0;
1036 }
1037 ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
1038 ei->i_da_metadata_calc_len = 1;
1039 blk_bits = roundup_pow_of_two(lblock + 1);
1040 return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001041}
1042
1043/*
1044 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001045 * to allocate a block located at @lblock
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001046 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001047static int ext4_calc_metadata_amount(struct inode *inode, sector_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001048{
1049 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001050 return ext4_ext_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001051
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001052 return ext4_indirect_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001053}
1054
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001055/*
1056 * Called with i_data_sem down, which is important since we can call
1057 * ext4_discard_preallocations() from here.
1058 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001059void ext4_da_update_reserve_space(struct inode *inode,
1060 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001061{
1062 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001063 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001064 int mdb_free = 0, allocated_meta_blocks = 0;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001065
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001066 spin_lock(&ei->i_block_reservation_lock);
1067 if (unlikely(used > ei->i_reserved_data_blocks)) {
1068 ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
1069 "with only %d reserved data blocks\n",
1070 __func__, inode->i_ino, used,
1071 ei->i_reserved_data_blocks);
1072 WARN_ON(1);
1073 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001074 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001075
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001076 /* Update per-inode reservations */
1077 ei->i_reserved_data_blocks -= used;
1078 used += ei->i_allocated_meta_blocks;
1079 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001080 allocated_meta_blocks = ei->i_allocated_meta_blocks;
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001081 ei->i_allocated_meta_blocks = 0;
1082 percpu_counter_sub(&sbi->s_dirtyblocks_counter, used);
1083
1084 if (ei->i_reserved_data_blocks == 0) {
1085 /*
1086 * We can release all of the reserved metadata blocks
1087 * only when we have written all of the delayed
1088 * allocation blocks.
1089 */
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001090 mdb_free = ei->i_reserved_meta_blocks;
1091 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001092 ei->i_da_metadata_calc_len = 0;
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001093 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001094 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001095 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001096
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001097 /* Update quota subsystem */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001098 if (quota_claim) {
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001099 dquot_claim_block(inode, used);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001100 if (mdb_free)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001101 dquot_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001102 } else {
1103 /*
1104 * We did fallocate with an offset that is already delayed
1105 * allocated. So on delayed allocated writeback we should
1106 * not update the quota for allocated blocks. But then
1107 * converting an fallocate region to initialized region would
1108 * have caused a metadata allocation. So claim quota for
1109 * that
1110 */
1111 if (allocated_meta_blocks)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001112 dquot_claim_block(inode, allocated_meta_blocks);
1113 dquot_release_reservation_block(inode, mdb_free + used);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001114 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001115
1116 /*
1117 * If we have done all the pending block allocations and if
1118 * there aren't any writers on the inode, we can discard the
1119 * inode's preallocations.
1120 */
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001121 if ((ei->i_reserved_data_blocks == 0) &&
1122 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001123 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001124}
1125
Theodore Ts'o80e42462009-09-08 08:21:26 -04001126static int check_block_validity(struct inode *inode, const char *msg,
1127 sector_t logical, sector_t phys, int len)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001128{
1129 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
Theodore Ts'o80e42462009-09-08 08:21:26 -04001130 ext4_error(inode->i_sb, msg,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001131 "inode #%lu logical block %llu mapped to %llu "
1132 "(size %d)", inode->i_ino,
1133 (unsigned long long) logical,
1134 (unsigned long long) phys, len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001135 return -EIO;
1136 }
1137 return 0;
1138}
1139
Mingming Caof5ab0d12008-02-25 15:29:55 -05001140/*
Theodore Ts'o1f945332009-09-30 22:57:41 -04001141 * Return the number of contiguous dirty pages in a given inode
1142 * starting at page frame idx.
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04001143 */
1144static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
1145 unsigned int max_pages)
1146{
1147 struct address_space *mapping = inode->i_mapping;
1148 pgoff_t index;
1149 struct pagevec pvec;
1150 pgoff_t num = 0;
1151 int i, nr_pages, done = 0;
1152
1153 if (max_pages == 0)
1154 return 0;
1155 pagevec_init(&pvec, 0);
1156 while (!done) {
1157 index = idx;
1158 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1159 PAGECACHE_TAG_DIRTY,
1160 (pgoff_t)PAGEVEC_SIZE);
1161 if (nr_pages == 0)
1162 break;
1163 for (i = 0; i < nr_pages; i++) {
1164 struct page *page = pvec.pages[i];
1165 struct buffer_head *bh, *head;
1166
1167 lock_page(page);
1168 if (unlikely(page->mapping != mapping) ||
1169 !PageDirty(page) ||
1170 PageWriteback(page) ||
1171 page->index != idx) {
1172 done = 1;
1173 unlock_page(page);
1174 break;
1175 }
Theodore Ts'o1f945332009-09-30 22:57:41 -04001176 if (page_has_buffers(page)) {
1177 bh = head = page_buffers(page);
1178 do {
1179 if (!buffer_delay(bh) &&
1180 !buffer_unwritten(bh))
1181 done = 1;
1182 bh = bh->b_this_page;
1183 } while (!done && (bh != head));
1184 }
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04001185 unlock_page(page);
1186 if (done)
1187 break;
1188 idx++;
1189 num++;
1190 if (num >= max_pages)
1191 break;
1192 }
1193 pagevec_release(&pvec);
1194 }
1195 return num;
1196}
1197
1198/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001199 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001200 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001201 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001202 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1203 * and store the allocated blocks in the result buffer head and mark it
1204 * mapped.
1205 *
1206 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001207 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001208 * based files
1209 *
1210 * On success, it returns the number of blocks being mapped or allocate.
1211 * if create==0 and the blocks are pre-allocated and uninitialized block,
1212 * the result buffer head is unmapped. If the create ==1, it will make sure
1213 * the buffer head is mapped.
1214 *
1215 * It returns 0 if plain look up failed (blocks have not been allocated), in
1216 * that casem, buffer head is unmapped
1217 *
1218 * It returns the error in case of allocation failure.
1219 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001220int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1221 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001222 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001223{
1224 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001225
1226 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001227 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001228
Mingming Cao00314622009-09-28 15:49:08 -04001229 ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u,"
1230 "logical block %lu\n", inode->i_ino, flags, max_blocks,
1231 (unsigned long)block);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001232 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001233 * Try to see if we can get the block without requesting a new
1234 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001235 */
1236 down_read((&EXT4_I(inode)->i_data_sem));
1237 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1238 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001239 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001240 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001241 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001242 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001243 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001244 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001245
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001246 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'o80e42462009-09-08 08:21:26 -04001247 int ret = check_block_validity(inode, "file system corruption",
1248 block, bh->b_blocknr, retval);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001249 if (ret != 0)
1250 return ret;
1251 }
1252
Mingming Caof5ab0d12008-02-25 15:29:55 -05001253 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001254 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001255 return retval;
1256
1257 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001258 * Returns if the blocks have already allocated
1259 *
1260 * Note that if blocks have been preallocated
1261 * ext4_ext_get_block() returns th create = 0
1262 * with buffer head unmapped.
1263 */
1264 if (retval > 0 && buffer_mapped(bh))
1265 return retval;
1266
1267 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001268 * When we call get_blocks without the create flag, the
1269 * BH_Unwritten flag could have gotten set if the blocks
1270 * requested were part of a uninitialized extent. We need to
1271 * clear this flag now that we are committed to convert all or
1272 * part of the uninitialized extent to be an initialized
1273 * extent. This is because we need to avoid the combination
1274 * of BH_Unwritten and BH_Mapped flags being simultaneously
1275 * set on the buffer_head.
1276 */
1277 clear_buffer_unwritten(bh);
1278
1279 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001280 * New blocks allocate and/or writing to uninitialized extent
1281 * will possibly result in updating i_data, so we take
1282 * the write lock of i_data_sem, and call get_blocks()
1283 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001284 */
1285 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001286
1287 /*
1288 * if the caller is from delayed allocation writeout path
1289 * we have already reserved fs blocks for allocation
1290 * let the underlying get_block() function know to
1291 * avoid double accounting
1292 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001293 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001294 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001295 /*
1296 * We need to check for EXT4 here because migrate
1297 * could have changed the inode type in between
1298 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001299 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1300 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001301 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001302 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001303 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001304 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001305
1306 if (retval > 0 && buffer_new(bh)) {
1307 /*
1308 * We allocated new blocks which will result in
1309 * i_data's format changing. Force the migrate
1310 * to fail by clearing migrate flags
1311 */
Theodore Ts'o1b9c12f2009-09-17 08:32:22 -04001312 EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE;
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001313 }
Mingming Caod2a17632008-07-14 17:52:37 -04001314
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001315 /*
1316 * Update reserved blocks/metadata blocks after successful
1317 * block allocation which had been deferred till now. We don't
1318 * support fallocate for non extent files. So we can update
1319 * reserve space here.
1320 */
1321 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001322 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001323 ext4_da_update_reserve_space(inode, retval, 1);
1324 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001325 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001326 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001327
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001328 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001329 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'o80e42462009-09-08 08:21:26 -04001330 int ret = check_block_validity(inode, "file system "
1331 "corruption after allocation",
1332 block, bh->b_blocknr, retval);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001333 if (ret != 0)
1334 return ret;
1335 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001336 return retval;
1337}
1338
Mingming Caof3bd1f32008-08-19 22:16:03 -04001339/* Maximum number of blocks we map for direct IO at once. */
1340#define DIO_MAX_BLOCKS 4096
1341
Eric Sandeen6873fa02008-10-07 00:46:36 -04001342int ext4_get_block(struct inode *inode, sector_t iblock,
1343 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001345 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001346 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001348 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349
Jan Kara7fb54092008-02-10 01:08:38 -05001350 if (create && !handle) {
1351 /* Direct IO write... */
1352 if (max_blocks > DIO_MAX_BLOCKS)
1353 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001354 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1355 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001356 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001357 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001358 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001359 }
Jan Kara7fb54092008-02-10 01:08:38 -05001360 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 }
1362
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001363 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001364 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001365 if (ret > 0) {
1366 bh_result->b_size = (ret << inode->i_blkbits);
1367 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 }
Jan Kara7fb54092008-02-10 01:08:38 -05001369 if (started)
1370 ext4_journal_stop(handle);
1371out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001372 return ret;
1373}
1374
1375/*
1376 * `handle' can be NULL if create is zero
1377 */
Mingming Cao617ba132006-10-11 01:20:53 -07001378struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001379 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001380{
1381 struct buffer_head dummy;
1382 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001383 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001384
1385 J_ASSERT(handle != NULL || create == 0);
1386
1387 dummy.b_state = 0;
1388 dummy.b_blocknr = -1000;
1389 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001390 if (create)
1391 flags |= EXT4_GET_BLOCKS_CREATE;
1392 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001394 * ext4_get_blocks() returns number of blocks mapped. 0 in
1395 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396 */
1397 if (err > 0) {
1398 if (err > 1)
1399 WARN_ON(1);
1400 err = 0;
1401 }
1402 *errp = err;
1403 if (!err && buffer_mapped(&dummy)) {
1404 struct buffer_head *bh;
1405 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1406 if (!bh) {
1407 *errp = -EIO;
1408 goto err;
1409 }
1410 if (buffer_new(&dummy)) {
1411 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001412 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413
1414 /*
1415 * Now that we do not always journal data, we should
1416 * keep in mind whether this should always journal the
1417 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001418 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419 * problem.
1420 */
1421 lock_buffer(bh);
1422 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001423 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001425 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001426 set_buffer_uptodate(bh);
1427 }
1428 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001429 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1430 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 if (!fatal)
1432 fatal = err;
1433 } else {
1434 BUFFER_TRACE(bh, "not a new buffer");
1435 }
1436 if (fatal) {
1437 *errp = fatal;
1438 brelse(bh);
1439 bh = NULL;
1440 }
1441 return bh;
1442 }
1443err:
1444 return NULL;
1445}
1446
Mingming Cao617ba132006-10-11 01:20:53 -07001447struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001448 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001450 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451
Mingming Cao617ba132006-10-11 01:20:53 -07001452 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 if (!bh)
1454 return bh;
1455 if (buffer_uptodate(bh))
1456 return bh;
1457 ll_rw_block(READ_META, 1, &bh);
1458 wait_on_buffer(bh);
1459 if (buffer_uptodate(bh))
1460 return bh;
1461 put_bh(bh);
1462 *err = -EIO;
1463 return NULL;
1464}
1465
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001466static int walk_page_buffers(handle_t *handle,
1467 struct buffer_head *head,
1468 unsigned from,
1469 unsigned to,
1470 int *partial,
1471 int (*fn)(handle_t *handle,
1472 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473{
1474 struct buffer_head *bh;
1475 unsigned block_start, block_end;
1476 unsigned blocksize = head->b_size;
1477 int err, ret = 0;
1478 struct buffer_head *next;
1479
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001480 for (bh = head, block_start = 0;
1481 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001482 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 next = bh->b_this_page;
1484 block_end = block_start + blocksize;
1485 if (block_end <= from || block_start >= to) {
1486 if (partial && !buffer_uptodate(bh))
1487 *partial = 1;
1488 continue;
1489 }
1490 err = (*fn)(handle, bh);
1491 if (!ret)
1492 ret = err;
1493 }
1494 return ret;
1495}
1496
1497/*
1498 * To preserve ordering, it is essential that the hole instantiation and
1499 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001500 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001501 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001502 * prepare_write() is the right place.
1503 *
Mingming Cao617ba132006-10-11 01:20:53 -07001504 * Also, this function can nest inside ext4_writepage() ->
1505 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001506 * has generated enough buffer credits to do the whole page. So we won't
1507 * block on the journal in that case, which is good, because the caller may
1508 * be PF_MEMALLOC.
1509 *
Mingming Cao617ba132006-10-11 01:20:53 -07001510 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 * quota file writes. If we were to commit the transaction while thus
1512 * reentered, there can be a deadlock - we would be holding a quota
1513 * lock, and the commit would never complete if another thread had a
1514 * transaction open and was blocking on the quota lock - a ranking
1515 * violation.
1516 *
Mingming Caodab291a2006-10-11 01:21:01 -07001517 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 * will _not_ run commit under these circumstances because handle->h_ref
1519 * is elevated. We'll still have enough credits for the tiny quotafile
1520 * write.
1521 */
1522static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001523 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524{
1525 if (!buffer_mapped(bh) || buffer_freed(bh))
1526 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001527 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528}
1529
Jan Karab9a42072009-12-08 21:24:33 -05001530/*
1531 * Truncate blocks that were not used by write. We have to truncate the
1532 * pagecache as well so that corresponding buffers get properly unmapped.
1533 */
1534static void ext4_truncate_failed_write(struct inode *inode)
1535{
1536 truncate_inode_pages(inode->i_mapping, inode->i_size);
1537 ext4_truncate(inode);
1538}
1539
Nick Pigginbfc1af62007-10-16 01:25:05 -07001540static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001541 loff_t pos, unsigned len, unsigned flags,
1542 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001544 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001545 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546 handle_t *handle;
1547 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001548 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001549 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001550 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001551
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001552 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001553 /*
1554 * Reserve one block more for addition to orphan list in case
1555 * we allocate blocks but write fails for some reason
1556 */
1557 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001558 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001559 from = pos & (PAGE_CACHE_SIZE - 1);
1560 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001561
1562retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001563 handle = ext4_journal_start(inode, needed_blocks);
1564 if (IS_ERR(handle)) {
1565 ret = PTR_ERR(handle);
1566 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001567 }
1568
Jan Karaebd36102009-02-22 21:09:59 -05001569 /* We cannot recurse into the filesystem as the transaction is already
1570 * started */
1571 flags |= AOP_FLAG_NOFS;
1572
Nick Piggin54566b22009-01-04 12:00:53 -08001573 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001574 if (!page) {
1575 ext4_journal_stop(handle);
1576 ret = -ENOMEM;
1577 goto out;
1578 }
1579 *pagep = page;
1580
Nick Pigginbfc1af62007-10-16 01:25:05 -07001581 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001582 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001583
1584 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 ret = walk_page_buffers(handle, page_buffers(page),
1586 from, to, NULL, do_journal_get_write_access);
1587 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001588
1589 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001590 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001591 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001592 /*
1593 * block_write_begin may have instantiated a few blocks
1594 * outside i_size. Trim these off again. Don't need
1595 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001596 *
1597 * Add inode to orphan list in case we crash before
1598 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001599 */
Jan Karaffacfa72009-07-13 16:22:22 -04001600 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001601 ext4_orphan_add(handle, inode);
1602
1603 ext4_journal_stop(handle);
1604 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001605 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001606 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001607 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001608 * still be on the orphan list; we need to
1609 * make sure the inode is removed from the
1610 * orphan list in that case.
1611 */
1612 if (inode->i_nlink)
1613 ext4_orphan_del(NULL, inode);
1614 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001615 }
1616
Mingming Cao617ba132006-10-11 01:20:53 -07001617 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001619out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001620 return ret;
1621}
1622
Nick Pigginbfc1af62007-10-16 01:25:05 -07001623/* For write_end() in data=journal mode */
1624static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625{
1626 if (!buffer_mapped(bh) || buffer_freed(bh))
1627 return 0;
1628 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001629 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630}
1631
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001632static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001633 struct address_space *mapping,
1634 loff_t pos, unsigned len, unsigned copied,
1635 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001636{
1637 int i_size_changed = 0;
1638 struct inode *inode = mapping->host;
1639 handle_t *handle = ext4_journal_current_handle();
1640
1641 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1642
1643 /*
1644 * No need to use i_size_read() here, the i_size
1645 * cannot change under us because we hold i_mutex.
1646 *
1647 * But it's important to update i_size while still holding page lock:
1648 * page writeout could otherwise come in and zero beyond i_size.
1649 */
1650 if (pos + copied > inode->i_size) {
1651 i_size_write(inode, pos + copied);
1652 i_size_changed = 1;
1653 }
1654
1655 if (pos + copied > EXT4_I(inode)->i_disksize) {
1656 /* We need to mark inode dirty even if
1657 * new_i_size is less that inode->i_size
1658 * bu greater than i_disksize.(hint delalloc)
1659 */
1660 ext4_update_i_disksize(inode, (pos + copied));
1661 i_size_changed = 1;
1662 }
1663 unlock_page(page);
1664 page_cache_release(page);
1665
1666 /*
1667 * Don't mark the inode dirty under page lock. First, it unnecessarily
1668 * makes the holding time of page lock longer. Second, it forces lock
1669 * ordering of page lock and transaction start for journaling
1670 * filesystems.
1671 */
1672 if (i_size_changed)
1673 ext4_mark_inode_dirty(handle, inode);
1674
1675 return copied;
1676}
1677
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001678/*
1679 * We need to pick up the new inode size which generic_commit_write gave us
1680 * `file' can be NULL - eg, when called from page_symlink().
1681 *
Mingming Cao617ba132006-10-11 01:20:53 -07001682 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 * buffers are managed internally.
1684 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001685static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001686 struct address_space *mapping,
1687 loff_t pos, unsigned len, unsigned copied,
1688 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689{
Mingming Cao617ba132006-10-11 01:20:53 -07001690 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001691 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001692 int ret = 0, ret2;
1693
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001694 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001695 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696
1697 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001698 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001699 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001700 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001701 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001702 /* if we have allocated more blocks and copied
1703 * less. We will have blocks allocated outside
1704 * inode->i_size. So truncate them
1705 */
1706 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001707 if (ret2 < 0)
1708 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 }
Mingming Cao617ba132006-10-11 01:20:53 -07001710 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711 if (!ret)
1712 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001713
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001714 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001715 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001716 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001717 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001718 * on the orphan list; we need to make sure the inode
1719 * is removed from the orphan list in that case.
1720 */
1721 if (inode->i_nlink)
1722 ext4_orphan_del(NULL, inode);
1723 }
1724
1725
Nick Pigginbfc1af62007-10-16 01:25:05 -07001726 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727}
1728
Nick Pigginbfc1af62007-10-16 01:25:05 -07001729static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001730 struct address_space *mapping,
1731 loff_t pos, unsigned len, unsigned copied,
1732 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733{
Mingming Cao617ba132006-10-11 01:20:53 -07001734 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001735 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001738 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001739 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001740 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001741 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001742 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001743 /* if we have allocated more blocks and copied
1744 * less. We will have blocks allocated outside
1745 * inode->i_size. So truncate them
1746 */
1747 ext4_orphan_add(handle, inode);
1748
Roel Kluinf8a87d82008-04-29 22:01:18 -04001749 if (ret2 < 0)
1750 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751
Mingming Cao617ba132006-10-11 01:20:53 -07001752 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001753 if (!ret)
1754 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001755
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001756 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001757 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001758 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001759 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001760 * on the orphan list; we need to make sure the inode
1761 * is removed from the orphan list in that case.
1762 */
1763 if (inode->i_nlink)
1764 ext4_orphan_del(NULL, inode);
1765 }
1766
Nick Pigginbfc1af62007-10-16 01:25:05 -07001767 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768}
1769
Nick Pigginbfc1af62007-10-16 01:25:05 -07001770static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001771 struct address_space *mapping,
1772 loff_t pos, unsigned len, unsigned copied,
1773 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774{
Mingming Cao617ba132006-10-11 01:20:53 -07001775 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001776 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777 int ret = 0, ret2;
1778 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001779 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001780 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001782 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001783 from = pos & (PAGE_CACHE_SIZE - 1);
1784 to = from + len;
1785
1786 if (copied < len) {
1787 if (!PageUptodate(page))
1788 copied = 0;
1789 page_zero_new_buffers(page, from+copied, to);
1790 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791
1792 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001793 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001794 if (!partial)
1795 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001796 new_i_size = pos + copied;
1797 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001798 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001799 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001800 if (new_i_size > EXT4_I(inode)->i_disksize) {
1801 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001802 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803 if (!ret)
1804 ret = ret2;
1805 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001806
Jan Karacf108bc2008-07-11 19:27:31 -04001807 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001808 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001809 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001810 /* if we have allocated more blocks and copied
1811 * less. We will have blocks allocated outside
1812 * inode->i_size. So truncate them
1813 */
1814 ext4_orphan_add(handle, inode);
1815
Mingming Cao617ba132006-10-11 01:20:53 -07001816 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 if (!ret)
1818 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001819 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001820 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001821 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001822 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001823 * on the orphan list; we need to make sure the inode
1824 * is removed from the orphan list in that case.
1825 */
1826 if (inode->i_nlink)
1827 ext4_orphan_del(NULL, inode);
1828 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001829
1830 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001831}
Mingming Caod2a17632008-07-14 17:52:37 -04001832
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001833/*
1834 * Reserve a single block located at lblock
1835 */
1836static int ext4_da_reserve_space(struct inode *inode, sector_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001837{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001838 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001839 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001840 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001841 unsigned long md_needed, md_reserved;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001842 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001843
1844 /*
1845 * recalculate the amount of metadata blocks to reserve
1846 * in order to allocate nrblocks
1847 * worse case is one extent per block
1848 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001849repeat:
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001850 spin_lock(&ei->i_block_reservation_lock);
1851 md_reserved = ei->i_reserved_meta_blocks;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001852 md_needed = ext4_calc_metadata_amount(inode, lblock);
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001853 spin_unlock(&ei->i_block_reservation_lock);
Mingming Caod2a17632008-07-14 17:52:37 -04001854
Mingming Cao60e58e02009-01-22 18:13:05 +01001855 /*
1856 * Make quota reservation here to prevent quota overflow
1857 * later. Real quota accounting is done at pages writeout
1858 * time.
1859 */
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001860 ret = dquot_reserve_block(inode, md_needed + 1);
1861 if (ret)
1862 return ret;
Mingming Cao60e58e02009-01-22 18:13:05 +01001863
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001864 if (ext4_claim_free_blocks(sbi, md_needed + 1)) {
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001865 dquot_release_reservation_block(inode, md_needed + 1);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001866 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1867 yield();
1868 goto repeat;
1869 }
Mingming Caod2a17632008-07-14 17:52:37 -04001870 return -ENOSPC;
1871 }
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001872 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001873 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001874 ei->i_reserved_meta_blocks += md_needed;
1875 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001876
Mingming Caod2a17632008-07-14 17:52:37 -04001877 return 0; /* success */
1878}
1879
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001880static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001881{
1882 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001883 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001884
Mingming Caocd213222008-08-19 22:16:59 -04001885 if (!to_free)
1886 return; /* Nothing to release, exit */
1887
Mingming Caod2a17632008-07-14 17:52:37 -04001888 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001889
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001890 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001891 /*
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001892 * if there aren't enough reserved blocks, then the
1893 * counter is messed up somewhere. Since this
1894 * function is called from invalidate page, it's
1895 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001896 */
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001897 ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
1898 "ino %lu, to_free %d with only %d reserved "
1899 "data blocks\n", inode->i_ino, to_free,
1900 ei->i_reserved_data_blocks);
1901 WARN_ON(1);
1902 to_free = ei->i_reserved_data_blocks;
1903 }
1904 ei->i_reserved_data_blocks -= to_free;
1905
1906 if (ei->i_reserved_data_blocks == 0) {
1907 /*
1908 * We can release all of the reserved metadata blocks
1909 * only when we have written all of the delayed
1910 * allocation blocks.
1911 */
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001912 to_free += ei->i_reserved_meta_blocks;
1913 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001914 ei->i_da_metadata_calc_len = 0;
Mingming Caocd213222008-08-19 22:16:59 -04001915 }
1916
Theodore Ts'o0637c6f42009-12-30 14:20:45 -05001917 /* update fs dirty blocks counter */
1918 percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001919
Mingming Caod2a17632008-07-14 17:52:37 -04001920 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001921
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001922 dquot_release_reservation_block(inode, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001923}
1924
1925static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001926 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001927{
1928 int to_release = 0;
1929 struct buffer_head *head, *bh;
1930 unsigned int curr_off = 0;
1931
1932 head = page_buffers(page);
1933 bh = head;
1934 do {
1935 unsigned int next_off = curr_off + bh->b_size;
1936
1937 if ((offset <= curr_off) && (buffer_delay(bh))) {
1938 to_release++;
1939 clear_buffer_delay(bh);
1940 }
1941 curr_off = next_off;
1942 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001943 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001944}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001945
1946/*
Alex Tomas64769242008-07-11 19:27:31 -04001947 * Delayed allocation stuff
1948 */
1949
Alex Tomas64769242008-07-11 19:27:31 -04001950/*
1951 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001952 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001953 *
1954 * @mpd->inode: inode
1955 * @mpd->first_page: first page of the extent
1956 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001957 *
1958 * By the time mpage_da_submit_io() is called we expect all blocks
1959 * to be allocated. this may be wrong if allocation failed.
1960 *
1961 * As pages are already locked by write_cache_pages(), we can't use it
1962 */
1963static int mpage_da_submit_io(struct mpage_da_data *mpd)
1964{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001965 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001966 struct pagevec pvec;
1967 unsigned long index, end;
1968 int ret = 0, err, nr_pages, i;
1969 struct inode *inode = mpd->inode;
1970 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001971
1972 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001973 /*
1974 * We need to start from the first_page to the next_page - 1
1975 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001976 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001977 * at the currently mapped buffer_heads.
1978 */
Alex Tomas64769242008-07-11 19:27:31 -04001979 index = mpd->first_page;
1980 end = mpd->next_page - 1;
1981
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001982 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001983 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001984 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001985 if (nr_pages == 0)
1986 break;
1987 for (i = 0; i < nr_pages; i++) {
1988 struct page *page = pvec.pages[i];
1989
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001990 index = page->index;
1991 if (index > end)
1992 break;
1993 index++;
1994
1995 BUG_ON(!PageLocked(page));
1996 BUG_ON(PageWriteback(page));
1997
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001998 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001999 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002000 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
2001 /*
2002 * have successfully written the page
2003 * without skipping the same
2004 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002005 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04002006 /*
2007 * In error case, we have to continue because
2008 * remaining pages are still locked
2009 * XXX: unlock and re-dirty them?
2010 */
2011 if (ret == 0)
2012 ret = err;
2013 }
2014 pagevec_release(&pvec);
2015 }
Alex Tomas64769242008-07-11 19:27:31 -04002016 return ret;
2017}
2018
2019/*
2020 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
2021 *
2022 * @mpd->inode - inode to walk through
2023 * @exbh->b_blocknr - first block on a disk
2024 * @exbh->b_size - amount of space in bytes
2025 * @logical - first logical block to start assignment with
2026 *
2027 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002028 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04002029 */
2030static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
2031 struct buffer_head *exbh)
2032{
2033 struct inode *inode = mpd->inode;
2034 struct address_space *mapping = inode->i_mapping;
2035 int blocks = exbh->b_size >> inode->i_blkbits;
2036 sector_t pblock = exbh->b_blocknr, cur_logical;
2037 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002038 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04002039 struct pagevec pvec;
2040 int nr_pages, i;
2041
2042 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2043 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2044 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2045
2046 pagevec_init(&pvec, 0);
2047
2048 while (index <= end) {
2049 /* XXX: optimize tail */
2050 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2051 if (nr_pages == 0)
2052 break;
2053 for (i = 0; i < nr_pages; i++) {
2054 struct page *page = pvec.pages[i];
2055
2056 index = page->index;
2057 if (index > end)
2058 break;
2059 index++;
2060
2061 BUG_ON(!PageLocked(page));
2062 BUG_ON(PageWriteback(page));
2063 BUG_ON(!page_has_buffers(page));
2064
2065 bh = page_buffers(page);
2066 head = bh;
2067
2068 /* skip blocks out of the range */
2069 do {
2070 if (cur_logical >= logical)
2071 break;
2072 cur_logical++;
2073 } while ((bh = bh->b_this_page) != head);
2074
2075 do {
2076 if (cur_logical >= logical + blocks)
2077 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002078
2079 if (buffer_delay(bh) ||
2080 buffer_unwritten(bh)) {
2081
2082 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2083
2084 if (buffer_delay(bh)) {
2085 clear_buffer_delay(bh);
2086 bh->b_blocknr = pblock;
2087 } else {
2088 /*
2089 * unwritten already should have
2090 * blocknr assigned. Verify that
2091 */
2092 clear_buffer_unwritten(bh);
2093 BUG_ON(bh->b_blocknr != pblock);
2094 }
2095
Mingming Cao61628a32008-07-11 19:27:31 -04002096 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002097 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002098
2099 cur_logical++;
2100 pblock++;
2101 } while ((bh = bh->b_this_page) != head);
2102 }
2103 pagevec_release(&pvec);
2104 }
2105}
2106
2107
2108/*
2109 * __unmap_underlying_blocks - just a helper function to unmap
2110 * set of blocks described by @bh
2111 */
2112static inline void __unmap_underlying_blocks(struct inode *inode,
2113 struct buffer_head *bh)
2114{
2115 struct block_device *bdev = inode->i_sb->s_bdev;
2116 int blocks, i;
2117
2118 blocks = bh->b_size >> inode->i_blkbits;
2119 for (i = 0; i < blocks; i++)
2120 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2121}
2122
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002123static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2124 sector_t logical, long blk_cnt)
2125{
2126 int nr_pages, i;
2127 pgoff_t index, end;
2128 struct pagevec pvec;
2129 struct inode *inode = mpd->inode;
2130 struct address_space *mapping = inode->i_mapping;
2131
2132 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2133 end = (logical + blk_cnt - 1) >>
2134 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2135 while (index <= end) {
2136 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2137 if (nr_pages == 0)
2138 break;
2139 for (i = 0; i < nr_pages; i++) {
2140 struct page *page = pvec.pages[i];
2141 index = page->index;
2142 if (index > end)
2143 break;
2144 index++;
2145
2146 BUG_ON(!PageLocked(page));
2147 BUG_ON(PageWriteback(page));
2148 block_invalidatepage(page, 0);
2149 ClearPageUptodate(page);
2150 unlock_page(page);
2151 }
2152 }
2153 return;
2154}
2155
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002156static void ext4_print_free_blocks(struct inode *inode)
2157{
2158 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o16939182009-09-26 17:43:59 -04002159 printk(KERN_CRIT "Total free blocks count %lld\n",
2160 ext4_count_free_blocks(inode->i_sb));
2161 printk(KERN_CRIT "Free/Dirty block details\n");
2162 printk(KERN_CRIT "free_blocks=%lld\n",
2163 (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
2164 printk(KERN_CRIT "dirty_blocks=%lld\n",
2165 (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2166 printk(KERN_CRIT "Block reservation details\n");
2167 printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
2168 EXT4_I(inode)->i_reserved_data_blocks);
2169 printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
2170 EXT4_I(inode)->i_reserved_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002171 return;
2172}
2173
Theodore Ts'ob920c752009-05-14 00:54:29 -04002174/*
Alex Tomas64769242008-07-11 19:27:31 -04002175 * mpage_da_map_blocks - go through given space
2176 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002177 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002178 *
2179 * The function skips space we know is already mapped to disk blocks.
2180 *
Alex Tomas64769242008-07-11 19:27:31 -04002181 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002182static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002183{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002184 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002185 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002186 sector_t next = mpd->b_blocknr;
2187 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2188 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2189 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002190
2191 /*
2192 * We consider only non-mapped and non-allocated blocks
2193 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002194 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002195 !(mpd->b_state & (1 << BH_Delay)) &&
2196 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002197 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002198
2199 /*
2200 * If we didn't accumulate anything to write simply return
2201 */
2202 if (!mpd->b_size)
2203 return 0;
2204
2205 handle = ext4_journal_current_handle();
2206 BUG_ON(!handle);
2207
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002208 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002209 * Call ext4_get_blocks() to allocate any delayed allocation
2210 * blocks, or to convert an uninitialized extent to be
2211 * initialized (in the case where we have written into
2212 * one or more preallocated blocks).
2213 *
2214 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2215 * indicate that we are on the delayed allocation path. This
2216 * affects functions in many different parts of the allocation
2217 * call path. This flag exists primarily because we don't
2218 * want to change *many* call functions, so ext4_get_blocks()
2219 * will set the magic i_delalloc_reserved_flag once the
2220 * inode's allocation semaphore is taken.
2221 *
2222 * If the blocks in questions were delalloc blocks, set
2223 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2224 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002225 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002226 new.b_state = 0;
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05002227 get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002228 if (mpd->b_state & (1 << BH_Delay))
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05002229 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2230
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002231 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002232 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002233 if (blks < 0) {
2234 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002235 /*
2236 * If get block returns with error we simply
2237 * return. Later writepage will redirty the page and
2238 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002239 */
2240 if (err == -EAGAIN)
2241 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002242
2243 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002244 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002245 mpd->retval = err;
2246 return 0;
2247 }
2248
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002249 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002250 * get block failure will cause us to loop in
2251 * writepages, because a_ops->writepage won't be able
2252 * to make progress. The page will be redirtied by
2253 * writepage and writepages will again try to write
2254 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002255 */
Theodore Ts'o16939182009-09-26 17:43:59 -04002256 ext4_msg(mpd->inode->i_sb, KERN_CRIT,
2257 "delayed block allocation failed for inode %lu at "
2258 "logical offset %llu with max blocks %zd with "
2259 "error %d\n", mpd->inode->i_ino,
2260 (unsigned long long) next,
2261 mpd->b_size >> mpd->inode->i_blkbits, err);
2262 printk(KERN_CRIT "This should not happen!! "
2263 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002264 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002265 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002266 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002267 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002268 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002269 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002270 return err;
2271 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002272 BUG_ON(blks == 0);
2273
2274 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002275
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002276 if (buffer_new(&new))
2277 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002278
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002279 /*
2280 * If blocks are delayed marked, we need to
2281 * put actual blocknr and drop delayed bit
2282 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002283 if ((mpd->b_state & (1 << BH_Delay)) ||
2284 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002285 mpage_put_bnr_to_bhs(mpd, next, &new);
2286
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002287 if (ext4_should_order_data(mpd->inode)) {
2288 err = ext4_jbd2_file_inode(handle, mpd->inode);
2289 if (err)
2290 return err;
2291 }
2292
2293 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002294 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002295 */
2296 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2297 if (disksize > i_size_read(mpd->inode))
2298 disksize = i_size_read(mpd->inode);
2299 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2300 ext4_update_i_disksize(mpd->inode, disksize);
2301 return ext4_mark_inode_dirty(handle, mpd->inode);
2302 }
2303
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002304 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002305}
2306
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002307#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2308 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002309
2310/*
2311 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2312 *
2313 * @mpd->lbh - extent of blocks
2314 * @logical - logical number of the block in the file
2315 * @bh - bh of the block (used to access block's state)
2316 *
2317 * the function is used to collect contig. blocks in same state
2318 */
2319static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002320 sector_t logical, size_t b_size,
2321 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002322{
Alex Tomas64769242008-07-11 19:27:31 -04002323 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002324 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002325
Mingming Cao525f4ed2008-08-19 22:15:58 -04002326 /* check if thereserved journal credits might overflow */
2327 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2328 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2329 /*
2330 * With non-extent format we are limited by the journal
2331 * credit available. Total credit needed to insert
2332 * nrblocks contiguous blocks is dependent on the
2333 * nrblocks. So limit nrblocks.
2334 */
2335 goto flush_it;
2336 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2337 EXT4_MAX_TRANS_DATA) {
2338 /*
2339 * Adding the new buffer_head would make it cross the
2340 * allowed limit for which we have journal credit
2341 * reserved. So limit the new bh->b_size
2342 */
2343 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2344 mpd->inode->i_blkbits;
2345 /* we will do mpage_da_submit_io in the next loop */
2346 }
2347 }
Alex Tomas64769242008-07-11 19:27:31 -04002348 /*
2349 * First block in the extent
2350 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002351 if (mpd->b_size == 0) {
2352 mpd->b_blocknr = logical;
2353 mpd->b_size = b_size;
2354 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002355 return;
2356 }
2357
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002358 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002359 /*
2360 * Can we merge the block to our big extent?
2361 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002362 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2363 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002364 return;
2365 }
2366
Mingming Cao525f4ed2008-08-19 22:15:58 -04002367flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002368 /*
2369 * We couldn't merge the block to our extent, so we
2370 * need to flush current extent and start new one
2371 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002372 if (mpage_da_map_blocks(mpd) == 0)
2373 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002374 mpd->io_done = 1;
2375 return;
Alex Tomas64769242008-07-11 19:27:31 -04002376}
2377
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002378static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002379{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002380 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002381}
2382
Alex Tomas64769242008-07-11 19:27:31 -04002383/*
2384 * __mpage_da_writepage - finds extent of pages and blocks
2385 *
2386 * @page: page to consider
2387 * @wbc: not used, we just follow rules
2388 * @data: context
2389 *
2390 * The function finds extents of pages and scan them for all blocks.
2391 */
2392static int __mpage_da_writepage(struct page *page,
2393 struct writeback_control *wbc, void *data)
2394{
2395 struct mpage_da_data *mpd = data;
2396 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002397 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002398 sector_t logical;
2399
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002400 if (mpd->io_done) {
2401 /*
2402 * Rest of the page in the page_vec
2403 * redirty then and skip then. We will
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002404 * try to write them again after
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002405 * starting a new transaction
2406 */
2407 redirty_page_for_writepage(wbc, page);
2408 unlock_page(page);
2409 return MPAGE_DA_EXTENT_TAIL;
2410 }
Alex Tomas64769242008-07-11 19:27:31 -04002411 /*
2412 * Can we merge this page to current extent?
2413 */
2414 if (mpd->next_page != page->index) {
2415 /*
2416 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002417 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002418 */
2419 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002420 if (mpage_da_map_blocks(mpd) == 0)
2421 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002422 /*
2423 * skip rest of the page in the page_vec
2424 */
2425 mpd->io_done = 1;
2426 redirty_page_for_writepage(wbc, page);
2427 unlock_page(page);
2428 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002429 }
2430
2431 /*
2432 * Start next extent of pages ...
2433 */
2434 mpd->first_page = page->index;
2435
2436 /*
2437 * ... and blocks
2438 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002439 mpd->b_size = 0;
2440 mpd->b_state = 0;
2441 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002442 }
2443
2444 mpd->next_page = page->index + 1;
2445 logical = (sector_t) page->index <<
2446 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2447
2448 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002449 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2450 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002451 if (mpd->io_done)
2452 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002453 } else {
2454 /*
2455 * Page with regular buffer heads, just add all dirty ones
2456 */
2457 head = page_buffers(page);
2458 bh = head;
2459 do {
2460 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002461 /*
2462 * We need to try to allocate
2463 * unmapped blocks in the same page.
2464 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002465 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002466 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002467 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002468 mpage_add_bh_to_extent(mpd, logical,
2469 bh->b_size,
2470 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002471 if (mpd->io_done)
2472 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002473 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2474 /*
2475 * mapped dirty buffer. We need to update
2476 * the b_state because we look at
2477 * b_state in mpage_da_map_blocks. We don't
2478 * update b_size because if we find an
2479 * unmapped buffer_head later we need to
2480 * use the b_state flag of that buffer_head.
2481 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002482 if (mpd->b_size == 0)
2483 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002484 }
Alex Tomas64769242008-07-11 19:27:31 -04002485 logical++;
2486 } while ((bh = bh->b_this_page) != head);
2487 }
2488
2489 return 0;
2490}
2491
2492/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002493 * This is a special get_blocks_t callback which is used by
2494 * ext4_da_write_begin(). It will either return mapped block or
2495 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002496 *
2497 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2498 * We also have b_blocknr = -1 and b_bdev initialized properly
2499 *
2500 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2501 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2502 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002503 */
2504static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2505 struct buffer_head *bh_result, int create)
2506{
2507 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002508 sector_t invalid_block = ~((sector_t) 0xffff);
2509
2510 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2511 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002512
2513 BUG_ON(create == 0);
2514 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2515
2516 /*
2517 * first, we need to know whether the block is allocated already
2518 * preallocated blocks are unmapped but should treated
2519 * the same as allocated blocks.
2520 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002521 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002522 if ((ret == 0) && !buffer_delay(bh_result)) {
2523 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002524 /*
2525 * XXX: __block_prepare_write() unmaps passed block,
2526 * is it OK?
2527 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05002528 ret = ext4_da_reserve_space(inode, iblock);
Mingming Caod2a17632008-07-14 17:52:37 -04002529 if (ret)
2530 /* not enough space to reserve */
2531 return ret;
2532
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002533 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002534 set_buffer_new(bh_result);
2535 set_buffer_delay(bh_result);
2536 } else if (ret > 0) {
2537 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002538 if (buffer_unwritten(bh_result)) {
2539 /* A delayed write to unwritten bh should
2540 * be marked new and mapped. Mapped ensures
2541 * that we don't do get_block multiple times
2542 * when we write to the same offset and new
2543 * ensures that we do proper zero out for
2544 * partial write.
2545 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002546 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002547 set_buffer_mapped(bh_result);
2548 }
Alex Tomas64769242008-07-11 19:27:31 -04002549 ret = 0;
2550 }
2551
2552 return ret;
2553}
Mingming Cao61628a32008-07-11 19:27:31 -04002554
Theodore Ts'ob920c752009-05-14 00:54:29 -04002555/*
2556 * This function is used as a standard get_block_t calback function
2557 * when there is no desire to allocate any blocks. It is used as a
2558 * callback function for block_prepare_write(), nobh_writepage(), and
2559 * block_write_full_page(). These functions should only try to map a
2560 * single block at a time.
2561 *
2562 * Since this function doesn't do block allocations even if the caller
2563 * requests it by passing in create=1, it is critically important that
2564 * any caller checks to make sure that any buffer heads are returned
2565 * by this function are either all already mapped or marked for
2566 * delayed allocation before calling nobh_writepage() or
2567 * block_write_full_page(). Otherwise, b_blocknr could be left
2568 * unitialized, and the page write functions will be taken by
2569 * surprise.
2570 */
2571static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002572 struct buffer_head *bh_result, int create)
2573{
2574 int ret = 0;
2575 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2576
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002577 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2578
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002579 /*
2580 * we don't want to do block allocation in writepage
2581 * so call get_block_wrap with create = 0
2582 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002583 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002584 if (ret > 0) {
2585 bh_result->b_size = (ret << inode->i_blkbits);
2586 ret = 0;
2587 }
2588 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002589}
2590
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002591static int bget_one(handle_t *handle, struct buffer_head *bh)
2592{
2593 get_bh(bh);
2594 return 0;
2595}
2596
2597static int bput_one(handle_t *handle, struct buffer_head *bh)
2598{
2599 put_bh(bh);
2600 return 0;
2601}
2602
2603static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002604 unsigned int len)
2605{
2606 struct address_space *mapping = page->mapping;
2607 struct inode *inode = mapping->host;
2608 struct buffer_head *page_bufs;
2609 handle_t *handle = NULL;
2610 int ret = 0;
2611 int err;
2612
2613 page_bufs = page_buffers(page);
2614 BUG_ON(!page_bufs);
2615 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2616 /* As soon as we unlock the page, it can go away, but we have
2617 * references to buffers so we are safe */
2618 unlock_page(page);
2619
2620 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2621 if (IS_ERR(handle)) {
2622 ret = PTR_ERR(handle);
2623 goto out;
2624 }
2625
2626 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2627 do_journal_get_write_access);
2628
2629 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2630 write_end_fn);
2631 if (ret == 0)
2632 ret = err;
2633 err = ext4_journal_stop(handle);
2634 if (!ret)
2635 ret = err;
2636
2637 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
2638 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2639out:
2640 return ret;
2641}
2642
Mingming Cao61628a32008-07-11 19:27:31 -04002643/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002644 * Note that we don't need to start a transaction unless we're journaling data
2645 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2646 * need to file the inode to the transaction's list in ordered mode because if
2647 * we are writing back data added by write(), the inode is already there and if
2648 * we are writing back data modified via mmap(), noone guarantees in which
2649 * transaction the data will hit the disk. In case we are journaling data, we
2650 * cannot start transaction directly because transaction start ranks above page
2651 * lock so we have to do some magic.
2652 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002653 * This function can get called via...
2654 * - ext4_da_writepages after taking page lock (have journal handle)
2655 * - journal_submit_inode_data_buffers (no journal handle)
2656 * - shrink_page_list via pdflush (no journal handle)
2657 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002658 *
2659 * We don't do any block allocation in this function. If we have page with
2660 * multiple blocks we need to write those buffer_heads that are mapped. This
2661 * is important for mmaped based write. So if we do with blocksize 1K
2662 * truncate(f, 1024);
2663 * a = mmap(f, 0, 4096);
2664 * a[0] = 'a';
2665 * truncate(f, 4096);
2666 * we have in the page first buffer_head mapped via page_mkwrite call back
2667 * but other bufer_heads would be unmapped but dirty(dirty done via the
2668 * do_wp_page). So writepage should write the first block. If we modify
2669 * the mmap area beyond 1024 we will again get a page_fault and the
2670 * page_mkwrite callback will do the block allocation and mark the
2671 * buffer_heads mapped.
2672 *
2673 * We redirty the page if we have any buffer_heads that is either delay or
2674 * unwritten in the page.
2675 *
2676 * We can get recursively called as show below.
2677 *
2678 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2679 * ext4_writepage()
2680 *
2681 * But since we don't do any block allocation we should not deadlock.
2682 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002683 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002684static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002685 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002686{
Alex Tomas64769242008-07-11 19:27:31 -04002687 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002688 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002689 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002690 struct buffer_head *page_bufs;
2691 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002692
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002693 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002694 size = i_size_read(inode);
2695 if (page->index == size >> PAGE_CACHE_SHIFT)
2696 len = size & ~PAGE_CACHE_MASK;
2697 else
2698 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002699
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002700 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002701 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002702 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002703 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002704 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002705 * We don't want to do block allocation
2706 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002707 * We may reach here when we do a journal commit
2708 * via journal_submit_inode_data_buffers.
2709 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002710 * them. We can also reach here via shrink_page_list
2711 */
2712 redirty_page_for_writepage(wbc, page);
2713 unlock_page(page);
2714 return 0;
2715 }
2716 } else {
2717 /*
2718 * The test for page_has_buffers() is subtle:
2719 * We know the page is dirty but it lost buffers. That means
2720 * that at some moment in time after write_begin()/write_end()
2721 * has been called all buffers have been clean and thus they
2722 * must have been written at least once. So they are all
2723 * mapped and we can happily proceed with mapping them
2724 * and writing the page.
2725 *
2726 * Try to initialize the buffer_heads and check whether
2727 * all are mapped and non delay. We don't want to
2728 * do block allocation here.
2729 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002730 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002731 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002732 if (!ret) {
2733 page_bufs = page_buffers(page);
2734 /* check whether all are mapped and non delay */
2735 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002736 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002737 redirty_page_for_writepage(wbc, page);
2738 unlock_page(page);
2739 return 0;
2740 }
2741 } else {
2742 /*
2743 * We can't do block allocation here
2744 * so just redity the page and unlock
2745 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002746 */
Mingming Cao61628a32008-07-11 19:27:31 -04002747 redirty_page_for_writepage(wbc, page);
2748 unlock_page(page);
2749 return 0;
2750 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002751 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002752 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002753 }
2754
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002755 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2756 /*
2757 * It's mmapped pagecache. Add buffers and journal it. There
2758 * doesn't seem much point in redirtying the page here.
2759 */
2760 ClearPageChecked(page);
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002761 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002762 }
2763
Alex Tomas64769242008-07-11 19:27:31 -04002764 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002765 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002766 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002767 ret = block_write_full_page(page, noalloc_get_block_write,
2768 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002769
Alex Tomas64769242008-07-11 19:27:31 -04002770 return ret;
2771}
2772
Mingming Cao61628a32008-07-11 19:27:31 -04002773/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002774 * This is called via ext4_da_writepages() to
2775 * calulate the total number of credits to reserve to fit
2776 * a single extent allocation into a single transaction,
2777 * ext4_da_writpeages() will loop calling this before
2778 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002779 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002780
2781static int ext4_da_writepages_trans_blocks(struct inode *inode)
2782{
2783 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2784
2785 /*
2786 * With non-extent format the journal credit needed to
2787 * insert nrblocks contiguous block is dependent on
2788 * number of contiguous block. So we will limit
2789 * number of contiguous block to a sane value
2790 */
Julia Lawall30c6e07a2009-11-15 15:30:58 -05002791 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) &&
Mingming Cao525f4ed2008-08-19 22:15:58 -04002792 (max_blocks > EXT4_MAX_TRANS_DATA))
2793 max_blocks = EXT4_MAX_TRANS_DATA;
2794
2795 return ext4_chunk_trans_blocks(inode, max_blocks);
2796}
Mingming Cao61628a32008-07-11 19:27:31 -04002797
Alex Tomas64769242008-07-11 19:27:31 -04002798static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002799 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002800{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002801 pgoff_t index;
2802 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002803 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002804 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002805 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002806 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002807 int pages_written = 0;
2808 long pages_skipped;
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002809 unsigned int max_pages;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002810 int range_cyclic, cycled = 1, io_done = 0;
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002811 int needed_blocks, ret = 0;
2812 long desired_nr_to_write, nr_to_writebump = 0;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002813 loff_t range_start = wbc->range_start;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002814 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002815
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002816 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002817
Mingming Cao61628a32008-07-11 19:27:31 -04002818 /*
2819 * No pages to write? This is mainly a kludge to avoid starting
2820 * a transaction for special inodes like journal inode on last iput()
2821 * because that could violate lock ordering on umount
2822 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002823 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002824 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002825
2826 /*
2827 * If the filesystem has aborted, it is read-only, so return
2828 * right away instead of dumping stack traces later on that
2829 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002830 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002831 * the latter could be true if the filesystem is mounted
2832 * read-only, and in that case, ext4_da_writepages should
2833 * *never* be called, so if that ever happens, we would want
2834 * the stack trace.
2835 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002836 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002837 return -EROFS;
2838
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002839 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2840 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002841
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002842 range_cyclic = wbc->range_cyclic;
2843 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002844 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002845 if (index)
2846 cycled = 0;
2847 wbc->range_start = index << PAGE_CACHE_SHIFT;
2848 wbc->range_end = LLONG_MAX;
2849 wbc->range_cyclic = 0;
2850 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002851 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002852
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002853 /*
2854 * This works around two forms of stupidity. The first is in
2855 * the writeback code, which caps the maximum number of pages
2856 * written to be 1024 pages. This is wrong on multiple
2857 * levels; different architectues have a different page size,
2858 * which changes the maximum amount of data which gets
2859 * written. Secondly, 4 megabytes is way too small. XFS
2860 * forces this value to be 16 megabytes by multiplying
2861 * nr_to_write parameter by four, and then relies on its
2862 * allocator to allocate larger extents to make them
2863 * contiguous. Unfortunately this brings us to the second
2864 * stupidity, which is that ext4's mballoc code only allocates
2865 * at most 2048 blocks. So we force contiguous writes up to
2866 * the number of dirty blocks in the inode, or
2867 * sbi->max_writeback_mb_bump whichever is smaller.
2868 */
2869 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2870 if (!range_cyclic && range_whole)
2871 desired_nr_to_write = wbc->nr_to_write * 8;
2872 else
2873 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2874 max_pages);
2875 if (desired_nr_to_write > max_pages)
2876 desired_nr_to_write = max_pages;
2877
2878 if (wbc->nr_to_write < desired_nr_to_write) {
2879 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2880 wbc->nr_to_write = desired_nr_to_write;
2881 }
2882
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002883 mpd.wbc = wbc;
2884 mpd.inode = mapping->host;
2885
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002886 /*
2887 * we don't want write_cache_pages to update
2888 * nr_to_write and writeback_index
2889 */
2890 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2891 wbc->no_nrwrite_index_update = 1;
2892 pages_skipped = wbc->pages_skipped;
2893
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002894retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002895 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002896
2897 /*
2898 * we insert one extent at a time. So we need
2899 * credit needed for single extent allocation.
2900 * journalled mode is currently not supported
2901 * by delalloc
2902 */
2903 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002904 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002905
Mingming Cao61628a32008-07-11 19:27:31 -04002906 /* start a new transaction*/
2907 handle = ext4_journal_start(inode, needed_blocks);
2908 if (IS_ERR(handle)) {
2909 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002910 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002911 "%ld pages, ino %lu; err %d\n", __func__,
2912 wbc->nr_to_write, inode->i_ino, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002913 goto out_writepages;
2914 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002915
2916 /*
2917 * Now call __mpage_da_writepage to find the next
2918 * contiguous region of logical blocks that need
2919 * blocks to be allocated by ext4. We don't actually
2920 * submit the blocks for I/O here, even though
2921 * write_cache_pages thinks it will, and will set the
2922 * pages as clean for write before calling
2923 * __mpage_da_writepage().
2924 */
2925 mpd.b_size = 0;
2926 mpd.b_state = 0;
2927 mpd.b_blocknr = 0;
2928 mpd.first_page = 0;
2929 mpd.next_page = 0;
2930 mpd.io_done = 0;
2931 mpd.pages_written = 0;
2932 mpd.retval = 0;
2933 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2934 &mpd);
2935 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002936 * If we have a contiguous extent of pages and we
Theodore Ts'of63e6002009-02-23 16:42:39 -05002937 * haven't done the I/O yet, map the blocks and submit
2938 * them for I/O.
2939 */
2940 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2941 if (mpage_da_map_blocks(&mpd) == 0)
2942 mpage_da_submit_io(&mpd);
2943 mpd.io_done = 1;
2944 ret = MPAGE_DA_EXTENT_TAIL;
2945 }
Theodore Ts'ob3a3ca82009-08-31 23:13:11 -04002946 trace_ext4_da_write_pages(inode, &mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002947 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002948
Mingming Cao61628a32008-07-11 19:27:31 -04002949 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002950
Eric Sandeen8f64b322009-02-26 00:57:35 -05002951 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002952 /* commit the transaction which would
2953 * free blocks released in the transaction
2954 * and try again
2955 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002956 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002957 wbc->pages_skipped = pages_skipped;
2958 ret = 0;
2959 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002960 /*
2961 * got one extent now try with
2962 * rest of the pages
2963 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002964 pages_written += mpd.pages_written;
2965 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002966 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002967 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002968 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002969 /*
2970 * There is no more writeout needed
2971 * or we requested for a noblocking writeout
2972 * and we found the device congested
2973 */
Mingming Cao61628a32008-07-11 19:27:31 -04002974 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002975 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002976 if (!io_done && !cycled) {
2977 cycled = 1;
2978 index = 0;
2979 wbc->range_start = index << PAGE_CACHE_SHIFT;
2980 wbc->range_end = mapping->writeback_index - 1;
2981 goto retry;
2982 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002983 if (pages_skipped != wbc->pages_skipped)
Theodore Ts'o16939182009-09-26 17:43:59 -04002984 ext4_msg(inode->i_sb, KERN_CRIT,
2985 "This should not happen leaving %s "
2986 "with nr_to_write = %ld ret = %d\n",
2987 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002988
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002989 /* Update index */
2990 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002991 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002992 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2993 /*
2994 * set the writeback_index so that range_cyclic
2995 * mode will write it back later
2996 */
2997 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002998
Mingming Cao61628a32008-07-11 19:27:31 -04002999out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003000 if (!no_nrwrite_index_update)
3001 wbc->no_nrwrite_index_update = 0;
Richard Kennedy2faf2e12009-12-25 15:46:07 -05003002 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'ode89de62009-08-31 17:00:59 -04003003 wbc->range_start = range_start;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003004 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04003005 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003006}
3007
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003008#define FALL_BACK_TO_NONDELALLOC 1
3009static int ext4_nonda_switch(struct super_block *sb)
3010{
3011 s64 free_blocks, dirty_blocks;
3012 struct ext4_sb_info *sbi = EXT4_SB(sb);
3013
3014 /*
3015 * switch to non delalloc mode if we are running low
3016 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08003017 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003018 * accumulated on each CPU without updating global counters
3019 * Delalloc need an accurate free block accounting. So switch
3020 * to non delalloc when we are near to error range.
3021 */
3022 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3023 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
3024 if (2 * free_blocks < 3 * dirty_blocks ||
3025 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
3026 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05003027 * free block count is less than 150% of dirty blocks
3028 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003029 */
3030 return 1;
3031 }
Eric Sandeenc8afb442009-12-23 07:58:12 -05003032 /*
3033 * Even if we don't switch but are nearing capacity,
3034 * start pushing delalloc when 1/2 of free blocks are dirty.
3035 */
3036 if (free_blocks < 2 * dirty_blocks)
3037 writeback_inodes_sb_if_idle(sb);
3038
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003039 return 0;
3040}
3041
Alex Tomas64769242008-07-11 19:27:31 -04003042static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003043 loff_t pos, unsigned len, unsigned flags,
3044 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003045{
Aneesh Kumar K.V1db91382010-01-22 17:06:20 -05003046 int ret, retries = 0, quota_retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04003047 struct page *page;
3048 pgoff_t index;
3049 unsigned from, to;
3050 struct inode *inode = mapping->host;
3051 handle_t *handle;
3052
3053 index = pos >> PAGE_CACHE_SHIFT;
3054 from = pos & (PAGE_CACHE_SIZE - 1);
3055 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003056
3057 if (ext4_nonda_switch(inode->i_sb)) {
3058 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
3059 return ext4_write_begin(file, mapping, pos,
3060 len, flags, pagep, fsdata);
3061 }
3062 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003063 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04003064retry:
Alex Tomas64769242008-07-11 19:27:31 -04003065 /*
3066 * With delayed allocation, we don't log the i_disksize update
3067 * if there is delayed block allocation. But we still need
3068 * to journalling the i_disksize update if writes to the end
3069 * of file which has an already mapped buffer.
3070 */
3071 handle = ext4_journal_start(inode, 1);
3072 if (IS_ERR(handle)) {
3073 ret = PTR_ERR(handle);
3074 goto out;
3075 }
Jan Karaebd36102009-02-22 21:09:59 -05003076 /* We cannot recurse into the filesystem as the transaction is already
3077 * started */
3078 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04003079
Nick Piggin54566b22009-01-04 12:00:53 -08003080 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003081 if (!page) {
3082 ext4_journal_stop(handle);
3083 ret = -ENOMEM;
3084 goto out;
3085 }
Alex Tomas64769242008-07-11 19:27:31 -04003086 *pagep = page;
3087
3088 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04003089 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04003090 if (ret < 0) {
3091 unlock_page(page);
3092 ext4_journal_stop(handle);
3093 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003094 /*
3095 * block_write_begin may have instantiated a few blocks
3096 * outside i_size. Trim these off again. Don't need
3097 * i_size_read because we hold i_mutex.
3098 */
3099 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003100 ext4_truncate_failed_write(inode);
Alex Tomas64769242008-07-11 19:27:31 -04003101 }
3102
Mingming Caod2a17632008-07-14 17:52:37 -04003103 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3104 goto retry;
Aneesh Kumar K.V1db91382010-01-22 17:06:20 -05003105
3106 if ((ret == -EDQUOT) &&
3107 EXT4_I(inode)->i_reserved_meta_blocks &&
3108 (quota_retries++ < 3)) {
3109 /*
3110 * Since we often over-estimate the number of meta
3111 * data blocks required, we may sometimes get a
3112 * spurios out of quota error even though there would
3113 * be enough space once we write the data blocks and
3114 * find out how many meta data blocks were _really_
3115 * required. So try forcing the inode write to see if
3116 * that helps.
3117 */
3118 write_inode_now(inode, (quota_retries == 3));
3119 goto retry;
3120 }
Alex Tomas64769242008-07-11 19:27:31 -04003121out:
3122 return ret;
3123}
3124
Mingming Cao632eaea2008-07-11 19:27:31 -04003125/*
3126 * Check if we should update i_disksize
3127 * when write to the end of file but not require block allocation
3128 */
3129static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003130 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003131{
3132 struct buffer_head *bh;
3133 struct inode *inode = page->mapping->host;
3134 unsigned int idx;
3135 int i;
3136
3137 bh = page_buffers(page);
3138 idx = offset >> inode->i_blkbits;
3139
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003140 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003141 bh = bh->b_this_page;
3142
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003143 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003144 return 0;
3145 return 1;
3146}
3147
Alex Tomas64769242008-07-11 19:27:31 -04003148static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003149 struct address_space *mapping,
3150 loff_t pos, unsigned len, unsigned copied,
3151 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003152{
3153 struct inode *inode = mapping->host;
3154 int ret = 0, ret2;
3155 handle_t *handle = ext4_journal_current_handle();
3156 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003157 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003158 int write_mode = (int)(unsigned long)fsdata;
3159
3160 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3161 if (ext4_should_order_data(inode)) {
3162 return ext4_ordered_write_end(file, mapping, pos,
3163 len, copied, page, fsdata);
3164 } else if (ext4_should_writeback_data(inode)) {
3165 return ext4_writeback_write_end(file, mapping, pos,
3166 len, copied, page, fsdata);
3167 } else {
3168 BUG();
3169 }
3170 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003171
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003172 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003173 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003174 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003175
3176 /*
3177 * generic_write_end() will run mark_inode_dirty() if i_size
3178 * changes. So let's piggyback the i_disksize mark_inode_dirty
3179 * into that.
3180 */
3181
3182 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003183 if (new_i_size > EXT4_I(inode)->i_disksize) {
3184 if (ext4_da_should_update_i_disksize(page, end)) {
3185 down_write(&EXT4_I(inode)->i_data_sem);
3186 if (new_i_size > EXT4_I(inode)->i_disksize) {
3187 /*
3188 * Updating i_disksize when extending file
3189 * without needing block allocation
3190 */
3191 if (ext4_should_order_data(inode))
3192 ret = ext4_jbd2_file_inode(handle,
3193 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003194
Mingming Cao632eaea2008-07-11 19:27:31 -04003195 EXT4_I(inode)->i_disksize = new_i_size;
3196 }
3197 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003198 /* We need to mark inode dirty even if
3199 * new_i_size is less that inode->i_size
3200 * bu greater than i_disksize.(hint delalloc)
3201 */
3202 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003203 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003204 }
Alex Tomas64769242008-07-11 19:27:31 -04003205 ret2 = generic_write_end(file, mapping, pos, len, copied,
3206 page, fsdata);
3207 copied = ret2;
3208 if (ret2 < 0)
3209 ret = ret2;
3210 ret2 = ext4_journal_stop(handle);
3211 if (!ret)
3212 ret = ret2;
3213
3214 return ret ? ret : copied;
3215}
3216
3217static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3218{
Alex Tomas64769242008-07-11 19:27:31 -04003219 /*
3220 * Drop reserved blocks
3221 */
3222 BUG_ON(!PageLocked(page));
3223 if (!page_has_buffers(page))
3224 goto out;
3225
Mingming Caod2a17632008-07-14 17:52:37 -04003226 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003227
3228out:
3229 ext4_invalidatepage(page, offset);
3230
3231 return;
3232}
3233
Theodore Ts'occd25062009-02-26 01:04:07 -05003234/*
3235 * Force all delayed allocation blocks to be allocated for a given inode.
3236 */
3237int ext4_alloc_da_blocks(struct inode *inode)
3238{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003239 trace_ext4_alloc_da_blocks(inode);
3240
Theodore Ts'occd25062009-02-26 01:04:07 -05003241 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3242 !EXT4_I(inode)->i_reserved_meta_blocks)
3243 return 0;
3244
3245 /*
3246 * We do something simple for now. The filemap_flush() will
3247 * also start triggering a write of the data blocks, which is
3248 * not strictly speaking necessary (and for users of
3249 * laptop_mode, not even desirable). However, to do otherwise
3250 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003251 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003252 * ext4_da_writepages() ->
3253 * write_cache_pages() ---> (via passed in callback function)
3254 * __mpage_da_writepage() -->
3255 * mpage_add_bh_to_extent()
3256 * mpage_da_map_blocks()
3257 *
3258 * The problem is that write_cache_pages(), located in
3259 * mm/page-writeback.c, marks pages clean in preparation for
3260 * doing I/O, which is not desirable if we're not planning on
3261 * doing I/O at all.
3262 *
3263 * We could call write_cache_pages(), and then redirty all of
3264 * the pages by calling redirty_page_for_writeback() but that
3265 * would be ugly in the extreme. So instead we would need to
3266 * replicate parts of the code in the above functions,
3267 * simplifying them becuase we wouldn't actually intend to
3268 * write out the pages, but rather only collect contiguous
3269 * logical block extents, call the multi-block allocator, and
3270 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003271 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003272 * For now, though, we'll cheat by calling filemap_flush(),
3273 * which will map the blocks, and start the I/O, but not
3274 * actually wait for the I/O to complete.
3275 */
3276 return filemap_flush(inode->i_mapping);
3277}
Alex Tomas64769242008-07-11 19:27:31 -04003278
3279/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003280 * bmap() is special. It gets used by applications such as lilo and by
3281 * the swapper to find the on-disk block of a specific piece of data.
3282 *
3283 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003284 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285 * filesystem and enables swap, then they may get a nasty shock when the
3286 * data getting swapped to that swapfile suddenly gets overwritten by
3287 * the original zero's written out previously to the journal and
3288 * awaiting writeback in the kernel's buffer cache.
3289 *
3290 * So, if we see any bmap calls here on a modified, data-journaled file,
3291 * take extra steps to flush any blocks which might be in the cache.
3292 */
Mingming Cao617ba132006-10-11 01:20:53 -07003293static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294{
3295 struct inode *inode = mapping->host;
3296 journal_t *journal;
3297 int err;
3298
Alex Tomas64769242008-07-11 19:27:31 -04003299 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3300 test_opt(inode->i_sb, DELALLOC)) {
3301 /*
3302 * With delalloc we want to sync the file
3303 * so that we can make sure we allocate
3304 * blocks for file
3305 */
3306 filemap_write_and_wait(mapping);
3307 }
3308
Frank Mayhar03901312009-01-07 00:06:22 -05003309 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003310 /*
3311 * This is a REALLY heavyweight approach, but the use of
3312 * bmap on dirty files is expected to be extremely rare:
3313 * only if we run lilo or swapon on a freshly made file
3314 * do we expect this to happen.
3315 *
3316 * (bmap requires CAP_SYS_RAWIO so this does not
3317 * represent an unprivileged user DOS attack --- we'd be
3318 * in trouble if mortal users could trigger this path at
3319 * will.)
3320 *
Mingming Cao617ba132006-10-11 01:20:53 -07003321 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322 * regular files. If somebody wants to bmap a directory
3323 * or symlink and gets confused because the buffer
3324 * hasn't yet been flushed to disk, they deserve
3325 * everything they get.
3326 */
3327
Mingming Cao617ba132006-10-11 01:20:53 -07003328 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3329 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003330 jbd2_journal_lock_updates(journal);
3331 err = jbd2_journal_flush(journal);
3332 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003333
3334 if (err)
3335 return 0;
3336 }
3337
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003338 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003339}
3340
Mingming Cao617ba132006-10-11 01:20:53 -07003341static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003342{
Mingming Cao617ba132006-10-11 01:20:53 -07003343 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003344}
3345
3346static int
Mingming Cao617ba132006-10-11 01:20:53 -07003347ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003348 struct list_head *pages, unsigned nr_pages)
3349{
Mingming Cao617ba132006-10-11 01:20:53 -07003350 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003351}
3352
Mingming Cao617ba132006-10-11 01:20:53 -07003353static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003354{
Mingming Cao617ba132006-10-11 01:20:53 -07003355 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003356
3357 /*
3358 * If it's a full truncate we just forget about the pending dirtying
3359 */
3360 if (offset == 0)
3361 ClearPageChecked(page);
3362
Frank Mayhar03901312009-01-07 00:06:22 -05003363 if (journal)
3364 jbd2_journal_invalidatepage(journal, page, offset);
3365 else
3366 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003367}
3368
Mingming Cao617ba132006-10-11 01:20:53 -07003369static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003370{
Mingming Cao617ba132006-10-11 01:20:53 -07003371 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003372
3373 WARN_ON(PageChecked(page));
3374 if (!page_has_buffers(page))
3375 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003376 if (journal)
3377 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3378 else
3379 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003380}
3381
3382/*
Mingming Cao4c0425f2009-09-28 15:48:41 -04003383 * O_DIRECT for ext3 (or indirect map) based files
3384 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003385 * If the O_DIRECT write will extend the file then add this inode to the
3386 * orphan list. So recovery will truncate it back to the original size
3387 * if the machine crashes during the write.
3388 *
3389 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003390 * crashes then stale disk data _may_ be exposed inside the file. But current
3391 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003392 */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003393static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003394 const struct iovec *iov, loff_t offset,
3395 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003396{
3397 struct file *file = iocb->ki_filp;
3398 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003399 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003400 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003401 ssize_t ret;
3402 int orphan = 0;
3403 size_t count = iov_length(iov, nr_segs);
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003404 int retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405
3406 if (rw == WRITE) {
3407 loff_t final_size = offset + count;
3408
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003409 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003410 /* Credits for sb + inode write */
3411 handle = ext4_journal_start(inode, 2);
3412 if (IS_ERR(handle)) {
3413 ret = PTR_ERR(handle);
3414 goto out;
3415 }
Mingming Cao617ba132006-10-11 01:20:53 -07003416 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003417 if (ret) {
3418 ext4_journal_stop(handle);
3419 goto out;
3420 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003421 orphan = 1;
3422 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003423 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003424 }
3425 }
3426
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003427retry:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003428 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3429 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003430 ext4_get_block, NULL);
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003431 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3432 goto retry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003433
Jan Kara7fb54092008-02-10 01:08:38 -05003434 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003435 int err;
3436
Jan Kara7fb54092008-02-10 01:08:38 -05003437 /* Credits for sb + inode write */
3438 handle = ext4_journal_start(inode, 2);
3439 if (IS_ERR(handle)) {
3440 /* This is really bad luck. We've written the data
3441 * but cannot extend i_size. Bail out and pretend
3442 * the write failed... */
3443 ret = PTR_ERR(handle);
3444 goto out;
3445 }
3446 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003447 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003448 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 loff_t end = offset + ret;
3450 if (end > inode->i_size) {
3451 ei->i_disksize = end;
3452 i_size_write(inode, end);
3453 /*
3454 * We're going to return a positive `ret'
3455 * here due to non-zero-length I/O, so there's
3456 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003457 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458 * ignore it.
3459 */
Mingming Cao617ba132006-10-11 01:20:53 -07003460 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003461 }
3462 }
Mingming Cao617ba132006-10-11 01:20:53 -07003463 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464 if (ret == 0)
3465 ret = err;
3466 }
3467out:
3468 return ret;
3469}
3470
Mingming Cao4c0425f2009-09-28 15:48:41 -04003471static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock,
3472 struct buffer_head *bh_result, int create)
3473{
3474 handle_t *handle = NULL;
3475 int ret = 0;
3476 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
3477 int dio_credits;
3478
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003479 ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n",
3480 inode->i_ino, create);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003481 /*
3482 * DIO VFS code passes create = 0 flag for write to
3483 * the middle of file. It does this to avoid block
3484 * allocation for holes, to prevent expose stale data
3485 * out when there is parallel buffered read (which does
3486 * not hold the i_mutex lock) while direct IO write has
3487 * not completed. DIO request on holes finally falls back
3488 * to buffered IO for this reason.
3489 *
3490 * For ext4 extent based file, since we support fallocate,
3491 * new allocated extent as uninitialized, for holes, we
3492 * could fallocate blocks for holes, thus parallel
3493 * buffered IO read will zero out the page when read on
3494 * a hole while parallel DIO write to the hole has not completed.
3495 *
3496 * when we come here, we know it's a direct IO write to
3497 * to the middle of file (<i_size)
3498 * so it's safe to override the create flag from VFS.
3499 */
3500 create = EXT4_GET_BLOCKS_DIO_CREATE_EXT;
3501
3502 if (max_blocks > DIO_MAX_BLOCKS)
3503 max_blocks = DIO_MAX_BLOCKS;
3504 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
3505 handle = ext4_journal_start(inode, dio_credits);
3506 if (IS_ERR(handle)) {
3507 ret = PTR_ERR(handle);
3508 goto out;
3509 }
3510 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
3511 create);
3512 if (ret > 0) {
3513 bh_result->b_size = (ret << inode->i_blkbits);
3514 ret = 0;
3515 }
3516 ext4_journal_stop(handle);
3517out:
3518 return ret;
3519}
3520
Mingming Cao4c0425f2009-09-28 15:48:41 -04003521static void ext4_free_io_end(ext4_io_end_t *io)
3522{
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003523 BUG_ON(!io);
3524 iput(io->inode);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003525 kfree(io);
3526}
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003527static void dump_aio_dio_list(struct inode * inode)
3528{
3529#ifdef EXT4_DEBUG
3530 struct list_head *cur, *before, *after;
3531 ext4_io_end_t *io, *io0, *io1;
3532
3533 if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
3534 ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino);
3535 return;
3536 }
3537
3538 ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino);
3539 list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){
3540 cur = &io->list;
3541 before = cur->prev;
3542 io0 = container_of(before, ext4_io_end_t, list);
3543 after = cur->next;
3544 io1 = container_of(after, ext4_io_end_t, list);
3545
3546 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
3547 io, inode->i_ino, io0, io1);
3548 }
3549#endif
3550}
Mingming Cao4c0425f2009-09-28 15:48:41 -04003551
3552/*
Mingming Cao4c0425f2009-09-28 15:48:41 -04003553 * check a range of space and convert unwritten extents to written.
3554 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003555static int ext4_end_aio_dio_nolock(ext4_io_end_t *io)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003556{
Mingming Cao4c0425f2009-09-28 15:48:41 -04003557 struct inode *inode = io->inode;
3558 loff_t offset = io->offset;
3559 size_t size = io->size;
3560 int ret = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003561
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003562 ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p,"
3563 "list->prev 0x%p\n",
3564 io, inode->i_ino, io->list.next, io->list.prev);
3565
3566 if (list_empty(&io->list))
3567 return ret;
3568
3569 if (io->flag != DIO_AIO_UNWRITTEN)
3570 return ret;
3571
Mingming Cao4c0425f2009-09-28 15:48:41 -04003572 if (offset + size <= i_size_read(inode))
3573 ret = ext4_convert_unwritten_extents(inode, offset, size);
3574
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003575 if (ret < 0) {
Mingming Cao4c0425f2009-09-28 15:48:41 -04003576 printk(KERN_EMERG "%s: failed to convert unwritten"
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003577 "extents to written extents, error is %d"
3578 " io is still on inode %lu aio dio list\n",
3579 __func__, ret, inode->i_ino);
3580 return ret;
3581 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003582
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003583 /* clear the DIO AIO unwritten flag */
3584 io->flag = 0;
3585 return ret;
3586}
3587/*
3588 * work on completed aio dio IO, to convert unwritten extents to extents
3589 */
3590static void ext4_end_aio_dio_work(struct work_struct *work)
3591{
3592 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
3593 struct inode *inode = io->inode;
3594 int ret = 0;
3595
3596 mutex_lock(&inode->i_mutex);
3597 ret = ext4_end_aio_dio_nolock(io);
3598 if (ret >= 0) {
3599 if (!list_empty(&io->list))
3600 list_del_init(&io->list);
3601 ext4_free_io_end(io);
3602 }
3603 mutex_unlock(&inode->i_mutex);
3604}
3605/*
3606 * This function is called from ext4_sync_file().
3607 *
3608 * When AIO DIO IO is completed, the work to convert unwritten
3609 * extents to written is queued on workqueue but may not get immediately
3610 * scheduled. When fsync is called, we need to ensure the
3611 * conversion is complete before fsync returns.
3612 * The inode keeps track of a list of completed AIO from DIO path
3613 * that might needs to do the conversion. This function walks through
3614 * the list and convert the related unwritten extents to written.
3615 */
3616int flush_aio_dio_completed_IO(struct inode *inode)
3617{
3618 ext4_io_end_t *io;
3619 int ret = 0;
3620 int ret2 = 0;
3621
3622 if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list))
3623 return ret;
3624
3625 dump_aio_dio_list(inode);
3626 while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
3627 io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next,
3628 ext4_io_end_t, list);
3629 /*
3630 * Calling ext4_end_aio_dio_nolock() to convert completed
3631 * IO to written.
3632 *
3633 * When ext4_sync_file() is called, run_queue() may already
3634 * about to flush the work corresponding to this io structure.
3635 * It will be upset if it founds the io structure related
3636 * to the work-to-be schedule is freed.
3637 *
3638 * Thus we need to keep the io structure still valid here after
3639 * convertion finished. The io structure has a flag to
3640 * avoid double converting from both fsync and background work
3641 * queue work.
3642 */
3643 ret = ext4_end_aio_dio_nolock(io);
3644 if (ret < 0)
3645 ret2 = ret;
3646 else
3647 list_del_init(&io->list);
3648 }
3649 return (ret2 < 0) ? ret2 : 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003650}
3651
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003652static ext4_io_end_t *ext4_init_io_end (struct inode *inode)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003653{
3654 ext4_io_end_t *io = NULL;
3655
3656 io = kmalloc(sizeof(*io), GFP_NOFS);
3657
3658 if (io) {
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003659 igrab(inode);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003660 io->inode = inode;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003661 io->flag = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003662 io->offset = 0;
3663 io->size = 0;
3664 io->error = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003665 INIT_WORK(&io->work, ext4_end_aio_dio_work);
3666 INIT_LIST_HEAD(&io->list);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003667 }
3668
3669 return io;
3670}
3671
3672static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3673 ssize_t size, void *private)
3674{
3675 ext4_io_end_t *io_end = iocb->private;
3676 struct workqueue_struct *wq;
3677
Mingming4b70df12009-11-03 14:44:54 -05003678 /* if not async direct IO or dio with 0 bytes write, just return */
3679 if (!io_end || !size)
3680 return;
3681
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003682 ext_debug("ext4_end_io_dio(): io_end 0x%p"
3683 "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
3684 iocb->private, io_end->inode->i_ino, iocb, offset,
3685 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003686
3687 /* if not aio dio with unwritten extents, just free io and return */
3688 if (io_end->flag != DIO_AIO_UNWRITTEN){
3689 ext4_free_io_end(io_end);
3690 iocb->private = NULL;
3691 return;
3692 }
3693
Mingming Cao4c0425f2009-09-28 15:48:41 -04003694 io_end->offset = offset;
3695 io_end->size = size;
3696 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
3697
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003698 /* queue the work to convert unwritten extents to written */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003699 queue_work(wq, &io_end->work);
3700
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003701 /* Add the io_end to per-inode completed aio dio list*/
3702 list_add_tail(&io_end->list,
3703 &EXT4_I(io_end->inode)->i_aio_dio_complete_list);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003704 iocb->private = NULL;
3705}
3706/*
3707 * For ext4 extent files, ext4 will do direct-io write to holes,
3708 * preallocated extents, and those write extend the file, no need to
3709 * fall back to buffered IO.
3710 *
3711 * For holes, we fallocate those blocks, mark them as unintialized
3712 * If those blocks were preallocated, we mark sure they are splited, but
3713 * still keep the range to write as unintialized.
3714 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003715 * The unwrritten extents will be converted to written when DIO is completed.
3716 * For async direct IO, since the IO may still pending when return, we
3717 * set up an end_io call back function, which will do the convertion
3718 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003719 *
3720 * If the O_DIRECT write will extend the file then add this inode to the
3721 * orphan list. So recovery will truncate it back to the original size
3722 * if the machine crashes during the write.
3723 *
3724 */
3725static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3726 const struct iovec *iov, loff_t offset,
3727 unsigned long nr_segs)
3728{
3729 struct file *file = iocb->ki_filp;
3730 struct inode *inode = file->f_mapping->host;
3731 ssize_t ret;
3732 size_t count = iov_length(iov, nr_segs);
3733
3734 loff_t final_size = offset + count;
3735 if (rw == WRITE && final_size <= inode->i_size) {
3736 /*
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003737 * We could direct write to holes and fallocate.
3738 *
3739 * Allocated blocks to fill the hole are marked as uninitialized
Mingming Cao4c0425f2009-09-28 15:48:41 -04003740 * to prevent paralel buffered read to expose the stale data
3741 * before DIO complete the data IO.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003742 *
3743 * As to previously fallocated extents, ext4 get_block
Mingming Cao4c0425f2009-09-28 15:48:41 -04003744 * will just simply mark the buffer mapped but still
3745 * keep the extents uninitialized.
3746 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003747 * for non AIO case, we will convert those unwritten extents
3748 * to written after return back from blockdev_direct_IO.
3749 *
3750 * for async DIO, the conversion needs to be defered when
3751 * the IO is completed. The ext4 end_io callback function
3752 * will be called to take care of the conversion work.
3753 * Here for async case, we allocate an io_end structure to
3754 * hook to the iocb.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003755 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003756 iocb->private = NULL;
3757 EXT4_I(inode)->cur_aio_dio = NULL;
3758 if (!is_sync_kiocb(iocb)) {
3759 iocb->private = ext4_init_io_end(inode);
3760 if (!iocb->private)
3761 return -ENOMEM;
3762 /*
3763 * we save the io structure for current async
3764 * direct IO, so that later ext4_get_blocks()
3765 * could flag the io structure whether there
3766 * is a unwritten extents needs to be converted
3767 * when IO is completed.
3768 */
3769 EXT4_I(inode)->cur_aio_dio = iocb->private;
3770 }
3771
Mingming Cao4c0425f2009-09-28 15:48:41 -04003772 ret = blockdev_direct_IO(rw, iocb, inode,
3773 inode->i_sb->s_bdev, iov,
3774 offset, nr_segs,
3775 ext4_get_block_dio_write,
3776 ext4_end_io_dio);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003777 if (iocb->private)
3778 EXT4_I(inode)->cur_aio_dio = NULL;
3779 /*
3780 * The io_end structure takes a reference to the inode,
3781 * that structure needs to be destroyed and the
3782 * reference to the inode need to be dropped, when IO is
3783 * complete, even with 0 byte write, or failed.
3784 *
3785 * In the successful AIO DIO case, the io_end structure will be
3786 * desctroyed and the reference to the inode will be dropped
3787 * after the end_io call back function is called.
3788 *
3789 * In the case there is 0 byte write, or error case, since
3790 * VFS direct IO won't invoke the end_io call back function,
3791 * we need to free the end_io structure here.
3792 */
3793 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3794 ext4_free_io_end(iocb->private);
3795 iocb->private = NULL;
Mingming5f524952009-11-10 10:48:04 -05003796 } else if (ret > 0 && (EXT4_I(inode)->i_state &
3797 EXT4_STATE_DIO_UNWRITTEN)) {
Mingming109f5562009-11-10 10:48:08 -05003798 int err;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003799 /*
3800 * for non AIO case, since the IO is already
3801 * completed, we could do the convertion right here
3802 */
Mingming109f5562009-11-10 10:48:08 -05003803 err = ext4_convert_unwritten_extents(inode,
3804 offset, ret);
3805 if (err < 0)
3806 ret = err;
Mingming5f524952009-11-10 10:48:04 -05003807 EXT4_I(inode)->i_state &= ~EXT4_STATE_DIO_UNWRITTEN;
Mingming109f5562009-11-10 10:48:08 -05003808 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003809 return ret;
3810 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003811
3812 /* for write the the end of file case, we fall back to old way */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003813 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3814}
3815
3816static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
3817 const struct iovec *iov, loff_t offset,
3818 unsigned long nr_segs)
3819{
3820 struct file *file = iocb->ki_filp;
3821 struct inode *inode = file->f_mapping->host;
3822
3823 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
3824 return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3825
3826 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3827}
3828
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003829/*
Mingming Cao617ba132006-10-11 01:20:53 -07003830 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003831 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3832 * much here because ->set_page_dirty is called under VFS locks. The page is
3833 * not necessarily locked.
3834 *
3835 * We cannot just dirty the page and leave attached buffers clean, because the
3836 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3837 * or jbddirty because all the journalling code will explode.
3838 *
3839 * So what we do is to mark the page "pending dirty" and next time writepage
3840 * is called, propagate that into the buffers appropriately.
3841 */
Mingming Cao617ba132006-10-11 01:20:53 -07003842static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003843{
3844 SetPageChecked(page);
3845 return __set_page_dirty_nobuffers(page);
3846}
3847
Mingming Cao617ba132006-10-11 01:20:53 -07003848static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003849 .readpage = ext4_readpage,
3850 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003851 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003852 .sync_page = block_sync_page,
3853 .write_begin = ext4_write_begin,
3854 .write_end = ext4_ordered_write_end,
3855 .bmap = ext4_bmap,
3856 .invalidatepage = ext4_invalidatepage,
3857 .releasepage = ext4_releasepage,
3858 .direct_IO = ext4_direct_IO,
3859 .migratepage = buffer_migrate_page,
3860 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003861 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003862};
3863
Mingming Cao617ba132006-10-11 01:20:53 -07003864static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003865 .readpage = ext4_readpage,
3866 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003867 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003868 .sync_page = block_sync_page,
3869 .write_begin = ext4_write_begin,
3870 .write_end = ext4_writeback_write_end,
3871 .bmap = ext4_bmap,
3872 .invalidatepage = ext4_invalidatepage,
3873 .releasepage = ext4_releasepage,
3874 .direct_IO = ext4_direct_IO,
3875 .migratepage = buffer_migrate_page,
3876 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003877 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003878};
3879
Mingming Cao617ba132006-10-11 01:20:53 -07003880static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003881 .readpage = ext4_readpage,
3882 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003883 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003884 .sync_page = block_sync_page,
3885 .write_begin = ext4_write_begin,
3886 .write_end = ext4_journalled_write_end,
3887 .set_page_dirty = ext4_journalled_set_page_dirty,
3888 .bmap = ext4_bmap,
3889 .invalidatepage = ext4_invalidatepage,
3890 .releasepage = ext4_releasepage,
3891 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003892 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893};
3894
Alex Tomas64769242008-07-11 19:27:31 -04003895static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003896 .readpage = ext4_readpage,
3897 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003898 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003899 .writepages = ext4_da_writepages,
3900 .sync_page = block_sync_page,
3901 .write_begin = ext4_da_write_begin,
3902 .write_end = ext4_da_write_end,
3903 .bmap = ext4_bmap,
3904 .invalidatepage = ext4_da_invalidatepage,
3905 .releasepage = ext4_releasepage,
3906 .direct_IO = ext4_direct_IO,
3907 .migratepage = buffer_migrate_page,
3908 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003909 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003910};
3911
Mingming Cao617ba132006-10-11 01:20:53 -07003912void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003913{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003914 if (ext4_should_order_data(inode) &&
3915 test_opt(inode->i_sb, DELALLOC))
3916 inode->i_mapping->a_ops = &ext4_da_aops;
3917 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003918 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003919 else if (ext4_should_writeback_data(inode) &&
3920 test_opt(inode->i_sb, DELALLOC))
3921 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003922 else if (ext4_should_writeback_data(inode))
3923 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003924 else
Mingming Cao617ba132006-10-11 01:20:53 -07003925 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926}
3927
3928/*
Mingming Cao617ba132006-10-11 01:20:53 -07003929 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003930 * up to the end of the block which corresponds to `from'.
3931 * This required during truncate. We need to physically zero the tail end
3932 * of that block so it doesn't yield old data if the file is later grown.
3933 */
Jan Karacf108bc2008-07-11 19:27:31 -04003934int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003935 struct address_space *mapping, loff_t from)
3936{
Mingming Cao617ba132006-10-11 01:20:53 -07003937 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003938 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003939 unsigned blocksize, length, pos;
3940 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941 struct inode *inode = mapping->host;
3942 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003943 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003944 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003945
Theodore Ts'of4a01012009-07-05 22:08:16 -04003946 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3947 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003948 if (!page)
3949 return -EINVAL;
3950
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003951 blocksize = inode->i_sb->s_blocksize;
3952 length = blocksize - (offset & (blocksize - 1));
3953 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3954
3955 /*
3956 * For "nobh" option, we can only work if we don't need to
3957 * read-in the page - otherwise we create buffers to do the IO.
3958 */
3959 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003960 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003961 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003962 set_page_dirty(page);
3963 goto unlock;
3964 }
3965
3966 if (!page_has_buffers(page))
3967 create_empty_buffers(page, blocksize, 0);
3968
3969 /* Find the buffer that contains "offset" */
3970 bh = page_buffers(page);
3971 pos = blocksize;
3972 while (offset >= pos) {
3973 bh = bh->b_this_page;
3974 iblock++;
3975 pos += blocksize;
3976 }
3977
3978 err = 0;
3979 if (buffer_freed(bh)) {
3980 BUFFER_TRACE(bh, "freed: skip");
3981 goto unlock;
3982 }
3983
3984 if (!buffer_mapped(bh)) {
3985 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003986 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003987 /* unmapped? It's a hole - nothing to do */
3988 if (!buffer_mapped(bh)) {
3989 BUFFER_TRACE(bh, "still unmapped");
3990 goto unlock;
3991 }
3992 }
3993
3994 /* Ok, it's mapped. Make sure it's up-to-date */
3995 if (PageUptodate(page))
3996 set_buffer_uptodate(bh);
3997
3998 if (!buffer_uptodate(bh)) {
3999 err = -EIO;
4000 ll_rw_block(READ, 1, &bh);
4001 wait_on_buffer(bh);
4002 /* Uhhuh. Read error. Complain and punt. */
4003 if (!buffer_uptodate(bh))
4004 goto unlock;
4005 }
4006
Mingming Cao617ba132006-10-11 01:20:53 -07004007 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004008 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07004009 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004010 if (err)
4011 goto unlock;
4012 }
4013
Christoph Lametereebd2aa2008-02-04 22:28:29 -08004014 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004015
4016 BUFFER_TRACE(bh, "zeroed end of block");
4017
4018 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07004019 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05004020 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004021 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004022 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04004023 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004024 mark_buffer_dirty(bh);
4025 }
4026
4027unlock:
4028 unlock_page(page);
4029 page_cache_release(page);
4030 return err;
4031}
4032
4033/*
4034 * Probably it should be a library function... search for first non-zero word
4035 * or memcmp with zero_page, whatever is better for particular architecture.
4036 * Linus?
4037 */
4038static inline int all_zeroes(__le32 *p, __le32 *q)
4039{
4040 while (p < q)
4041 if (*p++)
4042 return 0;
4043 return 1;
4044}
4045
4046/**
Mingming Cao617ba132006-10-11 01:20:53 -07004047 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004048 * @inode: inode in question
4049 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07004050 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004051 * @chain: place to store the pointers to partial indirect blocks
4052 * @top: place to the (detached) top of branch
4053 *
Mingming Cao617ba132006-10-11 01:20:53 -07004054 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004055 *
4056 * When we do truncate() we may have to clean the ends of several
4057 * indirect blocks but leave the blocks themselves alive. Block is
4058 * partially truncated if some data below the new i_size is refered
4059 * from it (and it is on the path to the first completely truncated
4060 * data block, indeed). We have to free the top of that path along
4061 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07004062 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004063 * finishes, we may safely do the latter, but top of branch may
4064 * require special attention - pageout below the truncation point
4065 * might try to populate it.
4066 *
4067 * We atomically detach the top of branch from the tree, store the
4068 * block number of its root in *@top, pointers to buffer_heads of
4069 * partially truncated blocks - in @chain[].bh and pointers to
4070 * their last elements that should not be removed - in
4071 * @chain[].p. Return value is the pointer to last filled element
4072 * of @chain.
4073 *
4074 * The work left to caller to do the actual freeing of subtrees:
4075 * a) free the subtree starting from *@top
4076 * b) free the subtrees whose roots are stored in
4077 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
4078 * c) free the subtrees growing from the inode past the @chain[0].
4079 * (no partially truncated stuff there). */
4080
Mingming Cao617ba132006-10-11 01:20:53 -07004081static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004082 ext4_lblk_t offsets[4], Indirect chain[4],
4083 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004084{
4085 Indirect *partial, *p;
4086 int k, err;
4087
4088 *top = 0;
Uwe Kleine-Königbf48aab2009-10-28 20:11:03 +01004089 /* Make k index the deepest non-null offset + 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004090 for (k = depth; k > 1 && !offsets[k-1]; k--)
4091 ;
Mingming Cao617ba132006-10-11 01:20:53 -07004092 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004093 /* Writer: pointers */
4094 if (!partial)
4095 partial = chain + k-1;
4096 /*
4097 * If the branch acquired continuation since we've looked at it -
4098 * fine, it should all survive and (new) top doesn't belong to us.
4099 */
4100 if (!partial->key && *partial->p)
4101 /* Writer: end */
4102 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004103 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104 ;
4105 /*
4106 * OK, we've found the last block that must survive. The rest of our
4107 * branch should be detached before unlocking. However, if that rest
4108 * of branch is all ours and does not grow immediately from the inode
4109 * it's easier to cheat and just decrement partial->p.
4110 */
4111 if (p == chain + k - 1 && p > chain) {
4112 p->p--;
4113 } else {
4114 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07004115 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004116#if 0
4117 *p->p = 0;
4118#endif
4119 }
4120 /* Writer: end */
4121
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004122 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004123 brelse(partial->bh);
4124 partial--;
4125 }
4126no_top:
4127 return partial;
4128}
4129
4130/*
4131 * Zero a number of block pointers in either an inode or an indirect block.
4132 * If we restart the transaction we must again get write access to the
4133 * indirect block for further modification.
4134 *
4135 * We release `count' blocks on disk, but (last - first) may be greater
4136 * than `count' because there can be holes in there.
4137 */
Mingming Cao617ba132006-10-11 01:20:53 -07004138static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004139 struct buffer_head *bh,
4140 ext4_fsblk_t block_to_free,
4141 unsigned long count, __le32 *first,
4142 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004143{
4144 __le32 *p;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004145 int flags = EXT4_FREE_BLOCKS_FORGET;
4146
4147 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4148 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o50689692009-11-23 07:17:34 -05004149
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004150 if (try_to_extend_transaction(handle, inode)) {
4151 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004152 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4153 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004154 }
Mingming Cao617ba132006-10-11 01:20:53 -07004155 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04004156 ext4_truncate_restart_trans(handle, inode,
4157 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004158 if (bh) {
4159 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07004160 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004161 }
4162 }
4163
Theodore Ts'oe6362602009-11-23 07:17:05 -05004164 for (p = first; p < last; p++)
4165 *p = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004166
Theodore Ts'oe6362602009-11-23 07:17:05 -05004167 ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004168}
4169
4170/**
Mingming Cao617ba132006-10-11 01:20:53 -07004171 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004172 * @handle: handle for this transaction
4173 * @inode: inode we are dealing with
4174 * @this_bh: indirect buffer_head which contains *@first and *@last
4175 * @first: array of block numbers
4176 * @last: points immediately past the end of array
4177 *
4178 * We are freeing all blocks refered from that array (numbers are stored as
4179 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
4180 *
4181 * We accumulate contiguous runs of blocks to free. Conveniently, if these
4182 * blocks are contiguous then releasing them at one time will only affect one
4183 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
4184 * actually use a lot of journal space.
4185 *
4186 * @this_bh will be %NULL if @first and @last point into the inode's direct
4187 * block pointers.
4188 */
Mingming Cao617ba132006-10-11 01:20:53 -07004189static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004190 struct buffer_head *this_bh,
4191 __le32 *first, __le32 *last)
4192{
Mingming Cao617ba132006-10-11 01:20:53 -07004193 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004194 unsigned long count = 0; /* Number of blocks in the run */
4195 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
4196 corresponding to
4197 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07004198 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004199 __le32 *p; /* Pointer into inode/ind
4200 for current block */
4201 int err;
4202
4203 if (this_bh) { /* For indirect block */
4204 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004205 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004206 /* Important: if we can't update the indirect pointers
4207 * to the blocks, we can't free them. */
4208 if (err)
4209 return;
4210 }
4211
4212 for (p = first; p < last; p++) {
4213 nr = le32_to_cpu(*p);
4214 if (nr) {
4215 /* accumulate blocks to free if they're contiguous */
4216 if (count == 0) {
4217 block_to_free = nr;
4218 block_to_free_p = p;
4219 count = 1;
4220 } else if (nr == block_to_free + count) {
4221 count++;
4222 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004223 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004224 block_to_free,
4225 count, block_to_free_p, p);
4226 block_to_free = nr;
4227 block_to_free_p = p;
4228 count = 1;
4229 }
4230 }
4231 }
4232
4233 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07004234 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004235 count, block_to_free_p, p);
4236
4237 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004238 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004239
4240 /*
4241 * The buffer head should have an attached journal head at this
4242 * point. However, if the data is corrupted and an indirect
4243 * block pointed to itself, it would have been detached when
4244 * the block was cleared. Check for this instead of OOPSing.
4245 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05004246 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05004247 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004248 else
4249 ext4_error(inode->i_sb, __func__,
4250 "circular indirect block detected, "
4251 "inode=%lu, block=%llu",
4252 inode->i_ino,
4253 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004254 }
4255}
4256
4257/**
Mingming Cao617ba132006-10-11 01:20:53 -07004258 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004259 * @handle: JBD handle for this transaction
4260 * @inode: inode we are dealing with
4261 * @parent_bh: the buffer_head which contains *@first and *@last
4262 * @first: array of block numbers
4263 * @last: pointer immediately past the end of array
4264 * @depth: depth of the branches to free
4265 *
4266 * We are freeing all blocks refered from these branches (numbers are
4267 * stored as little-endian 32-bit) and updating @inode->i_blocks
4268 * appropriately.
4269 */
Mingming Cao617ba132006-10-11 01:20:53 -07004270static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004271 struct buffer_head *parent_bh,
4272 __le32 *first, __le32 *last, int depth)
4273{
Mingming Cao617ba132006-10-11 01:20:53 -07004274 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004275 __le32 *p;
4276
Frank Mayhar03901312009-01-07 00:06:22 -05004277 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004278 return;
4279
4280 if (depth--) {
4281 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004282 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004283 p = last;
4284 while (--p >= first) {
4285 nr = le32_to_cpu(*p);
4286 if (!nr)
4287 continue; /* A hole */
4288
4289 /* Go read the buffer for the next level down */
4290 bh = sb_bread(inode->i_sb, nr);
4291
4292 /*
4293 * A read failure? Report error and clear slot
4294 * (should be rare).
4295 */
4296 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07004297 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07004298 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004299 inode->i_ino, nr);
4300 continue;
4301 }
4302
4303 /* This zaps the entire block. Bottom up. */
4304 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07004305 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004306 (__le32 *) bh->b_data,
4307 (__le32 *) bh->b_data + addr_per_block,
4308 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004309
4310 /*
4311 * We've probably journalled the indirect block several
4312 * times during the truncate. But it's no longer
4313 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07004314 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004315 *
4316 * That's easy if it's exclusively part of this
4317 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07004318 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004319 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07004320 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004321 * unmap_underlying_metadata() will find this block
4322 * and will try to get rid of it. damn, damn.
4323 *
4324 * If this block has already been committed to the
4325 * journal, a revoke record will be written. And
4326 * revoke records must be emitted *before* clearing
4327 * this block's bit in the bitmaps.
4328 */
Mingming Cao617ba132006-10-11 01:20:53 -07004329 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004330
4331 /*
4332 * Everything below this this pointer has been
4333 * released. Now let this top-of-subtree go.
4334 *
4335 * We want the freeing of this indirect block to be
4336 * atomic in the journal with the updating of the
4337 * bitmap block which owns it. So make some room in
4338 * the journal.
4339 *
4340 * We zero the parent pointer *after* freeing its
4341 * pointee in the bitmaps, so if extend_transaction()
4342 * for some reason fails to put the bitmap changes and
4343 * the release into the same transaction, recovery
4344 * will merely complain about releasing a free block,
4345 * rather than leaking blocks.
4346 */
Frank Mayhar03901312009-01-07 00:06:22 -05004347 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004348 return;
4349 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004350 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04004351 ext4_truncate_restart_trans(handle, inode,
4352 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004353 }
4354
Theodore Ts'oe6362602009-11-23 07:17:05 -05004355 ext4_free_blocks(handle, inode, 0, nr, 1,
4356 EXT4_FREE_BLOCKS_METADATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004357
4358 if (parent_bh) {
4359 /*
4360 * The block which we have just freed is
4361 * pointed to by an indirect block: journal it
4362 */
4363 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004364 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365 parent_bh)){
4366 *p = 0;
4367 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05004368 "call ext4_handle_dirty_metadata");
4369 ext4_handle_dirty_metadata(handle,
4370 inode,
4371 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004372 }
4373 }
4374 }
4375 } else {
4376 /* We have reached the bottom of the tree. */
4377 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07004378 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004379 }
4380}
4381
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004382int ext4_can_truncate(struct inode *inode)
4383{
4384 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4385 return 0;
4386 if (S_ISREG(inode->i_mode))
4387 return 1;
4388 if (S_ISDIR(inode->i_mode))
4389 return 1;
4390 if (S_ISLNK(inode->i_mode))
4391 return !ext4_inode_is_fast_symlink(inode);
4392 return 0;
4393}
4394
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004395/*
Mingming Cao617ba132006-10-11 01:20:53 -07004396 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004397 *
Mingming Cao617ba132006-10-11 01:20:53 -07004398 * We block out ext4_get_block() block instantiations across the entire
4399 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004400 * simultaneously on behalf of the same inode.
4401 *
4402 * As we work through the truncate and commmit bits of it to the journal there
4403 * is one core, guiding principle: the file's tree must always be consistent on
4404 * disk. We must be able to restart the truncate after a crash.
4405 *
4406 * The file's tree may be transiently inconsistent in memory (although it
4407 * probably isn't), but whenever we close off and commit a journal transaction,
4408 * the contents of (the filesystem + the journal) must be consistent and
4409 * restartable. It's pretty simple, really: bottom up, right to left (although
4410 * left-to-right works OK too).
4411 *
4412 * Note that at recovery time, journal replay occurs *before* the restart of
4413 * truncate against the orphan inode list.
4414 *
4415 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004416 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004417 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004418 * ext4_truncate() to have another go. So there will be instantiated blocks
4419 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004420 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004421 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004422 */
Mingming Cao617ba132006-10-11 01:20:53 -07004423void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004424{
4425 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07004426 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004427 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004428 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004429 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004430 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004431 Indirect chain[4];
4432 Indirect *partial;
4433 __le32 nr = 0;
4434 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004435 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004436 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004437
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004438 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004439 return;
4440
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004441 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004442 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
4443
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004444 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04004445 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004446 return;
4447 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004448
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004449 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04004450 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004451 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004452
4453 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07004454 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004455
Jan Karacf108bc2008-07-11 19:27:31 -04004456 if (inode->i_size & (blocksize - 1))
4457 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
4458 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004459
Mingming Cao617ba132006-10-11 01:20:53 -07004460 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004461 if (n == 0)
4462 goto out_stop; /* error */
4463
4464 /*
4465 * OK. This truncate is going to happen. We add the inode to the
4466 * orphan list, so that if this truncate spans multiple transactions,
4467 * and we crash, we will resume the truncate when the filesystem
4468 * recovers. It also marks the inode dirty, to catch the new size.
4469 *
4470 * Implication: the file must always be in a sane, consistent
4471 * truncatable state while each transaction commits.
4472 */
Mingming Cao617ba132006-10-11 01:20:53 -07004473 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004474 goto out_stop;
4475
4476 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004477 * From here we block out all ext4_get_block() callers who want to
4478 * modify the block allocation tree.
4479 */
4480 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004481
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004482 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004483
Mingming Cao632eaea2008-07-11 19:27:31 -04004484 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004485 * The orphan list entry will now protect us from any crash which
4486 * occurs before the truncate completes, so it is now safe to propagate
4487 * the new, shorter inode size (held for now in i_size) into the
4488 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004489 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004490 */
4491 ei->i_disksize = inode->i_size;
4492
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004493 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004494 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4495 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004496 goto do_indirects;
4497 }
4498
Mingming Cao617ba132006-10-11 01:20:53 -07004499 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004500 /* Kill the top of shared branch (not detached) */
4501 if (nr) {
4502 if (partial == chain) {
4503 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004504 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004505 &nr, &nr+1, (chain+n-1) - partial);
4506 *partial->p = 0;
4507 /*
4508 * We mark the inode dirty prior to restart,
4509 * and prior to stop. No need for it here.
4510 */
4511 } else {
4512 /* Shared branch grows from an indirect block */
4513 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004514 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004515 partial->p,
4516 partial->p+1, (chain+n-1) - partial);
4517 }
4518 }
4519 /* Clear the ends of indirect blocks on the shared branch */
4520 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004521 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004522 (__le32*)partial->bh->b_data+addr_per_block,
4523 (chain+n-1) - partial);
4524 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004525 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004526 partial--;
4527 }
4528do_indirects:
4529 /* Kill the remaining (whole) subtrees */
4530 switch (offsets[0]) {
4531 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004532 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004533 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004534 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4535 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004536 }
Mingming Cao617ba132006-10-11 01:20:53 -07004537 case EXT4_IND_BLOCK:
4538 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004539 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004540 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4541 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004542 }
Mingming Cao617ba132006-10-11 01:20:53 -07004543 case EXT4_DIND_BLOCK:
4544 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004545 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004546 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4547 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548 }
Mingming Cao617ba132006-10-11 01:20:53 -07004549 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004550 ;
4551 }
4552
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004553 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004554 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004555 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004556
4557 /*
4558 * In a multi-transaction truncate, we only make the final transaction
4559 * synchronous
4560 */
4561 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004562 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004563out_stop:
4564 /*
4565 * If this was a simple ftruncate(), and the file will remain alive
4566 * then we need to clear up the orphan record which we created above.
4567 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004568 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004569 * orphan info for us.
4570 */
4571 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004572 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004573
Mingming Cao617ba132006-10-11 01:20:53 -07004574 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004575}
4576
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004577/*
Mingming Cao617ba132006-10-11 01:20:53 -07004578 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004579 * underlying buffer_head on success. If 'in_mem' is true, we have all
4580 * data in memory that is needed to recreate the on-disk version of this
4581 * inode.
4582 */
Mingming Cao617ba132006-10-11 01:20:53 -07004583static int __ext4_get_inode_loc(struct inode *inode,
4584 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004585{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004586 struct ext4_group_desc *gdp;
4587 struct buffer_head *bh;
4588 struct super_block *sb = inode->i_sb;
4589 ext4_fsblk_t block;
4590 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004591
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004592 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004593 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004594 return -EIO;
4595
Theodore Ts'o240799c2008-10-09 23:53:47 -04004596 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4597 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4598 if (!gdp)
4599 return -EIO;
4600
4601 /*
4602 * Figure out the offset within the block group inode table
4603 */
4604 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4605 inode_offset = ((inode->i_ino - 1) %
4606 EXT4_INODES_PER_GROUP(sb));
4607 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4608 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4609
4610 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004611 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004612 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4613 "inode block - inode=%lu, block=%llu",
4614 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004615 return -EIO;
4616 }
4617 if (!buffer_uptodate(bh)) {
4618 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004619
4620 /*
4621 * If the buffer has the write error flag, we have failed
4622 * to write out another inode in the same block. In this
4623 * case, we don't have to read the block because we may
4624 * read the old inode data successfully.
4625 */
4626 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4627 set_buffer_uptodate(bh);
4628
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004629 if (buffer_uptodate(bh)) {
4630 /* someone brought it uptodate while we waited */
4631 unlock_buffer(bh);
4632 goto has_buffer;
4633 }
4634
4635 /*
4636 * If we have all information of the inode in memory and this
4637 * is the only valid inode in the block, we need not read the
4638 * block.
4639 */
4640 if (in_mem) {
4641 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004642 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004643
Theodore Ts'o240799c2008-10-09 23:53:47 -04004644 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004645
4646 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004647 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004648 if (!bitmap_bh)
4649 goto make_io;
4650
4651 /*
4652 * If the inode bitmap isn't in cache then the
4653 * optimisation may end up performing two reads instead
4654 * of one, so skip it.
4655 */
4656 if (!buffer_uptodate(bitmap_bh)) {
4657 brelse(bitmap_bh);
4658 goto make_io;
4659 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004660 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004661 if (i == inode_offset)
4662 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004663 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004664 break;
4665 }
4666 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004667 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004668 /* all other inodes are free, so skip I/O */
4669 memset(bh->b_data, 0, bh->b_size);
4670 set_buffer_uptodate(bh);
4671 unlock_buffer(bh);
4672 goto has_buffer;
4673 }
4674 }
4675
4676make_io:
4677 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004678 * If we need to do any I/O, try to pre-readahead extra
4679 * blocks from the inode table.
4680 */
4681 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4682 ext4_fsblk_t b, end, table;
4683 unsigned num;
4684
4685 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004686 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004687 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4688 if (table > b)
4689 b = table;
4690 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4691 num = EXT4_INODES_PER_GROUP(sb);
4692 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4693 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004694 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004695 table += num / inodes_per_block;
4696 if (end > table)
4697 end = table;
4698 while (b <= end)
4699 sb_breadahead(sb, b++);
4700 }
4701
4702 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004703 * There are other valid inodes in the buffer, this inode
4704 * has in-inode xattrs, or we don't have this inode in memory.
4705 * Read the block from disk.
4706 */
4707 get_bh(bh);
4708 bh->b_end_io = end_buffer_read_sync;
4709 submit_bh(READ_META, bh);
4710 wait_on_buffer(bh);
4711 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004712 ext4_error(sb, __func__,
4713 "unable to read inode block - inode=%lu, "
4714 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004715 brelse(bh);
4716 return -EIO;
4717 }
4718 }
4719has_buffer:
4720 iloc->bh = bh;
4721 return 0;
4722}
4723
Mingming Cao617ba132006-10-11 01:20:53 -07004724int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004725{
4726 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004727 return __ext4_get_inode_loc(inode, iloc,
4728 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004729}
4730
Mingming Cao617ba132006-10-11 01:20:53 -07004731void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004732{
Mingming Cao617ba132006-10-11 01:20:53 -07004733 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004734
4735 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004736 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004737 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004738 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004739 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004740 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004741 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004742 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004743 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004744 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004745 inode->i_flags |= S_DIRSYNC;
4746}
4747
Jan Karaff9ddf72007-07-18 09:24:20 -04004748/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4749void ext4_get_inode_flags(struct ext4_inode_info *ei)
4750{
4751 unsigned int flags = ei->vfs_inode.i_flags;
4752
4753 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4754 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4755 if (flags & S_SYNC)
4756 ei->i_flags |= EXT4_SYNC_FL;
4757 if (flags & S_APPEND)
4758 ei->i_flags |= EXT4_APPEND_FL;
4759 if (flags & S_IMMUTABLE)
4760 ei->i_flags |= EXT4_IMMUTABLE_FL;
4761 if (flags & S_NOATIME)
4762 ei->i_flags |= EXT4_NOATIME_FL;
4763 if (flags & S_DIRSYNC)
4764 ei->i_flags |= EXT4_DIRSYNC_FL;
4765}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004766
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004767static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004768 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004769{
4770 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004771 struct inode *inode = &(ei->vfs_inode);
4772 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004773
4774 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4775 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4776 /* we are using combined 48 bit field */
4777 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4778 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004779 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4780 /* i_blocks represent file system block size */
4781 return i_blocks << (inode->i_blkbits - 9);
4782 } else {
4783 return i_blocks;
4784 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004785 } else {
4786 return le32_to_cpu(raw_inode->i_blocks_lo);
4787 }
4788}
Jan Karaff9ddf72007-07-18 09:24:20 -04004789
David Howells1d1fe1e2008-02-07 00:15:37 -08004790struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004791{
Mingming Cao617ba132006-10-11 01:20:53 -07004792 struct ext4_iloc iloc;
4793 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004794 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004795 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004796 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004797 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004798 int block;
4799
David Howells1d1fe1e2008-02-07 00:15:37 -08004800 inode = iget_locked(sb, ino);
4801 if (!inode)
4802 return ERR_PTR(-ENOMEM);
4803 if (!(inode->i_state & I_NEW))
4804 return inode;
4805
4806 ei = EXT4_I(inode);
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004807 iloc.bh = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004808
David Howells1d1fe1e2008-02-07 00:15:37 -08004809 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4810 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004811 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004812 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004813 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4814 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4815 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004816 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4818 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4819 }
4820 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004821
4822 ei->i_state = 0;
4823 ei->i_dir_start_lookup = 0;
4824 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4825 /* We now have enough fields to check if the inode was active or not.
4826 * This is needed because nfsd might try to access dead inodes
4827 * the test is that same one that e2fsck uses
4828 * NeilBrown 1999oct15
4829 */
4830 if (inode->i_nlink == 0) {
4831 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004832 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004833 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004834 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004835 goto bad_inode;
4836 }
4837 /* The only unlinked inodes we let through here have
4838 * valid i_mode and are being read by the orphan
4839 * recovery code: that's fine, we're about to complete
4840 * the process of deleting those. */
4841 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004842 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004843 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004844 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004845 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004846 ei->i_file_acl |=
4847 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004848 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004849 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004850#ifdef CONFIG_QUOTA
4851 ei->i_reserved_quota = 0;
4852#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004853 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4854 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004855 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004856 /*
4857 * NOTE! The in-memory inode i_data array is in little-endian order
4858 * even on big-endian machines: we do NOT byteswap the block numbers!
4859 */
Mingming Cao617ba132006-10-11 01:20:53 -07004860 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861 ei->i_data[block] = raw_inode->i_block[block];
4862 INIT_LIST_HEAD(&ei->i_orphan);
4863
Jan Karab436b9b2009-12-08 23:51:10 -05004864 /*
4865 * Set transaction id's of transactions that have to be committed
4866 * to finish f[data]sync. We set them to currently running transaction
4867 * as we cannot be sure that the inode or some of its metadata isn't
4868 * part of the transaction - the inode could have been reclaimed and
4869 * now it is reread from disk.
4870 */
4871 if (journal) {
4872 transaction_t *transaction;
4873 tid_t tid;
4874
4875 spin_lock(&journal->j_state_lock);
4876 if (journal->j_running_transaction)
4877 transaction = journal->j_running_transaction;
4878 else
4879 transaction = journal->j_committing_transaction;
4880 if (transaction)
4881 tid = transaction->t_tid;
4882 else
4883 tid = journal->j_commit_sequence;
4884 spin_unlock(&journal->j_state_lock);
4885 ei->i_sync_tid = tid;
4886 ei->i_datasync_tid = tid;
4887 }
4888
Eric Sandeen0040d982008-02-05 22:36:43 -05004889 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004890 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004891 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004892 EXT4_INODE_SIZE(inode->i_sb)) {
David Howells1d1fe1e2008-02-07 00:15:37 -08004893 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004894 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004895 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004896 if (ei->i_extra_isize == 0) {
4897 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004898 ei->i_extra_isize = sizeof(struct ext4_inode) -
4899 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004900 } else {
4901 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004902 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004903 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004904 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004905 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004906 }
4907 } else
4908 ei->i_extra_isize = 0;
4909
Kalpak Shahef7f3832007-07-18 09:15:20 -04004910 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4911 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4912 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4913 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4914
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004915 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4916 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4917 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4918 inode->i_version |=
4919 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4920 }
4921
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004922 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004923 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004924 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004925 ext4_error(sb, __func__,
4926 "bad extended attribute block %llu in inode #%lu",
4927 ei->i_file_acl, inode->i_ino);
4928 ret = -EIO;
4929 goto bad_inode;
4930 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004931 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4932 (S_ISLNK(inode->i_mode) &&
4933 !ext4_inode_is_fast_symlink(inode)))
4934 /* Validate extent which is part of inode */
4935 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004936 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004937 (S_ISLNK(inode->i_mode) &&
4938 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004939 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004940 ret = ext4_check_inode_blockref(inode);
4941 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004942 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004943 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004944
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004945 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004946 inode->i_op = &ext4_file_inode_operations;
4947 inode->i_fop = &ext4_file_operations;
4948 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004949 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004950 inode->i_op = &ext4_dir_inode_operations;
4951 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004952 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004953 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004954 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004955 nd_terminate_link(ei->i_data, inode->i_size,
4956 sizeof(ei->i_data) - 1);
4957 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004958 inode->i_op = &ext4_symlink_inode_operations;
4959 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004960 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004961 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4962 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004963 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004964 if (raw_inode->i_block[0])
4965 init_special_inode(inode, inode->i_mode,
4966 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4967 else
4968 init_special_inode(inode, inode->i_mode,
4969 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004970 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004971 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004972 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004973 "bogus i_mode (%o) for inode=%lu",
4974 inode->i_mode, inode->i_ino);
4975 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004976 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004977 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004978 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004979 unlock_new_inode(inode);
4980 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004981
4982bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004983 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004984 iget_failed(inode);
4985 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004986}
4987
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004988static int ext4_inode_blocks_set(handle_t *handle,
4989 struct ext4_inode *raw_inode,
4990 struct ext4_inode_info *ei)
4991{
4992 struct inode *inode = &(ei->vfs_inode);
4993 u64 i_blocks = inode->i_blocks;
4994 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004995
4996 if (i_blocks <= ~0U) {
4997 /*
4998 * i_blocks can be represnted in a 32 bit variable
4999 * as multiple of 512 bytes
5000 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005001 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005002 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005003 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04005004 return 0;
5005 }
5006 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
5007 return -EFBIG;
5008
5009 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005010 /*
5011 * i_blocks can be represented in a 48 bit variable
5012 * as multiple of 512 bytes
5013 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005014 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005015 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005016 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005017 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005018 ei->i_flags |= EXT4_HUGE_FILE_FL;
5019 /* i_block is stored in file system block size */
5020 i_blocks = i_blocks >> (inode->i_blkbits - 9);
5021 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
5022 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005023 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04005024 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005025}
5026
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005027/*
5028 * Post the struct inode info into an on-disk inode location in the
5029 * buffer-cache. This gobbles the caller's reference to the
5030 * buffer_head in the inode location struct.
5031 *
5032 * The caller must have write access to iloc->bh.
5033 */
Mingming Cao617ba132006-10-11 01:20:53 -07005034static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005035 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04005036 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005037{
Mingming Cao617ba132006-10-11 01:20:53 -07005038 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
5039 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005040 struct buffer_head *bh = iloc->bh;
5041 int err = 0, rc, block;
5042
5043 /* For fields not not tracking in the in-memory inode,
5044 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07005045 if (ei->i_state & EXT4_STATE_NEW)
5046 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005047
Jan Karaff9ddf72007-07-18 09:24:20 -04005048 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005049 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005050 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005051 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
5052 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
5053/*
5054 * Fix up interoperability with old kernels. Otherwise, old inodes get
5055 * re-used with the upper 16 bits of the uid/gid intact
5056 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005057 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005058 raw_inode->i_uid_high =
5059 cpu_to_le16(high_16_bits(inode->i_uid));
5060 raw_inode->i_gid_high =
5061 cpu_to_le16(high_16_bits(inode->i_gid));
5062 } else {
5063 raw_inode->i_uid_high = 0;
5064 raw_inode->i_gid_high = 0;
5065 }
5066 } else {
5067 raw_inode->i_uid_low =
5068 cpu_to_le16(fs_high2lowuid(inode->i_uid));
5069 raw_inode->i_gid_low =
5070 cpu_to_le16(fs_high2lowgid(inode->i_gid));
5071 raw_inode->i_uid_high = 0;
5072 raw_inode->i_gid_high = 0;
5073 }
5074 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04005075
5076 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5077 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5078 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5079 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5080
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005081 if (ext4_inode_blocks_set(handle, raw_inode, ei))
5082 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005083 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o1b9c12f2009-09-17 08:32:22 -04005084 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07005085 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
5086 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005087 raw_inode->i_file_acl_high =
5088 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005089 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005090 ext4_isize_set(raw_inode, ei->i_disksize);
5091 if (ei->i_disksize > 0x7fffffffULL) {
5092 struct super_block *sb = inode->i_sb;
5093 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
5094 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
5095 EXT4_SB(sb)->s_es->s_rev_level ==
5096 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
5097 /* If this is the first large file
5098 * created, add a flag to the superblock.
5099 */
5100 err = ext4_journal_get_write_access(handle,
5101 EXT4_SB(sb)->s_sbh);
5102 if (err)
5103 goto out_brelse;
5104 ext4_update_dynamic_rev(sb);
5105 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07005106 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005107 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05005108 ext4_handle_sync(handle);
5109 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005110 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005111 }
5112 }
5113 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5114 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5115 if (old_valid_dev(inode->i_rdev)) {
5116 raw_inode->i_block[0] =
5117 cpu_to_le32(old_encode_dev(inode->i_rdev));
5118 raw_inode->i_block[1] = 0;
5119 } else {
5120 raw_inode->i_block[0] = 0;
5121 raw_inode->i_block[1] =
5122 cpu_to_le32(new_encode_dev(inode->i_rdev));
5123 raw_inode->i_block[2] = 0;
5124 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005125 } else
5126 for (block = 0; block < EXT4_N_BLOCKS; block++)
5127 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005128
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005129 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
5130 if (ei->i_extra_isize) {
5131 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5132 raw_inode->i_version_hi =
5133 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005134 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005135 }
5136
Frank Mayhar830156c2009-09-29 10:07:47 -04005137 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
5138 rc = ext4_handle_dirty_metadata(handle, inode, bh);
5139 if (!err)
5140 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07005141 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005142
Jan Karab436b9b2009-12-08 23:51:10 -05005143 ext4_update_inode_fsync_trans(handle, inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005144out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005145 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005146 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005147 return err;
5148}
5149
5150/*
Mingming Cao617ba132006-10-11 01:20:53 -07005151 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005152 *
5153 * We are called from a few places:
5154 *
5155 * - Within generic_file_write() for O_SYNC files.
5156 * Here, there will be no transaction running. We wait for any running
5157 * trasnaction to commit.
5158 *
5159 * - Within sys_sync(), kupdate and such.
5160 * We wait on commit, if tol to.
5161 *
5162 * - Within prune_icache() (PF_MEMALLOC == true)
5163 * Here we simply return. We can't afford to block kswapd on the
5164 * journal commit.
5165 *
5166 * In all cases it is actually safe for us to return without doing anything,
5167 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07005168 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005169 * knfsd.
5170 *
5171 * Note that we are absolutely dependent upon all inode dirtiers doing the
5172 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5173 * which we are interested.
5174 *
5175 * It would be a bug for them to not do this. The code:
5176 *
5177 * mark_inode_dirty(inode)
5178 * stuff();
5179 * inode->i_size = expr;
5180 *
5181 * is in error because a kswapd-driven write_inode() could occur while
5182 * `stuff()' is running, and the new i_size will be lost. Plus the inode
5183 * will no longer be on the superblock's dirty inode list.
5184 */
Mingming Cao617ba132006-10-11 01:20:53 -07005185int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005186{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005187 int err;
5188
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005189 if (current->flags & PF_MEMALLOC)
5190 return 0;
5191
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005192 if (EXT4_SB(inode->i_sb)->s_journal) {
5193 if (ext4_journal_current_handle()) {
5194 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5195 dump_stack();
5196 return -EIO;
5197 }
5198
5199 if (!wait)
5200 return 0;
5201
5202 err = ext4_force_commit(inode->i_sb);
5203 } else {
5204 struct ext4_iloc iloc;
5205
5206 err = ext4_get_inode_loc(inode, &iloc);
5207 if (err)
5208 return err;
Frank Mayhar830156c2009-09-29 10:07:47 -04005209 if (wait)
5210 sync_dirty_buffer(iloc.bh);
5211 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
5212 ext4_error(inode->i_sb, __func__,
5213 "IO error syncing inode, "
5214 "inode=%lu, block=%llu",
5215 inode->i_ino,
5216 (unsigned long long)iloc.bh->b_blocknr);
5217 err = -EIO;
5218 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005219 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005220 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005221}
5222
5223/*
Mingming Cao617ba132006-10-11 01:20:53 -07005224 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005225 *
5226 * Called from notify_change.
5227 *
5228 * We want to trap VFS attempts to truncate the file as soon as
5229 * possible. In particular, we want to make sure that when the VFS
5230 * shrinks i_size, we put the inode on the orphan list and modify
5231 * i_disksize immediately, so that during the subsequent flushing of
5232 * dirty pages and freeing of disk blocks, we can guarantee that any
5233 * commit will leave the blocks being flushed in an unused state on
5234 * disk. (On recovery, the inode will get truncated and the blocks will
5235 * be freed, so we have a strong guarantee that no future commit will
5236 * leave these blocks visible to the user.)
5237 *
Jan Kara678aaf42008-07-11 19:27:31 -04005238 * Another thing we have to assure is that if we are in ordered mode
5239 * and inode is still attached to the committing transaction, we must
5240 * we start writeout of all the dirty pages which are being truncated.
5241 * This way we are sure that all the data written in the previous
5242 * transaction are already on disk (truncate waits for pages under
5243 * writeback).
5244 *
5245 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005246 */
Mingming Cao617ba132006-10-11 01:20:53 -07005247int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005248{
5249 struct inode *inode = dentry->d_inode;
5250 int error, rc = 0;
5251 const unsigned int ia_valid = attr->ia_valid;
5252
5253 error = inode_change_ok(inode, attr);
5254 if (error)
5255 return error;
5256
Christoph Hellwig907f4552010-03-03 09:05:06 -05005257 if (ia_valid & ATTR_SIZE)
Christoph Hellwig871a2932010-03-03 09:05:07 -05005258 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005259 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
5260 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
5261 handle_t *handle;
5262
5263 /* (user+group)*(old+new) structure, inode write (sb,
5264 * inode block, ? - but truncate inode update has it) */
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05005265 handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
Dmitry Monakhov194074a2009-12-08 22:42:28 -05005266 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005267 if (IS_ERR(handle)) {
5268 error = PTR_ERR(handle);
5269 goto err_out;
5270 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005271 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005272 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005273 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005274 return error;
5275 }
5276 /* Update corresponding info in inode so that everything is in
5277 * one transaction */
5278 if (attr->ia_valid & ATTR_UID)
5279 inode->i_uid = attr->ia_uid;
5280 if (attr->ia_valid & ATTR_GID)
5281 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005282 error = ext4_mark_inode_dirty(handle, inode);
5283 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005284 }
5285
Eric Sandeene2b46572008-01-28 23:58:27 -05005286 if (attr->ia_valid & ATTR_SIZE) {
5287 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
5288 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5289
5290 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
5291 error = -EFBIG;
5292 goto err_out;
5293 }
5294 }
5295 }
5296
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005297 if (S_ISREG(inode->i_mode) &&
5298 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
5299 handle_t *handle;
5300
Mingming Cao617ba132006-10-11 01:20:53 -07005301 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005302 if (IS_ERR(handle)) {
5303 error = PTR_ERR(handle);
5304 goto err_out;
5305 }
5306
Mingming Cao617ba132006-10-11 01:20:53 -07005307 error = ext4_orphan_add(handle, inode);
5308 EXT4_I(inode)->i_disksize = attr->ia_size;
5309 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005310 if (!error)
5311 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07005312 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04005313
5314 if (ext4_should_order_data(inode)) {
5315 error = ext4_begin_ordered_truncate(inode,
5316 attr->ia_size);
5317 if (error) {
5318 /* Do as much error cleanup as possible */
5319 handle = ext4_journal_start(inode, 3);
5320 if (IS_ERR(handle)) {
5321 ext4_orphan_del(NULL, inode);
5322 goto err_out;
5323 }
5324 ext4_orphan_del(handle, inode);
5325 ext4_journal_stop(handle);
5326 goto err_out;
5327 }
5328 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005329 }
5330
5331 rc = inode_setattr(inode, attr);
5332
Mingming Cao617ba132006-10-11 01:20:53 -07005333 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005334 * transaction handle at all, we need to clean up the in-core
5335 * orphan list manually. */
5336 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005337 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005338
5339 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07005340 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005341
5342err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005343 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005344 if (!error)
5345 error = rc;
5346 return error;
5347}
5348
Mingming Cao3e3398a2008-07-11 19:27:31 -04005349int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5350 struct kstat *stat)
5351{
5352 struct inode *inode;
5353 unsigned long delalloc_blocks;
5354
5355 inode = dentry->d_inode;
5356 generic_fillattr(inode, stat);
5357
5358 /*
5359 * We can't update i_blocks if the block allocation is delayed
5360 * otherwise in the case of system crash before the real block
5361 * allocation is done, we will have i_blocks inconsistent with
5362 * on-disk file blocks.
5363 * We always keep i_blocks updated together with real
5364 * allocation. But to not confuse with user, stat
5365 * will return the blocks that include the delayed allocation
5366 * blocks for this file.
5367 */
5368 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
5369 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
5370 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
5371
5372 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
5373 return 0;
5374}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005375
Mingming Caoa02908f2008-08-19 22:16:07 -04005376static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
5377 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005378{
Mingming Caoa02908f2008-08-19 22:16:07 -04005379 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005380
Mingming Caoa02908f2008-08-19 22:16:07 -04005381 /* if nrblocks are contiguous */
5382 if (chunk) {
5383 /*
5384 * With N contiguous data blocks, it need at most
5385 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
5386 * 2 dindirect blocks
5387 * 1 tindirect block
5388 */
5389 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
5390 return indirects + 3;
5391 }
5392 /*
5393 * if nrblocks are not contiguous, worse case, each block touch
5394 * a indirect block, and each indirect block touch a double indirect
5395 * block, plus a triple indirect block
5396 */
5397 indirects = nrblocks * 2 + 1;
5398 return indirects;
5399}
Alex Tomasa86c6182006-10-11 01:21:03 -07005400
Mingming Caoa02908f2008-08-19 22:16:07 -04005401static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5402{
5403 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05005404 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
5405 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04005406}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005407
Mingming Caoa02908f2008-08-19 22:16:07 -04005408/*
5409 * Account for index blocks, block groups bitmaps and block group
5410 * descriptor blocks if modify datablocks and index blocks
5411 * worse case, the indexs blocks spread over different block groups
5412 *
5413 * If datablocks are discontiguous, they are possible to spread over
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005414 * different block groups too. If they are contiuguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005415 * they could still across block group boundary.
5416 *
5417 * Also account for superblock, inode, quota and xattr blocks
5418 */
5419int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5420{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005421 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5422 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005423 int idxblocks;
5424 int ret = 0;
5425
5426 /*
5427 * How many index blocks need to touch to modify nrblocks?
5428 * The "Chunk" flag indicating whether the nrblocks is
5429 * physically contiguous on disk
5430 *
5431 * For Direct IO and fallocate, they calls get_block to allocate
5432 * one single extent at a time, so they could set the "Chunk" flag
5433 */
5434 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
5435
5436 ret = idxblocks;
5437
5438 /*
5439 * Now let's see how many group bitmaps and group descriptors need
5440 * to account
5441 */
5442 groups = idxblocks;
5443 if (chunk)
5444 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005445 else
Mingming Caoa02908f2008-08-19 22:16:07 -04005446 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005447
Mingming Caoa02908f2008-08-19 22:16:07 -04005448 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005449 if (groups > ngroups)
5450 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005451 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5452 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5453
5454 /* bitmaps and block group descriptor blocks */
5455 ret += groups + gdpblocks;
5456
5457 /* Blocks for super block, inode, quota and xattr blocks */
5458 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005459
5460 return ret;
5461}
5462
5463/*
Mingming Caoa02908f2008-08-19 22:16:07 -04005464 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005465 * the modification of a single pages into a single transaction,
5466 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005467 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005468 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005469 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005470 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005471 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005472 */
5473int ext4_writepage_trans_blocks(struct inode *inode)
5474{
5475 int bpp = ext4_journal_blocks_per_page(inode);
5476 int ret;
5477
5478 ret = ext4_meta_trans_blocks(inode, bpp, 0);
5479
5480 /* Account for data blocks for journalled mode */
5481 if (ext4_should_journal_data(inode))
5482 ret += bpp;
5483 return ret;
5484}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005485
5486/*
5487 * Calculate the journal credits for a chunk of data modification.
5488 *
5489 * This is called from DIO, fallocate or whoever calling
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005490 * ext4_get_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005491 *
5492 * journal buffers for data blocks are not included here, as DIO
5493 * and fallocate do no need to journal data buffers.
5494 */
5495int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5496{
5497 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5498}
5499
Mingming Caoa02908f2008-08-19 22:16:07 -04005500/*
Mingming Cao617ba132006-10-11 01:20:53 -07005501 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005502 * Give this, we know that the caller already has write access to iloc->bh.
5503 */
Mingming Cao617ba132006-10-11 01:20:53 -07005504int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005505 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005506{
5507 int err = 0;
5508
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005509 if (test_opt(inode->i_sb, I_VERSION))
5510 inode_inc_iversion(inode);
5511
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005512 /* the do_update_inode consumes one bh->b_count */
5513 get_bh(iloc->bh);
5514
Mingming Caodab291a2006-10-11 01:21:01 -07005515 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005516 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005517 put_bh(iloc->bh);
5518 return err;
5519}
5520
5521/*
5522 * On success, We end up with an outstanding reference count against
5523 * iloc->bh. This _must_ be cleaned up later.
5524 */
5525
5526int
Mingming Cao617ba132006-10-11 01:20:53 -07005527ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5528 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005529{
Frank Mayhar03901312009-01-07 00:06:22 -05005530 int err;
5531
5532 err = ext4_get_inode_loc(inode, iloc);
5533 if (!err) {
5534 BUFFER_TRACE(iloc->bh, "get_write_access");
5535 err = ext4_journal_get_write_access(handle, iloc->bh);
5536 if (err) {
5537 brelse(iloc->bh);
5538 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005539 }
5540 }
Mingming Cao617ba132006-10-11 01:20:53 -07005541 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005542 return err;
5543}
5544
5545/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005546 * Expand an inode by new_extra_isize bytes.
5547 * Returns 0 on success or negative error number on failure.
5548 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005549static int ext4_expand_extra_isize(struct inode *inode,
5550 unsigned int new_extra_isize,
5551 struct ext4_iloc iloc,
5552 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005553{
5554 struct ext4_inode *raw_inode;
5555 struct ext4_xattr_ibody_header *header;
5556 struct ext4_xattr_entry *entry;
5557
5558 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5559 return 0;
5560
5561 raw_inode = ext4_raw_inode(&iloc);
5562
5563 header = IHDR(inode, raw_inode);
5564 entry = IFIRST(header);
5565
5566 /* No extended attributes present */
5567 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5568 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5569 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5570 new_extra_isize);
5571 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5572 return 0;
5573 }
5574
5575 /* try to expand with EAs present */
5576 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5577 raw_inode, handle);
5578}
5579
5580/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005581 * What we do here is to mark the in-core inode as clean with respect to inode
5582 * dirtiness (it may still be data-dirty).
5583 * This means that the in-core inode may be reaped by prune_icache
5584 * without having to perform any I/O. This is a very good thing,
5585 * because *any* task may call prune_icache - even ones which
5586 * have a transaction open against a different journal.
5587 *
5588 * Is this cheating? Not really. Sure, we haven't written the
5589 * inode out, but prune_icache isn't a user-visible syncing function.
5590 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5591 * we start and wait on commits.
5592 *
5593 * Is this efficient/effective? Well, we're being nice to the system
5594 * by cleaning up our inodes proactively so they can be reaped
5595 * without I/O. But we are potentially leaving up to five seconds'
5596 * worth of inodes floating about which prune_icache wants us to
5597 * write out. One way to fix that would be to get prune_icache()
5598 * to do a write_super() to free up some memory. It has the desired
5599 * effect.
5600 */
Mingming Cao617ba132006-10-11 01:20:53 -07005601int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005602{
Mingming Cao617ba132006-10-11 01:20:53 -07005603 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005604 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5605 static unsigned int mnt_count;
5606 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005607
5608 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005609 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005610 if (ext4_handle_valid(handle) &&
5611 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005612 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5613 /*
5614 * We need extra buffer credits since we may write into EA block
5615 * with this same handle. If journal_extend fails, then it will
5616 * only result in a minor loss of functionality for that inode.
5617 * If this is felt to be critical, then e2fsck should be run to
5618 * force a large enough s_min_extra_isize.
5619 */
5620 if ((jbd2_journal_extend(handle,
5621 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5622 ret = ext4_expand_extra_isize(inode,
5623 sbi->s_want_extra_isize,
5624 iloc, handle);
5625 if (ret) {
5626 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005627 if (mnt_count !=
5628 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005629 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005630 "Unable to expand inode %lu. Delete"
5631 " some EAs or run e2fsck.",
5632 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005633 mnt_count =
5634 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005635 }
5636 }
5637 }
5638 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005639 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005640 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005641 return err;
5642}
5643
5644/*
Mingming Cao617ba132006-10-11 01:20:53 -07005645 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005646 *
5647 * We're really interested in the case where a file is being extended.
5648 * i_size has been changed by generic_commit_write() and we thus need
5649 * to include the updated inode in the current transaction.
5650 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005651 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005652 * are allocated to the file.
5653 *
5654 * If the inode is marked synchronous, we don't honour that here - doing
5655 * so would cause a commit on atime updates, which we don't bother doing.
5656 * We handle synchronous inodes at the highest possible level.
5657 */
Mingming Cao617ba132006-10-11 01:20:53 -07005658void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005659{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005660 handle_t *handle;
5661
Mingming Cao617ba132006-10-11 01:20:53 -07005662 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005663 if (IS_ERR(handle))
5664 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005665
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005666 ext4_mark_inode_dirty(handle, inode);
5667
Mingming Cao617ba132006-10-11 01:20:53 -07005668 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005669out:
5670 return;
5671}
5672
5673#if 0
5674/*
5675 * Bind an inode's backing buffer_head into this transaction, to prevent
5676 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005677 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005678 * returns no iloc structure, so the caller needs to repeat the iloc
5679 * lookup to mark the inode dirty later.
5680 */
Mingming Cao617ba132006-10-11 01:20:53 -07005681static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005682{
Mingming Cao617ba132006-10-11 01:20:53 -07005683 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005684
5685 int err = 0;
5686 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005687 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005688 if (!err) {
5689 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005690 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005691 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005692 err = ext4_handle_dirty_metadata(handle,
5693 inode,
5694 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005695 brelse(iloc.bh);
5696 }
5697 }
Mingming Cao617ba132006-10-11 01:20:53 -07005698 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005699 return err;
5700}
5701#endif
5702
Mingming Cao617ba132006-10-11 01:20:53 -07005703int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005704{
5705 journal_t *journal;
5706 handle_t *handle;
5707 int err;
5708
5709 /*
5710 * We have to be very careful here: changing a data block's
5711 * journaling status dynamically is dangerous. If we write a
5712 * data block to the journal, change the status and then delete
5713 * that block, we risk forgetting to revoke the old log record
5714 * from the journal and so a subsequent replay can corrupt data.
5715 * So, first we make sure that the journal is empty and that
5716 * nobody is changing anything.
5717 */
5718
Mingming Cao617ba132006-10-11 01:20:53 -07005719 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005720 if (!journal)
5721 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005722 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005723 return -EROFS;
5724
Mingming Caodab291a2006-10-11 01:21:01 -07005725 jbd2_journal_lock_updates(journal);
5726 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005727
5728 /*
5729 * OK, there are no updates running now, and all cached data is
5730 * synced to disk. We are now in a completely consistent state
5731 * which doesn't have anything in the journal, and we know that
5732 * no filesystem updates are running, so it is safe to modify
5733 * the inode's in-core data-journaling state flag now.
5734 */
5735
5736 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005737 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005738 else
Mingming Cao617ba132006-10-11 01:20:53 -07005739 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5740 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005741
Mingming Caodab291a2006-10-11 01:21:01 -07005742 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005743
5744 /* Finally we can mark the inode as dirty. */
5745
Mingming Cao617ba132006-10-11 01:20:53 -07005746 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005747 if (IS_ERR(handle))
5748 return PTR_ERR(handle);
5749
Mingming Cao617ba132006-10-11 01:20:53 -07005750 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005751 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005752 ext4_journal_stop(handle);
5753 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005754
5755 return err;
5756}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005757
5758static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5759{
5760 return !buffer_mapped(bh);
5761}
5762
Nick Pigginc2ec1752009-03-31 15:23:21 -07005763int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005764{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005765 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005766 loff_t size;
5767 unsigned long len;
5768 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005769 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005770 struct file *file = vma->vm_file;
5771 struct inode *inode = file->f_path.dentry->d_inode;
5772 struct address_space *mapping = inode->i_mapping;
5773
5774 /*
5775 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5776 * get i_mutex because we are already holding mmap_sem.
5777 */
5778 down_read(&inode->i_alloc_sem);
5779 size = i_size_read(inode);
5780 if (page->mapping != mapping || size <= page_offset(page)
5781 || !PageUptodate(page)) {
5782 /* page got truncated from under us? */
5783 goto out_unlock;
5784 }
5785 ret = 0;
5786 if (PageMappedToDisk(page))
5787 goto out_unlock;
5788
5789 if (page->index == size >> PAGE_CACHE_SHIFT)
5790 len = size & ~PAGE_CACHE_MASK;
5791 else
5792 len = PAGE_CACHE_SIZE;
5793
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005794 lock_page(page);
5795 /*
5796 * return if we have all the buffers mapped. This avoid
5797 * the need to call write_begin/write_end which does a
5798 * journal_start/journal_stop which can block and take
5799 * long time
5800 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005801 if (page_has_buffers(page)) {
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005802 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005803 ext4_bh_unmapped)) {
5804 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005805 goto out_unlock;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005806 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005807 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005808 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005809 /*
5810 * OK, we need to fill the hole... Do write_begin write_end
5811 * to do block allocation/reservation.We are not holding
5812 * inode.i__mutex here. That allow * parallel write_begin,
5813 * write_end call. lock_page prevent this from happening
5814 * on the same page though
5815 */
5816 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005817 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005818 if (ret < 0)
5819 goto out_unlock;
5820 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005821 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005822 if (ret < 0)
5823 goto out_unlock;
5824 ret = 0;
5825out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005826 if (ret)
5827 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005828 up_read(&inode->i_alloc_sem);
5829 return ret;
5830}