xref: /openbmc/linux/fs/ext4/indirect.c (revision facab4d9711e7aa3532cb82643803e8f1b9518e8)
1dae1e52cSAmir Goldstein /*
2dae1e52cSAmir Goldstein  *  linux/fs/ext4/indirect.c
3dae1e52cSAmir Goldstein  *
4dae1e52cSAmir Goldstein  *  from
5dae1e52cSAmir Goldstein  *
6dae1e52cSAmir Goldstein  *  linux/fs/ext4/inode.c
7dae1e52cSAmir Goldstein  *
8dae1e52cSAmir Goldstein  * Copyright (C) 1992, 1993, 1994, 1995
9dae1e52cSAmir Goldstein  * Remy Card (card@masi.ibp.fr)
10dae1e52cSAmir Goldstein  * Laboratoire MASI - Institut Blaise Pascal
11dae1e52cSAmir Goldstein  * Universite Pierre et Marie Curie (Paris VI)
12dae1e52cSAmir Goldstein  *
13dae1e52cSAmir Goldstein  *  from
14dae1e52cSAmir Goldstein  *
15dae1e52cSAmir Goldstein  *  linux/fs/minix/inode.c
16dae1e52cSAmir Goldstein  *
17dae1e52cSAmir Goldstein  *  Copyright (C) 1991, 1992  Linus Torvalds
18dae1e52cSAmir Goldstein  *
19dae1e52cSAmir Goldstein  *  Goal-directed block allocation by Stephen Tweedie
20dae1e52cSAmir Goldstein  *	(sct@redhat.com), 1993, 1998
21dae1e52cSAmir Goldstein  */
22dae1e52cSAmir Goldstein 
23dae1e52cSAmir Goldstein #include "ext4_jbd2.h"
24dae1e52cSAmir Goldstein #include "truncate.h"
25c94c2acfSMatthew Wilcox #include <linux/dax.h>
26e2e40f2cSChristoph Hellwig #include <linux/uio.h>
27dae1e52cSAmir Goldstein 
28dae1e52cSAmir Goldstein #include <trace/events/ext4.h>
29dae1e52cSAmir Goldstein 
30dae1e52cSAmir Goldstein typedef struct {
31dae1e52cSAmir Goldstein 	__le32	*p;
32dae1e52cSAmir Goldstein 	__le32	key;
33dae1e52cSAmir Goldstein 	struct buffer_head *bh;
34dae1e52cSAmir Goldstein } Indirect;
35dae1e52cSAmir Goldstein 
36dae1e52cSAmir Goldstein static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
37dae1e52cSAmir Goldstein {
38dae1e52cSAmir Goldstein 	p->key = *(p->p = v);
39dae1e52cSAmir Goldstein 	p->bh = bh;
40dae1e52cSAmir Goldstein }
41dae1e52cSAmir Goldstein 
42dae1e52cSAmir Goldstein /**
43dae1e52cSAmir Goldstein  *	ext4_block_to_path - parse the block number into array of offsets
44dae1e52cSAmir Goldstein  *	@inode: inode in question (we are only interested in its superblock)
45dae1e52cSAmir Goldstein  *	@i_block: block number to be parsed
46dae1e52cSAmir Goldstein  *	@offsets: array to store the offsets in
47dae1e52cSAmir Goldstein  *	@boundary: set this non-zero if the referred-to block is likely to be
48dae1e52cSAmir Goldstein  *	       followed (on disk) by an indirect block.
49dae1e52cSAmir Goldstein  *
50dae1e52cSAmir Goldstein  *	To store the locations of file's data ext4 uses a data structure common
51dae1e52cSAmir Goldstein  *	for UNIX filesystems - tree of pointers anchored in the inode, with
52dae1e52cSAmir Goldstein  *	data blocks at leaves and indirect blocks in intermediate nodes.
53dae1e52cSAmir Goldstein  *	This function translates the block number into path in that tree -
54dae1e52cSAmir Goldstein  *	return value is the path length and @offsets[n] is the offset of
55dae1e52cSAmir Goldstein  *	pointer to (n+1)th node in the nth one. If @block is out of range
56dae1e52cSAmir Goldstein  *	(negative or too large) warning is printed and zero returned.
57dae1e52cSAmir Goldstein  *
58dae1e52cSAmir Goldstein  *	Note: function doesn't find node addresses, so no IO is needed. All
59dae1e52cSAmir Goldstein  *	we need to know is the capacity of indirect blocks (taken from the
60dae1e52cSAmir Goldstein  *	inode->i_sb).
61dae1e52cSAmir Goldstein  */
62dae1e52cSAmir Goldstein 
63dae1e52cSAmir Goldstein /*
64dae1e52cSAmir Goldstein  * Portability note: the last comparison (check that we fit into triple
65dae1e52cSAmir Goldstein  * indirect block) is spelled differently, because otherwise on an
66dae1e52cSAmir Goldstein  * architecture with 32-bit longs and 8Kb pages we might get into trouble
67dae1e52cSAmir Goldstein  * if our filesystem had 8Kb blocks. We might use long long, but that would
68dae1e52cSAmir Goldstein  * kill us on x86. Oh, well, at least the sign propagation does not matter -
69dae1e52cSAmir Goldstein  * i_block would have to be negative in the very beginning, so we would not
70dae1e52cSAmir Goldstein  * get there at all.
71dae1e52cSAmir Goldstein  */
72dae1e52cSAmir Goldstein 
73dae1e52cSAmir Goldstein static int ext4_block_to_path(struct inode *inode,
74dae1e52cSAmir Goldstein 			      ext4_lblk_t i_block,
75dae1e52cSAmir Goldstein 			      ext4_lblk_t offsets[4], int *boundary)
76dae1e52cSAmir Goldstein {
77dae1e52cSAmir Goldstein 	int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
78dae1e52cSAmir Goldstein 	int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
79dae1e52cSAmir Goldstein 	const long direct_blocks = EXT4_NDIR_BLOCKS,
80dae1e52cSAmir Goldstein 		indirect_blocks = ptrs,
81dae1e52cSAmir Goldstein 		double_blocks = (1 << (ptrs_bits * 2));
82dae1e52cSAmir Goldstein 	int n = 0;
83dae1e52cSAmir Goldstein 	int final = 0;
84dae1e52cSAmir Goldstein 
85dae1e52cSAmir Goldstein 	if (i_block < direct_blocks) {
86dae1e52cSAmir Goldstein 		offsets[n++] = i_block;
87dae1e52cSAmir Goldstein 		final = direct_blocks;
88dae1e52cSAmir Goldstein 	} else if ((i_block -= direct_blocks) < indirect_blocks) {
89dae1e52cSAmir Goldstein 		offsets[n++] = EXT4_IND_BLOCK;
90dae1e52cSAmir Goldstein 		offsets[n++] = i_block;
91dae1e52cSAmir Goldstein 		final = ptrs;
92dae1e52cSAmir Goldstein 	} else if ((i_block -= indirect_blocks) < double_blocks) {
93dae1e52cSAmir Goldstein 		offsets[n++] = EXT4_DIND_BLOCK;
94dae1e52cSAmir Goldstein 		offsets[n++] = i_block >> ptrs_bits;
95dae1e52cSAmir Goldstein 		offsets[n++] = i_block & (ptrs - 1);
96dae1e52cSAmir Goldstein 		final = ptrs;
97dae1e52cSAmir Goldstein 	} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
98dae1e52cSAmir Goldstein 		offsets[n++] = EXT4_TIND_BLOCK;
99dae1e52cSAmir Goldstein 		offsets[n++] = i_block >> (ptrs_bits * 2);
100dae1e52cSAmir Goldstein 		offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
101dae1e52cSAmir Goldstein 		offsets[n++] = i_block & (ptrs - 1);
102dae1e52cSAmir Goldstein 		final = ptrs;
103dae1e52cSAmir Goldstein 	} else {
104dae1e52cSAmir Goldstein 		ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
105dae1e52cSAmir Goldstein 			     i_block + direct_blocks +
106dae1e52cSAmir Goldstein 			     indirect_blocks + double_blocks, inode->i_ino);
107dae1e52cSAmir Goldstein 	}
108dae1e52cSAmir Goldstein 	if (boundary)
109dae1e52cSAmir Goldstein 		*boundary = final - 1 - (i_block & (ptrs - 1));
110dae1e52cSAmir Goldstein 	return n;
111dae1e52cSAmir Goldstein }
112dae1e52cSAmir Goldstein 
113dae1e52cSAmir Goldstein /**
114dae1e52cSAmir Goldstein  *	ext4_get_branch - read the chain of indirect blocks leading to data
115dae1e52cSAmir Goldstein  *	@inode: inode in question
116dae1e52cSAmir Goldstein  *	@depth: depth of the chain (1 - direct pointer, etc.)
117dae1e52cSAmir Goldstein  *	@offsets: offsets of pointers in inode/indirect blocks
118dae1e52cSAmir Goldstein  *	@chain: place to store the result
119dae1e52cSAmir Goldstein  *	@err: here we store the error value
120dae1e52cSAmir Goldstein  *
121dae1e52cSAmir Goldstein  *	Function fills the array of triples <key, p, bh> and returns %NULL
122dae1e52cSAmir Goldstein  *	if everything went OK or the pointer to the last filled triple
123dae1e52cSAmir Goldstein  *	(incomplete one) otherwise. Upon the return chain[i].key contains
124dae1e52cSAmir Goldstein  *	the number of (i+1)-th block in the chain (as it is stored in memory,
125dae1e52cSAmir Goldstein  *	i.e. little-endian 32-bit), chain[i].p contains the address of that
126dae1e52cSAmir Goldstein  *	number (it points into struct inode for i==0 and into the bh->b_data
127dae1e52cSAmir Goldstein  *	for i>0) and chain[i].bh points to the buffer_head of i-th indirect
128dae1e52cSAmir Goldstein  *	block for i>0 and NULL for i==0. In other words, it holds the block
129dae1e52cSAmir Goldstein  *	numbers of the chain, addresses they were taken from (and where we can
130dae1e52cSAmir Goldstein  *	verify that chain did not change) and buffer_heads hosting these
131dae1e52cSAmir Goldstein  *	numbers.
132dae1e52cSAmir Goldstein  *
133dae1e52cSAmir Goldstein  *	Function stops when it stumbles upon zero pointer (absent block)
134dae1e52cSAmir Goldstein  *		(pointer to last triple returned, *@err == 0)
135dae1e52cSAmir Goldstein  *	or when it gets an IO error reading an indirect block
136dae1e52cSAmir Goldstein  *		(ditto, *@err == -EIO)
137dae1e52cSAmir Goldstein  *	or when it reads all @depth-1 indirect blocks successfully and finds
138dae1e52cSAmir Goldstein  *	the whole chain, all way to the data (returns %NULL, *err == 0).
139dae1e52cSAmir Goldstein  *
140dae1e52cSAmir Goldstein  *      Need to be called with
141dae1e52cSAmir Goldstein  *      down_read(&EXT4_I(inode)->i_data_sem)
142dae1e52cSAmir Goldstein  */
143dae1e52cSAmir Goldstein static Indirect *ext4_get_branch(struct inode *inode, int depth,
144dae1e52cSAmir Goldstein 				 ext4_lblk_t  *offsets,
145dae1e52cSAmir Goldstein 				 Indirect chain[4], int *err)
146dae1e52cSAmir Goldstein {
147dae1e52cSAmir Goldstein 	struct super_block *sb = inode->i_sb;
148dae1e52cSAmir Goldstein 	Indirect *p = chain;
149dae1e52cSAmir Goldstein 	struct buffer_head *bh;
150860d21e2STheodore Ts'o 	int ret = -EIO;
151dae1e52cSAmir Goldstein 
152dae1e52cSAmir Goldstein 	*err = 0;
153dae1e52cSAmir Goldstein 	/* i_data is not going away, no lock needed */
154dae1e52cSAmir Goldstein 	add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
155dae1e52cSAmir Goldstein 	if (!p->key)
156dae1e52cSAmir Goldstein 		goto no_block;
157dae1e52cSAmir Goldstein 	while (--depth) {
158dae1e52cSAmir Goldstein 		bh = sb_getblk(sb, le32_to_cpu(p->key));
159860d21e2STheodore Ts'o 		if (unlikely(!bh)) {
160860d21e2STheodore Ts'o 			ret = -ENOMEM;
161dae1e52cSAmir Goldstein 			goto failure;
162860d21e2STheodore Ts'o 		}
163dae1e52cSAmir Goldstein 
164dae1e52cSAmir Goldstein 		if (!bh_uptodate_or_lock(bh)) {
165dae1e52cSAmir Goldstein 			if (bh_submit_read(bh) < 0) {
166dae1e52cSAmir Goldstein 				put_bh(bh);
167dae1e52cSAmir Goldstein 				goto failure;
168dae1e52cSAmir Goldstein 			}
169dae1e52cSAmir Goldstein 			/* validate block references */
170dae1e52cSAmir Goldstein 			if (ext4_check_indirect_blockref(inode, bh)) {
171dae1e52cSAmir Goldstein 				put_bh(bh);
172dae1e52cSAmir Goldstein 				goto failure;
173dae1e52cSAmir Goldstein 			}
174dae1e52cSAmir Goldstein 		}
175dae1e52cSAmir Goldstein 
176dae1e52cSAmir Goldstein 		add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
177dae1e52cSAmir Goldstein 		/* Reader: end */
178dae1e52cSAmir Goldstein 		if (!p->key)
179dae1e52cSAmir Goldstein 			goto no_block;
180dae1e52cSAmir Goldstein 	}
181dae1e52cSAmir Goldstein 	return NULL;
182dae1e52cSAmir Goldstein 
183dae1e52cSAmir Goldstein failure:
184860d21e2STheodore Ts'o 	*err = ret;
185dae1e52cSAmir Goldstein no_block:
186dae1e52cSAmir Goldstein 	return p;
187dae1e52cSAmir Goldstein }
188dae1e52cSAmir Goldstein 
189dae1e52cSAmir Goldstein /**
190dae1e52cSAmir Goldstein  *	ext4_find_near - find a place for allocation with sufficient locality
191dae1e52cSAmir Goldstein  *	@inode: owner
192dae1e52cSAmir Goldstein  *	@ind: descriptor of indirect block.
193dae1e52cSAmir Goldstein  *
194dae1e52cSAmir Goldstein  *	This function returns the preferred place for block allocation.
195dae1e52cSAmir Goldstein  *	It is used when heuristic for sequential allocation fails.
196dae1e52cSAmir Goldstein  *	Rules are:
197dae1e52cSAmir Goldstein  *	  + if there is a block to the left of our position - allocate near it.
198dae1e52cSAmir Goldstein  *	  + if pointer will live in indirect block - allocate near that block.
199dae1e52cSAmir Goldstein  *	  + if pointer will live in inode - allocate in the same
200dae1e52cSAmir Goldstein  *	    cylinder group.
201dae1e52cSAmir Goldstein  *
202dae1e52cSAmir Goldstein  * In the latter case we colour the starting block by the callers PID to
203dae1e52cSAmir Goldstein  * prevent it from clashing with concurrent allocations for a different inode
204dae1e52cSAmir Goldstein  * in the same block group.   The PID is used here so that functionally related
205dae1e52cSAmir Goldstein  * files will be close-by on-disk.
206dae1e52cSAmir Goldstein  *
207dae1e52cSAmir Goldstein  *	Caller must make sure that @ind is valid and will stay that way.
208dae1e52cSAmir Goldstein  */
209dae1e52cSAmir Goldstein static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
210dae1e52cSAmir Goldstein {
211dae1e52cSAmir Goldstein 	struct ext4_inode_info *ei = EXT4_I(inode);
212dae1e52cSAmir Goldstein 	__le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
213dae1e52cSAmir Goldstein 	__le32 *p;
214dae1e52cSAmir Goldstein 
215dae1e52cSAmir Goldstein 	/* Try to find previous block */
216dae1e52cSAmir Goldstein 	for (p = ind->p - 1; p >= start; p--) {
217dae1e52cSAmir Goldstein 		if (*p)
218dae1e52cSAmir Goldstein 			return le32_to_cpu(*p);
219dae1e52cSAmir Goldstein 	}
220dae1e52cSAmir Goldstein 
221dae1e52cSAmir Goldstein 	/* No such thing, so let's try location of indirect block */
222dae1e52cSAmir Goldstein 	if (ind->bh)
223dae1e52cSAmir Goldstein 		return ind->bh->b_blocknr;
224dae1e52cSAmir Goldstein 
225dae1e52cSAmir Goldstein 	/*
226dae1e52cSAmir Goldstein 	 * It is going to be referred to from the inode itself? OK, just put it
227dae1e52cSAmir Goldstein 	 * into the same cylinder group then.
228dae1e52cSAmir Goldstein 	 */
229f86186b4SEric Sandeen 	return ext4_inode_to_goal_block(inode);
230dae1e52cSAmir Goldstein }
231dae1e52cSAmir Goldstein 
232dae1e52cSAmir Goldstein /**
233dae1e52cSAmir Goldstein  *	ext4_find_goal - find a preferred place for allocation.
234dae1e52cSAmir Goldstein  *	@inode: owner
235dae1e52cSAmir Goldstein  *	@block:  block we want
236dae1e52cSAmir Goldstein  *	@partial: pointer to the last triple within a chain
237dae1e52cSAmir Goldstein  *
238dae1e52cSAmir Goldstein  *	Normally this function find the preferred place for block allocation,
239dae1e52cSAmir Goldstein  *	returns it.
240dae1e52cSAmir Goldstein  *	Because this is only used for non-extent files, we limit the block nr
241dae1e52cSAmir Goldstein  *	to 32 bits.
242dae1e52cSAmir Goldstein  */
243dae1e52cSAmir Goldstein static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
244dae1e52cSAmir Goldstein 				   Indirect *partial)
245dae1e52cSAmir Goldstein {
246dae1e52cSAmir Goldstein 	ext4_fsblk_t goal;
247dae1e52cSAmir Goldstein 
248dae1e52cSAmir Goldstein 	/*
249dae1e52cSAmir Goldstein 	 * XXX need to get goal block from mballoc's data structures
250dae1e52cSAmir Goldstein 	 */
251dae1e52cSAmir Goldstein 
252dae1e52cSAmir Goldstein 	goal = ext4_find_near(inode, partial);
253dae1e52cSAmir Goldstein 	goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
254dae1e52cSAmir Goldstein 	return goal;
255dae1e52cSAmir Goldstein }
256dae1e52cSAmir Goldstein 
257dae1e52cSAmir Goldstein /**
258dae1e52cSAmir Goldstein  *	ext4_blks_to_allocate - Look up the block map and count the number
259dae1e52cSAmir Goldstein  *	of direct blocks need to be allocated for the given branch.
260dae1e52cSAmir Goldstein  *
261dae1e52cSAmir Goldstein  *	@branch: chain of indirect blocks
262dae1e52cSAmir Goldstein  *	@k: number of blocks need for indirect blocks
263dae1e52cSAmir Goldstein  *	@blks: number of data blocks to be mapped.
264dae1e52cSAmir Goldstein  *	@blocks_to_boundary:  the offset in the indirect block
265dae1e52cSAmir Goldstein  *
266dae1e52cSAmir Goldstein  *	return the total number of blocks to be allocate, including the
267dae1e52cSAmir Goldstein  *	direct and indirect blocks.
268dae1e52cSAmir Goldstein  */
269dae1e52cSAmir Goldstein static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
270dae1e52cSAmir Goldstein 				 int blocks_to_boundary)
271dae1e52cSAmir Goldstein {
272dae1e52cSAmir Goldstein 	unsigned int count = 0;
273dae1e52cSAmir Goldstein 
274dae1e52cSAmir Goldstein 	/*
275dae1e52cSAmir Goldstein 	 * Simple case, [t,d]Indirect block(s) has not allocated yet
276dae1e52cSAmir Goldstein 	 * then it's clear blocks on that path have not allocated
277dae1e52cSAmir Goldstein 	 */
278dae1e52cSAmir Goldstein 	if (k > 0) {
279dae1e52cSAmir Goldstein 		/* right now we don't handle cross boundary allocation */
280dae1e52cSAmir Goldstein 		if (blks < blocks_to_boundary + 1)
281dae1e52cSAmir Goldstein 			count += blks;
282dae1e52cSAmir Goldstein 		else
283dae1e52cSAmir Goldstein 			count += blocks_to_boundary + 1;
284dae1e52cSAmir Goldstein 		return count;
285dae1e52cSAmir Goldstein 	}
286dae1e52cSAmir Goldstein 
287dae1e52cSAmir Goldstein 	count++;
288dae1e52cSAmir Goldstein 	while (count < blks && count <= blocks_to_boundary &&
289dae1e52cSAmir Goldstein 		le32_to_cpu(*(branch[0].p + count)) == 0) {
290dae1e52cSAmir Goldstein 		count++;
291dae1e52cSAmir Goldstein 	}
292dae1e52cSAmir Goldstein 	return count;
293dae1e52cSAmir Goldstein }
294dae1e52cSAmir Goldstein 
295dae1e52cSAmir Goldstein /**
296dae1e52cSAmir Goldstein  *	ext4_alloc_branch - allocate and set up a chain of blocks.
297dae1e52cSAmir Goldstein  *	@handle: handle for this transaction
298dae1e52cSAmir Goldstein  *	@inode: owner
299dae1e52cSAmir Goldstein  *	@indirect_blks: number of allocated indirect blocks
300dae1e52cSAmir Goldstein  *	@blks: number of allocated direct blocks
301dae1e52cSAmir Goldstein  *	@goal: preferred place for allocation
302dae1e52cSAmir Goldstein  *	@offsets: offsets (in the blocks) to store the pointers to next.
303dae1e52cSAmir Goldstein  *	@branch: place to store the chain in.
304dae1e52cSAmir Goldstein  *
305dae1e52cSAmir Goldstein  *	This function allocates blocks, zeroes out all but the last one,
306dae1e52cSAmir Goldstein  *	links them into chain and (if we are synchronous) writes them to disk.
307dae1e52cSAmir Goldstein  *	In other words, it prepares a branch that can be spliced onto the
308dae1e52cSAmir Goldstein  *	inode. It stores the information about that chain in the branch[], in
309dae1e52cSAmir Goldstein  *	the same format as ext4_get_branch() would do. We are calling it after
310dae1e52cSAmir Goldstein  *	we had read the existing part of chain and partial points to the last
311dae1e52cSAmir Goldstein  *	triple of that (one with zero ->key). Upon the exit we have the same
312dae1e52cSAmir Goldstein  *	picture as after the successful ext4_get_block(), except that in one
313dae1e52cSAmir Goldstein  *	place chain is disconnected - *branch->p is still zero (we did not
314dae1e52cSAmir Goldstein  *	set the last link), but branch->key contains the number that should
315dae1e52cSAmir Goldstein  *	be placed into *branch->p to fill that gap.
316dae1e52cSAmir Goldstein  *
317dae1e52cSAmir Goldstein  *	If allocation fails we free all blocks we've allocated (and forget
318dae1e52cSAmir Goldstein  *	their buffer_heads) and return the error value the from failed
319dae1e52cSAmir Goldstein  *	ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
320dae1e52cSAmir Goldstein  *	as described above and return 0.
321dae1e52cSAmir Goldstein  */
322a5211002STheodore Ts'o static int ext4_alloc_branch(handle_t *handle,
323a5211002STheodore Ts'o 			     struct ext4_allocation_request *ar,
324a5211002STheodore Ts'o 			     int indirect_blks, ext4_lblk_t *offsets,
325a5211002STheodore Ts'o 			     Indirect *branch)
326dae1e52cSAmir Goldstein {
327dae1e52cSAmir Goldstein 	struct buffer_head *		bh;
328781f143eSTheodore Ts'o 	ext4_fsblk_t			b, new_blocks[4];
329781f143eSTheodore Ts'o 	__le32				*p;
330781f143eSTheodore Ts'o 	int				i, j, err, len = 1;
331dae1e52cSAmir Goldstein 
332781f143eSTheodore Ts'o 	for (i = 0; i <= indirect_blks; i++) {
333781f143eSTheodore Ts'o 		if (i == indirect_blks) {
334a5211002STheodore Ts'o 			new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err);
335781f143eSTheodore Ts'o 		} else
336a5211002STheodore Ts'o 			ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle,
337e3cf5d5dSTheodore Ts'o 					ar->inode, ar->goal,
338e3cf5d5dSTheodore Ts'o 					ar->flags & EXT4_MB_DELALLOC_RESERVED,
339e3cf5d5dSTheodore Ts'o 					NULL, &err);
340781f143eSTheodore Ts'o 		if (err) {
341781f143eSTheodore Ts'o 			i--;
342781f143eSTheodore Ts'o 			goto failed;
343781f143eSTheodore Ts'o 		}
344781f143eSTheodore Ts'o 		branch[i].key = cpu_to_le32(new_blocks[i]);
345781f143eSTheodore Ts'o 		if (i == 0)
346781f143eSTheodore Ts'o 			continue;
347781f143eSTheodore Ts'o 
348a5211002STheodore Ts'o 		bh = branch[i].bh = sb_getblk(ar->inode->i_sb, new_blocks[i-1]);
349dae1e52cSAmir Goldstein 		if (unlikely(!bh)) {
350860d21e2STheodore Ts'o 			err = -ENOMEM;
351dae1e52cSAmir Goldstein 			goto failed;
352dae1e52cSAmir Goldstein 		}
353dae1e52cSAmir Goldstein 		lock_buffer(bh);
354dae1e52cSAmir Goldstein 		BUFFER_TRACE(bh, "call get_create_access");
355dae1e52cSAmir Goldstein 		err = ext4_journal_get_create_access(handle, bh);
356dae1e52cSAmir Goldstein 		if (err) {
357dae1e52cSAmir Goldstein 			unlock_buffer(bh);
358dae1e52cSAmir Goldstein 			goto failed;
359dae1e52cSAmir Goldstein 		}
360dae1e52cSAmir Goldstein 
361781f143eSTheodore Ts'o 		memset(bh->b_data, 0, bh->b_size);
362781f143eSTheodore Ts'o 		p = branch[i].p = (__le32 *) bh->b_data + offsets[i];
363781f143eSTheodore Ts'o 		b = new_blocks[i];
364781f143eSTheodore Ts'o 
365781f143eSTheodore Ts'o 		if (i == indirect_blks)
366a5211002STheodore Ts'o 			len = ar->len;
367781f143eSTheodore Ts'o 		for (j = 0; j < len; j++)
368781f143eSTheodore Ts'o 			*p++ = cpu_to_le32(b++);
369781f143eSTheodore Ts'o 
370dae1e52cSAmir Goldstein 		BUFFER_TRACE(bh, "marking uptodate");
371dae1e52cSAmir Goldstein 		set_buffer_uptodate(bh);
372dae1e52cSAmir Goldstein 		unlock_buffer(bh);
373dae1e52cSAmir Goldstein 
374dae1e52cSAmir Goldstein 		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
375a5211002STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, ar->inode, bh);
376dae1e52cSAmir Goldstein 		if (err)
377dae1e52cSAmir Goldstein 			goto failed;
378dae1e52cSAmir Goldstein 	}
379781f143eSTheodore Ts'o 	return 0;
380dae1e52cSAmir Goldstein failed:
381781f143eSTheodore Ts'o 	for (; i >= 0; i--) {
382c5c7b8ddSJan Kara 		/*
383c5c7b8ddSJan Kara 		 * We want to ext4_forget() only freshly allocated indirect
384c5c7b8ddSJan Kara 		 * blocks.  Buffer for new_blocks[i-1] is at branch[i].bh and
385c5c7b8ddSJan Kara 		 * buffer at branch[0].bh is indirect block / inode already
386c5c7b8ddSJan Kara 		 * existing before ext4_alloc_branch() was called.
387c5c7b8ddSJan Kara 		 */
388c5c7b8ddSJan Kara 		if (i > 0 && i != indirect_blks && branch[i].bh)
389a5211002STheodore Ts'o 			ext4_forget(handle, 1, ar->inode, branch[i].bh,
390781f143eSTheodore Ts'o 				    branch[i].bh->b_blocknr);
391a5211002STheodore Ts'o 		ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i],
392a5211002STheodore Ts'o 				 (i == indirect_blks) ? ar->len : 1, 0);
393dae1e52cSAmir Goldstein 	}
394dae1e52cSAmir Goldstein 	return err;
395dae1e52cSAmir Goldstein }
396dae1e52cSAmir Goldstein 
397dae1e52cSAmir Goldstein /**
398dae1e52cSAmir Goldstein  * ext4_splice_branch - splice the allocated branch onto inode.
399dae1e52cSAmir Goldstein  * @handle: handle for this transaction
400dae1e52cSAmir Goldstein  * @inode: owner
401dae1e52cSAmir Goldstein  * @block: (logical) number of block we are adding
402dae1e52cSAmir Goldstein  * @chain: chain of indirect blocks (with a missing link - see
403dae1e52cSAmir Goldstein  *	ext4_alloc_branch)
404dae1e52cSAmir Goldstein  * @where: location of missing link
405dae1e52cSAmir Goldstein  * @num:   number of indirect blocks we are adding
406dae1e52cSAmir Goldstein  * @blks:  number of direct blocks we are adding
407dae1e52cSAmir Goldstein  *
408dae1e52cSAmir Goldstein  * This function fills the missing link and does all housekeeping needed in
409dae1e52cSAmir Goldstein  * inode (->i_blocks, etc.). In case of success we end up with the full
410dae1e52cSAmir Goldstein  * chain to new block and return 0.
411dae1e52cSAmir Goldstein  */
412a5211002STheodore Ts'o static int ext4_splice_branch(handle_t *handle,
413a5211002STheodore Ts'o 			      struct ext4_allocation_request *ar,
414a5211002STheodore Ts'o 			      Indirect *where, int num)
415dae1e52cSAmir Goldstein {
416dae1e52cSAmir Goldstein 	int i;
417dae1e52cSAmir Goldstein 	int err = 0;
418dae1e52cSAmir Goldstein 	ext4_fsblk_t current_block;
419dae1e52cSAmir Goldstein 
420dae1e52cSAmir Goldstein 	/*
421dae1e52cSAmir Goldstein 	 * If we're splicing into a [td]indirect block (as opposed to the
422dae1e52cSAmir Goldstein 	 * inode) then we need to get write access to the [td]indirect block
423dae1e52cSAmir Goldstein 	 * before the splice.
424dae1e52cSAmir Goldstein 	 */
425dae1e52cSAmir Goldstein 	if (where->bh) {
426dae1e52cSAmir Goldstein 		BUFFER_TRACE(where->bh, "get_write_access");
427dae1e52cSAmir Goldstein 		err = ext4_journal_get_write_access(handle, where->bh);
428dae1e52cSAmir Goldstein 		if (err)
429dae1e52cSAmir Goldstein 			goto err_out;
430dae1e52cSAmir Goldstein 	}
431dae1e52cSAmir Goldstein 	/* That's it */
432dae1e52cSAmir Goldstein 
433dae1e52cSAmir Goldstein 	*where->p = where->key;
434dae1e52cSAmir Goldstein 
435dae1e52cSAmir Goldstein 	/*
436dae1e52cSAmir Goldstein 	 * Update the host buffer_head or inode to point to more just allocated
437dae1e52cSAmir Goldstein 	 * direct blocks blocks
438dae1e52cSAmir Goldstein 	 */
439a5211002STheodore Ts'o 	if (num == 0 && ar->len > 1) {
440dae1e52cSAmir Goldstein 		current_block = le32_to_cpu(where->key) + 1;
441a5211002STheodore Ts'o 		for (i = 1; i < ar->len; i++)
442dae1e52cSAmir Goldstein 			*(where->p + i) = cpu_to_le32(current_block++);
443dae1e52cSAmir Goldstein 	}
444dae1e52cSAmir Goldstein 
445dae1e52cSAmir Goldstein 	/* We are done with atomic stuff, now do the rest of housekeeping */
446dae1e52cSAmir Goldstein 	/* had we spliced it onto indirect block? */
447dae1e52cSAmir Goldstein 	if (where->bh) {
448dae1e52cSAmir Goldstein 		/*
449dae1e52cSAmir Goldstein 		 * If we spliced it onto an indirect block, we haven't
450dae1e52cSAmir Goldstein 		 * altered the inode.  Note however that if it is being spliced
451dae1e52cSAmir Goldstein 		 * onto an indirect block at the very end of the file (the
452dae1e52cSAmir Goldstein 		 * file is growing) then we *will* alter the inode to reflect
453dae1e52cSAmir Goldstein 		 * the new i_size.  But that is not done here - it is done in
454dae1e52cSAmir Goldstein 		 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
455dae1e52cSAmir Goldstein 		 */
456dae1e52cSAmir Goldstein 		jbd_debug(5, "splicing indirect only\n");
457dae1e52cSAmir Goldstein 		BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
458a5211002STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, ar->inode, where->bh);
459dae1e52cSAmir Goldstein 		if (err)
460dae1e52cSAmir Goldstein 			goto err_out;
461dae1e52cSAmir Goldstein 	} else {
462dae1e52cSAmir Goldstein 		/*
463dae1e52cSAmir Goldstein 		 * OK, we spliced it into the inode itself on a direct block.
464dae1e52cSAmir Goldstein 		 */
465a5211002STheodore Ts'o 		ext4_mark_inode_dirty(handle, ar->inode);
466dae1e52cSAmir Goldstein 		jbd_debug(5, "splicing direct\n");
467dae1e52cSAmir Goldstein 	}
468dae1e52cSAmir Goldstein 	return err;
469dae1e52cSAmir Goldstein 
470dae1e52cSAmir Goldstein err_out:
471dae1e52cSAmir Goldstein 	for (i = 1; i <= num; i++) {
472dae1e52cSAmir Goldstein 		/*
473dae1e52cSAmir Goldstein 		 * branch[i].bh is newly allocated, so there is no
474dae1e52cSAmir Goldstein 		 * need to revoke the block, which is why we don't
475dae1e52cSAmir Goldstein 		 * need to set EXT4_FREE_BLOCKS_METADATA.
476dae1e52cSAmir Goldstein 		 */
477a5211002STheodore Ts'o 		ext4_free_blocks(handle, ar->inode, where[i].bh, 0, 1,
478dae1e52cSAmir Goldstein 				 EXT4_FREE_BLOCKS_FORGET);
479dae1e52cSAmir Goldstein 	}
480a5211002STheodore Ts'o 	ext4_free_blocks(handle, ar->inode, NULL, le32_to_cpu(where[num].key),
481a5211002STheodore Ts'o 			 ar->len, 0);
482dae1e52cSAmir Goldstein 
483dae1e52cSAmir Goldstein 	return err;
484dae1e52cSAmir Goldstein }
485dae1e52cSAmir Goldstein 
486dae1e52cSAmir Goldstein /*
487dae1e52cSAmir Goldstein  * The ext4_ind_map_blocks() function handles non-extents inodes
488dae1e52cSAmir Goldstein  * (i.e., using the traditional indirect/double-indirect i_blocks
489dae1e52cSAmir Goldstein  * scheme) for ext4_map_blocks().
490dae1e52cSAmir Goldstein  *
491dae1e52cSAmir Goldstein  * Allocation strategy is simple: if we have to allocate something, we will
492dae1e52cSAmir Goldstein  * have to go the whole way to leaf. So let's do it before attaching anything
493dae1e52cSAmir Goldstein  * to tree, set linkage between the newborn blocks, write them if sync is
494dae1e52cSAmir Goldstein  * required, recheck the path, free and repeat if check fails, otherwise
495dae1e52cSAmir Goldstein  * set the last missing link (that will protect us from any truncate-generated
496dae1e52cSAmir Goldstein  * removals - all blocks on the path are immune now) and possibly force the
497dae1e52cSAmir Goldstein  * write on the parent block.
498dae1e52cSAmir Goldstein  * That has a nice additional property: no special recovery from the failed
499dae1e52cSAmir Goldstein  * allocations is needed - we simply release blocks and do not touch anything
500dae1e52cSAmir Goldstein  * reachable from inode.
501dae1e52cSAmir Goldstein  *
502dae1e52cSAmir Goldstein  * `handle' can be NULL if create == 0.
503dae1e52cSAmir Goldstein  *
504dae1e52cSAmir Goldstein  * return > 0, # of blocks mapped or allocated.
505dae1e52cSAmir Goldstein  * return = 0, if plain lookup failed.
506dae1e52cSAmir Goldstein  * return < 0, error case.
507dae1e52cSAmir Goldstein  *
508dae1e52cSAmir Goldstein  * The ext4_ind_get_blocks() function should be called with
509dae1e52cSAmir Goldstein  * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
510dae1e52cSAmir Goldstein  * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
511dae1e52cSAmir Goldstein  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
512dae1e52cSAmir Goldstein  * blocks.
513dae1e52cSAmir Goldstein  */
514dae1e52cSAmir Goldstein int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
515dae1e52cSAmir Goldstein 			struct ext4_map_blocks *map,
516dae1e52cSAmir Goldstein 			int flags)
517dae1e52cSAmir Goldstein {
518a5211002STheodore Ts'o 	struct ext4_allocation_request ar;
519dae1e52cSAmir Goldstein 	int err = -EIO;
520dae1e52cSAmir Goldstein 	ext4_lblk_t offsets[4];
521dae1e52cSAmir Goldstein 	Indirect chain[4];
522dae1e52cSAmir Goldstein 	Indirect *partial;
523dae1e52cSAmir Goldstein 	int indirect_blks;
524dae1e52cSAmir Goldstein 	int blocks_to_boundary = 0;
525dae1e52cSAmir Goldstein 	int depth;
526dae1e52cSAmir Goldstein 	int count = 0;
527dae1e52cSAmir Goldstein 	ext4_fsblk_t first_block = 0;
528dae1e52cSAmir Goldstein 
529dae1e52cSAmir Goldstein 	trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
530dae1e52cSAmir Goldstein 	J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
531dae1e52cSAmir Goldstein 	J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
532dae1e52cSAmir Goldstein 	depth = ext4_block_to_path(inode, map->m_lblk, offsets,
533dae1e52cSAmir Goldstein 				   &blocks_to_boundary);
534dae1e52cSAmir Goldstein 
535dae1e52cSAmir Goldstein 	if (depth == 0)
536dae1e52cSAmir Goldstein 		goto out;
537dae1e52cSAmir Goldstein 
538dae1e52cSAmir Goldstein 	partial = ext4_get_branch(inode, depth, offsets, chain, &err);
539dae1e52cSAmir Goldstein 
540dae1e52cSAmir Goldstein 	/* Simplest case - block found, no allocation needed */
541dae1e52cSAmir Goldstein 	if (!partial) {
542dae1e52cSAmir Goldstein 		first_block = le32_to_cpu(chain[depth - 1].key);
543dae1e52cSAmir Goldstein 		count++;
544dae1e52cSAmir Goldstein 		/*map more blocks*/
545dae1e52cSAmir Goldstein 		while (count < map->m_len && count <= blocks_to_boundary) {
546dae1e52cSAmir Goldstein 			ext4_fsblk_t blk;
547dae1e52cSAmir Goldstein 
548dae1e52cSAmir Goldstein 			blk = le32_to_cpu(*(chain[depth-1].p + count));
549dae1e52cSAmir Goldstein 
550dae1e52cSAmir Goldstein 			if (blk == first_block + count)
551dae1e52cSAmir Goldstein 				count++;
552dae1e52cSAmir Goldstein 			else
553dae1e52cSAmir Goldstein 				break;
554dae1e52cSAmir Goldstein 		}
555dae1e52cSAmir Goldstein 		goto got_it;
556dae1e52cSAmir Goldstein 	}
557dae1e52cSAmir Goldstein 
558*facab4d9SJan Kara 	/* Next simple case - plain lookup failed */
559*facab4d9SJan Kara 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
560*facab4d9SJan Kara 		unsigned epb = inode->i_sb->s_blocksize / sizeof(u32);
561*facab4d9SJan Kara 		int i;
562*facab4d9SJan Kara 
563*facab4d9SJan Kara 		/* Count number blocks in a subtree under 'partial' */
564*facab4d9SJan Kara 		count = 1;
565*facab4d9SJan Kara 		for (i = 0; partial + i != chain + depth - 1; i++)
566*facab4d9SJan Kara 			count *= epb;
567*facab4d9SJan Kara 		/* Fill in size of a hole we found */
568*facab4d9SJan Kara 		map->m_pblk = 0;
569*facab4d9SJan Kara 		map->m_len = min_t(unsigned int, map->m_len, count);
570*facab4d9SJan Kara 		goto cleanup;
571*facab4d9SJan Kara 	}
572*facab4d9SJan Kara 
573*facab4d9SJan Kara 	/* Failed read of indirect block */
574*facab4d9SJan Kara 	if (err == -EIO)
575dae1e52cSAmir Goldstein 		goto cleanup;
576dae1e52cSAmir Goldstein 
577dae1e52cSAmir Goldstein 	/*
578dae1e52cSAmir Goldstein 	 * Okay, we need to do block allocation.
579dae1e52cSAmir Goldstein 	*/
580e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(inode->i_sb)) {
581bab08ab9STheodore Ts'o 		EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
582bab08ab9STheodore Ts'o 				 "non-extent mapped inodes with bigalloc");
5836a797d27SDarrick J. Wong 		return -EFSCORRUPTED;
584bab08ab9STheodore Ts'o 	}
585bab08ab9STheodore Ts'o 
586a5211002STheodore Ts'o 	/* Set up for the direct block allocation */
587a5211002STheodore Ts'o 	memset(&ar, 0, sizeof(ar));
588a5211002STheodore Ts'o 	ar.inode = inode;
589a5211002STheodore Ts'o 	ar.logical = map->m_lblk;
590a5211002STheodore Ts'o 	if (S_ISREG(inode->i_mode))
591a5211002STheodore Ts'o 		ar.flags = EXT4_MB_HINT_DATA;
592e3cf5d5dSTheodore Ts'o 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
593e3cf5d5dSTheodore Ts'o 		ar.flags |= EXT4_MB_DELALLOC_RESERVED;
594c5e298aeSTheodore Ts'o 	if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
595c5e298aeSTheodore Ts'o 		ar.flags |= EXT4_MB_USE_RESERVED;
596a5211002STheodore Ts'o 
597a5211002STheodore Ts'o 	ar.goal = ext4_find_goal(inode, map->m_lblk, partial);
598dae1e52cSAmir Goldstein 
599dae1e52cSAmir Goldstein 	/* the number of blocks need to allocate for [d,t]indirect blocks */
600dae1e52cSAmir Goldstein 	indirect_blks = (chain + depth) - partial - 1;
601dae1e52cSAmir Goldstein 
602dae1e52cSAmir Goldstein 	/*
603dae1e52cSAmir Goldstein 	 * Next look up the indirect map to count the totoal number of
604dae1e52cSAmir Goldstein 	 * direct blocks to allocate for this branch.
605dae1e52cSAmir Goldstein 	 */
606a5211002STheodore Ts'o 	ar.len = ext4_blks_to_allocate(partial, indirect_blks,
607dae1e52cSAmir Goldstein 				       map->m_len, blocks_to_boundary);
608a5211002STheodore Ts'o 
609dae1e52cSAmir Goldstein 	/*
610dae1e52cSAmir Goldstein 	 * Block out ext4_truncate while we alter the tree
611dae1e52cSAmir Goldstein 	 */
612a5211002STheodore Ts'o 	err = ext4_alloc_branch(handle, &ar, indirect_blks,
613dae1e52cSAmir Goldstein 				offsets + (partial - chain), partial);
614dae1e52cSAmir Goldstein 
615dae1e52cSAmir Goldstein 	/*
616dae1e52cSAmir Goldstein 	 * The ext4_splice_branch call will free and forget any buffers
617dae1e52cSAmir Goldstein 	 * on the new chain if there is a failure, but that risks using
618dae1e52cSAmir Goldstein 	 * up transaction credits, especially for bitmaps where the
619dae1e52cSAmir Goldstein 	 * credits cannot be returned.  Can we handle this somehow?  We
620dae1e52cSAmir Goldstein 	 * may need to return -EAGAIN upwards in the worst case.  --sct
621dae1e52cSAmir Goldstein 	 */
622dae1e52cSAmir Goldstein 	if (!err)
623a5211002STheodore Ts'o 		err = ext4_splice_branch(handle, &ar, partial, indirect_blks);
624dae1e52cSAmir Goldstein 	if (err)
625dae1e52cSAmir Goldstein 		goto cleanup;
626dae1e52cSAmir Goldstein 
627dae1e52cSAmir Goldstein 	map->m_flags |= EXT4_MAP_NEW;
628dae1e52cSAmir Goldstein 
629dae1e52cSAmir Goldstein 	ext4_update_inode_fsync_trans(handle, inode, 1);
630a5211002STheodore Ts'o 	count = ar.len;
631dae1e52cSAmir Goldstein got_it:
632dae1e52cSAmir Goldstein 	map->m_flags |= EXT4_MAP_MAPPED;
633dae1e52cSAmir Goldstein 	map->m_pblk = le32_to_cpu(chain[depth-1].key);
634dae1e52cSAmir Goldstein 	map->m_len = count;
635dae1e52cSAmir Goldstein 	if (count > blocks_to_boundary)
636dae1e52cSAmir Goldstein 		map->m_flags |= EXT4_MAP_BOUNDARY;
637dae1e52cSAmir Goldstein 	err = count;
638dae1e52cSAmir Goldstein 	/* Clean up and exit */
639dae1e52cSAmir Goldstein 	partial = chain + depth - 1;	/* the whole chain */
640dae1e52cSAmir Goldstein cleanup:
641dae1e52cSAmir Goldstein 	while (partial > chain) {
642dae1e52cSAmir Goldstein 		BUFFER_TRACE(partial->bh, "call brelse");
643dae1e52cSAmir Goldstein 		brelse(partial->bh);
644dae1e52cSAmir Goldstein 		partial--;
645dae1e52cSAmir Goldstein 	}
646dae1e52cSAmir Goldstein out:
64721ddd568STheodore Ts'o 	trace_ext4_ind_map_blocks_exit(inode, flags, map, err);
648dae1e52cSAmir Goldstein 	return err;
649dae1e52cSAmir Goldstein }
650dae1e52cSAmir Goldstein 
651dae1e52cSAmir Goldstein /*
652dae1e52cSAmir Goldstein  * O_DIRECT for ext3 (or indirect map) based files
653dae1e52cSAmir Goldstein  *
654dae1e52cSAmir Goldstein  * If the O_DIRECT write will extend the file then add this inode to the
655dae1e52cSAmir Goldstein  * orphan list.  So recovery will truncate it back to the original size
656dae1e52cSAmir Goldstein  * if the machine crashes during the write.
657dae1e52cSAmir Goldstein  *
658dae1e52cSAmir Goldstein  * If the O_DIRECT write is intantiating holes inside i_size and the machine
659dae1e52cSAmir Goldstein  * crashes then stale disk data _may_ be exposed inside the file. But current
660dae1e52cSAmir Goldstein  * VFS code falls back into buffered path in that case so we are safe.
661dae1e52cSAmir Goldstein  */
6626f673763SOmar Sandoval ssize_t ext4_ind_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
6636f673763SOmar Sandoval 			   loff_t offset)
664dae1e52cSAmir Goldstein {
665dae1e52cSAmir Goldstein 	struct file *file = iocb->ki_filp;
666dae1e52cSAmir Goldstein 	struct inode *inode = file->f_mapping->host;
667dae1e52cSAmir Goldstein 	struct ext4_inode_info *ei = EXT4_I(inode);
668dae1e52cSAmir Goldstein 	handle_t *handle;
669dae1e52cSAmir Goldstein 	ssize_t ret;
670dae1e52cSAmir Goldstein 	int orphan = 0;
671a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
672dae1e52cSAmir Goldstein 	int retries = 0;
673dae1e52cSAmir Goldstein 
6746f673763SOmar Sandoval 	if (iov_iter_rw(iter) == WRITE) {
675dae1e52cSAmir Goldstein 		loff_t final_size = offset + count;
676dae1e52cSAmir Goldstein 
677dae1e52cSAmir Goldstein 		if (final_size > inode->i_size) {
678dae1e52cSAmir Goldstein 			/* Credits for sb + inode write */
6799924a92aSTheodore Ts'o 			handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
680dae1e52cSAmir Goldstein 			if (IS_ERR(handle)) {
681dae1e52cSAmir Goldstein 				ret = PTR_ERR(handle);
682dae1e52cSAmir Goldstein 				goto out;
683dae1e52cSAmir Goldstein 			}
684dae1e52cSAmir Goldstein 			ret = ext4_orphan_add(handle, inode);
685dae1e52cSAmir Goldstein 			if (ret) {
686dae1e52cSAmir Goldstein 				ext4_journal_stop(handle);
687dae1e52cSAmir Goldstein 				goto out;
688dae1e52cSAmir Goldstein 			}
689dae1e52cSAmir Goldstein 			orphan = 1;
690dae1e52cSAmir Goldstein 			ei->i_disksize = inode->i_size;
691dae1e52cSAmir Goldstein 			ext4_journal_stop(handle);
692dae1e52cSAmir Goldstein 		}
693dae1e52cSAmir Goldstein 	}
694dae1e52cSAmir Goldstein 
695dae1e52cSAmir Goldstein retry:
6966f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ && ext4_should_dioread_nolock(inode)) {
69717335dccSDmitry Monakhov 		/*
69817335dccSDmitry Monakhov 		 * Nolock dioread optimization may be dynamically disabled
69917335dccSDmitry Monakhov 		 * via ext4_inode_block_unlocked_dio(). Check inode's state
70017335dccSDmitry Monakhov 		 * while holding extra i_dio_count ref.
70117335dccSDmitry Monakhov 		 */
702fe0f07d0SJens Axboe 		inode_dio_begin(inode);
70317335dccSDmitry Monakhov 		smp_mb();
70417335dccSDmitry Monakhov 		if (unlikely(ext4_test_inode_state(inode,
70517335dccSDmitry Monakhov 						    EXT4_STATE_DIOREAD_LOCK))) {
706fe0f07d0SJens Axboe 			inode_dio_end(inode);
70717335dccSDmitry Monakhov 			goto locked;
70817335dccSDmitry Monakhov 		}
709923ae0ffSRoss Zwisler 		if (IS_DAX(inode))
710a95cd631SOmar Sandoval 			ret = dax_do_io(iocb, inode, iter, offset,
711705965bdSJan Kara 					ext4_dio_get_block, NULL, 0);
712923ae0ffSRoss Zwisler 		else
71317f8c842SOmar Sandoval 			ret = __blockdev_direct_IO(iocb, inode,
71417f8c842SOmar Sandoval 						   inode->i_sb->s_bdev, iter,
715705965bdSJan Kara 						   offset, ext4_dio_get_block,
716705965bdSJan Kara 						   NULL, NULL, 0);
717fe0f07d0SJens Axboe 		inode_dio_end(inode);
718dccaf33fSJiaying Zhang 	} else {
71917335dccSDmitry Monakhov locked:
720923ae0ffSRoss Zwisler 		if (IS_DAX(inode))
721a95cd631SOmar Sandoval 			ret = dax_do_io(iocb, inode, iter, offset,
722705965bdSJan Kara 					ext4_dio_get_block, NULL, DIO_LOCKING);
723923ae0ffSRoss Zwisler 		else
72417f8c842SOmar Sandoval 			ret = blockdev_direct_IO(iocb, inode, iter, offset,
725705965bdSJan Kara 						 ext4_dio_get_block);
726dae1e52cSAmir Goldstein 
7276f673763SOmar Sandoval 		if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
728dae1e52cSAmir Goldstein 			loff_t isize = i_size_read(inode);
72916b1f05dSAl Viro 			loff_t end = offset + count;
730dae1e52cSAmir Goldstein 
731dae1e52cSAmir Goldstein 			if (end > isize)
732dae1e52cSAmir Goldstein 				ext4_truncate_failed_write(inode);
733dae1e52cSAmir Goldstein 		}
734dae1e52cSAmir Goldstein 	}
735dae1e52cSAmir Goldstein 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
736dae1e52cSAmir Goldstein 		goto retry;
737dae1e52cSAmir Goldstein 
738dae1e52cSAmir Goldstein 	if (orphan) {
739dae1e52cSAmir Goldstein 		int err;
740dae1e52cSAmir Goldstein 
741dae1e52cSAmir Goldstein 		/* Credits for sb + inode write */
7429924a92aSTheodore Ts'o 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
743dae1e52cSAmir Goldstein 		if (IS_ERR(handle)) {
744dae1e52cSAmir Goldstein 			/* This is really bad luck. We've written the data
745dae1e52cSAmir Goldstein 			 * but cannot extend i_size. Bail out and pretend
746dae1e52cSAmir Goldstein 			 * the write failed... */
747dae1e52cSAmir Goldstein 			ret = PTR_ERR(handle);
748dae1e52cSAmir Goldstein 			if (inode->i_nlink)
749dae1e52cSAmir Goldstein 				ext4_orphan_del(NULL, inode);
750dae1e52cSAmir Goldstein 
751dae1e52cSAmir Goldstein 			goto out;
752dae1e52cSAmir Goldstein 		}
753dae1e52cSAmir Goldstein 		if (inode->i_nlink)
754dae1e52cSAmir Goldstein 			ext4_orphan_del(handle, inode);
755dae1e52cSAmir Goldstein 		if (ret > 0) {
756dae1e52cSAmir Goldstein 			loff_t end = offset + ret;
757dae1e52cSAmir Goldstein 			if (end > inode->i_size) {
758dae1e52cSAmir Goldstein 				ei->i_disksize = end;
759dae1e52cSAmir Goldstein 				i_size_write(inode, end);
760dae1e52cSAmir Goldstein 				/*
761dae1e52cSAmir Goldstein 				 * We're going to return a positive `ret'
762dae1e52cSAmir Goldstein 				 * here due to non-zero-length I/O, so there's
763dae1e52cSAmir Goldstein 				 * no way of reporting error returns from
764dae1e52cSAmir Goldstein 				 * ext4_mark_inode_dirty() to userspace.  So
765dae1e52cSAmir Goldstein 				 * ignore it.
766dae1e52cSAmir Goldstein 				 */
767dae1e52cSAmir Goldstein 				ext4_mark_inode_dirty(handle, inode);
768dae1e52cSAmir Goldstein 			}
769dae1e52cSAmir Goldstein 		}
770dae1e52cSAmir Goldstein 		err = ext4_journal_stop(handle);
771dae1e52cSAmir Goldstein 		if (ret == 0)
772dae1e52cSAmir Goldstein 			ret = err;
773dae1e52cSAmir Goldstein 	}
774dae1e52cSAmir Goldstein out:
775dae1e52cSAmir Goldstein 	return ret;
776dae1e52cSAmir Goldstein }
777dae1e52cSAmir Goldstein 
778dae1e52cSAmir Goldstein /*
779dae1e52cSAmir Goldstein  * Calculate the number of metadata blocks need to reserve
780dae1e52cSAmir Goldstein  * to allocate a new block at @lblocks for non extent file based file
781dae1e52cSAmir Goldstein  */
782dae1e52cSAmir Goldstein int ext4_ind_calc_metadata_amount(struct inode *inode, sector_t lblock)
783dae1e52cSAmir Goldstein {
784dae1e52cSAmir Goldstein 	struct ext4_inode_info *ei = EXT4_I(inode);
785dae1e52cSAmir Goldstein 	sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1);
786dae1e52cSAmir Goldstein 	int blk_bits;
787dae1e52cSAmir Goldstein 
788dae1e52cSAmir Goldstein 	if (lblock < EXT4_NDIR_BLOCKS)
789dae1e52cSAmir Goldstein 		return 0;
790dae1e52cSAmir Goldstein 
791dae1e52cSAmir Goldstein 	lblock -= EXT4_NDIR_BLOCKS;
792dae1e52cSAmir Goldstein 
793dae1e52cSAmir Goldstein 	if (ei->i_da_metadata_calc_len &&
794dae1e52cSAmir Goldstein 	    (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
795dae1e52cSAmir Goldstein 		ei->i_da_metadata_calc_len++;
796dae1e52cSAmir Goldstein 		return 0;
797dae1e52cSAmir Goldstein 	}
798dae1e52cSAmir Goldstein 	ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
799dae1e52cSAmir Goldstein 	ei->i_da_metadata_calc_len = 1;
800dae1e52cSAmir Goldstein 	blk_bits = order_base_2(lblock);
801dae1e52cSAmir Goldstein 	return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
802dae1e52cSAmir Goldstein }
803dae1e52cSAmir Goldstein 
804fa55a0edSJan Kara /*
805fa55a0edSJan Kara  * Calculate number of indirect blocks touched by mapping @nrblocks logically
806fa55a0edSJan Kara  * contiguous blocks
807fa55a0edSJan Kara  */
808fa55a0edSJan Kara int ext4_ind_trans_blocks(struct inode *inode, int nrblocks)
809dae1e52cSAmir Goldstein {
810dae1e52cSAmir Goldstein 	/*
811dae1e52cSAmir Goldstein 	 * With N contiguous data blocks, we need at most
812dae1e52cSAmir Goldstein 	 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
813dae1e52cSAmir Goldstein 	 * 2 dindirect blocks, and 1 tindirect block
814dae1e52cSAmir Goldstein 	 */
815fa55a0edSJan Kara 	return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
816dae1e52cSAmir Goldstein }
817dae1e52cSAmir Goldstein 
818dae1e52cSAmir Goldstein /*
819dae1e52cSAmir Goldstein  * Truncate transactions can be complex and absolutely huge.  So we need to
820dae1e52cSAmir Goldstein  * be able to restart the transaction at a conventient checkpoint to make
821dae1e52cSAmir Goldstein  * sure we don't overflow the journal.
822dae1e52cSAmir Goldstein  *
823819c4920STheodore Ts'o  * Try to extend this transaction for the purposes of truncation.  If
824dae1e52cSAmir Goldstein  * extend fails, we need to propagate the failure up and restart the
825dae1e52cSAmir Goldstein  * transaction in the top-level truncate loop. --sct
826dae1e52cSAmir Goldstein  *
827dae1e52cSAmir Goldstein  * Returns 0 if we managed to create more room.  If we can't create more
828dae1e52cSAmir Goldstein  * room, and the transaction must be restarted we return 1.
829dae1e52cSAmir Goldstein  */
830dae1e52cSAmir Goldstein static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
831dae1e52cSAmir Goldstein {
832dae1e52cSAmir Goldstein 	if (!ext4_handle_valid(handle))
833dae1e52cSAmir Goldstein 		return 0;
834dae1e52cSAmir Goldstein 	if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
835dae1e52cSAmir Goldstein 		return 0;
836dae1e52cSAmir Goldstein 	if (!ext4_journal_extend(handle, ext4_blocks_for_truncate(inode)))
837dae1e52cSAmir Goldstein 		return 0;
838dae1e52cSAmir Goldstein 	return 1;
839dae1e52cSAmir Goldstein }
840dae1e52cSAmir Goldstein 
841dae1e52cSAmir Goldstein /*
842dae1e52cSAmir Goldstein  * Probably it should be a library function... search for first non-zero word
843dae1e52cSAmir Goldstein  * or memcmp with zero_page, whatever is better for particular architecture.
844dae1e52cSAmir Goldstein  * Linus?
845dae1e52cSAmir Goldstein  */
846dae1e52cSAmir Goldstein static inline int all_zeroes(__le32 *p, __le32 *q)
847dae1e52cSAmir Goldstein {
848dae1e52cSAmir Goldstein 	while (p < q)
849dae1e52cSAmir Goldstein 		if (*p++)
850dae1e52cSAmir Goldstein 			return 0;
851dae1e52cSAmir Goldstein 	return 1;
852dae1e52cSAmir Goldstein }
853dae1e52cSAmir Goldstein 
854dae1e52cSAmir Goldstein /**
855dae1e52cSAmir Goldstein  *	ext4_find_shared - find the indirect blocks for partial truncation.
856dae1e52cSAmir Goldstein  *	@inode:	  inode in question
857dae1e52cSAmir Goldstein  *	@depth:	  depth of the affected branch
858dae1e52cSAmir Goldstein  *	@offsets: offsets of pointers in that branch (see ext4_block_to_path)
859dae1e52cSAmir Goldstein  *	@chain:	  place to store the pointers to partial indirect blocks
860dae1e52cSAmir Goldstein  *	@top:	  place to the (detached) top of branch
861dae1e52cSAmir Goldstein  *
862dae1e52cSAmir Goldstein  *	This is a helper function used by ext4_truncate().
863dae1e52cSAmir Goldstein  *
864dae1e52cSAmir Goldstein  *	When we do truncate() we may have to clean the ends of several
865dae1e52cSAmir Goldstein  *	indirect blocks but leave the blocks themselves alive. Block is
866dae1e52cSAmir Goldstein  *	partially truncated if some data below the new i_size is referred
867dae1e52cSAmir Goldstein  *	from it (and it is on the path to the first completely truncated
868dae1e52cSAmir Goldstein  *	data block, indeed).  We have to free the top of that path along
869dae1e52cSAmir Goldstein  *	with everything to the right of the path. Since no allocation
870dae1e52cSAmir Goldstein  *	past the truncation point is possible until ext4_truncate()
871dae1e52cSAmir Goldstein  *	finishes, we may safely do the latter, but top of branch may
872dae1e52cSAmir Goldstein  *	require special attention - pageout below the truncation point
873dae1e52cSAmir Goldstein  *	might try to populate it.
874dae1e52cSAmir Goldstein  *
875dae1e52cSAmir Goldstein  *	We atomically detach the top of branch from the tree, store the
876dae1e52cSAmir Goldstein  *	block number of its root in *@top, pointers to buffer_heads of
877dae1e52cSAmir Goldstein  *	partially truncated blocks - in @chain[].bh and pointers to
878dae1e52cSAmir Goldstein  *	their last elements that should not be removed - in
879dae1e52cSAmir Goldstein  *	@chain[].p. Return value is the pointer to last filled element
880dae1e52cSAmir Goldstein  *	of @chain.
881dae1e52cSAmir Goldstein  *
882dae1e52cSAmir Goldstein  *	The work left to caller to do the actual freeing of subtrees:
883dae1e52cSAmir Goldstein  *		a) free the subtree starting from *@top
884dae1e52cSAmir Goldstein  *		b) free the subtrees whose roots are stored in
885dae1e52cSAmir Goldstein  *			(@chain[i].p+1 .. end of @chain[i].bh->b_data)
886dae1e52cSAmir Goldstein  *		c) free the subtrees growing from the inode past the @chain[0].
887dae1e52cSAmir Goldstein  *			(no partially truncated stuff there).  */
888dae1e52cSAmir Goldstein 
889dae1e52cSAmir Goldstein static Indirect *ext4_find_shared(struct inode *inode, int depth,
890dae1e52cSAmir Goldstein 				  ext4_lblk_t offsets[4], Indirect chain[4],
891dae1e52cSAmir Goldstein 				  __le32 *top)
892dae1e52cSAmir Goldstein {
893dae1e52cSAmir Goldstein 	Indirect *partial, *p;
894dae1e52cSAmir Goldstein 	int k, err;
895dae1e52cSAmir Goldstein 
896dae1e52cSAmir Goldstein 	*top = 0;
897dae1e52cSAmir Goldstein 	/* Make k index the deepest non-null offset + 1 */
898dae1e52cSAmir Goldstein 	for (k = depth; k > 1 && !offsets[k-1]; k--)
899dae1e52cSAmir Goldstein 		;
900dae1e52cSAmir Goldstein 	partial = ext4_get_branch(inode, k, offsets, chain, &err);
901dae1e52cSAmir Goldstein 	/* Writer: pointers */
902dae1e52cSAmir Goldstein 	if (!partial)
903dae1e52cSAmir Goldstein 		partial = chain + k-1;
904dae1e52cSAmir Goldstein 	/*
905dae1e52cSAmir Goldstein 	 * If the branch acquired continuation since we've looked at it -
906dae1e52cSAmir Goldstein 	 * fine, it should all survive and (new) top doesn't belong to us.
907dae1e52cSAmir Goldstein 	 */
908dae1e52cSAmir Goldstein 	if (!partial->key && *partial->p)
909dae1e52cSAmir Goldstein 		/* Writer: end */
910dae1e52cSAmir Goldstein 		goto no_top;
911dae1e52cSAmir Goldstein 	for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
912dae1e52cSAmir Goldstein 		;
913dae1e52cSAmir Goldstein 	/*
914dae1e52cSAmir Goldstein 	 * OK, we've found the last block that must survive. The rest of our
915dae1e52cSAmir Goldstein 	 * branch should be detached before unlocking. However, if that rest
916dae1e52cSAmir Goldstein 	 * of branch is all ours and does not grow immediately from the inode
917dae1e52cSAmir Goldstein 	 * it's easier to cheat and just decrement partial->p.
918dae1e52cSAmir Goldstein 	 */
919dae1e52cSAmir Goldstein 	if (p == chain + k - 1 && p > chain) {
920dae1e52cSAmir Goldstein 		p->p--;
921dae1e52cSAmir Goldstein 	} else {
922dae1e52cSAmir Goldstein 		*top = *p->p;
923dae1e52cSAmir Goldstein 		/* Nope, don't do this in ext4.  Must leave the tree intact */
924dae1e52cSAmir Goldstein #if 0
925dae1e52cSAmir Goldstein 		*p->p = 0;
926dae1e52cSAmir Goldstein #endif
927dae1e52cSAmir Goldstein 	}
928dae1e52cSAmir Goldstein 	/* Writer: end */
929dae1e52cSAmir Goldstein 
930dae1e52cSAmir Goldstein 	while (partial > p) {
931dae1e52cSAmir Goldstein 		brelse(partial->bh);
932dae1e52cSAmir Goldstein 		partial--;
933dae1e52cSAmir Goldstein 	}
934dae1e52cSAmir Goldstein no_top:
935dae1e52cSAmir Goldstein 	return partial;
936dae1e52cSAmir Goldstein }
937dae1e52cSAmir Goldstein 
938dae1e52cSAmir Goldstein /*
939dae1e52cSAmir Goldstein  * Zero a number of block pointers in either an inode or an indirect block.
940dae1e52cSAmir Goldstein  * If we restart the transaction we must again get write access to the
941dae1e52cSAmir Goldstein  * indirect block for further modification.
942dae1e52cSAmir Goldstein  *
943dae1e52cSAmir Goldstein  * We release `count' blocks on disk, but (last - first) may be greater
944dae1e52cSAmir Goldstein  * than `count' because there can be holes in there.
945dae1e52cSAmir Goldstein  *
946dae1e52cSAmir Goldstein  * Return 0 on success, 1 on invalid block range
947dae1e52cSAmir Goldstein  * and < 0 on fatal error.
948dae1e52cSAmir Goldstein  */
949dae1e52cSAmir Goldstein static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
950dae1e52cSAmir Goldstein 			     struct buffer_head *bh,
951dae1e52cSAmir Goldstein 			     ext4_fsblk_t block_to_free,
952dae1e52cSAmir Goldstein 			     unsigned long count, __le32 *first,
953dae1e52cSAmir Goldstein 			     __le32 *last)
954dae1e52cSAmir Goldstein {
955dae1e52cSAmir Goldstein 	__le32 *p;
956981250caSTheodore Ts'o 	int	flags = EXT4_FREE_BLOCKS_VALIDATED;
957dae1e52cSAmir Goldstein 	int	err;
958dae1e52cSAmir Goldstein 
959dae1e52cSAmir Goldstein 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
960981250caSTheodore Ts'o 		flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
961981250caSTheodore Ts'o 	else if (ext4_should_journal_data(inode))
962981250caSTheodore Ts'o 		flags |= EXT4_FREE_BLOCKS_FORGET;
963dae1e52cSAmir Goldstein 
964dae1e52cSAmir Goldstein 	if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
965dae1e52cSAmir Goldstein 				   count)) {
966dae1e52cSAmir Goldstein 		EXT4_ERROR_INODE(inode, "attempt to clear invalid "
967dae1e52cSAmir Goldstein 				 "blocks %llu len %lu",
968dae1e52cSAmir Goldstein 				 (unsigned long long) block_to_free, count);
969dae1e52cSAmir Goldstein 		return 1;
970dae1e52cSAmir Goldstein 	}
971dae1e52cSAmir Goldstein 
972dae1e52cSAmir Goldstein 	if (try_to_extend_transaction(handle, inode)) {
973dae1e52cSAmir Goldstein 		if (bh) {
974dae1e52cSAmir Goldstein 			BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
975dae1e52cSAmir Goldstein 			err = ext4_handle_dirty_metadata(handle, inode, bh);
976dae1e52cSAmir Goldstein 			if (unlikely(err))
977dae1e52cSAmir Goldstein 				goto out_err;
978dae1e52cSAmir Goldstein 		}
979dae1e52cSAmir Goldstein 		err = ext4_mark_inode_dirty(handle, inode);
980dae1e52cSAmir Goldstein 		if (unlikely(err))
981dae1e52cSAmir Goldstein 			goto out_err;
982dae1e52cSAmir Goldstein 		err = ext4_truncate_restart_trans(handle, inode,
983dae1e52cSAmir Goldstein 					ext4_blocks_for_truncate(inode));
984dae1e52cSAmir Goldstein 		if (unlikely(err))
985dae1e52cSAmir Goldstein 			goto out_err;
986dae1e52cSAmir Goldstein 		if (bh) {
987dae1e52cSAmir Goldstein 			BUFFER_TRACE(bh, "retaking write access");
988dae1e52cSAmir Goldstein 			err = ext4_journal_get_write_access(handle, bh);
989dae1e52cSAmir Goldstein 			if (unlikely(err))
990dae1e52cSAmir Goldstein 				goto out_err;
991dae1e52cSAmir Goldstein 		}
992dae1e52cSAmir Goldstein 	}
993dae1e52cSAmir Goldstein 
994dae1e52cSAmir Goldstein 	for (p = first; p < last; p++)
995dae1e52cSAmir Goldstein 		*p = 0;
996dae1e52cSAmir Goldstein 
997dae1e52cSAmir Goldstein 	ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
998dae1e52cSAmir Goldstein 	return 0;
999dae1e52cSAmir Goldstein out_err:
1000dae1e52cSAmir Goldstein 	ext4_std_error(inode->i_sb, err);
1001dae1e52cSAmir Goldstein 	return err;
1002dae1e52cSAmir Goldstein }
1003dae1e52cSAmir Goldstein 
1004dae1e52cSAmir Goldstein /**
1005dae1e52cSAmir Goldstein  * ext4_free_data - free a list of data blocks
1006dae1e52cSAmir Goldstein  * @handle:	handle for this transaction
1007dae1e52cSAmir Goldstein  * @inode:	inode we are dealing with
1008dae1e52cSAmir Goldstein  * @this_bh:	indirect buffer_head which contains *@first and *@last
1009dae1e52cSAmir Goldstein  * @first:	array of block numbers
1010dae1e52cSAmir Goldstein  * @last:	points immediately past the end of array
1011dae1e52cSAmir Goldstein  *
1012dae1e52cSAmir Goldstein  * We are freeing all blocks referred from that array (numbers are stored as
1013dae1e52cSAmir Goldstein  * little-endian 32-bit) and updating @inode->i_blocks appropriately.
1014dae1e52cSAmir Goldstein  *
1015dae1e52cSAmir Goldstein  * We accumulate contiguous runs of blocks to free.  Conveniently, if these
1016dae1e52cSAmir Goldstein  * blocks are contiguous then releasing them at one time will only affect one
1017dae1e52cSAmir Goldstein  * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
1018dae1e52cSAmir Goldstein  * actually use a lot of journal space.
1019dae1e52cSAmir Goldstein  *
1020dae1e52cSAmir Goldstein  * @this_bh will be %NULL if @first and @last point into the inode's direct
1021dae1e52cSAmir Goldstein  * block pointers.
1022dae1e52cSAmir Goldstein  */
1023dae1e52cSAmir Goldstein static void ext4_free_data(handle_t *handle, struct inode *inode,
1024dae1e52cSAmir Goldstein 			   struct buffer_head *this_bh,
1025dae1e52cSAmir Goldstein 			   __le32 *first, __le32 *last)
1026dae1e52cSAmir Goldstein {
1027dae1e52cSAmir Goldstein 	ext4_fsblk_t block_to_free = 0;    /* Starting block # of a run */
1028dae1e52cSAmir Goldstein 	unsigned long count = 0;	    /* Number of blocks in the run */
1029dae1e52cSAmir Goldstein 	__le32 *block_to_free_p = NULL;	    /* Pointer into inode/ind
1030dae1e52cSAmir Goldstein 					       corresponding to
1031dae1e52cSAmir Goldstein 					       block_to_free */
1032dae1e52cSAmir Goldstein 	ext4_fsblk_t nr;		    /* Current block # */
1033dae1e52cSAmir Goldstein 	__le32 *p;			    /* Pointer into inode/ind
1034dae1e52cSAmir Goldstein 					       for current block */
1035dae1e52cSAmir Goldstein 	int err = 0;
1036dae1e52cSAmir Goldstein 
1037dae1e52cSAmir Goldstein 	if (this_bh) {				/* For indirect block */
1038dae1e52cSAmir Goldstein 		BUFFER_TRACE(this_bh, "get_write_access");
1039dae1e52cSAmir Goldstein 		err = ext4_journal_get_write_access(handle, this_bh);
1040dae1e52cSAmir Goldstein 		/* Important: if we can't update the indirect pointers
1041dae1e52cSAmir Goldstein 		 * to the blocks, we can't free them. */
1042dae1e52cSAmir Goldstein 		if (err)
1043dae1e52cSAmir Goldstein 			return;
1044dae1e52cSAmir Goldstein 	}
1045dae1e52cSAmir Goldstein 
1046dae1e52cSAmir Goldstein 	for (p = first; p < last; p++) {
1047dae1e52cSAmir Goldstein 		nr = le32_to_cpu(*p);
1048dae1e52cSAmir Goldstein 		if (nr) {
1049dae1e52cSAmir Goldstein 			/* accumulate blocks to free if they're contiguous */
1050dae1e52cSAmir Goldstein 			if (count == 0) {
1051dae1e52cSAmir Goldstein 				block_to_free = nr;
1052dae1e52cSAmir Goldstein 				block_to_free_p = p;
1053dae1e52cSAmir Goldstein 				count = 1;
1054dae1e52cSAmir Goldstein 			} else if (nr == block_to_free + count) {
1055dae1e52cSAmir Goldstein 				count++;
1056dae1e52cSAmir Goldstein 			} else {
1057dae1e52cSAmir Goldstein 				err = ext4_clear_blocks(handle, inode, this_bh,
1058dae1e52cSAmir Goldstein 						        block_to_free, count,
1059dae1e52cSAmir Goldstein 						        block_to_free_p, p);
1060dae1e52cSAmir Goldstein 				if (err)
1061dae1e52cSAmir Goldstein 					break;
1062dae1e52cSAmir Goldstein 				block_to_free = nr;
1063dae1e52cSAmir Goldstein 				block_to_free_p = p;
1064dae1e52cSAmir Goldstein 				count = 1;
1065dae1e52cSAmir Goldstein 			}
1066dae1e52cSAmir Goldstein 		}
1067dae1e52cSAmir Goldstein 	}
1068dae1e52cSAmir Goldstein 
1069dae1e52cSAmir Goldstein 	if (!err && count > 0)
1070dae1e52cSAmir Goldstein 		err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
1071dae1e52cSAmir Goldstein 					count, block_to_free_p, p);
1072dae1e52cSAmir Goldstein 	if (err < 0)
1073dae1e52cSAmir Goldstein 		/* fatal error */
1074dae1e52cSAmir Goldstein 		return;
1075dae1e52cSAmir Goldstein 
1076dae1e52cSAmir Goldstein 	if (this_bh) {
1077dae1e52cSAmir Goldstein 		BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
1078dae1e52cSAmir Goldstein 
1079dae1e52cSAmir Goldstein 		/*
1080dae1e52cSAmir Goldstein 		 * The buffer head should have an attached journal head at this
1081dae1e52cSAmir Goldstein 		 * point. However, if the data is corrupted and an indirect
1082dae1e52cSAmir Goldstein 		 * block pointed to itself, it would have been detached when
1083dae1e52cSAmir Goldstein 		 * the block was cleared. Check for this instead of OOPSing.
1084dae1e52cSAmir Goldstein 		 */
1085dae1e52cSAmir Goldstein 		if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
1086dae1e52cSAmir Goldstein 			ext4_handle_dirty_metadata(handle, inode, this_bh);
1087dae1e52cSAmir Goldstein 		else
1088dae1e52cSAmir Goldstein 			EXT4_ERROR_INODE(inode,
1089dae1e52cSAmir Goldstein 					 "circular indirect block detected at "
1090dae1e52cSAmir Goldstein 					 "block %llu",
1091dae1e52cSAmir Goldstein 				(unsigned long long) this_bh->b_blocknr);
1092dae1e52cSAmir Goldstein 	}
1093dae1e52cSAmir Goldstein }
1094dae1e52cSAmir Goldstein 
1095dae1e52cSAmir Goldstein /**
1096dae1e52cSAmir Goldstein  *	ext4_free_branches - free an array of branches
1097dae1e52cSAmir Goldstein  *	@handle: JBD handle for this transaction
1098dae1e52cSAmir Goldstein  *	@inode:	inode we are dealing with
1099dae1e52cSAmir Goldstein  *	@parent_bh: the buffer_head which contains *@first and *@last
1100dae1e52cSAmir Goldstein  *	@first:	array of block numbers
1101dae1e52cSAmir Goldstein  *	@last:	pointer immediately past the end of array
1102dae1e52cSAmir Goldstein  *	@depth:	depth of the branches to free
1103dae1e52cSAmir Goldstein  *
1104dae1e52cSAmir Goldstein  *	We are freeing all blocks referred from these branches (numbers are
1105dae1e52cSAmir Goldstein  *	stored as little-endian 32-bit) and updating @inode->i_blocks
1106dae1e52cSAmir Goldstein  *	appropriately.
1107dae1e52cSAmir Goldstein  */
1108dae1e52cSAmir Goldstein static void ext4_free_branches(handle_t *handle, struct inode *inode,
1109dae1e52cSAmir Goldstein 			       struct buffer_head *parent_bh,
1110dae1e52cSAmir Goldstein 			       __le32 *first, __le32 *last, int depth)
1111dae1e52cSAmir Goldstein {
1112dae1e52cSAmir Goldstein 	ext4_fsblk_t nr;
1113dae1e52cSAmir Goldstein 	__le32 *p;
1114dae1e52cSAmir Goldstein 
1115dae1e52cSAmir Goldstein 	if (ext4_handle_is_aborted(handle))
1116dae1e52cSAmir Goldstein 		return;
1117dae1e52cSAmir Goldstein 
1118dae1e52cSAmir Goldstein 	if (depth--) {
1119dae1e52cSAmir Goldstein 		struct buffer_head *bh;
1120dae1e52cSAmir Goldstein 		int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1121dae1e52cSAmir Goldstein 		p = last;
1122dae1e52cSAmir Goldstein 		while (--p >= first) {
1123dae1e52cSAmir Goldstein 			nr = le32_to_cpu(*p);
1124dae1e52cSAmir Goldstein 			if (!nr)
1125dae1e52cSAmir Goldstein 				continue;		/* A hole */
1126dae1e52cSAmir Goldstein 
1127dae1e52cSAmir Goldstein 			if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
1128dae1e52cSAmir Goldstein 						   nr, 1)) {
1129dae1e52cSAmir Goldstein 				EXT4_ERROR_INODE(inode,
1130dae1e52cSAmir Goldstein 						 "invalid indirect mapped "
1131dae1e52cSAmir Goldstein 						 "block %lu (level %d)",
1132dae1e52cSAmir Goldstein 						 (unsigned long) nr, depth);
1133dae1e52cSAmir Goldstein 				break;
1134dae1e52cSAmir Goldstein 			}
1135dae1e52cSAmir Goldstein 
1136dae1e52cSAmir Goldstein 			/* Go read the buffer for the next level down */
1137dae1e52cSAmir Goldstein 			bh = sb_bread(inode->i_sb, nr);
1138dae1e52cSAmir Goldstein 
1139dae1e52cSAmir Goldstein 			/*
1140dae1e52cSAmir Goldstein 			 * A read failure? Report error and clear slot
1141dae1e52cSAmir Goldstein 			 * (should be rare).
1142dae1e52cSAmir Goldstein 			 */
1143dae1e52cSAmir Goldstein 			if (!bh) {
1144dae1e52cSAmir Goldstein 				EXT4_ERROR_INODE_BLOCK(inode, nr,
1145dae1e52cSAmir Goldstein 						       "Read failure");
1146dae1e52cSAmir Goldstein 				continue;
1147dae1e52cSAmir Goldstein 			}
1148dae1e52cSAmir Goldstein 
1149dae1e52cSAmir Goldstein 			/* This zaps the entire block.  Bottom up. */
1150dae1e52cSAmir Goldstein 			BUFFER_TRACE(bh, "free child branches");
1151dae1e52cSAmir Goldstein 			ext4_free_branches(handle, inode, bh,
1152dae1e52cSAmir Goldstein 					(__le32 *) bh->b_data,
1153dae1e52cSAmir Goldstein 					(__le32 *) bh->b_data + addr_per_block,
1154dae1e52cSAmir Goldstein 					depth);
1155dae1e52cSAmir Goldstein 			brelse(bh);
1156dae1e52cSAmir Goldstein 
1157dae1e52cSAmir Goldstein 			/*
1158dae1e52cSAmir Goldstein 			 * Everything below this this pointer has been
1159dae1e52cSAmir Goldstein 			 * released.  Now let this top-of-subtree go.
1160dae1e52cSAmir Goldstein 			 *
1161dae1e52cSAmir Goldstein 			 * We want the freeing of this indirect block to be
1162dae1e52cSAmir Goldstein 			 * atomic in the journal with the updating of the
1163dae1e52cSAmir Goldstein 			 * bitmap block which owns it.  So make some room in
1164dae1e52cSAmir Goldstein 			 * the journal.
1165dae1e52cSAmir Goldstein 			 *
1166dae1e52cSAmir Goldstein 			 * We zero the parent pointer *after* freeing its
1167dae1e52cSAmir Goldstein 			 * pointee in the bitmaps, so if extend_transaction()
1168dae1e52cSAmir Goldstein 			 * for some reason fails to put the bitmap changes and
1169dae1e52cSAmir Goldstein 			 * the release into the same transaction, recovery
1170dae1e52cSAmir Goldstein 			 * will merely complain about releasing a free block,
1171dae1e52cSAmir Goldstein 			 * rather than leaking blocks.
1172dae1e52cSAmir Goldstein 			 */
1173dae1e52cSAmir Goldstein 			if (ext4_handle_is_aborted(handle))
1174dae1e52cSAmir Goldstein 				return;
1175dae1e52cSAmir Goldstein 			if (try_to_extend_transaction(handle, inode)) {
1176dae1e52cSAmir Goldstein 				ext4_mark_inode_dirty(handle, inode);
1177dae1e52cSAmir Goldstein 				ext4_truncate_restart_trans(handle, inode,
1178dae1e52cSAmir Goldstein 					    ext4_blocks_for_truncate(inode));
1179dae1e52cSAmir Goldstein 			}
1180dae1e52cSAmir Goldstein 
1181dae1e52cSAmir Goldstein 			/*
1182dae1e52cSAmir Goldstein 			 * The forget flag here is critical because if
1183dae1e52cSAmir Goldstein 			 * we are journaling (and not doing data
1184dae1e52cSAmir Goldstein 			 * journaling), we have to make sure a revoke
1185dae1e52cSAmir Goldstein 			 * record is written to prevent the journal
1186dae1e52cSAmir Goldstein 			 * replay from overwriting the (former)
1187dae1e52cSAmir Goldstein 			 * indirect block if it gets reallocated as a
1188dae1e52cSAmir Goldstein 			 * data block.  This must happen in the same
1189dae1e52cSAmir Goldstein 			 * transaction where the data blocks are
1190dae1e52cSAmir Goldstein 			 * actually freed.
1191dae1e52cSAmir Goldstein 			 */
1192dae1e52cSAmir Goldstein 			ext4_free_blocks(handle, inode, NULL, nr, 1,
1193dae1e52cSAmir Goldstein 					 EXT4_FREE_BLOCKS_METADATA|
1194dae1e52cSAmir Goldstein 					 EXT4_FREE_BLOCKS_FORGET);
1195dae1e52cSAmir Goldstein 
1196dae1e52cSAmir Goldstein 			if (parent_bh) {
1197dae1e52cSAmir Goldstein 				/*
1198dae1e52cSAmir Goldstein 				 * The block which we have just freed is
1199dae1e52cSAmir Goldstein 				 * pointed to by an indirect block: journal it
1200dae1e52cSAmir Goldstein 				 */
1201dae1e52cSAmir Goldstein 				BUFFER_TRACE(parent_bh, "get_write_access");
1202dae1e52cSAmir Goldstein 				if (!ext4_journal_get_write_access(handle,
1203dae1e52cSAmir Goldstein 								   parent_bh)){
1204dae1e52cSAmir Goldstein 					*p = 0;
1205dae1e52cSAmir Goldstein 					BUFFER_TRACE(parent_bh,
1206dae1e52cSAmir Goldstein 					"call ext4_handle_dirty_metadata");
1207dae1e52cSAmir Goldstein 					ext4_handle_dirty_metadata(handle,
1208dae1e52cSAmir Goldstein 								   inode,
1209dae1e52cSAmir Goldstein 								   parent_bh);
1210dae1e52cSAmir Goldstein 				}
1211dae1e52cSAmir Goldstein 			}
1212dae1e52cSAmir Goldstein 		}
1213dae1e52cSAmir Goldstein 	} else {
1214dae1e52cSAmir Goldstein 		/* We have reached the bottom of the tree. */
1215dae1e52cSAmir Goldstein 		BUFFER_TRACE(parent_bh, "free data blocks");
1216dae1e52cSAmir Goldstein 		ext4_free_data(handle, inode, parent_bh, first, last);
1217dae1e52cSAmir Goldstein 	}
1218dae1e52cSAmir Goldstein }
1219dae1e52cSAmir Goldstein 
1220819c4920STheodore Ts'o void ext4_ind_truncate(handle_t *handle, struct inode *inode)
1221dae1e52cSAmir Goldstein {
1222dae1e52cSAmir Goldstein 	struct ext4_inode_info *ei = EXT4_I(inode);
1223dae1e52cSAmir Goldstein 	__le32 *i_data = ei->i_data;
1224dae1e52cSAmir Goldstein 	int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1225dae1e52cSAmir Goldstein 	ext4_lblk_t offsets[4];
1226dae1e52cSAmir Goldstein 	Indirect chain[4];
1227dae1e52cSAmir Goldstein 	Indirect *partial;
1228dae1e52cSAmir Goldstein 	__le32 nr = 0;
1229dae1e52cSAmir Goldstein 	int n = 0;
1230dae1e52cSAmir Goldstein 	ext4_lblk_t last_block, max_block;
1231dae1e52cSAmir Goldstein 	unsigned blocksize = inode->i_sb->s_blocksize;
1232dae1e52cSAmir Goldstein 
1233dae1e52cSAmir Goldstein 	last_block = (inode->i_size + blocksize-1)
1234dae1e52cSAmir Goldstein 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1235dae1e52cSAmir Goldstein 	max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
1236dae1e52cSAmir Goldstein 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1237dae1e52cSAmir Goldstein 
1238dae1e52cSAmir Goldstein 	if (last_block != max_block) {
1239dae1e52cSAmir Goldstein 		n = ext4_block_to_path(inode, last_block, offsets, NULL);
1240dae1e52cSAmir Goldstein 		if (n == 0)
1241819c4920STheodore Ts'o 			return;
1242dae1e52cSAmir Goldstein 	}
1243dae1e52cSAmir Goldstein 
124451865fdaSZheng Liu 	ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
1245dae1e52cSAmir Goldstein 
1246dae1e52cSAmir Goldstein 	/*
1247dae1e52cSAmir Goldstein 	 * The orphan list entry will now protect us from any crash which
1248dae1e52cSAmir Goldstein 	 * occurs before the truncate completes, so it is now safe to propagate
1249dae1e52cSAmir Goldstein 	 * the new, shorter inode size (held for now in i_size) into the
1250dae1e52cSAmir Goldstein 	 * on-disk inode. We do this via i_disksize, which is the value which
1251dae1e52cSAmir Goldstein 	 * ext4 *really* writes onto the disk inode.
1252dae1e52cSAmir Goldstein 	 */
1253dae1e52cSAmir Goldstein 	ei->i_disksize = inode->i_size;
1254dae1e52cSAmir Goldstein 
1255dae1e52cSAmir Goldstein 	if (last_block == max_block) {
1256dae1e52cSAmir Goldstein 		/*
1257dae1e52cSAmir Goldstein 		 * It is unnecessary to free any data blocks if last_block is
1258dae1e52cSAmir Goldstein 		 * equal to the indirect block limit.
1259dae1e52cSAmir Goldstein 		 */
1260819c4920STheodore Ts'o 		return;
1261dae1e52cSAmir Goldstein 	} else if (n == 1) {		/* direct blocks */
1262dae1e52cSAmir Goldstein 		ext4_free_data(handle, inode, NULL, i_data+offsets[0],
1263dae1e52cSAmir Goldstein 			       i_data + EXT4_NDIR_BLOCKS);
1264dae1e52cSAmir Goldstein 		goto do_indirects;
1265dae1e52cSAmir Goldstein 	}
1266dae1e52cSAmir Goldstein 
1267dae1e52cSAmir Goldstein 	partial = ext4_find_shared(inode, n, offsets, chain, &nr);
1268dae1e52cSAmir Goldstein 	/* Kill the top of shared branch (not detached) */
1269dae1e52cSAmir Goldstein 	if (nr) {
1270dae1e52cSAmir Goldstein 		if (partial == chain) {
1271dae1e52cSAmir Goldstein 			/* Shared branch grows from the inode */
1272dae1e52cSAmir Goldstein 			ext4_free_branches(handle, inode, NULL,
1273dae1e52cSAmir Goldstein 					   &nr, &nr+1, (chain+n-1) - partial);
1274dae1e52cSAmir Goldstein 			*partial->p = 0;
1275dae1e52cSAmir Goldstein 			/*
1276dae1e52cSAmir Goldstein 			 * We mark the inode dirty prior to restart,
1277dae1e52cSAmir Goldstein 			 * and prior to stop.  No need for it here.
1278dae1e52cSAmir Goldstein 			 */
1279dae1e52cSAmir Goldstein 		} else {
1280dae1e52cSAmir Goldstein 			/* Shared branch grows from an indirect block */
1281dae1e52cSAmir Goldstein 			BUFFER_TRACE(partial->bh, "get_write_access");
1282dae1e52cSAmir Goldstein 			ext4_free_branches(handle, inode, partial->bh,
1283dae1e52cSAmir Goldstein 					partial->p,
1284dae1e52cSAmir Goldstein 					partial->p+1, (chain+n-1) - partial);
1285dae1e52cSAmir Goldstein 		}
1286dae1e52cSAmir Goldstein 	}
1287dae1e52cSAmir Goldstein 	/* Clear the ends of indirect blocks on the shared branch */
1288dae1e52cSAmir Goldstein 	while (partial > chain) {
1289dae1e52cSAmir Goldstein 		ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
1290dae1e52cSAmir Goldstein 				   (__le32*)partial->bh->b_data+addr_per_block,
1291dae1e52cSAmir Goldstein 				   (chain+n-1) - partial);
1292dae1e52cSAmir Goldstein 		BUFFER_TRACE(partial->bh, "call brelse");
1293dae1e52cSAmir Goldstein 		brelse(partial->bh);
1294dae1e52cSAmir Goldstein 		partial--;
1295dae1e52cSAmir Goldstein 	}
1296dae1e52cSAmir Goldstein do_indirects:
1297dae1e52cSAmir Goldstein 	/* Kill the remaining (whole) subtrees */
1298dae1e52cSAmir Goldstein 	switch (offsets[0]) {
1299dae1e52cSAmir Goldstein 	default:
1300dae1e52cSAmir Goldstein 		nr = i_data[EXT4_IND_BLOCK];
1301dae1e52cSAmir Goldstein 		if (nr) {
1302dae1e52cSAmir Goldstein 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
1303dae1e52cSAmir Goldstein 			i_data[EXT4_IND_BLOCK] = 0;
1304dae1e52cSAmir Goldstein 		}
1305dae1e52cSAmir Goldstein 	case EXT4_IND_BLOCK:
1306dae1e52cSAmir Goldstein 		nr = i_data[EXT4_DIND_BLOCK];
1307dae1e52cSAmir Goldstein 		if (nr) {
1308dae1e52cSAmir Goldstein 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
1309dae1e52cSAmir Goldstein 			i_data[EXT4_DIND_BLOCK] = 0;
1310dae1e52cSAmir Goldstein 		}
1311dae1e52cSAmir Goldstein 	case EXT4_DIND_BLOCK:
1312dae1e52cSAmir Goldstein 		nr = i_data[EXT4_TIND_BLOCK];
1313dae1e52cSAmir Goldstein 		if (nr) {
1314dae1e52cSAmir Goldstein 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
1315dae1e52cSAmir Goldstein 			i_data[EXT4_TIND_BLOCK] = 0;
1316dae1e52cSAmir Goldstein 		}
1317dae1e52cSAmir Goldstein 	case EXT4_TIND_BLOCK:
1318dae1e52cSAmir Goldstein 		;
1319dae1e52cSAmir Goldstein 	}
1320dae1e52cSAmir Goldstein }
1321dae1e52cSAmir Goldstein 
13224f579ae7SLukas Czerner /**
13234f579ae7SLukas Czerner  *	ext4_ind_remove_space - remove space from the range
13244f579ae7SLukas Czerner  *	@handle: JBD handle for this transaction
13254f579ae7SLukas Czerner  *	@inode:	inode we are dealing with
13264f579ae7SLukas Czerner  *	@start:	First block to remove
13274f579ae7SLukas Czerner  *	@end:	One block after the last block to remove (exclusive)
13284f579ae7SLukas Czerner  *
13294f579ae7SLukas Czerner  *	Free the blocks in the defined range (end is exclusive endpoint of
13304f579ae7SLukas Czerner  *	range). This is used by ext4_punch_hole().
13314f579ae7SLukas Czerner  */
13324f579ae7SLukas Czerner int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
13334f579ae7SLukas Czerner 			  ext4_lblk_t start, ext4_lblk_t end)
13348bad6fc8SZheng Liu {
13354f579ae7SLukas Czerner 	struct ext4_inode_info *ei = EXT4_I(inode);
13364f579ae7SLukas Czerner 	__le32 *i_data = ei->i_data;
13378bad6fc8SZheng Liu 	int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
13384f579ae7SLukas Czerner 	ext4_lblk_t offsets[4], offsets2[4];
13394f579ae7SLukas Czerner 	Indirect chain[4], chain2[4];
13404f579ae7SLukas Czerner 	Indirect *partial, *partial2;
13414f579ae7SLukas Czerner 	ext4_lblk_t max_block;
13424f579ae7SLukas Czerner 	__le32 nr = 0, nr2 = 0;
13434f579ae7SLukas Czerner 	int n = 0, n2 = 0;
13444f579ae7SLukas Czerner 	unsigned blocksize = inode->i_sb->s_blocksize;
13458bad6fc8SZheng Liu 
13464f579ae7SLukas Czerner 	max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
13474f579ae7SLukas Czerner 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
13484f579ae7SLukas Czerner 	if (end >= max_block)
13494f579ae7SLukas Czerner 		end = max_block;
13504f579ae7SLukas Czerner 	if ((start >= end) || (start > max_block))
13514f579ae7SLukas Czerner 		return 0;
1352a93cd4cfSJan Kara 
13534f579ae7SLukas Czerner 	n = ext4_block_to_path(inode, start, offsets, NULL);
13544f579ae7SLukas Czerner 	n2 = ext4_block_to_path(inode, end, offsets2, NULL);
13554f579ae7SLukas Czerner 
13564f579ae7SLukas Czerner 	BUG_ON(n > n2);
13574f579ae7SLukas Czerner 
13584f579ae7SLukas Czerner 	if ((n == 1) && (n == n2)) {
13594f579ae7SLukas Czerner 		/* We're punching only within direct block range */
13604f579ae7SLukas Czerner 		ext4_free_data(handle, inode, NULL, i_data + offsets[0],
13614f579ae7SLukas Czerner 			       i_data + offsets2[0]);
13624f579ae7SLukas Czerner 		return 0;
13634f579ae7SLukas Czerner 	} else if (n2 > n) {
13644f579ae7SLukas Czerner 		/*
13654f579ae7SLukas Czerner 		 * Start and end are on a different levels so we're going to
13664f579ae7SLukas Czerner 		 * free partial block at start, and partial block at end of
13674f579ae7SLukas Czerner 		 * the range. If there are some levels in between then
13684f579ae7SLukas Czerner 		 * do_indirects label will take care of that.
13694f579ae7SLukas Czerner 		 */
13704f579ae7SLukas Czerner 
13714f579ae7SLukas Czerner 		if (n == 1) {
13724f579ae7SLukas Czerner 			/*
13734f579ae7SLukas Czerner 			 * Start is at the direct block level, free
13744f579ae7SLukas Czerner 			 * everything to the end of the level.
13754f579ae7SLukas Czerner 			 */
13764f579ae7SLukas Czerner 			ext4_free_data(handle, inode, NULL, i_data + offsets[0],
13774f579ae7SLukas Czerner 				       i_data + EXT4_NDIR_BLOCKS);
13784f579ae7SLukas Czerner 			goto end_range;
13798bad6fc8SZheng Liu 		}
13804f579ae7SLukas Czerner 
13814f579ae7SLukas Czerner 
13824f579ae7SLukas Czerner 		partial = ext4_find_shared(inode, n, offsets, chain, &nr);
13834f579ae7SLukas Czerner 		if (nr) {
13844f579ae7SLukas Czerner 			if (partial == chain) {
13854f579ae7SLukas Czerner 				/* Shared branch grows from the inode */
13864f579ae7SLukas Czerner 				ext4_free_branches(handle, inode, NULL,
13874f579ae7SLukas Czerner 					   &nr, &nr+1, (chain+n-1) - partial);
13884f579ae7SLukas Czerner 				*partial->p = 0;
1389a93cd4cfSJan Kara 			} else {
13904f579ae7SLukas Czerner 				/* Shared branch grows from an indirect block */
13914f579ae7SLukas Czerner 				BUFFER_TRACE(partial->bh, "get_write_access");
13924f579ae7SLukas Czerner 				ext4_free_branches(handle, inode, partial->bh,
13934f579ae7SLukas Czerner 					partial->p,
13944f579ae7SLukas Czerner 					partial->p+1, (chain+n-1) - partial);
1395a93cd4cfSJan Kara 			}
13968bad6fc8SZheng Liu 		}
13978bad6fc8SZheng Liu 
13984f579ae7SLukas Czerner 		/*
13994f579ae7SLukas Czerner 		 * Clear the ends of indirect blocks on the shared branch
14004f579ae7SLukas Czerner 		 * at the start of the range
14014f579ae7SLukas Czerner 		 */
14024f579ae7SLukas Czerner 		while (partial > chain) {
14034f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, partial->bh,
14044f579ae7SLukas Czerner 				partial->p + 1,
14054f579ae7SLukas Czerner 				(__le32 *)partial->bh->b_data+addr_per_block,
14064f579ae7SLukas Czerner 				(chain+n-1) - partial);
14074f579ae7SLukas Czerner 			BUFFER_TRACE(partial->bh, "call brelse");
14084f579ae7SLukas Czerner 			brelse(partial->bh);
14094f579ae7SLukas Czerner 			partial--;
14108bad6fc8SZheng Liu 		}
14118bad6fc8SZheng Liu 
14124f579ae7SLukas Czerner end_range:
14134f579ae7SLukas Czerner 		partial2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
14144f579ae7SLukas Czerner 		if (nr2) {
14154f579ae7SLukas Czerner 			if (partial2 == chain2) {
14164f579ae7SLukas Czerner 				/*
14174f579ae7SLukas Czerner 				 * Remember, end is exclusive so here we're at
14184f579ae7SLukas Czerner 				 * the start of the next level we're not going
14194f579ae7SLukas Czerner 				 * to free. Everything was covered by the start
14204f579ae7SLukas Czerner 				 * of the range.
14214f579ae7SLukas Czerner 				 */
14226f30b7e3SOmar Sandoval 				goto do_indirects;
14238bad6fc8SZheng Liu 			}
14244f579ae7SLukas Czerner 		} else {
14254f579ae7SLukas Czerner 			/*
14264f579ae7SLukas Czerner 			 * ext4_find_shared returns Indirect structure which
14274f579ae7SLukas Czerner 			 * points to the last element which should not be
14284f579ae7SLukas Czerner 			 * removed by truncate. But this is end of the range
14294f579ae7SLukas Czerner 			 * in punch_hole so we need to point to the next element
14304f579ae7SLukas Czerner 			 */
14314f579ae7SLukas Czerner 			partial2->p++;
14324f579ae7SLukas Czerner 		}
14334f579ae7SLukas Czerner 
14344f579ae7SLukas Czerner 		/*
14354f579ae7SLukas Czerner 		 * Clear the ends of indirect blocks on the shared branch
14364f579ae7SLukas Czerner 		 * at the end of the range
14374f579ae7SLukas Czerner 		 */
14384f579ae7SLukas Czerner 		while (partial2 > chain2) {
14394f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, partial2->bh,
14404f579ae7SLukas Czerner 					   (__le32 *)partial2->bh->b_data,
14414f579ae7SLukas Czerner 					   partial2->p,
14424f579ae7SLukas Czerner 					   (chain2+n2-1) - partial2);
14434f579ae7SLukas Czerner 			BUFFER_TRACE(partial2->bh, "call brelse");
14444f579ae7SLukas Czerner 			brelse(partial2->bh);
14454f579ae7SLukas Czerner 			partial2--;
14464f579ae7SLukas Czerner 		}
14474f579ae7SLukas Czerner 		goto do_indirects;
14484f579ae7SLukas Czerner 	}
14494f579ae7SLukas Czerner 
14504f579ae7SLukas Czerner 	/* Punch happened within the same level (n == n2) */
14514f579ae7SLukas Czerner 	partial = ext4_find_shared(inode, n, offsets, chain, &nr);
14524f579ae7SLukas Czerner 	partial2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
14536f30b7e3SOmar Sandoval 
14546f30b7e3SOmar Sandoval 	/* Free top, but only if partial2 isn't its subtree. */
14556f30b7e3SOmar Sandoval 	if (nr) {
14566f30b7e3SOmar Sandoval 		int level = min(partial - chain, partial2 - chain2);
14576f30b7e3SOmar Sandoval 		int i;
14586f30b7e3SOmar Sandoval 		int subtree = 1;
14596f30b7e3SOmar Sandoval 
14606f30b7e3SOmar Sandoval 		for (i = 0; i <= level; i++) {
14616f30b7e3SOmar Sandoval 			if (offsets[i] != offsets2[i]) {
14626f30b7e3SOmar Sandoval 				subtree = 0;
14636f30b7e3SOmar Sandoval 				break;
14646f30b7e3SOmar Sandoval 			}
14656f30b7e3SOmar Sandoval 		}
14666f30b7e3SOmar Sandoval 
14676f30b7e3SOmar Sandoval 		if (!subtree) {
14686f30b7e3SOmar Sandoval 			if (partial == chain) {
14696f30b7e3SOmar Sandoval 				/* Shared branch grows from the inode */
14706f30b7e3SOmar Sandoval 				ext4_free_branches(handle, inode, NULL,
14716f30b7e3SOmar Sandoval 						   &nr, &nr+1,
14726f30b7e3SOmar Sandoval 						   (chain+n-1) - partial);
14736f30b7e3SOmar Sandoval 				*partial->p = 0;
14746f30b7e3SOmar Sandoval 			} else {
14756f30b7e3SOmar Sandoval 				/* Shared branch grows from an indirect block */
14766f30b7e3SOmar Sandoval 				BUFFER_TRACE(partial->bh, "get_write_access");
14776f30b7e3SOmar Sandoval 				ext4_free_branches(handle, inode, partial->bh,
14786f30b7e3SOmar Sandoval 						   partial->p,
14796f30b7e3SOmar Sandoval 						   partial->p+1,
14806f30b7e3SOmar Sandoval 						   (chain+n-1) - partial);
14816f30b7e3SOmar Sandoval 			}
14826f30b7e3SOmar Sandoval 		}
14836f30b7e3SOmar Sandoval 	}
14846f30b7e3SOmar Sandoval 
14856f30b7e3SOmar Sandoval 	if (!nr2) {
14864f579ae7SLukas Czerner 		/*
14874f579ae7SLukas Czerner 		 * ext4_find_shared returns Indirect structure which
14884f579ae7SLukas Czerner 		 * points to the last element which should not be
14894f579ae7SLukas Czerner 		 * removed by truncate. But this is end of the range
14904f579ae7SLukas Czerner 		 * in punch_hole so we need to point to the next element
14914f579ae7SLukas Czerner 		 */
14924f579ae7SLukas Czerner 		partial2->p++;
14936f30b7e3SOmar Sandoval 	}
14946f30b7e3SOmar Sandoval 
14956f30b7e3SOmar Sandoval 	while (partial > chain || partial2 > chain2) {
14966f30b7e3SOmar Sandoval 		int depth = (chain+n-1) - partial;
14976f30b7e3SOmar Sandoval 		int depth2 = (chain2+n2-1) - partial2;
14986f30b7e3SOmar Sandoval 
14996f30b7e3SOmar Sandoval 		if (partial > chain && partial2 > chain2 &&
15006f30b7e3SOmar Sandoval 		    partial->bh->b_blocknr == partial2->bh->b_blocknr) {
15016f30b7e3SOmar Sandoval 			/*
15026f30b7e3SOmar Sandoval 			 * We've converged on the same block. Clear the range,
15036f30b7e3SOmar Sandoval 			 * then we're done.
15046f30b7e3SOmar Sandoval 			 */
15054f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, partial->bh,
15064f579ae7SLukas Czerner 					   partial->p + 1,
15074f579ae7SLukas Czerner 					   partial2->p,
15084f579ae7SLukas Czerner 					   (chain+n-1) - partial);
15094f579ae7SLukas Czerner 			BUFFER_TRACE(partial->bh, "call brelse");
15104f579ae7SLukas Czerner 			brelse(partial->bh);
15114f579ae7SLukas Czerner 			BUFFER_TRACE(partial2->bh, "call brelse");
15124f579ae7SLukas Czerner 			brelse(partial2->bh);
15134f579ae7SLukas Czerner 			return 0;
15144f579ae7SLukas Czerner 		}
15156f30b7e3SOmar Sandoval 
15164f579ae7SLukas Czerner 		/*
15176f30b7e3SOmar Sandoval 		 * The start and end partial branches may not be at the same
15186f30b7e3SOmar Sandoval 		 * level even though the punch happened within one level. So, we
15196f30b7e3SOmar Sandoval 		 * give them a chance to arrive at the same level, then walk
15206f30b7e3SOmar Sandoval 		 * them in step with each other until we converge on the same
15216f30b7e3SOmar Sandoval 		 * block.
15224f579ae7SLukas Czerner 		 */
15236f30b7e3SOmar Sandoval 		if (partial > chain && depth <= depth2) {
15244f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, partial->bh,
15254f579ae7SLukas Czerner 					   partial->p + 1,
15264f579ae7SLukas Czerner 					   (__le32 *)partial->bh->b_data+addr_per_block,
15274f579ae7SLukas Czerner 					   (chain+n-1) - partial);
15284f579ae7SLukas Czerner 			BUFFER_TRACE(partial->bh, "call brelse");
15294f579ae7SLukas Czerner 			brelse(partial->bh);
15304f579ae7SLukas Czerner 			partial--;
15314f579ae7SLukas Czerner 		}
15326f30b7e3SOmar Sandoval 		if (partial2 > chain2 && depth2 <= depth) {
15334f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, partial2->bh,
15344f579ae7SLukas Czerner 					   (__le32 *)partial2->bh->b_data,
15354f579ae7SLukas Czerner 					   partial2->p,
15366f30b7e3SOmar Sandoval 					   (chain2+n2-1) - partial2);
15374f579ae7SLukas Czerner 			BUFFER_TRACE(partial2->bh, "call brelse");
15384f579ae7SLukas Czerner 			brelse(partial2->bh);
15394f579ae7SLukas Czerner 			partial2--;
15408bad6fc8SZheng Liu 		}
15418bad6fc8SZheng Liu 	}
15426f30b7e3SOmar Sandoval 	return 0;
15438bad6fc8SZheng Liu 
15444f579ae7SLukas Czerner do_indirects:
15454f579ae7SLukas Czerner 	/* Kill the remaining (whole) subtrees */
15464f579ae7SLukas Czerner 	switch (offsets[0]) {
15474f579ae7SLukas Czerner 	default:
15484f579ae7SLukas Czerner 		if (++n >= n2)
15494f579ae7SLukas Czerner 			return 0;
15504f579ae7SLukas Czerner 		nr = i_data[EXT4_IND_BLOCK];
15514f579ae7SLukas Czerner 		if (nr) {
15524f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
15534f579ae7SLukas Czerner 			i_data[EXT4_IND_BLOCK] = 0;
15548bad6fc8SZheng Liu 		}
15554f579ae7SLukas Czerner 	case EXT4_IND_BLOCK:
15564f579ae7SLukas Czerner 		if (++n >= n2)
15574f579ae7SLukas Czerner 			return 0;
15584f579ae7SLukas Czerner 		nr = i_data[EXT4_DIND_BLOCK];
15594f579ae7SLukas Czerner 		if (nr) {
15604f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
15614f579ae7SLukas Czerner 			i_data[EXT4_DIND_BLOCK] = 0;
15624f579ae7SLukas Czerner 		}
15634f579ae7SLukas Czerner 	case EXT4_DIND_BLOCK:
15644f579ae7SLukas Czerner 		if (++n >= n2)
15654f579ae7SLukas Czerner 			return 0;
15664f579ae7SLukas Czerner 		nr = i_data[EXT4_TIND_BLOCK];
15674f579ae7SLukas Czerner 		if (nr) {
15684f579ae7SLukas Czerner 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
15694f579ae7SLukas Czerner 			i_data[EXT4_TIND_BLOCK] = 0;
15704f579ae7SLukas Czerner 		}
15714f579ae7SLukas Czerner 	case EXT4_TIND_BLOCK:
15724f579ae7SLukas Czerner 		;
15734f579ae7SLukas Czerner 	}
15744f579ae7SLukas Czerner 	return 0;
15754f579ae7SLukas Czerner }
1576