blob: 9b82ac7b0f55ed1b4498853dc2ac2b601c38f4fc [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>
Jiaying Zhang744692d2010-03-04 16:14:02 -050041#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080042#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Theodore Ts'oa8901d32010-12-17 10:40:47 -050044#include <linux/ratelimit.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040045
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040046#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047#include "xattr.h"
48#include "acl.h"
Mingming Caod2a17632008-07-14 17:52:37 -040049#include "ext4_extents.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040050#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051
Theodore Ts'o9bffad12009-06-17 11:48:11 -040052#include <trace/events/ext4.h>
53
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040054#define MPAGE_DA_EXTENT_TAIL 0x01
55
Jan Kara678aaf42008-07-11 19:27:31 -040056static inline int ext4_begin_ordered_truncate(struct inode *inode,
57 loff_t new_size)
58{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -050059 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050060 /*
61 * If jinode is zero, then we never opened the file for
62 * writing, so there's no need to call
63 * jbd2_journal_begin_ordered_truncate() since there's no
64 * outstanding writes we need to flush.
65 */
66 if (!EXT4_I(inode)->jinode)
67 return 0;
68 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
69 EXT4_I(inode)->jinode,
70 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040071}
72
Alex Tomas64769242008-07-11 19:27:31 -040073static void ext4_invalidatepage(struct page *page, unsigned long offset);
Theodore Ts'ocb20d512010-10-27 21:30:09 -040074static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
75 struct buffer_head *bh_result, int create);
76static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
77static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
78static int __ext4_journalled_writepage(struct page *page, unsigned int len);
79static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Alex Tomas64769242008-07-11 19:27:31 -040080
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081/*
82 * Test whether an inode is a fast symlink.
83 */
Mingming Cao617ba132006-10-11 01:20:53 -070084static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070085{
Mingming Cao617ba132006-10-11 01:20:53 -070086 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070087 (inode->i_sb->s_blocksize >> 9) : 0;
88
89 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
90}
91
92/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093 * Truncate transactions can be complex and absolutely huge. So we need to
94 * be able to restart the transaction at a conventient checkpoint to make
95 * sure we don't overflow the journal.
96 *
97 * start_transaction gets us a new handle for a truncate transaction,
98 * and extend_transaction tries to extend the existing one a bit. If
99 * extend fails, we need to propagate the failure up and restart the
100 * transaction in the top-level truncate loop. --sct
101 */
102static handle_t *start_transaction(struct inode *inode)
103{
104 handle_t *result;
105
Theodore Ts'o9f125d62011-06-27 19:16:04 -0400106 result = ext4_journal_start(inode, ext4_blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107 if (!IS_ERR(result))
108 return result;
109
Mingming Cao617ba132006-10-11 01:20:53 -0700110 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111 return result;
112}
113
114/*
115 * Try to extend this transaction for the purposes of truncation.
116 *
117 * Returns 0 if we managed to create more room. If we can't create more
118 * room, and the transaction must be restarted we return 1.
119 */
120static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
121{
Frank Mayhar03901312009-01-07 00:06:22 -0500122 if (!ext4_handle_valid(handle))
123 return 0;
124 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700125 return 0;
Theodore Ts'o9f125d62011-06-27 19:16:04 -0400126 if (!ext4_journal_extend(handle, ext4_blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700127 return 0;
128 return 1;
129}
130
131/*
132 * Restart the transaction associated with *handle. This does a commit,
133 * so before we call here everything must be consistently dirtied against
134 * this transaction.
135 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500136int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400137 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138{
Jan Kara487caee2009-08-17 22:17:20 -0400139 int ret;
140
141 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400142 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400143 * moment, get_block can be called only for blocks inside i_size since
144 * page cache has been already dropped and writes are blocked by
145 * i_mutex. So we can safely drop the i_data_sem here.
146 */
Frank Mayhar03901312009-01-07 00:06:22 -0500147 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400149 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500150 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400151 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500152 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400153
154 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700155}
156
157/*
158 * Called at the last iput() if i_nlink is zero.
159 */
Al Viro0930fcc2010-06-07 13:16:22 -0400160void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161{
162 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400163 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500165 trace_ext4_evict_inode(inode);
Al Viro0930fcc2010-06-07 13:16:22 -0400166 if (inode->i_nlink) {
167 truncate_inode_pages(&inode->i_data, 0);
168 goto no_delete;
169 }
170
Christoph Hellwig907f4552010-03-03 09:05:06 -0500171 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500172 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500173
Jan Kara678aaf42008-07-11 19:27:31 -0400174 if (ext4_should_order_data(inode))
175 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 truncate_inode_pages(&inode->i_data, 0);
177
178 if (is_bad_inode(inode))
179 goto no_delete;
180
Theodore Ts'o9f125d62011-06-27 19:16:04 -0400181 handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400183 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 /*
185 * If we're going to skip the normal cleanup, we still need to
186 * make sure that the in-core orphan linked list is properly
187 * cleaned up.
188 */
Mingming Cao617ba132006-10-11 01:20:53 -0700189 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 goto no_delete;
191 }
192
193 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500194 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400196 err = ext4_mark_inode_dirty(handle, inode);
197 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500198 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400199 "couldn't mark inode dirty (err %d)", err);
200 goto stop_handle;
201 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700203 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400204
205 /*
206 * ext4_ext_truncate() doesn't reserve any slop when it
207 * restarts journal transactions; therefore there may not be
208 * enough credits left in the handle to remove the inode from
209 * the orphan list and set the dtime field.
210 */
Frank Mayhar03901312009-01-07 00:06:22 -0500211 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400212 err = ext4_journal_extend(handle, 3);
213 if (err > 0)
214 err = ext4_journal_restart(handle, 3);
215 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500216 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400217 "couldn't extend journal (err %d)", err);
218 stop_handle:
219 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400220 ext4_orphan_del(NULL, inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400221 goto no_delete;
222 }
223 }
224
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700226 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700228 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700230 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 * (Well, we could do this if we need to, but heck - it works)
232 */
Mingming Cao617ba132006-10-11 01:20:53 -0700233 ext4_orphan_del(handle, inode);
234 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235
236 /*
237 * One subtle ordering requirement: if anything has gone wrong
238 * (transaction abort, IO errors, whatever), then we can still
239 * do these next steps (the fs will already have been marked as
240 * having errors), but we can't free the inode if the mark_dirty
241 * fails.
242 */
Mingming Cao617ba132006-10-11 01:20:53 -0700243 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400245 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 else
Mingming Cao617ba132006-10-11 01:20:53 -0700247 ext4_free_inode(handle, inode);
248 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 return;
250no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400251 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252}
253
254typedef struct {
255 __le32 *p;
256 __le32 key;
257 struct buffer_head *bh;
258} Indirect;
259
260static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
261{
262 p->key = *(p->p = v);
263 p->bh = bh;
264}
265
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266/**
Mingming Cao617ba132006-10-11 01:20:53 -0700267 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 * @inode: inode in question (we are only interested in its superblock)
269 * @i_block: block number to be parsed
270 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400271 * @boundary: set this non-zero if the referred-to block is likely to be
272 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 *
Mingming Cao617ba132006-10-11 01:20:53 -0700274 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 * for UNIX filesystems - tree of pointers anchored in the inode, with
276 * data blocks at leaves and indirect blocks in intermediate nodes.
277 * This function translates the block number into path in that tree -
278 * return value is the path length and @offsets[n] is the offset of
279 * pointer to (n+1)th node in the nth one. If @block is out of range
280 * (negative or too large) warning is printed and zero returned.
281 *
282 * Note: function doesn't find node addresses, so no IO is needed. All
283 * we need to know is the capacity of indirect blocks (taken from the
284 * inode->i_sb).
285 */
286
287/*
288 * Portability note: the last comparison (check that we fit into triple
289 * indirect block) is spelled differently, because otherwise on an
290 * architecture with 32-bit longs and 8Kb pages we might get into trouble
291 * if our filesystem had 8Kb blocks. We might use long long, but that would
292 * kill us on x86. Oh, well, at least the sign propagation does not matter -
293 * i_block would have to be negative in the very beginning, so we would not
294 * get there at all.
295 */
296
Mingming Cao617ba132006-10-11 01:20:53 -0700297static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400298 ext4_lblk_t i_block,
299 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300{
Mingming Cao617ba132006-10-11 01:20:53 -0700301 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
302 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
303 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 indirect_blocks = ptrs,
305 double_blocks = (1 << (ptrs_bits * 2));
306 int n = 0;
307 int final = 0;
308
Roel Kluinc333e072009-08-10 22:47:22 -0400309 if (i_block < direct_blocks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 offsets[n++] = i_block;
311 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400312 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700313 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 offsets[n++] = i_block;
315 final = ptrs;
316 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700317 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 offsets[n++] = i_block >> ptrs_bits;
319 offsets[n++] = i_block & (ptrs - 1);
320 final = ptrs;
321 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700322 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 offsets[n++] = i_block >> (ptrs_bits * 2);
324 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
325 offsets[n++] = i_block & (ptrs - 1);
326 final = ptrs;
327 } else {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500328 ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400329 i_block + direct_blocks +
330 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 }
332 if (boundary)
333 *boundary = final - 1 - (i_block & (ptrs - 1));
334 return n;
335}
336
337/**
Mingming Cao617ba132006-10-11 01:20:53 -0700338 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700339 * @inode: inode in question
340 * @depth: depth of the chain (1 - direct pointer, etc.)
341 * @offsets: offsets of pointers in inode/indirect blocks
342 * @chain: place to store the result
343 * @err: here we store the error value
344 *
345 * Function fills the array of triples <key, p, bh> and returns %NULL
346 * if everything went OK or the pointer to the last filled triple
347 * (incomplete one) otherwise. Upon the return chain[i].key contains
348 * the number of (i+1)-th block in the chain (as it is stored in memory,
349 * i.e. little-endian 32-bit), chain[i].p contains the address of that
350 * number (it points into struct inode for i==0 and into the bh->b_data
351 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
352 * block for i>0 and NULL for i==0. In other words, it holds the block
353 * numbers of the chain, addresses they were taken from (and where we can
354 * verify that chain did not change) and buffer_heads hosting these
355 * numbers.
356 *
357 * Function stops when it stumbles upon zero pointer (absent block)
358 * (pointer to last triple returned, *@err == 0)
359 * or when it gets an IO error reading an indirect block
360 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 * or when it reads all @depth-1 indirect blocks successfully and finds
362 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500363 *
364 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500365 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500367static Indirect *ext4_get_branch(struct inode *inode, int depth,
368 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700369 Indirect chain[4], int *err)
370{
371 struct super_block *sb = inode->i_sb;
372 Indirect *p = chain;
373 struct buffer_head *bh;
374
375 *err = 0;
376 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400377 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378 if (!p->key)
379 goto no_block;
380 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400381 bh = sb_getblk(sb, le32_to_cpu(p->key));
382 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700383 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400384
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400385 if (!bh_uptodate_or_lock(bh)) {
386 if (bh_submit_read(bh) < 0) {
387 put_bh(bh);
388 goto failure;
389 }
390 /* validate block references */
391 if (ext4_check_indirect_blockref(inode, bh)) {
392 put_bh(bh);
393 goto failure;
394 }
395 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400396
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400397 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398 /* Reader: end */
399 if (!p->key)
400 goto no_block;
401 }
402 return NULL;
403
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404failure:
405 *err = -EIO;
406no_block:
407 return p;
408}
409
410/**
Mingming Cao617ba132006-10-11 01:20:53 -0700411 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 * @inode: owner
413 * @ind: descriptor of indirect block.
414 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000415 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700416 * It is used when heuristic for sequential allocation fails.
417 * Rules are:
418 * + if there is a block to the left of our position - allocate near it.
419 * + if pointer will live in indirect block - allocate near that block.
420 * + if pointer will live in inode - allocate in the same
421 * cylinder group.
422 *
423 * In the latter case we colour the starting block by the callers PID to
424 * prevent it from clashing with concurrent allocations for a different inode
425 * in the same block group. The PID is used here so that functionally related
426 * files will be close-by on-disk.
427 *
428 * Caller must make sure that @ind is valid and will stay that way.
429 */
Mingming Cao617ba132006-10-11 01:20:53 -0700430static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431{
Mingming Cao617ba132006-10-11 01:20:53 -0700432 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400433 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700435 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500436 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700437 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400438 ext4_group_t block_group;
439 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440
441 /* Try to find previous block */
442 for (p = ind->p - 1; p >= start; p--) {
443 if (*p)
444 return le32_to_cpu(*p);
445 }
446
447 /* No such thing, so let's try location of indirect block */
448 if (ind->bh)
449 return ind->bh->b_blocknr;
450
451 /*
452 * It is going to be referred to from the inode itself? OK, just put it
453 * into the same cylinder group then.
454 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400455 block_group = ei->i_block_group;
456 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
457 block_group &= ~(flex_size-1);
458 if (S_ISREG(inode->i_mode))
459 block_group++;
460 }
461 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500462 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
463
Theodore Ts'oa4912122009-03-12 12:18:34 -0400464 /*
465 * If we are doing delayed allocation, we don't need take
466 * colour into account.
467 */
468 if (test_opt(inode->i_sb, DELALLOC))
469 return bg_start;
470
Valerie Clement74d34872008-02-15 13:43:07 -0500471 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
472 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700473 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500474 else
475 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 return bg_start + colour;
477}
478
479/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000480 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 * @inode: owner
482 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000485 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800486 * returns it.
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400487 * Because this is only used for non-extent files, we limit the block nr
488 * to 32 bits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500490static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400491 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492{
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400493 ext4_fsblk_t goal;
494
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400496 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400499 goal = ext4_find_near(inode, partial);
500 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
501 return goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502}
503
504/**
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500505 * ext4_blks_to_allocate - Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506 * of direct blocks need to be allocated for the given branch.
507 *
508 * @branch: chain of indirect blocks
509 * @k: number of blocks need for indirect blocks
510 * @blks: number of data blocks to be mapped.
511 * @blocks_to_boundary: the offset in the indirect block
512 *
513 * return the total number of blocks to be allocate, including the
514 * direct and indirect blocks.
515 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500516static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400517 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500519 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520
521 /*
522 * Simple case, [t,d]Indirect block(s) has not allocated yet
523 * then it's clear blocks on that path have not allocated
524 */
525 if (k > 0) {
526 /* right now we don't handle cross boundary allocation */
527 if (blks < blocks_to_boundary + 1)
528 count += blks;
529 else
530 count += blocks_to_boundary + 1;
531 return count;
532 }
533
534 count++;
535 while (count < blks && count <= blocks_to_boundary &&
536 le32_to_cpu(*(branch[0].p + count)) == 0) {
537 count++;
538 }
539 return count;
540}
541
542/**
Mingming Cao617ba132006-10-11 01:20:53 -0700543 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500544 * @handle: handle for this transaction
545 * @inode: inode which needs allocated blocks
546 * @iblock: the logical block to start allocated at
547 * @goal: preferred physical block of allocation
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 * @indirect_blks: the number of blocks need to allocate for indirect
549 * blocks
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500550 * @blks: number of desired blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 * @new_blocks: on return it will store the new block numbers for
552 * the indirect blocks(if needed) and the first direct block,
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500553 * @err: on return it will store the error code
554 *
555 * This function will return the number of blocks allocated as
556 * requested by the passed-in parameters.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 */
Mingming Cao617ba132006-10-11 01:20:53 -0700558static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400559 ext4_lblk_t iblock, ext4_fsblk_t goal,
560 int indirect_blks, int blks,
561 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500563 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400565 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700567 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 int ret = 0;
569
570 /*
571 * Here we try to allocate the requested multiple blocks at once,
572 * on a best-effort basis.
573 * To build a branch, we should allocate blocks for
574 * the indirect blocks(if not allocated yet), and at least
575 * the first direct block of this branch. That's the
576 * minimum number of blocks need to allocate(required)
577 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400578 /* first we try to allocate the indirect blocks */
579 target = indirect_blks;
580 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581 count = target;
582 /* allocating blocks for indirect blocks and direct blocks */
Allison Henderson55f020d2011-05-25 07:41:26 -0400583 current_block = ext4_new_meta_blocks(handle, inode, goal,
584 0, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 if (*err)
586 goto failed_out;
587
Frank Mayhar273df552010-03-02 11:46:09 -0500588 if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) {
589 EXT4_ERROR_INODE(inode,
590 "current_block %llu + count %lu > %d!",
591 current_block, count,
592 EXT4_MAX_BLOCK_FILE_PHYS);
593 *err = -EIO;
594 goto failed_out;
595 }
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400596
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 target -= count;
598 /* allocate blocks for indirect blocks */
599 while (index < indirect_blks && count) {
600 new_blocks[index++] = current_block++;
601 count--;
602 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400603 if (count > 0) {
604 /*
605 * save the new block number
606 * for the first direct block
607 */
608 new_blocks[index] = current_block;
609 printk(KERN_INFO "%s returned more blocks than "
610 "requested\n", __func__);
611 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400613 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614 }
615
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400616 target = blks - count ;
617 blk_allocated = count;
618 if (!target)
619 goto allocated;
620 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500621 memset(&ar, 0, sizeof(ar));
622 ar.inode = inode;
623 ar.goal = goal;
624 ar.len = target;
625 ar.logical = iblock;
626 if (S_ISREG(inode->i_mode))
627 /* enable in-core preallocation only for regular files */
628 ar.flags = EXT4_MB_HINT_DATA;
629
630 current_block = ext4_mb_new_blocks(handle, &ar, err);
Frank Mayhar273df552010-03-02 11:46:09 -0500631 if (unlikely(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS)) {
632 EXT4_ERROR_INODE(inode,
633 "current_block %llu + ar.len %d > %d!",
634 current_block, ar.len,
635 EXT4_MAX_BLOCK_FILE_PHYS);
636 *err = -EIO;
637 goto failed_out;
638 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500639
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400640 if (*err && (target == blks)) {
641 /*
642 * if the allocation failed and we didn't allocate
643 * any blocks before
644 */
645 goto failed_out;
646 }
647 if (!*err) {
648 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400649 /*
650 * save the new block number
651 * for the first direct block
652 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400653 new_blocks[index] = current_block;
654 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500655 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400656 }
657allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400659 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 *err = 0;
661 return ret;
662failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400663 for (i = 0; i < index; i++)
Peter Huewe7dc57612011-02-21 21:01:42 -0500664 ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 return ret;
666}
667
668/**
Mingming Cao617ba132006-10-11 01:20:53 -0700669 * ext4_alloc_branch - allocate and set up a chain of blocks.
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500670 * @handle: handle for this transaction
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 * @inode: owner
672 * @indirect_blks: number of allocated indirect blocks
673 * @blks: number of allocated direct blocks
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500674 * @goal: preferred place for allocation
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 * @offsets: offsets (in the blocks) to store the pointers to next.
676 * @branch: place to store the chain in.
677 *
678 * This function allocates blocks, zeroes out all but the last one,
679 * links them into chain and (if we are synchronous) writes them to disk.
680 * In other words, it prepares a branch that can be spliced onto the
681 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700682 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 * we had read the existing part of chain and partial points to the last
684 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700685 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 * place chain is disconnected - *branch->p is still zero (we did not
687 * set the last link), but branch->key contains the number that should
688 * be placed into *branch->p to fill that gap.
689 *
690 * If allocation fails we free all blocks we've allocated (and forget
691 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700692 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 * as described above and return 0.
694 */
Mingming Cao617ba132006-10-11 01:20:53 -0700695static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400696 ext4_lblk_t iblock, int indirect_blks,
697 int *blks, ext4_fsblk_t goal,
698 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699{
700 int blocksize = inode->i_sb->s_blocksize;
701 int i, n = 0;
702 int err = 0;
703 struct buffer_head *bh;
704 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700705 ext4_fsblk_t new_blocks[4];
706 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400708 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 *blks, new_blocks, &err);
710 if (err)
711 return err;
712
713 branch[0].key = cpu_to_le32(new_blocks[0]);
714 /*
715 * metadata blocks and data blocks are allocated.
716 */
717 for (n = 1; n <= indirect_blks; n++) {
718 /*
719 * Get buffer_head for parent block, zero it out
720 * and set the pointer to new one, then send
721 * parent to disk.
722 */
723 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
Namhyung Kim87783692010-10-27 21:30:11 -0400724 if (unlikely(!bh)) {
725 err = -EIO;
726 goto failed;
727 }
728
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 branch[n].bh = bh;
730 lock_buffer(bh);
731 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700732 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400734 /* Don't brelse(bh) here; it's done in
735 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 goto failed;
738 }
739
740 memset(bh->b_data, 0, blocksize);
741 branch[n].p = (__le32 *) bh->b_data + offsets[n];
742 branch[n].key = cpu_to_le32(new_blocks[n]);
743 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400744 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 current_block = new_blocks[n];
746 /*
747 * End of chain, update the last new metablock of
748 * the chain to point to the new allocated
749 * data blocks numbers
750 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400751 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 *(branch[n].p + i) = cpu_to_le32(++current_block);
753 }
754 BUFFER_TRACE(bh, "marking uptodate");
755 set_buffer_uptodate(bh);
756 unlock_buffer(bh);
757
Frank Mayhar03901312009-01-07 00:06:22 -0500758 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
759 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 if (err)
761 goto failed;
762 }
763 *blks = num;
764 return err;
765failed:
766 /* Allocation failed, free what we already allocated */
Peter Huewe7dc57612011-02-21 21:01:42 -0500767 ext4_free_blocks(handle, inode, NULL, new_blocks[0], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 for (i = 1; i <= n ; i++) {
Theodore Ts'o60e66792010-05-17 07:00:00 -0400769 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500770 * branch[i].bh is newly allocated, so there is no
771 * need to revoke the block, which is why we don't
772 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500773 */
Peter Huewe7dc57612011-02-21 21:01:42 -0500774 ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500775 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 }
Theodore Ts'oe6362602009-11-23 07:17:05 -0500777 for (i = n+1; i < indirect_blks; i++)
Peter Huewe7dc57612011-02-21 21:01:42 -0500778 ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779
Peter Huewe7dc57612011-02-21 21:01:42 -0500780 ext4_free_blocks(handle, inode, NULL, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781
782 return err;
783}
784
785/**
Mingming Cao617ba132006-10-11 01:20:53 -0700786 * ext4_splice_branch - splice the allocated branch onto inode.
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500787 * @handle: handle for this transaction
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 * @inode: owner
789 * @block: (logical) number of block we are adding
790 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700791 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 * @where: location of missing link
793 * @num: number of indirect blocks we are adding
794 * @blks: number of direct blocks we are adding
795 *
796 * This function fills the missing link and does all housekeeping needed in
797 * inode (->i_blocks, etc.). In case of success we end up with the full
798 * chain to new block and return 0.
799 */
Mingming Cao617ba132006-10-11 01:20:53 -0700800static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400801 ext4_lblk_t block, Indirect *where, int num,
802 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803{
804 int i;
805 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700806 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700808 /*
809 * If we're splicing into a [td]indirect block (as opposed to the
810 * inode) then we need to get write access to the [td]indirect block
811 * before the splice.
812 */
813 if (where->bh) {
814 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700815 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 if (err)
817 goto err_out;
818 }
819 /* That's it */
820
821 *where->p = where->key;
822
823 /*
824 * Update the host buffer_head or inode to point to more just allocated
825 * direct blocks blocks
826 */
827 if (num == 0 && blks > 1) {
828 current_block = le32_to_cpu(where->key) + 1;
829 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400830 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831 }
832
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834 /* had we spliced it onto indirect block? */
835 if (where->bh) {
836 /*
837 * If we spliced it onto an indirect block, we haven't
838 * altered the inode. Note however that if it is being spliced
839 * onto an indirect block at the very end of the file (the
840 * file is growing) then we *will* alter the inode to reflect
841 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700842 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 */
844 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500845 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
846 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847 if (err)
848 goto err_out;
849 } else {
850 /*
851 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400853 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 jbd_debug(5, "splicing direct\n");
855 }
856 return err;
857
858err_out:
859 for (i = 1; i <= num; i++) {
Theodore Ts'o60e66792010-05-17 07:00:00 -0400860 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500861 * branch[i].bh is newly allocated, so there is no
862 * need to revoke the block, which is why we don't
863 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500864 */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500865 ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
866 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 }
Peter Huewe7dc57612011-02-21 21:01:42 -0500868 ext4_free_blocks(handle, inode, NULL, le32_to_cpu(where[num].key),
Theodore Ts'oe6362602009-11-23 07:17:05 -0500869 blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870
871 return err;
872}
873
874/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400875 * The ext4_ind_map_blocks() function handles non-extents inodes
Theodore Ts'ob920c752009-05-14 00:54:29 -0400876 * (i.e., using the traditional indirect/double-indirect i_blocks
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400877 * scheme) for ext4_map_blocks().
Theodore Ts'ob920c752009-05-14 00:54:29 -0400878 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 * Allocation strategy is simple: if we have to allocate something, we will
880 * have to go the whole way to leaf. So let's do it before attaching anything
881 * to tree, set linkage between the newborn blocks, write them if sync is
882 * required, recheck the path, free and repeat if check fails, otherwise
883 * set the last missing link (that will protect us from any truncate-generated
884 * removals - all blocks on the path are immune now) and possibly force the
885 * write on the parent block.
886 * That has a nice additional property: no special recovery from the failed
887 * allocations is needed - we simply release blocks and do not touch anything
888 * reachable from inode.
889 *
890 * `handle' can be NULL if create == 0.
891 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 * return > 0, # of blocks mapped or allocated.
893 * return = 0, if plain lookup failed.
894 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500895 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400896 * The ext4_ind_get_blocks() function should be called with
897 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
898 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
899 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
900 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400902static int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
903 struct ext4_map_blocks *map,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400904 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905{
906 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500907 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 Indirect chain[4];
909 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700910 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 int indirect_blks;
912 int blocks_to_boundary = 0;
913 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700915 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400917 trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400918 J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400919 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400920 depth = ext4_block_to_path(inode, map->m_lblk, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400921 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922
923 if (depth == 0)
924 goto out;
925
Mingming Cao617ba132006-10-11 01:20:53 -0700926 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927
928 /* Simplest case - block found, no allocation needed */
929 if (!partial) {
930 first_block = le32_to_cpu(chain[depth - 1].key);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931 count++;
932 /*map more blocks*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400933 while (count < map->m_len && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700934 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 blk = le32_to_cpu(*(chain[depth-1].p + count));
937
938 if (blk == first_block + count)
939 count++;
940 else
941 break;
942 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500943 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944 }
945
946 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400947 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 goto cleanup;
949
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400951 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400953 goal = ext4_find_goal(inode, map->m_lblk, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954
955 /* the number of blocks need to allocate for [d,t]indirect blocks */
956 indirect_blks = (chain + depth) - partial - 1;
957
958 /*
959 * Next look up the indirect map to count the totoal number of
960 * direct blocks to allocate for this branch.
961 */
Mingming Cao617ba132006-10-11 01:20:53 -0700962 count = ext4_blks_to_allocate(partial, indirect_blks,
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400963 map->m_len, blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700965 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400967 err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400968 &count, goal,
969 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970
971 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700972 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 * on the new chain if there is a failure, but that risks using
974 * up transaction credits, especially for bitmaps where the
975 * credits cannot be returned. Can we handle this somehow? We
976 * may need to return -EAGAIN upwards in the worst case. --sct
977 */
978 if (!err)
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400979 err = ext4_splice_branch(handle, inode, map->m_lblk,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400980 partial, indirect_blks, count);
Jan Kara2bba7022009-11-23 07:24:48 -0500981 if (err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 goto cleanup;
983
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400984 map->m_flags |= EXT4_MAP_NEW;
Jan Karab436b9b2009-12-08 23:51:10 -0500985
986 ext4_update_inode_fsync_trans(handle, inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987got_it:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400988 map->m_flags |= EXT4_MAP_MAPPED;
989 map->m_pblk = le32_to_cpu(chain[depth-1].key);
990 map->m_len = count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 if (count > blocks_to_boundary)
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400992 map->m_flags |= EXT4_MAP_BOUNDARY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 err = count;
994 /* Clean up and exit */
995 partial = chain + depth - 1; /* the whole chain */
996cleanup:
997 while (partial > chain) {
998 BUFFER_TRACE(partial->bh, "call brelse");
999 brelse(partial->bh);
1000 partial--;
1001 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002out:
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04001003 trace_ext4_ind_map_blocks_exit(inode, map->m_lblk,
1004 map->m_pblk, map->m_len, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 return err;
1006}
1007
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001008#ifdef CONFIG_QUOTA
1009qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +01001010{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001011 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +01001012}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001013#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001014
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001015/*
1016 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001017 * to allocate a new block at @lblocks for non extent file based file
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001018 */
Amir Goldstein8bb2b242011-06-27 17:10:28 -04001019static int ext4_ind_calc_metadata_amount(struct inode *inode, sector_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001020{
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001021 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karad330a5b2010-03-14 18:17:54 -04001022 sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001023 int blk_bits;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001024
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001025 if (lblock < EXT4_NDIR_BLOCKS)
1026 return 0;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001027
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001028 lblock -= EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001029
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001030 if (ei->i_da_metadata_calc_len &&
1031 (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
1032 ei->i_da_metadata_calc_len++;
1033 return 0;
1034 }
1035 ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
1036 ei->i_da_metadata_calc_len = 1;
Jan Karad330a5b2010-03-14 18:17:54 -04001037 blk_bits = order_base_2(lblock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001038 return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001039}
1040
1041/*
1042 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001043 * to allocate a block located at @lblock
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001044 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001045static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001046{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001047 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001048 return ext4_ext_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001049
Amir Goldstein8bb2b242011-06-27 17:10:28 -04001050 return ext4_ind_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001051}
1052
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001053/*
1054 * Called with i_data_sem down, which is important since we can call
1055 * ext4_discard_preallocations() from here.
1056 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001057void ext4_da_update_reserve_space(struct inode *inode,
1058 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001059{
1060 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001061 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001062
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001063 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'of8ec9d62010-01-01 01:00:21 -05001064 trace_ext4_da_update_reserve_space(inode, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001065 if (unlikely(used > ei->i_reserved_data_blocks)) {
1066 ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
1067 "with only %d reserved data blocks\n",
1068 __func__, inode->i_ino, used,
1069 ei->i_reserved_data_blocks);
1070 WARN_ON(1);
1071 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001072 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001073
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001074 /* Update per-inode reservations */
1075 ei->i_reserved_data_blocks -= used;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001076 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001077 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1078 used + ei->i_allocated_meta_blocks);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001079 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001080
1081 if (ei->i_reserved_data_blocks == 0) {
1082 /*
1083 * We can release all of the reserved metadata blocks
1084 * only when we have written all of the delayed
1085 * allocation blocks.
1086 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001087 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1088 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001089 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001090 ei->i_da_metadata_calc_len = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001091 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001092 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001093
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001094 /* Update quota subsystem for data blocks */
1095 if (quota_claim)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001096 dquot_claim_block(inode, used);
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001097 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001098 /*
1099 * We did fallocate with an offset that is already delayed
1100 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001101 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001102 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001103 dquot_release_reservation_block(inode, used);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001104 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001105
1106 /*
1107 * If we have done all the pending block allocations and if
1108 * there aren't any writers on the inode, we can discard the
1109 * inode's preallocations.
1110 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001111 if ((ei->i_reserved_data_blocks == 0) &&
1112 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001113 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001114}
1115
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001116static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -04001117 unsigned int line,
1118 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001119{
Theodore Ts'o24676da2010-05-16 21:00:00 -04001120 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
1121 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04001122 ext4_error_inode(inode, func, line, map->m_pblk,
1123 "lblock %lu mapped to illegal pblock "
1124 "(length %d)", (unsigned long) map->m_lblk,
1125 map->m_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001126 return -EIO;
1127 }
1128 return 0;
1129}
1130
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001131#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -04001132 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001133
Mingming Caof5ab0d12008-02-25 15:29:55 -05001134/*
Theodore Ts'o1f945332009-09-30 22:57:41 -04001135 * Return the number of contiguous dirty pages in a given inode
1136 * starting at page frame idx.
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04001137 */
1138static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
1139 unsigned int max_pages)
1140{
1141 struct address_space *mapping = inode->i_mapping;
1142 pgoff_t index;
1143 struct pagevec pvec;
1144 pgoff_t num = 0;
1145 int i, nr_pages, done = 0;
1146
1147 if (max_pages == 0)
1148 return 0;
1149 pagevec_init(&pvec, 0);
1150 while (!done) {
1151 index = idx;
1152 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1153 PAGECACHE_TAG_DIRTY,
1154 (pgoff_t)PAGEVEC_SIZE);
1155 if (nr_pages == 0)
1156 break;
1157 for (i = 0; i < nr_pages; i++) {
1158 struct page *page = pvec.pages[i];
1159 struct buffer_head *bh, *head;
1160
1161 lock_page(page);
1162 if (unlikely(page->mapping != mapping) ||
1163 !PageDirty(page) ||
1164 PageWriteback(page) ||
1165 page->index != idx) {
1166 done = 1;
1167 unlock_page(page);
1168 break;
1169 }
Theodore Ts'o1f945332009-09-30 22:57:41 -04001170 if (page_has_buffers(page)) {
1171 bh = head = page_buffers(page);
1172 do {
1173 if (!buffer_delay(bh) &&
1174 !buffer_unwritten(bh))
1175 done = 1;
1176 bh = bh->b_this_page;
1177 } while (!done && (bh != head));
1178 }
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04001179 unlock_page(page);
1180 if (done)
1181 break;
1182 idx++;
1183 num++;
Eric Sandeen659c6002010-10-27 21:30:03 -04001184 if (num >= max_pages) {
1185 done = 1;
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04001186 break;
Eric Sandeen659c6002010-10-27 21:30:03 -04001187 }
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04001188 }
1189 pagevec_release(&pvec);
1190 }
1191 return num;
1192}
1193
1194/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001195 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001196 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001197 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001198 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1199 * and store the allocated blocks in the result buffer head and mark it
1200 * mapped.
1201 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001202 * If file type is extents based, it will call ext4_ext_map_blocks(),
1203 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001204 * based files
1205 *
1206 * On success, it returns the number of blocks being mapped or allocate.
1207 * if create==0 and the blocks are pre-allocated and uninitialized block,
1208 * the result buffer head is unmapped. If the create ==1, it will make sure
1209 * the buffer head is mapped.
1210 *
1211 * It returns 0 if plain look up failed (blocks have not been allocated), in
1212 * that casem, buffer head is unmapped
1213 *
1214 * It returns the error in case of allocation failure.
1215 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001216int ext4_map_blocks(handle_t *handle, struct inode *inode,
1217 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001218{
1219 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001220
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001221 map->m_flags = 0;
1222 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
1223 "logical block %lu\n", inode->i_ino, flags, map->m_len,
1224 (unsigned long) map->m_lblk);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001225 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001226 * Try to see if we can get the block without requesting a new
1227 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001228 */
1229 down_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001230 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001231 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001232 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001233 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001234 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001235 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001236
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001237 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001238 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001239 if (ret != 0)
1240 return ret;
1241 }
1242
Mingming Caof5ab0d12008-02-25 15:29:55 -05001243 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001244 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001245 return retval;
1246
1247 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001248 * Returns if the blocks have already allocated
1249 *
1250 * Note that if blocks have been preallocated
1251 * ext4_ext_get_block() returns th create = 0
1252 * with buffer head unmapped.
1253 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001254 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Mingming Caof5ab0d12008-02-25 15:29:55 -05001255 return retval;
1256
1257 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001258 * When we call get_blocks without the create flag, the
1259 * BH_Unwritten flag could have gotten set if the blocks
1260 * requested were part of a uninitialized extent. We need to
1261 * clear this flag now that we are committed to convert all or
1262 * part of the uninitialized extent to be an initialized
1263 * extent. This is because we need to avoid the combination
1264 * of BH_Unwritten and BH_Mapped flags being simultaneously
1265 * set on the buffer_head.
1266 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001267 map->m_flags &= ~EXT4_MAP_UNWRITTEN;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001268
1269 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001270 * New blocks allocate and/or writing to uninitialized extent
1271 * will possibly result in updating i_data, so we take
1272 * the write lock of i_data_sem, and call get_blocks()
1273 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001274 */
1275 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001276
1277 /*
1278 * if the caller is from delayed allocation writeout path
1279 * we have already reserved fs blocks for allocation
1280 * let the underlying get_block() function know to
1281 * avoid double accounting
1282 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001283 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -05001284 ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001285 /*
1286 * We need to check for EXT4 here because migrate
1287 * could have changed the inode type in between
1288 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001289 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001290 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001291 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001292 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001293
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001294 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001295 /*
1296 * We allocated new blocks which will result in
1297 * i_data's format changing. Force the migrate
1298 * to fail by clearing migrate flags
1299 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001300 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001301 }
Mingming Caod2a17632008-07-14 17:52:37 -04001302
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001303 /*
1304 * Update reserved blocks/metadata blocks after successful
1305 * block allocation which had been deferred till now. We don't
1306 * support fallocate for non extent files. So we can update
1307 * reserve space here.
1308 */
1309 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001310 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001311 ext4_da_update_reserve_space(inode, retval, 1);
1312 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001313 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -05001314 ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001315
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001316 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001317 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001318 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001319 if (ret != 0)
1320 return ret;
1321 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001322 return retval;
1323}
1324
Mingming Caof3bd1f32008-08-19 22:16:03 -04001325/* Maximum number of blocks we map for direct IO at once. */
1326#define DIO_MAX_BLOCKS 4096
1327
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001328static int _ext4_get_block(struct inode *inode, sector_t iblock,
1329 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001331 handle_t *handle = ext4_journal_current_handle();
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001332 struct ext4_map_blocks map;
Jan Kara7fb54092008-02-10 01:08:38 -05001333 int ret = 0, started = 0;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001334 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001335
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001336 map.m_lblk = iblock;
1337 map.m_len = bh->b_size >> inode->i_blkbits;
1338
1339 if (flags && !handle) {
Jan Kara7fb54092008-02-10 01:08:38 -05001340 /* Direct IO write... */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001341 if (map.m_len > DIO_MAX_BLOCKS)
1342 map.m_len = DIO_MAX_BLOCKS;
1343 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
Mingming Caof3bd1f32008-08-19 22:16:03 -04001344 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001345 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001346 ret = PTR_ERR(handle);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001347 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 }
Jan Kara7fb54092008-02-10 01:08:38 -05001349 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 }
1351
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001352 ret = ext4_map_blocks(handle, inode, &map, flags);
Jan Kara7fb54092008-02-10 01:08:38 -05001353 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001354 map_bh(bh, inode->i_sb, map.m_pblk);
1355 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1356 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -05001357 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001358 }
Jan Kara7fb54092008-02-10 01:08:38 -05001359 if (started)
1360 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 return ret;
1362}
1363
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001364int ext4_get_block(struct inode *inode, sector_t iblock,
1365 struct buffer_head *bh, int create)
1366{
1367 return _ext4_get_block(inode, iblock, bh,
1368 create ? EXT4_GET_BLOCKS_CREATE : 0);
1369}
1370
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001371/*
1372 * `handle' can be NULL if create is zero
1373 */
Mingming Cao617ba132006-10-11 01:20:53 -07001374struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001375 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001377 struct ext4_map_blocks map;
1378 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 int fatal = 0, err;
1380
1381 J_ASSERT(handle != NULL || create == 0);
1382
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001383 map.m_lblk = block;
1384 map.m_len = 1;
1385 err = ext4_map_blocks(handle, inode, &map,
1386 create ? EXT4_GET_BLOCKS_CREATE : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001388 if (err < 0)
1389 *errp = err;
1390 if (err <= 0)
1391 return NULL;
1392 *errp = 0;
1393
1394 bh = sb_getblk(inode->i_sb, map.m_pblk);
1395 if (!bh) {
1396 *errp = -EIO;
1397 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001398 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001399 if (map.m_flags & EXT4_MAP_NEW) {
1400 J_ASSERT(create != 0);
1401 J_ASSERT(handle != NULL);
1402
1403 /*
1404 * Now that we do not always journal data, we should
1405 * keep in mind whether this should always journal the
1406 * new buffer as metadata. For now, regular file
1407 * writes use ext4_get_block instead, so it's not a
1408 * problem.
1409 */
1410 lock_buffer(bh);
1411 BUFFER_TRACE(bh, "call get_create_access");
1412 fatal = ext4_journal_get_create_access(handle, bh);
1413 if (!fatal && !buffer_uptodate(bh)) {
1414 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1415 set_buffer_uptodate(bh);
1416 }
1417 unlock_buffer(bh);
1418 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1419 err = ext4_handle_dirty_metadata(handle, inode, bh);
1420 if (!fatal)
1421 fatal = err;
1422 } else {
1423 BUFFER_TRACE(bh, "not a new buffer");
1424 }
1425 if (fatal) {
1426 *errp = fatal;
1427 brelse(bh);
1428 bh = NULL;
1429 }
1430 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431}
1432
Mingming Cao617ba132006-10-11 01:20:53 -07001433struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001434 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001435{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001436 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001437
Mingming Cao617ba132006-10-11 01:20:53 -07001438 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 if (!bh)
1440 return bh;
1441 if (buffer_uptodate(bh))
1442 return bh;
1443 ll_rw_block(READ_META, 1, &bh);
1444 wait_on_buffer(bh);
1445 if (buffer_uptodate(bh))
1446 return bh;
1447 put_bh(bh);
1448 *err = -EIO;
1449 return NULL;
1450}
1451
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001452static int walk_page_buffers(handle_t *handle,
1453 struct buffer_head *head,
1454 unsigned from,
1455 unsigned to,
1456 int *partial,
1457 int (*fn)(handle_t *handle,
1458 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459{
1460 struct buffer_head *bh;
1461 unsigned block_start, block_end;
1462 unsigned blocksize = head->b_size;
1463 int err, ret = 0;
1464 struct buffer_head *next;
1465
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001466 for (bh = head, block_start = 0;
1467 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001468 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 next = bh->b_this_page;
1470 block_end = block_start + blocksize;
1471 if (block_end <= from || block_start >= to) {
1472 if (partial && !buffer_uptodate(bh))
1473 *partial = 1;
1474 continue;
1475 }
1476 err = (*fn)(handle, bh);
1477 if (!ret)
1478 ret = err;
1479 }
1480 return ret;
1481}
1482
1483/*
1484 * To preserve ordering, it is essential that the hole instantiation and
1485 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001486 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001487 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001488 * prepare_write() is the right place.
1489 *
Mingming Cao617ba132006-10-11 01:20:53 -07001490 * Also, this function can nest inside ext4_writepage() ->
1491 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 * has generated enough buffer credits to do the whole page. So we won't
1493 * block on the journal in that case, which is good, because the caller may
1494 * be PF_MEMALLOC.
1495 *
Mingming Cao617ba132006-10-11 01:20:53 -07001496 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497 * quota file writes. If we were to commit the transaction while thus
1498 * reentered, there can be a deadlock - we would be holding a quota
1499 * lock, and the commit would never complete if another thread had a
1500 * transaction open and was blocking on the quota lock - a ranking
1501 * violation.
1502 *
Mingming Caodab291a2006-10-11 01:21:01 -07001503 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 * will _not_ run commit under these circumstances because handle->h_ref
1505 * is elevated. We'll still have enough credits for the tiny quotafile
1506 * write.
1507 */
1508static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001509 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510{
Jan Kara56d35a42010-08-05 14:41:42 -04001511 int dirty = buffer_dirty(bh);
1512 int ret;
1513
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001514 if (!buffer_mapped(bh) || buffer_freed(bh))
1515 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -04001516 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02001517 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001518 * the dirty bit as jbd2_journal_get_write_access() could complain
1519 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001520 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001521 * the bit before releasing a page lock and thus writeback cannot
1522 * ever write the buffer.
1523 */
1524 if (dirty)
1525 clear_buffer_dirty(bh);
1526 ret = ext4_journal_get_write_access(handle, bh);
1527 if (!ret && dirty)
1528 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1529 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530}
1531
Jiaying Zhang744692d2010-03-04 16:14:02 -05001532static int ext4_get_block_write(struct inode *inode, sector_t iblock,
1533 struct buffer_head *bh_result, int create);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001534static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001535 loff_t pos, unsigned len, unsigned flags,
1536 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001538 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001539 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 handle_t *handle;
1541 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001542 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001543 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001544 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001545
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001546 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001547 /*
1548 * Reserve one block more for addition to orphan list in case
1549 * we allocate blocks but write fails for some reason
1550 */
1551 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001552 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001553 from = pos & (PAGE_CACHE_SIZE - 1);
1554 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555
1556retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001557 handle = ext4_journal_start(inode, needed_blocks);
1558 if (IS_ERR(handle)) {
1559 ret = PTR_ERR(handle);
1560 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001561 }
1562
Jan Karaebd36102009-02-22 21:09:59 -05001563 /* We cannot recurse into the filesystem as the transaction is already
1564 * started */
1565 flags |= AOP_FLAG_NOFS;
1566
Nick Piggin54566b22009-01-04 12:00:53 -08001567 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001568 if (!page) {
1569 ext4_journal_stop(handle);
1570 ret = -ENOMEM;
1571 goto out;
1572 }
1573 *pagep = page;
1574
Jiaying Zhang744692d2010-03-04 16:14:02 -05001575 if (ext4_should_dioread_nolock(inode))
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001576 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001577 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001578 ret = __block_write_begin(page, pos, len, ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001579
1580 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001581 ret = walk_page_buffers(handle, page_buffers(page),
1582 from, to, NULL, do_journal_get_write_access);
1583 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001584
1585 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001586 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001587 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001588 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001589 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001590 * outside i_size. Trim these off again. Don't need
1591 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001592 *
1593 * Add inode to orphan list in case we crash before
1594 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001595 */
Jan Karaffacfa72009-07-13 16:22:22 -04001596 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001597 ext4_orphan_add(handle, inode);
1598
1599 ext4_journal_stop(handle);
1600 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001601 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001602 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001603 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001604 * still be on the orphan list; we need to
1605 * make sure the inode is removed from the
1606 * orphan list in that case.
1607 */
1608 if (inode->i_nlink)
1609 ext4_orphan_del(NULL, inode);
1610 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001611 }
1612
Mingming Cao617ba132006-10-11 01:20:53 -07001613 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001615out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616 return ret;
1617}
1618
Nick Pigginbfc1af62007-10-16 01:25:05 -07001619/* For write_end() in data=journal mode */
1620static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621{
1622 if (!buffer_mapped(bh) || buffer_freed(bh))
1623 return 0;
1624 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001625 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001626}
1627
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001628static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001629 struct address_space *mapping,
1630 loff_t pos, unsigned len, unsigned copied,
1631 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001632{
1633 int i_size_changed = 0;
1634 struct inode *inode = mapping->host;
1635 handle_t *handle = ext4_journal_current_handle();
1636
1637 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1638
1639 /*
1640 * No need to use i_size_read() here, the i_size
1641 * cannot change under us because we hold i_mutex.
1642 *
1643 * But it's important to update i_size while still holding page lock:
1644 * page writeout could otherwise come in and zero beyond i_size.
1645 */
1646 if (pos + copied > inode->i_size) {
1647 i_size_write(inode, pos + copied);
1648 i_size_changed = 1;
1649 }
1650
1651 if (pos + copied > EXT4_I(inode)->i_disksize) {
1652 /* We need to mark inode dirty even if
1653 * new_i_size is less that inode->i_size
1654 * bu greater than i_disksize.(hint delalloc)
1655 */
1656 ext4_update_i_disksize(inode, (pos + copied));
1657 i_size_changed = 1;
1658 }
1659 unlock_page(page);
1660 page_cache_release(page);
1661
1662 /*
1663 * Don't mark the inode dirty under page lock. First, it unnecessarily
1664 * makes the holding time of page lock longer. Second, it forces lock
1665 * ordering of page lock and transaction start for journaling
1666 * filesystems.
1667 */
1668 if (i_size_changed)
1669 ext4_mark_inode_dirty(handle, inode);
1670
1671 return copied;
1672}
1673
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674/*
1675 * We need to pick up the new inode size which generic_commit_write gave us
1676 * `file' can be NULL - eg, when called from page_symlink().
1677 *
Mingming Cao617ba132006-10-11 01:20:53 -07001678 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679 * buffers are managed internally.
1680 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001681static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001682 struct address_space *mapping,
1683 loff_t pos, unsigned len, unsigned copied,
1684 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001685{
Mingming Cao617ba132006-10-11 01:20:53 -07001686 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001687 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688 int ret = 0, ret2;
1689
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001690 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001691 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001692
1693 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001694 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001695 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001696 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001697 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001698 /* if we have allocated more blocks and copied
1699 * less. We will have blocks allocated outside
1700 * inode->i_size. So truncate them
1701 */
1702 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001703 if (ret2 < 0)
1704 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705 }
Mingming Cao617ba132006-10-11 01:20:53 -07001706 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707 if (!ret)
1708 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001709
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001710 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001711 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001712 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001713 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001714 * on the orphan list; we need to make sure the inode
1715 * is removed from the orphan list in that case.
1716 */
1717 if (inode->i_nlink)
1718 ext4_orphan_del(NULL, inode);
1719 }
1720
1721
Nick Pigginbfc1af62007-10-16 01:25:05 -07001722 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723}
1724
Nick Pigginbfc1af62007-10-16 01:25:05 -07001725static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001726 struct address_space *mapping,
1727 loff_t pos, unsigned len, unsigned copied,
1728 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729{
Mingming Cao617ba132006-10-11 01:20:53 -07001730 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001731 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001734 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001735 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001736 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001737 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001738 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001739 /* if we have allocated more blocks and copied
1740 * less. We will have blocks allocated outside
1741 * inode->i_size. So truncate them
1742 */
1743 ext4_orphan_add(handle, inode);
1744
Roel Kluinf8a87d82008-04-29 22:01:18 -04001745 if (ret2 < 0)
1746 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747
Mingming Cao617ba132006-10-11 01:20:53 -07001748 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001749 if (!ret)
1750 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001751
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001752 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001753 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001754 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001755 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001756 * on the orphan list; we need to make sure the inode
1757 * is removed from the orphan list in that case.
1758 */
1759 if (inode->i_nlink)
1760 ext4_orphan_del(NULL, inode);
1761 }
1762
Nick Pigginbfc1af62007-10-16 01:25:05 -07001763 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001764}
1765
Nick Pigginbfc1af62007-10-16 01:25:05 -07001766static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001767 struct address_space *mapping,
1768 loff_t pos, unsigned len, unsigned copied,
1769 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770{
Mingming Cao617ba132006-10-11 01:20:53 -07001771 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001772 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773 int ret = 0, ret2;
1774 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001775 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001776 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001778 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001779 from = pos & (PAGE_CACHE_SIZE - 1);
1780 to = from + len;
1781
1782 if (copied < len) {
1783 if (!PageUptodate(page))
1784 copied = 0;
1785 page_zero_new_buffers(page, from+copied, to);
1786 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787
1788 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001789 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001790 if (!partial)
1791 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001792 new_i_size = pos + copied;
1793 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001794 i_size_write(inode, pos+copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001795 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001796 if (new_i_size > EXT4_I(inode)->i_disksize) {
1797 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001798 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001799 if (!ret)
1800 ret = ret2;
1801 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001802
Jan Karacf108bc2008-07-11 19:27:31 -04001803 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001804 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001805 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001806 /* if we have allocated more blocks and copied
1807 * less. We will have blocks allocated outside
1808 * inode->i_size. So truncate them
1809 */
1810 ext4_orphan_add(handle, inode);
1811
Mingming Cao617ba132006-10-11 01:20:53 -07001812 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813 if (!ret)
1814 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001815 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001816 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001817 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001818 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001819 * on the orphan list; we need to make sure the inode
1820 * is removed from the orphan list in that case.
1821 */
1822 if (inode->i_nlink)
1823 ext4_orphan_del(NULL, inode);
1824 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001825
1826 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001827}
Mingming Caod2a17632008-07-14 17:52:37 -04001828
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001829/*
1830 * Reserve a single block located at lblock
1831 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001832static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001833{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001834 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001835 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001836 struct ext4_inode_info *ei = EXT4_I(inode);
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001837 unsigned long md_needed;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001838 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001839
1840 /*
1841 * recalculate the amount of metadata blocks to reserve
1842 * in order to allocate nrblocks
1843 * worse case is one extent per block
1844 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001845repeat:
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001846 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001847 md_needed = ext4_calc_metadata_amount(inode, lblock);
Theodore Ts'of8ec9d62010-01-01 01:00:21 -05001848 trace_ext4_da_reserve_space(inode, md_needed);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001849 spin_unlock(&ei->i_block_reservation_lock);
Mingming Caod2a17632008-07-14 17:52:37 -04001850
Mingming Cao60e58e02009-01-22 18:13:05 +01001851 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001852 * We will charge metadata quota at writeout time; this saves
1853 * us from metadata over-estimation, though we may go over by
1854 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001855 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001856 ret = dquot_reserve_block(inode, 1);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001857 if (ret)
1858 return ret;
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001859 /*
1860 * We do still charge estimated metadata to the sb though;
1861 * we cannot afford to run out of free blocks.
1862 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001863 if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) {
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001864 dquot_release_reservation_block(inode, 1);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001865 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1866 yield();
1867 goto repeat;
1868 }
Mingming Caod2a17632008-07-14 17:52:37 -04001869 return -ENOSPC;
1870 }
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001871 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001872 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001873 ei->i_reserved_meta_blocks += md_needed;
1874 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001875
Mingming Caod2a17632008-07-14 17:52:37 -04001876 return 0; /* success */
1877}
1878
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001879static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001880{
1881 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001882 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001883
Mingming Caocd213222008-08-19 22:16:59 -04001884 if (!to_free)
1885 return; /* Nothing to release, exit */
1886
Mingming Caod2a17632008-07-14 17:52:37 -04001887 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001888
Li Zefan5a58ec872010-05-17 02:00:00 -04001889 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-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'o0637c6f2009-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'o0637c6f2009-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 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001912 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1913 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001914 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001915 ei->i_da_metadata_calc_len = 0;
Mingming Caocd213222008-08-19 22:16:59 -04001916 }
1917
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001918 /* update fs dirty data blocks counter */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001919 percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001920
Mingming Caod2a17632008-07-14 17:52:37 -04001921 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001922
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001923 dquot_release_reservation_block(inode, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001924}
1925
1926static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001927 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001928{
1929 int to_release = 0;
1930 struct buffer_head *head, *bh;
1931 unsigned int curr_off = 0;
1932
1933 head = page_buffers(page);
1934 bh = head;
1935 do {
1936 unsigned int next_off = curr_off + bh->b_size;
1937
1938 if ((offset <= curr_off) && (buffer_delay(bh))) {
1939 to_release++;
1940 clear_buffer_delay(bh);
1941 }
1942 curr_off = next_off;
1943 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001944 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001945}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946
1947/*
Alex Tomas64769242008-07-11 19:27:31 -04001948 * Delayed allocation stuff
1949 */
1950
Alex Tomas64769242008-07-11 19:27:31 -04001951/*
1952 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001953 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001954 *
1955 * @mpd->inode: inode
1956 * @mpd->first_page: first page of the extent
1957 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001958 *
1959 * By the time mpage_da_submit_io() is called we expect all blocks
1960 * to be allocated. this may be wrong if allocation failed.
1961 *
1962 * As pages are already locked by write_cache_pages(), we can't use it
1963 */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001964static int mpage_da_submit_io(struct mpage_da_data *mpd,
1965 struct ext4_map_blocks *map)
Alex Tomas64769242008-07-11 19:27:31 -04001966{
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001967 struct pagevec pvec;
1968 unsigned long index, end;
1969 int ret = 0, err, nr_pages, i;
1970 struct inode *inode = mpd->inode;
1971 struct address_space *mapping = inode->i_mapping;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001972 loff_t size = i_size_read(inode);
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001973 unsigned int len, block_start;
1974 struct buffer_head *bh, *page_bufs = NULL;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001975 int journal_data = ext4_should_journal_data(inode);
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001976 sector_t pblock = 0, cur_logical = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001977 struct ext4_io_submit io_submit;
Alex Tomas64769242008-07-11 19:27:31 -04001978
1979 BUG_ON(mpd->next_page <= mpd->first_page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001980 memset(&io_submit, 0, sizeof(io_submit));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001981 /*
1982 * We need to start from the first_page to the next_page - 1
1983 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001984 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001985 * at the currently mapped buffer_heads.
1986 */
Alex Tomas64769242008-07-11 19:27:31 -04001987 index = mpd->first_page;
1988 end = mpd->next_page - 1;
1989
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001990 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001991 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001992 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001993 if (nr_pages == 0)
1994 break;
1995 for (i = 0; i < nr_pages; i++) {
Theodore Ts'o97498952011-02-26 14:08:01 -05001996 int commit_write = 0, skip_page = 0;
Alex Tomas64769242008-07-11 19:27:31 -04001997 struct page *page = pvec.pages[i];
1998
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001999 index = page->index;
2000 if (index > end)
2001 break;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002002
2003 if (index == size >> PAGE_CACHE_SHIFT)
2004 len = size & ~PAGE_CACHE_MASK;
2005 else
2006 len = PAGE_CACHE_SIZE;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002007 if (map) {
2008 cur_logical = index << (PAGE_CACHE_SHIFT -
2009 inode->i_blkbits);
2010 pblock = map->m_pblk + (cur_logical -
2011 map->m_lblk);
2012 }
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002013 index++;
2014
2015 BUG_ON(!PageLocked(page));
2016 BUG_ON(PageWriteback(page));
2017
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002018 /*
2019 * If the page does not have buffers (for
2020 * whatever reason), try to create them using
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002021 * __block_write_begin. If this fails,
Theodore Ts'o97498952011-02-26 14:08:01 -05002022 * skip the page and move on.
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002023 */
2024 if (!page_has_buffers(page)) {
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002025 if (__block_write_begin(page, 0, len,
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002026 noalloc_get_block_write)) {
Theodore Ts'o97498952011-02-26 14:08:01 -05002027 skip_page:
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002028 unlock_page(page);
2029 continue;
2030 }
2031 commit_write = 1;
2032 }
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04002033
2034 bh = page_bufs = page_buffers(page);
2035 block_start = 0;
2036 do {
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002037 if (!bh)
Theodore Ts'o97498952011-02-26 14:08:01 -05002038 goto skip_page;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002039 if (map && (cur_logical >= map->m_lblk) &&
2040 (cur_logical <= (map->m_lblk +
2041 (map->m_len - 1)))) {
2042 if (buffer_delay(bh)) {
2043 clear_buffer_delay(bh);
2044 bh->b_blocknr = pblock;
2045 }
2046 if (buffer_unwritten(bh) ||
2047 buffer_mapped(bh))
2048 BUG_ON(bh->b_blocknr != pblock);
2049 if (map->m_flags & EXT4_MAP_UNINIT)
2050 set_buffer_uninit(bh);
2051 clear_buffer_unwritten(bh);
2052 }
2053
Theodore Ts'o97498952011-02-26 14:08:01 -05002054 /* skip page if block allocation undone */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002055 if (buffer_delay(bh) || buffer_unwritten(bh))
Theodore Ts'o97498952011-02-26 14:08:01 -05002056 skip_page = 1;
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04002057 bh = bh->b_this_page;
2058 block_start += bh->b_size;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002059 cur_logical++;
2060 pblock++;
2061 } while (bh != page_bufs);
2062
Theodore Ts'o97498952011-02-26 14:08:01 -05002063 if (skip_page)
2064 goto skip_page;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002065
2066 if (commit_write)
2067 /* mark the buffer_heads as dirty & uptodate */
2068 block_commit_write(page, 0, len);
2069
Theodore Ts'o97498952011-02-26 14:08:01 -05002070 clear_page_dirty_for_io(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002071 /*
2072 * Delalloc doesn't support data journalling,
2073 * but eventually maybe we'll lift this
2074 * restriction.
2075 */
2076 if (unlikely(journal_data && PageChecked(page)))
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002077 err = __ext4_journalled_writepage(page, len);
Theodore Ts'o14490322010-12-14 15:27:50 -05002078 else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT))
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002079 err = ext4_bio_write_page(&io_submit, page,
2080 len, mpd->wbc);
Theodore Ts'o14490322010-12-14 15:27:50 -05002081 else
2082 err = block_write_full_page(page,
2083 noalloc_get_block_write, mpd->wbc);
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002084
2085 if (!err)
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002086 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04002087 /*
2088 * In error case, we have to continue because
2089 * remaining pages are still locked
Alex Tomas64769242008-07-11 19:27:31 -04002090 */
2091 if (ret == 0)
2092 ret = err;
2093 }
2094 pagevec_release(&pvec);
2095 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002096 ext4_io_submit(&io_submit);
Alex Tomas64769242008-07-11 19:27:31 -04002097 return ret;
2098}
2099
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05002100static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002101{
2102 int nr_pages, i;
2103 pgoff_t index, end;
2104 struct pagevec pvec;
2105 struct inode *inode = mpd->inode;
2106 struct address_space *mapping = inode->i_mapping;
2107
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05002108 index = mpd->first_page;
2109 end = mpd->next_page - 1;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002110 while (index <= end) {
2111 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2112 if (nr_pages == 0)
2113 break;
2114 for (i = 0; i < nr_pages; i++) {
2115 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05002116 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002117 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002118 BUG_ON(!PageLocked(page));
2119 BUG_ON(PageWriteback(page));
2120 block_invalidatepage(page, 0);
2121 ClearPageUptodate(page);
2122 unlock_page(page);
2123 }
Jan Kara9b1d09982010-03-03 16:19:32 -05002124 index = pvec.pages[nr_pages - 1]->index + 1;
2125 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002126 }
2127 return;
2128}
2129
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002130static void ext4_print_free_blocks(struct inode *inode)
2131{
2132 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o16939182009-09-26 17:43:59 -04002133 printk(KERN_CRIT "Total free blocks count %lld\n",
2134 ext4_count_free_blocks(inode->i_sb));
2135 printk(KERN_CRIT "Free/Dirty block details\n");
2136 printk(KERN_CRIT "free_blocks=%lld\n",
2137 (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
2138 printk(KERN_CRIT "dirty_blocks=%lld\n",
2139 (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2140 printk(KERN_CRIT "Block reservation details\n");
2141 printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
2142 EXT4_I(inode)->i_reserved_data_blocks);
2143 printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
2144 EXT4_I(inode)->i_reserved_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002145 return;
2146}
2147
Theodore Ts'ob920c752009-05-14 00:54:29 -04002148/*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002149 * mpage_da_map_and_submit - go through given space, map them
2150 * if necessary, and then submit them for I/O
Alex Tomas64769242008-07-11 19:27:31 -04002151 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002152 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002153 *
2154 * The function skips space we know is already mapped to disk blocks.
2155 *
Alex Tomas64769242008-07-11 19:27:31 -04002156 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002157static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002158{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002159 int err, blks, get_blocks_flags;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002160 struct ext4_map_blocks map, *mapp = NULL;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002161 sector_t next = mpd->b_blocknr;
2162 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2163 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2164 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002165
2166 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002167 * If the blocks are mapped already, or we couldn't accumulate
2168 * any blocks, then proceed immediately to the submission stage.
Alex Tomas64769242008-07-11 19:27:31 -04002169 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002170 if ((mpd->b_size == 0) ||
2171 ((mpd->b_state & (1 << BH_Mapped)) &&
2172 !(mpd->b_state & (1 << BH_Delay)) &&
2173 !(mpd->b_state & (1 << BH_Unwritten))))
2174 goto submit_io;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002175
2176 handle = ext4_journal_current_handle();
2177 BUG_ON(!handle);
2178
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002179 /*
Eric Sandeen79e83032010-07-27 11:56:07 -04002180 * Call ext4_map_blocks() to allocate any delayed allocation
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002181 * blocks, or to convert an uninitialized extent to be
2182 * initialized (in the case where we have written into
2183 * one or more preallocated blocks).
2184 *
2185 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2186 * indicate that we are on the delayed allocation path. This
2187 * affects functions in many different parts of the allocation
2188 * call path. This flag exists primarily because we don't
Eric Sandeen79e83032010-07-27 11:56:07 -04002189 * want to change *many* call functions, so ext4_map_blocks()
Theodore Ts'of2321092011-01-10 12:12:36 -05002190 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002191 * inode's allocation semaphore is taken.
2192 *
2193 * If the blocks in questions were delalloc blocks, set
2194 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2195 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002196 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002197 map.m_lblk = next;
2198 map.m_len = max_blocks;
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05002199 get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002200 if (ext4_should_dioread_nolock(mpd->inode))
2201 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002202 if (mpd->b_state & (1 << BH_Delay))
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05002203 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2204
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002205 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002206 if (blks < 0) {
Eric Sandeene3570632010-07-27 11:56:08 -04002207 struct super_block *sb = mpd->inode->i_sb;
2208
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002209 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002210 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002211 * If get block returns EAGAIN or ENOSPC and there
Theodore Ts'o97498952011-02-26 14:08:01 -05002212 * appears to be free blocks we will just let
2213 * mpage_da_submit_io() unlock all of the pages.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002214 */
2215 if (err == -EAGAIN)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002216 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002217
2218 if (err == -ENOSPC &&
Eric Sandeene3570632010-07-27 11:56:08 -04002219 ext4_count_free_blocks(sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002220 mpd->retval = err;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002221 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002222 }
2223
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002224 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002225 * get block failure will cause us to loop in
2226 * writepages, because a_ops->writepage won't be able
2227 * to make progress. The page will be redirtied by
2228 * writepage and writepages will again try to write
2229 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002230 */
Eric Sandeene3570632010-07-27 11:56:08 -04002231 if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2232 ext4_msg(sb, KERN_CRIT,
2233 "delayed block allocation failed for inode %lu "
2234 "at logical offset %llu with max blocks %zd "
2235 "with error %d", mpd->inode->i_ino,
2236 (unsigned long long) next,
2237 mpd->b_size >> mpd->inode->i_blkbits, err);
2238 ext4_msg(sb, KERN_CRIT,
2239 "This should not happen!! Data will be lost\n");
2240 if (err == -ENOSPC)
2241 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002242 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002243 /* invalidate all the pages */
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05002244 ext4_da_block_invalidatepages(mpd);
Curt Wohlgemuthe0fd9b92011-02-26 12:25:52 -05002245
2246 /* Mark this page range as having been completed */
2247 mpd->io_done = 1;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002248 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002249 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002250 BUG_ON(blks == 0);
2251
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002252 mapp = &map;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002253 if (map.m_flags & EXT4_MAP_NEW) {
2254 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
2255 int i;
Alex Tomas64769242008-07-11 19:27:31 -04002256
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002257 for (i = 0; i < map.m_len; i++)
2258 unmap_underlying_metadata(bdev, map.m_pblk + i);
2259 }
Alex Tomas64769242008-07-11 19:27:31 -04002260
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002261 if (ext4_should_order_data(mpd->inode)) {
2262 err = ext4_jbd2_file_inode(handle, mpd->inode);
2263 if (err)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002264 /* This only happens if the journal is aborted */
2265 return;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002266 }
2267
2268 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002269 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002270 */
2271 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2272 if (disksize > i_size_read(mpd->inode))
2273 disksize = i_size_read(mpd->inode);
2274 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2275 ext4_update_i_disksize(mpd->inode, disksize);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002276 err = ext4_mark_inode_dirty(handle, mpd->inode);
2277 if (err)
2278 ext4_error(mpd->inode->i_sb,
2279 "Failed to mark inode %lu dirty",
2280 mpd->inode->i_ino);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002281 }
2282
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002283submit_io:
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002284 mpage_da_submit_io(mpd, mapp);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002285 mpd->io_done = 1;
Alex Tomas64769242008-07-11 19:27:31 -04002286}
2287
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002288#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2289 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002290
2291/*
2292 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2293 *
2294 * @mpd->lbh - extent of blocks
2295 * @logical - logical number of the block in the file
2296 * @bh - bh of the block (used to access block's state)
2297 *
2298 * the function is used to collect contig. blocks in same state
2299 */
2300static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002301 sector_t logical, size_t b_size,
2302 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002303{
Alex Tomas64769242008-07-11 19:27:31 -04002304 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002305 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002306
Eric Sandeenc445e3e2010-05-16 04:00:00 -04002307 /*
2308 * XXX Don't go larger than mballoc is willing to allocate
2309 * This is a stopgap solution. We eventually need to fold
2310 * mpage_da_submit_io() into this function and then call
Eric Sandeen79e83032010-07-27 11:56:07 -04002311 * ext4_map_blocks() multiple times in a loop
Eric Sandeenc445e3e2010-05-16 04:00:00 -04002312 */
2313 if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
2314 goto flush_it;
2315
Mingming Cao525f4ed2008-08-19 22:15:58 -04002316 /* check if thereserved journal credits might overflow */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002317 if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04002318 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2319 /*
2320 * With non-extent format we are limited by the journal
2321 * credit available. Total credit needed to insert
2322 * nrblocks contiguous blocks is dependent on the
2323 * nrblocks. So limit nrblocks.
2324 */
2325 goto flush_it;
2326 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2327 EXT4_MAX_TRANS_DATA) {
2328 /*
2329 * Adding the new buffer_head would make it cross the
2330 * allowed limit for which we have journal credit
2331 * reserved. So limit the new bh->b_size
2332 */
2333 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2334 mpd->inode->i_blkbits;
2335 /* we will do mpage_da_submit_io in the next loop */
2336 }
2337 }
Alex Tomas64769242008-07-11 19:27:31 -04002338 /*
2339 * First block in the extent
2340 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002341 if (mpd->b_size == 0) {
2342 mpd->b_blocknr = logical;
2343 mpd->b_size = b_size;
2344 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002345 return;
2346 }
2347
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002348 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002349 /*
2350 * Can we merge the block to our big extent?
2351 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002352 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2353 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002354 return;
2355 }
2356
Mingming Cao525f4ed2008-08-19 22:15:58 -04002357flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002358 /*
2359 * We couldn't merge the block to our extent, so we
2360 * need to flush current extent and start new one
2361 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002362 mpage_da_map_and_submit(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002363 return;
Alex Tomas64769242008-07-11 19:27:31 -04002364}
2365
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002366static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002367{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002368 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002369}
2370
Alex Tomas64769242008-07-11 19:27:31 -04002371/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002372 * This is a special get_blocks_t callback which is used by
2373 * ext4_da_write_begin(). It will either return mapped block or
2374 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002375 *
2376 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2377 * We also have b_blocknr = -1 and b_bdev initialized properly
2378 *
2379 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2380 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2381 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002382 */
2383static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002384 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04002385{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002386 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04002387 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002388 sector_t invalid_block = ~((sector_t) 0xffff);
2389
2390 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2391 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002392
2393 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002394 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
2395
2396 map.m_lblk = iblock;
2397 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04002398
2399 /*
2400 * first, we need to know whether the block is allocated already
2401 * preallocated blocks are unmapped but should treated
2402 * the same as allocated blocks.
2403 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002404 ret = ext4_map_blocks(NULL, inode, &map, 0);
2405 if (ret < 0)
2406 return ret;
2407 if (ret == 0) {
2408 if (buffer_delay(bh))
2409 return 0; /* Not sure this could or should happen */
Alex Tomas64769242008-07-11 19:27:31 -04002410 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02002411 * XXX: __block_write_begin() unmaps passed block, is it OK?
Alex Tomas64769242008-07-11 19:27:31 -04002412 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05002413 ret = ext4_da_reserve_space(inode, iblock);
Mingming Caod2a17632008-07-14 17:52:37 -04002414 if (ret)
2415 /* not enough space to reserve */
2416 return ret;
2417
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002418 map_bh(bh, inode->i_sb, invalid_block);
2419 set_buffer_new(bh);
2420 set_buffer_delay(bh);
2421 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002422 }
2423
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002424 map_bh(bh, inode->i_sb, map.m_pblk);
2425 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
2426
2427 if (buffer_unwritten(bh)) {
2428 /* A delayed write to unwritten bh should be marked
2429 * new and mapped. Mapped ensures that we don't do
2430 * get_block multiple times when we write to the same
2431 * offset and new ensures that we do proper zero out
2432 * for partial write.
2433 */
2434 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04002435 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002436 }
2437 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002438}
Mingming Cao61628a32008-07-11 19:27:31 -04002439
Theodore Ts'ob920c752009-05-14 00:54:29 -04002440/*
2441 * This function is used as a standard get_block_t calback function
2442 * when there is no desire to allocate any blocks. It is used as a
Christoph Hellwigebdec242010-10-06 10:47:23 +02002443 * callback function for block_write_begin() and block_write_full_page().
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04002444 * These functions should only try to map a single block at a time.
Theodore Ts'ob920c752009-05-14 00:54:29 -04002445 *
2446 * Since this function doesn't do block allocations even if the caller
2447 * requests it by passing in create=1, it is critically important that
2448 * any caller checks to make sure that any buffer heads are returned
2449 * by this function are either all already mapped or marked for
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04002450 * delayed allocation before calling block_write_full_page(). Otherwise,
2451 * b_blocknr could be left unitialized, and the page write functions will
2452 * be taken by surprise.
Theodore Ts'ob920c752009-05-14 00:54:29 -04002453 */
2454static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002455 struct buffer_head *bh_result, int create)
2456{
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002457 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002458 return _ext4_get_block(inode, iblock, bh_result, 0);
Mingming Cao61628a32008-07-11 19:27:31 -04002459}
2460
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002461static int bget_one(handle_t *handle, struct buffer_head *bh)
2462{
2463 get_bh(bh);
2464 return 0;
2465}
2466
2467static int bput_one(handle_t *handle, struct buffer_head *bh)
2468{
2469 put_bh(bh);
2470 return 0;
2471}
2472
2473static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002474 unsigned int len)
2475{
2476 struct address_space *mapping = page->mapping;
2477 struct inode *inode = mapping->host;
2478 struct buffer_head *page_bufs;
2479 handle_t *handle = NULL;
2480 int ret = 0;
2481 int err;
2482
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002483 ClearPageChecked(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002484 page_bufs = page_buffers(page);
2485 BUG_ON(!page_bufs);
2486 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2487 /* As soon as we unlock the page, it can go away, but we have
2488 * references to buffers so we are safe */
2489 unlock_page(page);
2490
2491 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2492 if (IS_ERR(handle)) {
2493 ret = PTR_ERR(handle);
2494 goto out;
2495 }
2496
2497 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2498 do_journal_get_write_access);
2499
2500 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2501 write_end_fn);
2502 if (ret == 0)
2503 ret = err;
2504 err = ext4_journal_stop(handle);
2505 if (!ret)
2506 ret = err;
2507
2508 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002509 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002510out:
2511 return ret;
2512}
2513
Jiaying Zhang744692d2010-03-04 16:14:02 -05002514static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
2515static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
2516
Mingming Cao61628a32008-07-11 19:27:31 -04002517/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002518 * Note that we don't need to start a transaction unless we're journaling data
2519 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2520 * need to file the inode to the transaction's list in ordered mode because if
2521 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002522 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002523 * transaction the data will hit the disk. In case we are journaling data, we
2524 * cannot start transaction directly because transaction start ranks above page
2525 * lock so we have to do some magic.
2526 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002527 * This function can get called via...
2528 * - ext4_da_writepages after taking page lock (have journal handle)
2529 * - journal_submit_inode_data_buffers (no journal handle)
2530 * - shrink_page_list via pdflush (no journal handle)
2531 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002532 *
2533 * We don't do any block allocation in this function. If we have page with
2534 * multiple blocks we need to write those buffer_heads that are mapped. This
2535 * is important for mmaped based write. So if we do with blocksize 1K
2536 * truncate(f, 1024);
2537 * a = mmap(f, 0, 4096);
2538 * a[0] = 'a';
2539 * truncate(f, 4096);
2540 * we have in the page first buffer_head mapped via page_mkwrite call back
2541 * but other bufer_heads would be unmapped but dirty(dirty done via the
2542 * do_wp_page). So writepage should write the first block. If we modify
2543 * the mmap area beyond 1024 we will again get a page_fault and the
2544 * page_mkwrite callback will do the block allocation and mark the
2545 * buffer_heads mapped.
2546 *
2547 * We redirty the page if we have any buffer_heads that is either delay or
2548 * unwritten in the page.
2549 *
2550 * We can get recursively called as show below.
2551 *
2552 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2553 * ext4_writepage()
2554 *
2555 * But since we don't do any block allocation we should not deadlock.
2556 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002557 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002558static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002559 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002560{
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002561 int ret = 0, commit_write = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002562 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002563 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002564 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04002565 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002566
Lukas Czernera9c667f2011-06-06 09:51:52 -04002567 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002568 size = i_size_read(inode);
2569 if (page->index == size >> PAGE_CACHE_SHIFT)
2570 len = size & ~PAGE_CACHE_MASK;
2571 else
2572 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002573
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002574 /*
2575 * If the page does not have buffers (for whatever reason),
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002576 * try to create them using __block_write_begin. If this
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002577 * fails, redirty the page and move on.
2578 */
Theodore Ts'ob1142e82010-10-28 17:33:57 -04002579 if (!page_has_buffers(page)) {
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002580 if (__block_write_begin(page, 0, len,
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002581 noalloc_get_block_write)) {
2582 redirty_page:
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002583 redirty_page_for_writepage(wbc, page);
2584 unlock_page(page);
2585 return 0;
2586 }
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002587 commit_write = 1;
2588 }
2589 page_bufs = page_buffers(page);
2590 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2591 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002592 /*
Theodore Ts'ob1142e82010-10-28 17:33:57 -04002593 * We don't want to do block allocation, so redirty
2594 * the page and return. We may reach here when we do
2595 * a journal commit via journal_submit_inode_data_buffers.
2596 * We can also reach here via shrink_page_list
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002597 */
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002598 goto redirty_page;
2599 }
2600 if (commit_write)
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002601 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002602 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002603
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002604 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002605 /*
2606 * It's mmapped pagecache. Add buffers and journal it. There
2607 * doesn't seem much point in redirtying the page here.
2608 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002609 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002610
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002611 if (buffer_uninit(page_bufs)) {
Jiaying Zhang744692d2010-03-04 16:14:02 -05002612 ext4_set_bh_endio(page_bufs, inode);
2613 ret = block_write_full_page_endio(page, noalloc_get_block_write,
2614 wbc, ext4_end_io_buffer_write);
2615 } else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002616 ret = block_write_full_page(page, noalloc_get_block_write,
2617 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002618
Alex Tomas64769242008-07-11 19:27:31 -04002619 return ret;
2620}
2621
Mingming Cao61628a32008-07-11 19:27:31 -04002622/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002623 * This is called via ext4_da_writepages() to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002624 * calculate the total number of credits to reserve to fit
Mingming Cao525f4ed2008-08-19 22:15:58 -04002625 * a single extent allocation into a single transaction,
2626 * ext4_da_writpeages() will loop calling this before
2627 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002628 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002629
2630static int ext4_da_writepages_trans_blocks(struct inode *inode)
2631{
2632 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2633
2634 /*
2635 * With non-extent format the journal credit needed to
2636 * insert nrblocks contiguous block is dependent on
2637 * number of contiguous block. So we will limit
2638 * number of contiguous block to a sane value
2639 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002640 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
Mingming Cao525f4ed2008-08-19 22:15:58 -04002641 (max_blocks > EXT4_MAX_TRANS_DATA))
2642 max_blocks = EXT4_MAX_TRANS_DATA;
2643
2644 return ext4_chunk_trans_blocks(inode, max_blocks);
2645}
Mingming Cao61628a32008-07-11 19:27:31 -04002646
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002647/*
2648 * write_cache_pages_da - walk the list of dirty pages of the given
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002649 * address space and accumulate pages that need writing, and call
Theodore Ts'o168fc022011-02-26 14:09:20 -05002650 * mpage_da_map_and_submit to map a single contiguous memory region
2651 * and then write them.
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002652 */
2653static int write_cache_pages_da(struct address_space *mapping,
2654 struct writeback_control *wbc,
Eric Sandeen72f84e62010-10-27 21:30:13 -04002655 struct mpage_da_data *mpd,
2656 pgoff_t *done_index)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002657{
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002658 struct buffer_head *bh, *head;
Theodore Ts'o168fc022011-02-26 14:09:20 -05002659 struct inode *inode = mapping->host;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002660 struct pagevec pvec;
2661 unsigned int nr_pages;
2662 sector_t logical;
2663 pgoff_t index, end;
2664 long nr_to_write = wbc->nr_to_write;
2665 int i, tag, ret = 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002666
Theodore Ts'o168fc022011-02-26 14:09:20 -05002667 memset(mpd, 0, sizeof(struct mpage_da_data));
2668 mpd->wbc = wbc;
2669 mpd->inode = inode;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002670 pagevec_init(&pvec, 0);
2671 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2672 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2673
Eric Sandeen5b41d922010-10-27 21:30:13 -04002674 if (wbc->sync_mode == WB_SYNC_ALL)
2675 tag = PAGECACHE_TAG_TOWRITE;
2676 else
2677 tag = PAGECACHE_TAG_DIRTY;
2678
Eric Sandeen72f84e62010-10-27 21:30:13 -04002679 *done_index = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002680 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002681 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002682 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2683 if (nr_pages == 0)
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002684 return 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002685
2686 for (i = 0; i < nr_pages; i++) {
2687 struct page *page = pvec.pages[i];
2688
2689 /*
2690 * At this point, the page may be truncated or
2691 * invalidated (changing page->mapping to NULL), or
2692 * even swizzled back from swapper_space to tmpfs file
2693 * mapping. However, page->index will not change
2694 * because we have a reference on the page.
2695 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002696 if (page->index > end)
2697 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002698
Eric Sandeen72f84e62010-10-27 21:30:13 -04002699 *done_index = page->index + 1;
2700
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002701 /*
2702 * If we can't merge this page, and we have
2703 * accumulated an contiguous region, write it
2704 */
2705 if ((mpd->next_page != page->index) &&
2706 (mpd->next_page != mpd->first_page)) {
2707 mpage_da_map_and_submit(mpd);
2708 goto ret_extent_tail;
2709 }
2710
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002711 lock_page(page);
2712
2713 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002714 * If the page is no longer dirty, or its
2715 * mapping no longer corresponds to inode we
2716 * are writing (which means it has been
2717 * truncated or invalidated), or the page is
2718 * already under writeback and we are not
2719 * doing a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002720 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002721 if (!PageDirty(page) ||
2722 (PageWriteback(page) &&
2723 (wbc->sync_mode == WB_SYNC_NONE)) ||
2724 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002725 unlock_page(page);
2726 continue;
2727 }
2728
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002729 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002730 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002731
Theodore Ts'o168fc022011-02-26 14:09:20 -05002732 if (mpd->next_page != page->index)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002733 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002734 mpd->next_page = page->index + 1;
2735 logical = (sector_t) page->index <<
2736 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2737
2738 if (!page_has_buffers(page)) {
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002739 mpage_add_bh_to_extent(mpd, logical,
2740 PAGE_CACHE_SIZE,
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002741 (1 << BH_Dirty) | (1 << BH_Uptodate));
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002742 if (mpd->io_done)
2743 goto ret_extent_tail;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002744 } else {
2745 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002746 * Page with regular buffer heads,
2747 * just add all dirty ones
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002748 */
2749 head = page_buffers(page);
2750 bh = head;
2751 do {
2752 BUG_ON(buffer_locked(bh));
2753 /*
2754 * We need to try to allocate
2755 * unmapped blocks in the same page.
2756 * Otherwise we won't make progress
2757 * with the page in ext4_writepage
2758 */
2759 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
2760 mpage_add_bh_to_extent(mpd, logical,
2761 bh->b_size,
2762 bh->b_state);
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002763 if (mpd->io_done)
2764 goto ret_extent_tail;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002765 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2766 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002767 * mapped dirty buffer. We need
2768 * to update the b_state
2769 * because we look at b_state
2770 * in mpage_da_map_blocks. We
2771 * don't update b_size because
2772 * if we find an unmapped
2773 * buffer_head later we need to
2774 * use the b_state flag of that
2775 * buffer_head.
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002776 */
2777 if (mpd->b_size == 0)
2778 mpd->b_state = bh->b_state & BH_FLAGS;
2779 }
2780 logical++;
2781 } while ((bh = bh->b_this_page) != head);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002782 }
2783
2784 if (nr_to_write > 0) {
2785 nr_to_write--;
2786 if (nr_to_write == 0 &&
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002787 wbc->sync_mode == WB_SYNC_NONE)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002788 /*
2789 * We stop writing back only if we are
2790 * not doing integrity sync. In case of
2791 * integrity sync we have to keep going
2792 * because someone may be concurrently
2793 * dirtying pages, and we might have
2794 * synced a lot of newly appeared dirty
2795 * pages, but have not synced all of the
2796 * old dirty pages.
2797 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002798 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002799 }
2800 }
2801 pagevec_release(&pvec);
2802 cond_resched();
2803 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002804 return 0;
2805ret_extent_tail:
2806 ret = MPAGE_DA_EXTENT_TAIL;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002807out:
2808 pagevec_release(&pvec);
2809 cond_resched();
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002810 return ret;
2811}
2812
2813
Alex Tomas64769242008-07-11 19:27:31 -04002814static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002815 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002816{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002817 pgoff_t index;
2818 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002819 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002820 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002821 struct inode *inode = mapping->host;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002822 int pages_written = 0;
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002823 unsigned int max_pages;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002824 int range_cyclic, cycled = 1, io_done = 0;
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002825 int needed_blocks, ret = 0;
2826 long desired_nr_to_write, nr_to_writebump = 0;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002827 loff_t range_start = wbc->range_start;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002828 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Eric Sandeen72f84e62010-10-27 21:30:13 -04002829 pgoff_t done_index = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002830 pgoff_t end;
Mingming Cao61628a32008-07-11 19:27:31 -04002831
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002832 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002833
Mingming Cao61628a32008-07-11 19:27:31 -04002834 /*
2835 * No pages to write? This is mainly a kludge to avoid starting
2836 * a transaction for special inodes like journal inode on last iput()
2837 * because that could violate lock ordering on umount
2838 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002839 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002840 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002841
2842 /*
2843 * If the filesystem has aborted, it is read-only, so return
2844 * right away instead of dumping stack traces later on that
2845 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002846 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002847 * the latter could be true if the filesystem is mounted
2848 * read-only, and in that case, ext4_da_writepages should
2849 * *never* be called, so if that ever happens, we would want
2850 * the stack trace.
2851 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002852 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002853 return -EROFS;
2854
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002855 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2856 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002857
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002858 range_cyclic = wbc->range_cyclic;
2859 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002860 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002861 if (index)
2862 cycled = 0;
2863 wbc->range_start = index << PAGE_CACHE_SHIFT;
2864 wbc->range_end = LLONG_MAX;
2865 wbc->range_cyclic = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002866 end = -1;
2867 } else {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002868 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002869 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2870 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002871
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002872 /*
2873 * This works around two forms of stupidity. The first is in
2874 * the writeback code, which caps the maximum number of pages
2875 * written to be 1024 pages. This is wrong on multiple
2876 * levels; different architectues have a different page size,
2877 * which changes the maximum amount of data which gets
2878 * written. Secondly, 4 megabytes is way too small. XFS
2879 * forces this value to be 16 megabytes by multiplying
2880 * nr_to_write parameter by four, and then relies on its
2881 * allocator to allocate larger extents to make them
2882 * contiguous. Unfortunately this brings us to the second
2883 * stupidity, which is that ext4's mballoc code only allocates
2884 * at most 2048 blocks. So we force contiguous writes up to
2885 * the number of dirty blocks in the inode, or
2886 * sbi->max_writeback_mb_bump whichever is smaller.
2887 */
2888 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
Eric Sandeenb443e732010-10-27 21:30:03 -04002889 if (!range_cyclic && range_whole) {
2890 if (wbc->nr_to_write == LONG_MAX)
2891 desired_nr_to_write = wbc->nr_to_write;
2892 else
2893 desired_nr_to_write = wbc->nr_to_write * 8;
2894 } else
Theodore Ts'o55138e0b2009-09-29 13:31:31 -04002895 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2896 max_pages);
2897 if (desired_nr_to_write > max_pages)
2898 desired_nr_to_write = max_pages;
2899
2900 if (wbc->nr_to_write < desired_nr_to_write) {
2901 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2902 wbc->nr_to_write = desired_nr_to_write;
2903 }
2904
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002905retry:
Eric Sandeen5b41d922010-10-27 21:30:13 -04002906 if (wbc->sync_mode == WB_SYNC_ALL)
2907 tag_pages_for_writeback(mapping, index, end);
2908
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002909 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002910
2911 /*
2912 * we insert one extent at a time. So we need
2913 * credit needed for single extent allocation.
2914 * journalled mode is currently not supported
2915 * by delalloc
2916 */
2917 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002918 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002919
Mingming Cao61628a32008-07-11 19:27:31 -04002920 /* start a new transaction*/
2921 handle = ext4_journal_start(inode, needed_blocks);
2922 if (IS_ERR(handle)) {
2923 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002924 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002925 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002926 wbc->nr_to_write, inode->i_ino, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002927 goto out_writepages;
2928 }
Theodore Ts'of63e60052009-02-23 16:42:39 -05002929
2930 /*
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002931 * Now call write_cache_pages_da() to find the next
Theodore Ts'of63e60052009-02-23 16:42:39 -05002932 * contiguous region of logical blocks that need
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002933 * blocks to be allocated by ext4 and submit them.
Theodore Ts'of63e60052009-02-23 16:42:39 -05002934 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04002935 ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index);
Theodore Ts'of63e60052009-02-23 16:42:39 -05002936 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002937 * If we have a contiguous extent of pages and we
Theodore Ts'of63e60052009-02-23 16:42:39 -05002938 * haven't done the I/O yet, map the blocks and submit
2939 * them for I/O.
2940 */
2941 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002942 mpage_da_map_and_submit(&mpd);
Theodore Ts'of63e60052009-02-23 16:42:39 -05002943 ret = MPAGE_DA_EXTENT_TAIL;
2944 }
Theodore Ts'ob3a3ca82009-08-31 23:13:11 -04002945 trace_ext4_da_write_pages(inode, &mpd);
Theodore Ts'of63e60052009-02-23 16:42:39 -05002946 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002947
Mingming Cao61628a32008-07-11 19:27:31 -04002948 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002949
Eric Sandeen8f64b322009-02-26 00:57:35 -05002950 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002951 /* commit the transaction which would
2952 * free blocks released in the transaction
2953 * and try again
2954 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002955 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002956 ret = 0;
2957 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002958 /*
2959 * got one extent now try with
2960 * rest of the pages
2961 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002962 pages_written += mpd.pages_written;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002963 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002964 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002965 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002966 /*
2967 * There is no more writeout needed
2968 * or we requested for a noblocking writeout
2969 * and we found the device congested
2970 */
Mingming Cao61628a32008-07-11 19:27:31 -04002971 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002972 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002973 if (!io_done && !cycled) {
2974 cycled = 1;
2975 index = 0;
2976 wbc->range_start = index << PAGE_CACHE_SHIFT;
2977 wbc->range_end = mapping->writeback_index - 1;
2978 goto retry;
2979 }
Mingming Cao61628a32008-07-11 19:27:31 -04002980
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002981 /* Update index */
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002982 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002983 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2984 /*
2985 * set the writeback_index so that range_cyclic
2986 * mode will write it back later
2987 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04002988 mapping->writeback_index = done_index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002989
Mingming Cao61628a32008-07-11 19:27:31 -04002990out_writepages:
Richard Kennedy2faf2e12009-12-25 15:46:07 -05002991 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002992 wbc->range_start = range_start;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002993 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002994 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002995}
2996
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002997#define FALL_BACK_TO_NONDELALLOC 1
2998static int ext4_nonda_switch(struct super_block *sb)
2999{
3000 s64 free_blocks, dirty_blocks;
3001 struct ext4_sb_info *sbi = EXT4_SB(sb);
3002
3003 /*
3004 * switch to non delalloc mode if we are running low
3005 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08003006 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003007 * accumulated on each CPU without updating global counters
3008 * Delalloc need an accurate free block accounting. So switch
3009 * to non delalloc when we are near to error range.
3010 */
3011 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3012 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
3013 if (2 * free_blocks < 3 * dirty_blocks ||
3014 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
3015 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05003016 * free block count is less than 150% of dirty blocks
3017 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003018 */
3019 return 1;
3020 }
Eric Sandeenc8afb442009-12-23 07:58:12 -05003021 /*
3022 * Even if we don't switch but are nearing capacity,
3023 * start pushing delalloc when 1/2 of free blocks are dirty.
3024 */
3025 if (free_blocks < 2 * dirty_blocks)
3026 writeback_inodes_sb_if_idle(sb);
3027
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003028 return 0;
3029}
3030
Alex Tomas64769242008-07-11 19:27:31 -04003031static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003032 loff_t pos, unsigned len, unsigned flags,
3033 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003034{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04003035 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04003036 struct page *page;
3037 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04003038 struct inode *inode = mapping->host;
3039 handle_t *handle;
3040
3041 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003042
3043 if (ext4_nonda_switch(inode->i_sb)) {
3044 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
3045 return ext4_write_begin(file, mapping, pos,
3046 len, flags, pagep, fsdata);
3047 }
3048 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003049 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04003050retry:
Alex Tomas64769242008-07-11 19:27:31 -04003051 /*
3052 * With delayed allocation, we don't log the i_disksize update
3053 * if there is delayed block allocation. But we still need
3054 * to journalling the i_disksize update if writes to the end
3055 * of file which has an already mapped buffer.
3056 */
3057 handle = ext4_journal_start(inode, 1);
3058 if (IS_ERR(handle)) {
3059 ret = PTR_ERR(handle);
3060 goto out;
3061 }
Jan Karaebd36102009-02-22 21:09:59 -05003062 /* We cannot recurse into the filesystem as the transaction is already
3063 * started */
3064 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04003065
Nick Piggin54566b22009-01-04 12:00:53 -08003066 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003067 if (!page) {
3068 ext4_journal_stop(handle);
3069 ret = -ENOMEM;
3070 goto out;
3071 }
Alex Tomas64769242008-07-11 19:27:31 -04003072 *pagep = page;
3073
Christoph Hellwig6e1db882010-06-04 11:29:57 +02003074 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04003075 if (ret < 0) {
3076 unlock_page(page);
3077 ext4_journal_stop(handle);
3078 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003079 /*
3080 * block_write_begin may have instantiated a few blocks
3081 * outside i_size. Trim these off again. Don't need
3082 * i_size_read because we hold i_mutex.
3083 */
3084 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003085 ext4_truncate_failed_write(inode);
Alex Tomas64769242008-07-11 19:27:31 -04003086 }
3087
Mingming Caod2a17632008-07-14 17:52:37 -04003088 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3089 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003090out:
3091 return ret;
3092}
3093
Mingming Cao632eaea2008-07-11 19:27:31 -04003094/*
3095 * Check if we should update i_disksize
3096 * when write to the end of file but not require block allocation
3097 */
3098static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003099 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003100{
3101 struct buffer_head *bh;
3102 struct inode *inode = page->mapping->host;
3103 unsigned int idx;
3104 int i;
3105
3106 bh = page_buffers(page);
3107 idx = offset >> inode->i_blkbits;
3108
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003109 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003110 bh = bh->b_this_page;
3111
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003112 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003113 return 0;
3114 return 1;
3115}
3116
Alex Tomas64769242008-07-11 19:27:31 -04003117static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003118 struct address_space *mapping,
3119 loff_t pos, unsigned len, unsigned copied,
3120 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003121{
3122 struct inode *inode = mapping->host;
3123 int ret = 0, ret2;
3124 handle_t *handle = ext4_journal_current_handle();
3125 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003126 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003127 int write_mode = (int)(unsigned long)fsdata;
3128
3129 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3130 if (ext4_should_order_data(inode)) {
3131 return ext4_ordered_write_end(file, mapping, pos,
3132 len, copied, page, fsdata);
3133 } else if (ext4_should_writeback_data(inode)) {
3134 return ext4_writeback_write_end(file, mapping, pos,
3135 len, copied, page, fsdata);
3136 } else {
3137 BUG();
3138 }
3139 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003140
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003141 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003142 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003143 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003144
3145 /*
3146 * generic_write_end() will run mark_inode_dirty() if i_size
3147 * changes. So let's piggyback the i_disksize mark_inode_dirty
3148 * into that.
3149 */
3150
3151 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003152 if (new_i_size > EXT4_I(inode)->i_disksize) {
3153 if (ext4_da_should_update_i_disksize(page, end)) {
3154 down_write(&EXT4_I(inode)->i_data_sem);
3155 if (new_i_size > EXT4_I(inode)->i_disksize) {
3156 /*
3157 * Updating i_disksize when extending file
3158 * without needing block allocation
3159 */
3160 if (ext4_should_order_data(inode))
3161 ret = ext4_jbd2_file_inode(handle,
3162 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003163
Mingming Cao632eaea2008-07-11 19:27:31 -04003164 EXT4_I(inode)->i_disksize = new_i_size;
3165 }
3166 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003167 /* We need to mark inode dirty even if
3168 * new_i_size is less that inode->i_size
3169 * bu greater than i_disksize.(hint delalloc)
3170 */
3171 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003172 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003173 }
Alex Tomas64769242008-07-11 19:27:31 -04003174 ret2 = generic_write_end(file, mapping, pos, len, copied,
3175 page, fsdata);
3176 copied = ret2;
3177 if (ret2 < 0)
3178 ret = ret2;
3179 ret2 = ext4_journal_stop(handle);
3180 if (!ret)
3181 ret = ret2;
3182
3183 return ret ? ret : copied;
3184}
3185
3186static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3187{
Alex Tomas64769242008-07-11 19:27:31 -04003188 /*
3189 * Drop reserved blocks
3190 */
3191 BUG_ON(!PageLocked(page));
3192 if (!page_has_buffers(page))
3193 goto out;
3194
Mingming Caod2a17632008-07-14 17:52:37 -04003195 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003196
3197out:
3198 ext4_invalidatepage(page, offset);
3199
3200 return;
3201}
3202
Theodore Ts'occd25062009-02-26 01:04:07 -05003203/*
3204 * Force all delayed allocation blocks to be allocated for a given inode.
3205 */
3206int ext4_alloc_da_blocks(struct inode *inode)
3207{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003208 trace_ext4_alloc_da_blocks(inode);
3209
Theodore Ts'occd25062009-02-26 01:04:07 -05003210 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3211 !EXT4_I(inode)->i_reserved_meta_blocks)
3212 return 0;
3213
3214 /*
3215 * We do something simple for now. The filemap_flush() will
3216 * also start triggering a write of the data blocks, which is
3217 * not strictly speaking necessary (and for users of
3218 * laptop_mode, not even desirable). However, to do otherwise
3219 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003220 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003221 * ext4_da_writepages() ->
3222 * write_cache_pages() ---> (via passed in callback function)
3223 * __mpage_da_writepage() -->
3224 * mpage_add_bh_to_extent()
3225 * mpage_da_map_blocks()
3226 *
3227 * The problem is that write_cache_pages(), located in
3228 * mm/page-writeback.c, marks pages clean in preparation for
3229 * doing I/O, which is not desirable if we're not planning on
3230 * doing I/O at all.
3231 *
3232 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003233 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003234 * would be ugly in the extreme. So instead we would need to
3235 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003236 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003237 * write out the pages, but rather only collect contiguous
3238 * logical block extents, call the multi-block allocator, and
3239 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003240 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003241 * For now, though, we'll cheat by calling filemap_flush(),
3242 * which will map the blocks, and start the I/O, but not
3243 * actually wait for the I/O to complete.
3244 */
3245 return filemap_flush(inode->i_mapping);
3246}
Alex Tomas64769242008-07-11 19:27:31 -04003247
3248/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003249 * bmap() is special. It gets used by applications such as lilo and by
3250 * the swapper to find the on-disk block of a specific piece of data.
3251 *
3252 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003253 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003254 * filesystem and enables swap, then they may get a nasty shock when the
3255 * data getting swapped to that swapfile suddenly gets overwritten by
3256 * the original zero's written out previously to the journal and
3257 * awaiting writeback in the kernel's buffer cache.
3258 *
3259 * So, if we see any bmap calls here on a modified, data-journaled file,
3260 * take extra steps to flush any blocks which might be in the cache.
3261 */
Mingming Cao617ba132006-10-11 01:20:53 -07003262static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003263{
3264 struct inode *inode = mapping->host;
3265 journal_t *journal;
3266 int err;
3267
Alex Tomas64769242008-07-11 19:27:31 -04003268 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3269 test_opt(inode->i_sb, DELALLOC)) {
3270 /*
3271 * With delalloc we want to sync the file
3272 * so that we can make sure we allocate
3273 * blocks for file
3274 */
3275 filemap_write_and_wait(mapping);
3276 }
3277
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003278 if (EXT4_JOURNAL(inode) &&
3279 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003280 /*
3281 * This is a REALLY heavyweight approach, but the use of
3282 * bmap on dirty files is expected to be extremely rare:
3283 * only if we run lilo or swapon on a freshly made file
3284 * do we expect this to happen.
3285 *
3286 * (bmap requires CAP_SYS_RAWIO so this does not
3287 * represent an unprivileged user DOS attack --- we'd be
3288 * in trouble if mortal users could trigger this path at
3289 * will.)
3290 *
Mingming Cao617ba132006-10-11 01:20:53 -07003291 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 * regular files. If somebody wants to bmap a directory
3293 * or symlink and gets confused because the buffer
3294 * hasn't yet been flushed to disk, they deserve
3295 * everything they get.
3296 */
3297
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003298 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003299 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003300 jbd2_journal_lock_updates(journal);
3301 err = jbd2_journal_flush(journal);
3302 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303
3304 if (err)
3305 return 0;
3306 }
3307
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003308 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309}
3310
Mingming Cao617ba132006-10-11 01:20:53 -07003311static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312{
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003313 trace_ext4_readpage(page);
Mingming Cao617ba132006-10-11 01:20:53 -07003314 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003315}
3316
3317static int
Mingming Cao617ba132006-10-11 01:20:53 -07003318ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003319 struct list_head *pages, unsigned nr_pages)
3320{
Mingming Cao617ba132006-10-11 01:20:53 -07003321 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322}
3323
Jiaying Zhang744692d2010-03-04 16:14:02 -05003324static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset)
3325{
3326 struct buffer_head *head, *bh;
3327 unsigned int curr_off = 0;
3328
3329 if (!page_has_buffers(page))
3330 return;
3331 head = bh = page_buffers(page);
3332 do {
3333 if (offset <= curr_off && test_clear_buffer_uninit(bh)
3334 && bh->b_private) {
3335 ext4_free_io_end(bh->b_private);
3336 bh->b_private = NULL;
3337 bh->b_end_io = NULL;
3338 }
3339 curr_off = curr_off + bh->b_size;
3340 bh = bh->b_this_page;
3341 } while (bh != head);
3342}
3343
Mingming Cao617ba132006-10-11 01:20:53 -07003344static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345{
Mingming Cao617ba132006-10-11 01:20:53 -07003346 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003347
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003348 trace_ext4_invalidatepage(page, offset);
3349
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003350 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003351 * free any io_end structure allocated for buffers to be discarded
3352 */
3353 if (ext4_should_dioread_nolock(page->mapping->host))
3354 ext4_invalidatepage_free_endio(page, offset);
3355 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003356 * If it's a full truncate we just forget about the pending dirtying
3357 */
3358 if (offset == 0)
3359 ClearPageChecked(page);
3360
Frank Mayhar03901312009-01-07 00:06:22 -05003361 if (journal)
3362 jbd2_journal_invalidatepage(journal, page, offset);
3363 else
3364 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003365}
3366
Mingming Cao617ba132006-10-11 01:20:53 -07003367static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003368{
Mingming Cao617ba132006-10-11 01:20:53 -07003369 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003370
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003371 trace_ext4_releasepage(page);
3372
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003373 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:
Jiaying Zhangb7adc1f2010-03-02 13:26:36 -05003428 if (rw == READ && ext4_should_dioread_nolock(inode))
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003429 ret = __blockdev_direct_IO(rw, iocb, inode,
Jiaying Zhangb7adc1f2010-03-02 13:26:36 -05003430 inode->i_sb->s_bdev, iov,
3431 offset, nr_segs,
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003432 ext4_get_block, NULL, NULL, 0);
3433 else {
Jiaying Zhangb7adc1f2010-03-02 13:26:36 -05003434 ret = blockdev_direct_IO(rw, iocb, inode,
3435 inode->i_sb->s_bdev, iov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003437 ext4_get_block, NULL);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003438
3439 if (unlikely((rw & WRITE) && ret < 0)) {
3440 loff_t isize = i_size_read(inode);
3441 loff_t end = offset + iov_length(iov, nr_segs);
3442
3443 if (end > isize)
Jan Karaae24f282011-05-25 17:39:48 -04003444 ext4_truncate_failed_write(inode);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003445 }
3446 }
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003447 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3448 goto retry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449
Jan Kara7fb54092008-02-10 01:08:38 -05003450 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003451 int err;
3452
Jan Kara7fb54092008-02-10 01:08:38 -05003453 /* Credits for sb + inode write */
3454 handle = ext4_journal_start(inode, 2);
3455 if (IS_ERR(handle)) {
3456 /* This is really bad luck. We've written the data
3457 * but cannot extend i_size. Bail out and pretend
3458 * the write failed... */
3459 ret = PTR_ERR(handle);
Dmitry Monakhovda1dafc2010-03-01 23:15:02 -05003460 if (inode->i_nlink)
3461 ext4_orphan_del(NULL, inode);
3462
Jan Kara7fb54092008-02-10 01:08:38 -05003463 goto out;
3464 }
3465 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003466 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003467 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003468 loff_t end = offset + ret;
3469 if (end > inode->i_size) {
3470 ei->i_disksize = end;
3471 i_size_write(inode, end);
3472 /*
3473 * We're going to return a positive `ret'
3474 * here due to non-zero-length I/O, so there's
3475 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003476 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003477 * ignore it.
3478 */
Mingming Cao617ba132006-10-11 01:20:53 -07003479 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003480 }
3481 }
Mingming Cao617ba132006-10-11 01:20:53 -07003482 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003483 if (ret == 0)
3484 ret = err;
3485 }
3486out:
3487 return ret;
3488}
3489
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003490/*
3491 * ext4_get_block used when preparing for a DIO write or buffer write.
3492 * We allocate an uinitialized extent if blocks haven't been allocated.
3493 * The extent will be converted to initialized after the IO is complete.
3494 */
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003495static int ext4_get_block_write(struct inode *inode, sector_t iblock,
Mingming Cao4c0425f2009-09-28 15:48:41 -04003496 struct buffer_head *bh_result, int create)
3497{
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003498 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003499 inode->i_ino, create);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003500 return _ext4_get_block(inode, iblock, bh_result,
3501 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003502}
3503
Mingming Cao4c0425f2009-09-28 15:48:41 -04003504static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig552ef8022010-07-27 11:56:06 -04003505 ssize_t size, void *private, int ret,
3506 bool is_async)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003507{
3508 ext4_io_end_t *io_end = iocb->private;
3509 struct workqueue_struct *wq;
Jiaying Zhang744692d2010-03-04 16:14:02 -05003510 unsigned long flags;
3511 struct ext4_inode_info *ei;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003512
Mingming4b70df12009-11-03 14:44:54 -05003513 /* if not async direct IO or dio with 0 bytes write, just return */
3514 if (!io_end || !size)
Christoph Hellwig552ef8022010-07-27 11:56:06 -04003515 goto out;
Mingming4b70df12009-11-03 14:44:54 -05003516
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003517 ext_debug("ext4_end_io_dio(): io_end 0x%p"
3518 "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
3519 iocb->private, io_end->inode->i_ino, iocb, offset,
3520 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003521
3522 /* if not aio dio with unwritten extents, just free io and return */
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003523 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003524 ext4_free_io_end(io_end);
3525 iocb->private = NULL;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04003526out:
3527 if (is_async)
3528 aio_complete(iocb, ret, 0);
3529 return;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003530 }
3531
Mingming Cao4c0425f2009-09-28 15:48:41 -04003532 io_end->offset = offset;
3533 io_end->size = size;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04003534 if (is_async) {
3535 io_end->iocb = iocb;
3536 io_end->result = ret;
3537 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003538 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
3539
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003540 /* Add the io_end to per-inode completed aio dio list*/
Jiaying Zhang744692d2010-03-04 16:14:02 -05003541 ei = EXT4_I(io_end->inode);
3542 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
3543 list_add_tail(&io_end->list, &ei->i_completed_io_list);
3544 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
Eric Sandeenc999af22010-10-27 21:30:07 -04003545
3546 /* queue the work to convert unwritten extents to written */
3547 queue_work(wq, &io_end->work);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003548 iocb->private = NULL;
3549}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003550
Jiaying Zhang744692d2010-03-04 16:14:02 -05003551static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate)
3552{
3553 ext4_io_end_t *io_end = bh->b_private;
3554 struct workqueue_struct *wq;
3555 struct inode *inode;
3556 unsigned long flags;
3557
3558 if (!test_clear_buffer_uninit(bh) || !io_end)
3559 goto out;
3560
3561 if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) {
3562 printk("sb umounted, discard end_io request for inode %lu\n",
3563 io_end->inode->i_ino);
3564 ext4_free_io_end(io_end);
3565 goto out;
3566 }
3567
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003568 io_end->flag = EXT4_IO_END_UNWRITTEN;
Jiaying Zhang744692d2010-03-04 16:14:02 -05003569 inode = io_end->inode;
3570
3571 /* Add the io_end to per-inode completed io list*/
3572 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
3573 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
3574 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
3575
3576 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
3577 /* queue the work to convert unwritten extents to written */
3578 queue_work(wq, &io_end->work);
3579out:
3580 bh->b_private = NULL;
3581 bh->b_end_io = NULL;
3582 clear_buffer_uninit(bh);
3583 end_buffer_async_write(bh, uptodate);
3584}
3585
3586static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode)
3587{
3588 ext4_io_end_t *io_end;
3589 struct page *page = bh->b_page;
3590 loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT;
3591 size_t size = bh->b_size;
3592
3593retry:
3594 io_end = ext4_init_io_end(inode, GFP_ATOMIC);
3595 if (!io_end) {
Andrew Morton6db26ff2011-01-12 16:59:13 -08003596 pr_warn_ratelimited("%s: allocation fail\n", __func__);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003597 schedule();
3598 goto retry;
3599 }
3600 io_end->offset = offset;
3601 io_end->size = size;
3602 /*
3603 * We need to hold a reference to the page to make sure it
3604 * doesn't get evicted before ext4_end_io_work() has a chance
3605 * to convert the extent from written to unwritten.
3606 */
3607 io_end->page = page;
3608 get_page(io_end->page);
3609
3610 bh->b_private = io_end;
3611 bh->b_end_io = ext4_end_io_buffer_write;
3612 return 0;
3613}
3614
Mingming Cao4c0425f2009-09-28 15:48:41 -04003615/*
3616 * For ext4 extent files, ext4 will do direct-io write to holes,
3617 * preallocated extents, and those write extend the file, no need to
3618 * fall back to buffered IO.
3619 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003620 * For holes, we fallocate those blocks, mark them as uninitialized
Mingming Cao4c0425f2009-09-28 15:48:41 -04003621 * If those blocks were preallocated, we mark sure they are splited, but
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003622 * still keep the range to write as uninitialized.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003623 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003624 * The unwrritten extents will be converted to written when DIO is completed.
3625 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003626 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003627 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003628 *
3629 * If the O_DIRECT write will extend the file then add this inode to the
3630 * orphan list. So recovery will truncate it back to the original size
3631 * if the machine crashes during the write.
3632 *
3633 */
3634static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3635 const struct iovec *iov, loff_t offset,
3636 unsigned long nr_segs)
3637{
3638 struct file *file = iocb->ki_filp;
3639 struct inode *inode = file->f_mapping->host;
3640 ssize_t ret;
3641 size_t count = iov_length(iov, nr_segs);
3642
3643 loff_t final_size = offset + count;
3644 if (rw == WRITE && final_size <= inode->i_size) {
3645 /*
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003646 * We could direct write to holes and fallocate.
3647 *
3648 * Allocated blocks to fill the hole are marked as uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003649 * to prevent parallel buffered read to expose the stale data
Mingming Cao4c0425f2009-09-28 15:48:41 -04003650 * before DIO complete the data IO.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003651 *
3652 * As to previously fallocated extents, ext4 get_block
Mingming Cao4c0425f2009-09-28 15:48:41 -04003653 * will just simply mark the buffer mapped but still
3654 * keep the extents uninitialized.
3655 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003656 * for non AIO case, we will convert those unwritten extents
3657 * to written after return back from blockdev_direct_IO.
3658 *
3659 * for async DIO, the conversion needs to be defered when
3660 * the IO is completed. The ext4 end_io callback function
3661 * will be called to take care of the conversion work.
3662 * Here for async case, we allocate an io_end structure to
3663 * hook to the iocb.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003664 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003665 iocb->private = NULL;
3666 EXT4_I(inode)->cur_aio_dio = NULL;
3667 if (!is_sync_kiocb(iocb)) {
Jiaying Zhang744692d2010-03-04 16:14:02 -05003668 iocb->private = ext4_init_io_end(inode, GFP_NOFS);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003669 if (!iocb->private)
3670 return -ENOMEM;
3671 /*
3672 * we save the io structure for current async
Eric Sandeen79e83032010-07-27 11:56:07 -04003673 * direct IO, so that later ext4_map_blocks()
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003674 * could flag the io structure whether there
3675 * is a unwritten extents needs to be converted
3676 * when IO is completed.
3677 */
3678 EXT4_I(inode)->cur_aio_dio = iocb->private;
3679 }
3680
Mingming Cao4c0425f2009-09-28 15:48:41 -04003681 ret = blockdev_direct_IO(rw, iocb, inode,
3682 inode->i_sb->s_bdev, iov,
3683 offset, nr_segs,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003684 ext4_get_block_write,
Mingming Cao4c0425f2009-09-28 15:48:41 -04003685 ext4_end_io_dio);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003686 if (iocb->private)
3687 EXT4_I(inode)->cur_aio_dio = NULL;
3688 /*
3689 * The io_end structure takes a reference to the inode,
3690 * that structure needs to be destroyed and the
3691 * reference to the inode need to be dropped, when IO is
3692 * complete, even with 0 byte write, or failed.
3693 *
3694 * In the successful AIO DIO case, the io_end structure will be
3695 * desctroyed and the reference to the inode will be dropped
3696 * after the end_io call back function is called.
3697 *
3698 * In the case there is 0 byte write, or error case, since
3699 * VFS direct IO won't invoke the end_io call back function,
3700 * we need to free the end_io structure here.
3701 */
3702 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3703 ext4_free_io_end(iocb->private);
3704 iocb->private = NULL;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003705 } else if (ret > 0 && ext4_test_inode_state(inode,
3706 EXT4_STATE_DIO_UNWRITTEN)) {
Mingming109f5562009-11-10 10:48:08 -05003707 int err;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003708 /*
3709 * for non AIO case, since the IO is already
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003710 * completed, we could do the conversion right here
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003711 */
Mingming109f5562009-11-10 10:48:08 -05003712 err = ext4_convert_unwritten_extents(inode,
3713 offset, ret);
3714 if (err < 0)
3715 ret = err;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003716 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Mingming109f5562009-11-10 10:48:08 -05003717 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003718 return ret;
3719 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003720
3721 /* for write the the end of file case, we fall back to old way */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003722 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3723}
3724
3725static ssize_t ext4_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;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003731 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003732
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003733 trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003734 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003735 ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3736 else
3737 ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3738 trace_ext4_direct_IO_exit(inode, offset,
3739 iov_length(iov, nr_segs), rw, ret);
3740 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003741}
3742
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003743/*
Mingming Cao617ba132006-10-11 01:20:53 -07003744 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003745 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3746 * much here because ->set_page_dirty is called under VFS locks. The page is
3747 * not necessarily locked.
3748 *
3749 * We cannot just dirty the page and leave attached buffers clean, because the
3750 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3751 * or jbddirty because all the journalling code will explode.
3752 *
3753 * So what we do is to mark the page "pending dirty" and next time writepage
3754 * is called, propagate that into the buffers appropriately.
3755 */
Mingming Cao617ba132006-10-11 01:20:53 -07003756static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003757{
3758 SetPageChecked(page);
3759 return __set_page_dirty_nobuffers(page);
3760}
3761
Mingming Cao617ba132006-10-11 01:20:53 -07003762static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003763 .readpage = ext4_readpage,
3764 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003765 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003766 .write_begin = ext4_write_begin,
3767 .write_end = ext4_ordered_write_end,
3768 .bmap = ext4_bmap,
3769 .invalidatepage = ext4_invalidatepage,
3770 .releasepage = ext4_releasepage,
3771 .direct_IO = ext4_direct_IO,
3772 .migratepage = buffer_migrate_page,
3773 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003774 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003775};
3776
Mingming Cao617ba132006-10-11 01:20:53 -07003777static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003778 .readpage = ext4_readpage,
3779 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003780 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003781 .write_begin = ext4_write_begin,
3782 .write_end = ext4_writeback_write_end,
3783 .bmap = ext4_bmap,
3784 .invalidatepage = ext4_invalidatepage,
3785 .releasepage = ext4_releasepage,
3786 .direct_IO = ext4_direct_IO,
3787 .migratepage = buffer_migrate_page,
3788 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003789 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003790};
3791
Mingming Cao617ba132006-10-11 01:20:53 -07003792static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003793 .readpage = ext4_readpage,
3794 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003795 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003796 .write_begin = ext4_write_begin,
3797 .write_end = ext4_journalled_write_end,
3798 .set_page_dirty = ext4_journalled_set_page_dirty,
3799 .bmap = ext4_bmap,
3800 .invalidatepage = ext4_invalidatepage,
3801 .releasepage = ext4_releasepage,
3802 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003803 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003804};
3805
Alex Tomas64769242008-07-11 19:27:31 -04003806static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003807 .readpage = ext4_readpage,
3808 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003809 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003810 .writepages = ext4_da_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003811 .write_begin = ext4_da_write_begin,
3812 .write_end = ext4_da_write_end,
3813 .bmap = ext4_bmap,
3814 .invalidatepage = ext4_da_invalidatepage,
3815 .releasepage = ext4_releasepage,
3816 .direct_IO = ext4_direct_IO,
3817 .migratepage = buffer_migrate_page,
3818 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003819 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003820};
3821
Mingming Cao617ba132006-10-11 01:20:53 -07003822void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003823{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003824 if (ext4_should_order_data(inode) &&
3825 test_opt(inode->i_sb, DELALLOC))
3826 inode->i_mapping->a_ops = &ext4_da_aops;
3827 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003828 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003829 else if (ext4_should_writeback_data(inode) &&
3830 test_opt(inode->i_sb, DELALLOC))
3831 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003832 else if (ext4_should_writeback_data(inode))
3833 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834 else
Mingming Cao617ba132006-10-11 01:20:53 -07003835 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003836}
3837
3838/*
Mingming Cao617ba132006-10-11 01:20:53 -07003839 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003840 * up to the end of the block which corresponds to `from'.
3841 * This required during truncate. We need to physically zero the tail end
3842 * of that block so it doesn't yield old data if the file is later grown.
3843 */
Jan Karacf108bc2008-07-11 19:27:31 -04003844int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003845 struct address_space *mapping, loff_t from)
3846{
Allison Henderson30848852011-05-25 07:41:32 -04003847 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3848 unsigned length;
3849 unsigned blocksize;
3850 struct inode *inode = mapping->host;
3851
3852 blocksize = inode->i_sb->s_blocksize;
3853 length = blocksize - (offset & (blocksize - 1));
3854
3855 return ext4_block_zero_page_range(handle, mapping, from, length);
3856}
3857
3858/*
3859 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3860 * starting from file offset 'from'. The range to be zero'd must
3861 * be contained with in one block. If the specified range exceeds
3862 * the end of the block it will be shortened to end of the block
3863 * that cooresponds to 'from'
3864 */
3865int ext4_block_zero_page_range(handle_t *handle,
3866 struct address_space *mapping, loff_t from, loff_t length)
3867{
Mingming Cao617ba132006-10-11 01:20:53 -07003868 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003869 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Allison Henderson30848852011-05-25 07:41:32 -04003870 unsigned blocksize, max, pos;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003871 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003872 struct inode *inode = mapping->host;
3873 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003874 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003875 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876
Theodore Ts'of4a01012009-07-05 22:08:16 -04003877 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3878 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003879 if (!page)
3880 return -EINVAL;
3881
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003882 blocksize = inode->i_sb->s_blocksize;
Allison Henderson30848852011-05-25 07:41:32 -04003883 max = blocksize - (offset & (blocksize - 1));
3884
3885 /*
3886 * correct length if it does not fall between
3887 * 'from' and the end of the block
3888 */
3889 if (length > max || length < 0)
3890 length = max;
3891
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003892 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3893
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003894 if (!page_has_buffers(page))
3895 create_empty_buffers(page, blocksize, 0);
3896
3897 /* Find the buffer that contains "offset" */
3898 bh = page_buffers(page);
3899 pos = blocksize;
3900 while (offset >= pos) {
3901 bh = bh->b_this_page;
3902 iblock++;
3903 pos += blocksize;
3904 }
3905
3906 err = 0;
3907 if (buffer_freed(bh)) {
3908 BUFFER_TRACE(bh, "freed: skip");
3909 goto unlock;
3910 }
3911
3912 if (!buffer_mapped(bh)) {
3913 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003914 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003915 /* unmapped? It's a hole - nothing to do */
3916 if (!buffer_mapped(bh)) {
3917 BUFFER_TRACE(bh, "still unmapped");
3918 goto unlock;
3919 }
3920 }
3921
3922 /* Ok, it's mapped. Make sure it's up-to-date */
3923 if (PageUptodate(page))
3924 set_buffer_uptodate(bh);
3925
3926 if (!buffer_uptodate(bh)) {
3927 err = -EIO;
3928 ll_rw_block(READ, 1, &bh);
3929 wait_on_buffer(bh);
3930 /* Uhhuh. Read error. Complain and punt. */
3931 if (!buffer_uptodate(bh))
3932 goto unlock;
3933 }
3934
Mingming Cao617ba132006-10-11 01:20:53 -07003935 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003936 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003937 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003938 if (err)
3939 goto unlock;
3940 }
3941
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003942 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003943
3944 BUFFER_TRACE(bh, "zeroed end of block");
3945
3946 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003947 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003948 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003949 } else {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05003950 if (ext4_should_order_data(inode) && EXT4_I(inode)->jinode)
Jan Kara678aaf42008-07-11 19:27:31 -04003951 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003952 mark_buffer_dirty(bh);
3953 }
3954
3955unlock:
3956 unlock_page(page);
3957 page_cache_release(page);
3958 return err;
3959}
3960
3961/*
3962 * Probably it should be a library function... search for first non-zero word
3963 * or memcmp with zero_page, whatever is better for particular architecture.
3964 * Linus?
3965 */
3966static inline int all_zeroes(__le32 *p, __le32 *q)
3967{
3968 while (p < q)
3969 if (*p++)
3970 return 0;
3971 return 1;
3972}
3973
3974/**
Mingming Cao617ba132006-10-11 01:20:53 -07003975 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003976 * @inode: inode in question
3977 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003978 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003979 * @chain: place to store the pointers to partial indirect blocks
3980 * @top: place to the (detached) top of branch
3981 *
Mingming Cao617ba132006-10-11 01:20:53 -07003982 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003983 *
3984 * When we do truncate() we may have to clean the ends of several
3985 * indirect blocks but leave the blocks themselves alive. Block is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003986 * partially truncated if some data below the new i_size is referred
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003987 * from it (and it is on the path to the first completely truncated
3988 * data block, indeed). We have to free the top of that path along
3989 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003990 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003991 * finishes, we may safely do the latter, but top of branch may
3992 * require special attention - pageout below the truncation point
3993 * might try to populate it.
3994 *
3995 * We atomically detach the top of branch from the tree, store the
3996 * block number of its root in *@top, pointers to buffer_heads of
3997 * partially truncated blocks - in @chain[].bh and pointers to
3998 * their last elements that should not be removed - in
3999 * @chain[].p. Return value is the pointer to last filled element
4000 * of @chain.
4001 *
4002 * The work left to caller to do the actual freeing of subtrees:
4003 * a) free the subtree starting from *@top
4004 * b) free the subtrees whose roots are stored in
4005 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
4006 * c) free the subtrees growing from the inode past the @chain[0].
4007 * (no partially truncated stuff there). */
4008
Mingming Cao617ba132006-10-11 01:20:53 -07004009static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004010 ext4_lblk_t offsets[4], Indirect chain[4],
4011 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012{
4013 Indirect *partial, *p;
4014 int k, err;
4015
4016 *top = 0;
Uwe Kleine-Königbf48aab2009-10-28 20:11:03 +01004017 /* Make k index the deepest non-null offset + 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004018 for (k = depth; k > 1 && !offsets[k-1]; k--)
4019 ;
Mingming Cao617ba132006-10-11 01:20:53 -07004020 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004021 /* Writer: pointers */
4022 if (!partial)
4023 partial = chain + k-1;
4024 /*
4025 * If the branch acquired continuation since we've looked at it -
4026 * fine, it should all survive and (new) top doesn't belong to us.
4027 */
4028 if (!partial->key && *partial->p)
4029 /* Writer: end */
4030 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004031 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004032 ;
4033 /*
4034 * OK, we've found the last block that must survive. The rest of our
4035 * branch should be detached before unlocking. However, if that rest
4036 * of branch is all ours and does not grow immediately from the inode
4037 * it's easier to cheat and just decrement partial->p.
4038 */
4039 if (p == chain + k - 1 && p > chain) {
4040 p->p--;
4041 } else {
4042 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07004043 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004044#if 0
4045 *p->p = 0;
4046#endif
4047 }
4048 /* Writer: end */
4049
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004050 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004051 brelse(partial->bh);
4052 partial--;
4053 }
4054no_top:
4055 return partial;
4056}
4057
4058/*
4059 * Zero a number of block pointers in either an inode or an indirect block.
4060 * If we restart the transaction we must again get write access to the
4061 * indirect block for further modification.
4062 *
4063 * We release `count' blocks on disk, but (last - first) may be greater
4064 * than `count' because there can be holes in there.
Amir Goldsteind67d1212011-03-20 22:59:02 -04004065 *
4066 * Return 0 on success, 1 on invalid block range
4067 * and < 0 on fatal error.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004068 */
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004069static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
4070 struct buffer_head *bh,
4071 ext4_fsblk_t block_to_free,
4072 unsigned long count, __le32 *first,
4073 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004074{
4075 __le32 *p;
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004076 int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED;
Theodore Ts'ob4097142011-01-10 12:46:59 -05004077 int err;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004078
4079 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4080 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o50689692009-11-23 07:17:34 -05004081
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004082 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
4083 count)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004084 EXT4_ERROR_INODE(inode, "attempt to clear invalid "
4085 "blocks %llu len %lu",
4086 (unsigned long long) block_to_free, count);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004087 return 1;
4088 }
4089
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004090 if (try_to_extend_transaction(handle, inode)) {
4091 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004092 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Theodore Ts'ob4097142011-01-10 12:46:59 -05004093 err = ext4_handle_dirty_metadata(handle, inode, bh);
Amir Goldsteind67d1212011-03-20 22:59:02 -04004094 if (unlikely(err))
4095 goto out_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004096 }
Theodore Ts'ob4097142011-01-10 12:46:59 -05004097 err = ext4_mark_inode_dirty(handle, inode);
Amir Goldsteind67d1212011-03-20 22:59:02 -04004098 if (unlikely(err))
4099 goto out_err;
Theodore Ts'ob4097142011-01-10 12:46:59 -05004100 err = ext4_truncate_restart_trans(handle, inode,
Theodore Ts'o9f125d62011-06-27 19:16:04 -04004101 ext4_blocks_for_truncate(inode));
Amir Goldsteind67d1212011-03-20 22:59:02 -04004102 if (unlikely(err))
4103 goto out_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104 if (bh) {
4105 BUFFER_TRACE(bh, "retaking write access");
Amir Goldsteind67d1212011-03-20 22:59:02 -04004106 err = ext4_journal_get_write_access(handle, bh);
4107 if (unlikely(err))
4108 goto out_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004109 }
4110 }
4111
Theodore Ts'oe6362602009-11-23 07:17:05 -05004112 for (p = first; p < last; p++)
4113 *p = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114
Peter Huewe7dc57612011-02-21 21:01:42 -05004115 ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004116 return 0;
Amir Goldsteind67d1212011-03-20 22:59:02 -04004117out_err:
4118 ext4_std_error(inode->i_sb, err);
4119 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004120}
4121
4122/**
Mingming Cao617ba132006-10-11 01:20:53 -07004123 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004124 * @handle: handle for this transaction
4125 * @inode: inode we are dealing with
4126 * @this_bh: indirect buffer_head which contains *@first and *@last
4127 * @first: array of block numbers
4128 * @last: points immediately past the end of array
4129 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004130 * We are freeing all blocks referred from that array (numbers are stored as
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004131 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
4132 *
4133 * We accumulate contiguous runs of blocks to free. Conveniently, if these
4134 * blocks are contiguous then releasing them at one time will only affect one
4135 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
4136 * actually use a lot of journal space.
4137 *
4138 * @this_bh will be %NULL if @first and @last point into the inode's direct
4139 * block pointers.
4140 */
Mingming Cao617ba132006-10-11 01:20:53 -07004141static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004142 struct buffer_head *this_bh,
4143 __le32 *first, __le32 *last)
4144{
Mingming Cao617ba132006-10-11 01:20:53 -07004145 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004146 unsigned long count = 0; /* Number of blocks in the run */
4147 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
4148 corresponding to
4149 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07004150 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004151 __le32 *p; /* Pointer into inode/ind
4152 for current block */
Amir Goldsteind67d1212011-03-20 22:59:02 -04004153 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004154
4155 if (this_bh) { /* For indirect block */
4156 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004157 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004158 /* Important: if we can't update the indirect pointers
4159 * to the blocks, we can't free them. */
4160 if (err)
4161 return;
4162 }
4163
4164 for (p = first; p < last; p++) {
4165 nr = le32_to_cpu(*p);
4166 if (nr) {
4167 /* accumulate blocks to free if they're contiguous */
4168 if (count == 0) {
4169 block_to_free = nr;
4170 block_to_free_p = p;
4171 count = 1;
4172 } else if (nr == block_to_free + count) {
4173 count++;
4174 } else {
Amir Goldsteind67d1212011-03-20 22:59:02 -04004175 err = ext4_clear_blocks(handle, inode, this_bh,
4176 block_to_free, count,
4177 block_to_free_p, p);
4178 if (err)
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004179 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004180 block_to_free = nr;
4181 block_to_free_p = p;
4182 count = 1;
4183 }
4184 }
4185 }
4186
Amir Goldsteind67d1212011-03-20 22:59:02 -04004187 if (!err && count > 0)
4188 err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
4189 count, block_to_free_p, p);
4190 if (err < 0)
4191 /* fatal error */
4192 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004193
4194 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004195 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004196
4197 /*
4198 * The buffer head should have an attached journal head at this
4199 * point. However, if the data is corrupted and an indirect
4200 * block pointed to itself, it would have been detached when
4201 * the block was cleared. Check for this instead of OOPSing.
4202 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05004203 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05004204 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004205 else
Theodore Ts'o24676da2010-05-16 21:00:00 -04004206 EXT4_ERROR_INODE(inode,
4207 "circular indirect block detected at "
4208 "block %llu",
4209 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004210 }
4211}
4212
4213/**
Mingming Cao617ba132006-10-11 01:20:53 -07004214 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004215 * @handle: JBD handle for this transaction
4216 * @inode: inode we are dealing with
4217 * @parent_bh: the buffer_head which contains *@first and *@last
4218 * @first: array of block numbers
4219 * @last: pointer immediately past the end of array
4220 * @depth: depth of the branches to free
4221 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004222 * We are freeing all blocks referred from these branches (numbers are
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004223 * stored as little-endian 32-bit) and updating @inode->i_blocks
4224 * appropriately.
4225 */
Mingming Cao617ba132006-10-11 01:20:53 -07004226static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004227 struct buffer_head *parent_bh,
4228 __le32 *first, __le32 *last, int depth)
4229{
Mingming Cao617ba132006-10-11 01:20:53 -07004230 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004231 __le32 *p;
4232
Frank Mayhar03901312009-01-07 00:06:22 -05004233 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004234 return;
4235
4236 if (depth--) {
4237 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004238 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004239 p = last;
4240 while (--p >= first) {
4241 nr = le32_to_cpu(*p);
4242 if (!nr)
4243 continue; /* A hole */
4244
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004245 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
4246 nr, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004247 EXT4_ERROR_INODE(inode,
4248 "invalid indirect mapped "
4249 "block %lu (level %d)",
4250 (unsigned long) nr, depth);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004251 break;
4252 }
4253
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004254 /* Go read the buffer for the next level down */
4255 bh = sb_bread(inode->i_sb, nr);
4256
4257 /*
4258 * A read failure? Report error and clear slot
4259 * (should be rare).
4260 */
4261 if (!bh) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004262 EXT4_ERROR_INODE_BLOCK(inode, nr,
4263 "Read failure");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004264 continue;
4265 }
4266
4267 /* This zaps the entire block. Bottom up. */
4268 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07004269 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004270 (__le32 *) bh->b_data,
4271 (__le32 *) bh->b_data + addr_per_block,
4272 depth);
Theodore Ts'o1c5b9e92011-01-10 12:51:28 -05004273 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004274
4275 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004276 * Everything below this this pointer has been
4277 * released. Now let this top-of-subtree go.
4278 *
4279 * We want the freeing of this indirect block to be
4280 * atomic in the journal with the updating of the
4281 * bitmap block which owns it. So make some room in
4282 * the journal.
4283 *
4284 * We zero the parent pointer *after* freeing its
4285 * pointee in the bitmaps, so if extend_transaction()
4286 * for some reason fails to put the bitmap changes and
4287 * the release into the same transaction, recovery
4288 * will merely complain about releasing a free block,
4289 * rather than leaking blocks.
4290 */
Frank Mayhar03901312009-01-07 00:06:22 -05004291 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004292 return;
4293 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004294 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04004295 ext4_truncate_restart_trans(handle, inode,
Theodore Ts'o9f125d62011-06-27 19:16:04 -04004296 ext4_blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004297 }
4298
Amir G40389682010-07-27 11:56:05 -04004299 /*
4300 * The forget flag here is critical because if
4301 * we are journaling (and not doing data
4302 * journaling), we have to make sure a revoke
4303 * record is written to prevent the journal
4304 * replay from overwriting the (former)
4305 * indirect block if it gets reallocated as a
4306 * data block. This must happen in the same
4307 * transaction where the data blocks are
4308 * actually freed.
4309 */
Peter Huewe7dc57612011-02-21 21:01:42 -05004310 ext4_free_blocks(handle, inode, NULL, nr, 1,
Amir G40389682010-07-27 11:56:05 -04004311 EXT4_FREE_BLOCKS_METADATA|
4312 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004313
4314 if (parent_bh) {
4315 /*
4316 * The block which we have just freed is
4317 * pointed to by an indirect block: journal it
4318 */
4319 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004320 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004321 parent_bh)){
4322 *p = 0;
4323 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05004324 "call ext4_handle_dirty_metadata");
4325 ext4_handle_dirty_metadata(handle,
4326 inode,
4327 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004328 }
4329 }
4330 }
4331 } else {
4332 /* We have reached the bottom of the tree. */
4333 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07004334 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004335 }
4336}
4337
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004338int ext4_can_truncate(struct inode *inode)
4339{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004340 if (S_ISREG(inode->i_mode))
4341 return 1;
4342 if (S_ISDIR(inode->i_mode))
4343 return 1;
4344 if (S_ISLNK(inode->i_mode))
4345 return !ext4_inode_is_fast_symlink(inode);
4346 return 0;
4347}
4348
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004349/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004350 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
4351 * associated with the given offset and length
4352 *
4353 * @inode: File inode
4354 * @offset: The offset where the hole will begin
4355 * @len: The length of the hole
4356 *
4357 * Returns: 0 on sucess or negative on failure
4358 */
4359
4360int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
4361{
4362 struct inode *inode = file->f_path.dentry->d_inode;
4363 if (!S_ISREG(inode->i_mode))
4364 return -ENOTSUPP;
4365
4366 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4367 /* TODO: Add support for non extent hole punching */
4368 return -ENOTSUPP;
4369 }
4370
4371 return ext4_ext_punch_hole(file, offset, length);
4372}
4373
4374/*
Mingming Cao617ba132006-10-11 01:20:53 -07004375 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004376 *
Mingming Cao617ba132006-10-11 01:20:53 -07004377 * We block out ext4_get_block() block instantiations across the entire
4378 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004379 * simultaneously on behalf of the same inode.
4380 *
4381 * As we work through the truncate and commmit bits of it to the journal there
4382 * is one core, guiding principle: the file's tree must always be consistent on
4383 * disk. We must be able to restart the truncate after a crash.
4384 *
4385 * The file's tree may be transiently inconsistent in memory (although it
4386 * probably isn't), but whenever we close off and commit a journal transaction,
4387 * the contents of (the filesystem + the journal) must be consistent and
4388 * restartable. It's pretty simple, really: bottom up, right to left (although
4389 * left-to-right works OK too).
4390 *
4391 * Note that at recovery time, journal replay occurs *before* the restart of
4392 * truncate against the orphan inode list.
4393 *
4394 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004395 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004396 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004397 * ext4_truncate() to have another go. So there will be instantiated blocks
4398 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004399 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004400 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004401 */
Mingming Cao617ba132006-10-11 01:20:53 -07004402void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004403{
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004404 trace_ext4_truncate_enter(inode);
4405
4406 if (!ext4_can_truncate(inode))
4407 return;
4408
4409 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4410
4411 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
4412 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
4413
4414 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4415 ext4_ext_truncate(inode);
4416 else
4417 ext4_ind_truncate(inode);
4418
4419 trace_ext4_truncate_exit(inode);
4420}
4421
4422void ext4_ind_truncate(struct inode *inode)
4423{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004424 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07004425 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004426 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004427 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004428 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004429 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004430 Indirect chain[4];
4431 Indirect *partial;
4432 __le32 nr = 0;
Kazuya Miof80da1e2011-04-10 22:06:36 -04004433 int n = 0;
4434 ext4_lblk_t last_block, max_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004435 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004436
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004437 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04004438 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004439 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004440
4441 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07004442 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Kazuya Miof80da1e2011-04-10 22:06:36 -04004443 max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
4444 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004445
Jan Karacf108bc2008-07-11 19:27:31 -04004446 if (inode->i_size & (blocksize - 1))
4447 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
4448 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004449
Kazuya Miof80da1e2011-04-10 22:06:36 -04004450 if (last_block != max_block) {
4451 n = ext4_block_to_path(inode, last_block, offsets, NULL);
4452 if (n == 0)
4453 goto out_stop; /* error */
4454 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004455
4456 /*
4457 * OK. This truncate is going to happen. We add the inode to the
4458 * orphan list, so that if this truncate spans multiple transactions,
4459 * and we crash, we will resume the truncate when the filesystem
4460 * recovers. It also marks the inode dirty, to catch the new size.
4461 *
4462 * Implication: the file must always be in a sane, consistent
4463 * truncatable state while each transaction commits.
4464 */
Mingming Cao617ba132006-10-11 01:20:53 -07004465 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004466 goto out_stop;
4467
4468 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004469 * From here we block out all ext4_get_block() callers who want to
4470 * modify the block allocation tree.
4471 */
4472 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004473
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004474 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004475
Mingming Cao632eaea2008-07-11 19:27:31 -04004476 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004477 * The orphan list entry will now protect us from any crash which
4478 * occurs before the truncate completes, so it is now safe to propagate
4479 * the new, shorter inode size (held for now in i_size) into the
4480 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004481 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004482 */
4483 ei->i_disksize = inode->i_size;
4484
Kazuya Miof80da1e2011-04-10 22:06:36 -04004485 if (last_block == max_block) {
4486 /*
4487 * It is unnecessary to free any data blocks if last_block is
4488 * equal to the indirect block limit.
4489 */
4490 goto out_unlock;
4491 } else if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004492 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4493 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004494 goto do_indirects;
4495 }
4496
Mingming Cao617ba132006-10-11 01:20:53 -07004497 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004498 /* Kill the top of shared branch (not detached) */
4499 if (nr) {
4500 if (partial == chain) {
4501 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004502 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004503 &nr, &nr+1, (chain+n-1) - partial);
4504 *partial->p = 0;
4505 /*
4506 * We mark the inode dirty prior to restart,
4507 * and prior to stop. No need for it here.
4508 */
4509 } else {
4510 /* Shared branch grows from an indirect block */
4511 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004512 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004513 partial->p,
4514 partial->p+1, (chain+n-1) - partial);
4515 }
4516 }
4517 /* Clear the ends of indirect blocks on the shared branch */
4518 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004519 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004520 (__le32*)partial->bh->b_data+addr_per_block,
4521 (chain+n-1) - partial);
4522 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004523 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004524 partial--;
4525 }
4526do_indirects:
4527 /* Kill the remaining (whole) subtrees */
4528 switch (offsets[0]) {
4529 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004530 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004531 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004532 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4533 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004534 }
Mingming Cao617ba132006-10-11 01:20:53 -07004535 case EXT4_IND_BLOCK:
4536 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004537 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004538 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4539 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004540 }
Mingming Cao617ba132006-10-11 01:20:53 -07004541 case EXT4_DIND_BLOCK:
4542 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004543 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004544 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4545 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004546 }
Mingming Cao617ba132006-10-11 01:20:53 -07004547 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548 ;
4549 }
4550
Kazuya Miof80da1e2011-04-10 22:06:36 -04004551out_unlock:
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004552 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004553 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004554 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004555
4556 /*
4557 * In a multi-transaction truncate, we only make the final transaction
4558 * synchronous
4559 */
4560 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004561 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004562out_stop:
4563 /*
4564 * If this was a simple ftruncate(), and the file will remain alive
4565 * then we need to clear up the orphan record which we created above.
4566 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004567 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004568 * orphan info for us.
4569 */
4570 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004571 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004572
Mingming Cao617ba132006-10-11 01:20:53 -07004573 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004574 trace_ext4_truncate_exit(inode);
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 */
Tao Ma00d09882011-05-09 10:26:41 -04004604 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004605 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'oc398eda2010-07-27 11:56:40 -04004612 EXT4_ERROR_INODE_BLOCK(inode, block,
4613 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004614 return -EIO;
4615 }
4616 if (!buffer_uptodate(bh)) {
4617 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004618
4619 /*
4620 * If the buffer has the write error flag, we have failed
4621 * to write out another inode in the same block. In this
4622 * case, we don't have to read the block because we may
4623 * read the old inode data successfully.
4624 */
4625 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4626 set_buffer_uptodate(bh);
4627
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004628 if (buffer_uptodate(bh)) {
4629 /* someone brought it uptodate while we waited */
4630 unlock_buffer(bh);
4631 goto has_buffer;
4632 }
4633
4634 /*
4635 * If we have all information of the inode in memory and this
4636 * is the only valid inode in the block, we need not read the
4637 * block.
4638 */
4639 if (in_mem) {
4640 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004641 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004642
Theodore Ts'o240799c2008-10-09 23:53:47 -04004643 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004644
4645 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004646 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004647 if (!bitmap_bh)
4648 goto make_io;
4649
4650 /*
4651 * If the inode bitmap isn't in cache then the
4652 * optimisation may end up performing two reads instead
4653 * of one, so skip it.
4654 */
4655 if (!buffer_uptodate(bitmap_bh)) {
4656 brelse(bitmap_bh);
4657 goto make_io;
4658 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004659 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004660 if (i == inode_offset)
4661 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004662 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004663 break;
4664 }
4665 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004666 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004667 /* all other inodes are free, so skip I/O */
4668 memset(bh->b_data, 0, bh->b_size);
4669 set_buffer_uptodate(bh);
4670 unlock_buffer(bh);
4671 goto has_buffer;
4672 }
4673 }
4674
4675make_io:
4676 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004677 * If we need to do any I/O, try to pre-readahead extra
4678 * blocks from the inode table.
4679 */
4680 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4681 ext4_fsblk_t b, end, table;
4682 unsigned num;
4683
4684 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004685 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004686 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4687 if (table > b)
4688 b = table;
4689 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4690 num = EXT4_INODES_PER_GROUP(sb);
4691 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4692 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004693 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004694 table += num / inodes_per_block;
4695 if (end > table)
4696 end = table;
4697 while (b <= end)
4698 sb_breadahead(sb, b++);
4699 }
4700
4701 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004702 * There are other valid inodes in the buffer, this inode
4703 * has in-inode xattrs, or we don't have this inode in memory.
4704 * Read the block from disk.
4705 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004706 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004707 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'oc398eda2010-07-27 11:56:40 -04004712 EXT4_ERROR_INODE_BLOCK(inode, block,
4713 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004714 brelse(bh);
4715 return -EIO;
4716 }
4717 }
4718has_buffer:
4719 iloc->bh = bh;
4720 return 0;
4721}
4722
Mingming Cao617ba132006-10-11 01:20:53 -07004723int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004724{
4725 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004726 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004727 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004728}
4729
Mingming Cao617ba132006-10-11 01:20:53 -07004730void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004731{
Mingming Cao617ba132006-10-11 01:20:53 -07004732 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004733
4734 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004735 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004736 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004737 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004738 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004739 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004740 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004741 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004742 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004743 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004744 inode->i_flags |= S_DIRSYNC;
4745}
4746
Jan Karaff9ddf72007-07-18 09:24:20 -04004747/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4748void ext4_get_inode_flags(struct ext4_inode_info *ei)
4749{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004750 unsigned int vfs_fl;
4751 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04004752
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004753 do {
4754 vfs_fl = ei->vfs_inode.i_flags;
4755 old_fl = ei->i_flags;
4756 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4757 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
4758 EXT4_DIRSYNC_FL);
4759 if (vfs_fl & S_SYNC)
4760 new_fl |= EXT4_SYNC_FL;
4761 if (vfs_fl & S_APPEND)
4762 new_fl |= EXT4_APPEND_FL;
4763 if (vfs_fl & S_IMMUTABLE)
4764 new_fl |= EXT4_IMMUTABLE_FL;
4765 if (vfs_fl & S_NOATIME)
4766 new_fl |= EXT4_NOATIME_FL;
4767 if (vfs_fl & S_DIRSYNC)
4768 new_fl |= EXT4_DIRSYNC_FL;
4769 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04004770}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004771
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004772static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004773 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004774{
4775 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004776 struct inode *inode = &(ei->vfs_inode);
4777 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004778
4779 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4780 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4781 /* we are using combined 48 bit field */
4782 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4783 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004784 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004785 /* i_blocks represent file system block size */
4786 return i_blocks << (inode->i_blkbits - 9);
4787 } else {
4788 return i_blocks;
4789 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004790 } else {
4791 return le32_to_cpu(raw_inode->i_blocks_lo);
4792 }
4793}
Jan Karaff9ddf72007-07-18 09:24:20 -04004794
David Howells1d1fe1e2008-02-07 00:15:37 -08004795struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004796{
Mingming Cao617ba132006-10-11 01:20:53 -07004797 struct ext4_iloc iloc;
4798 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004799 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004800 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004801 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004802 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004803 int block;
4804
David Howells1d1fe1e2008-02-07 00:15:37 -08004805 inode = iget_locked(sb, ino);
4806 if (!inode)
4807 return ERR_PTR(-ENOMEM);
4808 if (!(inode->i_state & I_NEW))
4809 return inode;
4810
4811 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004812 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004813
David Howells1d1fe1e2008-02-07 00:15:37 -08004814 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4815 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004816 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004817 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004818 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4819 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4820 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004821 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004822 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4823 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4824 }
4825 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004826
Theodore Ts'o353eb832011-01-10 12:18:25 -05004827 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004828 ei->i_dir_start_lookup = 0;
4829 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4830 /* We now have enough fields to check if the inode was active or not.
4831 * This is needed because nfsd might try to access dead inodes
4832 * the test is that same one that e2fsck uses
4833 * NeilBrown 1999oct15
4834 */
4835 if (inode->i_nlink == 0) {
4836 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004837 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004838 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004839 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004840 goto bad_inode;
4841 }
4842 /* The only unlinked inodes we let through here have
4843 * valid i_mode and are being read by the orphan
4844 * recovery code: that's fine, we're about to complete
4845 * the process of deleting those. */
4846 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004847 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004848 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004849 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004850 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004851 ei->i_file_acl |=
4852 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004853 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004854 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004855#ifdef CONFIG_QUOTA
4856 ei->i_reserved_quota = 0;
4857#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004858 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4859 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004860 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861 /*
4862 * NOTE! The in-memory inode i_data array is in little-endian order
4863 * even on big-endian machines: we do NOT byteswap the block numbers!
4864 */
Mingming Cao617ba132006-10-11 01:20:53 -07004865 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004866 ei->i_data[block] = raw_inode->i_block[block];
4867 INIT_LIST_HEAD(&ei->i_orphan);
4868
Jan Karab436b9b2009-12-08 23:51:10 -05004869 /*
4870 * Set transaction id's of transactions that have to be committed
4871 * to finish f[data]sync. We set them to currently running transaction
4872 * as we cannot be sure that the inode or some of its metadata isn't
4873 * part of the transaction - the inode could have been reclaimed and
4874 * now it is reread from disk.
4875 */
4876 if (journal) {
4877 transaction_t *transaction;
4878 tid_t tid;
4879
Theodore Ts'oa931da62010-08-03 21:35:12 -04004880 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004881 if (journal->j_running_transaction)
4882 transaction = journal->j_running_transaction;
4883 else
4884 transaction = journal->j_committing_transaction;
4885 if (transaction)
4886 tid = transaction->t_tid;
4887 else
4888 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004889 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004890 ei->i_sync_tid = tid;
4891 ei->i_datasync_tid = tid;
4892 }
4893
Eric Sandeen0040d982008-02-05 22:36:43 -05004894 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004895 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004896 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004897 EXT4_INODE_SIZE(inode->i_sb)) {
David Howells1d1fe1e2008-02-07 00:15:37 -08004898 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004899 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004900 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004901 if (ei->i_extra_isize == 0) {
4902 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004903 ei->i_extra_isize = sizeof(struct ext4_inode) -
4904 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004905 } else {
4906 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004907 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004908 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004909 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004910 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004911 }
4912 } else
4913 ei->i_extra_isize = 0;
4914
Kalpak Shahef7f3832007-07-18 09:15:20 -04004915 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4916 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4917 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4918 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4919
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004920 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4921 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4922 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4923 inode->i_version |=
4924 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4925 }
4926
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004927 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004928 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004929 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004930 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
4931 ei->i_file_acl);
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004932 ret = -EIO;
4933 goto bad_inode;
Theodore Ts'o07a03822010-06-14 09:54:48 -04004934 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004935 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4936 (S_ISLNK(inode->i_mode) &&
4937 !ext4_inode_is_fast_symlink(inode)))
4938 /* Validate extent which is part of inode */
4939 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004940 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004941 (S_ISLNK(inode->i_mode) &&
4942 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004943 /* Validate block references which are part of inode */
Theodore Ts'o1f7d1e72011-06-27 19:16:02 -04004944 ret = ext4_ind_check_inode(inode);
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004945 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004946 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004947 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004948
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004949 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004950 inode->i_op = &ext4_file_inode_operations;
4951 inode->i_fop = &ext4_file_operations;
4952 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004953 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004954 inode->i_op = &ext4_dir_inode_operations;
4955 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004956 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004957 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004958 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004959 nd_terminate_link(ei->i_data, inode->i_size,
4960 sizeof(ei->i_data) - 1);
4961 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004962 inode->i_op = &ext4_symlink_inode_operations;
4963 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004964 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004965 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4966 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004967 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004968 if (raw_inode->i_block[0])
4969 init_special_inode(inode, inode->i_mode,
4970 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4971 else
4972 init_special_inode(inode, inode->i_mode,
4973 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004974 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004975 ret = -EIO;
Theodore Ts'o24676da2010-05-16 21:00:00 -04004976 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004977 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004978 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004979 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004980 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004981 unlock_new_inode(inode);
4982 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004983
4984bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004985 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004986 iget_failed(inode);
4987 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004988}
4989
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004990static int ext4_inode_blocks_set(handle_t *handle,
4991 struct ext4_inode *raw_inode,
4992 struct ext4_inode_info *ei)
4993{
4994 struct inode *inode = &(ei->vfs_inode);
4995 u64 i_blocks = inode->i_blocks;
4996 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004997
4998 if (i_blocks <= ~0U) {
4999 /*
5000 * i_blocks can be represnted in a 32 bit variable
5001 * as multiple of 512 bytes
5002 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005003 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005004 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04005005 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04005006 return 0;
5007 }
5008 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
5009 return -EFBIG;
5010
5011 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005012 /*
5013 * i_blocks can be represented in a 48 bit variable
5014 * as multiple of 512 bytes
5015 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005016 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005017 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04005018 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005019 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04005020 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005021 /* i_block is stored in file system block size */
5022 i_blocks = i_blocks >> (inode->i_blkbits - 9);
5023 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
5024 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005025 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04005026 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005027}
5028
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005029/*
5030 * Post the struct inode info into an on-disk inode location in the
5031 * buffer-cache. This gobbles the caller's reference to the
5032 * buffer_head in the inode location struct.
5033 *
5034 * The caller must have write access to iloc->bh.
5035 */
Mingming Cao617ba132006-10-11 01:20:53 -07005036static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005037 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04005038 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005039{
Mingming Cao617ba132006-10-11 01:20:53 -07005040 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
5041 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005042 struct buffer_head *bh = iloc->bh;
5043 int err = 0, rc, block;
5044
5045 /* For fields not not tracking in the in-memory inode,
5046 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005047 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07005048 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005049
Jan Karaff9ddf72007-07-18 09:24:20 -04005050 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005051 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005052 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005053 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
5054 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
5055/*
5056 * Fix up interoperability with old kernels. Otherwise, old inodes get
5057 * re-used with the upper 16 bits of the uid/gid intact
5058 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005059 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005060 raw_inode->i_uid_high =
5061 cpu_to_le16(high_16_bits(inode->i_uid));
5062 raw_inode->i_gid_high =
5063 cpu_to_le16(high_16_bits(inode->i_gid));
5064 } else {
5065 raw_inode->i_uid_high = 0;
5066 raw_inode->i_gid_high = 0;
5067 }
5068 } else {
5069 raw_inode->i_uid_low =
5070 cpu_to_le16(fs_high2lowuid(inode->i_uid));
5071 raw_inode->i_gid_low =
5072 cpu_to_le16(fs_high2lowgid(inode->i_gid));
5073 raw_inode->i_uid_high = 0;
5074 raw_inode->i_gid_high = 0;
5075 }
5076 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04005077
5078 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5079 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5080 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5081 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5082
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005083 if (ext4_inode_blocks_set(handle, raw_inode, ei))
5084 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005085 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05005086 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07005087 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
5088 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005089 raw_inode->i_file_acl_high =
5090 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005091 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005092 ext4_isize_set(raw_inode, ei->i_disksize);
5093 if (ei->i_disksize > 0x7fffffffULL) {
5094 struct super_block *sb = inode->i_sb;
5095 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
5096 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
5097 EXT4_SB(sb)->s_es->s_rev_level ==
5098 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
5099 /* If this is the first large file
5100 * created, add a flag to the superblock.
5101 */
5102 err = ext4_journal_get_write_access(handle,
5103 EXT4_SB(sb)->s_sbh);
5104 if (err)
5105 goto out_brelse;
5106 ext4_update_dynamic_rev(sb);
5107 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07005108 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005109 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05005110 ext4_handle_sync(handle);
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005111 err = ext4_handle_dirty_metadata(handle, NULL,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005112 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005113 }
5114 }
5115 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5116 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5117 if (old_valid_dev(inode->i_rdev)) {
5118 raw_inode->i_block[0] =
5119 cpu_to_le32(old_encode_dev(inode->i_rdev));
5120 raw_inode->i_block[1] = 0;
5121 } else {
5122 raw_inode->i_block[0] = 0;
5123 raw_inode->i_block[1] =
5124 cpu_to_le32(new_encode_dev(inode->i_rdev));
5125 raw_inode->i_block[2] = 0;
5126 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005127 } else
5128 for (block = 0; block < EXT4_N_BLOCKS; block++)
5129 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005130
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005131 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
5132 if (ei->i_extra_isize) {
5133 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5134 raw_inode->i_version_hi =
5135 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005136 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005137 }
5138
Frank Mayhar830156c2009-09-29 10:07:47 -04005139 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005140 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04005141 if (!err)
5142 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005143 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005144
Jan Karab436b9b2009-12-08 23:51:10 -05005145 ext4_update_inode_fsync_trans(handle, inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005146out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005147 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005148 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005149 return err;
5150}
5151
5152/*
Mingming Cao617ba132006-10-11 01:20:53 -07005153 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005154 *
5155 * We are called from a few places:
5156 *
5157 * - Within generic_file_write() for O_SYNC files.
5158 * Here, there will be no transaction running. We wait for any running
5159 * trasnaction to commit.
5160 *
5161 * - Within sys_sync(), kupdate and such.
5162 * We wait on commit, if tol to.
5163 *
5164 * - Within prune_icache() (PF_MEMALLOC == true)
5165 * Here we simply return. We can't afford to block kswapd on the
5166 * journal commit.
5167 *
5168 * In all cases it is actually safe for us to return without doing anything,
5169 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07005170 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005171 * knfsd.
5172 *
5173 * Note that we are absolutely dependent upon all inode dirtiers doing the
5174 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5175 * which we are interested.
5176 *
5177 * It would be a bug for them to not do this. The code:
5178 *
5179 * mark_inode_dirty(inode)
5180 * stuff();
5181 * inode->i_size = expr;
5182 *
5183 * is in error because a kswapd-driven write_inode() could occur while
5184 * `stuff()' is running, and the new i_size will be lost. Plus the inode
5185 * will no longer be on the superblock's dirty inode list.
5186 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005187int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005188{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005189 int err;
5190
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005191 if (current->flags & PF_MEMALLOC)
5192 return 0;
5193
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005194 if (EXT4_SB(inode->i_sb)->s_journal) {
5195 if (ext4_journal_current_handle()) {
5196 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5197 dump_stack();
5198 return -EIO;
5199 }
5200
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005201 if (wbc->sync_mode != WB_SYNC_ALL)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005202 return 0;
5203
5204 err = ext4_force_commit(inode->i_sb);
5205 } else {
5206 struct ext4_iloc iloc;
5207
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005208 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005209 if (err)
5210 return err;
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005211 if (wbc->sync_mode == WB_SYNC_ALL)
Frank Mayhar830156c2009-09-29 10:07:47 -04005212 sync_dirty_buffer(iloc.bh);
5213 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04005214 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
5215 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005216 err = -EIO;
5217 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005218 brelse(iloc.bh);
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;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005251 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005252 const unsigned int ia_valid = attr->ia_valid;
5253
5254 error = inode_change_ok(inode, attr);
5255 if (error)
5256 return error;
5257
Dmitry Monakhov12755622010-04-08 22:04:20 +04005258 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05005259 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005260 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
5261 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
5262 handle_t *handle;
5263
5264 /* (user+group)*(old+new) structure, inode write (sb,
5265 * inode block, ? - but truncate inode update has it) */
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05005266 handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
Dmitry Monakhov194074a2009-12-08 22:42:28 -05005267 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005268 if (IS_ERR(handle)) {
5269 error = PTR_ERR(handle);
5270 goto err_out;
5271 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005272 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005273 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005274 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005275 return error;
5276 }
5277 /* Update corresponding info in inode so that everything is in
5278 * one transaction */
5279 if (attr->ia_valid & ATTR_UID)
5280 inode->i_uid = attr->ia_uid;
5281 if (attr->ia_valid & ATTR_GID)
5282 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005283 error = ext4_mark_inode_dirty(handle, inode);
5284 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005285 }
5286
Eric Sandeene2b46572008-01-28 23:58:27 -05005287 if (attr->ia_valid & ATTR_SIZE) {
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005288 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005289 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5290
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005291 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5292 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005293 }
5294 }
5295
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005296 if (S_ISREG(inode->i_mode) &&
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05005297 attr->ia_valid & ATTR_SIZE &&
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005298 (attr->ia_size < inode->i_size)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005299 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 }
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005306 if (ext4_handle_valid(handle)) {
5307 error = ext4_orphan_add(handle, inode);
5308 orphan = 1;
5309 }
Mingming Cao617ba132006-10-11 01:20:53 -07005310 EXT4_I(inode)->i_disksize = attr->ia_size;
5311 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005312 if (!error)
5313 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07005314 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04005315
5316 if (ext4_should_order_data(inode)) {
5317 error = ext4_begin_ordered_truncate(inode,
5318 attr->ia_size);
5319 if (error) {
5320 /* Do as much error cleanup as possible */
5321 handle = ext4_journal_start(inode, 3);
5322 if (IS_ERR(handle)) {
5323 ext4_orphan_del(NULL, inode);
5324 goto err_out;
5325 }
5326 ext4_orphan_del(handle, inode);
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005327 orphan = 0;
Jan Kara678aaf42008-07-11 19:27:31 -04005328 ext4_journal_stop(handle);
5329 goto err_out;
5330 }
5331 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005332 }
5333
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005334 if (attr->ia_valid & ATTR_SIZE) {
5335 if (attr->ia_size != i_size_read(inode)) {
5336 truncate_setsize(inode, attr->ia_size);
5337 ext4_truncate(inode);
5338 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
5339 ext4_truncate(inode);
5340 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005341
Christoph Hellwig10257742010-06-04 11:30:02 +02005342 if (!rc) {
5343 setattr_copy(inode, attr);
5344 mark_inode_dirty(inode);
5345 }
5346
5347 /*
5348 * If the call to ext4_truncate failed to get a transaction handle at
5349 * all, we need to clean up the in-core orphan list manually.
5350 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005351 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005352 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005353
5354 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07005355 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005356
5357err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005358 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005359 if (!error)
5360 error = rc;
5361 return error;
5362}
5363
Mingming Cao3e3398a2008-07-11 19:27:31 -04005364int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5365 struct kstat *stat)
5366{
5367 struct inode *inode;
5368 unsigned long delalloc_blocks;
5369
5370 inode = dentry->d_inode;
5371 generic_fillattr(inode, stat);
5372
5373 /*
5374 * We can't update i_blocks if the block allocation is delayed
5375 * otherwise in the case of system crash before the real block
5376 * allocation is done, we will have i_blocks inconsistent with
5377 * on-disk file blocks.
5378 * We always keep i_blocks updated together with real
5379 * allocation. But to not confuse with user, stat
5380 * will return the blocks that include the delayed allocation
5381 * blocks for this file.
5382 */
Mingming Cao3e3398a2008-07-11 19:27:31 -04005383 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005384
5385 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
5386 return 0;
5387}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005388
Amir Goldstein8bb2b242011-06-27 17:10:28 -04005389static int ext4_ind_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005390{
Mingming Caoa02908f2008-08-19 22:16:07 -04005391 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005392
Mingming Caoa02908f2008-08-19 22:16:07 -04005393 /* if nrblocks are contiguous */
5394 if (chunk) {
5395 /*
Yongqiang Yang5b413952011-04-04 15:40:24 -04005396 * With N contiguous data blocks, we need at most
5397 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
5398 * 2 dindirect blocks, and 1 tindirect block
Mingming Caoa02908f2008-08-19 22:16:07 -04005399 */
Yongqiang Yang5b413952011-04-04 15:40:24 -04005400 return DIV_ROUND_UP(nrblocks,
5401 EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
Mingming Caoa02908f2008-08-19 22:16:07 -04005402 }
5403 /*
5404 * if nrblocks are not contiguous, worse case, each block touch
5405 * a indirect block, and each indirect block touch a double indirect
5406 * block, plus a triple indirect block
5407 */
5408 indirects = nrblocks * 2 + 1;
5409 return indirects;
5410}
Alex Tomasa86c6182006-10-11 01:21:03 -07005411
Mingming Caoa02908f2008-08-19 22:16:07 -04005412static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5413{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005414 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amir Goldstein8bb2b242011-06-27 17:10:28 -04005415 return ext4_ind_trans_blocks(inode, nrblocks, chunk);
Theodore Ts'oac51d832008-11-06 16:49:36 -05005416 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04005417}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005418
Mingming Caoa02908f2008-08-19 22:16:07 -04005419/*
5420 * Account for index blocks, block groups bitmaps and block group
5421 * descriptor blocks if modify datablocks and index blocks
5422 * worse case, the indexs blocks spread over different block groups
5423 *
5424 * If datablocks are discontiguous, they are possible to spread over
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005425 * different block groups too. If they are contiuguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005426 * they could still across block group boundary.
5427 *
5428 * Also account for superblock, inode, quota and xattr blocks
5429 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04005430static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoa02908f2008-08-19 22:16:07 -04005431{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005432 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5433 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005434 int idxblocks;
5435 int ret = 0;
5436
5437 /*
5438 * How many index blocks need to touch to modify nrblocks?
5439 * The "Chunk" flag indicating whether the nrblocks is
5440 * physically contiguous on disk
5441 *
5442 * For Direct IO and fallocate, they calls get_block to allocate
5443 * one single extent at a time, so they could set the "Chunk" flag
5444 */
5445 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
5446
5447 ret = idxblocks;
5448
5449 /*
5450 * Now let's see how many group bitmaps and group descriptors need
5451 * to account
5452 */
5453 groups = idxblocks;
5454 if (chunk)
5455 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005456 else
Mingming Caoa02908f2008-08-19 22:16:07 -04005457 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005458
Mingming Caoa02908f2008-08-19 22:16:07 -04005459 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005460 if (groups > ngroups)
5461 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005462 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5463 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5464
5465 /* bitmaps and block group descriptor blocks */
5466 ret += groups + gdpblocks;
5467
5468 /* Blocks for super block, inode, quota and xattr blocks */
5469 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005470
5471 return ret;
5472}
5473
5474/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005475 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005476 * the modification of a single pages into a single transaction,
5477 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005478 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005479 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005480 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005481 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005482 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005483 */
5484int ext4_writepage_trans_blocks(struct inode *inode)
5485{
5486 int bpp = ext4_journal_blocks_per_page(inode);
5487 int ret;
5488
5489 ret = ext4_meta_trans_blocks(inode, bpp, 0);
5490
5491 /* Account for data blocks for journalled mode */
5492 if (ext4_should_journal_data(inode))
5493 ret += bpp;
5494 return ret;
5495}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005496
5497/*
5498 * Calculate the journal credits for a chunk of data modification.
5499 *
5500 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005501 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005502 *
5503 * journal buffers for data blocks are not included here, as DIO
5504 * and fallocate do no need to journal data buffers.
5505 */
5506int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5507{
5508 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5509}
5510
Mingming Caoa02908f2008-08-19 22:16:07 -04005511/*
Mingming Cao617ba132006-10-11 01:20:53 -07005512 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005513 * Give this, we know that the caller already has write access to iloc->bh.
5514 */
Mingming Cao617ba132006-10-11 01:20:53 -07005515int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005516 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005517{
5518 int err = 0;
5519
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005520 if (test_opt(inode->i_sb, I_VERSION))
5521 inode_inc_iversion(inode);
5522
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005523 /* the do_update_inode consumes one bh->b_count */
5524 get_bh(iloc->bh);
5525
Mingming Caodab291a2006-10-11 01:21:01 -07005526 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005527 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005528 put_bh(iloc->bh);
5529 return err;
5530}
5531
5532/*
5533 * On success, We end up with an outstanding reference count against
5534 * iloc->bh. This _must_ be cleaned up later.
5535 */
5536
5537int
Mingming Cao617ba132006-10-11 01:20:53 -07005538ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5539 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005540{
Frank Mayhar03901312009-01-07 00:06:22 -05005541 int err;
5542
5543 err = ext4_get_inode_loc(inode, iloc);
5544 if (!err) {
5545 BUFFER_TRACE(iloc->bh, "get_write_access");
5546 err = ext4_journal_get_write_access(handle, iloc->bh);
5547 if (err) {
5548 brelse(iloc->bh);
5549 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005550 }
5551 }
Mingming Cao617ba132006-10-11 01:20:53 -07005552 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005553 return err;
5554}
5555
5556/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005557 * Expand an inode by new_extra_isize bytes.
5558 * Returns 0 on success or negative error number on failure.
5559 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005560static int ext4_expand_extra_isize(struct inode *inode,
5561 unsigned int new_extra_isize,
5562 struct ext4_iloc iloc,
5563 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005564{
5565 struct ext4_inode *raw_inode;
5566 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005567
5568 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5569 return 0;
5570
5571 raw_inode = ext4_raw_inode(&iloc);
5572
5573 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005574
5575 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005576 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5577 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005578 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5579 new_extra_isize);
5580 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5581 return 0;
5582 }
5583
5584 /* try to expand with EAs present */
5585 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5586 raw_inode, handle);
5587}
5588
5589/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005590 * What we do here is to mark the in-core inode as clean with respect to inode
5591 * dirtiness (it may still be data-dirty).
5592 * This means that the in-core inode may be reaped by prune_icache
5593 * without having to perform any I/O. This is a very good thing,
5594 * because *any* task may call prune_icache - even ones which
5595 * have a transaction open against a different journal.
5596 *
5597 * Is this cheating? Not really. Sure, we haven't written the
5598 * inode out, but prune_icache isn't a user-visible syncing function.
5599 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5600 * we start and wait on commits.
5601 *
5602 * Is this efficient/effective? Well, we're being nice to the system
5603 * by cleaning up our inodes proactively so they can be reaped
5604 * without I/O. But we are potentially leaving up to five seconds'
5605 * worth of inodes floating about which prune_icache wants us to
5606 * write out. One way to fix that would be to get prune_icache()
5607 * to do a write_super() to free up some memory. It has the desired
5608 * effect.
5609 */
Mingming Cao617ba132006-10-11 01:20:53 -07005610int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005611{
Mingming Cao617ba132006-10-11 01:20:53 -07005612 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005613 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5614 static unsigned int mnt_count;
5615 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005616
5617 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005618 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005619 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005620 if (ext4_handle_valid(handle) &&
5621 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005622 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005623 /*
5624 * We need extra buffer credits since we may write into EA block
5625 * with this same handle. If journal_extend fails, then it will
5626 * only result in a minor loss of functionality for that inode.
5627 * If this is felt to be critical, then e2fsck should be run to
5628 * force a large enough s_min_extra_isize.
5629 */
5630 if ((jbd2_journal_extend(handle,
5631 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5632 ret = ext4_expand_extra_isize(inode,
5633 sbi->s_want_extra_isize,
5634 iloc, handle);
5635 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005636 ext4_set_inode_state(inode,
5637 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005638 if (mnt_count !=
5639 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05005640 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005641 "Unable to expand inode %lu. Delete"
5642 " some EAs or run e2fsck.",
5643 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005644 mnt_count =
5645 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005646 }
5647 }
5648 }
5649 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005650 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005651 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005652 return err;
5653}
5654
5655/*
Mingming Cao617ba132006-10-11 01:20:53 -07005656 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005657 *
5658 * We're really interested in the case where a file is being extended.
5659 * i_size has been changed by generic_commit_write() and we thus need
5660 * to include the updated inode in the current transaction.
5661 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005662 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005663 * are allocated to the file.
5664 *
5665 * If the inode is marked synchronous, we don't honour that here - doing
5666 * so would cause a commit on atime updates, which we don't bother doing.
5667 * We handle synchronous inodes at the highest possible level.
5668 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005669void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005670{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005671 handle_t *handle;
5672
Mingming Cao617ba132006-10-11 01:20:53 -07005673 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005674 if (IS_ERR(handle))
5675 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005676
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005677 ext4_mark_inode_dirty(handle, inode);
5678
Mingming Cao617ba132006-10-11 01:20:53 -07005679 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005680out:
5681 return;
5682}
5683
5684#if 0
5685/*
5686 * Bind an inode's backing buffer_head into this transaction, to prevent
5687 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005688 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005689 * returns no iloc structure, so the caller needs to repeat the iloc
5690 * lookup to mark the inode dirty later.
5691 */
Mingming Cao617ba132006-10-11 01:20:53 -07005692static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005693{
Mingming Cao617ba132006-10-11 01:20:53 -07005694 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005695
5696 int err = 0;
5697 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005698 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005699 if (!err) {
5700 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005701 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005702 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005703 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005704 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05005705 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005706 brelse(iloc.bh);
5707 }
5708 }
Mingming Cao617ba132006-10-11 01:20:53 -07005709 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005710 return err;
5711}
5712#endif
5713
Mingming Cao617ba132006-10-11 01:20:53 -07005714int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005715{
5716 journal_t *journal;
5717 handle_t *handle;
5718 int err;
5719
5720 /*
5721 * We have to be very careful here: changing a data block's
5722 * journaling status dynamically is dangerous. If we write a
5723 * data block to the journal, change the status and then delete
5724 * that block, we risk forgetting to revoke the old log record
5725 * from the journal and so a subsequent replay can corrupt data.
5726 * So, first we make sure that the journal is empty and that
5727 * nobody is changing anything.
5728 */
5729
Mingming Cao617ba132006-10-11 01:20:53 -07005730 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005731 if (!journal)
5732 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005733 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005734 return -EROFS;
5735
Mingming Caodab291a2006-10-11 01:21:01 -07005736 jbd2_journal_lock_updates(journal);
5737 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005738
5739 /*
5740 * OK, there are no updates running now, and all cached data is
5741 * synced to disk. We are now in a completely consistent state
5742 * which doesn't have anything in the journal, and we know that
5743 * no filesystem updates are running, so it is safe to modify
5744 * the inode's in-core data-journaling state flag now.
5745 */
5746
5747 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005748 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005749 else
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005750 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07005751 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005752
Mingming Caodab291a2006-10-11 01:21:01 -07005753 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005754
5755 /* Finally we can mark the inode as dirty. */
5756
Mingming Cao617ba132006-10-11 01:20:53 -07005757 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005758 if (IS_ERR(handle))
5759 return PTR_ERR(handle);
5760
Mingming Cao617ba132006-10-11 01:20:53 -07005761 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005762 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005763 ext4_journal_stop(handle);
5764 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005765
5766 return err;
5767}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005768
5769static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5770{
5771 return !buffer_mapped(bh);
5772}
5773
Nick Pigginc2ec1752009-03-31 15:23:21 -07005774int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005775{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005776 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005777 loff_t size;
5778 unsigned long len;
5779 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005780 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005781 struct file *file = vma->vm_file;
5782 struct inode *inode = file->f_path.dentry->d_inode;
5783 struct address_space *mapping = inode->i_mapping;
5784
5785 /*
5786 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5787 * get i_mutex because we are already holding mmap_sem.
5788 */
5789 down_read(&inode->i_alloc_sem);
5790 size = i_size_read(inode);
5791 if (page->mapping != mapping || size <= page_offset(page)
5792 || !PageUptodate(page)) {
5793 /* page got truncated from under us? */
5794 goto out_unlock;
5795 }
5796 ret = 0;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005797
5798 lock_page(page);
5799 wait_on_page_writeback(page);
5800 if (PageMappedToDisk(page)) {
5801 up_read(&inode->i_alloc_sem);
5802 return VM_FAULT_LOCKED;
5803 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005804
5805 if (page->index == size >> PAGE_CACHE_SHIFT)
5806 len = size & ~PAGE_CACHE_MASK;
5807 else
5808 len = PAGE_CACHE_SIZE;
5809
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005810 /*
5811 * return if we have all the buffers mapped. This avoid
5812 * the need to call write_begin/write_end which does a
5813 * journal_start/journal_stop which can block and take
5814 * long time
5815 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005816 if (page_has_buffers(page)) {
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005817 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005818 ext4_bh_unmapped)) {
Darrick J. Wong0e499892011-05-18 13:55:20 -04005819 up_read(&inode->i_alloc_sem);
5820 return VM_FAULT_LOCKED;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005821 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005822 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005823 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005824 /*
5825 * OK, we need to fill the hole... Do write_begin write_end
5826 * to do block allocation/reservation.We are not holding
5827 * inode.i__mutex here. That allow * parallel write_begin,
5828 * write_end call. lock_page prevent this from happening
5829 * on the same page though
5830 */
5831 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005832 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005833 if (ret < 0)
5834 goto out_unlock;
5835 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005836 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005837 if (ret < 0)
5838 goto out_unlock;
5839 ret = 0;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005840
5841 /*
5842 * write_begin/end might have created a dirty page and someone
5843 * could wander in and start the IO. Make sure that hasn't
5844 * happened.
5845 */
5846 lock_page(page);
5847 wait_on_page_writeback(page);
5848 up_read(&inode->i_alloc_sem);
5849 return VM_FAULT_LOCKED;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005850out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005851 if (ret)
5852 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005853 up_read(&inode->i_alloc_sem);
5854 return ret;
5855}