xref: /openbmc/linux/fs/ext4/inode.c (revision 819c4920b7e60ecfd6f0f61d890af4cdf3873d18)
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 
58814525f4SDarrick J. Wong 	csum_lo = 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)) {
62814525f4SDarrick J. Wong 		csum_hi = 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 
69814525f4SDarrick J. Wong 	raw->i_checksum_lo = 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))
72814525f4SDarrick J. Wong 		raw->i_checksum_hi = 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 
2132d859db3SJan Kara 			jbd2_log_start_commit(journal, commit_tid);
2142d859db3SJan Kara 			jbd2_log_wait_commit(journal, commit_tid);
2152d859db3SJan Kara 			filemap_write_and_wait(&inode->i_data);
2162d859db3SJan Kara 		}
2170930fcc1SAl Viro 		truncate_inode_pages(&inode->i_data, 0);
2181ada47d9STheodore Ts'o 		ext4_ioend_shutdown(inode);
2190930fcc1SAl Viro 		goto no_delete;
2200930fcc1SAl Viro 	}
2210930fcc1SAl Viro 
222907f4554SChristoph Hellwig 	if (!is_bad_inode(inode))
223871a2931SChristoph Hellwig 		dquot_initialize(inode);
224907f4554SChristoph Hellwig 
225678aaf48SJan Kara 	if (ext4_should_order_data(inode))
226678aaf48SJan Kara 		ext4_begin_ordered_truncate(inode, 0);
227ac27a0ecSDave Kleikamp 	truncate_inode_pages(&inode->i_data, 0);
2281ada47d9STheodore Ts'o 	ext4_ioend_shutdown(inode);
229ac27a0ecSDave Kleikamp 
230ac27a0ecSDave Kleikamp 	if (is_bad_inode(inode))
231ac27a0ecSDave Kleikamp 		goto no_delete;
232ac27a0ecSDave Kleikamp 
2338e8ad8a5SJan Kara 	/*
2348e8ad8a5SJan Kara 	 * Protect us against freezing - iput() caller didn't have to have any
2358e8ad8a5SJan Kara 	 * protection against it
2368e8ad8a5SJan Kara 	 */
2378e8ad8a5SJan Kara 	sb_start_intwrite(inode->i_sb);
2389924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
2399924a92aSTheodore Ts'o 				    ext4_blocks_for_truncate(inode)+3);
240ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
241bc965ab3STheodore Ts'o 		ext4_std_error(inode->i_sb, PTR_ERR(handle));
242ac27a0ecSDave Kleikamp 		/*
243ac27a0ecSDave Kleikamp 		 * If we're going to skip the normal cleanup, we still need to
244ac27a0ecSDave Kleikamp 		 * make sure that the in-core orphan linked list is properly
245ac27a0ecSDave Kleikamp 		 * cleaned up.
246ac27a0ecSDave Kleikamp 		 */
247617ba13bSMingming Cao 		ext4_orphan_del(NULL, inode);
2488e8ad8a5SJan Kara 		sb_end_intwrite(inode->i_sb);
249ac27a0ecSDave Kleikamp 		goto no_delete;
250ac27a0ecSDave Kleikamp 	}
251ac27a0ecSDave Kleikamp 
252ac27a0ecSDave Kleikamp 	if (IS_SYNC(inode))
2530390131bSFrank Mayhar 		ext4_handle_sync(handle);
254ac27a0ecSDave Kleikamp 	inode->i_size = 0;
255bc965ab3STheodore Ts'o 	err = ext4_mark_inode_dirty(handle, inode);
256bc965ab3STheodore Ts'o 	if (err) {
25712062dddSEric Sandeen 		ext4_warning(inode->i_sb,
258bc965ab3STheodore Ts'o 			     "couldn't mark inode dirty (err %d)", err);
259bc965ab3STheodore Ts'o 		goto stop_handle;
260bc965ab3STheodore Ts'o 	}
261ac27a0ecSDave Kleikamp 	if (inode->i_blocks)
262617ba13bSMingming Cao 		ext4_truncate(inode);
263bc965ab3STheodore Ts'o 
264bc965ab3STheodore Ts'o 	/*
265bc965ab3STheodore Ts'o 	 * ext4_ext_truncate() doesn't reserve any slop when it
266bc965ab3STheodore Ts'o 	 * restarts journal transactions; therefore there may not be
267bc965ab3STheodore Ts'o 	 * enough credits left in the handle to remove the inode from
268bc965ab3STheodore Ts'o 	 * the orphan list and set the dtime field.
269bc965ab3STheodore Ts'o 	 */
2700390131bSFrank Mayhar 	if (!ext4_handle_has_enough_credits(handle, 3)) {
271bc965ab3STheodore Ts'o 		err = ext4_journal_extend(handle, 3);
272bc965ab3STheodore Ts'o 		if (err > 0)
273bc965ab3STheodore Ts'o 			err = ext4_journal_restart(handle, 3);
274bc965ab3STheodore Ts'o 		if (err != 0) {
27512062dddSEric Sandeen 			ext4_warning(inode->i_sb,
276bc965ab3STheodore Ts'o 				     "couldn't extend journal (err %d)", err);
277bc965ab3STheodore Ts'o 		stop_handle:
278bc965ab3STheodore Ts'o 			ext4_journal_stop(handle);
27945388219STheodore Ts'o 			ext4_orphan_del(NULL, inode);
2808e8ad8a5SJan Kara 			sb_end_intwrite(inode->i_sb);
281bc965ab3STheodore Ts'o 			goto no_delete;
282bc965ab3STheodore Ts'o 		}
283bc965ab3STheodore Ts'o 	}
284bc965ab3STheodore Ts'o 
285ac27a0ecSDave Kleikamp 	/*
286617ba13bSMingming Cao 	 * Kill off the orphan record which ext4_truncate created.
287ac27a0ecSDave Kleikamp 	 * AKPM: I think this can be inside the above `if'.
288617ba13bSMingming Cao 	 * Note that ext4_orphan_del() has to be able to cope with the
289ac27a0ecSDave Kleikamp 	 * deletion of a non-existent orphan - this is because we don't
290617ba13bSMingming Cao 	 * know if ext4_truncate() actually created an orphan record.
291ac27a0ecSDave Kleikamp 	 * (Well, we could do this if we need to, but heck - it works)
292ac27a0ecSDave Kleikamp 	 */
293617ba13bSMingming Cao 	ext4_orphan_del(handle, inode);
294617ba13bSMingming Cao 	EXT4_I(inode)->i_dtime	= get_seconds();
295ac27a0ecSDave Kleikamp 
296ac27a0ecSDave Kleikamp 	/*
297ac27a0ecSDave Kleikamp 	 * One subtle ordering requirement: if anything has gone wrong
298ac27a0ecSDave Kleikamp 	 * (transaction abort, IO errors, whatever), then we can still
299ac27a0ecSDave Kleikamp 	 * do these next steps (the fs will already have been marked as
300ac27a0ecSDave Kleikamp 	 * having errors), but we can't free the inode if the mark_dirty
301ac27a0ecSDave Kleikamp 	 * fails.
302ac27a0ecSDave Kleikamp 	 */
303617ba13bSMingming Cao 	if (ext4_mark_inode_dirty(handle, inode))
304ac27a0ecSDave Kleikamp 		/* If that failed, just do the required in-core inode clear. */
3050930fcc1SAl Viro 		ext4_clear_inode(inode);
306ac27a0ecSDave Kleikamp 	else
307617ba13bSMingming Cao 		ext4_free_inode(handle, inode);
308617ba13bSMingming Cao 	ext4_journal_stop(handle);
3098e8ad8a5SJan Kara 	sb_end_intwrite(inode->i_sb);
310ac27a0ecSDave Kleikamp 	return;
311ac27a0ecSDave Kleikamp no_delete:
3120930fcc1SAl Viro 	ext4_clear_inode(inode);	/* We must guarantee clearing of inode... */
313ac27a0ecSDave Kleikamp }
314ac27a0ecSDave Kleikamp 
315a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
316a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode)
31760e58e0fSMingming Cao {
318a9e7f447SDmitry Monakhov 	return &EXT4_I(inode)->i_reserved_quota;
31960e58e0fSMingming Cao }
320a9e7f447SDmitry Monakhov #endif
3219d0be502STheodore Ts'o 
32212219aeaSAneesh Kumar K.V /*
32312219aeaSAneesh Kumar K.V  * Calculate the number of metadata blocks need to reserve
3249d0be502STheodore Ts'o  * to allocate a block located at @lblock
32512219aeaSAneesh Kumar K.V  */
32601f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
32712219aeaSAneesh Kumar K.V {
32812e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3299d0be502STheodore Ts'o 		return ext4_ext_calc_metadata_amount(inode, lblock);
33012219aeaSAneesh Kumar K.V 
3318bb2b247SAmir Goldstein 	return ext4_ind_calc_metadata_amount(inode, lblock);
33212219aeaSAneesh Kumar K.V }
33312219aeaSAneesh Kumar K.V 
3340637c6f4STheodore Ts'o /*
3350637c6f4STheodore Ts'o  * Called with i_data_sem down, which is important since we can call
3360637c6f4STheodore Ts'o  * ext4_discard_preallocations() from here.
3370637c6f4STheodore Ts'o  */
3385f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode,
3395f634d06SAneesh Kumar K.V 					int used, int quota_claim)
34012219aeaSAneesh Kumar K.V {
34112219aeaSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3420637c6f4STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
34312219aeaSAneesh Kumar K.V 
3440637c6f4STheodore Ts'o 	spin_lock(&ei->i_block_reservation_lock);
345d8990240SAditya Kali 	trace_ext4_da_update_reserve_space(inode, used, quota_claim);
3460637c6f4STheodore Ts'o 	if (unlikely(used > ei->i_reserved_data_blocks)) {
3478de5c325STheodore Ts'o 		ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
3481084f252STheodore Ts'o 			 "with only %d reserved data blocks",
3490637c6f4STheodore Ts'o 			 __func__, inode->i_ino, used,
3500637c6f4STheodore Ts'o 			 ei->i_reserved_data_blocks);
3510637c6f4STheodore Ts'o 		WARN_ON(1);
3520637c6f4STheodore Ts'o 		used = ei->i_reserved_data_blocks;
3536bc6e63fSAneesh Kumar K.V 	}
35412219aeaSAneesh Kumar K.V 
35597795d2aSBrian Foster 	if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
35601a523ebSTheodore Ts'o 		ext4_warning(inode->i_sb, "ino %lu, allocated %d "
35701a523ebSTheodore Ts'o 			"with only %d reserved metadata blocks "
35801a523ebSTheodore Ts'o 			"(releasing %d blocks with reserved %d data blocks)",
35997795d2aSBrian Foster 			inode->i_ino, ei->i_allocated_meta_blocks,
36001a523ebSTheodore Ts'o 			     ei->i_reserved_meta_blocks, used,
36101a523ebSTheodore Ts'o 			     ei->i_reserved_data_blocks);
36297795d2aSBrian Foster 		WARN_ON(1);
36397795d2aSBrian Foster 		ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
36497795d2aSBrian Foster 	}
36597795d2aSBrian Foster 
3660637c6f4STheodore Ts'o 	/* Update per-inode reservations */
3670637c6f4STheodore Ts'o 	ei->i_reserved_data_blocks -= used;
3680637c6f4STheodore Ts'o 	ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
36957042651STheodore Ts'o 	percpu_counter_sub(&sbi->s_dirtyclusters_counter,
37072b8ab9dSEric Sandeen 			   used + ei->i_allocated_meta_blocks);
3710637c6f4STheodore Ts'o 	ei->i_allocated_meta_blocks = 0;
3720637c6f4STheodore Ts'o 
3730637c6f4STheodore Ts'o 	if (ei->i_reserved_data_blocks == 0) {
3740637c6f4STheodore Ts'o 		/*
3750637c6f4STheodore Ts'o 		 * We can release all of the reserved metadata blocks
3760637c6f4STheodore Ts'o 		 * only when we have written all of the delayed
3770637c6f4STheodore Ts'o 		 * allocation blocks.
3780637c6f4STheodore Ts'o 		 */
37957042651STheodore Ts'o 		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
38072b8ab9dSEric Sandeen 				   ei->i_reserved_meta_blocks);
381ee5f4d9cSTheodore Ts'o 		ei->i_reserved_meta_blocks = 0;
3829d0be502STheodore Ts'o 		ei->i_da_metadata_calc_len = 0;
3830637c6f4STheodore Ts'o 	}
38412219aeaSAneesh Kumar K.V 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
38560e58e0fSMingming Cao 
38672b8ab9dSEric Sandeen 	/* Update quota subsystem for data blocks */
38772b8ab9dSEric Sandeen 	if (quota_claim)
3887b415bf6SAditya Kali 		dquot_claim_block(inode, EXT4_C2B(sbi, used));
38972b8ab9dSEric Sandeen 	else {
3905f634d06SAneesh Kumar K.V 		/*
3915f634d06SAneesh Kumar K.V 		 * We did fallocate with an offset that is already delayed
3925f634d06SAneesh Kumar K.V 		 * allocated. So on delayed allocated writeback we should
39372b8ab9dSEric Sandeen 		 * not re-claim the quota for fallocated blocks.
3945f634d06SAneesh Kumar K.V 		 */
3957b415bf6SAditya Kali 		dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
3965f634d06SAneesh Kumar K.V 	}
397d6014301SAneesh Kumar K.V 
398d6014301SAneesh Kumar K.V 	/*
399d6014301SAneesh Kumar K.V 	 * If we have done all the pending block allocations and if
400d6014301SAneesh Kumar K.V 	 * there aren't any writers on the inode, we can discard the
401d6014301SAneesh Kumar K.V 	 * inode's preallocations.
402d6014301SAneesh Kumar K.V 	 */
4030637c6f4STheodore Ts'o 	if ((ei->i_reserved_data_blocks == 0) &&
4040637c6f4STheodore Ts'o 	    (atomic_read(&inode->i_writecount) == 0))
405d6014301SAneesh Kumar K.V 		ext4_discard_preallocations(inode);
40612219aeaSAneesh Kumar K.V }
40712219aeaSAneesh Kumar K.V 
408e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func,
409c398eda0STheodore Ts'o 				unsigned int line,
41024676da4STheodore Ts'o 				struct ext4_map_blocks *map)
4116fd058f7STheodore Ts'o {
41224676da4STheodore Ts'o 	if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
41324676da4STheodore Ts'o 				   map->m_len)) {
414c398eda0STheodore Ts'o 		ext4_error_inode(inode, func, line, map->m_pblk,
415c398eda0STheodore Ts'o 				 "lblock %lu mapped to illegal pblock "
41624676da4STheodore Ts'o 				 "(length %d)", (unsigned long) map->m_lblk,
417c398eda0STheodore Ts'o 				 map->m_len);
4186fd058f7STheodore Ts'o 		return -EIO;
4196fd058f7STheodore Ts'o 	}
4206fd058f7STheodore Ts'o 	return 0;
4216fd058f7STheodore Ts'o }
4226fd058f7STheodore Ts'o 
423e29136f8STheodore Ts'o #define check_block_validity(inode, map)	\
424c398eda0STheodore Ts'o 	__check_block_validity((inode), __func__, __LINE__, (map))
425e29136f8STheodore Ts'o 
426f5ab0d1fSMingming Cao /*
4271f94533dSTheodore Ts'o  * Return the number of contiguous dirty pages in a given inode
4281f94533dSTheodore Ts'o  * starting at page frame idx.
42955138e0bSTheodore Ts'o  */
43055138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
43155138e0bSTheodore Ts'o 				    unsigned int max_pages)
43255138e0bSTheodore Ts'o {
43355138e0bSTheodore Ts'o 	struct address_space *mapping = inode->i_mapping;
43455138e0bSTheodore Ts'o 	pgoff_t	index;
43555138e0bSTheodore Ts'o 	struct pagevec pvec;
43655138e0bSTheodore Ts'o 	pgoff_t num = 0;
43755138e0bSTheodore Ts'o 	int i, nr_pages, done = 0;
43855138e0bSTheodore Ts'o 
43955138e0bSTheodore Ts'o 	if (max_pages == 0)
44055138e0bSTheodore Ts'o 		return 0;
44155138e0bSTheodore Ts'o 	pagevec_init(&pvec, 0);
44255138e0bSTheodore Ts'o 	while (!done) {
44355138e0bSTheodore Ts'o 		index = idx;
44455138e0bSTheodore Ts'o 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
44555138e0bSTheodore Ts'o 					      PAGECACHE_TAG_DIRTY,
44655138e0bSTheodore Ts'o 					      (pgoff_t)PAGEVEC_SIZE);
44755138e0bSTheodore Ts'o 		if (nr_pages == 0)
44855138e0bSTheodore Ts'o 			break;
44955138e0bSTheodore Ts'o 		for (i = 0; i < nr_pages; i++) {
45055138e0bSTheodore Ts'o 			struct page *page = pvec.pages[i];
45155138e0bSTheodore Ts'o 			struct buffer_head *bh, *head;
45255138e0bSTheodore Ts'o 
45355138e0bSTheodore Ts'o 			lock_page(page);
45455138e0bSTheodore Ts'o 			if (unlikely(page->mapping != mapping) ||
45555138e0bSTheodore Ts'o 			    !PageDirty(page) ||
45655138e0bSTheodore Ts'o 			    PageWriteback(page) ||
45755138e0bSTheodore Ts'o 			    page->index != idx) {
45855138e0bSTheodore Ts'o 				done = 1;
45955138e0bSTheodore Ts'o 				unlock_page(page);
46055138e0bSTheodore Ts'o 				break;
46155138e0bSTheodore Ts'o 			}
4621f94533dSTheodore Ts'o 			if (page_has_buffers(page)) {
4631f94533dSTheodore Ts'o 				bh = head = page_buffers(page);
46455138e0bSTheodore Ts'o 				do {
46555138e0bSTheodore Ts'o 					if (!buffer_delay(bh) &&
4661f94533dSTheodore Ts'o 					    !buffer_unwritten(bh))
46755138e0bSTheodore Ts'o 						done = 1;
4681f94533dSTheodore Ts'o 					bh = bh->b_this_page;
4691f94533dSTheodore Ts'o 				} while (!done && (bh != head));
47055138e0bSTheodore Ts'o 			}
47155138e0bSTheodore Ts'o 			unlock_page(page);
47255138e0bSTheodore Ts'o 			if (done)
47355138e0bSTheodore Ts'o 				break;
47455138e0bSTheodore Ts'o 			idx++;
47555138e0bSTheodore Ts'o 			num++;
476659c6009SEric Sandeen 			if (num >= max_pages) {
477659c6009SEric Sandeen 				done = 1;
47855138e0bSTheodore Ts'o 				break;
47955138e0bSTheodore Ts'o 			}
480659c6009SEric Sandeen 		}
48155138e0bSTheodore Ts'o 		pagevec_release(&pvec);
48255138e0bSTheodore Ts'o 	}
48355138e0bSTheodore Ts'o 	return num;
48455138e0bSTheodore Ts'o }
48555138e0bSTheodore Ts'o 
486921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
487921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle,
488921f266bSDmitry Monakhov 				       struct inode *inode,
489921f266bSDmitry Monakhov 				       struct ext4_map_blocks *es_map,
490921f266bSDmitry Monakhov 				       struct ext4_map_blocks *map,
491921f266bSDmitry Monakhov 				       int flags)
492921f266bSDmitry Monakhov {
493921f266bSDmitry Monakhov 	int retval;
494921f266bSDmitry Monakhov 
495921f266bSDmitry Monakhov 	map->m_flags = 0;
496921f266bSDmitry Monakhov 	/*
497921f266bSDmitry Monakhov 	 * There is a race window that the result is not the same.
498921f266bSDmitry Monakhov 	 * e.g. xfstests #223 when dioread_nolock enables.  The reason
499921f266bSDmitry Monakhov 	 * is that we lookup a block mapping in extent status tree with
500921f266bSDmitry Monakhov 	 * out taking i_data_sem.  So at the time the unwritten extent
501921f266bSDmitry Monakhov 	 * could be converted.
502921f266bSDmitry Monakhov 	 */
503921f266bSDmitry Monakhov 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
504921f266bSDmitry Monakhov 		down_read((&EXT4_I(inode)->i_data_sem));
505921f266bSDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
506921f266bSDmitry Monakhov 		retval = ext4_ext_map_blocks(handle, inode, map, flags &
507921f266bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
508921f266bSDmitry Monakhov 	} else {
509921f266bSDmitry Monakhov 		retval = ext4_ind_map_blocks(handle, inode, map, flags &
510921f266bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
511921f266bSDmitry Monakhov 	}
512921f266bSDmitry Monakhov 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
513921f266bSDmitry Monakhov 		up_read((&EXT4_I(inode)->i_data_sem));
514921f266bSDmitry Monakhov 	/*
515921f266bSDmitry Monakhov 	 * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag
516921f266bSDmitry Monakhov 	 * because it shouldn't be marked in es_map->m_flags.
517921f266bSDmitry Monakhov 	 */
518921f266bSDmitry Monakhov 	map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY);
519921f266bSDmitry Monakhov 
520921f266bSDmitry Monakhov 	/*
521921f266bSDmitry Monakhov 	 * We don't check m_len because extent will be collpased in status
522921f266bSDmitry Monakhov 	 * tree.  So the m_len might not equal.
523921f266bSDmitry Monakhov 	 */
524921f266bSDmitry Monakhov 	if (es_map->m_lblk != map->m_lblk ||
525921f266bSDmitry Monakhov 	    es_map->m_flags != map->m_flags ||
526921f266bSDmitry Monakhov 	    es_map->m_pblk != map->m_pblk) {
527921f266bSDmitry Monakhov 		printk("ES cache assertation failed for inode: %lu "
528921f266bSDmitry Monakhov 		       "es_cached ex [%d/%d/%llu/%x] != "
529921f266bSDmitry Monakhov 		       "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
530921f266bSDmitry Monakhov 		       inode->i_ino, es_map->m_lblk, es_map->m_len,
531921f266bSDmitry Monakhov 		       es_map->m_pblk, es_map->m_flags, map->m_lblk,
532921f266bSDmitry Monakhov 		       map->m_len, map->m_pblk, map->m_flags,
533921f266bSDmitry Monakhov 		       retval, flags);
534921f266bSDmitry Monakhov 	}
535921f266bSDmitry Monakhov }
536921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */
537921f266bSDmitry Monakhov 
53855138e0bSTheodore Ts'o /*
539e35fd660STheodore Ts'o  * The ext4_map_blocks() function tries to look up the requested blocks,
5402b2d6d01STheodore Ts'o  * and returns if the blocks are already mapped.
541f5ab0d1fSMingming Cao  *
542f5ab0d1fSMingming Cao  * Otherwise it takes the write lock of the i_data_sem and allocate blocks
543f5ab0d1fSMingming Cao  * and store the allocated blocks in the result buffer head and mark it
544f5ab0d1fSMingming Cao  * mapped.
545f5ab0d1fSMingming Cao  *
546e35fd660STheodore Ts'o  * If file type is extents based, it will call ext4_ext_map_blocks(),
547e35fd660STheodore Ts'o  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
548f5ab0d1fSMingming Cao  * based files
549f5ab0d1fSMingming Cao  *
550f5ab0d1fSMingming Cao  * On success, it returns the number of blocks being mapped or allocate.
551f5ab0d1fSMingming Cao  * if create==0 and the blocks are pre-allocated and uninitialized block,
552f5ab0d1fSMingming Cao  * the result buffer head is unmapped. If the create ==1, it will make sure
553f5ab0d1fSMingming Cao  * the buffer head is mapped.
554f5ab0d1fSMingming Cao  *
555f5ab0d1fSMingming Cao  * It returns 0 if plain look up failed (blocks have not been allocated), in
556df3ab170STao Ma  * that case, buffer head is unmapped
557f5ab0d1fSMingming Cao  *
558f5ab0d1fSMingming Cao  * It returns the error in case of allocation failure.
559f5ab0d1fSMingming Cao  */
560e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode,
561e35fd660STheodore Ts'o 		    struct ext4_map_blocks *map, int flags)
5620e855ac8SAneesh Kumar K.V {
563d100eef2SZheng Liu 	struct extent_status es;
5640e855ac8SAneesh Kumar K.V 	int retval;
565921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
566921f266bSDmitry Monakhov 	struct ext4_map_blocks orig_map;
567921f266bSDmitry Monakhov 
568921f266bSDmitry Monakhov 	memcpy(&orig_map, map, sizeof(*map));
569921f266bSDmitry Monakhov #endif
570f5ab0d1fSMingming Cao 
571e35fd660STheodore Ts'o 	map->m_flags = 0;
572e35fd660STheodore Ts'o 	ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
573e35fd660STheodore Ts'o 		  "logical block %lu\n", inode->i_ino, flags, map->m_len,
574e35fd660STheodore Ts'o 		  (unsigned long) map->m_lblk);
575d100eef2SZheng Liu 
576d100eef2SZheng Liu 	/* Lookup extent status tree firstly */
577d100eef2SZheng Liu 	if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
578d100eef2SZheng Liu 		if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
579d100eef2SZheng Liu 			map->m_pblk = ext4_es_pblock(&es) +
580d100eef2SZheng Liu 					map->m_lblk - es.es_lblk;
581d100eef2SZheng Liu 			map->m_flags |= ext4_es_is_written(&es) ?
582d100eef2SZheng Liu 					EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
583d100eef2SZheng Liu 			retval = es.es_len - (map->m_lblk - es.es_lblk);
584d100eef2SZheng Liu 			if (retval > map->m_len)
585d100eef2SZheng Liu 				retval = map->m_len;
586d100eef2SZheng Liu 			map->m_len = retval;
587d100eef2SZheng Liu 		} else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
588d100eef2SZheng Liu 			retval = 0;
589d100eef2SZheng Liu 		} else {
590d100eef2SZheng Liu 			BUG_ON(1);
591d100eef2SZheng Liu 		}
592921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
593921f266bSDmitry Monakhov 		ext4_map_blocks_es_recheck(handle, inode, map,
594921f266bSDmitry Monakhov 					   &orig_map, flags);
595921f266bSDmitry Monakhov #endif
596d100eef2SZheng Liu 		goto found;
597d100eef2SZheng Liu 	}
598d100eef2SZheng Liu 
5994df3d265SAneesh Kumar K.V 	/*
600b920c755STheodore Ts'o 	 * Try to see if we can get the block without requesting a new
601b920c755STheodore Ts'o 	 * file system block.
6024df3d265SAneesh Kumar K.V 	 */
603729f52c6SZheng Liu 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
6040e855ac8SAneesh Kumar K.V 		down_read((&EXT4_I(inode)->i_data_sem));
60512e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
606a4e5d88bSDmitry Monakhov 		retval = ext4_ext_map_blocks(handle, inode, map, flags &
607a4e5d88bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
6084df3d265SAneesh Kumar K.V 	} else {
609a4e5d88bSDmitry Monakhov 		retval = ext4_ind_map_blocks(handle, inode, map, flags &
610a4e5d88bSDmitry Monakhov 					     EXT4_GET_BLOCKS_KEEP_SIZE);
6110e855ac8SAneesh Kumar K.V 	}
612f7fec032SZheng Liu 	if (retval > 0) {
613f7fec032SZheng Liu 		int ret;
614f7fec032SZheng Liu 		unsigned long long status;
615f7fec032SZheng Liu 
616921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
617921f266bSDmitry Monakhov 		if (retval != map->m_len) {
618921f266bSDmitry Monakhov 			printk("ES len assertation failed for inode: %lu "
619921f266bSDmitry Monakhov 			       "retval %d != map->m_len %d "
620921f266bSDmitry Monakhov 			       "in %s (lookup)\n", inode->i_ino, retval,
621921f266bSDmitry Monakhov 			       map->m_len, __func__);
622921f266bSDmitry Monakhov 		}
623921f266bSDmitry Monakhov #endif
624921f266bSDmitry Monakhov 
625f7fec032SZheng Liu 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
626f7fec032SZheng Liu 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
627f7fec032SZheng Liu 		if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
628f7fec032SZheng Liu 		    ext4_find_delalloc_range(inode, map->m_lblk,
629f7fec032SZheng Liu 					     map->m_lblk + map->m_len - 1))
630f7fec032SZheng Liu 			status |= EXTENT_STATUS_DELAYED;
631f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk,
632f7fec032SZheng Liu 					    map->m_len, map->m_pblk, status);
633f7fec032SZheng Liu 		if (ret < 0)
634f7fec032SZheng Liu 			retval = ret;
635f7fec032SZheng Liu 	}
636729f52c6SZheng Liu 	if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
6374df3d265SAneesh Kumar K.V 		up_read((&EXT4_I(inode)->i_data_sem));
638f5ab0d1fSMingming Cao 
639d100eef2SZheng Liu found:
640e35fd660STheodore Ts'o 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
641f7fec032SZheng Liu 		int ret = check_block_validity(inode, map);
6426fd058f7STheodore Ts'o 		if (ret != 0)
6436fd058f7STheodore Ts'o 			return ret;
6446fd058f7STheodore Ts'o 	}
6456fd058f7STheodore Ts'o 
646f5ab0d1fSMingming Cao 	/* If it is only a block(s) look up */
647c2177057STheodore Ts'o 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
6484df3d265SAneesh Kumar K.V 		return retval;
6494df3d265SAneesh Kumar K.V 
6504df3d265SAneesh Kumar K.V 	/*
651f5ab0d1fSMingming Cao 	 * Returns if the blocks have already allocated
652f5ab0d1fSMingming Cao 	 *
653f5ab0d1fSMingming Cao 	 * Note that if blocks have been preallocated
654df3ab170STao Ma 	 * ext4_ext_get_block() returns the create = 0
655f5ab0d1fSMingming Cao 	 * with buffer head unmapped.
656f5ab0d1fSMingming Cao 	 */
657e35fd660STheodore Ts'o 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
658f5ab0d1fSMingming Cao 		return retval;
659f5ab0d1fSMingming Cao 
660f5ab0d1fSMingming Cao 	/*
661a25a4e1aSZheng Liu 	 * Here we clear m_flags because after allocating an new extent,
662a25a4e1aSZheng Liu 	 * it will be set again.
6632a8964d6SAneesh Kumar K.V 	 */
664a25a4e1aSZheng Liu 	map->m_flags &= ~EXT4_MAP_FLAGS;
6652a8964d6SAneesh Kumar K.V 
6662a8964d6SAneesh Kumar K.V 	/*
667f5ab0d1fSMingming Cao 	 * New blocks allocate and/or writing to uninitialized extent
668f5ab0d1fSMingming Cao 	 * will possibly result in updating i_data, so we take
669f5ab0d1fSMingming Cao 	 * the write lock of i_data_sem, and call get_blocks()
670f5ab0d1fSMingming Cao 	 * with create == 1 flag.
6714df3d265SAneesh Kumar K.V 	 */
6724df3d265SAneesh Kumar K.V 	down_write((&EXT4_I(inode)->i_data_sem));
673d2a17637SMingming Cao 
674d2a17637SMingming Cao 	/*
675d2a17637SMingming Cao 	 * if the caller is from delayed allocation writeout path
676d2a17637SMingming Cao 	 * we have already reserved fs blocks for allocation
677d2a17637SMingming Cao 	 * let the underlying get_block() function know to
678d2a17637SMingming Cao 	 * avoid double accounting
679d2a17637SMingming Cao 	 */
680c2177057STheodore Ts'o 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
681f2321097STheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
6824df3d265SAneesh Kumar K.V 	/*
6834df3d265SAneesh Kumar K.V 	 * We need to check for EXT4 here because migrate
6844df3d265SAneesh Kumar K.V 	 * could have changed the inode type in between
6854df3d265SAneesh Kumar K.V 	 */
68612e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
687e35fd660STheodore Ts'o 		retval = ext4_ext_map_blocks(handle, inode, map, flags);
6880e855ac8SAneesh Kumar K.V 	} else {
689e35fd660STheodore Ts'o 		retval = ext4_ind_map_blocks(handle, inode, map, flags);
690267e4db9SAneesh Kumar K.V 
691e35fd660STheodore Ts'o 		if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
692267e4db9SAneesh Kumar K.V 			/*
693267e4db9SAneesh Kumar K.V 			 * We allocated new blocks which will result in
694267e4db9SAneesh Kumar K.V 			 * i_data's format changing.  Force the migrate
695267e4db9SAneesh Kumar K.V 			 * to fail by clearing migrate flags
696267e4db9SAneesh Kumar K.V 			 */
69719f5fb7aSTheodore Ts'o 			ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
698267e4db9SAneesh Kumar K.V 		}
6992ac3b6e0STheodore Ts'o 
700d2a17637SMingming Cao 		/*
7012ac3b6e0STheodore Ts'o 		 * Update reserved blocks/metadata blocks after successful
7025f634d06SAneesh Kumar K.V 		 * block allocation which had been deferred till now. We don't
7035f634d06SAneesh Kumar K.V 		 * support fallocate for non extent files. So we can update
7045f634d06SAneesh Kumar K.V 		 * reserve space here.
705d2a17637SMingming Cao 		 */
7065f634d06SAneesh Kumar K.V 		if ((retval > 0) &&
7071296cc85SAneesh Kumar K.V 			(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
7085f634d06SAneesh Kumar K.V 			ext4_da_update_reserve_space(inode, retval, 1);
7095f634d06SAneesh Kumar K.V 	}
710f7fec032SZheng Liu 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
711f2321097STheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
712d2a17637SMingming Cao 
713f7fec032SZheng Liu 	if (retval > 0) {
71451865fdaSZheng Liu 		int ret;
715f7fec032SZheng Liu 		unsigned long long status;
716f7fec032SZheng Liu 
717921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
718921f266bSDmitry Monakhov 		if (retval != map->m_len) {
719921f266bSDmitry Monakhov 			printk("ES len assertation failed for inode: %lu "
720921f266bSDmitry Monakhov 			       "retval %d != map->m_len %d "
721921f266bSDmitry Monakhov 			       "in %s (allocation)\n", inode->i_ino, retval,
722921f266bSDmitry Monakhov 			       map->m_len, __func__);
723921f266bSDmitry Monakhov 		}
724921f266bSDmitry Monakhov #endif
725921f266bSDmitry Monakhov 
726adb23551SZheng Liu 		/*
727adb23551SZheng Liu 		 * If the extent has been zeroed out, we don't need to update
728adb23551SZheng Liu 		 * extent status tree.
729adb23551SZheng Liu 		 */
730adb23551SZheng Liu 		if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
731adb23551SZheng Liu 		    ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
732adb23551SZheng Liu 			if (ext4_es_is_written(&es))
733adb23551SZheng Liu 				goto has_zeroout;
734adb23551SZheng Liu 		}
735f7fec032SZheng Liu 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
736f7fec032SZheng Liu 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
737f7fec032SZheng Liu 		if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
738f7fec032SZheng Liu 		    ext4_find_delalloc_range(inode, map->m_lblk,
739f7fec032SZheng Liu 					     map->m_lblk + map->m_len - 1))
740f7fec032SZheng Liu 			status |= EXTENT_STATUS_DELAYED;
741f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
742f7fec032SZheng Liu 					    map->m_pblk, status);
74351865fdaSZheng Liu 		if (ret < 0)
74451865fdaSZheng Liu 			retval = ret;
74551865fdaSZheng Liu 	}
7465356f261SAditya Kali 
747adb23551SZheng Liu has_zeroout:
7480e855ac8SAneesh Kumar K.V 	up_write((&EXT4_I(inode)->i_data_sem));
749e35fd660STheodore Ts'o 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
750e29136f8STheodore Ts'o 		int ret = check_block_validity(inode, map);
7516fd058f7STheodore Ts'o 		if (ret != 0)
7526fd058f7STheodore Ts'o 			return ret;
7536fd058f7STheodore Ts'o 	}
7540e855ac8SAneesh Kumar K.V 	return retval;
7550e855ac8SAneesh Kumar K.V }
7560e855ac8SAneesh Kumar K.V 
757f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */
758f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096
759f3bd1f3fSMingming Cao 
7602ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock,
7612ed88685STheodore Ts'o 			   struct buffer_head *bh, int flags)
762ac27a0ecSDave Kleikamp {
7633e4fdaf8SDmitriy Monakhov 	handle_t *handle = ext4_journal_current_handle();
7642ed88685STheodore Ts'o 	struct ext4_map_blocks map;
7657fb5409dSJan Kara 	int ret = 0, started = 0;
766f3bd1f3fSMingming Cao 	int dio_credits;
767ac27a0ecSDave Kleikamp 
76846c7f254STao Ma 	if (ext4_has_inline_data(inode))
76946c7f254STao Ma 		return -ERANGE;
77046c7f254STao Ma 
7712ed88685STheodore Ts'o 	map.m_lblk = iblock;
7722ed88685STheodore Ts'o 	map.m_len = bh->b_size >> inode->i_blkbits;
7732ed88685STheodore Ts'o 
7748b0f165fSAnatol Pomozov 	if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
7757fb5409dSJan Kara 		/* Direct IO write... */
7762ed88685STheodore Ts'o 		if (map.m_len > DIO_MAX_BLOCKS)
7772ed88685STheodore Ts'o 			map.m_len = DIO_MAX_BLOCKS;
7782ed88685STheodore Ts'o 		dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
7799924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
7809924a92aSTheodore Ts'o 					    dio_credits);
7817fb5409dSJan Kara 		if (IS_ERR(handle)) {
782ac27a0ecSDave Kleikamp 			ret = PTR_ERR(handle);
7832ed88685STheodore Ts'o 			return ret;
7847fb5409dSJan Kara 		}
7857fb5409dSJan Kara 		started = 1;
786ac27a0ecSDave Kleikamp 	}
787ac27a0ecSDave Kleikamp 
7882ed88685STheodore Ts'o 	ret = ext4_map_blocks(handle, inode, &map, flags);
789ac27a0ecSDave Kleikamp 	if (ret > 0) {
7902ed88685STheodore Ts'o 		map_bh(bh, inode->i_sb, map.m_pblk);
7912ed88685STheodore Ts'o 		bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
7922ed88685STheodore Ts'o 		bh->b_size = inode->i_sb->s_blocksize * map.m_len;
793ac27a0ecSDave Kleikamp 		ret = 0;
794ac27a0ecSDave Kleikamp 	}
7957fb5409dSJan Kara 	if (started)
7967fb5409dSJan Kara 		ext4_journal_stop(handle);
797ac27a0ecSDave Kleikamp 	return ret;
798ac27a0ecSDave Kleikamp }
799ac27a0ecSDave Kleikamp 
8002ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock,
8012ed88685STheodore Ts'o 		   struct buffer_head *bh, int create)
8022ed88685STheodore Ts'o {
8032ed88685STheodore Ts'o 	return _ext4_get_block(inode, iblock, bh,
8042ed88685STheodore Ts'o 			       create ? EXT4_GET_BLOCKS_CREATE : 0);
8052ed88685STheodore Ts'o }
8062ed88685STheodore Ts'o 
807ac27a0ecSDave Kleikamp /*
808ac27a0ecSDave Kleikamp  * `handle' can be NULL if create is zero
809ac27a0ecSDave Kleikamp  */
810617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
811725d26d3SAneesh Kumar K.V 				ext4_lblk_t block, int create, int *errp)
812ac27a0ecSDave Kleikamp {
8132ed88685STheodore Ts'o 	struct ext4_map_blocks map;
8142ed88685STheodore Ts'o 	struct buffer_head *bh;
815ac27a0ecSDave Kleikamp 	int fatal = 0, err;
816ac27a0ecSDave Kleikamp 
817ac27a0ecSDave Kleikamp 	J_ASSERT(handle != NULL || create == 0);
818ac27a0ecSDave Kleikamp 
8192ed88685STheodore Ts'o 	map.m_lblk = block;
8202ed88685STheodore Ts'o 	map.m_len = 1;
8212ed88685STheodore Ts'o 	err = ext4_map_blocks(handle, inode, &map,
8222ed88685STheodore Ts'o 			      create ? EXT4_GET_BLOCKS_CREATE : 0);
8232ed88685STheodore Ts'o 
82490b0a973SCarlos Maiolino 	/* ensure we send some value back into *errp */
82590b0a973SCarlos Maiolino 	*errp = 0;
82690b0a973SCarlos Maiolino 
8270f70b406STheodore Ts'o 	if (create && err == 0)
8280f70b406STheodore Ts'o 		err = -ENOSPC;	/* should never happen */
8292ed88685STheodore Ts'o 	if (err < 0)
830ac27a0ecSDave Kleikamp 		*errp = err;
8312ed88685STheodore Ts'o 	if (err <= 0)
8322ed88685STheodore Ts'o 		return NULL;
8332ed88685STheodore Ts'o 
8342ed88685STheodore Ts'o 	bh = sb_getblk(inode->i_sb, map.m_pblk);
835aebf0243SWang Shilong 	if (unlikely(!bh)) {
836860d21e2STheodore Ts'o 		*errp = -ENOMEM;
8372ed88685STheodore Ts'o 		return NULL;
838ac27a0ecSDave Kleikamp 	}
8392ed88685STheodore Ts'o 	if (map.m_flags & EXT4_MAP_NEW) {
840ac27a0ecSDave Kleikamp 		J_ASSERT(create != 0);
841ac39849dSAneesh Kumar K.V 		J_ASSERT(handle != NULL);
842ac27a0ecSDave Kleikamp 
843ac27a0ecSDave Kleikamp 		/*
844ac27a0ecSDave Kleikamp 		 * Now that we do not always journal data, we should
845ac27a0ecSDave Kleikamp 		 * keep in mind whether this should always journal the
846ac27a0ecSDave Kleikamp 		 * new buffer as metadata.  For now, regular file
847617ba13bSMingming Cao 		 * writes use ext4_get_block instead, so it's not a
848ac27a0ecSDave Kleikamp 		 * problem.
849ac27a0ecSDave Kleikamp 		 */
850ac27a0ecSDave Kleikamp 		lock_buffer(bh);
851ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "call get_create_access");
852617ba13bSMingming Cao 		fatal = ext4_journal_get_create_access(handle, bh);
853ac27a0ecSDave Kleikamp 		if (!fatal && !buffer_uptodate(bh)) {
854ac27a0ecSDave Kleikamp 			memset(bh->b_data, 0, inode->i_sb->s_blocksize);
855ac27a0ecSDave Kleikamp 			set_buffer_uptodate(bh);
856ac27a0ecSDave Kleikamp 		}
857ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
8580390131bSFrank Mayhar 		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
8590390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, inode, bh);
860ac27a0ecSDave Kleikamp 		if (!fatal)
861ac27a0ecSDave Kleikamp 			fatal = err;
862ac27a0ecSDave Kleikamp 	} else {
863ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "not a new buffer");
864ac27a0ecSDave Kleikamp 	}
865ac27a0ecSDave Kleikamp 	if (fatal) {
866ac27a0ecSDave Kleikamp 		*errp = fatal;
867ac27a0ecSDave Kleikamp 		brelse(bh);
868ac27a0ecSDave Kleikamp 		bh = NULL;
869ac27a0ecSDave Kleikamp 	}
870ac27a0ecSDave Kleikamp 	return bh;
871ac27a0ecSDave Kleikamp }
872ac27a0ecSDave Kleikamp 
873617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
874725d26d3SAneesh Kumar K.V 			       ext4_lblk_t block, int create, int *err)
875ac27a0ecSDave Kleikamp {
876ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
877ac27a0ecSDave Kleikamp 
878617ba13bSMingming Cao 	bh = ext4_getblk(handle, inode, block, create, err);
879ac27a0ecSDave Kleikamp 	if (!bh)
880ac27a0ecSDave Kleikamp 		return bh;
881ac27a0ecSDave Kleikamp 	if (buffer_uptodate(bh))
882ac27a0ecSDave Kleikamp 		return bh;
88365299a3bSChristoph Hellwig 	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
884ac27a0ecSDave Kleikamp 	wait_on_buffer(bh);
885ac27a0ecSDave Kleikamp 	if (buffer_uptodate(bh))
886ac27a0ecSDave Kleikamp 		return bh;
887ac27a0ecSDave Kleikamp 	put_bh(bh);
888ac27a0ecSDave Kleikamp 	*err = -EIO;
889ac27a0ecSDave Kleikamp 	return NULL;
890ac27a0ecSDave Kleikamp }
891ac27a0ecSDave Kleikamp 
892f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle,
893ac27a0ecSDave Kleikamp 			   struct buffer_head *head,
894ac27a0ecSDave Kleikamp 			   unsigned from,
895ac27a0ecSDave Kleikamp 			   unsigned to,
896ac27a0ecSDave Kleikamp 			   int *partial,
897ac27a0ecSDave Kleikamp 			   int (*fn)(handle_t *handle,
898ac27a0ecSDave Kleikamp 				     struct buffer_head *bh))
899ac27a0ecSDave Kleikamp {
900ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
901ac27a0ecSDave Kleikamp 	unsigned block_start, block_end;
902ac27a0ecSDave Kleikamp 	unsigned blocksize = head->b_size;
903ac27a0ecSDave Kleikamp 	int err, ret = 0;
904ac27a0ecSDave Kleikamp 	struct buffer_head *next;
905ac27a0ecSDave Kleikamp 
906ac27a0ecSDave Kleikamp 	for (bh = head, block_start = 0;
907ac27a0ecSDave Kleikamp 	     ret == 0 && (bh != head || !block_start);
908de9a55b8STheodore Ts'o 	     block_start = block_end, bh = next) {
909ac27a0ecSDave Kleikamp 		next = bh->b_this_page;
910ac27a0ecSDave Kleikamp 		block_end = block_start + blocksize;
911ac27a0ecSDave Kleikamp 		if (block_end <= from || block_start >= to) {
912ac27a0ecSDave Kleikamp 			if (partial && !buffer_uptodate(bh))
913ac27a0ecSDave Kleikamp 				*partial = 1;
914ac27a0ecSDave Kleikamp 			continue;
915ac27a0ecSDave Kleikamp 		}
916ac27a0ecSDave Kleikamp 		err = (*fn)(handle, bh);
917ac27a0ecSDave Kleikamp 		if (!ret)
918ac27a0ecSDave Kleikamp 			ret = err;
919ac27a0ecSDave Kleikamp 	}
920ac27a0ecSDave Kleikamp 	return ret;
921ac27a0ecSDave Kleikamp }
922ac27a0ecSDave Kleikamp 
923ac27a0ecSDave Kleikamp /*
924ac27a0ecSDave Kleikamp  * To preserve ordering, it is essential that the hole instantiation and
925ac27a0ecSDave Kleikamp  * the data write be encapsulated in a single transaction.  We cannot
926617ba13bSMingming Cao  * close off a transaction and start a new one between the ext4_get_block()
927dab291afSMingming Cao  * and the commit_write().  So doing the jbd2_journal_start at the start of
928ac27a0ecSDave Kleikamp  * prepare_write() is the right place.
929ac27a0ecSDave Kleikamp  *
93036ade451SJan Kara  * Also, this function can nest inside ext4_writepage().  In that case, we
93136ade451SJan Kara  * *know* that ext4_writepage() has generated enough buffer credits to do the
93236ade451SJan Kara  * whole page.  So we won't block on the journal in that case, which is good,
93336ade451SJan Kara  * because the caller may be PF_MEMALLOC.
934ac27a0ecSDave Kleikamp  *
935617ba13bSMingming Cao  * By accident, ext4 can be reentered when a transaction is open via
936ac27a0ecSDave Kleikamp  * quota file writes.  If we were to commit the transaction while thus
937ac27a0ecSDave Kleikamp  * reentered, there can be a deadlock - we would be holding a quota
938ac27a0ecSDave Kleikamp  * lock, and the commit would never complete if another thread had a
939ac27a0ecSDave Kleikamp  * transaction open and was blocking on the quota lock - a ranking
940ac27a0ecSDave Kleikamp  * violation.
941ac27a0ecSDave Kleikamp  *
942dab291afSMingming Cao  * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
943ac27a0ecSDave Kleikamp  * will _not_ run commit under these circumstances because handle->h_ref
944ac27a0ecSDave Kleikamp  * is elevated.  We'll still have enough credits for the tiny quotafile
945ac27a0ecSDave Kleikamp  * write.
946ac27a0ecSDave Kleikamp  */
947f19d5870STao Ma int do_journal_get_write_access(handle_t *handle,
948ac27a0ecSDave Kleikamp 				struct buffer_head *bh)
949ac27a0ecSDave Kleikamp {
95056d35a4cSJan Kara 	int dirty = buffer_dirty(bh);
95156d35a4cSJan Kara 	int ret;
95256d35a4cSJan Kara 
953ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh) || buffer_freed(bh))
954ac27a0ecSDave Kleikamp 		return 0;
95556d35a4cSJan Kara 	/*
956ebdec241SChristoph Hellwig 	 * __block_write_begin() could have dirtied some buffers. Clean
95756d35a4cSJan Kara 	 * the dirty bit as jbd2_journal_get_write_access() could complain
95856d35a4cSJan Kara 	 * otherwise about fs integrity issues. Setting of the dirty bit
959ebdec241SChristoph Hellwig 	 * by __block_write_begin() isn't a real problem here as we clear
96056d35a4cSJan Kara 	 * the bit before releasing a page lock and thus writeback cannot
96156d35a4cSJan Kara 	 * ever write the buffer.
96256d35a4cSJan Kara 	 */
96356d35a4cSJan Kara 	if (dirty)
96456d35a4cSJan Kara 		clear_buffer_dirty(bh);
96556d35a4cSJan Kara 	ret = ext4_journal_get_write_access(handle, bh);
96656d35a4cSJan Kara 	if (!ret && dirty)
96756d35a4cSJan Kara 		ret = ext4_handle_dirty_metadata(handle, NULL, bh);
96856d35a4cSJan Kara 	return ret;
969ac27a0ecSDave Kleikamp }
970ac27a0ecSDave Kleikamp 
9718b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
9728b0f165fSAnatol Pomozov 		   struct buffer_head *bh_result, int create);
973bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping,
974bfc1af65SNick Piggin 			    loff_t pos, unsigned len, unsigned flags,
975bfc1af65SNick Piggin 			    struct page **pagep, void **fsdata)
976ac27a0ecSDave Kleikamp {
977bfc1af65SNick Piggin 	struct inode *inode = mapping->host;
9781938a150SAneesh Kumar K.V 	int ret, needed_blocks;
979ac27a0ecSDave Kleikamp 	handle_t *handle;
980ac27a0ecSDave Kleikamp 	int retries = 0;
981bfc1af65SNick Piggin 	struct page *page;
982bfc1af65SNick Piggin 	pgoff_t index;
983bfc1af65SNick Piggin 	unsigned from, to;
984bfc1af65SNick Piggin 
9859bffad1eSTheodore Ts'o 	trace_ext4_write_begin(inode, pos, len, flags);
9861938a150SAneesh Kumar K.V 	/*
9871938a150SAneesh Kumar K.V 	 * Reserve one block more for addition to orphan list in case
9881938a150SAneesh Kumar K.V 	 * we allocate blocks but write fails for some reason
9891938a150SAneesh Kumar K.V 	 */
9901938a150SAneesh Kumar K.V 	needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
991bfc1af65SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
992bfc1af65SNick Piggin 	from = pos & (PAGE_CACHE_SIZE - 1);
993bfc1af65SNick Piggin 	to = from + len;
994ac27a0ecSDave Kleikamp 
995f19d5870STao Ma 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
996f19d5870STao Ma 		ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
997f19d5870STao Ma 						    flags, pagep);
998f19d5870STao Ma 		if (ret < 0)
99947564bfbSTheodore Ts'o 			return ret;
100047564bfbSTheodore Ts'o 		if (ret == 1)
100147564bfbSTheodore Ts'o 			return 0;
1002f19d5870STao Ma 	}
1003f19d5870STao Ma 
100447564bfbSTheodore Ts'o 	/*
100547564bfbSTheodore Ts'o 	 * grab_cache_page_write_begin() can take a long time if the
100647564bfbSTheodore Ts'o 	 * system is thrashing due to memory pressure, or if the page
100747564bfbSTheodore Ts'o 	 * is being written back.  So grab it first before we start
100847564bfbSTheodore Ts'o 	 * the transaction handle.  This also allows us to allocate
100947564bfbSTheodore Ts'o 	 * the page (if needed) without using GFP_NOFS.
101047564bfbSTheodore Ts'o 	 */
101147564bfbSTheodore Ts'o retry_grab:
101254566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
101347564bfbSTheodore Ts'o 	if (!page)
101447564bfbSTheodore Ts'o 		return -ENOMEM;
101547564bfbSTheodore Ts'o 	unlock_page(page);
101647564bfbSTheodore Ts'o 
101747564bfbSTheodore Ts'o retry_journal:
10189924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
1019ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
102047564bfbSTheodore Ts'o 		page_cache_release(page);
102147564bfbSTheodore Ts'o 		return PTR_ERR(handle);
1022cf108bcaSJan Kara 	}
1023f19d5870STao Ma 
102447564bfbSTheodore Ts'o 	lock_page(page);
102547564bfbSTheodore Ts'o 	if (page->mapping != mapping) {
102647564bfbSTheodore Ts'o 		/* The page got truncated from under us */
102747564bfbSTheodore Ts'o 		unlock_page(page);
102847564bfbSTheodore Ts'o 		page_cache_release(page);
1029cf108bcaSJan Kara 		ext4_journal_stop(handle);
103047564bfbSTheodore Ts'o 		goto retry_grab;
1031cf108bcaSJan Kara 	}
103247564bfbSTheodore Ts'o 	wait_on_page_writeback(page);
1033cf108bcaSJan Kara 
1034744692dcSJiaying Zhang 	if (ext4_should_dioread_nolock(inode))
10356e1db88dSChristoph Hellwig 		ret = __block_write_begin(page, pos, len, ext4_get_block_write);
1036744692dcSJiaying Zhang 	else
10376e1db88dSChristoph Hellwig 		ret = __block_write_begin(page, pos, len, ext4_get_block);
1038bfc1af65SNick Piggin 
1039bfc1af65SNick Piggin 	if (!ret && ext4_should_journal_data(inode)) {
1040f19d5870STao Ma 		ret = ext4_walk_page_buffers(handle, page_buffers(page),
1041f19d5870STao Ma 					     from, to, NULL,
1042f19d5870STao Ma 					     do_journal_get_write_access);
1043b46be050SAndrey Savochkin 	}
1044bfc1af65SNick Piggin 
1045bfc1af65SNick Piggin 	if (ret) {
1046bfc1af65SNick Piggin 		unlock_page(page);
1047ae4d5372SAneesh Kumar K.V 		/*
10486e1db88dSChristoph Hellwig 		 * __block_write_begin may have instantiated a few blocks
1049ae4d5372SAneesh Kumar K.V 		 * outside i_size.  Trim these off again. Don't need
1050ae4d5372SAneesh Kumar K.V 		 * i_size_read because we hold i_mutex.
10511938a150SAneesh Kumar K.V 		 *
10521938a150SAneesh Kumar K.V 		 * Add inode to orphan list in case we crash before
10531938a150SAneesh Kumar K.V 		 * truncate finishes
1054ae4d5372SAneesh Kumar K.V 		 */
1055ffacfa7aSJan Kara 		if (pos + len > inode->i_size && ext4_can_truncate(inode))
10561938a150SAneesh Kumar K.V 			ext4_orphan_add(handle, inode);
10571938a150SAneesh Kumar K.V 
10581938a150SAneesh Kumar K.V 		ext4_journal_stop(handle);
10591938a150SAneesh Kumar K.V 		if (pos + len > inode->i_size) {
1060b9a4207dSJan Kara 			ext4_truncate_failed_write(inode);
10611938a150SAneesh Kumar K.V 			/*
1062ffacfa7aSJan Kara 			 * If truncate failed early the inode might
10631938a150SAneesh Kumar K.V 			 * still be on the orphan list; we need to
10641938a150SAneesh Kumar K.V 			 * make sure the inode is removed from the
10651938a150SAneesh Kumar K.V 			 * orphan list in that case.
10661938a150SAneesh Kumar K.V 			 */
10671938a150SAneesh Kumar K.V 			if (inode->i_nlink)
10681938a150SAneesh Kumar K.V 				ext4_orphan_del(NULL, inode);
10691938a150SAneesh Kumar K.V 		}
1070bfc1af65SNick Piggin 
107147564bfbSTheodore Ts'o 		if (ret == -ENOSPC &&
107247564bfbSTheodore Ts'o 		    ext4_should_retry_alloc(inode->i_sb, &retries))
107347564bfbSTheodore Ts'o 			goto retry_journal;
107447564bfbSTheodore Ts'o 		page_cache_release(page);
107547564bfbSTheodore Ts'o 		return ret;
107647564bfbSTheodore Ts'o 	}
107747564bfbSTheodore Ts'o 	*pagep = page;
1078ac27a0ecSDave Kleikamp 	return ret;
1079ac27a0ecSDave Kleikamp }
1080ac27a0ecSDave Kleikamp 
1081bfc1af65SNick Piggin /* For write_end() in data=journal mode */
1082bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1083ac27a0ecSDave Kleikamp {
1084ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh) || buffer_freed(bh))
1085ac27a0ecSDave Kleikamp 		return 0;
1086ac27a0ecSDave Kleikamp 	set_buffer_uptodate(bh);
10870390131bSFrank Mayhar 	return ext4_handle_dirty_metadata(handle, NULL, bh);
1088ac27a0ecSDave Kleikamp }
1089ac27a0ecSDave Kleikamp 
1090eed4333fSZheng Liu /*
1091eed4333fSZheng Liu  * We need to pick up the new inode size which generic_commit_write gave us
1092eed4333fSZheng Liu  * `file' can be NULL - eg, when called from page_symlink().
1093eed4333fSZheng Liu  *
1094eed4333fSZheng Liu  * ext4 never places buffers on inode->i_mapping->private_list.  metadata
1095eed4333fSZheng Liu  * buffers are managed internally.
1096eed4333fSZheng Liu  */
1097eed4333fSZheng Liu static int ext4_write_end(struct file *file,
1098f8514083SAneesh Kumar K.V 			  struct address_space *mapping,
1099f8514083SAneesh Kumar K.V 			  loff_t pos, unsigned len, unsigned copied,
1100f8514083SAneesh Kumar K.V 			  struct page *page, void *fsdata)
1101f8514083SAneesh Kumar K.V {
1102f8514083SAneesh Kumar K.V 	handle_t *handle = ext4_journal_current_handle();
1103eed4333fSZheng Liu 	struct inode *inode = mapping->host;
1104eed4333fSZheng Liu 	int ret = 0, ret2;
1105eed4333fSZheng Liu 	int i_size_changed = 0;
1106eed4333fSZheng Liu 
1107eed4333fSZheng Liu 	trace_ext4_write_end(inode, pos, len, copied);
1108eed4333fSZheng Liu 	if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) {
1109eed4333fSZheng Liu 		ret = ext4_jbd2_file_inode(handle, inode);
1110eed4333fSZheng Liu 		if (ret) {
1111eed4333fSZheng Liu 			unlock_page(page);
1112eed4333fSZheng Liu 			page_cache_release(page);
1113eed4333fSZheng Liu 			goto errout;
1114eed4333fSZheng Liu 		}
1115eed4333fSZheng Liu 	}
1116f8514083SAneesh Kumar K.V 
1117f19d5870STao Ma 	if (ext4_has_inline_data(inode))
1118f19d5870STao Ma 		copied = ext4_write_inline_data_end(inode, pos, len,
1119f19d5870STao Ma 						    copied, page);
1120f19d5870STao Ma 	else
1121f19d5870STao Ma 		copied = block_write_end(file, mapping, pos,
1122f19d5870STao Ma 					 len, copied, page, fsdata);
1123f8514083SAneesh Kumar K.V 
1124f8514083SAneesh Kumar K.V 	/*
1125f8514083SAneesh Kumar K.V 	 * No need to use i_size_read() here, the i_size
1126eed4333fSZheng Liu 	 * cannot change under us because we hole i_mutex.
1127f8514083SAneesh Kumar K.V 	 *
1128f8514083SAneesh Kumar K.V 	 * But it's important to update i_size while still holding page lock:
1129f8514083SAneesh Kumar K.V 	 * page writeout could otherwise come in and zero beyond i_size.
1130f8514083SAneesh Kumar K.V 	 */
1131f8514083SAneesh Kumar K.V 	if (pos + copied > inode->i_size) {
1132f8514083SAneesh Kumar K.V 		i_size_write(inode, pos + copied);
1133f8514083SAneesh Kumar K.V 		i_size_changed = 1;
1134f8514083SAneesh Kumar K.V 	}
1135f8514083SAneesh Kumar K.V 
1136f8514083SAneesh Kumar K.V 	if (pos + copied > EXT4_I(inode)->i_disksize) {
1137f8514083SAneesh Kumar K.V 		/* We need to mark inode dirty even if
1138f8514083SAneesh Kumar K.V 		 * new_i_size is less that inode->i_size
1139eed4333fSZheng Liu 		 * but greater than i_disksize. (hint delalloc)
1140f8514083SAneesh Kumar K.V 		 */
1141f8514083SAneesh Kumar K.V 		ext4_update_i_disksize(inode, (pos + copied));
1142f8514083SAneesh Kumar K.V 		i_size_changed = 1;
1143f8514083SAneesh Kumar K.V 	}
1144f8514083SAneesh Kumar K.V 	unlock_page(page);
1145f8514083SAneesh Kumar K.V 	page_cache_release(page);
1146f8514083SAneesh Kumar K.V 
1147f8514083SAneesh Kumar K.V 	/*
1148f8514083SAneesh Kumar K.V 	 * Don't mark the inode dirty under page lock. First, it unnecessarily
1149f8514083SAneesh Kumar K.V 	 * makes the holding time of page lock longer. Second, it forces lock
1150f8514083SAneesh Kumar K.V 	 * ordering of page lock and transaction start for journaling
1151f8514083SAneesh Kumar K.V 	 * filesystems.
1152f8514083SAneesh Kumar K.V 	 */
1153f8514083SAneesh Kumar K.V 	if (i_size_changed)
1154f8514083SAneesh Kumar K.V 		ext4_mark_inode_dirty(handle, inode);
1155f8514083SAneesh Kumar K.V 
115674d553aaSTheodore Ts'o 	if (copied < 0)
115774d553aaSTheodore Ts'o 		ret = copied;
1158ffacfa7aSJan Kara 	if (pos + len > inode->i_size && ext4_can_truncate(inode))
1159f8514083SAneesh Kumar K.V 		/* if we have allocated more blocks and copied
1160f8514083SAneesh Kumar K.V 		 * less. We will have blocks allocated outside
1161f8514083SAneesh Kumar K.V 		 * inode->i_size. So truncate them
1162f8514083SAneesh Kumar K.V 		 */
1163f8514083SAneesh Kumar K.V 		ext4_orphan_add(handle, inode);
116474d553aaSTheodore Ts'o errout:
1165617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1166ac27a0ecSDave Kleikamp 	if (!ret)
1167ac27a0ecSDave Kleikamp 		ret = ret2;
1168bfc1af65SNick Piggin 
1169f8514083SAneesh Kumar K.V 	if (pos + len > inode->i_size) {
1170b9a4207dSJan Kara 		ext4_truncate_failed_write(inode);
1171f8514083SAneesh Kumar K.V 		/*
1172ffacfa7aSJan Kara 		 * If truncate failed early the inode might still be
1173f8514083SAneesh Kumar K.V 		 * on the orphan list; we need to make sure the inode
1174f8514083SAneesh Kumar K.V 		 * is removed from the orphan list in that case.
1175f8514083SAneesh Kumar K.V 		 */
1176f8514083SAneesh Kumar K.V 		if (inode->i_nlink)
1177f8514083SAneesh Kumar K.V 			ext4_orphan_del(NULL, inode);
1178f8514083SAneesh Kumar K.V 	}
1179f8514083SAneesh Kumar K.V 
1180bfc1af65SNick Piggin 	return ret ? ret : copied;
1181ac27a0ecSDave Kleikamp }
1182ac27a0ecSDave Kleikamp 
1183bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file,
1184bfc1af65SNick Piggin 				     struct address_space *mapping,
1185bfc1af65SNick Piggin 				     loff_t pos, unsigned len, unsigned copied,
1186bfc1af65SNick Piggin 				     struct page *page, void *fsdata)
1187ac27a0ecSDave Kleikamp {
1188617ba13bSMingming Cao 	handle_t *handle = ext4_journal_current_handle();
1189bfc1af65SNick Piggin 	struct inode *inode = mapping->host;
1190ac27a0ecSDave Kleikamp 	int ret = 0, ret2;
1191ac27a0ecSDave Kleikamp 	int partial = 0;
1192bfc1af65SNick Piggin 	unsigned from, to;
1193cf17fea6SAneesh Kumar K.V 	loff_t new_i_size;
1194ac27a0ecSDave Kleikamp 
11959bffad1eSTheodore Ts'o 	trace_ext4_journalled_write_end(inode, pos, len, copied);
1196bfc1af65SNick Piggin 	from = pos & (PAGE_CACHE_SIZE - 1);
1197bfc1af65SNick Piggin 	to = from + len;
1198bfc1af65SNick Piggin 
1199441c8508SCurt Wohlgemuth 	BUG_ON(!ext4_handle_valid(handle));
1200441c8508SCurt Wohlgemuth 
12013fdcfb66STao Ma 	if (ext4_has_inline_data(inode))
12023fdcfb66STao Ma 		copied = ext4_write_inline_data_end(inode, pos, len,
12033fdcfb66STao Ma 						    copied, page);
12043fdcfb66STao Ma 	else {
1205bfc1af65SNick Piggin 		if (copied < len) {
1206bfc1af65SNick Piggin 			if (!PageUptodate(page))
1207bfc1af65SNick Piggin 				copied = 0;
1208bfc1af65SNick Piggin 			page_zero_new_buffers(page, from+copied, to);
1209bfc1af65SNick Piggin 		}
1210ac27a0ecSDave Kleikamp 
1211f19d5870STao Ma 		ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1212bfc1af65SNick Piggin 					     to, &partial, write_end_fn);
1213ac27a0ecSDave Kleikamp 		if (!partial)
1214ac27a0ecSDave Kleikamp 			SetPageUptodate(page);
12153fdcfb66STao Ma 	}
1216cf17fea6SAneesh Kumar K.V 	new_i_size = pos + copied;
1217cf17fea6SAneesh Kumar K.V 	if (new_i_size > inode->i_size)
1218bfc1af65SNick Piggin 		i_size_write(inode, pos+copied);
121919f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
12202d859db3SJan Kara 	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
1221cf17fea6SAneesh Kumar K.V 	if (new_i_size > EXT4_I(inode)->i_disksize) {
1222cf17fea6SAneesh Kumar K.V 		ext4_update_i_disksize(inode, new_i_size);
1223617ba13bSMingming Cao 		ret2 = ext4_mark_inode_dirty(handle, inode);
1224ac27a0ecSDave Kleikamp 		if (!ret)
1225ac27a0ecSDave Kleikamp 			ret = ret2;
1226ac27a0ecSDave Kleikamp 	}
1227bfc1af65SNick Piggin 
1228cf108bcaSJan Kara 	unlock_page(page);
1229f8514083SAneesh Kumar K.V 	page_cache_release(page);
1230ffacfa7aSJan Kara 	if (pos + len > inode->i_size && ext4_can_truncate(inode))
1231f8514083SAneesh Kumar K.V 		/* if we have allocated more blocks and copied
1232f8514083SAneesh Kumar K.V 		 * less. We will have blocks allocated outside
1233f8514083SAneesh Kumar K.V 		 * inode->i_size. So truncate them
1234f8514083SAneesh Kumar K.V 		 */
1235f8514083SAneesh Kumar K.V 		ext4_orphan_add(handle, inode);
1236f8514083SAneesh Kumar K.V 
1237617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1238ac27a0ecSDave Kleikamp 	if (!ret)
1239ac27a0ecSDave Kleikamp 		ret = ret2;
1240f8514083SAneesh Kumar K.V 	if (pos + len > inode->i_size) {
1241b9a4207dSJan Kara 		ext4_truncate_failed_write(inode);
1242f8514083SAneesh Kumar K.V 		/*
1243ffacfa7aSJan Kara 		 * If truncate failed early the inode might still be
1244f8514083SAneesh Kumar K.V 		 * on the orphan list; we need to make sure the inode
1245f8514083SAneesh Kumar K.V 		 * is removed from the orphan list in that case.
1246f8514083SAneesh Kumar K.V 		 */
1247f8514083SAneesh Kumar K.V 		if (inode->i_nlink)
1248f8514083SAneesh Kumar K.V 			ext4_orphan_del(NULL, inode);
1249f8514083SAneesh Kumar K.V 	}
1250bfc1af65SNick Piggin 
1251bfc1af65SNick Piggin 	return ret ? ret : copied;
1252ac27a0ecSDave Kleikamp }
1253d2a17637SMingming Cao 
12549d0be502STheodore Ts'o /*
1255386ad67cSLukas Czerner  * Reserve a metadata for a single block located at lblock
1256386ad67cSLukas Czerner  */
1257386ad67cSLukas Czerner static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
1258386ad67cSLukas Czerner {
1259386ad67cSLukas Czerner 	int retries = 0;
1260386ad67cSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1261386ad67cSLukas Czerner 	struct ext4_inode_info *ei = EXT4_I(inode);
1262386ad67cSLukas Czerner 	unsigned int md_needed;
1263386ad67cSLukas Czerner 	ext4_lblk_t save_last_lblock;
1264386ad67cSLukas Czerner 	int save_len;
1265386ad67cSLukas Czerner 
1266386ad67cSLukas Czerner 	/*
1267386ad67cSLukas Czerner 	 * recalculate the amount of metadata blocks to reserve
1268386ad67cSLukas Czerner 	 * in order to allocate nrblocks
1269386ad67cSLukas Czerner 	 * worse case is one extent per block
1270386ad67cSLukas Czerner 	 */
1271386ad67cSLukas Czerner repeat:
1272386ad67cSLukas Czerner 	spin_lock(&ei->i_block_reservation_lock);
1273386ad67cSLukas Czerner 	/*
1274386ad67cSLukas Czerner 	 * ext4_calc_metadata_amount() has side effects, which we have
1275386ad67cSLukas Czerner 	 * to be prepared undo if we fail to claim space.
1276386ad67cSLukas Czerner 	 */
1277386ad67cSLukas Czerner 	save_len = ei->i_da_metadata_calc_len;
1278386ad67cSLukas Czerner 	save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1279386ad67cSLukas Czerner 	md_needed = EXT4_NUM_B2C(sbi,
1280386ad67cSLukas Czerner 				 ext4_calc_metadata_amount(inode, lblock));
1281386ad67cSLukas Czerner 	trace_ext4_da_reserve_space(inode, md_needed);
1282386ad67cSLukas Czerner 
1283386ad67cSLukas Czerner 	/*
1284386ad67cSLukas Czerner 	 * We do still charge estimated metadata to the sb though;
1285386ad67cSLukas Czerner 	 * we cannot afford to run out of free blocks.
1286386ad67cSLukas Czerner 	 */
1287386ad67cSLukas Czerner 	if (ext4_claim_free_clusters(sbi, md_needed, 0)) {
1288386ad67cSLukas Czerner 		ei->i_da_metadata_calc_len = save_len;
1289386ad67cSLukas Czerner 		ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1290386ad67cSLukas Czerner 		spin_unlock(&ei->i_block_reservation_lock);
1291386ad67cSLukas Czerner 		if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1292386ad67cSLukas Czerner 			cond_resched();
1293386ad67cSLukas Czerner 			goto repeat;
1294386ad67cSLukas Czerner 		}
1295386ad67cSLukas Czerner 		return -ENOSPC;
1296386ad67cSLukas Czerner 	}
1297386ad67cSLukas Czerner 	ei->i_reserved_meta_blocks += md_needed;
1298386ad67cSLukas Czerner 	spin_unlock(&ei->i_block_reservation_lock);
1299386ad67cSLukas Czerner 
1300386ad67cSLukas Czerner 	return 0;       /* success */
1301386ad67cSLukas Czerner }
1302386ad67cSLukas Czerner 
1303386ad67cSLukas Czerner /*
13047b415bf6SAditya Kali  * Reserve a single cluster located at lblock
13059d0be502STheodore Ts'o  */
130601f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
1307d2a17637SMingming Cao {
1308030ba6bcSAneesh Kumar K.V 	int retries = 0;
1309d2a17637SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
13100637c6f4STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
13117b415bf6SAditya Kali 	unsigned int md_needed;
13125dd4056dSChristoph Hellwig 	int ret;
131303179fe9STheodore Ts'o 	ext4_lblk_t save_last_lblock;
131403179fe9STheodore Ts'o 	int save_len;
1315d2a17637SMingming Cao 
131660e58e0fSMingming Cao 	/*
131772b8ab9dSEric Sandeen 	 * We will charge metadata quota at writeout time; this saves
131872b8ab9dSEric Sandeen 	 * us from metadata over-estimation, though we may go over by
131972b8ab9dSEric Sandeen 	 * a small amount in the end.  Here we just reserve for data.
132060e58e0fSMingming Cao 	 */
13217b415bf6SAditya Kali 	ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
13225dd4056dSChristoph Hellwig 	if (ret)
13235dd4056dSChristoph Hellwig 		return ret;
132403179fe9STheodore Ts'o 
132503179fe9STheodore Ts'o 	/*
132603179fe9STheodore Ts'o 	 * recalculate the amount of metadata blocks to reserve
132703179fe9STheodore Ts'o 	 * in order to allocate nrblocks
132803179fe9STheodore Ts'o 	 * worse case is one extent per block
132903179fe9STheodore Ts'o 	 */
133003179fe9STheodore Ts'o repeat:
133103179fe9STheodore Ts'o 	spin_lock(&ei->i_block_reservation_lock);
133203179fe9STheodore Ts'o 	/*
133303179fe9STheodore Ts'o 	 * ext4_calc_metadata_amount() has side effects, which we have
133403179fe9STheodore Ts'o 	 * to be prepared undo if we fail to claim space.
133503179fe9STheodore Ts'o 	 */
133603179fe9STheodore Ts'o 	save_len = ei->i_da_metadata_calc_len;
133703179fe9STheodore Ts'o 	save_last_lblock = ei->i_da_metadata_calc_last_lblock;
133803179fe9STheodore Ts'o 	md_needed = EXT4_NUM_B2C(sbi,
133903179fe9STheodore Ts'o 				 ext4_calc_metadata_amount(inode, lblock));
134003179fe9STheodore Ts'o 	trace_ext4_da_reserve_space(inode, md_needed);
134103179fe9STheodore Ts'o 
134272b8ab9dSEric Sandeen 	/*
134372b8ab9dSEric Sandeen 	 * We do still charge estimated metadata to the sb though;
134472b8ab9dSEric Sandeen 	 * we cannot afford to run out of free blocks.
134572b8ab9dSEric Sandeen 	 */
1346e7d5f315STheodore Ts'o 	if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
134703179fe9STheodore Ts'o 		ei->i_da_metadata_calc_len = save_len;
134803179fe9STheodore Ts'o 		ei->i_da_metadata_calc_last_lblock = save_last_lblock;
134903179fe9STheodore Ts'o 		spin_unlock(&ei->i_block_reservation_lock);
1350030ba6bcSAneesh Kumar K.V 		if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1351bb8b20edSLukas Czerner 			cond_resched();
1352030ba6bcSAneesh Kumar K.V 			goto repeat;
1353030ba6bcSAneesh Kumar K.V 		}
135403179fe9STheodore Ts'o 		dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
1355d2a17637SMingming Cao 		return -ENOSPC;
1356d2a17637SMingming Cao 	}
13579d0be502STheodore Ts'o 	ei->i_reserved_data_blocks++;
13580637c6f4STheodore Ts'o 	ei->i_reserved_meta_blocks += md_needed;
13590637c6f4STheodore Ts'o 	spin_unlock(&ei->i_block_reservation_lock);
136039bc680aSDmitry Monakhov 
1361d2a17637SMingming Cao 	return 0;       /* success */
1362d2a17637SMingming Cao }
1363d2a17637SMingming Cao 
136412219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free)
1365d2a17637SMingming Cao {
1366d2a17637SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
13670637c6f4STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
1368d2a17637SMingming Cao 
1369cd213226SMingming Cao 	if (!to_free)
1370cd213226SMingming Cao 		return;		/* Nothing to release, exit */
1371cd213226SMingming Cao 
1372d2a17637SMingming Cao 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1373cd213226SMingming Cao 
13745a58ec87SLi Zefan 	trace_ext4_da_release_space(inode, to_free);
13750637c6f4STheodore Ts'o 	if (unlikely(to_free > ei->i_reserved_data_blocks)) {
1376cd213226SMingming Cao 		/*
13770637c6f4STheodore Ts'o 		 * if there aren't enough reserved blocks, then the
13780637c6f4STheodore Ts'o 		 * counter is messed up somewhere.  Since this
13790637c6f4STheodore Ts'o 		 * function is called from invalidate page, it's
13800637c6f4STheodore Ts'o 		 * harmless to return without any action.
1381cd213226SMingming Cao 		 */
13828de5c325STheodore Ts'o 		ext4_warning(inode->i_sb, "ext4_da_release_space: "
13830637c6f4STheodore Ts'o 			 "ino %lu, to_free %d with only %d reserved "
13841084f252STheodore Ts'o 			 "data blocks", inode->i_ino, to_free,
13850637c6f4STheodore Ts'o 			 ei->i_reserved_data_blocks);
13860637c6f4STheodore Ts'o 		WARN_ON(1);
13870637c6f4STheodore Ts'o 		to_free = ei->i_reserved_data_blocks;
13880637c6f4STheodore Ts'o 	}
13890637c6f4STheodore Ts'o 	ei->i_reserved_data_blocks -= to_free;
13900637c6f4STheodore Ts'o 
13910637c6f4STheodore Ts'o 	if (ei->i_reserved_data_blocks == 0) {
13920637c6f4STheodore Ts'o 		/*
13930637c6f4STheodore Ts'o 		 * We can release all of the reserved metadata blocks
13940637c6f4STheodore Ts'o 		 * only when we have written all of the delayed
13950637c6f4STheodore Ts'o 		 * allocation blocks.
13967b415bf6SAditya Kali 		 * Note that in case of bigalloc, i_reserved_meta_blocks,
13977b415bf6SAditya Kali 		 * i_reserved_data_blocks, etc. refer to number of clusters.
13980637c6f4STheodore Ts'o 		 */
139957042651STheodore Ts'o 		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
140072b8ab9dSEric Sandeen 				   ei->i_reserved_meta_blocks);
1401ee5f4d9cSTheodore Ts'o 		ei->i_reserved_meta_blocks = 0;
14029d0be502STheodore Ts'o 		ei->i_da_metadata_calc_len = 0;
1403cd213226SMingming Cao 	}
1404cd213226SMingming Cao 
140572b8ab9dSEric Sandeen 	/* update fs dirty data blocks counter */
140657042651STheodore Ts'o 	percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
1407d2a17637SMingming Cao 
1408d2a17637SMingming Cao 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
140960e58e0fSMingming Cao 
14107b415bf6SAditya Kali 	dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
1411d2a17637SMingming Cao }
1412d2a17637SMingming Cao 
1413d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page,
1414d2a17637SMingming Cao 					     unsigned long offset)
1415d2a17637SMingming Cao {
1416d2a17637SMingming Cao 	int to_release = 0;
1417d2a17637SMingming Cao 	struct buffer_head *head, *bh;
1418d2a17637SMingming Cao 	unsigned int curr_off = 0;
14197b415bf6SAditya Kali 	struct inode *inode = page->mapping->host;
14207b415bf6SAditya Kali 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
14217b415bf6SAditya Kali 	int num_clusters;
142251865fdaSZheng Liu 	ext4_fsblk_t lblk;
1423d2a17637SMingming Cao 
1424d2a17637SMingming Cao 	head = page_buffers(page);
1425d2a17637SMingming Cao 	bh = head;
1426d2a17637SMingming Cao 	do {
1427d2a17637SMingming Cao 		unsigned int next_off = curr_off + bh->b_size;
1428d2a17637SMingming Cao 
1429d2a17637SMingming Cao 		if ((offset <= curr_off) && (buffer_delay(bh))) {
1430d2a17637SMingming Cao 			to_release++;
1431d2a17637SMingming Cao 			clear_buffer_delay(bh);
1432d2a17637SMingming Cao 		}
1433d2a17637SMingming Cao 		curr_off = next_off;
1434d2a17637SMingming Cao 	} while ((bh = bh->b_this_page) != head);
14357b415bf6SAditya Kali 
143651865fdaSZheng Liu 	if (to_release) {
143751865fdaSZheng Liu 		lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
143851865fdaSZheng Liu 		ext4_es_remove_extent(inode, lblk, to_release);
143951865fdaSZheng Liu 	}
144051865fdaSZheng Liu 
14417b415bf6SAditya Kali 	/* If we have released all the blocks belonging to a cluster, then we
14427b415bf6SAditya Kali 	 * need to release the reserved space for that cluster. */
14437b415bf6SAditya Kali 	num_clusters = EXT4_NUM_B2C(sbi, to_release);
14447b415bf6SAditya Kali 	while (num_clusters > 0) {
14457b415bf6SAditya Kali 		lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
14467b415bf6SAditya Kali 			((num_clusters - 1) << sbi->s_cluster_bits);
14477b415bf6SAditya Kali 		if (sbi->s_cluster_ratio == 1 ||
14487d1b1fbcSZheng Liu 		    !ext4_find_delalloc_cluster(inode, lblk))
14497b415bf6SAditya Kali 			ext4_da_release_space(inode, 1);
14507b415bf6SAditya Kali 
14517b415bf6SAditya Kali 		num_clusters--;
14527b415bf6SAditya Kali 	}
1453d2a17637SMingming Cao }
1454ac27a0ecSDave Kleikamp 
1455ac27a0ecSDave Kleikamp /*
145664769240SAlex Tomas  * Delayed allocation stuff
145764769240SAlex Tomas  */
145864769240SAlex Tomas 
145964769240SAlex Tomas /*
146064769240SAlex Tomas  * mpage_da_submit_io - walks through extent of pages and try to write
1461a1d6cc56SAneesh Kumar K.V  * them with writepage() call back
146264769240SAlex Tomas  *
146364769240SAlex Tomas  * @mpd->inode: inode
146464769240SAlex Tomas  * @mpd->first_page: first page of the extent
146564769240SAlex Tomas  * @mpd->next_page: page after the last page of the extent
146664769240SAlex Tomas  *
146764769240SAlex Tomas  * By the time mpage_da_submit_io() is called we expect all blocks
146864769240SAlex Tomas  * to be allocated. this may be wrong if allocation failed.
146964769240SAlex Tomas  *
147064769240SAlex Tomas  * As pages are already locked by write_cache_pages(), we can't use it
147164769240SAlex Tomas  */
14721de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd,
14731de3e3dfSTheodore Ts'o 			      struct ext4_map_blocks *map)
147464769240SAlex Tomas {
1475791b7f08SAneesh Kumar K.V 	struct pagevec pvec;
1476791b7f08SAneesh Kumar K.V 	unsigned long index, end;
1477791b7f08SAneesh Kumar K.V 	int ret = 0, err, nr_pages, i;
1478791b7f08SAneesh Kumar K.V 	struct inode *inode = mpd->inode;
1479791b7f08SAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
1480cb20d518STheodore Ts'o 	loff_t size = i_size_read(inode);
14813ecdb3a1STheodore Ts'o 	unsigned int len, block_start;
14823ecdb3a1STheodore Ts'o 	struct buffer_head *bh, *page_bufs = NULL;
14831de3e3dfSTheodore Ts'o 	sector_t pblock = 0, cur_logical = 0;
1484bd2d0210STheodore Ts'o 	struct ext4_io_submit io_submit;
148564769240SAlex Tomas 
148664769240SAlex Tomas 	BUG_ON(mpd->next_page <= mpd->first_page);
1487bd2d0210STheodore Ts'o 	memset(&io_submit, 0, sizeof(io_submit));
1488791b7f08SAneesh Kumar K.V 	/*
1489791b7f08SAneesh Kumar K.V 	 * We need to start from the first_page to the next_page - 1
1490791b7f08SAneesh Kumar K.V 	 * to make sure we also write the mapped dirty buffer_heads.
14918dc207c0STheodore Ts'o 	 * If we look at mpd->b_blocknr we would only be looking
1492791b7f08SAneesh Kumar K.V 	 * at the currently mapped buffer_heads.
1493791b7f08SAneesh Kumar K.V 	 */
149464769240SAlex Tomas 	index = mpd->first_page;
149564769240SAlex Tomas 	end = mpd->next_page - 1;
149664769240SAlex Tomas 
1497791b7f08SAneesh Kumar K.V 	pagevec_init(&pvec, 0);
149864769240SAlex Tomas 	while (index <= end) {
1499791b7f08SAneesh Kumar K.V 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
150064769240SAlex Tomas 		if (nr_pages == 0)
150164769240SAlex Tomas 			break;
150264769240SAlex Tomas 		for (i = 0; i < nr_pages; i++) {
1503f8bec370SJan Kara 			int skip_page = 0;
150464769240SAlex Tomas 			struct page *page = pvec.pages[i];
150564769240SAlex Tomas 
1506791b7f08SAneesh Kumar K.V 			index = page->index;
1507791b7f08SAneesh Kumar K.V 			if (index > end)
1508791b7f08SAneesh Kumar K.V 				break;
1509cb20d518STheodore Ts'o 
1510cb20d518STheodore Ts'o 			if (index == size >> PAGE_CACHE_SHIFT)
1511cb20d518STheodore Ts'o 				len = size & ~PAGE_CACHE_MASK;
1512cb20d518STheodore Ts'o 			else
1513cb20d518STheodore Ts'o 				len = PAGE_CACHE_SIZE;
15141de3e3dfSTheodore Ts'o 			if (map) {
15151de3e3dfSTheodore Ts'o 				cur_logical = index << (PAGE_CACHE_SHIFT -
15161de3e3dfSTheodore Ts'o 							inode->i_blkbits);
15171de3e3dfSTheodore Ts'o 				pblock = map->m_pblk + (cur_logical -
15181de3e3dfSTheodore Ts'o 							map->m_lblk);
15191de3e3dfSTheodore Ts'o 			}
1520791b7f08SAneesh Kumar K.V 			index++;
1521791b7f08SAneesh Kumar K.V 
1522791b7f08SAneesh Kumar K.V 			BUG_ON(!PageLocked(page));
1523791b7f08SAneesh Kumar K.V 			BUG_ON(PageWriteback(page));
1524791b7f08SAneesh Kumar K.V 
15253ecdb3a1STheodore Ts'o 			bh = page_bufs = page_buffers(page);
15263ecdb3a1STheodore Ts'o 			block_start = 0;
15273ecdb3a1STheodore Ts'o 			do {
15281de3e3dfSTheodore Ts'o 				if (map && (cur_logical >= map->m_lblk) &&
15291de3e3dfSTheodore Ts'o 				    (cur_logical <= (map->m_lblk +
15301de3e3dfSTheodore Ts'o 						     (map->m_len - 1)))) {
15311de3e3dfSTheodore Ts'o 					if (buffer_delay(bh)) {
15321de3e3dfSTheodore Ts'o 						clear_buffer_delay(bh);
15331de3e3dfSTheodore Ts'o 						bh->b_blocknr = pblock;
15341de3e3dfSTheodore Ts'o 					}
15351de3e3dfSTheodore Ts'o 					if (buffer_unwritten(bh) ||
15361de3e3dfSTheodore Ts'o 					    buffer_mapped(bh))
15371de3e3dfSTheodore Ts'o 						BUG_ON(bh->b_blocknr != pblock);
15381de3e3dfSTheodore Ts'o 					if (map->m_flags & EXT4_MAP_UNINIT)
15391de3e3dfSTheodore Ts'o 						set_buffer_uninit(bh);
15401de3e3dfSTheodore Ts'o 					clear_buffer_unwritten(bh);
15411de3e3dfSTheodore Ts'o 				}
15421de3e3dfSTheodore Ts'o 
154313a79a47SYongqiang Yang 				/*
154413a79a47SYongqiang Yang 				 * skip page if block allocation undone and
154513a79a47SYongqiang Yang 				 * block is dirty
154613a79a47SYongqiang Yang 				 */
154713a79a47SYongqiang Yang 				if (ext4_bh_delay_or_unwritten(NULL, bh))
154897498956STheodore Ts'o 					skip_page = 1;
15493ecdb3a1STheodore Ts'o 				bh = bh->b_this_page;
15503ecdb3a1STheodore Ts'o 				block_start += bh->b_size;
15511de3e3dfSTheodore Ts'o 				cur_logical++;
15521de3e3dfSTheodore Ts'o 				pblock++;
15531de3e3dfSTheodore Ts'o 			} while (bh != page_bufs);
15541de3e3dfSTheodore Ts'o 
1555f8bec370SJan Kara 			if (skip_page) {
1556f8bec370SJan Kara 				unlock_page(page);
1557f8bec370SJan Kara 				continue;
1558f8bec370SJan Kara 			}
1559cb20d518STheodore Ts'o 
156097498956STheodore Ts'o 			clear_page_dirty_for_io(page);
1561fe089c77SJan Kara 			err = ext4_bio_write_page(&io_submit, page, len,
1562fe089c77SJan Kara 						  mpd->wbc);
1563cb20d518STheodore Ts'o 			if (!err)
1564a1d6cc56SAneesh Kumar K.V 				mpd->pages_written++;
156564769240SAlex Tomas 			/*
156664769240SAlex Tomas 			 * In error case, we have to continue because
156764769240SAlex Tomas 			 * remaining pages are still locked
156864769240SAlex Tomas 			 */
156964769240SAlex Tomas 			if (ret == 0)
157064769240SAlex Tomas 				ret = err;
157164769240SAlex Tomas 		}
157264769240SAlex Tomas 		pagevec_release(&pvec);
157364769240SAlex Tomas 	}
1574bd2d0210STheodore Ts'o 	ext4_io_submit(&io_submit);
157564769240SAlex Tomas 	return ret;
157664769240SAlex Tomas }
157764769240SAlex Tomas 
1578c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
1579c4a0c46eSAneesh Kumar K.V {
1580c4a0c46eSAneesh Kumar K.V 	int nr_pages, i;
1581c4a0c46eSAneesh Kumar K.V 	pgoff_t index, end;
1582c4a0c46eSAneesh Kumar K.V 	struct pagevec pvec;
1583c4a0c46eSAneesh Kumar K.V 	struct inode *inode = mpd->inode;
1584c4a0c46eSAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
158551865fdaSZheng Liu 	ext4_lblk_t start, last;
1586c4a0c46eSAneesh Kumar K.V 
1587c7f5938aSCurt Wohlgemuth 	index = mpd->first_page;
1588c7f5938aSCurt Wohlgemuth 	end   = mpd->next_page - 1;
158951865fdaSZheng Liu 
159051865fdaSZheng Liu 	start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
159151865fdaSZheng Liu 	last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
159251865fdaSZheng Liu 	ext4_es_remove_extent(inode, start, last - start + 1);
159351865fdaSZheng Liu 
159466bea92cSEric Sandeen 	pagevec_init(&pvec, 0);
1595c4a0c46eSAneesh Kumar K.V 	while (index <= end) {
1596c4a0c46eSAneesh Kumar K.V 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1597c4a0c46eSAneesh Kumar K.V 		if (nr_pages == 0)
1598c4a0c46eSAneesh Kumar K.V 			break;
1599c4a0c46eSAneesh Kumar K.V 		for (i = 0; i < nr_pages; i++) {
1600c4a0c46eSAneesh Kumar K.V 			struct page *page = pvec.pages[i];
16019b1d0998SJan Kara 			if (page->index > end)
1602c4a0c46eSAneesh Kumar K.V 				break;
1603c4a0c46eSAneesh Kumar K.V 			BUG_ON(!PageLocked(page));
1604c4a0c46eSAneesh Kumar K.V 			BUG_ON(PageWriteback(page));
1605c4a0c46eSAneesh Kumar K.V 			block_invalidatepage(page, 0);
1606c4a0c46eSAneesh Kumar K.V 			ClearPageUptodate(page);
1607c4a0c46eSAneesh Kumar K.V 			unlock_page(page);
1608c4a0c46eSAneesh Kumar K.V 		}
16099b1d0998SJan Kara 		index = pvec.pages[nr_pages - 1]->index + 1;
16109b1d0998SJan Kara 		pagevec_release(&pvec);
1611c4a0c46eSAneesh Kumar K.V 	}
1612c4a0c46eSAneesh Kumar K.V 	return;
1613c4a0c46eSAneesh Kumar K.V }
1614c4a0c46eSAneesh Kumar K.V 
1615df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode)
1616df22291fSAneesh Kumar K.V {
1617df22291fSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
161892b97816STheodore Ts'o 	struct super_block *sb = inode->i_sb;
161992b97816STheodore Ts'o 
162092b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
16215dee5437STheodore Ts'o 	       EXT4_C2B(EXT4_SB(inode->i_sb),
16225dee5437STheodore Ts'o 			ext4_count_free_clusters(inode->i_sb)));
162392b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
162492b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
162557042651STheodore Ts'o 	       (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
162657042651STheodore Ts'o 		percpu_counter_sum(&sbi->s_freeclusters_counter)));
162792b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
16287b415bf6SAditya Kali 	       (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
16297b415bf6SAditya Kali 		percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
163092b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "Block reservation details");
163192b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
1632df22291fSAneesh Kumar K.V 		 EXT4_I(inode)->i_reserved_data_blocks);
163392b97816STheodore Ts'o 	ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
1634df22291fSAneesh Kumar K.V 	       EXT4_I(inode)->i_reserved_meta_blocks);
1635df22291fSAneesh Kumar K.V 	return;
1636df22291fSAneesh Kumar K.V }
1637df22291fSAneesh Kumar K.V 
1638b920c755STheodore Ts'o /*
16395a87b7a5STheodore Ts'o  * mpage_da_map_and_submit - go through given space, map them
16405a87b7a5STheodore Ts'o  *       if necessary, and then submit them for I/O
164164769240SAlex Tomas  *
16428dc207c0STheodore Ts'o  * @mpd - bh describing space
164364769240SAlex Tomas  *
164464769240SAlex Tomas  * The function skips space we know is already mapped to disk blocks.
164564769240SAlex Tomas  *
164664769240SAlex Tomas  */
16475a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
164864769240SAlex Tomas {
16492ac3b6e0STheodore Ts'o 	int err, blks, get_blocks_flags;
16501de3e3dfSTheodore Ts'o 	struct ext4_map_blocks map, *mapp = NULL;
16512fa3cdfbSTheodore Ts'o 	sector_t next = mpd->b_blocknr;
16522fa3cdfbSTheodore Ts'o 	unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
16532fa3cdfbSTheodore Ts'o 	loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
16542fa3cdfbSTheodore Ts'o 	handle_t *handle = NULL;
165564769240SAlex Tomas 
165664769240SAlex Tomas 	/*
16575a87b7a5STheodore Ts'o 	 * If the blocks are mapped already, or we couldn't accumulate
16585a87b7a5STheodore Ts'o 	 * any blocks, then proceed immediately to the submission stage.
165964769240SAlex Tomas 	 */
16605a87b7a5STheodore Ts'o 	if ((mpd->b_size == 0) ||
16615a87b7a5STheodore Ts'o 	    ((mpd->b_state  & (1 << BH_Mapped)) &&
166229fa89d0SAneesh Kumar K.V 	     !(mpd->b_state & (1 << BH_Delay)) &&
16635a87b7a5STheodore Ts'o 	     !(mpd->b_state & (1 << BH_Unwritten))))
16645a87b7a5STheodore Ts'o 		goto submit_io;
16652fa3cdfbSTheodore Ts'o 
16662fa3cdfbSTheodore Ts'o 	handle = ext4_journal_current_handle();
16672fa3cdfbSTheodore Ts'o 	BUG_ON(!handle);
16682fa3cdfbSTheodore Ts'o 
166979ffab34SAneesh Kumar K.V 	/*
167079e83036SEric Sandeen 	 * Call ext4_map_blocks() to allocate any delayed allocation
16712ac3b6e0STheodore Ts'o 	 * blocks, or to convert an uninitialized extent to be
16722ac3b6e0STheodore Ts'o 	 * initialized (in the case where we have written into
16732ac3b6e0STheodore Ts'o 	 * one or more preallocated blocks).
16742ac3b6e0STheodore Ts'o 	 *
16752ac3b6e0STheodore Ts'o 	 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
16762ac3b6e0STheodore Ts'o 	 * indicate that we are on the delayed allocation path.  This
16772ac3b6e0STheodore Ts'o 	 * affects functions in many different parts of the allocation
16782ac3b6e0STheodore Ts'o 	 * call path.  This flag exists primarily because we don't
167979e83036SEric Sandeen 	 * want to change *many* call functions, so ext4_map_blocks()
1680f2321097STheodore Ts'o 	 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
16812ac3b6e0STheodore Ts'o 	 * inode's allocation semaphore is taken.
16822ac3b6e0STheodore Ts'o 	 *
16832ac3b6e0STheodore Ts'o 	 * If the blocks in questions were delalloc blocks, set
16842ac3b6e0STheodore Ts'o 	 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
16852ac3b6e0STheodore Ts'o 	 * variables are updated after the blocks have been allocated.
168679ffab34SAneesh Kumar K.V 	 */
16872ed88685STheodore Ts'o 	map.m_lblk = next;
16882ed88685STheodore Ts'o 	map.m_len = max_blocks;
16891296cc85SAneesh Kumar K.V 	get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
1690744692dcSJiaying Zhang 	if (ext4_should_dioread_nolock(mpd->inode))
1691744692dcSJiaying Zhang 		get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
16922ac3b6e0STheodore Ts'o 	if (mpd->b_state & (1 << BH_Delay))
16931296cc85SAneesh Kumar K.V 		get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
16941296cc85SAneesh Kumar K.V 
16952ed88685STheodore Ts'o 	blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
16962fa3cdfbSTheodore Ts'o 	if (blks < 0) {
1697e3570639SEric Sandeen 		struct super_block *sb = mpd->inode->i_sb;
1698e3570639SEric Sandeen 
16992fa3cdfbSTheodore Ts'o 		err = blks;
1700ed5bde0bSTheodore Ts'o 		/*
17015a87b7a5STheodore Ts'o 		 * If get block returns EAGAIN or ENOSPC and there
170297498956STheodore Ts'o 		 * appears to be free blocks we will just let
170397498956STheodore Ts'o 		 * mpage_da_submit_io() unlock all of the pages.
1704c4a0c46eSAneesh Kumar K.V 		 */
1705c4a0c46eSAneesh Kumar K.V 		if (err == -EAGAIN)
17065a87b7a5STheodore Ts'o 			goto submit_io;
1707df22291fSAneesh Kumar K.V 
17085dee5437STheodore Ts'o 		if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
1709df22291fSAneesh Kumar K.V 			mpd->retval = err;
17105a87b7a5STheodore Ts'o 			goto submit_io;
1711df22291fSAneesh Kumar K.V 		}
1712df22291fSAneesh Kumar K.V 
1713c4a0c46eSAneesh Kumar K.V 		/*
1714ed5bde0bSTheodore Ts'o 		 * get block failure will cause us to loop in
1715ed5bde0bSTheodore Ts'o 		 * writepages, because a_ops->writepage won't be able
1716ed5bde0bSTheodore Ts'o 		 * to make progress. The page will be redirtied by
1717ed5bde0bSTheodore Ts'o 		 * writepage and writepages will again try to write
1718ed5bde0bSTheodore Ts'o 		 * the same.
1719c4a0c46eSAneesh Kumar K.V 		 */
1720e3570639SEric Sandeen 		if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
1721e3570639SEric Sandeen 			ext4_msg(sb, KERN_CRIT,
1722e3570639SEric Sandeen 				 "delayed block allocation failed for inode %lu "
1723e3570639SEric Sandeen 				 "at logical offset %llu with max blocks %zd "
1724e3570639SEric Sandeen 				 "with error %d", mpd->inode->i_ino,
1725c4a0c46eSAneesh Kumar K.V 				 (unsigned long long) next,
17268dc207c0STheodore Ts'o 				 mpd->b_size >> mpd->inode->i_blkbits, err);
1727e3570639SEric Sandeen 			ext4_msg(sb, KERN_CRIT,
172801a523ebSTheodore Ts'o 				"This should not happen!! Data will be lost");
1729e3570639SEric Sandeen 			if (err == -ENOSPC)
1730df22291fSAneesh Kumar K.V 				ext4_print_free_blocks(mpd->inode);
1731030ba6bcSAneesh Kumar K.V 		}
17322fa3cdfbSTheodore Ts'o 		/* invalidate all the pages */
1733c7f5938aSCurt Wohlgemuth 		ext4_da_block_invalidatepages(mpd);
1734e0fd9b90SCurt Wohlgemuth 
1735e0fd9b90SCurt Wohlgemuth 		/* Mark this page range as having been completed */
1736e0fd9b90SCurt Wohlgemuth 		mpd->io_done = 1;
17375a87b7a5STheodore Ts'o 		return;
1738c4a0c46eSAneesh Kumar K.V 	}
17392fa3cdfbSTheodore Ts'o 	BUG_ON(blks == 0);
17402fa3cdfbSTheodore Ts'o 
17411de3e3dfSTheodore Ts'o 	mapp = &map;
17422ed88685STheodore Ts'o 	if (map.m_flags & EXT4_MAP_NEW) {
17432ed88685STheodore Ts'o 		struct block_device *bdev = mpd->inode->i_sb->s_bdev;
17442ed88685STheodore Ts'o 		int i;
174564769240SAlex Tomas 
17462ed88685STheodore Ts'o 		for (i = 0; i < map.m_len; i++)
17472ed88685STheodore Ts'o 			unmap_underlying_metadata(bdev, map.m_pblk + i);
17482fa3cdfbSTheodore Ts'o 	}
17492fa3cdfbSTheodore Ts'o 
17502fa3cdfbSTheodore Ts'o 	/*
175103f5d8bcSJan Kara 	 * Update on-disk size along with block allocation.
17522fa3cdfbSTheodore Ts'o 	 */
17532fa3cdfbSTheodore Ts'o 	disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
17542fa3cdfbSTheodore Ts'o 	if (disksize > i_size_read(mpd->inode))
17552fa3cdfbSTheodore Ts'o 		disksize = i_size_read(mpd->inode);
17562fa3cdfbSTheodore Ts'o 	if (disksize > EXT4_I(mpd->inode)->i_disksize) {
17572fa3cdfbSTheodore Ts'o 		ext4_update_i_disksize(mpd->inode, disksize);
17585a87b7a5STheodore Ts'o 		err = ext4_mark_inode_dirty(handle, mpd->inode);
17595a87b7a5STheodore Ts'o 		if (err)
17605a87b7a5STheodore Ts'o 			ext4_error(mpd->inode->i_sb,
17615a87b7a5STheodore Ts'o 				   "Failed to mark inode %lu dirty",
17625a87b7a5STheodore Ts'o 				   mpd->inode->i_ino);
17632fa3cdfbSTheodore Ts'o 	}
17642fa3cdfbSTheodore Ts'o 
17655a87b7a5STheodore Ts'o submit_io:
17661de3e3dfSTheodore Ts'o 	mpage_da_submit_io(mpd, mapp);
17675a87b7a5STheodore Ts'o 	mpd->io_done = 1;
176864769240SAlex Tomas }
176964769240SAlex Tomas 
1770bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1771bf068ee2SAneesh Kumar K.V 		(1 << BH_Delay) | (1 << BH_Unwritten))
177264769240SAlex Tomas 
177364769240SAlex Tomas /*
177464769240SAlex Tomas  * mpage_add_bh_to_extent - try to add one more block to extent of blocks
177564769240SAlex Tomas  *
177664769240SAlex Tomas  * @mpd->lbh - extent of blocks
177764769240SAlex Tomas  * @logical - logical number of the block in the file
1778b6a8e62fSJan Kara  * @b_state - b_state of the buffer head added
177964769240SAlex Tomas  *
178064769240SAlex Tomas  * the function is used to collect contig. blocks in same state
178164769240SAlex Tomas  */
1782b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical,
17838dc207c0STheodore Ts'o 				   unsigned long b_state)
178464769240SAlex Tomas {
178564769240SAlex Tomas 	sector_t next;
1786b6a8e62fSJan Kara 	int blkbits = mpd->inode->i_blkbits;
1787b6a8e62fSJan Kara 	int nrblocks = mpd->b_size >> blkbits;
178864769240SAlex Tomas 
1789c445e3e0SEric Sandeen 	/*
1790c445e3e0SEric Sandeen 	 * XXX Don't go larger than mballoc is willing to allocate
1791c445e3e0SEric Sandeen 	 * This is a stopgap solution.  We eventually need to fold
1792c445e3e0SEric Sandeen 	 * mpage_da_submit_io() into this function and then call
179379e83036SEric Sandeen 	 * ext4_map_blocks() multiple times in a loop
1794c445e3e0SEric Sandeen 	 */
1795b6a8e62fSJan Kara 	if (nrblocks >= (8*1024*1024 >> blkbits))
1796c445e3e0SEric Sandeen 		goto flush_it;
1797c445e3e0SEric Sandeen 
1798525f4ed8SMingming Cao 	/* check if the reserved journal credits might overflow */
1799b6a8e62fSJan Kara 	if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) {
1800525f4ed8SMingming Cao 		if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1801525f4ed8SMingming Cao 			/*
1802525f4ed8SMingming Cao 			 * With non-extent format we are limited by the journal
1803525f4ed8SMingming Cao 			 * credit available.  Total credit needed to insert
1804525f4ed8SMingming Cao 			 * nrblocks contiguous blocks is dependent on the
1805525f4ed8SMingming Cao 			 * nrblocks.  So limit nrblocks.
1806525f4ed8SMingming Cao 			 */
1807525f4ed8SMingming Cao 			goto flush_it;
1808525f4ed8SMingming Cao 		}
1809525f4ed8SMingming Cao 	}
181064769240SAlex Tomas 	/*
181164769240SAlex Tomas 	 * First block in the extent
181264769240SAlex Tomas 	 */
18138dc207c0STheodore Ts'o 	if (mpd->b_size == 0) {
18148dc207c0STheodore Ts'o 		mpd->b_blocknr = logical;
1815b6a8e62fSJan Kara 		mpd->b_size = 1 << blkbits;
18168dc207c0STheodore Ts'o 		mpd->b_state = b_state & BH_FLAGS;
181764769240SAlex Tomas 		return;
181864769240SAlex Tomas 	}
181964769240SAlex Tomas 
18208dc207c0STheodore Ts'o 	next = mpd->b_blocknr + nrblocks;
182164769240SAlex Tomas 	/*
182264769240SAlex Tomas 	 * Can we merge the block to our big extent?
182364769240SAlex Tomas 	 */
18248dc207c0STheodore Ts'o 	if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
1825b6a8e62fSJan Kara 		mpd->b_size += 1 << blkbits;
182664769240SAlex Tomas 		return;
182764769240SAlex Tomas 	}
182864769240SAlex Tomas 
1829525f4ed8SMingming Cao flush_it:
183064769240SAlex Tomas 	/*
183164769240SAlex Tomas 	 * We couldn't merge the block to our extent, so we
183264769240SAlex Tomas 	 * need to flush current  extent and start new one
183364769240SAlex Tomas 	 */
18345a87b7a5STheodore Ts'o 	mpage_da_map_and_submit(mpd);
1835a1d6cc56SAneesh Kumar K.V 	return;
183664769240SAlex Tomas }
183764769240SAlex Tomas 
1838c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
183929fa89d0SAneesh Kumar K.V {
1840c364b22cSAneesh Kumar K.V 	return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
184129fa89d0SAneesh Kumar K.V }
184229fa89d0SAneesh Kumar K.V 
184364769240SAlex Tomas /*
18445356f261SAditya Kali  * This function is grabs code from the very beginning of
18455356f261SAditya Kali  * ext4_map_blocks, but assumes that the caller is from delayed write
18465356f261SAditya Kali  * time. This function looks up the requested blocks and sets the
18475356f261SAditya Kali  * buffer delay bit under the protection of i_data_sem.
18485356f261SAditya Kali  */
18495356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
18505356f261SAditya Kali 			      struct ext4_map_blocks *map,
18515356f261SAditya Kali 			      struct buffer_head *bh)
18525356f261SAditya Kali {
1853d100eef2SZheng Liu 	struct extent_status es;
18545356f261SAditya Kali 	int retval;
18555356f261SAditya Kali 	sector_t invalid_block = ~((sector_t) 0xffff);
1856921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
1857921f266bSDmitry Monakhov 	struct ext4_map_blocks orig_map;
1858921f266bSDmitry Monakhov 
1859921f266bSDmitry Monakhov 	memcpy(&orig_map, map, sizeof(*map));
1860921f266bSDmitry Monakhov #endif
18615356f261SAditya Kali 
18625356f261SAditya Kali 	if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
18635356f261SAditya Kali 		invalid_block = ~0;
18645356f261SAditya Kali 
18655356f261SAditya Kali 	map->m_flags = 0;
18665356f261SAditya Kali 	ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
18675356f261SAditya Kali 		  "logical block %lu\n", inode->i_ino, map->m_len,
18685356f261SAditya Kali 		  (unsigned long) map->m_lblk);
1869d100eef2SZheng Liu 
1870d100eef2SZheng Liu 	/* Lookup extent status tree firstly */
1871d100eef2SZheng Liu 	if (ext4_es_lookup_extent(inode, iblock, &es)) {
1872d100eef2SZheng Liu 
1873d100eef2SZheng Liu 		if (ext4_es_is_hole(&es)) {
1874d100eef2SZheng Liu 			retval = 0;
1875d100eef2SZheng Liu 			down_read((&EXT4_I(inode)->i_data_sem));
1876d100eef2SZheng Liu 			goto add_delayed;
1877d100eef2SZheng Liu 		}
1878d100eef2SZheng Liu 
1879d100eef2SZheng Liu 		/*
1880d100eef2SZheng Liu 		 * Delayed extent could be allocated by fallocate.
1881d100eef2SZheng Liu 		 * So we need to check it.
1882d100eef2SZheng Liu 		 */
1883d100eef2SZheng Liu 		if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1884d100eef2SZheng Liu 			map_bh(bh, inode->i_sb, invalid_block);
1885d100eef2SZheng Liu 			set_buffer_new(bh);
1886d100eef2SZheng Liu 			set_buffer_delay(bh);
1887d100eef2SZheng Liu 			return 0;
1888d100eef2SZheng Liu 		}
1889d100eef2SZheng Liu 
1890d100eef2SZheng Liu 		map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1891d100eef2SZheng Liu 		retval = es.es_len - (iblock - es.es_lblk);
1892d100eef2SZheng Liu 		if (retval > map->m_len)
1893d100eef2SZheng Liu 			retval = map->m_len;
1894d100eef2SZheng Liu 		map->m_len = retval;
1895d100eef2SZheng Liu 		if (ext4_es_is_written(&es))
1896d100eef2SZheng Liu 			map->m_flags |= EXT4_MAP_MAPPED;
1897d100eef2SZheng Liu 		else if (ext4_es_is_unwritten(&es))
1898d100eef2SZheng Liu 			map->m_flags |= EXT4_MAP_UNWRITTEN;
1899d100eef2SZheng Liu 		else
1900d100eef2SZheng Liu 			BUG_ON(1);
1901d100eef2SZheng Liu 
1902921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
1903921f266bSDmitry Monakhov 		ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1904921f266bSDmitry Monakhov #endif
1905d100eef2SZheng Liu 		return retval;
1906d100eef2SZheng Liu 	}
1907d100eef2SZheng Liu 
19085356f261SAditya Kali 	/*
19095356f261SAditya Kali 	 * Try to see if we can get the block without requesting a new
19105356f261SAditya Kali 	 * file system block.
19115356f261SAditya Kali 	 */
19125356f261SAditya Kali 	down_read((&EXT4_I(inode)->i_data_sem));
19139c3569b5STao Ma 	if (ext4_has_inline_data(inode)) {
19149c3569b5STao Ma 		/*
19159c3569b5STao Ma 		 * We will soon create blocks for this page, and let
19169c3569b5STao Ma 		 * us pretend as if the blocks aren't allocated yet.
19179c3569b5STao Ma 		 * In case of clusters, we have to handle the work
19189c3569b5STao Ma 		 * of mapping from cluster so that the reserved space
19199c3569b5STao Ma 		 * is calculated properly.
19209c3569b5STao Ma 		 */
19219c3569b5STao Ma 		if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
19229c3569b5STao Ma 		    ext4_find_delalloc_cluster(inode, map->m_lblk))
19239c3569b5STao Ma 			map->m_flags |= EXT4_MAP_FROM_CLUSTER;
19249c3569b5STao Ma 		retval = 0;
19259c3569b5STao Ma 	} else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
1926d100eef2SZheng Liu 		retval = ext4_ext_map_blocks(NULL, inode, map,
1927d100eef2SZheng Liu 					     EXT4_GET_BLOCKS_NO_PUT_HOLE);
19285356f261SAditya Kali 	else
1929d100eef2SZheng Liu 		retval = ext4_ind_map_blocks(NULL, inode, map,
1930d100eef2SZheng Liu 					     EXT4_GET_BLOCKS_NO_PUT_HOLE);
19315356f261SAditya Kali 
1932d100eef2SZheng Liu add_delayed:
19335356f261SAditya Kali 	if (retval == 0) {
1934f7fec032SZheng Liu 		int ret;
19355356f261SAditya Kali 		/*
19365356f261SAditya Kali 		 * XXX: __block_prepare_write() unmaps passed block,
19375356f261SAditya Kali 		 * is it OK?
19385356f261SAditya Kali 		 */
1939386ad67cSLukas Czerner 		/*
1940386ad67cSLukas Czerner 		 * If the block was allocated from previously allocated cluster,
1941386ad67cSLukas Czerner 		 * then we don't need to reserve it again. However we still need
1942386ad67cSLukas Czerner 		 * to reserve metadata for every block we're going to write.
1943386ad67cSLukas Czerner 		 */
19445356f261SAditya Kali 		if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
1945f7fec032SZheng Liu 			ret = ext4_da_reserve_space(inode, iblock);
1946f7fec032SZheng Liu 			if (ret) {
19475356f261SAditya Kali 				/* not enough space to reserve */
1948f7fec032SZheng Liu 				retval = ret;
19495356f261SAditya Kali 				goto out_unlock;
19505356f261SAditya Kali 			}
1951386ad67cSLukas Czerner 		} else {
1952386ad67cSLukas Czerner 			ret = ext4_da_reserve_metadata(inode, iblock);
1953386ad67cSLukas Czerner 			if (ret) {
1954386ad67cSLukas Czerner 				/* not enough space to reserve */
1955386ad67cSLukas Czerner 				retval = ret;
1956386ad67cSLukas Czerner 				goto out_unlock;
1957386ad67cSLukas Czerner 			}
1958f7fec032SZheng Liu 		}
19595356f261SAditya Kali 
1960f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1961fdc0212eSZheng Liu 					    ~0, EXTENT_STATUS_DELAYED);
1962f7fec032SZheng Liu 		if (ret) {
1963f7fec032SZheng Liu 			retval = ret;
196451865fdaSZheng Liu 			goto out_unlock;
1965f7fec032SZheng Liu 		}
196651865fdaSZheng Liu 
19675356f261SAditya Kali 		/* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
19685356f261SAditya Kali 		 * and it should not appear on the bh->b_state.
19695356f261SAditya Kali 		 */
19705356f261SAditya Kali 		map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
19715356f261SAditya Kali 
19725356f261SAditya Kali 		map_bh(bh, inode->i_sb, invalid_block);
19735356f261SAditya Kali 		set_buffer_new(bh);
19745356f261SAditya Kali 		set_buffer_delay(bh);
1975f7fec032SZheng Liu 	} else if (retval > 0) {
1976f7fec032SZheng Liu 		int ret;
1977f7fec032SZheng Liu 		unsigned long long status;
1978f7fec032SZheng Liu 
1979921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST
1980921f266bSDmitry Monakhov 		if (retval != map->m_len) {
1981921f266bSDmitry Monakhov 			printk("ES len assertation failed for inode: %lu "
1982921f266bSDmitry Monakhov 			       "retval %d != map->m_len %d "
1983921f266bSDmitry Monakhov 			       "in %s (lookup)\n", inode->i_ino, retval,
1984921f266bSDmitry Monakhov 			       map->m_len, __func__);
1985921f266bSDmitry Monakhov 		}
1986921f266bSDmitry Monakhov #endif
1987921f266bSDmitry Monakhov 
1988f7fec032SZheng Liu 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1989f7fec032SZheng Liu 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1990f7fec032SZheng Liu 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1991f7fec032SZheng Liu 					    map->m_pblk, status);
1992f7fec032SZheng Liu 		if (ret != 0)
1993f7fec032SZheng Liu 			retval = ret;
19945356f261SAditya Kali 	}
19955356f261SAditya Kali 
19965356f261SAditya Kali out_unlock:
19975356f261SAditya Kali 	up_read((&EXT4_I(inode)->i_data_sem));
19985356f261SAditya Kali 
19995356f261SAditya Kali 	return retval;
20005356f261SAditya Kali }
20015356f261SAditya Kali 
20025356f261SAditya Kali /*
2003b920c755STheodore Ts'o  * This is a special get_blocks_t callback which is used by
2004b920c755STheodore Ts'o  * ext4_da_write_begin().  It will either return mapped block or
2005b920c755STheodore Ts'o  * reserve space for a single block.
200629fa89d0SAneesh Kumar K.V  *
200729fa89d0SAneesh Kumar K.V  * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
200829fa89d0SAneesh Kumar K.V  * We also have b_blocknr = -1 and b_bdev initialized properly
200929fa89d0SAneesh Kumar K.V  *
201029fa89d0SAneesh Kumar K.V  * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
201129fa89d0SAneesh Kumar K.V  * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
201229fa89d0SAneesh Kumar K.V  * initialized properly.
201364769240SAlex Tomas  */
20149c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
20152ed88685STheodore Ts'o 			   struct buffer_head *bh, int create)
201664769240SAlex Tomas {
20172ed88685STheodore Ts'o 	struct ext4_map_blocks map;
201864769240SAlex Tomas 	int ret = 0;
201964769240SAlex Tomas 
202064769240SAlex Tomas 	BUG_ON(create == 0);
20212ed88685STheodore Ts'o 	BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
20222ed88685STheodore Ts'o 
20232ed88685STheodore Ts'o 	map.m_lblk = iblock;
20242ed88685STheodore Ts'o 	map.m_len = 1;
202564769240SAlex Tomas 
202664769240SAlex Tomas 	/*
202764769240SAlex Tomas 	 * first, we need to know whether the block is allocated already
202864769240SAlex Tomas 	 * preallocated blocks are unmapped but should treated
202964769240SAlex Tomas 	 * the same as allocated blocks.
203064769240SAlex Tomas 	 */
20315356f261SAditya Kali 	ret = ext4_da_map_blocks(inode, iblock, &map, bh);
20325356f261SAditya Kali 	if (ret <= 0)
20332ed88685STheodore Ts'o 		return ret;
203464769240SAlex Tomas 
20352ed88685STheodore Ts'o 	map_bh(bh, inode->i_sb, map.m_pblk);
20362ed88685STheodore Ts'o 	bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
20372ed88685STheodore Ts'o 
20382ed88685STheodore Ts'o 	if (buffer_unwritten(bh)) {
20392ed88685STheodore Ts'o 		/* A delayed write to unwritten bh should be marked
20402ed88685STheodore Ts'o 		 * new and mapped.  Mapped ensures that we don't do
20412ed88685STheodore Ts'o 		 * get_block multiple times when we write to the same
20422ed88685STheodore Ts'o 		 * offset and new ensures that we do proper zero out
20432ed88685STheodore Ts'o 		 * for partial write.
20442ed88685STheodore Ts'o 		 */
20452ed88685STheodore Ts'o 		set_buffer_new(bh);
2046c8205636STheodore Ts'o 		set_buffer_mapped(bh);
20472ed88685STheodore Ts'o 	}
20482ed88685STheodore Ts'o 	return 0;
204964769240SAlex Tomas }
205061628a3fSMingming Cao 
205162e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh)
205262e086beSAneesh Kumar K.V {
205362e086beSAneesh Kumar K.V 	get_bh(bh);
205462e086beSAneesh Kumar K.V 	return 0;
205562e086beSAneesh Kumar K.V }
205662e086beSAneesh Kumar K.V 
205762e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh)
205862e086beSAneesh Kumar K.V {
205962e086beSAneesh Kumar K.V 	put_bh(bh);
206062e086beSAneesh Kumar K.V 	return 0;
206162e086beSAneesh Kumar K.V }
206262e086beSAneesh Kumar K.V 
206362e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page,
206462e086beSAneesh Kumar K.V 				       unsigned int len)
206562e086beSAneesh Kumar K.V {
206662e086beSAneesh Kumar K.V 	struct address_space *mapping = page->mapping;
206762e086beSAneesh Kumar K.V 	struct inode *inode = mapping->host;
20683fdcfb66STao Ma 	struct buffer_head *page_bufs = NULL;
206962e086beSAneesh Kumar K.V 	handle_t *handle = NULL;
20703fdcfb66STao Ma 	int ret = 0, err = 0;
20713fdcfb66STao Ma 	int inline_data = ext4_has_inline_data(inode);
20723fdcfb66STao Ma 	struct buffer_head *inode_bh = NULL;
207362e086beSAneesh Kumar K.V 
2074cb20d518STheodore Ts'o 	ClearPageChecked(page);
20753fdcfb66STao Ma 
20763fdcfb66STao Ma 	if (inline_data) {
20773fdcfb66STao Ma 		BUG_ON(page->index != 0);
20783fdcfb66STao Ma 		BUG_ON(len > ext4_get_max_inline_size(inode));
20793fdcfb66STao Ma 		inode_bh = ext4_journalled_write_inline_data(inode, len, page);
20803fdcfb66STao Ma 		if (inode_bh == NULL)
20813fdcfb66STao Ma 			goto out;
20823fdcfb66STao Ma 	} else {
208362e086beSAneesh Kumar K.V 		page_bufs = page_buffers(page);
20843fdcfb66STao Ma 		if (!page_bufs) {
20853fdcfb66STao Ma 			BUG();
20863fdcfb66STao Ma 			goto out;
20873fdcfb66STao Ma 		}
20883fdcfb66STao Ma 		ext4_walk_page_buffers(handle, page_bufs, 0, len,
20893fdcfb66STao Ma 				       NULL, bget_one);
20903fdcfb66STao Ma 	}
209162e086beSAneesh Kumar K.V 	/* As soon as we unlock the page, it can go away, but we have
209262e086beSAneesh Kumar K.V 	 * references to buffers so we are safe */
209362e086beSAneesh Kumar K.V 	unlock_page(page);
209462e086beSAneesh Kumar K.V 
20959924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
20969924a92aSTheodore Ts'o 				    ext4_writepage_trans_blocks(inode));
209762e086beSAneesh Kumar K.V 	if (IS_ERR(handle)) {
209862e086beSAneesh Kumar K.V 		ret = PTR_ERR(handle);
209962e086beSAneesh Kumar K.V 		goto out;
210062e086beSAneesh Kumar K.V 	}
210162e086beSAneesh Kumar K.V 
2102441c8508SCurt Wohlgemuth 	BUG_ON(!ext4_handle_valid(handle));
2103441c8508SCurt Wohlgemuth 
21043fdcfb66STao Ma 	if (inline_data) {
21053fdcfb66STao Ma 		ret = ext4_journal_get_write_access(handle, inode_bh);
21063fdcfb66STao Ma 
21073fdcfb66STao Ma 		err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
21083fdcfb66STao Ma 
21093fdcfb66STao Ma 	} else {
2110f19d5870STao Ma 		ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
211162e086beSAneesh Kumar K.V 					     do_journal_get_write_access);
211262e086beSAneesh Kumar K.V 
2113f19d5870STao Ma 		err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
211462e086beSAneesh Kumar K.V 					     write_end_fn);
21153fdcfb66STao Ma 	}
211662e086beSAneesh Kumar K.V 	if (ret == 0)
211762e086beSAneesh Kumar K.V 		ret = err;
21182d859db3SJan Kara 	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
211962e086beSAneesh Kumar K.V 	err = ext4_journal_stop(handle);
212062e086beSAneesh Kumar K.V 	if (!ret)
212162e086beSAneesh Kumar K.V 		ret = err;
212262e086beSAneesh Kumar K.V 
21233fdcfb66STao Ma 	if (!ext4_has_inline_data(inode))
21243fdcfb66STao Ma 		ext4_walk_page_buffers(handle, page_bufs, 0, len,
21253fdcfb66STao Ma 				       NULL, bput_one);
212619f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
212762e086beSAneesh Kumar K.V out:
21283fdcfb66STao Ma 	brelse(inode_bh);
212962e086beSAneesh Kumar K.V 	return ret;
213062e086beSAneesh Kumar K.V }
213162e086beSAneesh Kumar K.V 
213261628a3fSMingming Cao /*
213343ce1d23SAneesh Kumar K.V  * Note that we don't need to start a transaction unless we're journaling data
213443ce1d23SAneesh Kumar K.V  * because we should have holes filled from ext4_page_mkwrite(). We even don't
213543ce1d23SAneesh Kumar K.V  * need to file the inode to the transaction's list in ordered mode because if
213643ce1d23SAneesh Kumar K.V  * we are writing back data added by write(), the inode is already there and if
213743ce1d23SAneesh Kumar K.V  * we are writing back data modified via mmap(), no one guarantees in which
213843ce1d23SAneesh Kumar K.V  * transaction the data will hit the disk. In case we are journaling data, we
213943ce1d23SAneesh Kumar K.V  * cannot start transaction directly because transaction start ranks above page
214043ce1d23SAneesh Kumar K.V  * lock so we have to do some magic.
214143ce1d23SAneesh Kumar K.V  *
2142b920c755STheodore Ts'o  * This function can get called via...
2143b920c755STheodore Ts'o  *   - ext4_da_writepages after taking page lock (have journal handle)
2144b920c755STheodore Ts'o  *   - journal_submit_inode_data_buffers (no journal handle)
2145f6463b0dSArtem Bityutskiy  *   - shrink_page_list via the kswapd/direct reclaim (no journal handle)
2146b920c755STheodore Ts'o  *   - grab_page_cache when doing write_begin (have journal handle)
214743ce1d23SAneesh Kumar K.V  *
214843ce1d23SAneesh Kumar K.V  * We don't do any block allocation in this function. If we have page with
214943ce1d23SAneesh Kumar K.V  * multiple blocks we need to write those buffer_heads that are mapped. This
215043ce1d23SAneesh Kumar K.V  * is important for mmaped based write. So if we do with blocksize 1K
215143ce1d23SAneesh Kumar K.V  * truncate(f, 1024);
215243ce1d23SAneesh Kumar K.V  * a = mmap(f, 0, 4096);
215343ce1d23SAneesh Kumar K.V  * a[0] = 'a';
215443ce1d23SAneesh Kumar K.V  * truncate(f, 4096);
215543ce1d23SAneesh Kumar K.V  * we have in the page first buffer_head mapped via page_mkwrite call back
215690802ed9SPaul Bolle  * but other buffer_heads would be unmapped but dirty (dirty done via the
215743ce1d23SAneesh Kumar K.V  * do_wp_page). So writepage should write the first block. If we modify
215843ce1d23SAneesh Kumar K.V  * the mmap area beyond 1024 we will again get a page_fault and the
215943ce1d23SAneesh Kumar K.V  * page_mkwrite callback will do the block allocation and mark the
216043ce1d23SAneesh Kumar K.V  * buffer_heads mapped.
216143ce1d23SAneesh Kumar K.V  *
216243ce1d23SAneesh Kumar K.V  * We redirty the page if we have any buffer_heads that is either delay or
216343ce1d23SAneesh Kumar K.V  * unwritten in the page.
216443ce1d23SAneesh Kumar K.V  *
216543ce1d23SAneesh Kumar K.V  * We can get recursively called as show below.
216643ce1d23SAneesh Kumar K.V  *
216743ce1d23SAneesh Kumar K.V  *	ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
216843ce1d23SAneesh Kumar K.V  *		ext4_writepage()
216943ce1d23SAneesh Kumar K.V  *
217043ce1d23SAneesh Kumar K.V  * But since we don't do any block allocation we should not deadlock.
217143ce1d23SAneesh Kumar K.V  * Page also have the dirty flag cleared so we don't get recurive page_lock.
217261628a3fSMingming Cao  */
217343ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page,
217464769240SAlex Tomas 			  struct writeback_control *wbc)
217564769240SAlex Tomas {
2176f8bec370SJan Kara 	int ret = 0;
217761628a3fSMingming Cao 	loff_t size;
2178498e5f24STheodore Ts'o 	unsigned int len;
2179744692dcSJiaying Zhang 	struct buffer_head *page_bufs = NULL;
218061628a3fSMingming Cao 	struct inode *inode = page->mapping->host;
218136ade451SJan Kara 	struct ext4_io_submit io_submit;
218264769240SAlex Tomas 
2183a9c667f8SLukas Czerner 	trace_ext4_writepage(page);
218461628a3fSMingming Cao 	size = i_size_read(inode);
218561628a3fSMingming Cao 	if (page->index == size >> PAGE_CACHE_SHIFT)
218661628a3fSMingming Cao 		len = size & ~PAGE_CACHE_MASK;
218761628a3fSMingming Cao 	else
218861628a3fSMingming Cao 		len = PAGE_CACHE_SIZE;
218961628a3fSMingming Cao 
2190f0e6c985SAneesh Kumar K.V 	page_bufs = page_buffers(page);
219164769240SAlex Tomas 	/*
2192fe386132SJan Kara 	 * We cannot do block allocation or other extent handling in this
2193fe386132SJan Kara 	 * function. If there are buffers needing that, we have to redirty
2194fe386132SJan Kara 	 * the page. But we may reach here when we do a journal commit via
2195fe386132SJan Kara 	 * journal_submit_inode_data_buffers() and in that case we must write
2196fe386132SJan Kara 	 * allocated buffers to achieve data=ordered mode guarantees.
219764769240SAlex Tomas 	 */
2198f19d5870STao Ma 	if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2199c364b22cSAneesh Kumar K.V 				   ext4_bh_delay_or_unwritten)) {
220061628a3fSMingming Cao 		redirty_page_for_writepage(wbc, page);
2201fe386132SJan Kara 		if (current->flags & PF_MEMALLOC) {
2202fe386132SJan Kara 			/*
2203fe386132SJan Kara 			 * For memory cleaning there's no point in writing only
2204fe386132SJan Kara 			 * some buffers. So just bail out. Warn if we came here
2205fe386132SJan Kara 			 * from direct reclaim.
2206fe386132SJan Kara 			 */
2207fe386132SJan Kara 			WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2208fe386132SJan Kara 							== PF_MEMALLOC);
220961628a3fSMingming Cao 			unlock_page(page);
221061628a3fSMingming Cao 			return 0;
221161628a3fSMingming Cao 		}
2212f0e6c985SAneesh Kumar K.V 	}
221364769240SAlex Tomas 
2214cb20d518STheodore Ts'o 	if (PageChecked(page) && ext4_should_journal_data(inode))
221543ce1d23SAneesh Kumar K.V 		/*
221643ce1d23SAneesh Kumar K.V 		 * It's mmapped pagecache.  Add buffers and journal it.  There
221743ce1d23SAneesh Kumar K.V 		 * doesn't seem much point in redirtying the page here.
221843ce1d23SAneesh Kumar K.V 		 */
22193f0ca309SWu Fengguang 		return __ext4_journalled_writepage(page, len);
222043ce1d23SAneesh Kumar K.V 
222136ade451SJan Kara 	memset(&io_submit, 0, sizeof(io_submit));
222236ade451SJan Kara 	ret = ext4_bio_write_page(&io_submit, page, len, wbc);
222336ade451SJan Kara 	ext4_io_submit(&io_submit);
222464769240SAlex Tomas 	return ret;
222564769240SAlex Tomas }
222664769240SAlex Tomas 
222761628a3fSMingming Cao /*
2228525f4ed8SMingming Cao  * This is called via ext4_da_writepages() to
222925985edcSLucas De Marchi  * calculate the total number of credits to reserve to fit
2230525f4ed8SMingming Cao  * a single extent allocation into a single transaction,
2231525f4ed8SMingming Cao  * ext4_da_writpeages() will loop calling this before
2232525f4ed8SMingming Cao  * the block allocation.
223361628a3fSMingming Cao  */
2234525f4ed8SMingming Cao 
2235525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode)
2236525f4ed8SMingming Cao {
2237525f4ed8SMingming Cao 	int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2238525f4ed8SMingming Cao 
2239525f4ed8SMingming Cao 	/*
2240525f4ed8SMingming Cao 	 * With non-extent format the journal credit needed to
2241525f4ed8SMingming Cao 	 * insert nrblocks contiguous block is dependent on
2242525f4ed8SMingming Cao 	 * number of contiguous block. So we will limit
2243525f4ed8SMingming Cao 	 * number of contiguous block to a sane value
2244525f4ed8SMingming Cao 	 */
224512e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
2246525f4ed8SMingming Cao 	    (max_blocks > EXT4_MAX_TRANS_DATA))
2247525f4ed8SMingming Cao 		max_blocks = EXT4_MAX_TRANS_DATA;
2248525f4ed8SMingming Cao 
2249525f4ed8SMingming Cao 	return ext4_chunk_trans_blocks(inode, max_blocks);
2250525f4ed8SMingming Cao }
225161628a3fSMingming Cao 
22528e48dcfbSTheodore Ts'o /*
22538e48dcfbSTheodore Ts'o  * write_cache_pages_da - walk the list of dirty pages of the given
22548eb9e5ceSTheodore Ts'o  * address space and accumulate pages that need writing, and call
2255168fc022STheodore Ts'o  * mpage_da_map_and_submit to map a single contiguous memory region
2256168fc022STheodore Ts'o  * and then write them.
22578e48dcfbSTheodore Ts'o  */
22589c3569b5STao Ma static int write_cache_pages_da(handle_t *handle,
22599c3569b5STao Ma 				struct address_space *mapping,
22608e48dcfbSTheodore Ts'o 				struct writeback_control *wbc,
226172f84e65SEric Sandeen 				struct mpage_da_data *mpd,
226272f84e65SEric Sandeen 				pgoff_t *done_index)
22638e48dcfbSTheodore Ts'o {
22648eb9e5ceSTheodore Ts'o 	struct buffer_head	*bh, *head;
2265168fc022STheodore Ts'o 	struct inode		*inode = mapping->host;
22668e48dcfbSTheodore Ts'o 	struct pagevec		pvec;
22674f01b02cSTheodore Ts'o 	unsigned int		nr_pages;
22684f01b02cSTheodore Ts'o 	sector_t		logical;
22694f01b02cSTheodore Ts'o 	pgoff_t			index, end;
22708e48dcfbSTheodore Ts'o 	long			nr_to_write = wbc->nr_to_write;
22714f01b02cSTheodore Ts'o 	int			i, tag, ret = 0;
22728e48dcfbSTheodore Ts'o 
2273168fc022STheodore Ts'o 	memset(mpd, 0, sizeof(struct mpage_da_data));
2274168fc022STheodore Ts'o 	mpd->wbc = wbc;
2275168fc022STheodore Ts'o 	mpd->inode = inode;
22768e48dcfbSTheodore Ts'o 	pagevec_init(&pvec, 0);
22778e48dcfbSTheodore Ts'o 	index = wbc->range_start >> PAGE_CACHE_SHIFT;
22788e48dcfbSTheodore Ts'o 	end = wbc->range_end >> PAGE_CACHE_SHIFT;
22798e48dcfbSTheodore Ts'o 
22806e6938b6SWu Fengguang 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
22815b41d924SEric Sandeen 		tag = PAGECACHE_TAG_TOWRITE;
22825b41d924SEric Sandeen 	else
22835b41d924SEric Sandeen 		tag = PAGECACHE_TAG_DIRTY;
22845b41d924SEric Sandeen 
228572f84e65SEric Sandeen 	*done_index = index;
22864f01b02cSTheodore Ts'o 	while (index <= end) {
22875b41d924SEric Sandeen 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
22888e48dcfbSTheodore Ts'o 			      min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
22898e48dcfbSTheodore Ts'o 		if (nr_pages == 0)
22904f01b02cSTheodore Ts'o 			return 0;
22918e48dcfbSTheodore Ts'o 
22928e48dcfbSTheodore Ts'o 		for (i = 0; i < nr_pages; i++) {
22938e48dcfbSTheodore Ts'o 			struct page *page = pvec.pages[i];
22948e48dcfbSTheodore Ts'o 
22958e48dcfbSTheodore Ts'o 			/*
22968e48dcfbSTheodore Ts'o 			 * At this point, the page may be truncated or
22978e48dcfbSTheodore Ts'o 			 * invalidated (changing page->mapping to NULL), or
22988e48dcfbSTheodore Ts'o 			 * even swizzled back from swapper_space to tmpfs file
22998e48dcfbSTheodore Ts'o 			 * mapping. However, page->index will not change
23008e48dcfbSTheodore Ts'o 			 * because we have a reference on the page.
23018e48dcfbSTheodore Ts'o 			 */
23024f01b02cSTheodore Ts'o 			if (page->index > end)
23034f01b02cSTheodore Ts'o 				goto out;
23048e48dcfbSTheodore Ts'o 
230572f84e65SEric Sandeen 			*done_index = page->index + 1;
230672f84e65SEric Sandeen 
230778aaced3STheodore Ts'o 			/*
230878aaced3STheodore Ts'o 			 * If we can't merge this page, and we have
230978aaced3STheodore Ts'o 			 * accumulated an contiguous region, write it
231078aaced3STheodore Ts'o 			 */
231178aaced3STheodore Ts'o 			if ((mpd->next_page != page->index) &&
231278aaced3STheodore Ts'o 			    (mpd->next_page != mpd->first_page)) {
231378aaced3STheodore Ts'o 				mpage_da_map_and_submit(mpd);
231478aaced3STheodore Ts'o 				goto ret_extent_tail;
231578aaced3STheodore Ts'o 			}
231678aaced3STheodore Ts'o 
23178e48dcfbSTheodore Ts'o 			lock_page(page);
23188e48dcfbSTheodore Ts'o 
23198e48dcfbSTheodore Ts'o 			/*
23204f01b02cSTheodore Ts'o 			 * If the page is no longer dirty, or its
23214f01b02cSTheodore Ts'o 			 * mapping no longer corresponds to inode we
23224f01b02cSTheodore Ts'o 			 * are writing (which means it has been
23234f01b02cSTheodore Ts'o 			 * truncated or invalidated), or the page is
23244f01b02cSTheodore Ts'o 			 * already under writeback and we are not
23254f01b02cSTheodore Ts'o 			 * doing a data integrity writeback, skip the page
23268e48dcfbSTheodore Ts'o 			 */
23274f01b02cSTheodore Ts'o 			if (!PageDirty(page) ||
23284f01b02cSTheodore Ts'o 			    (PageWriteback(page) &&
23294f01b02cSTheodore Ts'o 			     (wbc->sync_mode == WB_SYNC_NONE)) ||
23304f01b02cSTheodore Ts'o 			    unlikely(page->mapping != mapping)) {
23318e48dcfbSTheodore Ts'o 				unlock_page(page);
23328e48dcfbSTheodore Ts'o 				continue;
23338e48dcfbSTheodore Ts'o 			}
23348e48dcfbSTheodore Ts'o 
23358e48dcfbSTheodore Ts'o 			wait_on_page_writeback(page);
23368e48dcfbSTheodore Ts'o 			BUG_ON(PageWriteback(page));
23378e48dcfbSTheodore Ts'o 
23389c3569b5STao Ma 			/*
23399c3569b5STao Ma 			 * If we have inline data and arrive here, it means that
23409c3569b5STao Ma 			 * we will soon create the block for the 1st page, so
23419c3569b5STao Ma 			 * we'd better clear the inline data here.
23429c3569b5STao Ma 			 */
23439c3569b5STao Ma 			if (ext4_has_inline_data(inode)) {
23449c3569b5STao Ma 				BUG_ON(ext4_test_inode_state(inode,
23459c3569b5STao Ma 						EXT4_STATE_MAY_INLINE_DATA));
23469c3569b5STao Ma 				ext4_destroy_inline_data(handle, inode);
23479c3569b5STao Ma 			}
23489c3569b5STao Ma 
2349168fc022STheodore Ts'o 			if (mpd->next_page != page->index)
23508eb9e5ceSTheodore Ts'o 				mpd->first_page = page->index;
23518eb9e5ceSTheodore Ts'o 			mpd->next_page = page->index + 1;
23528eb9e5ceSTheodore Ts'o 			logical = (sector_t) page->index <<
23538eb9e5ceSTheodore Ts'o 				(PAGE_CACHE_SHIFT - inode->i_blkbits);
23548eb9e5ceSTheodore Ts'o 
2355f8bec370SJan Kara 			/* Add all dirty buffers to mpd */
23568eb9e5ceSTheodore Ts'o 			head = page_buffers(page);
23578eb9e5ceSTheodore Ts'o 			bh = head;
23588eb9e5ceSTheodore Ts'o 			do {
23598eb9e5ceSTheodore Ts'o 				BUG_ON(buffer_locked(bh));
23608eb9e5ceSTheodore Ts'o 				/*
2361f8bec370SJan Kara 				 * We need to try to allocate unmapped blocks
2362f8bec370SJan Kara 				 * in the same page.  Otherwise we won't make
2363f8bec370SJan Kara 				 * progress with the page in ext4_writepage
23648eb9e5ceSTheodore Ts'o 				 */
23658eb9e5ceSTheodore Ts'o 				if (ext4_bh_delay_or_unwritten(NULL, bh)) {
23668eb9e5ceSTheodore Ts'o 					mpage_add_bh_to_extent(mpd, logical,
23678eb9e5ceSTheodore Ts'o 							       bh->b_state);
23684f01b02cSTheodore Ts'o 					if (mpd->io_done)
23694f01b02cSTheodore Ts'o 						goto ret_extent_tail;
2370f8bec370SJan Kara 				} else if (buffer_dirty(bh) &&
2371f8bec370SJan Kara 					   buffer_mapped(bh)) {
23728eb9e5ceSTheodore Ts'o 					/*
2373f8bec370SJan Kara 					 * mapped dirty buffer. We need to
2374f8bec370SJan Kara 					 * update the b_state because we look
2375f8bec370SJan Kara 					 * at b_state in mpage_da_map_blocks.
2376f8bec370SJan Kara 					 * We don't update b_size because if we
2377f8bec370SJan Kara 					 * find an unmapped buffer_head later
2378f8bec370SJan Kara 					 * we need to use the b_state flag of
2379f8bec370SJan Kara 					 * that buffer_head.
23808eb9e5ceSTheodore Ts'o 					 */
23818eb9e5ceSTheodore Ts'o 					if (mpd->b_size == 0)
2382f8bec370SJan Kara 						mpd->b_state =
2383f8bec370SJan Kara 							bh->b_state & BH_FLAGS;
23848e48dcfbSTheodore Ts'o 				}
23858eb9e5ceSTheodore Ts'o 				logical++;
23868eb9e5ceSTheodore Ts'o 			} while ((bh = bh->b_this_page) != head);
23878e48dcfbSTheodore Ts'o 
23888e48dcfbSTheodore Ts'o 			if (nr_to_write > 0) {
23898e48dcfbSTheodore Ts'o 				nr_to_write--;
23908e48dcfbSTheodore Ts'o 				if (nr_to_write == 0 &&
23914f01b02cSTheodore Ts'o 				    wbc->sync_mode == WB_SYNC_NONE)
23928e48dcfbSTheodore Ts'o 					/*
23938e48dcfbSTheodore Ts'o 					 * We stop writing back only if we are
23948e48dcfbSTheodore Ts'o 					 * not doing integrity sync. In case of
23958e48dcfbSTheodore Ts'o 					 * integrity sync we have to keep going
23968e48dcfbSTheodore Ts'o 					 * because someone may be concurrently
23978e48dcfbSTheodore Ts'o 					 * dirtying pages, and we might have
23988e48dcfbSTheodore Ts'o 					 * synced a lot of newly appeared dirty
23998e48dcfbSTheodore Ts'o 					 * pages, but have not synced all of the
24008e48dcfbSTheodore Ts'o 					 * old dirty pages.
24018e48dcfbSTheodore Ts'o 					 */
24024f01b02cSTheodore Ts'o 					goto out;
24038e48dcfbSTheodore Ts'o 			}
24048e48dcfbSTheodore Ts'o 		}
24058e48dcfbSTheodore Ts'o 		pagevec_release(&pvec);
24068e48dcfbSTheodore Ts'o 		cond_resched();
24078e48dcfbSTheodore Ts'o 	}
24084f01b02cSTheodore Ts'o 	return 0;
24094f01b02cSTheodore Ts'o ret_extent_tail:
24104f01b02cSTheodore Ts'o 	ret = MPAGE_DA_EXTENT_TAIL;
24118eb9e5ceSTheodore Ts'o out:
24128eb9e5ceSTheodore Ts'o 	pagevec_release(&pvec);
24138eb9e5ceSTheodore Ts'o 	cond_resched();
24148e48dcfbSTheodore Ts'o 	return ret;
24158e48dcfbSTheodore Ts'o }
24168e48dcfbSTheodore Ts'o 
24178e48dcfbSTheodore Ts'o 
241864769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping,
241964769240SAlex Tomas 			      struct writeback_control *wbc)
242064769240SAlex Tomas {
242122208dedSAneesh Kumar K.V 	pgoff_t	index;
242222208dedSAneesh Kumar K.V 	int range_whole = 0;
242361628a3fSMingming Cao 	handle_t *handle = NULL;
2424df22291fSAneesh Kumar K.V 	struct mpage_da_data mpd;
24255e745b04SAneesh Kumar K.V 	struct inode *inode = mapping->host;
2426498e5f24STheodore Ts'o 	int pages_written = 0;
242755138e0bSTheodore Ts'o 	unsigned int max_pages;
24282acf2c26SAneesh Kumar K.V 	int range_cyclic, cycled = 1, io_done = 0;
242955138e0bSTheodore Ts'o 	int needed_blocks, ret = 0;
243055138e0bSTheodore Ts'o 	long desired_nr_to_write, nr_to_writebump = 0;
2431de89de6eSTheodore Ts'o 	loff_t range_start = wbc->range_start;
24325e745b04SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
243372f84e65SEric Sandeen 	pgoff_t done_index = 0;
24345b41d924SEric Sandeen 	pgoff_t end;
24351bce63d1SShaohua Li 	struct blk_plug plug;
243661628a3fSMingming Cao 
24379bffad1eSTheodore Ts'o 	trace_ext4_da_writepages(inode, wbc);
2438ba80b101STheodore Ts'o 
243961628a3fSMingming Cao 	/*
244061628a3fSMingming Cao 	 * No pages to write? This is mainly a kludge to avoid starting
244161628a3fSMingming Cao 	 * a transaction for special inodes like journal inode on last iput()
244261628a3fSMingming Cao 	 * because that could violate lock ordering on umount
244361628a3fSMingming Cao 	 */
2444a1d6cc56SAneesh Kumar K.V 	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
244561628a3fSMingming Cao 		return 0;
24462a21e37eSTheodore Ts'o 
24472a21e37eSTheodore Ts'o 	/*
24482a21e37eSTheodore Ts'o 	 * If the filesystem has aborted, it is read-only, so return
24492a21e37eSTheodore Ts'o 	 * right away instead of dumping stack traces later on that
24502a21e37eSTheodore Ts'o 	 * will obscure the real source of the problem.  We test
24514ab2f15bSTheodore Ts'o 	 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
24522a21e37eSTheodore Ts'o 	 * the latter could be true if the filesystem is mounted
24532a21e37eSTheodore Ts'o 	 * read-only, and in that case, ext4_da_writepages should
24542a21e37eSTheodore Ts'o 	 * *never* be called, so if that ever happens, we would want
24552a21e37eSTheodore Ts'o 	 * the stack trace.
24562a21e37eSTheodore Ts'o 	 */
24574ab2f15bSTheodore Ts'o 	if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
24582a21e37eSTheodore Ts'o 		return -EROFS;
24592a21e37eSTheodore Ts'o 
246022208dedSAneesh Kumar K.V 	if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
246122208dedSAneesh Kumar K.V 		range_whole = 1;
246261628a3fSMingming Cao 
24632acf2c26SAneesh Kumar K.V 	range_cyclic = wbc->range_cyclic;
24642acf2c26SAneesh Kumar K.V 	if (wbc->range_cyclic) {
246522208dedSAneesh Kumar K.V 		index = mapping->writeback_index;
24662acf2c26SAneesh Kumar K.V 		if (index)
24672acf2c26SAneesh Kumar K.V 			cycled = 0;
24682acf2c26SAneesh Kumar K.V 		wbc->range_start = index << PAGE_CACHE_SHIFT;
24692acf2c26SAneesh Kumar K.V 		wbc->range_end  = LLONG_MAX;
24702acf2c26SAneesh Kumar K.V 		wbc->range_cyclic = 0;
24715b41d924SEric Sandeen 		end = -1;
24725b41d924SEric Sandeen 	} else {
247322208dedSAneesh Kumar K.V 		index = wbc->range_start >> PAGE_CACHE_SHIFT;
24745b41d924SEric Sandeen 		end = wbc->range_end >> PAGE_CACHE_SHIFT;
24755b41d924SEric Sandeen 	}
2476a1d6cc56SAneesh Kumar K.V 
247755138e0bSTheodore Ts'o 	/*
247855138e0bSTheodore Ts'o 	 * This works around two forms of stupidity.  The first is in
247955138e0bSTheodore Ts'o 	 * the writeback code, which caps the maximum number of pages
248055138e0bSTheodore Ts'o 	 * written to be 1024 pages.  This is wrong on multiple
248155138e0bSTheodore Ts'o 	 * levels; different architectues have a different page size,
248255138e0bSTheodore Ts'o 	 * which changes the maximum amount of data which gets
248355138e0bSTheodore Ts'o 	 * written.  Secondly, 4 megabytes is way too small.  XFS
248455138e0bSTheodore Ts'o 	 * forces this value to be 16 megabytes by multiplying
248555138e0bSTheodore Ts'o 	 * nr_to_write parameter by four, and then relies on its
248655138e0bSTheodore Ts'o 	 * allocator to allocate larger extents to make them
248755138e0bSTheodore Ts'o 	 * contiguous.  Unfortunately this brings us to the second
248855138e0bSTheodore Ts'o 	 * stupidity, which is that ext4's mballoc code only allocates
248955138e0bSTheodore Ts'o 	 * at most 2048 blocks.  So we force contiguous writes up to
249055138e0bSTheodore Ts'o 	 * the number of dirty blocks in the inode, or
249155138e0bSTheodore Ts'o 	 * sbi->max_writeback_mb_bump whichever is smaller.
249255138e0bSTheodore Ts'o 	 */
249355138e0bSTheodore Ts'o 	max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2494b443e733SEric Sandeen 	if (!range_cyclic && range_whole) {
2495b443e733SEric Sandeen 		if (wbc->nr_to_write == LONG_MAX)
2496b443e733SEric Sandeen 			desired_nr_to_write = wbc->nr_to_write;
249755138e0bSTheodore Ts'o 		else
2498b443e733SEric Sandeen 			desired_nr_to_write = wbc->nr_to_write * 8;
2499b443e733SEric Sandeen 	} else
250055138e0bSTheodore Ts'o 		desired_nr_to_write = ext4_num_dirty_pages(inode, index,
250155138e0bSTheodore Ts'o 							   max_pages);
250255138e0bSTheodore Ts'o 	if (desired_nr_to_write > max_pages)
250355138e0bSTheodore Ts'o 		desired_nr_to_write = max_pages;
250455138e0bSTheodore Ts'o 
250555138e0bSTheodore Ts'o 	if (wbc->nr_to_write < desired_nr_to_write) {
250655138e0bSTheodore Ts'o 		nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
250755138e0bSTheodore Ts'o 		wbc->nr_to_write = desired_nr_to_write;
250855138e0bSTheodore Ts'o 	}
250955138e0bSTheodore Ts'o 
25102acf2c26SAneesh Kumar K.V retry:
25116e6938b6SWu Fengguang 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
25125b41d924SEric Sandeen 		tag_pages_for_writeback(mapping, index, end);
25135b41d924SEric Sandeen 
25141bce63d1SShaohua Li 	blk_start_plug(&plug);
251522208dedSAneesh Kumar K.V 	while (!ret && wbc->nr_to_write > 0) {
2516a1d6cc56SAneesh Kumar K.V 
2517a1d6cc56SAneesh Kumar K.V 		/*
2518a1d6cc56SAneesh Kumar K.V 		 * we  insert one extent at a time. So we need
2519a1d6cc56SAneesh Kumar K.V 		 * credit needed for single extent allocation.
2520a1d6cc56SAneesh Kumar K.V 		 * journalled mode is currently not supported
2521a1d6cc56SAneesh Kumar K.V 		 * by delalloc
2522a1d6cc56SAneesh Kumar K.V 		 */
2523a1d6cc56SAneesh Kumar K.V 		BUG_ON(ext4_should_journal_data(inode));
2524525f4ed8SMingming Cao 		needed_blocks = ext4_da_writepages_trans_blocks(inode);
2525a1d6cc56SAneesh Kumar K.V 
252661628a3fSMingming Cao 		/* start a new transaction*/
25279924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
25289924a92aSTheodore Ts'o 					    needed_blocks);
252961628a3fSMingming Cao 		if (IS_ERR(handle)) {
253061628a3fSMingming Cao 			ret = PTR_ERR(handle);
25311693918eSTheodore Ts'o 			ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
2532fbe845ddSCurt Wohlgemuth 			       "%ld pages, ino %lu; err %d", __func__,
2533a1d6cc56SAneesh Kumar K.V 				wbc->nr_to_write, inode->i_ino, ret);
25343c1fcb2cSNamjae Jeon 			blk_finish_plug(&plug);
253561628a3fSMingming Cao 			goto out_writepages;
253661628a3fSMingming Cao 		}
2537f63e6005STheodore Ts'o 
2538f63e6005STheodore Ts'o 		/*
25398eb9e5ceSTheodore Ts'o 		 * Now call write_cache_pages_da() to find the next
2540f63e6005STheodore Ts'o 		 * contiguous region of logical blocks that need
25418eb9e5ceSTheodore Ts'o 		 * blocks to be allocated by ext4 and submit them.
2542f63e6005STheodore Ts'o 		 */
25439c3569b5STao Ma 		ret = write_cache_pages_da(handle, mapping,
25449c3569b5STao Ma 					   wbc, &mpd, &done_index);
2545f63e6005STheodore Ts'o 		/*
2546af901ca1SAndré Goddard Rosa 		 * If we have a contiguous extent of pages and we
2547f63e6005STheodore Ts'o 		 * haven't done the I/O yet, map the blocks and submit
2548f63e6005STheodore Ts'o 		 * them for I/O.
2549f63e6005STheodore Ts'o 		 */
2550f63e6005STheodore Ts'o 		if (!mpd.io_done && mpd.next_page != mpd.first_page) {
25515a87b7a5STheodore Ts'o 			mpage_da_map_and_submit(&mpd);
2552f63e6005STheodore Ts'o 			ret = MPAGE_DA_EXTENT_TAIL;
2553f63e6005STheodore Ts'o 		}
2554b3a3ca8cSTheodore Ts'o 		trace_ext4_da_write_pages(inode, &mpd);
2555f63e6005STheodore Ts'o 		wbc->nr_to_write -= mpd.pages_written;
2556df22291fSAneesh Kumar K.V 
255761628a3fSMingming Cao 		ext4_journal_stop(handle);
2558df22291fSAneesh Kumar K.V 
25598f64b32eSEric Sandeen 		if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
256022208dedSAneesh Kumar K.V 			/* commit the transaction which would
256122208dedSAneesh Kumar K.V 			 * free blocks released in the transaction
256222208dedSAneesh Kumar K.V 			 * and try again
256322208dedSAneesh Kumar K.V 			 */
2564df22291fSAneesh Kumar K.V 			jbd2_journal_force_commit_nested(sbi->s_journal);
256522208dedSAneesh Kumar K.V 			ret = 0;
256622208dedSAneesh Kumar K.V 		} else if (ret == MPAGE_DA_EXTENT_TAIL) {
2567a1d6cc56SAneesh Kumar K.V 			/*
25688de49e67SKazuya Mio 			 * Got one extent now try with rest of the pages.
25698de49e67SKazuya Mio 			 * If mpd.retval is set -EIO, journal is aborted.
25708de49e67SKazuya Mio 			 * So we don't need to write any more.
2571a1d6cc56SAneesh Kumar K.V 			 */
257222208dedSAneesh Kumar K.V 			pages_written += mpd.pages_written;
25738de49e67SKazuya Mio 			ret = mpd.retval;
25742acf2c26SAneesh Kumar K.V 			io_done = 1;
257522208dedSAneesh Kumar K.V 		} else if (wbc->nr_to_write)
257661628a3fSMingming Cao 			/*
257761628a3fSMingming Cao 			 * There is no more writeout needed
257861628a3fSMingming Cao 			 * or we requested for a noblocking writeout
257961628a3fSMingming Cao 			 * and we found the device congested
258061628a3fSMingming Cao 			 */
258161628a3fSMingming Cao 			break;
258261628a3fSMingming Cao 	}
25831bce63d1SShaohua Li 	blk_finish_plug(&plug);
25842acf2c26SAneesh Kumar K.V 	if (!io_done && !cycled) {
25852acf2c26SAneesh Kumar K.V 		cycled = 1;
25862acf2c26SAneesh Kumar K.V 		index = 0;
25872acf2c26SAneesh Kumar K.V 		wbc->range_start = index << PAGE_CACHE_SHIFT;
25882acf2c26SAneesh Kumar K.V 		wbc->range_end  = mapping->writeback_index - 1;
25892acf2c26SAneesh Kumar K.V 		goto retry;
25902acf2c26SAneesh Kumar K.V 	}
259161628a3fSMingming Cao 
259222208dedSAneesh Kumar K.V 	/* Update index */
25932acf2c26SAneesh Kumar K.V 	wbc->range_cyclic = range_cyclic;
259422208dedSAneesh Kumar K.V 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
259522208dedSAneesh Kumar K.V 		/*
259622208dedSAneesh Kumar K.V 		 * set the writeback_index so that range_cyclic
259722208dedSAneesh Kumar K.V 		 * mode will write it back later
259822208dedSAneesh Kumar K.V 		 */
259972f84e65SEric Sandeen 		mapping->writeback_index = done_index;
2600a1d6cc56SAneesh Kumar K.V 
260161628a3fSMingming Cao out_writepages:
260222208dedSAneesh Kumar K.V 	wbc->nr_to_write -= nr_to_writebump;
2603de89de6eSTheodore Ts'o 	wbc->range_start = range_start;
26049bffad1eSTheodore Ts'o 	trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
260561628a3fSMingming Cao 	return ret;
260664769240SAlex Tomas }
260764769240SAlex Tomas 
260879f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb)
260979f0be8dSAneesh Kumar K.V {
261079f0be8dSAneesh Kumar K.V 	s64 free_blocks, dirty_blocks;
261179f0be8dSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
261279f0be8dSAneesh Kumar K.V 
261379f0be8dSAneesh Kumar K.V 	/*
261479f0be8dSAneesh Kumar K.V 	 * switch to non delalloc mode if we are running low
261579f0be8dSAneesh Kumar K.V 	 * on free block. The free block accounting via percpu
2616179f7ebfSEric Dumazet 	 * counters can get slightly wrong with percpu_counter_batch getting
261779f0be8dSAneesh Kumar K.V 	 * accumulated on each CPU without updating global counters
261879f0be8dSAneesh Kumar K.V 	 * Delalloc need an accurate free block accounting. So switch
261979f0be8dSAneesh Kumar K.V 	 * to non delalloc when we are near to error range.
262079f0be8dSAneesh Kumar K.V 	 */
262157042651STheodore Ts'o 	free_blocks  = EXT4_C2B(sbi,
262257042651STheodore Ts'o 		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
262357042651STheodore Ts'o 	dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
262400d4e736STheodore Ts'o 	/*
262500d4e736STheodore Ts'o 	 * Start pushing delalloc when 1/2 of free blocks are dirty.
262600d4e736STheodore Ts'o 	 */
262710ee27a0SMiao Xie 	if (dirty_blocks && (free_blocks < 2 * dirty_blocks))
262810ee27a0SMiao Xie 		try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
262900d4e736STheodore Ts'o 
263079f0be8dSAneesh Kumar K.V 	if (2 * free_blocks < 3 * dirty_blocks ||
2631df55c99dSTheodore Ts'o 		free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
263279f0be8dSAneesh Kumar K.V 		/*
2633c8afb446SEric Sandeen 		 * free block count is less than 150% of dirty blocks
2634c8afb446SEric Sandeen 		 * or free blocks is less than watermark
263579f0be8dSAneesh Kumar K.V 		 */
263679f0be8dSAneesh Kumar K.V 		return 1;
263779f0be8dSAneesh Kumar K.V 	}
263879f0be8dSAneesh Kumar K.V 	return 0;
263979f0be8dSAneesh Kumar K.V }
264079f0be8dSAneesh Kumar K.V 
264164769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
264264769240SAlex Tomas 			       loff_t pos, unsigned len, unsigned flags,
264364769240SAlex Tomas 			       struct page **pagep, void **fsdata)
264464769240SAlex Tomas {
264572b8ab9dSEric Sandeen 	int ret, retries = 0;
264664769240SAlex Tomas 	struct page *page;
264764769240SAlex Tomas 	pgoff_t index;
264864769240SAlex Tomas 	struct inode *inode = mapping->host;
264964769240SAlex Tomas 	handle_t *handle;
265064769240SAlex Tomas 
265164769240SAlex Tomas 	index = pos >> PAGE_CACHE_SHIFT;
265279f0be8dSAneesh Kumar K.V 
265379f0be8dSAneesh Kumar K.V 	if (ext4_nonda_switch(inode->i_sb)) {
265479f0be8dSAneesh Kumar K.V 		*fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
265579f0be8dSAneesh Kumar K.V 		return ext4_write_begin(file, mapping, pos,
265679f0be8dSAneesh Kumar K.V 					len, flags, pagep, fsdata);
265779f0be8dSAneesh Kumar K.V 	}
265879f0be8dSAneesh Kumar K.V 	*fsdata = (void *)0;
26599bffad1eSTheodore Ts'o 	trace_ext4_da_write_begin(inode, pos, len, flags);
26609c3569b5STao Ma 
26619c3569b5STao Ma 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
26629c3569b5STao Ma 		ret = ext4_da_write_inline_data_begin(mapping, inode,
26639c3569b5STao Ma 						      pos, len, flags,
26649c3569b5STao Ma 						      pagep, fsdata);
26659c3569b5STao Ma 		if (ret < 0)
266647564bfbSTheodore Ts'o 			return ret;
266747564bfbSTheodore Ts'o 		if (ret == 1)
266847564bfbSTheodore Ts'o 			return 0;
26699c3569b5STao Ma 	}
26709c3569b5STao Ma 
267147564bfbSTheodore Ts'o 	/*
267247564bfbSTheodore Ts'o 	 * grab_cache_page_write_begin() can take a long time if the
267347564bfbSTheodore Ts'o 	 * system is thrashing due to memory pressure, or if the page
267447564bfbSTheodore Ts'o 	 * is being written back.  So grab it first before we start
267547564bfbSTheodore Ts'o 	 * the transaction handle.  This also allows us to allocate
267647564bfbSTheodore Ts'o 	 * the page (if needed) without using GFP_NOFS.
267747564bfbSTheodore Ts'o 	 */
267847564bfbSTheodore Ts'o retry_grab:
267947564bfbSTheodore Ts'o 	page = grab_cache_page_write_begin(mapping, index, flags);
268047564bfbSTheodore Ts'o 	if (!page)
268147564bfbSTheodore Ts'o 		return -ENOMEM;
268247564bfbSTheodore Ts'o 	unlock_page(page);
268347564bfbSTheodore Ts'o 
268464769240SAlex Tomas 	/*
268564769240SAlex Tomas 	 * With delayed allocation, we don't log the i_disksize update
268664769240SAlex Tomas 	 * if there is delayed block allocation. But we still need
268764769240SAlex Tomas 	 * to journalling the i_disksize update if writes to the end
268864769240SAlex Tomas 	 * of file which has an already mapped buffer.
268964769240SAlex Tomas 	 */
269047564bfbSTheodore Ts'o retry_journal:
26919924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1);
269264769240SAlex Tomas 	if (IS_ERR(handle)) {
269347564bfbSTheodore Ts'o 		page_cache_release(page);
269447564bfbSTheodore Ts'o 		return PTR_ERR(handle);
269564769240SAlex Tomas 	}
269664769240SAlex Tomas 
269747564bfbSTheodore Ts'o 	lock_page(page);
269847564bfbSTheodore Ts'o 	if (page->mapping != mapping) {
269947564bfbSTheodore Ts'o 		/* The page got truncated from under us */
270047564bfbSTheodore Ts'o 		unlock_page(page);
270147564bfbSTheodore Ts'o 		page_cache_release(page);
2702d5a0d4f7SEric Sandeen 		ext4_journal_stop(handle);
270347564bfbSTheodore Ts'o 		goto retry_grab;
2704d5a0d4f7SEric Sandeen 	}
270547564bfbSTheodore Ts'o 	/* In case writeback began while the page was unlocked */
270647564bfbSTheodore Ts'o 	wait_on_page_writeback(page);
270764769240SAlex Tomas 
27086e1db88dSChristoph Hellwig 	ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
270964769240SAlex Tomas 	if (ret < 0) {
271064769240SAlex Tomas 		unlock_page(page);
271164769240SAlex Tomas 		ext4_journal_stop(handle);
2712ae4d5372SAneesh Kumar K.V 		/*
2713ae4d5372SAneesh Kumar K.V 		 * block_write_begin may have instantiated a few blocks
2714ae4d5372SAneesh Kumar K.V 		 * outside i_size.  Trim these off again. Don't need
2715ae4d5372SAneesh Kumar K.V 		 * i_size_read because we hold i_mutex.
2716ae4d5372SAneesh Kumar K.V 		 */
2717ae4d5372SAneesh Kumar K.V 		if (pos + len > inode->i_size)
2718b9a4207dSJan Kara 			ext4_truncate_failed_write(inode);
271947564bfbSTheodore Ts'o 
272047564bfbSTheodore Ts'o 		if (ret == -ENOSPC &&
272147564bfbSTheodore Ts'o 		    ext4_should_retry_alloc(inode->i_sb, &retries))
272247564bfbSTheodore Ts'o 			goto retry_journal;
272347564bfbSTheodore Ts'o 
272447564bfbSTheodore Ts'o 		page_cache_release(page);
272547564bfbSTheodore Ts'o 		return ret;
272664769240SAlex Tomas 	}
272764769240SAlex Tomas 
272847564bfbSTheodore Ts'o 	*pagep = page;
272964769240SAlex Tomas 	return ret;
273064769240SAlex Tomas }
273164769240SAlex Tomas 
2732632eaeabSMingming Cao /*
2733632eaeabSMingming Cao  * Check if we should update i_disksize
2734632eaeabSMingming Cao  * when write to the end of file but not require block allocation
2735632eaeabSMingming Cao  */
2736632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page,
2737632eaeabSMingming Cao 					    unsigned long offset)
2738632eaeabSMingming Cao {
2739632eaeabSMingming Cao 	struct buffer_head *bh;
2740632eaeabSMingming Cao 	struct inode *inode = page->mapping->host;
2741632eaeabSMingming Cao 	unsigned int idx;
2742632eaeabSMingming Cao 	int i;
2743632eaeabSMingming Cao 
2744632eaeabSMingming Cao 	bh = page_buffers(page);
2745632eaeabSMingming Cao 	idx = offset >> inode->i_blkbits;
2746632eaeabSMingming Cao 
2747632eaeabSMingming Cao 	for (i = 0; i < idx; i++)
2748632eaeabSMingming Cao 		bh = bh->b_this_page;
2749632eaeabSMingming Cao 
275029fa89d0SAneesh Kumar K.V 	if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
2751632eaeabSMingming Cao 		return 0;
2752632eaeabSMingming Cao 	return 1;
2753632eaeabSMingming Cao }
2754632eaeabSMingming Cao 
275564769240SAlex Tomas static int ext4_da_write_end(struct file *file,
275664769240SAlex Tomas 			     struct address_space *mapping,
275764769240SAlex Tomas 			     loff_t pos, unsigned len, unsigned copied,
275864769240SAlex Tomas 			     struct page *page, void *fsdata)
275964769240SAlex Tomas {
276064769240SAlex Tomas 	struct inode *inode = mapping->host;
276164769240SAlex Tomas 	int ret = 0, ret2;
276264769240SAlex Tomas 	handle_t *handle = ext4_journal_current_handle();
276364769240SAlex Tomas 	loff_t new_i_size;
2764632eaeabSMingming Cao 	unsigned long start, end;
276579f0be8dSAneesh Kumar K.V 	int write_mode = (int)(unsigned long)fsdata;
276679f0be8dSAneesh Kumar K.V 
276774d553aaSTheodore Ts'o 	if (write_mode == FALL_BACK_TO_NONDELALLOC)
276874d553aaSTheodore Ts'o 		return ext4_write_end(file, mapping, pos,
276979f0be8dSAneesh Kumar K.V 				      len, copied, page, fsdata);
2770632eaeabSMingming Cao 
27719bffad1eSTheodore Ts'o 	trace_ext4_da_write_end(inode, pos, len, copied);
2772632eaeabSMingming Cao 	start = pos & (PAGE_CACHE_SIZE - 1);
2773632eaeabSMingming Cao 	end = start + copied - 1;
277464769240SAlex Tomas 
277564769240SAlex Tomas 	/*
277664769240SAlex Tomas 	 * generic_write_end() will run mark_inode_dirty() if i_size
277764769240SAlex Tomas 	 * changes.  So let's piggyback the i_disksize mark_inode_dirty
277864769240SAlex Tomas 	 * into that.
277964769240SAlex Tomas 	 */
278064769240SAlex Tomas 	new_i_size = pos + copied;
2781ea51d132SAndrea Arcangeli 	if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
27829c3569b5STao Ma 		if (ext4_has_inline_data(inode) ||
27839c3569b5STao Ma 		    ext4_da_should_update_i_disksize(page, end)) {
2784632eaeabSMingming Cao 			down_write(&EXT4_I(inode)->i_data_sem);
2785f3b59291STheodore Ts'o 			if (new_i_size > EXT4_I(inode)->i_disksize)
278664769240SAlex Tomas 				EXT4_I(inode)->i_disksize = new_i_size;
2787632eaeabSMingming Cao 			up_write(&EXT4_I(inode)->i_data_sem);
2788cf17fea6SAneesh Kumar K.V 			/* We need to mark inode dirty even if
2789cf17fea6SAneesh Kumar K.V 			 * new_i_size is less that inode->i_size
2790cf17fea6SAneesh Kumar K.V 			 * bu greater than i_disksize.(hint delalloc)
2791cf17fea6SAneesh Kumar K.V 			 */
2792cf17fea6SAneesh Kumar K.V 			ext4_mark_inode_dirty(handle, inode);
2793632eaeabSMingming Cao 		}
2794632eaeabSMingming Cao 	}
27959c3569b5STao Ma 
27969c3569b5STao Ma 	if (write_mode != CONVERT_INLINE_DATA &&
27979c3569b5STao Ma 	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
27989c3569b5STao Ma 	    ext4_has_inline_data(inode))
27999c3569b5STao Ma 		ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
28009c3569b5STao Ma 						     page);
28019c3569b5STao Ma 	else
280264769240SAlex Tomas 		ret2 = generic_write_end(file, mapping, pos, len, copied,
280364769240SAlex Tomas 							page, fsdata);
28049c3569b5STao Ma 
280564769240SAlex Tomas 	copied = ret2;
280664769240SAlex Tomas 	if (ret2 < 0)
280764769240SAlex Tomas 		ret = ret2;
280864769240SAlex Tomas 	ret2 = ext4_journal_stop(handle);
280964769240SAlex Tomas 	if (!ret)
281064769240SAlex Tomas 		ret = ret2;
281164769240SAlex Tomas 
281264769240SAlex Tomas 	return ret ? ret : copied;
281364769240SAlex Tomas }
281464769240SAlex Tomas 
281564769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
281664769240SAlex Tomas {
281764769240SAlex Tomas 	/*
281864769240SAlex Tomas 	 * Drop reserved blocks
281964769240SAlex Tomas 	 */
282064769240SAlex Tomas 	BUG_ON(!PageLocked(page));
282164769240SAlex Tomas 	if (!page_has_buffers(page))
282264769240SAlex Tomas 		goto out;
282364769240SAlex Tomas 
2824d2a17637SMingming Cao 	ext4_da_page_release_reservation(page, offset);
282564769240SAlex Tomas 
282664769240SAlex Tomas out:
282764769240SAlex Tomas 	ext4_invalidatepage(page, offset);
282864769240SAlex Tomas 
282964769240SAlex Tomas 	return;
283064769240SAlex Tomas }
283164769240SAlex Tomas 
2832ccd2506bSTheodore Ts'o /*
2833ccd2506bSTheodore Ts'o  * Force all delayed allocation blocks to be allocated for a given inode.
2834ccd2506bSTheodore Ts'o  */
2835ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode)
2836ccd2506bSTheodore Ts'o {
2837fb40ba0dSTheodore Ts'o 	trace_ext4_alloc_da_blocks(inode);
2838fb40ba0dSTheodore Ts'o 
2839ccd2506bSTheodore Ts'o 	if (!EXT4_I(inode)->i_reserved_data_blocks &&
2840ccd2506bSTheodore Ts'o 	    !EXT4_I(inode)->i_reserved_meta_blocks)
2841ccd2506bSTheodore Ts'o 		return 0;
2842ccd2506bSTheodore Ts'o 
2843ccd2506bSTheodore Ts'o 	/*
2844ccd2506bSTheodore Ts'o 	 * We do something simple for now.  The filemap_flush() will
2845ccd2506bSTheodore Ts'o 	 * also start triggering a write of the data blocks, which is
2846ccd2506bSTheodore Ts'o 	 * not strictly speaking necessary (and for users of
2847ccd2506bSTheodore Ts'o 	 * laptop_mode, not even desirable).  However, to do otherwise
2848ccd2506bSTheodore Ts'o 	 * would require replicating code paths in:
2849ccd2506bSTheodore Ts'o 	 *
2850ccd2506bSTheodore Ts'o 	 * ext4_da_writepages() ->
2851ccd2506bSTheodore Ts'o 	 *    write_cache_pages() ---> (via passed in callback function)
2852ccd2506bSTheodore Ts'o 	 *        __mpage_da_writepage() -->
2853ccd2506bSTheodore Ts'o 	 *           mpage_add_bh_to_extent()
2854ccd2506bSTheodore Ts'o 	 *           mpage_da_map_blocks()
2855ccd2506bSTheodore Ts'o 	 *
2856ccd2506bSTheodore Ts'o 	 * The problem is that write_cache_pages(), located in
2857ccd2506bSTheodore Ts'o 	 * mm/page-writeback.c, marks pages clean in preparation for
2858ccd2506bSTheodore Ts'o 	 * doing I/O, which is not desirable if we're not planning on
2859ccd2506bSTheodore Ts'o 	 * doing I/O at all.
2860ccd2506bSTheodore Ts'o 	 *
2861ccd2506bSTheodore Ts'o 	 * We could call write_cache_pages(), and then redirty all of
2862380cf090SWu Fengguang 	 * the pages by calling redirty_page_for_writepage() but that
2863ccd2506bSTheodore Ts'o 	 * would be ugly in the extreme.  So instead we would need to
2864ccd2506bSTheodore Ts'o 	 * replicate parts of the code in the above functions,
286525985edcSLucas De Marchi 	 * simplifying them because we wouldn't actually intend to
2866ccd2506bSTheodore Ts'o 	 * write out the pages, but rather only collect contiguous
2867ccd2506bSTheodore Ts'o 	 * logical block extents, call the multi-block allocator, and
2868ccd2506bSTheodore Ts'o 	 * then update the buffer heads with the block allocations.
2869ccd2506bSTheodore Ts'o 	 *
2870ccd2506bSTheodore Ts'o 	 * For now, though, we'll cheat by calling filemap_flush(),
2871ccd2506bSTheodore Ts'o 	 * which will map the blocks, and start the I/O, but not
2872ccd2506bSTheodore Ts'o 	 * actually wait for the I/O to complete.
2873ccd2506bSTheodore Ts'o 	 */
2874ccd2506bSTheodore Ts'o 	return filemap_flush(inode->i_mapping);
2875ccd2506bSTheodore Ts'o }
287664769240SAlex Tomas 
287764769240SAlex Tomas /*
2878ac27a0ecSDave Kleikamp  * bmap() is special.  It gets used by applications such as lilo and by
2879ac27a0ecSDave Kleikamp  * the swapper to find the on-disk block of a specific piece of data.
2880ac27a0ecSDave Kleikamp  *
2881ac27a0ecSDave Kleikamp  * Naturally, this is dangerous if the block concerned is still in the
2882617ba13bSMingming Cao  * journal.  If somebody makes a swapfile on an ext4 data-journaling
2883ac27a0ecSDave Kleikamp  * filesystem and enables swap, then they may get a nasty shock when the
2884ac27a0ecSDave Kleikamp  * data getting swapped to that swapfile suddenly gets overwritten by
2885ac27a0ecSDave Kleikamp  * the original zero's written out previously to the journal and
2886ac27a0ecSDave Kleikamp  * awaiting writeback in the kernel's buffer cache.
2887ac27a0ecSDave Kleikamp  *
2888ac27a0ecSDave Kleikamp  * So, if we see any bmap calls here on a modified, data-journaled file,
2889ac27a0ecSDave Kleikamp  * take extra steps to flush any blocks which might be in the cache.
2890ac27a0ecSDave Kleikamp  */
2891617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
2892ac27a0ecSDave Kleikamp {
2893ac27a0ecSDave Kleikamp 	struct inode *inode = mapping->host;
2894ac27a0ecSDave Kleikamp 	journal_t *journal;
2895ac27a0ecSDave Kleikamp 	int err;
2896ac27a0ecSDave Kleikamp 
289746c7f254STao Ma 	/*
289846c7f254STao Ma 	 * We can get here for an inline file via the FIBMAP ioctl
289946c7f254STao Ma 	 */
290046c7f254STao Ma 	if (ext4_has_inline_data(inode))
290146c7f254STao Ma 		return 0;
290246c7f254STao Ma 
290364769240SAlex Tomas 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
290464769240SAlex Tomas 			test_opt(inode->i_sb, DELALLOC)) {
290564769240SAlex Tomas 		/*
290664769240SAlex Tomas 		 * With delalloc we want to sync the file
290764769240SAlex Tomas 		 * so that we can make sure we allocate
290864769240SAlex Tomas 		 * blocks for file
290964769240SAlex Tomas 		 */
291064769240SAlex Tomas 		filemap_write_and_wait(mapping);
291164769240SAlex Tomas 	}
291264769240SAlex Tomas 
291319f5fb7aSTheodore Ts'o 	if (EXT4_JOURNAL(inode) &&
291419f5fb7aSTheodore Ts'o 	    ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
2915ac27a0ecSDave Kleikamp 		/*
2916ac27a0ecSDave Kleikamp 		 * This is a REALLY heavyweight approach, but the use of
2917ac27a0ecSDave Kleikamp 		 * bmap on dirty files is expected to be extremely rare:
2918ac27a0ecSDave Kleikamp 		 * only if we run lilo or swapon on a freshly made file
2919ac27a0ecSDave Kleikamp 		 * do we expect this to happen.
2920ac27a0ecSDave Kleikamp 		 *
2921ac27a0ecSDave Kleikamp 		 * (bmap requires CAP_SYS_RAWIO so this does not
2922ac27a0ecSDave Kleikamp 		 * represent an unprivileged user DOS attack --- we'd be
2923ac27a0ecSDave Kleikamp 		 * in trouble if mortal users could trigger this path at
2924ac27a0ecSDave Kleikamp 		 * will.)
2925ac27a0ecSDave Kleikamp 		 *
2926617ba13bSMingming Cao 		 * NB. EXT4_STATE_JDATA is not set on files other than
2927ac27a0ecSDave Kleikamp 		 * regular files.  If somebody wants to bmap a directory
2928ac27a0ecSDave Kleikamp 		 * or symlink and gets confused because the buffer
2929ac27a0ecSDave Kleikamp 		 * hasn't yet been flushed to disk, they deserve
2930ac27a0ecSDave Kleikamp 		 * everything they get.
2931ac27a0ecSDave Kleikamp 		 */
2932ac27a0ecSDave Kleikamp 
293319f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
2934617ba13bSMingming Cao 		journal = EXT4_JOURNAL(inode);
2935dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2936dab291afSMingming Cao 		err = jbd2_journal_flush(journal);
2937dab291afSMingming Cao 		jbd2_journal_unlock_updates(journal);
2938ac27a0ecSDave Kleikamp 
2939ac27a0ecSDave Kleikamp 		if (err)
2940ac27a0ecSDave Kleikamp 			return 0;
2941ac27a0ecSDave Kleikamp 	}
2942ac27a0ecSDave Kleikamp 
2943617ba13bSMingming Cao 	return generic_block_bmap(mapping, block, ext4_get_block);
2944ac27a0ecSDave Kleikamp }
2945ac27a0ecSDave Kleikamp 
2946617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page)
2947ac27a0ecSDave Kleikamp {
294846c7f254STao Ma 	int ret = -EAGAIN;
294946c7f254STao Ma 	struct inode *inode = page->mapping->host;
295046c7f254STao Ma 
29510562e0baSJiaying Zhang 	trace_ext4_readpage(page);
295246c7f254STao Ma 
295346c7f254STao Ma 	if (ext4_has_inline_data(inode))
295446c7f254STao Ma 		ret = ext4_readpage_inline(inode, page);
295546c7f254STao Ma 
295646c7f254STao Ma 	if (ret == -EAGAIN)
2957617ba13bSMingming Cao 		return mpage_readpage(page, ext4_get_block);
295846c7f254STao Ma 
295946c7f254STao Ma 	return ret;
2960ac27a0ecSDave Kleikamp }
2961ac27a0ecSDave Kleikamp 
2962ac27a0ecSDave Kleikamp static int
2963617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping,
2964ac27a0ecSDave Kleikamp 		struct list_head *pages, unsigned nr_pages)
2965ac27a0ecSDave Kleikamp {
296646c7f254STao Ma 	struct inode *inode = mapping->host;
296746c7f254STao Ma 
296846c7f254STao Ma 	/* If the file has inline data, no need to do readpages. */
296946c7f254STao Ma 	if (ext4_has_inline_data(inode))
297046c7f254STao Ma 		return 0;
297146c7f254STao Ma 
2972617ba13bSMingming Cao 	return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
2973ac27a0ecSDave Kleikamp }
2974ac27a0ecSDave Kleikamp 
2975617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset)
2976ac27a0ecSDave Kleikamp {
29770562e0baSJiaying Zhang 	trace_ext4_invalidatepage(page, offset);
29780562e0baSJiaying Zhang 
29794520fb3cSJan Kara 	/* No journalling happens on data buffers when this function is used */
29804520fb3cSJan Kara 	WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
29814520fb3cSJan Kara 
29824520fb3cSJan Kara 	block_invalidatepage(page, offset);
29834520fb3cSJan Kara }
29844520fb3cSJan Kara 
298553e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page,
29864520fb3cSJan Kara 					    unsigned long offset)
29874520fb3cSJan Kara {
29884520fb3cSJan Kara 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
29894520fb3cSJan Kara 
29904520fb3cSJan Kara 	trace_ext4_journalled_invalidatepage(page, offset);
29914520fb3cSJan Kara 
2992744692dcSJiaying Zhang 	/*
2993ac27a0ecSDave Kleikamp 	 * If it's a full truncate we just forget about the pending dirtying
2994ac27a0ecSDave Kleikamp 	 */
2995ac27a0ecSDave Kleikamp 	if (offset == 0)
2996ac27a0ecSDave Kleikamp 		ClearPageChecked(page);
2997ac27a0ecSDave Kleikamp 
299853e87268SJan Kara 	return jbd2_journal_invalidatepage(journal, page, offset);
299953e87268SJan Kara }
300053e87268SJan Kara 
300153e87268SJan Kara /* Wrapper for aops... */
300253e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page,
300353e87268SJan Kara 					   unsigned long offset)
300453e87268SJan Kara {
300553e87268SJan Kara 	WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
3006ac27a0ecSDave Kleikamp }
3007ac27a0ecSDave Kleikamp 
3008617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait)
3009ac27a0ecSDave Kleikamp {
3010617ba13bSMingming Cao 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3011ac27a0ecSDave Kleikamp 
30120562e0baSJiaying Zhang 	trace_ext4_releasepage(page);
30130562e0baSJiaying Zhang 
3014e1c36595SJan Kara 	/* Page has dirty journalled data -> cannot release */
3015e1c36595SJan Kara 	if (PageChecked(page))
3016ac27a0ecSDave Kleikamp 		return 0;
30170390131bSFrank Mayhar 	if (journal)
3018dab291afSMingming Cao 		return jbd2_journal_try_to_free_buffers(journal, page, wait);
30190390131bSFrank Mayhar 	else
30200390131bSFrank Mayhar 		return try_to_free_buffers(page);
3021ac27a0ecSDave Kleikamp }
3022ac27a0ecSDave Kleikamp 
3023ac27a0ecSDave Kleikamp /*
30242ed88685STheodore Ts'o  * ext4_get_block used when preparing for a DIO write or buffer write.
30252ed88685STheodore Ts'o  * We allocate an uinitialized extent if blocks haven't been allocated.
30262ed88685STheodore Ts'o  * The extent will be converted to initialized after the IO is complete.
30272ed88685STheodore Ts'o  */
3028f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock,
30294c0425ffSMingming Cao 		   struct buffer_head *bh_result, int create)
30304c0425ffSMingming Cao {
3031c7064ef1SJiaying Zhang 	ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
30328d5d02e6SMingming Cao 		   inode->i_ino, create);
30332ed88685STheodore Ts'o 	return _ext4_get_block(inode, iblock, bh_result,
30342ed88685STheodore Ts'o 			       EXT4_GET_BLOCKS_IO_CREATE_EXT);
30354c0425ffSMingming Cao }
30364c0425ffSMingming Cao 
3037729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
30388b0f165fSAnatol Pomozov 		   struct buffer_head *bh_result, int create)
3039729f52c6SZheng Liu {
30408b0f165fSAnatol Pomozov 	ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
30418b0f165fSAnatol Pomozov 		   inode->i_ino, create);
30428b0f165fSAnatol Pomozov 	return _ext4_get_block(inode, iblock, bh_result,
30438b0f165fSAnatol Pomozov 			       EXT4_GET_BLOCKS_NO_LOCK);
3044729f52c6SZheng Liu }
3045729f52c6SZheng Liu 
30464c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3047552ef802SChristoph Hellwig 			    ssize_t size, void *private, int ret,
3048552ef802SChristoph Hellwig 			    bool is_async)
30494c0425ffSMingming Cao {
3050496ad9aaSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
30514c0425ffSMingming Cao         ext4_io_end_t *io_end = iocb->private;
30524c0425ffSMingming Cao 
30534b70df18SMingming 	/* if not async direct IO or dio with 0 bytes write, just return */
30544b70df18SMingming 	if (!io_end || !size)
3055552ef802SChristoph Hellwig 		goto out;
30564b70df18SMingming 
30578d5d02e6SMingming Cao 	ext_debug("ext4_end_io_dio(): io_end 0x%p "
3058ace36ad4SJoe Perches 		  "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
30598d5d02e6SMingming Cao  		  iocb->private, io_end->inode->i_ino, iocb, offset,
30608d5d02e6SMingming Cao 		  size);
30618d5d02e6SMingming Cao 
3062b5a7e970STheodore Ts'o 	iocb->private = NULL;
3063b5a7e970STheodore Ts'o 
30648d5d02e6SMingming Cao 	/* if not aio dio with unwritten extents, just free io and return */
3065bd2d0210STheodore Ts'o 	if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
30668d5d02e6SMingming Cao 		ext4_free_io_end(io_end);
30675b3ff237Sjiayingz@google.com (Jiaying Zhang) out:
3068091e26dfSJan Kara 		inode_dio_done(inode);
30695b3ff237Sjiayingz@google.com (Jiaying Zhang) 		if (is_async)
30705b3ff237Sjiayingz@google.com (Jiaying Zhang) 			aio_complete(iocb, ret, 0);
30715b3ff237Sjiayingz@google.com (Jiaying Zhang) 		return;
30728d5d02e6SMingming Cao 	}
30738d5d02e6SMingming Cao 
30744c0425ffSMingming Cao 	io_end->offset = offset;
30754c0425ffSMingming Cao 	io_end->size = size;
30765b3ff237Sjiayingz@google.com (Jiaying Zhang) 	if (is_async) {
30775b3ff237Sjiayingz@google.com (Jiaying Zhang) 		io_end->iocb = iocb;
30785b3ff237Sjiayingz@google.com (Jiaying Zhang) 		io_end->result = ret;
30795b3ff237Sjiayingz@google.com (Jiaying Zhang) 	}
30804c0425ffSMingming Cao 
308128a535f9SDmitry Monakhov 	ext4_add_complete_io(io_end);
30824c0425ffSMingming Cao }
3083c7064ef1SJiaying Zhang 
30844c0425ffSMingming Cao /*
30854c0425ffSMingming Cao  * For ext4 extent files, ext4 will do direct-io write to holes,
30864c0425ffSMingming Cao  * preallocated extents, and those write extend the file, no need to
30874c0425ffSMingming Cao  * fall back to buffered IO.
30884c0425ffSMingming Cao  *
3089b595076aSUwe Kleine-König  * For holes, we fallocate those blocks, mark them as uninitialized
309069c499d1STheodore Ts'o  * If those blocks were preallocated, we mark sure they are split, but
3091b595076aSUwe Kleine-König  * still keep the range to write as uninitialized.
30924c0425ffSMingming Cao  *
309369c499d1STheodore Ts'o  * The unwritten extents will be converted to written when DIO is completed.
30948d5d02e6SMingming Cao  * For async direct IO, since the IO may still pending when return, we
309525985edcSLucas De Marchi  * set up an end_io call back function, which will do the conversion
30968d5d02e6SMingming Cao  * when async direct IO completed.
30974c0425ffSMingming Cao  *
30984c0425ffSMingming Cao  * If the O_DIRECT write will extend the file then add this inode to the
30994c0425ffSMingming Cao  * orphan list.  So recovery will truncate it back to the original size
31004c0425ffSMingming Cao  * if the machine crashes during the write.
31014c0425ffSMingming Cao  *
31024c0425ffSMingming Cao  */
31034c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
31044c0425ffSMingming Cao 			      const struct iovec *iov, loff_t offset,
31054c0425ffSMingming Cao 			      unsigned long nr_segs)
31064c0425ffSMingming Cao {
31074c0425ffSMingming Cao 	struct file *file = iocb->ki_filp;
31084c0425ffSMingming Cao 	struct inode *inode = file->f_mapping->host;
31094c0425ffSMingming Cao 	ssize_t ret;
31104c0425ffSMingming Cao 	size_t count = iov_length(iov, nr_segs);
3111729f52c6SZheng Liu 	int overwrite = 0;
31128b0f165fSAnatol Pomozov 	get_block_t *get_block_func = NULL;
31138b0f165fSAnatol Pomozov 	int dio_flags = 0;
311469c499d1STheodore Ts'o 	loff_t final_size = offset + count;
311569c499d1STheodore Ts'o 
311669c499d1STheodore Ts'o 	/* Use the old path for reads and writes beyond i_size. */
311769c499d1STheodore Ts'o 	if (rw != WRITE || final_size > inode->i_size)
311869c499d1STheodore Ts'o 		return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3119729f52c6SZheng Liu 
31204bd809dbSZheng Liu 	BUG_ON(iocb->private == NULL);
31214bd809dbSZheng Liu 
31224bd809dbSZheng Liu 	/* If we do a overwrite dio, i_mutex locking can be released */
31234bd809dbSZheng Liu 	overwrite = *((int *)iocb->private);
31244bd809dbSZheng Liu 
31254bd809dbSZheng Liu 	if (overwrite) {
31261f555cfaSDmitry Monakhov 		atomic_inc(&inode->i_dio_count);
31274bd809dbSZheng Liu 		down_read(&EXT4_I(inode)->i_data_sem);
31284bd809dbSZheng Liu 		mutex_unlock(&inode->i_mutex);
31294bd809dbSZheng Liu 	}
31304bd809dbSZheng Liu 
31314c0425ffSMingming Cao 	/*
31328d5d02e6SMingming Cao 	 * We could direct write to holes and fallocate.
31338d5d02e6SMingming Cao 	 *
313469c499d1STheodore Ts'o 	 * Allocated blocks to fill the hole are marked as
313569c499d1STheodore Ts'o 	 * uninitialized to prevent parallel buffered read to expose
313669c499d1STheodore Ts'o 	 * the stale data before DIO complete the data IO.
31378d5d02e6SMingming Cao 	 *
313869c499d1STheodore Ts'o 	 * As to previously fallocated extents, ext4 get_block will
313969c499d1STheodore Ts'o 	 * just simply mark the buffer mapped but still keep the
314069c499d1STheodore Ts'o 	 * extents uninitialized.
31414c0425ffSMingming Cao 	 *
314269c499d1STheodore Ts'o 	 * For non AIO case, we will convert those unwritten extents
31438d5d02e6SMingming Cao 	 * to written after return back from blockdev_direct_IO.
31444c0425ffSMingming Cao 	 *
314569c499d1STheodore Ts'o 	 * For async DIO, the conversion needs to be deferred when the
314669c499d1STheodore Ts'o 	 * IO is completed. The ext4 end_io callback function will be
314769c499d1STheodore Ts'o 	 * called to take care of the conversion work.  Here for async
314869c499d1STheodore Ts'o 	 * case, we allocate an io_end structure to hook to the iocb.
31494c0425ffSMingming Cao 	 */
31508d5d02e6SMingming Cao 	iocb->private = NULL;
3151f45ee3a1SDmitry Monakhov 	ext4_inode_aio_set(inode, NULL);
31528d5d02e6SMingming Cao 	if (!is_sync_kiocb(iocb)) {
315369c499d1STheodore Ts'o 		ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS);
31544bd809dbSZheng Liu 		if (!io_end) {
31554bd809dbSZheng Liu 			ret = -ENOMEM;
31564bd809dbSZheng Liu 			goto retake_lock;
31574bd809dbSZheng Liu 		}
3158266991b1SJeff Moyer 		io_end->flag |= EXT4_IO_END_DIRECT;
3159266991b1SJeff Moyer 		iocb->private = io_end;
31608d5d02e6SMingming Cao 		/*
316169c499d1STheodore Ts'o 		 * we save the io structure for current async direct
316269c499d1STheodore Ts'o 		 * IO, so that later ext4_map_blocks() could flag the
316369c499d1STheodore Ts'o 		 * io structure whether there is a unwritten extents
316469c499d1STheodore Ts'o 		 * needs to be converted when IO is completed.
31658d5d02e6SMingming Cao 		 */
3166f45ee3a1SDmitry Monakhov 		ext4_inode_aio_set(inode, io_end);
31678d5d02e6SMingming Cao 	}
31688d5d02e6SMingming Cao 
31698b0f165fSAnatol Pomozov 	if (overwrite) {
31708b0f165fSAnatol Pomozov 		get_block_func = ext4_get_block_write_nolock;
31718b0f165fSAnatol Pomozov 	} else {
31728b0f165fSAnatol Pomozov 		get_block_func = ext4_get_block_write;
31738b0f165fSAnatol Pomozov 		dio_flags = DIO_LOCKING;
31748b0f165fSAnatol Pomozov 	}
3175729f52c6SZheng Liu 	ret = __blockdev_direct_IO(rw, iocb, inode,
3176729f52c6SZheng Liu 				   inode->i_sb->s_bdev, iov,
3177729f52c6SZheng Liu 				   offset, nr_segs,
31788b0f165fSAnatol Pomozov 				   get_block_func,
3179729f52c6SZheng Liu 				   ext4_end_io_dio,
3180729f52c6SZheng Liu 				   NULL,
31818b0f165fSAnatol Pomozov 				   dio_flags);
31828b0f165fSAnatol Pomozov 
31838d5d02e6SMingming Cao 	if (iocb->private)
3184f45ee3a1SDmitry Monakhov 		ext4_inode_aio_set(inode, NULL);
31858d5d02e6SMingming Cao 	/*
318669c499d1STheodore Ts'o 	 * The io_end structure takes a reference to the inode, that
318769c499d1STheodore Ts'o 	 * structure needs to be destroyed and the reference to the
318869c499d1STheodore Ts'o 	 * inode need to be dropped, when IO is complete, even with 0
318969c499d1STheodore Ts'o 	 * byte write, or failed.
31908d5d02e6SMingming Cao 	 *
319169c499d1STheodore Ts'o 	 * In the successful AIO DIO case, the io_end structure will
319269c499d1STheodore Ts'o 	 * be destroyed and the reference to the inode will be dropped
31938d5d02e6SMingming Cao 	 * after the end_io call back function is called.
31948d5d02e6SMingming Cao 	 *
319569c499d1STheodore Ts'o 	 * In the case there is 0 byte write, or error case, since VFS
319669c499d1STheodore Ts'o 	 * direct IO won't invoke the end_io call back function, we
319769c499d1STheodore Ts'o 	 * need to free the end_io structure here.
31988d5d02e6SMingming Cao 	 */
31998d5d02e6SMingming Cao 	if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
32008d5d02e6SMingming Cao 		ext4_free_io_end(iocb->private);
32018d5d02e6SMingming Cao 		iocb->private = NULL;
3202729f52c6SZheng Liu 	} else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
32035f524950SMingming 						EXT4_STATE_DIO_UNWRITTEN)) {
3204109f5565SMingming 		int err;
32058d5d02e6SMingming Cao 		/*
32068d5d02e6SMingming Cao 		 * for non AIO case, since the IO is already
320725985edcSLucas De Marchi 		 * completed, we could do the conversion right here
32088d5d02e6SMingming Cao 		 */
3209109f5565SMingming 		err = ext4_convert_unwritten_extents(inode,
32108d5d02e6SMingming Cao 						     offset, ret);
3211109f5565SMingming 		if (err < 0)
3212109f5565SMingming 			ret = err;
321319f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3214109f5565SMingming 	}
32154bd809dbSZheng Liu 
32164bd809dbSZheng Liu retake_lock:
32174bd809dbSZheng Liu 	/* take i_mutex locking again if we do a ovewrite dio */
32184bd809dbSZheng Liu 	if (overwrite) {
32191f555cfaSDmitry Monakhov 		inode_dio_done(inode);
32204bd809dbSZheng Liu 		up_read(&EXT4_I(inode)->i_data_sem);
32214bd809dbSZheng Liu 		mutex_lock(&inode->i_mutex);
32224bd809dbSZheng Liu 	}
32234bd809dbSZheng Liu 
32244c0425ffSMingming Cao 	return ret;
32254c0425ffSMingming Cao }
32268d5d02e6SMingming Cao 
32274c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
32284c0425ffSMingming Cao 			      const struct iovec *iov, loff_t offset,
32294c0425ffSMingming Cao 			      unsigned long nr_segs)
32304c0425ffSMingming Cao {
32314c0425ffSMingming Cao 	struct file *file = iocb->ki_filp;
32324c0425ffSMingming Cao 	struct inode *inode = file->f_mapping->host;
32330562e0baSJiaying Zhang 	ssize_t ret;
32344c0425ffSMingming Cao 
323584ebd795STheodore Ts'o 	/*
323684ebd795STheodore Ts'o 	 * If we are doing data journalling we don't support O_DIRECT
323784ebd795STheodore Ts'o 	 */
323884ebd795STheodore Ts'o 	if (ext4_should_journal_data(inode))
323984ebd795STheodore Ts'o 		return 0;
324084ebd795STheodore Ts'o 
324146c7f254STao Ma 	/* Let buffer I/O handle the inline data case. */
324246c7f254STao Ma 	if (ext4_has_inline_data(inode))
324346c7f254STao Ma 		return 0;
324446c7f254STao Ma 
32450562e0baSJiaying Zhang 	trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
324612e9b892SDmitry Monakhov 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
32470562e0baSJiaying Zhang 		ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
32480562e0baSJiaying Zhang 	else
32490562e0baSJiaying Zhang 		ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
32500562e0baSJiaying Zhang 	trace_ext4_direct_IO_exit(inode, offset,
32510562e0baSJiaying Zhang 				iov_length(iov, nr_segs), rw, ret);
32520562e0baSJiaying Zhang 	return ret;
32534c0425ffSMingming Cao }
32544c0425ffSMingming Cao 
3255ac27a0ecSDave Kleikamp /*
3256617ba13bSMingming Cao  * Pages can be marked dirty completely asynchronously from ext4's journalling
3257ac27a0ecSDave Kleikamp  * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do
3258ac27a0ecSDave Kleikamp  * much here because ->set_page_dirty is called under VFS locks.  The page is
3259ac27a0ecSDave Kleikamp  * not necessarily locked.
3260ac27a0ecSDave Kleikamp  *
3261ac27a0ecSDave Kleikamp  * We cannot just dirty the page and leave attached buffers clean, because the
3262ac27a0ecSDave Kleikamp  * buffers' dirty state is "definitive".  We cannot just set the buffers dirty
3263ac27a0ecSDave Kleikamp  * or jbddirty because all the journalling code will explode.
3264ac27a0ecSDave Kleikamp  *
3265ac27a0ecSDave Kleikamp  * So what we do is to mark the page "pending dirty" and next time writepage
3266ac27a0ecSDave Kleikamp  * is called, propagate that into the buffers appropriately.
3267ac27a0ecSDave Kleikamp  */
3268617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page)
3269ac27a0ecSDave Kleikamp {
3270ac27a0ecSDave Kleikamp 	SetPageChecked(page);
3271ac27a0ecSDave Kleikamp 	return __set_page_dirty_nobuffers(page);
3272ac27a0ecSDave Kleikamp }
3273ac27a0ecSDave Kleikamp 
327474d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = {
3275617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3276617ba13bSMingming Cao 	.readpages		= ext4_readpages,
327743ce1d23SAneesh Kumar K.V 	.writepage		= ext4_writepage,
3278bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
327974d553aaSTheodore Ts'o 	.write_end		= ext4_write_end,
3280617ba13bSMingming Cao 	.bmap			= ext4_bmap,
3281617ba13bSMingming Cao 	.invalidatepage		= ext4_invalidatepage,
3282617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
3283617ba13bSMingming Cao 	.direct_IO		= ext4_direct_IO,
3284ac27a0ecSDave Kleikamp 	.migratepage		= buffer_migrate_page,
32858ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3286aa261f54SAndi Kleen 	.error_remove_page	= generic_error_remove_page,
3287ac27a0ecSDave Kleikamp };
3288ac27a0ecSDave Kleikamp 
3289617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = {
3290617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3291617ba13bSMingming Cao 	.readpages		= ext4_readpages,
329243ce1d23SAneesh Kumar K.V 	.writepage		= ext4_writepage,
3293bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
3294bfc1af65SNick Piggin 	.write_end		= ext4_journalled_write_end,
3295617ba13bSMingming Cao 	.set_page_dirty		= ext4_journalled_set_page_dirty,
3296617ba13bSMingming Cao 	.bmap			= ext4_bmap,
32974520fb3cSJan Kara 	.invalidatepage		= ext4_journalled_invalidatepage,
3298617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
329984ebd795STheodore Ts'o 	.direct_IO		= ext4_direct_IO,
33008ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3301aa261f54SAndi Kleen 	.error_remove_page	= generic_error_remove_page,
3302ac27a0ecSDave Kleikamp };
3303ac27a0ecSDave Kleikamp 
330464769240SAlex Tomas static const struct address_space_operations ext4_da_aops = {
330564769240SAlex Tomas 	.readpage		= ext4_readpage,
330664769240SAlex Tomas 	.readpages		= ext4_readpages,
330743ce1d23SAneesh Kumar K.V 	.writepage		= ext4_writepage,
330864769240SAlex Tomas 	.writepages		= ext4_da_writepages,
330964769240SAlex Tomas 	.write_begin		= ext4_da_write_begin,
331064769240SAlex Tomas 	.write_end		= ext4_da_write_end,
331164769240SAlex Tomas 	.bmap			= ext4_bmap,
331264769240SAlex Tomas 	.invalidatepage		= ext4_da_invalidatepage,
331364769240SAlex Tomas 	.releasepage		= ext4_releasepage,
331464769240SAlex Tomas 	.direct_IO		= ext4_direct_IO,
331564769240SAlex Tomas 	.migratepage		= buffer_migrate_page,
33168ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3317aa261f54SAndi Kleen 	.error_remove_page	= generic_error_remove_page,
331864769240SAlex Tomas };
331964769240SAlex Tomas 
3320617ba13bSMingming Cao void ext4_set_aops(struct inode *inode)
3321ac27a0ecSDave Kleikamp {
33223d2b1582SLukas Czerner 	switch (ext4_inode_journal_mode(inode)) {
33233d2b1582SLukas Czerner 	case EXT4_INODE_ORDERED_DATA_MODE:
332474d553aaSTheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
33253d2b1582SLukas Czerner 		break;
33263d2b1582SLukas Czerner 	case EXT4_INODE_WRITEBACK_DATA_MODE:
332774d553aaSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
33283d2b1582SLukas Czerner 		break;
33293d2b1582SLukas Czerner 	case EXT4_INODE_JOURNAL_DATA_MODE:
3330617ba13bSMingming Cao 		inode->i_mapping->a_ops = &ext4_journalled_aops;
333174d553aaSTheodore Ts'o 		return;
33323d2b1582SLukas Czerner 	default:
33333d2b1582SLukas Czerner 		BUG();
33343d2b1582SLukas Czerner 	}
333574d553aaSTheodore Ts'o 	if (test_opt(inode->i_sb, DELALLOC))
333674d553aaSTheodore Ts'o 		inode->i_mapping->a_ops = &ext4_da_aops;
333774d553aaSTheodore Ts'o 	else
333874d553aaSTheodore Ts'o 		inode->i_mapping->a_ops = &ext4_aops;
3339ac27a0ecSDave Kleikamp }
3340ac27a0ecSDave Kleikamp 
33414e96b2dbSAllison Henderson 
33424e96b2dbSAllison Henderson /*
33434e96b2dbSAllison Henderson  * ext4_discard_partial_page_buffers()
33444e96b2dbSAllison Henderson  * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
33454e96b2dbSAllison Henderson  * This function finds and locks the page containing the offset
33464e96b2dbSAllison Henderson  * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
33474e96b2dbSAllison Henderson  * Calling functions that already have the page locked should call
33484e96b2dbSAllison Henderson  * ext4_discard_partial_page_buffers_no_lock directly.
33494e96b2dbSAllison Henderson  */
33504e96b2dbSAllison Henderson int ext4_discard_partial_page_buffers(handle_t *handle,
33514e96b2dbSAllison Henderson 		struct address_space *mapping, loff_t from,
33524e96b2dbSAllison Henderson 		loff_t length, int flags)
33534e96b2dbSAllison Henderson {
33544e96b2dbSAllison Henderson 	struct inode *inode = mapping->host;
33554e96b2dbSAllison Henderson 	struct page *page;
33564e96b2dbSAllison Henderson 	int err = 0;
33574e96b2dbSAllison Henderson 
33584e96b2dbSAllison Henderson 	page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
33594e96b2dbSAllison Henderson 				   mapping_gfp_mask(mapping) & ~__GFP_FS);
33604e96b2dbSAllison Henderson 	if (!page)
33615129d05fSYongqiang Yang 		return -ENOMEM;
33624e96b2dbSAllison Henderson 
33634e96b2dbSAllison Henderson 	err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
33644e96b2dbSAllison Henderson 		from, length, flags);
33654e96b2dbSAllison Henderson 
33664e96b2dbSAllison Henderson 	unlock_page(page);
33674e96b2dbSAllison Henderson 	page_cache_release(page);
33684e96b2dbSAllison Henderson 	return err;
33694e96b2dbSAllison Henderson }
33704e96b2dbSAllison Henderson 
33714e96b2dbSAllison Henderson /*
33724e96b2dbSAllison Henderson  * ext4_discard_partial_page_buffers_no_lock()
33734e96b2dbSAllison Henderson  * Zeros a page range of length 'length' starting from offset 'from'.
33744e96b2dbSAllison Henderson  * Buffer heads that correspond to the block aligned regions of the
33754e96b2dbSAllison Henderson  * zeroed range will be unmapped.  Unblock aligned regions
33764e96b2dbSAllison Henderson  * will have the corresponding buffer head mapped if needed so that
33774e96b2dbSAllison Henderson  * that region of the page can be updated with the partial zero out.
33784e96b2dbSAllison Henderson  *
33794e96b2dbSAllison Henderson  * This function assumes that the page has already been  locked.  The
33804e96b2dbSAllison Henderson  * The range to be discarded must be contained with in the given page.
33814e96b2dbSAllison Henderson  * If the specified range exceeds the end of the page it will be shortened
33824e96b2dbSAllison Henderson  * to the end of the page that corresponds to 'from'.  This function is
33834e96b2dbSAllison Henderson  * appropriate for updating a page and it buffer heads to be unmapped and
33844e96b2dbSAllison Henderson  * zeroed for blocks that have been either released, or are going to be
33854e96b2dbSAllison Henderson  * released.
33864e96b2dbSAllison Henderson  *
33874e96b2dbSAllison Henderson  * handle: The journal handle
33884e96b2dbSAllison Henderson  * inode:  The files inode
33894e96b2dbSAllison Henderson  * page:   A locked page that contains the offset "from"
33904907cb7bSAnatol Pomozov  * from:   The starting byte offset (from the beginning of the file)
33914e96b2dbSAllison Henderson  *         to begin discarding
33924e96b2dbSAllison Henderson  * len:    The length of bytes to discard
33934e96b2dbSAllison Henderson  * flags:  Optional flags that may be used:
33944e96b2dbSAllison Henderson  *
33954e96b2dbSAllison Henderson  *         EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
33964e96b2dbSAllison Henderson  *         Only zero the regions of the page whose buffer heads
33974e96b2dbSAllison Henderson  *         have already been unmapped.  This flag is appropriate
33984907cb7bSAnatol Pomozov  *         for updating the contents of a page whose blocks may
33994e96b2dbSAllison Henderson  *         have already been released, and we only want to zero
34004e96b2dbSAllison Henderson  *         out the regions that correspond to those released blocks.
34014e96b2dbSAllison Henderson  *
34024907cb7bSAnatol Pomozov  * Returns zero on success or negative on failure.
34034e96b2dbSAllison Henderson  */
34045f163cc7SEric Sandeen static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
34054e96b2dbSAllison Henderson 		struct inode *inode, struct page *page, loff_t from,
34064e96b2dbSAllison Henderson 		loff_t length, int flags)
34074e96b2dbSAllison Henderson {
34084e96b2dbSAllison Henderson 	ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
34094e96b2dbSAllison Henderson 	unsigned int offset = from & (PAGE_CACHE_SIZE-1);
34104e96b2dbSAllison Henderson 	unsigned int blocksize, max, pos;
34114e96b2dbSAllison Henderson 	ext4_lblk_t iblock;
34124e96b2dbSAllison Henderson 	struct buffer_head *bh;
34134e96b2dbSAllison Henderson 	int err = 0;
34144e96b2dbSAllison Henderson 
34154e96b2dbSAllison Henderson 	blocksize = inode->i_sb->s_blocksize;
34164e96b2dbSAllison Henderson 	max = PAGE_CACHE_SIZE - offset;
34174e96b2dbSAllison Henderson 
34184e96b2dbSAllison Henderson 	if (index != page->index)
34194e96b2dbSAllison Henderson 		return -EINVAL;
34204e96b2dbSAllison Henderson 
34214e96b2dbSAllison Henderson 	/*
34224e96b2dbSAllison Henderson 	 * correct length if it does not fall between
34234e96b2dbSAllison Henderson 	 * 'from' and the end of the page
34244e96b2dbSAllison Henderson 	 */
34254e96b2dbSAllison Henderson 	if (length > max || length < 0)
34264e96b2dbSAllison Henderson 		length = max;
34274e96b2dbSAllison Henderson 
34284e96b2dbSAllison Henderson 	iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
34294e96b2dbSAllison Henderson 
3430093e6e36SYongqiang Yang 	if (!page_has_buffers(page))
34314e96b2dbSAllison Henderson 		create_empty_buffers(page, blocksize, 0);
34324e96b2dbSAllison Henderson 
34334e96b2dbSAllison Henderson 	/* Find the buffer that contains "offset" */
34344e96b2dbSAllison Henderson 	bh = page_buffers(page);
34354e96b2dbSAllison Henderson 	pos = blocksize;
34364e96b2dbSAllison Henderson 	while (offset >= pos) {
34374e96b2dbSAllison Henderson 		bh = bh->b_this_page;
34384e96b2dbSAllison Henderson 		iblock++;
34394e96b2dbSAllison Henderson 		pos += blocksize;
34404e96b2dbSAllison Henderson 	}
34414e96b2dbSAllison Henderson 
34424e96b2dbSAllison Henderson 	pos = offset;
34434e96b2dbSAllison Henderson 	while (pos < offset + length) {
3444e260daf2SYongqiang Yang 		unsigned int end_of_block, range_to_discard;
3445e260daf2SYongqiang Yang 
34464e96b2dbSAllison Henderson 		err = 0;
34474e96b2dbSAllison Henderson 
34484e96b2dbSAllison Henderson 		/* The length of space left to zero and unmap */
34494e96b2dbSAllison Henderson 		range_to_discard = offset + length - pos;
34504e96b2dbSAllison Henderson 
34514e96b2dbSAllison Henderson 		/* The length of space until the end of the block */
34524e96b2dbSAllison Henderson 		end_of_block = blocksize - (pos & (blocksize-1));
34534e96b2dbSAllison Henderson 
34544e96b2dbSAllison Henderson 		/*
34554e96b2dbSAllison Henderson 		 * Do not unmap or zero past end of block
34564e96b2dbSAllison Henderson 		 * for this buffer head
34574e96b2dbSAllison Henderson 		 */
34584e96b2dbSAllison Henderson 		if (range_to_discard > end_of_block)
34594e96b2dbSAllison Henderson 			range_to_discard = end_of_block;
34604e96b2dbSAllison Henderson 
34614e96b2dbSAllison Henderson 
34624e96b2dbSAllison Henderson 		/*
34634e96b2dbSAllison Henderson 		 * Skip this buffer head if we are only zeroing unampped
34644e96b2dbSAllison Henderson 		 * regions of the page
34654e96b2dbSAllison Henderson 		 */
34664e96b2dbSAllison Henderson 		if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
34674e96b2dbSAllison Henderson 			buffer_mapped(bh))
34684e96b2dbSAllison Henderson 				goto next;
34694e96b2dbSAllison Henderson 
34704e96b2dbSAllison Henderson 		/* If the range is block aligned, unmap */
34714e96b2dbSAllison Henderson 		if (range_to_discard == blocksize) {
34724e96b2dbSAllison Henderson 			clear_buffer_dirty(bh);
34734e96b2dbSAllison Henderson 			bh->b_bdev = NULL;
34744e96b2dbSAllison Henderson 			clear_buffer_mapped(bh);
34754e96b2dbSAllison Henderson 			clear_buffer_req(bh);
34764e96b2dbSAllison Henderson 			clear_buffer_new(bh);
34774e96b2dbSAllison Henderson 			clear_buffer_delay(bh);
34784e96b2dbSAllison Henderson 			clear_buffer_unwritten(bh);
34794e96b2dbSAllison Henderson 			clear_buffer_uptodate(bh);
34804e96b2dbSAllison Henderson 			zero_user(page, pos, range_to_discard);
34814e96b2dbSAllison Henderson 			BUFFER_TRACE(bh, "Buffer discarded");
34824e96b2dbSAllison Henderson 			goto next;
34834e96b2dbSAllison Henderson 		}
34844e96b2dbSAllison Henderson 
34854e96b2dbSAllison Henderson 		/*
34864e96b2dbSAllison Henderson 		 * If this block is not completely contained in the range
34874e96b2dbSAllison Henderson 		 * to be discarded, then it is not going to be released. Because
34884e96b2dbSAllison Henderson 		 * we need to keep this block, we need to make sure this part
34894e96b2dbSAllison Henderson 		 * of the page is uptodate before we modify it by writeing
34904e96b2dbSAllison Henderson 		 * partial zeros on it.
34914e96b2dbSAllison Henderson 		 */
34924e96b2dbSAllison Henderson 		if (!buffer_mapped(bh)) {
34934e96b2dbSAllison Henderson 			/*
34944e96b2dbSAllison Henderson 			 * Buffer head must be mapped before we can read
34954e96b2dbSAllison Henderson 			 * from the block
34964e96b2dbSAllison Henderson 			 */
34974e96b2dbSAllison Henderson 			BUFFER_TRACE(bh, "unmapped");
34984e96b2dbSAllison Henderson 			ext4_get_block(inode, iblock, bh, 0);
34994e96b2dbSAllison Henderson 			/* unmapped? It's a hole - nothing to do */
35004e96b2dbSAllison Henderson 			if (!buffer_mapped(bh)) {
35014e96b2dbSAllison Henderson 				BUFFER_TRACE(bh, "still unmapped");
35024e96b2dbSAllison Henderson 				goto next;
35034e96b2dbSAllison Henderson 			}
35044e96b2dbSAllison Henderson 		}
35054e96b2dbSAllison Henderson 
35064e96b2dbSAllison Henderson 		/* Ok, it's mapped. Make sure it's up-to-date */
35074e96b2dbSAllison Henderson 		if (PageUptodate(page))
35084e96b2dbSAllison Henderson 			set_buffer_uptodate(bh);
35094e96b2dbSAllison Henderson 
35104e96b2dbSAllison Henderson 		if (!buffer_uptodate(bh)) {
35114e96b2dbSAllison Henderson 			err = -EIO;
35124e96b2dbSAllison Henderson 			ll_rw_block(READ, 1, &bh);
35134e96b2dbSAllison Henderson 			wait_on_buffer(bh);
35144e96b2dbSAllison Henderson 			/* Uhhuh. Read error. Complain and punt.*/
35154e96b2dbSAllison Henderson 			if (!buffer_uptodate(bh))
35164e96b2dbSAllison Henderson 				goto next;
35174e96b2dbSAllison Henderson 		}
35184e96b2dbSAllison Henderson 
35194e96b2dbSAllison Henderson 		if (ext4_should_journal_data(inode)) {
35204e96b2dbSAllison Henderson 			BUFFER_TRACE(bh, "get write access");
35214e96b2dbSAllison Henderson 			err = ext4_journal_get_write_access(handle, bh);
35224e96b2dbSAllison Henderson 			if (err)
35234e96b2dbSAllison Henderson 				goto next;
35244e96b2dbSAllison Henderson 		}
35254e96b2dbSAllison Henderson 
35264e96b2dbSAllison Henderson 		zero_user(page, pos, range_to_discard);
35274e96b2dbSAllison Henderson 
35284e96b2dbSAllison Henderson 		err = 0;
35294e96b2dbSAllison Henderson 		if (ext4_should_journal_data(inode)) {
35304e96b2dbSAllison Henderson 			err = ext4_handle_dirty_metadata(handle, inode, bh);
3531decbd919STheodore Ts'o 		} else
35324e96b2dbSAllison Henderson 			mark_buffer_dirty(bh);
35334e96b2dbSAllison Henderson 
35344e96b2dbSAllison Henderson 		BUFFER_TRACE(bh, "Partial buffer zeroed");
35354e96b2dbSAllison Henderson next:
35364e96b2dbSAllison Henderson 		bh = bh->b_this_page;
35374e96b2dbSAllison Henderson 		iblock++;
35384e96b2dbSAllison Henderson 		pos += range_to_discard;
35394e96b2dbSAllison Henderson 	}
35404e96b2dbSAllison Henderson 
35414e96b2dbSAllison Henderson 	return err;
35424e96b2dbSAllison Henderson }
35434e96b2dbSAllison Henderson 
354491ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode)
354591ef4cafSDuane Griffin {
354691ef4cafSDuane Griffin 	if (S_ISREG(inode->i_mode))
354791ef4cafSDuane Griffin 		return 1;
354891ef4cafSDuane Griffin 	if (S_ISDIR(inode->i_mode))
354991ef4cafSDuane Griffin 		return 1;
355091ef4cafSDuane Griffin 	if (S_ISLNK(inode->i_mode))
355191ef4cafSDuane Griffin 		return !ext4_inode_is_fast_symlink(inode);
355291ef4cafSDuane Griffin 	return 0;
355391ef4cafSDuane Griffin }
355491ef4cafSDuane Griffin 
3555ac27a0ecSDave Kleikamp /*
3556a4bb6b64SAllison Henderson  * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3557a4bb6b64SAllison Henderson  * associated with the given offset and length
3558a4bb6b64SAllison Henderson  *
3559a4bb6b64SAllison Henderson  * @inode:  File inode
3560a4bb6b64SAllison Henderson  * @offset: The offset where the hole will begin
3561a4bb6b64SAllison Henderson  * @len:    The length of the hole
3562a4bb6b64SAllison Henderson  *
35634907cb7bSAnatol Pomozov  * Returns: 0 on success or negative on failure
3564a4bb6b64SAllison Henderson  */
3565a4bb6b64SAllison Henderson 
3566a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3567a4bb6b64SAllison Henderson {
3568496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
356926a4c0c6STheodore Ts'o 	struct super_block *sb = inode->i_sb;
357026a4c0c6STheodore Ts'o 	ext4_lblk_t first_block, stop_block;
357126a4c0c6STheodore Ts'o 	struct address_space *mapping = inode->i_mapping;
357226a4c0c6STheodore Ts'o 	loff_t first_page, last_page, page_len;
357326a4c0c6STheodore Ts'o 	loff_t first_page_offset, last_page_offset;
357426a4c0c6STheodore Ts'o 	handle_t *handle;
357526a4c0c6STheodore Ts'o 	unsigned int credits;
357626a4c0c6STheodore Ts'o 	int ret = 0;
357726a4c0c6STheodore Ts'o 
3578a4bb6b64SAllison Henderson 	if (!S_ISREG(inode->i_mode))
357973355192SAllison Henderson 		return -EOPNOTSUPP;
3580a4bb6b64SAllison Henderson 
358126a4c0c6STheodore Ts'o 	if (EXT4_SB(sb)->s_cluster_ratio > 1) {
3582bab08ab9STheodore Ts'o 		/* TODO: Add support for bigalloc file systems */
358373355192SAllison Henderson 		return -EOPNOTSUPP;
3584bab08ab9STheodore Ts'o 	}
3585bab08ab9STheodore Ts'o 
3586aaddea81SZheng Liu 	trace_ext4_punch_hole(inode, offset, length);
3587aaddea81SZheng Liu 
358826a4c0c6STheodore Ts'o 	/*
358926a4c0c6STheodore Ts'o 	 * Write out all dirty pages to avoid race conditions
359026a4c0c6STheodore Ts'o 	 * Then release them.
359126a4c0c6STheodore Ts'o 	 */
359226a4c0c6STheodore Ts'o 	if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
359326a4c0c6STheodore Ts'o 		ret = filemap_write_and_wait_range(mapping, offset,
359426a4c0c6STheodore Ts'o 						   offset + length - 1);
359526a4c0c6STheodore Ts'o 		if (ret)
359626a4c0c6STheodore Ts'o 			return ret;
359726a4c0c6STheodore Ts'o 	}
359826a4c0c6STheodore Ts'o 
359926a4c0c6STheodore Ts'o 	mutex_lock(&inode->i_mutex);
360026a4c0c6STheodore Ts'o 	/* It's not possible punch hole on append only file */
360126a4c0c6STheodore Ts'o 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
360226a4c0c6STheodore Ts'o 		ret = -EPERM;
360326a4c0c6STheodore Ts'o 		goto out_mutex;
360426a4c0c6STheodore Ts'o 	}
360526a4c0c6STheodore Ts'o 	if (IS_SWAPFILE(inode)) {
360626a4c0c6STheodore Ts'o 		ret = -ETXTBSY;
360726a4c0c6STheodore Ts'o 		goto out_mutex;
360826a4c0c6STheodore Ts'o 	}
360926a4c0c6STheodore Ts'o 
361026a4c0c6STheodore Ts'o 	/* No need to punch hole beyond i_size */
361126a4c0c6STheodore Ts'o 	if (offset >= inode->i_size)
361226a4c0c6STheodore Ts'o 		goto out_mutex;
361326a4c0c6STheodore Ts'o 
361426a4c0c6STheodore Ts'o 	/*
361526a4c0c6STheodore Ts'o 	 * If the hole extends beyond i_size, set the hole
361626a4c0c6STheodore Ts'o 	 * to end after the page that contains i_size
361726a4c0c6STheodore Ts'o 	 */
361826a4c0c6STheodore Ts'o 	if (offset + length > inode->i_size) {
361926a4c0c6STheodore Ts'o 		length = inode->i_size +
362026a4c0c6STheodore Ts'o 		   PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
362126a4c0c6STheodore Ts'o 		   offset;
362226a4c0c6STheodore Ts'o 	}
362326a4c0c6STheodore Ts'o 
362426a4c0c6STheodore Ts'o 	first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
362526a4c0c6STheodore Ts'o 	last_page = (offset + length) >> PAGE_CACHE_SHIFT;
362626a4c0c6STheodore Ts'o 
362726a4c0c6STheodore Ts'o 	first_page_offset = first_page << PAGE_CACHE_SHIFT;
362826a4c0c6STheodore Ts'o 	last_page_offset = last_page << PAGE_CACHE_SHIFT;
362926a4c0c6STheodore Ts'o 
363026a4c0c6STheodore Ts'o 	/* Now release the pages */
363126a4c0c6STheodore Ts'o 	if (last_page_offset > first_page_offset) {
363226a4c0c6STheodore Ts'o 		truncate_pagecache_range(inode, first_page_offset,
363326a4c0c6STheodore Ts'o 					 last_page_offset - 1);
363426a4c0c6STheodore Ts'o 	}
363526a4c0c6STheodore Ts'o 
363626a4c0c6STheodore Ts'o 	/* Wait all existing dio workers, newcomers will block on i_mutex */
363726a4c0c6STheodore Ts'o 	ext4_inode_block_unlocked_dio(inode);
363826a4c0c6STheodore Ts'o 	ret = ext4_flush_unwritten_io(inode);
363926a4c0c6STheodore Ts'o 	if (ret)
364026a4c0c6STheodore Ts'o 		goto out_dio;
364126a4c0c6STheodore Ts'o 	inode_dio_wait(inode);
364226a4c0c6STheodore Ts'o 
364326a4c0c6STheodore Ts'o 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
364426a4c0c6STheodore Ts'o 		credits = ext4_writepage_trans_blocks(inode);
364526a4c0c6STheodore Ts'o 	else
364626a4c0c6STheodore Ts'o 		credits = ext4_blocks_for_truncate(inode);
364726a4c0c6STheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
364826a4c0c6STheodore Ts'o 	if (IS_ERR(handle)) {
364926a4c0c6STheodore Ts'o 		ret = PTR_ERR(handle);
365026a4c0c6STheodore Ts'o 		ext4_std_error(sb, ret);
365126a4c0c6STheodore Ts'o 		goto out_dio;
365226a4c0c6STheodore Ts'o 	}
365326a4c0c6STheodore Ts'o 
365426a4c0c6STheodore Ts'o 	/*
365526a4c0c6STheodore Ts'o 	 * Now we need to zero out the non-page-aligned data in the
365626a4c0c6STheodore Ts'o 	 * pages at the start and tail of the hole, and unmap the
365726a4c0c6STheodore Ts'o 	 * buffer heads for the block aligned regions of the page that
365826a4c0c6STheodore Ts'o 	 * were completely zeroed.
365926a4c0c6STheodore Ts'o 	 */
366026a4c0c6STheodore Ts'o 	if (first_page > last_page) {
366126a4c0c6STheodore Ts'o 		/*
366226a4c0c6STheodore Ts'o 		 * If the file space being truncated is contained
366326a4c0c6STheodore Ts'o 		 * within a page just zero out and unmap the middle of
366426a4c0c6STheodore Ts'o 		 * that page
366526a4c0c6STheodore Ts'o 		 */
366626a4c0c6STheodore Ts'o 		ret = ext4_discard_partial_page_buffers(handle,
366726a4c0c6STheodore Ts'o 			mapping, offset, length, 0);
366826a4c0c6STheodore Ts'o 
366926a4c0c6STheodore Ts'o 		if (ret)
367026a4c0c6STheodore Ts'o 			goto out_stop;
367126a4c0c6STheodore Ts'o 	} else {
367226a4c0c6STheodore Ts'o 		/*
367326a4c0c6STheodore Ts'o 		 * zero out and unmap the partial page that contains
367426a4c0c6STheodore Ts'o 		 * the start of the hole
367526a4c0c6STheodore Ts'o 		 */
367626a4c0c6STheodore Ts'o 		page_len = first_page_offset - offset;
367726a4c0c6STheodore Ts'o 		if (page_len > 0) {
367826a4c0c6STheodore Ts'o 			ret = ext4_discard_partial_page_buffers(handle, mapping,
367926a4c0c6STheodore Ts'o 						offset, page_len, 0);
368026a4c0c6STheodore Ts'o 			if (ret)
368126a4c0c6STheodore Ts'o 				goto out_stop;
368226a4c0c6STheodore Ts'o 		}
368326a4c0c6STheodore Ts'o 
368426a4c0c6STheodore Ts'o 		/*
368526a4c0c6STheodore Ts'o 		 * zero out and unmap the partial page that contains
368626a4c0c6STheodore Ts'o 		 * the end of the hole
368726a4c0c6STheodore Ts'o 		 */
368826a4c0c6STheodore Ts'o 		page_len = offset + length - last_page_offset;
368926a4c0c6STheodore Ts'o 		if (page_len > 0) {
369026a4c0c6STheodore Ts'o 			ret = ext4_discard_partial_page_buffers(handle, mapping,
369126a4c0c6STheodore Ts'o 					last_page_offset, page_len, 0);
369226a4c0c6STheodore Ts'o 			if (ret)
369326a4c0c6STheodore Ts'o 				goto out_stop;
369426a4c0c6STheodore Ts'o 		}
369526a4c0c6STheodore Ts'o 	}
369626a4c0c6STheodore Ts'o 
369726a4c0c6STheodore Ts'o 	/*
369826a4c0c6STheodore Ts'o 	 * If i_size is contained in the last page, we need to
369926a4c0c6STheodore Ts'o 	 * unmap and zero the partial page after i_size
370026a4c0c6STheodore Ts'o 	 */
370126a4c0c6STheodore Ts'o 	if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
370226a4c0c6STheodore Ts'o 	   inode->i_size % PAGE_CACHE_SIZE != 0) {
370326a4c0c6STheodore Ts'o 		page_len = PAGE_CACHE_SIZE -
370426a4c0c6STheodore Ts'o 			(inode->i_size & (PAGE_CACHE_SIZE - 1));
370526a4c0c6STheodore Ts'o 
370626a4c0c6STheodore Ts'o 		if (page_len > 0) {
370726a4c0c6STheodore Ts'o 			ret = ext4_discard_partial_page_buffers(handle,
370826a4c0c6STheodore Ts'o 					mapping, inode->i_size, page_len, 0);
370926a4c0c6STheodore Ts'o 
371026a4c0c6STheodore Ts'o 			if (ret)
371126a4c0c6STheodore Ts'o 				goto out_stop;
371226a4c0c6STheodore Ts'o 		}
371326a4c0c6STheodore Ts'o 	}
371426a4c0c6STheodore Ts'o 
371526a4c0c6STheodore Ts'o 	first_block = (offset + sb->s_blocksize - 1) >>
371626a4c0c6STheodore Ts'o 		EXT4_BLOCK_SIZE_BITS(sb);
371726a4c0c6STheodore Ts'o 	stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
371826a4c0c6STheodore Ts'o 
371926a4c0c6STheodore Ts'o 	/* If there are no blocks to remove, return now */
372026a4c0c6STheodore Ts'o 	if (first_block >= stop_block)
372126a4c0c6STheodore Ts'o 		goto out_stop;
372226a4c0c6STheodore Ts'o 
372326a4c0c6STheodore Ts'o 	down_write(&EXT4_I(inode)->i_data_sem);
372426a4c0c6STheodore Ts'o 	ext4_discard_preallocations(inode);
372526a4c0c6STheodore Ts'o 
372626a4c0c6STheodore Ts'o 	ret = ext4_es_remove_extent(inode, first_block,
372726a4c0c6STheodore Ts'o 				    stop_block - first_block);
372826a4c0c6STheodore Ts'o 	if (ret) {
372926a4c0c6STheodore Ts'o 		up_write(&EXT4_I(inode)->i_data_sem);
373026a4c0c6STheodore Ts'o 		goto out_stop;
373126a4c0c6STheodore Ts'o 	}
373226a4c0c6STheodore Ts'o 
373326a4c0c6STheodore Ts'o 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
373426a4c0c6STheodore Ts'o 		ret = ext4_ext_remove_space(inode, first_block,
373526a4c0c6STheodore Ts'o 					    stop_block - 1);
373626a4c0c6STheodore Ts'o 	else
373726a4c0c6STheodore Ts'o 		ret = ext4_free_hole_blocks(handle, inode, first_block,
373826a4c0c6STheodore Ts'o 					    stop_block);
373926a4c0c6STheodore Ts'o 
374026a4c0c6STheodore Ts'o 	ext4_discard_preallocations(inode);
3741*819c4920STheodore Ts'o 	up_write(&EXT4_I(inode)->i_data_sem);
374226a4c0c6STheodore Ts'o 	if (IS_SYNC(inode))
374326a4c0c6STheodore Ts'o 		ext4_handle_sync(handle);
374426a4c0c6STheodore Ts'o 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
374526a4c0c6STheodore Ts'o 	ext4_mark_inode_dirty(handle, inode);
374626a4c0c6STheodore Ts'o out_stop:
374726a4c0c6STheodore Ts'o 	ext4_journal_stop(handle);
374826a4c0c6STheodore Ts'o out_dio:
374926a4c0c6STheodore Ts'o 	ext4_inode_resume_unlocked_dio(inode);
375026a4c0c6STheodore Ts'o out_mutex:
375126a4c0c6STheodore Ts'o 	mutex_unlock(&inode->i_mutex);
375226a4c0c6STheodore Ts'o 	return ret;
3753a4bb6b64SAllison Henderson }
3754a4bb6b64SAllison Henderson 
3755a4bb6b64SAllison Henderson /*
3756617ba13bSMingming Cao  * ext4_truncate()
3757ac27a0ecSDave Kleikamp  *
3758617ba13bSMingming Cao  * We block out ext4_get_block() block instantiations across the entire
3759617ba13bSMingming Cao  * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3760ac27a0ecSDave Kleikamp  * simultaneously on behalf of the same inode.
3761ac27a0ecSDave Kleikamp  *
376242b2aa86SJustin P. Mattock  * As we work through the truncate and commit bits of it to the journal there
3763ac27a0ecSDave Kleikamp  * is one core, guiding principle: the file's tree must always be consistent on
3764ac27a0ecSDave Kleikamp  * disk.  We must be able to restart the truncate after a crash.
3765ac27a0ecSDave Kleikamp  *
3766ac27a0ecSDave Kleikamp  * The file's tree may be transiently inconsistent in memory (although it
3767ac27a0ecSDave Kleikamp  * probably isn't), but whenever we close off and commit a journal transaction,
3768ac27a0ecSDave Kleikamp  * the contents of (the filesystem + the journal) must be consistent and
3769ac27a0ecSDave Kleikamp  * restartable.  It's pretty simple, really: bottom up, right to left (although
3770ac27a0ecSDave Kleikamp  * left-to-right works OK too).
3771ac27a0ecSDave Kleikamp  *
3772ac27a0ecSDave Kleikamp  * Note that at recovery time, journal replay occurs *before* the restart of
3773ac27a0ecSDave Kleikamp  * truncate against the orphan inode list.
3774ac27a0ecSDave Kleikamp  *
3775ac27a0ecSDave Kleikamp  * The committed inode has the new, desired i_size (which is the same as
3776617ba13bSMingming Cao  * i_disksize in this case).  After a crash, ext4_orphan_cleanup() will see
3777ac27a0ecSDave Kleikamp  * that this inode's truncate did not complete and it will again call
3778617ba13bSMingming Cao  * ext4_truncate() to have another go.  So there will be instantiated blocks
3779617ba13bSMingming Cao  * to the right of the truncation point in a crashed ext4 filesystem.  But
3780ac27a0ecSDave Kleikamp  * that's fine - as long as they are linked from the inode, the post-crash
3781617ba13bSMingming Cao  * ext4_truncate() run will find them and release them.
3782ac27a0ecSDave Kleikamp  */
3783617ba13bSMingming Cao void ext4_truncate(struct inode *inode)
3784ac27a0ecSDave Kleikamp {
3785*819c4920STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
3786*819c4920STheodore Ts'o 	unsigned int credits;
3787*819c4920STheodore Ts'o 	handle_t *handle;
3788*819c4920STheodore Ts'o 	struct address_space *mapping = inode->i_mapping;
3789*819c4920STheodore Ts'o 	loff_t page_len;
3790*819c4920STheodore Ts'o 
37910562e0baSJiaying Zhang 	trace_ext4_truncate_enter(inode);
37920562e0baSJiaying Zhang 
379391ef4cafSDuane Griffin 	if (!ext4_can_truncate(inode))
3794ac27a0ecSDave Kleikamp 		return;
3795ac27a0ecSDave Kleikamp 
379612e9b892SDmitry Monakhov 	ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3797c8d46e41SJiaying Zhang 
37985534fb5bSTheodore Ts'o 	if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
379919f5fb7aSTheodore Ts'o 		ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
38007d8f9f7dSTheodore Ts'o 
3801aef1c851STao Ma 	if (ext4_has_inline_data(inode)) {
3802aef1c851STao Ma 		int has_inline = 1;
3803aef1c851STao Ma 
3804aef1c851STao Ma 		ext4_inline_data_truncate(inode, &has_inline);
3805aef1c851STao Ma 		if (has_inline)
3806aef1c851STao Ma 			return;
3807aef1c851STao Ma 	}
3808aef1c851STao Ma 
3809*819c4920STheodore Ts'o 	/*
3810*819c4920STheodore Ts'o 	 * finish any pending end_io work so we won't run the risk of
3811*819c4920STheodore Ts'o 	 * converting any truncated blocks to initialized later
3812*819c4920STheodore Ts'o 	 */
3813*819c4920STheodore Ts'o 	ext4_flush_unwritten_io(inode);
3814*819c4920STheodore Ts'o 
3815ff9893dcSAmir Goldstein 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3816*819c4920STheodore Ts'o 		credits = ext4_writepage_trans_blocks(inode);
3817ff9893dcSAmir Goldstein 	else
3818*819c4920STheodore Ts'o 		credits = ext4_blocks_for_truncate(inode);
3819*819c4920STheodore Ts'o 
3820*819c4920STheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3821*819c4920STheodore Ts'o 	if (IS_ERR(handle)) {
3822*819c4920STheodore Ts'o 		ext4_std_error(inode->i_sb, PTR_ERR(handle));
3823*819c4920STheodore Ts'o 		return;
3824*819c4920STheodore Ts'o 	}
3825*819c4920STheodore Ts'o 
3826*819c4920STheodore Ts'o 	if (inode->i_size % PAGE_CACHE_SIZE != 0) {
3827*819c4920STheodore Ts'o 		page_len = PAGE_CACHE_SIZE -
3828*819c4920STheodore Ts'o 			(inode->i_size & (PAGE_CACHE_SIZE - 1));
3829*819c4920STheodore Ts'o 
3830*819c4920STheodore Ts'o 		if (ext4_discard_partial_page_buffers(handle,
3831*819c4920STheodore Ts'o 				mapping, inode->i_size, page_len, 0))
3832*819c4920STheodore Ts'o 			goto out_stop;
3833*819c4920STheodore Ts'o 	}
3834*819c4920STheodore Ts'o 
3835*819c4920STheodore Ts'o 	/*
3836*819c4920STheodore Ts'o 	 * We add the inode to the orphan list, so that if this
3837*819c4920STheodore Ts'o 	 * truncate spans multiple transactions, and we crash, we will
3838*819c4920STheodore Ts'o 	 * resume the truncate when the filesystem recovers.  It also
3839*819c4920STheodore Ts'o 	 * marks the inode dirty, to catch the new size.
3840*819c4920STheodore Ts'o 	 *
3841*819c4920STheodore Ts'o 	 * Implication: the file must always be in a sane, consistent
3842*819c4920STheodore Ts'o 	 * truncatable state while each transaction commits.
3843*819c4920STheodore Ts'o 	 */
3844*819c4920STheodore Ts'o 	if (ext4_orphan_add(handle, inode))
3845*819c4920STheodore Ts'o 		goto out_stop;
3846*819c4920STheodore Ts'o 
3847*819c4920STheodore Ts'o 	down_write(&EXT4_I(inode)->i_data_sem);
3848*819c4920STheodore Ts'o 
3849*819c4920STheodore Ts'o 	ext4_discard_preallocations(inode);
3850*819c4920STheodore Ts'o 
3851*819c4920STheodore Ts'o 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3852*819c4920STheodore Ts'o 		ext4_ext_truncate(handle, inode);
3853*819c4920STheodore Ts'o 	else
3854*819c4920STheodore Ts'o 		ext4_ind_truncate(handle, inode);
3855*819c4920STheodore Ts'o 
3856*819c4920STheodore Ts'o 	up_write(&ei->i_data_sem);
3857*819c4920STheodore Ts'o 
3858*819c4920STheodore Ts'o 	if (IS_SYNC(inode))
3859*819c4920STheodore Ts'o 		ext4_handle_sync(handle);
3860*819c4920STheodore Ts'o 
3861*819c4920STheodore Ts'o out_stop:
3862*819c4920STheodore Ts'o 	/*
3863*819c4920STheodore Ts'o 	 * If this was a simple ftruncate() and the file will remain alive,
3864*819c4920STheodore Ts'o 	 * then we need to clear up the orphan record which we created above.
3865*819c4920STheodore Ts'o 	 * However, if this was a real unlink then we were called by
3866*819c4920STheodore Ts'o 	 * ext4_delete_inode(), and we allow that function to clean up the
3867*819c4920STheodore Ts'o 	 * orphan info for us.
3868*819c4920STheodore Ts'o 	 */
3869*819c4920STheodore Ts'o 	if (inode->i_nlink)
3870*819c4920STheodore Ts'o 		ext4_orphan_del(handle, inode);
3871*819c4920STheodore Ts'o 
3872*819c4920STheodore Ts'o 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3873*819c4920STheodore Ts'o 	ext4_mark_inode_dirty(handle, inode);
3874*819c4920STheodore Ts'o 	ext4_journal_stop(handle);
3875a86c6181SAlex Tomas 
38760562e0baSJiaying Zhang 	trace_ext4_truncate_exit(inode);
3877ac27a0ecSDave Kleikamp }
3878ac27a0ecSDave Kleikamp 
3879ac27a0ecSDave Kleikamp /*
3880617ba13bSMingming Cao  * ext4_get_inode_loc returns with an extra refcount against the inode's
3881ac27a0ecSDave Kleikamp  * underlying buffer_head on success. If 'in_mem' is true, we have all
3882ac27a0ecSDave Kleikamp  * data in memory that is needed to recreate the on-disk version of this
3883ac27a0ecSDave Kleikamp  * inode.
3884ac27a0ecSDave Kleikamp  */
3885617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode,
3886617ba13bSMingming Cao 				struct ext4_iloc *iloc, int in_mem)
3887ac27a0ecSDave Kleikamp {
3888240799cdSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3889ac27a0ecSDave Kleikamp 	struct buffer_head	*bh;
3890240799cdSTheodore Ts'o 	struct super_block	*sb = inode->i_sb;
3891240799cdSTheodore Ts'o 	ext4_fsblk_t		block;
3892240799cdSTheodore Ts'o 	int			inodes_per_block, inode_offset;
3893ac27a0ecSDave Kleikamp 
38943a06d778SAneesh Kumar K.V 	iloc->bh = NULL;
3895240799cdSTheodore Ts'o 	if (!ext4_valid_inum(sb, inode->i_ino))
3896ac27a0ecSDave Kleikamp 		return -EIO;
3897ac27a0ecSDave Kleikamp 
3898240799cdSTheodore Ts'o 	iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3899240799cdSTheodore Ts'o 	gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3900240799cdSTheodore Ts'o 	if (!gdp)
3901240799cdSTheodore Ts'o 		return -EIO;
3902240799cdSTheodore Ts'o 
3903240799cdSTheodore Ts'o 	/*
3904240799cdSTheodore Ts'o 	 * Figure out the offset within the block group inode table
3905240799cdSTheodore Ts'o 	 */
390600d09882STao Ma 	inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
3907240799cdSTheodore Ts'o 	inode_offset = ((inode->i_ino - 1) %
3908240799cdSTheodore Ts'o 			EXT4_INODES_PER_GROUP(sb));
3909240799cdSTheodore Ts'o 	block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3910240799cdSTheodore Ts'o 	iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3911240799cdSTheodore Ts'o 
3912240799cdSTheodore Ts'o 	bh = sb_getblk(sb, block);
3913aebf0243SWang Shilong 	if (unlikely(!bh))
3914860d21e2STheodore Ts'o 		return -ENOMEM;
3915ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(bh)) {
3916ac27a0ecSDave Kleikamp 		lock_buffer(bh);
39179c83a923SHidehiro Kawai 
39189c83a923SHidehiro Kawai 		/*
39199c83a923SHidehiro Kawai 		 * If the buffer has the write error flag, we have failed
39209c83a923SHidehiro Kawai 		 * to write out another inode in the same block.  In this
39219c83a923SHidehiro Kawai 		 * case, we don't have to read the block because we may
39229c83a923SHidehiro Kawai 		 * read the old inode data successfully.
39239c83a923SHidehiro Kawai 		 */
39249c83a923SHidehiro Kawai 		if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
39259c83a923SHidehiro Kawai 			set_buffer_uptodate(bh);
39269c83a923SHidehiro Kawai 
3927ac27a0ecSDave Kleikamp 		if (buffer_uptodate(bh)) {
3928ac27a0ecSDave Kleikamp 			/* someone brought it uptodate while we waited */
3929ac27a0ecSDave Kleikamp 			unlock_buffer(bh);
3930ac27a0ecSDave Kleikamp 			goto has_buffer;
3931ac27a0ecSDave Kleikamp 		}
3932ac27a0ecSDave Kleikamp 
3933ac27a0ecSDave Kleikamp 		/*
3934ac27a0ecSDave Kleikamp 		 * If we have all information of the inode in memory and this
3935ac27a0ecSDave Kleikamp 		 * is the only valid inode in the block, we need not read the
3936ac27a0ecSDave Kleikamp 		 * block.
3937ac27a0ecSDave Kleikamp 		 */
3938ac27a0ecSDave Kleikamp 		if (in_mem) {
3939ac27a0ecSDave Kleikamp 			struct buffer_head *bitmap_bh;
3940240799cdSTheodore Ts'o 			int i, start;
3941ac27a0ecSDave Kleikamp 
3942240799cdSTheodore Ts'o 			start = inode_offset & ~(inodes_per_block - 1);
3943ac27a0ecSDave Kleikamp 
3944ac27a0ecSDave Kleikamp 			/* Is the inode bitmap in cache? */
3945240799cdSTheodore Ts'o 			bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
3946aebf0243SWang Shilong 			if (unlikely(!bitmap_bh))
3947ac27a0ecSDave Kleikamp 				goto make_io;
3948ac27a0ecSDave Kleikamp 
3949ac27a0ecSDave Kleikamp 			/*
3950ac27a0ecSDave Kleikamp 			 * If the inode bitmap isn't in cache then the
3951ac27a0ecSDave Kleikamp 			 * optimisation may end up performing two reads instead
3952ac27a0ecSDave Kleikamp 			 * of one, so skip it.
3953ac27a0ecSDave Kleikamp 			 */
3954ac27a0ecSDave Kleikamp 			if (!buffer_uptodate(bitmap_bh)) {
3955ac27a0ecSDave Kleikamp 				brelse(bitmap_bh);
3956ac27a0ecSDave Kleikamp 				goto make_io;
3957ac27a0ecSDave Kleikamp 			}
3958240799cdSTheodore Ts'o 			for (i = start; i < start + inodes_per_block; i++) {
3959ac27a0ecSDave Kleikamp 				if (i == inode_offset)
3960ac27a0ecSDave Kleikamp 					continue;
3961617ba13bSMingming Cao 				if (ext4_test_bit(i, bitmap_bh->b_data))
3962ac27a0ecSDave Kleikamp 					break;
3963ac27a0ecSDave Kleikamp 			}
3964ac27a0ecSDave Kleikamp 			brelse(bitmap_bh);
3965240799cdSTheodore Ts'o 			if (i == start + inodes_per_block) {
3966ac27a0ecSDave Kleikamp 				/* all other inodes are free, so skip I/O */
3967ac27a0ecSDave Kleikamp 				memset(bh->b_data, 0, bh->b_size);
3968ac27a0ecSDave Kleikamp 				set_buffer_uptodate(bh);
3969ac27a0ecSDave Kleikamp 				unlock_buffer(bh);
3970ac27a0ecSDave Kleikamp 				goto has_buffer;
3971ac27a0ecSDave Kleikamp 			}
3972ac27a0ecSDave Kleikamp 		}
3973ac27a0ecSDave Kleikamp 
3974ac27a0ecSDave Kleikamp make_io:
3975ac27a0ecSDave Kleikamp 		/*
3976240799cdSTheodore Ts'o 		 * If we need to do any I/O, try to pre-readahead extra
3977240799cdSTheodore Ts'o 		 * blocks from the inode table.
3978240799cdSTheodore Ts'o 		 */
3979240799cdSTheodore Ts'o 		if (EXT4_SB(sb)->s_inode_readahead_blks) {
3980240799cdSTheodore Ts'o 			ext4_fsblk_t b, end, table;
3981240799cdSTheodore Ts'o 			unsigned num;
3982240799cdSTheodore Ts'o 
3983240799cdSTheodore Ts'o 			table = ext4_inode_table(sb, gdp);
3984b713a5ecSTheodore Ts'o 			/* s_inode_readahead_blks is always a power of 2 */
3985240799cdSTheodore Ts'o 			b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
3986240799cdSTheodore Ts'o 			if (table > b)
3987240799cdSTheodore Ts'o 				b = table;
3988240799cdSTheodore Ts'o 			end = b + EXT4_SB(sb)->s_inode_readahead_blks;
3989240799cdSTheodore Ts'o 			num = EXT4_INODES_PER_GROUP(sb);
3990feb0ab32SDarrick J. Wong 			if (ext4_has_group_desc_csum(sb))
3991560671a0SAneesh Kumar K.V 				num -= ext4_itable_unused_count(sb, gdp);
3992240799cdSTheodore Ts'o 			table += num / inodes_per_block;
3993240799cdSTheodore Ts'o 			if (end > table)
3994240799cdSTheodore Ts'o 				end = table;
3995240799cdSTheodore Ts'o 			while (b <= end)
3996240799cdSTheodore Ts'o 				sb_breadahead(sb, b++);
3997240799cdSTheodore Ts'o 		}
3998240799cdSTheodore Ts'o 
3999240799cdSTheodore Ts'o 		/*
4000ac27a0ecSDave Kleikamp 		 * There are other valid inodes in the buffer, this inode
4001ac27a0ecSDave Kleikamp 		 * has in-inode xattrs, or we don't have this inode in memory.
4002ac27a0ecSDave Kleikamp 		 * Read the block from disk.
4003ac27a0ecSDave Kleikamp 		 */
40040562e0baSJiaying Zhang 		trace_ext4_load_inode(inode);
4005ac27a0ecSDave Kleikamp 		get_bh(bh);
4006ac27a0ecSDave Kleikamp 		bh->b_end_io = end_buffer_read_sync;
400765299a3bSChristoph Hellwig 		submit_bh(READ | REQ_META | REQ_PRIO, bh);
4008ac27a0ecSDave Kleikamp 		wait_on_buffer(bh);
4009ac27a0ecSDave Kleikamp 		if (!buffer_uptodate(bh)) {
4010c398eda0STheodore Ts'o 			EXT4_ERROR_INODE_BLOCK(inode, block,
4011c398eda0STheodore Ts'o 					       "unable to read itable block");
4012ac27a0ecSDave Kleikamp 			brelse(bh);
4013ac27a0ecSDave Kleikamp 			return -EIO;
4014ac27a0ecSDave Kleikamp 		}
4015ac27a0ecSDave Kleikamp 	}
4016ac27a0ecSDave Kleikamp has_buffer:
4017ac27a0ecSDave Kleikamp 	iloc->bh = bh;
4018ac27a0ecSDave Kleikamp 	return 0;
4019ac27a0ecSDave Kleikamp }
4020ac27a0ecSDave Kleikamp 
4021617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
4022ac27a0ecSDave Kleikamp {
4023ac27a0ecSDave Kleikamp 	/* We have all inode data except xattrs in memory here. */
4024617ba13bSMingming Cao 	return __ext4_get_inode_loc(inode, iloc,
402519f5fb7aSTheodore Ts'o 		!ext4_test_inode_state(inode, EXT4_STATE_XATTR));
4026ac27a0ecSDave Kleikamp }
4027ac27a0ecSDave Kleikamp 
4028617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode)
4029ac27a0ecSDave Kleikamp {
4030617ba13bSMingming Cao 	unsigned int flags = EXT4_I(inode)->i_flags;
4031ac27a0ecSDave Kleikamp 
4032ac27a0ecSDave Kleikamp 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
4033617ba13bSMingming Cao 	if (flags & EXT4_SYNC_FL)
4034ac27a0ecSDave Kleikamp 		inode->i_flags |= S_SYNC;
4035617ba13bSMingming Cao 	if (flags & EXT4_APPEND_FL)
4036ac27a0ecSDave Kleikamp 		inode->i_flags |= S_APPEND;
4037617ba13bSMingming Cao 	if (flags & EXT4_IMMUTABLE_FL)
4038ac27a0ecSDave Kleikamp 		inode->i_flags |= S_IMMUTABLE;
4039617ba13bSMingming Cao 	if (flags & EXT4_NOATIME_FL)
4040ac27a0ecSDave Kleikamp 		inode->i_flags |= S_NOATIME;
4041617ba13bSMingming Cao 	if (flags & EXT4_DIRSYNC_FL)
4042ac27a0ecSDave Kleikamp 		inode->i_flags |= S_DIRSYNC;
4043ac27a0ecSDave Kleikamp }
4044ac27a0ecSDave Kleikamp 
4045ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4046ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei)
4047ff9ddf7eSJan Kara {
404884a8dce2SDmitry Monakhov 	unsigned int vfs_fl;
404984a8dce2SDmitry Monakhov 	unsigned long old_fl, new_fl;
4050ff9ddf7eSJan Kara 
405184a8dce2SDmitry Monakhov 	do {
405284a8dce2SDmitry Monakhov 		vfs_fl = ei->vfs_inode.i_flags;
405384a8dce2SDmitry Monakhov 		old_fl = ei->i_flags;
405484a8dce2SDmitry Monakhov 		new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
405584a8dce2SDmitry Monakhov 				EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
405684a8dce2SDmitry Monakhov 				EXT4_DIRSYNC_FL);
405784a8dce2SDmitry Monakhov 		if (vfs_fl & S_SYNC)
405884a8dce2SDmitry Monakhov 			new_fl |= EXT4_SYNC_FL;
405984a8dce2SDmitry Monakhov 		if (vfs_fl & S_APPEND)
406084a8dce2SDmitry Monakhov 			new_fl |= EXT4_APPEND_FL;
406184a8dce2SDmitry Monakhov 		if (vfs_fl & S_IMMUTABLE)
406284a8dce2SDmitry Monakhov 			new_fl |= EXT4_IMMUTABLE_FL;
406384a8dce2SDmitry Monakhov 		if (vfs_fl & S_NOATIME)
406484a8dce2SDmitry Monakhov 			new_fl |= EXT4_NOATIME_FL;
406584a8dce2SDmitry Monakhov 		if (vfs_fl & S_DIRSYNC)
406684a8dce2SDmitry Monakhov 			new_fl |= EXT4_DIRSYNC_FL;
406784a8dce2SDmitry Monakhov 	} while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
4068ff9ddf7eSJan Kara }
4069de9a55b8STheodore Ts'o 
40700fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
40710fc1b451SAneesh Kumar K.V 				  struct ext4_inode_info *ei)
40720fc1b451SAneesh Kumar K.V {
40730fc1b451SAneesh Kumar K.V 	blkcnt_t i_blocks ;
40748180a562SAneesh Kumar K.V 	struct inode *inode = &(ei->vfs_inode);
40758180a562SAneesh Kumar K.V 	struct super_block *sb = inode->i_sb;
40760fc1b451SAneesh Kumar K.V 
40770fc1b451SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
40780fc1b451SAneesh Kumar K.V 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
40790fc1b451SAneesh Kumar K.V 		/* we are using combined 48 bit field */
40800fc1b451SAneesh Kumar K.V 		i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
40810fc1b451SAneesh Kumar K.V 					le32_to_cpu(raw_inode->i_blocks_lo);
408207a03824STheodore Ts'o 		if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
40838180a562SAneesh Kumar K.V 			/* i_blocks represent file system block size */
40848180a562SAneesh Kumar K.V 			return i_blocks  << (inode->i_blkbits - 9);
40858180a562SAneesh Kumar K.V 		} else {
40860fc1b451SAneesh Kumar K.V 			return i_blocks;
40878180a562SAneesh Kumar K.V 		}
40880fc1b451SAneesh Kumar K.V 	} else {
40890fc1b451SAneesh Kumar K.V 		return le32_to_cpu(raw_inode->i_blocks_lo);
40900fc1b451SAneesh Kumar K.V 	}
40910fc1b451SAneesh Kumar K.V }
4092ff9ddf7eSJan Kara 
4093152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode,
4094152a7b0aSTao Ma 					 struct ext4_inode *raw_inode,
4095152a7b0aSTao Ma 					 struct ext4_inode_info *ei)
4096152a7b0aSTao Ma {
4097152a7b0aSTao Ma 	__le32 *magic = (void *)raw_inode +
4098152a7b0aSTao Ma 			EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
409967cf5b09STao Ma 	if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
4100152a7b0aSTao Ma 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
410167cf5b09STao Ma 		ext4_find_inline_data_nolock(inode);
4102f19d5870STao Ma 	} else
4103f19d5870STao Ma 		EXT4_I(inode)->i_inline_off = 0;
4104152a7b0aSTao Ma }
4105152a7b0aSTao Ma 
41061d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
4107ac27a0ecSDave Kleikamp {
4108617ba13bSMingming Cao 	struct ext4_iloc iloc;
4109617ba13bSMingming Cao 	struct ext4_inode *raw_inode;
41101d1fe1eeSDavid Howells 	struct ext4_inode_info *ei;
41111d1fe1eeSDavid Howells 	struct inode *inode;
4112b436b9beSJan Kara 	journal_t *journal = EXT4_SB(sb)->s_journal;
41131d1fe1eeSDavid Howells 	long ret;
4114ac27a0ecSDave Kleikamp 	int block;
411508cefc7aSEric W. Biederman 	uid_t i_uid;
411608cefc7aSEric W. Biederman 	gid_t i_gid;
4117ac27a0ecSDave Kleikamp 
41181d1fe1eeSDavid Howells 	inode = iget_locked(sb, ino);
41191d1fe1eeSDavid Howells 	if (!inode)
41201d1fe1eeSDavid Howells 		return ERR_PTR(-ENOMEM);
41211d1fe1eeSDavid Howells 	if (!(inode->i_state & I_NEW))
41221d1fe1eeSDavid Howells 		return inode;
41231d1fe1eeSDavid Howells 
41241d1fe1eeSDavid Howells 	ei = EXT4_I(inode);
41257dc57615SPeter Huewe 	iloc.bh = NULL;
4126ac27a0ecSDave Kleikamp 
41271d1fe1eeSDavid Howells 	ret = __ext4_get_inode_loc(inode, &iloc, 0);
41281d1fe1eeSDavid Howells 	if (ret < 0)
4129ac27a0ecSDave Kleikamp 		goto bad_inode;
4130617ba13bSMingming Cao 	raw_inode = ext4_raw_inode(&iloc);
4131814525f4SDarrick J. Wong 
4132814525f4SDarrick J. Wong 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4133814525f4SDarrick J. Wong 		ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4134814525f4SDarrick J. Wong 		if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4135814525f4SDarrick J. Wong 		    EXT4_INODE_SIZE(inode->i_sb)) {
4136814525f4SDarrick J. Wong 			EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4137814525f4SDarrick J. Wong 				EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4138814525f4SDarrick J. Wong 				EXT4_INODE_SIZE(inode->i_sb));
4139814525f4SDarrick J. Wong 			ret = -EIO;
4140814525f4SDarrick J. Wong 			goto bad_inode;
4141814525f4SDarrick J. Wong 		}
4142814525f4SDarrick J. Wong 	} else
4143814525f4SDarrick J. Wong 		ei->i_extra_isize = 0;
4144814525f4SDarrick J. Wong 
4145814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
4146814525f4SDarrick J. Wong 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4147814525f4SDarrick J. Wong 			EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
4148814525f4SDarrick J. Wong 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4149814525f4SDarrick J. Wong 		__u32 csum;
4150814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
4151814525f4SDarrick J. Wong 		__le32 gen = raw_inode->i_generation;
4152814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4153814525f4SDarrick J. Wong 				   sizeof(inum));
4154814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4155814525f4SDarrick J. Wong 					      sizeof(gen));
4156814525f4SDarrick J. Wong 	}
4157814525f4SDarrick J. Wong 
4158814525f4SDarrick J. Wong 	if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4159814525f4SDarrick J. Wong 		EXT4_ERROR_INODE(inode, "checksum invalid");
4160814525f4SDarrick J. Wong 		ret = -EIO;
4161814525f4SDarrick J. Wong 		goto bad_inode;
4162814525f4SDarrick J. Wong 	}
4163814525f4SDarrick J. Wong 
4164ac27a0ecSDave Kleikamp 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
416508cefc7aSEric W. Biederman 	i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
416608cefc7aSEric W. Biederman 	i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
4167ac27a0ecSDave Kleikamp 	if (!(test_opt(inode->i_sb, NO_UID32))) {
416808cefc7aSEric W. Biederman 		i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
416908cefc7aSEric W. Biederman 		i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4170ac27a0ecSDave Kleikamp 	}
417108cefc7aSEric W. Biederman 	i_uid_write(inode, i_uid);
417208cefc7aSEric W. Biederman 	i_gid_write(inode, i_gid);
4173bfe86848SMiklos Szeredi 	set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
4174ac27a0ecSDave Kleikamp 
4175353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei);	/* Only relevant on 32-bit archs */
417667cf5b09STao Ma 	ei->i_inline_off = 0;
4177ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
4178ac27a0ecSDave Kleikamp 	ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4179ac27a0ecSDave Kleikamp 	/* We now have enough fields to check if the inode was active or not.
4180ac27a0ecSDave Kleikamp 	 * This is needed because nfsd might try to access dead inodes
4181ac27a0ecSDave Kleikamp 	 * the test is that same one that e2fsck uses
4182ac27a0ecSDave Kleikamp 	 * NeilBrown 1999oct15
4183ac27a0ecSDave Kleikamp 	 */
4184ac27a0ecSDave Kleikamp 	if (inode->i_nlink == 0) {
4185ac27a0ecSDave Kleikamp 		if (inode->i_mode == 0 ||
4186617ba13bSMingming Cao 		    !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
4187ac27a0ecSDave Kleikamp 			/* this inode is deleted */
41881d1fe1eeSDavid Howells 			ret = -ESTALE;
4189ac27a0ecSDave Kleikamp 			goto bad_inode;
4190ac27a0ecSDave Kleikamp 		}
4191ac27a0ecSDave Kleikamp 		/* The only unlinked inodes we let through here have
4192ac27a0ecSDave Kleikamp 		 * valid i_mode and are being read by the orphan
4193ac27a0ecSDave Kleikamp 		 * recovery code: that's fine, we're about to complete
4194ac27a0ecSDave Kleikamp 		 * the process of deleting those. */
4195ac27a0ecSDave Kleikamp 	}
4196ac27a0ecSDave Kleikamp 	ei->i_flags = le32_to_cpu(raw_inode->i_flags);
41970fc1b451SAneesh Kumar K.V 	inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
41987973c0c1SAneesh Kumar K.V 	ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
4199a9e81742STheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
4200a1ddeb7eSBadari Pulavarty 		ei->i_file_acl |=
4201a1ddeb7eSBadari Pulavarty 			((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
4202a48380f7SAneesh Kumar K.V 	inode->i_size = ext4_isize(raw_inode);
4203ac27a0ecSDave Kleikamp 	ei->i_disksize = inode->i_size;
4204a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
4205a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
4206a9e7f447SDmitry Monakhov #endif
4207ac27a0ecSDave Kleikamp 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4208ac27a0ecSDave Kleikamp 	ei->i_block_group = iloc.block_group;
4209a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
4210ac27a0ecSDave Kleikamp 	/*
4211ac27a0ecSDave Kleikamp 	 * NOTE! The in-memory inode i_data array is in little-endian order
4212ac27a0ecSDave Kleikamp 	 * even on big-endian machines: we do NOT byteswap the block numbers!
4213ac27a0ecSDave Kleikamp 	 */
4214617ba13bSMingming Cao 	for (block = 0; block < EXT4_N_BLOCKS; block++)
4215ac27a0ecSDave Kleikamp 		ei->i_data[block] = raw_inode->i_block[block];
4216ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
4217ac27a0ecSDave Kleikamp 
4218b436b9beSJan Kara 	/*
4219b436b9beSJan Kara 	 * Set transaction id's of transactions that have to be committed
4220b436b9beSJan Kara 	 * to finish f[data]sync. We set them to currently running transaction
4221b436b9beSJan Kara 	 * as we cannot be sure that the inode or some of its metadata isn't
4222b436b9beSJan Kara 	 * part of the transaction - the inode could have been reclaimed and
4223b436b9beSJan Kara 	 * now it is reread from disk.
4224b436b9beSJan Kara 	 */
4225b436b9beSJan Kara 	if (journal) {
4226b436b9beSJan Kara 		transaction_t *transaction;
4227b436b9beSJan Kara 		tid_t tid;
4228b436b9beSJan Kara 
4229a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
4230b436b9beSJan Kara 		if (journal->j_running_transaction)
4231b436b9beSJan Kara 			transaction = journal->j_running_transaction;
4232b436b9beSJan Kara 		else
4233b436b9beSJan Kara 			transaction = journal->j_committing_transaction;
4234b436b9beSJan Kara 		if (transaction)
4235b436b9beSJan Kara 			tid = transaction->t_tid;
4236b436b9beSJan Kara 		else
4237b436b9beSJan Kara 			tid = journal->j_commit_sequence;
4238a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
4239b436b9beSJan Kara 		ei->i_sync_tid = tid;
4240b436b9beSJan Kara 		ei->i_datasync_tid = tid;
4241b436b9beSJan Kara 	}
4242b436b9beSJan Kara 
42430040d987SEric Sandeen 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4244ac27a0ecSDave Kleikamp 		if (ei->i_extra_isize == 0) {
4245ac27a0ecSDave Kleikamp 			/* The extra space is currently unused. Use it. */
4246617ba13bSMingming Cao 			ei->i_extra_isize = sizeof(struct ext4_inode) -
4247617ba13bSMingming Cao 					    EXT4_GOOD_OLD_INODE_SIZE;
4248ac27a0ecSDave Kleikamp 		} else {
4249152a7b0aSTao Ma 			ext4_iget_extra_inode(inode, raw_inode, ei);
4250ac27a0ecSDave Kleikamp 		}
4251814525f4SDarrick J. Wong 	}
4252ac27a0ecSDave Kleikamp 
4253ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4254ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4255ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4256ef7f3835SKalpak Shah 	EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4257ef7f3835SKalpak Shah 
425825ec56b5SJean Noel Cordenner 	inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
425925ec56b5SJean Noel Cordenner 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
426025ec56b5SJean Noel Cordenner 		if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
426125ec56b5SJean Noel Cordenner 			inode->i_version |=
426225ec56b5SJean Noel Cordenner 			(__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
426325ec56b5SJean Noel Cordenner 	}
426425ec56b5SJean Noel Cordenner 
4265c4b5a614STheodore Ts'o 	ret = 0;
4266485c26ecSTheodore Ts'o 	if (ei->i_file_acl &&
42671032988cSTheodore Ts'o 	    !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
426824676da4STheodore Ts'o 		EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
426924676da4STheodore Ts'o 				 ei->i_file_acl);
4270485c26ecSTheodore Ts'o 		ret = -EIO;
4271485c26ecSTheodore Ts'o 		goto bad_inode;
4272f19d5870STao Ma 	} else if (!ext4_has_inline_data(inode)) {
4273f19d5870STao Ma 		if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4274f19d5870STao Ma 			if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4275c4b5a614STheodore Ts'o 			    (S_ISLNK(inode->i_mode) &&
4276f19d5870STao Ma 			     !ext4_inode_is_fast_symlink(inode))))
42777a262f7cSAneesh Kumar K.V 				/* Validate extent which is part of inode */
42787a262f7cSAneesh Kumar K.V 				ret = ext4_ext_check_inode(inode);
4279fe2c8191SThiemo Nagel 		} else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4280fe2c8191SThiemo Nagel 			   (S_ISLNK(inode->i_mode) &&
4281fe2c8191SThiemo Nagel 			    !ext4_inode_is_fast_symlink(inode))) {
4282fe2c8191SThiemo Nagel 			/* Validate block references which are part of inode */
42831f7d1e77STheodore Ts'o 			ret = ext4_ind_check_inode(inode);
4284fe2c8191SThiemo Nagel 		}
4285f19d5870STao Ma 	}
4286567f3e9aSTheodore Ts'o 	if (ret)
42877a262f7cSAneesh Kumar K.V 		goto bad_inode;
42887a262f7cSAneesh Kumar K.V 
4289ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode)) {
4290617ba13bSMingming Cao 		inode->i_op = &ext4_file_inode_operations;
4291617ba13bSMingming Cao 		inode->i_fop = &ext4_file_operations;
4292617ba13bSMingming Cao 		ext4_set_aops(inode);
4293ac27a0ecSDave Kleikamp 	} else if (S_ISDIR(inode->i_mode)) {
4294617ba13bSMingming Cao 		inode->i_op = &ext4_dir_inode_operations;
4295617ba13bSMingming Cao 		inode->i_fop = &ext4_dir_operations;
4296ac27a0ecSDave Kleikamp 	} else if (S_ISLNK(inode->i_mode)) {
4297e83c1397SDuane Griffin 		if (ext4_inode_is_fast_symlink(inode)) {
4298617ba13bSMingming Cao 			inode->i_op = &ext4_fast_symlink_inode_operations;
4299e83c1397SDuane Griffin 			nd_terminate_link(ei->i_data, inode->i_size,
4300e83c1397SDuane Griffin 				sizeof(ei->i_data) - 1);
4301e83c1397SDuane Griffin 		} else {
4302617ba13bSMingming Cao 			inode->i_op = &ext4_symlink_inode_operations;
4303617ba13bSMingming Cao 			ext4_set_aops(inode);
4304ac27a0ecSDave Kleikamp 		}
4305563bdd61STheodore Ts'o 	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4306563bdd61STheodore Ts'o 	      S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
4307617ba13bSMingming Cao 		inode->i_op = &ext4_special_inode_operations;
4308ac27a0ecSDave Kleikamp 		if (raw_inode->i_block[0])
4309ac27a0ecSDave Kleikamp 			init_special_inode(inode, inode->i_mode,
4310ac27a0ecSDave Kleikamp 			   old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4311ac27a0ecSDave Kleikamp 		else
4312ac27a0ecSDave Kleikamp 			init_special_inode(inode, inode->i_mode,
4313ac27a0ecSDave Kleikamp 			   new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4314563bdd61STheodore Ts'o 	} else {
4315563bdd61STheodore Ts'o 		ret = -EIO;
431624676da4STheodore Ts'o 		EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
4317563bdd61STheodore Ts'o 		goto bad_inode;
4318ac27a0ecSDave Kleikamp 	}
4319ac27a0ecSDave Kleikamp 	brelse(iloc.bh);
4320617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
43211d1fe1eeSDavid Howells 	unlock_new_inode(inode);
43221d1fe1eeSDavid Howells 	return inode;
4323ac27a0ecSDave Kleikamp 
4324ac27a0ecSDave Kleikamp bad_inode:
4325567f3e9aSTheodore Ts'o 	brelse(iloc.bh);
43261d1fe1eeSDavid Howells 	iget_failed(inode);
43271d1fe1eeSDavid Howells 	return ERR_PTR(ret);
4328ac27a0ecSDave Kleikamp }
4329ac27a0ecSDave Kleikamp 
43300fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle,
43310fc1b451SAneesh Kumar K.V 				struct ext4_inode *raw_inode,
43320fc1b451SAneesh Kumar K.V 				struct ext4_inode_info *ei)
43330fc1b451SAneesh Kumar K.V {
43340fc1b451SAneesh Kumar K.V 	struct inode *inode = &(ei->vfs_inode);
43350fc1b451SAneesh Kumar K.V 	u64 i_blocks = inode->i_blocks;
43360fc1b451SAneesh Kumar K.V 	struct super_block *sb = inode->i_sb;
43370fc1b451SAneesh Kumar K.V 
43380fc1b451SAneesh Kumar K.V 	if (i_blocks <= ~0U) {
43390fc1b451SAneesh Kumar K.V 		/*
43404907cb7bSAnatol Pomozov 		 * i_blocks can be represented in a 32 bit variable
43410fc1b451SAneesh Kumar K.V 		 * as multiple of 512 bytes
43420fc1b451SAneesh Kumar K.V 		 */
43438180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
43440fc1b451SAneesh Kumar K.V 		raw_inode->i_blocks_high = 0;
434584a8dce2SDmitry Monakhov 		ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4346f287a1a5STheodore Ts'o 		return 0;
4347f287a1a5STheodore Ts'o 	}
4348f287a1a5STheodore Ts'o 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4349f287a1a5STheodore Ts'o 		return -EFBIG;
4350f287a1a5STheodore Ts'o 
4351f287a1a5STheodore Ts'o 	if (i_blocks <= 0xffffffffffffULL) {
43520fc1b451SAneesh Kumar K.V 		/*
43530fc1b451SAneesh Kumar K.V 		 * i_blocks can be represented in a 48 bit variable
43540fc1b451SAneesh Kumar K.V 		 * as multiple of 512 bytes
43550fc1b451SAneesh Kumar K.V 		 */
43568180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
43570fc1b451SAneesh Kumar K.V 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
435884a8dce2SDmitry Monakhov 		ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
43590fc1b451SAneesh Kumar K.V 	} else {
436084a8dce2SDmitry Monakhov 		ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
43618180a562SAneesh Kumar K.V 		/* i_block is stored in file system block size */
43628180a562SAneesh Kumar K.V 		i_blocks = i_blocks >> (inode->i_blkbits - 9);
43638180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
43648180a562SAneesh Kumar K.V 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
43650fc1b451SAneesh Kumar K.V 	}
4366f287a1a5STheodore Ts'o 	return 0;
43670fc1b451SAneesh Kumar K.V }
43680fc1b451SAneesh Kumar K.V 
4369ac27a0ecSDave Kleikamp /*
4370ac27a0ecSDave Kleikamp  * Post the struct inode info into an on-disk inode location in the
4371ac27a0ecSDave Kleikamp  * buffer-cache.  This gobbles the caller's reference to the
4372ac27a0ecSDave Kleikamp  * buffer_head in the inode location struct.
4373ac27a0ecSDave Kleikamp  *
4374ac27a0ecSDave Kleikamp  * The caller must have write access to iloc->bh.
4375ac27a0ecSDave Kleikamp  */
4376617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle,
4377ac27a0ecSDave Kleikamp 				struct inode *inode,
4378830156c7SFrank Mayhar 				struct ext4_iloc *iloc)
4379ac27a0ecSDave Kleikamp {
4380617ba13bSMingming Cao 	struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4381617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
4382ac27a0ecSDave Kleikamp 	struct buffer_head *bh = iloc->bh;
4383ac27a0ecSDave Kleikamp 	int err = 0, rc, block;
4384b71fc079SJan Kara 	int need_datasync = 0;
438508cefc7aSEric W. Biederman 	uid_t i_uid;
438608cefc7aSEric W. Biederman 	gid_t i_gid;
4387ac27a0ecSDave Kleikamp 
4388ac27a0ecSDave Kleikamp 	/* For fields not not tracking in the in-memory inode,
4389ac27a0ecSDave Kleikamp 	 * initialise them to zero for new inodes. */
439019f5fb7aSTheodore Ts'o 	if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
4391617ba13bSMingming Cao 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
4392ac27a0ecSDave Kleikamp 
4393ff9ddf7eSJan Kara 	ext4_get_inode_flags(ei);
4394ac27a0ecSDave Kleikamp 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
439508cefc7aSEric W. Biederman 	i_uid = i_uid_read(inode);
439608cefc7aSEric W. Biederman 	i_gid = i_gid_read(inode);
4397ac27a0ecSDave Kleikamp 	if (!(test_opt(inode->i_sb, NO_UID32))) {
439808cefc7aSEric W. Biederman 		raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
439908cefc7aSEric W. Biederman 		raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
4400ac27a0ecSDave Kleikamp /*
4401ac27a0ecSDave Kleikamp  * Fix up interoperability with old kernels. Otherwise, old inodes get
4402ac27a0ecSDave Kleikamp  * re-used with the upper 16 bits of the uid/gid intact
4403ac27a0ecSDave Kleikamp  */
4404ac27a0ecSDave Kleikamp 		if (!ei->i_dtime) {
4405ac27a0ecSDave Kleikamp 			raw_inode->i_uid_high =
440608cefc7aSEric W. Biederman 				cpu_to_le16(high_16_bits(i_uid));
4407ac27a0ecSDave Kleikamp 			raw_inode->i_gid_high =
440808cefc7aSEric W. Biederman 				cpu_to_le16(high_16_bits(i_gid));
4409ac27a0ecSDave Kleikamp 		} else {
4410ac27a0ecSDave Kleikamp 			raw_inode->i_uid_high = 0;
4411ac27a0ecSDave Kleikamp 			raw_inode->i_gid_high = 0;
4412ac27a0ecSDave Kleikamp 		}
4413ac27a0ecSDave Kleikamp 	} else {
441408cefc7aSEric W. Biederman 		raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
441508cefc7aSEric W. Biederman 		raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
4416ac27a0ecSDave Kleikamp 		raw_inode->i_uid_high = 0;
4417ac27a0ecSDave Kleikamp 		raw_inode->i_gid_high = 0;
4418ac27a0ecSDave Kleikamp 	}
4419ac27a0ecSDave Kleikamp 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
4420ef7f3835SKalpak Shah 
4421ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4422ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4423ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4424ef7f3835SKalpak Shah 	EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4425ef7f3835SKalpak Shah 
44260fc1b451SAneesh Kumar K.V 	if (ext4_inode_blocks_set(handle, raw_inode, ei))
44270fc1b451SAneesh Kumar K.V 		goto out_brelse;
4428ac27a0ecSDave Kleikamp 	raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
4429353eb83cSTheodore Ts'o 	raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
44309b8f1f01SMingming Cao 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
44319b8f1f01SMingming Cao 	    cpu_to_le32(EXT4_OS_HURD))
4432a1ddeb7eSBadari Pulavarty 		raw_inode->i_file_acl_high =
4433a1ddeb7eSBadari Pulavarty 			cpu_to_le16(ei->i_file_acl >> 32);
44347973c0c1SAneesh Kumar K.V 	raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
4435b71fc079SJan Kara 	if (ei->i_disksize != ext4_isize(raw_inode)) {
4436a48380f7SAneesh Kumar K.V 		ext4_isize_set(raw_inode, ei->i_disksize);
4437b71fc079SJan Kara 		need_datasync = 1;
4438b71fc079SJan Kara 	}
4439ac27a0ecSDave Kleikamp 	if (ei->i_disksize > 0x7fffffffULL) {
4440ac27a0ecSDave Kleikamp 		struct super_block *sb = inode->i_sb;
4441617ba13bSMingming Cao 		if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4442617ba13bSMingming Cao 				EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4443617ba13bSMingming Cao 				EXT4_SB(sb)->s_es->s_rev_level ==
4444617ba13bSMingming Cao 				cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4445ac27a0ecSDave Kleikamp 			/* If this is the first large file
4446ac27a0ecSDave Kleikamp 			 * created, add a flag to the superblock.
4447ac27a0ecSDave Kleikamp 			 */
4448617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle,
4449617ba13bSMingming Cao 					EXT4_SB(sb)->s_sbh);
4450ac27a0ecSDave Kleikamp 			if (err)
4451ac27a0ecSDave Kleikamp 				goto out_brelse;
4452617ba13bSMingming Cao 			ext4_update_dynamic_rev(sb);
4453617ba13bSMingming Cao 			EXT4_SET_RO_COMPAT_FEATURE(sb,
4454617ba13bSMingming Cao 					EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
44550390131bSFrank Mayhar 			ext4_handle_sync(handle);
4456b50924c2SArtem Bityutskiy 			err = ext4_handle_dirty_super(handle, sb);
4457ac27a0ecSDave Kleikamp 		}
4458ac27a0ecSDave Kleikamp 	}
4459ac27a0ecSDave Kleikamp 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4460ac27a0ecSDave Kleikamp 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4461ac27a0ecSDave Kleikamp 		if (old_valid_dev(inode->i_rdev)) {
4462ac27a0ecSDave Kleikamp 			raw_inode->i_block[0] =
4463ac27a0ecSDave Kleikamp 				cpu_to_le32(old_encode_dev(inode->i_rdev));
4464ac27a0ecSDave Kleikamp 			raw_inode->i_block[1] = 0;
4465ac27a0ecSDave Kleikamp 		} else {
4466ac27a0ecSDave Kleikamp 			raw_inode->i_block[0] = 0;
4467ac27a0ecSDave Kleikamp 			raw_inode->i_block[1] =
4468ac27a0ecSDave Kleikamp 				cpu_to_le32(new_encode_dev(inode->i_rdev));
4469ac27a0ecSDave Kleikamp 			raw_inode->i_block[2] = 0;
4470ac27a0ecSDave Kleikamp 		}
4471f19d5870STao Ma 	} else if (!ext4_has_inline_data(inode)) {
4472de9a55b8STheodore Ts'o 		for (block = 0; block < EXT4_N_BLOCKS; block++)
4473ac27a0ecSDave Kleikamp 			raw_inode->i_block[block] = ei->i_data[block];
4474f19d5870STao Ma 	}
4475ac27a0ecSDave Kleikamp 
447625ec56b5SJean Noel Cordenner 	raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
447725ec56b5SJean Noel Cordenner 	if (ei->i_extra_isize) {
447825ec56b5SJean Noel Cordenner 		if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
447925ec56b5SJean Noel Cordenner 			raw_inode->i_version_hi =
448025ec56b5SJean Noel Cordenner 			cpu_to_le32(inode->i_version >> 32);
4481ac27a0ecSDave Kleikamp 		raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
448225ec56b5SJean Noel Cordenner 	}
448325ec56b5SJean Noel Cordenner 
4484814525f4SDarrick J. Wong 	ext4_inode_csum_set(inode, raw_inode, ei);
4485814525f4SDarrick J. Wong 
44860390131bSFrank Mayhar 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
448773b50c1cSCurt Wohlgemuth 	rc = ext4_handle_dirty_metadata(handle, NULL, bh);
4488ac27a0ecSDave Kleikamp 	if (!err)
4489ac27a0ecSDave Kleikamp 		err = rc;
449019f5fb7aSTheodore Ts'o 	ext4_clear_inode_state(inode, EXT4_STATE_NEW);
4491ac27a0ecSDave Kleikamp 
4492b71fc079SJan Kara 	ext4_update_inode_fsync_trans(handle, inode, need_datasync);
4493ac27a0ecSDave Kleikamp out_brelse:
4494ac27a0ecSDave Kleikamp 	brelse(bh);
4495617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4496ac27a0ecSDave Kleikamp 	return err;
4497ac27a0ecSDave Kleikamp }
4498ac27a0ecSDave Kleikamp 
4499ac27a0ecSDave Kleikamp /*
4500617ba13bSMingming Cao  * ext4_write_inode()
4501ac27a0ecSDave Kleikamp  *
4502ac27a0ecSDave Kleikamp  * We are called from a few places:
4503ac27a0ecSDave Kleikamp  *
4504ac27a0ecSDave Kleikamp  * - Within generic_file_write() for O_SYNC files.
4505ac27a0ecSDave Kleikamp  *   Here, there will be no transaction running. We wait for any running
45064907cb7bSAnatol Pomozov  *   transaction to commit.
4507ac27a0ecSDave Kleikamp  *
4508ac27a0ecSDave Kleikamp  * - Within sys_sync(), kupdate and such.
4509ac27a0ecSDave Kleikamp  *   We wait on commit, if tol to.
4510ac27a0ecSDave Kleikamp  *
4511ac27a0ecSDave Kleikamp  * - Within prune_icache() (PF_MEMALLOC == true)
4512ac27a0ecSDave Kleikamp  *   Here we simply return.  We can't afford to block kswapd on the
4513ac27a0ecSDave Kleikamp  *   journal commit.
4514ac27a0ecSDave Kleikamp  *
4515ac27a0ecSDave Kleikamp  * In all cases it is actually safe for us to return without doing anything,
4516ac27a0ecSDave Kleikamp  * because the inode has been copied into a raw inode buffer in
4517617ba13bSMingming Cao  * ext4_mark_inode_dirty().  This is a correctness thing for O_SYNC and for
4518ac27a0ecSDave Kleikamp  * knfsd.
4519ac27a0ecSDave Kleikamp  *
4520ac27a0ecSDave Kleikamp  * Note that we are absolutely dependent upon all inode dirtiers doing the
4521ac27a0ecSDave Kleikamp  * right thing: they *must* call mark_inode_dirty() after dirtying info in
4522ac27a0ecSDave Kleikamp  * which we are interested.
4523ac27a0ecSDave Kleikamp  *
4524ac27a0ecSDave Kleikamp  * It would be a bug for them to not do this.  The code:
4525ac27a0ecSDave Kleikamp  *
4526ac27a0ecSDave Kleikamp  *	mark_inode_dirty(inode)
4527ac27a0ecSDave Kleikamp  *	stuff();
4528ac27a0ecSDave Kleikamp  *	inode->i_size = expr;
4529ac27a0ecSDave Kleikamp  *
4530ac27a0ecSDave Kleikamp  * is in error because a kswapd-driven write_inode() could occur while
4531ac27a0ecSDave Kleikamp  * `stuff()' is running, and the new i_size will be lost.  Plus the inode
4532ac27a0ecSDave Kleikamp  * will no longer be on the superblock's dirty inode list.
4533ac27a0ecSDave Kleikamp  */
4534a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
4535ac27a0ecSDave Kleikamp {
453691ac6f43SFrank Mayhar 	int err;
453791ac6f43SFrank Mayhar 
4538ac27a0ecSDave Kleikamp 	if (current->flags & PF_MEMALLOC)
4539ac27a0ecSDave Kleikamp 		return 0;
4540ac27a0ecSDave Kleikamp 
454191ac6f43SFrank Mayhar 	if (EXT4_SB(inode->i_sb)->s_journal) {
4542617ba13bSMingming Cao 		if (ext4_journal_current_handle()) {
4543b38bd33aSMingming Cao 			jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4544ac27a0ecSDave Kleikamp 			dump_stack();
4545ac27a0ecSDave Kleikamp 			return -EIO;
4546ac27a0ecSDave Kleikamp 		}
4547ac27a0ecSDave Kleikamp 
4548a9185b41SChristoph Hellwig 		if (wbc->sync_mode != WB_SYNC_ALL)
4549ac27a0ecSDave Kleikamp 			return 0;
4550ac27a0ecSDave Kleikamp 
455191ac6f43SFrank Mayhar 		err = ext4_force_commit(inode->i_sb);
455291ac6f43SFrank Mayhar 	} else {
455391ac6f43SFrank Mayhar 		struct ext4_iloc iloc;
455491ac6f43SFrank Mayhar 
45558b472d73SCurt Wohlgemuth 		err = __ext4_get_inode_loc(inode, &iloc, 0);
455691ac6f43SFrank Mayhar 		if (err)
455791ac6f43SFrank Mayhar 			return err;
4558a9185b41SChristoph Hellwig 		if (wbc->sync_mode == WB_SYNC_ALL)
4559830156c7SFrank Mayhar 			sync_dirty_buffer(iloc.bh);
4560830156c7SFrank Mayhar 		if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
4561c398eda0STheodore Ts'o 			EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4562c398eda0STheodore Ts'o 					 "IO error syncing inode");
4563830156c7SFrank Mayhar 			err = -EIO;
4564830156c7SFrank Mayhar 		}
4565fd2dd9fbSCurt Wohlgemuth 		brelse(iloc.bh);
456691ac6f43SFrank Mayhar 	}
456791ac6f43SFrank Mayhar 	return err;
4568ac27a0ecSDave Kleikamp }
4569ac27a0ecSDave Kleikamp 
4570ac27a0ecSDave Kleikamp /*
457153e87268SJan Kara  * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
457253e87268SJan Kara  * buffers that are attached to a page stradding i_size and are undergoing
457353e87268SJan Kara  * commit. In that case we have to wait for commit to finish and try again.
457453e87268SJan Kara  */
457553e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode)
457653e87268SJan Kara {
457753e87268SJan Kara 	struct page *page;
457853e87268SJan Kara 	unsigned offset;
457953e87268SJan Kara 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
458053e87268SJan Kara 	tid_t commit_tid = 0;
458153e87268SJan Kara 	int ret;
458253e87268SJan Kara 
458353e87268SJan Kara 	offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
458453e87268SJan Kara 	/*
458553e87268SJan Kara 	 * All buffers in the last page remain valid? Then there's nothing to
458653e87268SJan Kara 	 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
458753e87268SJan Kara 	 * blocksize case
458853e87268SJan Kara 	 */
458953e87268SJan Kara 	if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
459053e87268SJan Kara 		return;
459153e87268SJan Kara 	while (1) {
459253e87268SJan Kara 		page = find_lock_page(inode->i_mapping,
459353e87268SJan Kara 				      inode->i_size >> PAGE_CACHE_SHIFT);
459453e87268SJan Kara 		if (!page)
459553e87268SJan Kara 			return;
459653e87268SJan Kara 		ret = __ext4_journalled_invalidatepage(page, offset);
459753e87268SJan Kara 		unlock_page(page);
459853e87268SJan Kara 		page_cache_release(page);
459953e87268SJan Kara 		if (ret != -EBUSY)
460053e87268SJan Kara 			return;
460153e87268SJan Kara 		commit_tid = 0;
460253e87268SJan Kara 		read_lock(&journal->j_state_lock);
460353e87268SJan Kara 		if (journal->j_committing_transaction)
460453e87268SJan Kara 			commit_tid = journal->j_committing_transaction->t_tid;
460553e87268SJan Kara 		read_unlock(&journal->j_state_lock);
460653e87268SJan Kara 		if (commit_tid)
460753e87268SJan Kara 			jbd2_log_wait_commit(journal, commit_tid);
460853e87268SJan Kara 	}
460953e87268SJan Kara }
461053e87268SJan Kara 
461153e87268SJan Kara /*
4612617ba13bSMingming Cao  * ext4_setattr()
4613ac27a0ecSDave Kleikamp  *
4614ac27a0ecSDave Kleikamp  * Called from notify_change.
4615ac27a0ecSDave Kleikamp  *
4616ac27a0ecSDave Kleikamp  * We want to trap VFS attempts to truncate the file as soon as
4617ac27a0ecSDave Kleikamp  * possible.  In particular, we want to make sure that when the VFS
4618ac27a0ecSDave Kleikamp  * shrinks i_size, we put the inode on the orphan list and modify
4619ac27a0ecSDave Kleikamp  * i_disksize immediately, so that during the subsequent flushing of
4620ac27a0ecSDave Kleikamp  * dirty pages and freeing of disk blocks, we can guarantee that any
4621ac27a0ecSDave Kleikamp  * commit will leave the blocks being flushed in an unused state on
4622ac27a0ecSDave Kleikamp  * disk.  (On recovery, the inode will get truncated and the blocks will
4623ac27a0ecSDave Kleikamp  * be freed, so we have a strong guarantee that no future commit will
4624ac27a0ecSDave Kleikamp  * leave these blocks visible to the user.)
4625ac27a0ecSDave Kleikamp  *
4626678aaf48SJan Kara  * Another thing we have to assure is that if we are in ordered mode
4627678aaf48SJan Kara  * and inode is still attached to the committing transaction, we must
4628678aaf48SJan Kara  * we start writeout of all the dirty pages which are being truncated.
4629678aaf48SJan Kara  * This way we are sure that all the data written in the previous
4630678aaf48SJan Kara  * transaction are already on disk (truncate waits for pages under
4631678aaf48SJan Kara  * writeback).
4632678aaf48SJan Kara  *
4633678aaf48SJan Kara  * Called with inode->i_mutex down.
4634ac27a0ecSDave Kleikamp  */
4635617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr)
4636ac27a0ecSDave Kleikamp {
4637ac27a0ecSDave Kleikamp 	struct inode *inode = dentry->d_inode;
4638ac27a0ecSDave Kleikamp 	int error, rc = 0;
46393d287de3SDmitry Monakhov 	int orphan = 0;
4640ac27a0ecSDave Kleikamp 	const unsigned int ia_valid = attr->ia_valid;
4641ac27a0ecSDave Kleikamp 
4642ac27a0ecSDave Kleikamp 	error = inode_change_ok(inode, attr);
4643ac27a0ecSDave Kleikamp 	if (error)
4644ac27a0ecSDave Kleikamp 		return error;
4645ac27a0ecSDave Kleikamp 
464612755627SDmitry Monakhov 	if (is_quota_modification(inode, attr))
4647871a2931SChristoph Hellwig 		dquot_initialize(inode);
464808cefc7aSEric W. Biederman 	if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
464908cefc7aSEric W. Biederman 	    (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
4650ac27a0ecSDave Kleikamp 		handle_t *handle;
4651ac27a0ecSDave Kleikamp 
4652ac27a0ecSDave Kleikamp 		/* (user+group)*(old+new) structure, inode write (sb,
4653ac27a0ecSDave Kleikamp 		 * inode block, ? - but truncate inode update has it) */
46549924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
46559924a92aSTheodore Ts'o 			(EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4656194074acSDmitry Monakhov 			 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
4657ac27a0ecSDave Kleikamp 		if (IS_ERR(handle)) {
4658ac27a0ecSDave Kleikamp 			error = PTR_ERR(handle);
4659ac27a0ecSDave Kleikamp 			goto err_out;
4660ac27a0ecSDave Kleikamp 		}
4661b43fa828SChristoph Hellwig 		error = dquot_transfer(inode, attr);
4662ac27a0ecSDave Kleikamp 		if (error) {
4663617ba13bSMingming Cao 			ext4_journal_stop(handle);
4664ac27a0ecSDave Kleikamp 			return error;
4665ac27a0ecSDave Kleikamp 		}
4666ac27a0ecSDave Kleikamp 		/* Update corresponding info in inode so that everything is in
4667ac27a0ecSDave Kleikamp 		 * one transaction */
4668ac27a0ecSDave Kleikamp 		if (attr->ia_valid & ATTR_UID)
4669ac27a0ecSDave Kleikamp 			inode->i_uid = attr->ia_uid;
4670ac27a0ecSDave Kleikamp 		if (attr->ia_valid & ATTR_GID)
4671ac27a0ecSDave Kleikamp 			inode->i_gid = attr->ia_gid;
4672617ba13bSMingming Cao 		error = ext4_mark_inode_dirty(handle, inode);
4673617ba13bSMingming Cao 		ext4_journal_stop(handle);
4674ac27a0ecSDave Kleikamp 	}
4675ac27a0ecSDave Kleikamp 
4676e2b46574SEric Sandeen 	if (attr->ia_valid & ATTR_SIZE) {
4677562c72aaSChristoph Hellwig 
467812e9b892SDmitry Monakhov 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4679e2b46574SEric Sandeen 			struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4680e2b46574SEric Sandeen 
46810c095c7fSTheodore Ts'o 			if (attr->ia_size > sbi->s_bitmap_maxbytes)
46820c095c7fSTheodore Ts'o 				return -EFBIG;
4683e2b46574SEric Sandeen 		}
4684e2b46574SEric Sandeen 	}
4685e2b46574SEric Sandeen 
4686ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode) &&
4687c8d46e41SJiaying Zhang 	    attr->ia_valid & ATTR_SIZE &&
4688072bd7eaSTheodore Ts'o 	    (attr->ia_size < inode->i_size)) {
4689ac27a0ecSDave Kleikamp 		handle_t *handle;
4690ac27a0ecSDave Kleikamp 
46919924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
4692ac27a0ecSDave Kleikamp 		if (IS_ERR(handle)) {
4693ac27a0ecSDave Kleikamp 			error = PTR_ERR(handle);
4694ac27a0ecSDave Kleikamp 			goto err_out;
4695ac27a0ecSDave Kleikamp 		}
46963d287de3SDmitry Monakhov 		if (ext4_handle_valid(handle)) {
4697617ba13bSMingming Cao 			error = ext4_orphan_add(handle, inode);
46983d287de3SDmitry Monakhov 			orphan = 1;
46993d287de3SDmitry Monakhov 		}
4700617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = attr->ia_size;
4701617ba13bSMingming Cao 		rc = ext4_mark_inode_dirty(handle, inode);
4702ac27a0ecSDave Kleikamp 		if (!error)
4703ac27a0ecSDave Kleikamp 			error = rc;
4704617ba13bSMingming Cao 		ext4_journal_stop(handle);
4705678aaf48SJan Kara 
4706678aaf48SJan Kara 		if (ext4_should_order_data(inode)) {
4707678aaf48SJan Kara 			error = ext4_begin_ordered_truncate(inode,
4708678aaf48SJan Kara 							    attr->ia_size);
4709678aaf48SJan Kara 			if (error) {
4710678aaf48SJan Kara 				/* Do as much error cleanup as possible */
47119924a92aSTheodore Ts'o 				handle = ext4_journal_start(inode,
47129924a92aSTheodore Ts'o 							    EXT4_HT_INODE, 3);
4713678aaf48SJan Kara 				if (IS_ERR(handle)) {
4714678aaf48SJan Kara 					ext4_orphan_del(NULL, inode);
4715678aaf48SJan Kara 					goto err_out;
4716678aaf48SJan Kara 				}
4717678aaf48SJan Kara 				ext4_orphan_del(handle, inode);
47183d287de3SDmitry Monakhov 				orphan = 0;
4719678aaf48SJan Kara 				ext4_journal_stop(handle);
4720678aaf48SJan Kara 				goto err_out;
4721678aaf48SJan Kara 			}
4722678aaf48SJan Kara 		}
4723ac27a0ecSDave Kleikamp 	}
4724ac27a0ecSDave Kleikamp 
4725072bd7eaSTheodore Ts'o 	if (attr->ia_valid & ATTR_SIZE) {
472653e87268SJan Kara 		if (attr->ia_size != inode->i_size) {
472753e87268SJan Kara 			loff_t oldsize = inode->i_size;
472853e87268SJan Kara 
472953e87268SJan Kara 			i_size_write(inode, attr->ia_size);
473053e87268SJan Kara 			/*
473153e87268SJan Kara 			 * Blocks are going to be removed from the inode. Wait
473253e87268SJan Kara 			 * for dio in flight.  Temporarily disable
473353e87268SJan Kara 			 * dioread_nolock to prevent livelock.
473453e87268SJan Kara 			 */
47351b65007eSDmitry Monakhov 			if (orphan) {
473653e87268SJan Kara 				if (!ext4_should_journal_data(inode)) {
47371b65007eSDmitry Monakhov 					ext4_inode_block_unlocked_dio(inode);
47381c9114f9SDmitry Monakhov 					inode_dio_wait(inode);
47391b65007eSDmitry Monakhov 					ext4_inode_resume_unlocked_dio(inode);
474053e87268SJan Kara 				} else
474153e87268SJan Kara 					ext4_wait_for_tail_page_commit(inode);
47421b65007eSDmitry Monakhov 			}
474353e87268SJan Kara 			/*
474453e87268SJan Kara 			 * Truncate pagecache after we've waited for commit
474553e87268SJan Kara 			 * in data=journal mode to make pages freeable.
474653e87268SJan Kara 			 */
474753e87268SJan Kara 			truncate_pagecache(inode, oldsize, inode->i_size);
47481c9114f9SDmitry Monakhov 		}
4749072bd7eaSTheodore Ts'o 		ext4_truncate(inode);
4750072bd7eaSTheodore Ts'o 	}
4751ac27a0ecSDave Kleikamp 
47521025774cSChristoph Hellwig 	if (!rc) {
47531025774cSChristoph Hellwig 		setattr_copy(inode, attr);
47541025774cSChristoph Hellwig 		mark_inode_dirty(inode);
47551025774cSChristoph Hellwig 	}
47561025774cSChristoph Hellwig 
47571025774cSChristoph Hellwig 	/*
47581025774cSChristoph Hellwig 	 * If the call to ext4_truncate failed to get a transaction handle at
47591025774cSChristoph Hellwig 	 * all, we need to clean up the in-core orphan list manually.
47601025774cSChristoph Hellwig 	 */
47613d287de3SDmitry Monakhov 	if (orphan && inode->i_nlink)
4762617ba13bSMingming Cao 		ext4_orphan_del(NULL, inode);
4763ac27a0ecSDave Kleikamp 
4764ac27a0ecSDave Kleikamp 	if (!rc && (ia_valid & ATTR_MODE))
4765617ba13bSMingming Cao 		rc = ext4_acl_chmod(inode);
4766ac27a0ecSDave Kleikamp 
4767ac27a0ecSDave Kleikamp err_out:
4768617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, error);
4769ac27a0ecSDave Kleikamp 	if (!error)
4770ac27a0ecSDave Kleikamp 		error = rc;
4771ac27a0ecSDave Kleikamp 	return error;
4772ac27a0ecSDave Kleikamp }
4773ac27a0ecSDave Kleikamp 
47743e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
47753e3398a0SMingming Cao 		 struct kstat *stat)
47763e3398a0SMingming Cao {
47773e3398a0SMingming Cao 	struct inode *inode;
47783e3398a0SMingming Cao 	unsigned long delalloc_blocks;
47793e3398a0SMingming Cao 
47803e3398a0SMingming Cao 	inode = dentry->d_inode;
47813e3398a0SMingming Cao 	generic_fillattr(inode, stat);
47823e3398a0SMingming Cao 
47833e3398a0SMingming Cao 	/*
47843e3398a0SMingming Cao 	 * We can't update i_blocks if the block allocation is delayed
47853e3398a0SMingming Cao 	 * otherwise in the case of system crash before the real block
47863e3398a0SMingming Cao 	 * allocation is done, we will have i_blocks inconsistent with
47873e3398a0SMingming Cao 	 * on-disk file blocks.
47883e3398a0SMingming Cao 	 * We always keep i_blocks updated together with real
47893e3398a0SMingming Cao 	 * allocation. But to not confuse with user, stat
47903e3398a0SMingming Cao 	 * will return the blocks that include the delayed allocation
47913e3398a0SMingming Cao 	 * blocks for this file.
47923e3398a0SMingming Cao 	 */
479396607551STao Ma 	delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
479496607551STao Ma 				EXT4_I(inode)->i_reserved_data_blocks);
47953e3398a0SMingming Cao 
47963e3398a0SMingming Cao 	stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
47973e3398a0SMingming Cao 	return 0;
47983e3398a0SMingming Cao }
4799ac27a0ecSDave Kleikamp 
4800a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4801a02908f1SMingming Cao {
480212e9b892SDmitry Monakhov 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
48038bb2b247SAmir Goldstein 		return ext4_ind_trans_blocks(inode, nrblocks, chunk);
4804ac51d837STheodore Ts'o 	return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
4805a02908f1SMingming Cao }
4806ac51d837STheodore Ts'o 
4807a02908f1SMingming Cao /*
4808a02908f1SMingming Cao  * Account for index blocks, block groups bitmaps and block group
4809a02908f1SMingming Cao  * descriptor blocks if modify datablocks and index blocks
4810a02908f1SMingming Cao  * worse case, the indexs blocks spread over different block groups
4811a02908f1SMingming Cao  *
4812a02908f1SMingming Cao  * If datablocks are discontiguous, they are possible to spread over
48134907cb7bSAnatol Pomozov  * different block groups too. If they are contiguous, with flexbg,
4814a02908f1SMingming Cao  * they could still across block group boundary.
4815a02908f1SMingming Cao  *
4816a02908f1SMingming Cao  * Also account for superblock, inode, quota and xattr blocks
4817a02908f1SMingming Cao  */
48181f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4819a02908f1SMingming Cao {
48208df9675fSTheodore Ts'o 	ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
48218df9675fSTheodore Ts'o 	int gdpblocks;
4822a02908f1SMingming Cao 	int idxblocks;
4823a02908f1SMingming Cao 	int ret = 0;
4824a02908f1SMingming Cao 
4825a02908f1SMingming Cao 	/*
4826a02908f1SMingming Cao 	 * How many index blocks need to touch to modify nrblocks?
4827a02908f1SMingming Cao 	 * The "Chunk" flag indicating whether the nrblocks is
4828a02908f1SMingming Cao 	 * physically contiguous on disk
4829a02908f1SMingming Cao 	 *
4830a02908f1SMingming Cao 	 * For Direct IO and fallocate, they calls get_block to allocate
4831a02908f1SMingming Cao 	 * one single extent at a time, so they could set the "Chunk" flag
4832a02908f1SMingming Cao 	 */
4833a02908f1SMingming Cao 	idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4834a02908f1SMingming Cao 
4835a02908f1SMingming Cao 	ret = idxblocks;
4836a02908f1SMingming Cao 
4837a02908f1SMingming Cao 	/*
4838a02908f1SMingming Cao 	 * Now let's see how many group bitmaps and group descriptors need
4839a02908f1SMingming Cao 	 * to account
4840a02908f1SMingming Cao 	 */
4841a02908f1SMingming Cao 	groups = idxblocks;
4842a02908f1SMingming Cao 	if (chunk)
4843a02908f1SMingming Cao 		groups += 1;
4844ac27a0ecSDave Kleikamp 	else
4845a02908f1SMingming Cao 		groups += nrblocks;
4846ac27a0ecSDave Kleikamp 
4847a02908f1SMingming Cao 	gdpblocks = groups;
48488df9675fSTheodore Ts'o 	if (groups > ngroups)
48498df9675fSTheodore Ts'o 		groups = ngroups;
4850a02908f1SMingming Cao 	if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4851a02908f1SMingming Cao 		gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4852a02908f1SMingming Cao 
4853a02908f1SMingming Cao 	/* bitmaps and block group descriptor blocks */
4854a02908f1SMingming Cao 	ret += groups + gdpblocks;
4855a02908f1SMingming Cao 
4856a02908f1SMingming Cao 	/* Blocks for super block, inode, quota and xattr blocks */
4857a02908f1SMingming Cao 	ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
4858ac27a0ecSDave Kleikamp 
4859ac27a0ecSDave Kleikamp 	return ret;
4860ac27a0ecSDave Kleikamp }
4861ac27a0ecSDave Kleikamp 
4862ac27a0ecSDave Kleikamp /*
486325985edcSLucas De Marchi  * Calculate the total number of credits to reserve to fit
4864f3bd1f3fSMingming Cao  * the modification of a single pages into a single transaction,
4865f3bd1f3fSMingming Cao  * which may include multiple chunks of block allocations.
4866a02908f1SMingming Cao  *
4867525f4ed8SMingming Cao  * This could be called via ext4_write_begin()
4868a02908f1SMingming Cao  *
4869525f4ed8SMingming Cao  * We need to consider the worse case, when
4870a02908f1SMingming Cao  * one new block per extent.
4871a02908f1SMingming Cao  */
4872a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode)
4873a02908f1SMingming Cao {
4874a02908f1SMingming Cao 	int bpp = ext4_journal_blocks_per_page(inode);
4875a02908f1SMingming Cao 	int ret;
4876a02908f1SMingming Cao 
4877a02908f1SMingming Cao 	ret = ext4_meta_trans_blocks(inode, bpp, 0);
4878a02908f1SMingming Cao 
4879a02908f1SMingming Cao 	/* Account for data blocks for journalled mode */
4880a02908f1SMingming Cao 	if (ext4_should_journal_data(inode))
4881a02908f1SMingming Cao 		ret += bpp;
4882a02908f1SMingming Cao 	return ret;
4883a02908f1SMingming Cao }
4884f3bd1f3fSMingming Cao 
4885f3bd1f3fSMingming Cao /*
4886f3bd1f3fSMingming Cao  * Calculate the journal credits for a chunk of data modification.
4887f3bd1f3fSMingming Cao  *
4888f3bd1f3fSMingming Cao  * This is called from DIO, fallocate or whoever calling
488979e83036SEric Sandeen  * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
4890f3bd1f3fSMingming Cao  *
4891f3bd1f3fSMingming Cao  * journal buffers for data blocks are not included here, as DIO
4892f3bd1f3fSMingming Cao  * and fallocate do no need to journal data buffers.
4893f3bd1f3fSMingming Cao  */
4894f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4895f3bd1f3fSMingming Cao {
4896f3bd1f3fSMingming Cao 	return ext4_meta_trans_blocks(inode, nrblocks, 1);
4897f3bd1f3fSMingming Cao }
4898f3bd1f3fSMingming Cao 
4899a02908f1SMingming Cao /*
4900617ba13bSMingming Cao  * The caller must have previously called ext4_reserve_inode_write().
4901ac27a0ecSDave Kleikamp  * Give this, we know that the caller already has write access to iloc->bh.
4902ac27a0ecSDave Kleikamp  */
4903617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle,
4904617ba13bSMingming Cao 			 struct inode *inode, struct ext4_iloc *iloc)
4905ac27a0ecSDave Kleikamp {
4906ac27a0ecSDave Kleikamp 	int err = 0;
4907ac27a0ecSDave Kleikamp 
4908c64db50eSTheodore Ts'o 	if (IS_I_VERSION(inode))
490925ec56b5SJean Noel Cordenner 		inode_inc_iversion(inode);
491025ec56b5SJean Noel Cordenner 
4911ac27a0ecSDave Kleikamp 	/* the do_update_inode consumes one bh->b_count */
4912ac27a0ecSDave Kleikamp 	get_bh(iloc->bh);
4913ac27a0ecSDave Kleikamp 
4914dab291afSMingming Cao 	/* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
4915830156c7SFrank Mayhar 	err = ext4_do_update_inode(handle, inode, iloc);
4916ac27a0ecSDave Kleikamp 	put_bh(iloc->bh);
4917ac27a0ecSDave Kleikamp 	return err;
4918ac27a0ecSDave Kleikamp }
4919ac27a0ecSDave Kleikamp 
4920ac27a0ecSDave Kleikamp /*
4921ac27a0ecSDave Kleikamp  * On success, We end up with an outstanding reference count against
4922ac27a0ecSDave Kleikamp  * iloc->bh.  This _must_ be cleaned up later.
4923ac27a0ecSDave Kleikamp  */
4924ac27a0ecSDave Kleikamp 
4925ac27a0ecSDave Kleikamp int
4926617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4927617ba13bSMingming Cao 			 struct ext4_iloc *iloc)
4928ac27a0ecSDave Kleikamp {
49290390131bSFrank Mayhar 	int err;
49300390131bSFrank Mayhar 
4931617ba13bSMingming Cao 	err = ext4_get_inode_loc(inode, iloc);
4932ac27a0ecSDave Kleikamp 	if (!err) {
4933ac27a0ecSDave Kleikamp 		BUFFER_TRACE(iloc->bh, "get_write_access");
4934617ba13bSMingming Cao 		err = ext4_journal_get_write_access(handle, iloc->bh);
4935ac27a0ecSDave Kleikamp 		if (err) {
4936ac27a0ecSDave Kleikamp 			brelse(iloc->bh);
4937ac27a0ecSDave Kleikamp 			iloc->bh = NULL;
4938ac27a0ecSDave Kleikamp 		}
4939ac27a0ecSDave Kleikamp 	}
4940617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4941ac27a0ecSDave Kleikamp 	return err;
4942ac27a0ecSDave Kleikamp }
4943ac27a0ecSDave Kleikamp 
4944ac27a0ecSDave Kleikamp /*
49456dd4ee7cSKalpak Shah  * Expand an inode by new_extra_isize bytes.
49466dd4ee7cSKalpak Shah  * Returns 0 on success or negative error number on failure.
49476dd4ee7cSKalpak Shah  */
49481d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode,
49491d03ec98SAneesh Kumar K.V 				   unsigned int new_extra_isize,
49501d03ec98SAneesh Kumar K.V 				   struct ext4_iloc iloc,
49511d03ec98SAneesh Kumar K.V 				   handle_t *handle)
49526dd4ee7cSKalpak Shah {
49536dd4ee7cSKalpak Shah 	struct ext4_inode *raw_inode;
49546dd4ee7cSKalpak Shah 	struct ext4_xattr_ibody_header *header;
49556dd4ee7cSKalpak Shah 
49566dd4ee7cSKalpak Shah 	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
49576dd4ee7cSKalpak Shah 		return 0;
49586dd4ee7cSKalpak Shah 
49596dd4ee7cSKalpak Shah 	raw_inode = ext4_raw_inode(&iloc);
49606dd4ee7cSKalpak Shah 
49616dd4ee7cSKalpak Shah 	header = IHDR(inode, raw_inode);
49626dd4ee7cSKalpak Shah 
49636dd4ee7cSKalpak Shah 	/* No extended attributes present */
496419f5fb7aSTheodore Ts'o 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
49656dd4ee7cSKalpak Shah 	    header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
49666dd4ee7cSKalpak Shah 		memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
49676dd4ee7cSKalpak Shah 			new_extra_isize);
49686dd4ee7cSKalpak Shah 		EXT4_I(inode)->i_extra_isize = new_extra_isize;
49696dd4ee7cSKalpak Shah 		return 0;
49706dd4ee7cSKalpak Shah 	}
49716dd4ee7cSKalpak Shah 
49726dd4ee7cSKalpak Shah 	/* try to expand with EAs present */
49736dd4ee7cSKalpak Shah 	return ext4_expand_extra_isize_ea(inode, new_extra_isize,
49746dd4ee7cSKalpak Shah 					  raw_inode, handle);
49756dd4ee7cSKalpak Shah }
49766dd4ee7cSKalpak Shah 
49776dd4ee7cSKalpak Shah /*
4978ac27a0ecSDave Kleikamp  * What we do here is to mark the in-core inode as clean with respect to inode
4979ac27a0ecSDave Kleikamp  * dirtiness (it may still be data-dirty).
4980ac27a0ecSDave Kleikamp  * This means that the in-core inode may be reaped by prune_icache
4981ac27a0ecSDave Kleikamp  * without having to perform any I/O.  This is a very good thing,
4982ac27a0ecSDave Kleikamp  * because *any* task may call prune_icache - even ones which
4983ac27a0ecSDave Kleikamp  * have a transaction open against a different journal.
4984ac27a0ecSDave Kleikamp  *
4985ac27a0ecSDave Kleikamp  * Is this cheating?  Not really.  Sure, we haven't written the
4986ac27a0ecSDave Kleikamp  * inode out, but prune_icache isn't a user-visible syncing function.
4987ac27a0ecSDave Kleikamp  * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4988ac27a0ecSDave Kleikamp  * we start and wait on commits.
4989ac27a0ecSDave Kleikamp  */
4990617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
4991ac27a0ecSDave Kleikamp {
4992617ba13bSMingming Cao 	struct ext4_iloc iloc;
49936dd4ee7cSKalpak Shah 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
49946dd4ee7cSKalpak Shah 	static unsigned int mnt_count;
49956dd4ee7cSKalpak Shah 	int err, ret;
4996ac27a0ecSDave Kleikamp 
4997ac27a0ecSDave Kleikamp 	might_sleep();
49987ff9c073STheodore Ts'o 	trace_ext4_mark_inode_dirty(inode, _RET_IP_);
4999617ba13bSMingming Cao 	err = ext4_reserve_inode_write(handle, inode, &iloc);
50000390131bSFrank Mayhar 	if (ext4_handle_valid(handle) &&
50010390131bSFrank Mayhar 	    EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
500219f5fb7aSTheodore Ts'o 	    !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
50036dd4ee7cSKalpak Shah 		/*
50046dd4ee7cSKalpak Shah 		 * We need extra buffer credits since we may write into EA block
50056dd4ee7cSKalpak Shah 		 * with this same handle. If journal_extend fails, then it will
50066dd4ee7cSKalpak Shah 		 * only result in a minor loss of functionality for that inode.
50076dd4ee7cSKalpak Shah 		 * If this is felt to be critical, then e2fsck should be run to
50086dd4ee7cSKalpak Shah 		 * force a large enough s_min_extra_isize.
50096dd4ee7cSKalpak Shah 		 */
50106dd4ee7cSKalpak Shah 		if ((jbd2_journal_extend(handle,
50116dd4ee7cSKalpak Shah 			     EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
50126dd4ee7cSKalpak Shah 			ret = ext4_expand_extra_isize(inode,
50136dd4ee7cSKalpak Shah 						      sbi->s_want_extra_isize,
50146dd4ee7cSKalpak Shah 						      iloc, handle);
50156dd4ee7cSKalpak Shah 			if (ret) {
501619f5fb7aSTheodore Ts'o 				ext4_set_inode_state(inode,
501719f5fb7aSTheodore Ts'o 						     EXT4_STATE_NO_EXPAND);
5018c1bddad9SAneesh Kumar K.V 				if (mnt_count !=
5019c1bddad9SAneesh Kumar K.V 					le16_to_cpu(sbi->s_es->s_mnt_count)) {
502012062dddSEric Sandeen 					ext4_warning(inode->i_sb,
50216dd4ee7cSKalpak Shah 					"Unable to expand inode %lu. Delete"
50226dd4ee7cSKalpak Shah 					" some EAs or run e2fsck.",
50236dd4ee7cSKalpak Shah 					inode->i_ino);
5024c1bddad9SAneesh Kumar K.V 					mnt_count =
5025c1bddad9SAneesh Kumar K.V 					  le16_to_cpu(sbi->s_es->s_mnt_count);
50266dd4ee7cSKalpak Shah 				}
50276dd4ee7cSKalpak Shah 			}
50286dd4ee7cSKalpak Shah 		}
50296dd4ee7cSKalpak Shah 	}
5030ac27a0ecSDave Kleikamp 	if (!err)
5031617ba13bSMingming Cao 		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
5032ac27a0ecSDave Kleikamp 	return err;
5033ac27a0ecSDave Kleikamp }
5034ac27a0ecSDave Kleikamp 
5035ac27a0ecSDave Kleikamp /*
5036617ba13bSMingming Cao  * ext4_dirty_inode() is called from __mark_inode_dirty()
5037ac27a0ecSDave Kleikamp  *
5038ac27a0ecSDave Kleikamp  * We're really interested in the case where a file is being extended.
5039ac27a0ecSDave Kleikamp  * i_size has been changed by generic_commit_write() and we thus need
5040ac27a0ecSDave Kleikamp  * to include the updated inode in the current transaction.
5041ac27a0ecSDave Kleikamp  *
50425dd4056dSChristoph Hellwig  * Also, dquot_alloc_block() will always dirty the inode when blocks
5043ac27a0ecSDave Kleikamp  * are allocated to the file.
5044ac27a0ecSDave Kleikamp  *
5045ac27a0ecSDave Kleikamp  * If the inode is marked synchronous, we don't honour that here - doing
5046ac27a0ecSDave Kleikamp  * so would cause a commit on atime updates, which we don't bother doing.
5047ac27a0ecSDave Kleikamp  * We handle synchronous inodes at the highest possible level.
5048ac27a0ecSDave Kleikamp  */
5049aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags)
5050ac27a0ecSDave Kleikamp {
5051ac27a0ecSDave Kleikamp 	handle_t *handle;
5052ac27a0ecSDave Kleikamp 
50539924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
5054ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5055ac27a0ecSDave Kleikamp 		goto out;
5056f3dc272fSCurt Wohlgemuth 
5057617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
5058f3dc272fSCurt Wohlgemuth 
5059617ba13bSMingming Cao 	ext4_journal_stop(handle);
5060ac27a0ecSDave Kleikamp out:
5061ac27a0ecSDave Kleikamp 	return;
5062ac27a0ecSDave Kleikamp }
5063ac27a0ecSDave Kleikamp 
5064ac27a0ecSDave Kleikamp #if 0
5065ac27a0ecSDave Kleikamp /*
5066ac27a0ecSDave Kleikamp  * Bind an inode's backing buffer_head into this transaction, to prevent
5067ac27a0ecSDave Kleikamp  * it from being flushed to disk early.  Unlike
5068617ba13bSMingming Cao  * ext4_reserve_inode_write, this leaves behind no bh reference and
5069ac27a0ecSDave Kleikamp  * returns no iloc structure, so the caller needs to repeat the iloc
5070ac27a0ecSDave Kleikamp  * lookup to mark the inode dirty later.
5071ac27a0ecSDave Kleikamp  */
5072617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode)
5073ac27a0ecSDave Kleikamp {
5074617ba13bSMingming Cao 	struct ext4_iloc iloc;
5075ac27a0ecSDave Kleikamp 
5076ac27a0ecSDave Kleikamp 	int err = 0;
5077ac27a0ecSDave Kleikamp 	if (handle) {
5078617ba13bSMingming Cao 		err = ext4_get_inode_loc(inode, &iloc);
5079ac27a0ecSDave Kleikamp 		if (!err) {
5080ac27a0ecSDave Kleikamp 			BUFFER_TRACE(iloc.bh, "get_write_access");
5081dab291afSMingming Cao 			err = jbd2_journal_get_write_access(handle, iloc.bh);
5082ac27a0ecSDave Kleikamp 			if (!err)
50830390131bSFrank Mayhar 				err = ext4_handle_dirty_metadata(handle,
508473b50c1cSCurt Wohlgemuth 								 NULL,
5085ac27a0ecSDave Kleikamp 								 iloc.bh);
5086ac27a0ecSDave Kleikamp 			brelse(iloc.bh);
5087ac27a0ecSDave Kleikamp 		}
5088ac27a0ecSDave Kleikamp 	}
5089617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
5090ac27a0ecSDave Kleikamp 	return err;
5091ac27a0ecSDave Kleikamp }
5092ac27a0ecSDave Kleikamp #endif
5093ac27a0ecSDave Kleikamp 
5094617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val)
5095ac27a0ecSDave Kleikamp {
5096ac27a0ecSDave Kleikamp 	journal_t *journal;
5097ac27a0ecSDave Kleikamp 	handle_t *handle;
5098ac27a0ecSDave Kleikamp 	int err;
5099ac27a0ecSDave Kleikamp 
5100ac27a0ecSDave Kleikamp 	/*
5101ac27a0ecSDave Kleikamp 	 * We have to be very careful here: changing a data block's
5102ac27a0ecSDave Kleikamp 	 * journaling status dynamically is dangerous.  If we write a
5103ac27a0ecSDave Kleikamp 	 * data block to the journal, change the status and then delete
5104ac27a0ecSDave Kleikamp 	 * that block, we risk forgetting to revoke the old log record
5105ac27a0ecSDave Kleikamp 	 * from the journal and so a subsequent replay can corrupt data.
5106ac27a0ecSDave Kleikamp 	 * So, first we make sure that the journal is empty and that
5107ac27a0ecSDave Kleikamp 	 * nobody is changing anything.
5108ac27a0ecSDave Kleikamp 	 */
5109ac27a0ecSDave Kleikamp 
5110617ba13bSMingming Cao 	journal = EXT4_JOURNAL(inode);
51110390131bSFrank Mayhar 	if (!journal)
51120390131bSFrank Mayhar 		return 0;
5113d699594dSDave Hansen 	if (is_journal_aborted(journal))
5114ac27a0ecSDave Kleikamp 		return -EROFS;
51152aff57b0SYongqiang Yang 	/* We have to allocate physical blocks for delalloc blocks
51162aff57b0SYongqiang Yang 	 * before flushing journal. otherwise delalloc blocks can not
51172aff57b0SYongqiang Yang 	 * be allocated any more. even more truncate on delalloc blocks
51182aff57b0SYongqiang Yang 	 * could trigger BUG by flushing delalloc blocks in journal.
51192aff57b0SYongqiang Yang 	 * There is no delalloc block in non-journal data mode.
51202aff57b0SYongqiang Yang 	 */
51212aff57b0SYongqiang Yang 	if (val && test_opt(inode->i_sb, DELALLOC)) {
51222aff57b0SYongqiang Yang 		err = ext4_alloc_da_blocks(inode);
51232aff57b0SYongqiang Yang 		if (err < 0)
51242aff57b0SYongqiang Yang 			return err;
51252aff57b0SYongqiang Yang 	}
5126ac27a0ecSDave Kleikamp 
512717335dccSDmitry Monakhov 	/* Wait for all existing dio workers */
512817335dccSDmitry Monakhov 	ext4_inode_block_unlocked_dio(inode);
512917335dccSDmitry Monakhov 	inode_dio_wait(inode);
513017335dccSDmitry Monakhov 
5131dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
5132ac27a0ecSDave Kleikamp 
5133ac27a0ecSDave Kleikamp 	/*
5134ac27a0ecSDave Kleikamp 	 * OK, there are no updates running now, and all cached data is
5135ac27a0ecSDave Kleikamp 	 * synced to disk.  We are now in a completely consistent state
5136ac27a0ecSDave Kleikamp 	 * which doesn't have anything in the journal, and we know that
5137ac27a0ecSDave Kleikamp 	 * no filesystem updates are running, so it is safe to modify
5138ac27a0ecSDave Kleikamp 	 * the inode's in-core data-journaling state flag now.
5139ac27a0ecSDave Kleikamp 	 */
5140ac27a0ecSDave Kleikamp 
5141ac27a0ecSDave Kleikamp 	if (val)
514212e9b892SDmitry Monakhov 		ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
51435872ddaaSYongqiang Yang 	else {
51445872ddaaSYongqiang Yang 		jbd2_journal_flush(journal);
514512e9b892SDmitry Monakhov 		ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
51465872ddaaSYongqiang Yang 	}
5147617ba13bSMingming Cao 	ext4_set_aops(inode);
5148ac27a0ecSDave Kleikamp 
5149dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
515017335dccSDmitry Monakhov 	ext4_inode_resume_unlocked_dio(inode);
5151ac27a0ecSDave Kleikamp 
5152ac27a0ecSDave Kleikamp 	/* Finally we can mark the inode as dirty. */
5153ac27a0ecSDave Kleikamp 
51549924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
5155ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5156ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5157ac27a0ecSDave Kleikamp 
5158617ba13bSMingming Cao 	err = ext4_mark_inode_dirty(handle, inode);
51590390131bSFrank Mayhar 	ext4_handle_sync(handle);
5160617ba13bSMingming Cao 	ext4_journal_stop(handle);
5161617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
5162ac27a0ecSDave Kleikamp 
5163ac27a0ecSDave Kleikamp 	return err;
5164ac27a0ecSDave Kleikamp }
51652e9ee850SAneesh Kumar K.V 
51662e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
51672e9ee850SAneesh Kumar K.V {
51682e9ee850SAneesh Kumar K.V 	return !buffer_mapped(bh);
51692e9ee850SAneesh Kumar K.V }
51702e9ee850SAneesh Kumar K.V 
5171c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
51722e9ee850SAneesh Kumar K.V {
5173c2ec175cSNick Piggin 	struct page *page = vmf->page;
51742e9ee850SAneesh Kumar K.V 	loff_t size;
51752e9ee850SAneesh Kumar K.V 	unsigned long len;
51769ea7df53SJan Kara 	int ret;
51772e9ee850SAneesh Kumar K.V 	struct file *file = vma->vm_file;
5178496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
51792e9ee850SAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
51809ea7df53SJan Kara 	handle_t *handle;
51819ea7df53SJan Kara 	get_block_t *get_block;
51829ea7df53SJan Kara 	int retries = 0;
51832e9ee850SAneesh Kumar K.V 
51848e8ad8a5SJan Kara 	sb_start_pagefault(inode->i_sb);
5185041bbb6dSTheodore Ts'o 	file_update_time(vma->vm_file);
51869ea7df53SJan Kara 	/* Delalloc case is easy... */
51879ea7df53SJan Kara 	if (test_opt(inode->i_sb, DELALLOC) &&
51889ea7df53SJan Kara 	    !ext4_should_journal_data(inode) &&
51899ea7df53SJan Kara 	    !ext4_nonda_switch(inode->i_sb)) {
51909ea7df53SJan Kara 		do {
51919ea7df53SJan Kara 			ret = __block_page_mkwrite(vma, vmf,
51929ea7df53SJan Kara 						   ext4_da_get_block_prep);
51939ea7df53SJan Kara 		} while (ret == -ENOSPC &&
51949ea7df53SJan Kara 		       ext4_should_retry_alloc(inode->i_sb, &retries));
51959ea7df53SJan Kara 		goto out_ret;
51962e9ee850SAneesh Kumar K.V 	}
51970e499890SDarrick J. Wong 
51980e499890SDarrick J. Wong 	lock_page(page);
51999ea7df53SJan Kara 	size = i_size_read(inode);
52009ea7df53SJan Kara 	/* Page got truncated from under us? */
52019ea7df53SJan Kara 	if (page->mapping != mapping || page_offset(page) > size) {
52029ea7df53SJan Kara 		unlock_page(page);
52039ea7df53SJan Kara 		ret = VM_FAULT_NOPAGE;
52049ea7df53SJan Kara 		goto out;
52050e499890SDarrick J. Wong 	}
52062e9ee850SAneesh Kumar K.V 
52072e9ee850SAneesh Kumar K.V 	if (page->index == size >> PAGE_CACHE_SHIFT)
52082e9ee850SAneesh Kumar K.V 		len = size & ~PAGE_CACHE_MASK;
52092e9ee850SAneesh Kumar K.V 	else
52102e9ee850SAneesh Kumar K.V 		len = PAGE_CACHE_SIZE;
5211a827eaffSAneesh Kumar K.V 	/*
52129ea7df53SJan Kara 	 * Return if we have all the buffers mapped. This avoids the need to do
52139ea7df53SJan Kara 	 * journal_start/journal_stop which can block and take a long time
5214a827eaffSAneesh Kumar K.V 	 */
52152e9ee850SAneesh Kumar K.V 	if (page_has_buffers(page)) {
5216f19d5870STao Ma 		if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5217f19d5870STao Ma 					    0, len, NULL,
5218a827eaffSAneesh Kumar K.V 					    ext4_bh_unmapped)) {
52199ea7df53SJan Kara 			/* Wait so that we don't change page under IO */
52201d1d1a76SDarrick J. Wong 			wait_for_stable_page(page);
52219ea7df53SJan Kara 			ret = VM_FAULT_LOCKED;
52229ea7df53SJan Kara 			goto out;
52232e9ee850SAneesh Kumar K.V 		}
5224a827eaffSAneesh Kumar K.V 	}
5225a827eaffSAneesh Kumar K.V 	unlock_page(page);
52269ea7df53SJan Kara 	/* OK, we need to fill the hole... */
52279ea7df53SJan Kara 	if (ext4_should_dioread_nolock(inode))
52289ea7df53SJan Kara 		get_block = ext4_get_block_write;
52299ea7df53SJan Kara 	else
52309ea7df53SJan Kara 		get_block = ext4_get_block;
52319ea7df53SJan Kara retry_alloc:
52329924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
52339924a92aSTheodore Ts'o 				    ext4_writepage_trans_blocks(inode));
52349ea7df53SJan Kara 	if (IS_ERR(handle)) {
5235c2ec175cSNick Piggin 		ret = VM_FAULT_SIGBUS;
52369ea7df53SJan Kara 		goto out;
52379ea7df53SJan Kara 	}
52389ea7df53SJan Kara 	ret = __block_page_mkwrite(vma, vmf, get_block);
52399ea7df53SJan Kara 	if (!ret && ext4_should_journal_data(inode)) {
5240f19d5870STao Ma 		if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
52419ea7df53SJan Kara 			  PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
52429ea7df53SJan Kara 			unlock_page(page);
52439ea7df53SJan Kara 			ret = VM_FAULT_SIGBUS;
5244fcbb5515SYongqiang Yang 			ext4_journal_stop(handle);
52459ea7df53SJan Kara 			goto out;
52469ea7df53SJan Kara 		}
52479ea7df53SJan Kara 		ext4_set_inode_state(inode, EXT4_STATE_JDATA);
52489ea7df53SJan Kara 	}
52499ea7df53SJan Kara 	ext4_journal_stop(handle);
52509ea7df53SJan Kara 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
52519ea7df53SJan Kara 		goto retry_alloc;
52529ea7df53SJan Kara out_ret:
52539ea7df53SJan Kara 	ret = block_page_mkwrite_return(ret);
52549ea7df53SJan Kara out:
52558e8ad8a5SJan Kara 	sb_end_pagefault(inode->i_sb);
52562e9ee850SAneesh Kumar K.V 	return ret;
52572e9ee850SAneesh Kumar K.V }
5258