xref: /OK3568_Linux_fs/kernel/fs/ext4/indirect.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  linux/fs/ext4/indirect.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  from
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  linux/fs/ext4/inode.c
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (C) 1992, 1993, 1994, 1995
10*4882a593Smuzhiyun  * Remy Card (card@masi.ibp.fr)
11*4882a593Smuzhiyun  * Laboratoire MASI - Institut Blaise Pascal
12*4882a593Smuzhiyun  * Universite Pierre et Marie Curie (Paris VI)
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *  from
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  linux/fs/minix/inode.c
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *  Copyright (C) 1991, 1992  Linus Torvalds
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *  Goal-directed block allocation by Stephen Tweedie
21*4882a593Smuzhiyun  *	(sct@redhat.com), 1993, 1998
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "ext4_jbd2.h"
25*4882a593Smuzhiyun #include "truncate.h"
26*4882a593Smuzhiyun #include <linux/dax.h>
27*4882a593Smuzhiyun #include <linux/uio.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <trace/events/ext4.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun typedef struct {
32*4882a593Smuzhiyun 	__le32	*p;
33*4882a593Smuzhiyun 	__le32	key;
34*4882a593Smuzhiyun 	struct buffer_head *bh;
35*4882a593Smuzhiyun } Indirect;
36*4882a593Smuzhiyun 
add_chain(Indirect * p,struct buffer_head * bh,__le32 * v)37*4882a593Smuzhiyun static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	p->key = *(p->p = v);
40*4882a593Smuzhiyun 	p->bh = bh;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun  *	ext4_block_to_path - parse the block number into array of offsets
45*4882a593Smuzhiyun  *	@inode: inode in question (we are only interested in its superblock)
46*4882a593Smuzhiyun  *	@i_block: block number to be parsed
47*4882a593Smuzhiyun  *	@offsets: array to store the offsets in
48*4882a593Smuzhiyun  *	@boundary: set this non-zero if the referred-to block is likely to be
49*4882a593Smuzhiyun  *	       followed (on disk) by an indirect block.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  *	To store the locations of file's data ext4 uses a data structure common
52*4882a593Smuzhiyun  *	for UNIX filesystems - tree of pointers anchored in the inode, with
53*4882a593Smuzhiyun  *	data blocks at leaves and indirect blocks in intermediate nodes.
54*4882a593Smuzhiyun  *	This function translates the block number into path in that tree -
55*4882a593Smuzhiyun  *	return value is the path length and @offsets[n] is the offset of
56*4882a593Smuzhiyun  *	pointer to (n+1)th node in the nth one. If @block is out of range
57*4882a593Smuzhiyun  *	(negative or too large) warning is printed and zero returned.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  *	Note: function doesn't find node addresses, so no IO is needed. All
60*4882a593Smuzhiyun  *	we need to know is the capacity of indirect blocks (taken from the
61*4882a593Smuzhiyun  *	inode->i_sb).
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * Portability note: the last comparison (check that we fit into triple
66*4882a593Smuzhiyun  * indirect block) is spelled differently, because otherwise on an
67*4882a593Smuzhiyun  * architecture with 32-bit longs and 8Kb pages we might get into trouble
68*4882a593Smuzhiyun  * if our filesystem had 8Kb blocks. We might use long long, but that would
69*4882a593Smuzhiyun  * kill us on x86. Oh, well, at least the sign propagation does not matter -
70*4882a593Smuzhiyun  * i_block would have to be negative in the very beginning, so we would not
71*4882a593Smuzhiyun  * get there at all.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun 
ext4_block_to_path(struct inode * inode,ext4_lblk_t i_block,ext4_lblk_t offsets[4],int * boundary)74*4882a593Smuzhiyun static int ext4_block_to_path(struct inode *inode,
75*4882a593Smuzhiyun 			      ext4_lblk_t i_block,
76*4882a593Smuzhiyun 			      ext4_lblk_t offsets[4], int *boundary)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
79*4882a593Smuzhiyun 	int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
80*4882a593Smuzhiyun 	const long direct_blocks = EXT4_NDIR_BLOCKS,
81*4882a593Smuzhiyun 		indirect_blocks = ptrs,
82*4882a593Smuzhiyun 		double_blocks = (1 << (ptrs_bits * 2));
83*4882a593Smuzhiyun 	int n = 0;
84*4882a593Smuzhiyun 	int final = 0;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	if (i_block < direct_blocks) {
87*4882a593Smuzhiyun 		offsets[n++] = i_block;
88*4882a593Smuzhiyun 		final = direct_blocks;
89*4882a593Smuzhiyun 	} else if ((i_block -= direct_blocks) < indirect_blocks) {
90*4882a593Smuzhiyun 		offsets[n++] = EXT4_IND_BLOCK;
91*4882a593Smuzhiyun 		offsets[n++] = i_block;
92*4882a593Smuzhiyun 		final = ptrs;
93*4882a593Smuzhiyun 	} else if ((i_block -= indirect_blocks) < double_blocks) {
94*4882a593Smuzhiyun 		offsets[n++] = EXT4_DIND_BLOCK;
95*4882a593Smuzhiyun 		offsets[n++] = i_block >> ptrs_bits;
96*4882a593Smuzhiyun 		offsets[n++] = i_block & (ptrs - 1);
97*4882a593Smuzhiyun 		final = ptrs;
98*4882a593Smuzhiyun 	} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
99*4882a593Smuzhiyun 		offsets[n++] = EXT4_TIND_BLOCK;
100*4882a593Smuzhiyun 		offsets[n++] = i_block >> (ptrs_bits * 2);
101*4882a593Smuzhiyun 		offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
102*4882a593Smuzhiyun 		offsets[n++] = i_block & (ptrs - 1);
103*4882a593Smuzhiyun 		final = ptrs;
104*4882a593Smuzhiyun 	} else {
105*4882a593Smuzhiyun 		ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
106*4882a593Smuzhiyun 			     i_block + direct_blocks +
107*4882a593Smuzhiyun 			     indirect_blocks + double_blocks, inode->i_ino);
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 	if (boundary)
110*4882a593Smuzhiyun 		*boundary = final - 1 - (i_block & (ptrs - 1));
111*4882a593Smuzhiyun 	return n;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun  *	ext4_get_branch - read the chain of indirect blocks leading to data
116*4882a593Smuzhiyun  *	@inode: inode in question
117*4882a593Smuzhiyun  *	@depth: depth of the chain (1 - direct pointer, etc.)
118*4882a593Smuzhiyun  *	@offsets: offsets of pointers in inode/indirect blocks
119*4882a593Smuzhiyun  *	@chain: place to store the result
120*4882a593Smuzhiyun  *	@err: here we store the error value
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  *	Function fills the array of triples <key, p, bh> and returns %NULL
123*4882a593Smuzhiyun  *	if everything went OK or the pointer to the last filled triple
124*4882a593Smuzhiyun  *	(incomplete one) otherwise. Upon the return chain[i].key contains
125*4882a593Smuzhiyun  *	the number of (i+1)-th block in the chain (as it is stored in memory,
126*4882a593Smuzhiyun  *	i.e. little-endian 32-bit), chain[i].p contains the address of that
127*4882a593Smuzhiyun  *	number (it points into struct inode for i==0 and into the bh->b_data
128*4882a593Smuzhiyun  *	for i>0) and chain[i].bh points to the buffer_head of i-th indirect
129*4882a593Smuzhiyun  *	block for i>0 and NULL for i==0. In other words, it holds the block
130*4882a593Smuzhiyun  *	numbers of the chain, addresses they were taken from (and where we can
131*4882a593Smuzhiyun  *	verify that chain did not change) and buffer_heads hosting these
132*4882a593Smuzhiyun  *	numbers.
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  *	Function stops when it stumbles upon zero pointer (absent block)
135*4882a593Smuzhiyun  *		(pointer to last triple returned, *@err == 0)
136*4882a593Smuzhiyun  *	or when it gets an IO error reading an indirect block
137*4882a593Smuzhiyun  *		(ditto, *@err == -EIO)
138*4882a593Smuzhiyun  *	or when it reads all @depth-1 indirect blocks successfully and finds
139*4882a593Smuzhiyun  *	the whole chain, all way to the data (returns %NULL, *err == 0).
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  *      Need to be called with
142*4882a593Smuzhiyun  *      down_read(&EXT4_I(inode)->i_data_sem)
143*4882a593Smuzhiyun  */
ext4_get_branch(struct inode * inode,int depth,ext4_lblk_t * offsets,Indirect chain[4],int * err)144*4882a593Smuzhiyun static Indirect *ext4_get_branch(struct inode *inode, int depth,
145*4882a593Smuzhiyun 				 ext4_lblk_t  *offsets,
146*4882a593Smuzhiyun 				 Indirect chain[4], int *err)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
149*4882a593Smuzhiyun 	Indirect *p = chain;
150*4882a593Smuzhiyun 	struct buffer_head *bh;
151*4882a593Smuzhiyun 	int ret = -EIO;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	*err = 0;
154*4882a593Smuzhiyun 	/* i_data is not going away, no lock needed */
155*4882a593Smuzhiyun 	add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
156*4882a593Smuzhiyun 	if (!p->key)
157*4882a593Smuzhiyun 		goto no_block;
158*4882a593Smuzhiyun 	while (--depth) {
159*4882a593Smuzhiyun 		bh = sb_getblk(sb, le32_to_cpu(p->key));
160*4882a593Smuzhiyun 		if (unlikely(!bh)) {
161*4882a593Smuzhiyun 			ret = -ENOMEM;
162*4882a593Smuzhiyun 			goto failure;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		if (!bh_uptodate_or_lock(bh)) {
166*4882a593Smuzhiyun 			if (ext4_read_bh(bh, 0, NULL) < 0) {
167*4882a593Smuzhiyun 				put_bh(bh);
168*4882a593Smuzhiyun 				goto failure;
169*4882a593Smuzhiyun 			}
170*4882a593Smuzhiyun 			/* validate block references */
171*4882a593Smuzhiyun 			if (ext4_check_indirect_blockref(inode, bh)) {
172*4882a593Smuzhiyun 				put_bh(bh);
173*4882a593Smuzhiyun 				goto failure;
174*4882a593Smuzhiyun 			}
175*4882a593Smuzhiyun 		}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
178*4882a593Smuzhiyun 		/* Reader: end */
179*4882a593Smuzhiyun 		if (!p->key)
180*4882a593Smuzhiyun 			goto no_block;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 	return NULL;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun failure:
185*4882a593Smuzhiyun 	*err = ret;
186*4882a593Smuzhiyun no_block:
187*4882a593Smuzhiyun 	return p;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun  *	ext4_find_near - find a place for allocation with sufficient locality
192*4882a593Smuzhiyun  *	@inode: owner
193*4882a593Smuzhiyun  *	@ind: descriptor of indirect block.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  *	This function returns the preferred place for block allocation.
196*4882a593Smuzhiyun  *	It is used when heuristic for sequential allocation fails.
197*4882a593Smuzhiyun  *	Rules are:
198*4882a593Smuzhiyun  *	  + if there is a block to the left of our position - allocate near it.
199*4882a593Smuzhiyun  *	  + if pointer will live in indirect block - allocate near that block.
200*4882a593Smuzhiyun  *	  + if pointer will live in inode - allocate in the same
201*4882a593Smuzhiyun  *	    cylinder group.
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * In the latter case we colour the starting block by the callers PID to
204*4882a593Smuzhiyun  * prevent it from clashing with concurrent allocations for a different inode
205*4882a593Smuzhiyun  * in the same block group.   The PID is used here so that functionally related
206*4882a593Smuzhiyun  * files will be close-by on-disk.
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  *	Caller must make sure that @ind is valid and will stay that way.
209*4882a593Smuzhiyun  */
ext4_find_near(struct inode * inode,Indirect * ind)210*4882a593Smuzhiyun static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct ext4_inode_info *ei = EXT4_I(inode);
213*4882a593Smuzhiyun 	__le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
214*4882a593Smuzhiyun 	__le32 *p;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* Try to find previous block */
217*4882a593Smuzhiyun 	for (p = ind->p - 1; p >= start; p--) {
218*4882a593Smuzhiyun 		if (*p)
219*4882a593Smuzhiyun 			return le32_to_cpu(*p);
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* No such thing, so let's try location of indirect block */
223*4882a593Smuzhiyun 	if (ind->bh)
224*4882a593Smuzhiyun 		return ind->bh->b_blocknr;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/*
227*4882a593Smuzhiyun 	 * It is going to be referred to from the inode itself? OK, just put it
228*4882a593Smuzhiyun 	 * into the same cylinder group then.
229*4882a593Smuzhiyun 	 */
230*4882a593Smuzhiyun 	return ext4_inode_to_goal_block(inode);
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun /**
234*4882a593Smuzhiyun  *	ext4_find_goal - find a preferred place for allocation.
235*4882a593Smuzhiyun  *	@inode: owner
236*4882a593Smuzhiyun  *	@block:  block we want
237*4882a593Smuzhiyun  *	@partial: pointer to the last triple within a chain
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  *	Normally this function find the preferred place for block allocation,
240*4882a593Smuzhiyun  *	returns it.
241*4882a593Smuzhiyun  *	Because this is only used for non-extent files, we limit the block nr
242*4882a593Smuzhiyun  *	to 32 bits.
243*4882a593Smuzhiyun  */
ext4_find_goal(struct inode * inode,ext4_lblk_t block,Indirect * partial)244*4882a593Smuzhiyun static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
245*4882a593Smuzhiyun 				   Indirect *partial)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	ext4_fsblk_t goal;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/*
250*4882a593Smuzhiyun 	 * XXX need to get goal block from mballoc's data structures
251*4882a593Smuzhiyun 	 */
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	goal = ext4_find_near(inode, partial);
254*4882a593Smuzhiyun 	goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
255*4882a593Smuzhiyun 	return goal;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun /**
259*4882a593Smuzhiyun  *	ext4_blks_to_allocate - Look up the block map and count the number
260*4882a593Smuzhiyun  *	of direct blocks need to be allocated for the given branch.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  *	@branch: chain of indirect blocks
263*4882a593Smuzhiyun  *	@k: number of blocks need for indirect blocks
264*4882a593Smuzhiyun  *	@blks: number of data blocks to be mapped.
265*4882a593Smuzhiyun  *	@blocks_to_boundary:  the offset in the indirect block
266*4882a593Smuzhiyun  *
267*4882a593Smuzhiyun  *	return the total number of blocks to be allocate, including the
268*4882a593Smuzhiyun  *	direct and indirect blocks.
269*4882a593Smuzhiyun  */
ext4_blks_to_allocate(Indirect * branch,int k,unsigned int blks,int blocks_to_boundary)270*4882a593Smuzhiyun static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
271*4882a593Smuzhiyun 				 int blocks_to_boundary)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	unsigned int count = 0;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	/*
276*4882a593Smuzhiyun 	 * Simple case, [t,d]Indirect block(s) has not allocated yet
277*4882a593Smuzhiyun 	 * then it's clear blocks on that path have not allocated
278*4882a593Smuzhiyun 	 */
279*4882a593Smuzhiyun 	if (k > 0) {
280*4882a593Smuzhiyun 		/* right now we don't handle cross boundary allocation */
281*4882a593Smuzhiyun 		if (blks < blocks_to_boundary + 1)
282*4882a593Smuzhiyun 			count += blks;
283*4882a593Smuzhiyun 		else
284*4882a593Smuzhiyun 			count += blocks_to_boundary + 1;
285*4882a593Smuzhiyun 		return count;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	count++;
289*4882a593Smuzhiyun 	while (count < blks && count <= blocks_to_boundary &&
290*4882a593Smuzhiyun 		le32_to_cpu(*(branch[0].p + count)) == 0) {
291*4882a593Smuzhiyun 		count++;
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 	return count;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun /**
297*4882a593Smuzhiyun  * ext4_alloc_branch() - allocate and set up a chain of blocks
298*4882a593Smuzhiyun  * @handle: handle for this transaction
299*4882a593Smuzhiyun  * @ar: structure describing the allocation request
300*4882a593Smuzhiyun  * @indirect_blks: number of allocated indirect blocks
301*4882a593Smuzhiyun  * @offsets: offsets (in the blocks) to store the pointers to next.
302*4882a593Smuzhiyun  * @branch: place to store the chain in.
303*4882a593Smuzhiyun  *
304*4882a593Smuzhiyun  *	This function allocates blocks, zeroes out all but the last one,
305*4882a593Smuzhiyun  *	links them into chain and (if we are synchronous) writes them to disk.
306*4882a593Smuzhiyun  *	In other words, it prepares a branch that can be spliced onto the
307*4882a593Smuzhiyun  *	inode. It stores the information about that chain in the branch[], in
308*4882a593Smuzhiyun  *	the same format as ext4_get_branch() would do. We are calling it after
309*4882a593Smuzhiyun  *	we had read the existing part of chain and partial points to the last
310*4882a593Smuzhiyun  *	triple of that (one with zero ->key). Upon the exit we have the same
311*4882a593Smuzhiyun  *	picture as after the successful ext4_get_block(), except that in one
312*4882a593Smuzhiyun  *	place chain is disconnected - *branch->p is still zero (we did not
313*4882a593Smuzhiyun  *	set the last link), but branch->key contains the number that should
314*4882a593Smuzhiyun  *	be placed into *branch->p to fill that gap.
315*4882a593Smuzhiyun  *
316*4882a593Smuzhiyun  *	If allocation fails we free all blocks we've allocated (and forget
317*4882a593Smuzhiyun  *	their buffer_heads) and return the error value the from failed
318*4882a593Smuzhiyun  *	ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
319*4882a593Smuzhiyun  *	as described above and return 0.
320*4882a593Smuzhiyun  */
ext4_alloc_branch(handle_t * handle,struct ext4_allocation_request * ar,int indirect_blks,ext4_lblk_t * offsets,Indirect * branch)321*4882a593Smuzhiyun static int ext4_alloc_branch(handle_t *handle,
322*4882a593Smuzhiyun 			     struct ext4_allocation_request *ar,
323*4882a593Smuzhiyun 			     int indirect_blks, ext4_lblk_t *offsets,
324*4882a593Smuzhiyun 			     Indirect *branch)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct buffer_head *		bh;
327*4882a593Smuzhiyun 	ext4_fsblk_t			b, new_blocks[4];
328*4882a593Smuzhiyun 	__le32				*p;
329*4882a593Smuzhiyun 	int				i, j, err, len = 1;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	for (i = 0; i <= indirect_blks; i++) {
332*4882a593Smuzhiyun 		if (i == indirect_blks) {
333*4882a593Smuzhiyun 			new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err);
334*4882a593Smuzhiyun 		} else {
335*4882a593Smuzhiyun 			ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle,
336*4882a593Smuzhiyun 					ar->inode, ar->goal,
337*4882a593Smuzhiyun 					ar->flags & EXT4_MB_DELALLOC_RESERVED,
338*4882a593Smuzhiyun 					NULL, &err);
339*4882a593Smuzhiyun 			/* Simplify error cleanup... */
340*4882a593Smuzhiyun 			branch[i+1].bh = NULL;
341*4882a593Smuzhiyun 		}
342*4882a593Smuzhiyun 		if (err) {
343*4882a593Smuzhiyun 			i--;
344*4882a593Smuzhiyun 			goto failed;
345*4882a593Smuzhiyun 		}
346*4882a593Smuzhiyun 		branch[i].key = cpu_to_le32(new_blocks[i]);
347*4882a593Smuzhiyun 		if (i == 0)
348*4882a593Smuzhiyun 			continue;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 		bh = branch[i].bh = sb_getblk(ar->inode->i_sb, new_blocks[i-1]);
351*4882a593Smuzhiyun 		if (unlikely(!bh)) {
352*4882a593Smuzhiyun 			err = -ENOMEM;
353*4882a593Smuzhiyun 			goto failed;
354*4882a593Smuzhiyun 		}
355*4882a593Smuzhiyun 		lock_buffer(bh);
356*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "call get_create_access");
357*4882a593Smuzhiyun 		err = ext4_journal_get_create_access(handle, bh);
358*4882a593Smuzhiyun 		if (err) {
359*4882a593Smuzhiyun 			unlock_buffer(bh);
360*4882a593Smuzhiyun 			goto failed;
361*4882a593Smuzhiyun 		}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 		memset(bh->b_data, 0, bh->b_size);
364*4882a593Smuzhiyun 		p = branch[i].p = (__le32 *) bh->b_data + offsets[i];
365*4882a593Smuzhiyun 		b = new_blocks[i];
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		if (i == indirect_blks)
368*4882a593Smuzhiyun 			len = ar->len;
369*4882a593Smuzhiyun 		for (j = 0; j < len; j++)
370*4882a593Smuzhiyun 			*p++ = cpu_to_le32(b++);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "marking uptodate");
373*4882a593Smuzhiyun 		set_buffer_uptodate(bh);
374*4882a593Smuzhiyun 		unlock_buffer(bh);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
377*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, ar->inode, bh);
378*4882a593Smuzhiyun 		if (err)
379*4882a593Smuzhiyun 			goto failed;
380*4882a593Smuzhiyun 	}
381*4882a593Smuzhiyun 	return 0;
382*4882a593Smuzhiyun failed:
383*4882a593Smuzhiyun 	if (i == indirect_blks) {
384*4882a593Smuzhiyun 		/* Free data blocks */
385*4882a593Smuzhiyun 		ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i],
386*4882a593Smuzhiyun 				 ar->len, 0);
387*4882a593Smuzhiyun 		i--;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 	for (; i >= 0; i--) {
390*4882a593Smuzhiyun 		/*
391*4882a593Smuzhiyun 		 * We want to ext4_forget() only freshly allocated indirect
392*4882a593Smuzhiyun 		 * blocks. Buffer for new_blocks[i] is at branch[i+1].bh
393*4882a593Smuzhiyun 		 * (buffer at branch[0].bh is indirect block / inode already
394*4882a593Smuzhiyun 		 * existing before ext4_alloc_branch() was called). Also
395*4882a593Smuzhiyun 		 * because blocks are freshly allocated, we don't need to
396*4882a593Smuzhiyun 		 * revoke them which is why we don't set
397*4882a593Smuzhiyun 		 * EXT4_FREE_BLOCKS_METADATA.
398*4882a593Smuzhiyun 		 */
399*4882a593Smuzhiyun 		ext4_free_blocks(handle, ar->inode, branch[i+1].bh,
400*4882a593Smuzhiyun 				 new_blocks[i], 1,
401*4882a593Smuzhiyun 				 branch[i+1].bh ? EXT4_FREE_BLOCKS_FORGET : 0);
402*4882a593Smuzhiyun 	}
403*4882a593Smuzhiyun 	return err;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun  * ext4_splice_branch() - splice the allocated branch onto inode.
408*4882a593Smuzhiyun  * @handle: handle for this transaction
409*4882a593Smuzhiyun  * @ar: structure describing the allocation request
410*4882a593Smuzhiyun  * @where: location of missing link
411*4882a593Smuzhiyun  * @num:   number of indirect blocks we are adding
412*4882a593Smuzhiyun  *
413*4882a593Smuzhiyun  * This function fills the missing link and does all housekeeping needed in
414*4882a593Smuzhiyun  * inode (->i_blocks, etc.). In case of success we end up with the full
415*4882a593Smuzhiyun  * chain to new block and return 0.
416*4882a593Smuzhiyun  */
ext4_splice_branch(handle_t * handle,struct ext4_allocation_request * ar,Indirect * where,int num)417*4882a593Smuzhiyun static int ext4_splice_branch(handle_t *handle,
418*4882a593Smuzhiyun 			      struct ext4_allocation_request *ar,
419*4882a593Smuzhiyun 			      Indirect *where, int num)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	int i;
422*4882a593Smuzhiyun 	int err = 0;
423*4882a593Smuzhiyun 	ext4_fsblk_t current_block;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	/*
426*4882a593Smuzhiyun 	 * If we're splicing into a [td]indirect block (as opposed to the
427*4882a593Smuzhiyun 	 * inode) then we need to get write access to the [td]indirect block
428*4882a593Smuzhiyun 	 * before the splice.
429*4882a593Smuzhiyun 	 */
430*4882a593Smuzhiyun 	if (where->bh) {
431*4882a593Smuzhiyun 		BUFFER_TRACE(where->bh, "get_write_access");
432*4882a593Smuzhiyun 		err = ext4_journal_get_write_access(handle, where->bh);
433*4882a593Smuzhiyun 		if (err)
434*4882a593Smuzhiyun 			goto err_out;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 	/* That's it */
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	*where->p = where->key;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	/*
441*4882a593Smuzhiyun 	 * Update the host buffer_head or inode to point to more just allocated
442*4882a593Smuzhiyun 	 * direct blocks blocks
443*4882a593Smuzhiyun 	 */
444*4882a593Smuzhiyun 	if (num == 0 && ar->len > 1) {
445*4882a593Smuzhiyun 		current_block = le32_to_cpu(where->key) + 1;
446*4882a593Smuzhiyun 		for (i = 1; i < ar->len; i++)
447*4882a593Smuzhiyun 			*(where->p + i) = cpu_to_le32(current_block++);
448*4882a593Smuzhiyun 	}
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	/* We are done with atomic stuff, now do the rest of housekeeping */
451*4882a593Smuzhiyun 	/* had we spliced it onto indirect block? */
452*4882a593Smuzhiyun 	if (where->bh) {
453*4882a593Smuzhiyun 		/*
454*4882a593Smuzhiyun 		 * If we spliced it onto an indirect block, we haven't
455*4882a593Smuzhiyun 		 * altered the inode.  Note however that if it is being spliced
456*4882a593Smuzhiyun 		 * onto an indirect block at the very end of the file (the
457*4882a593Smuzhiyun 		 * file is growing) then we *will* alter the inode to reflect
458*4882a593Smuzhiyun 		 * the new i_size.  But that is not done here - it is done in
459*4882a593Smuzhiyun 		 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
460*4882a593Smuzhiyun 		 */
461*4882a593Smuzhiyun 		jbd_debug(5, "splicing indirect only\n");
462*4882a593Smuzhiyun 		BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
463*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, ar->inode, where->bh);
464*4882a593Smuzhiyun 		if (err)
465*4882a593Smuzhiyun 			goto err_out;
466*4882a593Smuzhiyun 	} else {
467*4882a593Smuzhiyun 		/*
468*4882a593Smuzhiyun 		 * OK, we spliced it into the inode itself on a direct block.
469*4882a593Smuzhiyun 		 */
470*4882a593Smuzhiyun 		err = ext4_mark_inode_dirty(handle, ar->inode);
471*4882a593Smuzhiyun 		if (unlikely(err))
472*4882a593Smuzhiyun 			goto err_out;
473*4882a593Smuzhiyun 		jbd_debug(5, "splicing direct\n");
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 	return err;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun err_out:
478*4882a593Smuzhiyun 	for (i = 1; i <= num; i++) {
479*4882a593Smuzhiyun 		/*
480*4882a593Smuzhiyun 		 * branch[i].bh is newly allocated, so there is no
481*4882a593Smuzhiyun 		 * need to revoke the block, which is why we don't
482*4882a593Smuzhiyun 		 * need to set EXT4_FREE_BLOCKS_METADATA.
483*4882a593Smuzhiyun 		 */
484*4882a593Smuzhiyun 		ext4_free_blocks(handle, ar->inode, where[i].bh, 0, 1,
485*4882a593Smuzhiyun 				 EXT4_FREE_BLOCKS_FORGET);
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun 	ext4_free_blocks(handle, ar->inode, NULL, le32_to_cpu(where[num].key),
488*4882a593Smuzhiyun 			 ar->len, 0);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	return err;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun /*
494*4882a593Smuzhiyun  * The ext4_ind_map_blocks() function handles non-extents inodes
495*4882a593Smuzhiyun  * (i.e., using the traditional indirect/double-indirect i_blocks
496*4882a593Smuzhiyun  * scheme) for ext4_map_blocks().
497*4882a593Smuzhiyun  *
498*4882a593Smuzhiyun  * Allocation strategy is simple: if we have to allocate something, we will
499*4882a593Smuzhiyun  * have to go the whole way to leaf. So let's do it before attaching anything
500*4882a593Smuzhiyun  * to tree, set linkage between the newborn blocks, write them if sync is
501*4882a593Smuzhiyun  * required, recheck the path, free and repeat if check fails, otherwise
502*4882a593Smuzhiyun  * set the last missing link (that will protect us from any truncate-generated
503*4882a593Smuzhiyun  * removals - all blocks on the path are immune now) and possibly force the
504*4882a593Smuzhiyun  * write on the parent block.
505*4882a593Smuzhiyun  * That has a nice additional property: no special recovery from the failed
506*4882a593Smuzhiyun  * allocations is needed - we simply release blocks and do not touch anything
507*4882a593Smuzhiyun  * reachable from inode.
508*4882a593Smuzhiyun  *
509*4882a593Smuzhiyun  * `handle' can be NULL if create == 0.
510*4882a593Smuzhiyun  *
511*4882a593Smuzhiyun  * return > 0, # of blocks mapped or allocated.
512*4882a593Smuzhiyun  * return = 0, if plain lookup failed.
513*4882a593Smuzhiyun  * return < 0, error case.
514*4882a593Smuzhiyun  *
515*4882a593Smuzhiyun  * The ext4_ind_get_blocks() function should be called with
516*4882a593Smuzhiyun  * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
517*4882a593Smuzhiyun  * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
518*4882a593Smuzhiyun  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
519*4882a593Smuzhiyun  * blocks.
520*4882a593Smuzhiyun  */
ext4_ind_map_blocks(handle_t * handle,struct inode * inode,struct ext4_map_blocks * map,int flags)521*4882a593Smuzhiyun int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
522*4882a593Smuzhiyun 			struct ext4_map_blocks *map,
523*4882a593Smuzhiyun 			int flags)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	struct ext4_allocation_request ar;
526*4882a593Smuzhiyun 	int err = -EIO;
527*4882a593Smuzhiyun 	ext4_lblk_t offsets[4];
528*4882a593Smuzhiyun 	Indirect chain[4];
529*4882a593Smuzhiyun 	Indirect *partial;
530*4882a593Smuzhiyun 	int indirect_blks;
531*4882a593Smuzhiyun 	int blocks_to_boundary = 0;
532*4882a593Smuzhiyun 	int depth;
533*4882a593Smuzhiyun 	int count = 0;
534*4882a593Smuzhiyun 	ext4_fsblk_t first_block = 0;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
537*4882a593Smuzhiyun 	J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
538*4882a593Smuzhiyun 	J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
539*4882a593Smuzhiyun 	depth = ext4_block_to_path(inode, map->m_lblk, offsets,
540*4882a593Smuzhiyun 				   &blocks_to_boundary);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	if (depth == 0)
543*4882a593Smuzhiyun 		goto out;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	partial = ext4_get_branch(inode, depth, offsets, chain, &err);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	/* Simplest case - block found, no allocation needed */
548*4882a593Smuzhiyun 	if (!partial) {
549*4882a593Smuzhiyun 		first_block = le32_to_cpu(chain[depth - 1].key);
550*4882a593Smuzhiyun 		count++;
551*4882a593Smuzhiyun 		/*map more blocks*/
552*4882a593Smuzhiyun 		while (count < map->m_len && count <= blocks_to_boundary) {
553*4882a593Smuzhiyun 			ext4_fsblk_t blk;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 			blk = le32_to_cpu(*(chain[depth-1].p + count));
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 			if (blk == first_block + count)
558*4882a593Smuzhiyun 				count++;
559*4882a593Smuzhiyun 			else
560*4882a593Smuzhiyun 				break;
561*4882a593Smuzhiyun 		}
562*4882a593Smuzhiyun 		goto got_it;
563*4882a593Smuzhiyun 	}
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* Next simple case - plain lookup failed */
566*4882a593Smuzhiyun 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
567*4882a593Smuzhiyun 		unsigned epb = inode->i_sb->s_blocksize / sizeof(u32);
568*4882a593Smuzhiyun 		int i;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		/*
571*4882a593Smuzhiyun 		 * Count number blocks in a subtree under 'partial'. At each
572*4882a593Smuzhiyun 		 * level we count number of complete empty subtrees beyond
573*4882a593Smuzhiyun 		 * current offset and then descend into the subtree only
574*4882a593Smuzhiyun 		 * partially beyond current offset.
575*4882a593Smuzhiyun 		 */
576*4882a593Smuzhiyun 		count = 0;
577*4882a593Smuzhiyun 		for (i = partial - chain + 1; i < depth; i++)
578*4882a593Smuzhiyun 			count = count * epb + (epb - offsets[i] - 1);
579*4882a593Smuzhiyun 		count++;
580*4882a593Smuzhiyun 		/* Fill in size of a hole we found */
581*4882a593Smuzhiyun 		map->m_pblk = 0;
582*4882a593Smuzhiyun 		map->m_len = min_t(unsigned int, map->m_len, count);
583*4882a593Smuzhiyun 		goto cleanup;
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	/* Failed read of indirect block */
587*4882a593Smuzhiyun 	if (err == -EIO)
588*4882a593Smuzhiyun 		goto cleanup;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	/*
591*4882a593Smuzhiyun 	 * Okay, we need to do block allocation.
592*4882a593Smuzhiyun 	*/
593*4882a593Smuzhiyun 	if (ext4_has_feature_bigalloc(inode->i_sb)) {
594*4882a593Smuzhiyun 		EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
595*4882a593Smuzhiyun 				 "non-extent mapped inodes with bigalloc");
596*4882a593Smuzhiyun 		err = -EFSCORRUPTED;
597*4882a593Smuzhiyun 		goto out;
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	/* Set up for the direct block allocation */
601*4882a593Smuzhiyun 	memset(&ar, 0, sizeof(ar));
602*4882a593Smuzhiyun 	ar.inode = inode;
603*4882a593Smuzhiyun 	ar.logical = map->m_lblk;
604*4882a593Smuzhiyun 	if (S_ISREG(inode->i_mode))
605*4882a593Smuzhiyun 		ar.flags = EXT4_MB_HINT_DATA;
606*4882a593Smuzhiyun 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
607*4882a593Smuzhiyun 		ar.flags |= EXT4_MB_DELALLOC_RESERVED;
608*4882a593Smuzhiyun 	if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
609*4882a593Smuzhiyun 		ar.flags |= EXT4_MB_USE_RESERVED;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	ar.goal = ext4_find_goal(inode, map->m_lblk, partial);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	/* the number of blocks need to allocate for [d,t]indirect blocks */
614*4882a593Smuzhiyun 	indirect_blks = (chain + depth) - partial - 1;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/*
617*4882a593Smuzhiyun 	 * Next look up the indirect map to count the totoal number of
618*4882a593Smuzhiyun 	 * direct blocks to allocate for this branch.
619*4882a593Smuzhiyun 	 */
620*4882a593Smuzhiyun 	ar.len = ext4_blks_to_allocate(partial, indirect_blks,
621*4882a593Smuzhiyun 				       map->m_len, blocks_to_boundary);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	/*
624*4882a593Smuzhiyun 	 * Block out ext4_truncate while we alter the tree
625*4882a593Smuzhiyun 	 */
626*4882a593Smuzhiyun 	err = ext4_alloc_branch(handle, &ar, indirect_blks,
627*4882a593Smuzhiyun 				offsets + (partial - chain), partial);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/*
630*4882a593Smuzhiyun 	 * The ext4_splice_branch call will free and forget any buffers
631*4882a593Smuzhiyun 	 * on the new chain if there is a failure, but that risks using
632*4882a593Smuzhiyun 	 * up transaction credits, especially for bitmaps where the
633*4882a593Smuzhiyun 	 * credits cannot be returned.  Can we handle this somehow?  We
634*4882a593Smuzhiyun 	 * may need to return -EAGAIN upwards in the worst case.  --sct
635*4882a593Smuzhiyun 	 */
636*4882a593Smuzhiyun 	if (!err)
637*4882a593Smuzhiyun 		err = ext4_splice_branch(handle, &ar, partial, indirect_blks);
638*4882a593Smuzhiyun 	if (err)
639*4882a593Smuzhiyun 		goto cleanup;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	map->m_flags |= EXT4_MAP_NEW;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	ext4_update_inode_fsync_trans(handle, inode, 1);
644*4882a593Smuzhiyun 	count = ar.len;
645*4882a593Smuzhiyun got_it:
646*4882a593Smuzhiyun 	map->m_flags |= EXT4_MAP_MAPPED;
647*4882a593Smuzhiyun 	map->m_pblk = le32_to_cpu(chain[depth-1].key);
648*4882a593Smuzhiyun 	map->m_len = count;
649*4882a593Smuzhiyun 	if (count > blocks_to_boundary)
650*4882a593Smuzhiyun 		map->m_flags |= EXT4_MAP_BOUNDARY;
651*4882a593Smuzhiyun 	err = count;
652*4882a593Smuzhiyun 	/* Clean up and exit */
653*4882a593Smuzhiyun 	partial = chain + depth - 1;	/* the whole chain */
654*4882a593Smuzhiyun cleanup:
655*4882a593Smuzhiyun 	while (partial > chain) {
656*4882a593Smuzhiyun 		BUFFER_TRACE(partial->bh, "call brelse");
657*4882a593Smuzhiyun 		brelse(partial->bh);
658*4882a593Smuzhiyun 		partial--;
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun out:
661*4882a593Smuzhiyun 	trace_ext4_ind_map_blocks_exit(inode, flags, map, err);
662*4882a593Smuzhiyun 	return err;
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun /*
666*4882a593Smuzhiyun  * Calculate number of indirect blocks touched by mapping @nrblocks logically
667*4882a593Smuzhiyun  * contiguous blocks
668*4882a593Smuzhiyun  */
ext4_ind_trans_blocks(struct inode * inode,int nrblocks)669*4882a593Smuzhiyun int ext4_ind_trans_blocks(struct inode *inode, int nrblocks)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	/*
672*4882a593Smuzhiyun 	 * With N contiguous data blocks, we need at most
673*4882a593Smuzhiyun 	 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
674*4882a593Smuzhiyun 	 * 2 dindirect blocks, and 1 tindirect block
675*4882a593Smuzhiyun 	 */
676*4882a593Smuzhiyun 	return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
ext4_ind_trunc_restart_fn(handle_t * handle,struct inode * inode,struct buffer_head * bh,int * dropped)679*4882a593Smuzhiyun static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode,
680*4882a593Smuzhiyun 				     struct buffer_head *bh, int *dropped)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun 	int err;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	if (bh) {
685*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
686*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, inode, bh);
687*4882a593Smuzhiyun 		if (unlikely(err))
688*4882a593Smuzhiyun 			return err;
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 	err = ext4_mark_inode_dirty(handle, inode);
691*4882a593Smuzhiyun 	if (unlikely(err))
692*4882a593Smuzhiyun 		return err;
693*4882a593Smuzhiyun 	/*
694*4882a593Smuzhiyun 	 * Drop i_data_sem to avoid deadlock with ext4_map_blocks.  At this
695*4882a593Smuzhiyun 	 * moment, get_block can be called only for blocks inside i_size since
696*4882a593Smuzhiyun 	 * page cache has been already dropped and writes are blocked by
697*4882a593Smuzhiyun 	 * i_mutex. So we can safely drop the i_data_sem here.
698*4882a593Smuzhiyun 	 */
699*4882a593Smuzhiyun 	BUG_ON(EXT4_JOURNAL(inode) == NULL);
700*4882a593Smuzhiyun 	ext4_discard_preallocations(inode, 0);
701*4882a593Smuzhiyun 	up_write(&EXT4_I(inode)->i_data_sem);
702*4882a593Smuzhiyun 	*dropped = 1;
703*4882a593Smuzhiyun 	return 0;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun /*
707*4882a593Smuzhiyun  * Truncate transactions can be complex and absolutely huge.  So we need to
708*4882a593Smuzhiyun  * be able to restart the transaction at a conventient checkpoint to make
709*4882a593Smuzhiyun  * sure we don't overflow the journal.
710*4882a593Smuzhiyun  *
711*4882a593Smuzhiyun  * Try to extend this transaction for the purposes of truncation.  If
712*4882a593Smuzhiyun  * extend fails, we restart transaction.
713*4882a593Smuzhiyun  */
ext4_ind_truncate_ensure_credits(handle_t * handle,struct inode * inode,struct buffer_head * bh,int revoke_creds)714*4882a593Smuzhiyun static int ext4_ind_truncate_ensure_credits(handle_t *handle,
715*4882a593Smuzhiyun 					    struct inode *inode,
716*4882a593Smuzhiyun 					    struct buffer_head *bh,
717*4882a593Smuzhiyun 					    int revoke_creds)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	int ret;
720*4882a593Smuzhiyun 	int dropped = 0;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	ret = ext4_journal_ensure_credits_fn(handle, EXT4_RESERVE_TRANS_BLOCKS,
723*4882a593Smuzhiyun 			ext4_blocks_for_truncate(inode), revoke_creds,
724*4882a593Smuzhiyun 			ext4_ind_trunc_restart_fn(handle, inode, bh, &dropped));
725*4882a593Smuzhiyun 	if (dropped)
726*4882a593Smuzhiyun 		down_write(&EXT4_I(inode)->i_data_sem);
727*4882a593Smuzhiyun 	if (ret <= 0)
728*4882a593Smuzhiyun 		return ret;
729*4882a593Smuzhiyun 	if (bh) {
730*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "retaking write access");
731*4882a593Smuzhiyun 		ret = ext4_journal_get_write_access(handle, bh);
732*4882a593Smuzhiyun 		if (unlikely(ret))
733*4882a593Smuzhiyun 			return ret;
734*4882a593Smuzhiyun 	}
735*4882a593Smuzhiyun 	return 0;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /*
739*4882a593Smuzhiyun  * Probably it should be a library function... search for first non-zero word
740*4882a593Smuzhiyun  * or memcmp with zero_page, whatever is better for particular architecture.
741*4882a593Smuzhiyun  * Linus?
742*4882a593Smuzhiyun  */
all_zeroes(__le32 * p,__le32 * q)743*4882a593Smuzhiyun static inline int all_zeroes(__le32 *p, __le32 *q)
744*4882a593Smuzhiyun {
745*4882a593Smuzhiyun 	while (p < q)
746*4882a593Smuzhiyun 		if (*p++)
747*4882a593Smuzhiyun 			return 0;
748*4882a593Smuzhiyun 	return 1;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun /**
752*4882a593Smuzhiyun  *	ext4_find_shared - find the indirect blocks for partial truncation.
753*4882a593Smuzhiyun  *	@inode:	  inode in question
754*4882a593Smuzhiyun  *	@depth:	  depth of the affected branch
755*4882a593Smuzhiyun  *	@offsets: offsets of pointers in that branch (see ext4_block_to_path)
756*4882a593Smuzhiyun  *	@chain:	  place to store the pointers to partial indirect blocks
757*4882a593Smuzhiyun  *	@top:	  place to the (detached) top of branch
758*4882a593Smuzhiyun  *
759*4882a593Smuzhiyun  *	This is a helper function used by ext4_truncate().
760*4882a593Smuzhiyun  *
761*4882a593Smuzhiyun  *	When we do truncate() we may have to clean the ends of several
762*4882a593Smuzhiyun  *	indirect blocks but leave the blocks themselves alive. Block is
763*4882a593Smuzhiyun  *	partially truncated if some data below the new i_size is referred
764*4882a593Smuzhiyun  *	from it (and it is on the path to the first completely truncated
765*4882a593Smuzhiyun  *	data block, indeed).  We have to free the top of that path along
766*4882a593Smuzhiyun  *	with everything to the right of the path. Since no allocation
767*4882a593Smuzhiyun  *	past the truncation point is possible until ext4_truncate()
768*4882a593Smuzhiyun  *	finishes, we may safely do the latter, but top of branch may
769*4882a593Smuzhiyun  *	require special attention - pageout below the truncation point
770*4882a593Smuzhiyun  *	might try to populate it.
771*4882a593Smuzhiyun  *
772*4882a593Smuzhiyun  *	We atomically detach the top of branch from the tree, store the
773*4882a593Smuzhiyun  *	block number of its root in *@top, pointers to buffer_heads of
774*4882a593Smuzhiyun  *	partially truncated blocks - in @chain[].bh and pointers to
775*4882a593Smuzhiyun  *	their last elements that should not be removed - in
776*4882a593Smuzhiyun  *	@chain[].p. Return value is the pointer to last filled element
777*4882a593Smuzhiyun  *	of @chain.
778*4882a593Smuzhiyun  *
779*4882a593Smuzhiyun  *	The work left to caller to do the actual freeing of subtrees:
780*4882a593Smuzhiyun  *		a) free the subtree starting from *@top
781*4882a593Smuzhiyun  *		b) free the subtrees whose roots are stored in
782*4882a593Smuzhiyun  *			(@chain[i].p+1 .. end of @chain[i].bh->b_data)
783*4882a593Smuzhiyun  *		c) free the subtrees growing from the inode past the @chain[0].
784*4882a593Smuzhiyun  *			(no partially truncated stuff there).  */
785*4882a593Smuzhiyun 
ext4_find_shared(struct inode * inode,int depth,ext4_lblk_t offsets[4],Indirect chain[4],__le32 * top)786*4882a593Smuzhiyun static Indirect *ext4_find_shared(struct inode *inode, int depth,
787*4882a593Smuzhiyun 				  ext4_lblk_t offsets[4], Indirect chain[4],
788*4882a593Smuzhiyun 				  __le32 *top)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun 	Indirect *partial, *p;
791*4882a593Smuzhiyun 	int k, err;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	*top = 0;
794*4882a593Smuzhiyun 	/* Make k index the deepest non-null offset + 1 */
795*4882a593Smuzhiyun 	for (k = depth; k > 1 && !offsets[k-1]; k--)
796*4882a593Smuzhiyun 		;
797*4882a593Smuzhiyun 	partial = ext4_get_branch(inode, k, offsets, chain, &err);
798*4882a593Smuzhiyun 	/* Writer: pointers */
799*4882a593Smuzhiyun 	if (!partial)
800*4882a593Smuzhiyun 		partial = chain + k-1;
801*4882a593Smuzhiyun 	/*
802*4882a593Smuzhiyun 	 * If the branch acquired continuation since we've looked at it -
803*4882a593Smuzhiyun 	 * fine, it should all survive and (new) top doesn't belong to us.
804*4882a593Smuzhiyun 	 */
805*4882a593Smuzhiyun 	if (!partial->key && *partial->p)
806*4882a593Smuzhiyun 		/* Writer: end */
807*4882a593Smuzhiyun 		goto no_top;
808*4882a593Smuzhiyun 	for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
809*4882a593Smuzhiyun 		;
810*4882a593Smuzhiyun 	/*
811*4882a593Smuzhiyun 	 * OK, we've found the last block that must survive. The rest of our
812*4882a593Smuzhiyun 	 * branch should be detached before unlocking. However, if that rest
813*4882a593Smuzhiyun 	 * of branch is all ours and does not grow immediately from the inode
814*4882a593Smuzhiyun 	 * it's easier to cheat and just decrement partial->p.
815*4882a593Smuzhiyun 	 */
816*4882a593Smuzhiyun 	if (p == chain + k - 1 && p > chain) {
817*4882a593Smuzhiyun 		p->p--;
818*4882a593Smuzhiyun 	} else {
819*4882a593Smuzhiyun 		*top = *p->p;
820*4882a593Smuzhiyun 		/* Nope, don't do this in ext4.  Must leave the tree intact */
821*4882a593Smuzhiyun #if 0
822*4882a593Smuzhiyun 		*p->p = 0;
823*4882a593Smuzhiyun #endif
824*4882a593Smuzhiyun 	}
825*4882a593Smuzhiyun 	/* Writer: end */
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	while (partial > p) {
828*4882a593Smuzhiyun 		brelse(partial->bh);
829*4882a593Smuzhiyun 		partial--;
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun no_top:
832*4882a593Smuzhiyun 	return partial;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun /*
836*4882a593Smuzhiyun  * Zero a number of block pointers in either an inode or an indirect block.
837*4882a593Smuzhiyun  * If we restart the transaction we must again get write access to the
838*4882a593Smuzhiyun  * indirect block for further modification.
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * We release `count' blocks on disk, but (last - first) may be greater
841*4882a593Smuzhiyun  * than `count' because there can be holes in there.
842*4882a593Smuzhiyun  *
843*4882a593Smuzhiyun  * Return 0 on success, 1 on invalid block range
844*4882a593Smuzhiyun  * and < 0 on fatal error.
845*4882a593Smuzhiyun  */
ext4_clear_blocks(handle_t * handle,struct inode * inode,struct buffer_head * bh,ext4_fsblk_t block_to_free,unsigned long count,__le32 * first,__le32 * last)846*4882a593Smuzhiyun static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
847*4882a593Smuzhiyun 			     struct buffer_head *bh,
848*4882a593Smuzhiyun 			     ext4_fsblk_t block_to_free,
849*4882a593Smuzhiyun 			     unsigned long count, __le32 *first,
850*4882a593Smuzhiyun 			     __le32 *last)
851*4882a593Smuzhiyun {
852*4882a593Smuzhiyun 	__le32 *p;
853*4882a593Smuzhiyun 	int	flags = EXT4_FREE_BLOCKS_VALIDATED;
854*4882a593Smuzhiyun 	int	err;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
857*4882a593Smuzhiyun 	    ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
858*4882a593Smuzhiyun 		flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
859*4882a593Smuzhiyun 	else if (ext4_should_journal_data(inode))
860*4882a593Smuzhiyun 		flags |= EXT4_FREE_BLOCKS_FORGET;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	if (!ext4_inode_block_valid(inode, block_to_free, count)) {
863*4882a593Smuzhiyun 		EXT4_ERROR_INODE(inode, "attempt to clear invalid "
864*4882a593Smuzhiyun 				 "blocks %llu len %lu",
865*4882a593Smuzhiyun 				 (unsigned long long) block_to_free, count);
866*4882a593Smuzhiyun 		return 1;
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	err = ext4_ind_truncate_ensure_credits(handle, inode, bh,
870*4882a593Smuzhiyun 				ext4_free_data_revoke_credits(inode, count));
871*4882a593Smuzhiyun 	if (err < 0)
872*4882a593Smuzhiyun 		goto out_err;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	for (p = first; p < last; p++)
875*4882a593Smuzhiyun 		*p = 0;
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
878*4882a593Smuzhiyun 	return 0;
879*4882a593Smuzhiyun out_err:
880*4882a593Smuzhiyun 	ext4_std_error(inode->i_sb, err);
881*4882a593Smuzhiyun 	return err;
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun /**
885*4882a593Smuzhiyun  * ext4_free_data - free a list of data blocks
886*4882a593Smuzhiyun  * @handle:	handle for this transaction
887*4882a593Smuzhiyun  * @inode:	inode we are dealing with
888*4882a593Smuzhiyun  * @this_bh:	indirect buffer_head which contains *@first and *@last
889*4882a593Smuzhiyun  * @first:	array of block numbers
890*4882a593Smuzhiyun  * @last:	points immediately past the end of array
891*4882a593Smuzhiyun  *
892*4882a593Smuzhiyun  * We are freeing all blocks referred from that array (numbers are stored as
893*4882a593Smuzhiyun  * little-endian 32-bit) and updating @inode->i_blocks appropriately.
894*4882a593Smuzhiyun  *
895*4882a593Smuzhiyun  * We accumulate contiguous runs of blocks to free.  Conveniently, if these
896*4882a593Smuzhiyun  * blocks are contiguous then releasing them at one time will only affect one
897*4882a593Smuzhiyun  * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
898*4882a593Smuzhiyun  * actually use a lot of journal space.
899*4882a593Smuzhiyun  *
900*4882a593Smuzhiyun  * @this_bh will be %NULL if @first and @last point into the inode's direct
901*4882a593Smuzhiyun  * block pointers.
902*4882a593Smuzhiyun  */
ext4_free_data(handle_t * handle,struct inode * inode,struct buffer_head * this_bh,__le32 * first,__le32 * last)903*4882a593Smuzhiyun static void ext4_free_data(handle_t *handle, struct inode *inode,
904*4882a593Smuzhiyun 			   struct buffer_head *this_bh,
905*4882a593Smuzhiyun 			   __le32 *first, __le32 *last)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	ext4_fsblk_t block_to_free = 0;    /* Starting block # of a run */
908*4882a593Smuzhiyun 	unsigned long count = 0;	    /* Number of blocks in the run */
909*4882a593Smuzhiyun 	__le32 *block_to_free_p = NULL;	    /* Pointer into inode/ind
910*4882a593Smuzhiyun 					       corresponding to
911*4882a593Smuzhiyun 					       block_to_free */
912*4882a593Smuzhiyun 	ext4_fsblk_t nr;		    /* Current block # */
913*4882a593Smuzhiyun 	__le32 *p;			    /* Pointer into inode/ind
914*4882a593Smuzhiyun 					       for current block */
915*4882a593Smuzhiyun 	int err = 0;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	if (this_bh) {				/* For indirect block */
918*4882a593Smuzhiyun 		BUFFER_TRACE(this_bh, "get_write_access");
919*4882a593Smuzhiyun 		err = ext4_journal_get_write_access(handle, this_bh);
920*4882a593Smuzhiyun 		/* Important: if we can't update the indirect pointers
921*4882a593Smuzhiyun 		 * to the blocks, we can't free them. */
922*4882a593Smuzhiyun 		if (err)
923*4882a593Smuzhiyun 			return;
924*4882a593Smuzhiyun 	}
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	for (p = first; p < last; p++) {
927*4882a593Smuzhiyun 		nr = le32_to_cpu(*p);
928*4882a593Smuzhiyun 		if (nr) {
929*4882a593Smuzhiyun 			/* accumulate blocks to free if they're contiguous */
930*4882a593Smuzhiyun 			if (count == 0) {
931*4882a593Smuzhiyun 				block_to_free = nr;
932*4882a593Smuzhiyun 				block_to_free_p = p;
933*4882a593Smuzhiyun 				count = 1;
934*4882a593Smuzhiyun 			} else if (nr == block_to_free + count) {
935*4882a593Smuzhiyun 				count++;
936*4882a593Smuzhiyun 			} else {
937*4882a593Smuzhiyun 				err = ext4_clear_blocks(handle, inode, this_bh,
938*4882a593Smuzhiyun 						        block_to_free, count,
939*4882a593Smuzhiyun 						        block_to_free_p, p);
940*4882a593Smuzhiyun 				if (err)
941*4882a593Smuzhiyun 					break;
942*4882a593Smuzhiyun 				block_to_free = nr;
943*4882a593Smuzhiyun 				block_to_free_p = p;
944*4882a593Smuzhiyun 				count = 1;
945*4882a593Smuzhiyun 			}
946*4882a593Smuzhiyun 		}
947*4882a593Smuzhiyun 	}
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	if (!err && count > 0)
950*4882a593Smuzhiyun 		err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
951*4882a593Smuzhiyun 					count, block_to_free_p, p);
952*4882a593Smuzhiyun 	if (err < 0)
953*4882a593Smuzhiyun 		/* fatal error */
954*4882a593Smuzhiyun 		return;
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	if (this_bh) {
957*4882a593Smuzhiyun 		BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 		/*
960*4882a593Smuzhiyun 		 * The buffer head should have an attached journal head at this
961*4882a593Smuzhiyun 		 * point. However, if the data is corrupted and an indirect
962*4882a593Smuzhiyun 		 * block pointed to itself, it would have been detached when
963*4882a593Smuzhiyun 		 * the block was cleared. Check for this instead of OOPSing.
964*4882a593Smuzhiyun 		 */
965*4882a593Smuzhiyun 		if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
966*4882a593Smuzhiyun 			ext4_handle_dirty_metadata(handle, inode, this_bh);
967*4882a593Smuzhiyun 		else
968*4882a593Smuzhiyun 			EXT4_ERROR_INODE(inode,
969*4882a593Smuzhiyun 					 "circular indirect block detected at "
970*4882a593Smuzhiyun 					 "block %llu",
971*4882a593Smuzhiyun 				(unsigned long long) this_bh->b_blocknr);
972*4882a593Smuzhiyun 	}
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun /**
976*4882a593Smuzhiyun  *	ext4_free_branches - free an array of branches
977*4882a593Smuzhiyun  *	@handle: JBD handle for this transaction
978*4882a593Smuzhiyun  *	@inode:	inode we are dealing with
979*4882a593Smuzhiyun  *	@parent_bh: the buffer_head which contains *@first and *@last
980*4882a593Smuzhiyun  *	@first:	array of block numbers
981*4882a593Smuzhiyun  *	@last:	pointer immediately past the end of array
982*4882a593Smuzhiyun  *	@depth:	depth of the branches to free
983*4882a593Smuzhiyun  *
984*4882a593Smuzhiyun  *	We are freeing all blocks referred from these branches (numbers are
985*4882a593Smuzhiyun  *	stored as little-endian 32-bit) and updating @inode->i_blocks
986*4882a593Smuzhiyun  *	appropriately.
987*4882a593Smuzhiyun  */
ext4_free_branches(handle_t * handle,struct inode * inode,struct buffer_head * parent_bh,__le32 * first,__le32 * last,int depth)988*4882a593Smuzhiyun static void ext4_free_branches(handle_t *handle, struct inode *inode,
989*4882a593Smuzhiyun 			       struct buffer_head *parent_bh,
990*4882a593Smuzhiyun 			       __le32 *first, __le32 *last, int depth)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun 	ext4_fsblk_t nr;
993*4882a593Smuzhiyun 	__le32 *p;
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	if (ext4_handle_is_aborted(handle))
996*4882a593Smuzhiyun 		return;
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	if (depth--) {
999*4882a593Smuzhiyun 		struct buffer_head *bh;
1000*4882a593Smuzhiyun 		int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1001*4882a593Smuzhiyun 		p = last;
1002*4882a593Smuzhiyun 		while (--p >= first) {
1003*4882a593Smuzhiyun 			nr = le32_to_cpu(*p);
1004*4882a593Smuzhiyun 			if (!nr)
1005*4882a593Smuzhiyun 				continue;		/* A hole */
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 			if (!ext4_inode_block_valid(inode, nr, 1)) {
1008*4882a593Smuzhiyun 				EXT4_ERROR_INODE(inode,
1009*4882a593Smuzhiyun 						 "invalid indirect mapped "
1010*4882a593Smuzhiyun 						 "block %lu (level %d)",
1011*4882a593Smuzhiyun 						 (unsigned long) nr, depth);
1012*4882a593Smuzhiyun 				break;
1013*4882a593Smuzhiyun 			}
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 			/* Go read the buffer for the next level down */
1016*4882a593Smuzhiyun 			bh = ext4_sb_bread(inode->i_sb, nr, 0);
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 			/*
1019*4882a593Smuzhiyun 			 * A read failure? Report error and clear slot
1020*4882a593Smuzhiyun 			 * (should be rare).
1021*4882a593Smuzhiyun 			 */
1022*4882a593Smuzhiyun 			if (IS_ERR(bh)) {
1023*4882a593Smuzhiyun 				ext4_error_inode_block(inode, nr, -PTR_ERR(bh),
1024*4882a593Smuzhiyun 						       "Read failure");
1025*4882a593Smuzhiyun 				continue;
1026*4882a593Smuzhiyun 			}
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 			/* This zaps the entire block.  Bottom up. */
1029*4882a593Smuzhiyun 			BUFFER_TRACE(bh, "free child branches");
1030*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, bh,
1031*4882a593Smuzhiyun 					(__le32 *) bh->b_data,
1032*4882a593Smuzhiyun 					(__le32 *) bh->b_data + addr_per_block,
1033*4882a593Smuzhiyun 					depth);
1034*4882a593Smuzhiyun 			brelse(bh);
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 			/*
1037*4882a593Smuzhiyun 			 * Everything below this pointer has been
1038*4882a593Smuzhiyun 			 * released.  Now let this top-of-subtree go.
1039*4882a593Smuzhiyun 			 *
1040*4882a593Smuzhiyun 			 * We want the freeing of this indirect block to be
1041*4882a593Smuzhiyun 			 * atomic in the journal with the updating of the
1042*4882a593Smuzhiyun 			 * bitmap block which owns it.  So make some room in
1043*4882a593Smuzhiyun 			 * the journal.
1044*4882a593Smuzhiyun 			 *
1045*4882a593Smuzhiyun 			 * We zero the parent pointer *after* freeing its
1046*4882a593Smuzhiyun 			 * pointee in the bitmaps, so if extend_transaction()
1047*4882a593Smuzhiyun 			 * for some reason fails to put the bitmap changes and
1048*4882a593Smuzhiyun 			 * the release into the same transaction, recovery
1049*4882a593Smuzhiyun 			 * will merely complain about releasing a free block,
1050*4882a593Smuzhiyun 			 * rather than leaking blocks.
1051*4882a593Smuzhiyun 			 */
1052*4882a593Smuzhiyun 			if (ext4_handle_is_aborted(handle))
1053*4882a593Smuzhiyun 				return;
1054*4882a593Smuzhiyun 			if (ext4_ind_truncate_ensure_credits(handle, inode,
1055*4882a593Smuzhiyun 					NULL,
1056*4882a593Smuzhiyun 					ext4_free_metadata_revoke_credits(
1057*4882a593Smuzhiyun 							inode->i_sb, 1)) < 0)
1058*4882a593Smuzhiyun 				return;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 			/*
1061*4882a593Smuzhiyun 			 * The forget flag here is critical because if
1062*4882a593Smuzhiyun 			 * we are journaling (and not doing data
1063*4882a593Smuzhiyun 			 * journaling), we have to make sure a revoke
1064*4882a593Smuzhiyun 			 * record is written to prevent the journal
1065*4882a593Smuzhiyun 			 * replay from overwriting the (former)
1066*4882a593Smuzhiyun 			 * indirect block if it gets reallocated as a
1067*4882a593Smuzhiyun 			 * data block.  This must happen in the same
1068*4882a593Smuzhiyun 			 * transaction where the data blocks are
1069*4882a593Smuzhiyun 			 * actually freed.
1070*4882a593Smuzhiyun 			 */
1071*4882a593Smuzhiyun 			ext4_free_blocks(handle, inode, NULL, nr, 1,
1072*4882a593Smuzhiyun 					 EXT4_FREE_BLOCKS_METADATA|
1073*4882a593Smuzhiyun 					 EXT4_FREE_BLOCKS_FORGET);
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 			if (parent_bh) {
1076*4882a593Smuzhiyun 				/*
1077*4882a593Smuzhiyun 				 * The block which we have just freed is
1078*4882a593Smuzhiyun 				 * pointed to by an indirect block: journal it
1079*4882a593Smuzhiyun 				 */
1080*4882a593Smuzhiyun 				BUFFER_TRACE(parent_bh, "get_write_access");
1081*4882a593Smuzhiyun 				if (!ext4_journal_get_write_access(handle,
1082*4882a593Smuzhiyun 								   parent_bh)){
1083*4882a593Smuzhiyun 					*p = 0;
1084*4882a593Smuzhiyun 					BUFFER_TRACE(parent_bh,
1085*4882a593Smuzhiyun 					"call ext4_handle_dirty_metadata");
1086*4882a593Smuzhiyun 					ext4_handle_dirty_metadata(handle,
1087*4882a593Smuzhiyun 								   inode,
1088*4882a593Smuzhiyun 								   parent_bh);
1089*4882a593Smuzhiyun 				}
1090*4882a593Smuzhiyun 			}
1091*4882a593Smuzhiyun 		}
1092*4882a593Smuzhiyun 	} else {
1093*4882a593Smuzhiyun 		/* We have reached the bottom of the tree. */
1094*4882a593Smuzhiyun 		BUFFER_TRACE(parent_bh, "free data blocks");
1095*4882a593Smuzhiyun 		ext4_free_data(handle, inode, parent_bh, first, last);
1096*4882a593Smuzhiyun 	}
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun 
ext4_ind_truncate(handle_t * handle,struct inode * inode)1099*4882a593Smuzhiyun void ext4_ind_truncate(handle_t *handle, struct inode *inode)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun 	struct ext4_inode_info *ei = EXT4_I(inode);
1102*4882a593Smuzhiyun 	__le32 *i_data = ei->i_data;
1103*4882a593Smuzhiyun 	int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1104*4882a593Smuzhiyun 	ext4_lblk_t offsets[4];
1105*4882a593Smuzhiyun 	Indirect chain[4];
1106*4882a593Smuzhiyun 	Indirect *partial;
1107*4882a593Smuzhiyun 	__le32 nr = 0;
1108*4882a593Smuzhiyun 	int n = 0;
1109*4882a593Smuzhiyun 	ext4_lblk_t last_block, max_block;
1110*4882a593Smuzhiyun 	unsigned blocksize = inode->i_sb->s_blocksize;
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	last_block = (inode->i_size + blocksize-1)
1113*4882a593Smuzhiyun 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1114*4882a593Smuzhiyun 	max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
1115*4882a593Smuzhiyun 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	if (last_block != max_block) {
1118*4882a593Smuzhiyun 		n = ext4_block_to_path(inode, last_block, offsets, NULL);
1119*4882a593Smuzhiyun 		if (n == 0)
1120*4882a593Smuzhiyun 			return;
1121*4882a593Smuzhiyun 	}
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	/*
1126*4882a593Smuzhiyun 	 * The orphan list entry will now protect us from any crash which
1127*4882a593Smuzhiyun 	 * occurs before the truncate completes, so it is now safe to propagate
1128*4882a593Smuzhiyun 	 * the new, shorter inode size (held for now in i_size) into the
1129*4882a593Smuzhiyun 	 * on-disk inode. We do this via i_disksize, which is the value which
1130*4882a593Smuzhiyun 	 * ext4 *really* writes onto the disk inode.
1131*4882a593Smuzhiyun 	 */
1132*4882a593Smuzhiyun 	ei->i_disksize = inode->i_size;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	if (last_block == max_block) {
1135*4882a593Smuzhiyun 		/*
1136*4882a593Smuzhiyun 		 * It is unnecessary to free any data blocks if last_block is
1137*4882a593Smuzhiyun 		 * equal to the indirect block limit.
1138*4882a593Smuzhiyun 		 */
1139*4882a593Smuzhiyun 		return;
1140*4882a593Smuzhiyun 	} else if (n == 1) {		/* direct blocks */
1141*4882a593Smuzhiyun 		ext4_free_data(handle, inode, NULL, i_data+offsets[0],
1142*4882a593Smuzhiyun 			       i_data + EXT4_NDIR_BLOCKS);
1143*4882a593Smuzhiyun 		goto do_indirects;
1144*4882a593Smuzhiyun 	}
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	partial = ext4_find_shared(inode, n, offsets, chain, &nr);
1147*4882a593Smuzhiyun 	/* Kill the top of shared branch (not detached) */
1148*4882a593Smuzhiyun 	if (nr) {
1149*4882a593Smuzhiyun 		if (partial == chain) {
1150*4882a593Smuzhiyun 			/* Shared branch grows from the inode */
1151*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL,
1152*4882a593Smuzhiyun 					   &nr, &nr+1, (chain+n-1) - partial);
1153*4882a593Smuzhiyun 			*partial->p = 0;
1154*4882a593Smuzhiyun 			/*
1155*4882a593Smuzhiyun 			 * We mark the inode dirty prior to restart,
1156*4882a593Smuzhiyun 			 * and prior to stop.  No need for it here.
1157*4882a593Smuzhiyun 			 */
1158*4882a593Smuzhiyun 		} else {
1159*4882a593Smuzhiyun 			/* Shared branch grows from an indirect block */
1160*4882a593Smuzhiyun 			BUFFER_TRACE(partial->bh, "get_write_access");
1161*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, partial->bh,
1162*4882a593Smuzhiyun 					partial->p,
1163*4882a593Smuzhiyun 					partial->p+1, (chain+n-1) - partial);
1164*4882a593Smuzhiyun 		}
1165*4882a593Smuzhiyun 	}
1166*4882a593Smuzhiyun 	/* Clear the ends of indirect blocks on the shared branch */
1167*4882a593Smuzhiyun 	while (partial > chain) {
1168*4882a593Smuzhiyun 		ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
1169*4882a593Smuzhiyun 				   (__le32*)partial->bh->b_data+addr_per_block,
1170*4882a593Smuzhiyun 				   (chain+n-1) - partial);
1171*4882a593Smuzhiyun 		BUFFER_TRACE(partial->bh, "call brelse");
1172*4882a593Smuzhiyun 		brelse(partial->bh);
1173*4882a593Smuzhiyun 		partial--;
1174*4882a593Smuzhiyun 	}
1175*4882a593Smuzhiyun do_indirects:
1176*4882a593Smuzhiyun 	/* Kill the remaining (whole) subtrees */
1177*4882a593Smuzhiyun 	switch (offsets[0]) {
1178*4882a593Smuzhiyun 	default:
1179*4882a593Smuzhiyun 		nr = i_data[EXT4_IND_BLOCK];
1180*4882a593Smuzhiyun 		if (nr) {
1181*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
1182*4882a593Smuzhiyun 			i_data[EXT4_IND_BLOCK] = 0;
1183*4882a593Smuzhiyun 		}
1184*4882a593Smuzhiyun 		fallthrough;
1185*4882a593Smuzhiyun 	case EXT4_IND_BLOCK:
1186*4882a593Smuzhiyun 		nr = i_data[EXT4_DIND_BLOCK];
1187*4882a593Smuzhiyun 		if (nr) {
1188*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
1189*4882a593Smuzhiyun 			i_data[EXT4_DIND_BLOCK] = 0;
1190*4882a593Smuzhiyun 		}
1191*4882a593Smuzhiyun 		fallthrough;
1192*4882a593Smuzhiyun 	case EXT4_DIND_BLOCK:
1193*4882a593Smuzhiyun 		nr = i_data[EXT4_TIND_BLOCK];
1194*4882a593Smuzhiyun 		if (nr) {
1195*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
1196*4882a593Smuzhiyun 			i_data[EXT4_TIND_BLOCK] = 0;
1197*4882a593Smuzhiyun 		}
1198*4882a593Smuzhiyun 		fallthrough;
1199*4882a593Smuzhiyun 	case EXT4_TIND_BLOCK:
1200*4882a593Smuzhiyun 		;
1201*4882a593Smuzhiyun 	}
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun /**
1205*4882a593Smuzhiyun  *	ext4_ind_remove_space - remove space from the range
1206*4882a593Smuzhiyun  *	@handle: JBD handle for this transaction
1207*4882a593Smuzhiyun  *	@inode:	inode we are dealing with
1208*4882a593Smuzhiyun  *	@start:	First block to remove
1209*4882a593Smuzhiyun  *	@end:	One block after the last block to remove (exclusive)
1210*4882a593Smuzhiyun  *
1211*4882a593Smuzhiyun  *	Free the blocks in the defined range (end is exclusive endpoint of
1212*4882a593Smuzhiyun  *	range). This is used by ext4_punch_hole().
1213*4882a593Smuzhiyun  */
ext4_ind_remove_space(handle_t * handle,struct inode * inode,ext4_lblk_t start,ext4_lblk_t end)1214*4882a593Smuzhiyun int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
1215*4882a593Smuzhiyun 			  ext4_lblk_t start, ext4_lblk_t end)
1216*4882a593Smuzhiyun {
1217*4882a593Smuzhiyun 	struct ext4_inode_info *ei = EXT4_I(inode);
1218*4882a593Smuzhiyun 	__le32 *i_data = ei->i_data;
1219*4882a593Smuzhiyun 	int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1220*4882a593Smuzhiyun 	ext4_lblk_t offsets[4], offsets2[4];
1221*4882a593Smuzhiyun 	Indirect chain[4], chain2[4];
1222*4882a593Smuzhiyun 	Indirect *partial, *partial2;
1223*4882a593Smuzhiyun 	Indirect *p = NULL, *p2 = NULL;
1224*4882a593Smuzhiyun 	ext4_lblk_t max_block;
1225*4882a593Smuzhiyun 	__le32 nr = 0, nr2 = 0;
1226*4882a593Smuzhiyun 	int n = 0, n2 = 0;
1227*4882a593Smuzhiyun 	unsigned blocksize = inode->i_sb->s_blocksize;
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
1230*4882a593Smuzhiyun 					>> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1231*4882a593Smuzhiyun 	if (end >= max_block)
1232*4882a593Smuzhiyun 		end = max_block;
1233*4882a593Smuzhiyun 	if ((start >= end) || (start > max_block))
1234*4882a593Smuzhiyun 		return 0;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	n = ext4_block_to_path(inode, start, offsets, NULL);
1237*4882a593Smuzhiyun 	n2 = ext4_block_to_path(inode, end, offsets2, NULL);
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	BUG_ON(n > n2);
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 	if ((n == 1) && (n == n2)) {
1242*4882a593Smuzhiyun 		/* We're punching only within direct block range */
1243*4882a593Smuzhiyun 		ext4_free_data(handle, inode, NULL, i_data + offsets[0],
1244*4882a593Smuzhiyun 			       i_data + offsets2[0]);
1245*4882a593Smuzhiyun 		return 0;
1246*4882a593Smuzhiyun 	} else if (n2 > n) {
1247*4882a593Smuzhiyun 		/*
1248*4882a593Smuzhiyun 		 * Start and end are on a different levels so we're going to
1249*4882a593Smuzhiyun 		 * free partial block at start, and partial block at end of
1250*4882a593Smuzhiyun 		 * the range. If there are some levels in between then
1251*4882a593Smuzhiyun 		 * do_indirects label will take care of that.
1252*4882a593Smuzhiyun 		 */
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 		if (n == 1) {
1255*4882a593Smuzhiyun 			/*
1256*4882a593Smuzhiyun 			 * Start is at the direct block level, free
1257*4882a593Smuzhiyun 			 * everything to the end of the level.
1258*4882a593Smuzhiyun 			 */
1259*4882a593Smuzhiyun 			ext4_free_data(handle, inode, NULL, i_data + offsets[0],
1260*4882a593Smuzhiyun 				       i_data + EXT4_NDIR_BLOCKS);
1261*4882a593Smuzhiyun 			goto end_range;
1262*4882a593Smuzhiyun 		}
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 		partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
1266*4882a593Smuzhiyun 		if (nr) {
1267*4882a593Smuzhiyun 			if (partial == chain) {
1268*4882a593Smuzhiyun 				/* Shared branch grows from the inode */
1269*4882a593Smuzhiyun 				ext4_free_branches(handle, inode, NULL,
1270*4882a593Smuzhiyun 					   &nr, &nr+1, (chain+n-1) - partial);
1271*4882a593Smuzhiyun 				*partial->p = 0;
1272*4882a593Smuzhiyun 			} else {
1273*4882a593Smuzhiyun 				/* Shared branch grows from an indirect block */
1274*4882a593Smuzhiyun 				BUFFER_TRACE(partial->bh, "get_write_access");
1275*4882a593Smuzhiyun 				ext4_free_branches(handle, inode, partial->bh,
1276*4882a593Smuzhiyun 					partial->p,
1277*4882a593Smuzhiyun 					partial->p+1, (chain+n-1) - partial);
1278*4882a593Smuzhiyun 			}
1279*4882a593Smuzhiyun 		}
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 		/*
1282*4882a593Smuzhiyun 		 * Clear the ends of indirect blocks on the shared branch
1283*4882a593Smuzhiyun 		 * at the start of the range
1284*4882a593Smuzhiyun 		 */
1285*4882a593Smuzhiyun 		while (partial > chain) {
1286*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, partial->bh,
1287*4882a593Smuzhiyun 				partial->p + 1,
1288*4882a593Smuzhiyun 				(__le32 *)partial->bh->b_data+addr_per_block,
1289*4882a593Smuzhiyun 				(chain+n-1) - partial);
1290*4882a593Smuzhiyun 			partial--;
1291*4882a593Smuzhiyun 		}
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun end_range:
1294*4882a593Smuzhiyun 		partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
1295*4882a593Smuzhiyun 		if (nr2) {
1296*4882a593Smuzhiyun 			if (partial2 == chain2) {
1297*4882a593Smuzhiyun 				/*
1298*4882a593Smuzhiyun 				 * Remember, end is exclusive so here we're at
1299*4882a593Smuzhiyun 				 * the start of the next level we're not going
1300*4882a593Smuzhiyun 				 * to free. Everything was covered by the start
1301*4882a593Smuzhiyun 				 * of the range.
1302*4882a593Smuzhiyun 				 */
1303*4882a593Smuzhiyun 				goto do_indirects;
1304*4882a593Smuzhiyun 			}
1305*4882a593Smuzhiyun 		} else {
1306*4882a593Smuzhiyun 			/*
1307*4882a593Smuzhiyun 			 * ext4_find_shared returns Indirect structure which
1308*4882a593Smuzhiyun 			 * points to the last element which should not be
1309*4882a593Smuzhiyun 			 * removed by truncate. But this is end of the range
1310*4882a593Smuzhiyun 			 * in punch_hole so we need to point to the next element
1311*4882a593Smuzhiyun 			 */
1312*4882a593Smuzhiyun 			partial2->p++;
1313*4882a593Smuzhiyun 		}
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 		/*
1316*4882a593Smuzhiyun 		 * Clear the ends of indirect blocks on the shared branch
1317*4882a593Smuzhiyun 		 * at the end of the range
1318*4882a593Smuzhiyun 		 */
1319*4882a593Smuzhiyun 		while (partial2 > chain2) {
1320*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, partial2->bh,
1321*4882a593Smuzhiyun 					   (__le32 *)partial2->bh->b_data,
1322*4882a593Smuzhiyun 					   partial2->p,
1323*4882a593Smuzhiyun 					   (chain2+n2-1) - partial2);
1324*4882a593Smuzhiyun 			partial2--;
1325*4882a593Smuzhiyun 		}
1326*4882a593Smuzhiyun 		goto do_indirects;
1327*4882a593Smuzhiyun 	}
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	/* Punch happened within the same level (n == n2) */
1330*4882a593Smuzhiyun 	partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
1331*4882a593Smuzhiyun 	partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	/* Free top, but only if partial2 isn't its subtree. */
1334*4882a593Smuzhiyun 	if (nr) {
1335*4882a593Smuzhiyun 		int level = min(partial - chain, partial2 - chain2);
1336*4882a593Smuzhiyun 		int i;
1337*4882a593Smuzhiyun 		int subtree = 1;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 		for (i = 0; i <= level; i++) {
1340*4882a593Smuzhiyun 			if (offsets[i] != offsets2[i]) {
1341*4882a593Smuzhiyun 				subtree = 0;
1342*4882a593Smuzhiyun 				break;
1343*4882a593Smuzhiyun 			}
1344*4882a593Smuzhiyun 		}
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 		if (!subtree) {
1347*4882a593Smuzhiyun 			if (partial == chain) {
1348*4882a593Smuzhiyun 				/* Shared branch grows from the inode */
1349*4882a593Smuzhiyun 				ext4_free_branches(handle, inode, NULL,
1350*4882a593Smuzhiyun 						   &nr, &nr+1,
1351*4882a593Smuzhiyun 						   (chain+n-1) - partial);
1352*4882a593Smuzhiyun 				*partial->p = 0;
1353*4882a593Smuzhiyun 			} else {
1354*4882a593Smuzhiyun 				/* Shared branch grows from an indirect block */
1355*4882a593Smuzhiyun 				BUFFER_TRACE(partial->bh, "get_write_access");
1356*4882a593Smuzhiyun 				ext4_free_branches(handle, inode, partial->bh,
1357*4882a593Smuzhiyun 						   partial->p,
1358*4882a593Smuzhiyun 						   partial->p+1,
1359*4882a593Smuzhiyun 						   (chain+n-1) - partial);
1360*4882a593Smuzhiyun 			}
1361*4882a593Smuzhiyun 		}
1362*4882a593Smuzhiyun 	}
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	if (!nr2) {
1365*4882a593Smuzhiyun 		/*
1366*4882a593Smuzhiyun 		 * ext4_find_shared returns Indirect structure which
1367*4882a593Smuzhiyun 		 * points to the last element which should not be
1368*4882a593Smuzhiyun 		 * removed by truncate. But this is end of the range
1369*4882a593Smuzhiyun 		 * in punch_hole so we need to point to the next element
1370*4882a593Smuzhiyun 		 */
1371*4882a593Smuzhiyun 		partial2->p++;
1372*4882a593Smuzhiyun 	}
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 	while (partial > chain || partial2 > chain2) {
1375*4882a593Smuzhiyun 		int depth = (chain+n-1) - partial;
1376*4882a593Smuzhiyun 		int depth2 = (chain2+n2-1) - partial2;
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 		if (partial > chain && partial2 > chain2 &&
1379*4882a593Smuzhiyun 		    partial->bh->b_blocknr == partial2->bh->b_blocknr) {
1380*4882a593Smuzhiyun 			/*
1381*4882a593Smuzhiyun 			 * We've converged on the same block. Clear the range,
1382*4882a593Smuzhiyun 			 * then we're done.
1383*4882a593Smuzhiyun 			 */
1384*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, partial->bh,
1385*4882a593Smuzhiyun 					   partial->p + 1,
1386*4882a593Smuzhiyun 					   partial2->p,
1387*4882a593Smuzhiyun 					   (chain+n-1) - partial);
1388*4882a593Smuzhiyun 			goto cleanup;
1389*4882a593Smuzhiyun 		}
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 		/*
1392*4882a593Smuzhiyun 		 * The start and end partial branches may not be at the same
1393*4882a593Smuzhiyun 		 * level even though the punch happened within one level. So, we
1394*4882a593Smuzhiyun 		 * give them a chance to arrive at the same level, then walk
1395*4882a593Smuzhiyun 		 * them in step with each other until we converge on the same
1396*4882a593Smuzhiyun 		 * block.
1397*4882a593Smuzhiyun 		 */
1398*4882a593Smuzhiyun 		if (partial > chain && depth <= depth2) {
1399*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, partial->bh,
1400*4882a593Smuzhiyun 					   partial->p + 1,
1401*4882a593Smuzhiyun 					   (__le32 *)partial->bh->b_data+addr_per_block,
1402*4882a593Smuzhiyun 					   (chain+n-1) - partial);
1403*4882a593Smuzhiyun 			partial--;
1404*4882a593Smuzhiyun 		}
1405*4882a593Smuzhiyun 		if (partial2 > chain2 && depth2 <= depth) {
1406*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, partial2->bh,
1407*4882a593Smuzhiyun 					   (__le32 *)partial2->bh->b_data,
1408*4882a593Smuzhiyun 					   partial2->p,
1409*4882a593Smuzhiyun 					   (chain2+n2-1) - partial2);
1410*4882a593Smuzhiyun 			partial2--;
1411*4882a593Smuzhiyun 		}
1412*4882a593Smuzhiyun 	}
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun cleanup:
1415*4882a593Smuzhiyun 	while (p && p > chain) {
1416*4882a593Smuzhiyun 		BUFFER_TRACE(p->bh, "call brelse");
1417*4882a593Smuzhiyun 		brelse(p->bh);
1418*4882a593Smuzhiyun 		p--;
1419*4882a593Smuzhiyun 	}
1420*4882a593Smuzhiyun 	while (p2 && p2 > chain2) {
1421*4882a593Smuzhiyun 		BUFFER_TRACE(p2->bh, "call brelse");
1422*4882a593Smuzhiyun 		brelse(p2->bh);
1423*4882a593Smuzhiyun 		p2--;
1424*4882a593Smuzhiyun 	}
1425*4882a593Smuzhiyun 	return 0;
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun do_indirects:
1428*4882a593Smuzhiyun 	/* Kill the remaining (whole) subtrees */
1429*4882a593Smuzhiyun 	switch (offsets[0]) {
1430*4882a593Smuzhiyun 	default:
1431*4882a593Smuzhiyun 		if (++n >= n2)
1432*4882a593Smuzhiyun 			break;
1433*4882a593Smuzhiyun 		nr = i_data[EXT4_IND_BLOCK];
1434*4882a593Smuzhiyun 		if (nr) {
1435*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
1436*4882a593Smuzhiyun 			i_data[EXT4_IND_BLOCK] = 0;
1437*4882a593Smuzhiyun 		}
1438*4882a593Smuzhiyun 		fallthrough;
1439*4882a593Smuzhiyun 	case EXT4_IND_BLOCK:
1440*4882a593Smuzhiyun 		if (++n >= n2)
1441*4882a593Smuzhiyun 			break;
1442*4882a593Smuzhiyun 		nr = i_data[EXT4_DIND_BLOCK];
1443*4882a593Smuzhiyun 		if (nr) {
1444*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
1445*4882a593Smuzhiyun 			i_data[EXT4_DIND_BLOCK] = 0;
1446*4882a593Smuzhiyun 		}
1447*4882a593Smuzhiyun 		fallthrough;
1448*4882a593Smuzhiyun 	case EXT4_DIND_BLOCK:
1449*4882a593Smuzhiyun 		if (++n >= n2)
1450*4882a593Smuzhiyun 			break;
1451*4882a593Smuzhiyun 		nr = i_data[EXT4_TIND_BLOCK];
1452*4882a593Smuzhiyun 		if (nr) {
1453*4882a593Smuzhiyun 			ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
1454*4882a593Smuzhiyun 			i_data[EXT4_TIND_BLOCK] = 0;
1455*4882a593Smuzhiyun 		}
1456*4882a593Smuzhiyun 		fallthrough;
1457*4882a593Smuzhiyun 	case EXT4_TIND_BLOCK:
1458*4882a593Smuzhiyun 		;
1459*4882a593Smuzhiyun 	}
1460*4882a593Smuzhiyun 	goto cleanup;
1461*4882a593Smuzhiyun }
1462