xref: /openbmc/linux/fs/ext4/inode.c (revision 4eec708d263f0ee10861d69251708a225b64cac7)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  *  linux/fs/ext4/inode.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
5ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
6ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
7ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
8ac27a0ecSDave Kleikamp  *
9ac27a0ecSDave Kleikamp  *  from
10ac27a0ecSDave Kleikamp  *
11ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
12ac27a0ecSDave Kleikamp  *
13ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
14ac27a0ecSDave Kleikamp  *
15ac27a0ecSDave Kleikamp  *  64-bit file support on 64-bit platforms by Jakub Jelinek
16ac27a0ecSDave Kleikamp  *	(jj@sunsite.ms.mff.cuni.cz)
17ac27a0ecSDave Kleikamp  *
18617ba13bSMingming Cao  *  Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
19ac27a0ecSDave Kleikamp  */
20ac27a0ecSDave Kleikamp 
21ac27a0ecSDave Kleikamp #include <linux/fs.h>
22ac27a0ecSDave Kleikamp #include <linux/time.h>
23dab291afSMingming Cao #include <linux/jbd2.h>
24ac27a0ecSDave Kleikamp #include <linux/highuid.h>
25ac27a0ecSDave Kleikamp #include <linux/pagemap.h>
26ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
27ac27a0ecSDave Kleikamp #include <linux/string.h>
28ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
29ac27a0ecSDave Kleikamp #include <linux/writeback.h>
3064769240SAlex Tomas #include <linux/pagevec.h>
31ac27a0ecSDave Kleikamp #include <linux/mpage.h>
32e83c1397SDuane Griffin #include <linux/namei.h>
33ac27a0ecSDave Kleikamp #include <linux/uio.h>
34ac27a0ecSDave Kleikamp #include <linux/bio.h>
354c0425ffSMingming Cao #include <linux/workqueue.h>
36744692dcSJiaying Zhang #include <linux/kernel.h>
376db26ffcSAndrew Morton #include <linux/printk.h>
385a0e3ad6STejun Heo #include <linux/slab.h>
39a8901d34STheodore Ts'o #include <linux/ratelimit.h>
409bffad1eSTheodore Ts'o 
413dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
42ac27a0ecSDave Kleikamp #include "xattr.h"
43ac27a0ecSDave Kleikamp #include "acl.h"
449f125d64STheodore Ts'o #include "truncate.h"
45ac27a0ecSDave Kleikamp 
469bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
479bffad1eSTheodore Ts'o 
48a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01
49a1d6cc56SAneesh Kumar K.V 
50814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
51814525f4SDarrick J. Wong 			      struct ext4_inode_info *ei)
52814525f4SDarrick J. Wong {
53814525f4SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
54814525f4SDarrick J. Wong 	__u16 csum_lo;
55814525f4SDarrick J. Wong 	__u16 csum_hi = 0;
56814525f4SDarrick J. Wong 	__u32 csum;
57814525f4SDarrick J. Wong 
58171a7f21SDmitry Monakhov 	csum_lo = le16_to_cpu(raw->i_checksum_lo);
59814525f4SDarrick J. Wong 	raw->i_checksum_lo = 0;
60814525f4SDarrick J. Wong 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
61814525f4SDarrick J. Wong 	    EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
62171a7f21SDmitry Monakhov 		csum_hi = le16_to_cpu(raw->i_checksum_hi);
63814525f4SDarrick J. Wong 		raw->i_checksum_hi = 0;
64814525f4SDarrick J. Wong 	}
65814525f4SDarrick J. Wong 
66814525f4SDarrick J. Wong 	csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
67814525f4SDarrick J. Wong 			   EXT4_INODE_SIZE(inode->i_sb));
68814525f4SDarrick J. Wong 
69171a7f21SDmitry Monakhov 	raw->i_checksum_lo = cpu_to_le16(csum_lo);
70814525f4SDarrick J. Wong 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
71814525f4SDarrick J. Wong 	    EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
72171a7f21SDmitry Monakhov 		raw->i_checksum_hi = cpu_to_le16(csum_hi);
73814525f4SDarrick J. Wong 
74814525f4SDarrick J. Wong 	return csum;
75814525f4SDarrick J. Wong }
76814525f4SDarrick J. Wong 
77814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
78814525f4SDarrick J. Wong 				  struct ext4_inode_info *ei)
79814525f4SDarrick J. Wong {
80814525f4SDarrick J. Wong 	__u32 provided, calculated;
81814525f4SDarrick J. Wong 
82814525f4SDarrick J. Wong 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
83814525f4SDarrick J. Wong 	    cpu_to_le32(EXT4_OS_LINUX) ||
84814525f4SDarrick J. Wong 	    !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
85814525f4SDarrick J. Wong 		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
86814525f4SDarrick J. Wong 		return 1;
87814525f4SDarrick J. Wong 
88814525f4SDarrick J. Wong 	provided = le16_to_cpu(raw->i_checksum_lo);
89814525f4SDarrick J. Wong 	calculated = ext4_inode_csum(inode, raw, ei);
90814525f4SDarrick J. Wong 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
91814525f4SDarrick J. Wong 	    EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
92814525f4SDarrick J. Wong 		provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
93814525f4SDarrick J. Wong 	else
94814525f4SDarrick J. Wong 		calculated &= 0xFFFF;
95814525f4SDarrick J. Wong 
96814525f4SDarrick J. Wong 	return provided == calculated;
97814525f4SDarrick J. Wong }
98814525f4SDarrick J. Wong 
99814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
100814525f4SDarrick J. Wong 				struct ext4_inode_info *ei)
101814525f4SDarrick J. Wong {
102814525f4SDarrick J. Wong 	__u32 csum;
103814525f4SDarrick J. Wong 
104814525f4SDarrick J. Wong 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
105814525f4SDarrick J. Wong 	    cpu_to_le32(EXT4_OS_LINUX) ||
106814525f4SDarrick J. Wong 	    !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
107814525f4SDarrick J. Wong 		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
108814525f4SDarrick J. Wong 		return;
109814525f4SDarrick J. Wong 
110814525f4SDarrick J. Wong 	csum = ext4_inode_csum(inode, raw, ei);
111814525f4SDarrick J. Wong 	raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
112814525f4SDarrick J. Wong 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
113814525f4SDarrick J. Wong 	    EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
114814525f4SDarrick J. Wong 		raw->i_checksum_hi = cpu_to_le16(csum >> 16);
115814525f4SDarrick J. Wong }
116814525f4SDarrick J. Wong 
117678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode,
118678aaf48SJan Kara 					      loff_t new_size)
119678aaf48SJan Kara {
1207ff9c073STheodore Ts'o 	trace_ext4_begin_ordered_truncate(inode, new_size);
1218aefcd55STheodore Ts'o 	/*
1228aefcd55STheodore Ts'o 	 * If jinode is zero, then we never opened the file for
1238aefcd55STheodore Ts'o 	 * writing, so there's no need to call
1248aefcd55STheodore Ts'o 	 * jbd2_journal_begin_ordered_truncate() since there's no
1258aefcd55STheodore Ts'o 	 * outstanding writes we need to flush.
1268aefcd55STheodore Ts'o 	 */
1278aefcd55STheodore Ts'o 	if (!EXT4_I(inode)->jinode)
1288aefcd55STheodore Ts'o 		return 0;
1298aefcd55STheodore Ts'o 	return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
1308aefcd55STheodore Ts'o 						   EXT4_I(inode)->jinode,
131678aaf48SJan Kara 						   new_size);
132678aaf48SJan Kara }
133678aaf48SJan Kara 
13464769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset);
135cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len);
136cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
1375f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
1385f163cc7SEric Sandeen 		struct inode *inode, struct page *page, loff_t from,
1395f163cc7SEric Sandeen 		loff_t length, int flags);
14064769240SAlex Tomas 
141ac27a0ecSDave Kleikamp /*
142ac27a0ecSDave Kleikamp  * Test whether an inode is a fast symlink.
143ac27a0ecSDave Kleikamp  */
144617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode)
145ac27a0ecSDave Kleikamp {
146617ba13bSMingming Cao 	int ea_blocks = EXT4_I(inode)->i_file_acl ?
147ac27a0ecSDave Kleikamp 		(inode->i_sb->s_blocksize >> 9) : 0;
148ac27a0ecSDave Kleikamp 
149ac27a0ecSDave Kleikamp 	return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
150ac27a0ecSDave Kleikamp }
151ac27a0ecSDave Kleikamp 
152ac27a0ecSDave Kleikamp /*
153ac27a0ecSDave Kleikamp  * Restart the transaction associated with *handle.  This does a commit,
154ac27a0ecSDave Kleikamp  * so before we call here everything must be consistently dirtied against
155ac27a0ecSDave Kleikamp  * this transaction.
156ac27a0ecSDave Kleikamp  */
157487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
158487caeefSJan Kara 				 int nblocks)
159ac27a0ecSDave Kleikamp {
160487caeefSJan Kara 	int ret;
161487caeefSJan Kara 
162487caeefSJan Kara 	/*
163e35fd660STheodore Ts'o 	 * Drop i_data_sem to avoid deadlock with ext4_map_blocks.  At this
164487caeefSJan Kara 	 * moment, get_block can be called only for blocks inside i_size since
165487caeefSJan Kara 	 * page cache has been already dropped and writes are blocked by
166487caeefSJan Kara 	 * i_mutex. So we can safely drop the i_data_sem here.
167487caeefSJan Kara 	 */
1680390131bSFrank Mayhar 	BUG_ON(EXT4_JOURNAL(inode) == NULL);
169ac27a0ecSDave Kleikamp 	jbd_debug(2, "restarting handle %p\n", handle);
170487caeefSJan Kara 	up_write(&EXT4_I(inode)->i_data_sem);
1718e8eaabeSAmir Goldstein 	ret = ext4_journal_restart(handle, nblocks);
172487caeefSJan Kara 	down_write(&EXT4_I(inode)->i_data_sem);
173fa5d1113SAneesh Kumar K.V 	ext4_discard_preallocations(inode);
174487caeefSJan Kara 
175487caeefSJan Kara 	return ret;
176ac27a0ecSDave Kleikamp }
177ac27a0ecSDave Kleikamp 
178ac27a0ecSDave Kleikamp /*
179ac27a0ecSDave Kleikamp  * Called at the last iput() if i_nlink is zero.
180ac27a0ecSDave Kleikamp  */
1810930fcc1SAl Viro void ext4_evict_inode(struct inode *inode)
182ac27a0ecSDave Kleikamp {
183ac27a0ecSDave Kleikamp 	handle_t *handle;
184bc965ab3STheodore Ts'o 	int err;
185ac27a0ecSDave Kleikamp 
1867ff9c073STheodore Ts'o 	trace_ext4_evict_inode(inode);
1872581fdc8SJiaying Zhang 
1880930fcc1SAl Viro 	if (inode->i_nlink) {
1892d859db3SJan Kara 		/*
1902d859db3SJan Kara 		 * When journalling data dirty buffers are tracked only in the
1912d859db3SJan Kara 		 * journal. So although mm thinks everything is clean and
1922d859db3SJan Kara 		 * ready for reaping the inode might still have some pages to
1932d859db3SJan Kara 		 * write in the running transaction or waiting to be
1942d859db3SJan Kara 		 * checkpointed. Thus calling jbd2_journal_invalidatepage()
1952d859db3SJan Kara 		 * (via truncate_inode_pages()) to discard these buffers can
1962d859db3SJan Kara 		 * cause data loss. Also even if we did not discard these
1972d859db3SJan Kara 		 * buffers, we would have no way to find them after the inode
1982d859db3SJan Kara 		 * is reaped and thus user could see stale data if he tries to
1992d859db3SJan Kara 		 * read them before the transaction is checkpointed. So be
2002d859db3SJan Kara 		 * careful and force everything to disk here... We use
2012d859db3SJan Kara 		 * ei->i_datasync_tid to store the newest transaction
2022d859db3SJan Kara 		 * containing inode's data.
2032d859db3SJan Kara 		 *
2042d859db3SJan Kara 		 * Note that directories do not have this problem because they
2052d859db3SJan Kara 		 * don't use page cache.
2062d859db3SJan Kara 		 */
2072d859db3SJan Kara 		if (ext4_should_journal_data(inode) &&
2082b405bfaSTheodore Ts'o 		    (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
2092b405bfaSTheodore Ts'o 		    inode->i_ino != EXT4_JOURNAL_INO) {
2102d859db3SJan Kara 			journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
2112d859db3SJan Kara 			tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
2122d859db3SJan Kara 
213d76a3a77STheodore Ts'o 			jbd2_complete_transaction(journal, commit_tid);
2142d859db3SJan Kara 			filemap_write_and_wait(&inode->i_data);
2152d859db3SJan Kara 		}
2160930fcc1SAl Viro 		truncate_inode_pages(&inode->i_data, 0);
2171ada47d9STheodore Ts'o 		ext4_ioend_shutdown(inode);
2180930fcc1SAl Viro 		goto no_delete;
2190930fcc1SAl Viro 	}
2200930fcc1SAl Viro 
221907f4554SChristoph Hellwig 	if (!is_bad_inode(inode))
222871a2931SChristoph Hellwig 		dquot_initialize(inode);
223907f4554SChristoph Hellwig 
224678aaf48SJan Kara 	if (ext4_should_order_data(inode))
225678aaf48SJan Kara 		ext4_begin_ordered_truncate(inode, 0);
226ac27a0ecSDave Kleikamp 	truncate_inode_pages(&inode->i_data, 0);
2271ada47d9STheodore Ts'o 	ext4_ioend_shutdown(inode);
228ac27a0ecSDave Kleikamp 
229ac27a0ecSDave Kleikamp 	if (is_bad_inode(inode))
230ac27a0ecSDave Kleikamp 		goto no_delete;
231ac27a0ecSDave Kleikamp 
2328e8ad8a5SJan Kara 	/*
2338e8ad8a5SJan Kara 	 * Protect us against freezing - iput() caller didn't have to have any
2348e8ad8a5SJan Kara 	 * protection against it
2358e8ad8a5SJan Kara 	 */
2368e8ad8a5SJan Kara 	sb_start_intwrite(inode->i_sb);
2379924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
2389924a92aSTheodore Ts'o 				    ext4_blocks_for_truncate(inode)+3);
239ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
240bc965ab3STheodore Ts'o 		ext4_std_error(inode->i_sb, PTR_ERR(handle));
241ac27a0ecSDave Kleikamp 		/*
242ac27a0ecSDave Kleikamp 		 * If we're going to skip the normal cleanup, we still need to
243ac27a0ecSDave Kleikamp 		 * make sure that the in-core orphan linked list is properly
244ac27a0ecSDave Kleikamp 		 * cleaned up.
245ac27a0ecSDave Kleikamp 		 */
246617ba13bSMingming Cao 		ext4_orphan_del(NULL, inode);
2478e8ad8a5SJan Kara 		sb_end_intwrite(inode->i_sb);
248ac27a0ecSDave Kleikamp 		goto no_delete;
249ac27a0ecSDave Kleikamp 	}
250ac27a0ecSDave Kleikamp 
251ac27a0ecSDave Kleikamp 	if (IS_SYNC(inode))
2520390131bSFrank Mayhar 		ext4_handle_sync(handle);
253ac27a0ecSDave Kleikamp 	inode->i_size = 0;
254bc965ab3STheodore Ts'o 	err = ext4_mark_inode_dirty(handle, inode);
255bc965ab3STheodore Ts'o 	if (err) {
25612062dddSEric Sandeen 		ext4_warning(inode->i_sb,
257bc965ab3STheodore Ts'o 			     "couldn't mark inode dirty (err %d)", err);
258bc965ab3STheodore Ts'o 		goto stop_handle;
259bc965ab3STheodore Ts'o 	}
260ac27a0ecSDave Kleikamp 	if (inode->i_blocks)
261617ba13bSMingming Cao 		ext4_truncate(inode);
262bc965ab3STheodore Ts'o 
263bc965ab3STheodore Ts'o 	/*
264bc965ab3STheodore Ts'o 	 * ext4_ext_truncate() doesn't reserve any slop when it
265bc965ab3STheodore Ts'o 	 * restarts journal transactions; therefore there may not be
266bc965ab3STheodore Ts'o 	 * enough credits left in the handle to remove the inode from
267bc965ab3STheodore Ts'o 	 * the orphan list and set the dtime field.
268bc965ab3STheodore Ts'o 	 */
2690390131bSFrank Mayhar 	if (!ext4_handle_has_enough_credits(handle, 3)) {
270bc965ab3STheodore Ts'o 		err = ext4_journal_extend(handle, 3);
271bc965ab3STheodore Ts'o 		if (err > 0)
272bc965ab3STheodore Ts'o 			err = ext4_journal_restart(handle, 3);
273bc965ab3STheodore Ts'o 		if (err != 0) {
27412062dddSEric Sandeen 			ext4_warning(inode->i_sb,
275bc965ab3STheodore Ts'o 				     "couldn't extend journal (err %d)", err);
276bc965ab3STheodore Ts'o 		stop_handle:
277bc965ab3STheodore Ts'o 			ext4_journal_stop(handle);
27845388219STheodore Ts'o 			ext4_orphan_del(NULL, inode);
2798e8ad8a5SJan Kara 			sb_end_intwrite(inode->i_sb);
280bc965ab3STheodore Ts'o 			goto no_delete;
281bc965ab3STheodore Ts'o 		}
282bc965ab3STheodore Ts'o 	}
283bc965ab3STheodore Ts'o 
284ac27a0ecSDave Kleikamp 	/*
285617ba13bSMingming Cao 	 * Kill off the orphan record which ext4_truncate created.
286ac27a0ecSDave Kleikamp 	 * AKPM: I think this can be inside the above `if'.
287617ba13bSMingming Cao 	 * Note that ext4_orphan_del() has to be able to cope with the
288ac27a0ecSDave Kleikamp 	 * deletion of a non-existent orphan - this is because we don't
289617ba13bSMingming Cao 	 * know if ext4_truncate() actually created an orphan record.
290ac27a0ecSDave Kleikamp 	 * (Well, we could do this if we need to, but heck - it works)
291ac27a0ecSDave Kleikamp 	 */
292617ba13bSMingming Cao 	ext4_orphan_del(handle, inode);
293617ba13bSMingming Cao 	EXT4_I(inode)->i_dtime	= get_seconds();
294ac27a0ecSDave Kleikamp 
295ac27a0ecSDave Kleikamp 	/*
296ac27a0ecSDave Kleikamp 	 * One subtle ordering requirement: if anything has gone wrong
297ac27a0ecSDave Kleikamp 	 * (transaction abort, IO errors, whatever), then we can still
298ac27a0ecSDave Kleikamp 	 * do these next steps (the fs will already have been marked as
299ac27a0ecSDave Kleikamp 	 * having errors), but we can't free the inode if the mark_dirty
300ac27a0ecSDave Kleikamp 	 * fails.
301ac27a0ecSDave Kleikamp 	 */
302617ba13bSMingming Cao 	if (ext4_mark_inode_dirty(handle, inode))
303ac27a0ecSDave Kleikamp 		/* If that failed, just do the required in-core inode clear. */
3040930fcc1SAl Viro 		ext4_clear_inode(inode);
305ac27a0ecSDave Kleikamp 	else
306617ba13bSMingming Cao 		ext4_free_inode(handle, inode);
307617ba13bSMingming Cao 	ext4_journal_stop(handle);
3088e8ad8a5SJan Kara 	sb_end_intwrite(inode->i_sb);
309ac27a0ecSDave Kleikamp 	return;
310ac27a0ecSDave Kleikamp no_delete:
3110930fcc1SAl Viro 	ext4_clear_inode(inode);	/* We must guarantee clearing of inode... */
312ac27a0ecSDave Kleikamp }
313ac27a0ecSDave Kleikamp 
314a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
315a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode)
31660e58e0fSMingming Cao {
317a9e7f447SDmitry Monakhov 	return &EXT4_I(inode)->i_reserved_quota;
31860e58e0fSMingming Cao }
319a9e7f447SDmitry Monakhov #endif
3209d0be502STheodore Ts'o 
32112219aeaSAneesh Kumar K.V /*
32212219aeaSAneesh Kumar K.V  * Calculate the number of metadata blocks need to reserve
3239d0be502STheodore Ts'o  * to allocate a block located at @lblock
32412219aeaSAneesh Kumar K.V  */
32501f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
32612219aeaSAneesh Kumar K.V {
32712e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3289d0be502STheodore Ts'o 		return ext4_ext_calc_metadata_amount(inode, lblock);
32912219aeaSAneesh Kumar K.V 
3308bb2b247SAmir Goldstein 	return ext4_ind_calc_metadata_amount(inode, lblock);
33112219aeaSAneesh Kumar K.V }
33212219aeaSAneesh Kumar K.V 
3330637c6f4STheodore Ts'o /*
3340637c6f4STheodore Ts'o  * Called with i_data_sem down, which is important since we can call
3350637c6f4STheodore Ts'o  * ext4_discard_preallocations() from here.
3360637c6f4STheodore Ts'o  */
3375f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode,
3385f634d06SAneesh Kumar K.V 					int used, int quota_claim)
33912219aeaSAneesh Kumar K.V {
34012219aeaSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3410637c6f4STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
34212219aeaSAneesh Kumar K.V 
3430637c6f4STheodore Ts'o 	spin_lock(&ei->i_block_reservation_lock);
344d8990240SAditya Kali 	trace_ext4_da_update_reserve_space(inode, used, quota_claim);
3450637c6f4STheodore Ts'o 	if (unlikely(used > ei->i_reserved_data_blocks)) {
3468de5c325STheodore Ts'o 		ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
3471084f252STheodore Ts'o 			 "with only %d reserved data blocks",
3480637c6f4STheodore Ts'o 			 __func__, inode->i_ino, used,
3490637c6f4STheodore Ts'o 			 ei->i_reserved_data_blocks);
3500637c6f4STheodore Ts'o 		WARN_ON(1);
3510637c6f4STheodore Ts'o 		used = ei->i_reserved_data_blocks;
3526bc6e63fSAneesh Kumar K.V 	}
35312219aeaSAneesh Kumar K.V 
35497795d2aSBrian Foster 	if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
35501a523ebSTheodore Ts'o 		ext4_warning(inode->i_sb, "ino %lu, allocated %d "
35601a523ebSTheodore Ts'o 			"with only %d reserved metadata blocks "
35701a523ebSTheodore Ts'o 			"(releasing %d blocks with reserved %d data blocks)",
35897795d2aSBrian Foster 			inode->i_ino, ei->i_allocated_meta_blocks,
35901a523ebSTheodore Ts'o 			     ei->i_reserved_meta_blocks, used,
36001a523ebSTheodore Ts'o 			     ei->i_reserved_data_blocks);
36197795d2aSBrian Foster 		WARN_ON(1);
36297795d2aSBrian Foster 		ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
36397795d2aSBrian Foster 	}
36497795d2aSBrian Foster 
3650637c6f4STheodore Ts'o 	/* Update per-inode reservations */
3660637c6f4STheodore Ts'o 	ei->i_reserved_data_blocks -= used;
3670637c6f4STheodore Ts'o 	ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
36857042651STheodore Ts'o 	percpu_counter_sub(&sbi->s_dirtyclusters_counter,
36972b8ab9dSEric Sandeen 			   used + ei->i_allocated_meta_blocks);
3700637c6f4STheodore Ts'o 	ei->i_allocated_meta_blocks = 0;
3710637c6f4STheodore Ts'o 
3720637c6f4STheodore Ts'o 	if (ei->i_reserved_data_blocks == 0) {
3730637c6f4STheodore Ts'o 		/*
3740637c6f4STheodore Ts'o 		 * We can release all of the reserved metadata blocks
3750637c6f4STheodore Ts'o 		 * only when we have written all of the delayed
3760637c6f4STheodore Ts'o 		 * allocation blocks.
3770637c6f4STheodore Ts'o 		 */
37857042651STheodore Ts'o 		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
37972b8ab9dSEric Sandeen 				   ei->i_reserved_meta_blocks);
380ee5f4d9cSTheodore Ts'o 		ei->i_reserved_meta_blocks = 0;
3819d0be502STheodore Ts'o 		ei->i_da_metadata_calc_len = 0;
3820637c6f4STheodore Ts'o 	}
38312219aeaSAneesh Kumar K.V 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
38460e58e0fSMingming Cao 
38572b8ab9dSEric Sandeen 	/* Update quota subsystem for data blocks */
38672b8ab9dSEric Sandeen 	if (quota_claim)
3877b415bf6SAditya Kali 		dquot_claim_block(inode, EXT4_C2B(sbi, used));
38872b8ab9dSEric Sandeen 	else {
3895f634d06SAneesh Kumar K.V 		/*
3905f634d06SAneesh Kumar K.V 		 * We did fallocate with an offset that is already delayed
3915f634d06SAneesh Kumar K.V 		 * allocated. So on delayed allocated writeback we should
39272b8ab9dSEric Sandeen 		 * not re-claim the quota for fallocated blocks.
3935f634d06SAneesh Kumar K.V 		 */
3947b415bf6SAditya Kali 		dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
3955f634d06SAneesh Kumar K.V 	}
396d6014301SAneesh Kumar K.V 
397d6014301SAneesh Kumar K.V 	/*
398d6014301SAneesh Kumar K.V 	 * If we have done all the pending block allocations and if
399d6014301SAneesh Kumar K.V 	 * there aren't any writers on the inode, we can discard the
400d6014301SAneesh Kumar K.V 	 * inode's preallocations.
401d6014301SAneesh Kumar K.V 	 */
4020637c6f4STheodore Ts'o 	if ((ei->i_reserved_data_blocks == 0) &&
4030637c6f4STheodore Ts'o 	    (atomic_read(&inode->i_writecount) == 0))
404d6014301SAneesh Kumar K.V 		ext4_discard_preallocations(inode);
40512219aeaSAneesh Kumar K.V }
40612219aeaSAneesh Kumar K.V 
407e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func,
408c398eda0STheodore Ts'o 				unsigned int line,
40924676da4STheodore Ts'o 				struct ext4_map_blocks *map)
4106fd058f7STheodore Ts'o {
41124676da4STheodore Ts'o 	if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
41224676da4STheodore Ts'o 				   map->m_len)) {
413c398eda0STheodore Ts'o 		ext4_error_inode(inode, func, line, map->m_pblk,
414c398eda0STheodore Ts'o 				 "lblock %lu mapped to illegal pblock "
41524676da4STheodore Ts'o 				 "(length %d)", (unsigned long) map->m_lblk,
416c398eda0STheodore Ts'o 				 map->m_len);
4176fd058f7STheodore Ts'o 		return -EIO;
4186fd058f7STheodore Ts'o 	}
4196fd058f7STheodore Ts'o 	return 0;
4206fd058f7STheodore Ts'o }
4216fd058f7STheodore Ts'o 
422e29136f8STheodore Ts'o #define check_block_validity(inode, map)	\
423c398eda0STheodore Ts'o 	__check_block_validity((inode), __func__, __LINE__, (map))
424e29136f8STheodore Ts'o 
425f5ab0d1fSMingming Cao /*
4261f94533dSTheodore Ts'o  * Return the number of contiguous dirty pages in a given inode
4271f94533dSTheodore Ts'o  * starting at page frame idx.
42855138e0bSTheodore Ts'o  */
42955138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
43055138e0bSTheodore Ts'o 				    unsigned int max_pages)
43155138e0bSTheodore Ts'o {
43255138e0bSTheodore Ts'o 	struct address_space *mapping = inode->i_mapping;
43355138e0bSTheodore Ts'o 	pgoff_t	index;
43455138e0bSTheodore Ts'o 	struct pagevec pvec;
43555138e0bSTheodore Ts'o 	pgoff_t num = 0;
43655138e0bSTheodore Ts'o 	int i, nr_pages, done = 0;
43755138e0bSTheodore Ts'o 
43855138e0bSTheodore Ts'o 	if (max_pages == 0)
43955138e0bSTheodore Ts'o 		return 0;
44055138e0bSTheodore Ts'o 	pagevec_init(&pvec, 0);
44155138e0bSTheodore Ts'o 	while (!done) {
44255138e0bSTheodore Ts'o 		index = idx;
44355138e0bSTheodore Ts'o 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
44455138e0bSTheodore Ts'o 					      PAGECACHE_TAG_DIRTY,
44555138e0bSTheodore Ts'o 					      (pgoff_t)PAGEVEC_SIZE);
44655138e0bSTheodore Ts'o 		if (nr_pages == 0)
44755138e0bSTheodore Ts'o 			break;
44855138e0bSTheodore Ts'o 		for (i = 0; i < nr_pages; i++) {
44955138e0bSTheodore Ts'o 			struct page *page = pvec.pages[i];
45055138e0bSTheodore Ts'o 			struct buffer_head *bh, *head;
45155138e0bSTheodore Ts'o 
45255138e0bSTheodore Ts'o 			lock_page(page);
45355138e0bSTheodore Ts'o 			if (unlikely(page->mapping != mapping) ||
45455138e0bSTheodore Ts'o 			    !PageDirty(page) ||
45555138e0bSTheodore Ts'o 			    PageWriteback(page) ||
45655138e0bSTheodore Ts'o 			    page->index != idx) {
45755138e0bSTheodore Ts'o 				done = 1;
45855138e0bSTheodore Ts'o 				unlock_page(page);
45955138e0bSTheodore Ts'o 				break;
46055138e0bSTheodore Ts'o 			}
4611f94533dSTheodore Ts'o 			if (page_has_buffers(page)) {
4621f94533dSTheodore Ts'o 				bh = head = page_buffers(page);
46355138e0bSTheodore Ts'o 				do {
46455138e0bSTheodore Ts'o 					if (!buffer_delay(bh) &&
4651f94533dSTheodore Ts'o 					    !buffer_unwritten(bh))
46655138e0bSTheodore Ts'o 						done = 1;
4671f94533dSTheodore Ts'o 					bh = bh->b_this_page;
4681f94533dSTheodore Ts'o 				} while (!done && (bh != head));
46955138e0bSTheodore Ts'o 			}
47055138e0bSTheodore Ts'o 			unlock_page(page);
47155138e0bSTheodore Ts'o 			if (done)
47255138e0bSTheodore Ts'o 				break;
47355138e0bSTheodore Ts'o 			idx++;
47455138e0bSTheodore Ts'o 			num++;
475659c6009SEric Sandeen 			if (num >= max_pages) {
476659c6009SEric Sandeen 				done = 1;
47755138e0bSTheodore Ts'o 				break;
47855138e0bSTheodore Ts'o 			}
479659c6009SEric Sandeen 		}
48055138e0bSTheodore Ts'o 		pagevec_release(&pvec);
48155138e0bSTheodore Ts'o 	}
48255138e0bSTheodore Ts'o 	return num;
48355138e0bSTheodore Ts'o }
48455138e0bSTheodore Ts'o 
485921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
486921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle,
487921f266bSDmitry Monakhov 				       struct inode *inode,
488921f266bSDmitry Monakhov 				       struct ext4_map_blocks *es_map,
489921f266bSDmitry Monakhov 				       struct ext4_map_blocks *map,
490921f266bSDmitry Monakhov 				       int flags)
491921f266bSDmitry Monakhov {
492921f266bSDmitry Monakhov 	int retval;
493921f266bSDmitry Monakhov 
494921f266bSDmitry Monakhov 	map->m_flags = 0;
495921f266bSDmitry Monakhov 	/*
496921f266bSDmitry Monakhov 	 * There is a race window that the result is not the same.
497921f266bSDmitry Monakhov 	 * e.g. xfstests #223 when dioread_nolock enables.  The reason
498921f266bSDmitry Monakhov 	 * is that we lookup a block mapping in extent status tree with
499921f266bSDmitry Monakhov 	 * out taking i_data_sem.  So at the time the unwritten extent
500921f266bSDmitry Monakhov 	 * could be converted.
501921f266bSDmitry Monakhov 	 */
502921f266bSDmitry Monakhov 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
503921f266bSDmitry Monakhov 		down_read((&EXT4_I(inode)->i_data_sem));
504921f266bSDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
505921f266bSDmitry Monakhov 		retval = ext4_ext_map_blocks(handle, inode, map, flags &
506921f266bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
507921f266bSDmitry Monakhov 	} else {
508921f266bSDmitry Monakhov 		retval = ext4_ind_map_blocks(handle, inode, map, flags &
509921f266bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
510921f266bSDmitry Monakhov 	}
511921f266bSDmitry Monakhov 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
512921f266bSDmitry Monakhov 		up_read((&EXT4_I(inode)->i_data_sem));
513921f266bSDmitry Monakhov 	/*
514921f266bSDmitry Monakhov 	 * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag
515921f266bSDmitry Monakhov 	 * because it shouldn't be marked in es_map->m_flags.
516921f266bSDmitry Monakhov 	 */
517921f266bSDmitry Monakhov 	map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY);
518921f266bSDmitry Monakhov 
519921f266bSDmitry Monakhov 	/*
520921f266bSDmitry Monakhov 	 * We don't check m_len because extent will be collpased in status
521921f266bSDmitry Monakhov 	 * tree.  So the m_len might not equal.
522921f266bSDmitry Monakhov 	 */
523921f266bSDmitry Monakhov 	if (es_map->m_lblk != map->m_lblk ||
524921f266bSDmitry Monakhov 	    es_map->m_flags != map->m_flags ||
525921f266bSDmitry Monakhov 	    es_map->m_pblk != map->m_pblk) {
526921f266bSDmitry Monakhov 		printk("ES cache assertation failed for inode: %lu "
527921f266bSDmitry Monakhov 		       "es_cached ex [%d/%d/%llu/%x] != "
528921f266bSDmitry Monakhov 		       "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
529921f266bSDmitry Monakhov 		       inode->i_ino, es_map->m_lblk, es_map->m_len,
530921f266bSDmitry Monakhov 		       es_map->m_pblk, es_map->m_flags, map->m_lblk,
531921f266bSDmitry Monakhov 		       map->m_len, map->m_pblk, map->m_flags,
532921f266bSDmitry Monakhov 		       retval, flags);
533921f266bSDmitry Monakhov 	}
534921f266bSDmitry Monakhov }
535921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */
536921f266bSDmitry Monakhov 
53755138e0bSTheodore Ts'o /*
538e35fd660STheodore Ts'o  * The ext4_map_blocks() function tries to look up the requested blocks,
5392b2d6d01STheodore Ts'o  * and returns if the blocks are already mapped.
540f5ab0d1fSMingming Cao  *
541f5ab0d1fSMingming Cao  * Otherwise it takes the write lock of the i_data_sem and allocate blocks
542f5ab0d1fSMingming Cao  * and store the allocated blocks in the result buffer head and mark it
543f5ab0d1fSMingming Cao  * mapped.
544f5ab0d1fSMingming Cao  *
545e35fd660STheodore Ts'o  * If file type is extents based, it will call ext4_ext_map_blocks(),
546e35fd660STheodore Ts'o  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
547f5ab0d1fSMingming Cao  * based files
548f5ab0d1fSMingming Cao  *
549f5ab0d1fSMingming Cao  * On success, it returns the number of blocks being mapped or allocate.
550f5ab0d1fSMingming Cao  * if create==0 and the blocks are pre-allocated and uninitialized block,
551f5ab0d1fSMingming Cao  * the result buffer head is unmapped. If the create ==1, it will make sure
552f5ab0d1fSMingming Cao  * the buffer head is mapped.
553f5ab0d1fSMingming Cao  *
554f5ab0d1fSMingming Cao  * It returns 0 if plain look up failed (blocks have not been allocated), in
555df3ab170STao Ma  * that case, buffer head is unmapped
556f5ab0d1fSMingming Cao  *
557f5ab0d1fSMingming Cao  * It returns the error in case of allocation failure.
558f5ab0d1fSMingming Cao  */
559e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode,
560e35fd660STheodore Ts'o 		    struct ext4_map_blocks *map, int flags)
5610e855ac8SAneesh Kumar K.V {
562d100eef2SZheng Liu 	struct extent_status es;
5630e855ac8SAneesh Kumar K.V 	int retval;
564921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
565921f266bSDmitry Monakhov 	struct ext4_map_blocks orig_map;
566921f266bSDmitry Monakhov 
567921f266bSDmitry Monakhov 	memcpy(&orig_map, map, sizeof(*map));
568921f266bSDmitry Monakhov #endif
569f5ab0d1fSMingming Cao 
570e35fd660STheodore Ts'o 	map->m_flags = 0;
571e35fd660STheodore Ts'o 	ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
572e35fd660STheodore Ts'o 		  "logical block %lu\n", inode->i_ino, flags, map->m_len,
573e35fd660STheodore Ts'o 		  (unsigned long) map->m_lblk);
574d100eef2SZheng Liu 
575d100eef2SZheng Liu 	/* Lookup extent status tree firstly */
576d100eef2SZheng Liu 	if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
577d100eef2SZheng Liu 		if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
578d100eef2SZheng Liu 			map->m_pblk = ext4_es_pblock(&es) +
579d100eef2SZheng Liu 					map->m_lblk - es.es_lblk;
580d100eef2SZheng Liu 			map->m_flags |= ext4_es_is_written(&es) ?
581d100eef2SZheng Liu 					EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
582d100eef2SZheng Liu 			retval = es.es_len - (map->m_lblk - es.es_lblk);
583d100eef2SZheng Liu 			if (retval > map->m_len)
584d100eef2SZheng Liu 				retval = map->m_len;
585d100eef2SZheng Liu 			map->m_len = retval;
586d100eef2SZheng Liu 		} else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
587d100eef2SZheng Liu 			retval = 0;
588d100eef2SZheng Liu 		} else {
589d100eef2SZheng Liu 			BUG_ON(1);
590d100eef2SZheng Liu 		}
591921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
592921f266bSDmitry Monakhov 		ext4_map_blocks_es_recheck(handle, inode, map,
593921f266bSDmitry Monakhov 					   &orig_map, flags);
594921f266bSDmitry Monakhov #endif
595d100eef2SZheng Liu 		goto found;
596d100eef2SZheng Liu 	}
597d100eef2SZheng Liu 
5984df3d265SAneesh Kumar K.V 	/*
599b920c755STheodore Ts'o 	 * Try to see if we can get the block without requesting a new
600b920c755STheodore Ts'o 	 * file system block.
6014df3d265SAneesh Kumar K.V 	 */
602729f52c6SZheng Liu 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
6030e855ac8SAneesh Kumar K.V 		down_read((&EXT4_I(inode)->i_data_sem));
60412e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
605a4e5d88bSDmitry Monakhov 		retval = ext4_ext_map_blocks(handle, inode, map, flags &
606a4e5d88bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
6074df3d265SAneesh Kumar K.V 	} else {
608a4e5d88bSDmitry Monakhov 		retval = ext4_ind_map_blocks(handle, inode, map, flags &
609a4e5d88bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
6100e855ac8SAneesh Kumar K.V 	}
611f7fec032SZheng Liu 	if (retval > 0) {
612f7fec032SZheng Liu 		int ret;
613f7fec032SZheng Liu 		unsigned long long status;
614f7fec032SZheng Liu 
615921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
616921f266bSDmitry Monakhov 		if (retval != map->m_len) {
617921f266bSDmitry Monakhov 			printk("ES len assertation failed for inode: %lu "
618921f266bSDmitry Monakhov 			       "retval %d != map->m_len %d "
619921f266bSDmitry Monakhov 			       "in %s (lookup)\n", inode->i_ino, retval,
620921f266bSDmitry Monakhov 			       map->m_len, __func__);
621921f266bSDmitry Monakhov 		}
622921f266bSDmitry Monakhov #endif
623921f266bSDmitry Monakhov 
624f7fec032SZheng Liu 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
625f7fec032SZheng Liu 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
626f7fec032SZheng Liu 		if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
627f7fec032SZheng Liu 		    ext4_find_delalloc_range(inode, map->m_lblk,
628f7fec032SZheng Liu 					     map->m_lblk + map->m_len - 1))
629f7fec032SZheng Liu 			status |= EXTENT_STATUS_DELAYED;
630f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk,
631f7fec032SZheng Liu 					    map->m_len, map->m_pblk, status);
632f7fec032SZheng Liu 		if (ret < 0)
633f7fec032SZheng Liu 			retval = ret;
634f7fec032SZheng Liu 	}
635729f52c6SZheng Liu 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
6364df3d265SAneesh Kumar K.V 		up_read((&EXT4_I(inode)->i_data_sem));
637f5ab0d1fSMingming Cao 
638d100eef2SZheng Liu found:
639e35fd660STheodore Ts'o 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
640f7fec032SZheng Liu 		int ret = check_block_validity(inode, map);
6416fd058f7STheodore Ts'o 		if (ret != 0)
6426fd058f7STheodore Ts'o 			return ret;
6436fd058f7STheodore Ts'o 	}
6446fd058f7STheodore Ts'o 
645f5ab0d1fSMingming Cao 	/* If it is only a block(s) look up */
646c2177057STheodore Ts'o 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
6474df3d265SAneesh Kumar K.V 		return retval;
6484df3d265SAneesh Kumar K.V 
6494df3d265SAneesh Kumar K.V 	/*
650f5ab0d1fSMingming Cao 	 * Returns if the blocks have already allocated
651f5ab0d1fSMingming Cao 	 *
652f5ab0d1fSMingming Cao 	 * Note that if blocks have been preallocated
653df3ab170STao Ma 	 * ext4_ext_get_block() returns the create = 0
654f5ab0d1fSMingming Cao 	 * with buffer head unmapped.
655f5ab0d1fSMingming Cao 	 */
656e35fd660STheodore Ts'o 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
657f5ab0d1fSMingming Cao 		return retval;
658f5ab0d1fSMingming Cao 
659f5ab0d1fSMingming Cao 	/*
660a25a4e1aSZheng Liu 	 * Here we clear m_flags because after allocating an new extent,
661a25a4e1aSZheng Liu 	 * it will be set again.
6622a8964d6SAneesh Kumar K.V 	 */
663a25a4e1aSZheng Liu 	map->m_flags &= ~EXT4_MAP_FLAGS;
6642a8964d6SAneesh Kumar K.V 
6652a8964d6SAneesh Kumar K.V 	/*
666f5ab0d1fSMingming Cao 	 * New blocks allocate and/or writing to uninitialized extent
667f5ab0d1fSMingming Cao 	 * will possibly result in updating i_data, so we take
668f5ab0d1fSMingming Cao 	 * the write lock of i_data_sem, and call get_blocks()
669f5ab0d1fSMingming Cao 	 * with create == 1 flag.
6704df3d265SAneesh Kumar K.V 	 */
6714df3d265SAneesh Kumar K.V 	down_write((&EXT4_I(inode)->i_data_sem));
672d2a17637SMingming Cao 
673d2a17637SMingming Cao 	/*
674d2a17637SMingming Cao 	 * if the caller is from delayed allocation writeout path
675d2a17637SMingming Cao 	 * we have already reserved fs blocks for allocation
676d2a17637SMingming Cao 	 * let the underlying get_block() function know to
677d2a17637SMingming Cao 	 * avoid double accounting
678d2a17637SMingming Cao 	 */
679c2177057STheodore Ts'o 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
680f2321097STheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
6814df3d265SAneesh Kumar K.V 	/*
6824df3d265SAneesh Kumar K.V 	 * We need to check for EXT4 here because migrate
6834df3d265SAneesh Kumar K.V 	 * could have changed the inode type in between
6844df3d265SAneesh Kumar K.V 	 */
68512e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
686e35fd660STheodore Ts'o 		retval = ext4_ext_map_blocks(handle, inode, map, flags);
6870e855ac8SAneesh Kumar K.V 	} else {
688e35fd660STheodore Ts'o 		retval = ext4_ind_map_blocks(handle, inode, map, flags);
689267e4db9SAneesh Kumar K.V 
690e35fd660STheodore Ts'o 		if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
691267e4db9SAneesh Kumar K.V 			/*
692267e4db9SAneesh Kumar K.V 			 * We allocated new blocks which will result in
693267e4db9SAneesh Kumar K.V 			 * i_data's format changing.  Force the migrate
694267e4db9SAneesh Kumar K.V 			 * to fail by clearing migrate flags
695267e4db9SAneesh Kumar K.V 			 */
69619f5fb7aSTheodore Ts'o 			ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
697267e4db9SAneesh Kumar K.V 		}
6982ac3b6e0STheodore Ts'o 
699d2a17637SMingming Cao 		/*
7002ac3b6e0STheodore Ts'o 		 * Update reserved blocks/metadata blocks after successful
7015f634d06SAneesh Kumar K.V 		 * block allocation which had been deferred till now. We don't
7025f634d06SAneesh Kumar K.V 		 * support fallocate for non extent files. So we can update
7035f634d06SAneesh Kumar K.V 		 * reserve space here.
704d2a17637SMingming Cao 		 */
7055f634d06SAneesh Kumar K.V 		if ((retval > 0) &&
7061296cc85SAneesh Kumar K.V 			(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
7075f634d06SAneesh Kumar K.V 			ext4_da_update_reserve_space(inode, retval, 1);
7085f634d06SAneesh Kumar K.V 	}
709f7fec032SZheng Liu 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
710f2321097STheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
711d2a17637SMingming Cao 
712f7fec032SZheng Liu 	if (retval > 0) {
71351865fdaSZheng Liu 		int ret;
714f7fec032SZheng Liu 		unsigned long long status;
715f7fec032SZheng Liu 
716921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
717921f266bSDmitry Monakhov 		if (retval != map->m_len) {
718921f266bSDmitry Monakhov 			printk("ES len assertation failed for inode: %lu "
719921f266bSDmitry Monakhov 			       "retval %d != map->m_len %d "
720921f266bSDmitry Monakhov 			       "in %s (allocation)\n", inode->i_ino, retval,
721921f266bSDmitry Monakhov 			       map->m_len, __func__);
722921f266bSDmitry Monakhov 		}
723921f266bSDmitry Monakhov #endif
724921f266bSDmitry Monakhov 
725adb23551SZheng Liu 		/*
726adb23551SZheng Liu 		 * If the extent has been zeroed out, we don't need to update
727adb23551SZheng Liu 		 * extent status tree.
728adb23551SZheng Liu 		 */
729adb23551SZheng Liu 		if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
730adb23551SZheng Liu 		    ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
731adb23551SZheng Liu 			if (ext4_es_is_written(&es))
732adb23551SZheng Liu 				goto has_zeroout;
733adb23551SZheng Liu 		}
734f7fec032SZheng Liu 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
735f7fec032SZheng Liu 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
736f7fec032SZheng Liu 		if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
737f7fec032SZheng Liu 		    ext4_find_delalloc_range(inode, map->m_lblk,
738f7fec032SZheng Liu 					     map->m_lblk + map->m_len - 1))
739f7fec032SZheng Liu 			status |= EXTENT_STATUS_DELAYED;
740f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
741f7fec032SZheng Liu 					    map->m_pblk, status);
74251865fdaSZheng Liu 		if (ret < 0)
74351865fdaSZheng Liu 			retval = ret;
74451865fdaSZheng Liu 	}
7455356f261SAditya Kali 
746adb23551SZheng Liu has_zeroout:
7470e855ac8SAneesh Kumar K.V 	up_write((&EXT4_I(inode)->i_data_sem));
748e35fd660STheodore Ts'o 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
749e29136f8STheodore Ts'o 		int ret = check_block_validity(inode, map);
7506fd058f7STheodore Ts'o 		if (ret != 0)
7516fd058f7STheodore Ts'o 			return ret;
7526fd058f7STheodore Ts'o 	}
7530e855ac8SAneesh Kumar K.V 	return retval;
7540e855ac8SAneesh Kumar K.V }
7550e855ac8SAneesh Kumar K.V 
756f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */
757f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096
758f3bd1f3fSMingming Cao 
7592ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock,
7602ed88685STheodore Ts'o 			   struct buffer_head *bh, int flags)
761ac27a0ecSDave Kleikamp {
7623e4fdaf8SDmitriy Monakhov 	handle_t *handle = ext4_journal_current_handle();
7632ed88685STheodore Ts'o 	struct ext4_map_blocks map;
7647fb5409dSJan Kara 	int ret = 0, started = 0;
765f3bd1f3fSMingming Cao 	int dio_credits;
766ac27a0ecSDave Kleikamp 
76746c7f254STao Ma 	if (ext4_has_inline_data(inode))
76846c7f254STao Ma 		return -ERANGE;
76946c7f254STao Ma 
7702ed88685STheodore Ts'o 	map.m_lblk = iblock;
7712ed88685STheodore Ts'o 	map.m_len = bh->b_size >> inode->i_blkbits;
7722ed88685STheodore Ts'o 
7738b0f165fSAnatol Pomozov 	if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
7747fb5409dSJan Kara 		/* Direct IO write... */
7752ed88685STheodore Ts'o 		if (map.m_len > DIO_MAX_BLOCKS)
7762ed88685STheodore Ts'o 			map.m_len = DIO_MAX_BLOCKS;
7772ed88685STheodore Ts'o 		dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
7789924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
7799924a92aSTheodore Ts'o 					    dio_credits);
7807fb5409dSJan Kara 		if (IS_ERR(handle)) {
781ac27a0ecSDave Kleikamp 			ret = PTR_ERR(handle);
7822ed88685STheodore Ts'o 			return ret;
7837fb5409dSJan Kara 		}
7847fb5409dSJan Kara 		started = 1;
785ac27a0ecSDave Kleikamp 	}
786ac27a0ecSDave Kleikamp 
7872ed88685STheodore Ts'o 	ret = ext4_map_blocks(handle, inode, &map, flags);
788ac27a0ecSDave Kleikamp 	if (ret > 0) {
7892ed88685STheodore Ts'o 		map_bh(bh, inode->i_sb, map.m_pblk);
7902ed88685STheodore Ts'o 		bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
7912ed88685STheodore Ts'o 		bh->b_size = inode->i_sb->s_blocksize * map.m_len;
792ac27a0ecSDave Kleikamp 		ret = 0;
793ac27a0ecSDave Kleikamp 	}
7947fb5409dSJan Kara 	if (started)
7957fb5409dSJan Kara 		ext4_journal_stop(handle);
796ac27a0ecSDave Kleikamp 	return ret;
797ac27a0ecSDave Kleikamp }
798ac27a0ecSDave Kleikamp 
7992ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock,
8002ed88685STheodore Ts'o 		   struct buffer_head *bh, int create)
8012ed88685STheodore Ts'o {
8022ed88685STheodore Ts'o 	return _ext4_get_block(inode, iblock, bh,
8032ed88685STheodore Ts'o 			       create ? EXT4_GET_BLOCKS_CREATE : 0);
8042ed88685STheodore Ts'o }
8052ed88685STheodore Ts'o 
806ac27a0ecSDave Kleikamp /*
807ac27a0ecSDave Kleikamp  * `handle' can be NULL if create is zero
808ac27a0ecSDave Kleikamp  */
809617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
810725d26d3SAneesh Kumar K.V 				ext4_lblk_t block, int create, int *errp)
811ac27a0ecSDave Kleikamp {
8122ed88685STheodore Ts'o 	struct ext4_map_blocks map;
8132ed88685STheodore Ts'o 	struct buffer_head *bh;
814ac27a0ecSDave Kleikamp 	int fatal = 0, err;
815ac27a0ecSDave Kleikamp 
816ac27a0ecSDave Kleikamp 	J_ASSERT(handle != NULL || create == 0);
817ac27a0ecSDave Kleikamp 
8182ed88685STheodore Ts'o 	map.m_lblk = block;
8192ed88685STheodore Ts'o 	map.m_len = 1;
8202ed88685STheodore Ts'o 	err = ext4_map_blocks(handle, inode, &map,
8212ed88685STheodore Ts'o 			      create ? EXT4_GET_BLOCKS_CREATE : 0);
8222ed88685STheodore Ts'o 
82390b0a973SCarlos Maiolino 	/* ensure we send some value back into *errp */
82490b0a973SCarlos Maiolino 	*errp = 0;
82590b0a973SCarlos Maiolino 
8260f70b406STheodore Ts'o 	if (create && err == 0)
8270f70b406STheodore Ts'o 		err = -ENOSPC;	/* should never happen */
8282ed88685STheodore Ts'o 	if (err < 0)
829ac27a0ecSDave Kleikamp 		*errp = err;
8302ed88685STheodore Ts'o 	if (err <= 0)
8312ed88685STheodore Ts'o 		return NULL;
8322ed88685STheodore Ts'o 
8332ed88685STheodore Ts'o 	bh = sb_getblk(inode->i_sb, map.m_pblk);
834aebf0243SWang Shilong 	if (unlikely(!bh)) {
835860d21e2STheodore Ts'o 		*errp = -ENOMEM;
8362ed88685STheodore Ts'o 		return NULL;
837ac27a0ecSDave Kleikamp 	}
8382ed88685STheodore Ts'o 	if (map.m_flags & EXT4_MAP_NEW) {
839ac27a0ecSDave Kleikamp 		J_ASSERT(create != 0);
840ac39849dSAneesh Kumar K.V 		J_ASSERT(handle != NULL);
841ac27a0ecSDave Kleikamp 
842ac27a0ecSDave Kleikamp 		/*
843ac27a0ecSDave Kleikamp 		 * Now that we do not always journal data, we should
844ac27a0ecSDave Kleikamp 		 * keep in mind whether this should always journal the
845ac27a0ecSDave Kleikamp 		 * new buffer as metadata.  For now, regular file
846617ba13bSMingming Cao 		 * writes use ext4_get_block instead, so it's not a
847ac27a0ecSDave Kleikamp 		 * problem.
848ac27a0ecSDave Kleikamp 		 */
849ac27a0ecSDave Kleikamp 		lock_buffer(bh);
850ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "call get_create_access");
851617ba13bSMingming Cao 		fatal = ext4_journal_get_create_access(handle, bh);
852ac27a0ecSDave Kleikamp 		if (!fatal && !buffer_uptodate(bh)) {
853ac27a0ecSDave Kleikamp 			memset(bh->b_data, 0, inode->i_sb->s_blocksize);
854ac27a0ecSDave Kleikamp 			set_buffer_uptodate(bh);
855ac27a0ecSDave Kleikamp 		}
856ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
8570390131bSFrank Mayhar 		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
8580390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, bh);
859ac27a0ecSDave Kleikamp 		if (!fatal)
860ac27a0ecSDave Kleikamp 			fatal = err;
861ac27a0ecSDave Kleikamp 	} else {
862ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "not a new buffer");
863ac27a0ecSDave Kleikamp 	}
864ac27a0ecSDave Kleikamp 	if (fatal) {
865ac27a0ecSDave Kleikamp 		*errp = fatal;
866ac27a0ecSDave Kleikamp 		brelse(bh);
867ac27a0ecSDave Kleikamp 		bh = NULL;
868ac27a0ecSDave Kleikamp 	}
869ac27a0ecSDave Kleikamp 	return bh;
870ac27a0ecSDave Kleikamp }
871ac27a0ecSDave Kleikamp 
872617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
873725d26d3SAneesh Kumar K.V 			       ext4_lblk_t block, int create, int *err)
874ac27a0ecSDave Kleikamp {
875ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
876ac27a0ecSDave Kleikamp 
877617ba13bSMingming Cao 	bh = ext4_getblk(handle, inode, block, create, err);
878ac27a0ecSDave Kleikamp 	if (!bh)
879ac27a0ecSDave Kleikamp 		return bh;
880ac27a0ecSDave Kleikamp 	if (buffer_uptodate(bh))
881ac27a0ecSDave Kleikamp 		return bh;
88265299a3bSChristoph Hellwig 	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
883ac27a0ecSDave Kleikamp 	wait_on_buffer(bh);
884ac27a0ecSDave Kleikamp 	if (buffer_uptodate(bh))
885ac27a0ecSDave Kleikamp 		return bh;
886ac27a0ecSDave Kleikamp 	put_bh(bh);
887ac27a0ecSDave Kleikamp 	*err = -EIO;
888ac27a0ecSDave Kleikamp 	return NULL;
889ac27a0ecSDave Kleikamp }
890ac27a0ecSDave Kleikamp 
891f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle,
892ac27a0ecSDave Kleikamp 			   struct buffer_head *head,
893ac27a0ecSDave Kleikamp 			   unsigned from,
894ac27a0ecSDave Kleikamp 			   unsigned to,
895ac27a0ecSDave Kleikamp 			   int *partial,
896ac27a0ecSDave Kleikamp 			   int (*fn)(handle_t *handle,
897ac27a0ecSDave Kleikamp 				     struct buffer_head *bh))
898ac27a0ecSDave Kleikamp {
899ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
900ac27a0ecSDave Kleikamp 	unsigned block_start, block_end;
901ac27a0ecSDave Kleikamp 	unsigned blocksize = head->b_size;
902ac27a0ecSDave Kleikamp 	int err, ret = 0;
903ac27a0ecSDave Kleikamp 	struct buffer_head *next;
904ac27a0ecSDave Kleikamp 
905ac27a0ecSDave Kleikamp 	for (bh = head, block_start = 0;
906ac27a0ecSDave Kleikamp 	     ret == 0 && (bh != head || !block_start);
907de9a55b8STheodore Ts'o 	     block_start = block_end, bh = next) {
908ac27a0ecSDave Kleikamp 		next = bh->b_this_page;
909ac27a0ecSDave Kleikamp 		block_end = block_start + blocksize;
910ac27a0ecSDave Kleikamp 		if (block_end <= from || block_start >= to) {
911ac27a0ecSDave Kleikamp 			if (partial && !buffer_uptodate(bh))
912ac27a0ecSDave Kleikamp 				*partial = 1;
913ac27a0ecSDave Kleikamp 			continue;
914ac27a0ecSDave Kleikamp 		}
915ac27a0ecSDave Kleikamp 		err = (*fn)(handle, bh);
916ac27a0ecSDave Kleikamp 		if (!ret)
917ac27a0ecSDave Kleikamp 			ret = err;
918ac27a0ecSDave Kleikamp 	}
919ac27a0ecSDave Kleikamp 	return ret;
920ac27a0ecSDave Kleikamp }
921ac27a0ecSDave Kleikamp 
922ac27a0ecSDave Kleikamp /*
923ac27a0ecSDave Kleikamp  * To preserve ordering, it is essential that the hole instantiation and
924ac27a0ecSDave Kleikamp  * the data write be encapsulated in a single transaction.  We cannot
925617ba13bSMingming Cao  * close off a transaction and start a new one between the ext4_get_block()
926dab291afSMingming Cao  * and the commit_write().  So doing the jbd2_journal_start at the start of
927ac27a0ecSDave Kleikamp  * prepare_write() is the right place.
928ac27a0ecSDave Kleikamp  *
92936ade451SJan Kara  * Also, this function can nest inside ext4_writepage().  In that case, we
93036ade451SJan Kara  * *know* that ext4_writepage() has generated enough buffer credits to do the
93136ade451SJan Kara  * whole page.  So we won't block on the journal in that case, which is good,
93236ade451SJan Kara  * because the caller may be PF_MEMALLOC.
933ac27a0ecSDave Kleikamp  *
934617ba13bSMingming Cao  * By accident, ext4 can be reentered when a transaction is open via
935ac27a0ecSDave Kleikamp  * quota file writes.  If we were to commit the transaction while thus
936ac27a0ecSDave Kleikamp  * reentered, there can be a deadlock - we would be holding a quota
937ac27a0ecSDave Kleikamp  * lock, and the commit would never complete if another thread had a
938ac27a0ecSDave Kleikamp  * transaction open and was blocking on the quota lock - a ranking
939ac27a0ecSDave Kleikamp  * violation.
940ac27a0ecSDave Kleikamp  *
941dab291afSMingming Cao  * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
942ac27a0ecSDave Kleikamp  * will _not_ run commit under these circumstances because handle->h_ref
943ac27a0ecSDave Kleikamp  * is elevated.  We'll still have enough credits for the tiny quotafile
944ac27a0ecSDave Kleikamp  * write.
945ac27a0ecSDave Kleikamp  */
946f19d5870STao Ma int do_journal_get_write_access(handle_t *handle,
947ac27a0ecSDave Kleikamp 				struct buffer_head *bh)
948ac27a0ecSDave Kleikamp {
94956d35a4cSJan Kara 	int dirty = buffer_dirty(bh);
95056d35a4cSJan Kara 	int ret;
95156d35a4cSJan Kara 
952ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh) || buffer_freed(bh))
953ac27a0ecSDave Kleikamp 		return 0;
95456d35a4cSJan Kara 	/*
955ebdec241SChristoph Hellwig 	 * __block_write_begin() could have dirtied some buffers. Clean
95656d35a4cSJan Kara 	 * the dirty bit as jbd2_journal_get_write_access() could complain
95756d35a4cSJan Kara 	 * otherwise about fs integrity issues. Setting of the dirty bit
958ebdec241SChristoph Hellwig 	 * by __block_write_begin() isn't a real problem here as we clear
95956d35a4cSJan Kara 	 * the bit before releasing a page lock and thus writeback cannot
96056d35a4cSJan Kara 	 * ever write the buffer.
96156d35a4cSJan Kara 	 */
96256d35a4cSJan Kara 	if (dirty)
96356d35a4cSJan Kara 		clear_buffer_dirty(bh);
96456d35a4cSJan Kara 	ret = ext4_journal_get_write_access(handle, bh);
96556d35a4cSJan Kara 	if (!ret && dirty)
96656d35a4cSJan Kara 		ret = ext4_handle_dirty_metadata(handle, NULL, bh);
96756d35a4cSJan Kara 	return ret;
968ac27a0ecSDave Kleikamp }
969ac27a0ecSDave Kleikamp 
9708b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
9718b0f165fSAnatol Pomozov 		   struct buffer_head *bh_result, int create);
972bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping,
973bfc1af65SNick Piggin 			    loff_t pos, unsigned len, unsigned flags,
974bfc1af65SNick Piggin 			    struct page **pagep, void **fsdata)
975ac27a0ecSDave Kleikamp {
976bfc1af65SNick Piggin 	struct inode *inode = mapping->host;
9771938a150SAneesh Kumar K.V 	int ret, needed_blocks;
978ac27a0ecSDave Kleikamp 	handle_t *handle;
979ac27a0ecSDave Kleikamp 	int retries = 0;
980bfc1af65SNick Piggin 	struct page *page;
981bfc1af65SNick Piggin 	pgoff_t index;
982bfc1af65SNick Piggin 	unsigned from, to;
983bfc1af65SNick Piggin 
9849bffad1eSTheodore Ts'o 	trace_ext4_write_begin(inode, pos, len, flags);
9851938a150SAneesh Kumar K.V 	/*
9861938a150SAneesh Kumar K.V 	 * Reserve one block more for addition to orphan list in case
9871938a150SAneesh Kumar K.V 	 * we allocate blocks but write fails for some reason
9881938a150SAneesh Kumar K.V 	 */
9891938a150SAneesh Kumar K.V 	needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
990bfc1af65SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
991bfc1af65SNick Piggin 	from = pos & (PAGE_CACHE_SIZE - 1);
992bfc1af65SNick Piggin 	to = from + len;
993ac27a0ecSDave Kleikamp 
994f19d5870STao Ma 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
995f19d5870STao Ma 		ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
996f19d5870STao Ma 						    flags, pagep);
997f19d5870STao Ma 		if (ret < 0)
99847564bfbSTheodore Ts'o 			return ret;
99947564bfbSTheodore Ts'o 		if (ret == 1)
100047564bfbSTheodore Ts'o 			return 0;
1001f19d5870STao Ma 	}
1002f19d5870STao Ma 
100347564bfbSTheodore Ts'o 	/*
100447564bfbSTheodore Ts'o 	 * grab_cache_page_write_begin() can take a long time if the
100547564bfbSTheodore Ts'o 	 * system is thrashing due to memory pressure, or if the page
100647564bfbSTheodore Ts'o 	 * is being written back.  So grab it first before we start
100747564bfbSTheodore Ts'o 	 * the transaction handle.  This also allows us to allocate
100847564bfbSTheodore Ts'o 	 * the page (if needed) without using GFP_NOFS.
100947564bfbSTheodore Ts'o 	 */
101047564bfbSTheodore Ts'o retry_grab:
101154566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
101247564bfbSTheodore Ts'o 	if (!page)
101347564bfbSTheodore Ts'o 		return -ENOMEM;
101447564bfbSTheodore Ts'o 	unlock_page(page);
101547564bfbSTheodore Ts'o 
101647564bfbSTheodore Ts'o retry_journal:
10179924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
1018ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
101947564bfbSTheodore Ts'o 		page_cache_release(page);
102047564bfbSTheodore Ts'o 		return PTR_ERR(handle);
1021cf108bcaSJan Kara 	}
1022f19d5870STao Ma 
102347564bfbSTheodore Ts'o 	lock_page(page);
102447564bfbSTheodore Ts'o 	if (page->mapping != mapping) {
102547564bfbSTheodore Ts'o 		/* The page got truncated from under us */
102647564bfbSTheodore Ts'o 		unlock_page(page);
102747564bfbSTheodore Ts'o 		page_cache_release(page);
1028cf108bcaSJan Kara 		ext4_journal_stop(handle);
102947564bfbSTheodore Ts'o 		goto retry_grab;
1030cf108bcaSJan Kara 	}
103147564bfbSTheodore Ts'o 	wait_on_page_writeback(page);
1032cf108bcaSJan Kara 
1033744692dcSJiaying Zhang 	if (ext4_should_dioread_nolock(inode))
10346e1db88dSChristoph Hellwig 		ret = __block_write_begin(page, pos, len, ext4_get_block_write);
1035744692dcSJiaying Zhang 	else
10366e1db88dSChristoph Hellwig 		ret = __block_write_begin(page, pos, len, ext4_get_block);
1037bfc1af65SNick Piggin 
1038bfc1af65SNick Piggin 	if (!ret && ext4_should_journal_data(inode)) {
1039f19d5870STao Ma 		ret = ext4_walk_page_buffers(handle, page_buffers(page),
1040f19d5870STao Ma 					     from, to, NULL,
1041f19d5870STao Ma 					     do_journal_get_write_access);
1042b46be050SAndrey Savochkin 	}
1043bfc1af65SNick Piggin 
1044bfc1af65SNick Piggin 	if (ret) {
1045bfc1af65SNick Piggin 		unlock_page(page);
1046ae4d5372SAneesh Kumar K.V 		/*
10476e1db88dSChristoph Hellwig 		 * __block_write_begin may have instantiated a few blocks
1048ae4d5372SAneesh Kumar K.V 		 * outside i_size.  Trim these off again. Don't need
1049ae4d5372SAneesh Kumar K.V 		 * i_size_read because we hold i_mutex.
10501938a150SAneesh Kumar K.V 		 *
10511938a150SAneesh Kumar K.V 		 * Add inode to orphan list in case we crash before
10521938a150SAneesh Kumar K.V 		 * truncate finishes
1053ae4d5372SAneesh Kumar K.V 		 */
1054ffacfa7aSJan Kara 		if (pos + len > inode->i_size && ext4_can_truncate(inode))
10551938a150SAneesh Kumar K.V 			ext4_orphan_add(handle, inode);
10561938a150SAneesh Kumar K.V 
10571938a150SAneesh Kumar K.V 		ext4_journal_stop(handle);
10581938a150SAneesh Kumar K.V 		if (pos + len > inode->i_size) {
1059b9a4207dSJan Kara 			ext4_truncate_failed_write(inode);
10601938a150SAneesh Kumar K.V 			/*
1061ffacfa7aSJan Kara 			 * If truncate failed early the inode might
10621938a150SAneesh Kumar K.V 			 * still be on the orphan list; we need to
10631938a150SAneesh Kumar K.V 			 * make sure the inode is removed from the
10641938a150SAneesh Kumar K.V 			 * orphan list in that case.
10651938a150SAneesh Kumar K.V 			 */
10661938a150SAneesh Kumar K.V 			if (inode->i_nlink)
10671938a150SAneesh Kumar K.V 				ext4_orphan_del(NULL, inode);
10681938a150SAneesh Kumar K.V 		}
1069bfc1af65SNick Piggin 
107047564bfbSTheodore Ts'o 		if (ret == -ENOSPC &&
107147564bfbSTheodore Ts'o 		    ext4_should_retry_alloc(inode->i_sb, &retries))
107247564bfbSTheodore Ts'o 			goto retry_journal;
107347564bfbSTheodore Ts'o 		page_cache_release(page);
107447564bfbSTheodore Ts'o 		return ret;
107547564bfbSTheodore Ts'o 	}
107647564bfbSTheodore Ts'o 	*pagep = page;
1077ac27a0ecSDave Kleikamp 	return ret;
1078ac27a0ecSDave Kleikamp }
1079ac27a0ecSDave Kleikamp 
1080bfc1af65SNick Piggin /* For write_end() in data=journal mode */
1081bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1082ac27a0ecSDave Kleikamp {
1083ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh) || buffer_freed(bh))
1084ac27a0ecSDave Kleikamp 		return 0;
1085ac27a0ecSDave Kleikamp 	set_buffer_uptodate(bh);
10860390131bSFrank Mayhar 	return ext4_handle_dirty_metadata(handle, NULL, bh);
1087ac27a0ecSDave Kleikamp }
1088ac27a0ecSDave Kleikamp 
1089eed4333fSZheng Liu /*
1090eed4333fSZheng Liu  * We need to pick up the new inode size which generic_commit_write gave us
1091eed4333fSZheng Liu  * `file' can be NULL - eg, when called from page_symlink().
1092eed4333fSZheng Liu  *
1093eed4333fSZheng Liu  * ext4 never places buffers on inode->i_mapping->private_list.  metadata
1094eed4333fSZheng Liu  * buffers are managed internally.
1095eed4333fSZheng Liu  */
1096eed4333fSZheng Liu static int ext4_write_end(struct file *file,
1097f8514083SAneesh Kumar K.V 			  struct address_space *mapping,
1098f8514083SAneesh Kumar K.V 			  loff_t pos, unsigned len, unsigned copied,
1099f8514083SAneesh Kumar K.V 			  struct page *page, void *fsdata)
1100f8514083SAneesh Kumar K.V {
1101f8514083SAneesh Kumar K.V 	handle_t *handle = ext4_journal_current_handle();
1102eed4333fSZheng Liu 	struct inode *inode = mapping->host;
1103eed4333fSZheng Liu 	int ret = 0, ret2;
1104eed4333fSZheng Liu 	int i_size_changed = 0;
1105eed4333fSZheng Liu 
1106eed4333fSZheng Liu 	trace_ext4_write_end(inode, pos, len, copied);
1107eed4333fSZheng Liu 	if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) {
1108eed4333fSZheng Liu 		ret = ext4_jbd2_file_inode(handle, inode);
1109eed4333fSZheng Liu 		if (ret) {
1110eed4333fSZheng Liu 			unlock_page(page);
1111eed4333fSZheng Liu 			page_cache_release(page);
1112eed4333fSZheng Liu 			goto errout;
1113eed4333fSZheng Liu 		}
1114eed4333fSZheng Liu 	}
1115f8514083SAneesh Kumar K.V 
1116f19d5870STao Ma 	if (ext4_has_inline_data(inode))
1117f19d5870STao Ma 		copied = ext4_write_inline_data_end(inode, pos, len,
1118f19d5870STao Ma 						    copied, page);
1119f19d5870STao Ma 	else
1120f19d5870STao Ma 		copied = block_write_end(file, mapping, pos,
1121f19d5870STao Ma 					 len, copied, page, fsdata);
1122f8514083SAneesh Kumar K.V 
1123f8514083SAneesh Kumar K.V 	/*
1124f8514083SAneesh Kumar K.V 	 * No need to use i_size_read() here, the i_size
1125eed4333fSZheng Liu 	 * cannot change under us because we hole i_mutex.
1126f8514083SAneesh Kumar K.V 	 *
1127f8514083SAneesh Kumar K.V 	 * But it's important to update i_size while still holding page lock:
1128f8514083SAneesh Kumar K.V 	 * page writeout could otherwise come in and zero beyond i_size.
1129f8514083SAneesh Kumar K.V 	 */
1130f8514083SAneesh Kumar K.V 	if (pos + copied > inode->i_size) {
1131f8514083SAneesh Kumar K.V 		i_size_write(inode, pos + copied);
1132f8514083SAneesh Kumar K.V 		i_size_changed = 1;
1133f8514083SAneesh Kumar K.V 	}
1134f8514083SAneesh Kumar K.V 
1135f8514083SAneesh Kumar K.V 	if (pos + copied > EXT4_I(inode)->i_disksize) {
1136f8514083SAneesh Kumar K.V 		/* We need to mark inode dirty even if
1137f8514083SAneesh Kumar K.V 		 * new_i_size is less that inode->i_size
1138eed4333fSZheng Liu 		 * but greater than i_disksize. (hint delalloc)
1139f8514083SAneesh Kumar K.V 		 */
1140f8514083SAneesh Kumar K.V 		ext4_update_i_disksize(inode, (pos + copied));
1141f8514083SAneesh Kumar K.V 		i_size_changed = 1;
1142f8514083SAneesh Kumar K.V 	}
1143f8514083SAneesh Kumar K.V 	unlock_page(page);
1144f8514083SAneesh Kumar K.V 	page_cache_release(page);
1145f8514083SAneesh Kumar K.V 
1146f8514083SAneesh Kumar K.V 	/*
1147f8514083SAneesh Kumar K.V 	 * Don't mark the inode dirty under page lock. First, it unnecessarily
1148f8514083SAneesh Kumar K.V 	 * makes the holding time of page lock longer. Second, it forces lock
1149f8514083SAneesh Kumar K.V 	 * ordering of page lock and transaction start for journaling
1150f8514083SAneesh Kumar K.V 	 * filesystems.
1151f8514083SAneesh Kumar K.V 	 */
1152f8514083SAneesh Kumar K.V 	if (i_size_changed)
1153f8514083SAneesh Kumar K.V 		ext4_mark_inode_dirty(handle, inode);
1154f8514083SAneesh Kumar K.V 
115574d553aaSTheodore Ts'o 	if (copied < 0)
115674d553aaSTheodore Ts'o 		ret = copied;
1157ffacfa7aSJan Kara 	if (pos + len > inode->i_size && ext4_can_truncate(inode))
1158f8514083SAneesh Kumar K.V 		/* if we have allocated more blocks and copied
1159f8514083SAneesh Kumar K.V 		 * less. We will have blocks allocated outside
1160f8514083SAneesh Kumar K.V 		 * inode->i_size. So truncate them
1161f8514083SAneesh Kumar K.V 		 */
1162f8514083SAneesh Kumar K.V 		ext4_orphan_add(handle, inode);
116374d553aaSTheodore Ts'o errout:
1164617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1165ac27a0ecSDave Kleikamp 	if (!ret)
1166ac27a0ecSDave Kleikamp 		ret = ret2;
1167bfc1af65SNick Piggin 
1168f8514083SAneesh Kumar K.V 	if (pos + len > inode->i_size) {
1169b9a4207dSJan Kara 		ext4_truncate_failed_write(inode);
1170f8514083SAneesh Kumar K.V 		/*
1171ffacfa7aSJan Kara 		 * If truncate failed early the inode might still be
1172f8514083SAneesh Kumar K.V 		 * on the orphan list; we need to make sure the inode
1173f8514083SAneesh Kumar K.V 		 * is removed from the orphan list in that case.
1174f8514083SAneesh Kumar K.V 		 */
1175f8514083SAneesh Kumar K.V 		if (inode->i_nlink)
1176f8514083SAneesh Kumar K.V 			ext4_orphan_del(NULL, inode);
1177f8514083SAneesh Kumar K.V 	}
1178f8514083SAneesh Kumar K.V 
1179bfc1af65SNick Piggin 	return ret ? ret : copied;
1180ac27a0ecSDave Kleikamp }
1181ac27a0ecSDave Kleikamp 
1182bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file,
1183bfc1af65SNick Piggin 				     struct address_space *mapping,
1184bfc1af65SNick Piggin 				     loff_t pos, unsigned len, unsigned copied,
1185bfc1af65SNick Piggin 				     struct page *page, void *fsdata)
1186ac27a0ecSDave Kleikamp {
1187617ba13bSMingming Cao 	handle_t *handle = ext4_journal_current_handle();
1188bfc1af65SNick Piggin 	struct inode *inode = mapping->host;
1189ac27a0ecSDave Kleikamp 	int ret = 0, ret2;
1190ac27a0ecSDave Kleikamp 	int partial = 0;
1191bfc1af65SNick Piggin 	unsigned from, to;
1192cf17fea6SAneesh Kumar K.V 	loff_t new_i_size;
1193ac27a0ecSDave Kleikamp 
11949bffad1eSTheodore Ts'o 	trace_ext4_journalled_write_end(inode, pos, len, copied);
1195bfc1af65SNick Piggin 	from = pos & (PAGE_CACHE_SIZE - 1);
1196bfc1af65SNick Piggin 	to = from + len;
1197bfc1af65SNick Piggin 
1198441c8508SCurt Wohlgemuth 	BUG_ON(!ext4_handle_valid(handle));
1199441c8508SCurt Wohlgemuth 
12003fdcfb66STao Ma 	if (ext4_has_inline_data(inode))
12013fdcfb66STao Ma 		copied = ext4_write_inline_data_end(inode, pos, len,
12023fdcfb66STao Ma 						    copied, page);
12033fdcfb66STao Ma 	else {
1204bfc1af65SNick Piggin 		if (copied < len) {
1205bfc1af65SNick Piggin 			if (!PageUptodate(page))
1206bfc1af65SNick Piggin 				copied = 0;
1207bfc1af65SNick Piggin 			page_zero_new_buffers(page, from+copied, to);
1208bfc1af65SNick Piggin 		}
1209ac27a0ecSDave Kleikamp 
1210f19d5870STao Ma 		ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1211bfc1af65SNick Piggin 					     to, &partial, write_end_fn);
1212ac27a0ecSDave Kleikamp 		if (!partial)
1213ac27a0ecSDave Kleikamp 			SetPageUptodate(page);
12143fdcfb66STao Ma 	}
1215cf17fea6SAneesh Kumar K.V 	new_i_size = pos + copied;
1216cf17fea6SAneesh Kumar K.V 	if (new_i_size > inode->i_size)
1217bfc1af65SNick Piggin 		i_size_write(inode, pos+copied);
121819f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
12192d859db3SJan Kara 	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
1220cf17fea6SAneesh Kumar K.V 	if (new_i_size > EXT4_I(inode)->i_disksize) {
1221cf17fea6SAneesh Kumar K.V 		ext4_update_i_disksize(inode, new_i_size);
1222617ba13bSMingming Cao 		ret2 = ext4_mark_inode_dirty(handle, inode);
1223ac27a0ecSDave Kleikamp 		if (!ret)
1224ac27a0ecSDave Kleikamp 			ret = ret2;
1225ac27a0ecSDave Kleikamp 	}
1226bfc1af65SNick Piggin 
1227cf108bcaSJan Kara 	unlock_page(page);
1228f8514083SAneesh Kumar K.V 	page_cache_release(page);
1229ffacfa7aSJan Kara 	if (pos + len > inode->i_size && ext4_can_truncate(inode))
1230f8514083SAneesh Kumar K.V 		/* if we have allocated more blocks and copied
1231f8514083SAneesh Kumar K.V 		 * less. We will have blocks allocated outside
1232f8514083SAneesh Kumar K.V 		 * inode->i_size. So truncate them
1233f8514083SAneesh Kumar K.V 		 */
1234f8514083SAneesh Kumar K.V 		ext4_orphan_add(handle, inode);
1235f8514083SAneesh Kumar K.V 
1236617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1237ac27a0ecSDave Kleikamp 	if (!ret)
1238ac27a0ecSDave Kleikamp 		ret = ret2;
1239f8514083SAneesh Kumar K.V 	if (pos + len > inode->i_size) {
1240b9a4207dSJan Kara 		ext4_truncate_failed_write(inode);
1241f8514083SAneesh Kumar K.V 		/*
1242ffacfa7aSJan Kara 		 * If truncate failed early the inode might still be
1243f8514083SAneesh Kumar K.V 		 * on the orphan list; we need to make sure the inode
1244f8514083SAneesh Kumar K.V 		 * is removed from the orphan list in that case.
1245f8514083SAneesh Kumar K.V 		 */
1246f8514083SAneesh Kumar K.V 		if (inode->i_nlink)
1247f8514083SAneesh Kumar K.V 			ext4_orphan_del(NULL, inode);
1248f8514083SAneesh Kumar K.V 	}
1249bfc1af65SNick Piggin 
1250bfc1af65SNick Piggin 	return ret ? ret : copied;
1251ac27a0ecSDave Kleikamp }
1252d2a17637SMingming Cao 
12539d0be502STheodore Ts'o /*
1254386ad67cSLukas Czerner  * Reserve a metadata for a single block located at lblock
1255386ad67cSLukas Czerner  */
1256386ad67cSLukas Czerner static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
1257386ad67cSLukas Czerner {
1258386ad67cSLukas Czerner 	int retries = 0;
1259386ad67cSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1260386ad67cSLukas Czerner 	struct ext4_inode_info *ei = EXT4_I(inode);
1261386ad67cSLukas Czerner 	unsigned int md_needed;
1262386ad67cSLukas Czerner 	ext4_lblk_t save_last_lblock;
1263386ad67cSLukas Czerner 	int save_len;
1264386ad67cSLukas Czerner 
1265386ad67cSLukas Czerner 	/*
1266386ad67cSLukas Czerner 	 * recalculate the amount of metadata blocks to reserve
1267386ad67cSLukas Czerner 	 * in order to allocate nrblocks
1268386ad67cSLukas Czerner 	 * worse case is one extent per block
1269386ad67cSLukas Czerner 	 */
1270386ad67cSLukas Czerner repeat:
1271386ad67cSLukas Czerner 	spin_lock(&ei->i_block_reservation_lock);
1272386ad67cSLukas Czerner 	/*
1273386ad67cSLukas Czerner 	 * ext4_calc_metadata_amount() has side effects, which we have
1274386ad67cSLukas Czerner 	 * to be prepared undo if we fail to claim space.
1275386ad67cSLukas Czerner 	 */
1276386ad67cSLukas Czerner 	save_len = ei->i_da_metadata_calc_len;
1277386ad67cSLukas Czerner 	save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1278386ad67cSLukas Czerner 	md_needed = EXT4_NUM_B2C(sbi,
1279386ad67cSLukas Czerner 				 ext4_calc_metadata_amount(inode, lblock));
1280386ad67cSLukas Czerner 	trace_ext4_da_reserve_space(inode, md_needed);
1281386ad67cSLukas Czerner 
1282386ad67cSLukas Czerner 	/*
1283386ad67cSLukas Czerner 	 * We do still charge estimated metadata to the sb though;
1284386ad67cSLukas Czerner 	 * we cannot afford to run out of free blocks.
1285386ad67cSLukas Czerner 	 */
1286386ad67cSLukas Czerner 	if (ext4_claim_free_clusters(sbi, md_needed, 0)) {
1287386ad67cSLukas Czerner 		ei->i_da_metadata_calc_len = save_len;
1288386ad67cSLukas Czerner 		ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1289386ad67cSLukas Czerner 		spin_unlock(&ei->i_block_reservation_lock);
1290386ad67cSLukas Czerner 		if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1291386ad67cSLukas Czerner 			cond_resched();
1292386ad67cSLukas Czerner 			goto repeat;
1293386ad67cSLukas Czerner 		}
1294386ad67cSLukas Czerner 		return -ENOSPC;
1295386ad67cSLukas Czerner 	}
1296386ad67cSLukas Czerner 	ei->i_reserved_meta_blocks += md_needed;
1297386ad67cSLukas Czerner 	spin_unlock(&ei->i_block_reservation_lock);
1298386ad67cSLukas Czerner 
1299386ad67cSLukas Czerner 	return 0;       /* success */
1300386ad67cSLukas Czerner }
1301386ad67cSLukas Czerner 
1302386ad67cSLukas Czerner /*
13037b415bf6SAditya Kali  * Reserve a single cluster located at lblock
13049d0be502STheodore Ts'o  */
130501f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
1306d2a17637SMingming Cao {
1307030ba6bcSAneesh Kumar K.V 	int retries = 0;
1308d2a17637SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
13090637c6f4STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
13107b415bf6SAditya Kali 	unsigned int md_needed;
13115dd4056dSChristoph Hellwig 	int ret;
131203179fe9STheodore Ts'o 	ext4_lblk_t save_last_lblock;
131303179fe9STheodore Ts'o 	int save_len;
1314d2a17637SMingming Cao 
131560e58e0fSMingming Cao 	/*
131672b8ab9dSEric Sandeen 	 * We will charge metadata quota at writeout time; this saves
131772b8ab9dSEric Sandeen 	 * us from metadata over-estimation, though we may go over by
131872b8ab9dSEric Sandeen 	 * a small amount in the end.  Here we just reserve for data.
131960e58e0fSMingming Cao 	 */
13207b415bf6SAditya Kali 	ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
13215dd4056dSChristoph Hellwig 	if (ret)
13225dd4056dSChristoph Hellwig 		return ret;
132303179fe9STheodore Ts'o 
132403179fe9STheodore Ts'o 	/*
132503179fe9STheodore Ts'o 	 * recalculate the amount of metadata blocks to reserve
132603179fe9STheodore Ts'o 	 * in order to allocate nrblocks
132703179fe9STheodore Ts'o 	 * worse case is one extent per block
132803179fe9STheodore Ts'o 	 */
132903179fe9STheodore Ts'o repeat:
133003179fe9STheodore Ts'o 	spin_lock(&ei->i_block_reservation_lock);
133103179fe9STheodore Ts'o 	/*
133203179fe9STheodore Ts'o 	 * ext4_calc_metadata_amount() has side effects, which we have
133303179fe9STheodore Ts'o 	 * to be prepared undo if we fail to claim space.
133403179fe9STheodore Ts'o 	 */
133503179fe9STheodore Ts'o 	save_len = ei->i_da_metadata_calc_len;
133603179fe9STheodore Ts'o 	save_last_lblock = ei->i_da_metadata_calc_last_lblock;
133703179fe9STheodore Ts'o 	md_needed = EXT4_NUM_B2C(sbi,
133803179fe9STheodore Ts'o 				 ext4_calc_metadata_amount(inode, lblock));
133903179fe9STheodore Ts'o 	trace_ext4_da_reserve_space(inode, md_needed);
134003179fe9STheodore Ts'o 
134172b8ab9dSEric Sandeen 	/*
134272b8ab9dSEric Sandeen 	 * We do still charge estimated metadata to the sb though;
134372b8ab9dSEric Sandeen 	 * we cannot afford to run out of free blocks.
134472b8ab9dSEric Sandeen 	 */
1345e7d5f315STheodore Ts'o 	if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
134603179fe9STheodore Ts'o 		ei->i_da_metadata_calc_len = save_len;
134703179fe9STheodore Ts'o 		ei->i_da_metadata_calc_last_lblock = save_last_lblock;
134803179fe9STheodore Ts'o 		spin_unlock(&ei->i_block_reservation_lock);
1349030ba6bcSAneesh Kumar K.V 		if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1350bb8b20edSLukas Czerner 			cond_resched();
1351030ba6bcSAneesh Kumar K.V 			goto repeat;
1352030ba6bcSAneesh Kumar K.V 		}
135303179fe9STheodore Ts'o 		dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
1354d2a17637SMingming Cao 		return -ENOSPC;
1355d2a17637SMingming Cao 	}
13569d0be502STheodore Ts'o 	ei->i_reserved_data_blocks++;
13570637c6f4STheodore Ts'o 	ei->i_reserved_meta_blocks += md_needed;
13580637c6f4STheodore Ts'o 	spin_unlock(&ei->i_block_reservation_lock);
135939bc680aSDmitry Monakhov 
1360d2a17637SMingming Cao 	return 0;       /* success */
1361d2a17637SMingming Cao }
1362d2a17637SMingming Cao 
136312219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free)
1364d2a17637SMingming Cao {
1365d2a17637SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
13660637c6f4STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
1367d2a17637SMingming Cao 
1368cd213226SMingming Cao 	if (!to_free)
1369cd213226SMingming Cao 		return;		/* Nothing to release, exit */
1370cd213226SMingming Cao 
1371d2a17637SMingming Cao 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1372cd213226SMingming Cao 
13735a58ec87SLi Zefan 	trace_ext4_da_release_space(inode, to_free);
13740637c6f4STheodore Ts'o 	if (unlikely(to_free > ei->i_reserved_data_blocks)) {
1375cd213226SMingming Cao 		/*
13760637c6f4STheodore Ts'o 		 * if there aren't enough reserved blocks, then the
13770637c6f4STheodore Ts'o 		 * counter is messed up somewhere.  Since this
13780637c6f4STheodore Ts'o 		 * function is called from invalidate page, it's
13790637c6f4STheodore Ts'o 		 * harmless to return without any action.
1380cd213226SMingming Cao 		 */
13818de5c325STheodore Ts'o 		ext4_warning(inode->i_sb, "ext4_da_release_space: "
13820637c6f4STheodore Ts'o 			 "ino %lu, to_free %d with only %d reserved "
13831084f252STheodore Ts'o 			 "data blocks", inode->i_ino, to_free,
13840637c6f4STheodore Ts'o 			 ei->i_reserved_data_blocks);
13850637c6f4STheodore Ts'o 		WARN_ON(1);
13860637c6f4STheodore Ts'o 		to_free = ei->i_reserved_data_blocks;
13870637c6f4STheodore Ts'o 	}
13880637c6f4STheodore Ts'o 	ei->i_reserved_data_blocks -= to_free;
13890637c6f4STheodore Ts'o 
13900637c6f4STheodore Ts'o 	if (ei->i_reserved_data_blocks == 0) {
13910637c6f4STheodore Ts'o 		/*
13920637c6f4STheodore Ts'o 		 * We can release all of the reserved metadata blocks
13930637c6f4STheodore Ts'o 		 * only when we have written all of the delayed
13940637c6f4STheodore Ts'o 		 * allocation blocks.
13957b415bf6SAditya Kali 		 * Note that in case of bigalloc, i_reserved_meta_blocks,
13967b415bf6SAditya Kali 		 * i_reserved_data_blocks, etc. refer to number of clusters.
13970637c6f4STheodore Ts'o 		 */
139857042651STheodore Ts'o 		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
139972b8ab9dSEric Sandeen 				   ei->i_reserved_meta_blocks);
1400ee5f4d9cSTheodore Ts'o 		ei->i_reserved_meta_blocks = 0;
14019d0be502STheodore Ts'o 		ei->i_da_metadata_calc_len = 0;
1402cd213226SMingming Cao 	}
1403cd213226SMingming Cao 
140472b8ab9dSEric Sandeen 	/* update fs dirty data blocks counter */
140557042651STheodore Ts'o 	percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
1406d2a17637SMingming Cao 
1407d2a17637SMingming Cao 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
140860e58e0fSMingming Cao 
14097b415bf6SAditya Kali 	dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
1410d2a17637SMingming Cao }
1411d2a17637SMingming Cao 
1412d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page,
1413d2a17637SMingming Cao 					     unsigned long offset)
1414d2a17637SMingming Cao {
1415d2a17637SMingming Cao 	int to_release = 0;
1416d2a17637SMingming Cao 	struct buffer_head *head, *bh;
1417d2a17637SMingming Cao 	unsigned int curr_off = 0;
14187b415bf6SAditya Kali 	struct inode *inode = page->mapping->host;
14197b415bf6SAditya Kali 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
14207b415bf6SAditya Kali 	int num_clusters;
142151865fdaSZheng Liu 	ext4_fsblk_t lblk;
1422d2a17637SMingming Cao 
1423d2a17637SMingming Cao 	head = page_buffers(page);
1424d2a17637SMingming Cao 	bh = head;
1425d2a17637SMingming Cao 	do {
1426d2a17637SMingming Cao 		unsigned int next_off = curr_off + bh->b_size;
1427d2a17637SMingming Cao 
1428d2a17637SMingming Cao 		if ((offset <= curr_off) && (buffer_delay(bh))) {
1429d2a17637SMingming Cao 			to_release++;
1430d2a17637SMingming Cao 			clear_buffer_delay(bh);
1431d2a17637SMingming Cao 		}
1432d2a17637SMingming Cao 		curr_off = next_off;
1433d2a17637SMingming Cao 	} while ((bh = bh->b_this_page) != head);
14347b415bf6SAditya Kali 
143551865fdaSZheng Liu 	if (to_release) {
143651865fdaSZheng Liu 		lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
143751865fdaSZheng Liu 		ext4_es_remove_extent(inode, lblk, to_release);
143851865fdaSZheng Liu 	}
143951865fdaSZheng Liu 
14407b415bf6SAditya Kali 	/* If we have released all the blocks belonging to a cluster, then we
14417b415bf6SAditya Kali 	 * need to release the reserved space for that cluster. */
14427b415bf6SAditya Kali 	num_clusters = EXT4_NUM_B2C(sbi, to_release);
14437b415bf6SAditya Kali 	while (num_clusters > 0) {
14447b415bf6SAditya Kali 		lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
14457b415bf6SAditya Kali 			((num_clusters - 1) << sbi->s_cluster_bits);
14467b415bf6SAditya Kali 		if (sbi->s_cluster_ratio == 1 ||
14477d1b1fbcSZheng Liu 		    !ext4_find_delalloc_cluster(inode, lblk))
14487b415bf6SAditya Kali 			ext4_da_release_space(inode, 1);
14497b415bf6SAditya Kali 
14507b415bf6SAditya Kali 		num_clusters--;
14517b415bf6SAditya Kali 	}
1452d2a17637SMingming Cao }
1453ac27a0ecSDave Kleikamp 
1454ac27a0ecSDave Kleikamp /*
145564769240SAlex Tomas  * Delayed allocation stuff
145664769240SAlex Tomas  */
145764769240SAlex Tomas 
145864769240SAlex Tomas /*
145964769240SAlex Tomas  * mpage_da_submit_io - walks through extent of pages and try to write
1460a1d6cc56SAneesh Kumar K.V  * them with writepage() call back
146164769240SAlex Tomas  *
146264769240SAlex Tomas  * @mpd->inode: inode
146364769240SAlex Tomas  * @mpd->first_page: first page of the extent
146464769240SAlex Tomas  * @mpd->next_page: page after the last page of the extent
146564769240SAlex Tomas  *
146664769240SAlex Tomas  * By the time mpage_da_submit_io() is called we expect all blocks
146764769240SAlex Tomas  * to be allocated. this may be wrong if allocation failed.
146864769240SAlex Tomas  *
146964769240SAlex Tomas  * As pages are already locked by write_cache_pages(), we can't use it
147064769240SAlex Tomas  */
14711de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd,
14721de3e3dfSTheodore Ts'o 			      struct ext4_map_blocks *map)
147364769240SAlex Tomas {
1474791b7f08SAneesh Kumar K.V 	struct pagevec pvec;
1475791b7f08SAneesh Kumar K.V 	unsigned long index, end;
1476791b7f08SAneesh Kumar K.V 	int ret = 0, err, nr_pages, i;
1477791b7f08SAneesh Kumar K.V 	struct inode *inode = mpd->inode;
1478791b7f08SAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
1479cb20d518STheodore Ts'o 	loff_t size = i_size_read(inode);
14803ecdb3a1STheodore Ts'o 	unsigned int len, block_start;
14813ecdb3a1STheodore Ts'o 	struct buffer_head *bh, *page_bufs = NULL;
14821de3e3dfSTheodore Ts'o 	sector_t pblock = 0, cur_logical = 0;
1483bd2d0210STheodore Ts'o 	struct ext4_io_submit io_submit;
148464769240SAlex Tomas 
148564769240SAlex Tomas 	BUG_ON(mpd->next_page <= mpd->first_page);
1486*4eec708dSJan Kara 	ext4_io_submit_init(&io_submit, mpd->wbc);
1487*4eec708dSJan Kara 	io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
1488*4eec708dSJan Kara 	if (!io_submit.io_end)
1489*4eec708dSJan Kara 		return -ENOMEM;
1490791b7f08SAneesh Kumar K.V 	/*
1491791b7f08SAneesh Kumar K.V 	 * We need to start from the first_page to the next_page - 1
1492791b7f08SAneesh Kumar K.V 	 * to make sure we also write the mapped dirty buffer_heads.
14938dc207c0STheodore Ts'o 	 * If we look at mpd->b_blocknr we would only be looking
1494791b7f08SAneesh Kumar K.V 	 * at the currently mapped buffer_heads.
1495791b7f08SAneesh Kumar K.V 	 */
149664769240SAlex Tomas 	index = mpd->first_page;
149764769240SAlex Tomas 	end = mpd->next_page - 1;
149864769240SAlex Tomas 
1499791b7f08SAneesh Kumar K.V 	pagevec_init(&pvec, 0);
150064769240SAlex Tomas 	while (index <= end) {
1501791b7f08SAneesh Kumar K.V 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
150264769240SAlex Tomas 		if (nr_pages == 0)
150364769240SAlex Tomas 			break;
150464769240SAlex Tomas 		for (i = 0; i < nr_pages; i++) {
1505f8bec370SJan Kara 			int skip_page = 0;
150664769240SAlex Tomas 			struct page *page = pvec.pages[i];
150764769240SAlex Tomas 
1508791b7f08SAneesh Kumar K.V 			index = page->index;
1509791b7f08SAneesh Kumar K.V 			if (index > end)
1510791b7f08SAneesh Kumar K.V 				break;
1511cb20d518STheodore Ts'o 
1512cb20d518STheodore Ts'o 			if (index == size >> PAGE_CACHE_SHIFT)
1513cb20d518STheodore Ts'o 				len = size & ~PAGE_CACHE_MASK;
1514cb20d518STheodore Ts'o 			else
1515cb20d518STheodore Ts'o 				len = PAGE_CACHE_SIZE;
15161de3e3dfSTheodore Ts'o 			if (map) {
15171de3e3dfSTheodore Ts'o 				cur_logical = index << (PAGE_CACHE_SHIFT -
15181de3e3dfSTheodore Ts'o 							inode->i_blkbits);
15191de3e3dfSTheodore Ts'o 				pblock = map->m_pblk + (cur_logical -
15201de3e3dfSTheodore Ts'o 							map->m_lblk);
15211de3e3dfSTheodore Ts'o 			}
1522791b7f08SAneesh Kumar K.V 			index++;
1523791b7f08SAneesh Kumar K.V 
1524791b7f08SAneesh Kumar K.V 			BUG_ON(!PageLocked(page));
1525791b7f08SAneesh Kumar K.V 			BUG_ON(PageWriteback(page));
1526791b7f08SAneesh Kumar K.V 
15273ecdb3a1STheodore Ts'o 			bh = page_bufs = page_buffers(page);
15283ecdb3a1STheodore Ts'o 			block_start = 0;
15293ecdb3a1STheodore Ts'o 			do {
15301de3e3dfSTheodore Ts'o 				if (map && (cur_logical >= map->m_lblk) &&
15311de3e3dfSTheodore Ts'o 				    (cur_logical <= (map->m_lblk +
15321de3e3dfSTheodore Ts'o 						     (map->m_len - 1)))) {
15331de3e3dfSTheodore Ts'o 					if (buffer_delay(bh)) {
15341de3e3dfSTheodore Ts'o 						clear_buffer_delay(bh);
15351de3e3dfSTheodore Ts'o 						bh->b_blocknr = pblock;
15361de3e3dfSTheodore Ts'o 					}
15371de3e3dfSTheodore Ts'o 					if (buffer_unwritten(bh) ||
15381de3e3dfSTheodore Ts'o 					    buffer_mapped(bh))
15391de3e3dfSTheodore Ts'o 						BUG_ON(bh->b_blocknr != pblock);
15401de3e3dfSTheodore Ts'o 					if (map->m_flags & EXT4_MAP_UNINIT)
15411de3e3dfSTheodore Ts'o 						set_buffer_uninit(bh);
15421de3e3dfSTheodore Ts'o 					clear_buffer_unwritten(bh);
15431de3e3dfSTheodore Ts'o 				}
15441de3e3dfSTheodore Ts'o 
154513a79a47SYongqiang Yang 				/*
154613a79a47SYongqiang Yang 				 * skip page if block allocation undone and
154713a79a47SYongqiang Yang 				 * block is dirty
154813a79a47SYongqiang Yang 				 */
154913a79a47SYongqiang Yang 				if (ext4_bh_delay_or_unwritten(NULL, bh))
155097498956STheodore Ts'o 					skip_page = 1;
15513ecdb3a1STheodore Ts'o 				bh = bh->b_this_page;
15523ecdb3a1STheodore Ts'o 				block_start += bh->b_size;
15531de3e3dfSTheodore Ts'o 				cur_logical++;
15541de3e3dfSTheodore Ts'o 				pblock++;
15551de3e3dfSTheodore Ts'o 			} while (bh != page_bufs);
15561de3e3dfSTheodore Ts'o 
1557f8bec370SJan Kara 			if (skip_page) {
1558f8bec370SJan Kara 				unlock_page(page);
1559f8bec370SJan Kara 				continue;
1560f8bec370SJan Kara 			}
1561cb20d518STheodore Ts'o 
156297498956STheodore Ts'o 			clear_page_dirty_for_io(page);
1563fe089c77SJan Kara 			err = ext4_bio_write_page(&io_submit, page, len,
1564fe089c77SJan Kara 						  mpd->wbc);
1565cb20d518STheodore Ts'o 			if (!err)
1566a1d6cc56SAneesh Kumar K.V 				mpd->pages_written++;
156764769240SAlex Tomas 			/*
156864769240SAlex Tomas 			 * In error case, we have to continue because
156964769240SAlex Tomas 			 * remaining pages are still locked
157064769240SAlex Tomas 			 */
157164769240SAlex Tomas 			if (ret == 0)
157264769240SAlex Tomas 				ret = err;
157364769240SAlex Tomas 		}
157464769240SAlex Tomas 		pagevec_release(&pvec);
157564769240SAlex Tomas 	}
1576bd2d0210STheodore Ts'o 	ext4_io_submit(&io_submit);
1577*4eec708dSJan Kara 	/* Drop io_end reference we got from init */
1578*4eec708dSJan Kara 	ext4_put_io_end_defer(io_submit.io_end);
157964769240SAlex Tomas 	return ret;
158064769240SAlex Tomas }
158164769240SAlex Tomas 
1582c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
1583c4a0c46eSAneesh Kumar K.V {
1584c4a0c46eSAneesh Kumar K.V 	int nr_pages, i;
1585c4a0c46eSAneesh Kumar K.V 	pgoff_t index, end;
1586c4a0c46eSAneesh Kumar K.V 	struct pagevec pvec;
1587c4a0c46eSAneesh Kumar K.V 	struct inode *inode = mpd->inode;
1588c4a0c46eSAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
158951865fdaSZheng Liu 	ext4_lblk_t start, last;
1590c4a0c46eSAneesh Kumar K.V 
1591c7f5938aSCurt Wohlgemuth 	index = mpd->first_page;
1592c7f5938aSCurt Wohlgemuth 	end   = mpd->next_page - 1;
159351865fdaSZheng Liu 
159451865fdaSZheng Liu 	start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
159551865fdaSZheng Liu 	last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
159651865fdaSZheng Liu 	ext4_es_remove_extent(inode, start, last - start + 1);
159751865fdaSZheng Liu 
159866bea92cSEric Sandeen 	pagevec_init(&pvec, 0);
1599c4a0c46eSAneesh Kumar K.V 	while (index <= end) {
1600c4a0c46eSAneesh Kumar K.V 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1601c4a0c46eSAneesh Kumar K.V 		if (nr_pages == 0)
1602c4a0c46eSAneesh Kumar K.V 			break;
1603c4a0c46eSAneesh Kumar K.V 		for (i = 0; i < nr_pages; i++) {
1604c4a0c46eSAneesh Kumar K.V 			struct page *page = pvec.pages[i];
16059b1d0998SJan Kara 			if (page->index > end)
1606c4a0c46eSAneesh Kumar K.V 				break;
1607c4a0c46eSAneesh Kumar K.V 			BUG_ON(!PageLocked(page));
1608c4a0c46eSAneesh Kumar K.V 			BUG_ON(PageWriteback(page));
1609c4a0c46eSAneesh Kumar K.V 			block_invalidatepage(page, 0);
1610c4a0c46eSAneesh Kumar K.V 			ClearPageUptodate(page);
1611c4a0c46eSAneesh Kumar K.V 			unlock_page(page);
1612c4a0c46eSAneesh Kumar K.V 		}
16139b1d0998SJan Kara 		index = pvec.pages[nr_pages - 1]->index + 1;
16149b1d0998SJan Kara 		pagevec_release(&pvec);
1615c4a0c46eSAneesh Kumar K.V 	}
1616c4a0c46eSAneesh Kumar K.V 	return;
1617c4a0c46eSAneesh Kumar K.V }
1618c4a0c46eSAneesh Kumar K.V 
1619df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode)
1620df22291fSAneesh Kumar K.V {
1621df22291fSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
162292b97816STheodore Ts'o 	struct super_block *sb = inode->i_sb;
1623f78ee70dSLukas Czerner 	struct ext4_inode_info *ei = EXT4_I(inode);
162492b97816STheodore Ts'o 
162592b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
16265dee5437STheodore Ts'o 	       EXT4_C2B(EXT4_SB(inode->i_sb),
1627f78ee70dSLukas Czerner 			ext4_count_free_clusters(sb)));
162892b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
162992b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
1630f78ee70dSLukas Czerner 	       (long long) EXT4_C2B(EXT4_SB(sb),
163157042651STheodore Ts'o 		percpu_counter_sum(&sbi->s_freeclusters_counter)));
163292b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
1633f78ee70dSLukas Czerner 	       (long long) EXT4_C2B(EXT4_SB(sb),
16347b415bf6SAditya Kali 		percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
163592b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "Block reservation details");
163692b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
1637f78ee70dSLukas Czerner 		 ei->i_reserved_data_blocks);
163892b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
1639f78ee70dSLukas Czerner 	       ei->i_reserved_meta_blocks);
1640f78ee70dSLukas Czerner 	ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u",
1641f78ee70dSLukas Czerner 	       ei->i_allocated_meta_blocks);
1642df22291fSAneesh Kumar K.V 	return;
1643df22291fSAneesh Kumar K.V }
1644df22291fSAneesh Kumar K.V 
1645b920c755STheodore Ts'o /*
16465a87b7a5STheodore Ts'o  * mpage_da_map_and_submit - go through given space, map them
16475a87b7a5STheodore Ts'o  *       if necessary, and then submit them for I/O
164864769240SAlex Tomas  *
16498dc207c0STheodore Ts'o  * @mpd - bh describing space
165064769240SAlex Tomas  *
165164769240SAlex Tomas  * The function skips space we know is already mapped to disk blocks.
165264769240SAlex Tomas  *
165364769240SAlex Tomas  */
16545a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
165564769240SAlex Tomas {
16562ac3b6e0STheodore Ts'o 	int err, blks, get_blocks_flags;
16571de3e3dfSTheodore Ts'o 	struct ext4_map_blocks map, *mapp = NULL;
16582fa3cdfbSTheodore Ts'o 	sector_t next = mpd->b_blocknr;
16592fa3cdfbSTheodore Ts'o 	unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
16602fa3cdfbSTheodore Ts'o 	loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
16612fa3cdfbSTheodore Ts'o 	handle_t *handle = NULL;
166264769240SAlex Tomas 
166364769240SAlex Tomas 	/*
16645a87b7a5STheodore Ts'o 	 * If the blocks are mapped already, or we couldn't accumulate
16655a87b7a5STheodore Ts'o 	 * any blocks, then proceed immediately to the submission stage.
166664769240SAlex Tomas 	 */
16675a87b7a5STheodore Ts'o 	if ((mpd->b_size == 0) ||
16685a87b7a5STheodore Ts'o 	    ((mpd->b_state  & (1 << BH_Mapped)) &&
166929fa89d0SAneesh Kumar K.V 	     !(mpd->b_state & (1 << BH_Delay)) &&
16705a87b7a5STheodore Ts'o 	     !(mpd->b_state & (1 << BH_Unwritten))))
16715a87b7a5STheodore Ts'o 		goto submit_io;
16722fa3cdfbSTheodore Ts'o 
16732fa3cdfbSTheodore Ts'o 	handle = ext4_journal_current_handle();
16742fa3cdfbSTheodore Ts'o 	BUG_ON(!handle);
16752fa3cdfbSTheodore Ts'o 
167679ffab34SAneesh Kumar K.V 	/*
167779e83036SEric Sandeen 	 * Call ext4_map_blocks() to allocate any delayed allocation
16782ac3b6e0STheodore Ts'o 	 * blocks, or to convert an uninitialized extent to be
16792ac3b6e0STheodore Ts'o 	 * initialized (in the case where we have written into
16802ac3b6e0STheodore Ts'o 	 * one or more preallocated blocks).
16812ac3b6e0STheodore Ts'o 	 *
16822ac3b6e0STheodore Ts'o 	 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
16832ac3b6e0STheodore Ts'o 	 * indicate that we are on the delayed allocation path.  This
16842ac3b6e0STheodore Ts'o 	 * affects functions in many different parts of the allocation
16852ac3b6e0STheodore Ts'o 	 * call path.  This flag exists primarily because we don't
168679e83036SEric Sandeen 	 * want to change *many* call functions, so ext4_map_blocks()
1687f2321097STheodore Ts'o 	 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
16882ac3b6e0STheodore Ts'o 	 * inode's allocation semaphore is taken.
16892ac3b6e0STheodore Ts'o 	 *
16902ac3b6e0STheodore Ts'o 	 * If the blocks in questions were delalloc blocks, set
16912ac3b6e0STheodore Ts'o 	 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
16922ac3b6e0STheodore Ts'o 	 * variables are updated after the blocks have been allocated.
169379ffab34SAneesh Kumar K.V 	 */
16942ed88685STheodore Ts'o 	map.m_lblk = next;
16952ed88685STheodore Ts'o 	map.m_len = max_blocks;
169627dd4385SLukas Czerner 	/*
169727dd4385SLukas Czerner 	 * We're in delalloc path and it is possible that we're going to
169827dd4385SLukas Czerner 	 * need more metadata blocks than previously reserved. However
169927dd4385SLukas Czerner 	 * we must not fail because we're in writeback and there is
170027dd4385SLukas Czerner 	 * nothing we can do about it so it might result in data loss.
170127dd4385SLukas Czerner 	 * So use reserved blocks to allocate metadata if possible.
170227dd4385SLukas Czerner 	 */
170327dd4385SLukas Czerner 	get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
170427dd4385SLukas Czerner 			   EXT4_GET_BLOCKS_METADATA_NOFAIL;
1705744692dcSJiaying Zhang 	if (ext4_should_dioread_nolock(mpd->inode))
1706744692dcSJiaying Zhang 		get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
17072ac3b6e0STheodore Ts'o 	if (mpd->b_state & (1 << BH_Delay))
17081296cc85SAneesh Kumar K.V 		get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
17091296cc85SAneesh Kumar K.V 
171027dd4385SLukas Czerner 
17112ed88685STheodore Ts'o 	blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
17122fa3cdfbSTheodore Ts'o 	if (blks < 0) {
1713e3570639SEric Sandeen 		struct super_block *sb = mpd->inode->i_sb;
1714e3570639SEric Sandeen 
17152fa3cdfbSTheodore Ts'o 		err = blks;
1716ed5bde0bSTheodore Ts'o 		/*
17175a87b7a5STheodore Ts'o 		 * If get block returns EAGAIN or ENOSPC and there
171897498956STheodore Ts'o 		 * appears to be free blocks we will just let
171997498956STheodore Ts'o 		 * mpage_da_submit_io() unlock all of the pages.
1720c4a0c46eSAneesh Kumar K.V 		 */
1721c4a0c46eSAneesh Kumar K.V 		if (err == -EAGAIN)
17225a87b7a5STheodore Ts'o 			goto submit_io;
1723df22291fSAneesh Kumar K.V 
17245dee5437STheodore Ts'o 		if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
1725df22291fSAneesh Kumar K.V 			mpd->retval = err;
17265a87b7a5STheodore Ts'o 			goto submit_io;
1727df22291fSAneesh Kumar K.V 		}
1728df22291fSAneesh Kumar K.V 
1729c4a0c46eSAneesh Kumar K.V 		/*
1730ed5bde0bSTheodore Ts'o 		 * get block failure will cause us to loop in
1731ed5bde0bSTheodore Ts'o 		 * writepages, because a_ops->writepage won't be able
1732ed5bde0bSTheodore Ts'o 		 * to make progress. The page will be redirtied by
1733ed5bde0bSTheodore Ts'o 		 * writepage and writepages will again try to write
1734ed5bde0bSTheodore Ts'o 		 * the same.
1735c4a0c46eSAneesh Kumar K.V 		 */
1736e3570639SEric Sandeen 		if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
1737e3570639SEric Sandeen 			ext4_msg(sb, KERN_CRIT,
1738e3570639SEric Sandeen 				 "delayed block allocation failed for inode %lu "
1739e3570639SEric Sandeen 				 "at logical offset %llu with max blocks %zd "
1740e3570639SEric Sandeen 				 "with error %d", mpd->inode->i_ino,
1741c4a0c46eSAneesh Kumar K.V 				 (unsigned long long) next,
17428dc207c0STheodore Ts'o 				 mpd->b_size >> mpd->inode->i_blkbits, err);
1743e3570639SEric Sandeen 			ext4_msg(sb, KERN_CRIT,
174401a523ebSTheodore Ts'o 				"This should not happen!! Data will be lost");
1745e3570639SEric Sandeen 			if (err == -ENOSPC)
1746df22291fSAneesh Kumar K.V 				ext4_print_free_blocks(mpd->inode);
1747030ba6bcSAneesh Kumar K.V 		}
17482fa3cdfbSTheodore Ts'o 		/* invalidate all the pages */
1749c7f5938aSCurt Wohlgemuth 		ext4_da_block_invalidatepages(mpd);
1750e0fd9b90SCurt Wohlgemuth 
1751e0fd9b90SCurt Wohlgemuth 		/* Mark this page range as having been completed */
1752e0fd9b90SCurt Wohlgemuth 		mpd->io_done = 1;
17535a87b7a5STheodore Ts'o 		return;
1754c4a0c46eSAneesh Kumar K.V 	}
17552fa3cdfbSTheodore Ts'o 	BUG_ON(blks == 0);
17562fa3cdfbSTheodore Ts'o 
17571de3e3dfSTheodore Ts'o 	mapp = &map;
17582ed88685STheodore Ts'o 	if (map.m_flags & EXT4_MAP_NEW) {
17592ed88685STheodore Ts'o 		struct block_device *bdev = mpd->inode->i_sb->s_bdev;
17602ed88685STheodore Ts'o 		int i;
176164769240SAlex Tomas 
17622ed88685STheodore Ts'o 		for (i = 0; i < map.m_len; i++)
17632ed88685STheodore Ts'o 			unmap_underlying_metadata(bdev, map.m_pblk + i);
17642fa3cdfbSTheodore Ts'o 	}
17652fa3cdfbSTheodore Ts'o 
17662fa3cdfbSTheodore Ts'o 	/*
176703f5d8bcSJan Kara 	 * Update on-disk size along with block allocation.
17682fa3cdfbSTheodore Ts'o 	 */
17692fa3cdfbSTheodore Ts'o 	disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
17702fa3cdfbSTheodore Ts'o 	if (disksize > i_size_read(mpd->inode))
17712fa3cdfbSTheodore Ts'o 		disksize = i_size_read(mpd->inode);
17722fa3cdfbSTheodore Ts'o 	if (disksize > EXT4_I(mpd->inode)->i_disksize) {
17732fa3cdfbSTheodore Ts'o 		ext4_update_i_disksize(mpd->inode, disksize);
17745a87b7a5STheodore Ts'o 		err = ext4_mark_inode_dirty(handle, mpd->inode);
17755a87b7a5STheodore Ts'o 		if (err)
17765a87b7a5STheodore Ts'o 			ext4_error(mpd->inode->i_sb,
17775a87b7a5STheodore Ts'o 				   "Failed to mark inode %lu dirty",
17785a87b7a5STheodore Ts'o 				   mpd->inode->i_ino);
17792fa3cdfbSTheodore Ts'o 	}
17802fa3cdfbSTheodore Ts'o 
17815a87b7a5STheodore Ts'o submit_io:
17821de3e3dfSTheodore Ts'o 	mpage_da_submit_io(mpd, mapp);
17835a87b7a5STheodore Ts'o 	mpd->io_done = 1;
178464769240SAlex Tomas }
178564769240SAlex Tomas 
1786bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1787bf068ee2SAneesh Kumar K.V 		(1 << BH_Delay) | (1 << BH_Unwritten))
178864769240SAlex Tomas 
178964769240SAlex Tomas /*
179064769240SAlex Tomas  * mpage_add_bh_to_extent - try to add one more block to extent of blocks
179164769240SAlex Tomas  *
179264769240SAlex Tomas  * @mpd->lbh - extent of blocks
179364769240SAlex Tomas  * @logical - logical number of the block in the file
1794b6a8e62fSJan Kara  * @b_state - b_state of the buffer head added
179564769240SAlex Tomas  *
179664769240SAlex Tomas  * the function is used to collect contig. blocks in same state
179764769240SAlex Tomas  */
1798b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical,
17998dc207c0STheodore Ts'o 				   unsigned long b_state)
180064769240SAlex Tomas {
180164769240SAlex Tomas 	sector_t next;
1802b6a8e62fSJan Kara 	int blkbits = mpd->inode->i_blkbits;
1803b6a8e62fSJan Kara 	int nrblocks = mpd->b_size >> blkbits;
180464769240SAlex Tomas 
1805c445e3e0SEric Sandeen 	/*
1806c445e3e0SEric Sandeen 	 * XXX Don't go larger than mballoc is willing to allocate
1807c445e3e0SEric Sandeen 	 * This is a stopgap solution.  We eventually need to fold
1808c445e3e0SEric Sandeen 	 * mpage_da_submit_io() into this function and then call
180979e83036SEric Sandeen 	 * ext4_map_blocks() multiple times in a loop
1810c445e3e0SEric Sandeen 	 */
1811b6a8e62fSJan Kara 	if (nrblocks >= (8*1024*1024 >> blkbits))
1812c445e3e0SEric Sandeen 		goto flush_it;
1813c445e3e0SEric Sandeen 
1814525f4ed8SMingming Cao 	/* check if the reserved journal credits might overflow */
1815b6a8e62fSJan Kara 	if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) {
1816525f4ed8SMingming Cao 		if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1817525f4ed8SMingming Cao 			/*
1818525f4ed8SMingming Cao 			 * With non-extent format we are limited by the journal
1819525f4ed8SMingming Cao 			 * credit available.  Total credit needed to insert
1820525f4ed8SMingming Cao 			 * nrblocks contiguous blocks is dependent on the
1821525f4ed8SMingming Cao 			 * nrblocks.  So limit nrblocks.
1822525f4ed8SMingming Cao 			 */
1823525f4ed8SMingming Cao 			goto flush_it;
1824525f4ed8SMingming Cao 		}
1825525f4ed8SMingming Cao 	}
182664769240SAlex Tomas 	/*
182764769240SAlex Tomas 	 * First block in the extent
182864769240SAlex Tomas 	 */
18298dc207c0STheodore Ts'o 	if (mpd->b_size == 0) {
18308dc207c0STheodore Ts'o 		mpd->b_blocknr = logical;
1831b6a8e62fSJan Kara 		mpd->b_size = 1 << blkbits;
18328dc207c0STheodore Ts'o 		mpd->b_state = b_state & BH_FLAGS;
183364769240SAlex Tomas 		return;
183464769240SAlex Tomas 	}
183564769240SAlex Tomas 
18368dc207c0STheodore Ts'o 	next = mpd->b_blocknr + nrblocks;
183764769240SAlex Tomas 	/*
183864769240SAlex Tomas 	 * Can we merge the block to our big extent?
183964769240SAlex Tomas 	 */
18408dc207c0STheodore Ts'o 	if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
1841b6a8e62fSJan Kara 		mpd->b_size += 1 << blkbits;
184264769240SAlex Tomas 		return;
184364769240SAlex Tomas 	}
184464769240SAlex Tomas 
1845525f4ed8SMingming Cao flush_it:
184664769240SAlex Tomas 	/*
184764769240SAlex Tomas 	 * We couldn't merge the block to our extent, so we
184864769240SAlex Tomas 	 * need to flush current  extent and start new one
184964769240SAlex Tomas 	 */
18505a87b7a5STheodore Ts'o 	mpage_da_map_and_submit(mpd);
1851a1d6cc56SAneesh Kumar K.V 	return;
185264769240SAlex Tomas }
185364769240SAlex Tomas 
1854c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
185529fa89d0SAneesh Kumar K.V {
1856c364b22cSAneesh Kumar K.V 	return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
185729fa89d0SAneesh Kumar K.V }
185829fa89d0SAneesh Kumar K.V 
185964769240SAlex Tomas /*
18605356f261SAditya Kali  * This function is grabs code from the very beginning of
18615356f261SAditya Kali  * ext4_map_blocks, but assumes that the caller is from delayed write
18625356f261SAditya Kali  * time. This function looks up the requested blocks and sets the
18635356f261SAditya Kali  * buffer delay bit under the protection of i_data_sem.
18645356f261SAditya Kali  */
18655356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
18665356f261SAditya Kali 			      struct ext4_map_blocks *map,
18675356f261SAditya Kali 			      struct buffer_head *bh)
18685356f261SAditya Kali {
1869d100eef2SZheng Liu 	struct extent_status es;
18705356f261SAditya Kali 	int retval;
18715356f261SAditya Kali 	sector_t invalid_block = ~((sector_t) 0xffff);
1872921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
1873921f266bSDmitry Monakhov 	struct ext4_map_blocks orig_map;
1874921f266bSDmitry Monakhov 
1875921f266bSDmitry Monakhov 	memcpy(&orig_map, map, sizeof(*map));
1876921f266bSDmitry Monakhov #endif
18775356f261SAditya Kali 
18785356f261SAditya Kali 	if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
18795356f261SAditya Kali 		invalid_block = ~0;
18805356f261SAditya Kali 
18815356f261SAditya Kali 	map->m_flags = 0;
18825356f261SAditya Kali 	ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
18835356f261SAditya Kali 		  "logical block %lu\n", inode->i_ino, map->m_len,
18845356f261SAditya Kali 		  (unsigned long) map->m_lblk);
1885d100eef2SZheng Liu 
1886d100eef2SZheng Liu 	/* Lookup extent status tree firstly */
1887d100eef2SZheng Liu 	if (ext4_es_lookup_extent(inode, iblock, &es)) {
1888d100eef2SZheng Liu 
1889d100eef2SZheng Liu 		if (ext4_es_is_hole(&es)) {
1890d100eef2SZheng Liu 			retval = 0;
1891d100eef2SZheng Liu 			down_read((&EXT4_I(inode)->i_data_sem));
1892d100eef2SZheng Liu 			goto add_delayed;
1893d100eef2SZheng Liu 		}
1894d100eef2SZheng Liu 
1895d100eef2SZheng Liu 		/*
1896d100eef2SZheng Liu 		 * Delayed extent could be allocated by fallocate.
1897d100eef2SZheng Liu 		 * So we need to check it.
1898d100eef2SZheng Liu 		 */
1899d100eef2SZheng Liu 		if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1900d100eef2SZheng Liu 			map_bh(bh, inode->i_sb, invalid_block);
1901d100eef2SZheng Liu 			set_buffer_new(bh);
1902d100eef2SZheng Liu 			set_buffer_delay(bh);
1903d100eef2SZheng Liu 			return 0;
1904d100eef2SZheng Liu 		}
1905d100eef2SZheng Liu 
1906d100eef2SZheng Liu 		map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1907d100eef2SZheng Liu 		retval = es.es_len - (iblock - es.es_lblk);
1908d100eef2SZheng Liu 		if (retval > map->m_len)
1909d100eef2SZheng Liu 			retval = map->m_len;
1910d100eef2SZheng Liu 		map->m_len = retval;
1911d100eef2SZheng Liu 		if (ext4_es_is_written(&es))
1912d100eef2SZheng Liu 			map->m_flags |= EXT4_MAP_MAPPED;
1913d100eef2SZheng Liu 		else if (ext4_es_is_unwritten(&es))
1914d100eef2SZheng Liu 			map->m_flags |= EXT4_MAP_UNWRITTEN;
1915d100eef2SZheng Liu 		else
1916d100eef2SZheng Liu 			BUG_ON(1);
1917d100eef2SZheng Liu 
1918921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
1919921f266bSDmitry Monakhov 		ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1920921f266bSDmitry Monakhov #endif
1921d100eef2SZheng Liu 		return retval;
1922d100eef2SZheng Liu 	}
1923d100eef2SZheng Liu 
19245356f261SAditya Kali 	/*
19255356f261SAditya Kali 	 * Try to see if we can get the block without requesting a new
19265356f261SAditya Kali 	 * file system block.
19275356f261SAditya Kali 	 */
19285356f261SAditya Kali 	down_read((&EXT4_I(inode)->i_data_sem));
19299c3569b5STao Ma 	if (ext4_has_inline_data(inode)) {
19309c3569b5STao Ma 		/*
19319c3569b5STao Ma 		 * We will soon create blocks for this page, and let
19329c3569b5STao Ma 		 * us pretend as if the blocks aren't allocated yet.
19339c3569b5STao Ma 		 * In case of clusters, we have to handle the work
19349c3569b5STao Ma 		 * of mapping from cluster so that the reserved space
19359c3569b5STao Ma 		 * is calculated properly.
19369c3569b5STao Ma 		 */
19379c3569b5STao Ma 		if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
19389c3569b5STao Ma 		    ext4_find_delalloc_cluster(inode, map->m_lblk))
19399c3569b5STao Ma 			map->m_flags |= EXT4_MAP_FROM_CLUSTER;
19409c3569b5STao Ma 		retval = 0;
19419c3569b5STao Ma 	} else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
1942d100eef2SZheng Liu 		retval = ext4_ext_map_blocks(NULL, inode, map,
1943d100eef2SZheng Liu 					     EXT4_GET_BLOCKS_NO_PUT_HOLE);
19445356f261SAditya Kali 	else
1945d100eef2SZheng Liu 		retval = ext4_ind_map_blocks(NULL, inode, map,
1946d100eef2SZheng Liu 					     EXT4_GET_BLOCKS_NO_PUT_HOLE);
19475356f261SAditya Kali 
1948d100eef2SZheng Liu add_delayed:
19495356f261SAditya Kali 	if (retval == 0) {
1950f7fec032SZheng Liu 		int ret;
19515356f261SAditya Kali 		/*
19525356f261SAditya Kali 		 * XXX: __block_prepare_write() unmaps passed block,
19535356f261SAditya Kali 		 * is it OK?
19545356f261SAditya Kali 		 */
1955386ad67cSLukas Czerner 		/*
1956386ad67cSLukas Czerner 		 * If the block was allocated from previously allocated cluster,
1957386ad67cSLukas Czerner 		 * then we don't need to reserve it again. However we still need
1958386ad67cSLukas Czerner 		 * to reserve metadata for every block we're going to write.
1959386ad67cSLukas Czerner 		 */
19605356f261SAditya Kali 		if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
1961f7fec032SZheng Liu 			ret = ext4_da_reserve_space(inode, iblock);
1962f7fec032SZheng Liu 			if (ret) {
19635356f261SAditya Kali 				/* not enough space to reserve */
1964f7fec032SZheng Liu 				retval = ret;
19655356f261SAditya Kali 				goto out_unlock;
19665356f261SAditya Kali 			}
1967386ad67cSLukas Czerner 		} else {
1968386ad67cSLukas Czerner 			ret = ext4_da_reserve_metadata(inode, iblock);
1969386ad67cSLukas Czerner 			if (ret) {
1970386ad67cSLukas Czerner 				/* not enough space to reserve */
1971386ad67cSLukas Czerner 				retval = ret;
1972386ad67cSLukas Czerner 				goto out_unlock;
1973386ad67cSLukas Czerner 			}
1974f7fec032SZheng Liu 		}
19755356f261SAditya Kali 
1976f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1977fdc0212eSZheng Liu 					    ~0, EXTENT_STATUS_DELAYED);
1978f7fec032SZheng Liu 		if (ret) {
1979f7fec032SZheng Liu 			retval = ret;
198051865fdaSZheng Liu 			goto out_unlock;
1981f7fec032SZheng Liu 		}
198251865fdaSZheng Liu 
19835356f261SAditya Kali 		/* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
19845356f261SAditya Kali 		 * and it should not appear on the bh->b_state.
19855356f261SAditya Kali 		 */
19865356f261SAditya Kali 		map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
19875356f261SAditya Kali 
19885356f261SAditya Kali 		map_bh(bh, inode->i_sb, invalid_block);
19895356f261SAditya Kali 		set_buffer_new(bh);
19905356f261SAditya Kali 		set_buffer_delay(bh);
1991f7fec032SZheng Liu 	} else if (retval > 0) {
1992f7fec032SZheng Liu 		int ret;
1993f7fec032SZheng Liu 		unsigned long long status;
1994f7fec032SZheng Liu 
1995921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
1996921f266bSDmitry Monakhov 		if (retval != map->m_len) {
1997921f266bSDmitry Monakhov 			printk("ES len assertation failed for inode: %lu "
1998921f266bSDmitry Monakhov 			       "retval %d != map->m_len %d "
1999921f266bSDmitry Monakhov 			       "in %s (lookup)\n", inode->i_ino, retval,
2000921f266bSDmitry Monakhov 			       map->m_len, __func__);
2001921f266bSDmitry Monakhov 		}
2002921f266bSDmitry Monakhov #endif
2003921f266bSDmitry Monakhov 
2004f7fec032SZheng Liu 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
2005f7fec032SZheng Liu 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
2006f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
2007f7fec032SZheng Liu 					    map->m_pblk, status);
2008f7fec032SZheng Liu 		if (ret != 0)
2009f7fec032SZheng Liu 			retval = ret;
20105356f261SAditya Kali 	}
20115356f261SAditya Kali 
20125356f261SAditya Kali out_unlock:
20135356f261SAditya Kali 	up_read((&EXT4_I(inode)->i_data_sem));
20145356f261SAditya Kali 
20155356f261SAditya Kali 	return retval;
20165356f261SAditya Kali }
20175356f261SAditya Kali 
20185356f261SAditya Kali /*
2019b920c755STheodore Ts'o  * This is a special get_blocks_t callback which is used by
2020b920c755STheodore Ts'o  * ext4_da_write_begin().  It will either return mapped block or
2021b920c755STheodore Ts'o  * reserve space for a single block.
202229fa89d0SAneesh Kumar K.V  *
202329fa89d0SAneesh Kumar K.V  * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
202429fa89d0SAneesh Kumar K.V  * We also have b_blocknr = -1 and b_bdev initialized properly
202529fa89d0SAneesh Kumar K.V  *
202629fa89d0SAneesh Kumar K.V  * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
202729fa89d0SAneesh Kumar K.V  * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
202829fa89d0SAneesh Kumar K.V  * initialized properly.
202964769240SAlex Tomas  */
20309c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
20312ed88685STheodore Ts'o 			   struct buffer_head *bh, int create)
203264769240SAlex Tomas {
20332ed88685STheodore Ts'o 	struct ext4_map_blocks map;
203464769240SAlex Tomas 	int ret = 0;
203564769240SAlex Tomas 
203664769240SAlex Tomas 	BUG_ON(create == 0);
20372ed88685STheodore Ts'o 	BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
20382ed88685STheodore Ts'o 
20392ed88685STheodore Ts'o 	map.m_lblk = iblock;
20402ed88685STheodore Ts'o 	map.m_len = 1;
204164769240SAlex Tomas 
204264769240SAlex Tomas 	/*
204364769240SAlex Tomas 	 * first, we need to know whether the block is allocated already
204464769240SAlex Tomas 	 * preallocated blocks are unmapped but should treated
204564769240SAlex Tomas 	 * the same as allocated blocks.
204664769240SAlex Tomas 	 */
20475356f261SAditya Kali 	ret = ext4_da_map_blocks(inode, iblock, &map, bh);
20485356f261SAditya Kali 	if (ret <= 0)
20492ed88685STheodore Ts'o 		return ret;
205064769240SAlex Tomas 
20512ed88685STheodore Ts'o 	map_bh(bh, inode->i_sb, map.m_pblk);
20522ed88685STheodore Ts'o 	bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
20532ed88685STheodore Ts'o 
20542ed88685STheodore Ts'o 	if (buffer_unwritten(bh)) {
20552ed88685STheodore Ts'o 		/* A delayed write to unwritten bh should be marked
20562ed88685STheodore Ts'o 		 * new and mapped.  Mapped ensures that we don't do
20572ed88685STheodore Ts'o 		 * get_block multiple times when we write to the same
20582ed88685STheodore Ts'o 		 * offset and new ensures that we do proper zero out
20592ed88685STheodore Ts'o 		 * for partial write.
20602ed88685STheodore Ts'o 		 */
20612ed88685STheodore Ts'o 		set_buffer_new(bh);
2062c8205636STheodore Ts'o 		set_buffer_mapped(bh);
20632ed88685STheodore Ts'o 	}
20642ed88685STheodore Ts'o 	return 0;
206564769240SAlex Tomas }
206661628a3fSMingming Cao 
206762e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh)
206862e086beSAneesh Kumar K.V {
206962e086beSAneesh Kumar K.V 	get_bh(bh);
207062e086beSAneesh Kumar K.V 	return 0;
207162e086beSAneesh Kumar K.V }
207262e086beSAneesh Kumar K.V 
207362e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh)
207462e086beSAneesh Kumar K.V {
207562e086beSAneesh Kumar K.V 	put_bh(bh);
207662e086beSAneesh Kumar K.V 	return 0;
207762e086beSAneesh Kumar K.V }
207862e086beSAneesh Kumar K.V 
207962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page,
208062e086beSAneesh Kumar K.V 				       unsigned int len)
208162e086beSAneesh Kumar K.V {
208262e086beSAneesh Kumar K.V 	struct address_space *mapping = page->mapping;
208362e086beSAneesh Kumar K.V 	struct inode *inode = mapping->host;
20843fdcfb66STao Ma 	struct buffer_head *page_bufs = NULL;
208562e086beSAneesh Kumar K.V 	handle_t *handle = NULL;
20863fdcfb66STao Ma 	int ret = 0, err = 0;
20873fdcfb66STao Ma 	int inline_data = ext4_has_inline_data(inode);
20883fdcfb66STao Ma 	struct buffer_head *inode_bh = NULL;
208962e086beSAneesh Kumar K.V 
2090cb20d518STheodore Ts'o 	ClearPageChecked(page);
20913fdcfb66STao Ma 
20923fdcfb66STao Ma 	if (inline_data) {
20933fdcfb66STao Ma 		BUG_ON(page->index != 0);
20943fdcfb66STao Ma 		BUG_ON(len > ext4_get_max_inline_size(inode));
20953fdcfb66STao Ma 		inode_bh = ext4_journalled_write_inline_data(inode, len, page);
20963fdcfb66STao Ma 		if (inode_bh == NULL)
20973fdcfb66STao Ma 			goto out;
20983fdcfb66STao Ma 	} else {
209962e086beSAneesh Kumar K.V 		page_bufs = page_buffers(page);
21003fdcfb66STao Ma 		if (!page_bufs) {
21013fdcfb66STao Ma 			BUG();
21023fdcfb66STao Ma 			goto out;
21033fdcfb66STao Ma 		}
21043fdcfb66STao Ma 		ext4_walk_page_buffers(handle, page_bufs, 0, len,
21053fdcfb66STao Ma 				       NULL, bget_one);
21063fdcfb66STao Ma 	}
210762e086beSAneesh Kumar K.V 	/* As soon as we unlock the page, it can go away, but we have
210862e086beSAneesh Kumar K.V 	 * references to buffers so we are safe */
210962e086beSAneesh Kumar K.V 	unlock_page(page);
211062e086beSAneesh Kumar K.V 
21119924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
21129924a92aSTheodore Ts'o 				    ext4_writepage_trans_blocks(inode));
211362e086beSAneesh Kumar K.V 	if (IS_ERR(handle)) {
211462e086beSAneesh Kumar K.V 		ret = PTR_ERR(handle);
211562e086beSAneesh Kumar K.V 		goto out;
211662e086beSAneesh Kumar K.V 	}
211762e086beSAneesh Kumar K.V 
2118441c8508SCurt Wohlgemuth 	BUG_ON(!ext4_handle_valid(handle));
2119441c8508SCurt Wohlgemuth 
21203fdcfb66STao Ma 	if (inline_data) {
21213fdcfb66STao Ma 		ret = ext4_journal_get_write_access(handle, inode_bh);
21223fdcfb66STao Ma 
21233fdcfb66STao Ma 		err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
21243fdcfb66STao Ma 
21253fdcfb66STao Ma 	} else {
2126f19d5870STao Ma 		ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
212762e086beSAneesh Kumar K.V 					     do_journal_get_write_access);
212862e086beSAneesh Kumar K.V 
2129f19d5870STao Ma 		err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
213062e086beSAneesh Kumar K.V 					     write_end_fn);
21313fdcfb66STao Ma 	}
213262e086beSAneesh Kumar K.V 	if (ret == 0)
213362e086beSAneesh Kumar K.V 		ret = err;
21342d859db3SJan Kara 	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
213562e086beSAneesh Kumar K.V 	err = ext4_journal_stop(handle);
213662e086beSAneesh Kumar K.V 	if (!ret)
213762e086beSAneesh Kumar K.V 		ret = err;
213862e086beSAneesh Kumar K.V 
21393fdcfb66STao Ma 	if (!ext4_has_inline_data(inode))
21403fdcfb66STao Ma 		ext4_walk_page_buffers(handle, page_bufs, 0, len,
21413fdcfb66STao Ma 				       NULL, bput_one);
214219f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
214362e086beSAneesh Kumar K.V out:
21443fdcfb66STao Ma 	brelse(inode_bh);
214562e086beSAneesh Kumar K.V 	return ret;
214662e086beSAneesh Kumar K.V }
214762e086beSAneesh Kumar K.V 
214861628a3fSMingming Cao /*
214943ce1d23SAneesh Kumar K.V  * Note that we don't need to start a transaction unless we're journaling data
215043ce1d23SAneesh Kumar K.V  * because we should have holes filled from ext4_page_mkwrite(). We even don't
215143ce1d23SAneesh Kumar K.V  * need to file the inode to the transaction's list in ordered mode because if
215243ce1d23SAneesh Kumar K.V  * we are writing back data added by write(), the inode is already there and if
215343ce1d23SAneesh Kumar K.V  * we are writing back data modified via mmap(), no one guarantees in which
215443ce1d23SAneesh Kumar K.V  * transaction the data will hit the disk. In case we are journaling data, we
215543ce1d23SAneesh Kumar K.V  * cannot start transaction directly because transaction start ranks above page
215643ce1d23SAneesh Kumar K.V  * lock so we have to do some magic.
215743ce1d23SAneesh Kumar K.V  *
2158b920c755STheodore Ts'o  * This function can get called via...
2159b920c755STheodore Ts'o  *   - ext4_da_writepages after taking page lock (have journal handle)
2160b920c755STheodore Ts'o  *   - journal_submit_inode_data_buffers (no journal handle)
2161f6463b0dSArtem Bityutskiy  *   - shrink_page_list via the kswapd/direct reclaim (no journal handle)
2162b920c755STheodore Ts'o  *   - grab_page_cache when doing write_begin (have journal handle)
216343ce1d23SAneesh Kumar K.V  *
216443ce1d23SAneesh Kumar K.V  * We don't do any block allocation in this function. If we have page with
216543ce1d23SAneesh Kumar K.V  * multiple blocks we need to write those buffer_heads that are mapped. This
216643ce1d23SAneesh Kumar K.V  * is important for mmaped based write. So if we do with blocksize 1K
216743ce1d23SAneesh Kumar K.V  * truncate(f, 1024);
216843ce1d23SAneesh Kumar K.V  * a = mmap(f, 0, 4096);
216943ce1d23SAneesh Kumar K.V  * a[0] = 'a';
217043ce1d23SAneesh Kumar K.V  * truncate(f, 4096);
217143ce1d23SAneesh Kumar K.V  * we have in the page first buffer_head mapped via page_mkwrite call back
217290802ed9SPaul Bolle  * but other buffer_heads would be unmapped but dirty (dirty done via the
217343ce1d23SAneesh Kumar K.V  * do_wp_page). So writepage should write the first block. If we modify
217443ce1d23SAneesh Kumar K.V  * the mmap area beyond 1024 we will again get a page_fault and the
217543ce1d23SAneesh Kumar K.V  * page_mkwrite callback will do the block allocation and mark the
217643ce1d23SAneesh Kumar K.V  * buffer_heads mapped.
217743ce1d23SAneesh Kumar K.V  *
217843ce1d23SAneesh Kumar K.V  * We redirty the page if we have any buffer_heads that is either delay or
217943ce1d23SAneesh Kumar K.V  * unwritten in the page.
218043ce1d23SAneesh Kumar K.V  *
218143ce1d23SAneesh Kumar K.V  * We can get recursively called as show below.
218243ce1d23SAneesh Kumar K.V  *
218343ce1d23SAneesh Kumar K.V  *	ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
218443ce1d23SAneesh Kumar K.V  *		ext4_writepage()
218543ce1d23SAneesh Kumar K.V  *
218643ce1d23SAneesh Kumar K.V  * But since we don't do any block allocation we should not deadlock.
218743ce1d23SAneesh Kumar K.V  * Page also have the dirty flag cleared so we don't get recurive page_lock.
218861628a3fSMingming Cao  */
218943ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page,
219064769240SAlex Tomas 			  struct writeback_control *wbc)
219164769240SAlex Tomas {
2192f8bec370SJan Kara 	int ret = 0;
219361628a3fSMingming Cao 	loff_t size;
2194498e5f24STheodore Ts'o 	unsigned int len;
2195744692dcSJiaying Zhang 	struct buffer_head *page_bufs = NULL;
219661628a3fSMingming Cao 	struct inode *inode = page->mapping->host;
219736ade451SJan Kara 	struct ext4_io_submit io_submit;
219864769240SAlex Tomas 
2199a9c667f8SLukas Czerner 	trace_ext4_writepage(page);
220061628a3fSMingming Cao 	size = i_size_read(inode);
220161628a3fSMingming Cao 	if (page->index == size >> PAGE_CACHE_SHIFT)
220261628a3fSMingming Cao 		len = size & ~PAGE_CACHE_MASK;
220361628a3fSMingming Cao 	else
220461628a3fSMingming Cao 		len = PAGE_CACHE_SIZE;
220561628a3fSMingming Cao 
2206f0e6c985SAneesh Kumar K.V 	page_bufs = page_buffers(page);
220764769240SAlex Tomas 	/*
2208fe386132SJan Kara 	 * We cannot do block allocation or other extent handling in this
2209fe386132SJan Kara 	 * function. If there are buffers needing that, we have to redirty
2210fe386132SJan Kara 	 * the page. But we may reach here when we do a journal commit via
2211fe386132SJan Kara 	 * journal_submit_inode_data_buffers() and in that case we must write
2212fe386132SJan Kara 	 * allocated buffers to achieve data=ordered mode guarantees.
221364769240SAlex Tomas 	 */
2214f19d5870STao Ma 	if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2215c364b22cSAneesh Kumar K.V 				   ext4_bh_delay_or_unwritten)) {
221661628a3fSMingming Cao 		redirty_page_for_writepage(wbc, page);
2217fe386132SJan Kara 		if (current->flags & PF_MEMALLOC) {
2218fe386132SJan Kara 			/*
2219fe386132SJan Kara 			 * For memory cleaning there's no point in writing only
2220fe386132SJan Kara 			 * some buffers. So just bail out. Warn if we came here
2221fe386132SJan Kara 			 * from direct reclaim.
2222fe386132SJan Kara 			 */
2223fe386132SJan Kara 			WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2224fe386132SJan Kara 							== PF_MEMALLOC);
222561628a3fSMingming Cao 			unlock_page(page);
222661628a3fSMingming Cao 			return 0;
222761628a3fSMingming Cao 		}
2228f0e6c985SAneesh Kumar K.V 	}
222964769240SAlex Tomas 
2230cb20d518STheodore Ts'o 	if (PageChecked(page) && ext4_should_journal_data(inode))
223143ce1d23SAneesh Kumar K.V 		/*
223243ce1d23SAneesh Kumar K.V 		 * It's mmapped pagecache.  Add buffers and journal it.  There
223343ce1d23SAneesh Kumar K.V 		 * doesn't seem much point in redirtying the page here.
223443ce1d23SAneesh Kumar K.V 		 */
22353f0ca309SWu Fengguang 		return __ext4_journalled_writepage(page, len);
223643ce1d23SAneesh Kumar K.V 
2237*4eec708dSJan Kara 	ext4_io_submit_init(&io_submit, wbc);
2238*4eec708dSJan Kara 	io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2239*4eec708dSJan Kara 	if (!io_submit.io_end) {
2240*4eec708dSJan Kara 		redirty_page_for_writepage(wbc, page);
2241*4eec708dSJan Kara 		return -ENOMEM;
2242*4eec708dSJan Kara 	}
224336ade451SJan Kara 	ret = ext4_bio_write_page(&io_submit, page, len, wbc);
224436ade451SJan Kara 	ext4_io_submit(&io_submit);
2245*4eec708dSJan Kara 	/* Drop io_end reference we got from init */
2246*4eec708dSJan Kara 	ext4_put_io_end_defer(io_submit.io_end);
224764769240SAlex Tomas 	return ret;
224864769240SAlex Tomas }
224964769240SAlex Tomas 
225061628a3fSMingming Cao /*
2251525f4ed8SMingming Cao  * This is called via ext4_da_writepages() to
225225985edcSLucas De Marchi  * calculate the total number of credits to reserve to fit
2253525f4ed8SMingming Cao  * a single extent allocation into a single transaction,
2254525f4ed8SMingming Cao  * ext4_da_writpeages() will loop calling this before
2255525f4ed8SMingming Cao  * the block allocation.
225661628a3fSMingming Cao  */
2257525f4ed8SMingming Cao 
2258525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode)
2259525f4ed8SMingming Cao {
2260525f4ed8SMingming Cao 	int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2261525f4ed8SMingming Cao 
2262525f4ed8SMingming Cao 	/*
2263525f4ed8SMingming Cao 	 * With non-extent format the journal credit needed to
2264525f4ed8SMingming Cao 	 * insert nrblocks contiguous block is dependent on
2265525f4ed8SMingming Cao 	 * number of contiguous block. So we will limit
2266525f4ed8SMingming Cao 	 * number of contiguous block to a sane value
2267525f4ed8SMingming Cao 	 */
226812e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
2269525f4ed8SMingming Cao 	    (max_blocks > EXT4_MAX_TRANS_DATA))
2270525f4ed8SMingming Cao 		max_blocks = EXT4_MAX_TRANS_DATA;
2271525f4ed8SMingming Cao 
2272525f4ed8SMingming Cao 	return ext4_chunk_trans_blocks(inode, max_blocks);
2273525f4ed8SMingming Cao }
227461628a3fSMingming Cao 
22758e48dcfbSTheodore Ts'o /*
22768e48dcfbSTheodore Ts'o  * write_cache_pages_da - walk the list of dirty pages of the given
22778eb9e5ceSTheodore Ts'o  * address space and accumulate pages that need writing, and call
2278168fc022STheodore Ts'o  * mpage_da_map_and_submit to map a single contiguous memory region
2279168fc022STheodore Ts'o  * and then write them.
22808e48dcfbSTheodore Ts'o  */
22819c3569b5STao Ma static int write_cache_pages_da(handle_t *handle,
22829c3569b5STao Ma 				struct address_space *mapping,
22838e48dcfbSTheodore Ts'o 				struct writeback_control *wbc,
228472f84e65SEric Sandeen 				struct mpage_da_data *mpd,
228572f84e65SEric Sandeen 				pgoff_t *done_index)
22868e48dcfbSTheodore Ts'o {
22878eb9e5ceSTheodore Ts'o 	struct buffer_head	*bh, *head;
2288168fc022STheodore Ts'o 	struct inode		*inode = mapping->host;
22898e48dcfbSTheodore Ts'o 	struct pagevec		pvec;
22904f01b02cSTheodore Ts'o 	unsigned int		nr_pages;
22914f01b02cSTheodore Ts'o 	sector_t		logical;
22924f01b02cSTheodore Ts'o 	pgoff_t			index, end;
22938e48dcfbSTheodore Ts'o 	long			nr_to_write = wbc->nr_to_write;
22944f01b02cSTheodore Ts'o 	int			i, tag, ret = 0;
22958e48dcfbSTheodore Ts'o 
2296168fc022STheodore Ts'o 	memset(mpd, 0, sizeof(struct mpage_da_data));
2297168fc022STheodore Ts'o 	mpd->wbc = wbc;
2298168fc022STheodore Ts'o 	mpd->inode = inode;
22998e48dcfbSTheodore Ts'o 	pagevec_init(&pvec, 0);
23008e48dcfbSTheodore Ts'o 	index = wbc->range_start >> PAGE_CACHE_SHIFT;
23018e48dcfbSTheodore Ts'o 	end = wbc->range_end >> PAGE_CACHE_SHIFT;
23028e48dcfbSTheodore Ts'o 
23036e6938b6SWu Fengguang 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
23045b41d924SEric Sandeen 		tag = PAGECACHE_TAG_TOWRITE;
23055b41d924SEric Sandeen 	else
23065b41d924SEric Sandeen 		tag = PAGECACHE_TAG_DIRTY;
23075b41d924SEric Sandeen 
230872f84e65SEric Sandeen 	*done_index = index;
23094f01b02cSTheodore Ts'o 	while (index <= end) {
23105b41d924SEric Sandeen 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
23118e48dcfbSTheodore Ts'o 			      min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
23128e48dcfbSTheodore Ts'o 		if (nr_pages == 0)
23134f01b02cSTheodore Ts'o 			return 0;
23148e48dcfbSTheodore Ts'o 
23158e48dcfbSTheodore Ts'o 		for (i = 0; i < nr_pages; i++) {
23168e48dcfbSTheodore Ts'o 			struct page *page = pvec.pages[i];
23178e48dcfbSTheodore Ts'o 
23188e48dcfbSTheodore Ts'o 			/*
23198e48dcfbSTheodore Ts'o 			 * At this point, the page may be truncated or
23208e48dcfbSTheodore Ts'o 			 * invalidated (changing page->mapping to NULL), or
23218e48dcfbSTheodore Ts'o 			 * even swizzled back from swapper_space to tmpfs file
23228e48dcfbSTheodore Ts'o 			 * mapping. However, page->index will not change
23238e48dcfbSTheodore Ts'o 			 * because we have a reference on the page.
23248e48dcfbSTheodore Ts'o 			 */
23254f01b02cSTheodore Ts'o 			if (page->index > end)
23264f01b02cSTheodore Ts'o 				goto out;
23278e48dcfbSTheodore Ts'o 
232872f84e65SEric Sandeen 			*done_index = page->index + 1;
232972f84e65SEric Sandeen 
233078aaced3STheodore Ts'o 			/*
233178aaced3STheodore Ts'o 			 * If we can't merge this page, and we have
233278aaced3STheodore Ts'o 			 * accumulated an contiguous region, write it
233378aaced3STheodore Ts'o 			 */
233478aaced3STheodore Ts'o 			if ((mpd->next_page != page->index) &&
233578aaced3STheodore Ts'o 			    (mpd->next_page != mpd->first_page)) {
233678aaced3STheodore Ts'o 				mpage_da_map_and_submit(mpd);
233778aaced3STheodore Ts'o 				goto ret_extent_tail;
233878aaced3STheodore Ts'o 			}
233978aaced3STheodore Ts'o 
23408e48dcfbSTheodore Ts'o 			lock_page(page);
23418e48dcfbSTheodore Ts'o 
23428e48dcfbSTheodore Ts'o 			/*
23434f01b02cSTheodore Ts'o 			 * If the page is no longer dirty, or its
23444f01b02cSTheodore Ts'o 			 * mapping no longer corresponds to inode we
23454f01b02cSTheodore Ts'o 			 * are writing (which means it has been
23464f01b02cSTheodore Ts'o 			 * truncated or invalidated), or the page is
23474f01b02cSTheodore Ts'o 			 * already under writeback and we are not
23484f01b02cSTheodore Ts'o 			 * doing a data integrity writeback, skip the page
23498e48dcfbSTheodore Ts'o 			 */
23504f01b02cSTheodore Ts'o 			if (!PageDirty(page) ||
23514f01b02cSTheodore Ts'o 			    (PageWriteback(page) &&
23524f01b02cSTheodore Ts'o 			     (wbc->sync_mode == WB_SYNC_NONE)) ||
23534f01b02cSTheodore Ts'o 			    unlikely(page->mapping != mapping)) {
23548e48dcfbSTheodore Ts'o 				unlock_page(page);
23558e48dcfbSTheodore Ts'o 				continue;
23568e48dcfbSTheodore Ts'o 			}
23578e48dcfbSTheodore Ts'o 
23588e48dcfbSTheodore Ts'o 			wait_on_page_writeback(page);
23598e48dcfbSTheodore Ts'o 			BUG_ON(PageWriteback(page));
23608e48dcfbSTheodore Ts'o 
23619c3569b5STao Ma 			/*
23629c3569b5STao Ma 			 * If we have inline data and arrive here, it means that
23639c3569b5STao Ma 			 * we will soon create the block for the 1st page, so
23649c3569b5STao Ma 			 * we'd better clear the inline data here.
23659c3569b5STao Ma 			 */
23669c3569b5STao Ma 			if (ext4_has_inline_data(inode)) {
23679c3569b5STao Ma 				BUG_ON(ext4_test_inode_state(inode,
23689c3569b5STao Ma 						EXT4_STATE_MAY_INLINE_DATA));
23699c3569b5STao Ma 				ext4_destroy_inline_data(handle, inode);
23709c3569b5STao Ma 			}
23719c3569b5STao Ma 
2372168fc022STheodore Ts'o 			if (mpd->next_page != page->index)
23738eb9e5ceSTheodore Ts'o 				mpd->first_page = page->index;
23748eb9e5ceSTheodore Ts'o 			mpd->next_page = page->index + 1;
23758eb9e5ceSTheodore Ts'o 			logical = (sector_t) page->index <<
23768eb9e5ceSTheodore Ts'o 				(PAGE_CACHE_SHIFT - inode->i_blkbits);
23778eb9e5ceSTheodore Ts'o 
2378f8bec370SJan Kara 			/* Add all dirty buffers to mpd */
23798eb9e5ceSTheodore Ts'o 			head = page_buffers(page);
23808eb9e5ceSTheodore Ts'o 			bh = head;
23818eb9e5ceSTheodore Ts'o 			do {
23828eb9e5ceSTheodore Ts'o 				BUG_ON(buffer_locked(bh));
23838eb9e5ceSTheodore Ts'o 				/*
2384f8bec370SJan Kara 				 * We need to try to allocate unmapped blocks
2385f8bec370SJan Kara 				 * in the same page.  Otherwise we won't make
2386f8bec370SJan Kara 				 * progress with the page in ext4_writepage
23878eb9e5ceSTheodore Ts'o 				 */
23888eb9e5ceSTheodore Ts'o 				if (ext4_bh_delay_or_unwritten(NULL, bh)) {
23898eb9e5ceSTheodore Ts'o 					mpage_add_bh_to_extent(mpd, logical,
23908eb9e5ceSTheodore Ts'o 							       bh->b_state);
23914f01b02cSTheodore Ts'o 					if (mpd->io_done)
23924f01b02cSTheodore Ts'o 						goto ret_extent_tail;
2393f8bec370SJan Kara 				} else if (buffer_dirty(bh) &&
2394f8bec370SJan Kara 					   buffer_mapped(bh)) {
23958eb9e5ceSTheodore Ts'o 					/*
2396f8bec370SJan Kara 					 * mapped dirty buffer. We need to
2397f8bec370SJan Kara 					 * update the b_state because we look
2398f8bec370SJan Kara 					 * at b_state in mpage_da_map_blocks.
2399f8bec370SJan Kara 					 * We don't update b_size because if we
2400f8bec370SJan Kara 					 * find an unmapped buffer_head later
2401f8bec370SJan Kara 					 * we need to use the b_state flag of
2402f8bec370SJan Kara 					 * that buffer_head.
24038eb9e5ceSTheodore Ts'o 					 */
24048eb9e5ceSTheodore Ts'o 					if (mpd->b_size == 0)
2405f8bec370SJan Kara 						mpd->b_state =
2406f8bec370SJan Kara 							bh->b_state & BH_FLAGS;
24078e48dcfbSTheodore Ts'o 				}
24088eb9e5ceSTheodore Ts'o 				logical++;
24098eb9e5ceSTheodore Ts'o 			} while ((bh = bh->b_this_page) != head);
24108e48dcfbSTheodore Ts'o 
24118e48dcfbSTheodore Ts'o 			if (nr_to_write > 0) {
24128e48dcfbSTheodore Ts'o 				nr_to_write--;
24138e48dcfbSTheodore Ts'o 				if (nr_to_write == 0 &&
24144f01b02cSTheodore Ts'o 				    wbc->sync_mode == WB_SYNC_NONE)
24158e48dcfbSTheodore Ts'o 					/*
24168e48dcfbSTheodore Ts'o 					 * We stop writing back only if we are
24178e48dcfbSTheodore Ts'o 					 * not doing integrity sync. In case of
24188e48dcfbSTheodore Ts'o 					 * integrity sync we have to keep going
24198e48dcfbSTheodore Ts'o 					 * because someone may be concurrently
24208e48dcfbSTheodore Ts'o 					 * dirtying pages, and we might have
24218e48dcfbSTheodore Ts'o 					 * synced a lot of newly appeared dirty
24228e48dcfbSTheodore Ts'o 					 * pages, but have not synced all of the
24238e48dcfbSTheodore Ts'o 					 * old dirty pages.
24248e48dcfbSTheodore Ts'o 					 */
24254f01b02cSTheodore Ts'o 					goto out;
24268e48dcfbSTheodore Ts'o 			}
24278e48dcfbSTheodore Ts'o 		}
24288e48dcfbSTheodore Ts'o 		pagevec_release(&pvec);
24298e48dcfbSTheodore Ts'o 		cond_resched();
24308e48dcfbSTheodore Ts'o 	}
24314f01b02cSTheodore Ts'o 	return 0;
24324f01b02cSTheodore Ts'o ret_extent_tail:
24334f01b02cSTheodore Ts'o 	ret = MPAGE_DA_EXTENT_TAIL;
24348eb9e5ceSTheodore Ts'o out:
24358eb9e5ceSTheodore Ts'o 	pagevec_release(&pvec);
24368eb9e5ceSTheodore Ts'o 	cond_resched();
24378e48dcfbSTheodore Ts'o 	return ret;
24388e48dcfbSTheodore Ts'o }
24398e48dcfbSTheodore Ts'o 
24408e48dcfbSTheodore Ts'o 
244164769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping,
244264769240SAlex Tomas 			      struct writeback_control *wbc)
244364769240SAlex Tomas {
244422208dedSAneesh Kumar K.V 	pgoff_t	index;
244522208dedSAneesh Kumar K.V 	int range_whole = 0;
244661628a3fSMingming Cao 	handle_t *handle = NULL;
2447df22291fSAneesh Kumar K.V 	struct mpage_da_data mpd;
24485e745b04SAneesh Kumar K.V 	struct inode *inode = mapping->host;
2449498e5f24STheodore Ts'o 	int pages_written = 0;
245055138e0bSTheodore Ts'o 	unsigned int max_pages;
24512acf2c26SAneesh Kumar K.V 	int range_cyclic, cycled = 1, io_done = 0;
245255138e0bSTheodore Ts'o 	int needed_blocks, ret = 0;
245355138e0bSTheodore Ts'o 	long desired_nr_to_write, nr_to_writebump = 0;
2454de89de6eSTheodore Ts'o 	loff_t range_start = wbc->range_start;
24555e745b04SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
245672f84e65SEric Sandeen 	pgoff_t done_index = 0;
24575b41d924SEric Sandeen 	pgoff_t end;
24581bce63d1SShaohua Li 	struct blk_plug plug;
245961628a3fSMingming Cao 
24609bffad1eSTheodore Ts'o 	trace_ext4_da_writepages(inode, wbc);
2461ba80b101STheodore Ts'o 
246261628a3fSMingming Cao 	/*
246361628a3fSMingming Cao 	 * No pages to write? This is mainly a kludge to avoid starting
246461628a3fSMingming Cao 	 * a transaction for special inodes like journal inode on last iput()
246561628a3fSMingming Cao 	 * because that could violate lock ordering on umount
246661628a3fSMingming Cao 	 */
2467a1d6cc56SAneesh Kumar K.V 	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
246861628a3fSMingming Cao 		return 0;
24692a21e37eSTheodore Ts'o 
24702a21e37eSTheodore Ts'o 	/*
24712a21e37eSTheodore Ts'o 	 * If the filesystem has aborted, it is read-only, so return
24722a21e37eSTheodore Ts'o 	 * right away instead of dumping stack traces later on that
24732a21e37eSTheodore Ts'o 	 * will obscure the real source of the problem.  We test
24744ab2f15bSTheodore Ts'o 	 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
24752a21e37eSTheodore Ts'o 	 * the latter could be true if the filesystem is mounted
24762a21e37eSTheodore Ts'o 	 * read-only, and in that case, ext4_da_writepages should
24772a21e37eSTheodore Ts'o 	 * *never* be called, so if that ever happens, we would want
24782a21e37eSTheodore Ts'o 	 * the stack trace.
24792a21e37eSTheodore Ts'o 	 */
24804ab2f15bSTheodore Ts'o 	if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
24812a21e37eSTheodore Ts'o 		return -EROFS;
24822a21e37eSTheodore Ts'o 
248322208dedSAneesh Kumar K.V 	if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
248422208dedSAneesh Kumar K.V 		range_whole = 1;
248561628a3fSMingming Cao 
24862acf2c26SAneesh Kumar K.V 	range_cyclic = wbc->range_cyclic;
24872acf2c26SAneesh Kumar K.V 	if (wbc->range_cyclic) {
248822208dedSAneesh Kumar K.V 		index = mapping->writeback_index;
24892acf2c26SAneesh Kumar K.V 		if (index)
24902acf2c26SAneesh Kumar K.V 			cycled = 0;
24912acf2c26SAneesh Kumar K.V 		wbc->range_start = index << PAGE_CACHE_SHIFT;
24922acf2c26SAneesh Kumar K.V 		wbc->range_end  = LLONG_MAX;
24932acf2c26SAneesh Kumar K.V 		wbc->range_cyclic = 0;
24945b41d924SEric Sandeen 		end = -1;
24955b41d924SEric Sandeen 	} else {
249622208dedSAneesh Kumar K.V 		index = wbc->range_start >> PAGE_CACHE_SHIFT;
24975b41d924SEric Sandeen 		end = wbc->range_end >> PAGE_CACHE_SHIFT;
24985b41d924SEric Sandeen 	}
2499a1d6cc56SAneesh Kumar K.V 
250055138e0bSTheodore Ts'o 	/*
250155138e0bSTheodore Ts'o 	 * This works around two forms of stupidity.  The first is in
250255138e0bSTheodore Ts'o 	 * the writeback code, which caps the maximum number of pages
250355138e0bSTheodore Ts'o 	 * written to be 1024 pages.  This is wrong on multiple
250455138e0bSTheodore Ts'o 	 * levels; different architectues have a different page size,
250555138e0bSTheodore Ts'o 	 * which changes the maximum amount of data which gets
250655138e0bSTheodore Ts'o 	 * written.  Secondly, 4 megabytes is way too small.  XFS
250755138e0bSTheodore Ts'o 	 * forces this value to be 16 megabytes by multiplying
250855138e0bSTheodore Ts'o 	 * nr_to_write parameter by four, and then relies on its
250955138e0bSTheodore Ts'o 	 * allocator to allocate larger extents to make them
251055138e0bSTheodore Ts'o 	 * contiguous.  Unfortunately this brings us to the second
251155138e0bSTheodore Ts'o 	 * stupidity, which is that ext4's mballoc code only allocates
251255138e0bSTheodore Ts'o 	 * at most 2048 blocks.  So we force contiguous writes up to
251355138e0bSTheodore Ts'o 	 * the number of dirty blocks in the inode, or
251455138e0bSTheodore Ts'o 	 * sbi->max_writeback_mb_bump whichever is smaller.
251555138e0bSTheodore Ts'o 	 */
251655138e0bSTheodore Ts'o 	max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2517b443e733SEric Sandeen 	if (!range_cyclic && range_whole) {
2518b443e733SEric Sandeen 		if (wbc->nr_to_write == LONG_MAX)
2519b443e733SEric Sandeen 			desired_nr_to_write = wbc->nr_to_write;
252055138e0bSTheodore Ts'o 		else
2521b443e733SEric Sandeen 			desired_nr_to_write = wbc->nr_to_write * 8;
2522b443e733SEric Sandeen 	} else
252355138e0bSTheodore Ts'o 		desired_nr_to_write = ext4_num_dirty_pages(inode, index,
252455138e0bSTheodore Ts'o 							   max_pages);
252555138e0bSTheodore Ts'o 	if (desired_nr_to_write > max_pages)
252655138e0bSTheodore Ts'o 		desired_nr_to_write = max_pages;
252755138e0bSTheodore Ts'o 
252855138e0bSTheodore Ts'o 	if (wbc->nr_to_write < desired_nr_to_write) {
252955138e0bSTheodore Ts'o 		nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
253055138e0bSTheodore Ts'o 		wbc->nr_to_write = desired_nr_to_write;
253155138e0bSTheodore Ts'o 	}
253255138e0bSTheodore Ts'o 
25332acf2c26SAneesh Kumar K.V retry:
25346e6938b6SWu Fengguang 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
25355b41d924SEric Sandeen 		tag_pages_for_writeback(mapping, index, end);
25365b41d924SEric Sandeen 
25371bce63d1SShaohua Li 	blk_start_plug(&plug);
253822208dedSAneesh Kumar K.V 	while (!ret && wbc->nr_to_write > 0) {
2539a1d6cc56SAneesh Kumar K.V 
2540a1d6cc56SAneesh Kumar K.V 		/*
2541a1d6cc56SAneesh Kumar K.V 		 * we  insert one extent at a time. So we need
2542a1d6cc56SAneesh Kumar K.V 		 * credit needed for single extent allocation.
2543a1d6cc56SAneesh Kumar K.V 		 * journalled mode is currently not supported
2544a1d6cc56SAneesh Kumar K.V 		 * by delalloc
2545a1d6cc56SAneesh Kumar K.V 		 */
2546a1d6cc56SAneesh Kumar K.V 		BUG_ON(ext4_should_journal_data(inode));
2547525f4ed8SMingming Cao 		needed_blocks = ext4_da_writepages_trans_blocks(inode);
2548a1d6cc56SAneesh Kumar K.V 
254961628a3fSMingming Cao 		/* start a new transaction*/
25509924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
25519924a92aSTheodore Ts'o 					    needed_blocks);
255261628a3fSMingming Cao 		if (IS_ERR(handle)) {
255361628a3fSMingming Cao 			ret = PTR_ERR(handle);
25541693918eSTheodore Ts'o 			ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
2555fbe845ddSCurt Wohlgemuth 			       "%ld pages, ino %lu; err %d", __func__,
2556a1d6cc56SAneesh Kumar K.V 				wbc->nr_to_write, inode->i_ino, ret);
25573c1fcb2cSNamjae Jeon 			blk_finish_plug(&plug);
255861628a3fSMingming Cao 			goto out_writepages;
255961628a3fSMingming Cao 		}
2560f63e6005STheodore Ts'o 
2561f63e6005STheodore Ts'o 		/*
25628eb9e5ceSTheodore Ts'o 		 * Now call write_cache_pages_da() to find the next
2563f63e6005STheodore Ts'o 		 * contiguous region of logical blocks that need
25648eb9e5ceSTheodore Ts'o 		 * blocks to be allocated by ext4 and submit them.
2565f63e6005STheodore Ts'o 		 */
25669c3569b5STao Ma 		ret = write_cache_pages_da(handle, mapping,
25679c3569b5STao Ma 					   wbc, &mpd, &done_index);
2568f63e6005STheodore Ts'o 		/*
2569af901ca1SAndré Goddard Rosa 		 * If we have a contiguous extent of pages and we
2570f63e6005STheodore Ts'o 		 * haven't done the I/O yet, map the blocks and submit
2571f63e6005STheodore Ts'o 		 * them for I/O.
2572f63e6005STheodore Ts'o 		 */
2573f63e6005STheodore Ts'o 		if (!mpd.io_done && mpd.next_page != mpd.first_page) {
25745a87b7a5STheodore Ts'o 			mpage_da_map_and_submit(&mpd);
2575f63e6005STheodore Ts'o 			ret = MPAGE_DA_EXTENT_TAIL;
2576f63e6005STheodore Ts'o 		}
2577b3a3ca8cSTheodore Ts'o 		trace_ext4_da_write_pages(inode, &mpd);
2578f63e6005STheodore Ts'o 		wbc->nr_to_write -= mpd.pages_written;
2579df22291fSAneesh Kumar K.V 
258061628a3fSMingming Cao 		ext4_journal_stop(handle);
2581df22291fSAneesh Kumar K.V 
25828f64b32eSEric Sandeen 		if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
258322208dedSAneesh Kumar K.V 			/* commit the transaction which would
258422208dedSAneesh Kumar K.V 			 * free blocks released in the transaction
258522208dedSAneesh Kumar K.V 			 * and try again
258622208dedSAneesh Kumar K.V 			 */
2587df22291fSAneesh Kumar K.V 			jbd2_journal_force_commit_nested(sbi->s_journal);
258822208dedSAneesh Kumar K.V 			ret = 0;
258922208dedSAneesh Kumar K.V 		} else if (ret == MPAGE_DA_EXTENT_TAIL) {
2590a1d6cc56SAneesh Kumar K.V 			/*
25918de49e67SKazuya Mio 			 * Got one extent now try with rest of the pages.
25928de49e67SKazuya Mio 			 * If mpd.retval is set -EIO, journal is aborted.
25938de49e67SKazuya Mio 			 * So we don't need to write any more.
2594a1d6cc56SAneesh Kumar K.V 			 */
259522208dedSAneesh Kumar K.V 			pages_written += mpd.pages_written;
25968de49e67SKazuya Mio 			ret = mpd.retval;
25972acf2c26SAneesh Kumar K.V 			io_done = 1;
259822208dedSAneesh Kumar K.V 		} else if (wbc->nr_to_write)
259961628a3fSMingming Cao 			/*
260061628a3fSMingming Cao 			 * There is no more writeout needed
260161628a3fSMingming Cao 			 * or we requested for a noblocking writeout
260261628a3fSMingming Cao 			 * and we found the device congested
260361628a3fSMingming Cao 			 */
260461628a3fSMingming Cao 			break;
260561628a3fSMingming Cao 	}
26061bce63d1SShaohua Li 	blk_finish_plug(&plug);
26072acf2c26SAneesh Kumar K.V 	if (!io_done && !cycled) {
26082acf2c26SAneesh Kumar K.V 		cycled = 1;
26092acf2c26SAneesh Kumar K.V 		index = 0;
26102acf2c26SAneesh Kumar K.V 		wbc->range_start = index << PAGE_CACHE_SHIFT;
26112acf2c26SAneesh Kumar K.V 		wbc->range_end  = mapping->writeback_index - 1;
26122acf2c26SAneesh Kumar K.V 		goto retry;
26132acf2c26SAneesh Kumar K.V 	}
261461628a3fSMingming Cao 
261522208dedSAneesh Kumar K.V 	/* Update index */
26162acf2c26SAneesh Kumar K.V 	wbc->range_cyclic = range_cyclic;
261722208dedSAneesh Kumar K.V 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
261822208dedSAneesh Kumar K.V 		/*
261922208dedSAneesh Kumar K.V 		 * set the writeback_index so that range_cyclic
262022208dedSAneesh Kumar K.V 		 * mode will write it back later
262122208dedSAneesh Kumar K.V 		 */
262272f84e65SEric Sandeen 		mapping->writeback_index = done_index;
2623a1d6cc56SAneesh Kumar K.V 
262461628a3fSMingming Cao out_writepages:
262522208dedSAneesh Kumar K.V 	wbc->nr_to_write -= nr_to_writebump;
2626de89de6eSTheodore Ts'o 	wbc->range_start = range_start;
26279bffad1eSTheodore Ts'o 	trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
262861628a3fSMingming Cao 	return ret;
262964769240SAlex Tomas }
263064769240SAlex Tomas 
263179f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb)
263279f0be8dSAneesh Kumar K.V {
26335c1ff336SEric Whitney 	s64 free_clusters, dirty_clusters;
263479f0be8dSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
263579f0be8dSAneesh Kumar K.V 
263679f0be8dSAneesh Kumar K.V 	/*
263779f0be8dSAneesh Kumar K.V 	 * switch to non delalloc mode if we are running low
263879f0be8dSAneesh Kumar K.V 	 * on free block. The free block accounting via percpu
2639179f7ebfSEric Dumazet 	 * counters can get slightly wrong with percpu_counter_batch getting
264079f0be8dSAneesh Kumar K.V 	 * accumulated on each CPU without updating global counters
264179f0be8dSAneesh Kumar K.V 	 * Delalloc need an accurate free block accounting. So switch
264279f0be8dSAneesh Kumar K.V 	 * to non delalloc when we are near to error range.
264379f0be8dSAneesh Kumar K.V 	 */
26445c1ff336SEric Whitney 	free_clusters =
26455c1ff336SEric Whitney 		percpu_counter_read_positive(&sbi->s_freeclusters_counter);
26465c1ff336SEric Whitney 	dirty_clusters =
26475c1ff336SEric Whitney 		percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
264800d4e736STheodore Ts'o 	/*
264900d4e736STheodore Ts'o 	 * Start pushing delalloc when 1/2 of free blocks are dirty.
265000d4e736STheodore Ts'o 	 */
26515c1ff336SEric Whitney 	if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
265210ee27a0SMiao Xie 		try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
265300d4e736STheodore Ts'o 
26545c1ff336SEric Whitney 	if (2 * free_clusters < 3 * dirty_clusters ||
26555c1ff336SEric Whitney 	    free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
265679f0be8dSAneesh Kumar K.V 		/*
2657c8afb446SEric Sandeen 		 * free block count is less than 150% of dirty blocks
2658c8afb446SEric Sandeen 		 * or free blocks is less than watermark
265979f0be8dSAneesh Kumar K.V 		 */
266079f0be8dSAneesh Kumar K.V 		return 1;
266179f0be8dSAneesh Kumar K.V 	}
266279f0be8dSAneesh Kumar K.V 	return 0;
266379f0be8dSAneesh Kumar K.V }
266479f0be8dSAneesh Kumar K.V 
266564769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
266664769240SAlex Tomas 			       loff_t pos, unsigned len, unsigned flags,
266764769240SAlex Tomas 			       struct page **pagep, void **fsdata)
266864769240SAlex Tomas {
266972b8ab9dSEric Sandeen 	int ret, retries = 0;
267064769240SAlex Tomas 	struct page *page;
267164769240SAlex Tomas 	pgoff_t index;
267264769240SAlex Tomas 	struct inode *inode = mapping->host;
267364769240SAlex Tomas 	handle_t *handle;
267464769240SAlex Tomas 
267564769240SAlex Tomas 	index = pos >> PAGE_CACHE_SHIFT;
267679f0be8dSAneesh Kumar K.V 
267779f0be8dSAneesh Kumar K.V 	if (ext4_nonda_switch(inode->i_sb)) {
267879f0be8dSAneesh Kumar K.V 		*fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
267979f0be8dSAneesh Kumar K.V 		return ext4_write_begin(file, mapping, pos,
268079f0be8dSAneesh Kumar K.V 					len, flags, pagep, fsdata);
268179f0be8dSAneesh Kumar K.V 	}
268279f0be8dSAneesh Kumar K.V 	*fsdata = (void *)0;
26839bffad1eSTheodore Ts'o 	trace_ext4_da_write_begin(inode, pos, len, flags);
26849c3569b5STao Ma 
26859c3569b5STao Ma 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
26869c3569b5STao Ma 		ret = ext4_da_write_inline_data_begin(mapping, inode,
26879c3569b5STao Ma 						      pos, len, flags,
26889c3569b5STao Ma 						      pagep, fsdata);
26899c3569b5STao Ma 		if (ret < 0)
269047564bfbSTheodore Ts'o 			return ret;
269147564bfbSTheodore Ts'o 		if (ret == 1)
269247564bfbSTheodore Ts'o 			return 0;
26939c3569b5STao Ma 	}
26949c3569b5STao Ma 
269547564bfbSTheodore Ts'o 	/*
269647564bfbSTheodore Ts'o 	 * grab_cache_page_write_begin() can take a long time if the
269747564bfbSTheodore Ts'o 	 * system is thrashing due to memory pressure, or if the page
269847564bfbSTheodore Ts'o 	 * is being written back.  So grab it first before we start
269947564bfbSTheodore Ts'o 	 * the transaction handle.  This also allows us to allocate
270047564bfbSTheodore Ts'o 	 * the page (if needed) without using GFP_NOFS.
270147564bfbSTheodore Ts'o 	 */
270247564bfbSTheodore Ts'o retry_grab:
270347564bfbSTheodore Ts'o 	page = grab_cache_page_write_begin(mapping, index, flags);
270447564bfbSTheodore Ts'o 	if (!page)
270547564bfbSTheodore Ts'o 		return -ENOMEM;
270647564bfbSTheodore Ts'o 	unlock_page(page);
270747564bfbSTheodore Ts'o 
270864769240SAlex Tomas 	/*
270964769240SAlex Tomas 	 * With delayed allocation, we don't log the i_disksize update
271064769240SAlex Tomas 	 * if there is delayed block allocation. But we still need
271164769240SAlex Tomas 	 * to journalling the i_disksize update if writes to the end
271264769240SAlex Tomas 	 * of file which has an already mapped buffer.
271364769240SAlex Tomas 	 */
271447564bfbSTheodore Ts'o retry_journal:
27159924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1);
271664769240SAlex Tomas 	if (IS_ERR(handle)) {
271747564bfbSTheodore Ts'o 		page_cache_release(page);
271847564bfbSTheodore Ts'o 		return PTR_ERR(handle);
271964769240SAlex Tomas 	}
272064769240SAlex Tomas 
272147564bfbSTheodore Ts'o 	lock_page(page);
272247564bfbSTheodore Ts'o 	if (page->mapping != mapping) {
272347564bfbSTheodore Ts'o 		/* The page got truncated from under us */
272447564bfbSTheodore Ts'o 		unlock_page(page);
272547564bfbSTheodore Ts'o 		page_cache_release(page);
2726d5a0d4f7SEric Sandeen 		ext4_journal_stop(handle);
272747564bfbSTheodore Ts'o 		goto retry_grab;
2728d5a0d4f7SEric Sandeen 	}
272947564bfbSTheodore Ts'o 	/* In case writeback began while the page was unlocked */
273047564bfbSTheodore Ts'o 	wait_on_page_writeback(page);
273164769240SAlex Tomas 
27326e1db88dSChristoph Hellwig 	ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
273364769240SAlex Tomas 	if (ret < 0) {
273464769240SAlex Tomas 		unlock_page(page);
273564769240SAlex Tomas 		ext4_journal_stop(handle);
2736ae4d5372SAneesh Kumar K.V 		/*
2737ae4d5372SAneesh Kumar K.V 		 * block_write_begin may have instantiated a few blocks
2738ae4d5372SAneesh Kumar K.V 		 * outside i_size.  Trim these off again. Don't need
2739ae4d5372SAneesh Kumar K.V 		 * i_size_read because we hold i_mutex.
2740ae4d5372SAneesh Kumar K.V 		 */
2741ae4d5372SAneesh Kumar K.V 		if (pos + len > inode->i_size)
2742b9a4207dSJan Kara 			ext4_truncate_failed_write(inode);
274347564bfbSTheodore Ts'o 
274447564bfbSTheodore Ts'o 		if (ret == -ENOSPC &&
274547564bfbSTheodore Ts'o 		    ext4_should_retry_alloc(inode->i_sb, &retries))
274647564bfbSTheodore Ts'o 			goto retry_journal;
274747564bfbSTheodore Ts'o 
274847564bfbSTheodore Ts'o 		page_cache_release(page);
274947564bfbSTheodore Ts'o 		return ret;
275064769240SAlex Tomas 	}
275164769240SAlex Tomas 
275247564bfbSTheodore Ts'o 	*pagep = page;
275364769240SAlex Tomas 	return ret;
275464769240SAlex Tomas }
275564769240SAlex Tomas 
2756632eaeabSMingming Cao /*
2757632eaeabSMingming Cao  * Check if we should update i_disksize
2758632eaeabSMingming Cao  * when write to the end of file but not require block allocation
2759632eaeabSMingming Cao  */
2760632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page,
2761632eaeabSMingming Cao 					    unsigned long offset)
2762632eaeabSMingming Cao {
2763632eaeabSMingming Cao 	struct buffer_head *bh;
2764632eaeabSMingming Cao 	struct inode *inode = page->mapping->host;
2765632eaeabSMingming Cao 	unsigned int idx;
2766632eaeabSMingming Cao 	int i;
2767632eaeabSMingming Cao 
2768632eaeabSMingming Cao 	bh = page_buffers(page);
2769632eaeabSMingming Cao 	idx = offset >> inode->i_blkbits;
2770632eaeabSMingming Cao 
2771632eaeabSMingming Cao 	for (i = 0; i < idx; i++)
2772632eaeabSMingming Cao 		bh = bh->b_this_page;
2773632eaeabSMingming Cao 
277429fa89d0SAneesh Kumar K.V 	if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
2775632eaeabSMingming Cao 		return 0;
2776632eaeabSMingming Cao 	return 1;
2777632eaeabSMingming Cao }
2778632eaeabSMingming Cao 
277964769240SAlex Tomas static int ext4_da_write_end(struct file *file,
278064769240SAlex Tomas 			     struct address_space *mapping,
278164769240SAlex Tomas 			     loff_t pos, unsigned len, unsigned copied,
278264769240SAlex Tomas 			     struct page *page, void *fsdata)
278364769240SAlex Tomas {
278464769240SAlex Tomas 	struct inode *inode = mapping->host;
278564769240SAlex Tomas 	int ret = 0, ret2;
278664769240SAlex Tomas 	handle_t *handle = ext4_journal_current_handle();
278764769240SAlex Tomas 	loff_t new_i_size;
2788632eaeabSMingming Cao 	unsigned long start, end;
278979f0be8dSAneesh Kumar K.V 	int write_mode = (int)(unsigned long)fsdata;
279079f0be8dSAneesh Kumar K.V 
279174d553aaSTheodore Ts'o 	if (write_mode == FALL_BACK_TO_NONDELALLOC)
279274d553aaSTheodore Ts'o 		return ext4_write_end(file, mapping, pos,
279379f0be8dSAneesh Kumar K.V 				      len, copied, page, fsdata);
2794632eaeabSMingming Cao 
27959bffad1eSTheodore Ts'o 	trace_ext4_da_write_end(inode, pos, len, copied);
2796632eaeabSMingming Cao 	start = pos & (PAGE_CACHE_SIZE - 1);
2797632eaeabSMingming Cao 	end = start + copied - 1;
279864769240SAlex Tomas 
279964769240SAlex Tomas 	/*
280064769240SAlex Tomas 	 * generic_write_end() will run mark_inode_dirty() if i_size
280164769240SAlex Tomas 	 * changes.  So let's piggyback the i_disksize mark_inode_dirty
280264769240SAlex Tomas 	 * into that.
280364769240SAlex Tomas 	 */
280464769240SAlex Tomas 	new_i_size = pos + copied;
2805ea51d132SAndrea Arcangeli 	if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
28069c3569b5STao Ma 		if (ext4_has_inline_data(inode) ||
28079c3569b5STao Ma 		    ext4_da_should_update_i_disksize(page, end)) {
2808632eaeabSMingming Cao 			down_write(&EXT4_I(inode)->i_data_sem);
2809f3b59291STheodore Ts'o 			if (new_i_size > EXT4_I(inode)->i_disksize)
281064769240SAlex Tomas 				EXT4_I(inode)->i_disksize = new_i_size;
2811632eaeabSMingming Cao 			up_write(&EXT4_I(inode)->i_data_sem);
2812cf17fea6SAneesh Kumar K.V 			/* We need to mark inode dirty even if
2813cf17fea6SAneesh Kumar K.V 			 * new_i_size is less that inode->i_size
2814cf17fea6SAneesh Kumar K.V 			 * bu greater than i_disksize.(hint delalloc)
2815cf17fea6SAneesh Kumar K.V 			 */
2816cf17fea6SAneesh Kumar K.V 			ext4_mark_inode_dirty(handle, inode);
2817632eaeabSMingming Cao 		}
2818632eaeabSMingming Cao 	}
28199c3569b5STao Ma 
28209c3569b5STao Ma 	if (write_mode != CONVERT_INLINE_DATA &&
28219c3569b5STao Ma 	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
28229c3569b5STao Ma 	    ext4_has_inline_data(inode))
28239c3569b5STao Ma 		ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
28249c3569b5STao Ma 						     page);
28259c3569b5STao Ma 	else
282664769240SAlex Tomas 		ret2 = generic_write_end(file, mapping, pos, len, copied,
282764769240SAlex Tomas 							page, fsdata);
28289c3569b5STao Ma 
282964769240SAlex Tomas 	copied = ret2;
283064769240SAlex Tomas 	if (ret2 < 0)
283164769240SAlex Tomas 		ret = ret2;
283264769240SAlex Tomas 	ret2 = ext4_journal_stop(handle);
283364769240SAlex Tomas 	if (!ret)
283464769240SAlex Tomas 		ret = ret2;
283564769240SAlex Tomas 
283664769240SAlex Tomas 	return ret ? ret : copied;
283764769240SAlex Tomas }
283864769240SAlex Tomas 
283964769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
284064769240SAlex Tomas {
284164769240SAlex Tomas 	/*
284264769240SAlex Tomas 	 * Drop reserved blocks
284364769240SAlex Tomas 	 */
284464769240SAlex Tomas 	BUG_ON(!PageLocked(page));
284564769240SAlex Tomas 	if (!page_has_buffers(page))
284664769240SAlex Tomas 		goto out;
284764769240SAlex Tomas 
2848d2a17637SMingming Cao 	ext4_da_page_release_reservation(page, offset);
284964769240SAlex Tomas 
285064769240SAlex Tomas out:
285164769240SAlex Tomas 	ext4_invalidatepage(page, offset);
285264769240SAlex Tomas 
285364769240SAlex Tomas 	return;
285464769240SAlex Tomas }
285564769240SAlex Tomas 
2856ccd2506bSTheodore Ts'o /*
2857ccd2506bSTheodore Ts'o  * Force all delayed allocation blocks to be allocated for a given inode.
2858ccd2506bSTheodore Ts'o  */
2859ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode)
2860ccd2506bSTheodore Ts'o {
2861fb40ba0dSTheodore Ts'o 	trace_ext4_alloc_da_blocks(inode);
2862fb40ba0dSTheodore Ts'o 
2863ccd2506bSTheodore Ts'o 	if (!EXT4_I(inode)->i_reserved_data_blocks &&
2864ccd2506bSTheodore Ts'o 	    !EXT4_I(inode)->i_reserved_meta_blocks)
2865ccd2506bSTheodore Ts'o 		return 0;
2866ccd2506bSTheodore Ts'o 
2867ccd2506bSTheodore Ts'o 	/*
2868ccd2506bSTheodore Ts'o 	 * We do something simple for now.  The filemap_flush() will
2869ccd2506bSTheodore Ts'o 	 * also start triggering a write of the data blocks, which is
2870ccd2506bSTheodore Ts'o 	 * not strictly speaking necessary (and for users of
2871ccd2506bSTheodore Ts'o 	 * laptop_mode, not even desirable).  However, to do otherwise
2872ccd2506bSTheodore Ts'o 	 * would require replicating code paths in:
2873ccd2506bSTheodore Ts'o 	 *
2874ccd2506bSTheodore Ts'o 	 * ext4_da_writepages() ->
2875ccd2506bSTheodore Ts'o 	 *    write_cache_pages() ---> (via passed in callback function)
2876ccd2506bSTheodore Ts'o 	 *        __mpage_da_writepage() -->
2877ccd2506bSTheodore Ts'o 	 *           mpage_add_bh_to_extent()
2878ccd2506bSTheodore Ts'o 	 *           mpage_da_map_blocks()
2879ccd2506bSTheodore Ts'o 	 *
2880ccd2506bSTheodore Ts'o 	 * The problem is that write_cache_pages(), located in
2881ccd2506bSTheodore Ts'o 	 * mm/page-writeback.c, marks pages clean in preparation for
2882ccd2506bSTheodore Ts'o 	 * doing I/O, which is not desirable if we're not planning on
2883ccd2506bSTheodore Ts'o 	 * doing I/O at all.
2884ccd2506bSTheodore Ts'o 	 *
2885ccd2506bSTheodore Ts'o 	 * We could call write_cache_pages(), and then redirty all of
2886380cf090SWu Fengguang 	 * the pages by calling redirty_page_for_writepage() but that
2887ccd2506bSTheodore Ts'o 	 * would be ugly in the extreme.  So instead we would need to
2888ccd2506bSTheodore Ts'o 	 * replicate parts of the code in the above functions,
288925985edcSLucas De Marchi 	 * simplifying them because we wouldn't actually intend to
2890ccd2506bSTheodore Ts'o 	 * write out the pages, but rather only collect contiguous
2891ccd2506bSTheodore Ts'o 	 * logical block extents, call the multi-block allocator, and
2892ccd2506bSTheodore Ts'o 	 * then update the buffer heads with the block allocations.
2893ccd2506bSTheodore Ts'o 	 *
2894ccd2506bSTheodore Ts'o 	 * For now, though, we'll cheat by calling filemap_flush(),
2895ccd2506bSTheodore Ts'o 	 * which will map the blocks, and start the I/O, but not
2896ccd2506bSTheodore Ts'o 	 * actually wait for the I/O to complete.
2897ccd2506bSTheodore Ts'o 	 */
2898ccd2506bSTheodore Ts'o 	return filemap_flush(inode->i_mapping);
2899ccd2506bSTheodore Ts'o }
290064769240SAlex Tomas 
290164769240SAlex Tomas /*
2902ac27a0ecSDave Kleikamp  * bmap() is special.  It gets used by applications such as lilo and by
2903ac27a0ecSDave Kleikamp  * the swapper to find the on-disk block of a specific piece of data.
2904ac27a0ecSDave Kleikamp  *
2905ac27a0ecSDave Kleikamp  * Naturally, this is dangerous if the block concerned is still in the
2906617ba13bSMingming Cao  * journal.  If somebody makes a swapfile on an ext4 data-journaling
2907ac27a0ecSDave Kleikamp  * filesystem and enables swap, then they may get a nasty shock when the
2908ac27a0ecSDave Kleikamp  * data getting swapped to that swapfile suddenly gets overwritten by
2909ac27a0ecSDave Kleikamp  * the original zero's written out previously to the journal and
2910ac27a0ecSDave Kleikamp  * awaiting writeback in the kernel's buffer cache.
2911ac27a0ecSDave Kleikamp  *
2912ac27a0ecSDave Kleikamp  * So, if we see any bmap calls here on a modified, data-journaled file,
2913ac27a0ecSDave Kleikamp  * take extra steps to flush any blocks which might be in the cache.
2914ac27a0ecSDave Kleikamp  */
2915617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
2916ac27a0ecSDave Kleikamp {
2917ac27a0ecSDave Kleikamp 	struct inode *inode = mapping->host;
2918ac27a0ecSDave Kleikamp 	journal_t *journal;
2919ac27a0ecSDave Kleikamp 	int err;
2920ac27a0ecSDave Kleikamp 
292146c7f254STao Ma 	/*
292246c7f254STao Ma 	 * We can get here for an inline file via the FIBMAP ioctl
292346c7f254STao Ma 	 */
292446c7f254STao Ma 	if (ext4_has_inline_data(inode))
292546c7f254STao Ma 		return 0;
292646c7f254STao Ma 
292764769240SAlex Tomas 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
292864769240SAlex Tomas 			test_opt(inode->i_sb, DELALLOC)) {
292964769240SAlex Tomas 		/*
293064769240SAlex Tomas 		 * With delalloc we want to sync the file
293164769240SAlex Tomas 		 * so that we can make sure we allocate
293264769240SAlex Tomas 		 * blocks for file
293364769240SAlex Tomas 		 */
293464769240SAlex Tomas 		filemap_write_and_wait(mapping);
293564769240SAlex Tomas 	}
293664769240SAlex Tomas 
293719f5fb7aSTheodore Ts'o 	if (EXT4_JOURNAL(inode) &&
293819f5fb7aSTheodore Ts'o 	    ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
2939ac27a0ecSDave Kleikamp 		/*
2940ac27a0ecSDave Kleikamp 		 * This is a REALLY heavyweight approach, but the use of
2941ac27a0ecSDave Kleikamp 		 * bmap on dirty files is expected to be extremely rare:
2942ac27a0ecSDave Kleikamp 		 * only if we run lilo or swapon on a freshly made file
2943ac27a0ecSDave Kleikamp 		 * do we expect this to happen.
2944ac27a0ecSDave Kleikamp 		 *
2945ac27a0ecSDave Kleikamp 		 * (bmap requires CAP_SYS_RAWIO so this does not
2946ac27a0ecSDave Kleikamp 		 * represent an unprivileged user DOS attack --- we'd be
2947ac27a0ecSDave Kleikamp 		 * in trouble if mortal users could trigger this path at
2948ac27a0ecSDave Kleikamp 		 * will.)
2949ac27a0ecSDave Kleikamp 		 *
2950617ba13bSMingming Cao 		 * NB. EXT4_STATE_JDATA is not set on files other than
2951ac27a0ecSDave Kleikamp 		 * regular files.  If somebody wants to bmap a directory
2952ac27a0ecSDave Kleikamp 		 * or symlink and gets confused because the buffer
2953ac27a0ecSDave Kleikamp 		 * hasn't yet been flushed to disk, they deserve
2954ac27a0ecSDave Kleikamp 		 * everything they get.
2955ac27a0ecSDave Kleikamp 		 */
2956ac27a0ecSDave Kleikamp 
295719f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
2958617ba13bSMingming Cao 		journal = EXT4_JOURNAL(inode);
2959dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2960dab291afSMingming Cao 		err = jbd2_journal_flush(journal);
2961dab291afSMingming Cao 		jbd2_journal_unlock_updates(journal);
2962ac27a0ecSDave Kleikamp 
2963ac27a0ecSDave Kleikamp 		if (err)
2964ac27a0ecSDave Kleikamp 			return 0;
2965ac27a0ecSDave Kleikamp 	}
2966ac27a0ecSDave Kleikamp 
2967617ba13bSMingming Cao 	return generic_block_bmap(mapping, block, ext4_get_block);
2968ac27a0ecSDave Kleikamp }
2969ac27a0ecSDave Kleikamp 
2970617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page)
2971ac27a0ecSDave Kleikamp {
297246c7f254STao Ma 	int ret = -EAGAIN;
297346c7f254STao Ma 	struct inode *inode = page->mapping->host;
297446c7f254STao Ma 
29750562e0baSJiaying Zhang 	trace_ext4_readpage(page);
297646c7f254STao Ma 
297746c7f254STao Ma 	if (ext4_has_inline_data(inode))
297846c7f254STao Ma 		ret = ext4_readpage_inline(inode, page);
297946c7f254STao Ma 
298046c7f254STao Ma 	if (ret == -EAGAIN)
2981617ba13bSMingming Cao 		return mpage_readpage(page, ext4_get_block);
298246c7f254STao Ma 
298346c7f254STao Ma 	return ret;
2984ac27a0ecSDave Kleikamp }
2985ac27a0ecSDave Kleikamp 
2986ac27a0ecSDave Kleikamp static int
2987617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping,
2988ac27a0ecSDave Kleikamp 		struct list_head *pages, unsigned nr_pages)
2989ac27a0ecSDave Kleikamp {
299046c7f254STao Ma 	struct inode *inode = mapping->host;
299146c7f254STao Ma 
299246c7f254STao Ma 	/* If the file has inline data, no need to do readpages. */
299346c7f254STao Ma 	if (ext4_has_inline_data(inode))
299446c7f254STao Ma 		return 0;
299546c7f254STao Ma 
2996617ba13bSMingming Cao 	return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
2997ac27a0ecSDave Kleikamp }
2998ac27a0ecSDave Kleikamp 
2999617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset)
3000ac27a0ecSDave Kleikamp {
30010562e0baSJiaying Zhang 	trace_ext4_invalidatepage(page, offset);
30020562e0baSJiaying Zhang 
30034520fb3cSJan Kara 	/* No journalling happens on data buffers when this function is used */
30044520fb3cSJan Kara 	WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
30054520fb3cSJan Kara 
30064520fb3cSJan Kara 	block_invalidatepage(page, offset);
30074520fb3cSJan Kara }
30084520fb3cSJan Kara 
300953e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page,
30104520fb3cSJan Kara 					    unsigned long offset)
30114520fb3cSJan Kara {
30124520fb3cSJan Kara 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
30134520fb3cSJan Kara 
30144520fb3cSJan Kara 	trace_ext4_journalled_invalidatepage(page, offset);
30154520fb3cSJan Kara 
3016744692dcSJiaying Zhang 	/*
3017ac27a0ecSDave Kleikamp 	 * If it's a full truncate we just forget about the pending dirtying
3018ac27a0ecSDave Kleikamp 	 */
3019ac27a0ecSDave Kleikamp 	if (offset == 0)
3020ac27a0ecSDave Kleikamp 		ClearPageChecked(page);
3021ac27a0ecSDave Kleikamp 
302253e87268SJan Kara 	return jbd2_journal_invalidatepage(journal, page, offset);
302353e87268SJan Kara }
302453e87268SJan Kara 
302553e87268SJan Kara /* Wrapper for aops... */
302653e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page,
302753e87268SJan Kara 					   unsigned long offset)
302853e87268SJan Kara {
302953e87268SJan Kara 	WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
3030ac27a0ecSDave Kleikamp }
3031ac27a0ecSDave Kleikamp 
3032617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait)
3033ac27a0ecSDave Kleikamp {
3034617ba13bSMingming Cao 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3035ac27a0ecSDave Kleikamp 
30360562e0baSJiaying Zhang 	trace_ext4_releasepage(page);
30370562e0baSJiaying Zhang 
3038e1c36595SJan Kara 	/* Page has dirty journalled data -> cannot release */
3039e1c36595SJan Kara 	if (PageChecked(page))
3040ac27a0ecSDave Kleikamp 		return 0;
30410390131bSFrank Mayhar 	if (journal)
3042dab291afSMingming Cao 		return jbd2_journal_try_to_free_buffers(journal, page, wait);
30430390131bSFrank Mayhar 	else
30440390131bSFrank Mayhar 		return try_to_free_buffers(page);
3045ac27a0ecSDave Kleikamp }
3046ac27a0ecSDave Kleikamp 
3047ac27a0ecSDave Kleikamp /*
30482ed88685STheodore Ts'o  * ext4_get_block used when preparing for a DIO write or buffer write.
30492ed88685STheodore Ts'o  * We allocate an uinitialized extent if blocks haven't been allocated.
30502ed88685STheodore Ts'o  * The extent will be converted to initialized after the IO is complete.
30512ed88685STheodore Ts'o  */
3052f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock,
30534c0425ffSMingming Cao 		   struct buffer_head *bh_result, int create)
30544c0425ffSMingming Cao {
3055c7064ef1SJiaying Zhang 	ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
30568d5d02e6SMingming Cao 		   inode->i_ino, create);
30572ed88685STheodore Ts'o 	return _ext4_get_block(inode, iblock, bh_result,
30582ed88685STheodore Ts'o 			       EXT4_GET_BLOCKS_IO_CREATE_EXT);
30594c0425ffSMingming Cao }
30604c0425ffSMingming Cao 
3061729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
30628b0f165fSAnatol Pomozov 		   struct buffer_head *bh_result, int create)
3063729f52c6SZheng Liu {
30648b0f165fSAnatol Pomozov 	ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
30658b0f165fSAnatol Pomozov 		   inode->i_ino, create);
30668b0f165fSAnatol Pomozov 	return _ext4_get_block(inode, iblock, bh_result,
30678b0f165fSAnatol Pomozov 			       EXT4_GET_BLOCKS_NO_LOCK);
3068729f52c6SZheng Liu }
3069729f52c6SZheng Liu 
30704c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3071552ef802SChristoph Hellwig 			    ssize_t size, void *private, int ret,
3072552ef802SChristoph Hellwig 			    bool is_async)
30734c0425ffSMingming Cao {
3074496ad9aaSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
30754c0425ffSMingming Cao         ext4_io_end_t *io_end = iocb->private;
30764c0425ffSMingming Cao 
3077*4eec708dSJan Kara 	/* if not async direct IO just return */
3078*4eec708dSJan Kara 	if (!io_end) {
3079*4eec708dSJan Kara 		inode_dio_done(inode);
3080*4eec708dSJan Kara 		if (is_async)
3081*4eec708dSJan Kara 			aio_complete(iocb, ret, 0);
3082*4eec708dSJan Kara 		return;
3083*4eec708dSJan Kara 	}
30844b70df18SMingming 
30858d5d02e6SMingming Cao 	ext_debug("ext4_end_io_dio(): io_end 0x%p "
3086ace36ad4SJoe Perches 		  "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
30878d5d02e6SMingming Cao  		  iocb->private, io_end->inode->i_ino, iocb, offset,
30888d5d02e6SMingming Cao 		  size);
30898d5d02e6SMingming Cao 
3090b5a7e970STheodore Ts'o 	iocb->private = NULL;
30914c0425ffSMingming Cao 	io_end->offset = offset;
30924c0425ffSMingming Cao 	io_end->size = size;
30935b3ff237Sjiayingz@google.com (Jiaying Zhang) 	if (is_async) {
30945b3ff237Sjiayingz@google.com (Jiaying Zhang) 		io_end->iocb = iocb;
30955b3ff237Sjiayingz@google.com (Jiaying Zhang) 		io_end->result = ret;
30965b3ff237Sjiayingz@google.com (Jiaying Zhang) 	}
3097*4eec708dSJan Kara 	ext4_put_io_end_defer(io_end);
30984c0425ffSMingming Cao }
3099c7064ef1SJiaying Zhang 
31004c0425ffSMingming Cao /*
31014c0425ffSMingming Cao  * For ext4 extent files, ext4 will do direct-io write to holes,
31024c0425ffSMingming Cao  * preallocated extents, and those write extend the file, no need to
31034c0425ffSMingming Cao  * fall back to buffered IO.
31044c0425ffSMingming Cao  *
3105b595076aSUwe Kleine-König  * For holes, we fallocate those blocks, mark them as uninitialized
310669c499d1STheodore Ts'o  * If those blocks were preallocated, we mark sure they are split, but
3107b595076aSUwe Kleine-König  * still keep the range to write as uninitialized.
31084c0425ffSMingming Cao  *
310969c499d1STheodore Ts'o  * The unwritten extents will be converted to written when DIO is completed.
31108d5d02e6SMingming Cao  * For async direct IO, since the IO may still pending when return, we
311125985edcSLucas De Marchi  * set up an end_io call back function, which will do the conversion
31128d5d02e6SMingming Cao  * when async direct IO completed.
31134c0425ffSMingming Cao  *
31144c0425ffSMingming Cao  * If the O_DIRECT write will extend the file then add this inode to the
31154c0425ffSMingming Cao  * orphan list.  So recovery will truncate it back to the original size
31164c0425ffSMingming Cao  * if the machine crashes during the write.
31174c0425ffSMingming Cao  *
31184c0425ffSMingming Cao  */
31194c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
31204c0425ffSMingming Cao 			      const struct iovec *iov, loff_t offset,
31214c0425ffSMingming Cao 			      unsigned long nr_segs)
31224c0425ffSMingming Cao {
31234c0425ffSMingming Cao 	struct file *file = iocb->ki_filp;
31244c0425ffSMingming Cao 	struct inode *inode = file->f_mapping->host;
31254c0425ffSMingming Cao 	ssize_t ret;
31264c0425ffSMingming Cao 	size_t count = iov_length(iov, nr_segs);
3127729f52c6SZheng Liu 	int overwrite = 0;
31288b0f165fSAnatol Pomozov 	get_block_t *get_block_func = NULL;
31298b0f165fSAnatol Pomozov 	int dio_flags = 0;
313069c499d1STheodore Ts'o 	loff_t final_size = offset + count;
3131*4eec708dSJan Kara 	ext4_io_end_t *io_end = NULL;
313269c499d1STheodore Ts'o 
313369c499d1STheodore Ts'o 	/* Use the old path for reads and writes beyond i_size. */
313469c499d1STheodore Ts'o 	if (rw != WRITE || final_size > inode->i_size)
313569c499d1STheodore Ts'o 		return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3136729f52c6SZheng Liu 
31374bd809dbSZheng Liu 	BUG_ON(iocb->private == NULL);
31384bd809dbSZheng Liu 
31394bd809dbSZheng Liu 	/* If we do a overwrite dio, i_mutex locking can be released */
31404bd809dbSZheng Liu 	overwrite = *((int *)iocb->private);
31414bd809dbSZheng Liu 
31424bd809dbSZheng Liu 	if (overwrite) {
31431f555cfaSDmitry Monakhov 		atomic_inc(&inode->i_dio_count);
31444bd809dbSZheng Liu 		down_read(&EXT4_I(inode)->i_data_sem);
31454bd809dbSZheng Liu 		mutex_unlock(&inode->i_mutex);
31464bd809dbSZheng Liu 	}
31474bd809dbSZheng Liu 
31484c0425ffSMingming Cao 	/*
31498d5d02e6SMingming Cao 	 * We could direct write to holes and fallocate.
31508d5d02e6SMingming Cao 	 *
315169c499d1STheodore Ts'o 	 * Allocated blocks to fill the hole are marked as
315269c499d1STheodore Ts'o 	 * uninitialized to prevent parallel buffered read to expose
315369c499d1STheodore Ts'o 	 * the stale data before DIO complete the data IO.
31548d5d02e6SMingming Cao 	 *
315569c499d1STheodore Ts'o 	 * As to previously fallocated extents, ext4 get_block will
315669c499d1STheodore Ts'o 	 * just simply mark the buffer mapped but still keep the
315769c499d1STheodore Ts'o 	 * extents uninitialized.
31584c0425ffSMingming Cao 	 *
315969c499d1STheodore Ts'o 	 * For non AIO case, we will convert those unwritten extents
31608d5d02e6SMingming Cao 	 * to written after return back from blockdev_direct_IO.
31614c0425ffSMingming Cao 	 *
316269c499d1STheodore Ts'o 	 * For async DIO, the conversion needs to be deferred when the
316369c499d1STheodore Ts'o 	 * IO is completed. The ext4 end_io callback function will be
316469c499d1STheodore Ts'o 	 * called to take care of the conversion work.  Here for async
316569c499d1STheodore Ts'o 	 * case, we allocate an io_end structure to hook to the iocb.
31664c0425ffSMingming Cao 	 */
31678d5d02e6SMingming Cao 	iocb->private = NULL;
3168f45ee3a1SDmitry Monakhov 	ext4_inode_aio_set(inode, NULL);
31698d5d02e6SMingming Cao 	if (!is_sync_kiocb(iocb)) {
3170*4eec708dSJan Kara 		io_end = ext4_init_io_end(inode, GFP_NOFS);
31714bd809dbSZheng Liu 		if (!io_end) {
31724bd809dbSZheng Liu 			ret = -ENOMEM;
31734bd809dbSZheng Liu 			goto retake_lock;
31744bd809dbSZheng Liu 		}
3175266991b1SJeff Moyer 		io_end->flag |= EXT4_IO_END_DIRECT;
3176*4eec708dSJan Kara 		/*
3177*4eec708dSJan Kara 		 * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3178*4eec708dSJan Kara 		 */
3179*4eec708dSJan Kara 		iocb->private = ext4_get_io_end(io_end);
31808d5d02e6SMingming Cao 		/*
318169c499d1STheodore Ts'o 		 * we save the io structure for current async direct
318269c499d1STheodore Ts'o 		 * IO, so that later ext4_map_blocks() could flag the
318369c499d1STheodore Ts'o 		 * io structure whether there is a unwritten extents
318469c499d1STheodore Ts'o 		 * needs to be converted when IO is completed.
31858d5d02e6SMingming Cao 		 */
3186f45ee3a1SDmitry Monakhov 		ext4_inode_aio_set(inode, io_end);
31878d5d02e6SMingming Cao 	}
31888d5d02e6SMingming Cao 
31898b0f165fSAnatol Pomozov 	if (overwrite) {
31908b0f165fSAnatol Pomozov 		get_block_func = ext4_get_block_write_nolock;
31918b0f165fSAnatol Pomozov 	} else {
31928b0f165fSAnatol Pomozov 		get_block_func = ext4_get_block_write;
31938b0f165fSAnatol Pomozov 		dio_flags = DIO_LOCKING;
31948b0f165fSAnatol Pomozov 	}
3195729f52c6SZheng Liu 	ret = __blockdev_direct_IO(rw, iocb, inode,
3196729f52c6SZheng Liu 				   inode->i_sb->s_bdev, iov,
3197729f52c6SZheng Liu 				   offset, nr_segs,
31988b0f165fSAnatol Pomozov 				   get_block_func,
3199729f52c6SZheng Liu 				   ext4_end_io_dio,
3200729f52c6SZheng Liu 				   NULL,
32018b0f165fSAnatol Pomozov 				   dio_flags);
32028b0f165fSAnatol Pomozov 
32038d5d02e6SMingming Cao 	/*
3204*4eec708dSJan Kara 	 * Put our reference to io_end. This can free the io_end structure e.g.
3205*4eec708dSJan Kara 	 * in sync IO case or in case of error. It can even perform extent
3206*4eec708dSJan Kara 	 * conversion if all bios we submitted finished before we got here.
3207*4eec708dSJan Kara 	 * Note that in that case iocb->private can be already set to NULL
3208*4eec708dSJan Kara 	 * here.
32098d5d02e6SMingming Cao 	 */
3210*4eec708dSJan Kara 	if (io_end) {
3211*4eec708dSJan Kara 		ext4_inode_aio_set(inode, NULL);
3212*4eec708dSJan Kara 		ext4_put_io_end(io_end);
3213*4eec708dSJan Kara 		/*
3214*4eec708dSJan Kara 		 * In case of error or no write ext4_end_io_dio() was not
3215*4eec708dSJan Kara 		 * called so we have to put iocb's reference.
3216*4eec708dSJan Kara 		 */
3217*4eec708dSJan Kara 		if (ret <= 0 && ret != -EIOCBQUEUED) {
3218*4eec708dSJan Kara 			WARN_ON(iocb->private != io_end);
3219*4eec708dSJan Kara 			ext4_put_io_end(io_end);
32208d5d02e6SMingming Cao 			iocb->private = NULL;
3221*4eec708dSJan Kara 		}
3222*4eec708dSJan Kara 	}
3223*4eec708dSJan Kara 	if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
32245f524950SMingming 						EXT4_STATE_DIO_UNWRITTEN)) {
3225109f5565SMingming 		int err;
32268d5d02e6SMingming Cao 		/*
32278d5d02e6SMingming Cao 		 * for non AIO case, since the IO is already
322825985edcSLucas De Marchi 		 * completed, we could do the conversion right here
32298d5d02e6SMingming Cao 		 */
3230109f5565SMingming 		err = ext4_convert_unwritten_extents(inode,
32318d5d02e6SMingming Cao 						     offset, ret);
3232109f5565SMingming 		if (err < 0)
3233109f5565SMingming 			ret = err;
323419f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3235109f5565SMingming 	}
32364bd809dbSZheng Liu 
32374bd809dbSZheng Liu retake_lock:
32384bd809dbSZheng Liu 	/* take i_mutex locking again if we do a ovewrite dio */
32394bd809dbSZheng Liu 	if (overwrite) {
32401f555cfaSDmitry Monakhov 		inode_dio_done(inode);
32414bd809dbSZheng Liu 		up_read(&EXT4_I(inode)->i_data_sem);
32424bd809dbSZheng Liu 		mutex_lock(&inode->i_mutex);
32434bd809dbSZheng Liu 	}
32444bd809dbSZheng Liu 
32454c0425ffSMingming Cao 	return ret;
32464c0425ffSMingming Cao }
32478d5d02e6SMingming Cao 
32484c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
32494c0425ffSMingming Cao 			      const struct iovec *iov, loff_t offset,
32504c0425ffSMingming Cao 			      unsigned long nr_segs)
32514c0425ffSMingming Cao {
32524c0425ffSMingming Cao 	struct file *file = iocb->ki_filp;
32534c0425ffSMingming Cao 	struct inode *inode = file->f_mapping->host;
32540562e0baSJiaying Zhang 	ssize_t ret;
32554c0425ffSMingming Cao 
325684ebd795STheodore Ts'o 	/*
325784ebd795STheodore Ts'o 	 * If we are doing data journalling we don't support O_DIRECT
325884ebd795STheodore Ts'o 	 */
325984ebd795STheodore Ts'o 	if (ext4_should_journal_data(inode))
326084ebd795STheodore Ts'o 		return 0;
326184ebd795STheodore Ts'o 
326246c7f254STao Ma 	/* Let buffer I/O handle the inline data case. */
326346c7f254STao Ma 	if (ext4_has_inline_data(inode))
326446c7f254STao Ma 		return 0;
326546c7f254STao Ma 
32660562e0baSJiaying Zhang 	trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
326712e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
32680562e0baSJiaying Zhang 		ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
32690562e0baSJiaying Zhang 	else
32700562e0baSJiaying Zhang 		ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
32710562e0baSJiaying Zhang 	trace_ext4_direct_IO_exit(inode, offset,
32720562e0baSJiaying Zhang 				iov_length(iov, nr_segs), rw, ret);
32730562e0baSJiaying Zhang 	return ret;
32744c0425ffSMingming Cao }
32754c0425ffSMingming Cao 
3276ac27a0ecSDave Kleikamp /*
3277617ba13bSMingming Cao  * Pages can be marked dirty completely asynchronously from ext4's journalling
3278ac27a0ecSDave Kleikamp  * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do
3279ac27a0ecSDave Kleikamp  * much here because ->set_page_dirty is called under VFS locks.  The page is
3280ac27a0ecSDave Kleikamp  * not necessarily locked.
3281ac27a0ecSDave Kleikamp  *
3282ac27a0ecSDave Kleikamp  * We cannot just dirty the page and leave attached buffers clean, because the
3283ac27a0ecSDave Kleikamp  * buffers' dirty state is "definitive".  We cannot just set the buffers dirty
3284ac27a0ecSDave Kleikamp  * or jbddirty because all the journalling code will explode.
3285ac27a0ecSDave Kleikamp  *
3286ac27a0ecSDave Kleikamp  * So what we do is to mark the page "pending dirty" and next time writepage
3287ac27a0ecSDave Kleikamp  * is called, propagate that into the buffers appropriately.
3288ac27a0ecSDave Kleikamp  */
3289617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page)
3290ac27a0ecSDave Kleikamp {
3291ac27a0ecSDave Kleikamp 	SetPageChecked(page);
3292ac27a0ecSDave Kleikamp 	return __set_page_dirty_nobuffers(page);
3293ac27a0ecSDave Kleikamp }
3294ac27a0ecSDave Kleikamp 
329574d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = {
3296617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3297617ba13bSMingming Cao 	.readpages		= ext4_readpages,
329843ce1d23SAneesh Kumar K.V 	.writepage		= ext4_writepage,
3299bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
330074d553aaSTheodore Ts'o 	.write_end		= ext4_write_end,
3301617ba13bSMingming Cao 	.bmap			= ext4_bmap,
3302617ba13bSMingming Cao 	.invalidatepage		= ext4_invalidatepage,
3303617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
3304617ba13bSMingming Cao 	.direct_IO		= ext4_direct_IO,
3305ac27a0ecSDave Kleikamp 	.migratepage		= buffer_migrate_page,
33068ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3307aa261f54SAndi Kleen 	.error_remove_page	= generic_error_remove_page,
3308ac27a0ecSDave Kleikamp };
3309ac27a0ecSDave Kleikamp 
3310617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = {
3311617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3312617ba13bSMingming Cao 	.readpages		= ext4_readpages,
331343ce1d23SAneesh Kumar K.V 	.writepage		= ext4_writepage,
3314bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
3315bfc1af65SNick Piggin 	.write_end		= ext4_journalled_write_end,
3316617ba13bSMingming Cao 	.set_page_dirty		= ext4_journalled_set_page_dirty,
3317617ba13bSMingming Cao 	.bmap			= ext4_bmap,
33184520fb3cSJan Kara 	.invalidatepage		= ext4_journalled_invalidatepage,
3319617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
332084ebd795STheodore Ts'o 	.direct_IO		= ext4_direct_IO,
33218ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3322aa261f54SAndi Kleen 	.error_remove_page	= generic_error_remove_page,
3323ac27a0ecSDave Kleikamp };
3324ac27a0ecSDave Kleikamp 
332564769240SAlex Tomas static const struct address_space_operations ext4_da_aops = {
332664769240SAlex Tomas 	.readpage		= ext4_readpage,
332764769240SAlex Tomas 	.readpages		= ext4_readpages,
332843ce1d23SAneesh Kumar K.V 	.writepage		= ext4_writepage,
332964769240SAlex Tomas 	.writepages		= ext4_da_writepages,
333064769240SAlex Tomas 	.write_begin		= ext4_da_write_begin,
333164769240SAlex Tomas 	.write_end		= ext4_da_write_end,
333264769240SAlex Tomas 	.bmap			= ext4_bmap,
333364769240SAlex Tomas 	.invalidatepage		= ext4_da_invalidatepage,
333464769240SAlex Tomas 	.releasepage		= ext4_releasepage,
333564769240SAlex Tomas 	.direct_IO		= ext4_direct_IO,
333664769240SAlex Tomas 	.migratepage		= buffer_migrate_page,
33378ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3338aa261f54SAndi Kleen 	.error_remove_page	= generic_error_remove_page,
333964769240SAlex Tomas };
334064769240SAlex Tomas 
3341617ba13bSMingming Cao void ext4_set_aops(struct inode *inode)
3342ac27a0ecSDave Kleikamp {
33433d2b1582SLukas Czerner 	switch (ext4_inode_journal_mode(inode)) {
33443d2b1582SLukas Czerner 	case EXT4_INODE_ORDERED_DATA_MODE:
334574d553aaSTheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
33463d2b1582SLukas Czerner 		break;
33473d2b1582SLukas Czerner 	case EXT4_INODE_WRITEBACK_DATA_MODE:
334874d553aaSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
33493d2b1582SLukas Czerner 		break;
33503d2b1582SLukas Czerner 	case EXT4_INODE_JOURNAL_DATA_MODE:
3351617ba13bSMingming Cao 		inode->i_mapping->a_ops = &ext4_journalled_aops;
335274d553aaSTheodore Ts'o 		return;
33533d2b1582SLukas Czerner 	default:
33543d2b1582SLukas Czerner 		BUG();
33553d2b1582SLukas Czerner 	}
335674d553aaSTheodore Ts'o 	if (test_opt(inode->i_sb, DELALLOC))
335774d553aaSTheodore Ts'o 		inode->i_mapping->a_ops = &ext4_da_aops;
335874d553aaSTheodore Ts'o 	else
335974d553aaSTheodore Ts'o 		inode->i_mapping->a_ops = &ext4_aops;
3360ac27a0ecSDave Kleikamp }
3361ac27a0ecSDave Kleikamp 
33624e96b2dbSAllison Henderson 
33634e96b2dbSAllison Henderson /*
33644e96b2dbSAllison Henderson  * ext4_discard_partial_page_buffers()
33654e96b2dbSAllison Henderson  * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
33664e96b2dbSAllison Henderson  * This function finds and locks the page containing the offset
33674e96b2dbSAllison Henderson  * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
33684e96b2dbSAllison Henderson  * Calling functions that already have the page locked should call
33694e96b2dbSAllison Henderson  * ext4_discard_partial_page_buffers_no_lock directly.
33704e96b2dbSAllison Henderson  */
33714e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle,
33724e96b2dbSAllison Henderson 		struct address_space *mapping, loff_t from,
33734e96b2dbSAllison Henderson 		loff_t length, int flags)
33744e96b2dbSAllison Henderson {
33754e96b2dbSAllison Henderson 	struct inode *inode = mapping->host;
33764e96b2dbSAllison Henderson 	struct page *page;
33774e96b2dbSAllison Henderson 	int err = 0;
33784e96b2dbSAllison Henderson 
33794e96b2dbSAllison Henderson 	page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
33804e96b2dbSAllison Henderson 				   mapping_gfp_mask(mapping) & ~__GFP_FS);
33814e96b2dbSAllison Henderson 	if (!page)
33825129d05fSYongqiang Yang 		return -ENOMEM;
33834e96b2dbSAllison Henderson 
33844e96b2dbSAllison Henderson 	err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
33854e96b2dbSAllison Henderson 		from, length, flags);
33864e96b2dbSAllison Henderson 
33874e96b2dbSAllison Henderson 	unlock_page(page);
33884e96b2dbSAllison Henderson 	page_cache_release(page);
33894e96b2dbSAllison Henderson 	return err;
33904e96b2dbSAllison Henderson }
33914e96b2dbSAllison Henderson 
33924e96b2dbSAllison Henderson /*
33934e96b2dbSAllison Henderson  * ext4_discard_partial_page_buffers_no_lock()
33944e96b2dbSAllison Henderson  * Zeros a page range of length 'length' starting from offset 'from'.
33954e96b2dbSAllison Henderson  * Buffer heads that correspond to the block aligned regions of the
33964e96b2dbSAllison Henderson  * zeroed range will be unmapped.  Unblock aligned regions
33974e96b2dbSAllison Henderson  * will have the corresponding buffer head mapped if needed so that
33984e96b2dbSAllison Henderson  * that region of the page can be updated with the partial zero out.
33994e96b2dbSAllison Henderson  *
34004e96b2dbSAllison Henderson  * This function assumes that the page has already been  locked.  The
34014e96b2dbSAllison Henderson  * The range to be discarded must be contained with in the given page.
34024e96b2dbSAllison Henderson  * If the specified range exceeds the end of the page it will be shortened
34034e96b2dbSAllison Henderson  * to the end of the page that corresponds to 'from'.  This function is
34044e96b2dbSAllison Henderson  * appropriate for updating a page and it buffer heads to be unmapped and
34054e96b2dbSAllison Henderson  * zeroed for blocks that have been either released, or are going to be
34064e96b2dbSAllison Henderson  * released.
34074e96b2dbSAllison Henderson  *
34084e96b2dbSAllison Henderson  * handle: The journal handle
34094e96b2dbSAllison Henderson  * inode:  The files inode
34104e96b2dbSAllison Henderson  * page:   A locked page that contains the offset "from"
34114907cb7bSAnatol Pomozov  * from:   The starting byte offset (from the beginning of the file)
34124e96b2dbSAllison Henderson  *         to begin discarding
34134e96b2dbSAllison Henderson  * len:    The length of bytes to discard
34144e96b2dbSAllison Henderson  * flags:  Optional flags that may be used:
34154e96b2dbSAllison Henderson  *
34164e96b2dbSAllison Henderson  *         EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
34174e96b2dbSAllison Henderson  *         Only zero the regions of the page whose buffer heads
34184e96b2dbSAllison Henderson  *         have already been unmapped.  This flag is appropriate
34194907cb7bSAnatol Pomozov  *         for updating the contents of a page whose blocks may
34204e96b2dbSAllison Henderson  *         have already been released, and we only want to zero
34214e96b2dbSAllison Henderson  *         out the regions that correspond to those released blocks.
34224e96b2dbSAllison Henderson  *
34234907cb7bSAnatol Pomozov  * Returns zero on success or negative on failure.
34244e96b2dbSAllison Henderson  */
34255f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
34264e96b2dbSAllison Henderson 		struct inode *inode, struct page *page, loff_t from,
34274e96b2dbSAllison Henderson 		loff_t length, int flags)
34284e96b2dbSAllison Henderson {
34294e96b2dbSAllison Henderson 	ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
34304e96b2dbSAllison Henderson 	unsigned int offset = from & (PAGE_CACHE_SIZE-1);
34314e96b2dbSAllison Henderson 	unsigned int blocksize, max, pos;
34324e96b2dbSAllison Henderson 	ext4_lblk_t iblock;
34334e96b2dbSAllison Henderson 	struct buffer_head *bh;
34344e96b2dbSAllison Henderson 	int err = 0;
34354e96b2dbSAllison Henderson 
34364e96b2dbSAllison Henderson 	blocksize = inode->i_sb->s_blocksize;
34374e96b2dbSAllison Henderson 	max = PAGE_CACHE_SIZE - offset;
34384e96b2dbSAllison Henderson 
34394e96b2dbSAllison Henderson 	if (index != page->index)
34404e96b2dbSAllison Henderson 		return -EINVAL;
34414e96b2dbSAllison Henderson 
34424e96b2dbSAllison Henderson 	/*
34434e96b2dbSAllison Henderson 	 * correct length if it does not fall between
34444e96b2dbSAllison Henderson 	 * 'from' and the end of the page
34454e96b2dbSAllison Henderson 	 */
34464e96b2dbSAllison Henderson 	if (length > max || length < 0)
34474e96b2dbSAllison Henderson 		length = max;
34484e96b2dbSAllison Henderson 
34494e96b2dbSAllison Henderson 	iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
34504e96b2dbSAllison Henderson 
3451093e6e36SYongqiang Yang 	if (!page_has_buffers(page))
34524e96b2dbSAllison Henderson 		create_empty_buffers(page, blocksize, 0);
34534e96b2dbSAllison Henderson 
34544e96b2dbSAllison Henderson 	/* Find the buffer that contains "offset" */
34554e96b2dbSAllison Henderson 	bh = page_buffers(page);
34564e96b2dbSAllison Henderson 	pos = blocksize;
34574e96b2dbSAllison Henderson 	while (offset >= pos) {
34584e96b2dbSAllison Henderson 		bh = bh->b_this_page;
34594e96b2dbSAllison Henderson 		iblock++;
34604e96b2dbSAllison Henderson 		pos += blocksize;
34614e96b2dbSAllison Henderson 	}
34624e96b2dbSAllison Henderson 
34634e96b2dbSAllison Henderson 	pos = offset;
34644e96b2dbSAllison Henderson 	while (pos < offset + length) {
3465e260daf2SYongqiang Yang 		unsigned int end_of_block, range_to_discard;
3466e260daf2SYongqiang Yang 
34674e96b2dbSAllison Henderson 		err = 0;
34684e96b2dbSAllison Henderson 
34694e96b2dbSAllison Henderson 		/* The length of space left to zero and unmap */
34704e96b2dbSAllison Henderson 		range_to_discard = offset + length - pos;
34714e96b2dbSAllison Henderson 
34724e96b2dbSAllison Henderson 		/* The length of space until the end of the block */
34734e96b2dbSAllison Henderson 		end_of_block = blocksize - (pos & (blocksize-1));
34744e96b2dbSAllison Henderson 
34754e96b2dbSAllison Henderson 		/*
34764e96b2dbSAllison Henderson 		 * Do not unmap or zero past end of block
34774e96b2dbSAllison Henderson 		 * for this buffer head
34784e96b2dbSAllison Henderson 		 */
34794e96b2dbSAllison Henderson 		if (range_to_discard > end_of_block)
34804e96b2dbSAllison Henderson 			range_to_discard = end_of_block;
34814e96b2dbSAllison Henderson 
34824e96b2dbSAllison Henderson 
34834e96b2dbSAllison Henderson 		/*
34844e96b2dbSAllison Henderson 		 * Skip this buffer head if we are only zeroing unampped
34854e96b2dbSAllison Henderson 		 * regions of the page
34864e96b2dbSAllison Henderson 		 */
34874e96b2dbSAllison Henderson 		if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
34884e96b2dbSAllison Henderson 			buffer_mapped(bh))
34894e96b2dbSAllison Henderson 				goto next;
34904e96b2dbSAllison Henderson 
34914e96b2dbSAllison Henderson 		/* If the range is block aligned, unmap */
34924e96b2dbSAllison Henderson 		if (range_to_discard == blocksize) {
34934e96b2dbSAllison Henderson 			clear_buffer_dirty(bh);
34944e96b2dbSAllison Henderson 			bh->b_bdev = NULL;
34954e96b2dbSAllison Henderson 			clear_buffer_mapped(bh);
34964e96b2dbSAllison Henderson 			clear_buffer_req(bh);
34974e96b2dbSAllison Henderson 			clear_buffer_new(bh);
34984e96b2dbSAllison Henderson 			clear_buffer_delay(bh);
34994e96b2dbSAllison Henderson 			clear_buffer_unwritten(bh);
35004e96b2dbSAllison Henderson 			clear_buffer_uptodate(bh);
35014e96b2dbSAllison Henderson 			zero_user(page, pos, range_to_discard);
35024e96b2dbSAllison Henderson 			BUFFER_TRACE(bh, "Buffer discarded");
35034e96b2dbSAllison Henderson 			goto next;
35044e96b2dbSAllison Henderson 		}
35054e96b2dbSAllison Henderson 
35064e96b2dbSAllison Henderson 		/*
35074e96b2dbSAllison Henderson 		 * If this block is not completely contained in the range
35084e96b2dbSAllison Henderson 		 * to be discarded, then it is not going to be released. Because
35094e96b2dbSAllison Henderson 		 * we need to keep this block, we need to make sure this part
35104e96b2dbSAllison Henderson 		 * of the page is uptodate before we modify it by writeing
35114e96b2dbSAllison Henderson 		 * partial zeros on it.
35124e96b2dbSAllison Henderson 		 */
35134e96b2dbSAllison Henderson 		if (!buffer_mapped(bh)) {
35144e96b2dbSAllison Henderson 			/*
35154e96b2dbSAllison Henderson 			 * Buffer head must be mapped before we can read
35164e96b2dbSAllison Henderson 			 * from the block
35174e96b2dbSAllison Henderson 			 */
35184e96b2dbSAllison Henderson 			BUFFER_TRACE(bh, "unmapped");
35194e96b2dbSAllison Henderson 			ext4_get_block(inode, iblock, bh, 0);
35204e96b2dbSAllison Henderson 			/* unmapped? It's a hole - nothing to do */
35214e96b2dbSAllison Henderson 			if (!buffer_mapped(bh)) {
35224e96b2dbSAllison Henderson 				BUFFER_TRACE(bh, "still unmapped");
35234e96b2dbSAllison Henderson 				goto next;
35244e96b2dbSAllison Henderson 			}
35254e96b2dbSAllison Henderson 		}
35264e96b2dbSAllison Henderson 
35274e96b2dbSAllison Henderson 		/* Ok, it's mapped. Make sure it's up-to-date */
35284e96b2dbSAllison Henderson 		if (PageUptodate(page))
35294e96b2dbSAllison Henderson 			set_buffer_uptodate(bh);
35304e96b2dbSAllison Henderson 
35314e96b2dbSAllison Henderson 		if (!buffer_uptodate(bh)) {
35324e96b2dbSAllison Henderson 			err = -EIO;
35334e96b2dbSAllison Henderson 			ll_rw_block(READ, 1, &bh);
35344e96b2dbSAllison Henderson 			wait_on_buffer(bh);
35354e96b2dbSAllison Henderson 			/* Uhhuh. Read error. Complain and punt.*/
35364e96b2dbSAllison Henderson 			if (!buffer_uptodate(bh))
35374e96b2dbSAllison Henderson 				goto next;
35384e96b2dbSAllison Henderson 		}
35394e96b2dbSAllison Henderson 
35404e96b2dbSAllison Henderson 		if (ext4_should_journal_data(inode)) {
35414e96b2dbSAllison Henderson 			BUFFER_TRACE(bh, "get write access");
35424e96b2dbSAllison Henderson 			err = ext4_journal_get_write_access(handle, bh);
35434e96b2dbSAllison Henderson 			if (err)
35444e96b2dbSAllison Henderson 				goto next;
35454e96b2dbSAllison Henderson 		}
35464e96b2dbSAllison Henderson 
35474e96b2dbSAllison Henderson 		zero_user(page, pos, range_to_discard);
35484e96b2dbSAllison Henderson 
35494e96b2dbSAllison Henderson 		err = 0;
35504e96b2dbSAllison Henderson 		if (ext4_should_journal_data(inode)) {
35514e96b2dbSAllison Henderson 			err = ext4_handle_dirty_metadata(handle, inode, bh);
3552decbd919STheodore Ts'o 		} else
35534e96b2dbSAllison Henderson 			mark_buffer_dirty(bh);
35544e96b2dbSAllison Henderson 
35554e96b2dbSAllison Henderson 		BUFFER_TRACE(bh, "Partial buffer zeroed");
35564e96b2dbSAllison Henderson next:
35574e96b2dbSAllison Henderson 		bh = bh->b_this_page;
35584e96b2dbSAllison Henderson 		iblock++;
35594e96b2dbSAllison Henderson 		pos += range_to_discard;
35604e96b2dbSAllison Henderson 	}
35614e96b2dbSAllison Henderson 
35624e96b2dbSAllison Henderson 	return err;
35634e96b2dbSAllison Henderson }
35644e96b2dbSAllison Henderson 
356591ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode)
356691ef4cafSDuane Griffin {
356791ef4cafSDuane Griffin 	if (S_ISREG(inode->i_mode))
356891ef4cafSDuane Griffin 		return 1;
356991ef4cafSDuane Griffin 	if (S_ISDIR(inode->i_mode))
357091ef4cafSDuane Griffin 		return 1;
357191ef4cafSDuane Griffin 	if (S_ISLNK(inode->i_mode))
357291ef4cafSDuane Griffin 		return !ext4_inode_is_fast_symlink(inode);
357391ef4cafSDuane Griffin 	return 0;
357491ef4cafSDuane Griffin }
357591ef4cafSDuane Griffin 
3576ac27a0ecSDave Kleikamp /*
3577a4bb6b64SAllison Henderson  * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3578a4bb6b64SAllison Henderson  * associated with the given offset and length
3579a4bb6b64SAllison Henderson  *
3580a4bb6b64SAllison Henderson  * @inode:  File inode
3581a4bb6b64SAllison Henderson  * @offset: The offset where the hole will begin
3582a4bb6b64SAllison Henderson  * @len:    The length of the hole
3583a4bb6b64SAllison Henderson  *
35844907cb7bSAnatol Pomozov  * Returns: 0 on success or negative on failure
3585a4bb6b64SAllison Henderson  */
3586a4bb6b64SAllison Henderson 
3587a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3588a4bb6b64SAllison Henderson {
3589496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
359026a4c0c6STheodore Ts'o 	struct super_block *sb = inode->i_sb;
359126a4c0c6STheodore Ts'o 	ext4_lblk_t first_block, stop_block;
359226a4c0c6STheodore Ts'o 	struct address_space *mapping = inode->i_mapping;
359326a4c0c6STheodore Ts'o 	loff_t first_page, last_page, page_len;
359426a4c0c6STheodore Ts'o 	loff_t first_page_offset, last_page_offset;
359526a4c0c6STheodore Ts'o 	handle_t *handle;
359626a4c0c6STheodore Ts'o 	unsigned int credits;
359726a4c0c6STheodore Ts'o 	int ret = 0;
359826a4c0c6STheodore Ts'o 
3599a4bb6b64SAllison Henderson 	if (!S_ISREG(inode->i_mode))
360073355192SAllison Henderson 		return -EOPNOTSUPP;
3601a4bb6b64SAllison Henderson 
360226a4c0c6STheodore Ts'o 	if (EXT4_SB(sb)->s_cluster_ratio > 1) {
3603bab08ab9STheodore Ts'o 		/* TODO: Add support for bigalloc file systems */
360473355192SAllison Henderson 		return -EOPNOTSUPP;
3605bab08ab9STheodore Ts'o 	}
3606bab08ab9STheodore Ts'o 
3607aaddea81SZheng Liu 	trace_ext4_punch_hole(inode, offset, length);
3608aaddea81SZheng Liu 
360926a4c0c6STheodore Ts'o 	/*
361026a4c0c6STheodore Ts'o 	 * Write out all dirty pages to avoid race conditions
361126a4c0c6STheodore Ts'o 	 * Then release them.
361226a4c0c6STheodore Ts'o 	 */
361326a4c0c6STheodore Ts'o 	if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
361426a4c0c6STheodore Ts'o 		ret = filemap_write_and_wait_range(mapping, offset,
361526a4c0c6STheodore Ts'o 						   offset + length - 1);
361626a4c0c6STheodore Ts'o 		if (ret)
361726a4c0c6STheodore Ts'o 			return ret;
361826a4c0c6STheodore Ts'o 	}
361926a4c0c6STheodore Ts'o 
362026a4c0c6STheodore Ts'o 	mutex_lock(&inode->i_mutex);
362126a4c0c6STheodore Ts'o 	/* It's not possible punch hole on append only file */
362226a4c0c6STheodore Ts'o 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
362326a4c0c6STheodore Ts'o 		ret = -EPERM;
362426a4c0c6STheodore Ts'o 		goto out_mutex;
362526a4c0c6STheodore Ts'o 	}
362626a4c0c6STheodore Ts'o 	if (IS_SWAPFILE(inode)) {
362726a4c0c6STheodore Ts'o 		ret = -ETXTBSY;
362826a4c0c6STheodore Ts'o 		goto out_mutex;
362926a4c0c6STheodore Ts'o 	}
363026a4c0c6STheodore Ts'o 
363126a4c0c6STheodore Ts'o 	/* No need to punch hole beyond i_size */
363226a4c0c6STheodore Ts'o 	if (offset >= inode->i_size)
363326a4c0c6STheodore Ts'o 		goto out_mutex;
363426a4c0c6STheodore Ts'o 
363526a4c0c6STheodore Ts'o 	/*
363626a4c0c6STheodore Ts'o 	 * If the hole extends beyond i_size, set the hole
363726a4c0c6STheodore Ts'o 	 * to end after the page that contains i_size
363826a4c0c6STheodore Ts'o 	 */
363926a4c0c6STheodore Ts'o 	if (offset + length > inode->i_size) {
364026a4c0c6STheodore Ts'o 		length = inode->i_size +
364126a4c0c6STheodore Ts'o 		   PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
364226a4c0c6STheodore Ts'o 		   offset;
364326a4c0c6STheodore Ts'o 	}
364426a4c0c6STheodore Ts'o 
364526a4c0c6STheodore Ts'o 	first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
364626a4c0c6STheodore Ts'o 	last_page = (offset + length) >> PAGE_CACHE_SHIFT;
364726a4c0c6STheodore Ts'o 
364826a4c0c6STheodore Ts'o 	first_page_offset = first_page << PAGE_CACHE_SHIFT;
364926a4c0c6STheodore Ts'o 	last_page_offset = last_page << PAGE_CACHE_SHIFT;
365026a4c0c6STheodore Ts'o 
365126a4c0c6STheodore Ts'o 	/* Now release the pages */
365226a4c0c6STheodore Ts'o 	if (last_page_offset > first_page_offset) {
365326a4c0c6STheodore Ts'o 		truncate_pagecache_range(inode, first_page_offset,
365426a4c0c6STheodore Ts'o 					 last_page_offset - 1);
365526a4c0c6STheodore Ts'o 	}
365626a4c0c6STheodore Ts'o 
365726a4c0c6STheodore Ts'o 	/* Wait all existing dio workers, newcomers will block on i_mutex */
365826a4c0c6STheodore Ts'o 	ext4_inode_block_unlocked_dio(inode);
365926a4c0c6STheodore Ts'o 	ret = ext4_flush_unwritten_io(inode);
366026a4c0c6STheodore Ts'o 	if (ret)
366126a4c0c6STheodore Ts'o 		goto out_dio;
366226a4c0c6STheodore Ts'o 	inode_dio_wait(inode);
366326a4c0c6STheodore Ts'o 
366426a4c0c6STheodore Ts'o 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
366526a4c0c6STheodore Ts'o 		credits = ext4_writepage_trans_blocks(inode);
366626a4c0c6STheodore Ts'o 	else
366726a4c0c6STheodore Ts'o 		credits = ext4_blocks_for_truncate(inode);
366826a4c0c6STheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
366926a4c0c6STheodore Ts'o 	if (IS_ERR(handle)) {
367026a4c0c6STheodore Ts'o 		ret = PTR_ERR(handle);
367126a4c0c6STheodore Ts'o 		ext4_std_error(sb, ret);
367226a4c0c6STheodore Ts'o 		goto out_dio;
367326a4c0c6STheodore Ts'o 	}
367426a4c0c6STheodore Ts'o 
367526a4c0c6STheodore Ts'o 	/*
367626a4c0c6STheodore Ts'o 	 * Now we need to zero out the non-page-aligned data in the
367726a4c0c6STheodore Ts'o 	 * pages at the start and tail of the hole, and unmap the
367826a4c0c6STheodore Ts'o 	 * buffer heads for the block aligned regions of the page that
367926a4c0c6STheodore Ts'o 	 * were completely zeroed.
368026a4c0c6STheodore Ts'o 	 */
368126a4c0c6STheodore Ts'o 	if (first_page > last_page) {
368226a4c0c6STheodore Ts'o 		/*
368326a4c0c6STheodore Ts'o 		 * If the file space being truncated is contained
368426a4c0c6STheodore Ts'o 		 * within a page just zero out and unmap the middle of
368526a4c0c6STheodore Ts'o 		 * that page
368626a4c0c6STheodore Ts'o 		 */
368726a4c0c6STheodore Ts'o 		ret = ext4_discard_partial_page_buffers(handle,
368826a4c0c6STheodore Ts'o 			mapping, offset, length, 0);
368926a4c0c6STheodore Ts'o 
369026a4c0c6STheodore Ts'o 		if (ret)
369126a4c0c6STheodore Ts'o 			goto out_stop;
369226a4c0c6STheodore Ts'o 	} else {
369326a4c0c6STheodore Ts'o 		/*
369426a4c0c6STheodore Ts'o 		 * zero out and unmap the partial page that contains
369526a4c0c6STheodore Ts'o 		 * the start of the hole
369626a4c0c6STheodore Ts'o 		 */
369726a4c0c6STheodore Ts'o 		page_len = first_page_offset - offset;
369826a4c0c6STheodore Ts'o 		if (page_len > 0) {
369926a4c0c6STheodore Ts'o 			ret = ext4_discard_partial_page_buffers(handle, mapping,
370026a4c0c6STheodore Ts'o 						offset, page_len, 0);
370126a4c0c6STheodore Ts'o 			if (ret)
370226a4c0c6STheodore Ts'o 				goto out_stop;
370326a4c0c6STheodore Ts'o 		}
370426a4c0c6STheodore Ts'o 
370526a4c0c6STheodore Ts'o 		/*
370626a4c0c6STheodore Ts'o 		 * zero out and unmap the partial page that contains
370726a4c0c6STheodore Ts'o 		 * the end of the hole
370826a4c0c6STheodore Ts'o 		 */
370926a4c0c6STheodore Ts'o 		page_len = offset + length - last_page_offset;
371026a4c0c6STheodore Ts'o 		if (page_len > 0) {
371126a4c0c6STheodore Ts'o 			ret = ext4_discard_partial_page_buffers(handle, mapping,
371226a4c0c6STheodore Ts'o 					last_page_offset, page_len, 0);
371326a4c0c6STheodore Ts'o 			if (ret)
371426a4c0c6STheodore Ts'o 				goto out_stop;
371526a4c0c6STheodore Ts'o 		}
371626a4c0c6STheodore Ts'o 	}
371726a4c0c6STheodore Ts'o 
371826a4c0c6STheodore Ts'o 	/*
371926a4c0c6STheodore Ts'o 	 * If i_size is contained in the last page, we need to
372026a4c0c6STheodore Ts'o 	 * unmap and zero the partial page after i_size
372126a4c0c6STheodore Ts'o 	 */
372226a4c0c6STheodore Ts'o 	if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
372326a4c0c6STheodore Ts'o 	   inode->i_size % PAGE_CACHE_SIZE != 0) {
372426a4c0c6STheodore Ts'o 		page_len = PAGE_CACHE_SIZE -
372526a4c0c6STheodore Ts'o 			(inode->i_size & (PAGE_CACHE_SIZE - 1));
372626a4c0c6STheodore Ts'o 
372726a4c0c6STheodore Ts'o 		if (page_len > 0) {
372826a4c0c6STheodore Ts'o 			ret = ext4_discard_partial_page_buffers(handle,
372926a4c0c6STheodore Ts'o 					mapping, inode->i_size, page_len, 0);
373026a4c0c6STheodore Ts'o 
373126a4c0c6STheodore Ts'o 			if (ret)
373226a4c0c6STheodore Ts'o 				goto out_stop;
373326a4c0c6STheodore Ts'o 		}
373426a4c0c6STheodore Ts'o 	}
373526a4c0c6STheodore Ts'o 
373626a4c0c6STheodore Ts'o 	first_block = (offset + sb->s_blocksize - 1) >>
373726a4c0c6STheodore Ts'o 		EXT4_BLOCK_SIZE_BITS(sb);
373826a4c0c6STheodore Ts'o 	stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
373926a4c0c6STheodore Ts'o 
374026a4c0c6STheodore Ts'o 	/* If there are no blocks to remove, return now */
374126a4c0c6STheodore Ts'o 	if (first_block >= stop_block)
374226a4c0c6STheodore Ts'o 		goto out_stop;
374326a4c0c6STheodore Ts'o 
374426a4c0c6STheodore Ts'o 	down_write(&EXT4_I(inode)->i_data_sem);
374526a4c0c6STheodore Ts'o 	ext4_discard_preallocations(inode);
374626a4c0c6STheodore Ts'o 
374726a4c0c6STheodore Ts'o 	ret = ext4_es_remove_extent(inode, first_block,
374826a4c0c6STheodore Ts'o 				    stop_block - first_block);
374926a4c0c6STheodore Ts'o 	if (ret) {
375026a4c0c6STheodore Ts'o 		up_write(&EXT4_I(inode)->i_data_sem);
375126a4c0c6STheodore Ts'o 		goto out_stop;
375226a4c0c6STheodore Ts'o 	}
375326a4c0c6STheodore Ts'o 
375426a4c0c6STheodore Ts'o 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
375526a4c0c6STheodore Ts'o 		ret = ext4_ext_remove_space(inode, first_block,
375626a4c0c6STheodore Ts'o 					    stop_block - 1);
375726a4c0c6STheodore Ts'o 	else
375826a4c0c6STheodore Ts'o 		ret = ext4_free_hole_blocks(handle, inode, first_block,
375926a4c0c6STheodore Ts'o 					    stop_block);
376026a4c0c6STheodore Ts'o 
376126a4c0c6STheodore Ts'o 	ext4_discard_preallocations(inode);
3762819c4920STheodore Ts'o 	up_write(&EXT4_I(inode)->i_data_sem);
376326a4c0c6STheodore Ts'o 	if (IS_SYNC(inode))
376426a4c0c6STheodore Ts'o 		ext4_handle_sync(handle);
376526a4c0c6STheodore Ts'o 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
376626a4c0c6STheodore Ts'o 	ext4_mark_inode_dirty(handle, inode);
376726a4c0c6STheodore Ts'o out_stop:
376826a4c0c6STheodore Ts'o 	ext4_journal_stop(handle);
376926a4c0c6STheodore Ts'o out_dio:
377026a4c0c6STheodore Ts'o 	ext4_inode_resume_unlocked_dio(inode);
377126a4c0c6STheodore Ts'o out_mutex:
377226a4c0c6STheodore Ts'o 	mutex_unlock(&inode->i_mutex);
377326a4c0c6STheodore Ts'o 	return ret;
3774a4bb6b64SAllison Henderson }
3775a4bb6b64SAllison Henderson 
3776a4bb6b64SAllison Henderson /*
3777617ba13bSMingming Cao  * ext4_truncate()
3778ac27a0ecSDave Kleikamp  *
3779617ba13bSMingming Cao  * We block out ext4_get_block() block instantiations across the entire
3780617ba13bSMingming Cao  * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3781ac27a0ecSDave Kleikamp  * simultaneously on behalf of the same inode.
3782ac27a0ecSDave Kleikamp  *
378342b2aa86SJustin P. Mattock  * As we work through the truncate and commit bits of it to the journal there
3784ac27a0ecSDave Kleikamp  * is one core, guiding principle: the file's tree must always be consistent on
3785ac27a0ecSDave Kleikamp  * disk.  We must be able to restart the truncate after a crash.
3786ac27a0ecSDave Kleikamp  *
3787ac27a0ecSDave Kleikamp  * The file's tree may be transiently inconsistent in memory (although it
3788ac27a0ecSDave Kleikamp  * probably isn't), but whenever we close off and commit a journal transaction,
3789ac27a0ecSDave Kleikamp  * the contents of (the filesystem + the journal) must be consistent and
3790ac27a0ecSDave Kleikamp  * restartable.  It's pretty simple, really: bottom up, right to left (although
3791ac27a0ecSDave Kleikamp  * left-to-right works OK too).
3792ac27a0ecSDave Kleikamp  *
3793ac27a0ecSDave Kleikamp  * Note that at recovery time, journal replay occurs *before* the restart of
3794ac27a0ecSDave Kleikamp  * truncate against the orphan inode list.
3795ac27a0ecSDave Kleikamp  *
3796ac27a0ecSDave Kleikamp  * The committed inode has the new, desired i_size (which is the same as
3797617ba13bSMingming Cao  * i_disksize in this case).  After a crash, ext4_orphan_cleanup() will see
3798ac27a0ecSDave Kleikamp  * that this inode's truncate did not complete and it will again call
3799617ba13bSMingming Cao  * ext4_truncate() to have another go.  So there will be instantiated blocks
3800617ba13bSMingming Cao  * to the right of the truncation point in a crashed ext4 filesystem.  But
3801ac27a0ecSDave Kleikamp  * that's fine - as long as they are linked from the inode, the post-crash
3802617ba13bSMingming Cao  * ext4_truncate() run will find them and release them.
3803ac27a0ecSDave Kleikamp  */
3804617ba13bSMingming Cao void ext4_truncate(struct inode *inode)
3805ac27a0ecSDave Kleikamp {
3806819c4920STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
3807819c4920STheodore Ts'o 	unsigned int credits;
3808819c4920STheodore Ts'o 	handle_t *handle;
3809819c4920STheodore Ts'o 	struct address_space *mapping = inode->i_mapping;
3810819c4920STheodore Ts'o 	loff_t page_len;
3811819c4920STheodore Ts'o 
381219b5ef61STheodore Ts'o 	/*
381319b5ef61STheodore Ts'o 	 * There is a possibility that we're either freeing the inode
381419b5ef61STheodore Ts'o 	 * or it completely new indode. In those cases we might not
381519b5ef61STheodore Ts'o 	 * have i_mutex locked because it's not necessary.
381619b5ef61STheodore Ts'o 	 */
381719b5ef61STheodore Ts'o 	if (!(inode->i_state & (I_NEW|I_FREEING)))
381819b5ef61STheodore Ts'o 		WARN_ON(!mutex_is_locked(&inode->i_mutex));
38190562e0baSJiaying Zhang 	trace_ext4_truncate_enter(inode);
38200562e0baSJiaying Zhang 
382191ef4cafSDuane Griffin 	if (!ext4_can_truncate(inode))
3822ac27a0ecSDave Kleikamp 		return;
3823ac27a0ecSDave Kleikamp 
382412e9b892SDmitry Monakhov 	ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3825c8d46e41SJiaying Zhang 
38265534fb5bSTheodore Ts'o 	if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
382719f5fb7aSTheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
38287d8f9f7dSTheodore Ts'o 
3829aef1c851STao Ma 	if (ext4_has_inline_data(inode)) {
3830aef1c851STao Ma 		int has_inline = 1;
3831aef1c851STao Ma 
3832aef1c851STao Ma 		ext4_inline_data_truncate(inode, &has_inline);
3833aef1c851STao Ma 		if (has_inline)
3834aef1c851STao Ma 			return;
3835aef1c851STao Ma 	}
3836aef1c851STao Ma 
3837819c4920STheodore Ts'o 	/*
3838819c4920STheodore Ts'o 	 * finish any pending end_io work so we won't run the risk of
3839819c4920STheodore Ts'o 	 * converting any truncated blocks to initialized later
3840819c4920STheodore Ts'o 	 */
3841819c4920STheodore Ts'o 	ext4_flush_unwritten_io(inode);
3842819c4920STheodore Ts'o 
3843ff9893dcSAmir Goldstein 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3844819c4920STheodore Ts'o 		credits = ext4_writepage_trans_blocks(inode);
3845ff9893dcSAmir Goldstein 	else
3846819c4920STheodore Ts'o 		credits = ext4_blocks_for_truncate(inode);
3847819c4920STheodore Ts'o 
3848819c4920STheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3849819c4920STheodore Ts'o 	if (IS_ERR(handle)) {
3850819c4920STheodore Ts'o 		ext4_std_error(inode->i_sb, PTR_ERR(handle));
3851819c4920STheodore Ts'o 		return;
3852819c4920STheodore Ts'o 	}
3853819c4920STheodore Ts'o 
3854819c4920STheodore Ts'o 	if (inode->i_size % PAGE_CACHE_SIZE != 0) {
3855819c4920STheodore Ts'o 		page_len = PAGE_CACHE_SIZE -
3856819c4920STheodore Ts'o 			(inode->i_size & (PAGE_CACHE_SIZE - 1));
3857819c4920STheodore Ts'o 
3858819c4920STheodore Ts'o 		if (ext4_discard_partial_page_buffers(handle,
3859819c4920STheodore Ts'o 				mapping, inode->i_size, page_len, 0))
3860819c4920STheodore Ts'o 			goto out_stop;
3861819c4920STheodore Ts'o 	}
3862819c4920STheodore Ts'o 
3863819c4920STheodore Ts'o 	/*
3864819c4920STheodore Ts'o 	 * We add the inode to the orphan list, so that if this
3865819c4920STheodore Ts'o 	 * truncate spans multiple transactions, and we crash, we will
3866819c4920STheodore Ts'o 	 * resume the truncate when the filesystem recovers.  It also
3867819c4920STheodore Ts'o 	 * marks the inode dirty, to catch the new size.
3868819c4920STheodore Ts'o 	 *
3869819c4920STheodore Ts'o 	 * Implication: the file must always be in a sane, consistent
3870819c4920STheodore Ts'o 	 * truncatable state while each transaction commits.
3871819c4920STheodore Ts'o 	 */
3872819c4920STheodore Ts'o 	if (ext4_orphan_add(handle, inode))
3873819c4920STheodore Ts'o 		goto out_stop;
3874819c4920STheodore Ts'o 
3875819c4920STheodore Ts'o 	down_write(&EXT4_I(inode)->i_data_sem);
3876819c4920STheodore Ts'o 
3877819c4920STheodore Ts'o 	ext4_discard_preallocations(inode);
3878819c4920STheodore Ts'o 
3879819c4920STheodore Ts'o 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3880819c4920STheodore Ts'o 		ext4_ext_truncate(handle, inode);
3881819c4920STheodore Ts'o 	else
3882819c4920STheodore Ts'o 		ext4_ind_truncate(handle, inode);
3883819c4920STheodore Ts'o 
3884819c4920STheodore Ts'o 	up_write(&ei->i_data_sem);
3885819c4920STheodore Ts'o 
3886819c4920STheodore Ts'o 	if (IS_SYNC(inode))
3887819c4920STheodore Ts'o 		ext4_handle_sync(handle);
3888819c4920STheodore Ts'o 
3889819c4920STheodore Ts'o out_stop:
3890819c4920STheodore Ts'o 	/*
3891819c4920STheodore Ts'o 	 * If this was a simple ftruncate() and the file will remain alive,
3892819c4920STheodore Ts'o 	 * then we need to clear up the orphan record which we created above.
3893819c4920STheodore Ts'o 	 * However, if this was a real unlink then we were called by
3894819c4920STheodore Ts'o 	 * ext4_delete_inode(), and we allow that function to clean up the
3895819c4920STheodore Ts'o 	 * orphan info for us.
3896819c4920STheodore Ts'o 	 */
3897819c4920STheodore Ts'o 	if (inode->i_nlink)
3898819c4920STheodore Ts'o 		ext4_orphan_del(handle, inode);
3899819c4920STheodore Ts'o 
3900819c4920STheodore Ts'o 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3901819c4920STheodore Ts'o 	ext4_mark_inode_dirty(handle, inode);
3902819c4920STheodore Ts'o 	ext4_journal_stop(handle);
3903a86c6181SAlex Tomas 
39040562e0baSJiaying Zhang 	trace_ext4_truncate_exit(inode);
3905ac27a0ecSDave Kleikamp }
3906ac27a0ecSDave Kleikamp 
3907ac27a0ecSDave Kleikamp /*
3908617ba13bSMingming Cao  * ext4_get_inode_loc returns with an extra refcount against the inode's
3909ac27a0ecSDave Kleikamp  * underlying buffer_head on success. If 'in_mem' is true, we have all
3910ac27a0ecSDave Kleikamp  * data in memory that is needed to recreate the on-disk version of this
3911ac27a0ecSDave Kleikamp  * inode.
3912ac27a0ecSDave Kleikamp  */
3913617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode,
3914617ba13bSMingming Cao 				struct ext4_iloc *iloc, int in_mem)
3915ac27a0ecSDave Kleikamp {
3916240799cdSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3917ac27a0ecSDave Kleikamp 	struct buffer_head	*bh;
3918240799cdSTheodore Ts'o 	struct super_block	*sb = inode->i_sb;
3919240799cdSTheodore Ts'o 	ext4_fsblk_t		block;
3920240799cdSTheodore Ts'o 	int			inodes_per_block, inode_offset;
3921ac27a0ecSDave Kleikamp 
39223a06d778SAneesh Kumar K.V 	iloc->bh = NULL;
3923240799cdSTheodore Ts'o 	if (!ext4_valid_inum(sb, inode->i_ino))
3924ac27a0ecSDave Kleikamp 		return -EIO;
3925ac27a0ecSDave Kleikamp 
3926240799cdSTheodore Ts'o 	iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3927240799cdSTheodore Ts'o 	gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3928240799cdSTheodore Ts'o 	if (!gdp)
3929240799cdSTheodore Ts'o 		return -EIO;
3930240799cdSTheodore Ts'o 
3931240799cdSTheodore Ts'o 	/*
3932240799cdSTheodore Ts'o 	 * Figure out the offset within the block group inode table
3933240799cdSTheodore Ts'o 	 */
393400d09882STao Ma 	inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
3935240799cdSTheodore Ts'o 	inode_offset = ((inode->i_ino - 1) %
3936240799cdSTheodore Ts'o 			EXT4_INODES_PER_GROUP(sb));
3937240799cdSTheodore Ts'o 	block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3938240799cdSTheodore Ts'o 	iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3939240799cdSTheodore Ts'o 
3940240799cdSTheodore Ts'o 	bh = sb_getblk(sb, block);
3941aebf0243SWang Shilong 	if (unlikely(!bh))
3942860d21e2STheodore Ts'o 		return -ENOMEM;
3943ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(bh)) {
3944ac27a0ecSDave Kleikamp 		lock_buffer(bh);
39459c83a923SHidehiro Kawai 
39469c83a923SHidehiro Kawai 		/*
39479c83a923SHidehiro Kawai 		 * If the buffer has the write error flag, we have failed
39489c83a923SHidehiro Kawai 		 * to write out another inode in the same block.  In this
39499c83a923SHidehiro Kawai 		 * case, we don't have to read the block because we may
39509c83a923SHidehiro Kawai 		 * read the old inode data successfully.
39519c83a923SHidehiro Kawai 		 */
39529c83a923SHidehiro Kawai 		if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
39539c83a923SHidehiro Kawai 			set_buffer_uptodate(bh);
39549c83a923SHidehiro Kawai 
3955ac27a0ecSDave Kleikamp 		if (buffer_uptodate(bh)) {
3956ac27a0ecSDave Kleikamp 			/* someone brought it uptodate while we waited */
3957ac27a0ecSDave Kleikamp 			unlock_buffer(bh);
3958ac27a0ecSDave Kleikamp 			goto has_buffer;
3959ac27a0ecSDave Kleikamp 		}
3960ac27a0ecSDave Kleikamp 
3961ac27a0ecSDave Kleikamp 		/*
3962ac27a0ecSDave Kleikamp 		 * If we have all information of the inode in memory and this
3963ac27a0ecSDave Kleikamp 		 * is the only valid inode in the block, we need not read the
3964ac27a0ecSDave Kleikamp 		 * block.
3965ac27a0ecSDave Kleikamp 		 */
3966ac27a0ecSDave Kleikamp 		if (in_mem) {
3967ac27a0ecSDave Kleikamp 			struct buffer_head *bitmap_bh;
3968240799cdSTheodore Ts'o 			int i, start;
3969ac27a0ecSDave Kleikamp 
3970240799cdSTheodore Ts'o 			start = inode_offset & ~(inodes_per_block - 1);
3971ac27a0ecSDave Kleikamp 
3972ac27a0ecSDave Kleikamp 			/* Is the inode bitmap in cache? */
3973240799cdSTheodore Ts'o 			bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
3974aebf0243SWang Shilong 			if (unlikely(!bitmap_bh))
3975ac27a0ecSDave Kleikamp 				goto make_io;
3976ac27a0ecSDave Kleikamp 
3977ac27a0ecSDave Kleikamp 			/*
3978ac27a0ecSDave Kleikamp 			 * If the inode bitmap isn't in cache then the
3979ac27a0ecSDave Kleikamp 			 * optimisation may end up performing two reads instead
3980ac27a0ecSDave Kleikamp 			 * of one, so skip it.
3981ac27a0ecSDave Kleikamp 			 */
3982ac27a0ecSDave Kleikamp 			if (!buffer_uptodate(bitmap_bh)) {
3983ac27a0ecSDave Kleikamp 				brelse(bitmap_bh);
3984ac27a0ecSDave Kleikamp 				goto make_io;
3985ac27a0ecSDave Kleikamp 			}
3986240799cdSTheodore Ts'o 			for (i = start; i < start + inodes_per_block; i++) {
3987ac27a0ecSDave Kleikamp 				if (i == inode_offset)
3988ac27a0ecSDave Kleikamp 					continue;
3989617ba13bSMingming Cao 				if (ext4_test_bit(i, bitmap_bh->b_data))
3990ac27a0ecSDave Kleikamp 					break;
3991ac27a0ecSDave Kleikamp 			}
3992ac27a0ecSDave Kleikamp 			brelse(bitmap_bh);
3993240799cdSTheodore Ts'o 			if (i == start + inodes_per_block) {
3994ac27a0ecSDave Kleikamp 				/* all other inodes are free, so skip I/O */
3995ac27a0ecSDave Kleikamp 				memset(bh->b_data, 0, bh->b_size);
3996ac27a0ecSDave Kleikamp 				set_buffer_uptodate(bh);
3997ac27a0ecSDave Kleikamp 				unlock_buffer(bh);
3998ac27a0ecSDave Kleikamp 				goto has_buffer;
3999ac27a0ecSDave Kleikamp 			}
4000ac27a0ecSDave Kleikamp 		}
4001ac27a0ecSDave Kleikamp 
4002ac27a0ecSDave Kleikamp make_io:
4003ac27a0ecSDave Kleikamp 		/*
4004240799cdSTheodore Ts'o 		 * If we need to do any I/O, try to pre-readahead extra
4005240799cdSTheodore Ts'o 		 * blocks from the inode table.
4006240799cdSTheodore Ts'o 		 */
4007240799cdSTheodore Ts'o 		if (EXT4_SB(sb)->s_inode_readahead_blks) {
4008240799cdSTheodore Ts'o 			ext4_fsblk_t b, end, table;
4009240799cdSTheodore Ts'o 			unsigned num;
4010240799cdSTheodore Ts'o 
4011240799cdSTheodore Ts'o 			table = ext4_inode_table(sb, gdp);
4012b713a5ecSTheodore Ts'o 			/* s_inode_readahead_blks is always a power of 2 */
4013240799cdSTheodore Ts'o 			b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4014240799cdSTheodore Ts'o 			if (table > b)
4015240799cdSTheodore Ts'o 				b = table;
4016240799cdSTheodore Ts'o 			end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4017240799cdSTheodore Ts'o 			num = EXT4_INODES_PER_GROUP(sb);
4018feb0ab32SDarrick J. Wong 			if (ext4_has_group_desc_csum(sb))
4019560671a0SAneesh Kumar K.V 				num -= ext4_itable_unused_count(sb, gdp);
4020240799cdSTheodore Ts'o 			table += num / inodes_per_block;
4021240799cdSTheodore Ts'o 			if (end > table)
4022240799cdSTheodore Ts'o 				end = table;
4023240799cdSTheodore Ts'o 			while (b <= end)
4024240799cdSTheodore Ts'o 				sb_breadahead(sb, b++);
4025240799cdSTheodore Ts'o 		}
4026240799cdSTheodore Ts'o 
4027240799cdSTheodore Ts'o 		/*
4028ac27a0ecSDave Kleikamp 		 * There are other valid inodes in the buffer, this inode
4029ac27a0ecSDave Kleikamp 		 * has in-inode xattrs, or we don't have this inode in memory.
4030ac27a0ecSDave Kleikamp 		 * Read the block from disk.
4031ac27a0ecSDave Kleikamp 		 */
40320562e0baSJiaying Zhang 		trace_ext4_load_inode(inode);
4033ac27a0ecSDave Kleikamp 		get_bh(bh);
4034ac27a0ecSDave Kleikamp 		bh->b_end_io = end_buffer_read_sync;
403565299a3bSChristoph Hellwig 		submit_bh(READ | REQ_META | REQ_PRIO, bh);
4036ac27a0ecSDave Kleikamp 		wait_on_buffer(bh);
4037ac27a0ecSDave Kleikamp 		if (!buffer_uptodate(bh)) {
4038c398eda0STheodore Ts'o 			EXT4_ERROR_INODE_BLOCK(inode, block,
4039c398eda0STheodore Ts'o 					       "unable to read itable block");
4040ac27a0ecSDave Kleikamp 			brelse(bh);
4041ac27a0ecSDave Kleikamp 			return -EIO;
4042ac27a0ecSDave Kleikamp 		}
4043ac27a0ecSDave Kleikamp 	}
4044ac27a0ecSDave Kleikamp has_buffer:
4045ac27a0ecSDave Kleikamp 	iloc->bh = bh;
4046ac27a0ecSDave Kleikamp 	return 0;
4047ac27a0ecSDave Kleikamp }
4048ac27a0ecSDave Kleikamp 
4049617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
4050ac27a0ecSDave Kleikamp {
4051ac27a0ecSDave Kleikamp 	/* We have all inode data except xattrs in memory here. */
4052617ba13bSMingming Cao 	return __ext4_get_inode_loc(inode, iloc,
405319f5fb7aSTheodore Ts'o 		!ext4_test_inode_state(inode, EXT4_STATE_XATTR));
4054ac27a0ecSDave Kleikamp }
4055ac27a0ecSDave Kleikamp 
4056617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode)
4057ac27a0ecSDave Kleikamp {
4058617ba13bSMingming Cao 	unsigned int flags = EXT4_I(inode)->i_flags;
4059ac27a0ecSDave Kleikamp 
4060ac27a0ecSDave Kleikamp 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
4061617ba13bSMingming Cao 	if (flags & EXT4_SYNC_FL)
4062ac27a0ecSDave Kleikamp 		inode->i_flags |= S_SYNC;
4063617ba13bSMingming Cao 	if (flags & EXT4_APPEND_FL)
4064ac27a0ecSDave Kleikamp 		inode->i_flags |= S_APPEND;
4065617ba13bSMingming Cao 	if (flags & EXT4_IMMUTABLE_FL)
4066ac27a0ecSDave Kleikamp 		inode->i_flags |= S_IMMUTABLE;
4067617ba13bSMingming Cao 	if (flags & EXT4_NOATIME_FL)
4068ac27a0ecSDave Kleikamp 		inode->i_flags |= S_NOATIME;
4069617ba13bSMingming Cao 	if (flags & EXT4_DIRSYNC_FL)
4070ac27a0ecSDave Kleikamp 		inode->i_flags |= S_DIRSYNC;
4071ac27a0ecSDave Kleikamp }
4072ac27a0ecSDave Kleikamp 
4073ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4074ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei)
4075ff9ddf7eSJan Kara {
407684a8dce2SDmitry Monakhov 	unsigned int vfs_fl;
407784a8dce2SDmitry Monakhov 	unsigned long old_fl, new_fl;
4078ff9ddf7eSJan Kara 
407984a8dce2SDmitry Monakhov 	do {
408084a8dce2SDmitry Monakhov 		vfs_fl = ei->vfs_inode.i_flags;
408184a8dce2SDmitry Monakhov 		old_fl = ei->i_flags;
408284a8dce2SDmitry Monakhov 		new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
408384a8dce2SDmitry Monakhov 				EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
408484a8dce2SDmitry Monakhov 				EXT4_DIRSYNC_FL);
408584a8dce2SDmitry Monakhov 		if (vfs_fl & S_SYNC)
408684a8dce2SDmitry Monakhov 			new_fl |= EXT4_SYNC_FL;
408784a8dce2SDmitry Monakhov 		if (vfs_fl & S_APPEND)
408884a8dce2SDmitry Monakhov 			new_fl |= EXT4_APPEND_FL;
408984a8dce2SDmitry Monakhov 		if (vfs_fl & S_IMMUTABLE)
409084a8dce2SDmitry Monakhov 			new_fl |= EXT4_IMMUTABLE_FL;
409184a8dce2SDmitry Monakhov 		if (vfs_fl & S_NOATIME)
409284a8dce2SDmitry Monakhov 			new_fl |= EXT4_NOATIME_FL;
409384a8dce2SDmitry Monakhov 		if (vfs_fl & S_DIRSYNC)
409484a8dce2SDmitry Monakhov 			new_fl |= EXT4_DIRSYNC_FL;
409584a8dce2SDmitry Monakhov 	} while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
4096ff9ddf7eSJan Kara }
4097de9a55b8STheodore Ts'o 
40980fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
40990fc1b451SAneesh Kumar K.V 				  struct ext4_inode_info *ei)
41000fc1b451SAneesh Kumar K.V {
41010fc1b451SAneesh Kumar K.V 	blkcnt_t i_blocks ;
41028180a562SAneesh Kumar K.V 	struct inode *inode = &(ei->vfs_inode);
41038180a562SAneesh Kumar K.V 	struct super_block *sb = inode->i_sb;
41040fc1b451SAneesh Kumar K.V 
41050fc1b451SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
41060fc1b451SAneesh Kumar K.V 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
41070fc1b451SAneesh Kumar K.V 		/* we are using combined 48 bit field */
41080fc1b451SAneesh Kumar K.V 		i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
41090fc1b451SAneesh Kumar K.V 					le32_to_cpu(raw_inode->i_blocks_lo);
411007a03824STheodore Ts'o 		if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
41118180a562SAneesh Kumar K.V 			/* i_blocks represent file system block size */
41128180a562SAneesh Kumar K.V 			return i_blocks  << (inode->i_blkbits - 9);
41138180a562SAneesh Kumar K.V 		} else {
41140fc1b451SAneesh Kumar K.V 			return i_blocks;
41158180a562SAneesh Kumar K.V 		}
41160fc1b451SAneesh Kumar K.V 	} else {
41170fc1b451SAneesh Kumar K.V 		return le32_to_cpu(raw_inode->i_blocks_lo);
41180fc1b451SAneesh Kumar K.V 	}
41190fc1b451SAneesh Kumar K.V }
4120ff9ddf7eSJan Kara 
4121152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode,
4122152a7b0aSTao Ma 					 struct ext4_inode *raw_inode,
4123152a7b0aSTao Ma 					 struct ext4_inode_info *ei)
4124152a7b0aSTao Ma {
4125152a7b0aSTao Ma 	__le32 *magic = (void *)raw_inode +
4126152a7b0aSTao Ma 			EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
412767cf5b09STao Ma 	if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
4128152a7b0aSTao Ma 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
412967cf5b09STao Ma 		ext4_find_inline_data_nolock(inode);
4130f19d5870STao Ma 	} else
4131f19d5870STao Ma 		EXT4_I(inode)->i_inline_off = 0;
4132152a7b0aSTao Ma }
4133152a7b0aSTao Ma 
41341d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
4135ac27a0ecSDave Kleikamp {
4136617ba13bSMingming Cao 	struct ext4_iloc iloc;
4137617ba13bSMingming Cao 	struct ext4_inode *raw_inode;
41381d1fe1eeSDavid Howells 	struct ext4_inode_info *ei;
41391d1fe1eeSDavid Howells 	struct inode *inode;
4140b436b9beSJan Kara 	journal_t *journal = EXT4_SB(sb)->s_journal;
41411d1fe1eeSDavid Howells 	long ret;
4142ac27a0ecSDave Kleikamp 	int block;
414308cefc7aSEric W. Biederman 	uid_t i_uid;
414408cefc7aSEric W. Biederman 	gid_t i_gid;
4145ac27a0ecSDave Kleikamp 
41461d1fe1eeSDavid Howells 	inode = iget_locked(sb, ino);
41471d1fe1eeSDavid Howells 	if (!inode)
41481d1fe1eeSDavid Howells 		return ERR_PTR(-ENOMEM);
41491d1fe1eeSDavid Howells 	if (!(inode->i_state & I_NEW))
41501d1fe1eeSDavid Howells 		return inode;
41511d1fe1eeSDavid Howells 
41521d1fe1eeSDavid Howells 	ei = EXT4_I(inode);
41537dc57615SPeter Huewe 	iloc.bh = NULL;
4154ac27a0ecSDave Kleikamp 
41551d1fe1eeSDavid Howells 	ret = __ext4_get_inode_loc(inode, &iloc, 0);
41561d1fe1eeSDavid Howells 	if (ret < 0)
4157ac27a0ecSDave Kleikamp 		goto bad_inode;
4158617ba13bSMingming Cao 	raw_inode = ext4_raw_inode(&iloc);
4159814525f4SDarrick J. Wong 
4160814525f4SDarrick J. Wong 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4161814525f4SDarrick J. Wong 		ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4162814525f4SDarrick J. Wong 		if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4163814525f4SDarrick J. Wong 		    EXT4_INODE_SIZE(inode->i_sb)) {
4164814525f4SDarrick J. Wong 			EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4165814525f4SDarrick J. Wong 				EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4166814525f4SDarrick J. Wong 				EXT4_INODE_SIZE(inode->i_sb));
4167814525f4SDarrick J. Wong 			ret = -EIO;
4168814525f4SDarrick J. Wong 			goto bad_inode;
4169814525f4SDarrick J. Wong 		}
4170814525f4SDarrick J. Wong 	} else
4171814525f4SDarrick J. Wong 		ei->i_extra_isize = 0;
4172814525f4SDarrick J. Wong 
4173814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
4174814525f4SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4175814525f4SDarrick J. Wong 			EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
4176814525f4SDarrick J. Wong 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4177814525f4SDarrick J. Wong 		__u32 csum;
4178814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
4179814525f4SDarrick J. Wong 		__le32 gen = raw_inode->i_generation;
4180814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4181814525f4SDarrick J. Wong 				   sizeof(inum));
4182814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4183814525f4SDarrick J. Wong 					      sizeof(gen));
4184814525f4SDarrick J. Wong 	}
4185814525f4SDarrick J. Wong 
4186814525f4SDarrick J. Wong 	if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4187814525f4SDarrick J. Wong 		EXT4_ERROR_INODE(inode, "checksum invalid");
4188814525f4SDarrick J. Wong 		ret = -EIO;
4189814525f4SDarrick J. Wong 		goto bad_inode;
4190814525f4SDarrick J. Wong 	}
4191814525f4SDarrick J. Wong 
4192ac27a0ecSDave Kleikamp 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
419308cefc7aSEric W. Biederman 	i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
419408cefc7aSEric W. Biederman 	i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
4195ac27a0ecSDave Kleikamp 	if (!(test_opt(inode->i_sb, NO_UID32))) {
419608cefc7aSEric W. Biederman 		i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
419708cefc7aSEric W. Biederman 		i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4198ac27a0ecSDave Kleikamp 	}
419908cefc7aSEric W. Biederman 	i_uid_write(inode, i_uid);
420008cefc7aSEric W. Biederman 	i_gid_write(inode, i_gid);
4201bfe86848SMiklos Szeredi 	set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
4202ac27a0ecSDave Kleikamp 
4203353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei);	/* Only relevant on 32-bit archs */
420467cf5b09STao Ma 	ei->i_inline_off = 0;
4205ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
4206ac27a0ecSDave Kleikamp 	ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4207ac27a0ecSDave Kleikamp 	/* We now have enough fields to check if the inode was active or not.
4208ac27a0ecSDave Kleikamp 	 * This is needed because nfsd might try to access dead inodes
4209ac27a0ecSDave Kleikamp 	 * the test is that same one that e2fsck uses
4210ac27a0ecSDave Kleikamp 	 * NeilBrown 1999oct15
4211ac27a0ecSDave Kleikamp 	 */
4212ac27a0ecSDave Kleikamp 	if (inode->i_nlink == 0) {
4213393d1d1dSDr. Tilmann Bubeck 		if ((inode->i_mode == 0 ||
4214393d1d1dSDr. Tilmann Bubeck 		     !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4215393d1d1dSDr. Tilmann Bubeck 		    ino != EXT4_BOOT_LOADER_INO) {
4216ac27a0ecSDave Kleikamp 			/* this inode is deleted */
42171d1fe1eeSDavid Howells 			ret = -ESTALE;
4218ac27a0ecSDave Kleikamp 			goto bad_inode;
4219ac27a0ecSDave Kleikamp 		}
4220ac27a0ecSDave Kleikamp 		/* The only unlinked inodes we let through here have
4221ac27a0ecSDave Kleikamp 		 * valid i_mode and are being read by the orphan
4222ac27a0ecSDave Kleikamp 		 * recovery code: that's fine, we're about to complete
4223393d1d1dSDr. Tilmann Bubeck 		 * the process of deleting those.
4224393d1d1dSDr. Tilmann Bubeck 		 * OR it is the EXT4_BOOT_LOADER_INO which is
4225393d1d1dSDr. Tilmann Bubeck 		 * not initialized on a new filesystem. */
4226ac27a0ecSDave Kleikamp 	}
4227ac27a0ecSDave Kleikamp 	ei->i_flags = le32_to_cpu(raw_inode->i_flags);
42280fc1b451SAneesh Kumar K.V 	inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
42297973c0c1SAneesh Kumar K.V 	ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
4230a9e81742STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
4231a1ddeb7eSBadari Pulavarty 		ei->i_file_acl |=
4232a1ddeb7eSBadari Pulavarty 			((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
4233a48380f7SAneesh Kumar K.V 	inode->i_size = ext4_isize(raw_inode);
4234ac27a0ecSDave Kleikamp 	ei->i_disksize = inode->i_size;
4235a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
4236a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
4237a9e7f447SDmitry Monakhov #endif
4238ac27a0ecSDave Kleikamp 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4239ac27a0ecSDave Kleikamp 	ei->i_block_group = iloc.block_group;
4240a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
4241ac27a0ecSDave Kleikamp 	/*
4242ac27a0ecSDave Kleikamp 	 * NOTE! The in-memory inode i_data array is in little-endian order
4243ac27a0ecSDave Kleikamp 	 * even on big-endian machines: we do NOT byteswap the block numbers!
4244ac27a0ecSDave Kleikamp 	 */
4245617ba13bSMingming Cao 	for (block = 0; block < EXT4_N_BLOCKS; block++)
4246ac27a0ecSDave Kleikamp 		ei->i_data[block] = raw_inode->i_block[block];
4247ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
4248ac27a0ecSDave Kleikamp 
4249b436b9beSJan Kara 	/*
4250b436b9beSJan Kara 	 * Set transaction id's of transactions that have to be committed
4251b436b9beSJan Kara 	 * to finish f[data]sync. We set them to currently running transaction
4252b436b9beSJan Kara 	 * as we cannot be sure that the inode or some of its metadata isn't
4253b436b9beSJan Kara 	 * part of the transaction - the inode could have been reclaimed and
4254b436b9beSJan Kara 	 * now it is reread from disk.
4255b436b9beSJan Kara 	 */
4256b436b9beSJan Kara 	if (journal) {
4257b436b9beSJan Kara 		transaction_t *transaction;
4258b436b9beSJan Kara 		tid_t tid;
4259b436b9beSJan Kara 
4260a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
4261b436b9beSJan Kara 		if (journal->j_running_transaction)
4262b436b9beSJan Kara 			transaction = journal->j_running_transaction;
4263b436b9beSJan Kara 		else
4264b436b9beSJan Kara 			transaction = journal->j_committing_transaction;
4265b436b9beSJan Kara 		if (transaction)
4266b436b9beSJan Kara 			tid = transaction->t_tid;
4267b436b9beSJan Kara 		else
4268b436b9beSJan Kara 			tid = journal->j_commit_sequence;
4269a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
4270b436b9beSJan Kara 		ei->i_sync_tid = tid;
4271b436b9beSJan Kara 		ei->i_datasync_tid = tid;
4272b436b9beSJan Kara 	}
4273b436b9beSJan Kara 
42740040d987SEric Sandeen 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4275ac27a0ecSDave Kleikamp 		if (ei->i_extra_isize == 0) {
4276ac27a0ecSDave Kleikamp 			/* The extra space is currently unused. Use it. */
4277617ba13bSMingming Cao 			ei->i_extra_isize = sizeof(struct ext4_inode) -
4278617ba13bSMingming Cao 					    EXT4_GOOD_OLD_INODE_SIZE;
4279ac27a0ecSDave Kleikamp 		} else {
4280152a7b0aSTao Ma 			ext4_iget_extra_inode(inode, raw_inode, ei);
4281ac27a0ecSDave Kleikamp 		}
4282814525f4SDarrick J. Wong 	}
4283ac27a0ecSDave Kleikamp 
4284ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4285ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4286ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4287ef7f3835SKalpak Shah 	EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4288ef7f3835SKalpak Shah 
428925ec56b5SJean Noel Cordenner 	inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
429025ec56b5SJean Noel Cordenner 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
429125ec56b5SJean Noel Cordenner 		if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
429225ec56b5SJean Noel Cordenner 			inode->i_version |=
429325ec56b5SJean Noel Cordenner 			(__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
429425ec56b5SJean Noel Cordenner 	}
429525ec56b5SJean Noel Cordenner 
4296c4b5a614STheodore Ts'o 	ret = 0;
4297485c26ecSTheodore Ts'o 	if (ei->i_file_acl &&
42981032988cSTheodore Ts'o 	    !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
429924676da4STheodore Ts'o 		EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
430024676da4STheodore Ts'o 				 ei->i_file_acl);
4301485c26ecSTheodore Ts'o 		ret = -EIO;
4302485c26ecSTheodore Ts'o 		goto bad_inode;
4303f19d5870STao Ma 	} else if (!ext4_has_inline_data(inode)) {
4304f19d5870STao Ma 		if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4305f19d5870STao Ma 			if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4306c4b5a614STheodore Ts'o 			    (S_ISLNK(inode->i_mode) &&
4307f19d5870STao Ma 			     !ext4_inode_is_fast_symlink(inode))))
43087a262f7cSAneesh Kumar K.V 				/* Validate extent which is part of inode */
43097a262f7cSAneesh Kumar K.V 				ret = ext4_ext_check_inode(inode);
4310fe2c8191SThiemo Nagel 		} else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4311fe2c8191SThiemo Nagel 			   (S_ISLNK(inode->i_mode) &&
4312fe2c8191SThiemo Nagel 			    !ext4_inode_is_fast_symlink(inode))) {
4313fe2c8191SThiemo Nagel 			/* Validate block references which are part of inode */
43141f7d1e77STheodore Ts'o 			ret = ext4_ind_check_inode(inode);
4315fe2c8191SThiemo Nagel 		}
4316f19d5870STao Ma 	}
4317567f3e9aSTheodore Ts'o 	if (ret)
43187a262f7cSAneesh Kumar K.V 		goto bad_inode;
43197a262f7cSAneesh Kumar K.V 
4320ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode)) {
4321617ba13bSMingming Cao 		inode->i_op = &ext4_file_inode_operations;
4322617ba13bSMingming Cao 		inode->i_fop = &ext4_file_operations;
4323617ba13bSMingming Cao 		ext4_set_aops(inode);
4324ac27a0ecSDave Kleikamp 	} else if (S_ISDIR(inode->i_mode)) {
4325617ba13bSMingming Cao 		inode->i_op = &ext4_dir_inode_operations;
4326617ba13bSMingming Cao 		inode->i_fop = &ext4_dir_operations;
4327ac27a0ecSDave Kleikamp 	} else if (S_ISLNK(inode->i_mode)) {
4328e83c1397SDuane Griffin 		if (ext4_inode_is_fast_symlink(inode)) {
4329617ba13bSMingming Cao 			inode->i_op = &ext4_fast_symlink_inode_operations;
4330e83c1397SDuane Griffin 			nd_terminate_link(ei->i_data, inode->i_size,
4331e83c1397SDuane Griffin 				sizeof(ei->i_data) - 1);
4332e83c1397SDuane Griffin 		} else {
4333617ba13bSMingming Cao 			inode->i_op = &ext4_symlink_inode_operations;
4334617ba13bSMingming Cao 			ext4_set_aops(inode);
4335ac27a0ecSDave Kleikamp 		}
4336563bdd61STheodore Ts'o 	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4337563bdd61STheodore Ts'o 	      S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
4338617ba13bSMingming Cao 		inode->i_op = &ext4_special_inode_operations;
4339ac27a0ecSDave Kleikamp 		if (raw_inode->i_block[0])
4340ac27a0ecSDave Kleikamp 			init_special_inode(inode, inode->i_mode,
4341ac27a0ecSDave Kleikamp 			   old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4342ac27a0ecSDave Kleikamp 		else
4343ac27a0ecSDave Kleikamp 			init_special_inode(inode, inode->i_mode,
4344ac27a0ecSDave Kleikamp 			   new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4345393d1d1dSDr. Tilmann Bubeck 	} else if (ino == EXT4_BOOT_LOADER_INO) {
4346393d1d1dSDr. Tilmann Bubeck 		make_bad_inode(inode);
4347563bdd61STheodore Ts'o 	} else {
4348563bdd61STheodore Ts'o 		ret = -EIO;
434924676da4STheodore Ts'o 		EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
4350563bdd61STheodore Ts'o 		goto bad_inode;
4351ac27a0ecSDave Kleikamp 	}
4352ac27a0ecSDave Kleikamp 	brelse(iloc.bh);
4353617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
43541d1fe1eeSDavid Howells 	unlock_new_inode(inode);
43551d1fe1eeSDavid Howells 	return inode;
4356ac27a0ecSDave Kleikamp 
4357ac27a0ecSDave Kleikamp bad_inode:
4358567f3e9aSTheodore Ts'o 	brelse(iloc.bh);
43591d1fe1eeSDavid Howells 	iget_failed(inode);
43601d1fe1eeSDavid Howells 	return ERR_PTR(ret);
4361ac27a0ecSDave Kleikamp }
4362ac27a0ecSDave Kleikamp 
43630fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle,
43640fc1b451SAneesh Kumar K.V 				struct ext4_inode *raw_inode,
43650fc1b451SAneesh Kumar K.V 				struct ext4_inode_info *ei)
43660fc1b451SAneesh Kumar K.V {
43670fc1b451SAneesh Kumar K.V 	struct inode *inode = &(ei->vfs_inode);
43680fc1b451SAneesh Kumar K.V 	u64 i_blocks = inode->i_blocks;
43690fc1b451SAneesh Kumar K.V 	struct super_block *sb = inode->i_sb;
43700fc1b451SAneesh Kumar K.V 
43710fc1b451SAneesh Kumar K.V 	if (i_blocks <= ~0U) {
43720fc1b451SAneesh Kumar K.V 		/*
43734907cb7bSAnatol Pomozov 		 * i_blocks can be represented in a 32 bit variable
43740fc1b451SAneesh Kumar K.V 		 * as multiple of 512 bytes
43750fc1b451SAneesh Kumar K.V 		 */
43768180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
43770fc1b451SAneesh Kumar K.V 		raw_inode->i_blocks_high = 0;
437884a8dce2SDmitry Monakhov 		ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4379f287a1a5STheodore Ts'o 		return 0;
4380f287a1a5STheodore Ts'o 	}
4381f287a1a5STheodore Ts'o 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4382f287a1a5STheodore Ts'o 		return -EFBIG;
4383f287a1a5STheodore Ts'o 
4384f287a1a5STheodore Ts'o 	if (i_blocks <= 0xffffffffffffULL) {
43850fc1b451SAneesh Kumar K.V 		/*
43860fc1b451SAneesh Kumar K.V 		 * i_blocks can be represented in a 48 bit variable
43870fc1b451SAneesh Kumar K.V 		 * as multiple of 512 bytes
43880fc1b451SAneesh Kumar K.V 		 */
43898180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
43900fc1b451SAneesh Kumar K.V 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
439184a8dce2SDmitry Monakhov 		ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
43920fc1b451SAneesh Kumar K.V 	} else {
439384a8dce2SDmitry Monakhov 		ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
43948180a562SAneesh Kumar K.V 		/* i_block is stored in file system block size */
43958180a562SAneesh Kumar K.V 		i_blocks = i_blocks >> (inode->i_blkbits - 9);
43968180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
43978180a562SAneesh Kumar K.V 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
43980fc1b451SAneesh Kumar K.V 	}
4399f287a1a5STheodore Ts'o 	return 0;
44000fc1b451SAneesh Kumar K.V }
44010fc1b451SAneesh Kumar K.V 
4402ac27a0ecSDave Kleikamp /*
4403ac27a0ecSDave Kleikamp  * Post the struct inode info into an on-disk inode location in the
4404ac27a0ecSDave Kleikamp  * buffer-cache.  This gobbles the caller's reference to the
4405ac27a0ecSDave Kleikamp  * buffer_head in the inode location struct.
4406ac27a0ecSDave Kleikamp  *
4407ac27a0ecSDave Kleikamp  * The caller must have write access to iloc->bh.
4408ac27a0ecSDave Kleikamp  */
4409617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle,
4410ac27a0ecSDave Kleikamp 				struct inode *inode,
4411830156c7SFrank Mayhar 				struct ext4_iloc *iloc)
4412ac27a0ecSDave Kleikamp {
4413617ba13bSMingming Cao 	struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4414617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
4415ac27a0ecSDave Kleikamp 	struct buffer_head *bh = iloc->bh;
4416ac27a0ecSDave Kleikamp 	int err = 0, rc, block;
4417b71fc079SJan Kara 	int need_datasync = 0;
441808cefc7aSEric W. Biederman 	uid_t i_uid;
441908cefc7aSEric W. Biederman 	gid_t i_gid;
4420ac27a0ecSDave Kleikamp 
4421ac27a0ecSDave Kleikamp 	/* For fields not not tracking in the in-memory inode,
4422ac27a0ecSDave Kleikamp 	 * initialise them to zero for new inodes. */
442319f5fb7aSTheodore Ts'o 	if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
4424617ba13bSMingming Cao 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
4425ac27a0ecSDave Kleikamp 
4426ff9ddf7eSJan Kara 	ext4_get_inode_flags(ei);
4427ac27a0ecSDave Kleikamp 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
442808cefc7aSEric W. Biederman 	i_uid = i_uid_read(inode);
442908cefc7aSEric W. Biederman 	i_gid = i_gid_read(inode);
4430ac27a0ecSDave Kleikamp 	if (!(test_opt(inode->i_sb, NO_UID32))) {
443108cefc7aSEric W. Biederman 		raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
443208cefc7aSEric W. Biederman 		raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
4433ac27a0ecSDave Kleikamp /*
4434ac27a0ecSDave Kleikamp  * Fix up interoperability with old kernels. Otherwise, old inodes get
4435ac27a0ecSDave Kleikamp  * re-used with the upper 16 bits of the uid/gid intact
4436ac27a0ecSDave Kleikamp  */
4437ac27a0ecSDave Kleikamp 		if (!ei->i_dtime) {
4438ac27a0ecSDave Kleikamp 			raw_inode->i_uid_high =
443908cefc7aSEric W. Biederman 				cpu_to_le16(high_16_bits(i_uid));
4440ac27a0ecSDave Kleikamp 			raw_inode->i_gid_high =
444108cefc7aSEric W. Biederman 				cpu_to_le16(high_16_bits(i_gid));
4442ac27a0ecSDave Kleikamp 		} else {
4443ac27a0ecSDave Kleikamp 			raw_inode->i_uid_high = 0;
4444ac27a0ecSDave Kleikamp 			raw_inode->i_gid_high = 0;
4445ac27a0ecSDave Kleikamp 		}
4446ac27a0ecSDave Kleikamp 	} else {
444708cefc7aSEric W. Biederman 		raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
444808cefc7aSEric W. Biederman 		raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
4449ac27a0ecSDave Kleikamp 		raw_inode->i_uid_high = 0;
4450ac27a0ecSDave Kleikamp 		raw_inode->i_gid_high = 0;
4451ac27a0ecSDave Kleikamp 	}
4452ac27a0ecSDave Kleikamp 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
4453ef7f3835SKalpak Shah 
4454ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4455ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4456ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4457ef7f3835SKalpak Shah 	EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4458ef7f3835SKalpak Shah 
44590fc1b451SAneesh Kumar K.V 	if (ext4_inode_blocks_set(handle, raw_inode, ei))
44600fc1b451SAneesh Kumar K.V 		goto out_brelse;
4461ac27a0ecSDave Kleikamp 	raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
4462353eb83cSTheodore Ts'o 	raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
44639b8f1f01SMingming Cao 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
44649b8f1f01SMingming Cao 	    cpu_to_le32(EXT4_OS_HURD))
4465a1ddeb7eSBadari Pulavarty 		raw_inode->i_file_acl_high =
4466a1ddeb7eSBadari Pulavarty 			cpu_to_le16(ei->i_file_acl >> 32);
44677973c0c1SAneesh Kumar K.V 	raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
4468b71fc079SJan Kara 	if (ei->i_disksize != ext4_isize(raw_inode)) {
4469a48380f7SAneesh Kumar K.V 		ext4_isize_set(raw_inode, ei->i_disksize);
4470b71fc079SJan Kara 		need_datasync = 1;
4471b71fc079SJan Kara 	}
4472ac27a0ecSDave Kleikamp 	if (ei->i_disksize > 0x7fffffffULL) {
4473ac27a0ecSDave Kleikamp 		struct super_block *sb = inode->i_sb;
4474617ba13bSMingming Cao 		if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4475617ba13bSMingming Cao 				EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4476617ba13bSMingming Cao 				EXT4_SB(sb)->s_es->s_rev_level ==
4477617ba13bSMingming Cao 				cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4478ac27a0ecSDave Kleikamp 			/* If this is the first large file
4479ac27a0ecSDave Kleikamp 			 * created, add a flag to the superblock.
4480ac27a0ecSDave Kleikamp 			 */
4481617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle,
4482617ba13bSMingming Cao 					EXT4_SB(sb)->s_sbh);
4483ac27a0ecSDave Kleikamp 			if (err)
4484ac27a0ecSDave Kleikamp 				goto out_brelse;
4485617ba13bSMingming Cao 			ext4_update_dynamic_rev(sb);
4486617ba13bSMingming Cao 			EXT4_SET_RO_COMPAT_FEATURE(sb,
4487617ba13bSMingming Cao 					EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
44880390131bSFrank Mayhar 			ext4_handle_sync(handle);
4489b50924c2SArtem Bityutskiy 			err = ext4_handle_dirty_super(handle, sb);
4490ac27a0ecSDave Kleikamp 		}
4491ac27a0ecSDave Kleikamp 	}
4492ac27a0ecSDave Kleikamp 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4493ac27a0ecSDave Kleikamp 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4494ac27a0ecSDave Kleikamp 		if (old_valid_dev(inode->i_rdev)) {
4495ac27a0ecSDave Kleikamp 			raw_inode->i_block[0] =
4496ac27a0ecSDave Kleikamp 				cpu_to_le32(old_encode_dev(inode->i_rdev));
4497ac27a0ecSDave Kleikamp 			raw_inode->i_block[1] = 0;
4498ac27a0ecSDave Kleikamp 		} else {
4499ac27a0ecSDave Kleikamp 			raw_inode->i_block[0] = 0;
4500ac27a0ecSDave Kleikamp 			raw_inode->i_block[1] =
4501ac27a0ecSDave Kleikamp 				cpu_to_le32(new_encode_dev(inode->i_rdev));
4502ac27a0ecSDave Kleikamp 			raw_inode->i_block[2] = 0;
4503ac27a0ecSDave Kleikamp 		}
4504f19d5870STao Ma 	} else if (!ext4_has_inline_data(inode)) {
4505de9a55b8STheodore Ts'o 		for (block = 0; block < EXT4_N_BLOCKS; block++)
4506ac27a0ecSDave Kleikamp 			raw_inode->i_block[block] = ei->i_data[block];
4507f19d5870STao Ma 	}
4508ac27a0ecSDave Kleikamp 
450925ec56b5SJean Noel Cordenner 	raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
451025ec56b5SJean Noel Cordenner 	if (ei->i_extra_isize) {
451125ec56b5SJean Noel Cordenner 		if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
451225ec56b5SJean Noel Cordenner 			raw_inode->i_version_hi =
451325ec56b5SJean Noel Cordenner 			cpu_to_le32(inode->i_version >> 32);
4514ac27a0ecSDave Kleikamp 		raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
451525ec56b5SJean Noel Cordenner 	}
451625ec56b5SJean Noel Cordenner 
4517814525f4SDarrick J. Wong 	ext4_inode_csum_set(inode, raw_inode, ei);
4518814525f4SDarrick J. Wong 
45190390131bSFrank Mayhar 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
452073b50c1cSCurt Wohlgemuth 	rc = ext4_handle_dirty_metadata(handle, NULL, bh);
4521ac27a0ecSDave Kleikamp 	if (!err)
4522ac27a0ecSDave Kleikamp 		err = rc;
452319f5fb7aSTheodore Ts'o 	ext4_clear_inode_state(inode, EXT4_STATE_NEW);
4524ac27a0ecSDave Kleikamp 
4525b71fc079SJan Kara 	ext4_update_inode_fsync_trans(handle, inode, need_datasync);
4526ac27a0ecSDave Kleikamp out_brelse:
4527ac27a0ecSDave Kleikamp 	brelse(bh);
4528617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4529ac27a0ecSDave Kleikamp 	return err;
4530ac27a0ecSDave Kleikamp }
4531ac27a0ecSDave Kleikamp 
4532ac27a0ecSDave Kleikamp /*
4533617ba13bSMingming Cao  * ext4_write_inode()
4534ac27a0ecSDave Kleikamp  *
4535ac27a0ecSDave Kleikamp  * We are called from a few places:
4536ac27a0ecSDave Kleikamp  *
4537ac27a0ecSDave Kleikamp  * - Within generic_file_write() for O_SYNC files.
4538ac27a0ecSDave Kleikamp  *   Here, there will be no transaction running. We wait for any running
45394907cb7bSAnatol Pomozov  *   transaction to commit.
4540ac27a0ecSDave Kleikamp  *
4541ac27a0ecSDave Kleikamp  * - Within sys_sync(), kupdate and such.
4542ac27a0ecSDave Kleikamp  *   We wait on commit, if tol to.
4543ac27a0ecSDave Kleikamp  *
4544ac27a0ecSDave Kleikamp  * - Within prune_icache() (PF_MEMALLOC == true)
4545ac27a0ecSDave Kleikamp  *   Here we simply return.  We can't afford to block kswapd on the
4546ac27a0ecSDave Kleikamp  *   journal commit.
4547ac27a0ecSDave Kleikamp  *
4548ac27a0ecSDave Kleikamp  * In all cases it is actually safe for us to return without doing anything,
4549ac27a0ecSDave Kleikamp  * because the inode has been copied into a raw inode buffer in
4550617ba13bSMingming Cao  * ext4_mark_inode_dirty().  This is a correctness thing for O_SYNC and for
4551ac27a0ecSDave Kleikamp  * knfsd.
4552ac27a0ecSDave Kleikamp  *
4553ac27a0ecSDave Kleikamp  * Note that we are absolutely dependent upon all inode dirtiers doing the
4554ac27a0ecSDave Kleikamp  * right thing: they *must* call mark_inode_dirty() after dirtying info in
4555ac27a0ecSDave Kleikamp  * which we are interested.
4556ac27a0ecSDave Kleikamp  *
4557ac27a0ecSDave Kleikamp  * It would be a bug for them to not do this.  The code:
4558ac27a0ecSDave Kleikamp  *
4559ac27a0ecSDave Kleikamp  *	mark_inode_dirty(inode)
4560ac27a0ecSDave Kleikamp  *	stuff();
4561ac27a0ecSDave Kleikamp  *	inode->i_size = expr;
4562ac27a0ecSDave Kleikamp  *
4563ac27a0ecSDave Kleikamp  * is in error because a kswapd-driven write_inode() could occur while
4564ac27a0ecSDave Kleikamp  * `stuff()' is running, and the new i_size will be lost.  Plus the inode
4565ac27a0ecSDave Kleikamp  * will no longer be on the superblock's dirty inode list.
4566ac27a0ecSDave Kleikamp  */
4567a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
4568ac27a0ecSDave Kleikamp {
456991ac6f43SFrank Mayhar 	int err;
457091ac6f43SFrank Mayhar 
4571ac27a0ecSDave Kleikamp 	if (current->flags & PF_MEMALLOC)
4572ac27a0ecSDave Kleikamp 		return 0;
4573ac27a0ecSDave Kleikamp 
457491ac6f43SFrank Mayhar 	if (EXT4_SB(inode->i_sb)->s_journal) {
4575617ba13bSMingming Cao 		if (ext4_journal_current_handle()) {
4576b38bd33aSMingming Cao 			jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4577ac27a0ecSDave Kleikamp 			dump_stack();
4578ac27a0ecSDave Kleikamp 			return -EIO;
4579ac27a0ecSDave Kleikamp 		}
4580ac27a0ecSDave Kleikamp 
4581a9185b41SChristoph Hellwig 		if (wbc->sync_mode != WB_SYNC_ALL)
4582ac27a0ecSDave Kleikamp 			return 0;
4583ac27a0ecSDave Kleikamp 
458491ac6f43SFrank Mayhar 		err = ext4_force_commit(inode->i_sb);
458591ac6f43SFrank Mayhar 	} else {
458691ac6f43SFrank Mayhar 		struct ext4_iloc iloc;
458791ac6f43SFrank Mayhar 
45888b472d73SCurt Wohlgemuth 		err = __ext4_get_inode_loc(inode, &iloc, 0);
458991ac6f43SFrank Mayhar 		if (err)
459091ac6f43SFrank Mayhar 			return err;
4591a9185b41SChristoph Hellwig 		if (wbc->sync_mode == WB_SYNC_ALL)
4592830156c7SFrank Mayhar 			sync_dirty_buffer(iloc.bh);
4593830156c7SFrank Mayhar 		if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
4594c398eda0STheodore Ts'o 			EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4595c398eda0STheodore Ts'o 					 "IO error syncing inode");
4596830156c7SFrank Mayhar 			err = -EIO;
4597830156c7SFrank Mayhar 		}
4598fd2dd9fbSCurt Wohlgemuth 		brelse(iloc.bh);
459991ac6f43SFrank Mayhar 	}
460091ac6f43SFrank Mayhar 	return err;
4601ac27a0ecSDave Kleikamp }
4602ac27a0ecSDave Kleikamp 
4603ac27a0ecSDave Kleikamp /*
460453e87268SJan Kara  * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
460553e87268SJan Kara  * buffers that are attached to a page stradding i_size and are undergoing
460653e87268SJan Kara  * commit. In that case we have to wait for commit to finish and try again.
460753e87268SJan Kara  */
460853e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode)
460953e87268SJan Kara {
461053e87268SJan Kara 	struct page *page;
461153e87268SJan Kara 	unsigned offset;
461253e87268SJan Kara 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
461353e87268SJan Kara 	tid_t commit_tid = 0;
461453e87268SJan Kara 	int ret;
461553e87268SJan Kara 
461653e87268SJan Kara 	offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
461753e87268SJan Kara 	/*
461853e87268SJan Kara 	 * All buffers in the last page remain valid? Then there's nothing to
461953e87268SJan Kara 	 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
462053e87268SJan Kara 	 * blocksize case
462153e87268SJan Kara 	 */
462253e87268SJan Kara 	if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
462353e87268SJan Kara 		return;
462453e87268SJan Kara 	while (1) {
462553e87268SJan Kara 		page = find_lock_page(inode->i_mapping,
462653e87268SJan Kara 				      inode->i_size >> PAGE_CACHE_SHIFT);
462753e87268SJan Kara 		if (!page)
462853e87268SJan Kara 			return;
462953e87268SJan Kara 		ret = __ext4_journalled_invalidatepage(page, offset);
463053e87268SJan Kara 		unlock_page(page);
463153e87268SJan Kara 		page_cache_release(page);
463253e87268SJan Kara 		if (ret != -EBUSY)
463353e87268SJan Kara 			return;
463453e87268SJan Kara 		commit_tid = 0;
463553e87268SJan Kara 		read_lock(&journal->j_state_lock);
463653e87268SJan Kara 		if (journal->j_committing_transaction)
463753e87268SJan Kara 			commit_tid = journal->j_committing_transaction->t_tid;
463853e87268SJan Kara 		read_unlock(&journal->j_state_lock);
463953e87268SJan Kara 		if (commit_tid)
464053e87268SJan Kara 			jbd2_log_wait_commit(journal, commit_tid);
464153e87268SJan Kara 	}
464253e87268SJan Kara }
464353e87268SJan Kara 
464453e87268SJan Kara /*
4645617ba13bSMingming Cao  * ext4_setattr()
4646ac27a0ecSDave Kleikamp  *
4647ac27a0ecSDave Kleikamp  * Called from notify_change.
4648ac27a0ecSDave Kleikamp  *
4649ac27a0ecSDave Kleikamp  * We want to trap VFS attempts to truncate the file as soon as
4650ac27a0ecSDave Kleikamp  * possible.  In particular, we want to make sure that when the VFS
4651ac27a0ecSDave Kleikamp  * shrinks i_size, we put the inode on the orphan list and modify
4652ac27a0ecSDave Kleikamp  * i_disksize immediately, so that during the subsequent flushing of
4653ac27a0ecSDave Kleikamp  * dirty pages and freeing of disk blocks, we can guarantee that any
4654ac27a0ecSDave Kleikamp  * commit will leave the blocks being flushed in an unused state on
4655ac27a0ecSDave Kleikamp  * disk.  (On recovery, the inode will get truncated and the blocks will
4656ac27a0ecSDave Kleikamp  * be freed, so we have a strong guarantee that no future commit will
4657ac27a0ecSDave Kleikamp  * leave these blocks visible to the user.)
4658ac27a0ecSDave Kleikamp  *
4659678aaf48SJan Kara  * Another thing we have to assure is that if we are in ordered mode
4660678aaf48SJan Kara  * and inode is still attached to the committing transaction, we must
4661678aaf48SJan Kara  * we start writeout of all the dirty pages which are being truncated.
4662678aaf48SJan Kara  * This way we are sure that all the data written in the previous
4663678aaf48SJan Kara  * transaction are already on disk (truncate waits for pages under
4664678aaf48SJan Kara  * writeback).
4665678aaf48SJan Kara  *
4666678aaf48SJan Kara  * Called with inode->i_mutex down.
4667ac27a0ecSDave Kleikamp  */
4668617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr)
4669ac27a0ecSDave Kleikamp {
4670ac27a0ecSDave Kleikamp 	struct inode *inode = dentry->d_inode;
4671ac27a0ecSDave Kleikamp 	int error, rc = 0;
46723d287de3SDmitry Monakhov 	int orphan = 0;
4673ac27a0ecSDave Kleikamp 	const unsigned int ia_valid = attr->ia_valid;
4674ac27a0ecSDave Kleikamp 
4675ac27a0ecSDave Kleikamp 	error = inode_change_ok(inode, attr);
4676ac27a0ecSDave Kleikamp 	if (error)
4677ac27a0ecSDave Kleikamp 		return error;
4678ac27a0ecSDave Kleikamp 
467912755627SDmitry Monakhov 	if (is_quota_modification(inode, attr))
4680871a2931SChristoph Hellwig 		dquot_initialize(inode);
468108cefc7aSEric W. Biederman 	if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
468208cefc7aSEric W. Biederman 	    (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
4683ac27a0ecSDave Kleikamp 		handle_t *handle;
4684ac27a0ecSDave Kleikamp 
4685ac27a0ecSDave Kleikamp 		/* (user+group)*(old+new) structure, inode write (sb,
4686ac27a0ecSDave Kleikamp 		 * inode block, ? - but truncate inode update has it) */
46879924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
46889924a92aSTheodore Ts'o 			(EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4689194074acSDmitry Monakhov 			 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
4690ac27a0ecSDave Kleikamp 		if (IS_ERR(handle)) {
4691ac27a0ecSDave Kleikamp 			error = PTR_ERR(handle);
4692ac27a0ecSDave Kleikamp 			goto err_out;
4693ac27a0ecSDave Kleikamp 		}
4694b43fa828SChristoph Hellwig 		error = dquot_transfer(inode, attr);
4695ac27a0ecSDave Kleikamp 		if (error) {
4696617ba13bSMingming Cao 			ext4_journal_stop(handle);
4697ac27a0ecSDave Kleikamp 			return error;
4698ac27a0ecSDave Kleikamp 		}
4699ac27a0ecSDave Kleikamp 		/* Update corresponding info in inode so that everything is in
4700ac27a0ecSDave Kleikamp 		 * one transaction */
4701ac27a0ecSDave Kleikamp 		if (attr->ia_valid & ATTR_UID)
4702ac27a0ecSDave Kleikamp 			inode->i_uid = attr->ia_uid;
4703ac27a0ecSDave Kleikamp 		if (attr->ia_valid & ATTR_GID)
4704ac27a0ecSDave Kleikamp 			inode->i_gid = attr->ia_gid;
4705617ba13bSMingming Cao 		error = ext4_mark_inode_dirty(handle, inode);
4706617ba13bSMingming Cao 		ext4_journal_stop(handle);
4707ac27a0ecSDave Kleikamp 	}
4708ac27a0ecSDave Kleikamp 
4709e2b46574SEric Sandeen 	if (attr->ia_valid & ATTR_SIZE) {
4710562c72aaSChristoph Hellwig 
471112e9b892SDmitry Monakhov 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4712e2b46574SEric Sandeen 			struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4713e2b46574SEric Sandeen 
47140c095c7fSTheodore Ts'o 			if (attr->ia_size > sbi->s_bitmap_maxbytes)
47150c095c7fSTheodore Ts'o 				return -EFBIG;
4716e2b46574SEric Sandeen 		}
4717e2b46574SEric Sandeen 	}
4718e2b46574SEric Sandeen 
4719ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode) &&
4720c8d46e41SJiaying Zhang 	    attr->ia_valid & ATTR_SIZE &&
4721072bd7eaSTheodore Ts'o 	    (attr->ia_size < inode->i_size)) {
4722ac27a0ecSDave Kleikamp 		handle_t *handle;
4723ac27a0ecSDave Kleikamp 
47249924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
4725ac27a0ecSDave Kleikamp 		if (IS_ERR(handle)) {
4726ac27a0ecSDave Kleikamp 			error = PTR_ERR(handle);
4727ac27a0ecSDave Kleikamp 			goto err_out;
4728ac27a0ecSDave Kleikamp 		}
47293d287de3SDmitry Monakhov 		if (ext4_handle_valid(handle)) {
4730617ba13bSMingming Cao 			error = ext4_orphan_add(handle, inode);
47313d287de3SDmitry Monakhov 			orphan = 1;
47323d287de3SDmitry Monakhov 		}
4733617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = attr->ia_size;
4734617ba13bSMingming Cao 		rc = ext4_mark_inode_dirty(handle, inode);
4735ac27a0ecSDave Kleikamp 		if (!error)
4736ac27a0ecSDave Kleikamp 			error = rc;
4737617ba13bSMingming Cao 		ext4_journal_stop(handle);
4738678aaf48SJan Kara 
4739678aaf48SJan Kara 		if (ext4_should_order_data(inode)) {
4740678aaf48SJan Kara 			error = ext4_begin_ordered_truncate(inode,
4741678aaf48SJan Kara 							    attr->ia_size);
4742678aaf48SJan Kara 			if (error) {
4743678aaf48SJan Kara 				/* Do as much error cleanup as possible */
47449924a92aSTheodore Ts'o 				handle = ext4_journal_start(inode,
47459924a92aSTheodore Ts'o 							    EXT4_HT_INODE, 3);
4746678aaf48SJan Kara 				if (IS_ERR(handle)) {
4747678aaf48SJan Kara 					ext4_orphan_del(NULL, inode);
4748678aaf48SJan Kara 					goto err_out;
4749678aaf48SJan Kara 				}
4750678aaf48SJan Kara 				ext4_orphan_del(handle, inode);
47513d287de3SDmitry Monakhov 				orphan = 0;
4752678aaf48SJan Kara 				ext4_journal_stop(handle);
4753678aaf48SJan Kara 				goto err_out;
4754678aaf48SJan Kara 			}
4755678aaf48SJan Kara 		}
4756ac27a0ecSDave Kleikamp 	}
4757ac27a0ecSDave Kleikamp 
4758072bd7eaSTheodore Ts'o 	if (attr->ia_valid & ATTR_SIZE) {
475953e87268SJan Kara 		if (attr->ia_size != inode->i_size) {
476053e87268SJan Kara 			loff_t oldsize = inode->i_size;
476153e87268SJan Kara 
476253e87268SJan Kara 			i_size_write(inode, attr->ia_size);
476353e87268SJan Kara 			/*
476453e87268SJan Kara 			 * Blocks are going to be removed from the inode. Wait
476553e87268SJan Kara 			 * for dio in flight.  Temporarily disable
476653e87268SJan Kara 			 * dioread_nolock to prevent livelock.
476753e87268SJan Kara 			 */
47681b65007eSDmitry Monakhov 			if (orphan) {
476953e87268SJan Kara 				if (!ext4_should_journal_data(inode)) {
47701b65007eSDmitry Monakhov 					ext4_inode_block_unlocked_dio(inode);
47711c9114f9SDmitry Monakhov 					inode_dio_wait(inode);
47721b65007eSDmitry Monakhov 					ext4_inode_resume_unlocked_dio(inode);
477353e87268SJan Kara 				} else
477453e87268SJan Kara 					ext4_wait_for_tail_page_commit(inode);
47751b65007eSDmitry Monakhov 			}
477653e87268SJan Kara 			/*
477753e87268SJan Kara 			 * Truncate pagecache after we've waited for commit
477853e87268SJan Kara 			 * in data=journal mode to make pages freeable.
477953e87268SJan Kara 			 */
478053e87268SJan Kara 			truncate_pagecache(inode, oldsize, inode->i_size);
47811c9114f9SDmitry Monakhov 		}
4782072bd7eaSTheodore Ts'o 		ext4_truncate(inode);
4783072bd7eaSTheodore Ts'o 	}
4784ac27a0ecSDave Kleikamp 
47851025774cSChristoph Hellwig 	if (!rc) {
47861025774cSChristoph Hellwig 		setattr_copy(inode, attr);
47871025774cSChristoph Hellwig 		mark_inode_dirty(inode);
47881025774cSChristoph Hellwig 	}
47891025774cSChristoph Hellwig 
47901025774cSChristoph Hellwig 	/*
47911025774cSChristoph Hellwig 	 * If the call to ext4_truncate failed to get a transaction handle at
47921025774cSChristoph Hellwig 	 * all, we need to clean up the in-core orphan list manually.
47931025774cSChristoph Hellwig 	 */
47943d287de3SDmitry Monakhov 	if (orphan && inode->i_nlink)
4795617ba13bSMingming Cao 		ext4_orphan_del(NULL, inode);
4796ac27a0ecSDave Kleikamp 
4797ac27a0ecSDave Kleikamp 	if (!rc && (ia_valid & ATTR_MODE))
4798617ba13bSMingming Cao 		rc = ext4_acl_chmod(inode);
4799ac27a0ecSDave Kleikamp 
4800ac27a0ecSDave Kleikamp err_out:
4801617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, error);
4802ac27a0ecSDave Kleikamp 	if (!error)
4803ac27a0ecSDave Kleikamp 		error = rc;
4804ac27a0ecSDave Kleikamp 	return error;
4805ac27a0ecSDave Kleikamp }
4806ac27a0ecSDave Kleikamp 
48073e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
48083e3398a0SMingming Cao 		 struct kstat *stat)
48093e3398a0SMingming Cao {
48103e3398a0SMingming Cao 	struct inode *inode;
48113e3398a0SMingming Cao 	unsigned long delalloc_blocks;
48123e3398a0SMingming Cao 
48133e3398a0SMingming Cao 	inode = dentry->d_inode;
48143e3398a0SMingming Cao 	generic_fillattr(inode, stat);
48153e3398a0SMingming Cao 
48163e3398a0SMingming Cao 	/*
48173e3398a0SMingming Cao 	 * We can't update i_blocks if the block allocation is delayed
48183e3398a0SMingming Cao 	 * otherwise in the case of system crash before the real block
48193e3398a0SMingming Cao 	 * allocation is done, we will have i_blocks inconsistent with
48203e3398a0SMingming Cao 	 * on-disk file blocks.
48213e3398a0SMingming Cao 	 * We always keep i_blocks updated together with real
48223e3398a0SMingming Cao 	 * allocation. But to not confuse with user, stat
48233e3398a0SMingming Cao 	 * will return the blocks that include the delayed allocation
48243e3398a0SMingming Cao 	 * blocks for this file.
48253e3398a0SMingming Cao 	 */
482696607551STao Ma 	delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
482796607551STao Ma 				EXT4_I(inode)->i_reserved_data_blocks);
48283e3398a0SMingming Cao 
48293e3398a0SMingming Cao 	stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
48303e3398a0SMingming Cao 	return 0;
48313e3398a0SMingming Cao }
4832ac27a0ecSDave Kleikamp 
4833a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4834a02908f1SMingming Cao {
483512e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
48368bb2b247SAmir Goldstein 		return ext4_ind_trans_blocks(inode, nrblocks, chunk);
4837ac51d837STheodore Ts'o 	return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
4838a02908f1SMingming Cao }
4839ac51d837STheodore Ts'o 
4840a02908f1SMingming Cao /*
4841a02908f1SMingming Cao  * Account for index blocks, block groups bitmaps and block group
4842a02908f1SMingming Cao  * descriptor blocks if modify datablocks and index blocks
4843a02908f1SMingming Cao  * worse case, the indexs blocks spread over different block groups
4844a02908f1SMingming Cao  *
4845a02908f1SMingming Cao  * If datablocks are discontiguous, they are possible to spread over
48464907cb7bSAnatol Pomozov  * different block groups too. If they are contiguous, with flexbg,
4847a02908f1SMingming Cao  * they could still across block group boundary.
4848a02908f1SMingming Cao  *
4849a02908f1SMingming Cao  * Also account for superblock, inode, quota and xattr blocks
4850a02908f1SMingming Cao  */
48511f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4852a02908f1SMingming Cao {
48538df9675fSTheodore Ts'o 	ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
48548df9675fSTheodore Ts'o 	int gdpblocks;
4855a02908f1SMingming Cao 	int idxblocks;
4856a02908f1SMingming Cao 	int ret = 0;
4857a02908f1SMingming Cao 
4858a02908f1SMingming Cao 	/*
4859a02908f1SMingming Cao 	 * How many index blocks need to touch to modify nrblocks?
4860a02908f1SMingming Cao 	 * The "Chunk" flag indicating whether the nrblocks is
4861a02908f1SMingming Cao 	 * physically contiguous on disk
4862a02908f1SMingming Cao 	 *
4863a02908f1SMingming Cao 	 * For Direct IO and fallocate, they calls get_block to allocate
4864a02908f1SMingming Cao 	 * one single extent at a time, so they could set the "Chunk" flag
4865a02908f1SMingming Cao 	 */
4866a02908f1SMingming Cao 	idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4867a02908f1SMingming Cao 
4868a02908f1SMingming Cao 	ret = idxblocks;
4869a02908f1SMingming Cao 
4870a02908f1SMingming Cao 	/*
4871a02908f1SMingming Cao 	 * Now let's see how many group bitmaps and group descriptors need
4872a02908f1SMingming Cao 	 * to account
4873a02908f1SMingming Cao 	 */
4874a02908f1SMingming Cao 	groups = idxblocks;
4875a02908f1SMingming Cao 	if (chunk)
4876a02908f1SMingming Cao 		groups += 1;
4877ac27a0ecSDave Kleikamp 	else
4878a02908f1SMingming Cao 		groups += nrblocks;
4879ac27a0ecSDave Kleikamp 
4880a02908f1SMingming Cao 	gdpblocks = groups;
48818df9675fSTheodore Ts'o 	if (groups > ngroups)
48828df9675fSTheodore Ts'o 		groups = ngroups;
4883a02908f1SMingming Cao 	if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4884a02908f1SMingming Cao 		gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4885a02908f1SMingming Cao 
4886a02908f1SMingming Cao 	/* bitmaps and block group descriptor blocks */
4887a02908f1SMingming Cao 	ret += groups + gdpblocks;
4888a02908f1SMingming Cao 
4889a02908f1SMingming Cao 	/* Blocks for super block, inode, quota and xattr blocks */
4890a02908f1SMingming Cao 	ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
4891ac27a0ecSDave Kleikamp 
4892ac27a0ecSDave Kleikamp 	return ret;
4893ac27a0ecSDave Kleikamp }
4894ac27a0ecSDave Kleikamp 
4895ac27a0ecSDave Kleikamp /*
489625985edcSLucas De Marchi  * Calculate the total number of credits to reserve to fit
4897f3bd1f3fSMingming Cao  * the modification of a single pages into a single transaction,
4898f3bd1f3fSMingming Cao  * which may include multiple chunks of block allocations.
4899a02908f1SMingming Cao  *
4900525f4ed8SMingming Cao  * This could be called via ext4_write_begin()
4901a02908f1SMingming Cao  *
4902525f4ed8SMingming Cao  * We need to consider the worse case, when
4903a02908f1SMingming Cao  * one new block per extent.
4904a02908f1SMingming Cao  */
4905a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode)
4906a02908f1SMingming Cao {
4907a02908f1SMingming Cao 	int bpp = ext4_journal_blocks_per_page(inode);
4908a02908f1SMingming Cao 	int ret;
4909a02908f1SMingming Cao 
4910a02908f1SMingming Cao 	ret = ext4_meta_trans_blocks(inode, bpp, 0);
4911a02908f1SMingming Cao 
4912a02908f1SMingming Cao 	/* Account for data blocks for journalled mode */
4913a02908f1SMingming Cao 	if (ext4_should_journal_data(inode))
4914a02908f1SMingming Cao 		ret += bpp;
4915a02908f1SMingming Cao 	return ret;
4916a02908f1SMingming Cao }
4917f3bd1f3fSMingming Cao 
4918f3bd1f3fSMingming Cao /*
4919f3bd1f3fSMingming Cao  * Calculate the journal credits for a chunk of data modification.
4920f3bd1f3fSMingming Cao  *
4921f3bd1f3fSMingming Cao  * This is called from DIO, fallocate or whoever calling
492279e83036SEric Sandeen  * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
4923f3bd1f3fSMingming Cao  *
4924f3bd1f3fSMingming Cao  * journal buffers for data blocks are not included here, as DIO
4925f3bd1f3fSMingming Cao  * and fallocate do no need to journal data buffers.
4926f3bd1f3fSMingming Cao  */
4927f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4928f3bd1f3fSMingming Cao {
4929f3bd1f3fSMingming Cao 	return ext4_meta_trans_blocks(inode, nrblocks, 1);
4930f3bd1f3fSMingming Cao }
4931f3bd1f3fSMingming Cao 
4932a02908f1SMingming Cao /*
4933617ba13bSMingming Cao  * The caller must have previously called ext4_reserve_inode_write().
4934ac27a0ecSDave Kleikamp  * Give this, we know that the caller already has write access to iloc->bh.
4935ac27a0ecSDave Kleikamp  */
4936617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle,
4937617ba13bSMingming Cao 			 struct inode *inode, struct ext4_iloc *iloc)
4938ac27a0ecSDave Kleikamp {
4939ac27a0ecSDave Kleikamp 	int err = 0;
4940ac27a0ecSDave Kleikamp 
4941c64db50eSTheodore Ts'o 	if (IS_I_VERSION(inode))
494225ec56b5SJean Noel Cordenner 		inode_inc_iversion(inode);
494325ec56b5SJean Noel Cordenner 
4944ac27a0ecSDave Kleikamp 	/* the do_update_inode consumes one bh->b_count */
4945ac27a0ecSDave Kleikamp 	get_bh(iloc->bh);
4946ac27a0ecSDave Kleikamp 
4947dab291afSMingming Cao 	/* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
4948830156c7SFrank Mayhar 	err = ext4_do_update_inode(handle, inode, iloc);
4949ac27a0ecSDave Kleikamp 	put_bh(iloc->bh);
4950ac27a0ecSDave Kleikamp 	return err;
4951ac27a0ecSDave Kleikamp }
4952ac27a0ecSDave Kleikamp 
4953ac27a0ecSDave Kleikamp /*
4954ac27a0ecSDave Kleikamp  * On success, We end up with an outstanding reference count against
4955ac27a0ecSDave Kleikamp  * iloc->bh.  This _must_ be cleaned up later.
4956ac27a0ecSDave Kleikamp  */
4957ac27a0ecSDave Kleikamp 
4958ac27a0ecSDave Kleikamp int
4959617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4960617ba13bSMingming Cao 			 struct ext4_iloc *iloc)
4961ac27a0ecSDave Kleikamp {
49620390131bSFrank Mayhar 	int err;
49630390131bSFrank Mayhar 
4964617ba13bSMingming Cao 	err = ext4_get_inode_loc(inode, iloc);
4965ac27a0ecSDave Kleikamp 	if (!err) {
4966ac27a0ecSDave Kleikamp 		BUFFER_TRACE(iloc->bh, "get_write_access");
4967617ba13bSMingming Cao 		err = ext4_journal_get_write_access(handle, iloc->bh);
4968ac27a0ecSDave Kleikamp 		if (err) {
4969ac27a0ecSDave Kleikamp 			brelse(iloc->bh);
4970ac27a0ecSDave Kleikamp 			iloc->bh = NULL;
4971ac27a0ecSDave Kleikamp 		}
4972ac27a0ecSDave Kleikamp 	}
4973617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4974ac27a0ecSDave Kleikamp 	return err;
4975ac27a0ecSDave Kleikamp }
4976ac27a0ecSDave Kleikamp 
4977ac27a0ecSDave Kleikamp /*
49786dd4ee7cSKalpak Shah  * Expand an inode by new_extra_isize bytes.
49796dd4ee7cSKalpak Shah  * Returns 0 on success or negative error number on failure.
49806dd4ee7cSKalpak Shah  */
49811d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode,
49821d03ec98SAneesh Kumar K.V 				   unsigned int new_extra_isize,
49831d03ec98SAneesh Kumar K.V 				   struct ext4_iloc iloc,
49841d03ec98SAneesh Kumar K.V 				   handle_t *handle)
49856dd4ee7cSKalpak Shah {
49866dd4ee7cSKalpak Shah 	struct ext4_inode *raw_inode;
49876dd4ee7cSKalpak Shah 	struct ext4_xattr_ibody_header *header;
49886dd4ee7cSKalpak Shah 
49896dd4ee7cSKalpak Shah 	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
49906dd4ee7cSKalpak Shah 		return 0;
49916dd4ee7cSKalpak Shah 
49926dd4ee7cSKalpak Shah 	raw_inode = ext4_raw_inode(&iloc);
49936dd4ee7cSKalpak Shah 
49946dd4ee7cSKalpak Shah 	header = IHDR(inode, raw_inode);
49956dd4ee7cSKalpak Shah 
49966dd4ee7cSKalpak Shah 	/* No extended attributes present */
499719f5fb7aSTheodore Ts'o 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
49986dd4ee7cSKalpak Shah 	    header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
49996dd4ee7cSKalpak Shah 		memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
50006dd4ee7cSKalpak Shah 			new_extra_isize);
50016dd4ee7cSKalpak Shah 		EXT4_I(inode)->i_extra_isize = new_extra_isize;
50026dd4ee7cSKalpak Shah 		return 0;
50036dd4ee7cSKalpak Shah 	}
50046dd4ee7cSKalpak Shah 
50056dd4ee7cSKalpak Shah 	/* try to expand with EAs present */
50066dd4ee7cSKalpak Shah 	return ext4_expand_extra_isize_ea(inode, new_extra_isize,
50076dd4ee7cSKalpak Shah 					  raw_inode, handle);
50086dd4ee7cSKalpak Shah }
50096dd4ee7cSKalpak Shah 
50106dd4ee7cSKalpak Shah /*
5011ac27a0ecSDave Kleikamp  * What we do here is to mark the in-core inode as clean with respect to inode
5012ac27a0ecSDave Kleikamp  * dirtiness (it may still be data-dirty).
5013ac27a0ecSDave Kleikamp  * This means that the in-core inode may be reaped by prune_icache
5014ac27a0ecSDave Kleikamp  * without having to perform any I/O.  This is a very good thing,
5015ac27a0ecSDave Kleikamp  * because *any* task may call prune_icache - even ones which
5016ac27a0ecSDave Kleikamp  * have a transaction open against a different journal.
5017ac27a0ecSDave Kleikamp  *
5018ac27a0ecSDave Kleikamp  * Is this cheating?  Not really.  Sure, we haven't written the
5019ac27a0ecSDave Kleikamp  * inode out, but prune_icache isn't a user-visible syncing function.
5020ac27a0ecSDave Kleikamp  * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5021ac27a0ecSDave Kleikamp  * we start and wait on commits.
5022ac27a0ecSDave Kleikamp  */
5023617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
5024ac27a0ecSDave Kleikamp {
5025617ba13bSMingming Cao 	struct ext4_iloc iloc;
50266dd4ee7cSKalpak Shah 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
50276dd4ee7cSKalpak Shah 	static unsigned int mnt_count;
50286dd4ee7cSKalpak Shah 	int err, ret;
5029ac27a0ecSDave Kleikamp 
5030ac27a0ecSDave Kleikamp 	might_sleep();
50317ff9c073STheodore Ts'o 	trace_ext4_mark_inode_dirty(inode, _RET_IP_);
5032617ba13bSMingming Cao 	err = ext4_reserve_inode_write(handle, inode, &iloc);
50330390131bSFrank Mayhar 	if (ext4_handle_valid(handle) &&
50340390131bSFrank Mayhar 	    EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
503519f5fb7aSTheodore Ts'o 	    !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
50366dd4ee7cSKalpak Shah 		/*
50376dd4ee7cSKalpak Shah 		 * We need extra buffer credits since we may write into EA block
50386dd4ee7cSKalpak Shah 		 * with this same handle. If journal_extend fails, then it will
50396dd4ee7cSKalpak Shah 		 * only result in a minor loss of functionality for that inode.
50406dd4ee7cSKalpak Shah 		 * If this is felt to be critical, then e2fsck should be run to
50416dd4ee7cSKalpak Shah 		 * force a large enough s_min_extra_isize.
50426dd4ee7cSKalpak Shah 		 */
50436dd4ee7cSKalpak Shah 		if ((jbd2_journal_extend(handle,
50446dd4ee7cSKalpak Shah 			     EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
50456dd4ee7cSKalpak Shah 			ret = ext4_expand_extra_isize(inode,
50466dd4ee7cSKalpak Shah 						      sbi->s_want_extra_isize,
50476dd4ee7cSKalpak Shah 						      iloc, handle);
50486dd4ee7cSKalpak Shah 			if (ret) {
504919f5fb7aSTheodore Ts'o 				ext4_set_inode_state(inode,
505019f5fb7aSTheodore Ts'o 						     EXT4_STATE_NO_EXPAND);
5051c1bddad9SAneesh Kumar K.V 				if (mnt_count !=
5052c1bddad9SAneesh Kumar K.V 					le16_to_cpu(sbi->s_es->s_mnt_count)) {
505312062dddSEric Sandeen 					ext4_warning(inode->i_sb,
50546dd4ee7cSKalpak Shah 					"Unable to expand inode %lu. Delete"
50556dd4ee7cSKalpak Shah 					" some EAs or run e2fsck.",
50566dd4ee7cSKalpak Shah 					inode->i_ino);
5057c1bddad9SAneesh Kumar K.V 					mnt_count =
5058c1bddad9SAneesh Kumar K.V 					  le16_to_cpu(sbi->s_es->s_mnt_count);
50596dd4ee7cSKalpak Shah 				}
50606dd4ee7cSKalpak Shah 			}
50616dd4ee7cSKalpak Shah 		}
50626dd4ee7cSKalpak Shah 	}
5063ac27a0ecSDave Kleikamp 	if (!err)
5064617ba13bSMingming Cao 		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
5065ac27a0ecSDave Kleikamp 	return err;
5066ac27a0ecSDave Kleikamp }
5067ac27a0ecSDave Kleikamp 
5068ac27a0ecSDave Kleikamp /*
5069617ba13bSMingming Cao  * ext4_dirty_inode() is called from __mark_inode_dirty()
5070ac27a0ecSDave Kleikamp  *
5071ac27a0ecSDave Kleikamp  * We're really interested in the case where a file is being extended.
5072ac27a0ecSDave Kleikamp  * i_size has been changed by generic_commit_write() and we thus need
5073ac27a0ecSDave Kleikamp  * to include the updated inode in the current transaction.
5074ac27a0ecSDave Kleikamp  *
50755dd4056dSChristoph Hellwig  * Also, dquot_alloc_block() will always dirty the inode when blocks
5076ac27a0ecSDave Kleikamp  * are allocated to the file.
5077ac27a0ecSDave Kleikamp  *
5078ac27a0ecSDave Kleikamp  * If the inode is marked synchronous, we don't honour that here - doing
5079ac27a0ecSDave Kleikamp  * so would cause a commit on atime updates, which we don't bother doing.
5080ac27a0ecSDave Kleikamp  * We handle synchronous inodes at the highest possible level.
5081ac27a0ecSDave Kleikamp  */
5082aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags)
5083ac27a0ecSDave Kleikamp {
5084ac27a0ecSDave Kleikamp 	handle_t *handle;
5085ac27a0ecSDave Kleikamp 
50869924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
5087ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5088ac27a0ecSDave Kleikamp 		goto out;
5089f3dc272fSCurt Wohlgemuth 
5090617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
5091f3dc272fSCurt Wohlgemuth 
5092617ba13bSMingming Cao 	ext4_journal_stop(handle);
5093ac27a0ecSDave Kleikamp out:
5094ac27a0ecSDave Kleikamp 	return;
5095ac27a0ecSDave Kleikamp }
5096ac27a0ecSDave Kleikamp 
5097ac27a0ecSDave Kleikamp #if 0
5098ac27a0ecSDave Kleikamp /*
5099ac27a0ecSDave Kleikamp  * Bind an inode's backing buffer_head into this transaction, to prevent
5100ac27a0ecSDave Kleikamp  * it from being flushed to disk early.  Unlike
5101617ba13bSMingming Cao  * ext4_reserve_inode_write, this leaves behind no bh reference and
5102ac27a0ecSDave Kleikamp  * returns no iloc structure, so the caller needs to repeat the iloc
5103ac27a0ecSDave Kleikamp  * lookup to mark the inode dirty later.
5104ac27a0ecSDave Kleikamp  */
5105617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode)
5106ac27a0ecSDave Kleikamp {
5107617ba13bSMingming Cao 	struct ext4_iloc iloc;
5108ac27a0ecSDave Kleikamp 
5109ac27a0ecSDave Kleikamp 	int err = 0;
5110ac27a0ecSDave Kleikamp 	if (handle) {
5111617ba13bSMingming Cao 		err = ext4_get_inode_loc(inode, &iloc);
5112ac27a0ecSDave Kleikamp 		if (!err) {
5113ac27a0ecSDave Kleikamp 			BUFFER_TRACE(iloc.bh, "get_write_access");
5114dab291afSMingming Cao 			err = jbd2_journal_get_write_access(handle, iloc.bh);
5115ac27a0ecSDave Kleikamp 			if (!err)
51160390131bSFrank Mayhar 				err = ext4_handle_dirty_metadata(handle,
511773b50c1cSCurt Wohlgemuth 								 NULL,
5118ac27a0ecSDave Kleikamp 								 iloc.bh);
5119ac27a0ecSDave Kleikamp 			brelse(iloc.bh);
5120ac27a0ecSDave Kleikamp 		}
5121ac27a0ecSDave Kleikamp 	}
5122617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
5123ac27a0ecSDave Kleikamp 	return err;
5124ac27a0ecSDave Kleikamp }
5125ac27a0ecSDave Kleikamp #endif
5126ac27a0ecSDave Kleikamp 
5127617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val)
5128ac27a0ecSDave Kleikamp {
5129ac27a0ecSDave Kleikamp 	journal_t *journal;
5130ac27a0ecSDave Kleikamp 	handle_t *handle;
5131ac27a0ecSDave Kleikamp 	int err;
5132ac27a0ecSDave Kleikamp 
5133ac27a0ecSDave Kleikamp 	/*
5134ac27a0ecSDave Kleikamp 	 * We have to be very careful here: changing a data block's
5135ac27a0ecSDave Kleikamp 	 * journaling status dynamically is dangerous.  If we write a
5136ac27a0ecSDave Kleikamp 	 * data block to the journal, change the status and then delete
5137ac27a0ecSDave Kleikamp 	 * that block, we risk forgetting to revoke the old log record
5138ac27a0ecSDave Kleikamp 	 * from the journal and so a subsequent replay can corrupt data.
5139ac27a0ecSDave Kleikamp 	 * So, first we make sure that the journal is empty and that
5140ac27a0ecSDave Kleikamp 	 * nobody is changing anything.
5141ac27a0ecSDave Kleikamp 	 */
5142ac27a0ecSDave Kleikamp 
5143617ba13bSMingming Cao 	journal = EXT4_JOURNAL(inode);
51440390131bSFrank Mayhar 	if (!journal)
51450390131bSFrank Mayhar 		return 0;
5146d699594dSDave Hansen 	if (is_journal_aborted(journal))
5147ac27a0ecSDave Kleikamp 		return -EROFS;
51482aff57b0SYongqiang Yang 	/* We have to allocate physical blocks for delalloc blocks
51492aff57b0SYongqiang Yang 	 * before flushing journal. otherwise delalloc blocks can not
51502aff57b0SYongqiang Yang 	 * be allocated any more. even more truncate on delalloc blocks
51512aff57b0SYongqiang Yang 	 * could trigger BUG by flushing delalloc blocks in journal.
51522aff57b0SYongqiang Yang 	 * There is no delalloc block in non-journal data mode.
51532aff57b0SYongqiang Yang 	 */
51542aff57b0SYongqiang Yang 	if (val && test_opt(inode->i_sb, DELALLOC)) {
51552aff57b0SYongqiang Yang 		err = ext4_alloc_da_blocks(inode);
51562aff57b0SYongqiang Yang 		if (err < 0)
51572aff57b0SYongqiang Yang 			return err;
51582aff57b0SYongqiang Yang 	}
5159ac27a0ecSDave Kleikamp 
516017335dccSDmitry Monakhov 	/* Wait for all existing dio workers */
516117335dccSDmitry Monakhov 	ext4_inode_block_unlocked_dio(inode);
516217335dccSDmitry Monakhov 	inode_dio_wait(inode);
516317335dccSDmitry Monakhov 
5164dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
5165ac27a0ecSDave Kleikamp 
5166ac27a0ecSDave Kleikamp 	/*
5167ac27a0ecSDave Kleikamp 	 * OK, there are no updates running now, and all cached data is
5168ac27a0ecSDave Kleikamp 	 * synced to disk.  We are now in a completely consistent state
5169ac27a0ecSDave Kleikamp 	 * which doesn't have anything in the journal, and we know that
5170ac27a0ecSDave Kleikamp 	 * no filesystem updates are running, so it is safe to modify
5171ac27a0ecSDave Kleikamp 	 * the inode's in-core data-journaling state flag now.
5172ac27a0ecSDave Kleikamp 	 */
5173ac27a0ecSDave Kleikamp 
5174ac27a0ecSDave Kleikamp 	if (val)
517512e9b892SDmitry Monakhov 		ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
51765872ddaaSYongqiang Yang 	else {
51775872ddaaSYongqiang Yang 		jbd2_journal_flush(journal);
517812e9b892SDmitry Monakhov 		ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
51795872ddaaSYongqiang Yang 	}
5180617ba13bSMingming Cao 	ext4_set_aops(inode);
5181ac27a0ecSDave Kleikamp 
5182dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
518317335dccSDmitry Monakhov 	ext4_inode_resume_unlocked_dio(inode);
5184ac27a0ecSDave Kleikamp 
5185ac27a0ecSDave Kleikamp 	/* Finally we can mark the inode as dirty. */
5186ac27a0ecSDave Kleikamp 
51879924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
5188ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5189ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5190ac27a0ecSDave Kleikamp 
5191617ba13bSMingming Cao 	err = ext4_mark_inode_dirty(handle, inode);
51920390131bSFrank Mayhar 	ext4_handle_sync(handle);
5193617ba13bSMingming Cao 	ext4_journal_stop(handle);
5194617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
5195ac27a0ecSDave Kleikamp 
5196ac27a0ecSDave Kleikamp 	return err;
5197ac27a0ecSDave Kleikamp }
51982e9ee850SAneesh Kumar K.V 
51992e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
52002e9ee850SAneesh Kumar K.V {
52012e9ee850SAneesh Kumar K.V 	return !buffer_mapped(bh);
52022e9ee850SAneesh Kumar K.V }
52032e9ee850SAneesh Kumar K.V 
5204c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
52052e9ee850SAneesh Kumar K.V {
5206c2ec175cSNick Piggin 	struct page *page = vmf->page;
52072e9ee850SAneesh Kumar K.V 	loff_t size;
52082e9ee850SAneesh Kumar K.V 	unsigned long len;
52099ea7df53SJan Kara 	int ret;
52102e9ee850SAneesh Kumar K.V 	struct file *file = vma->vm_file;
5211496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
52122e9ee850SAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
52139ea7df53SJan Kara 	handle_t *handle;
52149ea7df53SJan Kara 	get_block_t *get_block;
52159ea7df53SJan Kara 	int retries = 0;
52162e9ee850SAneesh Kumar K.V 
52178e8ad8a5SJan Kara 	sb_start_pagefault(inode->i_sb);
5218041bbb6dSTheodore Ts'o 	file_update_time(vma->vm_file);
52199ea7df53SJan Kara 	/* Delalloc case is easy... */
52209ea7df53SJan Kara 	if (test_opt(inode->i_sb, DELALLOC) &&
52219ea7df53SJan Kara 	    !ext4_should_journal_data(inode) &&
52229ea7df53SJan Kara 	    !ext4_nonda_switch(inode->i_sb)) {
52239ea7df53SJan Kara 		do {
52249ea7df53SJan Kara 			ret = __block_page_mkwrite(vma, vmf,
52259ea7df53SJan Kara 						   ext4_da_get_block_prep);
52269ea7df53SJan Kara 		} while (ret == -ENOSPC &&
52279ea7df53SJan Kara 		       ext4_should_retry_alloc(inode->i_sb, &retries));
52289ea7df53SJan Kara 		goto out_ret;
52292e9ee850SAneesh Kumar K.V 	}
52300e499890SDarrick J. Wong 
52310e499890SDarrick J. Wong 	lock_page(page);
52329ea7df53SJan Kara 	size = i_size_read(inode);
52339ea7df53SJan Kara 	/* Page got truncated from under us? */
52349ea7df53SJan Kara 	if (page->mapping != mapping || page_offset(page) > size) {
52359ea7df53SJan Kara 		unlock_page(page);
52369ea7df53SJan Kara 		ret = VM_FAULT_NOPAGE;
52379ea7df53SJan Kara 		goto out;
52380e499890SDarrick J. Wong 	}
52392e9ee850SAneesh Kumar K.V 
52402e9ee850SAneesh Kumar K.V 	if (page->index == size >> PAGE_CACHE_SHIFT)
52412e9ee850SAneesh Kumar K.V 		len = size & ~PAGE_CACHE_MASK;
52422e9ee850SAneesh Kumar K.V 	else
52432e9ee850SAneesh Kumar K.V 		len = PAGE_CACHE_SIZE;
5244a827eaffSAneesh Kumar K.V 	/*
52459ea7df53SJan Kara 	 * Return if we have all the buffers mapped. This avoids the need to do
52469ea7df53SJan Kara 	 * journal_start/journal_stop which can block and take a long time
5247a827eaffSAneesh Kumar K.V 	 */
52482e9ee850SAneesh Kumar K.V 	if (page_has_buffers(page)) {
5249f19d5870STao Ma 		if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5250f19d5870STao Ma 					    0, len, NULL,
5251a827eaffSAneesh Kumar K.V 					    ext4_bh_unmapped)) {
52529ea7df53SJan Kara 			/* Wait so that we don't change page under IO */
52531d1d1a76SDarrick J. Wong 			wait_for_stable_page(page);
52549ea7df53SJan Kara 			ret = VM_FAULT_LOCKED;
52559ea7df53SJan Kara 			goto out;
52562e9ee850SAneesh Kumar K.V 		}
5257a827eaffSAneesh Kumar K.V 	}
5258a827eaffSAneesh Kumar K.V 	unlock_page(page);
52599ea7df53SJan Kara 	/* OK, we need to fill the hole... */
52609ea7df53SJan Kara 	if (ext4_should_dioread_nolock(inode))
52619ea7df53SJan Kara 		get_block = ext4_get_block_write;
52629ea7df53SJan Kara 	else
52639ea7df53SJan Kara 		get_block = ext4_get_block;
52649ea7df53SJan Kara retry_alloc:
52659924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
52669924a92aSTheodore Ts'o 				    ext4_writepage_trans_blocks(inode));
52679ea7df53SJan Kara 	if (IS_ERR(handle)) {
5268c2ec175cSNick Piggin 		ret = VM_FAULT_SIGBUS;
52699ea7df53SJan Kara 		goto out;
52709ea7df53SJan Kara 	}
52719ea7df53SJan Kara 	ret = __block_page_mkwrite(vma, vmf, get_block);
52729ea7df53SJan Kara 	if (!ret && ext4_should_journal_data(inode)) {
5273f19d5870STao Ma 		if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
52749ea7df53SJan Kara 			  PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
52759ea7df53SJan Kara 			unlock_page(page);
52769ea7df53SJan Kara 			ret = VM_FAULT_SIGBUS;
5277fcbb5515SYongqiang Yang 			ext4_journal_stop(handle);
52789ea7df53SJan Kara 			goto out;
52799ea7df53SJan Kara 		}
52809ea7df53SJan Kara 		ext4_set_inode_state(inode, EXT4_STATE_JDATA);
52819ea7df53SJan Kara 	}
52829ea7df53SJan Kara 	ext4_journal_stop(handle);
52839ea7df53SJan Kara 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
52849ea7df53SJan Kara 		goto retry_alloc;
52859ea7df53SJan Kara out_ret:
52869ea7df53SJan Kara 	ret = block_page_mkwrite_return(ret);
52879ea7df53SJan Kara out:
52888e8ad8a5SJan Kara 	sb_end_pagefault(inode->i_sb);
52892e9ee850SAneesh Kumar K.V 	return ret;
52902e9ee850SAneesh Kumar K.V }
5291