blob: 1265904a239f1fd803afe5d8cb6d048e81d153ab [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"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070050
Theodore Ts'o9bffad12009-06-17 11:48:11 -040051#include <trace/events/ext4.h>
52
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040053#define MPAGE_DA_EXTENT_TAIL 0x01
54
Jan Kara678aaf42008-07-11 19:27:31 -040055static inline int ext4_begin_ordered_truncate(struct inode *inode,
56 loff_t new_size)
57{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -050058 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050059 /*
60 * If jinode is zero, then we never opened the file for
61 * writing, so there's no need to call
62 * jbd2_journal_begin_ordered_truncate() since there's no
63 * outstanding writes we need to flush.
64 */
65 if (!EXT4_I(inode)->jinode)
66 return 0;
67 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
68 EXT4_I(inode)->jinode,
69 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040070}
71
Alex Tomas64769242008-07-11 19:27:31 -040072static void ext4_invalidatepage(struct page *page, unsigned long offset);
Theodore Ts'ocb20d512010-10-27 21:30:09 -040073static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
74 struct buffer_head *bh_result, int create);
75static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
76static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
77static int __ext4_journalled_writepage(struct page *page, unsigned int len);
78static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Alex Tomas64769242008-07-11 19:27:31 -040079
Dave Kleikampac27a0e2006-10-11 01:20:50 -070080/*
81 * Test whether an inode is a fast symlink.
82 */
Mingming Cao617ba132006-10-11 01:20:53 -070083static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084{
Mingming Cao617ba132006-10-11 01:20:53 -070085 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086 (inode->i_sb->s_blocksize >> 9) : 0;
87
88 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
89}
90
91/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092 * Work out how many blocks we need to proceed with the next chunk of a
93 * truncate transaction.
94 */
95static unsigned long blocks_for_truncate(struct inode *inode)
96{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -050097 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070098
99 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
100
101 /* Give ourselves just enough room to cope with inodes in which
102 * i_blocks is corrupt: we've seen disk corruptions in the past
103 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700104 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 * will go a bit crazy if that happens, but at least we should
106 * try not to panic the whole kernel. */
107 if (needed < 2)
108 needed = 2;
109
110 /* But we need to bound the transaction so we don't overflow the
111 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700112 if (needed > EXT4_MAX_TRANS_DATA)
113 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114
Mingming Cao617ba132006-10-11 01:20:53 -0700115 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116}
117
118/*
119 * Truncate transactions can be complex and absolutely huge. So we need to
120 * be able to restart the transaction at a conventient checkpoint to make
121 * sure we don't overflow the journal.
122 *
123 * start_transaction gets us a new handle for a truncate transaction,
124 * and extend_transaction tries to extend the existing one a bit. If
125 * extend fails, we need to propagate the failure up and restart the
126 * transaction in the top-level truncate loop. --sct
127 */
128static handle_t *start_transaction(struct inode *inode)
129{
130 handle_t *result;
131
Mingming Cao617ba132006-10-11 01:20:53 -0700132 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 if (!IS_ERR(result))
134 return result;
135
Mingming Cao617ba132006-10-11 01:20:53 -0700136 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137 return result;
138}
139
140/*
141 * Try to extend this transaction for the purposes of truncation.
142 *
143 * Returns 0 if we managed to create more room. If we can't create more
144 * room, and the transaction must be restarted we return 1.
145 */
146static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
147{
Frank Mayhar03901312009-01-07 00:06:22 -0500148 if (!ext4_handle_valid(handle))
149 return 0;
150 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700151 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700152 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153 return 0;
154 return 1;
155}
156
157/*
158 * Restart the transaction associated with *handle. This does a commit,
159 * so before we call here everything must be consistently dirtied against
160 * this transaction.
161 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500162int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400163 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164{
Jan Kara487caee2009-08-17 22:17:20 -0400165 int ret;
166
167 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400168 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400169 * moment, get_block can be called only for blocks inside i_size since
170 * page cache has been already dropped and writes are blocked by
171 * i_mutex. So we can safely drop the i_data_sem here.
172 */
Frank Mayhar03901312009-01-07 00:06:22 -0500173 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400175 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500176 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400177 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500178 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400179
180 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181}
182
183/*
184 * Called at the last iput() if i_nlink is zero.
185 */
Al Viro0930fcc2010-06-07 13:16:22 -0400186void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187{
188 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400189 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500191 trace_ext4_evict_inode(inode);
Jiaying Zhang2526f362011-08-13 12:17:13 -0400192
Jiaying Zhang2526f362011-08-13 12:17:13 -0400193 ext4_ioend_wait(inode);
194
Al Viro0930fcc2010-06-07 13:16:22 -0400195 if (inode->i_nlink) {
196 truncate_inode_pages(&inode->i_data, 0);
197 goto no_delete;
198 }
199
Christoph Hellwig907f4552010-03-03 09:05:06 -0500200 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500201 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500202
Jan Kara678aaf42008-07-11 19:27:31 -0400203 if (ext4_should_order_data(inode))
204 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205 truncate_inode_pages(&inode->i_data, 0);
206
207 if (is_bad_inode(inode))
208 goto no_delete;
209
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400210 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400212 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 /*
214 * If we're going to skip the normal cleanup, we still need to
215 * make sure that the in-core orphan linked list is properly
216 * cleaned up.
217 */
Mingming Cao617ba132006-10-11 01:20:53 -0700218 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 goto no_delete;
220 }
221
222 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500223 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400225 err = ext4_mark_inode_dirty(handle, inode);
226 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500227 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400228 "couldn't mark inode dirty (err %d)", err);
229 goto stop_handle;
230 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700232 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400233
234 /*
235 * ext4_ext_truncate() doesn't reserve any slop when it
236 * restarts journal transactions; therefore there may not be
237 * enough credits left in the handle to remove the inode from
238 * the orphan list and set the dtime field.
239 */
Frank Mayhar03901312009-01-07 00:06:22 -0500240 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400241 err = ext4_journal_extend(handle, 3);
242 if (err > 0)
243 err = ext4_journal_restart(handle, 3);
244 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500245 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400246 "couldn't extend journal (err %d)", err);
247 stop_handle:
248 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400249 ext4_orphan_del(NULL, inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400250 goto no_delete;
251 }
252 }
253
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700255 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700257 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700259 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 * (Well, we could do this if we need to, but heck - it works)
261 */
Mingming Cao617ba132006-10-11 01:20:53 -0700262 ext4_orphan_del(handle, inode);
263 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264
265 /*
266 * One subtle ordering requirement: if anything has gone wrong
267 * (transaction abort, IO errors, whatever), then we can still
268 * do these next steps (the fs will already have been marked as
269 * having errors), but we can't free the inode if the mark_dirty
270 * fails.
271 */
Mingming Cao617ba132006-10-11 01:20:53 -0700272 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400274 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 else
Mingming Cao617ba132006-10-11 01:20:53 -0700276 ext4_free_inode(handle, inode);
277 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 return;
279no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400280 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281}
282
283typedef struct {
284 __le32 *p;
285 __le32 key;
286 struct buffer_head *bh;
287} Indirect;
288
289static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
290{
291 p->key = *(p->p = v);
292 p->bh = bh;
293}
294
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295/**
Mingming Cao617ba132006-10-11 01:20:53 -0700296 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 * @inode: inode in question (we are only interested in its superblock)
298 * @i_block: block number to be parsed
299 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400300 * @boundary: set this non-zero if the referred-to block is likely to be
301 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 *
Mingming Cao617ba132006-10-11 01:20:53 -0700303 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 * for UNIX filesystems - tree of pointers anchored in the inode, with
305 * data blocks at leaves and indirect blocks in intermediate nodes.
306 * This function translates the block number into path in that tree -
307 * return value is the path length and @offsets[n] is the offset of
308 * pointer to (n+1)th node in the nth one. If @block is out of range
309 * (negative or too large) warning is printed and zero returned.
310 *
311 * Note: function doesn't find node addresses, so no IO is needed. All
312 * we need to know is the capacity of indirect blocks (taken from the
313 * inode->i_sb).
314 */
315
316/*
317 * Portability note: the last comparison (check that we fit into triple
318 * indirect block) is spelled differently, because otherwise on an
319 * architecture with 32-bit longs and 8Kb pages we might get into trouble
320 * if our filesystem had 8Kb blocks. We might use long long, but that would
321 * kill us on x86. Oh, well, at least the sign propagation does not matter -
322 * i_block would have to be negative in the very beginning, so we would not
323 * get there at all.
324 */
325
Mingming Cao617ba132006-10-11 01:20:53 -0700326static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400327 ext4_lblk_t i_block,
328 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329{
Mingming Cao617ba132006-10-11 01:20:53 -0700330 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
331 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
332 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 indirect_blocks = ptrs,
334 double_blocks = (1 << (ptrs_bits * 2));
335 int n = 0;
336 int final = 0;
337
Roel Kluinc333e072009-08-10 22:47:22 -0400338 if (i_block < direct_blocks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700339 offsets[n++] = i_block;
340 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400341 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700342 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 offsets[n++] = i_block;
344 final = ptrs;
345 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700346 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700347 offsets[n++] = i_block >> ptrs_bits;
348 offsets[n++] = i_block & (ptrs - 1);
349 final = ptrs;
350 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700351 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 offsets[n++] = i_block >> (ptrs_bits * 2);
353 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
354 offsets[n++] = i_block & (ptrs - 1);
355 final = ptrs;
356 } else {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500357 ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400358 i_block + direct_blocks +
359 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360 }
361 if (boundary)
362 *boundary = final - 1 - (i_block & (ptrs - 1));
363 return n;
364}
365
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400366static int __ext4_check_blockref(const char *function, unsigned int line,
367 struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400368 __le32 *p, unsigned int max)
369{
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400370 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400371 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400372 unsigned int blk;
373
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400374 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400375 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400376 if (blk &&
377 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400378 blk, 1))) {
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400379 es->s_last_error_block = cpu_to_le64(blk);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400380 ext4_error_inode(inode, function, line, blk,
381 "invalid block");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400382 return -EIO;
383 }
384 }
385 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400386}
387
388
389#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400390 __ext4_check_blockref(__func__, __LINE__, inode, \
391 (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400392 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
393
394#define ext4_check_inode_blockref(inode) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400395 __ext4_check_blockref(__func__, __LINE__, inode, \
396 EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400397 EXT4_NDIR_BLOCKS)
398
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399/**
Mingming Cao617ba132006-10-11 01:20:53 -0700400 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401 * @inode: inode in question
402 * @depth: depth of the chain (1 - direct pointer, etc.)
403 * @offsets: offsets of pointers in inode/indirect blocks
404 * @chain: place to store the result
405 * @err: here we store the error value
406 *
407 * Function fills the array of triples <key, p, bh> and returns %NULL
408 * if everything went OK or the pointer to the last filled triple
409 * (incomplete one) otherwise. Upon the return chain[i].key contains
410 * the number of (i+1)-th block in the chain (as it is stored in memory,
411 * i.e. little-endian 32-bit), chain[i].p contains the address of that
412 * number (it points into struct inode for i==0 and into the bh->b_data
413 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
414 * block for i>0 and NULL for i==0. In other words, it holds the block
415 * numbers of the chain, addresses they were taken from (and where we can
416 * verify that chain did not change) and buffer_heads hosting these
417 * numbers.
418 *
419 * Function stops when it stumbles upon zero pointer (absent block)
420 * (pointer to last triple returned, *@err == 0)
421 * or when it gets an IO error reading an indirect block
422 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423 * or when it reads all @depth-1 indirect blocks successfully and finds
424 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500425 *
426 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500427 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500429static Indirect *ext4_get_branch(struct inode *inode, int depth,
430 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431 Indirect chain[4], int *err)
432{
433 struct super_block *sb = inode->i_sb;
434 Indirect *p = chain;
435 struct buffer_head *bh;
436
437 *err = 0;
438 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400439 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 if (!p->key)
441 goto no_block;
442 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400443 bh = sb_getblk(sb, le32_to_cpu(p->key));
444 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400446
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400447 if (!bh_uptodate_or_lock(bh)) {
448 if (bh_submit_read(bh) < 0) {
449 put_bh(bh);
450 goto failure;
451 }
452 /* validate block references */
453 if (ext4_check_indirect_blockref(inode, bh)) {
454 put_bh(bh);
455 goto failure;
456 }
457 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400458
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400459 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 /* Reader: end */
461 if (!p->key)
462 goto no_block;
463 }
464 return NULL;
465
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466failure:
467 *err = -EIO;
468no_block:
469 return p;
470}
471
472/**
Mingming Cao617ba132006-10-11 01:20:53 -0700473 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 * @inode: owner
475 * @ind: descriptor of indirect block.
476 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000477 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 * It is used when heuristic for sequential allocation fails.
479 * Rules are:
480 * + if there is a block to the left of our position - allocate near it.
481 * + if pointer will live in indirect block - allocate near that block.
482 * + if pointer will live in inode - allocate in the same
483 * cylinder group.
484 *
485 * In the latter case we colour the starting block by the callers PID to
486 * prevent it from clashing with concurrent allocations for a different inode
487 * in the same block group. The PID is used here so that functionally related
488 * files will be close-by on-disk.
489 *
490 * Caller must make sure that @ind is valid and will stay that way.
491 */
Mingming Cao617ba132006-10-11 01:20:53 -0700492static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493{
Mingming Cao617ba132006-10-11 01:20:53 -0700494 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400495 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700497 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500498 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700499 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400500 ext4_group_t block_group;
501 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502
503 /* Try to find previous block */
504 for (p = ind->p - 1; p >= start; p--) {
505 if (*p)
506 return le32_to_cpu(*p);
507 }
508
509 /* No such thing, so let's try location of indirect block */
510 if (ind->bh)
511 return ind->bh->b_blocknr;
512
513 /*
514 * It is going to be referred to from the inode itself? OK, just put it
515 * into the same cylinder group then.
516 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400517 block_group = ei->i_block_group;
518 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
519 block_group &= ~(flex_size-1);
520 if (S_ISREG(inode->i_mode))
521 block_group++;
522 }
523 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500524 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
525
Theodore Ts'oa4912122009-03-12 12:18:34 -0400526 /*
527 * If we are doing delayed allocation, we don't need take
528 * colour into account.
529 */
530 if (test_opt(inode->i_sb, DELALLOC))
531 return bg_start;
532
Valerie Clement74d34872008-02-15 13:43:07 -0500533 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
534 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700535 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500536 else
537 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 return bg_start + colour;
539}
540
541/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000542 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 * @inode: owner
544 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000547 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800548 * returns it.
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400549 * Because this is only used for non-extent files, we limit the block nr
550 * to 32 bits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500552static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400553 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554{
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400555 ext4_fsblk_t goal;
556
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400558 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400561 goal = ext4_find_near(inode, partial);
562 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
563 return goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564}
565
566/**
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500567 * ext4_blks_to_allocate - Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 * of direct blocks need to be allocated for the given branch.
569 *
570 * @branch: chain of indirect blocks
571 * @k: number of blocks need for indirect blocks
572 * @blks: number of data blocks to be mapped.
573 * @blocks_to_boundary: the offset in the indirect block
574 *
575 * return the total number of blocks to be allocate, including the
576 * direct and indirect blocks.
577 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500578static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400579 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500581 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582
583 /*
584 * Simple case, [t,d]Indirect block(s) has not allocated yet
585 * then it's clear blocks on that path have not allocated
586 */
587 if (k > 0) {
588 /* right now we don't handle cross boundary allocation */
589 if (blks < blocks_to_boundary + 1)
590 count += blks;
591 else
592 count += blocks_to_boundary + 1;
593 return count;
594 }
595
596 count++;
597 while (count < blks && count <= blocks_to_boundary &&
598 le32_to_cpu(*(branch[0].p + count)) == 0) {
599 count++;
600 }
601 return count;
602}
603
604/**
Mingming Cao617ba132006-10-11 01:20:53 -0700605 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500606 * @handle: handle for this transaction
607 * @inode: inode which needs allocated blocks
608 * @iblock: the logical block to start allocated at
609 * @goal: preferred physical block of allocation
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 * @indirect_blks: the number of blocks need to allocate for indirect
611 * blocks
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500612 * @blks: number of desired blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613 * @new_blocks: on return it will store the new block numbers for
614 * the indirect blocks(if needed) and the first direct block,
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500615 * @err: on return it will store the error code
616 *
617 * This function will return the number of blocks allocated as
618 * requested by the passed-in parameters.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 */
Mingming Cao617ba132006-10-11 01:20:53 -0700620static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400621 ext4_lblk_t iblock, ext4_fsblk_t goal,
622 int indirect_blks, int blks,
623 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500625 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400627 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700629 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 int ret = 0;
631
632 /*
633 * Here we try to allocate the requested multiple blocks at once,
634 * on a best-effort basis.
635 * To build a branch, we should allocate blocks for
636 * the indirect blocks(if not allocated yet), and at least
637 * the first direct block of this branch. That's the
638 * minimum number of blocks need to allocate(required)
639 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400640 /* first we try to allocate the indirect blocks */
641 target = indirect_blks;
642 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 count = target;
644 /* allocating blocks for indirect blocks and direct blocks */
Allison Henderson55f020d2011-05-25 07:41:26 -0400645 current_block = ext4_new_meta_blocks(handle, inode, goal,
646 0, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 if (*err)
648 goto failed_out;
649
Frank Mayhar273df552010-03-02 11:46:09 -0500650 if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) {
651 EXT4_ERROR_INODE(inode,
652 "current_block %llu + count %lu > %d!",
653 current_block, count,
654 EXT4_MAX_BLOCK_FILE_PHYS);
655 *err = -EIO;
656 goto failed_out;
657 }
Eric Sandeenfb0a3872009-09-16 14:45:10 -0400658
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 target -= count;
660 /* allocate blocks for indirect blocks */
661 while (index < indirect_blks && count) {
662 new_blocks[index++] = current_block++;
663 count--;
664 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400665 if (count > 0) {
666 /*
667 * save the new block number
668 * for the first direct block
669 */
670 new_blocks[index] = current_block;
671 printk(KERN_INFO "%s returned more blocks than "
672 "requested\n", __func__);
673 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400675 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 }
677
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400678 target = blks - count ;
679 blk_allocated = count;
680 if (!target)
681 goto allocated;
682 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500683 memset(&ar, 0, sizeof(ar));
684 ar.inode = inode;
685 ar.goal = goal;
686 ar.len = target;
687 ar.logical = iblock;
688 if (S_ISREG(inode->i_mode))
689 /* enable in-core preallocation only for regular files */
690 ar.flags = EXT4_MB_HINT_DATA;
691
692 current_block = ext4_mb_new_blocks(handle, &ar, err);
Frank Mayhar273df552010-03-02 11:46:09 -0500693 if (unlikely(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS)) {
694 EXT4_ERROR_INODE(inode,
695 "current_block %llu + ar.len %d > %d!",
696 current_block, ar.len,
697 EXT4_MAX_BLOCK_FILE_PHYS);
698 *err = -EIO;
699 goto failed_out;
700 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500701
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400702 if (*err && (target == blks)) {
703 /*
704 * if the allocation failed and we didn't allocate
705 * any blocks before
706 */
707 goto failed_out;
708 }
709 if (!*err) {
710 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400711 /*
712 * save the new block number
713 * for the first direct block
714 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400715 new_blocks[index] = current_block;
716 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500717 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400718 }
719allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400721 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 *err = 0;
723 return ret;
724failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400725 for (i = 0; i < index; i++)
Peter Huewe7dc57612011-02-21 21:01:42 -0500726 ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 return ret;
728}
729
730/**
Mingming Cao617ba132006-10-11 01:20:53 -0700731 * ext4_alloc_branch - allocate and set up a chain of blocks.
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500732 * @handle: handle for this transaction
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 * @inode: owner
734 * @indirect_blks: number of allocated indirect blocks
735 * @blks: number of allocated direct blocks
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500736 * @goal: preferred place for allocation
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 * @offsets: offsets (in the blocks) to store the pointers to next.
738 * @branch: place to store the chain in.
739 *
740 * This function allocates blocks, zeroes out all but the last one,
741 * links them into chain and (if we are synchronous) writes them to disk.
742 * In other words, it prepares a branch that can be spliced onto the
743 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700744 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 * we had read the existing part of chain and partial points to the last
746 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700747 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748 * place chain is disconnected - *branch->p is still zero (we did not
749 * set the last link), but branch->key contains the number that should
750 * be placed into *branch->p to fill that gap.
751 *
752 * If allocation fails we free all blocks we've allocated (and forget
753 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700754 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755 * as described above and return 0.
756 */
Mingming Cao617ba132006-10-11 01:20:53 -0700757static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400758 ext4_lblk_t iblock, int indirect_blks,
759 int *blks, ext4_fsblk_t goal,
760 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761{
762 int blocksize = inode->i_sb->s_blocksize;
763 int i, n = 0;
764 int err = 0;
765 struct buffer_head *bh;
766 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700767 ext4_fsblk_t new_blocks[4];
768 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400770 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 *blks, new_blocks, &err);
772 if (err)
773 return err;
774
775 branch[0].key = cpu_to_le32(new_blocks[0]);
776 /*
777 * metadata blocks and data blocks are allocated.
778 */
779 for (n = 1; n <= indirect_blks; n++) {
780 /*
781 * Get buffer_head for parent block, zero it out
782 * and set the pointer to new one, then send
783 * parent to disk.
784 */
785 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
Namhyung Kim87783692010-10-27 21:30:11 -0400786 if (unlikely(!bh)) {
787 err = -EIO;
788 goto failed;
789 }
790
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 branch[n].bh = bh;
792 lock_buffer(bh);
793 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700794 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400796 /* Don't brelse(bh) here; it's done in
797 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799 goto failed;
800 }
801
802 memset(bh->b_data, 0, blocksize);
803 branch[n].p = (__le32 *) bh->b_data + offsets[n];
804 branch[n].key = cpu_to_le32(new_blocks[n]);
805 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400806 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 current_block = new_blocks[n];
808 /*
809 * End of chain, update the last new metablock of
810 * the chain to point to the new allocated
811 * data blocks numbers
812 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400813 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 *(branch[n].p + i) = cpu_to_le32(++current_block);
815 }
816 BUFFER_TRACE(bh, "marking uptodate");
817 set_buffer_uptodate(bh);
818 unlock_buffer(bh);
819
Frank Mayhar03901312009-01-07 00:06:22 -0500820 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
821 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 if (err)
823 goto failed;
824 }
825 *blks = num;
826 return err;
827failed:
828 /* Allocation failed, free what we already allocated */
Peter Huewe7dc57612011-02-21 21:01:42 -0500829 ext4_free_blocks(handle, inode, NULL, new_blocks[0], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 for (i = 1; i <= n ; i++) {
Theodore Ts'o60e66792010-05-17 07:00:00 -0400831 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500832 * branch[i].bh is newly allocated, so there is no
833 * need to revoke the block, which is why we don't
834 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500835 */
Peter Huewe7dc57612011-02-21 21:01:42 -0500836 ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -0500837 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 }
Theodore Ts'oe6362602009-11-23 07:17:05 -0500839 for (i = n+1; i < indirect_blks; i++)
Peter Huewe7dc57612011-02-21 21:01:42 -0500840 ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841
Peter Huewe7dc57612011-02-21 21:01:42 -0500842 ext4_free_blocks(handle, inode, NULL, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843
844 return err;
845}
846
847/**
Mingming Cao617ba132006-10-11 01:20:53 -0700848 * ext4_splice_branch - splice the allocated branch onto inode.
Theodore Ts'o225db7d2010-12-16 16:38:26 -0500849 * @handle: handle for this transaction
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 * @inode: owner
851 * @block: (logical) number of block we are adding
852 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700853 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 * @where: location of missing link
855 * @num: number of indirect blocks we are adding
856 * @blks: number of direct blocks we are adding
857 *
858 * This function fills the missing link and does all housekeeping needed in
859 * inode (->i_blocks, etc.). In case of success we end up with the full
860 * chain to new block and return 0.
861 */
Mingming Cao617ba132006-10-11 01:20:53 -0700862static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400863 ext4_lblk_t block, Indirect *where, int num,
864 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865{
866 int i;
867 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700868 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 /*
871 * If we're splicing into a [td]indirect block (as opposed to the
872 * inode) then we need to get write access to the [td]indirect block
873 * before the splice.
874 */
875 if (where->bh) {
876 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700877 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878 if (err)
879 goto err_out;
880 }
881 /* That's it */
882
883 *where->p = where->key;
884
885 /*
886 * Update the host buffer_head or inode to point to more just allocated
887 * direct blocks blocks
888 */
889 if (num == 0 && blks > 1) {
890 current_block = le32_to_cpu(where->key) + 1;
891 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400892 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893 }
894
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896 /* had we spliced it onto indirect block? */
897 if (where->bh) {
898 /*
899 * If we spliced it onto an indirect block, we haven't
900 * altered the inode. Note however that if it is being spliced
901 * onto an indirect block at the very end of the file (the
902 * file is growing) then we *will* alter the inode to reflect
903 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700904 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 */
906 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500907 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
908 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 if (err)
910 goto err_out;
911 } else {
912 /*
913 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400915 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916 jbd_debug(5, "splicing direct\n");
917 }
918 return err;
919
920err_out:
921 for (i = 1; i <= num; i++) {
Theodore Ts'o60e66792010-05-17 07:00:00 -0400922 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -0500923 * branch[i].bh is newly allocated, so there is no
924 * need to revoke the block, which is why we don't
925 * need to set EXT4_FREE_BLOCKS_METADATA.
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500926 */
Theodore Ts'oe6362602009-11-23 07:17:05 -0500927 ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
928 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929 }
Peter Huewe7dc57612011-02-21 21:01:42 -0500930 ext4_free_blocks(handle, inode, NULL, le32_to_cpu(where[num].key),
Theodore Ts'oe6362602009-11-23 07:17:05 -0500931 blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932
933 return err;
934}
935
936/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400937 * The ext4_ind_map_blocks() function handles non-extents inodes
Theodore Ts'ob920c752009-05-14 00:54:29 -0400938 * (i.e., using the traditional indirect/double-indirect i_blocks
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400939 * scheme) for ext4_map_blocks().
Theodore Ts'ob920c752009-05-14 00:54:29 -0400940 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941 * Allocation strategy is simple: if we have to allocate something, we will
942 * have to go the whole way to leaf. So let's do it before attaching anything
943 * to tree, set linkage between the newborn blocks, write them if sync is
944 * required, recheck the path, free and repeat if check fails, otherwise
945 * set the last missing link (that will protect us from any truncate-generated
946 * removals - all blocks on the path are immune now) and possibly force the
947 * write on the parent block.
948 * That has a nice additional property: no special recovery from the failed
949 * allocations is needed - we simply release blocks and do not touch anything
950 * reachable from inode.
951 *
952 * `handle' can be NULL if create == 0.
953 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 * return > 0, # of blocks mapped or allocated.
955 * return = 0, if plain lookup failed.
956 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500957 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400958 * The ext4_ind_get_blocks() function should be called with
959 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
960 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
961 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
962 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400964static int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
965 struct ext4_map_blocks *map,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400966 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967{
968 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500969 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 Indirect chain[4];
971 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700972 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 int indirect_blks;
974 int blocks_to_boundary = 0;
975 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700977 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400979 trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400980 J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400981 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400982 depth = ext4_block_to_path(inode, map->m_lblk, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400983 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984
985 if (depth == 0)
986 goto out;
987
Mingming Cao617ba132006-10-11 01:20:53 -0700988 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989
990 /* Simplest case - block found, no allocation needed */
991 if (!partial) {
992 first_block = le32_to_cpu(chain[depth - 1].key);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 count++;
994 /*map more blocks*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400995 while (count < map->m_len && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700996 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700997
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 blk = le32_to_cpu(*(chain[depth-1].p + count));
999
1000 if (blk == first_block + count)
1001 count++;
1002 else
1003 break;
1004 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05001005 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 }
1007
1008 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001009 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 goto cleanup;
1011
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04001013 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001015 goal = ext4_find_goal(inode, map->m_lblk, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016
1017 /* the number of blocks need to allocate for [d,t]indirect blocks */
1018 indirect_blks = (chain + depth) - partial - 1;
1019
1020 /*
1021 * Next look up the indirect map to count the totoal number of
1022 * direct blocks to allocate for this branch.
1023 */
Mingming Cao617ba132006-10-11 01:20:53 -07001024 count = ext4_blks_to_allocate(partial, indirect_blks,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001025 map->m_len, blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001027 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001028 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001029 err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001030 &count, goal,
1031 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032
1033 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001034 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 * on the new chain if there is a failure, but that risks using
1036 * up transaction credits, especially for bitmaps where the
1037 * credits cannot be returned. Can we handle this somehow? We
1038 * may need to return -EAGAIN upwards in the worst case. --sct
1039 */
1040 if (!err)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001041 err = ext4_splice_branch(handle, inode, map->m_lblk,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001042 partial, indirect_blks, count);
Jan Kara2bba7022009-11-23 07:24:48 -05001043 if (err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001044 goto cleanup;
1045
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001046 map->m_flags |= EXT4_MAP_NEW;
Jan Karab436b9b2009-12-08 23:51:10 -05001047
1048 ext4_update_inode_fsync_trans(handle, inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049got_it:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001050 map->m_flags |= EXT4_MAP_MAPPED;
1051 map->m_pblk = le32_to_cpu(chain[depth-1].key);
1052 map->m_len = count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 if (count > blocks_to_boundary)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001054 map->m_flags |= EXT4_MAP_BOUNDARY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 err = count;
1056 /* Clean up and exit */
1057 partial = chain + depth - 1; /* the whole chain */
1058cleanup:
1059 while (partial > chain) {
1060 BUFFER_TRACE(partial->bh, "call brelse");
1061 brelse(partial->bh);
1062 partial--;
1063 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064out:
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04001065 trace_ext4_ind_map_blocks_exit(inode, map->m_lblk,
1066 map->m_pblk, map->m_len, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 return err;
1068}
1069
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001070#ifdef CONFIG_QUOTA
1071qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +01001072{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001073 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +01001074}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001075#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001076
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001077/*
1078 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001079 * to allocate a new block at @lblocks for non extent file based file
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001080 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001081static int ext4_indirect_calc_metadata_amount(struct inode *inode,
1082 sector_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001083{
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001084 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karad330a5b2010-03-14 18:17:54 -04001085 sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001086 int blk_bits;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001087
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001088 if (lblock < EXT4_NDIR_BLOCKS)
1089 return 0;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001090
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001091 lblock -= EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001092
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001093 if (ei->i_da_metadata_calc_len &&
1094 (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
1095 ei->i_da_metadata_calc_len++;
1096 return 0;
1097 }
1098 ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
1099 ei->i_da_metadata_calc_len = 1;
Jan Karad330a5b2010-03-14 18:17:54 -04001100 blk_bits = order_base_2(lblock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001101 return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001102}
1103
1104/*
1105 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001106 * to allocate a block located at @lblock
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001107 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001108static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001109{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001110 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001111 return ext4_ext_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001112
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001113 return ext4_indirect_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001114}
1115
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001116/*
1117 * Called with i_data_sem down, which is important since we can call
1118 * ext4_discard_preallocations() from here.
1119 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001120void ext4_da_update_reserve_space(struct inode *inode,
1121 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001122{
1123 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001124 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001125
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001126 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'of8ec9d62010-01-01 01:00:21 -05001127 trace_ext4_da_update_reserve_space(inode, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001128 if (unlikely(used > ei->i_reserved_data_blocks)) {
1129 ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
1130 "with only %d reserved data blocks\n",
1131 __func__, inode->i_ino, used,
1132 ei->i_reserved_data_blocks);
1133 WARN_ON(1);
1134 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001135 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001136
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001137 /* Update per-inode reservations */
1138 ei->i_reserved_data_blocks -= used;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001139 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001140 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1141 used + ei->i_allocated_meta_blocks);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001142 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001143
1144 if (ei->i_reserved_data_blocks == 0) {
1145 /*
1146 * We can release all of the reserved metadata blocks
1147 * only when we have written all of the delayed
1148 * allocation blocks.
1149 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001150 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1151 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001152 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001153 ei->i_da_metadata_calc_len = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001154 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001155 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001156
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001157 /* Update quota subsystem for data blocks */
1158 if (quota_claim)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001159 dquot_claim_block(inode, used);
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001160 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001161 /*
1162 * We did fallocate with an offset that is already delayed
1163 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001164 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001165 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001166 dquot_release_reservation_block(inode, used);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001167 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001168
1169 /*
1170 * If we have done all the pending block allocations and if
1171 * there aren't any writers on the inode, we can discard the
1172 * inode's preallocations.
1173 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001174 if ((ei->i_reserved_data_blocks == 0) &&
1175 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001176 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001177}
1178
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001179static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -04001180 unsigned int line,
1181 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001182{
Theodore Ts'o24676da2010-05-16 21:00:00 -04001183 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
1184 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04001185 ext4_error_inode(inode, func, line, map->m_pblk,
1186 "lblock %lu mapped to illegal pblock "
1187 "(length %d)", (unsigned long) map->m_lblk,
1188 map->m_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001189 return -EIO;
1190 }
1191 return 0;
1192}
1193
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001194#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -04001195 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001196
Mingming Caof5ab0d12008-02-25 15:29:55 -05001197/*
Theodore Ts'o1f945332009-09-30 22:57:41 -04001198 * Return the number of contiguous dirty pages in a given inode
1199 * starting at page frame idx.
Theodore Ts'o55138e02009-09-29 13:31:31 -04001200 */
1201static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
1202 unsigned int max_pages)
1203{
1204 struct address_space *mapping = inode->i_mapping;
1205 pgoff_t index;
1206 struct pagevec pvec;
1207 pgoff_t num = 0;
1208 int i, nr_pages, done = 0;
1209
1210 if (max_pages == 0)
1211 return 0;
1212 pagevec_init(&pvec, 0);
1213 while (!done) {
1214 index = idx;
1215 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1216 PAGECACHE_TAG_DIRTY,
1217 (pgoff_t)PAGEVEC_SIZE);
1218 if (nr_pages == 0)
1219 break;
1220 for (i = 0; i < nr_pages; i++) {
1221 struct page *page = pvec.pages[i];
1222 struct buffer_head *bh, *head;
1223
1224 lock_page(page);
1225 if (unlikely(page->mapping != mapping) ||
1226 !PageDirty(page) ||
1227 PageWriteback(page) ||
1228 page->index != idx) {
1229 done = 1;
1230 unlock_page(page);
1231 break;
1232 }
Theodore Ts'o1f945332009-09-30 22:57:41 -04001233 if (page_has_buffers(page)) {
1234 bh = head = page_buffers(page);
1235 do {
1236 if (!buffer_delay(bh) &&
1237 !buffer_unwritten(bh))
1238 done = 1;
1239 bh = bh->b_this_page;
1240 } while (!done && (bh != head));
1241 }
Theodore Ts'o55138e02009-09-29 13:31:31 -04001242 unlock_page(page);
1243 if (done)
1244 break;
1245 idx++;
1246 num++;
Eric Sandeen659c6002010-10-27 21:30:03 -04001247 if (num >= max_pages) {
1248 done = 1;
Theodore Ts'o55138e02009-09-29 13:31:31 -04001249 break;
Eric Sandeen659c6002010-10-27 21:30:03 -04001250 }
Theodore Ts'o55138e02009-09-29 13:31:31 -04001251 }
1252 pagevec_release(&pvec);
1253 }
1254 return num;
1255}
1256
1257/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001258 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001259 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001260 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001261 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1262 * and store the allocated blocks in the result buffer head and mark it
1263 * mapped.
1264 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001265 * If file type is extents based, it will call ext4_ext_map_blocks(),
1266 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001267 * based files
1268 *
1269 * On success, it returns the number of blocks being mapped or allocate.
1270 * if create==0 and the blocks are pre-allocated and uninitialized block,
1271 * the result buffer head is unmapped. If the create ==1, it will make sure
1272 * the buffer head is mapped.
1273 *
1274 * It returns 0 if plain look up failed (blocks have not been allocated), in
1275 * that casem, buffer head is unmapped
1276 *
1277 * It returns the error in case of allocation failure.
1278 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001279int ext4_map_blocks(handle_t *handle, struct inode *inode,
1280 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001281{
1282 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001283
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001284 map->m_flags = 0;
1285 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
1286 "logical block %lu\n", inode->i_ino, flags, map->m_len,
1287 (unsigned long) map->m_lblk);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001288 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001289 * Try to see if we can get the block without requesting a new
1290 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001291 */
1292 down_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001293 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001294 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001295 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001296 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001297 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001298 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001299
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001300 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001301 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001302 if (ret != 0)
1303 return ret;
1304 }
1305
Mingming Caof5ab0d12008-02-25 15:29:55 -05001306 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001307 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001308 return retval;
1309
1310 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001311 * Returns if the blocks have already allocated
1312 *
1313 * Note that if blocks have been preallocated
1314 * ext4_ext_get_block() returns th create = 0
1315 * with buffer head unmapped.
1316 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001317 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Mingming Caof5ab0d12008-02-25 15:29:55 -05001318 return retval;
1319
1320 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001321 * When we call get_blocks without the create flag, the
1322 * BH_Unwritten flag could have gotten set if the blocks
1323 * requested were part of a uninitialized extent. We need to
1324 * clear this flag now that we are committed to convert all or
1325 * part of the uninitialized extent to be an initialized
1326 * extent. This is because we need to avoid the combination
1327 * of BH_Unwritten and BH_Mapped flags being simultaneously
1328 * set on the buffer_head.
1329 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001330 map->m_flags &= ~EXT4_MAP_UNWRITTEN;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001331
1332 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001333 * New blocks allocate and/or writing to uninitialized extent
1334 * will possibly result in updating i_data, so we take
1335 * the write lock of i_data_sem, and call get_blocks()
1336 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001337 */
1338 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001339
1340 /*
1341 * if the caller is from delayed allocation writeout path
1342 * we have already reserved fs blocks for allocation
1343 * let the underlying get_block() function know to
1344 * avoid double accounting
1345 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001346 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -05001347 ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001348 /*
1349 * We need to check for EXT4 here because migrate
1350 * could have changed the inode type in between
1351 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001352 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001353 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001354 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001355 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001356
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001357 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001358 /*
1359 * We allocated new blocks which will result in
1360 * i_data's format changing. Force the migrate
1361 * to fail by clearing migrate flags
1362 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001363 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001364 }
Mingming Caod2a17632008-07-14 17:52:37 -04001365
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001366 /*
1367 * Update reserved blocks/metadata blocks after successful
1368 * block allocation which had been deferred till now. We don't
1369 * support fallocate for non extent files. So we can update
1370 * reserve space here.
1371 */
1372 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001373 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05001374 ext4_da_update_reserve_space(inode, retval, 1);
1375 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001376 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -05001377 ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001378
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001379 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -04001380 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001381 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001382 if (ret != 0)
1383 return ret;
1384 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001385 return retval;
1386}
1387
Mingming Caof3bd1f32008-08-19 22:16:03 -04001388/* Maximum number of blocks we map for direct IO at once. */
1389#define DIO_MAX_BLOCKS 4096
1390
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001391static int _ext4_get_block(struct inode *inode, sector_t iblock,
1392 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001394 handle_t *handle = ext4_journal_current_handle();
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001395 struct ext4_map_blocks map;
Jan Kara7fb54092008-02-10 01:08:38 -05001396 int ret = 0, started = 0;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001397 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001398
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001399 map.m_lblk = iblock;
1400 map.m_len = bh->b_size >> inode->i_blkbits;
1401
1402 if (flags && !handle) {
Jan Kara7fb54092008-02-10 01:08:38 -05001403 /* Direct IO write... */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001404 if (map.m_len > DIO_MAX_BLOCKS)
1405 map.m_len = DIO_MAX_BLOCKS;
1406 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
Mingming Caof3bd1f32008-08-19 22:16:03 -04001407 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001408 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 ret = PTR_ERR(handle);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001410 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 }
Jan Kara7fb54092008-02-10 01:08:38 -05001412 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413 }
1414
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001415 ret = ext4_map_blocks(handle, inode, &map, flags);
Jan Kara7fb54092008-02-10 01:08:38 -05001416 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001417 map_bh(bh, inode->i_sb, map.m_pblk);
1418 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1419 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -05001420 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 }
Jan Kara7fb54092008-02-10 01:08:38 -05001422 if (started)
1423 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 return ret;
1425}
1426
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001427int ext4_get_block(struct inode *inode, sector_t iblock,
1428 struct buffer_head *bh, int create)
1429{
1430 return _ext4_get_block(inode, iblock, bh,
1431 create ? EXT4_GET_BLOCKS_CREATE : 0);
1432}
1433
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434/*
1435 * `handle' can be NULL if create is zero
1436 */
Mingming Cao617ba132006-10-11 01:20:53 -07001437struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001438 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001440 struct ext4_map_blocks map;
1441 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442 int fatal = 0, err;
1443
1444 J_ASSERT(handle != NULL || create == 0);
1445
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001446 map.m_lblk = block;
1447 map.m_len = 1;
1448 err = ext4_map_blocks(handle, inode, &map,
1449 create ? EXT4_GET_BLOCKS_CREATE : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001451 if (err < 0)
1452 *errp = err;
1453 if (err <= 0)
1454 return NULL;
1455 *errp = 0;
1456
1457 bh = sb_getblk(inode->i_sb, map.m_pblk);
1458 if (!bh) {
1459 *errp = -EIO;
1460 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001462 if (map.m_flags & EXT4_MAP_NEW) {
1463 J_ASSERT(create != 0);
1464 J_ASSERT(handle != NULL);
1465
1466 /*
1467 * Now that we do not always journal data, we should
1468 * keep in mind whether this should always journal the
1469 * new buffer as metadata. For now, regular file
1470 * writes use ext4_get_block instead, so it's not a
1471 * problem.
1472 */
1473 lock_buffer(bh);
1474 BUFFER_TRACE(bh, "call get_create_access");
1475 fatal = ext4_journal_get_create_access(handle, bh);
1476 if (!fatal && !buffer_uptodate(bh)) {
1477 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1478 set_buffer_uptodate(bh);
1479 }
1480 unlock_buffer(bh);
1481 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1482 err = ext4_handle_dirty_metadata(handle, inode, bh);
1483 if (!fatal)
1484 fatal = err;
1485 } else {
1486 BUFFER_TRACE(bh, "not a new buffer");
1487 }
1488 if (fatal) {
1489 *errp = fatal;
1490 brelse(bh);
1491 bh = NULL;
1492 }
1493 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494}
1495
Mingming Cao617ba132006-10-11 01:20:53 -07001496struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001497 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001499 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500
Mingming Cao617ba132006-10-11 01:20:53 -07001501 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001502 if (!bh)
1503 return bh;
1504 if (buffer_uptodate(bh))
1505 return bh;
1506 ll_rw_block(READ_META, 1, &bh);
1507 wait_on_buffer(bh);
1508 if (buffer_uptodate(bh))
1509 return bh;
1510 put_bh(bh);
1511 *err = -EIO;
1512 return NULL;
1513}
1514
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001515static int walk_page_buffers(handle_t *handle,
1516 struct buffer_head *head,
1517 unsigned from,
1518 unsigned to,
1519 int *partial,
1520 int (*fn)(handle_t *handle,
1521 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522{
1523 struct buffer_head *bh;
1524 unsigned block_start, block_end;
1525 unsigned blocksize = head->b_size;
1526 int err, ret = 0;
1527 struct buffer_head *next;
1528
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001529 for (bh = head, block_start = 0;
1530 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001531 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532 next = bh->b_this_page;
1533 block_end = block_start + blocksize;
1534 if (block_end <= from || block_start >= to) {
1535 if (partial && !buffer_uptodate(bh))
1536 *partial = 1;
1537 continue;
1538 }
1539 err = (*fn)(handle, bh);
1540 if (!ret)
1541 ret = err;
1542 }
1543 return ret;
1544}
1545
1546/*
1547 * To preserve ordering, it is essential that the hole instantiation and
1548 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001549 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001550 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 * prepare_write() is the right place.
1552 *
Mingming Cao617ba132006-10-11 01:20:53 -07001553 * Also, this function can nest inside ext4_writepage() ->
1554 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 * has generated enough buffer credits to do the whole page. So we won't
1556 * block on the journal in that case, which is good, because the caller may
1557 * be PF_MEMALLOC.
1558 *
Mingming Cao617ba132006-10-11 01:20:53 -07001559 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560 * quota file writes. If we were to commit the transaction while thus
1561 * reentered, there can be a deadlock - we would be holding a quota
1562 * lock, and the commit would never complete if another thread had a
1563 * transaction open and was blocking on the quota lock - a ranking
1564 * violation.
1565 *
Mingming Caodab291a2006-10-11 01:21:01 -07001566 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 * will _not_ run commit under these circumstances because handle->h_ref
1568 * is elevated. We'll still have enough credits for the tiny quotafile
1569 * write.
1570 */
1571static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001572 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573{
Jan Kara56d35a42010-08-05 14:41:42 -04001574 int dirty = buffer_dirty(bh);
1575 int ret;
1576
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 if (!buffer_mapped(bh) || buffer_freed(bh))
1578 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -04001579 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02001580 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001581 * the dirty bit as jbd2_journal_get_write_access() could complain
1582 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001583 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001584 * the bit before releasing a page lock and thus writeback cannot
1585 * ever write the buffer.
1586 */
1587 if (dirty)
1588 clear_buffer_dirty(bh);
1589 ret = ext4_journal_get_write_access(handle, bh);
1590 if (!ret && dirty)
1591 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1592 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593}
1594
Jan Karab9a42072009-12-08 21:24:33 -05001595/*
1596 * Truncate blocks that were not used by write. We have to truncate the
1597 * pagecache as well so that corresponding buffers get properly unmapped.
1598 */
1599static void ext4_truncate_failed_write(struct inode *inode)
1600{
1601 truncate_inode_pages(inode->i_mapping, inode->i_size);
1602 ext4_truncate(inode);
1603}
1604
Jiaying Zhang744692d2010-03-04 16:14:02 -05001605static int ext4_get_block_write(struct inode *inode, sector_t iblock,
1606 struct buffer_head *bh_result, int create);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001607static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001608 loff_t pos, unsigned len, unsigned flags,
1609 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001611 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001612 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 handle_t *handle;
1614 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001615 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001616 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001617 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001618
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001619 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001620 /*
1621 * Reserve one block more for addition to orphan list in case
1622 * we allocate blocks but write fails for some reason
1623 */
1624 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001625 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001626 from = pos & (PAGE_CACHE_SIZE - 1);
1627 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628
1629retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001630 handle = ext4_journal_start(inode, needed_blocks);
1631 if (IS_ERR(handle)) {
1632 ret = PTR_ERR(handle);
1633 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001634 }
1635
Jan Karaebd36102009-02-22 21:09:59 -05001636 /* We cannot recurse into the filesystem as the transaction is already
1637 * started */
1638 flags |= AOP_FLAG_NOFS;
1639
Nick Piggin54566b22009-01-04 12:00:53 -08001640 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001641 if (!page) {
1642 ext4_journal_stop(handle);
1643 ret = -ENOMEM;
1644 goto out;
1645 }
1646 *pagep = page;
1647
Jiaying Zhang744692d2010-03-04 16:14:02 -05001648 if (ext4_should_dioread_nolock(inode))
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001649 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001650 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001651 ret = __block_write_begin(page, pos, len, ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001652
1653 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654 ret = walk_page_buffers(handle, page_buffers(page),
1655 from, to, NULL, do_journal_get_write_access);
1656 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001657
1658 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001659 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001660 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001661 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001662 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001663 * outside i_size. Trim these off again. Don't need
1664 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001665 *
1666 * Add inode to orphan list in case we crash before
1667 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001668 */
Jan Karaffacfa72009-07-13 16:22:22 -04001669 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001670 ext4_orphan_add(handle, inode);
1671
1672 ext4_journal_stop(handle);
1673 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001674 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001675 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001676 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001677 * still be on the orphan list; we need to
1678 * make sure the inode is removed from the
1679 * orphan list in that case.
1680 */
1681 if (inode->i_nlink)
1682 ext4_orphan_del(NULL, inode);
1683 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001684 }
1685
Mingming Cao617ba132006-10-11 01:20:53 -07001686 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001688out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689 return ret;
1690}
1691
Nick Pigginbfc1af62007-10-16 01:25:05 -07001692/* For write_end() in data=journal mode */
1693static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694{
1695 if (!buffer_mapped(bh) || buffer_freed(bh))
1696 return 0;
1697 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001698 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001699}
1700
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001701static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001702 struct address_space *mapping,
1703 loff_t pos, unsigned len, unsigned copied,
1704 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001705{
1706 int i_size_changed = 0;
1707 struct inode *inode = mapping->host;
1708 handle_t *handle = ext4_journal_current_handle();
1709
1710 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1711
1712 /*
1713 * No need to use i_size_read() here, the i_size
1714 * cannot change under us because we hold i_mutex.
1715 *
1716 * But it's important to update i_size while still holding page lock:
1717 * page writeout could otherwise come in and zero beyond i_size.
1718 */
1719 if (pos + copied > inode->i_size) {
1720 i_size_write(inode, pos + copied);
1721 i_size_changed = 1;
1722 }
1723
1724 if (pos + copied > EXT4_I(inode)->i_disksize) {
1725 /* We need to mark inode dirty even if
1726 * new_i_size is less that inode->i_size
1727 * bu greater than i_disksize.(hint delalloc)
1728 */
1729 ext4_update_i_disksize(inode, (pos + copied));
1730 i_size_changed = 1;
1731 }
1732 unlock_page(page);
1733 page_cache_release(page);
1734
1735 /*
1736 * Don't mark the inode dirty under page lock. First, it unnecessarily
1737 * makes the holding time of page lock longer. Second, it forces lock
1738 * ordering of page lock and transaction start for journaling
1739 * filesystems.
1740 */
1741 if (i_size_changed)
1742 ext4_mark_inode_dirty(handle, inode);
1743
1744 return copied;
1745}
1746
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747/*
1748 * We need to pick up the new inode size which generic_commit_write gave us
1749 * `file' can be NULL - eg, when called from page_symlink().
1750 *
Mingming Cao617ba132006-10-11 01:20:53 -07001751 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 * buffers are managed internally.
1753 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001754static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001755 struct address_space *mapping,
1756 loff_t pos, unsigned len, unsigned copied,
1757 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758{
Mingming Cao617ba132006-10-11 01:20:53 -07001759 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001760 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001761 int ret = 0, ret2;
1762
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001763 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001764 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765
1766 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001767 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001768 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001769 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001770 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001771 /* if we have allocated more blocks and copied
1772 * less. We will have blocks allocated outside
1773 * inode->i_size. So truncate them
1774 */
1775 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001776 if (ret2 < 0)
1777 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001778 }
Mingming Cao617ba132006-10-11 01:20:53 -07001779 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780 if (!ret)
1781 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001782
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001783 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001784 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001785 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001786 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001787 * on the orphan list; we need to make sure the inode
1788 * is removed from the orphan list in that case.
1789 */
1790 if (inode->i_nlink)
1791 ext4_orphan_del(NULL, inode);
1792 }
1793
1794
Nick Pigginbfc1af62007-10-16 01:25:05 -07001795 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001796}
1797
Nick Pigginbfc1af62007-10-16 01:25:05 -07001798static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001799 struct address_space *mapping,
1800 loff_t pos, unsigned len, unsigned copied,
1801 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001802{
Mingming Cao617ba132006-10-11 01:20:53 -07001803 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001804 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001805 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001807 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001808 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001809 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001810 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001811 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001812 /* if we have allocated more blocks and copied
1813 * less. We will have blocks allocated outside
1814 * inode->i_size. So truncate them
1815 */
1816 ext4_orphan_add(handle, inode);
1817
Roel Kluinf8a87d82008-04-29 22:01:18 -04001818 if (ret2 < 0)
1819 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001820
Mingming Cao617ba132006-10-11 01:20:53 -07001821 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001822 if (!ret)
1823 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001824
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001825 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001826 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001827 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001828 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001829 * on the orphan list; we need to make sure the inode
1830 * is removed from the orphan list in that case.
1831 */
1832 if (inode->i_nlink)
1833 ext4_orphan_del(NULL, inode);
1834 }
1835
Nick Pigginbfc1af62007-10-16 01:25:05 -07001836 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837}
1838
Nick Pigginbfc1af62007-10-16 01:25:05 -07001839static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001840 struct address_space *mapping,
1841 loff_t pos, unsigned len, unsigned copied,
1842 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001843{
Mingming Cao617ba132006-10-11 01:20:53 -07001844 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001845 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001846 int ret = 0, ret2;
1847 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001848 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001849 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001850
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001851 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001852 from = pos & (PAGE_CACHE_SIZE - 1);
1853 to = from + len;
1854
Curt Wohlgemuth2fb522e2011-08-13 11:25:18 -04001855 BUG_ON(!ext4_handle_valid(handle));
1856
Nick Pigginbfc1af62007-10-16 01:25:05 -07001857 if (copied < len) {
1858 if (!PageUptodate(page))
1859 copied = 0;
1860 page_zero_new_buffers(page, from+copied, to);
1861 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862
1863 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001864 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 if (!partial)
1866 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001867 new_i_size = pos + copied;
1868 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001869 i_size_write(inode, pos+copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001870 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001871 if (new_i_size > EXT4_I(inode)->i_disksize) {
1872 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001873 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001874 if (!ret)
1875 ret = ret2;
1876 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001877
Jan Karacf108bc2008-07-11 19:27:31 -04001878 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001879 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001880 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001881 /* if we have allocated more blocks and copied
1882 * less. We will have blocks allocated outside
1883 * inode->i_size. So truncate them
1884 */
1885 ext4_orphan_add(handle, inode);
1886
Mingming Cao617ba132006-10-11 01:20:53 -07001887 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001888 if (!ret)
1889 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001890 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001891 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001892 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001893 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001894 * on the orphan list; we need to make sure the inode
1895 * is removed from the orphan list in that case.
1896 */
1897 if (inode->i_nlink)
1898 ext4_orphan_del(NULL, inode);
1899 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001900
1901 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001902}
Mingming Caod2a17632008-07-14 17:52:37 -04001903
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001904/*
1905 * Reserve a single block located at lblock
1906 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001907static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001908{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001909 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001910 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001911 struct ext4_inode_info *ei = EXT4_I(inode);
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001912 unsigned long md_needed;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001913 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001914
1915 /*
1916 * recalculate the amount of metadata blocks to reserve
1917 * in order to allocate nrblocks
1918 * worse case is one extent per block
1919 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001920repeat:
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001921 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001922 md_needed = ext4_calc_metadata_amount(inode, lblock);
Theodore Ts'of8ec9d62010-01-01 01:00:21 -05001923 trace_ext4_da_reserve_space(inode, md_needed);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001924 spin_unlock(&ei->i_block_reservation_lock);
Mingming Caod2a17632008-07-14 17:52:37 -04001925
Mingming Cao60e58e02009-01-22 18:13:05 +01001926 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001927 * We will charge metadata quota at writeout time; this saves
1928 * us from metadata over-estimation, though we may go over by
1929 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001930 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001931 ret = dquot_reserve_block(inode, 1);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001932 if (ret)
1933 return ret;
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001934 /*
1935 * We do still charge estimated metadata to the sb though;
1936 * we cannot afford to run out of free blocks.
1937 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001938 if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) {
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001939 dquot_release_reservation_block(inode, 1);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001940 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1941 yield();
1942 goto repeat;
1943 }
Mingming Caod2a17632008-07-14 17:52:37 -04001944 return -ENOSPC;
1945 }
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001946 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001947 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001948 ei->i_reserved_meta_blocks += md_needed;
1949 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001950
Mingming Caod2a17632008-07-14 17:52:37 -04001951 return 0; /* success */
1952}
1953
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001954static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001955{
1956 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001957 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001958
Mingming Caocd213222008-08-19 22:16:59 -04001959 if (!to_free)
1960 return; /* Nothing to release, exit */
1961
Mingming Caod2a17632008-07-14 17:52:37 -04001962 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001963
Li Zefan5a58ec82010-05-17 02:00:00 -04001964 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001965 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001966 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001967 * if there aren't enough reserved blocks, then the
1968 * counter is messed up somewhere. Since this
1969 * function is called from invalidate page, it's
1970 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001971 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001972 ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
1973 "ino %lu, to_free %d with only %d reserved "
1974 "data blocks\n", inode->i_ino, to_free,
1975 ei->i_reserved_data_blocks);
1976 WARN_ON(1);
1977 to_free = ei->i_reserved_data_blocks;
1978 }
1979 ei->i_reserved_data_blocks -= to_free;
1980
1981 if (ei->i_reserved_data_blocks == 0) {
1982 /*
1983 * We can release all of the reserved metadata blocks
1984 * only when we have written all of the delayed
1985 * allocation blocks.
1986 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001987 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1988 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001989 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001990 ei->i_da_metadata_calc_len = 0;
Mingming Caocd213222008-08-19 22:16:59 -04001991 }
1992
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001993 /* update fs dirty data blocks counter */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001994 percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001995
Mingming Caod2a17632008-07-14 17:52:37 -04001996 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001997
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001998 dquot_release_reservation_block(inode, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001999}
2000
2001static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002002 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04002003{
2004 int to_release = 0;
2005 struct buffer_head *head, *bh;
2006 unsigned int curr_off = 0;
2007
2008 head = page_buffers(page);
2009 bh = head;
2010 do {
2011 unsigned int next_off = curr_off + bh->b_size;
2012
2013 if ((offset <= curr_off) && (buffer_delay(bh))) {
2014 to_release++;
2015 clear_buffer_delay(bh);
2016 }
2017 curr_off = next_off;
2018 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04002019 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04002020}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021
2022/*
Alex Tomas64769242008-07-11 19:27:31 -04002023 * Delayed allocation stuff
2024 */
2025
Alex Tomas64769242008-07-11 19:27:31 -04002026/*
2027 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002028 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04002029 *
2030 * @mpd->inode: inode
2031 * @mpd->first_page: first page of the extent
2032 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04002033 *
2034 * By the time mpage_da_submit_io() is called we expect all blocks
2035 * to be allocated. this may be wrong if allocation failed.
2036 *
2037 * As pages are already locked by write_cache_pages(), we can't use it
2038 */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002039static int mpage_da_submit_io(struct mpage_da_data *mpd,
2040 struct ext4_map_blocks *map)
Alex Tomas64769242008-07-11 19:27:31 -04002041{
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002042 struct pagevec pvec;
2043 unsigned long index, end;
2044 int ret = 0, err, nr_pages, i;
2045 struct inode *inode = mpd->inode;
2046 struct address_space *mapping = inode->i_mapping;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002047 loff_t size = i_size_read(inode);
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04002048 unsigned int len, block_start;
2049 struct buffer_head *bh, *page_bufs = NULL;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002050 int journal_data = ext4_should_journal_data(inode);
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002051 sector_t pblock = 0, cur_logical = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002052 struct ext4_io_submit io_submit;
Alex Tomas64769242008-07-11 19:27:31 -04002053
2054 BUG_ON(mpd->next_page <= mpd->first_page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002055 memset(&io_submit, 0, sizeof(io_submit));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002056 /*
2057 * We need to start from the first_page to the next_page - 1
2058 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002059 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002060 * at the currently mapped buffer_heads.
2061 */
Alex Tomas64769242008-07-11 19:27:31 -04002062 index = mpd->first_page;
2063 end = mpd->next_page - 1;
2064
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002065 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04002066 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002067 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04002068 if (nr_pages == 0)
2069 break;
2070 for (i = 0; i < nr_pages; i++) {
Theodore Ts'o97498952011-02-26 14:08:01 -05002071 int commit_write = 0, skip_page = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002072 struct page *page = pvec.pages[i];
2073
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002074 index = page->index;
2075 if (index > end)
2076 break;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002077
2078 if (index == size >> PAGE_CACHE_SHIFT)
2079 len = size & ~PAGE_CACHE_MASK;
2080 else
2081 len = PAGE_CACHE_SIZE;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002082 if (map) {
2083 cur_logical = index << (PAGE_CACHE_SHIFT -
2084 inode->i_blkbits);
2085 pblock = map->m_pblk + (cur_logical -
2086 map->m_lblk);
2087 }
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002088 index++;
2089
2090 BUG_ON(!PageLocked(page));
2091 BUG_ON(PageWriteback(page));
2092
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002093 /*
2094 * If the page does not have buffers (for
2095 * whatever reason), try to create them using
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002096 * __block_write_begin. If this fails,
Theodore Ts'o97498952011-02-26 14:08:01 -05002097 * skip the page and move on.
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002098 */
2099 if (!page_has_buffers(page)) {
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002100 if (__block_write_begin(page, 0, len,
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002101 noalloc_get_block_write)) {
Theodore Ts'o97498952011-02-26 14:08:01 -05002102 skip_page:
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002103 unlock_page(page);
2104 continue;
2105 }
2106 commit_write = 1;
2107 }
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04002108
2109 bh = page_bufs = page_buffers(page);
2110 block_start = 0;
2111 do {
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002112 if (!bh)
Theodore Ts'o97498952011-02-26 14:08:01 -05002113 goto skip_page;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002114 if (map && (cur_logical >= map->m_lblk) &&
2115 (cur_logical <= (map->m_lblk +
2116 (map->m_len - 1)))) {
2117 if (buffer_delay(bh)) {
2118 clear_buffer_delay(bh);
2119 bh->b_blocknr = pblock;
2120 }
2121 if (buffer_unwritten(bh) ||
2122 buffer_mapped(bh))
2123 BUG_ON(bh->b_blocknr != pblock);
2124 if (map->m_flags & EXT4_MAP_UNINIT)
2125 set_buffer_uninit(bh);
2126 clear_buffer_unwritten(bh);
2127 }
2128
Theodore Ts'o97498952011-02-26 14:08:01 -05002129 /* skip page if block allocation undone */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002130 if (buffer_delay(bh) || buffer_unwritten(bh))
Theodore Ts'o97498952011-02-26 14:08:01 -05002131 skip_page = 1;
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04002132 bh = bh->b_this_page;
2133 block_start += bh->b_size;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002134 cur_logical++;
2135 pblock++;
2136 } while (bh != page_bufs);
2137
Theodore Ts'o97498952011-02-26 14:08:01 -05002138 if (skip_page)
2139 goto skip_page;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002140
2141 if (commit_write)
2142 /* mark the buffer_heads as dirty & uptodate */
2143 block_commit_write(page, 0, len);
2144
Theodore Ts'o97498952011-02-26 14:08:01 -05002145 clear_page_dirty_for_io(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002146 /*
2147 * Delalloc doesn't support data journalling,
2148 * but eventually maybe we'll lift this
2149 * restriction.
2150 */
2151 if (unlikely(journal_data && PageChecked(page)))
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002152 err = __ext4_journalled_writepage(page, len);
Theodore Ts'o14490322010-12-14 15:27:50 -05002153 else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT))
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002154 err = ext4_bio_write_page(&io_submit, page,
2155 len, mpd->wbc);
Theodore Ts'o45df4b82011-08-13 12:58:21 -04002156 else if (buffer_uninit(page_bufs)) {
2157 ext4_set_bh_endio(page_bufs, inode);
2158 err = block_write_full_page_endio(page,
2159 noalloc_get_block_write,
2160 mpd->wbc, ext4_end_io_buffer_write);
2161 } else
Theodore Ts'o14490322010-12-14 15:27:50 -05002162 err = block_write_full_page(page,
2163 noalloc_get_block_write, mpd->wbc);
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002164
2165 if (!err)
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002166 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04002167 /*
2168 * In error case, we have to continue because
2169 * remaining pages are still locked
Alex Tomas64769242008-07-11 19:27:31 -04002170 */
2171 if (ret == 0)
2172 ret = err;
2173 }
2174 pagevec_release(&pvec);
2175 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002176 ext4_io_submit(&io_submit);
Alex Tomas64769242008-07-11 19:27:31 -04002177 return ret;
2178}
2179
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05002180static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002181{
2182 int nr_pages, i;
2183 pgoff_t index, end;
2184 struct pagevec pvec;
2185 struct inode *inode = mpd->inode;
2186 struct address_space *mapping = inode->i_mapping;
2187
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05002188 index = mpd->first_page;
2189 end = mpd->next_page - 1;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002190 while (index <= end) {
2191 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2192 if (nr_pages == 0)
2193 break;
2194 for (i = 0; i < nr_pages; i++) {
2195 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05002196 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002197 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002198 BUG_ON(!PageLocked(page));
2199 BUG_ON(PageWriteback(page));
2200 block_invalidatepage(page, 0);
2201 ClearPageUptodate(page);
2202 unlock_page(page);
2203 }
Jan Kara9b1d09982010-03-03 16:19:32 -05002204 index = pvec.pages[nr_pages - 1]->index + 1;
2205 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002206 }
2207 return;
2208}
2209
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002210static void ext4_print_free_blocks(struct inode *inode)
2211{
2212 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o16939182009-09-26 17:43:59 -04002213 printk(KERN_CRIT "Total free blocks count %lld\n",
2214 ext4_count_free_blocks(inode->i_sb));
2215 printk(KERN_CRIT "Free/Dirty block details\n");
2216 printk(KERN_CRIT "free_blocks=%lld\n",
2217 (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
2218 printk(KERN_CRIT "dirty_blocks=%lld\n",
2219 (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2220 printk(KERN_CRIT "Block reservation details\n");
2221 printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
2222 EXT4_I(inode)->i_reserved_data_blocks);
2223 printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
2224 EXT4_I(inode)->i_reserved_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002225 return;
2226}
2227
Theodore Ts'ob920c752009-05-14 00:54:29 -04002228/*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002229 * mpage_da_map_and_submit - go through given space, map them
2230 * if necessary, and then submit them for I/O
Alex Tomas64769242008-07-11 19:27:31 -04002231 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002232 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002233 *
2234 * The function skips space we know is already mapped to disk blocks.
2235 *
Alex Tomas64769242008-07-11 19:27:31 -04002236 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002237static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002238{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002239 int err, blks, get_blocks_flags;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002240 struct ext4_map_blocks map, *mapp = NULL;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002241 sector_t next = mpd->b_blocknr;
2242 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2243 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2244 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002245
2246 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002247 * If the blocks are mapped already, or we couldn't accumulate
2248 * any blocks, then proceed immediately to the submission stage.
Alex Tomas64769242008-07-11 19:27:31 -04002249 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002250 if ((mpd->b_size == 0) ||
2251 ((mpd->b_state & (1 << BH_Mapped)) &&
2252 !(mpd->b_state & (1 << BH_Delay)) &&
2253 !(mpd->b_state & (1 << BH_Unwritten))))
2254 goto submit_io;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002255
2256 handle = ext4_journal_current_handle();
2257 BUG_ON(!handle);
2258
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002259 /*
Eric Sandeen79e83032010-07-27 11:56:07 -04002260 * Call ext4_map_blocks() to allocate any delayed allocation
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002261 * blocks, or to convert an uninitialized extent to be
2262 * initialized (in the case where we have written into
2263 * one or more preallocated blocks).
2264 *
2265 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2266 * indicate that we are on the delayed allocation path. This
2267 * affects functions in many different parts of the allocation
2268 * call path. This flag exists primarily because we don't
Eric Sandeen79e83032010-07-27 11:56:07 -04002269 * want to change *many* call functions, so ext4_map_blocks()
Theodore Ts'of2321092011-01-10 12:12:36 -05002270 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002271 * inode's allocation semaphore is taken.
2272 *
2273 * If the blocks in questions were delalloc blocks, set
2274 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2275 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002276 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002277 map.m_lblk = next;
2278 map.m_len = max_blocks;
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05002279 get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002280 if (ext4_should_dioread_nolock(mpd->inode))
2281 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002282 if (mpd->b_state & (1 << BH_Delay))
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05002283 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2284
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002285 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002286 if (blks < 0) {
Eric Sandeene3570632010-07-27 11:56:08 -04002287 struct super_block *sb = mpd->inode->i_sb;
2288
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002289 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002290 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002291 * If get block returns EAGAIN or ENOSPC and there
Theodore Ts'o97498952011-02-26 14:08:01 -05002292 * appears to be free blocks we will just let
2293 * mpage_da_submit_io() unlock all of the pages.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002294 */
2295 if (err == -EAGAIN)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002296 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002297
2298 if (err == -ENOSPC &&
Eric Sandeene3570632010-07-27 11:56:08 -04002299 ext4_count_free_blocks(sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002300 mpd->retval = err;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002301 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002302 }
2303
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002304 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002305 * get block failure will cause us to loop in
2306 * writepages, because a_ops->writepage won't be able
2307 * to make progress. The page will be redirtied by
2308 * writepage and writepages will again try to write
2309 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002310 */
Eric Sandeene3570632010-07-27 11:56:08 -04002311 if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2312 ext4_msg(sb, KERN_CRIT,
2313 "delayed block allocation failed for inode %lu "
2314 "at logical offset %llu with max blocks %zd "
2315 "with error %d", mpd->inode->i_ino,
2316 (unsigned long long) next,
2317 mpd->b_size >> mpd->inode->i_blkbits, err);
2318 ext4_msg(sb, KERN_CRIT,
2319 "This should not happen!! Data will be lost\n");
2320 if (err == -ENOSPC)
2321 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002322 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002323 /* invalidate all the pages */
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05002324 ext4_da_block_invalidatepages(mpd);
Curt Wohlgemuthe0fd9b92011-02-26 12:25:52 -05002325
2326 /* Mark this page range as having been completed */
2327 mpd->io_done = 1;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002328 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002329 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002330 BUG_ON(blks == 0);
2331
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002332 mapp = &map;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002333 if (map.m_flags & EXT4_MAP_NEW) {
2334 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
2335 int i;
Alex Tomas64769242008-07-11 19:27:31 -04002336
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002337 for (i = 0; i < map.m_len; i++)
2338 unmap_underlying_metadata(bdev, map.m_pblk + i);
2339 }
Alex Tomas64769242008-07-11 19:27:31 -04002340
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002341 if (ext4_should_order_data(mpd->inode)) {
2342 err = ext4_jbd2_file_inode(handle, mpd->inode);
2343 if (err)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002344 /* This only happens if the journal is aborted */
2345 return;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002346 }
2347
2348 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002349 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002350 */
2351 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2352 if (disksize > i_size_read(mpd->inode))
2353 disksize = i_size_read(mpd->inode);
2354 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2355 ext4_update_i_disksize(mpd->inode, disksize);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002356 err = ext4_mark_inode_dirty(handle, mpd->inode);
2357 if (err)
2358 ext4_error(mpd->inode->i_sb,
2359 "Failed to mark inode %lu dirty",
2360 mpd->inode->i_ino);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002361 }
2362
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002363submit_io:
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04002364 mpage_da_submit_io(mpd, mapp);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002365 mpd->io_done = 1;
Alex Tomas64769242008-07-11 19:27:31 -04002366}
2367
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002368#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2369 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002370
2371/*
2372 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2373 *
2374 * @mpd->lbh - extent of blocks
2375 * @logical - logical number of the block in the file
2376 * @bh - bh of the block (used to access block's state)
2377 *
2378 * the function is used to collect contig. blocks in same state
2379 */
2380static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002381 sector_t logical, size_t b_size,
2382 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002383{
Alex Tomas64769242008-07-11 19:27:31 -04002384 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002385 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002386
Eric Sandeenc445e3e2010-05-16 04:00:00 -04002387 /*
2388 * XXX Don't go larger than mballoc is willing to allocate
2389 * This is a stopgap solution. We eventually need to fold
2390 * mpage_da_submit_io() into this function and then call
Eric Sandeen79e83032010-07-27 11:56:07 -04002391 * ext4_map_blocks() multiple times in a loop
Eric Sandeenc445e3e2010-05-16 04:00:00 -04002392 */
2393 if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
2394 goto flush_it;
2395
Mingming Cao525f4ed2008-08-19 22:15:58 -04002396 /* check if thereserved journal credits might overflow */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002397 if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04002398 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2399 /*
2400 * With non-extent format we are limited by the journal
2401 * credit available. Total credit needed to insert
2402 * nrblocks contiguous blocks is dependent on the
2403 * nrblocks. So limit nrblocks.
2404 */
2405 goto flush_it;
2406 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2407 EXT4_MAX_TRANS_DATA) {
2408 /*
2409 * Adding the new buffer_head would make it cross the
2410 * allowed limit for which we have journal credit
2411 * reserved. So limit the new bh->b_size
2412 */
2413 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2414 mpd->inode->i_blkbits;
2415 /* we will do mpage_da_submit_io in the next loop */
2416 }
2417 }
Alex Tomas64769242008-07-11 19:27:31 -04002418 /*
2419 * First block in the extent
2420 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002421 if (mpd->b_size == 0) {
2422 mpd->b_blocknr = logical;
2423 mpd->b_size = b_size;
2424 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002425 return;
2426 }
2427
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002428 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002429 /*
2430 * Can we merge the block to our big extent?
2431 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002432 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2433 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002434 return;
2435 }
2436
Mingming Cao525f4ed2008-08-19 22:15:58 -04002437flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002438 /*
2439 * We couldn't merge the block to our extent, so we
2440 * need to flush current extent and start new one
2441 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002442 mpage_da_map_and_submit(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002443 return;
Alex Tomas64769242008-07-11 19:27:31 -04002444}
2445
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002446static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002447{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002448 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002449}
2450
Alex Tomas64769242008-07-11 19:27:31 -04002451/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002452 * This is a special get_blocks_t callback which is used by
2453 * ext4_da_write_begin(). It will either return mapped block or
2454 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002455 *
2456 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2457 * We also have b_blocknr = -1 and b_bdev initialized properly
2458 *
2459 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2460 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2461 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002462 */
2463static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002464 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04002465{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002466 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04002467 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002468 sector_t invalid_block = ~((sector_t) 0xffff);
2469
2470 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2471 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002472
2473 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002474 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
2475
2476 map.m_lblk = iblock;
2477 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04002478
2479 /*
2480 * first, we need to know whether the block is allocated already
2481 * preallocated blocks are unmapped but should treated
2482 * the same as allocated blocks.
2483 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002484 ret = ext4_map_blocks(NULL, inode, &map, 0);
2485 if (ret < 0)
2486 return ret;
2487 if (ret == 0) {
2488 if (buffer_delay(bh))
2489 return 0; /* Not sure this could or should happen */
Alex Tomas64769242008-07-11 19:27:31 -04002490 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02002491 * XXX: __block_write_begin() unmaps passed block, is it OK?
Alex Tomas64769242008-07-11 19:27:31 -04002492 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05002493 ret = ext4_da_reserve_space(inode, iblock);
Mingming Caod2a17632008-07-14 17:52:37 -04002494 if (ret)
2495 /* not enough space to reserve */
2496 return ret;
2497
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002498 map_bh(bh, inode->i_sb, invalid_block);
2499 set_buffer_new(bh);
2500 set_buffer_delay(bh);
2501 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002502 }
2503
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002504 map_bh(bh, inode->i_sb, map.m_pblk);
2505 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
2506
2507 if (buffer_unwritten(bh)) {
2508 /* A delayed write to unwritten bh should be marked
2509 * new and mapped. Mapped ensures that we don't do
2510 * get_block multiple times when we write to the same
2511 * offset and new ensures that we do proper zero out
2512 * for partial write.
2513 */
2514 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04002515 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002516 }
2517 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002518}
Mingming Cao61628a32008-07-11 19:27:31 -04002519
Theodore Ts'ob920c752009-05-14 00:54:29 -04002520/*
2521 * This function is used as a standard get_block_t calback function
2522 * when there is no desire to allocate any blocks. It is used as a
Christoph Hellwigebdec242010-10-06 10:47:23 +02002523 * callback function for block_write_begin() and block_write_full_page().
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04002524 * These functions should only try to map a single block at a time.
Theodore Ts'ob920c752009-05-14 00:54:29 -04002525 *
2526 * Since this function doesn't do block allocations even if the caller
2527 * requests it by passing in create=1, it is critically important that
2528 * any caller checks to make sure that any buffer heads are returned
2529 * by this function are either all already mapped or marked for
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04002530 * delayed allocation before calling block_write_full_page(). Otherwise,
2531 * b_blocknr could be left unitialized, and the page write functions will
2532 * be taken by surprise.
Theodore Ts'ob920c752009-05-14 00:54:29 -04002533 */
2534static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002535 struct buffer_head *bh_result, int create)
2536{
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002537 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002538 return _ext4_get_block(inode, iblock, bh_result, 0);
Mingming Cao61628a32008-07-11 19:27:31 -04002539}
2540
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002541static int bget_one(handle_t *handle, struct buffer_head *bh)
2542{
2543 get_bh(bh);
2544 return 0;
2545}
2546
2547static int bput_one(handle_t *handle, struct buffer_head *bh)
2548{
2549 put_bh(bh);
2550 return 0;
2551}
2552
2553static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002554 unsigned int len)
2555{
2556 struct address_space *mapping = page->mapping;
2557 struct inode *inode = mapping->host;
2558 struct buffer_head *page_bufs;
2559 handle_t *handle = NULL;
2560 int ret = 0;
2561 int err;
2562
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002563 ClearPageChecked(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002564 page_bufs = page_buffers(page);
2565 BUG_ON(!page_bufs);
2566 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2567 /* As soon as we unlock the page, it can go away, but we have
2568 * references to buffers so we are safe */
2569 unlock_page(page);
2570
2571 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2572 if (IS_ERR(handle)) {
2573 ret = PTR_ERR(handle);
2574 goto out;
2575 }
2576
Curt Wohlgemuth2fb522e2011-08-13 11:25:18 -04002577 BUG_ON(!ext4_handle_valid(handle));
2578
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002579 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2580 do_journal_get_write_access);
2581
2582 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2583 write_end_fn);
2584 if (ret == 0)
2585 ret = err;
2586 err = ext4_journal_stop(handle);
2587 if (!ret)
2588 ret = err;
2589
2590 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002591 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002592out:
2593 return ret;
2594}
2595
Jiaying Zhang744692d2010-03-04 16:14:02 -05002596static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
2597static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
2598
Mingming Cao61628a32008-07-11 19:27:31 -04002599/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002600 * Note that we don't need to start a transaction unless we're journaling data
2601 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2602 * need to file the inode to the transaction's list in ordered mode because if
2603 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002604 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002605 * transaction the data will hit the disk. In case we are journaling data, we
2606 * cannot start transaction directly because transaction start ranks above page
2607 * lock so we have to do some magic.
2608 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002609 * This function can get called via...
2610 * - ext4_da_writepages after taking page lock (have journal handle)
2611 * - journal_submit_inode_data_buffers (no journal handle)
2612 * - shrink_page_list via pdflush (no journal handle)
2613 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002614 *
2615 * We don't do any block allocation in this function. If we have page with
2616 * multiple blocks we need to write those buffer_heads that are mapped. This
2617 * is important for mmaped based write. So if we do with blocksize 1K
2618 * truncate(f, 1024);
2619 * a = mmap(f, 0, 4096);
2620 * a[0] = 'a';
2621 * truncate(f, 4096);
2622 * we have in the page first buffer_head mapped via page_mkwrite call back
2623 * but other bufer_heads would be unmapped but dirty(dirty done via the
2624 * do_wp_page). So writepage should write the first block. If we modify
2625 * the mmap area beyond 1024 we will again get a page_fault and the
2626 * page_mkwrite callback will do the block allocation and mark the
2627 * buffer_heads mapped.
2628 *
2629 * We redirty the page if we have any buffer_heads that is either delay or
2630 * unwritten in the page.
2631 *
2632 * We can get recursively called as show below.
2633 *
2634 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2635 * ext4_writepage()
2636 *
2637 * But since we don't do any block allocation we should not deadlock.
2638 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002639 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002640static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002641 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002642{
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002643 int ret = 0, commit_write = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002644 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002645 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002646 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04002647 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002648
Lukas Czernera9c667f2011-06-06 09:51:52 -04002649 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002650 size = i_size_read(inode);
2651 if (page->index == size >> PAGE_CACHE_SHIFT)
2652 len = size & ~PAGE_CACHE_MASK;
2653 else
2654 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002655
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002656 /*
2657 * If the page does not have buffers (for whatever reason),
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002658 * try to create them using __block_write_begin. If this
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002659 * fails, redirty the page and move on.
2660 */
Theodore Ts'ob1142e82010-10-28 17:33:57 -04002661 if (!page_has_buffers(page)) {
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002662 if (__block_write_begin(page, 0, len,
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002663 noalloc_get_block_write)) {
2664 redirty_page:
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002665 redirty_page_for_writepage(wbc, page);
2666 unlock_page(page);
2667 return 0;
2668 }
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002669 commit_write = 1;
2670 }
2671 page_bufs = page_buffers(page);
2672 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2673 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002674 /*
Theodore Ts'ob1142e82010-10-28 17:33:57 -04002675 * We don't want to do block allocation, so redirty
2676 * the page and return. We may reach here when we do
2677 * a journal commit via journal_submit_inode_data_buffers.
2678 * We can also reach here via shrink_page_list
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002679 */
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002680 goto redirty_page;
2681 }
2682 if (commit_write)
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002683 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002684 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002685
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002686 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002687 /*
2688 * It's mmapped pagecache. Add buffers and journal it. There
2689 * doesn't seem much point in redirtying the page here.
2690 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002691 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002692
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002693 if (buffer_uninit(page_bufs)) {
Jiaying Zhang744692d2010-03-04 16:14:02 -05002694 ext4_set_bh_endio(page_bufs, inode);
2695 ret = block_write_full_page_endio(page, noalloc_get_block_write,
2696 wbc, ext4_end_io_buffer_write);
2697 } else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002698 ret = block_write_full_page(page, noalloc_get_block_write,
2699 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002700
Alex Tomas64769242008-07-11 19:27:31 -04002701 return ret;
2702}
2703
Mingming Cao61628a32008-07-11 19:27:31 -04002704/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002705 * This is called via ext4_da_writepages() to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002706 * calculate the total number of credits to reserve to fit
Mingming Cao525f4ed2008-08-19 22:15:58 -04002707 * a single extent allocation into a single transaction,
2708 * ext4_da_writpeages() will loop calling this before
2709 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002710 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002711
2712static int ext4_da_writepages_trans_blocks(struct inode *inode)
2713{
2714 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2715
2716 /*
2717 * With non-extent format the journal credit needed to
2718 * insert nrblocks contiguous block is dependent on
2719 * number of contiguous block. So we will limit
2720 * number of contiguous block to a sane value
2721 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002722 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
Mingming Cao525f4ed2008-08-19 22:15:58 -04002723 (max_blocks > EXT4_MAX_TRANS_DATA))
2724 max_blocks = EXT4_MAX_TRANS_DATA;
2725
2726 return ext4_chunk_trans_blocks(inode, max_blocks);
2727}
Mingming Cao61628a32008-07-11 19:27:31 -04002728
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002729/*
2730 * write_cache_pages_da - walk the list of dirty pages of the given
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002731 * address space and accumulate pages that need writing, and call
Theodore Ts'o168fc022011-02-26 14:09:20 -05002732 * mpage_da_map_and_submit to map a single contiguous memory region
2733 * and then write them.
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002734 */
2735static int write_cache_pages_da(struct address_space *mapping,
2736 struct writeback_control *wbc,
Eric Sandeen72f84e62010-10-27 21:30:13 -04002737 struct mpage_da_data *mpd,
2738 pgoff_t *done_index)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002739{
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002740 struct buffer_head *bh, *head;
Theodore Ts'o168fc022011-02-26 14:09:20 -05002741 struct inode *inode = mapping->host;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002742 struct pagevec pvec;
2743 unsigned int nr_pages;
2744 sector_t logical;
2745 pgoff_t index, end;
2746 long nr_to_write = wbc->nr_to_write;
2747 int i, tag, ret = 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002748
Theodore Ts'o168fc022011-02-26 14:09:20 -05002749 memset(mpd, 0, sizeof(struct mpage_da_data));
2750 mpd->wbc = wbc;
2751 mpd->inode = inode;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002752 pagevec_init(&pvec, 0);
2753 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2754 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2755
Wu Fengguangac693062010-06-06 10:38:15 -06002756 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002757 tag = PAGECACHE_TAG_TOWRITE;
2758 else
2759 tag = PAGECACHE_TAG_DIRTY;
2760
Eric Sandeen72f84e62010-10-27 21:30:13 -04002761 *done_index = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002762 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002763 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002764 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2765 if (nr_pages == 0)
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002766 return 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002767
2768 for (i = 0; i < nr_pages; i++) {
2769 struct page *page = pvec.pages[i];
2770
2771 /*
2772 * At this point, the page may be truncated or
2773 * invalidated (changing page->mapping to NULL), or
2774 * even swizzled back from swapper_space to tmpfs file
2775 * mapping. However, page->index will not change
2776 * because we have a reference on the page.
2777 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002778 if (page->index > end)
2779 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002780
Eric Sandeen72f84e62010-10-27 21:30:13 -04002781 *done_index = page->index + 1;
2782
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002783 /*
2784 * If we can't merge this page, and we have
2785 * accumulated an contiguous region, write it
2786 */
2787 if ((mpd->next_page != page->index) &&
2788 (mpd->next_page != mpd->first_page)) {
2789 mpage_da_map_and_submit(mpd);
2790 goto ret_extent_tail;
2791 }
2792
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002793 lock_page(page);
2794
2795 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002796 * If the page is no longer dirty, or its
2797 * mapping no longer corresponds to inode we
2798 * are writing (which means it has been
2799 * truncated or invalidated), or the page is
2800 * already under writeback and we are not
2801 * doing a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002802 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002803 if (!PageDirty(page) ||
2804 (PageWriteback(page) &&
2805 (wbc->sync_mode == WB_SYNC_NONE)) ||
2806 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002807 unlock_page(page);
2808 continue;
2809 }
2810
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002811 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002812 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002813
Theodore Ts'o168fc022011-02-26 14:09:20 -05002814 if (mpd->next_page != page->index)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002815 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002816 mpd->next_page = page->index + 1;
2817 logical = (sector_t) page->index <<
2818 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2819
2820 if (!page_has_buffers(page)) {
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002821 mpage_add_bh_to_extent(mpd, logical,
2822 PAGE_CACHE_SIZE,
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002823 (1 << BH_Dirty) | (1 << BH_Uptodate));
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002824 if (mpd->io_done)
2825 goto ret_extent_tail;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002826 } else {
2827 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002828 * Page with regular buffer heads,
2829 * just add all dirty ones
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002830 */
2831 head = page_buffers(page);
2832 bh = head;
2833 do {
2834 BUG_ON(buffer_locked(bh));
2835 /*
2836 * We need to try to allocate
2837 * unmapped blocks in the same page.
2838 * Otherwise we won't make progress
2839 * with the page in ext4_writepage
2840 */
2841 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
2842 mpage_add_bh_to_extent(mpd, logical,
2843 bh->b_size,
2844 bh->b_state);
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002845 if (mpd->io_done)
2846 goto ret_extent_tail;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002847 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2848 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002849 * mapped dirty buffer. We need
2850 * to update the b_state
2851 * because we look at b_state
2852 * in mpage_da_map_blocks. We
2853 * don't update b_size because
2854 * if we find an unmapped
2855 * buffer_head later we need to
2856 * use the b_state flag of that
2857 * buffer_head.
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002858 */
2859 if (mpd->b_size == 0)
2860 mpd->b_state = bh->b_state & BH_FLAGS;
2861 }
2862 logical++;
2863 } while ((bh = bh->b_this_page) != head);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002864 }
2865
2866 if (nr_to_write > 0) {
2867 nr_to_write--;
2868 if (nr_to_write == 0 &&
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002869 wbc->sync_mode == WB_SYNC_NONE)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002870 /*
2871 * We stop writing back only if we are
2872 * not doing integrity sync. In case of
2873 * integrity sync we have to keep going
2874 * because someone may be concurrently
2875 * dirtying pages, and we might have
2876 * synced a lot of newly appeared dirty
2877 * pages, but have not synced all of the
2878 * old dirty pages.
2879 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002880 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002881 }
2882 }
2883 pagevec_release(&pvec);
2884 cond_resched();
2885 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002886 return 0;
2887ret_extent_tail:
2888 ret = MPAGE_DA_EXTENT_TAIL;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002889out:
2890 pagevec_release(&pvec);
2891 cond_resched();
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002892 return ret;
2893}
2894
2895
Alex Tomas64769242008-07-11 19:27:31 -04002896static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002897 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002898{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002899 pgoff_t index;
2900 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002901 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002902 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002903 struct inode *inode = mapping->host;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002904 int pages_written = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002905 unsigned int max_pages;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002906 int range_cyclic, cycled = 1, io_done = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002907 int needed_blocks, ret = 0;
2908 long desired_nr_to_write, nr_to_writebump = 0;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002909 loff_t range_start = wbc->range_start;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002910 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Eric Sandeen72f84e62010-10-27 21:30:13 -04002911 pgoff_t done_index = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002912 pgoff_t end;
Mingming Cao61628a32008-07-11 19:27:31 -04002913
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002914 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002915
Mingming Cao61628a32008-07-11 19:27:31 -04002916 /*
2917 * No pages to write? This is mainly a kludge to avoid starting
2918 * a transaction for special inodes like journal inode on last iput()
2919 * because that could violate lock ordering on umount
2920 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002921 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002922 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002923
2924 /*
2925 * If the filesystem has aborted, it is read-only, so return
2926 * right away instead of dumping stack traces later on that
2927 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002928 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002929 * the latter could be true if the filesystem is mounted
2930 * read-only, and in that case, ext4_da_writepages should
2931 * *never* be called, so if that ever happens, we would want
2932 * the stack trace.
2933 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002934 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002935 return -EROFS;
2936
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002937 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2938 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002939
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002940 range_cyclic = wbc->range_cyclic;
2941 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002942 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002943 if (index)
2944 cycled = 0;
2945 wbc->range_start = index << PAGE_CACHE_SHIFT;
2946 wbc->range_end = LLONG_MAX;
2947 wbc->range_cyclic = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002948 end = -1;
2949 } else {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002950 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002951 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2952 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002953
Theodore Ts'o55138e02009-09-29 13:31:31 -04002954 /*
2955 * This works around two forms of stupidity. The first is in
2956 * the writeback code, which caps the maximum number of pages
2957 * written to be 1024 pages. This is wrong on multiple
2958 * levels; different architectues have a different page size,
2959 * which changes the maximum amount of data which gets
2960 * written. Secondly, 4 megabytes is way too small. XFS
2961 * forces this value to be 16 megabytes by multiplying
2962 * nr_to_write parameter by four, and then relies on its
2963 * allocator to allocate larger extents to make them
2964 * contiguous. Unfortunately this brings us to the second
2965 * stupidity, which is that ext4's mballoc code only allocates
2966 * at most 2048 blocks. So we force contiguous writes up to
2967 * the number of dirty blocks in the inode, or
2968 * sbi->max_writeback_mb_bump whichever is smaller.
2969 */
2970 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
Eric Sandeenb443e732010-10-27 21:30:03 -04002971 if (!range_cyclic && range_whole) {
2972 if (wbc->nr_to_write == LONG_MAX)
2973 desired_nr_to_write = wbc->nr_to_write;
2974 else
2975 desired_nr_to_write = wbc->nr_to_write * 8;
2976 } else
Theodore Ts'o55138e02009-09-29 13:31:31 -04002977 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2978 max_pages);
2979 if (desired_nr_to_write > max_pages)
2980 desired_nr_to_write = max_pages;
2981
2982 if (wbc->nr_to_write < desired_nr_to_write) {
2983 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2984 wbc->nr_to_write = desired_nr_to_write;
2985 }
2986
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002987retry:
Wu Fengguangac693062010-06-06 10:38:15 -06002988 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002989 tag_pages_for_writeback(mapping, index, end);
2990
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002991 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002992
2993 /*
2994 * we insert one extent at a time. So we need
2995 * credit needed for single extent allocation.
2996 * journalled mode is currently not supported
2997 * by delalloc
2998 */
2999 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04003000 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04003001
Mingming Cao61628a32008-07-11 19:27:31 -04003002 /* start a new transaction*/
3003 handle = ext4_journal_start(inode, needed_blocks);
3004 if (IS_ERR(handle)) {
3005 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04003006 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04003007 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04003008 wbc->nr_to_write, inode->i_ino, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04003009 goto out_writepages;
3010 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05003011
3012 /*
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05003013 * Now call write_cache_pages_da() to find the next
Theodore Ts'of63e6002009-02-23 16:42:39 -05003014 * contiguous region of logical blocks that need
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05003015 * blocks to be allocated by ext4 and submit them.
Theodore Ts'of63e6002009-02-23 16:42:39 -05003016 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04003017 ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index);
Theodore Ts'of63e6002009-02-23 16:42:39 -05003018 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003019 * If we have a contiguous extent of pages and we
Theodore Ts'of63e6002009-02-23 16:42:39 -05003020 * haven't done the I/O yet, map the blocks and submit
3021 * them for I/O.
3022 */
3023 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04003024 mpage_da_map_and_submit(&mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05003025 ret = MPAGE_DA_EXTENT_TAIL;
3026 }
Theodore Ts'ob3a3ca82009-08-31 23:13:11 -04003027 trace_ext4_da_write_pages(inode, &mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05003028 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04003029
Mingming Cao61628a32008-07-11 19:27:31 -04003030 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04003031
Eric Sandeen8f64b322009-02-26 00:57:35 -05003032 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003033 /* commit the transaction which would
3034 * free blocks released in the transaction
3035 * and try again
3036 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04003037 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003038 ret = 0;
3039 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04003040 /*
3041 * got one extent now try with
3042 * rest of the pages
3043 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003044 pages_written += mpd.pages_written;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04003045 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05003046 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003047 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04003048 /*
3049 * There is no more writeout needed
3050 * or we requested for a noblocking writeout
3051 * and we found the device congested
3052 */
Mingming Cao61628a32008-07-11 19:27:31 -04003053 break;
Mingming Cao61628a32008-07-11 19:27:31 -04003054 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05003055 if (!io_done && !cycled) {
3056 cycled = 1;
3057 index = 0;
3058 wbc->range_start = index << PAGE_CACHE_SHIFT;
3059 wbc->range_end = mapping->writeback_index - 1;
3060 goto retry;
3061 }
Mingming Cao61628a32008-07-11 19:27:31 -04003062
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003063 /* Update index */
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05003064 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04003065 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3066 /*
3067 * set the writeback_index so that range_cyclic
3068 * mode will write it back later
3069 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04003070 mapping->writeback_index = done_index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04003071
Mingming Cao61628a32008-07-11 19:27:31 -04003072out_writepages:
Richard Kennedy2faf2e12009-12-25 15:46:07 -05003073 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'ode89de62009-08-31 17:00:59 -04003074 wbc->range_start = range_start;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003075 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04003076 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003077}
3078
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003079#define FALL_BACK_TO_NONDELALLOC 1
3080static int ext4_nonda_switch(struct super_block *sb)
3081{
3082 s64 free_blocks, dirty_blocks;
3083 struct ext4_sb_info *sbi = EXT4_SB(sb);
3084
3085 /*
3086 * switch to non delalloc mode if we are running low
3087 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08003088 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003089 * accumulated on each CPU without updating global counters
3090 * Delalloc need an accurate free block accounting. So switch
3091 * to non delalloc when we are near to error range.
3092 */
3093 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3094 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
3095 if (2 * free_blocks < 3 * dirty_blocks ||
3096 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
3097 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05003098 * free block count is less than 150% of dirty blocks
3099 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003100 */
3101 return 1;
3102 }
Eric Sandeenc8afb442009-12-23 07:58:12 -05003103 /*
3104 * Even if we don't switch but are nearing capacity,
3105 * start pushing delalloc when 1/2 of free blocks are dirty.
3106 */
3107 if (free_blocks < 2 * dirty_blocks)
3108 writeback_inodes_sb_if_idle(sb);
3109
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003110 return 0;
3111}
3112
Alex Tomas64769242008-07-11 19:27:31 -04003113static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003114 loff_t pos, unsigned len, unsigned flags,
3115 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003116{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04003117 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04003118 struct page *page;
3119 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04003120 struct inode *inode = mapping->host;
3121 handle_t *handle;
3122
3123 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003124
3125 if (ext4_nonda_switch(inode->i_sb)) {
3126 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
3127 return ext4_write_begin(file, mapping, pos,
3128 len, flags, pagep, fsdata);
3129 }
3130 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003131 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04003132retry:
Alex Tomas64769242008-07-11 19:27:31 -04003133 /*
3134 * With delayed allocation, we don't log the i_disksize update
3135 * if there is delayed block allocation. But we still need
3136 * to journalling the i_disksize update if writes to the end
3137 * of file which has an already mapped buffer.
3138 */
3139 handle = ext4_journal_start(inode, 1);
3140 if (IS_ERR(handle)) {
3141 ret = PTR_ERR(handle);
3142 goto out;
3143 }
Jan Karaebd36102009-02-22 21:09:59 -05003144 /* We cannot recurse into the filesystem as the transaction is already
3145 * started */
3146 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04003147
Nick Piggin54566b22009-01-04 12:00:53 -08003148 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003149 if (!page) {
3150 ext4_journal_stop(handle);
3151 ret = -ENOMEM;
3152 goto out;
3153 }
Alex Tomas64769242008-07-11 19:27:31 -04003154 *pagep = page;
3155
Christoph Hellwig6e1db882010-06-04 11:29:57 +02003156 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04003157 if (ret < 0) {
3158 unlock_page(page);
3159 ext4_journal_stop(handle);
3160 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003161 /*
3162 * block_write_begin may have instantiated a few blocks
3163 * outside i_size. Trim these off again. Don't need
3164 * i_size_read because we hold i_mutex.
3165 */
3166 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003167 ext4_truncate_failed_write(inode);
Alex Tomas64769242008-07-11 19:27:31 -04003168 }
3169
Mingming Caod2a17632008-07-14 17:52:37 -04003170 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3171 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003172out:
3173 return ret;
3174}
3175
Mingming Cao632eaea2008-07-11 19:27:31 -04003176/*
3177 * Check if we should update i_disksize
3178 * when write to the end of file but not require block allocation
3179 */
3180static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003181 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003182{
3183 struct buffer_head *bh;
3184 struct inode *inode = page->mapping->host;
3185 unsigned int idx;
3186 int i;
3187
3188 bh = page_buffers(page);
3189 idx = offset >> inode->i_blkbits;
3190
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003191 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003192 bh = bh->b_this_page;
3193
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003194 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003195 return 0;
3196 return 1;
3197}
3198
Alex Tomas64769242008-07-11 19:27:31 -04003199static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003200 struct address_space *mapping,
3201 loff_t pos, unsigned len, unsigned copied,
3202 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003203{
3204 struct inode *inode = mapping->host;
3205 int ret = 0, ret2;
3206 handle_t *handle = ext4_journal_current_handle();
3207 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003208 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003209 int write_mode = (int)(unsigned long)fsdata;
3210
3211 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3212 if (ext4_should_order_data(inode)) {
3213 return ext4_ordered_write_end(file, mapping, pos,
3214 len, copied, page, fsdata);
3215 } else if (ext4_should_writeback_data(inode)) {
3216 return ext4_writeback_write_end(file, mapping, pos,
3217 len, copied, page, fsdata);
3218 } else {
3219 BUG();
3220 }
3221 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003222
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003223 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003224 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003225 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003226
3227 /*
3228 * generic_write_end() will run mark_inode_dirty() if i_size
3229 * changes. So let's piggyback the i_disksize mark_inode_dirty
3230 * into that.
3231 */
3232
3233 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003234 if (new_i_size > EXT4_I(inode)->i_disksize) {
3235 if (ext4_da_should_update_i_disksize(page, end)) {
3236 down_write(&EXT4_I(inode)->i_data_sem);
3237 if (new_i_size > EXT4_I(inode)->i_disksize) {
3238 /*
3239 * Updating i_disksize when extending file
3240 * without needing block allocation
3241 */
3242 if (ext4_should_order_data(inode))
3243 ret = ext4_jbd2_file_inode(handle,
3244 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003245
Mingming Cao632eaea2008-07-11 19:27:31 -04003246 EXT4_I(inode)->i_disksize = new_i_size;
3247 }
3248 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003249 /* We need to mark inode dirty even if
3250 * new_i_size is less that inode->i_size
3251 * bu greater than i_disksize.(hint delalloc)
3252 */
3253 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003254 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003255 }
Alex Tomas64769242008-07-11 19:27:31 -04003256 ret2 = generic_write_end(file, mapping, pos, len, copied,
3257 page, fsdata);
3258 copied = ret2;
3259 if (ret2 < 0)
3260 ret = ret2;
3261 ret2 = ext4_journal_stop(handle);
3262 if (!ret)
3263 ret = ret2;
3264
3265 return ret ? ret : copied;
3266}
3267
3268static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3269{
Alex Tomas64769242008-07-11 19:27:31 -04003270 /*
3271 * Drop reserved blocks
3272 */
3273 BUG_ON(!PageLocked(page));
3274 if (!page_has_buffers(page))
3275 goto out;
3276
Mingming Caod2a17632008-07-14 17:52:37 -04003277 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003278
3279out:
3280 ext4_invalidatepage(page, offset);
3281
3282 return;
3283}
3284
Theodore Ts'occd25062009-02-26 01:04:07 -05003285/*
3286 * Force all delayed allocation blocks to be allocated for a given inode.
3287 */
3288int ext4_alloc_da_blocks(struct inode *inode)
3289{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003290 trace_ext4_alloc_da_blocks(inode);
3291
Theodore Ts'occd25062009-02-26 01:04:07 -05003292 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3293 !EXT4_I(inode)->i_reserved_meta_blocks)
3294 return 0;
3295
3296 /*
3297 * We do something simple for now. The filemap_flush() will
3298 * also start triggering a write of the data blocks, which is
3299 * not strictly speaking necessary (and for users of
3300 * laptop_mode, not even desirable). However, to do otherwise
3301 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003302 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003303 * ext4_da_writepages() ->
3304 * write_cache_pages() ---> (via passed in callback function)
3305 * __mpage_da_writepage() -->
3306 * mpage_add_bh_to_extent()
3307 * mpage_da_map_blocks()
3308 *
3309 * The problem is that write_cache_pages(), located in
3310 * mm/page-writeback.c, marks pages clean in preparation for
3311 * doing I/O, which is not desirable if we're not planning on
3312 * doing I/O at all.
3313 *
3314 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003315 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003316 * would be ugly in the extreme. So instead we would need to
3317 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003318 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003319 * write out the pages, but rather only collect contiguous
3320 * logical block extents, call the multi-block allocator, and
3321 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003322 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003323 * For now, though, we'll cheat by calling filemap_flush(),
3324 * which will map the blocks, and start the I/O, but not
3325 * actually wait for the I/O to complete.
3326 */
3327 return filemap_flush(inode->i_mapping);
3328}
Alex Tomas64769242008-07-11 19:27:31 -04003329
3330/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003331 * bmap() is special. It gets used by applications such as lilo and by
3332 * the swapper to find the on-disk block of a specific piece of data.
3333 *
3334 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003335 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003336 * filesystem and enables swap, then they may get a nasty shock when the
3337 * data getting swapped to that swapfile suddenly gets overwritten by
3338 * the original zero's written out previously to the journal and
3339 * awaiting writeback in the kernel's buffer cache.
3340 *
3341 * So, if we see any bmap calls here on a modified, data-journaled file,
3342 * take extra steps to flush any blocks which might be in the cache.
3343 */
Mingming Cao617ba132006-10-11 01:20:53 -07003344static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345{
3346 struct inode *inode = mapping->host;
3347 journal_t *journal;
3348 int err;
3349
Alex Tomas64769242008-07-11 19:27:31 -04003350 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3351 test_opt(inode->i_sb, DELALLOC)) {
3352 /*
3353 * With delalloc we want to sync the file
3354 * so that we can make sure we allocate
3355 * blocks for file
3356 */
3357 filemap_write_and_wait(mapping);
3358 }
3359
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003360 if (EXT4_JOURNAL(inode) &&
3361 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003362 /*
3363 * This is a REALLY heavyweight approach, but the use of
3364 * bmap on dirty files is expected to be extremely rare:
3365 * only if we run lilo or swapon on a freshly made file
3366 * do we expect this to happen.
3367 *
3368 * (bmap requires CAP_SYS_RAWIO so this does not
3369 * represent an unprivileged user DOS attack --- we'd be
3370 * in trouble if mortal users could trigger this path at
3371 * will.)
3372 *
Mingming Cao617ba132006-10-11 01:20:53 -07003373 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003374 * regular files. If somebody wants to bmap a directory
3375 * or symlink and gets confused because the buffer
3376 * hasn't yet been flushed to disk, they deserve
3377 * everything they get.
3378 */
3379
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003380 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003381 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003382 jbd2_journal_lock_updates(journal);
3383 err = jbd2_journal_flush(journal);
3384 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003385
3386 if (err)
3387 return 0;
3388 }
3389
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003390 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003391}
3392
Mingming Cao617ba132006-10-11 01:20:53 -07003393static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003394{
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003395 trace_ext4_readpage(page);
Mingming Cao617ba132006-10-11 01:20:53 -07003396 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003397}
3398
3399static int
Mingming Cao617ba132006-10-11 01:20:53 -07003400ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003401 struct list_head *pages, unsigned nr_pages)
3402{
Mingming Cao617ba132006-10-11 01:20:53 -07003403 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003404}
3405
Jiaying Zhang744692d2010-03-04 16:14:02 -05003406static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset)
3407{
3408 struct buffer_head *head, *bh;
3409 unsigned int curr_off = 0;
3410
3411 if (!page_has_buffers(page))
3412 return;
3413 head = bh = page_buffers(page);
3414 do {
3415 if (offset <= curr_off && test_clear_buffer_uninit(bh)
3416 && bh->b_private) {
3417 ext4_free_io_end(bh->b_private);
3418 bh->b_private = NULL;
3419 bh->b_end_io = NULL;
3420 }
3421 curr_off = curr_off + bh->b_size;
3422 bh = bh->b_this_page;
3423 } while (bh != head);
3424}
3425
Mingming Cao617ba132006-10-11 01:20:53 -07003426static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003427{
Mingming Cao617ba132006-10-11 01:20:53 -07003428 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003429
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003430 trace_ext4_invalidatepage(page, offset);
3431
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003432 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003433 * free any io_end structure allocated for buffers to be discarded
3434 */
3435 if (ext4_should_dioread_nolock(page->mapping->host))
3436 ext4_invalidatepage_free_endio(page, offset);
3437 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438 * If it's a full truncate we just forget about the pending dirtying
3439 */
3440 if (offset == 0)
3441 ClearPageChecked(page);
3442
Frank Mayhar03901312009-01-07 00:06:22 -05003443 if (journal)
3444 jbd2_journal_invalidatepage(journal, page, offset);
3445 else
3446 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447}
3448
Mingming Cao617ba132006-10-11 01:20:53 -07003449static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003450{
Mingming Cao617ba132006-10-11 01:20:53 -07003451 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003452
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003453 trace_ext4_releasepage(page);
3454
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003455 WARN_ON(PageChecked(page));
3456 if (!page_has_buffers(page))
3457 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003458 if (journal)
3459 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3460 else
3461 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003462}
3463
3464/*
Mingming Cao4c0425f2009-09-28 15:48:41 -04003465 * O_DIRECT for ext3 (or indirect map) based files
3466 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003467 * If the O_DIRECT write will extend the file then add this inode to the
3468 * orphan list. So recovery will truncate it back to the original size
3469 * if the machine crashes during the write.
3470 *
3471 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003472 * crashes then stale disk data _may_ be exposed inside the file. But current
3473 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003474 */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003475static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003476 const struct iovec *iov, loff_t offset,
3477 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003478{
3479 struct file *file = iocb->ki_filp;
3480 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003481 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003482 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003483 ssize_t ret;
3484 int orphan = 0;
3485 size_t count = iov_length(iov, nr_segs);
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003486 int retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003487
3488 if (rw == WRITE) {
3489 loff_t final_size = offset + count;
3490
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003491 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003492 /* Credits for sb + inode write */
3493 handle = ext4_journal_start(inode, 2);
3494 if (IS_ERR(handle)) {
3495 ret = PTR_ERR(handle);
3496 goto out;
3497 }
Mingming Cao617ba132006-10-11 01:20:53 -07003498 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003499 if (ret) {
3500 ext4_journal_stop(handle);
3501 goto out;
3502 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003503 orphan = 1;
3504 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003505 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003506 }
3507 }
3508
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003509retry:
Jiaying Zhangb7adc1f2010-03-02 13:26:36 -05003510 if (rw == READ && ext4_should_dioread_nolock(inode))
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003511 ret = __blockdev_direct_IO(rw, iocb, inode,
Jiaying Zhangb7adc1f2010-03-02 13:26:36 -05003512 inode->i_sb->s_bdev, iov,
3513 offset, nr_segs,
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003514 ext4_get_block, NULL, NULL, 0);
3515 else {
Jiaying Zhangb7adc1f2010-03-02 13:26:36 -05003516 ret = blockdev_direct_IO(rw, iocb, inode,
3517 inode->i_sb->s_bdev, iov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003518 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003519 ext4_get_block, NULL);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003520
3521 if (unlikely((rw & WRITE) && ret < 0)) {
3522 loff_t isize = i_size_read(inode);
3523 loff_t end = offset + iov_length(iov, nr_segs);
3524
3525 if (end > isize)
Jan Karaae24f282011-05-25 17:39:48 -04003526 ext4_truncate_failed_write(inode);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003527 }
3528 }
Eric Sandeenfbbf6942009-10-02 21:20:55 -04003529 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3530 goto retry;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531
Jan Kara7fb54092008-02-10 01:08:38 -05003532 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003533 int err;
3534
Jan Kara7fb54092008-02-10 01:08:38 -05003535 /* Credits for sb + inode write */
3536 handle = ext4_journal_start(inode, 2);
3537 if (IS_ERR(handle)) {
3538 /* This is really bad luck. We've written the data
3539 * but cannot extend i_size. Bail out and pretend
3540 * the write failed... */
3541 ret = PTR_ERR(handle);
Dmitry Monakhovda1dafc2010-03-01 23:15:02 -05003542 if (inode->i_nlink)
3543 ext4_orphan_del(NULL, inode);
3544
Jan Kara7fb54092008-02-10 01:08:38 -05003545 goto out;
3546 }
3547 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003548 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003549 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003550 loff_t end = offset + ret;
3551 if (end > inode->i_size) {
3552 ei->i_disksize = end;
3553 i_size_write(inode, end);
3554 /*
3555 * We're going to return a positive `ret'
3556 * here due to non-zero-length I/O, so there's
3557 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003558 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003559 * ignore it.
3560 */
Mingming Cao617ba132006-10-11 01:20:53 -07003561 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003562 }
3563 }
Mingming Cao617ba132006-10-11 01:20:53 -07003564 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565 if (ret == 0)
3566 ret = err;
3567 }
3568out:
3569 return ret;
3570}
3571
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003572/*
3573 * ext4_get_block used when preparing for a DIO write or buffer write.
3574 * We allocate an uinitialized extent if blocks haven't been allocated.
3575 * The extent will be converted to initialized after the IO is complete.
3576 */
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003577static int ext4_get_block_write(struct inode *inode, sector_t iblock,
Mingming Cao4c0425f2009-09-28 15:48:41 -04003578 struct buffer_head *bh_result, int create)
3579{
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003580 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003581 inode->i_ino, create);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003582 return _ext4_get_block(inode, iblock, bh_result,
3583 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003584}
3585
Mingming Cao4c0425f2009-09-28 15:48:41 -04003586static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig552ef802010-07-27 11:56:06 -04003587 ssize_t size, void *private, int ret,
3588 bool is_async)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003589{
3590 ext4_io_end_t *io_end = iocb->private;
3591 struct workqueue_struct *wq;
Jiaying Zhang744692d2010-03-04 16:14:02 -05003592 unsigned long flags;
3593 struct ext4_inode_info *ei;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003594
Mingming4b70df12009-11-03 14:44:54 -05003595 /* if not async direct IO or dio with 0 bytes write, just return */
3596 if (!io_end || !size)
Christoph Hellwig552ef802010-07-27 11:56:06 -04003597 goto out;
Mingming4b70df12009-11-03 14:44:54 -05003598
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003599 ext_debug("ext4_end_io_dio(): io_end 0x%p"
3600 "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
3601 iocb->private, io_end->inode->i_ino, iocb, offset,
3602 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003603
3604 /* if not aio dio with unwritten extents, just free io and return */
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003605 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003606 ext4_free_io_end(io_end);
3607 iocb->private = NULL;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04003608out:
3609 if (is_async)
3610 aio_complete(iocb, ret, 0);
3611 return;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003612 }
3613
Mingming Cao4c0425f2009-09-28 15:48:41 -04003614 io_end->offset = offset;
3615 io_end->size = size;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04003616 if (is_async) {
3617 io_end->iocb = iocb;
3618 io_end->result = ret;
3619 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003620 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
3621
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003622 /* Add the io_end to per-inode completed aio dio list*/
Jiaying Zhang744692d2010-03-04 16:14:02 -05003623 ei = EXT4_I(io_end->inode);
3624 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
3625 list_add_tail(&io_end->list, &ei->i_completed_io_list);
3626 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
Eric Sandeenc999af22010-10-27 21:30:07 -04003627
3628 /* queue the work to convert unwritten extents to written */
3629 queue_work(wq, &io_end->work);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003630 iocb->private = NULL;
3631}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003632
Jiaying Zhang744692d2010-03-04 16:14:02 -05003633static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate)
3634{
3635 ext4_io_end_t *io_end = bh->b_private;
3636 struct workqueue_struct *wq;
3637 struct inode *inode;
3638 unsigned long flags;
3639
3640 if (!test_clear_buffer_uninit(bh) || !io_end)
3641 goto out;
3642
3643 if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) {
3644 printk("sb umounted, discard end_io request for inode %lu\n",
3645 io_end->inode->i_ino);
3646 ext4_free_io_end(io_end);
3647 goto out;
3648 }
3649
Tao Ma4eddd2a2011-08-13 12:30:59 -04003650 /*
3651 * It may be over-defensive here to check EXT4_IO_END_UNWRITTEN now,
3652 * but being more careful is always safe for the future change.
3653 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003654 inode = io_end->inode;
Tao Ma4eddd2a2011-08-13 12:30:59 -04003655 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
3656 io_end->flag |= EXT4_IO_END_UNWRITTEN;
3657 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3658 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003659
3660 /* Add the io_end to per-inode completed io list*/
3661 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
3662 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
3663 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
3664
3665 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
3666 /* queue the work to convert unwritten extents to written */
3667 queue_work(wq, &io_end->work);
3668out:
3669 bh->b_private = NULL;
3670 bh->b_end_io = NULL;
3671 clear_buffer_uninit(bh);
3672 end_buffer_async_write(bh, uptodate);
3673}
3674
3675static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode)
3676{
3677 ext4_io_end_t *io_end;
3678 struct page *page = bh->b_page;
3679 loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT;
3680 size_t size = bh->b_size;
3681
3682retry:
3683 io_end = ext4_init_io_end(inode, GFP_ATOMIC);
3684 if (!io_end) {
Andrew Morton6db26ff2011-01-12 16:59:13 -08003685 pr_warn_ratelimited("%s: allocation fail\n", __func__);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003686 schedule();
3687 goto retry;
3688 }
3689 io_end->offset = offset;
3690 io_end->size = size;
3691 /*
3692 * We need to hold a reference to the page to make sure it
3693 * doesn't get evicted before ext4_end_io_work() has a chance
3694 * to convert the extent from written to unwritten.
3695 */
3696 io_end->page = page;
3697 get_page(io_end->page);
3698
3699 bh->b_private = io_end;
3700 bh->b_end_io = ext4_end_io_buffer_write;
3701 return 0;
3702}
3703
Mingming Cao4c0425f2009-09-28 15:48:41 -04003704/*
3705 * For ext4 extent files, ext4 will do direct-io write to holes,
3706 * preallocated extents, and those write extend the file, no need to
3707 * fall back to buffered IO.
3708 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003709 * For holes, we fallocate those blocks, mark them as uninitialized
Mingming Cao4c0425f2009-09-28 15:48:41 -04003710 * If those blocks were preallocated, we mark sure they are splited, but
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003711 * still keep the range to write as uninitialized.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003712 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003713 * The unwrritten extents will be converted to written when DIO is completed.
3714 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003715 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003716 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003717 *
3718 * If the O_DIRECT write will extend the file then add this inode to the
3719 * orphan list. So recovery will truncate it back to the original size
3720 * if the machine crashes during the write.
3721 *
3722 */
3723static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3724 const struct iovec *iov, loff_t offset,
3725 unsigned long nr_segs)
3726{
3727 struct file *file = iocb->ki_filp;
3728 struct inode *inode = file->f_mapping->host;
3729 ssize_t ret;
3730 size_t count = iov_length(iov, nr_segs);
3731
3732 loff_t final_size = offset + count;
3733 if (rw == WRITE && final_size <= inode->i_size) {
3734 /*
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003735 * We could direct write to holes and fallocate.
3736 *
3737 * Allocated blocks to fill the hole are marked as uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003738 * to prevent parallel buffered read to expose the stale data
Mingming Cao4c0425f2009-09-28 15:48:41 -04003739 * before DIO complete the data IO.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003740 *
3741 * As to previously fallocated extents, ext4 get_block
Mingming Cao4c0425f2009-09-28 15:48:41 -04003742 * will just simply mark the buffer mapped but still
3743 * keep the extents uninitialized.
3744 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003745 * for non AIO case, we will convert those unwritten extents
3746 * to written after return back from blockdev_direct_IO.
3747 *
3748 * for async DIO, the conversion needs to be defered when
3749 * the IO is completed. The ext4 end_io callback function
3750 * will be called to take care of the conversion work.
3751 * Here for async case, we allocate an io_end structure to
3752 * hook to the iocb.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003753 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003754 iocb->private = NULL;
3755 EXT4_I(inode)->cur_aio_dio = NULL;
3756 if (!is_sync_kiocb(iocb)) {
Jiaying Zhang744692d2010-03-04 16:14:02 -05003757 iocb->private = ext4_init_io_end(inode, GFP_NOFS);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003758 if (!iocb->private)
3759 return -ENOMEM;
3760 /*
3761 * we save the io structure for current async
Eric Sandeen79e83032010-07-27 11:56:07 -04003762 * direct IO, so that later ext4_map_blocks()
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003763 * could flag the io structure whether there
3764 * is a unwritten extents needs to be converted
3765 * when IO is completed.
3766 */
3767 EXT4_I(inode)->cur_aio_dio = iocb->private;
3768 }
3769
Mingming Cao4c0425f2009-09-28 15:48:41 -04003770 ret = blockdev_direct_IO(rw, iocb, inode,
3771 inode->i_sb->s_bdev, iov,
3772 offset, nr_segs,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003773 ext4_get_block_write,
Mingming Cao4c0425f2009-09-28 15:48:41 -04003774 ext4_end_io_dio);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003775 if (iocb->private)
3776 EXT4_I(inode)->cur_aio_dio = NULL;
3777 /*
3778 * The io_end structure takes a reference to the inode,
3779 * that structure needs to be destroyed and the
3780 * reference to the inode need to be dropped, when IO is
3781 * complete, even with 0 byte write, or failed.
3782 *
3783 * In the successful AIO DIO case, the io_end structure will be
3784 * desctroyed and the reference to the inode will be dropped
3785 * after the end_io call back function is called.
3786 *
3787 * In the case there is 0 byte write, or error case, since
3788 * VFS direct IO won't invoke the end_io call back function,
3789 * we need to free the end_io structure here.
3790 */
3791 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3792 ext4_free_io_end(iocb->private);
3793 iocb->private = NULL;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003794 } else if (ret > 0 && ext4_test_inode_state(inode,
3795 EXT4_STATE_DIO_UNWRITTEN)) {
Mingming109f5562009-11-10 10:48:08 -05003796 int err;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003797 /*
3798 * for non AIO case, since the IO is already
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003799 * completed, we could do the conversion right here
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003800 */
Mingming109f5562009-11-10 10:48:08 -05003801 err = ext4_convert_unwritten_extents(inode,
3802 offset, ret);
3803 if (err < 0)
3804 ret = err;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003805 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Mingming109f5562009-11-10 10:48:08 -05003806 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003807 return ret;
3808 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003809
3810 /* for write the the end of file case, we fall back to old way */
Mingming Cao4c0425f2009-09-28 15:48:41 -04003811 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3812}
3813
3814static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
3815 const struct iovec *iov, loff_t offset,
3816 unsigned long nr_segs)
3817{
3818 struct file *file = iocb->ki_filp;
3819 struct inode *inode = file->f_mapping->host;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003820 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003821
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003822 trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003823 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003824 ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3825 else
3826 ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3827 trace_ext4_direct_IO_exit(inode, offset,
3828 iov_length(iov, nr_segs), rw, ret);
3829 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003830}
3831
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003832/*
Mingming Cao617ba132006-10-11 01:20:53 -07003833 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3835 * much here because ->set_page_dirty is called under VFS locks. The page is
3836 * not necessarily locked.
3837 *
3838 * We cannot just dirty the page and leave attached buffers clean, because the
3839 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3840 * or jbddirty because all the journalling code will explode.
3841 *
3842 * So what we do is to mark the page "pending dirty" and next time writepage
3843 * is called, propagate that into the buffers appropriately.
3844 */
Mingming Cao617ba132006-10-11 01:20:53 -07003845static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003846{
3847 SetPageChecked(page);
3848 return __set_page_dirty_nobuffers(page);
3849}
3850
Mingming Cao617ba132006-10-11 01:20:53 -07003851static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003852 .readpage = ext4_readpage,
3853 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003854 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003855 .write_begin = ext4_write_begin,
3856 .write_end = ext4_ordered_write_end,
3857 .bmap = ext4_bmap,
3858 .invalidatepage = ext4_invalidatepage,
3859 .releasepage = ext4_releasepage,
3860 .direct_IO = ext4_direct_IO,
3861 .migratepage = buffer_migrate_page,
3862 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003863 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003864};
3865
Mingming Cao617ba132006-10-11 01:20:53 -07003866static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003867 .readpage = ext4_readpage,
3868 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003869 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003870 .write_begin = ext4_write_begin,
3871 .write_end = ext4_writeback_write_end,
3872 .bmap = ext4_bmap,
3873 .invalidatepage = ext4_invalidatepage,
3874 .releasepage = ext4_releasepage,
3875 .direct_IO = ext4_direct_IO,
3876 .migratepage = buffer_migrate_page,
3877 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003878 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879};
3880
Mingming Cao617ba132006-10-11 01:20:53 -07003881static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003882 .readpage = ext4_readpage,
3883 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003884 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003885 .write_begin = ext4_write_begin,
3886 .write_end = ext4_journalled_write_end,
3887 .set_page_dirty = ext4_journalled_set_page_dirty,
3888 .bmap = ext4_bmap,
3889 .invalidatepage = ext4_invalidatepage,
3890 .releasepage = ext4_releasepage,
3891 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003892 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893};
3894
Alex Tomas64769242008-07-11 19:27:31 -04003895static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003896 .readpage = ext4_readpage,
3897 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003898 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003899 .writepages = ext4_da_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003900 .write_begin = ext4_da_write_begin,
3901 .write_end = ext4_da_write_end,
3902 .bmap = ext4_bmap,
3903 .invalidatepage = ext4_da_invalidatepage,
3904 .releasepage = ext4_releasepage,
3905 .direct_IO = ext4_direct_IO,
3906 .migratepage = buffer_migrate_page,
3907 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003908 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003909};
3910
Mingming Cao617ba132006-10-11 01:20:53 -07003911void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003912{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003913 if (ext4_should_order_data(inode) &&
3914 test_opt(inode->i_sb, DELALLOC))
3915 inode->i_mapping->a_ops = &ext4_da_aops;
3916 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003917 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003918 else if (ext4_should_writeback_data(inode) &&
3919 test_opt(inode->i_sb, DELALLOC))
3920 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003921 else if (ext4_should_writeback_data(inode))
3922 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003923 else
Mingming Cao617ba132006-10-11 01:20:53 -07003924 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003925}
3926
3927/*
Mingming Cao617ba132006-10-11 01:20:53 -07003928 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003929 * up to the end of the block which corresponds to `from'.
3930 * This required during truncate. We need to physically zero the tail end
3931 * of that block so it doesn't yield old data if the file is later grown.
3932 */
Jan Karacf108bc2008-07-11 19:27:31 -04003933int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003934 struct address_space *mapping, loff_t from)
3935{
Allison Henderson30848852011-05-25 07:41:32 -04003936 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3937 unsigned length;
3938 unsigned blocksize;
3939 struct inode *inode = mapping->host;
3940
3941 blocksize = inode->i_sb->s_blocksize;
3942 length = blocksize - (offset & (blocksize - 1));
3943
3944 return ext4_block_zero_page_range(handle, mapping, from, length);
3945}
3946
3947/*
3948 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3949 * starting from file offset 'from'. The range to be zero'd must
3950 * be contained with in one block. If the specified range exceeds
3951 * the end of the block it will be shortened to end of the block
3952 * that cooresponds to 'from'
3953 */
3954int ext4_block_zero_page_range(handle_t *handle,
3955 struct address_space *mapping, loff_t from, loff_t length)
3956{
Mingming Cao617ba132006-10-11 01:20:53 -07003957 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Allison Henderson30848852011-05-25 07:41:32 -04003959 unsigned blocksize, max, pos;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003960 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003961 struct inode *inode = mapping->host;
3962 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003963 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003964 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003965
Theodore Ts'of4a01012009-07-05 22:08:16 -04003966 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3967 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003968 if (!page)
3969 return -EINVAL;
3970
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003971 blocksize = inode->i_sb->s_blocksize;
Allison Henderson30848852011-05-25 07:41:32 -04003972 max = blocksize - (offset & (blocksize - 1));
3973
3974 /*
3975 * correct length if it does not fall between
3976 * 'from' and the end of the block
3977 */
3978 if (length > max || length < 0)
3979 length = max;
3980
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003981 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3982
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003983 if (!page_has_buffers(page))
3984 create_empty_buffers(page, blocksize, 0);
3985
3986 /* Find the buffer that contains "offset" */
3987 bh = page_buffers(page);
3988 pos = blocksize;
3989 while (offset >= pos) {
3990 bh = bh->b_this_page;
3991 iblock++;
3992 pos += blocksize;
3993 }
3994
3995 err = 0;
3996 if (buffer_freed(bh)) {
3997 BUFFER_TRACE(bh, "freed: skip");
3998 goto unlock;
3999 }
4000
4001 if (!buffer_mapped(bh)) {
4002 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07004003 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004004 /* unmapped? It's a hole - nothing to do */
4005 if (!buffer_mapped(bh)) {
4006 BUFFER_TRACE(bh, "still unmapped");
4007 goto unlock;
4008 }
4009 }
4010
4011 /* Ok, it's mapped. Make sure it's up-to-date */
4012 if (PageUptodate(page))
4013 set_buffer_uptodate(bh);
4014
4015 if (!buffer_uptodate(bh)) {
4016 err = -EIO;
4017 ll_rw_block(READ, 1, &bh);
4018 wait_on_buffer(bh);
4019 /* Uhhuh. Read error. Complain and punt. */
4020 if (!buffer_uptodate(bh))
4021 goto unlock;
4022 }
4023
Mingming Cao617ba132006-10-11 01:20:53 -07004024 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004025 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07004026 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 if (err)
4028 goto unlock;
4029 }
4030
Christoph Lametereebd2aa2008-02-04 22:28:29 -08004031 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004032
4033 BUFFER_TRACE(bh, "zeroed end of block");
4034
4035 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07004036 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05004037 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004038 } else {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05004039 if (ext4_should_order_data(inode) && EXT4_I(inode)->jinode)
Jan Kara678aaf42008-07-11 19:27:31 -04004040 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004041 mark_buffer_dirty(bh);
4042 }
4043
4044unlock:
4045 unlock_page(page);
4046 page_cache_release(page);
4047 return err;
4048}
4049
4050/*
4051 * Probably it should be a library function... search for first non-zero word
4052 * or memcmp with zero_page, whatever is better for particular architecture.
4053 * Linus?
4054 */
4055static inline int all_zeroes(__le32 *p, __le32 *q)
4056{
4057 while (p < q)
4058 if (*p++)
4059 return 0;
4060 return 1;
4061}
4062
4063/**
Mingming Cao617ba132006-10-11 01:20:53 -07004064 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004065 * @inode: inode in question
4066 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07004067 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004068 * @chain: place to store the pointers to partial indirect blocks
4069 * @top: place to the (detached) top of branch
4070 *
Mingming Cao617ba132006-10-11 01:20:53 -07004071 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004072 *
4073 * When we do truncate() we may have to clean the ends of several
4074 * indirect blocks but leave the blocks themselves alive. Block is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004075 * partially truncated if some data below the new i_size is referred
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004076 * from it (and it is on the path to the first completely truncated
4077 * data block, indeed). We have to free the top of that path along
4078 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07004079 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004080 * finishes, we may safely do the latter, but top of branch may
4081 * require special attention - pageout below the truncation point
4082 * might try to populate it.
4083 *
4084 * We atomically detach the top of branch from the tree, store the
4085 * block number of its root in *@top, pointers to buffer_heads of
4086 * partially truncated blocks - in @chain[].bh and pointers to
4087 * their last elements that should not be removed - in
4088 * @chain[].p. Return value is the pointer to last filled element
4089 * of @chain.
4090 *
4091 * The work left to caller to do the actual freeing of subtrees:
4092 * a) free the subtree starting from *@top
4093 * b) free the subtrees whose roots are stored in
4094 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
4095 * c) free the subtrees growing from the inode past the @chain[0].
4096 * (no partially truncated stuff there). */
4097
Mingming Cao617ba132006-10-11 01:20:53 -07004098static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004099 ext4_lblk_t offsets[4], Indirect chain[4],
4100 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004101{
4102 Indirect *partial, *p;
4103 int k, err;
4104
4105 *top = 0;
Uwe Kleine-Königbf48aab2009-10-28 20:11:03 +01004106 /* Make k index the deepest non-null offset + 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004107 for (k = depth; k > 1 && !offsets[k-1]; k--)
4108 ;
Mingming Cao617ba132006-10-11 01:20:53 -07004109 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004110 /* Writer: pointers */
4111 if (!partial)
4112 partial = chain + k-1;
4113 /*
4114 * If the branch acquired continuation since we've looked at it -
4115 * fine, it should all survive and (new) top doesn't belong to us.
4116 */
4117 if (!partial->key && *partial->p)
4118 /* Writer: end */
4119 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004120 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004121 ;
4122 /*
4123 * OK, we've found the last block that must survive. The rest of our
4124 * branch should be detached before unlocking. However, if that rest
4125 * of branch is all ours and does not grow immediately from the inode
4126 * it's easier to cheat and just decrement partial->p.
4127 */
4128 if (p == chain + k - 1 && p > chain) {
4129 p->p--;
4130 } else {
4131 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07004132 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004133#if 0
4134 *p->p = 0;
4135#endif
4136 }
4137 /* Writer: end */
4138
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004139 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004140 brelse(partial->bh);
4141 partial--;
4142 }
4143no_top:
4144 return partial;
4145}
4146
4147/*
4148 * Zero a number of block pointers in either an inode or an indirect block.
4149 * If we restart the transaction we must again get write access to the
4150 * indirect block for further modification.
4151 *
4152 * We release `count' blocks on disk, but (last - first) may be greater
4153 * than `count' because there can be holes in there.
Amir Goldsteind67d1212011-03-20 22:59:02 -04004154 *
4155 * Return 0 on success, 1 on invalid block range
4156 * and < 0 on fatal error.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004157 */
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004158static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
4159 struct buffer_head *bh,
4160 ext4_fsblk_t block_to_free,
4161 unsigned long count, __le32 *first,
4162 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004163{
4164 __le32 *p;
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004165 int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED;
Theodore Ts'ob4097142011-01-10 12:46:59 -05004166 int err;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004167
4168 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4169 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o50689692009-11-23 07:17:34 -05004170
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004171 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
4172 count)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004173 EXT4_ERROR_INODE(inode, "attempt to clear invalid "
4174 "blocks %llu len %lu",
4175 (unsigned long long) block_to_free, count);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004176 return 1;
4177 }
4178
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004179 if (try_to_extend_transaction(handle, inode)) {
4180 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004181 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Theodore Ts'ob4097142011-01-10 12:46:59 -05004182 err = ext4_handle_dirty_metadata(handle, inode, bh);
Amir Goldsteind67d1212011-03-20 22:59:02 -04004183 if (unlikely(err))
4184 goto out_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004185 }
Theodore Ts'ob4097142011-01-10 12:46:59 -05004186 err = ext4_mark_inode_dirty(handle, inode);
Amir Goldsteind67d1212011-03-20 22:59:02 -04004187 if (unlikely(err))
4188 goto out_err;
Theodore Ts'ob4097142011-01-10 12:46:59 -05004189 err = ext4_truncate_restart_trans(handle, inode,
4190 blocks_for_truncate(inode));
Amir Goldsteind67d1212011-03-20 22:59:02 -04004191 if (unlikely(err))
4192 goto out_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004193 if (bh) {
4194 BUFFER_TRACE(bh, "retaking write access");
Amir Goldsteind67d1212011-03-20 22:59:02 -04004195 err = ext4_journal_get_write_access(handle, bh);
4196 if (unlikely(err))
4197 goto out_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004198 }
4199 }
4200
Theodore Ts'oe6362602009-11-23 07:17:05 -05004201 for (p = first; p < last; p++)
4202 *p = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004203
Peter Huewe7dc57612011-02-21 21:01:42 -05004204 ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004205 return 0;
Amir Goldsteind67d1212011-03-20 22:59:02 -04004206out_err:
4207 ext4_std_error(inode->i_sb, err);
4208 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004209}
4210
4211/**
Mingming Cao617ba132006-10-11 01:20:53 -07004212 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004213 * @handle: handle for this transaction
4214 * @inode: inode we are dealing with
4215 * @this_bh: indirect buffer_head which contains *@first and *@last
4216 * @first: array of block numbers
4217 * @last: points immediately past the end of array
4218 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004219 * We are freeing all blocks referred from that array (numbers are stored as
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004220 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
4221 *
4222 * We accumulate contiguous runs of blocks to free. Conveniently, if these
4223 * blocks are contiguous then releasing them at one time will only affect one
4224 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
4225 * actually use a lot of journal space.
4226 *
4227 * @this_bh will be %NULL if @first and @last point into the inode's direct
4228 * block pointers.
4229 */
Mingming Cao617ba132006-10-11 01:20:53 -07004230static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004231 struct buffer_head *this_bh,
4232 __le32 *first, __le32 *last)
4233{
Mingming Cao617ba132006-10-11 01:20:53 -07004234 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004235 unsigned long count = 0; /* Number of blocks in the run */
4236 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
4237 corresponding to
4238 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07004239 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004240 __le32 *p; /* Pointer into inode/ind
4241 for current block */
Amir Goldsteind67d1212011-03-20 22:59:02 -04004242 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004243
4244 if (this_bh) { /* For indirect block */
4245 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004246 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004247 /* Important: if we can't update the indirect pointers
4248 * to the blocks, we can't free them. */
4249 if (err)
4250 return;
4251 }
4252
4253 for (p = first; p < last; p++) {
4254 nr = le32_to_cpu(*p);
4255 if (nr) {
4256 /* accumulate blocks to free if they're contiguous */
4257 if (count == 0) {
4258 block_to_free = nr;
4259 block_to_free_p = p;
4260 count = 1;
4261 } else if (nr == block_to_free + count) {
4262 count++;
4263 } else {
Amir Goldsteind67d1212011-03-20 22:59:02 -04004264 err = ext4_clear_blocks(handle, inode, this_bh,
4265 block_to_free, count,
4266 block_to_free_p, p);
4267 if (err)
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004268 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004269 block_to_free = nr;
4270 block_to_free_p = p;
4271 count = 1;
4272 }
4273 }
4274 }
4275
Amir Goldsteind67d1212011-03-20 22:59:02 -04004276 if (!err && count > 0)
4277 err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
4278 count, block_to_free_p, p);
4279 if (err < 0)
4280 /* fatal error */
4281 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004282
4283 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05004284 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004285
4286 /*
4287 * The buffer head should have an attached journal head at this
4288 * point. However, if the data is corrupted and an indirect
4289 * block pointed to itself, it would have been detached when
4290 * the block was cleared. Check for this instead of OOPSing.
4291 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05004292 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05004293 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04004294 else
Theodore Ts'o24676da2010-05-16 21:00:00 -04004295 EXT4_ERROR_INODE(inode,
4296 "circular indirect block detected at "
4297 "block %llu",
4298 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004299 }
4300}
4301
4302/**
Mingming Cao617ba132006-10-11 01:20:53 -07004303 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004304 * @handle: JBD handle for this transaction
4305 * @inode: inode we are dealing with
4306 * @parent_bh: the buffer_head which contains *@first and *@last
4307 * @first: array of block numbers
4308 * @last: pointer immediately past the end of array
4309 * @depth: depth of the branches to free
4310 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004311 * We are freeing all blocks referred from these branches (numbers are
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004312 * stored as little-endian 32-bit) and updating @inode->i_blocks
4313 * appropriately.
4314 */
Mingming Cao617ba132006-10-11 01:20:53 -07004315static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004316 struct buffer_head *parent_bh,
4317 __le32 *first, __le32 *last, int depth)
4318{
Mingming Cao617ba132006-10-11 01:20:53 -07004319 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004320 __le32 *p;
4321
Frank Mayhar03901312009-01-07 00:06:22 -05004322 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004323 return;
4324
4325 if (depth--) {
4326 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004327 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004328 p = last;
4329 while (--p >= first) {
4330 nr = le32_to_cpu(*p);
4331 if (!nr)
4332 continue; /* A hole */
4333
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004334 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
4335 nr, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004336 EXT4_ERROR_INODE(inode,
4337 "invalid indirect mapped "
4338 "block %lu (level %d)",
4339 (unsigned long) nr, depth);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004340 break;
4341 }
4342
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004343 /* Go read the buffer for the next level down */
4344 bh = sb_bread(inode->i_sb, nr);
4345
4346 /*
4347 * A read failure? Report error and clear slot
4348 * (should be rare).
4349 */
4350 if (!bh) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004351 EXT4_ERROR_INODE_BLOCK(inode, nr,
4352 "Read failure");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004353 continue;
4354 }
4355
4356 /* This zaps the entire block. Bottom up. */
4357 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07004358 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004359 (__le32 *) bh->b_data,
4360 (__le32 *) bh->b_data + addr_per_block,
4361 depth);
Theodore Ts'o1c5b9e92011-01-10 12:51:28 -05004362 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004363
4364 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365 * Everything below this this pointer has been
4366 * released. Now let this top-of-subtree go.
4367 *
4368 * We want the freeing of this indirect block to be
4369 * atomic in the journal with the updating of the
4370 * bitmap block which owns it. So make some room in
4371 * the journal.
4372 *
4373 * We zero the parent pointer *after* freeing its
4374 * pointee in the bitmaps, so if extend_transaction()
4375 * for some reason fails to put the bitmap changes and
4376 * the release into the same transaction, recovery
4377 * will merely complain about releasing a free block,
4378 * rather than leaking blocks.
4379 */
Frank Mayhar03901312009-01-07 00:06:22 -05004380 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381 return;
4382 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004383 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04004384 ext4_truncate_restart_trans(handle, inode,
4385 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004386 }
4387
Amir G40389682010-07-27 11:56:05 -04004388 /*
4389 * The forget flag here is critical because if
4390 * we are journaling (and not doing data
4391 * journaling), we have to make sure a revoke
4392 * record is written to prevent the journal
4393 * replay from overwriting the (former)
4394 * indirect block if it gets reallocated as a
4395 * data block. This must happen in the same
4396 * transaction where the data blocks are
4397 * actually freed.
4398 */
Peter Huewe7dc57612011-02-21 21:01:42 -05004399 ext4_free_blocks(handle, inode, NULL, nr, 1,
Amir G40389682010-07-27 11:56:05 -04004400 EXT4_FREE_BLOCKS_METADATA|
4401 EXT4_FREE_BLOCKS_FORGET);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004402
4403 if (parent_bh) {
4404 /*
4405 * The block which we have just freed is
4406 * pointed to by an indirect block: journal it
4407 */
4408 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004409 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004410 parent_bh)){
4411 *p = 0;
4412 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05004413 "call ext4_handle_dirty_metadata");
4414 ext4_handle_dirty_metadata(handle,
4415 inode,
4416 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004417 }
4418 }
4419 }
4420 } else {
4421 /* We have reached the bottom of the tree. */
4422 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07004423 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004424 }
4425}
4426
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004427int ext4_can_truncate(struct inode *inode)
4428{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004429 if (S_ISREG(inode->i_mode))
4430 return 1;
4431 if (S_ISDIR(inode->i_mode))
4432 return 1;
4433 if (S_ISLNK(inode->i_mode))
4434 return !ext4_inode_is_fast_symlink(inode);
4435 return 0;
4436}
4437
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004438/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004439 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
4440 * associated with the given offset and length
4441 *
4442 * @inode: File inode
4443 * @offset: The offset where the hole will begin
4444 * @len: The length of the hole
4445 *
4446 * Returns: 0 on sucess or negative on failure
4447 */
4448
4449int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
4450{
4451 struct inode *inode = file->f_path.dentry->d_inode;
4452 if (!S_ISREG(inode->i_mode))
4453 return -ENOTSUPP;
4454
4455 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4456 /* TODO: Add support for non extent hole punching */
4457 return -ENOTSUPP;
4458 }
4459
4460 return ext4_ext_punch_hole(file, offset, length);
4461}
4462
4463/*
Mingming Cao617ba132006-10-11 01:20:53 -07004464 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004465 *
Mingming Cao617ba132006-10-11 01:20:53 -07004466 * We block out ext4_get_block() block instantiations across the entire
4467 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004468 * simultaneously on behalf of the same inode.
4469 *
4470 * As we work through the truncate and commmit bits of it to the journal there
4471 * is one core, guiding principle: the file's tree must always be consistent on
4472 * disk. We must be able to restart the truncate after a crash.
4473 *
4474 * The file's tree may be transiently inconsistent in memory (although it
4475 * probably isn't), but whenever we close off and commit a journal transaction,
4476 * the contents of (the filesystem + the journal) must be consistent and
4477 * restartable. It's pretty simple, really: bottom up, right to left (although
4478 * left-to-right works OK too).
4479 *
4480 * Note that at recovery time, journal replay occurs *before* the restart of
4481 * truncate against the orphan inode list.
4482 *
4483 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004484 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004485 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004486 * ext4_truncate() to have another go. So there will be instantiated blocks
4487 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004488 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004489 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004490 */
Mingming Cao617ba132006-10-11 01:20:53 -07004491void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004492{
4493 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07004494 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004495 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004496 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004497 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004498 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004499 Indirect chain[4];
4500 Indirect *partial;
4501 __le32 nr = 0;
Kazuya Miof80da1e2011-04-10 22:06:36 -04004502 int n = 0;
4503 ext4_lblk_t last_block, max_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004504 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004505
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004506 trace_ext4_truncate_enter(inode);
4507
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004508 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004509 return;
4510
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004511 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004512
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004513 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004514 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004515
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004516 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Jan Karacf108bc2008-07-11 19:27:31 -04004517 ext4_ext_truncate(inode);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004518 trace_ext4_truncate_exit(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004519 return;
4520 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004521
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004522 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04004523 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004524 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004525
4526 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07004527 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Kazuya Miof80da1e2011-04-10 22:06:36 -04004528 max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
4529 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004530
Jan Karacf108bc2008-07-11 19:27:31 -04004531 if (inode->i_size & (blocksize - 1))
4532 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
4533 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004534
Kazuya Miof80da1e2011-04-10 22:06:36 -04004535 if (last_block != max_block) {
4536 n = ext4_block_to_path(inode, last_block, offsets, NULL);
4537 if (n == 0)
4538 goto out_stop; /* error */
4539 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004540
4541 /*
4542 * OK. This truncate is going to happen. We add the inode to the
4543 * orphan list, so that if this truncate spans multiple transactions,
4544 * and we crash, we will resume the truncate when the filesystem
4545 * recovers. It also marks the inode dirty, to catch the new size.
4546 *
4547 * Implication: the file must always be in a sane, consistent
4548 * truncatable state while each transaction commits.
4549 */
Mingming Cao617ba132006-10-11 01:20:53 -07004550 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004551 goto out_stop;
4552
4553 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004554 * From here we block out all ext4_get_block() callers who want to
4555 * modify the block allocation tree.
4556 */
4557 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004558
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004559 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004560
Mingming Cao632eaea2008-07-11 19:27:31 -04004561 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004562 * The orphan list entry will now protect us from any crash which
4563 * occurs before the truncate completes, so it is now safe to propagate
4564 * the new, shorter inode size (held for now in i_size) into the
4565 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004566 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004567 */
4568 ei->i_disksize = inode->i_size;
4569
Kazuya Miof80da1e2011-04-10 22:06:36 -04004570 if (last_block == max_block) {
4571 /*
4572 * It is unnecessary to free any data blocks if last_block is
4573 * equal to the indirect block limit.
4574 */
4575 goto out_unlock;
4576 } else if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004577 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4578 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004579 goto do_indirects;
4580 }
4581
Mingming Cao617ba132006-10-11 01:20:53 -07004582 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004583 /* Kill the top of shared branch (not detached) */
4584 if (nr) {
4585 if (partial == chain) {
4586 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004587 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004588 &nr, &nr+1, (chain+n-1) - partial);
4589 *partial->p = 0;
4590 /*
4591 * We mark the inode dirty prior to restart,
4592 * and prior to stop. No need for it here.
4593 */
4594 } else {
4595 /* Shared branch grows from an indirect block */
4596 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004597 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004598 partial->p,
4599 partial->p+1, (chain+n-1) - partial);
4600 }
4601 }
4602 /* Clear the ends of indirect blocks on the shared branch */
4603 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004604 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004605 (__le32*)partial->bh->b_data+addr_per_block,
4606 (chain+n-1) - partial);
4607 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004608 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004609 partial--;
4610 }
4611do_indirects:
4612 /* Kill the remaining (whole) subtrees */
4613 switch (offsets[0]) {
4614 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004615 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004616 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004617 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4618 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004619 }
Mingming Cao617ba132006-10-11 01:20:53 -07004620 case EXT4_IND_BLOCK:
4621 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004622 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004623 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4624 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004625 }
Mingming Cao617ba132006-10-11 01:20:53 -07004626 case EXT4_DIND_BLOCK:
4627 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004628 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004629 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4630 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004631 }
Mingming Cao617ba132006-10-11 01:20:53 -07004632 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004633 ;
4634 }
4635
Kazuya Miof80da1e2011-04-10 22:06:36 -04004636out_unlock:
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004637 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004638 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004639 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004640
4641 /*
4642 * In a multi-transaction truncate, we only make the final transaction
4643 * synchronous
4644 */
4645 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004646 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004647out_stop:
4648 /*
4649 * If this was a simple ftruncate(), and the file will remain alive
4650 * then we need to clear up the orphan record which we created above.
4651 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004652 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004653 * orphan info for us.
4654 */
4655 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004656 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004657
Mingming Cao617ba132006-10-11 01:20:53 -07004658 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004659 trace_ext4_truncate_exit(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004660}
4661
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004662/*
Mingming Cao617ba132006-10-11 01:20:53 -07004663 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004664 * underlying buffer_head on success. If 'in_mem' is true, we have all
4665 * data in memory that is needed to recreate the on-disk version of this
4666 * inode.
4667 */
Mingming Cao617ba132006-10-11 01:20:53 -07004668static int __ext4_get_inode_loc(struct inode *inode,
4669 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004670{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004671 struct ext4_group_desc *gdp;
4672 struct buffer_head *bh;
4673 struct super_block *sb = inode->i_sb;
4674 ext4_fsblk_t block;
4675 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004676
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004677 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004678 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004679 return -EIO;
4680
Theodore Ts'o240799c2008-10-09 23:53:47 -04004681 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4682 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4683 if (!gdp)
4684 return -EIO;
4685
4686 /*
4687 * Figure out the offset within the block group inode table
4688 */
Tao Ma00d09882011-05-09 10:26:41 -04004689 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004690 inode_offset = ((inode->i_ino - 1) %
4691 EXT4_INODES_PER_GROUP(sb));
4692 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4693 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4694
4695 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004696 if (!bh) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004697 EXT4_ERROR_INODE_BLOCK(inode, block,
4698 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004699 return -EIO;
4700 }
4701 if (!buffer_uptodate(bh)) {
4702 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004703
4704 /*
4705 * If the buffer has the write error flag, we have failed
4706 * to write out another inode in the same block. In this
4707 * case, we don't have to read the block because we may
4708 * read the old inode data successfully.
4709 */
4710 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4711 set_buffer_uptodate(bh);
4712
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004713 if (buffer_uptodate(bh)) {
4714 /* someone brought it uptodate while we waited */
4715 unlock_buffer(bh);
4716 goto has_buffer;
4717 }
4718
4719 /*
4720 * If we have all information of the inode in memory and this
4721 * is the only valid inode in the block, we need not read the
4722 * block.
4723 */
4724 if (in_mem) {
4725 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004726 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004727
Theodore Ts'o240799c2008-10-09 23:53:47 -04004728 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004729
4730 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004731 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004732 if (!bitmap_bh)
4733 goto make_io;
4734
4735 /*
4736 * If the inode bitmap isn't in cache then the
4737 * optimisation may end up performing two reads instead
4738 * of one, so skip it.
4739 */
4740 if (!buffer_uptodate(bitmap_bh)) {
4741 brelse(bitmap_bh);
4742 goto make_io;
4743 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004744 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004745 if (i == inode_offset)
4746 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004747 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004748 break;
4749 }
4750 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004751 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004752 /* all other inodes are free, so skip I/O */
4753 memset(bh->b_data, 0, bh->b_size);
4754 set_buffer_uptodate(bh);
4755 unlock_buffer(bh);
4756 goto has_buffer;
4757 }
4758 }
4759
4760make_io:
4761 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004762 * If we need to do any I/O, try to pre-readahead extra
4763 * blocks from the inode table.
4764 */
4765 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4766 ext4_fsblk_t b, end, table;
4767 unsigned num;
4768
4769 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004770 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004771 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4772 if (table > b)
4773 b = table;
4774 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4775 num = EXT4_INODES_PER_GROUP(sb);
4776 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4777 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004778 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004779 table += num / inodes_per_block;
4780 if (end > table)
4781 end = table;
4782 while (b <= end)
4783 sb_breadahead(sb, b++);
4784 }
4785
4786 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004787 * There are other valid inodes in the buffer, this inode
4788 * has in-inode xattrs, or we don't have this inode in memory.
4789 * Read the block from disk.
4790 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004791 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004792 get_bh(bh);
4793 bh->b_end_io = end_buffer_read_sync;
4794 submit_bh(READ_META, bh);
4795 wait_on_buffer(bh);
4796 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004797 EXT4_ERROR_INODE_BLOCK(inode, block,
4798 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004799 brelse(bh);
4800 return -EIO;
4801 }
4802 }
4803has_buffer:
4804 iloc->bh = bh;
4805 return 0;
4806}
4807
Mingming Cao617ba132006-10-11 01:20:53 -07004808int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004809{
4810 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004811 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004812 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004813}
4814
Mingming Cao617ba132006-10-11 01:20:53 -07004815void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004816{
Mingming Cao617ba132006-10-11 01:20:53 -07004817 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004818
4819 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004820 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004821 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004822 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004823 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004824 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004825 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004826 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004827 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004828 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004829 inode->i_flags |= S_DIRSYNC;
4830}
4831
Jan Karaff9ddf72007-07-18 09:24:20 -04004832/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4833void ext4_get_inode_flags(struct ext4_inode_info *ei)
4834{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004835 unsigned int vfs_fl;
4836 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04004837
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004838 do {
4839 vfs_fl = ei->vfs_inode.i_flags;
4840 old_fl = ei->i_flags;
4841 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4842 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
4843 EXT4_DIRSYNC_FL);
4844 if (vfs_fl & S_SYNC)
4845 new_fl |= EXT4_SYNC_FL;
4846 if (vfs_fl & S_APPEND)
4847 new_fl |= EXT4_APPEND_FL;
4848 if (vfs_fl & S_IMMUTABLE)
4849 new_fl |= EXT4_IMMUTABLE_FL;
4850 if (vfs_fl & S_NOATIME)
4851 new_fl |= EXT4_NOATIME_FL;
4852 if (vfs_fl & S_DIRSYNC)
4853 new_fl |= EXT4_DIRSYNC_FL;
4854 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04004855}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004856
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004857static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004858 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004859{
4860 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004861 struct inode *inode = &(ei->vfs_inode);
4862 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004863
4864 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4865 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4866 /* we are using combined 48 bit field */
4867 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4868 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004869 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004870 /* i_blocks represent file system block size */
4871 return i_blocks << (inode->i_blkbits - 9);
4872 } else {
4873 return i_blocks;
4874 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004875 } else {
4876 return le32_to_cpu(raw_inode->i_blocks_lo);
4877 }
4878}
Jan Karaff9ddf72007-07-18 09:24:20 -04004879
David Howells1d1fe1e2008-02-07 00:15:37 -08004880struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004881{
Mingming Cao617ba132006-10-11 01:20:53 -07004882 struct ext4_iloc iloc;
4883 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004884 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004885 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004886 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004887 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004888 int block;
4889
David Howells1d1fe1e2008-02-07 00:15:37 -08004890 inode = iget_locked(sb, ino);
4891 if (!inode)
4892 return ERR_PTR(-ENOMEM);
4893 if (!(inode->i_state & I_NEW))
4894 return inode;
4895
4896 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004897 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004898
David Howells1d1fe1e2008-02-07 00:15:37 -08004899 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4900 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004901 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004902 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004903 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4904 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4905 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004906 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004907 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4908 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4909 }
4910 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004911
Theodore Ts'o353eb832011-01-10 12:18:25 -05004912 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004913 ei->i_dir_start_lookup = 0;
4914 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4915 /* We now have enough fields to check if the inode was active or not.
4916 * This is needed because nfsd might try to access dead inodes
4917 * the test is that same one that e2fsck uses
4918 * NeilBrown 1999oct15
4919 */
4920 if (inode->i_nlink == 0) {
4921 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004922 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004923 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004924 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004925 goto bad_inode;
4926 }
4927 /* The only unlinked inodes we let through here have
4928 * valid i_mode and are being read by the orphan
4929 * recovery code: that's fine, we're about to complete
4930 * the process of deleting those. */
4931 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004932 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004933 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004934 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004935 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004936 ei->i_file_acl |=
4937 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004938 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004939 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004940#ifdef CONFIG_QUOTA
4941 ei->i_reserved_quota = 0;
4942#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004943 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4944 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004945 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004946 /*
4947 * NOTE! The in-memory inode i_data array is in little-endian order
4948 * even on big-endian machines: we do NOT byteswap the block numbers!
4949 */
Mingming Cao617ba132006-10-11 01:20:53 -07004950 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004951 ei->i_data[block] = raw_inode->i_block[block];
4952 INIT_LIST_HEAD(&ei->i_orphan);
4953
Jan Karab436b9b2009-12-08 23:51:10 -05004954 /*
4955 * Set transaction id's of transactions that have to be committed
4956 * to finish f[data]sync. We set them to currently running transaction
4957 * as we cannot be sure that the inode or some of its metadata isn't
4958 * part of the transaction - the inode could have been reclaimed and
4959 * now it is reread from disk.
4960 */
4961 if (journal) {
4962 transaction_t *transaction;
4963 tid_t tid;
4964
Theodore Ts'oa931da62010-08-03 21:35:12 -04004965 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004966 if (journal->j_running_transaction)
4967 transaction = journal->j_running_transaction;
4968 else
4969 transaction = journal->j_committing_transaction;
4970 if (transaction)
4971 tid = transaction->t_tid;
4972 else
4973 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004974 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004975 ei->i_sync_tid = tid;
4976 ei->i_datasync_tid = tid;
4977 }
4978
Eric Sandeen0040d982008-02-05 22:36:43 -05004979 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004980 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004981 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004982 EXT4_INODE_SIZE(inode->i_sb)) {
David Howells1d1fe1e2008-02-07 00:15:37 -08004983 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004984 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004985 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004986 if (ei->i_extra_isize == 0) {
4987 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004988 ei->i_extra_isize = sizeof(struct ext4_inode) -
4989 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004990 } else {
4991 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004992 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004993 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004994 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004995 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004996 }
4997 } else
4998 ei->i_extra_isize = 0;
4999
Kalpak Shahef7f3832007-07-18 09:15:20 -04005000 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
5001 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
5002 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
5003 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
5004
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005005 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
5006 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
5007 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5008 inode->i_version |=
5009 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
5010 }
5011
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04005012 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04005013 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05005014 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04005015 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
5016 ei->i_file_acl);
Theodore Ts'o485c26e2009-04-24 13:43:20 -04005017 ret = -EIO;
5018 goto bad_inode;
Theodore Ts'o07a03822010-06-14 09:54:48 -04005019 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04005020 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
5021 (S_ISLNK(inode->i_mode) &&
5022 !ext4_inode_is_fast_symlink(inode)))
5023 /* Validate extent which is part of inode */
5024 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005025 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04005026 (S_ISLNK(inode->i_mode) &&
5027 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005028 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04005029 ret = ext4_check_inode_blockref(inode);
5030 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05005031 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005032 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04005033
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005034 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07005035 inode->i_op = &ext4_file_inode_operations;
5036 inode->i_fop = &ext4_file_operations;
5037 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005038 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07005039 inode->i_op = &ext4_dir_inode_operations;
5040 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005041 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00005042 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07005043 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00005044 nd_terminate_link(ei->i_data, inode->i_size,
5045 sizeof(ei->i_data) - 1);
5046 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07005047 inode->i_op = &ext4_symlink_inode_operations;
5048 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005049 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04005050 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
5051 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07005052 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005053 if (raw_inode->i_block[0])
5054 init_special_inode(inode, inode->i_mode,
5055 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
5056 else
5057 init_special_inode(inode, inode->i_mode,
5058 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04005059 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04005060 ret = -EIO;
Theodore Ts'o24676da2010-05-16 21:00:00 -04005061 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04005062 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005063 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005064 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005065 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08005066 unlock_new_inode(inode);
5067 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005068
5069bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05005070 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08005071 iget_failed(inode);
5072 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005073}
5074
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005075static int ext4_inode_blocks_set(handle_t *handle,
5076 struct ext4_inode *raw_inode,
5077 struct ext4_inode_info *ei)
5078{
5079 struct inode *inode = &(ei->vfs_inode);
5080 u64 i_blocks = inode->i_blocks;
5081 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005082
5083 if (i_blocks <= ~0U) {
5084 /*
5085 * i_blocks can be represnted in a 32 bit variable
5086 * as multiple of 512 bytes
5087 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005088 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005089 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04005090 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04005091 return 0;
5092 }
5093 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
5094 return -EFBIG;
5095
5096 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005097 /*
5098 * i_blocks can be represented in a 48 bit variable
5099 * as multiple of 512 bytes
5100 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005101 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005102 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04005103 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005104 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04005105 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05005106 /* i_block is stored in file system block size */
5107 i_blocks = i_blocks >> (inode->i_blkbits - 9);
5108 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
5109 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005110 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04005111 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005112}
5113
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005114/*
5115 * Post the struct inode info into an on-disk inode location in the
5116 * buffer-cache. This gobbles the caller's reference to the
5117 * buffer_head in the inode location struct.
5118 *
5119 * The caller must have write access to iloc->bh.
5120 */
Mingming Cao617ba132006-10-11 01:20:53 -07005121static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005122 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04005123 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005124{
Mingming Cao617ba132006-10-11 01:20:53 -07005125 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
5126 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005127 struct buffer_head *bh = iloc->bh;
5128 int err = 0, rc, block;
5129
5130 /* For fields not not tracking in the in-memory inode,
5131 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005132 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07005133 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005134
Jan Karaff9ddf72007-07-18 09:24:20 -04005135 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005136 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005137 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005138 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
5139 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
5140/*
5141 * Fix up interoperability with old kernels. Otherwise, old inodes get
5142 * re-used with the upper 16 bits of the uid/gid intact
5143 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005144 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005145 raw_inode->i_uid_high =
5146 cpu_to_le16(high_16_bits(inode->i_uid));
5147 raw_inode->i_gid_high =
5148 cpu_to_le16(high_16_bits(inode->i_gid));
5149 } else {
5150 raw_inode->i_uid_high = 0;
5151 raw_inode->i_gid_high = 0;
5152 }
5153 } else {
5154 raw_inode->i_uid_low =
5155 cpu_to_le16(fs_high2lowuid(inode->i_uid));
5156 raw_inode->i_gid_low =
5157 cpu_to_le16(fs_high2lowgid(inode->i_gid));
5158 raw_inode->i_uid_high = 0;
5159 raw_inode->i_gid_high = 0;
5160 }
5161 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04005162
5163 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5164 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5165 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5166 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5167
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005168 if (ext4_inode_blocks_set(handle, raw_inode, ei))
5169 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005170 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05005171 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07005172 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
5173 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005174 raw_inode->i_file_acl_high =
5175 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005176 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005177 ext4_isize_set(raw_inode, ei->i_disksize);
5178 if (ei->i_disksize > 0x7fffffffULL) {
5179 struct super_block *sb = inode->i_sb;
5180 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
5181 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
5182 EXT4_SB(sb)->s_es->s_rev_level ==
5183 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
5184 /* If this is the first large file
5185 * created, add a flag to the superblock.
5186 */
5187 err = ext4_journal_get_write_access(handle,
5188 EXT4_SB(sb)->s_sbh);
5189 if (err)
5190 goto out_brelse;
5191 ext4_update_dynamic_rev(sb);
5192 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07005193 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005194 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05005195 ext4_handle_sync(handle);
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005196 err = ext4_handle_dirty_metadata(handle, NULL,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005197 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005198 }
5199 }
5200 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5201 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5202 if (old_valid_dev(inode->i_rdev)) {
5203 raw_inode->i_block[0] =
5204 cpu_to_le32(old_encode_dev(inode->i_rdev));
5205 raw_inode->i_block[1] = 0;
5206 } else {
5207 raw_inode->i_block[0] = 0;
5208 raw_inode->i_block[1] =
5209 cpu_to_le32(new_encode_dev(inode->i_rdev));
5210 raw_inode->i_block[2] = 0;
5211 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005212 } else
5213 for (block = 0; block < EXT4_N_BLOCKS; block++)
5214 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005215
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005216 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
5217 if (ei->i_extra_isize) {
5218 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5219 raw_inode->i_version_hi =
5220 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005221 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005222 }
5223
Frank Mayhar830156c2009-09-29 10:07:47 -04005224 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005225 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04005226 if (!err)
5227 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005228 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005229
Jan Karab436b9b2009-12-08 23:51:10 -05005230 ext4_update_inode_fsync_trans(handle, inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005231out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005232 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005233 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005234 return err;
5235}
5236
5237/*
Mingming Cao617ba132006-10-11 01:20:53 -07005238 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005239 *
5240 * We are called from a few places:
5241 *
5242 * - Within generic_file_write() for O_SYNC files.
5243 * Here, there will be no transaction running. We wait for any running
5244 * trasnaction to commit.
5245 *
5246 * - Within sys_sync(), kupdate and such.
5247 * We wait on commit, if tol to.
5248 *
5249 * - Within prune_icache() (PF_MEMALLOC == true)
5250 * Here we simply return. We can't afford to block kswapd on the
5251 * journal commit.
5252 *
5253 * In all cases it is actually safe for us to return without doing anything,
5254 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07005255 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005256 * knfsd.
5257 *
5258 * Note that we are absolutely dependent upon all inode dirtiers doing the
5259 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5260 * which we are interested.
5261 *
5262 * It would be a bug for them to not do this. The code:
5263 *
5264 * mark_inode_dirty(inode)
5265 * stuff();
5266 * inode->i_size = expr;
5267 *
5268 * is in error because a kswapd-driven write_inode() could occur while
5269 * `stuff()' is running, and the new i_size will be lost. Plus the inode
5270 * will no longer be on the superblock's dirty inode list.
5271 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005272int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005273{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005274 int err;
5275
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005276 if (current->flags & PF_MEMALLOC)
5277 return 0;
5278
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005279 if (EXT4_SB(inode->i_sb)->s_journal) {
5280 if (ext4_journal_current_handle()) {
5281 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5282 dump_stack();
5283 return -EIO;
5284 }
5285
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005286 if (wbc->sync_mode != WB_SYNC_ALL)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005287 return 0;
5288
5289 err = ext4_force_commit(inode->i_sb);
5290 } else {
5291 struct ext4_iloc iloc;
5292
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005293 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005294 if (err)
5295 return err;
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005296 if (wbc->sync_mode == WB_SYNC_ALL)
Frank Mayhar830156c2009-09-29 10:07:47 -04005297 sync_dirty_buffer(iloc.bh);
5298 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04005299 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
5300 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005301 err = -EIO;
5302 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005303 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005304 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005305 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005306}
5307
5308/*
Mingming Cao617ba132006-10-11 01:20:53 -07005309 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005310 *
5311 * Called from notify_change.
5312 *
5313 * We want to trap VFS attempts to truncate the file as soon as
5314 * possible. In particular, we want to make sure that when the VFS
5315 * shrinks i_size, we put the inode on the orphan list and modify
5316 * i_disksize immediately, so that during the subsequent flushing of
5317 * dirty pages and freeing of disk blocks, we can guarantee that any
5318 * commit will leave the blocks being flushed in an unused state on
5319 * disk. (On recovery, the inode will get truncated and the blocks will
5320 * be freed, so we have a strong guarantee that no future commit will
5321 * leave these blocks visible to the user.)
5322 *
Jan Kara678aaf42008-07-11 19:27:31 -04005323 * Another thing we have to assure is that if we are in ordered mode
5324 * and inode is still attached to the committing transaction, we must
5325 * we start writeout of all the dirty pages which are being truncated.
5326 * This way we are sure that all the data written in the previous
5327 * transaction are already on disk (truncate waits for pages under
5328 * writeback).
5329 *
5330 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005331 */
Mingming Cao617ba132006-10-11 01:20:53 -07005332int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005333{
5334 struct inode *inode = dentry->d_inode;
5335 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005336 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005337 const unsigned int ia_valid = attr->ia_valid;
5338
5339 error = inode_change_ok(inode, attr);
5340 if (error)
5341 return error;
5342
Dmitry Monakhov12755622010-04-08 22:04:20 +04005343 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05005344 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005345 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
5346 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
5347 handle_t *handle;
5348
5349 /* (user+group)*(old+new) structure, inode write (sb,
5350 * inode block, ? - but truncate inode update has it) */
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05005351 handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
Dmitry Monakhov194074a2009-12-08 22:42:28 -05005352 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005353 if (IS_ERR(handle)) {
5354 error = PTR_ERR(handle);
5355 goto err_out;
5356 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005357 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005358 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005359 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005360 return error;
5361 }
5362 /* Update corresponding info in inode so that everything is in
5363 * one transaction */
5364 if (attr->ia_valid & ATTR_UID)
5365 inode->i_uid = attr->ia_uid;
5366 if (attr->ia_valid & ATTR_GID)
5367 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005368 error = ext4_mark_inode_dirty(handle, inode);
5369 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005370 }
5371
Eric Sandeene2b46572008-01-28 23:58:27 -05005372 if (attr->ia_valid & ATTR_SIZE) {
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005373 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005374 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5375
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005376 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5377 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005378 }
5379 }
5380
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005381 if (S_ISREG(inode->i_mode) &&
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05005382 attr->ia_valid & ATTR_SIZE &&
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005383 (attr->ia_size < inode->i_size)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005384 handle_t *handle;
5385
Mingming Cao617ba132006-10-11 01:20:53 -07005386 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005387 if (IS_ERR(handle)) {
5388 error = PTR_ERR(handle);
5389 goto err_out;
5390 }
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005391 if (ext4_handle_valid(handle)) {
5392 error = ext4_orphan_add(handle, inode);
5393 orphan = 1;
5394 }
Mingming Cao617ba132006-10-11 01:20:53 -07005395 EXT4_I(inode)->i_disksize = attr->ia_size;
5396 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005397 if (!error)
5398 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07005399 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04005400
5401 if (ext4_should_order_data(inode)) {
5402 error = ext4_begin_ordered_truncate(inode,
5403 attr->ia_size);
5404 if (error) {
5405 /* Do as much error cleanup as possible */
5406 handle = ext4_journal_start(inode, 3);
5407 if (IS_ERR(handle)) {
5408 ext4_orphan_del(NULL, inode);
5409 goto err_out;
5410 }
5411 ext4_orphan_del(handle, inode);
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005412 orphan = 0;
Jan Kara678aaf42008-07-11 19:27:31 -04005413 ext4_journal_stop(handle);
5414 goto err_out;
5415 }
5416 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005417 }
5418
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005419 if (attr->ia_valid & ATTR_SIZE) {
5420 if (attr->ia_size != i_size_read(inode)) {
5421 truncate_setsize(inode, attr->ia_size);
5422 ext4_truncate(inode);
5423 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
5424 ext4_truncate(inode);
5425 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005426
Christoph Hellwig10257742010-06-04 11:30:02 +02005427 if (!rc) {
5428 setattr_copy(inode, attr);
5429 mark_inode_dirty(inode);
5430 }
5431
5432 /*
5433 * If the call to ext4_truncate failed to get a transaction handle at
5434 * all, we need to clean up the in-core orphan list manually.
5435 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005436 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005437 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005438
5439 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07005440 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005441
5442err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005443 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005444 if (!error)
5445 error = rc;
5446 return error;
5447}
5448
Mingming Cao3e3398a2008-07-11 19:27:31 -04005449int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5450 struct kstat *stat)
5451{
5452 struct inode *inode;
5453 unsigned long delalloc_blocks;
5454
5455 inode = dentry->d_inode;
5456 generic_fillattr(inode, stat);
5457
5458 /*
5459 * We can't update i_blocks if the block allocation is delayed
5460 * otherwise in the case of system crash before the real block
5461 * allocation is done, we will have i_blocks inconsistent with
5462 * on-disk file blocks.
5463 * We always keep i_blocks updated together with real
5464 * allocation. But to not confuse with user, stat
5465 * will return the blocks that include the delayed allocation
5466 * blocks for this file.
5467 */
Mingming Cao3e3398a2008-07-11 19:27:31 -04005468 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005469
5470 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
5471 return 0;
5472}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005473
Mingming Caoa02908f2008-08-19 22:16:07 -04005474static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
5475 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005476{
Mingming Caoa02908f2008-08-19 22:16:07 -04005477 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005478
Mingming Caoa02908f2008-08-19 22:16:07 -04005479 /* if nrblocks are contiguous */
5480 if (chunk) {
5481 /*
Yongqiang Yang5b413952011-04-04 15:40:24 -04005482 * With N contiguous data blocks, we need at most
5483 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
5484 * 2 dindirect blocks, and 1 tindirect block
Mingming Caoa02908f2008-08-19 22:16:07 -04005485 */
Yongqiang Yang5b413952011-04-04 15:40:24 -04005486 return DIV_ROUND_UP(nrblocks,
5487 EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
Mingming Caoa02908f2008-08-19 22:16:07 -04005488 }
5489 /*
5490 * if nrblocks are not contiguous, worse case, each block touch
5491 * a indirect block, and each indirect block touch a double indirect
5492 * block, plus a triple indirect block
5493 */
5494 indirects = nrblocks * 2 + 1;
5495 return indirects;
5496}
Alex Tomasa86c6182006-10-11 01:21:03 -07005497
Mingming Caoa02908f2008-08-19 22:16:07 -04005498static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
5499{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005500 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Theodore Ts'oac51d832008-11-06 16:49:36 -05005501 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
5502 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04005503}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005504
Mingming Caoa02908f2008-08-19 22:16:07 -04005505/*
5506 * Account for index blocks, block groups bitmaps and block group
5507 * descriptor blocks if modify datablocks and index blocks
5508 * worse case, the indexs blocks spread over different block groups
5509 *
5510 * If datablocks are discontiguous, they are possible to spread over
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005511 * different block groups too. If they are contiuguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005512 * they could still across block group boundary.
5513 *
5514 * Also account for superblock, inode, quota and xattr blocks
5515 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04005516static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoa02908f2008-08-19 22:16:07 -04005517{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005518 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5519 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005520 int idxblocks;
5521 int ret = 0;
5522
5523 /*
5524 * How many index blocks need to touch to modify nrblocks?
5525 * The "Chunk" flag indicating whether the nrblocks is
5526 * physically contiguous on disk
5527 *
5528 * For Direct IO and fallocate, they calls get_block to allocate
5529 * one single extent at a time, so they could set the "Chunk" flag
5530 */
5531 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
5532
5533 ret = idxblocks;
5534
5535 /*
5536 * Now let's see how many group bitmaps and group descriptors need
5537 * to account
5538 */
5539 groups = idxblocks;
5540 if (chunk)
5541 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005542 else
Mingming Caoa02908f2008-08-19 22:16:07 -04005543 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005544
Mingming Caoa02908f2008-08-19 22:16:07 -04005545 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005546 if (groups > ngroups)
5547 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005548 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5549 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5550
5551 /* bitmaps and block group descriptor blocks */
5552 ret += groups + gdpblocks;
5553
5554 /* Blocks for super block, inode, quota and xattr blocks */
5555 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005556
5557 return ret;
5558}
5559
5560/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005561 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005562 * the modification of a single pages into a single transaction,
5563 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005564 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005565 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005566 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005567 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005568 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005569 */
5570int ext4_writepage_trans_blocks(struct inode *inode)
5571{
5572 int bpp = ext4_journal_blocks_per_page(inode);
5573 int ret;
5574
5575 ret = ext4_meta_trans_blocks(inode, bpp, 0);
5576
5577 /* Account for data blocks for journalled mode */
5578 if (ext4_should_journal_data(inode))
5579 ret += bpp;
5580 return ret;
5581}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005582
5583/*
5584 * Calculate the journal credits for a chunk of data modification.
5585 *
5586 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005587 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005588 *
5589 * journal buffers for data blocks are not included here, as DIO
5590 * and fallocate do no need to journal data buffers.
5591 */
5592int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5593{
5594 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5595}
5596
Mingming Caoa02908f2008-08-19 22:16:07 -04005597/*
Mingming Cao617ba132006-10-11 01:20:53 -07005598 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005599 * Give this, we know that the caller already has write access to iloc->bh.
5600 */
Mingming Cao617ba132006-10-11 01:20:53 -07005601int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005602 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005603{
5604 int err = 0;
5605
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005606 if (test_opt(inode->i_sb, I_VERSION))
5607 inode_inc_iversion(inode);
5608
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005609 /* the do_update_inode consumes one bh->b_count */
5610 get_bh(iloc->bh);
5611
Mingming Caodab291a2006-10-11 01:21:01 -07005612 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005613 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005614 put_bh(iloc->bh);
5615 return err;
5616}
5617
5618/*
5619 * On success, We end up with an outstanding reference count against
5620 * iloc->bh. This _must_ be cleaned up later.
5621 */
5622
5623int
Mingming Cao617ba132006-10-11 01:20:53 -07005624ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5625 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005626{
Frank Mayhar03901312009-01-07 00:06:22 -05005627 int err;
5628
5629 err = ext4_get_inode_loc(inode, iloc);
5630 if (!err) {
5631 BUFFER_TRACE(iloc->bh, "get_write_access");
5632 err = ext4_journal_get_write_access(handle, iloc->bh);
5633 if (err) {
5634 brelse(iloc->bh);
5635 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005636 }
5637 }
Mingming Cao617ba132006-10-11 01:20:53 -07005638 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005639 return err;
5640}
5641
5642/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005643 * Expand an inode by new_extra_isize bytes.
5644 * Returns 0 on success or negative error number on failure.
5645 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005646static int ext4_expand_extra_isize(struct inode *inode,
5647 unsigned int new_extra_isize,
5648 struct ext4_iloc iloc,
5649 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005650{
5651 struct ext4_inode *raw_inode;
5652 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005653
5654 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5655 return 0;
5656
5657 raw_inode = ext4_raw_inode(&iloc);
5658
5659 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005660
5661 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005662 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5663 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005664 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5665 new_extra_isize);
5666 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5667 return 0;
5668 }
5669
5670 /* try to expand with EAs present */
5671 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5672 raw_inode, handle);
5673}
5674
5675/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005676 * What we do here is to mark the in-core inode as clean with respect to inode
5677 * dirtiness (it may still be data-dirty).
5678 * This means that the in-core inode may be reaped by prune_icache
5679 * without having to perform any I/O. This is a very good thing,
5680 * because *any* task may call prune_icache - even ones which
5681 * have a transaction open against a different journal.
5682 *
5683 * Is this cheating? Not really. Sure, we haven't written the
5684 * inode out, but prune_icache isn't a user-visible syncing function.
5685 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5686 * we start and wait on commits.
5687 *
5688 * Is this efficient/effective? Well, we're being nice to the system
5689 * by cleaning up our inodes proactively so they can be reaped
5690 * without I/O. But we are potentially leaving up to five seconds'
5691 * worth of inodes floating about which prune_icache wants us to
5692 * write out. One way to fix that would be to get prune_icache()
5693 * to do a write_super() to free up some memory. It has the desired
5694 * effect.
5695 */
Mingming Cao617ba132006-10-11 01:20:53 -07005696int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005697{
Mingming Cao617ba132006-10-11 01:20:53 -07005698 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005699 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5700 static unsigned int mnt_count;
5701 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005702
5703 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005704 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005705 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005706 if (ext4_handle_valid(handle) &&
5707 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005708 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005709 /*
5710 * We need extra buffer credits since we may write into EA block
5711 * with this same handle. If journal_extend fails, then it will
5712 * only result in a minor loss of functionality for that inode.
5713 * If this is felt to be critical, then e2fsck should be run to
5714 * force a large enough s_min_extra_isize.
5715 */
5716 if ((jbd2_journal_extend(handle,
5717 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5718 ret = ext4_expand_extra_isize(inode,
5719 sbi->s_want_extra_isize,
5720 iloc, handle);
5721 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005722 ext4_set_inode_state(inode,
5723 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005724 if (mnt_count !=
5725 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05005726 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005727 "Unable to expand inode %lu. Delete"
5728 " some EAs or run e2fsck.",
5729 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005730 mnt_count =
5731 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005732 }
5733 }
5734 }
5735 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005736 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005737 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005738 return err;
5739}
5740
5741/*
Mingming Cao617ba132006-10-11 01:20:53 -07005742 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005743 *
5744 * We're really interested in the case where a file is being extended.
5745 * i_size has been changed by generic_commit_write() and we thus need
5746 * to include the updated inode in the current transaction.
5747 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005748 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005749 * are allocated to the file.
5750 *
5751 * If the inode is marked synchronous, we don't honour that here - doing
5752 * so would cause a commit on atime updates, which we don't bother doing.
5753 * We handle synchronous inodes at the highest possible level.
5754 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005755void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005756{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005757 handle_t *handle;
5758
Mingming Cao617ba132006-10-11 01:20:53 -07005759 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005760 if (IS_ERR(handle))
5761 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005762
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005763 ext4_mark_inode_dirty(handle, inode);
5764
Mingming Cao617ba132006-10-11 01:20:53 -07005765 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005766out:
5767 return;
5768}
5769
5770#if 0
5771/*
5772 * Bind an inode's backing buffer_head into this transaction, to prevent
5773 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005774 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005775 * returns no iloc structure, so the caller needs to repeat the iloc
5776 * lookup to mark the inode dirty later.
5777 */
Mingming Cao617ba132006-10-11 01:20:53 -07005778static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005779{
Mingming Cao617ba132006-10-11 01:20:53 -07005780 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005781
5782 int err = 0;
5783 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005784 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005785 if (!err) {
5786 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005787 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005788 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005789 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005790 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05005791 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005792 brelse(iloc.bh);
5793 }
5794 }
Mingming Cao617ba132006-10-11 01:20:53 -07005795 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005796 return err;
5797}
5798#endif
5799
Mingming Cao617ba132006-10-11 01:20:53 -07005800int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005801{
5802 journal_t *journal;
5803 handle_t *handle;
5804 int err;
5805
5806 /*
5807 * We have to be very careful here: changing a data block's
5808 * journaling status dynamically is dangerous. If we write a
5809 * data block to the journal, change the status and then delete
5810 * that block, we risk forgetting to revoke the old log record
5811 * from the journal and so a subsequent replay can corrupt data.
5812 * So, first we make sure that the journal is empty and that
5813 * nobody is changing anything.
5814 */
5815
Mingming Cao617ba132006-10-11 01:20:53 -07005816 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005817 if (!journal)
5818 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005819 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005820 return -EROFS;
5821
Mingming Caodab291a2006-10-11 01:21:01 -07005822 jbd2_journal_lock_updates(journal);
5823 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005824
5825 /*
5826 * OK, there are no updates running now, and all cached data is
5827 * synced to disk. We are now in a completely consistent state
5828 * which doesn't have anything in the journal, and we know that
5829 * no filesystem updates are running, so it is safe to modify
5830 * the inode's in-core data-journaling state flag now.
5831 */
5832
5833 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005834 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005835 else
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005836 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07005837 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005838
Mingming Caodab291a2006-10-11 01:21:01 -07005839 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005840
5841 /* Finally we can mark the inode as dirty. */
5842
Mingming Cao617ba132006-10-11 01:20:53 -07005843 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005844 if (IS_ERR(handle))
5845 return PTR_ERR(handle);
5846
Mingming Cao617ba132006-10-11 01:20:53 -07005847 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005848 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005849 ext4_journal_stop(handle);
5850 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005851
5852 return err;
5853}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005854
5855static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5856{
5857 return !buffer_mapped(bh);
5858}
5859
Nick Pigginc2ec1752009-03-31 15:23:21 -07005860int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005861{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005862 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005863 loff_t size;
5864 unsigned long len;
5865 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005866 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005867 struct file *file = vma->vm_file;
5868 struct inode *inode = file->f_path.dentry->d_inode;
5869 struct address_space *mapping = inode->i_mapping;
5870
5871 /*
5872 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5873 * get i_mutex because we are already holding mmap_sem.
5874 */
5875 down_read(&inode->i_alloc_sem);
5876 size = i_size_read(inode);
5877 if (page->mapping != mapping || size <= page_offset(page)
5878 || !PageUptodate(page)) {
5879 /* page got truncated from under us? */
5880 goto out_unlock;
5881 }
5882 ret = 0;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005883
5884 lock_page(page);
5885 wait_on_page_writeback(page);
5886 if (PageMappedToDisk(page)) {
5887 up_read(&inode->i_alloc_sem);
5888 return VM_FAULT_LOCKED;
5889 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005890
5891 if (page->index == size >> PAGE_CACHE_SHIFT)
5892 len = size & ~PAGE_CACHE_MASK;
5893 else
5894 len = PAGE_CACHE_SIZE;
5895
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005896 /*
5897 * return if we have all the buffers mapped. This avoid
5898 * the need to call write_begin/write_end which does a
5899 * journal_start/journal_stop which can block and take
5900 * long time
5901 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005902 if (page_has_buffers(page)) {
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005903 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005904 ext4_bh_unmapped)) {
Darrick J. Wong0e499892011-05-18 13:55:20 -04005905 up_read(&inode->i_alloc_sem);
5906 return VM_FAULT_LOCKED;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005907 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005908 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005909 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005910 /*
5911 * OK, we need to fill the hole... Do write_begin write_end
5912 * to do block allocation/reservation.We are not holding
5913 * inode.i__mutex here. That allow * parallel write_begin,
5914 * write_end call. lock_page prevent this from happening
5915 * on the same page though
5916 */
5917 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005918 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005919 if (ret < 0)
5920 goto out_unlock;
5921 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005922 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005923 if (ret < 0)
5924 goto out_unlock;
5925 ret = 0;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005926
5927 /*
5928 * write_begin/end might have created a dirty page and someone
5929 * could wander in and start the IO. Make sure that hasn't
5930 * happened.
5931 */
5932 lock_page(page);
5933 wait_on_page_writeback(page);
5934 up_read(&inode->i_alloc_sem);
5935 return VM_FAULT_LOCKED;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005936out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005937 if (ret)
5938 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005939 up_read(&inode->i_alloc_sem);
5940 return ret;
5941}