xref: /openbmc/linux/fs/ext4/inode.c (revision a30d542a0035b886ffaafd0057ced0a2b28c3a4f)
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  *  Goal-directed block allocation by Stephen Tweedie
16ac27a0ecSDave Kleikamp  *	(sct@redhat.com), 1993, 1998
17ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
18ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
19ac27a0ecSDave Kleikamp  *  64-bit file support on 64-bit platforms by Jakub Jelinek
20ac27a0ecSDave Kleikamp  *	(jj@sunsite.ms.mff.cuni.cz)
21ac27a0ecSDave Kleikamp  *
22617ba13bSMingming Cao  *  Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
23ac27a0ecSDave Kleikamp  */
24ac27a0ecSDave Kleikamp 
25ac27a0ecSDave Kleikamp #include <linux/module.h>
26ac27a0ecSDave Kleikamp #include <linux/fs.h>
27ac27a0ecSDave Kleikamp #include <linux/time.h>
28dab291afSMingming Cao #include <linux/jbd2.h>
29ac27a0ecSDave Kleikamp #include <linux/highuid.h>
30ac27a0ecSDave Kleikamp #include <linux/pagemap.h>
31ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
32ac27a0ecSDave Kleikamp #include <linux/string.h>
33ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
34ac27a0ecSDave Kleikamp #include <linux/writeback.h>
3564769240SAlex Tomas #include <linux/pagevec.h>
36ac27a0ecSDave Kleikamp #include <linux/mpage.h>
37ac27a0ecSDave Kleikamp #include <linux/uio.h>
38ac27a0ecSDave Kleikamp #include <linux/bio.h>
393dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
40ac27a0ecSDave Kleikamp #include "xattr.h"
41ac27a0ecSDave Kleikamp #include "acl.h"
42d2a17637SMingming Cao #include "ext4_extents.h"
43ac27a0ecSDave Kleikamp 
44a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01
45a1d6cc56SAneesh Kumar K.V 
46678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode,
47678aaf48SJan Kara 					      loff_t new_size)
48678aaf48SJan Kara {
49678aaf48SJan Kara 	return jbd2_journal_begin_ordered_truncate(&EXT4_I(inode)->jinode,
50678aaf48SJan Kara 						   new_size);
51678aaf48SJan Kara }
52678aaf48SJan Kara 
5364769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset);
5464769240SAlex Tomas 
55ac27a0ecSDave Kleikamp /*
56ac27a0ecSDave Kleikamp  * Test whether an inode is a fast symlink.
57ac27a0ecSDave Kleikamp  */
58617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode)
59ac27a0ecSDave Kleikamp {
60617ba13bSMingming Cao 	int ea_blocks = EXT4_I(inode)->i_file_acl ?
61ac27a0ecSDave Kleikamp 		(inode->i_sb->s_blocksize >> 9) : 0;
62ac27a0ecSDave Kleikamp 
63ac27a0ecSDave Kleikamp 	return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
64ac27a0ecSDave Kleikamp }
65ac27a0ecSDave Kleikamp 
66ac27a0ecSDave Kleikamp /*
67617ba13bSMingming Cao  * The ext4 forget function must perform a revoke if we are freeing data
68ac27a0ecSDave Kleikamp  * which has been journaled.  Metadata (eg. indirect blocks) must be
69ac27a0ecSDave Kleikamp  * revoked in all cases.
70ac27a0ecSDave Kleikamp  *
71ac27a0ecSDave Kleikamp  * "bh" may be NULL: a metadata block may have been freed from memory
72ac27a0ecSDave Kleikamp  * but there may still be a record of it in the journal, and that record
73ac27a0ecSDave Kleikamp  * still needs to be revoked.
74ac27a0ecSDave Kleikamp  */
75617ba13bSMingming Cao int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
76617ba13bSMingming Cao 			struct buffer_head *bh, ext4_fsblk_t blocknr)
77ac27a0ecSDave Kleikamp {
78ac27a0ecSDave Kleikamp 	int err;
79ac27a0ecSDave Kleikamp 
80ac27a0ecSDave Kleikamp 	might_sleep();
81ac27a0ecSDave Kleikamp 
82ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh, "enter");
83ac27a0ecSDave Kleikamp 
84ac27a0ecSDave Kleikamp 	jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
85ac27a0ecSDave Kleikamp 		  "data mode %lx\n",
86ac27a0ecSDave Kleikamp 		  bh, is_metadata, inode->i_mode,
87ac27a0ecSDave Kleikamp 		  test_opt(inode->i_sb, DATA_FLAGS));
88ac27a0ecSDave Kleikamp 
89ac27a0ecSDave Kleikamp 	/* Never use the revoke function if we are doing full data
90ac27a0ecSDave Kleikamp 	 * journaling: there is no need to, and a V1 superblock won't
91ac27a0ecSDave Kleikamp 	 * support it.  Otherwise, only skip the revoke on un-journaled
92ac27a0ecSDave Kleikamp 	 * data blocks. */
93ac27a0ecSDave Kleikamp 
94617ba13bSMingming Cao 	if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
95617ba13bSMingming Cao 	    (!is_metadata && !ext4_should_journal_data(inode))) {
96ac27a0ecSDave Kleikamp 		if (bh) {
97dab291afSMingming Cao 			BUFFER_TRACE(bh, "call jbd2_journal_forget");
98617ba13bSMingming Cao 			return ext4_journal_forget(handle, bh);
99ac27a0ecSDave Kleikamp 		}
100ac27a0ecSDave Kleikamp 		return 0;
101ac27a0ecSDave Kleikamp 	}
102ac27a0ecSDave Kleikamp 
103ac27a0ecSDave Kleikamp 	/*
104ac27a0ecSDave Kleikamp 	 * data!=journal && (is_metadata || should_journal_data(inode))
105ac27a0ecSDave Kleikamp 	 */
106617ba13bSMingming Cao 	BUFFER_TRACE(bh, "call ext4_journal_revoke");
107617ba13bSMingming Cao 	err = ext4_journal_revoke(handle, blocknr, bh);
108ac27a0ecSDave Kleikamp 	if (err)
10946e665e9SHarvey Harrison 		ext4_abort(inode->i_sb, __func__,
110ac27a0ecSDave Kleikamp 			   "error %d when attempting revoke", err);
111ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh, "exit");
112ac27a0ecSDave Kleikamp 	return err;
113ac27a0ecSDave Kleikamp }
114ac27a0ecSDave Kleikamp 
115ac27a0ecSDave Kleikamp /*
116ac27a0ecSDave Kleikamp  * Work out how many blocks we need to proceed with the next chunk of a
117ac27a0ecSDave Kleikamp  * truncate transaction.
118ac27a0ecSDave Kleikamp  */
119ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode)
120ac27a0ecSDave Kleikamp {
121725d26d3SAneesh Kumar K.V 	ext4_lblk_t needed;
122ac27a0ecSDave Kleikamp 
123ac27a0ecSDave Kleikamp 	needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
124ac27a0ecSDave Kleikamp 
125ac27a0ecSDave Kleikamp 	/* Give ourselves just enough room to cope with inodes in which
126ac27a0ecSDave Kleikamp 	 * i_blocks is corrupt: we've seen disk corruptions in the past
127ac27a0ecSDave Kleikamp 	 * which resulted in random data in an inode which looked enough
128617ba13bSMingming Cao 	 * like a regular file for ext4 to try to delete it.  Things
129ac27a0ecSDave Kleikamp 	 * will go a bit crazy if that happens, but at least we should
130ac27a0ecSDave Kleikamp 	 * try not to panic the whole kernel. */
131ac27a0ecSDave Kleikamp 	if (needed < 2)
132ac27a0ecSDave Kleikamp 		needed = 2;
133ac27a0ecSDave Kleikamp 
134ac27a0ecSDave Kleikamp 	/* But we need to bound the transaction so we don't overflow the
135ac27a0ecSDave Kleikamp 	 * journal. */
136617ba13bSMingming Cao 	if (needed > EXT4_MAX_TRANS_DATA)
137617ba13bSMingming Cao 		needed = EXT4_MAX_TRANS_DATA;
138ac27a0ecSDave Kleikamp 
139617ba13bSMingming Cao 	return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
140ac27a0ecSDave Kleikamp }
141ac27a0ecSDave Kleikamp 
142ac27a0ecSDave Kleikamp /*
143ac27a0ecSDave Kleikamp  * Truncate transactions can be complex and absolutely huge.  So we need to
144ac27a0ecSDave Kleikamp  * be able to restart the transaction at a conventient checkpoint to make
145ac27a0ecSDave Kleikamp  * sure we don't overflow the journal.
146ac27a0ecSDave Kleikamp  *
147ac27a0ecSDave Kleikamp  * start_transaction gets us a new handle for a truncate transaction,
148ac27a0ecSDave Kleikamp  * and extend_transaction tries to extend the existing one a bit.  If
149ac27a0ecSDave Kleikamp  * extend fails, we need to propagate the failure up and restart the
150ac27a0ecSDave Kleikamp  * transaction in the top-level truncate loop. --sct
151ac27a0ecSDave Kleikamp  */
152ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode)
153ac27a0ecSDave Kleikamp {
154ac27a0ecSDave Kleikamp 	handle_t *result;
155ac27a0ecSDave Kleikamp 
156617ba13bSMingming Cao 	result = ext4_journal_start(inode, blocks_for_truncate(inode));
157ac27a0ecSDave Kleikamp 	if (!IS_ERR(result))
158ac27a0ecSDave Kleikamp 		return result;
159ac27a0ecSDave Kleikamp 
160617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, PTR_ERR(result));
161ac27a0ecSDave Kleikamp 	return result;
162ac27a0ecSDave Kleikamp }
163ac27a0ecSDave Kleikamp 
164ac27a0ecSDave Kleikamp /*
165ac27a0ecSDave Kleikamp  * Try to extend this transaction for the purposes of truncation.
166ac27a0ecSDave Kleikamp  *
167ac27a0ecSDave Kleikamp  * Returns 0 if we managed to create more room.  If we can't create more
168ac27a0ecSDave Kleikamp  * room, and the transaction must be restarted we return 1.
169ac27a0ecSDave Kleikamp  */
170ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
171ac27a0ecSDave Kleikamp {
172617ba13bSMingming Cao 	if (handle->h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS)
173ac27a0ecSDave Kleikamp 		return 0;
174617ba13bSMingming Cao 	if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
175ac27a0ecSDave Kleikamp 		return 0;
176ac27a0ecSDave Kleikamp 	return 1;
177ac27a0ecSDave Kleikamp }
178ac27a0ecSDave Kleikamp 
179ac27a0ecSDave Kleikamp /*
180ac27a0ecSDave Kleikamp  * Restart the transaction associated with *handle.  This does a commit,
181ac27a0ecSDave Kleikamp  * so before we call here everything must be consistently dirtied against
182ac27a0ecSDave Kleikamp  * this transaction.
183ac27a0ecSDave Kleikamp  */
184617ba13bSMingming Cao static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
185ac27a0ecSDave Kleikamp {
186ac27a0ecSDave Kleikamp 	jbd_debug(2, "restarting handle %p\n", handle);
187617ba13bSMingming Cao 	return ext4_journal_restart(handle, blocks_for_truncate(inode));
188ac27a0ecSDave Kleikamp }
189ac27a0ecSDave Kleikamp 
190ac27a0ecSDave Kleikamp /*
191ac27a0ecSDave Kleikamp  * Called at the last iput() if i_nlink is zero.
192ac27a0ecSDave Kleikamp  */
193617ba13bSMingming Cao void ext4_delete_inode(struct inode *inode)
194ac27a0ecSDave Kleikamp {
195ac27a0ecSDave Kleikamp 	handle_t *handle;
196bc965ab3STheodore Ts'o 	int err;
197ac27a0ecSDave Kleikamp 
198678aaf48SJan Kara 	if (ext4_should_order_data(inode))
199678aaf48SJan Kara 		ext4_begin_ordered_truncate(inode, 0);
200ac27a0ecSDave Kleikamp 	truncate_inode_pages(&inode->i_data, 0);
201ac27a0ecSDave Kleikamp 
202ac27a0ecSDave Kleikamp 	if (is_bad_inode(inode))
203ac27a0ecSDave Kleikamp 		goto no_delete;
204ac27a0ecSDave Kleikamp 
205bc965ab3STheodore Ts'o 	handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
206ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
207bc965ab3STheodore Ts'o 		ext4_std_error(inode->i_sb, PTR_ERR(handle));
208ac27a0ecSDave Kleikamp 		/*
209ac27a0ecSDave Kleikamp 		 * If we're going to skip the normal cleanup, we still need to
210ac27a0ecSDave Kleikamp 		 * make sure that the in-core orphan linked list is properly
211ac27a0ecSDave Kleikamp 		 * cleaned up.
212ac27a0ecSDave Kleikamp 		 */
213617ba13bSMingming Cao 		ext4_orphan_del(NULL, inode);
214ac27a0ecSDave Kleikamp 		goto no_delete;
215ac27a0ecSDave Kleikamp 	}
216ac27a0ecSDave Kleikamp 
217ac27a0ecSDave Kleikamp 	if (IS_SYNC(inode))
218ac27a0ecSDave Kleikamp 		handle->h_sync = 1;
219ac27a0ecSDave Kleikamp 	inode->i_size = 0;
220bc965ab3STheodore Ts'o 	err = ext4_mark_inode_dirty(handle, inode);
221bc965ab3STheodore Ts'o 	if (err) {
222bc965ab3STheodore Ts'o 		ext4_warning(inode->i_sb, __func__,
223bc965ab3STheodore Ts'o 			     "couldn't mark inode dirty (err %d)", err);
224bc965ab3STheodore Ts'o 		goto stop_handle;
225bc965ab3STheodore Ts'o 	}
226ac27a0ecSDave Kleikamp 	if (inode->i_blocks)
227617ba13bSMingming Cao 		ext4_truncate(inode);
228bc965ab3STheodore Ts'o 
229bc965ab3STheodore Ts'o 	/*
230bc965ab3STheodore Ts'o 	 * ext4_ext_truncate() doesn't reserve any slop when it
231bc965ab3STheodore Ts'o 	 * restarts journal transactions; therefore there may not be
232bc965ab3STheodore Ts'o 	 * enough credits left in the handle to remove the inode from
233bc965ab3STheodore Ts'o 	 * the orphan list and set the dtime field.
234bc965ab3STheodore Ts'o 	 */
235bc965ab3STheodore Ts'o 	if (handle->h_buffer_credits < 3) {
236bc965ab3STheodore Ts'o 		err = ext4_journal_extend(handle, 3);
237bc965ab3STheodore Ts'o 		if (err > 0)
238bc965ab3STheodore Ts'o 			err = ext4_journal_restart(handle, 3);
239bc965ab3STheodore Ts'o 		if (err != 0) {
240bc965ab3STheodore Ts'o 			ext4_warning(inode->i_sb, __func__,
241bc965ab3STheodore Ts'o 				     "couldn't extend journal (err %d)", err);
242bc965ab3STheodore Ts'o 		stop_handle:
243bc965ab3STheodore Ts'o 			ext4_journal_stop(handle);
244bc965ab3STheodore Ts'o 			goto no_delete;
245bc965ab3STheodore Ts'o 		}
246bc965ab3STheodore Ts'o 	}
247bc965ab3STheodore Ts'o 
248ac27a0ecSDave Kleikamp 	/*
249617ba13bSMingming Cao 	 * Kill off the orphan record which ext4_truncate created.
250ac27a0ecSDave Kleikamp 	 * AKPM: I think this can be inside the above `if'.
251617ba13bSMingming Cao 	 * Note that ext4_orphan_del() has to be able to cope with the
252ac27a0ecSDave Kleikamp 	 * deletion of a non-existent orphan - this is because we don't
253617ba13bSMingming Cao 	 * know if ext4_truncate() actually created an orphan record.
254ac27a0ecSDave Kleikamp 	 * (Well, we could do this if we need to, but heck - it works)
255ac27a0ecSDave Kleikamp 	 */
256617ba13bSMingming Cao 	ext4_orphan_del(handle, inode);
257617ba13bSMingming Cao 	EXT4_I(inode)->i_dtime	= get_seconds();
258ac27a0ecSDave Kleikamp 
259ac27a0ecSDave Kleikamp 	/*
260ac27a0ecSDave Kleikamp 	 * One subtle ordering requirement: if anything has gone wrong
261ac27a0ecSDave Kleikamp 	 * (transaction abort, IO errors, whatever), then we can still
262ac27a0ecSDave Kleikamp 	 * do these next steps (the fs will already have been marked as
263ac27a0ecSDave Kleikamp 	 * having errors), but we can't free the inode if the mark_dirty
264ac27a0ecSDave Kleikamp 	 * fails.
265ac27a0ecSDave Kleikamp 	 */
266617ba13bSMingming Cao 	if (ext4_mark_inode_dirty(handle, inode))
267ac27a0ecSDave Kleikamp 		/* If that failed, just do the required in-core inode clear. */
268ac27a0ecSDave Kleikamp 		clear_inode(inode);
269ac27a0ecSDave Kleikamp 	else
270617ba13bSMingming Cao 		ext4_free_inode(handle, inode);
271617ba13bSMingming Cao 	ext4_journal_stop(handle);
272ac27a0ecSDave Kleikamp 	return;
273ac27a0ecSDave Kleikamp no_delete:
274ac27a0ecSDave Kleikamp 	clear_inode(inode);	/* We must guarantee clearing of inode... */
275ac27a0ecSDave Kleikamp }
276ac27a0ecSDave Kleikamp 
277ac27a0ecSDave Kleikamp typedef struct {
278ac27a0ecSDave Kleikamp 	__le32	*p;
279ac27a0ecSDave Kleikamp 	__le32	key;
280ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
281ac27a0ecSDave Kleikamp } Indirect;
282ac27a0ecSDave Kleikamp 
283ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
284ac27a0ecSDave Kleikamp {
285ac27a0ecSDave Kleikamp 	p->key = *(p->p = v);
286ac27a0ecSDave Kleikamp 	p->bh = bh;
287ac27a0ecSDave Kleikamp }
288ac27a0ecSDave Kleikamp 
289ac27a0ecSDave Kleikamp /**
290617ba13bSMingming Cao  *	ext4_block_to_path - parse the block number into array of offsets
291ac27a0ecSDave Kleikamp  *	@inode: inode in question (we are only interested in its superblock)
292ac27a0ecSDave Kleikamp  *	@i_block: block number to be parsed
293ac27a0ecSDave Kleikamp  *	@offsets: array to store the offsets in
294ac27a0ecSDave Kleikamp  *	@boundary: set this non-zero if the referred-to block is likely to be
295ac27a0ecSDave Kleikamp  *	       followed (on disk) by an indirect block.
296ac27a0ecSDave Kleikamp  *
297617ba13bSMingming Cao  *	To store the locations of file's data ext4 uses a data structure common
298ac27a0ecSDave Kleikamp  *	for UNIX filesystems - tree of pointers anchored in the inode, with
299ac27a0ecSDave Kleikamp  *	data blocks at leaves and indirect blocks in intermediate nodes.
300ac27a0ecSDave Kleikamp  *	This function translates the block number into path in that tree -
301ac27a0ecSDave Kleikamp  *	return value is the path length and @offsets[n] is the offset of
302ac27a0ecSDave Kleikamp  *	pointer to (n+1)th node in the nth one. If @block is out of range
303ac27a0ecSDave Kleikamp  *	(negative or too large) warning is printed and zero returned.
304ac27a0ecSDave Kleikamp  *
305ac27a0ecSDave Kleikamp  *	Note: function doesn't find node addresses, so no IO is needed. All
306ac27a0ecSDave Kleikamp  *	we need to know is the capacity of indirect blocks (taken from the
307ac27a0ecSDave Kleikamp  *	inode->i_sb).
308ac27a0ecSDave Kleikamp  */
309ac27a0ecSDave Kleikamp 
310ac27a0ecSDave Kleikamp /*
311ac27a0ecSDave Kleikamp  * Portability note: the last comparison (check that we fit into triple
312ac27a0ecSDave Kleikamp  * indirect block) is spelled differently, because otherwise on an
313ac27a0ecSDave Kleikamp  * architecture with 32-bit longs and 8Kb pages we might get into trouble
314ac27a0ecSDave Kleikamp  * if our filesystem had 8Kb blocks. We might use long long, but that would
315ac27a0ecSDave Kleikamp  * kill us on x86. Oh, well, at least the sign propagation does not matter -
316ac27a0ecSDave Kleikamp  * i_block would have to be negative in the very beginning, so we would not
317ac27a0ecSDave Kleikamp  * get there at all.
318ac27a0ecSDave Kleikamp  */
319ac27a0ecSDave Kleikamp 
320617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode,
321725d26d3SAneesh Kumar K.V 			ext4_lblk_t i_block,
322725d26d3SAneesh Kumar K.V 			ext4_lblk_t offsets[4], int *boundary)
323ac27a0ecSDave Kleikamp {
324617ba13bSMingming Cao 	int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
325617ba13bSMingming Cao 	int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
326617ba13bSMingming Cao 	const long direct_blocks = EXT4_NDIR_BLOCKS,
327ac27a0ecSDave Kleikamp 		indirect_blocks = ptrs,
328ac27a0ecSDave Kleikamp 		double_blocks = (1 << (ptrs_bits * 2));
329ac27a0ecSDave Kleikamp 	int n = 0;
330ac27a0ecSDave Kleikamp 	int final = 0;
331ac27a0ecSDave Kleikamp 
332ac27a0ecSDave Kleikamp 	if (i_block < 0) {
333617ba13bSMingming Cao 		ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
334ac27a0ecSDave Kleikamp 	} else if (i_block < direct_blocks) {
335ac27a0ecSDave Kleikamp 		offsets[n++] = i_block;
336ac27a0ecSDave Kleikamp 		final = direct_blocks;
337ac27a0ecSDave Kleikamp 	} else if ((i_block -= direct_blocks) < indirect_blocks) {
338617ba13bSMingming Cao 		offsets[n++] = EXT4_IND_BLOCK;
339ac27a0ecSDave Kleikamp 		offsets[n++] = i_block;
340ac27a0ecSDave Kleikamp 		final = ptrs;
341ac27a0ecSDave Kleikamp 	} else if ((i_block -= indirect_blocks) < double_blocks) {
342617ba13bSMingming Cao 		offsets[n++] = EXT4_DIND_BLOCK;
343ac27a0ecSDave Kleikamp 		offsets[n++] = i_block >> ptrs_bits;
344ac27a0ecSDave Kleikamp 		offsets[n++] = i_block & (ptrs - 1);
345ac27a0ecSDave Kleikamp 		final = ptrs;
346ac27a0ecSDave Kleikamp 	} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
347617ba13bSMingming Cao 		offsets[n++] = EXT4_TIND_BLOCK;
348ac27a0ecSDave Kleikamp 		offsets[n++] = i_block >> (ptrs_bits * 2);
349ac27a0ecSDave Kleikamp 		offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
350ac27a0ecSDave Kleikamp 		offsets[n++] = i_block & (ptrs - 1);
351ac27a0ecSDave Kleikamp 		final = ptrs;
352ac27a0ecSDave Kleikamp 	} else {
353e2b46574SEric Sandeen 		ext4_warning(inode->i_sb, "ext4_block_to_path",
3540e855ac8SAneesh Kumar K.V 				"block %lu > max",
355e2b46574SEric Sandeen 				i_block + direct_blocks +
356e2b46574SEric Sandeen 				indirect_blocks + double_blocks);
357ac27a0ecSDave Kleikamp 	}
358ac27a0ecSDave Kleikamp 	if (boundary)
359ac27a0ecSDave Kleikamp 		*boundary = final - 1 - (i_block & (ptrs - 1));
360ac27a0ecSDave Kleikamp 	return n;
361ac27a0ecSDave Kleikamp }
362ac27a0ecSDave Kleikamp 
363ac27a0ecSDave Kleikamp /**
364617ba13bSMingming Cao  *	ext4_get_branch - read the chain of indirect blocks leading to data
365ac27a0ecSDave Kleikamp  *	@inode: inode in question
366ac27a0ecSDave Kleikamp  *	@depth: depth of the chain (1 - direct pointer, etc.)
367ac27a0ecSDave Kleikamp  *	@offsets: offsets of pointers in inode/indirect blocks
368ac27a0ecSDave Kleikamp  *	@chain: place to store the result
369ac27a0ecSDave Kleikamp  *	@err: here we store the error value
370ac27a0ecSDave Kleikamp  *
371ac27a0ecSDave Kleikamp  *	Function fills the array of triples <key, p, bh> and returns %NULL
372ac27a0ecSDave Kleikamp  *	if everything went OK or the pointer to the last filled triple
373ac27a0ecSDave Kleikamp  *	(incomplete one) otherwise. Upon the return chain[i].key contains
374ac27a0ecSDave Kleikamp  *	the number of (i+1)-th block in the chain (as it is stored in memory,
375ac27a0ecSDave Kleikamp  *	i.e. little-endian 32-bit), chain[i].p contains the address of that
376ac27a0ecSDave Kleikamp  *	number (it points into struct inode for i==0 and into the bh->b_data
377ac27a0ecSDave Kleikamp  *	for i>0) and chain[i].bh points to the buffer_head of i-th indirect
378ac27a0ecSDave Kleikamp  *	block for i>0 and NULL for i==0. In other words, it holds the block
379ac27a0ecSDave Kleikamp  *	numbers of the chain, addresses they were taken from (and where we can
380ac27a0ecSDave Kleikamp  *	verify that chain did not change) and buffer_heads hosting these
381ac27a0ecSDave Kleikamp  *	numbers.
382ac27a0ecSDave Kleikamp  *
383ac27a0ecSDave Kleikamp  *	Function stops when it stumbles upon zero pointer (absent block)
384ac27a0ecSDave Kleikamp  *		(pointer to last triple returned, *@err == 0)
385ac27a0ecSDave Kleikamp  *	or when it gets an IO error reading an indirect block
386ac27a0ecSDave Kleikamp  *		(ditto, *@err == -EIO)
387ac27a0ecSDave Kleikamp  *	or when it reads all @depth-1 indirect blocks successfully and finds
388ac27a0ecSDave Kleikamp  *	the whole chain, all way to the data (returns %NULL, *err == 0).
389c278bfecSAneesh Kumar K.V  *
390c278bfecSAneesh Kumar K.V  *      Need to be called with
3910e855ac8SAneesh Kumar K.V  *      down_read(&EXT4_I(inode)->i_data_sem)
392ac27a0ecSDave Kleikamp  */
393725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth,
394725d26d3SAneesh Kumar K.V 				 ext4_lblk_t  *offsets,
395ac27a0ecSDave Kleikamp 				 Indirect chain[4], int *err)
396ac27a0ecSDave Kleikamp {
397ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
398ac27a0ecSDave Kleikamp 	Indirect *p = chain;
399ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
400ac27a0ecSDave Kleikamp 
401ac27a0ecSDave Kleikamp 	*err = 0;
402ac27a0ecSDave Kleikamp 	/* i_data is not going away, no lock needed */
403617ba13bSMingming Cao 	add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
404ac27a0ecSDave Kleikamp 	if (!p->key)
405ac27a0ecSDave Kleikamp 		goto no_block;
406ac27a0ecSDave Kleikamp 	while (--depth) {
407ac27a0ecSDave Kleikamp 		bh = sb_bread(sb, le32_to_cpu(p->key));
408ac27a0ecSDave Kleikamp 		if (!bh)
409ac27a0ecSDave Kleikamp 			goto failure;
410ac27a0ecSDave Kleikamp 		add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
411ac27a0ecSDave Kleikamp 		/* Reader: end */
412ac27a0ecSDave Kleikamp 		if (!p->key)
413ac27a0ecSDave Kleikamp 			goto no_block;
414ac27a0ecSDave Kleikamp 	}
415ac27a0ecSDave Kleikamp 	return NULL;
416ac27a0ecSDave Kleikamp 
417ac27a0ecSDave Kleikamp failure:
418ac27a0ecSDave Kleikamp 	*err = -EIO;
419ac27a0ecSDave Kleikamp no_block:
420ac27a0ecSDave Kleikamp 	return p;
421ac27a0ecSDave Kleikamp }
422ac27a0ecSDave Kleikamp 
423ac27a0ecSDave Kleikamp /**
424617ba13bSMingming Cao  *	ext4_find_near - find a place for allocation with sufficient locality
425ac27a0ecSDave Kleikamp  *	@inode: owner
426ac27a0ecSDave Kleikamp  *	@ind: descriptor of indirect block.
427ac27a0ecSDave Kleikamp  *
4281cc8dcf5SBenoit Boissinot  *	This function returns the preferred place for block allocation.
429ac27a0ecSDave Kleikamp  *	It is used when heuristic for sequential allocation fails.
430ac27a0ecSDave Kleikamp  *	Rules are:
431ac27a0ecSDave Kleikamp  *	  + if there is a block to the left of our position - allocate near it.
432ac27a0ecSDave Kleikamp  *	  + if pointer will live in indirect block - allocate near that block.
433ac27a0ecSDave Kleikamp  *	  + if pointer will live in inode - allocate in the same
434ac27a0ecSDave Kleikamp  *	    cylinder group.
435ac27a0ecSDave Kleikamp  *
436ac27a0ecSDave Kleikamp  * In the latter case we colour the starting block by the callers PID to
437ac27a0ecSDave Kleikamp  * prevent it from clashing with concurrent allocations for a different inode
438ac27a0ecSDave Kleikamp  * in the same block group.   The PID is used here so that functionally related
439ac27a0ecSDave Kleikamp  * files will be close-by on-disk.
440ac27a0ecSDave Kleikamp  *
441ac27a0ecSDave Kleikamp  *	Caller must make sure that @ind is valid and will stay that way.
442ac27a0ecSDave Kleikamp  */
443617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
444ac27a0ecSDave Kleikamp {
445617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
446ac27a0ecSDave Kleikamp 	__le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
447ac27a0ecSDave Kleikamp 	__le32 *p;
448617ba13bSMingming Cao 	ext4_fsblk_t bg_start;
44974d3487fSValerie Clement 	ext4_fsblk_t last_block;
450617ba13bSMingming Cao 	ext4_grpblk_t colour;
451ac27a0ecSDave Kleikamp 
452ac27a0ecSDave Kleikamp 	/* Try to find previous block */
453ac27a0ecSDave Kleikamp 	for (p = ind->p - 1; p >= start; p--) {
454ac27a0ecSDave Kleikamp 		if (*p)
455ac27a0ecSDave Kleikamp 			return le32_to_cpu(*p);
456ac27a0ecSDave Kleikamp 	}
457ac27a0ecSDave Kleikamp 
458ac27a0ecSDave Kleikamp 	/* No such thing, so let's try location of indirect block */
459ac27a0ecSDave Kleikamp 	if (ind->bh)
460ac27a0ecSDave Kleikamp 		return ind->bh->b_blocknr;
461ac27a0ecSDave Kleikamp 
462ac27a0ecSDave Kleikamp 	/*
463ac27a0ecSDave Kleikamp 	 * It is going to be referred to from the inode itself? OK, just put it
464ac27a0ecSDave Kleikamp 	 * into the same cylinder group then.
465ac27a0ecSDave Kleikamp 	 */
466617ba13bSMingming Cao 	bg_start = ext4_group_first_block_no(inode->i_sb, ei->i_block_group);
46774d3487fSValerie Clement 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
46874d3487fSValerie Clement 
46974d3487fSValerie Clement 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
470ac27a0ecSDave Kleikamp 		colour = (current->pid % 16) *
471617ba13bSMingming Cao 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
47274d3487fSValerie Clement 	else
47374d3487fSValerie Clement 		colour = (current->pid % 16) * ((last_block - bg_start) / 16);
474ac27a0ecSDave Kleikamp 	return bg_start + colour;
475ac27a0ecSDave Kleikamp }
476ac27a0ecSDave Kleikamp 
477ac27a0ecSDave Kleikamp /**
4781cc8dcf5SBenoit Boissinot  *	ext4_find_goal - find a preferred place for allocation.
479ac27a0ecSDave Kleikamp  *	@inode: owner
480ac27a0ecSDave Kleikamp  *	@block:  block we want
481ac27a0ecSDave Kleikamp  *	@partial: pointer to the last triple within a chain
482ac27a0ecSDave Kleikamp  *
4831cc8dcf5SBenoit Boissinot  *	Normally this function find the preferred place for block allocation,
484fb01bfdaSAkinobu Mita  *	returns it.
485ac27a0ecSDave Kleikamp  */
486725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
487fb01bfdaSAkinobu Mita 		Indirect *partial)
488ac27a0ecSDave Kleikamp {
489617ba13bSMingming Cao 	struct ext4_block_alloc_info *block_i;
490ac27a0ecSDave Kleikamp 
491617ba13bSMingming Cao 	block_i =  EXT4_I(inode)->i_block_alloc_info;
492ac27a0ecSDave Kleikamp 
493ac27a0ecSDave Kleikamp 	/*
494ac27a0ecSDave Kleikamp 	 * try the heuristic for sequential allocation,
495ac27a0ecSDave Kleikamp 	 * failing that at least try to get decent locality.
496ac27a0ecSDave Kleikamp 	 */
497ac27a0ecSDave Kleikamp 	if (block_i && (block == block_i->last_alloc_logical_block + 1)
498ac27a0ecSDave Kleikamp 		&& (block_i->last_alloc_physical_block != 0)) {
499ac27a0ecSDave Kleikamp 		return block_i->last_alloc_physical_block + 1;
500ac27a0ecSDave Kleikamp 	}
501ac27a0ecSDave Kleikamp 
502617ba13bSMingming Cao 	return ext4_find_near(inode, partial);
503ac27a0ecSDave Kleikamp }
504ac27a0ecSDave Kleikamp 
505ac27a0ecSDave Kleikamp /**
506617ba13bSMingming Cao  *	ext4_blks_to_allocate: Look up the block map and count the number
507ac27a0ecSDave Kleikamp  *	of direct blocks need to be allocated for the given branch.
508ac27a0ecSDave Kleikamp  *
509ac27a0ecSDave Kleikamp  *	@branch: chain of indirect blocks
510ac27a0ecSDave Kleikamp  *	@k: number of blocks need for indirect blocks
511ac27a0ecSDave Kleikamp  *	@blks: number of data blocks to be mapped.
512ac27a0ecSDave Kleikamp  *	@blocks_to_boundary:  the offset in the indirect block
513ac27a0ecSDave Kleikamp  *
514ac27a0ecSDave Kleikamp  *	return the total number of blocks to be allocate, including the
515ac27a0ecSDave Kleikamp  *	direct and indirect blocks.
516ac27a0ecSDave Kleikamp  */
517617ba13bSMingming Cao static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
518ac27a0ecSDave Kleikamp 		int blocks_to_boundary)
519ac27a0ecSDave Kleikamp {
520ac27a0ecSDave Kleikamp 	unsigned long count = 0;
521ac27a0ecSDave Kleikamp 
522ac27a0ecSDave Kleikamp 	/*
523ac27a0ecSDave Kleikamp 	 * Simple case, [t,d]Indirect block(s) has not allocated yet
524ac27a0ecSDave Kleikamp 	 * then it's clear blocks on that path have not allocated
525ac27a0ecSDave Kleikamp 	 */
526ac27a0ecSDave Kleikamp 	if (k > 0) {
527ac27a0ecSDave Kleikamp 		/* right now we don't handle cross boundary allocation */
528ac27a0ecSDave Kleikamp 		if (blks < blocks_to_boundary + 1)
529ac27a0ecSDave Kleikamp 			count += blks;
530ac27a0ecSDave Kleikamp 		else
531ac27a0ecSDave Kleikamp 			count += blocks_to_boundary + 1;
532ac27a0ecSDave Kleikamp 		return count;
533ac27a0ecSDave Kleikamp 	}
534ac27a0ecSDave Kleikamp 
535ac27a0ecSDave Kleikamp 	count++;
536ac27a0ecSDave Kleikamp 	while (count < blks && count <= blocks_to_boundary &&
537ac27a0ecSDave Kleikamp 		le32_to_cpu(*(branch[0].p + count)) == 0) {
538ac27a0ecSDave Kleikamp 		count++;
539ac27a0ecSDave Kleikamp 	}
540ac27a0ecSDave Kleikamp 	return count;
541ac27a0ecSDave Kleikamp }
542ac27a0ecSDave Kleikamp 
543ac27a0ecSDave Kleikamp /**
544617ba13bSMingming Cao  *	ext4_alloc_blocks: multiple allocate blocks needed for a branch
545ac27a0ecSDave Kleikamp  *	@indirect_blks: the number of blocks need to allocate for indirect
546ac27a0ecSDave Kleikamp  *			blocks
547ac27a0ecSDave Kleikamp  *
548ac27a0ecSDave Kleikamp  *	@new_blocks: on return it will store the new block numbers for
549ac27a0ecSDave Kleikamp  *	the indirect blocks(if needed) and the first direct block,
550ac27a0ecSDave Kleikamp  *	@blks:	on return it will store the total number of allocated
551ac27a0ecSDave Kleikamp  *		direct blocks
552ac27a0ecSDave Kleikamp  */
553617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
5547061eba7SAneesh Kumar K.V 				ext4_lblk_t iblock, ext4_fsblk_t goal,
5557061eba7SAneesh Kumar K.V 				int indirect_blks, int blks,
556617ba13bSMingming Cao 				ext4_fsblk_t new_blocks[4], int *err)
557ac27a0ecSDave Kleikamp {
558ac27a0ecSDave Kleikamp 	int target, i;
5597061eba7SAneesh Kumar K.V 	unsigned long count = 0, blk_allocated = 0;
560ac27a0ecSDave Kleikamp 	int index = 0;
561617ba13bSMingming Cao 	ext4_fsblk_t current_block = 0;
562ac27a0ecSDave Kleikamp 	int ret = 0;
563ac27a0ecSDave Kleikamp 
564ac27a0ecSDave Kleikamp 	/*
565ac27a0ecSDave Kleikamp 	 * Here we try to allocate the requested multiple blocks at once,
566ac27a0ecSDave Kleikamp 	 * on a best-effort basis.
567ac27a0ecSDave Kleikamp 	 * To build a branch, we should allocate blocks for
568ac27a0ecSDave Kleikamp 	 * the indirect blocks(if not allocated yet), and at least
569ac27a0ecSDave Kleikamp 	 * the first direct block of this branch.  That's the
570ac27a0ecSDave Kleikamp 	 * minimum number of blocks need to allocate(required)
571ac27a0ecSDave Kleikamp 	 */
5727061eba7SAneesh Kumar K.V 	/* first we try to allocate the indirect blocks */
5737061eba7SAneesh Kumar K.V 	target = indirect_blks;
5747061eba7SAneesh Kumar K.V 	while (target > 0) {
575ac27a0ecSDave Kleikamp 		count = target;
576ac27a0ecSDave Kleikamp 		/* allocating blocks for indirect blocks and direct blocks */
5777061eba7SAneesh Kumar K.V 		current_block = ext4_new_meta_blocks(handle, inode,
5787061eba7SAneesh Kumar K.V 							goal, &count, err);
579ac27a0ecSDave Kleikamp 		if (*err)
580ac27a0ecSDave Kleikamp 			goto failed_out;
581ac27a0ecSDave Kleikamp 
582ac27a0ecSDave Kleikamp 		target -= count;
583ac27a0ecSDave Kleikamp 		/* allocate blocks for indirect blocks */
584ac27a0ecSDave Kleikamp 		while (index < indirect_blks && count) {
585ac27a0ecSDave Kleikamp 			new_blocks[index++] = current_block++;
586ac27a0ecSDave Kleikamp 			count--;
587ac27a0ecSDave Kleikamp 		}
5887061eba7SAneesh Kumar K.V 		if (count > 0) {
5897061eba7SAneesh Kumar K.V 			/*
5907061eba7SAneesh Kumar K.V 			 * save the new block number
5917061eba7SAneesh Kumar K.V 			 * for the first direct block
5927061eba7SAneesh Kumar K.V 			 */
5937061eba7SAneesh Kumar K.V 			new_blocks[index] = current_block;
5947061eba7SAneesh Kumar K.V 			printk(KERN_INFO "%s returned more blocks than "
5957061eba7SAneesh Kumar K.V 						"requested\n", __func__);
5967061eba7SAneesh Kumar K.V 			WARN_ON(1);
597ac27a0ecSDave Kleikamp 			break;
598ac27a0ecSDave Kleikamp 		}
5997061eba7SAneesh Kumar K.V 	}
600ac27a0ecSDave Kleikamp 
6017061eba7SAneesh Kumar K.V 	target = blks - count ;
6027061eba7SAneesh Kumar K.V 	blk_allocated = count;
6037061eba7SAneesh Kumar K.V 	if (!target)
6047061eba7SAneesh Kumar K.V 		goto allocated;
6057061eba7SAneesh Kumar K.V 	/* Now allocate data blocks */
6067061eba7SAneesh Kumar K.V 	count = target;
607654b4908SAneesh Kumar K.V 	/* allocating blocks for data blocks */
6087061eba7SAneesh Kumar K.V 	current_block = ext4_new_blocks(handle, inode, iblock,
6097061eba7SAneesh Kumar K.V 						goal, &count, err);
6107061eba7SAneesh Kumar K.V 	if (*err && (target == blks)) {
6117061eba7SAneesh Kumar K.V 		/*
6127061eba7SAneesh Kumar K.V 		 * if the allocation failed and we didn't allocate
6137061eba7SAneesh Kumar K.V 		 * any blocks before
6147061eba7SAneesh Kumar K.V 		 */
6157061eba7SAneesh Kumar K.V 		goto failed_out;
6167061eba7SAneesh Kumar K.V 	}
6177061eba7SAneesh Kumar K.V 	if (!*err) {
6187061eba7SAneesh Kumar K.V 		if (target == blks) {
6197061eba7SAneesh Kumar K.V 		/*
6207061eba7SAneesh Kumar K.V 		 * save the new block number
6217061eba7SAneesh Kumar K.V 		 * for the first direct block
6227061eba7SAneesh Kumar K.V 		 */
623ac27a0ecSDave Kleikamp 			new_blocks[index] = current_block;
6247061eba7SAneesh Kumar K.V 		}
6257061eba7SAneesh Kumar K.V 		blk_allocated += count;
6267061eba7SAneesh Kumar K.V 	}
6277061eba7SAneesh Kumar K.V allocated:
628ac27a0ecSDave Kleikamp 	/* total number of blocks allocated for direct blocks */
6297061eba7SAneesh Kumar K.V 	ret = blk_allocated;
630ac27a0ecSDave Kleikamp 	*err = 0;
631ac27a0ecSDave Kleikamp 	return ret;
632ac27a0ecSDave Kleikamp failed_out:
633ac27a0ecSDave Kleikamp 	for (i = 0; i < index; i++)
634c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
635ac27a0ecSDave Kleikamp 	return ret;
636ac27a0ecSDave Kleikamp }
637ac27a0ecSDave Kleikamp 
638ac27a0ecSDave Kleikamp /**
639617ba13bSMingming Cao  *	ext4_alloc_branch - allocate and set up a chain of blocks.
640ac27a0ecSDave Kleikamp  *	@inode: owner
641ac27a0ecSDave Kleikamp  *	@indirect_blks: number of allocated indirect blocks
642ac27a0ecSDave Kleikamp  *	@blks: number of allocated direct blocks
643ac27a0ecSDave Kleikamp  *	@offsets: offsets (in the blocks) to store the pointers to next.
644ac27a0ecSDave Kleikamp  *	@branch: place to store the chain in.
645ac27a0ecSDave Kleikamp  *
646ac27a0ecSDave Kleikamp  *	This function allocates blocks, zeroes out all but the last one,
647ac27a0ecSDave Kleikamp  *	links them into chain and (if we are synchronous) writes them to disk.
648ac27a0ecSDave Kleikamp  *	In other words, it prepares a branch that can be spliced onto the
649ac27a0ecSDave Kleikamp  *	inode. It stores the information about that chain in the branch[], in
650617ba13bSMingming Cao  *	the same format as ext4_get_branch() would do. We are calling it after
651ac27a0ecSDave Kleikamp  *	we had read the existing part of chain and partial points to the last
652ac27a0ecSDave Kleikamp  *	triple of that (one with zero ->key). Upon the exit we have the same
653617ba13bSMingming Cao  *	picture as after the successful ext4_get_block(), except that in one
654ac27a0ecSDave Kleikamp  *	place chain is disconnected - *branch->p is still zero (we did not
655ac27a0ecSDave Kleikamp  *	set the last link), but branch->key contains the number that should
656ac27a0ecSDave Kleikamp  *	be placed into *branch->p to fill that gap.
657ac27a0ecSDave Kleikamp  *
658ac27a0ecSDave Kleikamp  *	If allocation fails we free all blocks we've allocated (and forget
659ac27a0ecSDave Kleikamp  *	their buffer_heads) and return the error value the from failed
660617ba13bSMingming Cao  *	ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
661ac27a0ecSDave Kleikamp  *	as described above and return 0.
662ac27a0ecSDave Kleikamp  */
663617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
6647061eba7SAneesh Kumar K.V 				ext4_lblk_t iblock, int indirect_blks,
6657061eba7SAneesh Kumar K.V 				int *blks, ext4_fsblk_t goal,
666725d26d3SAneesh Kumar K.V 				ext4_lblk_t *offsets, Indirect *branch)
667ac27a0ecSDave Kleikamp {
668ac27a0ecSDave Kleikamp 	int blocksize = inode->i_sb->s_blocksize;
669ac27a0ecSDave Kleikamp 	int i, n = 0;
670ac27a0ecSDave Kleikamp 	int err = 0;
671ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
672ac27a0ecSDave Kleikamp 	int num;
673617ba13bSMingming Cao 	ext4_fsblk_t new_blocks[4];
674617ba13bSMingming Cao 	ext4_fsblk_t current_block;
675ac27a0ecSDave Kleikamp 
6767061eba7SAneesh Kumar K.V 	num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
677ac27a0ecSDave Kleikamp 				*blks, new_blocks, &err);
678ac27a0ecSDave Kleikamp 	if (err)
679ac27a0ecSDave Kleikamp 		return err;
680ac27a0ecSDave Kleikamp 
681ac27a0ecSDave Kleikamp 	branch[0].key = cpu_to_le32(new_blocks[0]);
682ac27a0ecSDave Kleikamp 	/*
683ac27a0ecSDave Kleikamp 	 * metadata blocks and data blocks are allocated.
684ac27a0ecSDave Kleikamp 	 */
685ac27a0ecSDave Kleikamp 	for (n = 1; n <= indirect_blks;  n++) {
686ac27a0ecSDave Kleikamp 		/*
687ac27a0ecSDave Kleikamp 		 * Get buffer_head for parent block, zero it out
688ac27a0ecSDave Kleikamp 		 * and set the pointer to new one, then send
689ac27a0ecSDave Kleikamp 		 * parent to disk.
690ac27a0ecSDave Kleikamp 		 */
691ac27a0ecSDave Kleikamp 		bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
692ac27a0ecSDave Kleikamp 		branch[n].bh = bh;
693ac27a0ecSDave Kleikamp 		lock_buffer(bh);
694ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "call get_create_access");
695617ba13bSMingming Cao 		err = ext4_journal_get_create_access(handle, bh);
696ac27a0ecSDave Kleikamp 		if (err) {
697ac27a0ecSDave Kleikamp 			unlock_buffer(bh);
698ac27a0ecSDave Kleikamp 			brelse(bh);
699ac27a0ecSDave Kleikamp 			goto failed;
700ac27a0ecSDave Kleikamp 		}
701ac27a0ecSDave Kleikamp 
702ac27a0ecSDave Kleikamp 		memset(bh->b_data, 0, blocksize);
703ac27a0ecSDave Kleikamp 		branch[n].p = (__le32 *) bh->b_data + offsets[n];
704ac27a0ecSDave Kleikamp 		branch[n].key = cpu_to_le32(new_blocks[n]);
705ac27a0ecSDave Kleikamp 		*branch[n].p = branch[n].key;
706ac27a0ecSDave Kleikamp 		if (n == indirect_blks) {
707ac27a0ecSDave Kleikamp 			current_block = new_blocks[n];
708ac27a0ecSDave Kleikamp 			/*
709ac27a0ecSDave Kleikamp 			 * End of chain, update the last new metablock of
710ac27a0ecSDave Kleikamp 			 * the chain to point to the new allocated
711ac27a0ecSDave Kleikamp 			 * data blocks numbers
712ac27a0ecSDave Kleikamp 			 */
713ac27a0ecSDave Kleikamp 			for (i=1; i < num; i++)
714ac27a0ecSDave Kleikamp 				*(branch[n].p + i) = cpu_to_le32(++current_block);
715ac27a0ecSDave Kleikamp 		}
716ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "marking uptodate");
717ac27a0ecSDave Kleikamp 		set_buffer_uptodate(bh);
718ac27a0ecSDave Kleikamp 		unlock_buffer(bh);
719ac27a0ecSDave Kleikamp 
720617ba13bSMingming Cao 		BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
721617ba13bSMingming Cao 		err = ext4_journal_dirty_metadata(handle, bh);
722ac27a0ecSDave Kleikamp 		if (err)
723ac27a0ecSDave Kleikamp 			goto failed;
724ac27a0ecSDave Kleikamp 	}
725ac27a0ecSDave Kleikamp 	*blks = num;
726ac27a0ecSDave Kleikamp 	return err;
727ac27a0ecSDave Kleikamp failed:
728ac27a0ecSDave Kleikamp 	/* Allocation failed, free what we already allocated */
729ac27a0ecSDave Kleikamp 	for (i = 1; i <= n ; i++) {
730dab291afSMingming Cao 		BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
731617ba13bSMingming Cao 		ext4_journal_forget(handle, branch[i].bh);
732ac27a0ecSDave Kleikamp 	}
733ac27a0ecSDave Kleikamp 	for (i = 0; i < indirect_blks; i++)
734c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
735ac27a0ecSDave Kleikamp 
736c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
737ac27a0ecSDave Kleikamp 
738ac27a0ecSDave Kleikamp 	return err;
739ac27a0ecSDave Kleikamp }
740ac27a0ecSDave Kleikamp 
741ac27a0ecSDave Kleikamp /**
742617ba13bSMingming Cao  * ext4_splice_branch - splice the allocated branch onto inode.
743ac27a0ecSDave Kleikamp  * @inode: owner
744ac27a0ecSDave Kleikamp  * @block: (logical) number of block we are adding
745ac27a0ecSDave Kleikamp  * @chain: chain of indirect blocks (with a missing link - see
746617ba13bSMingming Cao  *	ext4_alloc_branch)
747ac27a0ecSDave Kleikamp  * @where: location of missing link
748ac27a0ecSDave Kleikamp  * @num:   number of indirect blocks we are adding
749ac27a0ecSDave Kleikamp  * @blks:  number of direct blocks we are adding
750ac27a0ecSDave Kleikamp  *
751ac27a0ecSDave Kleikamp  * This function fills the missing link and does all housekeeping needed in
752ac27a0ecSDave Kleikamp  * inode (->i_blocks, etc.). In case of success we end up with the full
753ac27a0ecSDave Kleikamp  * chain to new block and return 0.
754ac27a0ecSDave Kleikamp  */
755617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode,
756725d26d3SAneesh Kumar K.V 			ext4_lblk_t block, Indirect *where, int num, int blks)
757ac27a0ecSDave Kleikamp {
758ac27a0ecSDave Kleikamp 	int i;
759ac27a0ecSDave Kleikamp 	int err = 0;
760617ba13bSMingming Cao 	struct ext4_block_alloc_info *block_i;
761617ba13bSMingming Cao 	ext4_fsblk_t current_block;
762ac27a0ecSDave Kleikamp 
763617ba13bSMingming Cao 	block_i = EXT4_I(inode)->i_block_alloc_info;
764ac27a0ecSDave Kleikamp 	/*
765ac27a0ecSDave Kleikamp 	 * If we're splicing into a [td]indirect block (as opposed to the
766ac27a0ecSDave Kleikamp 	 * inode) then we need to get write access to the [td]indirect block
767ac27a0ecSDave Kleikamp 	 * before the splice.
768ac27a0ecSDave Kleikamp 	 */
769ac27a0ecSDave Kleikamp 	if (where->bh) {
770ac27a0ecSDave Kleikamp 		BUFFER_TRACE(where->bh, "get_write_access");
771617ba13bSMingming Cao 		err = ext4_journal_get_write_access(handle, where->bh);
772ac27a0ecSDave Kleikamp 		if (err)
773ac27a0ecSDave Kleikamp 			goto err_out;
774ac27a0ecSDave Kleikamp 	}
775ac27a0ecSDave Kleikamp 	/* That's it */
776ac27a0ecSDave Kleikamp 
777ac27a0ecSDave Kleikamp 	*where->p = where->key;
778ac27a0ecSDave Kleikamp 
779ac27a0ecSDave Kleikamp 	/*
780ac27a0ecSDave Kleikamp 	 * Update the host buffer_head or inode to point to more just allocated
781ac27a0ecSDave Kleikamp 	 * direct blocks blocks
782ac27a0ecSDave Kleikamp 	 */
783ac27a0ecSDave Kleikamp 	if (num == 0 && blks > 1) {
784ac27a0ecSDave Kleikamp 		current_block = le32_to_cpu(where->key) + 1;
785ac27a0ecSDave Kleikamp 		for (i = 1; i < blks; i++)
786ac27a0ecSDave Kleikamp 			*(where->p + i) = cpu_to_le32(current_block++);
787ac27a0ecSDave Kleikamp 	}
788ac27a0ecSDave Kleikamp 
789ac27a0ecSDave Kleikamp 	/*
790ac27a0ecSDave Kleikamp 	 * update the most recently allocated logical & physical block
791ac27a0ecSDave Kleikamp 	 * in i_block_alloc_info, to assist find the proper goal block for next
792ac27a0ecSDave Kleikamp 	 * allocation
793ac27a0ecSDave Kleikamp 	 */
794ac27a0ecSDave Kleikamp 	if (block_i) {
795ac27a0ecSDave Kleikamp 		block_i->last_alloc_logical_block = block + blks - 1;
796ac27a0ecSDave Kleikamp 		block_i->last_alloc_physical_block =
797ac27a0ecSDave Kleikamp 				le32_to_cpu(where[num].key) + blks - 1;
798ac27a0ecSDave Kleikamp 	}
799ac27a0ecSDave Kleikamp 
800ac27a0ecSDave Kleikamp 	/* We are done with atomic stuff, now do the rest of housekeeping */
801ac27a0ecSDave Kleikamp 
802ef7f3835SKalpak Shah 	inode->i_ctime = ext4_current_time(inode);
803617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
804ac27a0ecSDave Kleikamp 
805ac27a0ecSDave Kleikamp 	/* had we spliced it onto indirect block? */
806ac27a0ecSDave Kleikamp 	if (where->bh) {
807ac27a0ecSDave Kleikamp 		/*
808ac27a0ecSDave Kleikamp 		 * If we spliced it onto an indirect block, we haven't
809ac27a0ecSDave Kleikamp 		 * altered the inode.  Note however that if it is being spliced
810ac27a0ecSDave Kleikamp 		 * onto an indirect block at the very end of the file (the
811ac27a0ecSDave Kleikamp 		 * file is growing) then we *will* alter the inode to reflect
812ac27a0ecSDave Kleikamp 		 * the new i_size.  But that is not done here - it is done in
813617ba13bSMingming Cao 		 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
814ac27a0ecSDave Kleikamp 		 */
815ac27a0ecSDave Kleikamp 		jbd_debug(5, "splicing indirect only\n");
816617ba13bSMingming Cao 		BUFFER_TRACE(where->bh, "call ext4_journal_dirty_metadata");
817617ba13bSMingming Cao 		err = ext4_journal_dirty_metadata(handle, where->bh);
818ac27a0ecSDave Kleikamp 		if (err)
819ac27a0ecSDave Kleikamp 			goto err_out;
820ac27a0ecSDave Kleikamp 	} else {
821ac27a0ecSDave Kleikamp 		/*
822ac27a0ecSDave Kleikamp 		 * OK, we spliced it into the inode itself on a direct block.
823ac27a0ecSDave Kleikamp 		 * Inode was dirtied above.
824ac27a0ecSDave Kleikamp 		 */
825ac27a0ecSDave Kleikamp 		jbd_debug(5, "splicing direct\n");
826ac27a0ecSDave Kleikamp 	}
827ac27a0ecSDave Kleikamp 	return err;
828ac27a0ecSDave Kleikamp 
829ac27a0ecSDave Kleikamp err_out:
830ac27a0ecSDave Kleikamp 	for (i = 1; i <= num; i++) {
831dab291afSMingming Cao 		BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
832617ba13bSMingming Cao 		ext4_journal_forget(handle, where[i].bh);
833c9de560dSAlex Tomas 		ext4_free_blocks(handle, inode,
834c9de560dSAlex Tomas 					le32_to_cpu(where[i-1].key), 1, 0);
835ac27a0ecSDave Kleikamp 	}
836c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
837ac27a0ecSDave Kleikamp 
838ac27a0ecSDave Kleikamp 	return err;
839ac27a0ecSDave Kleikamp }
840ac27a0ecSDave Kleikamp 
841ac27a0ecSDave Kleikamp /*
842ac27a0ecSDave Kleikamp  * Allocation strategy is simple: if we have to allocate something, we will
843ac27a0ecSDave Kleikamp  * have to go the whole way to leaf. So let's do it before attaching anything
844ac27a0ecSDave Kleikamp  * to tree, set linkage between the newborn blocks, write them if sync is
845ac27a0ecSDave Kleikamp  * required, recheck the path, free and repeat if check fails, otherwise
846ac27a0ecSDave Kleikamp  * set the last missing link (that will protect us from any truncate-generated
847ac27a0ecSDave Kleikamp  * removals - all blocks on the path are immune now) and possibly force the
848ac27a0ecSDave Kleikamp  * write on the parent block.
849ac27a0ecSDave Kleikamp  * That has a nice additional property: no special recovery from the failed
850ac27a0ecSDave Kleikamp  * allocations is needed - we simply release blocks and do not touch anything
851ac27a0ecSDave Kleikamp  * reachable from inode.
852ac27a0ecSDave Kleikamp  *
853ac27a0ecSDave Kleikamp  * `handle' can be NULL if create == 0.
854ac27a0ecSDave Kleikamp  *
855ac27a0ecSDave Kleikamp  * return > 0, # of blocks mapped or allocated.
856ac27a0ecSDave Kleikamp  * return = 0, if plain lookup failed.
857ac27a0ecSDave Kleikamp  * return < 0, error case.
858c278bfecSAneesh Kumar K.V  *
859c278bfecSAneesh Kumar K.V  *
860c278bfecSAneesh Kumar K.V  * Need to be called with
8610e855ac8SAneesh Kumar K.V  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
8620e855ac8SAneesh Kumar K.V  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
863ac27a0ecSDave Kleikamp  */
864617ba13bSMingming Cao int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
865725d26d3SAneesh Kumar K.V 		ext4_lblk_t iblock, unsigned long maxblocks,
866ac27a0ecSDave Kleikamp 		struct buffer_head *bh_result,
867ac27a0ecSDave Kleikamp 		int create, int extend_disksize)
868ac27a0ecSDave Kleikamp {
869ac27a0ecSDave Kleikamp 	int err = -EIO;
870725d26d3SAneesh Kumar K.V 	ext4_lblk_t offsets[4];
871ac27a0ecSDave Kleikamp 	Indirect chain[4];
872ac27a0ecSDave Kleikamp 	Indirect *partial;
873617ba13bSMingming Cao 	ext4_fsblk_t goal;
874ac27a0ecSDave Kleikamp 	int indirect_blks;
875ac27a0ecSDave Kleikamp 	int blocks_to_boundary = 0;
876ac27a0ecSDave Kleikamp 	int depth;
877617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
878ac27a0ecSDave Kleikamp 	int count = 0;
879617ba13bSMingming Cao 	ext4_fsblk_t first_block = 0;
88061628a3fSMingming Cao 	loff_t disksize;
881ac27a0ecSDave Kleikamp 
882ac27a0ecSDave Kleikamp 
883a86c6181SAlex Tomas 	J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
884ac27a0ecSDave Kleikamp 	J_ASSERT(handle != NULL || create == 0);
885725d26d3SAneesh Kumar K.V 	depth = ext4_block_to_path(inode, iblock, offsets,
886725d26d3SAneesh Kumar K.V 					&blocks_to_boundary);
887ac27a0ecSDave Kleikamp 
888ac27a0ecSDave Kleikamp 	if (depth == 0)
889ac27a0ecSDave Kleikamp 		goto out;
890ac27a0ecSDave Kleikamp 
891617ba13bSMingming Cao 	partial = ext4_get_branch(inode, depth, offsets, chain, &err);
892ac27a0ecSDave Kleikamp 
893ac27a0ecSDave Kleikamp 	/* Simplest case - block found, no allocation needed */
894ac27a0ecSDave Kleikamp 	if (!partial) {
895ac27a0ecSDave Kleikamp 		first_block = le32_to_cpu(chain[depth - 1].key);
896ac27a0ecSDave Kleikamp 		clear_buffer_new(bh_result);
897ac27a0ecSDave Kleikamp 		count++;
898ac27a0ecSDave Kleikamp 		/*map more blocks*/
899ac27a0ecSDave Kleikamp 		while (count < maxblocks && count <= blocks_to_boundary) {
900617ba13bSMingming Cao 			ext4_fsblk_t blk;
901ac27a0ecSDave Kleikamp 
902ac27a0ecSDave Kleikamp 			blk = le32_to_cpu(*(chain[depth-1].p + count));
903ac27a0ecSDave Kleikamp 
904ac27a0ecSDave Kleikamp 			if (blk == first_block + count)
905ac27a0ecSDave Kleikamp 				count++;
906ac27a0ecSDave Kleikamp 			else
907ac27a0ecSDave Kleikamp 				break;
908ac27a0ecSDave Kleikamp 		}
909ac27a0ecSDave Kleikamp 		goto got_it;
910ac27a0ecSDave Kleikamp 	}
911ac27a0ecSDave Kleikamp 
912ac27a0ecSDave Kleikamp 	/* Next simple case - plain lookup or failed read of indirect block */
913ac27a0ecSDave Kleikamp 	if (!create || err == -EIO)
914ac27a0ecSDave Kleikamp 		goto cleanup;
915ac27a0ecSDave Kleikamp 
916ac27a0ecSDave Kleikamp 	/*
917ac27a0ecSDave Kleikamp 	 * Okay, we need to do block allocation.  Lazily initialize the block
918ac27a0ecSDave Kleikamp 	 * allocation info here if necessary
919ac27a0ecSDave Kleikamp 	*/
920ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
921617ba13bSMingming Cao 		ext4_init_block_alloc_info(inode);
922ac27a0ecSDave Kleikamp 
923fb01bfdaSAkinobu Mita 	goal = ext4_find_goal(inode, iblock, partial);
924ac27a0ecSDave Kleikamp 
925ac27a0ecSDave Kleikamp 	/* the number of blocks need to allocate for [d,t]indirect blocks */
926ac27a0ecSDave Kleikamp 	indirect_blks = (chain + depth) - partial - 1;
927ac27a0ecSDave Kleikamp 
928ac27a0ecSDave Kleikamp 	/*
929ac27a0ecSDave Kleikamp 	 * Next look up the indirect map to count the totoal number of
930ac27a0ecSDave Kleikamp 	 * direct blocks to allocate for this branch.
931ac27a0ecSDave Kleikamp 	 */
932617ba13bSMingming Cao 	count = ext4_blks_to_allocate(partial, indirect_blks,
933ac27a0ecSDave Kleikamp 					maxblocks, blocks_to_boundary);
934ac27a0ecSDave Kleikamp 	/*
935617ba13bSMingming Cao 	 * Block out ext4_truncate while we alter the tree
936ac27a0ecSDave Kleikamp 	 */
9377061eba7SAneesh Kumar K.V 	err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
9387061eba7SAneesh Kumar K.V 					&count, goal,
939ac27a0ecSDave Kleikamp 					offsets + (partial - chain), partial);
940ac27a0ecSDave Kleikamp 
941ac27a0ecSDave Kleikamp 	/*
942617ba13bSMingming Cao 	 * The ext4_splice_branch call will free and forget any buffers
943ac27a0ecSDave Kleikamp 	 * on the new chain if there is a failure, but that risks using
944ac27a0ecSDave Kleikamp 	 * up transaction credits, especially for bitmaps where the
945ac27a0ecSDave Kleikamp 	 * credits cannot be returned.  Can we handle this somehow?  We
946ac27a0ecSDave Kleikamp 	 * may need to return -EAGAIN upwards in the worst case.  --sct
947ac27a0ecSDave Kleikamp 	 */
948ac27a0ecSDave Kleikamp 	if (!err)
949617ba13bSMingming Cao 		err = ext4_splice_branch(handle, inode, iblock,
950ac27a0ecSDave Kleikamp 					partial, indirect_blks, count);
951ac27a0ecSDave Kleikamp 	/*
9520e855ac8SAneesh Kumar K.V 	 * i_disksize growing is protected by i_data_sem.  Don't forget to
953ac27a0ecSDave Kleikamp 	 * protect it if you're about to implement concurrent
954617ba13bSMingming Cao 	 * ext4_get_block() -bzzz
955ac27a0ecSDave Kleikamp 	*/
95661628a3fSMingming Cao 	if (!err && extend_disksize) {
95761628a3fSMingming Cao 		disksize = ((loff_t) iblock + count) << inode->i_blkbits;
95861628a3fSMingming Cao 		if (disksize > i_size_read(inode))
95961628a3fSMingming Cao 			disksize = i_size_read(inode);
96061628a3fSMingming Cao 		if (disksize > ei->i_disksize)
96161628a3fSMingming Cao 			ei->i_disksize = disksize;
96261628a3fSMingming Cao 	}
963ac27a0ecSDave Kleikamp 	if (err)
964ac27a0ecSDave Kleikamp 		goto cleanup;
965ac27a0ecSDave Kleikamp 
966ac27a0ecSDave Kleikamp 	set_buffer_new(bh_result);
967ac27a0ecSDave Kleikamp got_it:
968ac27a0ecSDave Kleikamp 	map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
969ac27a0ecSDave Kleikamp 	if (count > blocks_to_boundary)
970ac27a0ecSDave Kleikamp 		set_buffer_boundary(bh_result);
971ac27a0ecSDave Kleikamp 	err = count;
972ac27a0ecSDave Kleikamp 	/* Clean up and exit */
973ac27a0ecSDave Kleikamp 	partial = chain + depth - 1;	/* the whole chain */
974ac27a0ecSDave Kleikamp cleanup:
975ac27a0ecSDave Kleikamp 	while (partial > chain) {
976ac27a0ecSDave Kleikamp 		BUFFER_TRACE(partial->bh, "call brelse");
977ac27a0ecSDave Kleikamp 		brelse(partial->bh);
978ac27a0ecSDave Kleikamp 		partial--;
979ac27a0ecSDave Kleikamp 	}
980ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh_result, "returned");
981ac27a0ecSDave Kleikamp out:
982ac27a0ecSDave Kleikamp 	return err;
983ac27a0ecSDave Kleikamp }
984ac27a0ecSDave Kleikamp 
98512219aeaSAneesh Kumar K.V /*
98612219aeaSAneesh Kumar K.V  * Calculate the number of metadata blocks need to reserve
98712219aeaSAneesh Kumar K.V  * to allocate @blocks for non extent file based file
98812219aeaSAneesh Kumar K.V  */
98912219aeaSAneesh Kumar K.V static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
99012219aeaSAneesh Kumar K.V {
99112219aeaSAneesh Kumar K.V 	int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
99212219aeaSAneesh Kumar K.V 	int ind_blks, dind_blks, tind_blks;
99312219aeaSAneesh Kumar K.V 
99412219aeaSAneesh Kumar K.V 	/* number of new indirect blocks needed */
99512219aeaSAneesh Kumar K.V 	ind_blks = (blocks + icap - 1) / icap;
99612219aeaSAneesh Kumar K.V 
99712219aeaSAneesh Kumar K.V 	dind_blks = (ind_blks + icap - 1) / icap;
99812219aeaSAneesh Kumar K.V 
99912219aeaSAneesh Kumar K.V 	tind_blks = 1;
100012219aeaSAneesh Kumar K.V 
100112219aeaSAneesh Kumar K.V 	return ind_blks + dind_blks + tind_blks;
100212219aeaSAneesh Kumar K.V }
100312219aeaSAneesh Kumar K.V 
100412219aeaSAneesh Kumar K.V /*
100512219aeaSAneesh Kumar K.V  * Calculate the number of metadata blocks need to reserve
100612219aeaSAneesh Kumar K.V  * to allocate given number of blocks
100712219aeaSAneesh Kumar K.V  */
100812219aeaSAneesh Kumar K.V static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
100912219aeaSAneesh Kumar K.V {
1010cd213226SMingming Cao 	if (!blocks)
1011cd213226SMingming Cao 		return 0;
1012cd213226SMingming Cao 
101312219aeaSAneesh Kumar K.V 	if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
101412219aeaSAneesh Kumar K.V 		return ext4_ext_calc_metadata_amount(inode, blocks);
101512219aeaSAneesh Kumar K.V 
101612219aeaSAneesh Kumar K.V 	return ext4_indirect_calc_metadata_amount(inode, blocks);
101712219aeaSAneesh Kumar K.V }
101812219aeaSAneesh Kumar K.V 
101912219aeaSAneesh Kumar K.V static void ext4_da_update_reserve_space(struct inode *inode, int used)
102012219aeaSAneesh Kumar K.V {
102112219aeaSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
102212219aeaSAneesh Kumar K.V 	int total, mdb, mdb_free;
102312219aeaSAneesh Kumar K.V 
102412219aeaSAneesh Kumar K.V 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
102512219aeaSAneesh Kumar K.V 	/* recalculate the number of metablocks still need to be reserved */
102612219aeaSAneesh Kumar K.V 	total = EXT4_I(inode)->i_reserved_data_blocks - used;
102712219aeaSAneesh Kumar K.V 	mdb = ext4_calc_metadata_amount(inode, total);
102812219aeaSAneesh Kumar K.V 
102912219aeaSAneesh Kumar K.V 	/* figure out how many metablocks to release */
103012219aeaSAneesh Kumar K.V 	BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
103112219aeaSAneesh Kumar K.V 	mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
103212219aeaSAneesh Kumar K.V 
103312219aeaSAneesh Kumar K.V 	/* Account for allocated meta_blocks */
103412219aeaSAneesh Kumar K.V 	mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
103512219aeaSAneesh Kumar K.V 
103612219aeaSAneesh Kumar K.V 	/* update fs free blocks counter for truncate case */
103712219aeaSAneesh Kumar K.V 	percpu_counter_add(&sbi->s_freeblocks_counter, mdb_free);
103812219aeaSAneesh Kumar K.V 
103912219aeaSAneesh Kumar K.V 	/* update per-inode reservations */
104012219aeaSAneesh Kumar K.V 	BUG_ON(used  > EXT4_I(inode)->i_reserved_data_blocks);
104112219aeaSAneesh Kumar K.V 	EXT4_I(inode)->i_reserved_data_blocks -= used;
104212219aeaSAneesh Kumar K.V 
104312219aeaSAneesh Kumar K.V 	BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
104412219aeaSAneesh Kumar K.V 	EXT4_I(inode)->i_reserved_meta_blocks = mdb;
104512219aeaSAneesh Kumar K.V 	EXT4_I(inode)->i_allocated_meta_blocks = 0;
104612219aeaSAneesh Kumar K.V 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
104712219aeaSAneesh Kumar K.V }
104812219aeaSAneesh Kumar K.V 
1049f5ab0d1fSMingming Cao /*
10502b2d6d01STheodore Ts'o  * The ext4_get_blocks_wrap() function try to look up the requested blocks,
10512b2d6d01STheodore Ts'o  * and returns if the blocks are already mapped.
1052f5ab0d1fSMingming Cao  *
1053f5ab0d1fSMingming Cao  * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1054f5ab0d1fSMingming Cao  * and store the allocated blocks in the result buffer head and mark it
1055f5ab0d1fSMingming Cao  * mapped.
1056f5ab0d1fSMingming Cao  *
1057f5ab0d1fSMingming Cao  * If file type is extents based, it will call ext4_ext_get_blocks(),
1058f5ab0d1fSMingming Cao  * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping
1059f5ab0d1fSMingming Cao  * based files
1060f5ab0d1fSMingming Cao  *
1061f5ab0d1fSMingming Cao  * On success, it returns the number of blocks being mapped or allocate.
1062f5ab0d1fSMingming Cao  * if create==0 and the blocks are pre-allocated and uninitialized block,
1063f5ab0d1fSMingming Cao  * the result buffer head is unmapped. If the create ==1, it will make sure
1064f5ab0d1fSMingming Cao  * the buffer head is mapped.
1065f5ab0d1fSMingming Cao  *
1066f5ab0d1fSMingming Cao  * It returns 0 if plain look up failed (blocks have not been allocated), in
1067f5ab0d1fSMingming Cao  * that casem, buffer head is unmapped
1068f5ab0d1fSMingming Cao  *
1069f5ab0d1fSMingming Cao  * It returns the error in case of allocation failure.
1070f5ab0d1fSMingming Cao  */
10710e855ac8SAneesh Kumar K.V int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
10720e855ac8SAneesh Kumar K.V 			unsigned long max_blocks, struct buffer_head *bh,
1073d2a17637SMingming Cao 			int create, int extend_disksize, int flag)
10740e855ac8SAneesh Kumar K.V {
10750e855ac8SAneesh Kumar K.V 	int retval;
1076f5ab0d1fSMingming Cao 
1077f5ab0d1fSMingming Cao 	clear_buffer_mapped(bh);
1078f5ab0d1fSMingming Cao 
10794df3d265SAneesh Kumar K.V 	/*
10804df3d265SAneesh Kumar K.V 	 * Try to see if we can get  the block without requesting
10814df3d265SAneesh Kumar K.V 	 * for new file system block.
10824df3d265SAneesh Kumar K.V 	 */
10830e855ac8SAneesh Kumar K.V 	down_read((&EXT4_I(inode)->i_data_sem));
10844df3d265SAneesh Kumar K.V 	if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
10854df3d265SAneesh Kumar K.V 		retval =  ext4_ext_get_blocks(handle, inode, block, max_blocks,
10864df3d265SAneesh Kumar K.V 				bh, 0, 0);
10874df3d265SAneesh Kumar K.V 	} else {
10884df3d265SAneesh Kumar K.V 		retval = ext4_get_blocks_handle(handle,
10894df3d265SAneesh Kumar K.V 				inode, block, max_blocks, bh, 0, 0);
10900e855ac8SAneesh Kumar K.V 	}
10914df3d265SAneesh Kumar K.V 	up_read((&EXT4_I(inode)->i_data_sem));
1092f5ab0d1fSMingming Cao 
1093f5ab0d1fSMingming Cao 	/* If it is only a block(s) look up */
1094f5ab0d1fSMingming Cao 	if (!create)
10954df3d265SAneesh Kumar K.V 		return retval;
10964df3d265SAneesh Kumar K.V 
10974df3d265SAneesh Kumar K.V 	/*
1098f5ab0d1fSMingming Cao 	 * Returns if the blocks have already allocated
1099f5ab0d1fSMingming Cao 	 *
1100f5ab0d1fSMingming Cao 	 * Note that if blocks have been preallocated
1101f5ab0d1fSMingming Cao 	 * ext4_ext_get_block() returns th create = 0
1102f5ab0d1fSMingming Cao 	 * with buffer head unmapped.
1103f5ab0d1fSMingming Cao 	 */
1104f5ab0d1fSMingming Cao 	if (retval > 0 && buffer_mapped(bh))
1105f5ab0d1fSMingming Cao 		return retval;
1106f5ab0d1fSMingming Cao 
1107f5ab0d1fSMingming Cao 	/*
1108f5ab0d1fSMingming Cao 	 * New blocks allocate and/or writing to uninitialized extent
1109f5ab0d1fSMingming Cao 	 * will possibly result in updating i_data, so we take
1110f5ab0d1fSMingming Cao 	 * the write lock of i_data_sem, and call get_blocks()
1111f5ab0d1fSMingming Cao 	 * with create == 1 flag.
11124df3d265SAneesh Kumar K.V 	 */
11134df3d265SAneesh Kumar K.V 	down_write((&EXT4_I(inode)->i_data_sem));
1114d2a17637SMingming Cao 
1115d2a17637SMingming Cao 	/*
1116d2a17637SMingming Cao 	 * if the caller is from delayed allocation writeout path
1117d2a17637SMingming Cao 	 * we have already reserved fs blocks for allocation
1118d2a17637SMingming Cao 	 * let the underlying get_block() function know to
1119d2a17637SMingming Cao 	 * avoid double accounting
1120d2a17637SMingming Cao 	 */
1121d2a17637SMingming Cao 	if (flag)
1122d2a17637SMingming Cao 		EXT4_I(inode)->i_delalloc_reserved_flag = 1;
11234df3d265SAneesh Kumar K.V 	/*
11244df3d265SAneesh Kumar K.V 	 * We need to check for EXT4 here because migrate
11254df3d265SAneesh Kumar K.V 	 * could have changed the inode type in between
11264df3d265SAneesh Kumar K.V 	 */
11270e855ac8SAneesh Kumar K.V 	if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
11280e855ac8SAneesh Kumar K.V 		retval =  ext4_ext_get_blocks(handle, inode, block, max_blocks,
11290e855ac8SAneesh Kumar K.V 				bh, create, extend_disksize);
11300e855ac8SAneesh Kumar K.V 	} else {
11310e855ac8SAneesh Kumar K.V 		retval = ext4_get_blocks_handle(handle, inode, block,
11320e855ac8SAneesh Kumar K.V 				max_blocks, bh, create, extend_disksize);
1133267e4db9SAneesh Kumar K.V 
1134267e4db9SAneesh Kumar K.V 		if (retval > 0 && buffer_new(bh)) {
1135267e4db9SAneesh Kumar K.V 			/*
1136267e4db9SAneesh Kumar K.V 			 * We allocated new blocks which will result in
1137267e4db9SAneesh Kumar K.V 			 * i_data's format changing.  Force the migrate
1138267e4db9SAneesh Kumar K.V 			 * to fail by clearing migrate flags
1139267e4db9SAneesh Kumar K.V 			 */
1140267e4db9SAneesh Kumar K.V 			EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1141267e4db9SAneesh Kumar K.V 							~EXT4_EXT_MIGRATE;
1142267e4db9SAneesh Kumar K.V 		}
11430e855ac8SAneesh Kumar K.V 	}
1144d2a17637SMingming Cao 
1145d2a17637SMingming Cao 	if (flag) {
1146d2a17637SMingming Cao 		EXT4_I(inode)->i_delalloc_reserved_flag = 0;
1147d2a17637SMingming Cao 		/*
1148d2a17637SMingming Cao 		 * Update reserved blocks/metadata blocks
1149d2a17637SMingming Cao 		 * after successful block allocation
1150d2a17637SMingming Cao 		 * which were deferred till now
1151d2a17637SMingming Cao 		 */
1152d2a17637SMingming Cao 		if ((retval > 0) && buffer_delay(bh))
115312219aeaSAneesh Kumar K.V 			ext4_da_update_reserve_space(inode, retval);
1154d2a17637SMingming Cao 	}
1155d2a17637SMingming Cao 
11560e855ac8SAneesh Kumar K.V 	up_write((&EXT4_I(inode)->i_data_sem));
11570e855ac8SAneesh Kumar K.V 	return retval;
11580e855ac8SAneesh Kumar K.V }
11590e855ac8SAneesh Kumar K.V 
1160f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */
1161f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096
1162f3bd1f3fSMingming Cao 
1163617ba13bSMingming Cao static int ext4_get_block(struct inode *inode, sector_t iblock,
1164ac27a0ecSDave Kleikamp 			struct buffer_head *bh_result, int create)
1165ac27a0ecSDave Kleikamp {
11663e4fdaf8SDmitriy Monakhov 	handle_t *handle = ext4_journal_current_handle();
11677fb5409dSJan Kara 	int ret = 0, started = 0;
1168ac27a0ecSDave Kleikamp 	unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
1169f3bd1f3fSMingming Cao 	int dio_credits;
1170ac27a0ecSDave Kleikamp 
11717fb5409dSJan Kara 	if (create && !handle) {
11727fb5409dSJan Kara 		/* Direct IO write... */
11737fb5409dSJan Kara 		if (max_blocks > DIO_MAX_BLOCKS)
11747fb5409dSJan Kara 			max_blocks = DIO_MAX_BLOCKS;
1175f3bd1f3fSMingming Cao 		dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1176f3bd1f3fSMingming Cao 		handle = ext4_journal_start(inode, dio_credits);
11777fb5409dSJan Kara 		if (IS_ERR(handle)) {
1178ac27a0ecSDave Kleikamp 			ret = PTR_ERR(handle);
11797fb5409dSJan Kara 			goto out;
11807fb5409dSJan Kara 		}
11817fb5409dSJan Kara 		started = 1;
1182ac27a0ecSDave Kleikamp 	}
1183ac27a0ecSDave Kleikamp 
1184a86c6181SAlex Tomas 	ret = ext4_get_blocks_wrap(handle, inode, iblock,
1185d2a17637SMingming Cao 					max_blocks, bh_result, create, 0, 0);
1186ac27a0ecSDave Kleikamp 	if (ret > 0) {
1187ac27a0ecSDave Kleikamp 		bh_result->b_size = (ret << inode->i_blkbits);
1188ac27a0ecSDave Kleikamp 		ret = 0;
1189ac27a0ecSDave Kleikamp 	}
11907fb5409dSJan Kara 	if (started)
11917fb5409dSJan Kara 		ext4_journal_stop(handle);
11927fb5409dSJan Kara out:
1193ac27a0ecSDave Kleikamp 	return ret;
1194ac27a0ecSDave Kleikamp }
1195ac27a0ecSDave Kleikamp 
1196ac27a0ecSDave Kleikamp /*
1197ac27a0ecSDave Kleikamp  * `handle' can be NULL if create is zero
1198ac27a0ecSDave Kleikamp  */
1199617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
1200725d26d3SAneesh Kumar K.V 				ext4_lblk_t block, int create, int *errp)
1201ac27a0ecSDave Kleikamp {
1202ac27a0ecSDave Kleikamp 	struct buffer_head dummy;
1203ac27a0ecSDave Kleikamp 	int fatal = 0, err;
1204ac27a0ecSDave Kleikamp 
1205ac27a0ecSDave Kleikamp 	J_ASSERT(handle != NULL || create == 0);
1206ac27a0ecSDave Kleikamp 
1207ac27a0ecSDave Kleikamp 	dummy.b_state = 0;
1208ac27a0ecSDave Kleikamp 	dummy.b_blocknr = -1000;
1209ac27a0ecSDave Kleikamp 	buffer_trace_init(&dummy.b_history);
1210a86c6181SAlex Tomas 	err = ext4_get_blocks_wrap(handle, inode, block, 1,
1211d2a17637SMingming Cao 					&dummy, create, 1, 0);
1212ac27a0ecSDave Kleikamp 	/*
1213617ba13bSMingming Cao 	 * ext4_get_blocks_handle() returns number of blocks
1214ac27a0ecSDave Kleikamp 	 * mapped. 0 in case of a HOLE.
1215ac27a0ecSDave Kleikamp 	 */
1216ac27a0ecSDave Kleikamp 	if (err > 0) {
1217ac27a0ecSDave Kleikamp 		if (err > 1)
1218ac27a0ecSDave Kleikamp 			WARN_ON(1);
1219ac27a0ecSDave Kleikamp 		err = 0;
1220ac27a0ecSDave Kleikamp 	}
1221ac27a0ecSDave Kleikamp 	*errp = err;
1222ac27a0ecSDave Kleikamp 	if (!err && buffer_mapped(&dummy)) {
1223ac27a0ecSDave Kleikamp 		struct buffer_head *bh;
1224ac27a0ecSDave Kleikamp 		bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1225ac27a0ecSDave Kleikamp 		if (!bh) {
1226ac27a0ecSDave Kleikamp 			*errp = -EIO;
1227ac27a0ecSDave Kleikamp 			goto err;
1228ac27a0ecSDave Kleikamp 		}
1229ac27a0ecSDave Kleikamp 		if (buffer_new(&dummy)) {
1230ac27a0ecSDave Kleikamp 			J_ASSERT(create != 0);
1231ac39849dSAneesh Kumar K.V 			J_ASSERT(handle != NULL);
1232ac27a0ecSDave Kleikamp 
1233ac27a0ecSDave Kleikamp 			/*
1234ac27a0ecSDave Kleikamp 			 * Now that we do not always journal data, we should
1235ac27a0ecSDave Kleikamp 			 * keep in mind whether this should always journal the
1236ac27a0ecSDave Kleikamp 			 * new buffer as metadata.  For now, regular file
1237617ba13bSMingming Cao 			 * writes use ext4_get_block instead, so it's not a
1238ac27a0ecSDave Kleikamp 			 * problem.
1239ac27a0ecSDave Kleikamp 			 */
1240ac27a0ecSDave Kleikamp 			lock_buffer(bh);
1241ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bh, "call get_create_access");
1242617ba13bSMingming Cao 			fatal = ext4_journal_get_create_access(handle, bh);
1243ac27a0ecSDave Kleikamp 			if (!fatal && !buffer_uptodate(bh)) {
1244ac27a0ecSDave Kleikamp 				memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1245ac27a0ecSDave Kleikamp 				set_buffer_uptodate(bh);
1246ac27a0ecSDave Kleikamp 			}
1247ac27a0ecSDave Kleikamp 			unlock_buffer(bh);
1248617ba13bSMingming Cao 			BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1249617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle, bh);
1250ac27a0ecSDave Kleikamp 			if (!fatal)
1251ac27a0ecSDave Kleikamp 				fatal = err;
1252ac27a0ecSDave Kleikamp 		} else {
1253ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bh, "not a new buffer");
1254ac27a0ecSDave Kleikamp 		}
1255ac27a0ecSDave Kleikamp 		if (fatal) {
1256ac27a0ecSDave Kleikamp 			*errp = fatal;
1257ac27a0ecSDave Kleikamp 			brelse(bh);
1258ac27a0ecSDave Kleikamp 			bh = NULL;
1259ac27a0ecSDave Kleikamp 		}
1260ac27a0ecSDave Kleikamp 		return bh;
1261ac27a0ecSDave Kleikamp 	}
1262ac27a0ecSDave Kleikamp err:
1263ac27a0ecSDave Kleikamp 	return NULL;
1264ac27a0ecSDave Kleikamp }
1265ac27a0ecSDave Kleikamp 
1266617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
1267725d26d3SAneesh Kumar K.V 			       ext4_lblk_t block, int create, int *err)
1268ac27a0ecSDave Kleikamp {
1269ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
1270ac27a0ecSDave Kleikamp 
1271617ba13bSMingming Cao 	bh = ext4_getblk(handle, inode, block, create, err);
1272ac27a0ecSDave Kleikamp 	if (!bh)
1273ac27a0ecSDave Kleikamp 		return bh;
1274ac27a0ecSDave Kleikamp 	if (buffer_uptodate(bh))
1275ac27a0ecSDave Kleikamp 		return bh;
1276ac27a0ecSDave Kleikamp 	ll_rw_block(READ_META, 1, &bh);
1277ac27a0ecSDave Kleikamp 	wait_on_buffer(bh);
1278ac27a0ecSDave Kleikamp 	if (buffer_uptodate(bh))
1279ac27a0ecSDave Kleikamp 		return bh;
1280ac27a0ecSDave Kleikamp 	put_bh(bh);
1281ac27a0ecSDave Kleikamp 	*err = -EIO;
1282ac27a0ecSDave Kleikamp 	return NULL;
1283ac27a0ecSDave Kleikamp }
1284ac27a0ecSDave Kleikamp 
1285ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle,
1286ac27a0ecSDave Kleikamp 			     struct buffer_head *head,
1287ac27a0ecSDave Kleikamp 			     unsigned from,
1288ac27a0ecSDave Kleikamp 			     unsigned to,
1289ac27a0ecSDave Kleikamp 			     int *partial,
1290ac27a0ecSDave Kleikamp 			     int (*fn)(handle_t *handle,
1291ac27a0ecSDave Kleikamp 				       struct buffer_head *bh))
1292ac27a0ecSDave Kleikamp {
1293ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
1294ac27a0ecSDave Kleikamp 	unsigned block_start, block_end;
1295ac27a0ecSDave Kleikamp 	unsigned blocksize = head->b_size;
1296ac27a0ecSDave Kleikamp 	int err, ret = 0;
1297ac27a0ecSDave Kleikamp 	struct buffer_head *next;
1298ac27a0ecSDave Kleikamp 
1299ac27a0ecSDave Kleikamp 	for (bh = head, block_start = 0;
1300ac27a0ecSDave Kleikamp 	     ret == 0 && (bh != head || !block_start);
1301ac27a0ecSDave Kleikamp 	     block_start = block_end, bh = next)
1302ac27a0ecSDave Kleikamp 	{
1303ac27a0ecSDave Kleikamp 		next = bh->b_this_page;
1304ac27a0ecSDave Kleikamp 		block_end = block_start + blocksize;
1305ac27a0ecSDave Kleikamp 		if (block_end <= from || block_start >= to) {
1306ac27a0ecSDave Kleikamp 			if (partial && !buffer_uptodate(bh))
1307ac27a0ecSDave Kleikamp 				*partial = 1;
1308ac27a0ecSDave Kleikamp 			continue;
1309ac27a0ecSDave Kleikamp 		}
1310ac27a0ecSDave Kleikamp 		err = (*fn)(handle, bh);
1311ac27a0ecSDave Kleikamp 		if (!ret)
1312ac27a0ecSDave Kleikamp 			ret = err;
1313ac27a0ecSDave Kleikamp 	}
1314ac27a0ecSDave Kleikamp 	return ret;
1315ac27a0ecSDave Kleikamp }
1316ac27a0ecSDave Kleikamp 
1317ac27a0ecSDave Kleikamp /*
1318ac27a0ecSDave Kleikamp  * To preserve ordering, it is essential that the hole instantiation and
1319ac27a0ecSDave Kleikamp  * the data write be encapsulated in a single transaction.  We cannot
1320617ba13bSMingming Cao  * close off a transaction and start a new one between the ext4_get_block()
1321dab291afSMingming Cao  * and the commit_write().  So doing the jbd2_journal_start at the start of
1322ac27a0ecSDave Kleikamp  * prepare_write() is the right place.
1323ac27a0ecSDave Kleikamp  *
1324617ba13bSMingming Cao  * Also, this function can nest inside ext4_writepage() ->
1325617ba13bSMingming Cao  * block_write_full_page(). In that case, we *know* that ext4_writepage()
1326ac27a0ecSDave Kleikamp  * has generated enough buffer credits to do the whole page.  So we won't
1327ac27a0ecSDave Kleikamp  * block on the journal in that case, which is good, because the caller may
1328ac27a0ecSDave Kleikamp  * be PF_MEMALLOC.
1329ac27a0ecSDave Kleikamp  *
1330617ba13bSMingming Cao  * By accident, ext4 can be reentered when a transaction is open via
1331ac27a0ecSDave Kleikamp  * quota file writes.  If we were to commit the transaction while thus
1332ac27a0ecSDave Kleikamp  * reentered, there can be a deadlock - we would be holding a quota
1333ac27a0ecSDave Kleikamp  * lock, and the commit would never complete if another thread had a
1334ac27a0ecSDave Kleikamp  * transaction open and was blocking on the quota lock - a ranking
1335ac27a0ecSDave Kleikamp  * violation.
1336ac27a0ecSDave Kleikamp  *
1337dab291afSMingming Cao  * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
1338ac27a0ecSDave Kleikamp  * will _not_ run commit under these circumstances because handle->h_ref
1339ac27a0ecSDave Kleikamp  * is elevated.  We'll still have enough credits for the tiny quotafile
1340ac27a0ecSDave Kleikamp  * write.
1341ac27a0ecSDave Kleikamp  */
1342ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle,
1343ac27a0ecSDave Kleikamp 					struct buffer_head *bh)
1344ac27a0ecSDave Kleikamp {
1345ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh) || buffer_freed(bh))
1346ac27a0ecSDave Kleikamp 		return 0;
1347617ba13bSMingming Cao 	return ext4_journal_get_write_access(handle, bh);
1348ac27a0ecSDave Kleikamp }
1349ac27a0ecSDave Kleikamp 
1350bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping,
1351bfc1af65SNick Piggin 				loff_t pos, unsigned len, unsigned flags,
1352bfc1af65SNick Piggin 				struct page **pagep, void **fsdata)
1353ac27a0ecSDave Kleikamp {
1354bfc1af65SNick Piggin 	struct inode *inode = mapping->host;
13557479d2b9SAndrew Morton 	int ret, needed_blocks = ext4_writepage_trans_blocks(inode);
1356ac27a0ecSDave Kleikamp 	handle_t *handle;
1357ac27a0ecSDave Kleikamp 	int retries = 0;
1358bfc1af65SNick Piggin 	struct page *page;
1359bfc1af65SNick Piggin  	pgoff_t index;
1360bfc1af65SNick Piggin 	unsigned from, to;
1361bfc1af65SNick Piggin 
1362bfc1af65SNick Piggin  	index = pos >> PAGE_CACHE_SHIFT;
1363bfc1af65SNick Piggin 	from = pos & (PAGE_CACHE_SIZE - 1);
1364bfc1af65SNick Piggin 	to = from + len;
1365ac27a0ecSDave Kleikamp 
1366ac27a0ecSDave Kleikamp retry:
1367617ba13bSMingming Cao 	handle = ext4_journal_start(inode, needed_blocks);
13687479d2b9SAndrew Morton 	if (IS_ERR(handle)) {
13697479d2b9SAndrew Morton 		ret = PTR_ERR(handle);
13707479d2b9SAndrew Morton 		goto out;
13717479d2b9SAndrew Morton 	}
1372ac27a0ecSDave Kleikamp 
1373cf108bcaSJan Kara 	page = __grab_cache_page(mapping, index);
1374cf108bcaSJan Kara 	if (!page) {
1375cf108bcaSJan Kara 		ext4_journal_stop(handle);
1376cf108bcaSJan Kara 		ret = -ENOMEM;
1377cf108bcaSJan Kara 		goto out;
1378cf108bcaSJan Kara 	}
1379cf108bcaSJan Kara 	*pagep = page;
1380cf108bcaSJan Kara 
1381bfc1af65SNick Piggin 	ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1382bfc1af65SNick Piggin 							ext4_get_block);
1383bfc1af65SNick Piggin 
1384bfc1af65SNick Piggin 	if (!ret && ext4_should_journal_data(inode)) {
1385ac27a0ecSDave Kleikamp 		ret = walk_page_buffers(handle, page_buffers(page),
1386ac27a0ecSDave Kleikamp 				from, to, NULL, do_journal_get_write_access);
1387b46be050SAndrey Savochkin 	}
1388bfc1af65SNick Piggin 
1389bfc1af65SNick Piggin 	if (ret) {
1390bfc1af65SNick Piggin 		unlock_page(page);
1391cf108bcaSJan Kara 		ext4_journal_stop(handle);
1392bfc1af65SNick Piggin 		page_cache_release(page);
1393bfc1af65SNick Piggin 	}
1394bfc1af65SNick Piggin 
1395617ba13bSMingming Cao 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
1396ac27a0ecSDave Kleikamp 		goto retry;
13977479d2b9SAndrew Morton out:
1398ac27a0ecSDave Kleikamp 	return ret;
1399ac27a0ecSDave Kleikamp }
1400ac27a0ecSDave Kleikamp 
1401bfc1af65SNick Piggin /* For write_end() in data=journal mode */
1402bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1403ac27a0ecSDave Kleikamp {
1404ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh) || buffer_freed(bh))
1405ac27a0ecSDave Kleikamp 		return 0;
1406ac27a0ecSDave Kleikamp 	set_buffer_uptodate(bh);
1407617ba13bSMingming Cao 	return ext4_journal_dirty_metadata(handle, bh);
1408ac27a0ecSDave Kleikamp }
1409ac27a0ecSDave Kleikamp 
1410ac27a0ecSDave Kleikamp /*
1411ac27a0ecSDave Kleikamp  * We need to pick up the new inode size which generic_commit_write gave us
1412ac27a0ecSDave Kleikamp  * `file' can be NULL - eg, when called from page_symlink().
1413ac27a0ecSDave Kleikamp  *
1414617ba13bSMingming Cao  * ext4 never places buffers on inode->i_mapping->private_list.  metadata
1415ac27a0ecSDave Kleikamp  * buffers are managed internally.
1416ac27a0ecSDave Kleikamp  */
1417bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file,
1418bfc1af65SNick Piggin 				struct address_space *mapping,
1419bfc1af65SNick Piggin 				loff_t pos, unsigned len, unsigned copied,
1420bfc1af65SNick Piggin 				struct page *page, void *fsdata)
1421ac27a0ecSDave Kleikamp {
1422617ba13bSMingming Cao 	handle_t *handle = ext4_journal_current_handle();
1423cf108bcaSJan Kara 	struct inode *inode = mapping->host;
1424ac27a0ecSDave Kleikamp 	int ret = 0, ret2;
1425ac27a0ecSDave Kleikamp 
1426678aaf48SJan Kara 	ret = ext4_jbd2_file_inode(handle, inode);
1427ac27a0ecSDave Kleikamp 
1428ac27a0ecSDave Kleikamp 	if (ret == 0) {
1429ac27a0ecSDave Kleikamp 		/*
1430bfc1af65SNick Piggin 		 * generic_write_end() will run mark_inode_dirty() if i_size
1431ac27a0ecSDave Kleikamp 		 * changes.  So let's piggyback the i_disksize mark_inode_dirty
1432ac27a0ecSDave Kleikamp 		 * into that.
1433ac27a0ecSDave Kleikamp 		 */
1434ac27a0ecSDave Kleikamp 		loff_t new_i_size;
1435ac27a0ecSDave Kleikamp 
1436bfc1af65SNick Piggin 		new_i_size = pos + copied;
1437617ba13bSMingming Cao 		if (new_i_size > EXT4_I(inode)->i_disksize)
1438617ba13bSMingming Cao 			EXT4_I(inode)->i_disksize = new_i_size;
1439cf108bcaSJan Kara 		ret2 = generic_write_end(file, mapping, pos, len, copied,
1440bfc1af65SNick Piggin 							page, fsdata);
1441f8a87d89SRoel Kluin 		copied = ret2;
1442f8a87d89SRoel Kluin 		if (ret2 < 0)
1443f8a87d89SRoel Kluin 			ret = ret2;
1444ac27a0ecSDave Kleikamp 	}
1445617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1446ac27a0ecSDave Kleikamp 	if (!ret)
1447ac27a0ecSDave Kleikamp 		ret = ret2;
1448bfc1af65SNick Piggin 
1449bfc1af65SNick Piggin 	return ret ? ret : copied;
1450ac27a0ecSDave Kleikamp }
1451ac27a0ecSDave Kleikamp 
1452bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file,
1453bfc1af65SNick Piggin 				struct address_space *mapping,
1454bfc1af65SNick Piggin 				loff_t pos, unsigned len, unsigned copied,
1455bfc1af65SNick Piggin 				struct page *page, void *fsdata)
1456ac27a0ecSDave Kleikamp {
1457617ba13bSMingming Cao 	handle_t *handle = ext4_journal_current_handle();
1458cf108bcaSJan Kara 	struct inode *inode = mapping->host;
1459ac27a0ecSDave Kleikamp 	int ret = 0, ret2;
1460ac27a0ecSDave Kleikamp 	loff_t new_i_size;
1461ac27a0ecSDave Kleikamp 
1462bfc1af65SNick Piggin 	new_i_size = pos + copied;
1463617ba13bSMingming Cao 	if (new_i_size > EXT4_I(inode)->i_disksize)
1464617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = new_i_size;
1465ac27a0ecSDave Kleikamp 
1466cf108bcaSJan Kara 	ret2 = generic_write_end(file, mapping, pos, len, copied,
1467bfc1af65SNick Piggin 							page, fsdata);
1468f8a87d89SRoel Kluin 	copied = ret2;
1469f8a87d89SRoel Kluin 	if (ret2 < 0)
1470f8a87d89SRoel Kluin 		ret = ret2;
1471ac27a0ecSDave Kleikamp 
1472617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1473ac27a0ecSDave Kleikamp 	if (!ret)
1474ac27a0ecSDave Kleikamp 		ret = ret2;
1475bfc1af65SNick Piggin 
1476bfc1af65SNick Piggin 	return ret ? ret : copied;
1477ac27a0ecSDave Kleikamp }
1478ac27a0ecSDave Kleikamp 
1479bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file,
1480bfc1af65SNick Piggin 				struct address_space *mapping,
1481bfc1af65SNick Piggin 				loff_t pos, unsigned len, unsigned copied,
1482bfc1af65SNick Piggin 				struct page *page, void *fsdata)
1483ac27a0ecSDave Kleikamp {
1484617ba13bSMingming Cao 	handle_t *handle = ext4_journal_current_handle();
1485bfc1af65SNick Piggin 	struct inode *inode = mapping->host;
1486ac27a0ecSDave Kleikamp 	int ret = 0, ret2;
1487ac27a0ecSDave Kleikamp 	int partial = 0;
1488bfc1af65SNick Piggin 	unsigned from, to;
1489ac27a0ecSDave Kleikamp 
1490bfc1af65SNick Piggin 	from = pos & (PAGE_CACHE_SIZE - 1);
1491bfc1af65SNick Piggin 	to = from + len;
1492bfc1af65SNick Piggin 
1493bfc1af65SNick Piggin 	if (copied < len) {
1494bfc1af65SNick Piggin 		if (!PageUptodate(page))
1495bfc1af65SNick Piggin 			copied = 0;
1496bfc1af65SNick Piggin 		page_zero_new_buffers(page, from+copied, to);
1497bfc1af65SNick Piggin 	}
1498ac27a0ecSDave Kleikamp 
1499ac27a0ecSDave Kleikamp 	ret = walk_page_buffers(handle, page_buffers(page), from,
1500bfc1af65SNick Piggin 				to, &partial, write_end_fn);
1501ac27a0ecSDave Kleikamp 	if (!partial)
1502ac27a0ecSDave Kleikamp 		SetPageUptodate(page);
1503bfc1af65SNick Piggin 	if (pos+copied > inode->i_size)
1504bfc1af65SNick Piggin 		i_size_write(inode, pos+copied);
1505617ba13bSMingming Cao 	EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
1506617ba13bSMingming Cao 	if (inode->i_size > EXT4_I(inode)->i_disksize) {
1507617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
1508617ba13bSMingming Cao 		ret2 = ext4_mark_inode_dirty(handle, inode);
1509ac27a0ecSDave Kleikamp 		if (!ret)
1510ac27a0ecSDave Kleikamp 			ret = ret2;
1511ac27a0ecSDave Kleikamp 	}
1512bfc1af65SNick Piggin 
1513cf108bcaSJan Kara 	unlock_page(page);
1514617ba13bSMingming Cao 	ret2 = ext4_journal_stop(handle);
1515ac27a0ecSDave Kleikamp 	if (!ret)
1516ac27a0ecSDave Kleikamp 		ret = ret2;
1517bfc1af65SNick Piggin 	page_cache_release(page);
1518bfc1af65SNick Piggin 
1519bfc1af65SNick Piggin 	return ret ? ret : copied;
1520ac27a0ecSDave Kleikamp }
1521d2a17637SMingming Cao 
1522d2a17637SMingming Cao static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1523d2a17637SMingming Cao {
1524d2a17637SMingming Cao        struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1525d2a17637SMingming Cao        unsigned long md_needed, mdblocks, total = 0;
1526d2a17637SMingming Cao 
1527d2a17637SMingming Cao 	/*
1528d2a17637SMingming Cao 	 * recalculate the amount of metadata blocks to reserve
1529d2a17637SMingming Cao 	 * in order to allocate nrblocks
1530d2a17637SMingming Cao 	 * worse case is one extent per block
1531d2a17637SMingming Cao 	 */
1532d2a17637SMingming Cao 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1533d2a17637SMingming Cao 	total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1534d2a17637SMingming Cao 	mdblocks = ext4_calc_metadata_amount(inode, total);
1535d2a17637SMingming Cao 	BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1536d2a17637SMingming Cao 
1537d2a17637SMingming Cao 	md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1538d2a17637SMingming Cao 	total = md_needed + nrblocks;
1539d2a17637SMingming Cao 
1540*a30d542aSAneesh Kumar K.V 	if (ext4_claim_free_blocks(sbi, total)) {
1541d2a17637SMingming Cao 		spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1542d2a17637SMingming Cao 		return -ENOSPC;
1543d2a17637SMingming Cao 	}
1544d2a17637SMingming Cao 	EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1545d2a17637SMingming Cao 	EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1546d2a17637SMingming Cao 
1547d2a17637SMingming Cao 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1548d2a17637SMingming Cao 	return 0;       /* success */
1549d2a17637SMingming Cao }
1550d2a17637SMingming Cao 
155112219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free)
1552d2a17637SMingming Cao {
1553d2a17637SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1554d2a17637SMingming Cao 	int total, mdb, mdb_free, release;
1555d2a17637SMingming Cao 
1556cd213226SMingming Cao 	if (!to_free)
1557cd213226SMingming Cao 		return;		/* Nothing to release, exit */
1558cd213226SMingming Cao 
1559d2a17637SMingming Cao 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1560cd213226SMingming Cao 
1561cd213226SMingming Cao 	if (!EXT4_I(inode)->i_reserved_data_blocks) {
1562cd213226SMingming Cao 		/*
1563cd213226SMingming Cao 		 * if there is no reserved blocks, but we try to free some
1564cd213226SMingming Cao 		 * then the counter is messed up somewhere.
1565cd213226SMingming Cao 		 * but since this function is called from invalidate
1566cd213226SMingming Cao 		 * page, it's harmless to return without any action
1567cd213226SMingming Cao 		 */
1568cd213226SMingming Cao 		printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1569cd213226SMingming Cao 			    "blocks for inode %lu, but there is no reserved "
1570cd213226SMingming Cao 			    "data blocks\n", to_free, inode->i_ino);
1571cd213226SMingming Cao 		spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1572cd213226SMingming Cao 		return;
1573cd213226SMingming Cao 	}
1574cd213226SMingming Cao 
1575d2a17637SMingming Cao 	/* recalculate the number of metablocks still need to be reserved */
157612219aeaSAneesh Kumar K.V 	total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
1577d2a17637SMingming Cao 	mdb = ext4_calc_metadata_amount(inode, total);
1578d2a17637SMingming Cao 
1579d2a17637SMingming Cao 	/* figure out how many metablocks to release */
1580d2a17637SMingming Cao 	BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1581d2a17637SMingming Cao 	mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1582d2a17637SMingming Cao 
1583d2a17637SMingming Cao 	release = to_free + mdb_free;
1584d2a17637SMingming Cao 
1585d2a17637SMingming Cao 	/* update fs free blocks counter for truncate case */
1586d2a17637SMingming Cao 	percpu_counter_add(&sbi->s_freeblocks_counter, release);
1587d2a17637SMingming Cao 
1588d2a17637SMingming Cao 	/* update per-inode reservations */
158912219aeaSAneesh Kumar K.V 	BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
159012219aeaSAneesh Kumar K.V 	EXT4_I(inode)->i_reserved_data_blocks -= to_free;
1591d2a17637SMingming Cao 
1592d2a17637SMingming Cao 	BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1593d2a17637SMingming Cao 	EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1594d2a17637SMingming Cao 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1595d2a17637SMingming Cao }
1596d2a17637SMingming Cao 
1597d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page,
1598d2a17637SMingming Cao 						unsigned long offset)
1599d2a17637SMingming Cao {
1600d2a17637SMingming Cao 	int to_release = 0;
1601d2a17637SMingming Cao 	struct buffer_head *head, *bh;
1602d2a17637SMingming Cao 	unsigned int curr_off = 0;
1603d2a17637SMingming Cao 
1604d2a17637SMingming Cao 	head = page_buffers(page);
1605d2a17637SMingming Cao 	bh = head;
1606d2a17637SMingming Cao 	do {
1607d2a17637SMingming Cao 		unsigned int next_off = curr_off + bh->b_size;
1608d2a17637SMingming Cao 
1609d2a17637SMingming Cao 		if ((offset <= curr_off) && (buffer_delay(bh))) {
1610d2a17637SMingming Cao 			to_release++;
1611d2a17637SMingming Cao 			clear_buffer_delay(bh);
1612d2a17637SMingming Cao 		}
1613d2a17637SMingming Cao 		curr_off = next_off;
1614d2a17637SMingming Cao 	} while ((bh = bh->b_this_page) != head);
161512219aeaSAneesh Kumar K.V 	ext4_da_release_space(page->mapping->host, to_release);
1616d2a17637SMingming Cao }
1617ac27a0ecSDave Kleikamp 
1618ac27a0ecSDave Kleikamp /*
161964769240SAlex Tomas  * Delayed allocation stuff
162064769240SAlex Tomas  */
162164769240SAlex Tomas 
162264769240SAlex Tomas struct mpage_da_data {
162364769240SAlex Tomas 	struct inode *inode;
162464769240SAlex Tomas 	struct buffer_head lbh;			/* extent of blocks */
162564769240SAlex Tomas 	unsigned long first_page, next_page;	/* extent of pages */
162664769240SAlex Tomas 	get_block_t *get_block;
162764769240SAlex Tomas 	struct writeback_control *wbc;
1628a1d6cc56SAneesh Kumar K.V 	int io_done;
1629a1d6cc56SAneesh Kumar K.V 	long pages_written;
163064769240SAlex Tomas };
163164769240SAlex Tomas 
163264769240SAlex Tomas /*
163364769240SAlex Tomas  * mpage_da_submit_io - walks through extent of pages and try to write
1634a1d6cc56SAneesh Kumar K.V  * them with writepage() call back
163564769240SAlex Tomas  *
163664769240SAlex Tomas  * @mpd->inode: inode
163764769240SAlex Tomas  * @mpd->first_page: first page of the extent
163864769240SAlex Tomas  * @mpd->next_page: page after the last page of the extent
163964769240SAlex Tomas  * @mpd->get_block: the filesystem's block mapper function
164064769240SAlex Tomas  *
164164769240SAlex Tomas  * By the time mpage_da_submit_io() is called we expect all blocks
164264769240SAlex Tomas  * to be allocated. this may be wrong if allocation failed.
164364769240SAlex Tomas  *
164464769240SAlex Tomas  * As pages are already locked by write_cache_pages(), we can't use it
164564769240SAlex Tomas  */
164664769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd)
164764769240SAlex Tomas {
164864769240SAlex Tomas 	struct address_space *mapping = mpd->inode->i_mapping;
164964769240SAlex Tomas 	int ret = 0, err, nr_pages, i;
165064769240SAlex Tomas 	unsigned long index, end;
165164769240SAlex Tomas 	struct pagevec pvec;
165264769240SAlex Tomas 
165364769240SAlex Tomas 	BUG_ON(mpd->next_page <= mpd->first_page);
165464769240SAlex Tomas 	pagevec_init(&pvec, 0);
165564769240SAlex Tomas 	index = mpd->first_page;
165664769240SAlex Tomas 	end = mpd->next_page - 1;
165764769240SAlex Tomas 
165864769240SAlex Tomas 	while (index <= end) {
165964769240SAlex Tomas 		/* XXX: optimize tail */
166064769240SAlex Tomas 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
166164769240SAlex Tomas 		if (nr_pages == 0)
166264769240SAlex Tomas 			break;
166364769240SAlex Tomas 		for (i = 0; i < nr_pages; i++) {
166464769240SAlex Tomas 			struct page *page = pvec.pages[i];
166564769240SAlex Tomas 
166664769240SAlex Tomas 			index = page->index;
166764769240SAlex Tomas 			if (index > end)
166864769240SAlex Tomas 				break;
166964769240SAlex Tomas 			index++;
167064769240SAlex Tomas 
1671a1d6cc56SAneesh Kumar K.V 			err = mapping->a_ops->writepage(page, mpd->wbc);
1672a1d6cc56SAneesh Kumar K.V 			if (!err)
1673a1d6cc56SAneesh Kumar K.V 				mpd->pages_written++;
167464769240SAlex Tomas 			/*
167564769240SAlex Tomas 			 * In error case, we have to continue because
167664769240SAlex Tomas 			 * remaining pages are still locked
167764769240SAlex Tomas 			 * XXX: unlock and re-dirty them?
167864769240SAlex Tomas 			 */
167964769240SAlex Tomas 			if (ret == 0)
168064769240SAlex Tomas 				ret = err;
168164769240SAlex Tomas 		}
168264769240SAlex Tomas 		pagevec_release(&pvec);
168364769240SAlex Tomas 	}
168464769240SAlex Tomas 	return ret;
168564769240SAlex Tomas }
168664769240SAlex Tomas 
168764769240SAlex Tomas /*
168864769240SAlex Tomas  * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
168964769240SAlex Tomas  *
169064769240SAlex Tomas  * @mpd->inode - inode to walk through
169164769240SAlex Tomas  * @exbh->b_blocknr - first block on a disk
169264769240SAlex Tomas  * @exbh->b_size - amount of space in bytes
169364769240SAlex Tomas  * @logical - first logical block to start assignment with
169464769240SAlex Tomas  *
169564769240SAlex Tomas  * the function goes through all passed space and put actual disk
169664769240SAlex Tomas  * block numbers into buffer heads, dropping BH_Delay
169764769240SAlex Tomas  */
169864769240SAlex Tomas static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
169964769240SAlex Tomas 				 struct buffer_head *exbh)
170064769240SAlex Tomas {
170164769240SAlex Tomas 	struct inode *inode = mpd->inode;
170264769240SAlex Tomas 	struct address_space *mapping = inode->i_mapping;
170364769240SAlex Tomas 	int blocks = exbh->b_size >> inode->i_blkbits;
170464769240SAlex Tomas 	sector_t pblock = exbh->b_blocknr, cur_logical;
170564769240SAlex Tomas 	struct buffer_head *head, *bh;
1706a1d6cc56SAneesh Kumar K.V 	pgoff_t index, end;
170764769240SAlex Tomas 	struct pagevec pvec;
170864769240SAlex Tomas 	int nr_pages, i;
170964769240SAlex Tomas 
171064769240SAlex Tomas 	index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
171164769240SAlex Tomas 	end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
171264769240SAlex Tomas 	cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
171364769240SAlex Tomas 
171464769240SAlex Tomas 	pagevec_init(&pvec, 0);
171564769240SAlex Tomas 
171664769240SAlex Tomas 	while (index <= end) {
171764769240SAlex Tomas 		/* XXX: optimize tail */
171864769240SAlex Tomas 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
171964769240SAlex Tomas 		if (nr_pages == 0)
172064769240SAlex Tomas 			break;
172164769240SAlex Tomas 		for (i = 0; i < nr_pages; i++) {
172264769240SAlex Tomas 			struct page *page = pvec.pages[i];
172364769240SAlex Tomas 
172464769240SAlex Tomas 			index = page->index;
172564769240SAlex Tomas 			if (index > end)
172664769240SAlex Tomas 				break;
172764769240SAlex Tomas 			index++;
172864769240SAlex Tomas 
172964769240SAlex Tomas 			BUG_ON(!PageLocked(page));
173064769240SAlex Tomas 			BUG_ON(PageWriteback(page));
173164769240SAlex Tomas 			BUG_ON(!page_has_buffers(page));
173264769240SAlex Tomas 
173364769240SAlex Tomas 			bh = page_buffers(page);
173464769240SAlex Tomas 			head = bh;
173564769240SAlex Tomas 
173664769240SAlex Tomas 			/* skip blocks out of the range */
173764769240SAlex Tomas 			do {
173864769240SAlex Tomas 				if (cur_logical >= logical)
173964769240SAlex Tomas 					break;
174064769240SAlex Tomas 				cur_logical++;
174164769240SAlex Tomas 			} while ((bh = bh->b_this_page) != head);
174264769240SAlex Tomas 
174364769240SAlex Tomas 			do {
174464769240SAlex Tomas 				if (cur_logical >= logical + blocks)
174564769240SAlex Tomas 					break;
174664769240SAlex Tomas 				if (buffer_delay(bh)) {
174764769240SAlex Tomas 					bh->b_blocknr = pblock;
174864769240SAlex Tomas 					clear_buffer_delay(bh);
1749bf068ee2SAneesh Kumar K.V 					bh->b_bdev = inode->i_sb->s_bdev;
1750bf068ee2SAneesh Kumar K.V 				} else if (buffer_unwritten(bh)) {
1751bf068ee2SAneesh Kumar K.V 					bh->b_blocknr = pblock;
1752bf068ee2SAneesh Kumar K.V 					clear_buffer_unwritten(bh);
1753bf068ee2SAneesh Kumar K.V 					set_buffer_mapped(bh);
1754bf068ee2SAneesh Kumar K.V 					set_buffer_new(bh);
1755bf068ee2SAneesh Kumar K.V 					bh->b_bdev = inode->i_sb->s_bdev;
175661628a3fSMingming Cao 				} else if (buffer_mapped(bh))
175764769240SAlex Tomas 					BUG_ON(bh->b_blocknr != pblock);
175864769240SAlex Tomas 
175964769240SAlex Tomas 				cur_logical++;
176064769240SAlex Tomas 				pblock++;
176164769240SAlex Tomas 			} while ((bh = bh->b_this_page) != head);
176264769240SAlex Tomas 		}
176364769240SAlex Tomas 		pagevec_release(&pvec);
176464769240SAlex Tomas 	}
176564769240SAlex Tomas }
176664769240SAlex Tomas 
176764769240SAlex Tomas 
176864769240SAlex Tomas /*
176964769240SAlex Tomas  * __unmap_underlying_blocks - just a helper function to unmap
177064769240SAlex Tomas  * set of blocks described by @bh
177164769240SAlex Tomas  */
177264769240SAlex Tomas static inline void __unmap_underlying_blocks(struct inode *inode,
177364769240SAlex Tomas 					     struct buffer_head *bh)
177464769240SAlex Tomas {
177564769240SAlex Tomas 	struct block_device *bdev = inode->i_sb->s_bdev;
177664769240SAlex Tomas 	int blocks, i;
177764769240SAlex Tomas 
177864769240SAlex Tomas 	blocks = bh->b_size >> inode->i_blkbits;
177964769240SAlex Tomas 	for (i = 0; i < blocks; i++)
178064769240SAlex Tomas 		unmap_underlying_metadata(bdev, bh->b_blocknr + i);
178164769240SAlex Tomas }
178264769240SAlex Tomas 
1783c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
1784c4a0c46eSAneesh Kumar K.V 					sector_t logical, long blk_cnt)
1785c4a0c46eSAneesh Kumar K.V {
1786c4a0c46eSAneesh Kumar K.V 	int nr_pages, i;
1787c4a0c46eSAneesh Kumar K.V 	pgoff_t index, end;
1788c4a0c46eSAneesh Kumar K.V 	struct pagevec pvec;
1789c4a0c46eSAneesh Kumar K.V 	struct inode *inode = mpd->inode;
1790c4a0c46eSAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
1791c4a0c46eSAneesh Kumar K.V 
1792c4a0c46eSAneesh Kumar K.V 	index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1793c4a0c46eSAneesh Kumar K.V 	end   = (logical + blk_cnt - 1) >>
1794c4a0c46eSAneesh Kumar K.V 				(PAGE_CACHE_SHIFT - inode->i_blkbits);
1795c4a0c46eSAneesh Kumar K.V 	while (index <= end) {
1796c4a0c46eSAneesh Kumar K.V 		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1797c4a0c46eSAneesh Kumar K.V 		if (nr_pages == 0)
1798c4a0c46eSAneesh Kumar K.V 			break;
1799c4a0c46eSAneesh Kumar K.V 		for (i = 0; i < nr_pages; i++) {
1800c4a0c46eSAneesh Kumar K.V 			struct page *page = pvec.pages[i];
1801c4a0c46eSAneesh Kumar K.V 			index = page->index;
1802c4a0c46eSAneesh Kumar K.V 			if (index > end)
1803c4a0c46eSAneesh Kumar K.V 				break;
1804c4a0c46eSAneesh Kumar K.V 			index++;
1805c4a0c46eSAneesh Kumar K.V 
1806c4a0c46eSAneesh Kumar K.V 			BUG_ON(!PageLocked(page));
1807c4a0c46eSAneesh Kumar K.V 			BUG_ON(PageWriteback(page));
1808c4a0c46eSAneesh Kumar K.V 			block_invalidatepage(page, 0);
1809c4a0c46eSAneesh Kumar K.V 			ClearPageUptodate(page);
1810c4a0c46eSAneesh Kumar K.V 			unlock_page(page);
1811c4a0c46eSAneesh Kumar K.V 		}
1812c4a0c46eSAneesh Kumar K.V 	}
1813c4a0c46eSAneesh Kumar K.V 	return;
1814c4a0c46eSAneesh Kumar K.V }
1815c4a0c46eSAneesh Kumar K.V 
181664769240SAlex Tomas /*
181764769240SAlex Tomas  * mpage_da_map_blocks - go through given space
181864769240SAlex Tomas  *
181964769240SAlex Tomas  * @mpd->lbh - bh describing space
182064769240SAlex Tomas  * @mpd->get_block - the filesystem's block mapper function
182164769240SAlex Tomas  *
182264769240SAlex Tomas  * The function skips space we know is already mapped to disk blocks.
182364769240SAlex Tomas  *
182464769240SAlex Tomas  */
1825c4a0c46eSAneesh Kumar K.V static int  mpage_da_map_blocks(struct mpage_da_data *mpd)
182664769240SAlex Tomas {
1827a1d6cc56SAneesh Kumar K.V 	int err = 0;
182864769240SAlex Tomas 	struct buffer_head *lbh = &mpd->lbh;
182964769240SAlex Tomas 	sector_t next = lbh->b_blocknr;
183064769240SAlex Tomas 	struct buffer_head new;
183164769240SAlex Tomas 
183264769240SAlex Tomas 	/*
183364769240SAlex Tomas 	 * We consider only non-mapped and non-allocated blocks
183464769240SAlex Tomas 	 */
183564769240SAlex Tomas 	if (buffer_mapped(lbh) && !buffer_delay(lbh))
1836c4a0c46eSAneesh Kumar K.V 		return 0;
183764769240SAlex Tomas 
183864769240SAlex Tomas 	new.b_state = lbh->b_state;
183964769240SAlex Tomas 	new.b_blocknr = 0;
1840a1d6cc56SAneesh Kumar K.V 	new.b_size = lbh->b_size;
1841a1d6cc56SAneesh Kumar K.V 
184264769240SAlex Tomas 	/*
1843a1d6cc56SAneesh Kumar K.V 	 * If we didn't accumulate anything
1844a1d6cc56SAneesh Kumar K.V 	 * to write simply return
184564769240SAlex Tomas 	 */
1846a1d6cc56SAneesh Kumar K.V 	if (!new.b_size)
1847c4a0c46eSAneesh Kumar K.V 		return 0;
1848a1d6cc56SAneesh Kumar K.V 	err = mpd->get_block(mpd->inode, next, &new, 1);
1849c4a0c46eSAneesh Kumar K.V 	if (err) {
1850c4a0c46eSAneesh Kumar K.V 
1851c4a0c46eSAneesh Kumar K.V 		/* If get block returns with error
1852c4a0c46eSAneesh Kumar K.V 		 * we simply return. Later writepage
1853c4a0c46eSAneesh Kumar K.V 		 * will redirty the page and writepages
1854c4a0c46eSAneesh Kumar K.V 		 * will find the dirty page again
1855c4a0c46eSAneesh Kumar K.V 		 */
1856c4a0c46eSAneesh Kumar K.V 		if (err == -EAGAIN)
1857c4a0c46eSAneesh Kumar K.V 			return 0;
1858c4a0c46eSAneesh Kumar K.V 		/*
1859c4a0c46eSAneesh Kumar K.V 		 * get block failure will cause us
1860c4a0c46eSAneesh Kumar K.V 		 * to loop in writepages. Because
1861c4a0c46eSAneesh Kumar K.V 		 * a_ops->writepage won't be able to
1862c4a0c46eSAneesh Kumar K.V 		 * make progress. The page will be redirtied
1863c4a0c46eSAneesh Kumar K.V 		 * by writepage and writepages will again
1864c4a0c46eSAneesh Kumar K.V 		 * try to write the same.
1865c4a0c46eSAneesh Kumar K.V 		 */
1866c4a0c46eSAneesh Kumar K.V 		printk(KERN_EMERG "%s block allocation failed for inode %lu "
1867c4a0c46eSAneesh Kumar K.V 				  "at logical offset %llu with max blocks "
1868c4a0c46eSAneesh Kumar K.V 				  "%zd with error %d\n",
1869c4a0c46eSAneesh Kumar K.V 				  __func__, mpd->inode->i_ino,
1870c4a0c46eSAneesh Kumar K.V 				  (unsigned long long)next,
1871c4a0c46eSAneesh Kumar K.V 				  lbh->b_size >> mpd->inode->i_blkbits, err);
1872c4a0c46eSAneesh Kumar K.V 		printk(KERN_EMERG "This should not happen.!! "
1873c4a0c46eSAneesh Kumar K.V 					"Data will be lost\n");
1874c4a0c46eSAneesh Kumar K.V 		/* invlaidate all the pages */
1875c4a0c46eSAneesh Kumar K.V 		ext4_da_block_invalidatepages(mpd, next,
1876c4a0c46eSAneesh Kumar K.V 				lbh->b_size >> mpd->inode->i_blkbits);
1877c4a0c46eSAneesh Kumar K.V 		return err;
1878c4a0c46eSAneesh Kumar K.V 	}
187964769240SAlex Tomas 	BUG_ON(new.b_size == 0);
188064769240SAlex Tomas 
188164769240SAlex Tomas 	if (buffer_new(&new))
188264769240SAlex Tomas 		__unmap_underlying_blocks(mpd->inode, &new);
188364769240SAlex Tomas 
188464769240SAlex Tomas 	/*
188564769240SAlex Tomas 	 * If blocks are delayed marked, we need to
188664769240SAlex Tomas 	 * put actual blocknr and drop delayed bit
188764769240SAlex Tomas 	 */
1888bf068ee2SAneesh Kumar K.V 	if (buffer_delay(lbh) || buffer_unwritten(lbh))
188964769240SAlex Tomas 		mpage_put_bnr_to_bhs(mpd, next, &new);
189064769240SAlex Tomas 
1891c4a0c46eSAneesh Kumar K.V 	return 0;
189264769240SAlex Tomas }
189364769240SAlex Tomas 
1894bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1895bf068ee2SAneesh Kumar K.V 		(1 << BH_Delay) | (1 << BH_Unwritten))
189664769240SAlex Tomas 
189764769240SAlex Tomas /*
189864769240SAlex Tomas  * mpage_add_bh_to_extent - try to add one more block to extent of blocks
189964769240SAlex Tomas  *
190064769240SAlex Tomas  * @mpd->lbh - extent of blocks
190164769240SAlex Tomas  * @logical - logical number of the block in the file
190264769240SAlex Tomas  * @bh - bh of the block (used to access block's state)
190364769240SAlex Tomas  *
190464769240SAlex Tomas  * the function is used to collect contig. blocks in same state
190564769240SAlex Tomas  */
190664769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
190764769240SAlex Tomas 				   sector_t logical, struct buffer_head *bh)
190864769240SAlex Tomas {
190964769240SAlex Tomas 	sector_t next;
1910525f4ed8SMingming Cao 	size_t b_size = bh->b_size;
1911525f4ed8SMingming Cao 	struct buffer_head *lbh = &mpd->lbh;
1912525f4ed8SMingming Cao 	int nrblocks = lbh->b_size >> mpd->inode->i_blkbits;
191364769240SAlex Tomas 
1914525f4ed8SMingming Cao 	/* check if thereserved journal credits might overflow */
1915525f4ed8SMingming Cao 	if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
1916525f4ed8SMingming Cao 		if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1917525f4ed8SMingming Cao 			/*
1918525f4ed8SMingming Cao 			 * With non-extent format we are limited by the journal
1919525f4ed8SMingming Cao 			 * credit available.  Total credit needed to insert
1920525f4ed8SMingming Cao 			 * nrblocks contiguous blocks is dependent on the
1921525f4ed8SMingming Cao 			 * nrblocks.  So limit nrblocks.
1922525f4ed8SMingming Cao 			 */
1923525f4ed8SMingming Cao 			goto flush_it;
1924525f4ed8SMingming Cao 		} else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
1925525f4ed8SMingming Cao 				EXT4_MAX_TRANS_DATA) {
1926525f4ed8SMingming Cao 			/*
1927525f4ed8SMingming Cao 			 * Adding the new buffer_head would make it cross the
1928525f4ed8SMingming Cao 			 * allowed limit for which we have journal credit
1929525f4ed8SMingming Cao 			 * reserved. So limit the new bh->b_size
1930525f4ed8SMingming Cao 			 */
1931525f4ed8SMingming Cao 			b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
1932525f4ed8SMingming Cao 						mpd->inode->i_blkbits;
1933525f4ed8SMingming Cao 			/* we will do mpage_da_submit_io in the next loop */
1934525f4ed8SMingming Cao 		}
1935525f4ed8SMingming Cao 	}
193664769240SAlex Tomas 	/*
193764769240SAlex Tomas 	 * First block in the extent
193864769240SAlex Tomas 	 */
193964769240SAlex Tomas 	if (lbh->b_size == 0) {
194064769240SAlex Tomas 		lbh->b_blocknr = logical;
1941525f4ed8SMingming Cao 		lbh->b_size = b_size;
194264769240SAlex Tomas 		lbh->b_state = bh->b_state & BH_FLAGS;
194364769240SAlex Tomas 		return;
194464769240SAlex Tomas 	}
194564769240SAlex Tomas 
1946525f4ed8SMingming Cao 	next = lbh->b_blocknr + nrblocks;
194764769240SAlex Tomas 	/*
194864769240SAlex Tomas 	 * Can we merge the block to our big extent?
194964769240SAlex Tomas 	 */
195064769240SAlex Tomas 	if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) {
1951525f4ed8SMingming Cao 		lbh->b_size += b_size;
195264769240SAlex Tomas 		return;
195364769240SAlex Tomas 	}
195464769240SAlex Tomas 
1955525f4ed8SMingming Cao flush_it:
195664769240SAlex Tomas 	/*
195764769240SAlex Tomas 	 * We couldn't merge the block to our extent, so we
195864769240SAlex Tomas 	 * need to flush current  extent and start new one
195964769240SAlex Tomas 	 */
1960c4a0c46eSAneesh Kumar K.V 	if (mpage_da_map_blocks(mpd) == 0)
1961a1d6cc56SAneesh Kumar K.V 		mpage_da_submit_io(mpd);
1962a1d6cc56SAneesh Kumar K.V 	mpd->io_done = 1;
1963a1d6cc56SAneesh Kumar K.V 	return;
196464769240SAlex Tomas }
196564769240SAlex Tomas 
196664769240SAlex Tomas /*
196764769240SAlex Tomas  * __mpage_da_writepage - finds extent of pages and blocks
196864769240SAlex Tomas  *
196964769240SAlex Tomas  * @page: page to consider
197064769240SAlex Tomas  * @wbc: not used, we just follow rules
197164769240SAlex Tomas  * @data: context
197264769240SAlex Tomas  *
197364769240SAlex Tomas  * The function finds extents of pages and scan them for all blocks.
197464769240SAlex Tomas  */
197564769240SAlex Tomas static int __mpage_da_writepage(struct page *page,
197664769240SAlex Tomas 				struct writeback_control *wbc, void *data)
197764769240SAlex Tomas {
197864769240SAlex Tomas 	struct mpage_da_data *mpd = data;
197964769240SAlex Tomas 	struct inode *inode = mpd->inode;
198064769240SAlex Tomas 	struct buffer_head *bh, *head, fake;
198164769240SAlex Tomas 	sector_t logical;
198264769240SAlex Tomas 
1983a1d6cc56SAneesh Kumar K.V 	if (mpd->io_done) {
1984a1d6cc56SAneesh Kumar K.V 		/*
1985a1d6cc56SAneesh Kumar K.V 		 * Rest of the page in the page_vec
1986a1d6cc56SAneesh Kumar K.V 		 * redirty then and skip then. We will
1987a1d6cc56SAneesh Kumar K.V 		 * try to to write them again after
1988a1d6cc56SAneesh Kumar K.V 		 * starting a new transaction
1989a1d6cc56SAneesh Kumar K.V 		 */
1990a1d6cc56SAneesh Kumar K.V 		redirty_page_for_writepage(wbc, page);
1991a1d6cc56SAneesh Kumar K.V 		unlock_page(page);
1992a1d6cc56SAneesh Kumar K.V 		return MPAGE_DA_EXTENT_TAIL;
1993a1d6cc56SAneesh Kumar K.V 	}
199464769240SAlex Tomas 	/*
199564769240SAlex Tomas 	 * Can we merge this page to current extent?
199664769240SAlex Tomas 	 */
199764769240SAlex Tomas 	if (mpd->next_page != page->index) {
199864769240SAlex Tomas 		/*
199964769240SAlex Tomas 		 * Nope, we can't. So, we map non-allocated blocks
2000a1d6cc56SAneesh Kumar K.V 		 * and start IO on them using writepage()
200164769240SAlex Tomas 		 */
200264769240SAlex Tomas 		if (mpd->next_page != mpd->first_page) {
2003c4a0c46eSAneesh Kumar K.V 			if (mpage_da_map_blocks(mpd) == 0)
200464769240SAlex Tomas 				mpage_da_submit_io(mpd);
2005a1d6cc56SAneesh Kumar K.V 			/*
2006a1d6cc56SAneesh Kumar K.V 			 * skip rest of the page in the page_vec
2007a1d6cc56SAneesh Kumar K.V 			 */
2008a1d6cc56SAneesh Kumar K.V 			mpd->io_done = 1;
2009a1d6cc56SAneesh Kumar K.V 			redirty_page_for_writepage(wbc, page);
2010a1d6cc56SAneesh Kumar K.V 			unlock_page(page);
2011a1d6cc56SAneesh Kumar K.V 			return MPAGE_DA_EXTENT_TAIL;
201264769240SAlex Tomas 		}
201364769240SAlex Tomas 
201464769240SAlex Tomas 		/*
201564769240SAlex Tomas 		 * Start next extent of pages ...
201664769240SAlex Tomas 		 */
201764769240SAlex Tomas 		mpd->first_page = page->index;
201864769240SAlex Tomas 
201964769240SAlex Tomas 		/*
202064769240SAlex Tomas 		 * ... and blocks
202164769240SAlex Tomas 		 */
202264769240SAlex Tomas 		mpd->lbh.b_size = 0;
202364769240SAlex Tomas 		mpd->lbh.b_state = 0;
202464769240SAlex Tomas 		mpd->lbh.b_blocknr = 0;
202564769240SAlex Tomas 	}
202664769240SAlex Tomas 
202764769240SAlex Tomas 	mpd->next_page = page->index + 1;
202864769240SAlex Tomas 	logical = (sector_t) page->index <<
202964769240SAlex Tomas 		  (PAGE_CACHE_SHIFT - inode->i_blkbits);
203064769240SAlex Tomas 
203164769240SAlex Tomas 	if (!page_has_buffers(page)) {
203264769240SAlex Tomas 		/*
203364769240SAlex Tomas 		 * There is no attached buffer heads yet (mmap?)
203464769240SAlex Tomas 		 * we treat the page asfull of dirty blocks
203564769240SAlex Tomas 		 */
203664769240SAlex Tomas 		bh = &fake;
203764769240SAlex Tomas 		bh->b_size = PAGE_CACHE_SIZE;
203864769240SAlex Tomas 		bh->b_state = 0;
203964769240SAlex Tomas 		set_buffer_dirty(bh);
204064769240SAlex Tomas 		set_buffer_uptodate(bh);
204164769240SAlex Tomas 		mpage_add_bh_to_extent(mpd, logical, bh);
2042a1d6cc56SAneesh Kumar K.V 		if (mpd->io_done)
2043a1d6cc56SAneesh Kumar K.V 			return MPAGE_DA_EXTENT_TAIL;
204464769240SAlex Tomas 	} else {
204564769240SAlex Tomas 		/*
204664769240SAlex Tomas 		 * Page with regular buffer heads, just add all dirty ones
204764769240SAlex Tomas 		 */
204864769240SAlex Tomas 		head = page_buffers(page);
204964769240SAlex Tomas 		bh = head;
205064769240SAlex Tomas 		do {
205164769240SAlex Tomas 			BUG_ON(buffer_locked(bh));
2052a1d6cc56SAneesh Kumar K.V 			if (buffer_dirty(bh) &&
2053a1d6cc56SAneesh Kumar K.V 				(!buffer_mapped(bh) || buffer_delay(bh))) {
205464769240SAlex Tomas 				mpage_add_bh_to_extent(mpd, logical, bh);
2055a1d6cc56SAneesh Kumar K.V 				if (mpd->io_done)
2056a1d6cc56SAneesh Kumar K.V 					return MPAGE_DA_EXTENT_TAIL;
2057a1d6cc56SAneesh Kumar K.V 			}
205864769240SAlex Tomas 			logical++;
205964769240SAlex Tomas 		} while ((bh = bh->b_this_page) != head);
206064769240SAlex Tomas 	}
206164769240SAlex Tomas 
206264769240SAlex Tomas 	return 0;
206364769240SAlex Tomas }
206464769240SAlex Tomas 
206564769240SAlex Tomas /*
206664769240SAlex Tomas  * mpage_da_writepages - walk the list of dirty pages of the given
206764769240SAlex Tomas  * address space, allocates non-allocated blocks, maps newly-allocated
206864769240SAlex Tomas  * blocks to existing bhs and issue IO them
206964769240SAlex Tomas  *
207064769240SAlex Tomas  * @mapping: address space structure to write
207164769240SAlex Tomas  * @wbc: subtract the number of written pages from *@wbc->nr_to_write
207264769240SAlex Tomas  * @get_block: the filesystem's block mapper function.
207364769240SAlex Tomas  *
207464769240SAlex Tomas  * This is a library function, which implements the writepages()
207564769240SAlex Tomas  * address_space_operation.
207664769240SAlex Tomas  */
207764769240SAlex Tomas static int mpage_da_writepages(struct address_space *mapping,
207864769240SAlex Tomas 			       struct writeback_control *wbc,
207964769240SAlex Tomas 			       get_block_t get_block)
208064769240SAlex Tomas {
208164769240SAlex Tomas 	struct mpage_da_data mpd;
2082a1d6cc56SAneesh Kumar K.V 	long to_write;
208364769240SAlex Tomas 	int ret;
208464769240SAlex Tomas 
208564769240SAlex Tomas 	if (!get_block)
208664769240SAlex Tomas 		return generic_writepages(mapping, wbc);
208764769240SAlex Tomas 
208864769240SAlex Tomas 	mpd.wbc = wbc;
208964769240SAlex Tomas 	mpd.inode = mapping->host;
209064769240SAlex Tomas 	mpd.lbh.b_size = 0;
209164769240SAlex Tomas 	mpd.lbh.b_state = 0;
209264769240SAlex Tomas 	mpd.lbh.b_blocknr = 0;
209364769240SAlex Tomas 	mpd.first_page = 0;
209464769240SAlex Tomas 	mpd.next_page = 0;
209564769240SAlex Tomas 	mpd.get_block = get_block;
2096a1d6cc56SAneesh Kumar K.V 	mpd.io_done = 0;
2097a1d6cc56SAneesh Kumar K.V 	mpd.pages_written = 0;
2098a1d6cc56SAneesh Kumar K.V 
2099a1d6cc56SAneesh Kumar K.V 	to_write = wbc->nr_to_write;
210064769240SAlex Tomas 
210164769240SAlex Tomas 	ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, &mpd);
210264769240SAlex Tomas 
210364769240SAlex Tomas 	/*
210464769240SAlex Tomas 	 * Handle last extent of pages
210564769240SAlex Tomas 	 */
2106a1d6cc56SAneesh Kumar K.V 	if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2107c4a0c46eSAneesh Kumar K.V 		if (mpage_da_map_blocks(&mpd) == 0)
210864769240SAlex Tomas 			mpage_da_submit_io(&mpd);
210964769240SAlex Tomas 	}
211064769240SAlex Tomas 
2111a1d6cc56SAneesh Kumar K.V 	wbc->nr_to_write = to_write - mpd.pages_written;
211264769240SAlex Tomas 	return ret;
211364769240SAlex Tomas }
211464769240SAlex Tomas 
211564769240SAlex Tomas /*
211664769240SAlex Tomas  * this is a special callback for ->write_begin() only
211764769240SAlex Tomas  * it's intention is to return mapped block or reserve space
211864769240SAlex Tomas  */
211964769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
212064769240SAlex Tomas 				  struct buffer_head *bh_result, int create)
212164769240SAlex Tomas {
212264769240SAlex Tomas 	int ret = 0;
212364769240SAlex Tomas 
212464769240SAlex Tomas 	BUG_ON(create == 0);
212564769240SAlex Tomas 	BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
212664769240SAlex Tomas 
212764769240SAlex Tomas 	/*
212864769240SAlex Tomas 	 * first, we need to know whether the block is allocated already
212964769240SAlex Tomas 	 * preallocated blocks are unmapped but should treated
213064769240SAlex Tomas 	 * the same as allocated blocks.
213164769240SAlex Tomas 	 */
2132d2a17637SMingming Cao 	ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1,  bh_result, 0, 0, 0);
2133d2a17637SMingming Cao 	if ((ret == 0) && !buffer_delay(bh_result)) {
2134d2a17637SMingming Cao 		/* the block isn't (pre)allocated yet, let's reserve space */
213564769240SAlex Tomas 		/*
213664769240SAlex Tomas 		 * XXX: __block_prepare_write() unmaps passed block,
213764769240SAlex Tomas 		 * is it OK?
213864769240SAlex Tomas 		 */
2139d2a17637SMingming Cao 		ret = ext4_da_reserve_space(inode, 1);
2140d2a17637SMingming Cao 		if (ret)
2141d2a17637SMingming Cao 			/* not enough space to reserve */
2142d2a17637SMingming Cao 			return ret;
2143d2a17637SMingming Cao 
214464769240SAlex Tomas 		map_bh(bh_result, inode->i_sb, 0);
214564769240SAlex Tomas 		set_buffer_new(bh_result);
214664769240SAlex Tomas 		set_buffer_delay(bh_result);
214764769240SAlex Tomas 	} else if (ret > 0) {
214864769240SAlex Tomas 		bh_result->b_size = (ret << inode->i_blkbits);
214964769240SAlex Tomas 		ret = 0;
215064769240SAlex Tomas 	}
215164769240SAlex Tomas 
215264769240SAlex Tomas 	return ret;
215364769240SAlex Tomas }
2154d2a17637SMingming Cao #define		EXT4_DELALLOC_RSVED	1
215564769240SAlex Tomas static int ext4_da_get_block_write(struct inode *inode, sector_t iblock,
215664769240SAlex Tomas 				   struct buffer_head *bh_result, int create)
215764769240SAlex Tomas {
215861628a3fSMingming Cao 	int ret;
215964769240SAlex Tomas 	unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
216064769240SAlex Tomas 	loff_t disksize = EXT4_I(inode)->i_disksize;
216164769240SAlex Tomas 	handle_t *handle = NULL;
216264769240SAlex Tomas 
216361628a3fSMingming Cao 	handle = ext4_journal_current_handle();
2164f0e6c985SAneesh Kumar K.V 	if (!handle) {
2165f0e6c985SAneesh Kumar K.V 		ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
2166f0e6c985SAneesh Kumar K.V 				   bh_result, 0, 0, 0);
2167f0e6c985SAneesh Kumar K.V 		BUG_ON(!ret);
2168f0e6c985SAneesh Kumar K.V 	} else {
216964769240SAlex Tomas 		ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
2170d2a17637SMingming Cao 				   bh_result, create, 0, EXT4_DELALLOC_RSVED);
2171f0e6c985SAneesh Kumar K.V 	}
2172f0e6c985SAneesh Kumar K.V 
217364769240SAlex Tomas 	if (ret > 0) {
217464769240SAlex Tomas 		bh_result->b_size = (ret << inode->i_blkbits);
217564769240SAlex Tomas 
217664769240SAlex Tomas 		/*
217764769240SAlex Tomas 		 * Update on-disk size along with block allocation
217864769240SAlex Tomas 		 * we don't use 'extend_disksize' as size may change
217964769240SAlex Tomas 		 * within already allocated block -bzzz
218064769240SAlex Tomas 		 */
218164769240SAlex Tomas 		disksize = ((loff_t) iblock + ret) << inode->i_blkbits;
218264769240SAlex Tomas 		if (disksize > i_size_read(inode))
218364769240SAlex Tomas 			disksize = i_size_read(inode);
218464769240SAlex Tomas 		if (disksize > EXT4_I(inode)->i_disksize) {
218564769240SAlex Tomas 			/*
218664769240SAlex Tomas 			 * XXX: replace with spinlock if seen contended -bzzz
218764769240SAlex Tomas 			 */
218864769240SAlex Tomas 			down_write(&EXT4_I(inode)->i_data_sem);
218964769240SAlex Tomas 			if (disksize > EXT4_I(inode)->i_disksize)
219064769240SAlex Tomas 				EXT4_I(inode)->i_disksize = disksize;
219164769240SAlex Tomas 			up_write(&EXT4_I(inode)->i_data_sem);
219264769240SAlex Tomas 
219364769240SAlex Tomas 			if (EXT4_I(inode)->i_disksize == disksize) {
219461628a3fSMingming Cao 				ret = ext4_mark_inode_dirty(handle, inode);
219564769240SAlex Tomas 				return ret;
219664769240SAlex Tomas 			}
219761628a3fSMingming Cao 		}
219861628a3fSMingming Cao 		ret = 0;
219961628a3fSMingming Cao 	}
220061628a3fSMingming Cao 	return ret;
220161628a3fSMingming Cao }
220261628a3fSMingming Cao 
220361628a3fSMingming Cao static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh)
220461628a3fSMingming Cao {
2205f0e6c985SAneesh Kumar K.V 	/*
2206f0e6c985SAneesh Kumar K.V 	 * unmapped buffer is possible for holes.
2207f0e6c985SAneesh Kumar K.V 	 * delay buffer is possible with delayed allocation
2208f0e6c985SAneesh Kumar K.V 	 */
2209f0e6c985SAneesh Kumar K.V 	return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh));
2210f0e6c985SAneesh Kumar K.V }
2211f0e6c985SAneesh Kumar K.V 
2212f0e6c985SAneesh Kumar K.V static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock,
2213f0e6c985SAneesh Kumar K.V 				   struct buffer_head *bh_result, int create)
2214f0e6c985SAneesh Kumar K.V {
2215f0e6c985SAneesh Kumar K.V 	int ret = 0;
2216f0e6c985SAneesh Kumar K.V 	unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2217f0e6c985SAneesh Kumar K.V 
2218f0e6c985SAneesh Kumar K.V 	/*
2219f0e6c985SAneesh Kumar K.V 	 * we don't want to do block allocation in writepage
2220f0e6c985SAneesh Kumar K.V 	 * so call get_block_wrap with create = 0
2221f0e6c985SAneesh Kumar K.V 	 */
2222f0e6c985SAneesh Kumar K.V 	ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks,
2223f0e6c985SAneesh Kumar K.V 				   bh_result, 0, 0, 0);
2224f0e6c985SAneesh Kumar K.V 	if (ret > 0) {
2225f0e6c985SAneesh Kumar K.V 		bh_result->b_size = (ret << inode->i_blkbits);
2226f0e6c985SAneesh Kumar K.V 		ret = 0;
2227f0e6c985SAneesh Kumar K.V 	}
2228f0e6c985SAneesh Kumar K.V 	return ret;
222961628a3fSMingming Cao }
223061628a3fSMingming Cao 
223161628a3fSMingming Cao /*
2232f0e6c985SAneesh Kumar K.V  * get called vi ext4_da_writepages after taking page lock (have journal handle)
2233f0e6c985SAneesh Kumar K.V  * get called via journal_submit_inode_data_buffers (no journal handle)
2234f0e6c985SAneesh Kumar K.V  * get called via shrink_page_list via pdflush (no journal handle)
2235f0e6c985SAneesh Kumar K.V  * or grab_page_cache when doing write_begin (have journal handle)
223661628a3fSMingming Cao  */
223764769240SAlex Tomas static int ext4_da_writepage(struct page *page,
223864769240SAlex Tomas 				struct writeback_control *wbc)
223964769240SAlex Tomas {
224064769240SAlex Tomas 	int ret = 0;
224161628a3fSMingming Cao 	loff_t size;
224261628a3fSMingming Cao 	unsigned long len;
224361628a3fSMingming Cao 	struct buffer_head *page_bufs;
224461628a3fSMingming Cao 	struct inode *inode = page->mapping->host;
224564769240SAlex Tomas 
224661628a3fSMingming Cao 	size = i_size_read(inode);
224761628a3fSMingming Cao 	if (page->index == size >> PAGE_CACHE_SHIFT)
224861628a3fSMingming Cao 		len = size & ~PAGE_CACHE_MASK;
224961628a3fSMingming Cao 	else
225061628a3fSMingming Cao 		len = PAGE_CACHE_SIZE;
225161628a3fSMingming Cao 
2252f0e6c985SAneesh Kumar K.V 	if (page_has_buffers(page)) {
2253f0e6c985SAneesh Kumar K.V 		page_bufs = page_buffers(page);
2254f0e6c985SAneesh Kumar K.V 		if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2255f0e6c985SAneesh Kumar K.V 					ext4_bh_unmapped_or_delay)) {
225661628a3fSMingming Cao 			/*
2257f0e6c985SAneesh Kumar K.V 			 * We don't want to do  block allocation
2258f0e6c985SAneesh Kumar K.V 			 * So redirty the page and return
2259cd1aac32SAneesh Kumar K.V 			 * We may reach here when we do a journal commit
2260cd1aac32SAneesh Kumar K.V 			 * via journal_submit_inode_data_buffers.
2261cd1aac32SAneesh Kumar K.V 			 * If we don't have mapping block we just ignore
2262f0e6c985SAneesh Kumar K.V 			 * them. We can also reach here via shrink_page_list
2263f0e6c985SAneesh Kumar K.V 			 */
2264f0e6c985SAneesh Kumar K.V 			redirty_page_for_writepage(wbc, page);
2265f0e6c985SAneesh Kumar K.V 			unlock_page(page);
2266f0e6c985SAneesh Kumar K.V 			return 0;
2267f0e6c985SAneesh Kumar K.V 		}
2268f0e6c985SAneesh Kumar K.V 	} else {
2269f0e6c985SAneesh Kumar K.V 		/*
2270f0e6c985SAneesh Kumar K.V 		 * The test for page_has_buffers() is subtle:
2271f0e6c985SAneesh Kumar K.V 		 * We know the page is dirty but it lost buffers. That means
2272f0e6c985SAneesh Kumar K.V 		 * that at some moment in time after write_begin()/write_end()
2273f0e6c985SAneesh Kumar K.V 		 * has been called all buffers have been clean and thus they
2274f0e6c985SAneesh Kumar K.V 		 * must have been written at least once. So they are all
2275f0e6c985SAneesh Kumar K.V 		 * mapped and we can happily proceed with mapping them
2276f0e6c985SAneesh Kumar K.V 		 * and writing the page.
2277f0e6c985SAneesh Kumar K.V 		 *
2278f0e6c985SAneesh Kumar K.V 		 * Try to initialize the buffer_heads and check whether
2279f0e6c985SAneesh Kumar K.V 		 * all are mapped and non delay. We don't want to
2280f0e6c985SAneesh Kumar K.V 		 * do block allocation here.
2281f0e6c985SAneesh Kumar K.V 		 */
2282f0e6c985SAneesh Kumar K.V 		ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2283f0e6c985SAneesh Kumar K.V 						ext4_normal_get_block_write);
2284f0e6c985SAneesh Kumar K.V 		if (!ret) {
2285f0e6c985SAneesh Kumar K.V 			page_bufs = page_buffers(page);
2286f0e6c985SAneesh Kumar K.V 			/* check whether all are mapped and non delay */
2287f0e6c985SAneesh Kumar K.V 			if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2288f0e6c985SAneesh Kumar K.V 						ext4_bh_unmapped_or_delay)) {
2289f0e6c985SAneesh Kumar K.V 				redirty_page_for_writepage(wbc, page);
2290f0e6c985SAneesh Kumar K.V 				unlock_page(page);
2291f0e6c985SAneesh Kumar K.V 				return 0;
2292f0e6c985SAneesh Kumar K.V 			}
2293f0e6c985SAneesh Kumar K.V 		} else {
2294f0e6c985SAneesh Kumar K.V 			/*
2295f0e6c985SAneesh Kumar K.V 			 * We can't do block allocation here
2296f0e6c985SAneesh Kumar K.V 			 * so just redity the page and unlock
2297f0e6c985SAneesh Kumar K.V 			 * and return
229861628a3fSMingming Cao 			 */
229961628a3fSMingming Cao 			redirty_page_for_writepage(wbc, page);
230061628a3fSMingming Cao 			unlock_page(page);
230161628a3fSMingming Cao 			return 0;
230261628a3fSMingming Cao 		}
230364769240SAlex Tomas 	}
230464769240SAlex Tomas 
230564769240SAlex Tomas 	if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
2306f0e6c985SAneesh Kumar K.V 		ret = nobh_writepage(page, ext4_normal_get_block_write, wbc);
230764769240SAlex Tomas 	else
2308f0e6c985SAneesh Kumar K.V 		ret = block_write_full_page(page,
2309f0e6c985SAneesh Kumar K.V 						ext4_normal_get_block_write,
2310f0e6c985SAneesh Kumar K.V 						wbc);
231164769240SAlex Tomas 
231264769240SAlex Tomas 	return ret;
231364769240SAlex Tomas }
231464769240SAlex Tomas 
231561628a3fSMingming Cao /*
2316525f4ed8SMingming Cao  * This is called via ext4_da_writepages() to
2317525f4ed8SMingming Cao  * calulate the total number of credits to reserve to fit
2318525f4ed8SMingming Cao  * a single extent allocation into a single transaction,
2319525f4ed8SMingming Cao  * ext4_da_writpeages() will loop calling this before
2320525f4ed8SMingming Cao  * the block allocation.
232161628a3fSMingming Cao  */
2322525f4ed8SMingming Cao 
2323525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode)
2324525f4ed8SMingming Cao {
2325525f4ed8SMingming Cao 	int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2326525f4ed8SMingming Cao 
2327525f4ed8SMingming Cao 	/*
2328525f4ed8SMingming Cao 	 * With non-extent format the journal credit needed to
2329525f4ed8SMingming Cao 	 * insert nrblocks contiguous block is dependent on
2330525f4ed8SMingming Cao 	 * number of contiguous block. So we will limit
2331525f4ed8SMingming Cao 	 * number of contiguous block to a sane value
2332525f4ed8SMingming Cao 	 */
2333525f4ed8SMingming Cao 	if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2334525f4ed8SMingming Cao 	    (max_blocks > EXT4_MAX_TRANS_DATA))
2335525f4ed8SMingming Cao 		max_blocks = EXT4_MAX_TRANS_DATA;
2336525f4ed8SMingming Cao 
2337525f4ed8SMingming Cao 	return ext4_chunk_trans_blocks(inode, max_blocks);
2338525f4ed8SMingming Cao }
233961628a3fSMingming Cao 
234064769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping,
234164769240SAlex Tomas 			      struct writeback_control *wbc)
234264769240SAlex Tomas {
234361628a3fSMingming Cao 	handle_t *handle = NULL;
234461628a3fSMingming Cao 	loff_t range_start = 0;
23455e745b04SAneesh Kumar K.V 	struct inode *inode = mapping->host;
23465e745b04SAneesh Kumar K.V 	int needed_blocks, ret = 0, nr_to_writebump = 0;
23475e745b04SAneesh Kumar K.V 	long to_write, pages_skipped = 0;
23485e745b04SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
234961628a3fSMingming Cao 
235061628a3fSMingming Cao 	/*
235161628a3fSMingming Cao 	 * No pages to write? This is mainly a kludge to avoid starting
235261628a3fSMingming Cao 	 * a transaction for special inodes like journal inode on last iput()
235361628a3fSMingming Cao 	 * because that could violate lock ordering on umount
235461628a3fSMingming Cao 	 */
2355a1d6cc56SAneesh Kumar K.V 	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
235661628a3fSMingming Cao 		return 0;
23575e745b04SAneesh Kumar K.V 	/*
23585e745b04SAneesh Kumar K.V 	 * Make sure nr_to_write is >= sbi->s_mb_stream_request
23595e745b04SAneesh Kumar K.V 	 * This make sure small files blocks are allocated in
23605e745b04SAneesh Kumar K.V 	 * single attempt. This ensure that small files
23615e745b04SAneesh Kumar K.V 	 * get less fragmented.
23625e745b04SAneesh Kumar K.V 	 */
23635e745b04SAneesh Kumar K.V 	if (wbc->nr_to_write < sbi->s_mb_stream_request) {
23645e745b04SAneesh Kumar K.V 		nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
23655e745b04SAneesh Kumar K.V 		wbc->nr_to_write = sbi->s_mb_stream_request;
23665e745b04SAneesh Kumar K.V 	}
236761628a3fSMingming Cao 
2368a1d6cc56SAneesh Kumar K.V 	if (!wbc->range_cyclic)
236961628a3fSMingming Cao 		/*
237061628a3fSMingming Cao 		 * If range_cyclic is not set force range_cont
237161628a3fSMingming Cao 		 * and save the old writeback_index
237261628a3fSMingming Cao 		 */
237361628a3fSMingming Cao 		wbc->range_cont = 1;
237461628a3fSMingming Cao 
2375a1d6cc56SAneesh Kumar K.V 	range_start =  wbc->range_start;
2376a1d6cc56SAneesh Kumar K.V 	pages_skipped = wbc->pages_skipped;
2377a1d6cc56SAneesh Kumar K.V 
2378a1d6cc56SAneesh Kumar K.V restart_loop:
2379a1d6cc56SAneesh Kumar K.V 	to_write = wbc->nr_to_write;
2380a1d6cc56SAneesh Kumar K.V 	while (!ret && to_write > 0) {
2381a1d6cc56SAneesh Kumar K.V 
2382a1d6cc56SAneesh Kumar K.V 		/*
2383a1d6cc56SAneesh Kumar K.V 		 * we  insert one extent at a time. So we need
2384a1d6cc56SAneesh Kumar K.V 		 * credit needed for single extent allocation.
2385a1d6cc56SAneesh Kumar K.V 		 * journalled mode is currently not supported
2386a1d6cc56SAneesh Kumar K.V 		 * by delalloc
2387a1d6cc56SAneesh Kumar K.V 		 */
2388a1d6cc56SAneesh Kumar K.V 		BUG_ON(ext4_should_journal_data(inode));
2389525f4ed8SMingming Cao 		needed_blocks = ext4_da_writepages_trans_blocks(inode);
2390a1d6cc56SAneesh Kumar K.V 
239161628a3fSMingming Cao 		/* start a new transaction*/
239261628a3fSMingming Cao 		handle = ext4_journal_start(inode, needed_blocks);
239361628a3fSMingming Cao 		if (IS_ERR(handle)) {
239461628a3fSMingming Cao 			ret = PTR_ERR(handle);
2395a1d6cc56SAneesh Kumar K.V 			printk(KERN_EMERG "%s: jbd2_start: "
2396a1d6cc56SAneesh Kumar K.V 			       "%ld pages, ino %lu; err %d\n", __func__,
2397a1d6cc56SAneesh Kumar K.V 				wbc->nr_to_write, inode->i_ino, ret);
2398a1d6cc56SAneesh Kumar K.V 			dump_stack();
239961628a3fSMingming Cao 			goto out_writepages;
240061628a3fSMingming Cao 		}
2401cd1aac32SAneesh Kumar K.V 		if (ext4_should_order_data(inode)) {
2402cd1aac32SAneesh Kumar K.V 			/*
2403cd1aac32SAneesh Kumar K.V 			 * With ordered mode we need to add
2404a1d6cc56SAneesh Kumar K.V 			 * the inode to the journal handl
2405cd1aac32SAneesh Kumar K.V 			 * when we do block allocation.
2406cd1aac32SAneesh Kumar K.V 			 */
2407cd1aac32SAneesh Kumar K.V 			ret = ext4_jbd2_file_inode(handle, inode);
2408cd1aac32SAneesh Kumar K.V 			if (ret) {
2409cd1aac32SAneesh Kumar K.V 				ext4_journal_stop(handle);
2410cd1aac32SAneesh Kumar K.V 				goto out_writepages;
2411cd1aac32SAneesh Kumar K.V 			}
2412cd1aac32SAneesh Kumar K.V 		}
241361628a3fSMingming Cao 
241461628a3fSMingming Cao 		to_write -= wbc->nr_to_write;
241561628a3fSMingming Cao 		ret = mpage_da_writepages(mapping, wbc,
241661628a3fSMingming Cao 					  ext4_da_get_block_write);
241761628a3fSMingming Cao 		ext4_journal_stop(handle);
2418a1d6cc56SAneesh Kumar K.V 		if (ret == MPAGE_DA_EXTENT_TAIL) {
2419a1d6cc56SAneesh Kumar K.V 			/*
2420a1d6cc56SAneesh Kumar K.V 			 * got one extent now try with
2421a1d6cc56SAneesh Kumar K.V 			 * rest of the pages
2422a1d6cc56SAneesh Kumar K.V 			 */
2423a1d6cc56SAneesh Kumar K.V 			to_write += wbc->nr_to_write;
2424a1d6cc56SAneesh Kumar K.V 			ret = 0;
2425a1d6cc56SAneesh Kumar K.V 		} else if (wbc->nr_to_write) {
242661628a3fSMingming Cao 			/*
242761628a3fSMingming Cao 			 * There is no more writeout needed
242861628a3fSMingming Cao 			 * or we requested for a noblocking writeout
242961628a3fSMingming Cao 			 * and we found the device congested
243061628a3fSMingming Cao 			 */
243161628a3fSMingming Cao 			to_write += wbc->nr_to_write;
243261628a3fSMingming Cao 			break;
243361628a3fSMingming Cao 		}
243461628a3fSMingming Cao 		wbc->nr_to_write = to_write;
243561628a3fSMingming Cao 	}
243661628a3fSMingming Cao 
2437a1d6cc56SAneesh Kumar K.V 	if (wbc->range_cont && (pages_skipped != wbc->pages_skipped)) {
2438a1d6cc56SAneesh Kumar K.V 		/* We skipped pages in this loop */
2439a1d6cc56SAneesh Kumar K.V 		wbc->range_start = range_start;
2440a1d6cc56SAneesh Kumar K.V 		wbc->nr_to_write = to_write +
2441a1d6cc56SAneesh Kumar K.V 				wbc->pages_skipped - pages_skipped;
2442a1d6cc56SAneesh Kumar K.V 		wbc->pages_skipped = pages_skipped;
2443a1d6cc56SAneesh Kumar K.V 		goto restart_loop;
2444a1d6cc56SAneesh Kumar K.V 	}
2445a1d6cc56SAneesh Kumar K.V 
244661628a3fSMingming Cao out_writepages:
24475e745b04SAneesh Kumar K.V 	wbc->nr_to_write = to_write - nr_to_writebump;
244861628a3fSMingming Cao 	wbc->range_start = range_start;
244961628a3fSMingming Cao 	return ret;
245064769240SAlex Tomas }
245164769240SAlex Tomas 
245264769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
245364769240SAlex Tomas 				loff_t pos, unsigned len, unsigned flags,
245464769240SAlex Tomas 				struct page **pagep, void **fsdata)
245564769240SAlex Tomas {
2456d2a17637SMingming Cao 	int ret, retries = 0;
245764769240SAlex Tomas 	struct page *page;
245864769240SAlex Tomas 	pgoff_t index;
245964769240SAlex Tomas 	unsigned from, to;
246064769240SAlex Tomas 	struct inode *inode = mapping->host;
246164769240SAlex Tomas 	handle_t *handle;
246264769240SAlex Tomas 
246364769240SAlex Tomas 	index = pos >> PAGE_CACHE_SHIFT;
246464769240SAlex Tomas 	from = pos & (PAGE_CACHE_SIZE - 1);
246564769240SAlex Tomas 	to = from + len;
246664769240SAlex Tomas 
2467d2a17637SMingming Cao retry:
246864769240SAlex Tomas 	/*
246964769240SAlex Tomas 	 * With delayed allocation, we don't log the i_disksize update
247064769240SAlex Tomas 	 * if there is delayed block allocation. But we still need
247164769240SAlex Tomas 	 * to journalling the i_disksize update if writes to the end
247264769240SAlex Tomas 	 * of file which has an already mapped buffer.
247364769240SAlex Tomas 	 */
247464769240SAlex Tomas 	handle = ext4_journal_start(inode, 1);
247564769240SAlex Tomas 	if (IS_ERR(handle)) {
247664769240SAlex Tomas 		ret = PTR_ERR(handle);
247764769240SAlex Tomas 		goto out;
247864769240SAlex Tomas 	}
247964769240SAlex Tomas 
248064769240SAlex Tomas 	page = __grab_cache_page(mapping, index);
2481d5a0d4f7SEric Sandeen 	if (!page) {
2482d5a0d4f7SEric Sandeen 		ext4_journal_stop(handle);
2483d5a0d4f7SEric Sandeen 		ret = -ENOMEM;
2484d5a0d4f7SEric Sandeen 		goto out;
2485d5a0d4f7SEric Sandeen 	}
248664769240SAlex Tomas 	*pagep = page;
248764769240SAlex Tomas 
248864769240SAlex Tomas 	ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
248964769240SAlex Tomas 							ext4_da_get_block_prep);
249064769240SAlex Tomas 	if (ret < 0) {
249164769240SAlex Tomas 		unlock_page(page);
249264769240SAlex Tomas 		ext4_journal_stop(handle);
249364769240SAlex Tomas 		page_cache_release(page);
249464769240SAlex Tomas 	}
249564769240SAlex Tomas 
2496d2a17637SMingming Cao 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2497d2a17637SMingming Cao 		goto retry;
249864769240SAlex Tomas out:
249964769240SAlex Tomas 	return ret;
250064769240SAlex Tomas }
250164769240SAlex Tomas 
2502632eaeabSMingming Cao /*
2503632eaeabSMingming Cao  * Check if we should update i_disksize
2504632eaeabSMingming Cao  * when write to the end of file but not require block allocation
2505632eaeabSMingming Cao  */
2506632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page,
2507632eaeabSMingming Cao 					 unsigned long offset)
2508632eaeabSMingming Cao {
2509632eaeabSMingming Cao 	struct buffer_head *bh;
2510632eaeabSMingming Cao 	struct inode *inode = page->mapping->host;
2511632eaeabSMingming Cao 	unsigned int idx;
2512632eaeabSMingming Cao 	int i;
2513632eaeabSMingming Cao 
2514632eaeabSMingming Cao 	bh = page_buffers(page);
2515632eaeabSMingming Cao 	idx = offset >> inode->i_blkbits;
2516632eaeabSMingming Cao 
2517632eaeabSMingming Cao 	for (i = 0; i < idx; i++)
2518632eaeabSMingming Cao 		bh = bh->b_this_page;
2519632eaeabSMingming Cao 
2520632eaeabSMingming Cao 	if (!buffer_mapped(bh) || (buffer_delay(bh)))
2521632eaeabSMingming Cao 		return 0;
2522632eaeabSMingming Cao 	return 1;
2523632eaeabSMingming Cao }
2524632eaeabSMingming Cao 
252564769240SAlex Tomas static int ext4_da_write_end(struct file *file,
252664769240SAlex Tomas 				struct address_space *mapping,
252764769240SAlex Tomas 				loff_t pos, unsigned len, unsigned copied,
252864769240SAlex Tomas 				struct page *page, void *fsdata)
252964769240SAlex Tomas {
253064769240SAlex Tomas 	struct inode *inode = mapping->host;
253164769240SAlex Tomas 	int ret = 0, ret2;
253264769240SAlex Tomas 	handle_t *handle = ext4_journal_current_handle();
253364769240SAlex Tomas 	loff_t new_i_size;
2534632eaeabSMingming Cao 	unsigned long start, end;
2535632eaeabSMingming Cao 
2536632eaeabSMingming Cao 	start = pos & (PAGE_CACHE_SIZE - 1);
2537632eaeabSMingming Cao 	end = start + copied - 1;
253864769240SAlex Tomas 
253964769240SAlex Tomas 	/*
254064769240SAlex Tomas 	 * generic_write_end() will run mark_inode_dirty() if i_size
254164769240SAlex Tomas 	 * changes.  So let's piggyback the i_disksize mark_inode_dirty
254264769240SAlex Tomas 	 * into that.
254364769240SAlex Tomas 	 */
254464769240SAlex Tomas 
254564769240SAlex Tomas 	new_i_size = pos + copied;
2546632eaeabSMingming Cao 	if (new_i_size > EXT4_I(inode)->i_disksize) {
2547632eaeabSMingming Cao 		if (ext4_da_should_update_i_disksize(page, end)) {
2548632eaeabSMingming Cao 			down_write(&EXT4_I(inode)->i_data_sem);
2549632eaeabSMingming Cao 			if (new_i_size > EXT4_I(inode)->i_disksize) {
255064769240SAlex Tomas 				/*
2551632eaeabSMingming Cao 				 * Updating i_disksize when extending file
2552632eaeabSMingming Cao 				 * without needing block allocation
255364769240SAlex Tomas 				 */
255464769240SAlex Tomas 				if (ext4_should_order_data(inode))
2555632eaeabSMingming Cao 					ret = ext4_jbd2_file_inode(handle,
2556632eaeabSMingming Cao 								   inode);
255764769240SAlex Tomas 
255864769240SAlex Tomas 				EXT4_I(inode)->i_disksize = new_i_size;
255964769240SAlex Tomas 			}
2560632eaeabSMingming Cao 			up_write(&EXT4_I(inode)->i_data_sem);
2561632eaeabSMingming Cao 		}
2562632eaeabSMingming Cao 	}
256364769240SAlex Tomas 	ret2 = generic_write_end(file, mapping, pos, len, copied,
256464769240SAlex Tomas 							page, fsdata);
256564769240SAlex Tomas 	copied = ret2;
256664769240SAlex Tomas 	if (ret2 < 0)
256764769240SAlex Tomas 		ret = ret2;
256864769240SAlex Tomas 	ret2 = ext4_journal_stop(handle);
256964769240SAlex Tomas 	if (!ret)
257064769240SAlex Tomas 		ret = ret2;
257164769240SAlex Tomas 
257264769240SAlex Tomas 	return ret ? ret : copied;
257364769240SAlex Tomas }
257464769240SAlex Tomas 
257564769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
257664769240SAlex Tomas {
257764769240SAlex Tomas 	/*
257864769240SAlex Tomas 	 * Drop reserved blocks
257964769240SAlex Tomas 	 */
258064769240SAlex Tomas 	BUG_ON(!PageLocked(page));
258164769240SAlex Tomas 	if (!page_has_buffers(page))
258264769240SAlex Tomas 		goto out;
258364769240SAlex Tomas 
2584d2a17637SMingming Cao 	ext4_da_page_release_reservation(page, offset);
258564769240SAlex Tomas 
258664769240SAlex Tomas out:
258764769240SAlex Tomas 	ext4_invalidatepage(page, offset);
258864769240SAlex Tomas 
258964769240SAlex Tomas 	return;
259064769240SAlex Tomas }
259164769240SAlex Tomas 
259264769240SAlex Tomas 
259364769240SAlex Tomas /*
2594ac27a0ecSDave Kleikamp  * bmap() is special.  It gets used by applications such as lilo and by
2595ac27a0ecSDave Kleikamp  * the swapper to find the on-disk block of a specific piece of data.
2596ac27a0ecSDave Kleikamp  *
2597ac27a0ecSDave Kleikamp  * Naturally, this is dangerous if the block concerned is still in the
2598617ba13bSMingming Cao  * journal.  If somebody makes a swapfile on an ext4 data-journaling
2599ac27a0ecSDave Kleikamp  * filesystem and enables swap, then they may get a nasty shock when the
2600ac27a0ecSDave Kleikamp  * data getting swapped to that swapfile suddenly gets overwritten by
2601ac27a0ecSDave Kleikamp  * the original zero's written out previously to the journal and
2602ac27a0ecSDave Kleikamp  * awaiting writeback in the kernel's buffer cache.
2603ac27a0ecSDave Kleikamp  *
2604ac27a0ecSDave Kleikamp  * So, if we see any bmap calls here on a modified, data-journaled file,
2605ac27a0ecSDave Kleikamp  * take extra steps to flush any blocks which might be in the cache.
2606ac27a0ecSDave Kleikamp  */
2607617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
2608ac27a0ecSDave Kleikamp {
2609ac27a0ecSDave Kleikamp 	struct inode *inode = mapping->host;
2610ac27a0ecSDave Kleikamp 	journal_t *journal;
2611ac27a0ecSDave Kleikamp 	int err;
2612ac27a0ecSDave Kleikamp 
261364769240SAlex Tomas 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
261464769240SAlex Tomas 			test_opt(inode->i_sb, DELALLOC)) {
261564769240SAlex Tomas 		/*
261664769240SAlex Tomas 		 * With delalloc we want to sync the file
261764769240SAlex Tomas 		 * so that we can make sure we allocate
261864769240SAlex Tomas 		 * blocks for file
261964769240SAlex Tomas 		 */
262064769240SAlex Tomas 		filemap_write_and_wait(mapping);
262164769240SAlex Tomas 	}
262264769240SAlex Tomas 
2623617ba13bSMingming Cao 	if (EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
2624ac27a0ecSDave Kleikamp 		/*
2625ac27a0ecSDave Kleikamp 		 * This is a REALLY heavyweight approach, but the use of
2626ac27a0ecSDave Kleikamp 		 * bmap on dirty files is expected to be extremely rare:
2627ac27a0ecSDave Kleikamp 		 * only if we run lilo or swapon on a freshly made file
2628ac27a0ecSDave Kleikamp 		 * do we expect this to happen.
2629ac27a0ecSDave Kleikamp 		 *
2630ac27a0ecSDave Kleikamp 		 * (bmap requires CAP_SYS_RAWIO so this does not
2631ac27a0ecSDave Kleikamp 		 * represent an unprivileged user DOS attack --- we'd be
2632ac27a0ecSDave Kleikamp 		 * in trouble if mortal users could trigger this path at
2633ac27a0ecSDave Kleikamp 		 * will.)
2634ac27a0ecSDave Kleikamp 		 *
2635617ba13bSMingming Cao 		 * NB. EXT4_STATE_JDATA is not set on files other than
2636ac27a0ecSDave Kleikamp 		 * regular files.  If somebody wants to bmap a directory
2637ac27a0ecSDave Kleikamp 		 * or symlink and gets confused because the buffer
2638ac27a0ecSDave Kleikamp 		 * hasn't yet been flushed to disk, they deserve
2639ac27a0ecSDave Kleikamp 		 * everything they get.
2640ac27a0ecSDave Kleikamp 		 */
2641ac27a0ecSDave Kleikamp 
2642617ba13bSMingming Cao 		EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
2643617ba13bSMingming Cao 		journal = EXT4_JOURNAL(inode);
2644dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
2645dab291afSMingming Cao 		err = jbd2_journal_flush(journal);
2646dab291afSMingming Cao 		jbd2_journal_unlock_updates(journal);
2647ac27a0ecSDave Kleikamp 
2648ac27a0ecSDave Kleikamp 		if (err)
2649ac27a0ecSDave Kleikamp 			return 0;
2650ac27a0ecSDave Kleikamp 	}
2651ac27a0ecSDave Kleikamp 
2652617ba13bSMingming Cao 	return generic_block_bmap(mapping, block, ext4_get_block);
2653ac27a0ecSDave Kleikamp }
2654ac27a0ecSDave Kleikamp 
2655ac27a0ecSDave Kleikamp static int bget_one(handle_t *handle, struct buffer_head *bh)
2656ac27a0ecSDave Kleikamp {
2657ac27a0ecSDave Kleikamp 	get_bh(bh);
2658ac27a0ecSDave Kleikamp 	return 0;
2659ac27a0ecSDave Kleikamp }
2660ac27a0ecSDave Kleikamp 
2661ac27a0ecSDave Kleikamp static int bput_one(handle_t *handle, struct buffer_head *bh)
2662ac27a0ecSDave Kleikamp {
2663ac27a0ecSDave Kleikamp 	put_bh(bh);
2664ac27a0ecSDave Kleikamp 	return 0;
2665ac27a0ecSDave Kleikamp }
2666ac27a0ecSDave Kleikamp 
2667ac27a0ecSDave Kleikamp /*
2668678aaf48SJan Kara  * Note that we don't need to start a transaction unless we're journaling data
2669678aaf48SJan Kara  * because we should have holes filled from ext4_page_mkwrite(). We even don't
2670678aaf48SJan Kara  * need to file the inode to the transaction's list in ordered mode because if
2671678aaf48SJan Kara  * we are writing back data added by write(), the inode is already there and if
2672678aaf48SJan Kara  * we are writing back data modified via mmap(), noone guarantees in which
2673678aaf48SJan Kara  * transaction the data will hit the disk. In case we are journaling data, we
2674678aaf48SJan Kara  * cannot start transaction directly because transaction start ranks above page
2675678aaf48SJan Kara  * lock so we have to do some magic.
2676ac27a0ecSDave Kleikamp  *
2677678aaf48SJan Kara  * In all journaling modes block_write_full_page() will start the I/O.
2678ac27a0ecSDave Kleikamp  *
2679ac27a0ecSDave Kleikamp  * Problem:
2680ac27a0ecSDave Kleikamp  *
2681617ba13bSMingming Cao  *	ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2682617ba13bSMingming Cao  *		ext4_writepage()
2683ac27a0ecSDave Kleikamp  *
2684ac27a0ecSDave Kleikamp  * Similar for:
2685ac27a0ecSDave Kleikamp  *
2686617ba13bSMingming Cao  *	ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ...
2687ac27a0ecSDave Kleikamp  *
2688617ba13bSMingming Cao  * Same applies to ext4_get_block().  We will deadlock on various things like
26890e855ac8SAneesh Kumar K.V  * lock_journal and i_data_sem
2690ac27a0ecSDave Kleikamp  *
2691ac27a0ecSDave Kleikamp  * Setting PF_MEMALLOC here doesn't work - too many internal memory
2692ac27a0ecSDave Kleikamp  * allocations fail.
2693ac27a0ecSDave Kleikamp  *
2694ac27a0ecSDave Kleikamp  * 16May01: If we're reentered then journal_current_handle() will be
2695ac27a0ecSDave Kleikamp  *	    non-zero. We simply *return*.
2696ac27a0ecSDave Kleikamp  *
2697ac27a0ecSDave Kleikamp  * 1 July 2001: @@@ FIXME:
2698ac27a0ecSDave Kleikamp  *   In journalled data mode, a data buffer may be metadata against the
2699ac27a0ecSDave Kleikamp  *   current transaction.  But the same file is part of a shared mapping
2700ac27a0ecSDave Kleikamp  *   and someone does a writepage() on it.
2701ac27a0ecSDave Kleikamp  *
2702ac27a0ecSDave Kleikamp  *   We will move the buffer onto the async_data list, but *after* it has
2703ac27a0ecSDave Kleikamp  *   been dirtied. So there's a small window where we have dirty data on
2704ac27a0ecSDave Kleikamp  *   BJ_Metadata.
2705ac27a0ecSDave Kleikamp  *
2706ac27a0ecSDave Kleikamp  *   Note that this only applies to the last partial page in the file.  The
2707ac27a0ecSDave Kleikamp  *   bit which block_write_full_page() uses prepare/commit for.  (That's
2708ac27a0ecSDave Kleikamp  *   broken code anyway: it's wrong for msync()).
2709ac27a0ecSDave Kleikamp  *
2710ac27a0ecSDave Kleikamp  *   It's a rare case: affects the final partial page, for journalled data
2711ac27a0ecSDave Kleikamp  *   where the file is subject to bith write() and writepage() in the same
2712ac27a0ecSDave Kleikamp  *   transction.  To fix it we'll need a custom block_write_full_page().
2713ac27a0ecSDave Kleikamp  *   We'll probably need that anyway for journalling writepage() output.
2714ac27a0ecSDave Kleikamp  *
2715ac27a0ecSDave Kleikamp  * We don't honour synchronous mounts for writepage().  That would be
2716ac27a0ecSDave Kleikamp  * disastrous.  Any write() or metadata operation will sync the fs for
2717ac27a0ecSDave Kleikamp  * us.
2718ac27a0ecSDave Kleikamp  *
2719ac27a0ecSDave Kleikamp  */
2720678aaf48SJan Kara static int __ext4_normal_writepage(struct page *page,
2721cf108bcaSJan Kara 				struct writeback_control *wbc)
2722cf108bcaSJan Kara {
2723cf108bcaSJan Kara 	struct inode *inode = page->mapping->host;
2724cf108bcaSJan Kara 
2725cf108bcaSJan Kara 	if (test_opt(inode->i_sb, NOBH))
2726f0e6c985SAneesh Kumar K.V 		return nobh_writepage(page,
2727f0e6c985SAneesh Kumar K.V 					ext4_normal_get_block_write, wbc);
2728cf108bcaSJan Kara 	else
2729f0e6c985SAneesh Kumar K.V 		return block_write_full_page(page,
2730f0e6c985SAneesh Kumar K.V 						ext4_normal_get_block_write,
2731f0e6c985SAneesh Kumar K.V 						wbc);
2732cf108bcaSJan Kara }
2733cf108bcaSJan Kara 
2734678aaf48SJan Kara static int ext4_normal_writepage(struct page *page,
2735ac27a0ecSDave Kleikamp 				struct writeback_control *wbc)
2736ac27a0ecSDave Kleikamp {
2737ac27a0ecSDave Kleikamp 	struct inode *inode = page->mapping->host;
2738cf108bcaSJan Kara 	loff_t size = i_size_read(inode);
2739cf108bcaSJan Kara 	loff_t len;
2740cf108bcaSJan Kara 
2741cf108bcaSJan Kara 	J_ASSERT(PageLocked(page));
2742cf108bcaSJan Kara 	if (page->index == size >> PAGE_CACHE_SHIFT)
2743cf108bcaSJan Kara 		len = size & ~PAGE_CACHE_MASK;
2744cf108bcaSJan Kara 	else
2745cf108bcaSJan Kara 		len = PAGE_CACHE_SIZE;
2746f0e6c985SAneesh Kumar K.V 
2747f0e6c985SAneesh Kumar K.V 	if (page_has_buffers(page)) {
2748f0e6c985SAneesh Kumar K.V 		/* if page has buffers it should all be mapped
2749f0e6c985SAneesh Kumar K.V 		 * and allocated. If there are not buffers attached
2750f0e6c985SAneesh Kumar K.V 		 * to the page we know the page is dirty but it lost
2751f0e6c985SAneesh Kumar K.V 		 * buffers. That means that at some moment in time
2752f0e6c985SAneesh Kumar K.V 		 * after write_begin() / write_end() has been called
2753f0e6c985SAneesh Kumar K.V 		 * all buffers have been clean and thus they must have been
2754f0e6c985SAneesh Kumar K.V 		 * written at least once. So they are all mapped and we can
2755f0e6c985SAneesh Kumar K.V 		 * happily proceed with mapping them and writing the page.
2756f0e6c985SAneesh Kumar K.V 		 */
2757cf108bcaSJan Kara 		BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
2758cf108bcaSJan Kara 					ext4_bh_unmapped_or_delay));
2759f0e6c985SAneesh Kumar K.V 	}
2760cf108bcaSJan Kara 
2761cf108bcaSJan Kara 	if (!ext4_journal_current_handle())
2762678aaf48SJan Kara 		return __ext4_normal_writepage(page, wbc);
2763cf108bcaSJan Kara 
2764cf108bcaSJan Kara 	redirty_page_for_writepage(wbc, page);
2765cf108bcaSJan Kara 	unlock_page(page);
2766cf108bcaSJan Kara 	return 0;
2767cf108bcaSJan Kara }
2768cf108bcaSJan Kara 
2769cf108bcaSJan Kara static int __ext4_journalled_writepage(struct page *page,
2770cf108bcaSJan Kara 				struct writeback_control *wbc)
2771cf108bcaSJan Kara {
2772cf108bcaSJan Kara 	struct address_space *mapping = page->mapping;
2773cf108bcaSJan Kara 	struct inode *inode = mapping->host;
2774cf108bcaSJan Kara 	struct buffer_head *page_bufs;
2775ac27a0ecSDave Kleikamp 	handle_t *handle = NULL;
2776ac27a0ecSDave Kleikamp 	int ret = 0;
2777ac27a0ecSDave Kleikamp 	int err;
2778ac27a0ecSDave Kleikamp 
2779f0e6c985SAneesh Kumar K.V 	ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
2780f0e6c985SAneesh Kumar K.V 					ext4_normal_get_block_write);
2781cf108bcaSJan Kara 	if (ret != 0)
2782cf108bcaSJan Kara 		goto out_unlock;
2783cf108bcaSJan Kara 
2784cf108bcaSJan Kara 	page_bufs = page_buffers(page);
2785cf108bcaSJan Kara 	walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL,
2786cf108bcaSJan Kara 								bget_one);
2787cf108bcaSJan Kara 	/* As soon as we unlock the page, it can go away, but we have
2788cf108bcaSJan Kara 	 * references to buffers so we are safe */
2789cf108bcaSJan Kara 	unlock_page(page);
2790ac27a0ecSDave Kleikamp 
2791617ba13bSMingming Cao 	handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2792ac27a0ecSDave Kleikamp 	if (IS_ERR(handle)) {
2793ac27a0ecSDave Kleikamp 		ret = PTR_ERR(handle);
2794cf108bcaSJan Kara 		goto out;
2795ac27a0ecSDave Kleikamp 	}
2796ac27a0ecSDave Kleikamp 
2797cf108bcaSJan Kara 	ret = walk_page_buffers(handle, page_bufs, 0,
2798cf108bcaSJan Kara 			PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
2799ac27a0ecSDave Kleikamp 
2800cf108bcaSJan Kara 	err = walk_page_buffers(handle, page_bufs, 0,
2801cf108bcaSJan Kara 				PAGE_CACHE_SIZE, NULL, write_end_fn);
2802cf108bcaSJan Kara 	if (ret == 0)
2803cf108bcaSJan Kara 		ret = err;
2804617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
2805ac27a0ecSDave Kleikamp 	if (!ret)
2806ac27a0ecSDave Kleikamp 		ret = err;
2807ac27a0ecSDave Kleikamp 
2808cf108bcaSJan Kara 	walk_page_buffers(handle, page_bufs, 0,
2809cf108bcaSJan Kara 				PAGE_CACHE_SIZE, NULL, bput_one);
2810cf108bcaSJan Kara 	EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2811cf108bcaSJan Kara 	goto out;
2812cf108bcaSJan Kara 
2813cf108bcaSJan Kara out_unlock:
2814ac27a0ecSDave Kleikamp 	unlock_page(page);
2815cf108bcaSJan Kara out:
2816ac27a0ecSDave Kleikamp 	return ret;
2817ac27a0ecSDave Kleikamp }
2818ac27a0ecSDave Kleikamp 
2819617ba13bSMingming Cao static int ext4_journalled_writepage(struct page *page,
2820ac27a0ecSDave Kleikamp 				struct writeback_control *wbc)
2821ac27a0ecSDave Kleikamp {
2822ac27a0ecSDave Kleikamp 	struct inode *inode = page->mapping->host;
2823cf108bcaSJan Kara 	loff_t size = i_size_read(inode);
2824cf108bcaSJan Kara 	loff_t len;
2825cf108bcaSJan Kara 
2826cf108bcaSJan Kara 	J_ASSERT(PageLocked(page));
2827cf108bcaSJan Kara 	if (page->index == size >> PAGE_CACHE_SHIFT)
2828cf108bcaSJan Kara 		len = size & ~PAGE_CACHE_MASK;
2829cf108bcaSJan Kara 	else
2830cf108bcaSJan Kara 		len = PAGE_CACHE_SIZE;
2831f0e6c985SAneesh Kumar K.V 
2832f0e6c985SAneesh Kumar K.V 	if (page_has_buffers(page)) {
2833f0e6c985SAneesh Kumar K.V 		/* if page has buffers it should all be mapped
2834f0e6c985SAneesh Kumar K.V 		 * and allocated. If there are not buffers attached
2835f0e6c985SAneesh Kumar K.V 		 * to the page we know the page is dirty but it lost
2836f0e6c985SAneesh Kumar K.V 		 * buffers. That means that at some moment in time
2837f0e6c985SAneesh Kumar K.V 		 * after write_begin() / write_end() has been called
2838f0e6c985SAneesh Kumar K.V 		 * all buffers have been clean and thus they must have been
2839f0e6c985SAneesh Kumar K.V 		 * written at least once. So they are all mapped and we can
2840f0e6c985SAneesh Kumar K.V 		 * happily proceed with mapping them and writing the page.
2841f0e6c985SAneesh Kumar K.V 		 */
2842cf108bcaSJan Kara 		BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
2843cf108bcaSJan Kara 					ext4_bh_unmapped_or_delay));
2844f0e6c985SAneesh Kumar K.V 	}
2845ac27a0ecSDave Kleikamp 
2846617ba13bSMingming Cao 	if (ext4_journal_current_handle())
2847ac27a0ecSDave Kleikamp 		goto no_write;
2848ac27a0ecSDave Kleikamp 
2849cf108bcaSJan Kara 	if (PageChecked(page)) {
2850ac27a0ecSDave Kleikamp 		/*
2851ac27a0ecSDave Kleikamp 		 * It's mmapped pagecache.  Add buffers and journal it.  There
2852ac27a0ecSDave Kleikamp 		 * doesn't seem much point in redirtying the page here.
2853ac27a0ecSDave Kleikamp 		 */
2854ac27a0ecSDave Kleikamp 		ClearPageChecked(page);
2855cf108bcaSJan Kara 		return __ext4_journalled_writepage(page, wbc);
2856ac27a0ecSDave Kleikamp 	} else {
2857ac27a0ecSDave Kleikamp 		/*
2858ac27a0ecSDave Kleikamp 		 * It may be a page full of checkpoint-mode buffers.  We don't
2859ac27a0ecSDave Kleikamp 		 * really know unless we go poke around in the buffer_heads.
2860ac27a0ecSDave Kleikamp 		 * But block_write_full_page will do the right thing.
2861ac27a0ecSDave Kleikamp 		 */
2862f0e6c985SAneesh Kumar K.V 		return block_write_full_page(page,
2863f0e6c985SAneesh Kumar K.V 						ext4_normal_get_block_write,
2864f0e6c985SAneesh Kumar K.V 						wbc);
2865ac27a0ecSDave Kleikamp 	}
2866ac27a0ecSDave Kleikamp no_write:
2867ac27a0ecSDave Kleikamp 	redirty_page_for_writepage(wbc, page);
2868ac27a0ecSDave Kleikamp 	unlock_page(page);
2869cf108bcaSJan Kara 	return 0;
2870ac27a0ecSDave Kleikamp }
2871ac27a0ecSDave Kleikamp 
2872617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page)
2873ac27a0ecSDave Kleikamp {
2874617ba13bSMingming Cao 	return mpage_readpage(page, ext4_get_block);
2875ac27a0ecSDave Kleikamp }
2876ac27a0ecSDave Kleikamp 
2877ac27a0ecSDave Kleikamp static int
2878617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping,
2879ac27a0ecSDave Kleikamp 		struct list_head *pages, unsigned nr_pages)
2880ac27a0ecSDave Kleikamp {
2881617ba13bSMingming Cao 	return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
2882ac27a0ecSDave Kleikamp }
2883ac27a0ecSDave Kleikamp 
2884617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset)
2885ac27a0ecSDave Kleikamp {
2886617ba13bSMingming Cao 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
2887ac27a0ecSDave Kleikamp 
2888ac27a0ecSDave Kleikamp 	/*
2889ac27a0ecSDave Kleikamp 	 * If it's a full truncate we just forget about the pending dirtying
2890ac27a0ecSDave Kleikamp 	 */
2891ac27a0ecSDave Kleikamp 	if (offset == 0)
2892ac27a0ecSDave Kleikamp 		ClearPageChecked(page);
2893ac27a0ecSDave Kleikamp 
2894dab291afSMingming Cao 	jbd2_journal_invalidatepage(journal, page, offset);
2895ac27a0ecSDave Kleikamp }
2896ac27a0ecSDave Kleikamp 
2897617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait)
2898ac27a0ecSDave Kleikamp {
2899617ba13bSMingming Cao 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
2900ac27a0ecSDave Kleikamp 
2901ac27a0ecSDave Kleikamp 	WARN_ON(PageChecked(page));
2902ac27a0ecSDave Kleikamp 	if (!page_has_buffers(page))
2903ac27a0ecSDave Kleikamp 		return 0;
2904dab291afSMingming Cao 	return jbd2_journal_try_to_free_buffers(journal, page, wait);
2905ac27a0ecSDave Kleikamp }
2906ac27a0ecSDave Kleikamp 
2907ac27a0ecSDave Kleikamp /*
2908ac27a0ecSDave Kleikamp  * If the O_DIRECT write will extend the file then add this inode to the
2909ac27a0ecSDave Kleikamp  * orphan list.  So recovery will truncate it back to the original size
2910ac27a0ecSDave Kleikamp  * if the machine crashes during the write.
2911ac27a0ecSDave Kleikamp  *
2912ac27a0ecSDave Kleikamp  * If the O_DIRECT write is intantiating holes inside i_size and the machine
29137fb5409dSJan Kara  * crashes then stale disk data _may_ be exposed inside the file. But current
29147fb5409dSJan Kara  * VFS code falls back into buffered path in that case so we are safe.
2915ac27a0ecSDave Kleikamp  */
2916617ba13bSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
2917ac27a0ecSDave Kleikamp 			const struct iovec *iov, loff_t offset,
2918ac27a0ecSDave Kleikamp 			unsigned long nr_segs)
2919ac27a0ecSDave Kleikamp {
2920ac27a0ecSDave Kleikamp 	struct file *file = iocb->ki_filp;
2921ac27a0ecSDave Kleikamp 	struct inode *inode = file->f_mapping->host;
2922617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
29237fb5409dSJan Kara 	handle_t *handle;
2924ac27a0ecSDave Kleikamp 	ssize_t ret;
2925ac27a0ecSDave Kleikamp 	int orphan = 0;
2926ac27a0ecSDave Kleikamp 	size_t count = iov_length(iov, nr_segs);
2927ac27a0ecSDave Kleikamp 
2928ac27a0ecSDave Kleikamp 	if (rw == WRITE) {
2929ac27a0ecSDave Kleikamp 		loff_t final_size = offset + count;
2930ac27a0ecSDave Kleikamp 
29317fb5409dSJan Kara 		if (final_size > inode->i_size) {
29327fb5409dSJan Kara 			/* Credits for sb + inode write */
29337fb5409dSJan Kara 			handle = ext4_journal_start(inode, 2);
2934ac27a0ecSDave Kleikamp 			if (IS_ERR(handle)) {
2935ac27a0ecSDave Kleikamp 				ret = PTR_ERR(handle);
2936ac27a0ecSDave Kleikamp 				goto out;
2937ac27a0ecSDave Kleikamp 			}
2938617ba13bSMingming Cao 			ret = ext4_orphan_add(handle, inode);
29397fb5409dSJan Kara 			if (ret) {
29407fb5409dSJan Kara 				ext4_journal_stop(handle);
29417fb5409dSJan Kara 				goto out;
29427fb5409dSJan Kara 			}
2943ac27a0ecSDave Kleikamp 			orphan = 1;
2944ac27a0ecSDave Kleikamp 			ei->i_disksize = inode->i_size;
29457fb5409dSJan Kara 			ext4_journal_stop(handle);
2946ac27a0ecSDave Kleikamp 		}
2947ac27a0ecSDave Kleikamp 	}
2948ac27a0ecSDave Kleikamp 
2949ac27a0ecSDave Kleikamp 	ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2950ac27a0ecSDave Kleikamp 				 offset, nr_segs,
2951617ba13bSMingming Cao 				 ext4_get_block, NULL);
2952ac27a0ecSDave Kleikamp 
29537fb5409dSJan Kara 	if (orphan) {
2954ac27a0ecSDave Kleikamp 		int err;
2955ac27a0ecSDave Kleikamp 
29567fb5409dSJan Kara 		/* Credits for sb + inode write */
29577fb5409dSJan Kara 		handle = ext4_journal_start(inode, 2);
29587fb5409dSJan Kara 		if (IS_ERR(handle)) {
29597fb5409dSJan Kara 			/* This is really bad luck. We've written the data
29607fb5409dSJan Kara 			 * but cannot extend i_size. Bail out and pretend
29617fb5409dSJan Kara 			 * the write failed... */
29627fb5409dSJan Kara 			ret = PTR_ERR(handle);
29637fb5409dSJan Kara 			goto out;
29647fb5409dSJan Kara 		}
29657fb5409dSJan Kara 		if (inode->i_nlink)
2966617ba13bSMingming Cao 			ext4_orphan_del(handle, inode);
29677fb5409dSJan Kara 		if (ret > 0) {
2968ac27a0ecSDave Kleikamp 			loff_t end = offset + ret;
2969ac27a0ecSDave Kleikamp 			if (end > inode->i_size) {
2970ac27a0ecSDave Kleikamp 				ei->i_disksize = end;
2971ac27a0ecSDave Kleikamp 				i_size_write(inode, end);
2972ac27a0ecSDave Kleikamp 				/*
2973ac27a0ecSDave Kleikamp 				 * We're going to return a positive `ret'
2974ac27a0ecSDave Kleikamp 				 * here due to non-zero-length I/O, so there's
2975ac27a0ecSDave Kleikamp 				 * no way of reporting error returns from
2976617ba13bSMingming Cao 				 * ext4_mark_inode_dirty() to userspace.  So
2977ac27a0ecSDave Kleikamp 				 * ignore it.
2978ac27a0ecSDave Kleikamp 				 */
2979617ba13bSMingming Cao 				ext4_mark_inode_dirty(handle, inode);
2980ac27a0ecSDave Kleikamp 			}
2981ac27a0ecSDave Kleikamp 		}
2982617ba13bSMingming Cao 		err = ext4_journal_stop(handle);
2983ac27a0ecSDave Kleikamp 		if (ret == 0)
2984ac27a0ecSDave Kleikamp 			ret = err;
2985ac27a0ecSDave Kleikamp 	}
2986ac27a0ecSDave Kleikamp out:
2987ac27a0ecSDave Kleikamp 	return ret;
2988ac27a0ecSDave Kleikamp }
2989ac27a0ecSDave Kleikamp 
2990ac27a0ecSDave Kleikamp /*
2991617ba13bSMingming Cao  * Pages can be marked dirty completely asynchronously from ext4's journalling
2992ac27a0ecSDave Kleikamp  * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do
2993ac27a0ecSDave Kleikamp  * much here because ->set_page_dirty is called under VFS locks.  The page is
2994ac27a0ecSDave Kleikamp  * not necessarily locked.
2995ac27a0ecSDave Kleikamp  *
2996ac27a0ecSDave Kleikamp  * We cannot just dirty the page and leave attached buffers clean, because the
2997ac27a0ecSDave Kleikamp  * buffers' dirty state is "definitive".  We cannot just set the buffers dirty
2998ac27a0ecSDave Kleikamp  * or jbddirty because all the journalling code will explode.
2999ac27a0ecSDave Kleikamp  *
3000ac27a0ecSDave Kleikamp  * So what we do is to mark the page "pending dirty" and next time writepage
3001ac27a0ecSDave Kleikamp  * is called, propagate that into the buffers appropriately.
3002ac27a0ecSDave Kleikamp  */
3003617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page)
3004ac27a0ecSDave Kleikamp {
3005ac27a0ecSDave Kleikamp 	SetPageChecked(page);
3006ac27a0ecSDave Kleikamp 	return __set_page_dirty_nobuffers(page);
3007ac27a0ecSDave Kleikamp }
3008ac27a0ecSDave Kleikamp 
3009617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = {
3010617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3011617ba13bSMingming Cao 	.readpages		= ext4_readpages,
3012678aaf48SJan Kara 	.writepage		= ext4_normal_writepage,
3013ac27a0ecSDave Kleikamp 	.sync_page		= block_sync_page,
3014bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
3015bfc1af65SNick Piggin 	.write_end		= ext4_ordered_write_end,
3016617ba13bSMingming Cao 	.bmap			= ext4_bmap,
3017617ba13bSMingming Cao 	.invalidatepage		= ext4_invalidatepage,
3018617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
3019617ba13bSMingming Cao 	.direct_IO		= ext4_direct_IO,
3020ac27a0ecSDave Kleikamp 	.migratepage		= buffer_migrate_page,
30218ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3022ac27a0ecSDave Kleikamp };
3023ac27a0ecSDave Kleikamp 
3024617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = {
3025617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3026617ba13bSMingming Cao 	.readpages		= ext4_readpages,
3027678aaf48SJan Kara 	.writepage		= ext4_normal_writepage,
3028ac27a0ecSDave Kleikamp 	.sync_page		= block_sync_page,
3029bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
3030bfc1af65SNick Piggin 	.write_end		= ext4_writeback_write_end,
3031617ba13bSMingming Cao 	.bmap			= ext4_bmap,
3032617ba13bSMingming Cao 	.invalidatepage		= ext4_invalidatepage,
3033617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
3034617ba13bSMingming Cao 	.direct_IO		= ext4_direct_IO,
3035ac27a0ecSDave Kleikamp 	.migratepage		= buffer_migrate_page,
30368ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3037ac27a0ecSDave Kleikamp };
3038ac27a0ecSDave Kleikamp 
3039617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = {
3040617ba13bSMingming Cao 	.readpage		= ext4_readpage,
3041617ba13bSMingming Cao 	.readpages		= ext4_readpages,
3042617ba13bSMingming Cao 	.writepage		= ext4_journalled_writepage,
3043ac27a0ecSDave Kleikamp 	.sync_page		= block_sync_page,
3044bfc1af65SNick Piggin 	.write_begin		= ext4_write_begin,
3045bfc1af65SNick Piggin 	.write_end		= ext4_journalled_write_end,
3046617ba13bSMingming Cao 	.set_page_dirty		= ext4_journalled_set_page_dirty,
3047617ba13bSMingming Cao 	.bmap			= ext4_bmap,
3048617ba13bSMingming Cao 	.invalidatepage		= ext4_invalidatepage,
3049617ba13bSMingming Cao 	.releasepage		= ext4_releasepage,
30508ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
3051ac27a0ecSDave Kleikamp };
3052ac27a0ecSDave Kleikamp 
305364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = {
305464769240SAlex Tomas 	.readpage		= ext4_readpage,
305564769240SAlex Tomas 	.readpages		= ext4_readpages,
305664769240SAlex Tomas 	.writepage		= ext4_da_writepage,
305764769240SAlex Tomas 	.writepages		= ext4_da_writepages,
305864769240SAlex Tomas 	.sync_page		= block_sync_page,
305964769240SAlex Tomas 	.write_begin		= ext4_da_write_begin,
306064769240SAlex Tomas 	.write_end		= ext4_da_write_end,
306164769240SAlex Tomas 	.bmap			= ext4_bmap,
306264769240SAlex Tomas 	.invalidatepage		= ext4_da_invalidatepage,
306364769240SAlex Tomas 	.releasepage		= ext4_releasepage,
306464769240SAlex Tomas 	.direct_IO		= ext4_direct_IO,
306564769240SAlex Tomas 	.migratepage		= buffer_migrate_page,
30668ab22b9aSHisashi Hifumi 	.is_partially_uptodate  = block_is_partially_uptodate,
306764769240SAlex Tomas };
306864769240SAlex Tomas 
3069617ba13bSMingming Cao void ext4_set_aops(struct inode *inode)
3070ac27a0ecSDave Kleikamp {
3071cd1aac32SAneesh Kumar K.V 	if (ext4_should_order_data(inode) &&
3072cd1aac32SAneesh Kumar K.V 		test_opt(inode->i_sb, DELALLOC))
3073cd1aac32SAneesh Kumar K.V 		inode->i_mapping->a_ops = &ext4_da_aops;
3074cd1aac32SAneesh Kumar K.V 	else if (ext4_should_order_data(inode))
3075617ba13bSMingming Cao 		inode->i_mapping->a_ops = &ext4_ordered_aops;
307664769240SAlex Tomas 	else if (ext4_should_writeback_data(inode) &&
307764769240SAlex Tomas 		 test_opt(inode->i_sb, DELALLOC))
307864769240SAlex Tomas 		inode->i_mapping->a_ops = &ext4_da_aops;
3079617ba13bSMingming Cao 	else if (ext4_should_writeback_data(inode))
3080617ba13bSMingming Cao 		inode->i_mapping->a_ops = &ext4_writeback_aops;
3081ac27a0ecSDave Kleikamp 	else
3082617ba13bSMingming Cao 		inode->i_mapping->a_ops = &ext4_journalled_aops;
3083ac27a0ecSDave Kleikamp }
3084ac27a0ecSDave Kleikamp 
3085ac27a0ecSDave Kleikamp /*
3086617ba13bSMingming Cao  * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3087ac27a0ecSDave Kleikamp  * up to the end of the block which corresponds to `from'.
3088ac27a0ecSDave Kleikamp  * This required during truncate. We need to physically zero the tail end
3089ac27a0ecSDave Kleikamp  * of that block so it doesn't yield old data if the file is later grown.
3090ac27a0ecSDave Kleikamp  */
3091cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle,
3092ac27a0ecSDave Kleikamp 		struct address_space *mapping, loff_t from)
3093ac27a0ecSDave Kleikamp {
3094617ba13bSMingming Cao 	ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3095ac27a0ecSDave Kleikamp 	unsigned offset = from & (PAGE_CACHE_SIZE-1);
3096725d26d3SAneesh Kumar K.V 	unsigned blocksize, length, pos;
3097725d26d3SAneesh Kumar K.V 	ext4_lblk_t iblock;
3098ac27a0ecSDave Kleikamp 	struct inode *inode = mapping->host;
3099ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3100cf108bcaSJan Kara 	struct page *page;
3101ac27a0ecSDave Kleikamp 	int err = 0;
3102ac27a0ecSDave Kleikamp 
3103cf108bcaSJan Kara 	page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT);
3104cf108bcaSJan Kara 	if (!page)
3105cf108bcaSJan Kara 		return -EINVAL;
3106cf108bcaSJan Kara 
3107ac27a0ecSDave Kleikamp 	blocksize = inode->i_sb->s_blocksize;
3108ac27a0ecSDave Kleikamp 	length = blocksize - (offset & (blocksize - 1));
3109ac27a0ecSDave Kleikamp 	iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3110ac27a0ecSDave Kleikamp 
3111ac27a0ecSDave Kleikamp 	/*
3112ac27a0ecSDave Kleikamp 	 * For "nobh" option,  we can only work if we don't need to
3113ac27a0ecSDave Kleikamp 	 * read-in the page - otherwise we create buffers to do the IO.
3114ac27a0ecSDave Kleikamp 	 */
3115ac27a0ecSDave Kleikamp 	if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
3116617ba13bSMingming Cao 	     ext4_should_writeback_data(inode) && PageUptodate(page)) {
3117eebd2aa3SChristoph Lameter 		zero_user(page, offset, length);
3118ac27a0ecSDave Kleikamp 		set_page_dirty(page);
3119ac27a0ecSDave Kleikamp 		goto unlock;
3120ac27a0ecSDave Kleikamp 	}
3121ac27a0ecSDave Kleikamp 
3122ac27a0ecSDave Kleikamp 	if (!page_has_buffers(page))
3123ac27a0ecSDave Kleikamp 		create_empty_buffers(page, blocksize, 0);
3124ac27a0ecSDave Kleikamp 
3125ac27a0ecSDave Kleikamp 	/* Find the buffer that contains "offset" */
3126ac27a0ecSDave Kleikamp 	bh = page_buffers(page);
3127ac27a0ecSDave Kleikamp 	pos = blocksize;
3128ac27a0ecSDave Kleikamp 	while (offset >= pos) {
3129ac27a0ecSDave Kleikamp 		bh = bh->b_this_page;
3130ac27a0ecSDave Kleikamp 		iblock++;
3131ac27a0ecSDave Kleikamp 		pos += blocksize;
3132ac27a0ecSDave Kleikamp 	}
3133ac27a0ecSDave Kleikamp 
3134ac27a0ecSDave Kleikamp 	err = 0;
3135ac27a0ecSDave Kleikamp 	if (buffer_freed(bh)) {
3136ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "freed: skip");
3137ac27a0ecSDave Kleikamp 		goto unlock;
3138ac27a0ecSDave Kleikamp 	}
3139ac27a0ecSDave Kleikamp 
3140ac27a0ecSDave Kleikamp 	if (!buffer_mapped(bh)) {
3141ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "unmapped");
3142617ba13bSMingming Cao 		ext4_get_block(inode, iblock, bh, 0);
3143ac27a0ecSDave Kleikamp 		/* unmapped? It's a hole - nothing to do */
3144ac27a0ecSDave Kleikamp 		if (!buffer_mapped(bh)) {
3145ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bh, "still unmapped");
3146ac27a0ecSDave Kleikamp 			goto unlock;
3147ac27a0ecSDave Kleikamp 		}
3148ac27a0ecSDave Kleikamp 	}
3149ac27a0ecSDave Kleikamp 
3150ac27a0ecSDave Kleikamp 	/* Ok, it's mapped. Make sure it's up-to-date */
3151ac27a0ecSDave Kleikamp 	if (PageUptodate(page))
3152ac27a0ecSDave Kleikamp 		set_buffer_uptodate(bh);
3153ac27a0ecSDave Kleikamp 
3154ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(bh)) {
3155ac27a0ecSDave Kleikamp 		err = -EIO;
3156ac27a0ecSDave Kleikamp 		ll_rw_block(READ, 1, &bh);
3157ac27a0ecSDave Kleikamp 		wait_on_buffer(bh);
3158ac27a0ecSDave Kleikamp 		/* Uhhuh. Read error. Complain and punt. */
3159ac27a0ecSDave Kleikamp 		if (!buffer_uptodate(bh))
3160ac27a0ecSDave Kleikamp 			goto unlock;
3161ac27a0ecSDave Kleikamp 	}
3162ac27a0ecSDave Kleikamp 
3163617ba13bSMingming Cao 	if (ext4_should_journal_data(inode)) {
3164ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh, "get write access");
3165617ba13bSMingming Cao 		err = ext4_journal_get_write_access(handle, bh);
3166ac27a0ecSDave Kleikamp 		if (err)
3167ac27a0ecSDave Kleikamp 			goto unlock;
3168ac27a0ecSDave Kleikamp 	}
3169ac27a0ecSDave Kleikamp 
3170eebd2aa3SChristoph Lameter 	zero_user(page, offset, length);
3171ac27a0ecSDave Kleikamp 
3172ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh, "zeroed end of block");
3173ac27a0ecSDave Kleikamp 
3174ac27a0ecSDave Kleikamp 	err = 0;
3175617ba13bSMingming Cao 	if (ext4_should_journal_data(inode)) {
3176617ba13bSMingming Cao 		err = ext4_journal_dirty_metadata(handle, bh);
3177ac27a0ecSDave Kleikamp 	} else {
3178617ba13bSMingming Cao 		if (ext4_should_order_data(inode))
3179678aaf48SJan Kara 			err = ext4_jbd2_file_inode(handle, inode);
3180ac27a0ecSDave Kleikamp 		mark_buffer_dirty(bh);
3181ac27a0ecSDave Kleikamp 	}
3182ac27a0ecSDave Kleikamp 
3183ac27a0ecSDave Kleikamp unlock:
3184ac27a0ecSDave Kleikamp 	unlock_page(page);
3185ac27a0ecSDave Kleikamp 	page_cache_release(page);
3186ac27a0ecSDave Kleikamp 	return err;
3187ac27a0ecSDave Kleikamp }
3188ac27a0ecSDave Kleikamp 
3189ac27a0ecSDave Kleikamp /*
3190ac27a0ecSDave Kleikamp  * Probably it should be a library function... search for first non-zero word
3191ac27a0ecSDave Kleikamp  * or memcmp with zero_page, whatever is better for particular architecture.
3192ac27a0ecSDave Kleikamp  * Linus?
3193ac27a0ecSDave Kleikamp  */
3194ac27a0ecSDave Kleikamp static inline int all_zeroes(__le32 *p, __le32 *q)
3195ac27a0ecSDave Kleikamp {
3196ac27a0ecSDave Kleikamp 	while (p < q)
3197ac27a0ecSDave Kleikamp 		if (*p++)
3198ac27a0ecSDave Kleikamp 			return 0;
3199ac27a0ecSDave Kleikamp 	return 1;
3200ac27a0ecSDave Kleikamp }
3201ac27a0ecSDave Kleikamp 
3202ac27a0ecSDave Kleikamp /**
3203617ba13bSMingming Cao  *	ext4_find_shared - find the indirect blocks for partial truncation.
3204ac27a0ecSDave Kleikamp  *	@inode:	  inode in question
3205ac27a0ecSDave Kleikamp  *	@depth:	  depth of the affected branch
3206617ba13bSMingming Cao  *	@offsets: offsets of pointers in that branch (see ext4_block_to_path)
3207ac27a0ecSDave Kleikamp  *	@chain:	  place to store the pointers to partial indirect blocks
3208ac27a0ecSDave Kleikamp  *	@top:	  place to the (detached) top of branch
3209ac27a0ecSDave Kleikamp  *
3210617ba13bSMingming Cao  *	This is a helper function used by ext4_truncate().
3211ac27a0ecSDave Kleikamp  *
3212ac27a0ecSDave Kleikamp  *	When we do truncate() we may have to clean the ends of several
3213ac27a0ecSDave Kleikamp  *	indirect blocks but leave the blocks themselves alive. Block is
3214ac27a0ecSDave Kleikamp  *	partially truncated if some data below the new i_size is refered
3215ac27a0ecSDave Kleikamp  *	from it (and it is on the path to the first completely truncated
3216ac27a0ecSDave Kleikamp  *	data block, indeed).  We have to free the top of that path along
3217ac27a0ecSDave Kleikamp  *	with everything to the right of the path. Since no allocation
3218617ba13bSMingming Cao  *	past the truncation point is possible until ext4_truncate()
3219ac27a0ecSDave Kleikamp  *	finishes, we may safely do the latter, but top of branch may
3220ac27a0ecSDave Kleikamp  *	require special attention - pageout below the truncation point
3221ac27a0ecSDave Kleikamp  *	might try to populate it.
3222ac27a0ecSDave Kleikamp  *
3223ac27a0ecSDave Kleikamp  *	We atomically detach the top of branch from the tree, store the
3224ac27a0ecSDave Kleikamp  *	block number of its root in *@top, pointers to buffer_heads of
3225ac27a0ecSDave Kleikamp  *	partially truncated blocks - in @chain[].bh and pointers to
3226ac27a0ecSDave Kleikamp  *	their last elements that should not be removed - in
3227ac27a0ecSDave Kleikamp  *	@chain[].p. Return value is the pointer to last filled element
3228ac27a0ecSDave Kleikamp  *	of @chain.
3229ac27a0ecSDave Kleikamp  *
3230ac27a0ecSDave Kleikamp  *	The work left to caller to do the actual freeing of subtrees:
3231ac27a0ecSDave Kleikamp  *		a) free the subtree starting from *@top
3232ac27a0ecSDave Kleikamp  *		b) free the subtrees whose roots are stored in
3233ac27a0ecSDave Kleikamp  *			(@chain[i].p+1 .. end of @chain[i].bh->b_data)
3234ac27a0ecSDave Kleikamp  *		c) free the subtrees growing from the inode past the @chain[0].
3235ac27a0ecSDave Kleikamp  *			(no partially truncated stuff there).  */
3236ac27a0ecSDave Kleikamp 
3237617ba13bSMingming Cao static Indirect *ext4_find_shared(struct inode *inode, int depth,
3238725d26d3SAneesh Kumar K.V 			ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top)
3239ac27a0ecSDave Kleikamp {
3240ac27a0ecSDave Kleikamp 	Indirect *partial, *p;
3241ac27a0ecSDave Kleikamp 	int k, err;
3242ac27a0ecSDave Kleikamp 
3243ac27a0ecSDave Kleikamp 	*top = 0;
3244ac27a0ecSDave Kleikamp 	/* Make k index the deepest non-null offest + 1 */
3245ac27a0ecSDave Kleikamp 	for (k = depth; k > 1 && !offsets[k-1]; k--)
3246ac27a0ecSDave Kleikamp 		;
3247617ba13bSMingming Cao 	partial = ext4_get_branch(inode, k, offsets, chain, &err);
3248ac27a0ecSDave Kleikamp 	/* Writer: pointers */
3249ac27a0ecSDave Kleikamp 	if (!partial)
3250ac27a0ecSDave Kleikamp 		partial = chain + k-1;
3251ac27a0ecSDave Kleikamp 	/*
3252ac27a0ecSDave Kleikamp 	 * If the branch acquired continuation since we've looked at it -
3253ac27a0ecSDave Kleikamp 	 * fine, it should all survive and (new) top doesn't belong to us.
3254ac27a0ecSDave Kleikamp 	 */
3255ac27a0ecSDave Kleikamp 	if (!partial->key && *partial->p)
3256ac27a0ecSDave Kleikamp 		/* Writer: end */
3257ac27a0ecSDave Kleikamp 		goto no_top;
3258af5bc92dSTheodore Ts'o 	for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
3259ac27a0ecSDave Kleikamp 		;
3260ac27a0ecSDave Kleikamp 	/*
3261ac27a0ecSDave Kleikamp 	 * OK, we've found the last block that must survive. The rest of our
3262ac27a0ecSDave Kleikamp 	 * branch should be detached before unlocking. However, if that rest
3263ac27a0ecSDave Kleikamp 	 * of branch is all ours and does not grow immediately from the inode
3264ac27a0ecSDave Kleikamp 	 * it's easier to cheat and just decrement partial->p.
3265ac27a0ecSDave Kleikamp 	 */
3266ac27a0ecSDave Kleikamp 	if (p == chain + k - 1 && p > chain) {
3267ac27a0ecSDave Kleikamp 		p->p--;
3268ac27a0ecSDave Kleikamp 	} else {
3269ac27a0ecSDave Kleikamp 		*top = *p->p;
3270617ba13bSMingming Cao 		/* Nope, don't do this in ext4.  Must leave the tree intact */
3271ac27a0ecSDave Kleikamp #if 0
3272ac27a0ecSDave Kleikamp 		*p->p = 0;
3273ac27a0ecSDave Kleikamp #endif
3274ac27a0ecSDave Kleikamp 	}
3275ac27a0ecSDave Kleikamp 	/* Writer: end */
3276ac27a0ecSDave Kleikamp 
3277ac27a0ecSDave Kleikamp 	while (partial > p) {
3278ac27a0ecSDave Kleikamp 		brelse(partial->bh);
3279ac27a0ecSDave Kleikamp 		partial--;
3280ac27a0ecSDave Kleikamp 	}
3281ac27a0ecSDave Kleikamp no_top:
3282ac27a0ecSDave Kleikamp 	return partial;
3283ac27a0ecSDave Kleikamp }
3284ac27a0ecSDave Kleikamp 
3285ac27a0ecSDave Kleikamp /*
3286ac27a0ecSDave Kleikamp  * Zero a number of block pointers in either an inode or an indirect block.
3287ac27a0ecSDave Kleikamp  * If we restart the transaction we must again get write access to the
3288ac27a0ecSDave Kleikamp  * indirect block for further modification.
3289ac27a0ecSDave Kleikamp  *
3290ac27a0ecSDave Kleikamp  * We release `count' blocks on disk, but (last - first) may be greater
3291ac27a0ecSDave Kleikamp  * than `count' because there can be holes in there.
3292ac27a0ecSDave Kleikamp  */
3293617ba13bSMingming Cao static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
3294617ba13bSMingming Cao 		struct buffer_head *bh, ext4_fsblk_t block_to_free,
3295ac27a0ecSDave Kleikamp 		unsigned long count, __le32 *first, __le32 *last)
3296ac27a0ecSDave Kleikamp {
3297ac27a0ecSDave Kleikamp 	__le32 *p;
3298ac27a0ecSDave Kleikamp 	if (try_to_extend_transaction(handle, inode)) {
3299ac27a0ecSDave Kleikamp 		if (bh) {
3300617ba13bSMingming Cao 			BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
3301617ba13bSMingming Cao 			ext4_journal_dirty_metadata(handle, bh);
3302ac27a0ecSDave Kleikamp 		}
3303617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
3304617ba13bSMingming Cao 		ext4_journal_test_restart(handle, inode);
3305ac27a0ecSDave Kleikamp 		if (bh) {
3306ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bh, "retaking write access");
3307617ba13bSMingming Cao 			ext4_journal_get_write_access(handle, bh);
3308ac27a0ecSDave Kleikamp 		}
3309ac27a0ecSDave Kleikamp 	}
3310ac27a0ecSDave Kleikamp 
3311ac27a0ecSDave Kleikamp 	/*
3312ac27a0ecSDave Kleikamp 	 * Any buffers which are on the journal will be in memory. We find
3313dab291afSMingming Cao 	 * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget()
3314ac27a0ecSDave Kleikamp 	 * on them.  We've already detached each block from the file, so
3315dab291afSMingming Cao 	 * bforget() in jbd2_journal_forget() should be safe.
3316ac27a0ecSDave Kleikamp 	 *
3317dab291afSMingming Cao 	 * AKPM: turn on bforget in jbd2_journal_forget()!!!
3318ac27a0ecSDave Kleikamp 	 */
3319ac27a0ecSDave Kleikamp 	for (p = first; p < last; p++) {
3320ac27a0ecSDave Kleikamp 		u32 nr = le32_to_cpu(*p);
3321ac27a0ecSDave Kleikamp 		if (nr) {
33221d03ec98SAneesh Kumar K.V 			struct buffer_head *tbh;
3323ac27a0ecSDave Kleikamp 
3324ac27a0ecSDave Kleikamp 			*p = 0;
33251d03ec98SAneesh Kumar K.V 			tbh = sb_find_get_block(inode->i_sb, nr);
33261d03ec98SAneesh Kumar K.V 			ext4_forget(handle, 0, inode, tbh, nr);
3327ac27a0ecSDave Kleikamp 		}
3328ac27a0ecSDave Kleikamp 	}
3329ac27a0ecSDave Kleikamp 
3330c9de560dSAlex Tomas 	ext4_free_blocks(handle, inode, block_to_free, count, 0);
3331ac27a0ecSDave Kleikamp }
3332ac27a0ecSDave Kleikamp 
3333ac27a0ecSDave Kleikamp /**
3334617ba13bSMingming Cao  * ext4_free_data - free a list of data blocks
3335ac27a0ecSDave Kleikamp  * @handle:	handle for this transaction
3336ac27a0ecSDave Kleikamp  * @inode:	inode we are dealing with
3337ac27a0ecSDave Kleikamp  * @this_bh:	indirect buffer_head which contains *@first and *@last
3338ac27a0ecSDave Kleikamp  * @first:	array of block numbers
3339ac27a0ecSDave Kleikamp  * @last:	points immediately past the end of array
3340ac27a0ecSDave Kleikamp  *
3341ac27a0ecSDave Kleikamp  * We are freeing all blocks refered from that array (numbers are stored as
3342ac27a0ecSDave Kleikamp  * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3343ac27a0ecSDave Kleikamp  *
3344ac27a0ecSDave Kleikamp  * We accumulate contiguous runs of blocks to free.  Conveniently, if these
3345ac27a0ecSDave Kleikamp  * blocks are contiguous then releasing them at one time will only affect one
3346ac27a0ecSDave Kleikamp  * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3347ac27a0ecSDave Kleikamp  * actually use a lot of journal space.
3348ac27a0ecSDave Kleikamp  *
3349ac27a0ecSDave Kleikamp  * @this_bh will be %NULL if @first and @last point into the inode's direct
3350ac27a0ecSDave Kleikamp  * block pointers.
3351ac27a0ecSDave Kleikamp  */
3352617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode,
3353ac27a0ecSDave Kleikamp 			   struct buffer_head *this_bh,
3354ac27a0ecSDave Kleikamp 			   __le32 *first, __le32 *last)
3355ac27a0ecSDave Kleikamp {
3356617ba13bSMingming Cao 	ext4_fsblk_t block_to_free = 0;    /* Starting block # of a run */
3357ac27a0ecSDave Kleikamp 	unsigned long count = 0;	    /* Number of blocks in the run */
3358ac27a0ecSDave Kleikamp 	__le32 *block_to_free_p = NULL;	    /* Pointer into inode/ind
3359ac27a0ecSDave Kleikamp 					       corresponding to
3360ac27a0ecSDave Kleikamp 					       block_to_free */
3361617ba13bSMingming Cao 	ext4_fsblk_t nr;		    /* Current block # */
3362ac27a0ecSDave Kleikamp 	__le32 *p;			    /* Pointer into inode/ind
3363ac27a0ecSDave Kleikamp 					       for current block */
3364ac27a0ecSDave Kleikamp 	int err;
3365ac27a0ecSDave Kleikamp 
3366ac27a0ecSDave Kleikamp 	if (this_bh) {				/* For indirect block */
3367ac27a0ecSDave Kleikamp 		BUFFER_TRACE(this_bh, "get_write_access");
3368617ba13bSMingming Cao 		err = ext4_journal_get_write_access(handle, this_bh);
3369ac27a0ecSDave Kleikamp 		/* Important: if we can't update the indirect pointers
3370ac27a0ecSDave Kleikamp 		 * to the blocks, we can't free them. */
3371ac27a0ecSDave Kleikamp 		if (err)
3372ac27a0ecSDave Kleikamp 			return;
3373ac27a0ecSDave Kleikamp 	}
3374ac27a0ecSDave Kleikamp 
3375ac27a0ecSDave Kleikamp 	for (p = first; p < last; p++) {
3376ac27a0ecSDave Kleikamp 		nr = le32_to_cpu(*p);
3377ac27a0ecSDave Kleikamp 		if (nr) {
3378ac27a0ecSDave Kleikamp 			/* accumulate blocks to free if they're contiguous */
3379ac27a0ecSDave Kleikamp 			if (count == 0) {
3380ac27a0ecSDave Kleikamp 				block_to_free = nr;
3381ac27a0ecSDave Kleikamp 				block_to_free_p = p;
3382ac27a0ecSDave Kleikamp 				count = 1;
3383ac27a0ecSDave Kleikamp 			} else if (nr == block_to_free + count) {
3384ac27a0ecSDave Kleikamp 				count++;
3385ac27a0ecSDave Kleikamp 			} else {
3386617ba13bSMingming Cao 				ext4_clear_blocks(handle, inode, this_bh,
3387ac27a0ecSDave Kleikamp 						  block_to_free,
3388ac27a0ecSDave Kleikamp 						  count, block_to_free_p, p);
3389ac27a0ecSDave Kleikamp 				block_to_free = nr;
3390ac27a0ecSDave Kleikamp 				block_to_free_p = p;
3391ac27a0ecSDave Kleikamp 				count = 1;
3392ac27a0ecSDave Kleikamp 			}
3393ac27a0ecSDave Kleikamp 		}
3394ac27a0ecSDave Kleikamp 	}
3395ac27a0ecSDave Kleikamp 
3396ac27a0ecSDave Kleikamp 	if (count > 0)
3397617ba13bSMingming Cao 		ext4_clear_blocks(handle, inode, this_bh, block_to_free,
3398ac27a0ecSDave Kleikamp 				  count, block_to_free_p, p);
3399ac27a0ecSDave Kleikamp 
3400ac27a0ecSDave Kleikamp 	if (this_bh) {
3401617ba13bSMingming Cao 		BUFFER_TRACE(this_bh, "call ext4_journal_dirty_metadata");
340271dc8fbcSDuane Griffin 
340371dc8fbcSDuane Griffin 		/*
340471dc8fbcSDuane Griffin 		 * The buffer head should have an attached journal head at this
340571dc8fbcSDuane Griffin 		 * point. However, if the data is corrupted and an indirect
340671dc8fbcSDuane Griffin 		 * block pointed to itself, it would have been detached when
340771dc8fbcSDuane Griffin 		 * the block was cleared. Check for this instead of OOPSing.
340871dc8fbcSDuane Griffin 		 */
340971dc8fbcSDuane Griffin 		if (bh2jh(this_bh))
3410617ba13bSMingming Cao 			ext4_journal_dirty_metadata(handle, this_bh);
341171dc8fbcSDuane Griffin 		else
341271dc8fbcSDuane Griffin 			ext4_error(inode->i_sb, __func__,
341371dc8fbcSDuane Griffin 				   "circular indirect block detected, "
341471dc8fbcSDuane Griffin 				   "inode=%lu, block=%llu",
341571dc8fbcSDuane Griffin 				   inode->i_ino,
341671dc8fbcSDuane Griffin 				   (unsigned long long) this_bh->b_blocknr);
3417ac27a0ecSDave Kleikamp 	}
3418ac27a0ecSDave Kleikamp }
3419ac27a0ecSDave Kleikamp 
3420ac27a0ecSDave Kleikamp /**
3421617ba13bSMingming Cao  *	ext4_free_branches - free an array of branches
3422ac27a0ecSDave Kleikamp  *	@handle: JBD handle for this transaction
3423ac27a0ecSDave Kleikamp  *	@inode:	inode we are dealing with
3424ac27a0ecSDave Kleikamp  *	@parent_bh: the buffer_head which contains *@first and *@last
3425ac27a0ecSDave Kleikamp  *	@first:	array of block numbers
3426ac27a0ecSDave Kleikamp  *	@last:	pointer immediately past the end of array
3427ac27a0ecSDave Kleikamp  *	@depth:	depth of the branches to free
3428ac27a0ecSDave Kleikamp  *
3429ac27a0ecSDave Kleikamp  *	We are freeing all blocks refered from these branches (numbers are
3430ac27a0ecSDave Kleikamp  *	stored as little-endian 32-bit) and updating @inode->i_blocks
3431ac27a0ecSDave Kleikamp  *	appropriately.
3432ac27a0ecSDave Kleikamp  */
3433617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode,
3434ac27a0ecSDave Kleikamp 			       struct buffer_head *parent_bh,
3435ac27a0ecSDave Kleikamp 			       __le32 *first, __le32 *last, int depth)
3436ac27a0ecSDave Kleikamp {
3437617ba13bSMingming Cao 	ext4_fsblk_t nr;
3438ac27a0ecSDave Kleikamp 	__le32 *p;
3439ac27a0ecSDave Kleikamp 
3440ac27a0ecSDave Kleikamp 	if (is_handle_aborted(handle))
3441ac27a0ecSDave Kleikamp 		return;
3442ac27a0ecSDave Kleikamp 
3443ac27a0ecSDave Kleikamp 	if (depth--) {
3444ac27a0ecSDave Kleikamp 		struct buffer_head *bh;
3445617ba13bSMingming Cao 		int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
3446ac27a0ecSDave Kleikamp 		p = last;
3447ac27a0ecSDave Kleikamp 		while (--p >= first) {
3448ac27a0ecSDave Kleikamp 			nr = le32_to_cpu(*p);
3449ac27a0ecSDave Kleikamp 			if (!nr)
3450ac27a0ecSDave Kleikamp 				continue;		/* A hole */
3451ac27a0ecSDave Kleikamp 
3452ac27a0ecSDave Kleikamp 			/* Go read the buffer for the next level down */
3453ac27a0ecSDave Kleikamp 			bh = sb_bread(inode->i_sb, nr);
3454ac27a0ecSDave Kleikamp 
3455ac27a0ecSDave Kleikamp 			/*
3456ac27a0ecSDave Kleikamp 			 * A read failure? Report error and clear slot
3457ac27a0ecSDave Kleikamp 			 * (should be rare).
3458ac27a0ecSDave Kleikamp 			 */
3459ac27a0ecSDave Kleikamp 			if (!bh) {
3460617ba13bSMingming Cao 				ext4_error(inode->i_sb, "ext4_free_branches",
34612ae02107SMingming Cao 					   "Read failure, inode=%lu, block=%llu",
3462ac27a0ecSDave Kleikamp 					   inode->i_ino, nr);
3463ac27a0ecSDave Kleikamp 				continue;
3464ac27a0ecSDave Kleikamp 			}
3465ac27a0ecSDave Kleikamp 
3466ac27a0ecSDave Kleikamp 			/* This zaps the entire block.  Bottom up. */
3467ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bh, "free child branches");
3468617ba13bSMingming Cao 			ext4_free_branches(handle, inode, bh,
3469ac27a0ecSDave Kleikamp 					(__le32 *) bh->b_data,
3470ac27a0ecSDave Kleikamp 					(__le32 *) bh->b_data + addr_per_block,
3471ac27a0ecSDave Kleikamp 					depth);
3472ac27a0ecSDave Kleikamp 
3473ac27a0ecSDave Kleikamp 			/*
3474ac27a0ecSDave Kleikamp 			 * We've probably journalled the indirect block several
3475ac27a0ecSDave Kleikamp 			 * times during the truncate.  But it's no longer
3476ac27a0ecSDave Kleikamp 			 * needed and we now drop it from the transaction via
3477dab291afSMingming Cao 			 * jbd2_journal_revoke().
3478ac27a0ecSDave Kleikamp 			 *
3479ac27a0ecSDave Kleikamp 			 * That's easy if it's exclusively part of this
3480ac27a0ecSDave Kleikamp 			 * transaction.  But if it's part of the committing
3481dab291afSMingming Cao 			 * transaction then jbd2_journal_forget() will simply
3482ac27a0ecSDave Kleikamp 			 * brelse() it.  That means that if the underlying
3483617ba13bSMingming Cao 			 * block is reallocated in ext4_get_block(),
3484ac27a0ecSDave Kleikamp 			 * unmap_underlying_metadata() will find this block
3485ac27a0ecSDave Kleikamp 			 * and will try to get rid of it.  damn, damn.
3486ac27a0ecSDave Kleikamp 			 *
3487ac27a0ecSDave Kleikamp 			 * If this block has already been committed to the
3488ac27a0ecSDave Kleikamp 			 * journal, a revoke record will be written.  And
3489ac27a0ecSDave Kleikamp 			 * revoke records must be emitted *before* clearing
3490ac27a0ecSDave Kleikamp 			 * this block's bit in the bitmaps.
3491ac27a0ecSDave Kleikamp 			 */
3492617ba13bSMingming Cao 			ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
3493ac27a0ecSDave Kleikamp 
3494ac27a0ecSDave Kleikamp 			/*
3495ac27a0ecSDave Kleikamp 			 * Everything below this this pointer has been
3496ac27a0ecSDave Kleikamp 			 * released.  Now let this top-of-subtree go.
3497ac27a0ecSDave Kleikamp 			 *
3498ac27a0ecSDave Kleikamp 			 * We want the freeing of this indirect block to be
3499ac27a0ecSDave Kleikamp 			 * atomic in the journal with the updating of the
3500ac27a0ecSDave Kleikamp 			 * bitmap block which owns it.  So make some room in
3501ac27a0ecSDave Kleikamp 			 * the journal.
3502ac27a0ecSDave Kleikamp 			 *
3503ac27a0ecSDave Kleikamp 			 * We zero the parent pointer *after* freeing its
3504ac27a0ecSDave Kleikamp 			 * pointee in the bitmaps, so if extend_transaction()
3505ac27a0ecSDave Kleikamp 			 * for some reason fails to put the bitmap changes and
3506ac27a0ecSDave Kleikamp 			 * the release into the same transaction, recovery
3507ac27a0ecSDave Kleikamp 			 * will merely complain about releasing a free block,
3508ac27a0ecSDave Kleikamp 			 * rather than leaking blocks.
3509ac27a0ecSDave Kleikamp 			 */
3510ac27a0ecSDave Kleikamp 			if (is_handle_aborted(handle))
3511ac27a0ecSDave Kleikamp 				return;
3512ac27a0ecSDave Kleikamp 			if (try_to_extend_transaction(handle, inode)) {
3513617ba13bSMingming Cao 				ext4_mark_inode_dirty(handle, inode);
3514617ba13bSMingming Cao 				ext4_journal_test_restart(handle, inode);
3515ac27a0ecSDave Kleikamp 			}
3516ac27a0ecSDave Kleikamp 
3517c9de560dSAlex Tomas 			ext4_free_blocks(handle, inode, nr, 1, 1);
3518ac27a0ecSDave Kleikamp 
3519ac27a0ecSDave Kleikamp 			if (parent_bh) {
3520ac27a0ecSDave Kleikamp 				/*
3521ac27a0ecSDave Kleikamp 				 * The block which we have just freed is
3522ac27a0ecSDave Kleikamp 				 * pointed to by an indirect block: journal it
3523ac27a0ecSDave Kleikamp 				 */
3524ac27a0ecSDave Kleikamp 				BUFFER_TRACE(parent_bh, "get_write_access");
3525617ba13bSMingming Cao 				if (!ext4_journal_get_write_access(handle,
3526ac27a0ecSDave Kleikamp 								   parent_bh)){
3527ac27a0ecSDave Kleikamp 					*p = 0;
3528ac27a0ecSDave Kleikamp 					BUFFER_TRACE(parent_bh,
3529617ba13bSMingming Cao 					"call ext4_journal_dirty_metadata");
3530617ba13bSMingming Cao 					ext4_journal_dirty_metadata(handle,
3531ac27a0ecSDave Kleikamp 								    parent_bh);
3532ac27a0ecSDave Kleikamp 				}
3533ac27a0ecSDave Kleikamp 			}
3534ac27a0ecSDave Kleikamp 		}
3535ac27a0ecSDave Kleikamp 	} else {
3536ac27a0ecSDave Kleikamp 		/* We have reached the bottom of the tree. */
3537ac27a0ecSDave Kleikamp 		BUFFER_TRACE(parent_bh, "free data blocks");
3538617ba13bSMingming Cao 		ext4_free_data(handle, inode, parent_bh, first, last);
3539ac27a0ecSDave Kleikamp 	}
3540ac27a0ecSDave Kleikamp }
3541ac27a0ecSDave Kleikamp 
354291ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode)
354391ef4cafSDuane Griffin {
354491ef4cafSDuane Griffin 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
354591ef4cafSDuane Griffin 		return 0;
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 /*
3556617ba13bSMingming Cao  * ext4_truncate()
3557ac27a0ecSDave Kleikamp  *
3558617ba13bSMingming Cao  * We block out ext4_get_block() block instantiations across the entire
3559617ba13bSMingming Cao  * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3560ac27a0ecSDave Kleikamp  * simultaneously on behalf of the same inode.
3561ac27a0ecSDave Kleikamp  *
3562ac27a0ecSDave Kleikamp  * As we work through the truncate and commmit bits of it to the journal there
3563ac27a0ecSDave Kleikamp  * is one core, guiding principle: the file's tree must always be consistent on
3564ac27a0ecSDave Kleikamp  * disk.  We must be able to restart the truncate after a crash.
3565ac27a0ecSDave Kleikamp  *
3566ac27a0ecSDave Kleikamp  * The file's tree may be transiently inconsistent in memory (although it
3567ac27a0ecSDave Kleikamp  * probably isn't), but whenever we close off and commit a journal transaction,
3568ac27a0ecSDave Kleikamp  * the contents of (the filesystem + the journal) must be consistent and
3569ac27a0ecSDave Kleikamp  * restartable.  It's pretty simple, really: bottom up, right to left (although
3570ac27a0ecSDave Kleikamp  * left-to-right works OK too).
3571ac27a0ecSDave Kleikamp  *
3572ac27a0ecSDave Kleikamp  * Note that at recovery time, journal replay occurs *before* the restart of
3573ac27a0ecSDave Kleikamp  * truncate against the orphan inode list.
3574ac27a0ecSDave Kleikamp  *
3575ac27a0ecSDave Kleikamp  * The committed inode has the new, desired i_size (which is the same as
3576617ba13bSMingming Cao  * i_disksize in this case).  After a crash, ext4_orphan_cleanup() will see
3577ac27a0ecSDave Kleikamp  * that this inode's truncate did not complete and it will again call
3578617ba13bSMingming Cao  * ext4_truncate() to have another go.  So there will be instantiated blocks
3579617ba13bSMingming Cao  * to the right of the truncation point in a crashed ext4 filesystem.  But
3580ac27a0ecSDave Kleikamp  * that's fine - as long as they are linked from the inode, the post-crash
3581617ba13bSMingming Cao  * ext4_truncate() run will find them and release them.
3582ac27a0ecSDave Kleikamp  */
3583617ba13bSMingming Cao void ext4_truncate(struct inode *inode)
3584ac27a0ecSDave Kleikamp {
3585ac27a0ecSDave Kleikamp 	handle_t *handle;
3586617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
3587ac27a0ecSDave Kleikamp 	__le32 *i_data = ei->i_data;
3588617ba13bSMingming Cao 	int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
3589ac27a0ecSDave Kleikamp 	struct address_space *mapping = inode->i_mapping;
3590725d26d3SAneesh Kumar K.V 	ext4_lblk_t offsets[4];
3591ac27a0ecSDave Kleikamp 	Indirect chain[4];
3592ac27a0ecSDave Kleikamp 	Indirect *partial;
3593ac27a0ecSDave Kleikamp 	__le32 nr = 0;
3594ac27a0ecSDave Kleikamp 	int n;
3595725d26d3SAneesh Kumar K.V 	ext4_lblk_t last_block;
3596ac27a0ecSDave Kleikamp 	unsigned blocksize = inode->i_sb->s_blocksize;
3597ac27a0ecSDave Kleikamp 
359891ef4cafSDuane Griffin 	if (!ext4_can_truncate(inode))
3599ac27a0ecSDave Kleikamp 		return;
3600ac27a0ecSDave Kleikamp 
36011d03ec98SAneesh Kumar K.V 	if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
3602cf108bcaSJan Kara 		ext4_ext_truncate(inode);
36031d03ec98SAneesh Kumar K.V 		return;
36041d03ec98SAneesh Kumar K.V 	}
3605a86c6181SAlex Tomas 
3606ac27a0ecSDave Kleikamp 	handle = start_transaction(inode);
3607cf108bcaSJan Kara 	if (IS_ERR(handle))
3608ac27a0ecSDave Kleikamp 		return;		/* AKPM: return what? */
3609ac27a0ecSDave Kleikamp 
3610ac27a0ecSDave Kleikamp 	last_block = (inode->i_size + blocksize-1)
3611617ba13bSMingming Cao 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
3612ac27a0ecSDave Kleikamp 
3613cf108bcaSJan Kara 	if (inode->i_size & (blocksize - 1))
3614cf108bcaSJan Kara 		if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3615cf108bcaSJan Kara 			goto out_stop;
3616ac27a0ecSDave Kleikamp 
3617617ba13bSMingming Cao 	n = ext4_block_to_path(inode, last_block, offsets, NULL);
3618ac27a0ecSDave Kleikamp 	if (n == 0)
3619ac27a0ecSDave Kleikamp 		goto out_stop;	/* error */
3620ac27a0ecSDave Kleikamp 
3621ac27a0ecSDave Kleikamp 	/*
3622ac27a0ecSDave Kleikamp 	 * OK.  This truncate is going to happen.  We add the inode to the
3623ac27a0ecSDave Kleikamp 	 * orphan list, so that if this truncate spans multiple transactions,
3624ac27a0ecSDave Kleikamp 	 * and we crash, we will resume the truncate when the filesystem
3625ac27a0ecSDave Kleikamp 	 * recovers.  It also marks the inode dirty, to catch the new size.
3626ac27a0ecSDave Kleikamp 	 *
3627ac27a0ecSDave Kleikamp 	 * Implication: the file must always be in a sane, consistent
3628ac27a0ecSDave Kleikamp 	 * truncatable state while each transaction commits.
3629ac27a0ecSDave Kleikamp 	 */
3630617ba13bSMingming Cao 	if (ext4_orphan_add(handle, inode))
3631ac27a0ecSDave Kleikamp 		goto out_stop;
3632ac27a0ecSDave Kleikamp 
3633ac27a0ecSDave Kleikamp 	/*
3634632eaeabSMingming Cao 	 * From here we block out all ext4_get_block() callers who want to
3635632eaeabSMingming Cao 	 * modify the block allocation tree.
3636632eaeabSMingming Cao 	 */
3637632eaeabSMingming Cao 	down_write(&ei->i_data_sem);
3638b4df2030STheodore Ts'o 
3639b4df2030STheodore Ts'o 	ext4_discard_reservation(inode);
3640b4df2030STheodore Ts'o 
3641632eaeabSMingming Cao 	/*
3642ac27a0ecSDave Kleikamp 	 * The orphan list entry will now protect us from any crash which
3643ac27a0ecSDave Kleikamp 	 * occurs before the truncate completes, so it is now safe to propagate
3644ac27a0ecSDave Kleikamp 	 * the new, shorter inode size (held for now in i_size) into the
3645ac27a0ecSDave Kleikamp 	 * on-disk inode. We do this via i_disksize, which is the value which
3646617ba13bSMingming Cao 	 * ext4 *really* writes onto the disk inode.
3647ac27a0ecSDave Kleikamp 	 */
3648ac27a0ecSDave Kleikamp 	ei->i_disksize = inode->i_size;
3649ac27a0ecSDave Kleikamp 
3650ac27a0ecSDave Kleikamp 	if (n == 1) {		/* direct blocks */
3651617ba13bSMingming Cao 		ext4_free_data(handle, inode, NULL, i_data+offsets[0],
3652617ba13bSMingming Cao 			       i_data + EXT4_NDIR_BLOCKS);
3653ac27a0ecSDave Kleikamp 		goto do_indirects;
3654ac27a0ecSDave Kleikamp 	}
3655ac27a0ecSDave Kleikamp 
3656617ba13bSMingming Cao 	partial = ext4_find_shared(inode, n, offsets, chain, &nr);
3657ac27a0ecSDave Kleikamp 	/* Kill the top of shared branch (not detached) */
3658ac27a0ecSDave Kleikamp 	if (nr) {
3659ac27a0ecSDave Kleikamp 		if (partial == chain) {
3660ac27a0ecSDave Kleikamp 			/* Shared branch grows from the inode */
3661617ba13bSMingming Cao 			ext4_free_branches(handle, inode, NULL,
3662ac27a0ecSDave Kleikamp 					   &nr, &nr+1, (chain+n-1) - partial);
3663ac27a0ecSDave Kleikamp 			*partial->p = 0;
3664ac27a0ecSDave Kleikamp 			/*
3665ac27a0ecSDave Kleikamp 			 * We mark the inode dirty prior to restart,
3666ac27a0ecSDave Kleikamp 			 * and prior to stop.  No need for it here.
3667ac27a0ecSDave Kleikamp 			 */
3668ac27a0ecSDave Kleikamp 		} else {
3669ac27a0ecSDave Kleikamp 			/* Shared branch grows from an indirect block */
3670ac27a0ecSDave Kleikamp 			BUFFER_TRACE(partial->bh, "get_write_access");
3671617ba13bSMingming Cao 			ext4_free_branches(handle, inode, partial->bh,
3672ac27a0ecSDave Kleikamp 					partial->p,
3673ac27a0ecSDave Kleikamp 					partial->p+1, (chain+n-1) - partial);
3674ac27a0ecSDave Kleikamp 		}
3675ac27a0ecSDave Kleikamp 	}
3676ac27a0ecSDave Kleikamp 	/* Clear the ends of indirect blocks on the shared branch */
3677ac27a0ecSDave Kleikamp 	while (partial > chain) {
3678617ba13bSMingming Cao 		ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
3679ac27a0ecSDave Kleikamp 				   (__le32*)partial->bh->b_data+addr_per_block,
3680ac27a0ecSDave Kleikamp 				   (chain+n-1) - partial);
3681ac27a0ecSDave Kleikamp 		BUFFER_TRACE(partial->bh, "call brelse");
3682ac27a0ecSDave Kleikamp 		brelse (partial->bh);
3683ac27a0ecSDave Kleikamp 		partial--;
3684ac27a0ecSDave Kleikamp 	}
3685ac27a0ecSDave Kleikamp do_indirects:
3686ac27a0ecSDave Kleikamp 	/* Kill the remaining (whole) subtrees */
3687ac27a0ecSDave Kleikamp 	switch (offsets[0]) {
3688ac27a0ecSDave Kleikamp 	default:
3689617ba13bSMingming Cao 		nr = i_data[EXT4_IND_BLOCK];
3690ac27a0ecSDave Kleikamp 		if (nr) {
3691617ba13bSMingming Cao 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
3692617ba13bSMingming Cao 			i_data[EXT4_IND_BLOCK] = 0;
3693ac27a0ecSDave Kleikamp 		}
3694617ba13bSMingming Cao 	case EXT4_IND_BLOCK:
3695617ba13bSMingming Cao 		nr = i_data[EXT4_DIND_BLOCK];
3696ac27a0ecSDave Kleikamp 		if (nr) {
3697617ba13bSMingming Cao 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
3698617ba13bSMingming Cao 			i_data[EXT4_DIND_BLOCK] = 0;
3699ac27a0ecSDave Kleikamp 		}
3700617ba13bSMingming Cao 	case EXT4_DIND_BLOCK:
3701617ba13bSMingming Cao 		nr = i_data[EXT4_TIND_BLOCK];
3702ac27a0ecSDave Kleikamp 		if (nr) {
3703617ba13bSMingming Cao 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
3704617ba13bSMingming Cao 			i_data[EXT4_TIND_BLOCK] = 0;
3705ac27a0ecSDave Kleikamp 		}
3706617ba13bSMingming Cao 	case EXT4_TIND_BLOCK:
3707ac27a0ecSDave Kleikamp 		;
3708ac27a0ecSDave Kleikamp 	}
3709ac27a0ecSDave Kleikamp 
37100e855ac8SAneesh Kumar K.V 	up_write(&ei->i_data_sem);
3711ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3712617ba13bSMingming Cao 	ext4_mark_inode_dirty(handle, inode);
3713ac27a0ecSDave Kleikamp 
3714ac27a0ecSDave Kleikamp 	/*
3715ac27a0ecSDave Kleikamp 	 * In a multi-transaction truncate, we only make the final transaction
3716ac27a0ecSDave Kleikamp 	 * synchronous
3717ac27a0ecSDave Kleikamp 	 */
3718ac27a0ecSDave Kleikamp 	if (IS_SYNC(inode))
3719ac27a0ecSDave Kleikamp 		handle->h_sync = 1;
3720ac27a0ecSDave Kleikamp out_stop:
3721ac27a0ecSDave Kleikamp 	/*
3722ac27a0ecSDave Kleikamp 	 * If this was a simple ftruncate(), and the file will remain alive
3723ac27a0ecSDave Kleikamp 	 * then we need to clear up the orphan record which we created above.
3724ac27a0ecSDave Kleikamp 	 * However, if this was a real unlink then we were called by
3725617ba13bSMingming Cao 	 * ext4_delete_inode(), and we allow that function to clean up the
3726ac27a0ecSDave Kleikamp 	 * orphan info for us.
3727ac27a0ecSDave Kleikamp 	 */
3728ac27a0ecSDave Kleikamp 	if (inode->i_nlink)
3729617ba13bSMingming Cao 		ext4_orphan_del(handle, inode);
3730ac27a0ecSDave Kleikamp 
3731617ba13bSMingming Cao 	ext4_journal_stop(handle);
3732ac27a0ecSDave Kleikamp }
3733ac27a0ecSDave Kleikamp 
3734617ba13bSMingming Cao static ext4_fsblk_t ext4_get_inode_block(struct super_block *sb,
3735617ba13bSMingming Cao 		unsigned long ino, struct ext4_iloc *iloc)
3736ac27a0ecSDave Kleikamp {
3737fd2d4291SAvantika Mathur 	ext4_group_t block_group;
3738ac27a0ecSDave Kleikamp 	unsigned long offset;
3739617ba13bSMingming Cao 	ext4_fsblk_t block;
3740617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
3741ac27a0ecSDave Kleikamp 
3742617ba13bSMingming Cao 	if (!ext4_valid_inum(sb, ino)) {
3743ac27a0ecSDave Kleikamp 		/*
3744ac27a0ecSDave Kleikamp 		 * This error is already checked for in namei.c unless we are
3745ac27a0ecSDave Kleikamp 		 * looking at an NFS filehandle, in which case no error
3746ac27a0ecSDave Kleikamp 		 * report is needed
3747ac27a0ecSDave Kleikamp 		 */
3748ac27a0ecSDave Kleikamp 		return 0;
3749ac27a0ecSDave Kleikamp 	}
3750ac27a0ecSDave Kleikamp 
3751617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
3752c0a4ef38SAkinobu Mita 	gdp = ext4_get_group_desc(sb, block_group, NULL);
3753c0a4ef38SAkinobu Mita 	if (!gdp)
3754ac27a0ecSDave Kleikamp 		return 0;
3755ac27a0ecSDave Kleikamp 
3756ac27a0ecSDave Kleikamp 	/*
3757ac27a0ecSDave Kleikamp 	 * Figure out the offset within the block group inode table
3758ac27a0ecSDave Kleikamp 	 */
3759617ba13bSMingming Cao 	offset = ((ino - 1) % EXT4_INODES_PER_GROUP(sb)) *
3760617ba13bSMingming Cao 		EXT4_INODE_SIZE(sb);
37618fadc143SAlexandre Ratchov 	block = ext4_inode_table(sb, gdp) +
37628fadc143SAlexandre Ratchov 		(offset >> EXT4_BLOCK_SIZE_BITS(sb));
3763ac27a0ecSDave Kleikamp 
3764ac27a0ecSDave Kleikamp 	iloc->block_group = block_group;
3765617ba13bSMingming Cao 	iloc->offset = offset & (EXT4_BLOCK_SIZE(sb) - 1);
3766ac27a0ecSDave Kleikamp 	return block;
3767ac27a0ecSDave Kleikamp }
3768ac27a0ecSDave Kleikamp 
3769ac27a0ecSDave Kleikamp /*
3770617ba13bSMingming Cao  * ext4_get_inode_loc returns with an extra refcount against the inode's
3771ac27a0ecSDave Kleikamp  * underlying buffer_head on success. If 'in_mem' is true, we have all
3772ac27a0ecSDave Kleikamp  * data in memory that is needed to recreate the on-disk version of this
3773ac27a0ecSDave Kleikamp  * inode.
3774ac27a0ecSDave Kleikamp  */
3775617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode,
3776617ba13bSMingming Cao 				struct ext4_iloc *iloc, int in_mem)
3777ac27a0ecSDave Kleikamp {
3778617ba13bSMingming Cao 	ext4_fsblk_t block;
3779ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3780ac27a0ecSDave Kleikamp 
3781617ba13bSMingming Cao 	block = ext4_get_inode_block(inode->i_sb, inode->i_ino, iloc);
3782ac27a0ecSDave Kleikamp 	if (!block)
3783ac27a0ecSDave Kleikamp 		return -EIO;
3784ac27a0ecSDave Kleikamp 
3785ac27a0ecSDave Kleikamp 	bh = sb_getblk(inode->i_sb, block);
3786ac27a0ecSDave Kleikamp 	if (!bh) {
3787617ba13bSMingming Cao 		ext4_error (inode->i_sb, "ext4_get_inode_loc",
3788ac27a0ecSDave Kleikamp 				"unable to read inode block - "
37892ae02107SMingming Cao 				"inode=%lu, block=%llu",
3790ac27a0ecSDave Kleikamp 				 inode->i_ino, block);
3791ac27a0ecSDave Kleikamp 		return -EIO;
3792ac27a0ecSDave Kleikamp 	}
3793ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(bh)) {
3794ac27a0ecSDave Kleikamp 		lock_buffer(bh);
37959c83a923SHidehiro Kawai 
37969c83a923SHidehiro Kawai 		/*
37979c83a923SHidehiro Kawai 		 * If the buffer has the write error flag, we have failed
37989c83a923SHidehiro Kawai 		 * to write out another inode in the same block.  In this
37999c83a923SHidehiro Kawai 		 * case, we don't have to read the block because we may
38009c83a923SHidehiro Kawai 		 * read the old inode data successfully.
38019c83a923SHidehiro Kawai 		 */
38029c83a923SHidehiro Kawai 		if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
38039c83a923SHidehiro Kawai 			set_buffer_uptodate(bh);
38049c83a923SHidehiro Kawai 
3805ac27a0ecSDave Kleikamp 		if (buffer_uptodate(bh)) {
3806ac27a0ecSDave Kleikamp 			/* someone brought it uptodate while we waited */
3807ac27a0ecSDave Kleikamp 			unlock_buffer(bh);
3808ac27a0ecSDave Kleikamp 			goto has_buffer;
3809ac27a0ecSDave Kleikamp 		}
3810ac27a0ecSDave Kleikamp 
3811ac27a0ecSDave Kleikamp 		/*
3812ac27a0ecSDave Kleikamp 		 * If we have all information of the inode in memory and this
3813ac27a0ecSDave Kleikamp 		 * is the only valid inode in the block, we need not read the
3814ac27a0ecSDave Kleikamp 		 * block.
3815ac27a0ecSDave Kleikamp 		 */
3816ac27a0ecSDave Kleikamp 		if (in_mem) {
3817ac27a0ecSDave Kleikamp 			struct buffer_head *bitmap_bh;
3818617ba13bSMingming Cao 			struct ext4_group_desc *desc;
3819ac27a0ecSDave Kleikamp 			int inodes_per_buffer;
3820ac27a0ecSDave Kleikamp 			int inode_offset, i;
3821fd2d4291SAvantika Mathur 			ext4_group_t block_group;
3822ac27a0ecSDave Kleikamp 			int start;
3823ac27a0ecSDave Kleikamp 
3824ac27a0ecSDave Kleikamp 			block_group = (inode->i_ino - 1) /
3825617ba13bSMingming Cao 					EXT4_INODES_PER_GROUP(inode->i_sb);
3826ac27a0ecSDave Kleikamp 			inodes_per_buffer = bh->b_size /
3827617ba13bSMingming Cao 				EXT4_INODE_SIZE(inode->i_sb);
3828ac27a0ecSDave Kleikamp 			inode_offset = ((inode->i_ino - 1) %
3829617ba13bSMingming Cao 					EXT4_INODES_PER_GROUP(inode->i_sb));
3830ac27a0ecSDave Kleikamp 			start = inode_offset & ~(inodes_per_buffer - 1);
3831ac27a0ecSDave Kleikamp 
3832ac27a0ecSDave Kleikamp 			/* Is the inode bitmap in cache? */
3833617ba13bSMingming Cao 			desc = ext4_get_group_desc(inode->i_sb,
3834ac27a0ecSDave Kleikamp 						block_group, NULL);
3835ac27a0ecSDave Kleikamp 			if (!desc)
3836ac27a0ecSDave Kleikamp 				goto make_io;
3837ac27a0ecSDave Kleikamp 
3838ac27a0ecSDave Kleikamp 			bitmap_bh = sb_getblk(inode->i_sb,
38398fadc143SAlexandre Ratchov 				ext4_inode_bitmap(inode->i_sb, desc));
3840ac27a0ecSDave Kleikamp 			if (!bitmap_bh)
3841ac27a0ecSDave Kleikamp 				goto make_io;
3842ac27a0ecSDave Kleikamp 
3843ac27a0ecSDave Kleikamp 			/*
3844ac27a0ecSDave Kleikamp 			 * If the inode bitmap isn't in cache then the
3845ac27a0ecSDave Kleikamp 			 * optimisation may end up performing two reads instead
3846ac27a0ecSDave Kleikamp 			 * of one, so skip it.
3847ac27a0ecSDave Kleikamp 			 */
3848ac27a0ecSDave Kleikamp 			if (!buffer_uptodate(bitmap_bh)) {
3849ac27a0ecSDave Kleikamp 				brelse(bitmap_bh);
3850ac27a0ecSDave Kleikamp 				goto make_io;
3851ac27a0ecSDave Kleikamp 			}
3852ac27a0ecSDave Kleikamp 			for (i = start; i < start + inodes_per_buffer; i++) {
3853ac27a0ecSDave Kleikamp 				if (i == inode_offset)
3854ac27a0ecSDave Kleikamp 					continue;
3855617ba13bSMingming Cao 				if (ext4_test_bit(i, bitmap_bh->b_data))
3856ac27a0ecSDave Kleikamp 					break;
3857ac27a0ecSDave Kleikamp 			}
3858ac27a0ecSDave Kleikamp 			brelse(bitmap_bh);
3859ac27a0ecSDave Kleikamp 			if (i == start + inodes_per_buffer) {
3860ac27a0ecSDave Kleikamp 				/* all other inodes are free, so skip I/O */
3861ac27a0ecSDave Kleikamp 				memset(bh->b_data, 0, bh->b_size);
3862ac27a0ecSDave Kleikamp 				set_buffer_uptodate(bh);
3863ac27a0ecSDave Kleikamp 				unlock_buffer(bh);
3864ac27a0ecSDave Kleikamp 				goto has_buffer;
3865ac27a0ecSDave Kleikamp 			}
3866ac27a0ecSDave Kleikamp 		}
3867ac27a0ecSDave Kleikamp 
3868ac27a0ecSDave Kleikamp make_io:
3869ac27a0ecSDave Kleikamp 		/*
3870ac27a0ecSDave Kleikamp 		 * There are other valid inodes in the buffer, this inode
3871ac27a0ecSDave Kleikamp 		 * has in-inode xattrs, or we don't have this inode in memory.
3872ac27a0ecSDave Kleikamp 		 * Read the block from disk.
3873ac27a0ecSDave Kleikamp 		 */
3874ac27a0ecSDave Kleikamp 		get_bh(bh);
3875ac27a0ecSDave Kleikamp 		bh->b_end_io = end_buffer_read_sync;
3876ac27a0ecSDave Kleikamp 		submit_bh(READ_META, bh);
3877ac27a0ecSDave Kleikamp 		wait_on_buffer(bh);
3878ac27a0ecSDave Kleikamp 		if (!buffer_uptodate(bh)) {
3879617ba13bSMingming Cao 			ext4_error(inode->i_sb, "ext4_get_inode_loc",
3880ac27a0ecSDave Kleikamp 					"unable to read inode block - "
38812ae02107SMingming Cao 					"inode=%lu, block=%llu",
3882ac27a0ecSDave Kleikamp 					inode->i_ino, block);
3883ac27a0ecSDave Kleikamp 			brelse(bh);
3884ac27a0ecSDave Kleikamp 			return -EIO;
3885ac27a0ecSDave Kleikamp 		}
3886ac27a0ecSDave Kleikamp 	}
3887ac27a0ecSDave Kleikamp has_buffer:
3888ac27a0ecSDave Kleikamp 	iloc->bh = bh;
3889ac27a0ecSDave Kleikamp 	return 0;
3890ac27a0ecSDave Kleikamp }
3891ac27a0ecSDave Kleikamp 
3892617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
3893ac27a0ecSDave Kleikamp {
3894ac27a0ecSDave Kleikamp 	/* We have all inode data except xattrs in memory here. */
3895617ba13bSMingming Cao 	return __ext4_get_inode_loc(inode, iloc,
3896617ba13bSMingming Cao 		!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
3897ac27a0ecSDave Kleikamp }
3898ac27a0ecSDave Kleikamp 
3899617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode)
3900ac27a0ecSDave Kleikamp {
3901617ba13bSMingming Cao 	unsigned int flags = EXT4_I(inode)->i_flags;
3902ac27a0ecSDave Kleikamp 
3903ac27a0ecSDave Kleikamp 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
3904617ba13bSMingming Cao 	if (flags & EXT4_SYNC_FL)
3905ac27a0ecSDave Kleikamp 		inode->i_flags |= S_SYNC;
3906617ba13bSMingming Cao 	if (flags & EXT4_APPEND_FL)
3907ac27a0ecSDave Kleikamp 		inode->i_flags |= S_APPEND;
3908617ba13bSMingming Cao 	if (flags & EXT4_IMMUTABLE_FL)
3909ac27a0ecSDave Kleikamp 		inode->i_flags |= S_IMMUTABLE;
3910617ba13bSMingming Cao 	if (flags & EXT4_NOATIME_FL)
3911ac27a0ecSDave Kleikamp 		inode->i_flags |= S_NOATIME;
3912617ba13bSMingming Cao 	if (flags & EXT4_DIRSYNC_FL)
3913ac27a0ecSDave Kleikamp 		inode->i_flags |= S_DIRSYNC;
3914ac27a0ecSDave Kleikamp }
3915ac27a0ecSDave Kleikamp 
3916ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
3917ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei)
3918ff9ddf7eSJan Kara {
3919ff9ddf7eSJan Kara 	unsigned int flags = ei->vfs_inode.i_flags;
3920ff9ddf7eSJan Kara 
3921ff9ddf7eSJan Kara 	ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
3922ff9ddf7eSJan Kara 			EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
3923ff9ddf7eSJan Kara 	if (flags & S_SYNC)
3924ff9ddf7eSJan Kara 		ei->i_flags |= EXT4_SYNC_FL;
3925ff9ddf7eSJan Kara 	if (flags & S_APPEND)
3926ff9ddf7eSJan Kara 		ei->i_flags |= EXT4_APPEND_FL;
3927ff9ddf7eSJan Kara 	if (flags & S_IMMUTABLE)
3928ff9ddf7eSJan Kara 		ei->i_flags |= EXT4_IMMUTABLE_FL;
3929ff9ddf7eSJan Kara 	if (flags & S_NOATIME)
3930ff9ddf7eSJan Kara 		ei->i_flags |= EXT4_NOATIME_FL;
3931ff9ddf7eSJan Kara 	if (flags & S_DIRSYNC)
3932ff9ddf7eSJan Kara 		ei->i_flags |= EXT4_DIRSYNC_FL;
3933ff9ddf7eSJan Kara }
39340fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
39350fc1b451SAneesh Kumar K.V 					struct ext4_inode_info *ei)
39360fc1b451SAneesh Kumar K.V {
39370fc1b451SAneesh Kumar K.V 	blkcnt_t i_blocks ;
39388180a562SAneesh Kumar K.V 	struct inode *inode = &(ei->vfs_inode);
39398180a562SAneesh Kumar K.V 	struct super_block *sb = inode->i_sb;
39400fc1b451SAneesh Kumar K.V 
39410fc1b451SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
39420fc1b451SAneesh Kumar K.V 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
39430fc1b451SAneesh Kumar K.V 		/* we are using combined 48 bit field */
39440fc1b451SAneesh Kumar K.V 		i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
39450fc1b451SAneesh Kumar K.V 					le32_to_cpu(raw_inode->i_blocks_lo);
39468180a562SAneesh Kumar K.V 		if (ei->i_flags & EXT4_HUGE_FILE_FL) {
39478180a562SAneesh Kumar K.V 			/* i_blocks represent file system block size */
39488180a562SAneesh Kumar K.V 			return i_blocks  << (inode->i_blkbits - 9);
39498180a562SAneesh Kumar K.V 		} else {
39500fc1b451SAneesh Kumar K.V 			return i_blocks;
39518180a562SAneesh Kumar K.V 		}
39520fc1b451SAneesh Kumar K.V 	} else {
39530fc1b451SAneesh Kumar K.V 		return le32_to_cpu(raw_inode->i_blocks_lo);
39540fc1b451SAneesh Kumar K.V 	}
39550fc1b451SAneesh Kumar K.V }
3956ff9ddf7eSJan Kara 
39571d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
3958ac27a0ecSDave Kleikamp {
3959617ba13bSMingming Cao 	struct ext4_iloc iloc;
3960617ba13bSMingming Cao 	struct ext4_inode *raw_inode;
39611d1fe1eeSDavid Howells 	struct ext4_inode_info *ei;
3962ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
39631d1fe1eeSDavid Howells 	struct inode *inode;
39641d1fe1eeSDavid Howells 	long ret;
3965ac27a0ecSDave Kleikamp 	int block;
3966ac27a0ecSDave Kleikamp 
39671d1fe1eeSDavid Howells 	inode = iget_locked(sb, ino);
39681d1fe1eeSDavid Howells 	if (!inode)
39691d1fe1eeSDavid Howells 		return ERR_PTR(-ENOMEM);
39701d1fe1eeSDavid Howells 	if (!(inode->i_state & I_NEW))
39711d1fe1eeSDavid Howells 		return inode;
39721d1fe1eeSDavid Howells 
39731d1fe1eeSDavid Howells 	ei = EXT4_I(inode);
3974617ba13bSMingming Cao #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
3975617ba13bSMingming Cao 	ei->i_acl = EXT4_ACL_NOT_CACHED;
3976617ba13bSMingming Cao 	ei->i_default_acl = EXT4_ACL_NOT_CACHED;
3977ac27a0ecSDave Kleikamp #endif
3978ac27a0ecSDave Kleikamp 	ei->i_block_alloc_info = NULL;
3979ac27a0ecSDave Kleikamp 
39801d1fe1eeSDavid Howells 	ret = __ext4_get_inode_loc(inode, &iloc, 0);
39811d1fe1eeSDavid Howells 	if (ret < 0)
3982ac27a0ecSDave Kleikamp 		goto bad_inode;
3983ac27a0ecSDave Kleikamp 	bh = iloc.bh;
3984617ba13bSMingming Cao 	raw_inode = ext4_raw_inode(&iloc);
3985ac27a0ecSDave Kleikamp 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
3986ac27a0ecSDave Kleikamp 	inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
3987ac27a0ecSDave Kleikamp 	inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
3988ac27a0ecSDave Kleikamp 	if (!(test_opt(inode->i_sb, NO_UID32))) {
3989ac27a0ecSDave Kleikamp 		inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
3990ac27a0ecSDave Kleikamp 		inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
3991ac27a0ecSDave Kleikamp 	}
3992ac27a0ecSDave Kleikamp 	inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
3993ac27a0ecSDave Kleikamp 
3994ac27a0ecSDave Kleikamp 	ei->i_state = 0;
3995ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
3996ac27a0ecSDave Kleikamp 	ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
3997ac27a0ecSDave Kleikamp 	/* We now have enough fields to check if the inode was active or not.
3998ac27a0ecSDave Kleikamp 	 * This is needed because nfsd might try to access dead inodes
3999ac27a0ecSDave Kleikamp 	 * the test is that same one that e2fsck uses
4000ac27a0ecSDave Kleikamp 	 * NeilBrown 1999oct15
4001ac27a0ecSDave Kleikamp 	 */
4002ac27a0ecSDave Kleikamp 	if (inode->i_nlink == 0) {
4003ac27a0ecSDave Kleikamp 		if (inode->i_mode == 0 ||
4004617ba13bSMingming Cao 		    !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
4005ac27a0ecSDave Kleikamp 			/* this inode is deleted */
4006ac27a0ecSDave Kleikamp 			brelse(bh);
40071d1fe1eeSDavid Howells 			ret = -ESTALE;
4008ac27a0ecSDave Kleikamp 			goto bad_inode;
4009ac27a0ecSDave Kleikamp 		}
4010ac27a0ecSDave Kleikamp 		/* The only unlinked inodes we let through here have
4011ac27a0ecSDave Kleikamp 		 * valid i_mode and are being read by the orphan
4012ac27a0ecSDave Kleikamp 		 * recovery code: that's fine, we're about to complete
4013ac27a0ecSDave Kleikamp 		 * the process of deleting those. */
4014ac27a0ecSDave Kleikamp 	}
4015ac27a0ecSDave Kleikamp 	ei->i_flags = le32_to_cpu(raw_inode->i_flags);
40160fc1b451SAneesh Kumar K.V 	inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
40177973c0c1SAneesh Kumar K.V 	ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
40189b8f1f01SMingming Cao 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4019a48380f7SAneesh Kumar K.V 	    cpu_to_le32(EXT4_OS_HURD)) {
4020a1ddeb7eSBadari Pulavarty 		ei->i_file_acl |=
4021a1ddeb7eSBadari Pulavarty 			((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
4022ac27a0ecSDave Kleikamp 	}
4023a48380f7SAneesh Kumar K.V 	inode->i_size = ext4_isize(raw_inode);
4024ac27a0ecSDave Kleikamp 	ei->i_disksize = inode->i_size;
4025ac27a0ecSDave Kleikamp 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4026ac27a0ecSDave Kleikamp 	ei->i_block_group = iloc.block_group;
4027ac27a0ecSDave Kleikamp 	/*
4028ac27a0ecSDave Kleikamp 	 * NOTE! The in-memory inode i_data array is in little-endian order
4029ac27a0ecSDave Kleikamp 	 * even on big-endian machines: we do NOT byteswap the block numbers!
4030ac27a0ecSDave Kleikamp 	 */
4031617ba13bSMingming Cao 	for (block = 0; block < EXT4_N_BLOCKS; block++)
4032ac27a0ecSDave Kleikamp 		ei->i_data[block] = raw_inode->i_block[block];
4033ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
4034ac27a0ecSDave Kleikamp 
40350040d987SEric Sandeen 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4036ac27a0ecSDave Kleikamp 		ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4037617ba13bSMingming Cao 		if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4038e5d2861fSKirill Korotaev 		    EXT4_INODE_SIZE(inode->i_sb)) {
4039e5d2861fSKirill Korotaev 			brelse(bh);
40401d1fe1eeSDavid Howells 			ret = -EIO;
4041ac27a0ecSDave Kleikamp 			goto bad_inode;
4042e5d2861fSKirill Korotaev 		}
4043ac27a0ecSDave Kleikamp 		if (ei->i_extra_isize == 0) {
4044ac27a0ecSDave Kleikamp 			/* The extra space is currently unused. Use it. */
4045617ba13bSMingming Cao 			ei->i_extra_isize = sizeof(struct ext4_inode) -
4046617ba13bSMingming Cao 					    EXT4_GOOD_OLD_INODE_SIZE;
4047ac27a0ecSDave Kleikamp 		} else {
4048ac27a0ecSDave Kleikamp 			__le32 *magic = (void *)raw_inode +
4049617ba13bSMingming Cao 					EXT4_GOOD_OLD_INODE_SIZE +
4050ac27a0ecSDave Kleikamp 					ei->i_extra_isize;
4051617ba13bSMingming Cao 			if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
4052617ba13bSMingming Cao 				 ei->i_state |= EXT4_STATE_XATTR;
4053ac27a0ecSDave Kleikamp 		}
4054ac27a0ecSDave Kleikamp 	} else
4055ac27a0ecSDave Kleikamp 		ei->i_extra_isize = 0;
4056ac27a0ecSDave Kleikamp 
4057ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4058ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4059ef7f3835SKalpak Shah 	EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4060ef7f3835SKalpak Shah 	EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4061ef7f3835SKalpak Shah 
406225ec56b5SJean Noel Cordenner 	inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
406325ec56b5SJean Noel Cordenner 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
406425ec56b5SJean Noel Cordenner 		if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
406525ec56b5SJean Noel Cordenner 			inode->i_version |=
406625ec56b5SJean Noel Cordenner 			(__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
406725ec56b5SJean Noel Cordenner 	}
406825ec56b5SJean Noel Cordenner 
4069ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode)) {
4070617ba13bSMingming Cao 		inode->i_op = &ext4_file_inode_operations;
4071617ba13bSMingming Cao 		inode->i_fop = &ext4_file_operations;
4072617ba13bSMingming Cao 		ext4_set_aops(inode);
4073ac27a0ecSDave Kleikamp 	} else if (S_ISDIR(inode->i_mode)) {
4074617ba13bSMingming Cao 		inode->i_op = &ext4_dir_inode_operations;
4075617ba13bSMingming Cao 		inode->i_fop = &ext4_dir_operations;
4076ac27a0ecSDave Kleikamp 	} else if (S_ISLNK(inode->i_mode)) {
4077617ba13bSMingming Cao 		if (ext4_inode_is_fast_symlink(inode))
4078617ba13bSMingming Cao 			inode->i_op = &ext4_fast_symlink_inode_operations;
4079ac27a0ecSDave Kleikamp 		else {
4080617ba13bSMingming Cao 			inode->i_op = &ext4_symlink_inode_operations;
4081617ba13bSMingming Cao 			ext4_set_aops(inode);
4082ac27a0ecSDave Kleikamp 		}
4083ac27a0ecSDave Kleikamp 	} else {
4084617ba13bSMingming Cao 		inode->i_op = &ext4_special_inode_operations;
4085ac27a0ecSDave Kleikamp 		if (raw_inode->i_block[0])
4086ac27a0ecSDave Kleikamp 			init_special_inode(inode, inode->i_mode,
4087ac27a0ecSDave Kleikamp 			   old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4088ac27a0ecSDave Kleikamp 		else
4089ac27a0ecSDave Kleikamp 			init_special_inode(inode, inode->i_mode,
4090ac27a0ecSDave Kleikamp 			   new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4091ac27a0ecSDave Kleikamp 	}
4092ac27a0ecSDave Kleikamp 	brelse(iloc.bh);
4093617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
40941d1fe1eeSDavid Howells 	unlock_new_inode(inode);
40951d1fe1eeSDavid Howells 	return inode;
4096ac27a0ecSDave Kleikamp 
4097ac27a0ecSDave Kleikamp bad_inode:
40981d1fe1eeSDavid Howells 	iget_failed(inode);
40991d1fe1eeSDavid Howells 	return ERR_PTR(ret);
4100ac27a0ecSDave Kleikamp }
4101ac27a0ecSDave Kleikamp 
41020fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle,
41030fc1b451SAneesh Kumar K.V 				struct ext4_inode *raw_inode,
41040fc1b451SAneesh Kumar K.V 				struct ext4_inode_info *ei)
41050fc1b451SAneesh Kumar K.V {
41060fc1b451SAneesh Kumar K.V 	struct inode *inode = &(ei->vfs_inode);
41070fc1b451SAneesh Kumar K.V 	u64 i_blocks = inode->i_blocks;
41080fc1b451SAneesh Kumar K.V 	struct super_block *sb = inode->i_sb;
41090fc1b451SAneesh Kumar K.V 	int err = 0;
41100fc1b451SAneesh Kumar K.V 
41110fc1b451SAneesh Kumar K.V 	if (i_blocks <= ~0U) {
41120fc1b451SAneesh Kumar K.V 		/*
41130fc1b451SAneesh Kumar K.V 		 * i_blocks can be represnted in a 32 bit variable
41140fc1b451SAneesh Kumar K.V 		 * as multiple of 512 bytes
41150fc1b451SAneesh Kumar K.V 		 */
41168180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
41170fc1b451SAneesh Kumar K.V 		raw_inode->i_blocks_high = 0;
41188180a562SAneesh Kumar K.V 		ei->i_flags &= ~EXT4_HUGE_FILE_FL;
41190fc1b451SAneesh Kumar K.V 	} else if (i_blocks <= 0xffffffffffffULL) {
41200fc1b451SAneesh Kumar K.V 		/*
41210fc1b451SAneesh Kumar K.V 		 * i_blocks can be represented in a 48 bit variable
41220fc1b451SAneesh Kumar K.V 		 * as multiple of 512 bytes
41230fc1b451SAneesh Kumar K.V 		 */
41240fc1b451SAneesh Kumar K.V 		err = ext4_update_rocompat_feature(handle, sb,
41250fc1b451SAneesh Kumar K.V 					    EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
41260fc1b451SAneesh Kumar K.V 		if (err)
41270fc1b451SAneesh Kumar K.V 			goto  err_out;
41280fc1b451SAneesh Kumar K.V 		/* i_block is stored in the split  48 bit fields */
41298180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
41300fc1b451SAneesh Kumar K.V 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
41318180a562SAneesh Kumar K.V 		ei->i_flags &= ~EXT4_HUGE_FILE_FL;
41320fc1b451SAneesh Kumar K.V 	} else {
41338180a562SAneesh Kumar K.V 		/*
41348180a562SAneesh Kumar K.V 		 * i_blocks should be represented in a 48 bit variable
41358180a562SAneesh Kumar K.V 		 * as multiple of  file system block size
41368180a562SAneesh Kumar K.V 		 */
41378180a562SAneesh Kumar K.V 		err = ext4_update_rocompat_feature(handle, sb,
41388180a562SAneesh Kumar K.V 					    EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
41398180a562SAneesh Kumar K.V 		if (err)
41408180a562SAneesh Kumar K.V 			goto  err_out;
41418180a562SAneesh Kumar K.V 		ei->i_flags |= EXT4_HUGE_FILE_FL;
41428180a562SAneesh Kumar K.V 		/* i_block is stored in file system block size */
41438180a562SAneesh Kumar K.V 		i_blocks = i_blocks >> (inode->i_blkbits - 9);
41448180a562SAneesh Kumar K.V 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
41458180a562SAneesh Kumar K.V 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
41460fc1b451SAneesh Kumar K.V 	}
41470fc1b451SAneesh Kumar K.V err_out:
41480fc1b451SAneesh Kumar K.V 	return err;
41490fc1b451SAneesh Kumar K.V }
41500fc1b451SAneesh Kumar K.V 
4151ac27a0ecSDave Kleikamp /*
4152ac27a0ecSDave Kleikamp  * Post the struct inode info into an on-disk inode location in the
4153ac27a0ecSDave Kleikamp  * buffer-cache.  This gobbles the caller's reference to the
4154ac27a0ecSDave Kleikamp  * buffer_head in the inode location struct.
4155ac27a0ecSDave Kleikamp  *
4156ac27a0ecSDave Kleikamp  * The caller must have write access to iloc->bh.
4157ac27a0ecSDave Kleikamp  */
4158617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle,
4159ac27a0ecSDave Kleikamp 				struct inode *inode,
4160617ba13bSMingming Cao 				struct ext4_iloc *iloc)
4161ac27a0ecSDave Kleikamp {
4162617ba13bSMingming Cao 	struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4163617ba13bSMingming Cao 	struct ext4_inode_info *ei = EXT4_I(inode);
4164ac27a0ecSDave Kleikamp 	struct buffer_head *bh = iloc->bh;
4165ac27a0ecSDave Kleikamp 	int err = 0, rc, block;
4166ac27a0ecSDave Kleikamp 
4167ac27a0ecSDave Kleikamp 	/* For fields not not tracking in the in-memory inode,
4168ac27a0ecSDave Kleikamp 	 * initialise them to zero for new inodes. */
4169617ba13bSMingming Cao 	if (ei->i_state & EXT4_STATE_NEW)
4170617ba13bSMingming Cao 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
4171ac27a0ecSDave Kleikamp 
4172ff9ddf7eSJan Kara 	ext4_get_inode_flags(ei);
4173ac27a0ecSDave Kleikamp 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
4174ac27a0ecSDave Kleikamp 	if (!(test_opt(inode->i_sb, NO_UID32))) {
4175ac27a0ecSDave Kleikamp 		raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4176ac27a0ecSDave Kleikamp 		raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4177ac27a0ecSDave Kleikamp /*
4178ac27a0ecSDave Kleikamp  * Fix up interoperability with old kernels. Otherwise, old inodes get
4179ac27a0ecSDave Kleikamp  * re-used with the upper 16 bits of the uid/gid intact
4180ac27a0ecSDave Kleikamp  */
4181ac27a0ecSDave Kleikamp 		if (!ei->i_dtime) {
4182ac27a0ecSDave Kleikamp 			raw_inode->i_uid_high =
4183ac27a0ecSDave Kleikamp 				cpu_to_le16(high_16_bits(inode->i_uid));
4184ac27a0ecSDave Kleikamp 			raw_inode->i_gid_high =
4185ac27a0ecSDave Kleikamp 				cpu_to_le16(high_16_bits(inode->i_gid));
4186ac27a0ecSDave Kleikamp 		} else {
4187ac27a0ecSDave Kleikamp 			raw_inode->i_uid_high = 0;
4188ac27a0ecSDave Kleikamp 			raw_inode->i_gid_high = 0;
4189ac27a0ecSDave Kleikamp 		}
4190ac27a0ecSDave Kleikamp 	} else {
4191ac27a0ecSDave Kleikamp 		raw_inode->i_uid_low =
4192ac27a0ecSDave Kleikamp 			cpu_to_le16(fs_high2lowuid(inode->i_uid));
4193ac27a0ecSDave Kleikamp 		raw_inode->i_gid_low =
4194ac27a0ecSDave Kleikamp 			cpu_to_le16(fs_high2lowgid(inode->i_gid));
4195ac27a0ecSDave Kleikamp 		raw_inode->i_uid_high = 0;
4196ac27a0ecSDave Kleikamp 		raw_inode->i_gid_high = 0;
4197ac27a0ecSDave Kleikamp 	}
4198ac27a0ecSDave Kleikamp 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
4199ef7f3835SKalpak Shah 
4200ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4201ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4202ef7f3835SKalpak Shah 	EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4203ef7f3835SKalpak Shah 	EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4204ef7f3835SKalpak Shah 
42050fc1b451SAneesh Kumar K.V 	if (ext4_inode_blocks_set(handle, raw_inode, ei))
42060fc1b451SAneesh Kumar K.V 		goto out_brelse;
4207ac27a0ecSDave Kleikamp 	raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
4208267e4db9SAneesh Kumar K.V 	/* clear the migrate flag in the raw_inode */
4209267e4db9SAneesh Kumar K.V 	raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
42109b8f1f01SMingming Cao 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
42119b8f1f01SMingming Cao 	    cpu_to_le32(EXT4_OS_HURD))
4212a1ddeb7eSBadari Pulavarty 		raw_inode->i_file_acl_high =
4213a1ddeb7eSBadari Pulavarty 			cpu_to_le16(ei->i_file_acl >> 32);
42147973c0c1SAneesh Kumar K.V 	raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
4215a48380f7SAneesh Kumar K.V 	ext4_isize_set(raw_inode, ei->i_disksize);
4216ac27a0ecSDave Kleikamp 	if (ei->i_disksize > 0x7fffffffULL) {
4217ac27a0ecSDave Kleikamp 		struct super_block *sb = inode->i_sb;
4218617ba13bSMingming Cao 		if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4219617ba13bSMingming Cao 				EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4220617ba13bSMingming Cao 				EXT4_SB(sb)->s_es->s_rev_level ==
4221617ba13bSMingming Cao 				cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4222ac27a0ecSDave Kleikamp 			/* If this is the first large file
4223ac27a0ecSDave Kleikamp 			 * created, add a flag to the superblock.
4224ac27a0ecSDave Kleikamp 			 */
4225617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle,
4226617ba13bSMingming Cao 					EXT4_SB(sb)->s_sbh);
4227ac27a0ecSDave Kleikamp 			if (err)
4228ac27a0ecSDave Kleikamp 				goto out_brelse;
4229617ba13bSMingming Cao 			ext4_update_dynamic_rev(sb);
4230617ba13bSMingming Cao 			EXT4_SET_RO_COMPAT_FEATURE(sb,
4231617ba13bSMingming Cao 					EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
4232ac27a0ecSDave Kleikamp 			sb->s_dirt = 1;
4233ac27a0ecSDave Kleikamp 			handle->h_sync = 1;
4234617ba13bSMingming Cao 			err = ext4_journal_dirty_metadata(handle,
4235617ba13bSMingming Cao 					EXT4_SB(sb)->s_sbh);
4236ac27a0ecSDave Kleikamp 		}
4237ac27a0ecSDave Kleikamp 	}
4238ac27a0ecSDave Kleikamp 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4239ac27a0ecSDave Kleikamp 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4240ac27a0ecSDave Kleikamp 		if (old_valid_dev(inode->i_rdev)) {
4241ac27a0ecSDave Kleikamp 			raw_inode->i_block[0] =
4242ac27a0ecSDave Kleikamp 				cpu_to_le32(old_encode_dev(inode->i_rdev));
4243ac27a0ecSDave Kleikamp 			raw_inode->i_block[1] = 0;
4244ac27a0ecSDave Kleikamp 		} else {
4245ac27a0ecSDave Kleikamp 			raw_inode->i_block[0] = 0;
4246ac27a0ecSDave Kleikamp 			raw_inode->i_block[1] =
4247ac27a0ecSDave Kleikamp 				cpu_to_le32(new_encode_dev(inode->i_rdev));
4248ac27a0ecSDave Kleikamp 			raw_inode->i_block[2] = 0;
4249ac27a0ecSDave Kleikamp 		}
4250617ba13bSMingming Cao 	} else for (block = 0; block < EXT4_N_BLOCKS; block++)
4251ac27a0ecSDave Kleikamp 		raw_inode->i_block[block] = ei->i_data[block];
4252ac27a0ecSDave Kleikamp 
425325ec56b5SJean Noel Cordenner 	raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
425425ec56b5SJean Noel Cordenner 	if (ei->i_extra_isize) {
425525ec56b5SJean Noel Cordenner 		if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
425625ec56b5SJean Noel Cordenner 			raw_inode->i_version_hi =
425725ec56b5SJean Noel Cordenner 			cpu_to_le32(inode->i_version >> 32);
4258ac27a0ecSDave Kleikamp 		raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
425925ec56b5SJean Noel Cordenner 	}
426025ec56b5SJean Noel Cordenner 
4261ac27a0ecSDave Kleikamp 
4262617ba13bSMingming Cao 	BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
4263617ba13bSMingming Cao 	rc = ext4_journal_dirty_metadata(handle, bh);
4264ac27a0ecSDave Kleikamp 	if (!err)
4265ac27a0ecSDave Kleikamp 		err = rc;
4266617ba13bSMingming Cao 	ei->i_state &= ~EXT4_STATE_NEW;
4267ac27a0ecSDave Kleikamp 
4268ac27a0ecSDave Kleikamp out_brelse:
4269ac27a0ecSDave Kleikamp 	brelse(bh);
4270617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4271ac27a0ecSDave Kleikamp 	return err;
4272ac27a0ecSDave Kleikamp }
4273ac27a0ecSDave Kleikamp 
4274ac27a0ecSDave Kleikamp /*
4275617ba13bSMingming Cao  * ext4_write_inode()
4276ac27a0ecSDave Kleikamp  *
4277ac27a0ecSDave Kleikamp  * We are called from a few places:
4278ac27a0ecSDave Kleikamp  *
4279ac27a0ecSDave Kleikamp  * - Within generic_file_write() for O_SYNC files.
4280ac27a0ecSDave Kleikamp  *   Here, there will be no transaction running. We wait for any running
4281ac27a0ecSDave Kleikamp  *   trasnaction to commit.
4282ac27a0ecSDave Kleikamp  *
4283ac27a0ecSDave Kleikamp  * - Within sys_sync(), kupdate and such.
4284ac27a0ecSDave Kleikamp  *   We wait on commit, if tol to.
4285ac27a0ecSDave Kleikamp  *
4286ac27a0ecSDave Kleikamp  * - Within prune_icache() (PF_MEMALLOC == true)
4287ac27a0ecSDave Kleikamp  *   Here we simply return.  We can't afford to block kswapd on the
4288ac27a0ecSDave Kleikamp  *   journal commit.
4289ac27a0ecSDave Kleikamp  *
4290ac27a0ecSDave Kleikamp  * In all cases it is actually safe for us to return without doing anything,
4291ac27a0ecSDave Kleikamp  * because the inode has been copied into a raw inode buffer in
4292617ba13bSMingming Cao  * ext4_mark_inode_dirty().  This is a correctness thing for O_SYNC and for
4293ac27a0ecSDave Kleikamp  * knfsd.
4294ac27a0ecSDave Kleikamp  *
4295ac27a0ecSDave Kleikamp  * Note that we are absolutely dependent upon all inode dirtiers doing the
4296ac27a0ecSDave Kleikamp  * right thing: they *must* call mark_inode_dirty() after dirtying info in
4297ac27a0ecSDave Kleikamp  * which we are interested.
4298ac27a0ecSDave Kleikamp  *
4299ac27a0ecSDave Kleikamp  * It would be a bug for them to not do this.  The code:
4300ac27a0ecSDave Kleikamp  *
4301ac27a0ecSDave Kleikamp  *	mark_inode_dirty(inode)
4302ac27a0ecSDave Kleikamp  *	stuff();
4303ac27a0ecSDave Kleikamp  *	inode->i_size = expr;
4304ac27a0ecSDave Kleikamp  *
4305ac27a0ecSDave Kleikamp  * is in error because a kswapd-driven write_inode() could occur while
4306ac27a0ecSDave Kleikamp  * `stuff()' is running, and the new i_size will be lost.  Plus the inode
4307ac27a0ecSDave Kleikamp  * will no longer be on the superblock's dirty inode list.
4308ac27a0ecSDave Kleikamp  */
4309617ba13bSMingming Cao int ext4_write_inode(struct inode *inode, int wait)
4310ac27a0ecSDave Kleikamp {
4311ac27a0ecSDave Kleikamp 	if (current->flags & PF_MEMALLOC)
4312ac27a0ecSDave Kleikamp 		return 0;
4313ac27a0ecSDave Kleikamp 
4314617ba13bSMingming Cao 	if (ext4_journal_current_handle()) {
4315b38bd33aSMingming Cao 		jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4316ac27a0ecSDave Kleikamp 		dump_stack();
4317ac27a0ecSDave Kleikamp 		return -EIO;
4318ac27a0ecSDave Kleikamp 	}
4319ac27a0ecSDave Kleikamp 
4320ac27a0ecSDave Kleikamp 	if (!wait)
4321ac27a0ecSDave Kleikamp 		return 0;
4322ac27a0ecSDave Kleikamp 
4323617ba13bSMingming Cao 	return ext4_force_commit(inode->i_sb);
4324ac27a0ecSDave Kleikamp }
4325ac27a0ecSDave Kleikamp 
4326ac27a0ecSDave Kleikamp /*
4327617ba13bSMingming Cao  * ext4_setattr()
4328ac27a0ecSDave Kleikamp  *
4329ac27a0ecSDave Kleikamp  * Called from notify_change.
4330ac27a0ecSDave Kleikamp  *
4331ac27a0ecSDave Kleikamp  * We want to trap VFS attempts to truncate the file as soon as
4332ac27a0ecSDave Kleikamp  * possible.  In particular, we want to make sure that when the VFS
4333ac27a0ecSDave Kleikamp  * shrinks i_size, we put the inode on the orphan list and modify
4334ac27a0ecSDave Kleikamp  * i_disksize immediately, so that during the subsequent flushing of
4335ac27a0ecSDave Kleikamp  * dirty pages and freeing of disk blocks, we can guarantee that any
4336ac27a0ecSDave Kleikamp  * commit will leave the blocks being flushed in an unused state on
4337ac27a0ecSDave Kleikamp  * disk.  (On recovery, the inode will get truncated and the blocks will
4338ac27a0ecSDave Kleikamp  * be freed, so we have a strong guarantee that no future commit will
4339ac27a0ecSDave Kleikamp  * leave these blocks visible to the user.)
4340ac27a0ecSDave Kleikamp  *
4341678aaf48SJan Kara  * Another thing we have to assure is that if we are in ordered mode
4342678aaf48SJan Kara  * and inode is still attached to the committing transaction, we must
4343678aaf48SJan Kara  * we start writeout of all the dirty pages which are being truncated.
4344678aaf48SJan Kara  * This way we are sure that all the data written in the previous
4345678aaf48SJan Kara  * transaction are already on disk (truncate waits for pages under
4346678aaf48SJan Kara  * writeback).
4347678aaf48SJan Kara  *
4348678aaf48SJan Kara  * Called with inode->i_mutex down.
4349ac27a0ecSDave Kleikamp  */
4350617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr)
4351ac27a0ecSDave Kleikamp {
4352ac27a0ecSDave Kleikamp 	struct inode *inode = dentry->d_inode;
4353ac27a0ecSDave Kleikamp 	int error, rc = 0;
4354ac27a0ecSDave Kleikamp 	const unsigned int ia_valid = attr->ia_valid;
4355ac27a0ecSDave Kleikamp 
4356ac27a0ecSDave Kleikamp 	error = inode_change_ok(inode, attr);
4357ac27a0ecSDave Kleikamp 	if (error)
4358ac27a0ecSDave Kleikamp 		return error;
4359ac27a0ecSDave Kleikamp 
4360ac27a0ecSDave Kleikamp 	if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4361ac27a0ecSDave Kleikamp 		(ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4362ac27a0ecSDave Kleikamp 		handle_t *handle;
4363ac27a0ecSDave Kleikamp 
4364ac27a0ecSDave Kleikamp 		/* (user+group)*(old+new) structure, inode write (sb,
4365ac27a0ecSDave Kleikamp 		 * inode block, ? - but truncate inode update has it) */
4366617ba13bSMingming Cao 		handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4367617ba13bSMingming Cao 					EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
4368ac27a0ecSDave Kleikamp 		if (IS_ERR(handle)) {
4369ac27a0ecSDave Kleikamp 			error = PTR_ERR(handle);
4370ac27a0ecSDave Kleikamp 			goto err_out;
4371ac27a0ecSDave Kleikamp 		}
4372ac27a0ecSDave Kleikamp 		error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
4373ac27a0ecSDave Kleikamp 		if (error) {
4374617ba13bSMingming Cao 			ext4_journal_stop(handle);
4375ac27a0ecSDave Kleikamp 			return error;
4376ac27a0ecSDave Kleikamp 		}
4377ac27a0ecSDave Kleikamp 		/* Update corresponding info in inode so that everything is in
4378ac27a0ecSDave Kleikamp 		 * one transaction */
4379ac27a0ecSDave Kleikamp 		if (attr->ia_valid & ATTR_UID)
4380ac27a0ecSDave Kleikamp 			inode->i_uid = attr->ia_uid;
4381ac27a0ecSDave Kleikamp 		if (attr->ia_valid & ATTR_GID)
4382ac27a0ecSDave Kleikamp 			inode->i_gid = attr->ia_gid;
4383617ba13bSMingming Cao 		error = ext4_mark_inode_dirty(handle, inode);
4384617ba13bSMingming Cao 		ext4_journal_stop(handle);
4385ac27a0ecSDave Kleikamp 	}
4386ac27a0ecSDave Kleikamp 
4387e2b46574SEric Sandeen 	if (attr->ia_valid & ATTR_SIZE) {
4388e2b46574SEric Sandeen 		if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4389e2b46574SEric Sandeen 			struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4390e2b46574SEric Sandeen 
4391e2b46574SEric Sandeen 			if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4392e2b46574SEric Sandeen 				error = -EFBIG;
4393e2b46574SEric Sandeen 				goto err_out;
4394e2b46574SEric Sandeen 			}
4395e2b46574SEric Sandeen 		}
4396e2b46574SEric Sandeen 	}
4397e2b46574SEric Sandeen 
4398ac27a0ecSDave Kleikamp 	if (S_ISREG(inode->i_mode) &&
4399ac27a0ecSDave Kleikamp 	    attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4400ac27a0ecSDave Kleikamp 		handle_t *handle;
4401ac27a0ecSDave Kleikamp 
4402617ba13bSMingming Cao 		handle = ext4_journal_start(inode, 3);
4403ac27a0ecSDave Kleikamp 		if (IS_ERR(handle)) {
4404ac27a0ecSDave Kleikamp 			error = PTR_ERR(handle);
4405ac27a0ecSDave Kleikamp 			goto err_out;
4406ac27a0ecSDave Kleikamp 		}
4407ac27a0ecSDave Kleikamp 
4408617ba13bSMingming Cao 		error = ext4_orphan_add(handle, inode);
4409617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = attr->ia_size;
4410617ba13bSMingming Cao 		rc = ext4_mark_inode_dirty(handle, inode);
4411ac27a0ecSDave Kleikamp 		if (!error)
4412ac27a0ecSDave Kleikamp 			error = rc;
4413617ba13bSMingming Cao 		ext4_journal_stop(handle);
4414678aaf48SJan Kara 
4415678aaf48SJan Kara 		if (ext4_should_order_data(inode)) {
4416678aaf48SJan Kara 			error = ext4_begin_ordered_truncate(inode,
4417678aaf48SJan Kara 							    attr->ia_size);
4418678aaf48SJan Kara 			if (error) {
4419678aaf48SJan Kara 				/* Do as much error cleanup as possible */
4420678aaf48SJan Kara 				handle = ext4_journal_start(inode, 3);
4421678aaf48SJan Kara 				if (IS_ERR(handle)) {
4422678aaf48SJan Kara 					ext4_orphan_del(NULL, inode);
4423678aaf48SJan Kara 					goto err_out;
4424678aaf48SJan Kara 				}
4425678aaf48SJan Kara 				ext4_orphan_del(handle, inode);
4426678aaf48SJan Kara 				ext4_journal_stop(handle);
4427678aaf48SJan Kara 				goto err_out;
4428678aaf48SJan Kara 			}
4429678aaf48SJan Kara 		}
4430ac27a0ecSDave Kleikamp 	}
4431ac27a0ecSDave Kleikamp 
4432ac27a0ecSDave Kleikamp 	rc = inode_setattr(inode, attr);
4433ac27a0ecSDave Kleikamp 
4434617ba13bSMingming Cao 	/* If inode_setattr's call to ext4_truncate failed to get a
4435ac27a0ecSDave Kleikamp 	 * transaction handle at all, we need to clean up the in-core
4436ac27a0ecSDave Kleikamp 	 * orphan list manually. */
4437ac27a0ecSDave Kleikamp 	if (inode->i_nlink)
4438617ba13bSMingming Cao 		ext4_orphan_del(NULL, inode);
4439ac27a0ecSDave Kleikamp 
4440ac27a0ecSDave Kleikamp 	if (!rc && (ia_valid & ATTR_MODE))
4441617ba13bSMingming Cao 		rc = ext4_acl_chmod(inode);
4442ac27a0ecSDave Kleikamp 
4443ac27a0ecSDave Kleikamp err_out:
4444617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, error);
4445ac27a0ecSDave Kleikamp 	if (!error)
4446ac27a0ecSDave Kleikamp 		error = rc;
4447ac27a0ecSDave Kleikamp 	return error;
4448ac27a0ecSDave Kleikamp }
4449ac27a0ecSDave Kleikamp 
44503e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
44513e3398a0SMingming Cao 		 struct kstat *stat)
44523e3398a0SMingming Cao {
44533e3398a0SMingming Cao 	struct inode *inode;
44543e3398a0SMingming Cao 	unsigned long delalloc_blocks;
44553e3398a0SMingming Cao 
44563e3398a0SMingming Cao 	inode = dentry->d_inode;
44573e3398a0SMingming Cao 	generic_fillattr(inode, stat);
44583e3398a0SMingming Cao 
44593e3398a0SMingming Cao 	/*
44603e3398a0SMingming Cao 	 * We can't update i_blocks if the block allocation is delayed
44613e3398a0SMingming Cao 	 * otherwise in the case of system crash before the real block
44623e3398a0SMingming Cao 	 * allocation is done, we will have i_blocks inconsistent with
44633e3398a0SMingming Cao 	 * on-disk file blocks.
44643e3398a0SMingming Cao 	 * We always keep i_blocks updated together with real
44653e3398a0SMingming Cao 	 * allocation. But to not confuse with user, stat
44663e3398a0SMingming Cao 	 * will return the blocks that include the delayed allocation
44673e3398a0SMingming Cao 	 * blocks for this file.
44683e3398a0SMingming Cao 	 */
44693e3398a0SMingming Cao 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
44703e3398a0SMingming Cao 	delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
44713e3398a0SMingming Cao 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
44723e3398a0SMingming Cao 
44733e3398a0SMingming Cao 	stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
44743e3398a0SMingming Cao 	return 0;
44753e3398a0SMingming Cao }
4476ac27a0ecSDave Kleikamp 
4477a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4478a02908f1SMingming Cao 				      int chunk)
4479ac27a0ecSDave Kleikamp {
4480a02908f1SMingming Cao 	int indirects;
4481ac27a0ecSDave Kleikamp 
4482a02908f1SMingming Cao 	/* if nrblocks are contiguous */
4483a02908f1SMingming Cao 	if (chunk) {
4484a02908f1SMingming Cao 		/*
4485a02908f1SMingming Cao 		 * With N contiguous data blocks, it need at most
4486a02908f1SMingming Cao 		 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4487a02908f1SMingming Cao 		 * 2 dindirect blocks
4488a02908f1SMingming Cao 		 * 1 tindirect block
4489a02908f1SMingming Cao 		 */
4490a02908f1SMingming Cao 		indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4491a02908f1SMingming Cao 		return indirects + 3;
4492a02908f1SMingming Cao 	}
4493a02908f1SMingming Cao 	/*
4494a02908f1SMingming Cao 	 * if nrblocks are not contiguous, worse case, each block touch
4495a02908f1SMingming Cao 	 * a indirect block, and each indirect block touch a double indirect
4496a02908f1SMingming Cao 	 * block, plus a triple indirect block
4497a02908f1SMingming Cao 	 */
4498a02908f1SMingming Cao 	indirects = nrblocks * 2 + 1;
4499a02908f1SMingming Cao 	return indirects;
4500a02908f1SMingming Cao }
4501a86c6181SAlex Tomas 
4502a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4503a02908f1SMingming Cao {
4504a02908f1SMingming Cao 	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
4505a02908f1SMingming Cao 		return ext4_indirect_trans_blocks(inode, nrblocks, 0);
4506a02908f1SMingming Cao 	return ext4_ext_index_trans_blocks(inode, nrblocks, 0);
4507a02908f1SMingming Cao }
4508a02908f1SMingming Cao /*
4509a02908f1SMingming Cao  * Account for index blocks, block groups bitmaps and block group
4510a02908f1SMingming Cao  * descriptor blocks if modify datablocks and index blocks
4511a02908f1SMingming Cao  * worse case, the indexs blocks spread over different block groups
4512a02908f1SMingming Cao  *
4513a02908f1SMingming Cao  * If datablocks are discontiguous, they are possible to spread over
4514a02908f1SMingming Cao  * different block groups too. If they are contiugous, with flexbg,
4515a02908f1SMingming Cao  * they could still across block group boundary.
4516a02908f1SMingming Cao  *
4517a02908f1SMingming Cao  * Also account for superblock, inode, quota and xattr blocks
4518a02908f1SMingming Cao  */
4519a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4520a02908f1SMingming Cao {
4521a02908f1SMingming Cao 	int groups, gdpblocks;
4522a02908f1SMingming Cao 	int idxblocks;
4523a02908f1SMingming Cao 	int ret = 0;
4524a02908f1SMingming Cao 
4525a02908f1SMingming Cao 	/*
4526a02908f1SMingming Cao 	 * How many index blocks need to touch to modify nrblocks?
4527a02908f1SMingming Cao 	 * The "Chunk" flag indicating whether the nrblocks is
4528a02908f1SMingming Cao 	 * physically contiguous on disk
4529a02908f1SMingming Cao 	 *
4530a02908f1SMingming Cao 	 * For Direct IO and fallocate, they calls get_block to allocate
4531a02908f1SMingming Cao 	 * one single extent at a time, so they could set the "Chunk" flag
4532a02908f1SMingming Cao 	 */
4533a02908f1SMingming Cao 	idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4534a02908f1SMingming Cao 
4535a02908f1SMingming Cao 	ret = idxblocks;
4536a02908f1SMingming Cao 
4537a02908f1SMingming Cao 	/*
4538a02908f1SMingming Cao 	 * Now let's see how many group bitmaps and group descriptors need
4539a02908f1SMingming Cao 	 * to account
4540a02908f1SMingming Cao 	 */
4541a02908f1SMingming Cao 	groups = idxblocks;
4542a02908f1SMingming Cao 	if (chunk)
4543a02908f1SMingming Cao 		groups += 1;
4544ac27a0ecSDave Kleikamp 	else
4545a02908f1SMingming Cao 		groups += nrblocks;
4546ac27a0ecSDave Kleikamp 
4547a02908f1SMingming Cao 	gdpblocks = groups;
4548a02908f1SMingming Cao 	if (groups > EXT4_SB(inode->i_sb)->s_groups_count)
4549a02908f1SMingming Cao 		groups = EXT4_SB(inode->i_sb)->s_groups_count;
4550a02908f1SMingming Cao 	if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4551a02908f1SMingming Cao 		gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4552a02908f1SMingming Cao 
4553a02908f1SMingming Cao 	/* bitmaps and block group descriptor blocks */
4554a02908f1SMingming Cao 	ret += groups + gdpblocks;
4555a02908f1SMingming Cao 
4556a02908f1SMingming Cao 	/* Blocks for super block, inode, quota and xattr blocks */
4557a02908f1SMingming Cao 	ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
4558ac27a0ecSDave Kleikamp 
4559ac27a0ecSDave Kleikamp 	return ret;
4560ac27a0ecSDave Kleikamp }
4561ac27a0ecSDave Kleikamp 
4562ac27a0ecSDave Kleikamp /*
4563a02908f1SMingming Cao  * Calulate the total number of credits to reserve to fit
4564f3bd1f3fSMingming Cao  * the modification of a single pages into a single transaction,
4565f3bd1f3fSMingming Cao  * which may include multiple chunks of block allocations.
4566a02908f1SMingming Cao  *
4567525f4ed8SMingming Cao  * This could be called via ext4_write_begin()
4568a02908f1SMingming Cao  *
4569525f4ed8SMingming Cao  * We need to consider the worse case, when
4570a02908f1SMingming Cao  * one new block per extent.
4571a02908f1SMingming Cao  */
4572a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode)
4573a02908f1SMingming Cao {
4574a02908f1SMingming Cao 	int bpp = ext4_journal_blocks_per_page(inode);
4575a02908f1SMingming Cao 	int ret;
4576a02908f1SMingming Cao 
4577a02908f1SMingming Cao 	ret = ext4_meta_trans_blocks(inode, bpp, 0);
4578a02908f1SMingming Cao 
4579a02908f1SMingming Cao 	/* Account for data blocks for journalled mode */
4580a02908f1SMingming Cao 	if (ext4_should_journal_data(inode))
4581a02908f1SMingming Cao 		ret += bpp;
4582a02908f1SMingming Cao 	return ret;
4583a02908f1SMingming Cao }
4584f3bd1f3fSMingming Cao 
4585f3bd1f3fSMingming Cao /*
4586f3bd1f3fSMingming Cao  * Calculate the journal credits for a chunk of data modification.
4587f3bd1f3fSMingming Cao  *
4588f3bd1f3fSMingming Cao  * This is called from DIO, fallocate or whoever calling
4589f3bd1f3fSMingming Cao  * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks.
4590f3bd1f3fSMingming Cao  *
4591f3bd1f3fSMingming Cao  * journal buffers for data blocks are not included here, as DIO
4592f3bd1f3fSMingming Cao  * and fallocate do no need to journal data buffers.
4593f3bd1f3fSMingming Cao  */
4594f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4595f3bd1f3fSMingming Cao {
4596f3bd1f3fSMingming Cao 	return ext4_meta_trans_blocks(inode, nrblocks, 1);
4597f3bd1f3fSMingming Cao }
4598f3bd1f3fSMingming Cao 
4599a02908f1SMingming Cao /*
4600617ba13bSMingming Cao  * The caller must have previously called ext4_reserve_inode_write().
4601ac27a0ecSDave Kleikamp  * Give this, we know that the caller already has write access to iloc->bh.
4602ac27a0ecSDave Kleikamp  */
4603617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle,
4604617ba13bSMingming Cao 		struct inode *inode, struct ext4_iloc *iloc)
4605ac27a0ecSDave Kleikamp {
4606ac27a0ecSDave Kleikamp 	int err = 0;
4607ac27a0ecSDave Kleikamp 
460825ec56b5SJean Noel Cordenner 	if (test_opt(inode->i_sb, I_VERSION))
460925ec56b5SJean Noel Cordenner 		inode_inc_iversion(inode);
461025ec56b5SJean Noel Cordenner 
4611ac27a0ecSDave Kleikamp 	/* the do_update_inode consumes one bh->b_count */
4612ac27a0ecSDave Kleikamp 	get_bh(iloc->bh);
4613ac27a0ecSDave Kleikamp 
4614dab291afSMingming Cao 	/* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
4615617ba13bSMingming Cao 	err = ext4_do_update_inode(handle, inode, iloc);
4616ac27a0ecSDave Kleikamp 	put_bh(iloc->bh);
4617ac27a0ecSDave Kleikamp 	return err;
4618ac27a0ecSDave Kleikamp }
4619ac27a0ecSDave Kleikamp 
4620ac27a0ecSDave Kleikamp /*
4621ac27a0ecSDave Kleikamp  * On success, We end up with an outstanding reference count against
4622ac27a0ecSDave Kleikamp  * iloc->bh.  This _must_ be cleaned up later.
4623ac27a0ecSDave Kleikamp  */
4624ac27a0ecSDave Kleikamp 
4625ac27a0ecSDave Kleikamp int
4626617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4627617ba13bSMingming Cao 			 struct ext4_iloc *iloc)
4628ac27a0ecSDave Kleikamp {
4629ac27a0ecSDave Kleikamp 	int err = 0;
4630ac27a0ecSDave Kleikamp 	if (handle) {
4631617ba13bSMingming Cao 		err = ext4_get_inode_loc(inode, iloc);
4632ac27a0ecSDave Kleikamp 		if (!err) {
4633ac27a0ecSDave Kleikamp 			BUFFER_TRACE(iloc->bh, "get_write_access");
4634617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, iloc->bh);
4635ac27a0ecSDave Kleikamp 			if (err) {
4636ac27a0ecSDave Kleikamp 				brelse(iloc->bh);
4637ac27a0ecSDave Kleikamp 				iloc->bh = NULL;
4638ac27a0ecSDave Kleikamp 			}
4639ac27a0ecSDave Kleikamp 		}
4640ac27a0ecSDave Kleikamp 	}
4641617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4642ac27a0ecSDave Kleikamp 	return err;
4643ac27a0ecSDave Kleikamp }
4644ac27a0ecSDave Kleikamp 
4645ac27a0ecSDave Kleikamp /*
46466dd4ee7cSKalpak Shah  * Expand an inode by new_extra_isize bytes.
46476dd4ee7cSKalpak Shah  * Returns 0 on success or negative error number on failure.
46486dd4ee7cSKalpak Shah  */
46491d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode,
46501d03ec98SAneesh Kumar K.V 				   unsigned int new_extra_isize,
46511d03ec98SAneesh Kumar K.V 				   struct ext4_iloc iloc,
46521d03ec98SAneesh Kumar K.V 				   handle_t *handle)
46536dd4ee7cSKalpak Shah {
46546dd4ee7cSKalpak Shah 	struct ext4_inode *raw_inode;
46556dd4ee7cSKalpak Shah 	struct ext4_xattr_ibody_header *header;
46566dd4ee7cSKalpak Shah 	struct ext4_xattr_entry *entry;
46576dd4ee7cSKalpak Shah 
46586dd4ee7cSKalpak Shah 	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
46596dd4ee7cSKalpak Shah 		return 0;
46606dd4ee7cSKalpak Shah 
46616dd4ee7cSKalpak Shah 	raw_inode = ext4_raw_inode(&iloc);
46626dd4ee7cSKalpak Shah 
46636dd4ee7cSKalpak Shah 	header = IHDR(inode, raw_inode);
46646dd4ee7cSKalpak Shah 	entry = IFIRST(header);
46656dd4ee7cSKalpak Shah 
46666dd4ee7cSKalpak Shah 	/* No extended attributes present */
46676dd4ee7cSKalpak Shah 	if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
46686dd4ee7cSKalpak Shah 		header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
46696dd4ee7cSKalpak Shah 		memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
46706dd4ee7cSKalpak Shah 			new_extra_isize);
46716dd4ee7cSKalpak Shah 		EXT4_I(inode)->i_extra_isize = new_extra_isize;
46726dd4ee7cSKalpak Shah 		return 0;
46736dd4ee7cSKalpak Shah 	}
46746dd4ee7cSKalpak Shah 
46756dd4ee7cSKalpak Shah 	/* try to expand with EAs present */
46766dd4ee7cSKalpak Shah 	return ext4_expand_extra_isize_ea(inode, new_extra_isize,
46776dd4ee7cSKalpak Shah 					  raw_inode, handle);
46786dd4ee7cSKalpak Shah }
46796dd4ee7cSKalpak Shah 
46806dd4ee7cSKalpak Shah /*
4681ac27a0ecSDave Kleikamp  * What we do here is to mark the in-core inode as clean with respect to inode
4682ac27a0ecSDave Kleikamp  * dirtiness (it may still be data-dirty).
4683ac27a0ecSDave Kleikamp  * This means that the in-core inode may be reaped by prune_icache
4684ac27a0ecSDave Kleikamp  * without having to perform any I/O.  This is a very good thing,
4685ac27a0ecSDave Kleikamp  * because *any* task may call prune_icache - even ones which
4686ac27a0ecSDave Kleikamp  * have a transaction open against a different journal.
4687ac27a0ecSDave Kleikamp  *
4688ac27a0ecSDave Kleikamp  * Is this cheating?  Not really.  Sure, we haven't written the
4689ac27a0ecSDave Kleikamp  * inode out, but prune_icache isn't a user-visible syncing function.
4690ac27a0ecSDave Kleikamp  * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4691ac27a0ecSDave Kleikamp  * we start and wait on commits.
4692ac27a0ecSDave Kleikamp  *
4693ac27a0ecSDave Kleikamp  * Is this efficient/effective?  Well, we're being nice to the system
4694ac27a0ecSDave Kleikamp  * by cleaning up our inodes proactively so they can be reaped
4695ac27a0ecSDave Kleikamp  * without I/O.  But we are potentially leaving up to five seconds'
4696ac27a0ecSDave Kleikamp  * worth of inodes floating about which prune_icache wants us to
4697ac27a0ecSDave Kleikamp  * write out.  One way to fix that would be to get prune_icache()
4698ac27a0ecSDave Kleikamp  * to do a write_super() to free up some memory.  It has the desired
4699ac27a0ecSDave Kleikamp  * effect.
4700ac27a0ecSDave Kleikamp  */
4701617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
4702ac27a0ecSDave Kleikamp {
4703617ba13bSMingming Cao 	struct ext4_iloc iloc;
47046dd4ee7cSKalpak Shah 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
47056dd4ee7cSKalpak Shah 	static unsigned int mnt_count;
47066dd4ee7cSKalpak Shah 	int err, ret;
4707ac27a0ecSDave Kleikamp 
4708ac27a0ecSDave Kleikamp 	might_sleep();
4709617ba13bSMingming Cao 	err = ext4_reserve_inode_write(handle, inode, &iloc);
47106dd4ee7cSKalpak Shah 	if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
47116dd4ee7cSKalpak Shah 	    !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
47126dd4ee7cSKalpak Shah 		/*
47136dd4ee7cSKalpak Shah 		 * We need extra buffer credits since we may write into EA block
47146dd4ee7cSKalpak Shah 		 * with this same handle. If journal_extend fails, then it will
47156dd4ee7cSKalpak Shah 		 * only result in a minor loss of functionality for that inode.
47166dd4ee7cSKalpak Shah 		 * If this is felt to be critical, then e2fsck should be run to
47176dd4ee7cSKalpak Shah 		 * force a large enough s_min_extra_isize.
47186dd4ee7cSKalpak Shah 		 */
47196dd4ee7cSKalpak Shah 		if ((jbd2_journal_extend(handle,
47206dd4ee7cSKalpak Shah 			     EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
47216dd4ee7cSKalpak Shah 			ret = ext4_expand_extra_isize(inode,
47226dd4ee7cSKalpak Shah 						      sbi->s_want_extra_isize,
47236dd4ee7cSKalpak Shah 						      iloc, handle);
47246dd4ee7cSKalpak Shah 			if (ret) {
47256dd4ee7cSKalpak Shah 				EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
4726c1bddad9SAneesh Kumar K.V 				if (mnt_count !=
4727c1bddad9SAneesh Kumar K.V 					le16_to_cpu(sbi->s_es->s_mnt_count)) {
472846e665e9SHarvey Harrison 					ext4_warning(inode->i_sb, __func__,
47296dd4ee7cSKalpak Shah 					"Unable to expand inode %lu. Delete"
47306dd4ee7cSKalpak Shah 					" some EAs or run e2fsck.",
47316dd4ee7cSKalpak Shah 					inode->i_ino);
4732c1bddad9SAneesh Kumar K.V 					mnt_count =
4733c1bddad9SAneesh Kumar K.V 					  le16_to_cpu(sbi->s_es->s_mnt_count);
47346dd4ee7cSKalpak Shah 				}
47356dd4ee7cSKalpak Shah 			}
47366dd4ee7cSKalpak Shah 		}
47376dd4ee7cSKalpak Shah 	}
4738ac27a0ecSDave Kleikamp 	if (!err)
4739617ba13bSMingming Cao 		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
4740ac27a0ecSDave Kleikamp 	return err;
4741ac27a0ecSDave Kleikamp }
4742ac27a0ecSDave Kleikamp 
4743ac27a0ecSDave Kleikamp /*
4744617ba13bSMingming Cao  * ext4_dirty_inode() is called from __mark_inode_dirty()
4745ac27a0ecSDave Kleikamp  *
4746ac27a0ecSDave Kleikamp  * We're really interested in the case where a file is being extended.
4747ac27a0ecSDave Kleikamp  * i_size has been changed by generic_commit_write() and we thus need
4748ac27a0ecSDave Kleikamp  * to include the updated inode in the current transaction.
4749ac27a0ecSDave Kleikamp  *
4750ac27a0ecSDave Kleikamp  * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
4751ac27a0ecSDave Kleikamp  * are allocated to the file.
4752ac27a0ecSDave Kleikamp  *
4753ac27a0ecSDave Kleikamp  * If the inode is marked synchronous, we don't honour that here - doing
4754ac27a0ecSDave Kleikamp  * so would cause a commit on atime updates, which we don't bother doing.
4755ac27a0ecSDave Kleikamp  * We handle synchronous inodes at the highest possible level.
4756ac27a0ecSDave Kleikamp  */
4757617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode)
4758ac27a0ecSDave Kleikamp {
4759617ba13bSMingming Cao 	handle_t *current_handle = ext4_journal_current_handle();
4760ac27a0ecSDave Kleikamp 	handle_t *handle;
4761ac27a0ecSDave Kleikamp 
4762617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 2);
4763ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4764ac27a0ecSDave Kleikamp 		goto out;
4765ac27a0ecSDave Kleikamp 	if (current_handle &&
4766ac27a0ecSDave Kleikamp 		current_handle->h_transaction != handle->h_transaction) {
4767ac27a0ecSDave Kleikamp 		/* This task has a transaction open against a different fs */
4768ac27a0ecSDave Kleikamp 		printk(KERN_EMERG "%s: transactions do not match!\n",
476946e665e9SHarvey Harrison 		       __func__);
4770ac27a0ecSDave Kleikamp 	} else {
4771ac27a0ecSDave Kleikamp 		jbd_debug(5, "marking dirty.  outer handle=%p\n",
4772ac27a0ecSDave Kleikamp 				current_handle);
4773617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
4774ac27a0ecSDave Kleikamp 	}
4775617ba13bSMingming Cao 	ext4_journal_stop(handle);
4776ac27a0ecSDave Kleikamp out:
4777ac27a0ecSDave Kleikamp 	return;
4778ac27a0ecSDave Kleikamp }
4779ac27a0ecSDave Kleikamp 
4780ac27a0ecSDave Kleikamp #if 0
4781ac27a0ecSDave Kleikamp /*
4782ac27a0ecSDave Kleikamp  * Bind an inode's backing buffer_head into this transaction, to prevent
4783ac27a0ecSDave Kleikamp  * it from being flushed to disk early.  Unlike
4784617ba13bSMingming Cao  * ext4_reserve_inode_write, this leaves behind no bh reference and
4785ac27a0ecSDave Kleikamp  * returns no iloc structure, so the caller needs to repeat the iloc
4786ac27a0ecSDave Kleikamp  * lookup to mark the inode dirty later.
4787ac27a0ecSDave Kleikamp  */
4788617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode)
4789ac27a0ecSDave Kleikamp {
4790617ba13bSMingming Cao 	struct ext4_iloc iloc;
4791ac27a0ecSDave Kleikamp 
4792ac27a0ecSDave Kleikamp 	int err = 0;
4793ac27a0ecSDave Kleikamp 	if (handle) {
4794617ba13bSMingming Cao 		err = ext4_get_inode_loc(inode, &iloc);
4795ac27a0ecSDave Kleikamp 		if (!err) {
4796ac27a0ecSDave Kleikamp 			BUFFER_TRACE(iloc.bh, "get_write_access");
4797dab291afSMingming Cao 			err = jbd2_journal_get_write_access(handle, iloc.bh);
4798ac27a0ecSDave Kleikamp 			if (!err)
4799617ba13bSMingming Cao 				err = ext4_journal_dirty_metadata(handle,
4800ac27a0ecSDave Kleikamp 								  iloc.bh);
4801ac27a0ecSDave Kleikamp 			brelse(iloc.bh);
4802ac27a0ecSDave Kleikamp 		}
4803ac27a0ecSDave Kleikamp 	}
4804617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4805ac27a0ecSDave Kleikamp 	return err;
4806ac27a0ecSDave Kleikamp }
4807ac27a0ecSDave Kleikamp #endif
4808ac27a0ecSDave Kleikamp 
4809617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val)
4810ac27a0ecSDave Kleikamp {
4811ac27a0ecSDave Kleikamp 	journal_t *journal;
4812ac27a0ecSDave Kleikamp 	handle_t *handle;
4813ac27a0ecSDave Kleikamp 	int err;
4814ac27a0ecSDave Kleikamp 
4815ac27a0ecSDave Kleikamp 	/*
4816ac27a0ecSDave Kleikamp 	 * We have to be very careful here: changing a data block's
4817ac27a0ecSDave Kleikamp 	 * journaling status dynamically is dangerous.  If we write a
4818ac27a0ecSDave Kleikamp 	 * data block to the journal, change the status and then delete
4819ac27a0ecSDave Kleikamp 	 * that block, we risk forgetting to revoke the old log record
4820ac27a0ecSDave Kleikamp 	 * from the journal and so a subsequent replay can corrupt data.
4821ac27a0ecSDave Kleikamp 	 * So, first we make sure that the journal is empty and that
4822ac27a0ecSDave Kleikamp 	 * nobody is changing anything.
4823ac27a0ecSDave Kleikamp 	 */
4824ac27a0ecSDave Kleikamp 
4825617ba13bSMingming Cao 	journal = EXT4_JOURNAL(inode);
4826d699594dSDave Hansen 	if (is_journal_aborted(journal))
4827ac27a0ecSDave Kleikamp 		return -EROFS;
4828ac27a0ecSDave Kleikamp 
4829dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
4830dab291afSMingming Cao 	jbd2_journal_flush(journal);
4831ac27a0ecSDave Kleikamp 
4832ac27a0ecSDave Kleikamp 	/*
4833ac27a0ecSDave Kleikamp 	 * OK, there are no updates running now, and all cached data is
4834ac27a0ecSDave Kleikamp 	 * synced to disk.  We are now in a completely consistent state
4835ac27a0ecSDave Kleikamp 	 * which doesn't have anything in the journal, and we know that
4836ac27a0ecSDave Kleikamp 	 * no filesystem updates are running, so it is safe to modify
4837ac27a0ecSDave Kleikamp 	 * the inode's in-core data-journaling state flag now.
4838ac27a0ecSDave Kleikamp 	 */
4839ac27a0ecSDave Kleikamp 
4840ac27a0ecSDave Kleikamp 	if (val)
4841617ba13bSMingming Cao 		EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
4842ac27a0ecSDave Kleikamp 	else
4843617ba13bSMingming Cao 		EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
4844617ba13bSMingming Cao 	ext4_set_aops(inode);
4845ac27a0ecSDave Kleikamp 
4846dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
4847ac27a0ecSDave Kleikamp 
4848ac27a0ecSDave Kleikamp 	/* Finally we can mark the inode as dirty. */
4849ac27a0ecSDave Kleikamp 
4850617ba13bSMingming Cao 	handle = ext4_journal_start(inode, 1);
4851ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
4852ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
4853ac27a0ecSDave Kleikamp 
4854617ba13bSMingming Cao 	err = ext4_mark_inode_dirty(handle, inode);
4855ac27a0ecSDave Kleikamp 	handle->h_sync = 1;
4856617ba13bSMingming Cao 	ext4_journal_stop(handle);
4857617ba13bSMingming Cao 	ext4_std_error(inode->i_sb, err);
4858ac27a0ecSDave Kleikamp 
4859ac27a0ecSDave Kleikamp 	return err;
4860ac27a0ecSDave Kleikamp }
48612e9ee850SAneesh Kumar K.V 
48622e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
48632e9ee850SAneesh Kumar K.V {
48642e9ee850SAneesh Kumar K.V 	return !buffer_mapped(bh);
48652e9ee850SAneesh Kumar K.V }
48662e9ee850SAneesh Kumar K.V 
48672e9ee850SAneesh Kumar K.V int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page)
48682e9ee850SAneesh Kumar K.V {
48692e9ee850SAneesh Kumar K.V 	loff_t size;
48702e9ee850SAneesh Kumar K.V 	unsigned long len;
48712e9ee850SAneesh Kumar K.V 	int ret = -EINVAL;
48722e9ee850SAneesh Kumar K.V 	struct file *file = vma->vm_file;
48732e9ee850SAneesh Kumar K.V 	struct inode *inode = file->f_path.dentry->d_inode;
48742e9ee850SAneesh Kumar K.V 	struct address_space *mapping = inode->i_mapping;
48752e9ee850SAneesh Kumar K.V 
48762e9ee850SAneesh Kumar K.V 	/*
48772e9ee850SAneesh Kumar K.V 	 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
48782e9ee850SAneesh Kumar K.V 	 * get i_mutex because we are already holding mmap_sem.
48792e9ee850SAneesh Kumar K.V 	 */
48802e9ee850SAneesh Kumar K.V 	down_read(&inode->i_alloc_sem);
48812e9ee850SAneesh Kumar K.V 	size = i_size_read(inode);
48822e9ee850SAneesh Kumar K.V 	if (page->mapping != mapping || size <= page_offset(page)
48832e9ee850SAneesh Kumar K.V 	    || !PageUptodate(page)) {
48842e9ee850SAneesh Kumar K.V 		/* page got truncated from under us? */
48852e9ee850SAneesh Kumar K.V 		goto out_unlock;
48862e9ee850SAneesh Kumar K.V 	}
48872e9ee850SAneesh Kumar K.V 	ret = 0;
48882e9ee850SAneesh Kumar K.V 	if (PageMappedToDisk(page))
48892e9ee850SAneesh Kumar K.V 		goto out_unlock;
48902e9ee850SAneesh Kumar K.V 
48912e9ee850SAneesh Kumar K.V 	if (page->index == size >> PAGE_CACHE_SHIFT)
48922e9ee850SAneesh Kumar K.V 		len = size & ~PAGE_CACHE_MASK;
48932e9ee850SAneesh Kumar K.V 	else
48942e9ee850SAneesh Kumar K.V 		len = PAGE_CACHE_SIZE;
48952e9ee850SAneesh Kumar K.V 
48962e9ee850SAneesh Kumar K.V 	if (page_has_buffers(page)) {
48972e9ee850SAneesh Kumar K.V 		/* return if we have all the buffers mapped */
48982e9ee850SAneesh Kumar K.V 		if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
48992e9ee850SAneesh Kumar K.V 				       ext4_bh_unmapped))
49002e9ee850SAneesh Kumar K.V 			goto out_unlock;
49012e9ee850SAneesh Kumar K.V 	}
49022e9ee850SAneesh Kumar K.V 	/*
49032e9ee850SAneesh Kumar K.V 	 * OK, we need to fill the hole... Do write_begin write_end
49042e9ee850SAneesh Kumar K.V 	 * to do block allocation/reservation.We are not holding
49052e9ee850SAneesh Kumar K.V 	 * inode.i__mutex here. That allow * parallel write_begin,
49062e9ee850SAneesh Kumar K.V 	 * write_end call. lock_page prevent this from happening
49072e9ee850SAneesh Kumar K.V 	 * on the same page though
49082e9ee850SAneesh Kumar K.V 	 */
49092e9ee850SAneesh Kumar K.V 	ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
49102e9ee850SAneesh Kumar K.V 			len, AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
49112e9ee850SAneesh Kumar K.V 	if (ret < 0)
49122e9ee850SAneesh Kumar K.V 		goto out_unlock;
49132e9ee850SAneesh Kumar K.V 	ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
49142e9ee850SAneesh Kumar K.V 			len, len, page, NULL);
49152e9ee850SAneesh Kumar K.V 	if (ret < 0)
49162e9ee850SAneesh Kumar K.V 		goto out_unlock;
49172e9ee850SAneesh Kumar K.V 	ret = 0;
49182e9ee850SAneesh Kumar K.V out_unlock:
49192e9ee850SAneesh Kumar K.V 	up_read(&inode->i_alloc_sem);
49202e9ee850SAneesh Kumar K.V 	return ret;
49212e9ee850SAneesh Kumar K.V }
4922