1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2dae1e52cSAmir Goldstein /*
3dae1e52cSAmir Goldstein * linux/fs/ext4/indirect.c
4dae1e52cSAmir Goldstein *
5dae1e52cSAmir Goldstein * from
6dae1e52cSAmir Goldstein *
7dae1e52cSAmir Goldstein * linux/fs/ext4/inode.c
8dae1e52cSAmir Goldstein *
9dae1e52cSAmir Goldstein * Copyright (C) 1992, 1993, 1994, 1995
10dae1e52cSAmir Goldstein * Remy Card (card@masi.ibp.fr)
11dae1e52cSAmir Goldstein * Laboratoire MASI - Institut Blaise Pascal
12dae1e52cSAmir Goldstein * Universite Pierre et Marie Curie (Paris VI)
13dae1e52cSAmir Goldstein *
14dae1e52cSAmir Goldstein * from
15dae1e52cSAmir Goldstein *
16dae1e52cSAmir Goldstein * linux/fs/minix/inode.c
17dae1e52cSAmir Goldstein *
18dae1e52cSAmir Goldstein * Copyright (C) 1991, 1992 Linus Torvalds
19dae1e52cSAmir Goldstein *
20dae1e52cSAmir Goldstein * Goal-directed block allocation by Stephen Tweedie
21dae1e52cSAmir Goldstein * (sct@redhat.com), 1993, 1998
22dae1e52cSAmir Goldstein */
23dae1e52cSAmir Goldstein
24dae1e52cSAmir Goldstein #include "ext4_jbd2.h"
25dae1e52cSAmir Goldstein #include "truncate.h"
26c94c2acfSMatthew Wilcox #include <linux/dax.h>
27e2e40f2cSChristoph Hellwig #include <linux/uio.h>
28dae1e52cSAmir Goldstein
29dae1e52cSAmir Goldstein #include <trace/events/ext4.h>
30dae1e52cSAmir Goldstein
31dae1e52cSAmir Goldstein typedef struct {
32dae1e52cSAmir Goldstein __le32 *p;
33dae1e52cSAmir Goldstein __le32 key;
34dae1e52cSAmir Goldstein struct buffer_head *bh;
35dae1e52cSAmir Goldstein } Indirect;
36dae1e52cSAmir Goldstein
add_chain(Indirect * p,struct buffer_head * bh,__le32 * v)37dae1e52cSAmir Goldstein static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
38dae1e52cSAmir Goldstein {
39dae1e52cSAmir Goldstein p->key = *(p->p = v);
40dae1e52cSAmir Goldstein p->bh = bh;
41dae1e52cSAmir Goldstein }
42dae1e52cSAmir Goldstein
43dae1e52cSAmir Goldstein /**
44dae1e52cSAmir Goldstein * ext4_block_to_path - parse the block number into array of offsets
45dae1e52cSAmir Goldstein * @inode: inode in question (we are only interested in its superblock)
46dae1e52cSAmir Goldstein * @i_block: block number to be parsed
47dae1e52cSAmir Goldstein * @offsets: array to store the offsets in
48dae1e52cSAmir Goldstein * @boundary: set this non-zero if the referred-to block is likely to be
49dae1e52cSAmir Goldstein * followed (on disk) by an indirect block.
50dae1e52cSAmir Goldstein *
51dae1e52cSAmir Goldstein * To store the locations of file's data ext4 uses a data structure common
52dae1e52cSAmir Goldstein * for UNIX filesystems - tree of pointers anchored in the inode, with
53dae1e52cSAmir Goldstein * data blocks at leaves and indirect blocks in intermediate nodes.
54dae1e52cSAmir Goldstein * This function translates the block number into path in that tree -
55dae1e52cSAmir Goldstein * return value is the path length and @offsets[n] is the offset of
56dae1e52cSAmir Goldstein * pointer to (n+1)th node in the nth one. If @block is out of range
57dae1e52cSAmir Goldstein * (negative or too large) warning is printed and zero returned.
58dae1e52cSAmir Goldstein *
59dae1e52cSAmir Goldstein * Note: function doesn't find node addresses, so no IO is needed. All
60dae1e52cSAmir Goldstein * we need to know is the capacity of indirect blocks (taken from the
61dae1e52cSAmir Goldstein * inode->i_sb).
62dae1e52cSAmir Goldstein */
63dae1e52cSAmir Goldstein
64dae1e52cSAmir Goldstein /*
65dae1e52cSAmir Goldstein * Portability note: the last comparison (check that we fit into triple
66dae1e52cSAmir Goldstein * indirect block) is spelled differently, because otherwise on an
67dae1e52cSAmir Goldstein * architecture with 32-bit longs and 8Kb pages we might get into trouble
68dae1e52cSAmir Goldstein * if our filesystem had 8Kb blocks. We might use long long, but that would
69dae1e52cSAmir Goldstein * kill us on x86. Oh, well, at least the sign propagation does not matter -
70dae1e52cSAmir Goldstein * i_block would have to be negative in the very beginning, so we would not
71dae1e52cSAmir Goldstein * get there at all.
72dae1e52cSAmir Goldstein */
73dae1e52cSAmir Goldstein
ext4_block_to_path(struct inode * inode,ext4_lblk_t i_block,ext4_lblk_t offsets[4],int * boundary)74dae1e52cSAmir Goldstein static int ext4_block_to_path(struct inode *inode,
75dae1e52cSAmir Goldstein ext4_lblk_t i_block,
76dae1e52cSAmir Goldstein ext4_lblk_t offsets[4], int *boundary)
77dae1e52cSAmir Goldstein {
78dae1e52cSAmir Goldstein int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
79dae1e52cSAmir Goldstein int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
80dae1e52cSAmir Goldstein const long direct_blocks = EXT4_NDIR_BLOCKS,
81dae1e52cSAmir Goldstein indirect_blocks = ptrs,
82dae1e52cSAmir Goldstein double_blocks = (1 << (ptrs_bits * 2));
83dae1e52cSAmir Goldstein int n = 0;
84dae1e52cSAmir Goldstein int final = 0;
85dae1e52cSAmir Goldstein
86dae1e52cSAmir Goldstein if (i_block < direct_blocks) {
87dae1e52cSAmir Goldstein offsets[n++] = i_block;
88dae1e52cSAmir Goldstein final = direct_blocks;
89dae1e52cSAmir Goldstein } else if ((i_block -= direct_blocks) < indirect_blocks) {
90dae1e52cSAmir Goldstein offsets[n++] = EXT4_IND_BLOCK;
91dae1e52cSAmir Goldstein offsets[n++] = i_block;
92dae1e52cSAmir Goldstein final = ptrs;
93dae1e52cSAmir Goldstein } else if ((i_block -= indirect_blocks) < double_blocks) {
94dae1e52cSAmir Goldstein offsets[n++] = EXT4_DIND_BLOCK;
95dae1e52cSAmir Goldstein offsets[n++] = i_block >> ptrs_bits;
96dae1e52cSAmir Goldstein offsets[n++] = i_block & (ptrs - 1);
97dae1e52cSAmir Goldstein final = ptrs;
98dae1e52cSAmir Goldstein } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
99dae1e52cSAmir Goldstein offsets[n++] = EXT4_TIND_BLOCK;
100dae1e52cSAmir Goldstein offsets[n++] = i_block >> (ptrs_bits * 2);
101dae1e52cSAmir Goldstein offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
102dae1e52cSAmir Goldstein offsets[n++] = i_block & (ptrs - 1);
103dae1e52cSAmir Goldstein final = ptrs;
104dae1e52cSAmir Goldstein } else {
105dae1e52cSAmir Goldstein ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
106dae1e52cSAmir Goldstein i_block + direct_blocks +
107dae1e52cSAmir Goldstein indirect_blocks + double_blocks, inode->i_ino);
108dae1e52cSAmir Goldstein }
109dae1e52cSAmir Goldstein if (boundary)
110dae1e52cSAmir Goldstein *boundary = final - 1 - (i_block & (ptrs - 1));
111dae1e52cSAmir Goldstein return n;
112dae1e52cSAmir Goldstein }
113dae1e52cSAmir Goldstein
114dae1e52cSAmir Goldstein /**
115dae1e52cSAmir Goldstein * ext4_get_branch - read the chain of indirect blocks leading to data
116dae1e52cSAmir Goldstein * @inode: inode in question
117dae1e52cSAmir Goldstein * @depth: depth of the chain (1 - direct pointer, etc.)
118dae1e52cSAmir Goldstein * @offsets: offsets of pointers in inode/indirect blocks
119dae1e52cSAmir Goldstein * @chain: place to store the result
120dae1e52cSAmir Goldstein * @err: here we store the error value
121dae1e52cSAmir Goldstein *
122dae1e52cSAmir Goldstein * Function fills the array of triples <key, p, bh> and returns %NULL
123dae1e52cSAmir Goldstein * if everything went OK or the pointer to the last filled triple
124dae1e52cSAmir Goldstein * (incomplete one) otherwise. Upon the return chain[i].key contains
125dae1e52cSAmir Goldstein * the number of (i+1)-th block in the chain (as it is stored in memory,
126dae1e52cSAmir Goldstein * i.e. little-endian 32-bit), chain[i].p contains the address of that
127dae1e52cSAmir Goldstein * number (it points into struct inode for i==0 and into the bh->b_data
128dae1e52cSAmir Goldstein * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
129dae1e52cSAmir Goldstein * block for i>0 and NULL for i==0. In other words, it holds the block
130dae1e52cSAmir Goldstein * numbers of the chain, addresses they were taken from (and where we can
131dae1e52cSAmir Goldstein * verify that chain did not change) and buffer_heads hosting these
132dae1e52cSAmir Goldstein * numbers.
133dae1e52cSAmir Goldstein *
134dae1e52cSAmir Goldstein * Function stops when it stumbles upon zero pointer (absent block)
135dae1e52cSAmir Goldstein * (pointer to last triple returned, *@err == 0)
136dae1e52cSAmir Goldstein * or when it gets an IO error reading an indirect block
137dae1e52cSAmir Goldstein * (ditto, *@err == -EIO)
138dae1e52cSAmir Goldstein * or when it reads all @depth-1 indirect blocks successfully and finds
139dae1e52cSAmir Goldstein * the whole chain, all way to the data (returns %NULL, *err == 0).
140dae1e52cSAmir Goldstein *
141dae1e52cSAmir Goldstein * Need to be called with
142dae1e52cSAmir Goldstein * down_read(&EXT4_I(inode)->i_data_sem)
143dae1e52cSAmir Goldstein */
ext4_get_branch(struct inode * inode,int depth,ext4_lblk_t * offsets,Indirect chain[4],int * err)144dae1e52cSAmir Goldstein static Indirect *ext4_get_branch(struct inode *inode, int depth,
145dae1e52cSAmir Goldstein ext4_lblk_t *offsets,
146dae1e52cSAmir Goldstein Indirect chain[4], int *err)
147dae1e52cSAmir Goldstein {
148dae1e52cSAmir Goldstein struct super_block *sb = inode->i_sb;
149dae1e52cSAmir Goldstein Indirect *p = chain;
150dae1e52cSAmir Goldstein struct buffer_head *bh;
15126d75a16SLuís Henriques unsigned int key;
152860d21e2STheodore Ts'o int ret = -EIO;
153dae1e52cSAmir Goldstein
154dae1e52cSAmir Goldstein *err = 0;
155dae1e52cSAmir Goldstein /* i_data is not going away, no lock needed */
156dae1e52cSAmir Goldstein add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
157dae1e52cSAmir Goldstein if (!p->key)
158dae1e52cSAmir Goldstein goto no_block;
159dae1e52cSAmir Goldstein while (--depth) {
16026d75a16SLuís Henriques key = le32_to_cpu(p->key);
16126d75a16SLuís Henriques if (key > ext4_blocks_count(EXT4_SB(sb)->s_es)) {
16226d75a16SLuís Henriques /* the block was out of range */
16326d75a16SLuís Henriques ret = -EFSCORRUPTED;
16426d75a16SLuís Henriques goto failure;
16526d75a16SLuís Henriques }
16626d75a16SLuís Henriques bh = sb_getblk(sb, key);
167860d21e2STheodore Ts'o if (unlikely(!bh)) {
168860d21e2STheodore Ts'o ret = -ENOMEM;
169dae1e52cSAmir Goldstein goto failure;
170860d21e2STheodore Ts'o }
171dae1e52cSAmir Goldstein
172dae1e52cSAmir Goldstein if (!bh_uptodate_or_lock(bh)) {
173*77035e4dSLong Li if (ext4_read_bh(bh, 0, NULL, false) < 0) {
174dae1e52cSAmir Goldstein put_bh(bh);
175dae1e52cSAmir Goldstein goto failure;
176dae1e52cSAmir Goldstein }
177dae1e52cSAmir Goldstein /* validate block references */
178dae1e52cSAmir Goldstein if (ext4_check_indirect_blockref(inode, bh)) {
179dae1e52cSAmir Goldstein put_bh(bh);
180dae1e52cSAmir Goldstein goto failure;
181dae1e52cSAmir Goldstein }
182dae1e52cSAmir Goldstein }
183dae1e52cSAmir Goldstein
184dae1e52cSAmir Goldstein add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
185dae1e52cSAmir Goldstein /* Reader: end */
186dae1e52cSAmir Goldstein if (!p->key)
187dae1e52cSAmir Goldstein goto no_block;
188dae1e52cSAmir Goldstein }
189dae1e52cSAmir Goldstein return NULL;
190dae1e52cSAmir Goldstein
191dae1e52cSAmir Goldstein failure:
192860d21e2STheodore Ts'o *err = ret;
193dae1e52cSAmir Goldstein no_block:
194dae1e52cSAmir Goldstein return p;
195dae1e52cSAmir Goldstein }
196dae1e52cSAmir Goldstein
197dae1e52cSAmir Goldstein /**
198dae1e52cSAmir Goldstein * ext4_find_near - find a place for allocation with sufficient locality
199dae1e52cSAmir Goldstein * @inode: owner
200dae1e52cSAmir Goldstein * @ind: descriptor of indirect block.
201dae1e52cSAmir Goldstein *
202dae1e52cSAmir Goldstein * This function returns the preferred place for block allocation.
203dae1e52cSAmir Goldstein * It is used when heuristic for sequential allocation fails.
204dae1e52cSAmir Goldstein * Rules are:
205dae1e52cSAmir Goldstein * + if there is a block to the left of our position - allocate near it.
206dae1e52cSAmir Goldstein * + if pointer will live in indirect block - allocate near that block.
207dae1e52cSAmir Goldstein * + if pointer will live in inode - allocate in the same
208dae1e52cSAmir Goldstein * cylinder group.
209dae1e52cSAmir Goldstein *
210dae1e52cSAmir Goldstein * In the latter case we colour the starting block by the callers PID to
211dae1e52cSAmir Goldstein * prevent it from clashing with concurrent allocations for a different inode
212dae1e52cSAmir Goldstein * in the same block group. The PID is used here so that functionally related
213dae1e52cSAmir Goldstein * files will be close-by on-disk.
214dae1e52cSAmir Goldstein *
215dae1e52cSAmir Goldstein * Caller must make sure that @ind is valid and will stay that way.
216dae1e52cSAmir Goldstein */
ext4_find_near(struct inode * inode,Indirect * ind)217dae1e52cSAmir Goldstein static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
218dae1e52cSAmir Goldstein {
219dae1e52cSAmir Goldstein struct ext4_inode_info *ei = EXT4_I(inode);
220dae1e52cSAmir Goldstein __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
221dae1e52cSAmir Goldstein __le32 *p;
222dae1e52cSAmir Goldstein
223dae1e52cSAmir Goldstein /* Try to find previous block */
224dae1e52cSAmir Goldstein for (p = ind->p - 1; p >= start; p--) {
225dae1e52cSAmir Goldstein if (*p)
226dae1e52cSAmir Goldstein return le32_to_cpu(*p);
227dae1e52cSAmir Goldstein }
228dae1e52cSAmir Goldstein
229dae1e52cSAmir Goldstein /* No such thing, so let's try location of indirect block */
230dae1e52cSAmir Goldstein if (ind->bh)
231dae1e52cSAmir Goldstein return ind->bh->b_blocknr;
232dae1e52cSAmir Goldstein
233dae1e52cSAmir Goldstein /*
234dae1e52cSAmir Goldstein * It is going to be referred to from the inode itself? OK, just put it
235dae1e52cSAmir Goldstein * into the same cylinder group then.
236dae1e52cSAmir Goldstein */
237f86186b4SEric Sandeen return ext4_inode_to_goal_block(inode);
238dae1e52cSAmir Goldstein }
239dae1e52cSAmir Goldstein
240dae1e52cSAmir Goldstein /**
241dae1e52cSAmir Goldstein * ext4_find_goal - find a preferred place for allocation.
242dae1e52cSAmir Goldstein * @inode: owner
243dae1e52cSAmir Goldstein * @block: block we want
244dae1e52cSAmir Goldstein * @partial: pointer to the last triple within a chain
245dae1e52cSAmir Goldstein *
246dae1e52cSAmir Goldstein * Normally this function find the preferred place for block allocation,
247dae1e52cSAmir Goldstein * returns it.
248dae1e52cSAmir Goldstein * Because this is only used for non-extent files, we limit the block nr
249dae1e52cSAmir Goldstein * to 32 bits.
250dae1e52cSAmir Goldstein */
ext4_find_goal(struct inode * inode,ext4_lblk_t block,Indirect * partial)251dae1e52cSAmir Goldstein static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
252dae1e52cSAmir Goldstein Indirect *partial)
253dae1e52cSAmir Goldstein {
254dae1e52cSAmir Goldstein ext4_fsblk_t goal;
255dae1e52cSAmir Goldstein
256dae1e52cSAmir Goldstein /*
257dae1e52cSAmir Goldstein * XXX need to get goal block from mballoc's data structures
258dae1e52cSAmir Goldstein */
259dae1e52cSAmir Goldstein
260dae1e52cSAmir Goldstein goal = ext4_find_near(inode, partial);
261dae1e52cSAmir Goldstein goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
262dae1e52cSAmir Goldstein return goal;
263dae1e52cSAmir Goldstein }
264dae1e52cSAmir Goldstein
265dae1e52cSAmir Goldstein /**
266dae1e52cSAmir Goldstein * ext4_blks_to_allocate - Look up the block map and count the number
267dae1e52cSAmir Goldstein * of direct blocks need to be allocated for the given branch.
268dae1e52cSAmir Goldstein *
269dae1e52cSAmir Goldstein * @branch: chain of indirect blocks
270dae1e52cSAmir Goldstein * @k: number of blocks need for indirect blocks
271dae1e52cSAmir Goldstein * @blks: number of data blocks to be mapped.
272dae1e52cSAmir Goldstein * @blocks_to_boundary: the offset in the indirect block
273dae1e52cSAmir Goldstein *
274dae1e52cSAmir Goldstein * return the total number of blocks to be allocate, including the
275dae1e52cSAmir Goldstein * direct and indirect blocks.
276dae1e52cSAmir Goldstein */
ext4_blks_to_allocate(Indirect * branch,int k,unsigned int blks,int blocks_to_boundary)277dae1e52cSAmir Goldstein static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
278dae1e52cSAmir Goldstein int blocks_to_boundary)
279dae1e52cSAmir Goldstein {
280dae1e52cSAmir Goldstein unsigned int count = 0;
281dae1e52cSAmir Goldstein
282dae1e52cSAmir Goldstein /*
283dae1e52cSAmir Goldstein * Simple case, [t,d]Indirect block(s) has not allocated yet
284dae1e52cSAmir Goldstein * then it's clear blocks on that path have not allocated
285dae1e52cSAmir Goldstein */
286dae1e52cSAmir Goldstein if (k > 0) {
287dae1e52cSAmir Goldstein /* right now we don't handle cross boundary allocation */
288dae1e52cSAmir Goldstein if (blks < blocks_to_boundary + 1)
289dae1e52cSAmir Goldstein count += blks;
290dae1e52cSAmir Goldstein else
291dae1e52cSAmir Goldstein count += blocks_to_boundary + 1;
292dae1e52cSAmir Goldstein return count;
293dae1e52cSAmir Goldstein }
294dae1e52cSAmir Goldstein
295dae1e52cSAmir Goldstein count++;
296dae1e52cSAmir Goldstein while (count < blks && count <= blocks_to_boundary &&
297dae1e52cSAmir Goldstein le32_to_cpu(*(branch[0].p + count)) == 0) {
298dae1e52cSAmir Goldstein count++;
299dae1e52cSAmir Goldstein }
300dae1e52cSAmir Goldstein return count;
301dae1e52cSAmir Goldstein }
302dae1e52cSAmir Goldstein
303dae1e52cSAmir Goldstein /**
304c60990b3STheodore Ts'o * ext4_alloc_branch() - allocate and set up a chain of blocks
305dae1e52cSAmir Goldstein * @handle: handle for this transaction
306c60990b3STheodore Ts'o * @ar: structure describing the allocation request
307dae1e52cSAmir Goldstein * @indirect_blks: number of allocated indirect blocks
308dae1e52cSAmir Goldstein * @offsets: offsets (in the blocks) to store the pointers to next.
309dae1e52cSAmir Goldstein * @branch: place to store the chain in.
310dae1e52cSAmir Goldstein *
311dae1e52cSAmir Goldstein * This function allocates blocks, zeroes out all but the last one,
312dae1e52cSAmir Goldstein * links them into chain and (if we are synchronous) writes them to disk.
313dae1e52cSAmir Goldstein * In other words, it prepares a branch that can be spliced onto the
314dae1e52cSAmir Goldstein * inode. It stores the information about that chain in the branch[], in
315dae1e52cSAmir Goldstein * the same format as ext4_get_branch() would do. We are calling it after
316dae1e52cSAmir Goldstein * we had read the existing part of chain and partial points to the last
317dae1e52cSAmir Goldstein * triple of that (one with zero ->key). Upon the exit we have the same
318dae1e52cSAmir Goldstein * picture as after the successful ext4_get_block(), except that in one
319dae1e52cSAmir Goldstein * place chain is disconnected - *branch->p is still zero (we did not
320dae1e52cSAmir Goldstein * set the last link), but branch->key contains the number that should
321dae1e52cSAmir Goldstein * be placed into *branch->p to fill that gap.
322dae1e52cSAmir Goldstein *
323dae1e52cSAmir Goldstein * If allocation fails we free all blocks we've allocated (and forget
324dae1e52cSAmir Goldstein * their buffer_heads) and return the error value the from failed
325dae1e52cSAmir Goldstein * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
326dae1e52cSAmir Goldstein * as described above and return 0.
327dae1e52cSAmir Goldstein */
ext4_alloc_branch(handle_t * handle,struct ext4_allocation_request * ar,int indirect_blks,ext4_lblk_t * offsets,Indirect * branch)328a5211002STheodore Ts'o static int ext4_alloc_branch(handle_t *handle,
329a5211002STheodore Ts'o struct ext4_allocation_request *ar,
330a5211002STheodore Ts'o int indirect_blks, ext4_lblk_t *offsets,
331a5211002STheodore Ts'o Indirect *branch)
332dae1e52cSAmir Goldstein {
333dae1e52cSAmir Goldstein struct buffer_head * bh;
334781f143eSTheodore Ts'o ext4_fsblk_t b, new_blocks[4];
335781f143eSTheodore Ts'o __le32 *p;
336781f143eSTheodore Ts'o int i, j, err, len = 1;
337dae1e52cSAmir Goldstein
338781f143eSTheodore Ts'o for (i = 0; i <= indirect_blks; i++) {
339781f143eSTheodore Ts'o if (i == indirect_blks) {
340a5211002STheodore Ts'o new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err);
341f2890730SJan Kara } else {
342a5211002STheodore Ts'o ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle,
343e3cf5d5dSTheodore Ts'o ar->inode, ar->goal,
344e3cf5d5dSTheodore Ts'o ar->flags & EXT4_MB_DELALLOC_RESERVED,
345e3cf5d5dSTheodore Ts'o NULL, &err);
346f2890730SJan Kara /* Simplify error cleanup... */
347f2890730SJan Kara branch[i+1].bh = NULL;
348f2890730SJan Kara }
349781f143eSTheodore Ts'o if (err) {
350781f143eSTheodore Ts'o i--;
351781f143eSTheodore Ts'o goto failed;
352781f143eSTheodore Ts'o }
353781f143eSTheodore Ts'o branch[i].key = cpu_to_le32(new_blocks[i]);
354781f143eSTheodore Ts'o if (i == 0)
355781f143eSTheodore Ts'o continue;
356781f143eSTheodore Ts'o
357a5211002STheodore Ts'o bh = branch[i].bh = sb_getblk(ar->inode->i_sb, new_blocks[i-1]);
358dae1e52cSAmir Goldstein if (unlikely(!bh)) {
359860d21e2STheodore Ts'o err = -ENOMEM;
360dae1e52cSAmir Goldstein goto failed;
361dae1e52cSAmir Goldstein }
362dae1e52cSAmir Goldstein lock_buffer(bh);
363dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "call get_create_access");
364188c299eSJan Kara err = ext4_journal_get_create_access(handle, ar->inode->i_sb,
365188c299eSJan Kara bh, EXT4_JTR_NONE);
366dae1e52cSAmir Goldstein if (err) {
367dae1e52cSAmir Goldstein unlock_buffer(bh);
368dae1e52cSAmir Goldstein goto failed;
369dae1e52cSAmir Goldstein }
370dae1e52cSAmir Goldstein
371781f143eSTheodore Ts'o memset(bh->b_data, 0, bh->b_size);
372781f143eSTheodore Ts'o p = branch[i].p = (__le32 *) bh->b_data + offsets[i];
373781f143eSTheodore Ts'o b = new_blocks[i];
374781f143eSTheodore Ts'o
375781f143eSTheodore Ts'o if (i == indirect_blks)
376a5211002STheodore Ts'o len = ar->len;
377781f143eSTheodore Ts'o for (j = 0; j < len; j++)
378781f143eSTheodore Ts'o *p++ = cpu_to_le32(b++);
379781f143eSTheodore Ts'o
380dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "marking uptodate");
381dae1e52cSAmir Goldstein set_buffer_uptodate(bh);
382dae1e52cSAmir Goldstein unlock_buffer(bh);
383dae1e52cSAmir Goldstein
384dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
385a5211002STheodore Ts'o err = ext4_handle_dirty_metadata(handle, ar->inode, bh);
386dae1e52cSAmir Goldstein if (err)
387dae1e52cSAmir Goldstein goto failed;
388dae1e52cSAmir Goldstein }
389781f143eSTheodore Ts'o return 0;
390dae1e52cSAmir Goldstein failed:
391f2890730SJan Kara if (i == indirect_blks) {
392f2890730SJan Kara /* Free data blocks */
393f2890730SJan Kara ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i],
394f2890730SJan Kara ar->len, 0);
395f2890730SJan Kara i--;
396f2890730SJan Kara }
397781f143eSTheodore Ts'o for (; i >= 0; i--) {
398c5c7b8ddSJan Kara /*
399c5c7b8ddSJan Kara * We want to ext4_forget() only freshly allocated indirect
400f2890730SJan Kara * blocks. Buffer for new_blocks[i] is at branch[i+1].bh
401f2890730SJan Kara * (buffer at branch[0].bh is indirect block / inode already
402f2890730SJan Kara * existing before ext4_alloc_branch() was called). Also
403f2890730SJan Kara * because blocks are freshly allocated, we don't need to
404f2890730SJan Kara * revoke them which is why we don't set
405f2890730SJan Kara * EXT4_FREE_BLOCKS_METADATA.
406c5c7b8ddSJan Kara */
407f2890730SJan Kara ext4_free_blocks(handle, ar->inode, branch[i+1].bh,
408f2890730SJan Kara new_blocks[i], 1,
409f2890730SJan Kara branch[i+1].bh ? EXT4_FREE_BLOCKS_FORGET : 0);
410dae1e52cSAmir Goldstein }
411dae1e52cSAmir Goldstein return err;
412dae1e52cSAmir Goldstein }
413dae1e52cSAmir Goldstein
414dae1e52cSAmir Goldstein /**
415c60990b3STheodore Ts'o * ext4_splice_branch() - splice the allocated branch onto inode.
416dae1e52cSAmir Goldstein * @handle: handle for this transaction
417c60990b3STheodore Ts'o * @ar: structure describing the allocation request
418dae1e52cSAmir Goldstein * @where: location of missing link
419dae1e52cSAmir Goldstein * @num: number of indirect blocks we are adding
420dae1e52cSAmir Goldstein *
421dae1e52cSAmir Goldstein * This function fills the missing link and does all housekeeping needed in
422dae1e52cSAmir Goldstein * inode (->i_blocks, etc.). In case of success we end up with the full
423dae1e52cSAmir Goldstein * chain to new block and return 0.
424dae1e52cSAmir Goldstein */
ext4_splice_branch(handle_t * handle,struct ext4_allocation_request * ar,Indirect * where,int num)425a5211002STheodore Ts'o static int ext4_splice_branch(handle_t *handle,
426a5211002STheodore Ts'o struct ext4_allocation_request *ar,
427a5211002STheodore Ts'o Indirect *where, int num)
428dae1e52cSAmir Goldstein {
429dae1e52cSAmir Goldstein int i;
430dae1e52cSAmir Goldstein int err = 0;
431dae1e52cSAmir Goldstein ext4_fsblk_t current_block;
432dae1e52cSAmir Goldstein
433dae1e52cSAmir Goldstein /*
434dae1e52cSAmir Goldstein * If we're splicing into a [td]indirect block (as opposed to the
435dae1e52cSAmir Goldstein * inode) then we need to get write access to the [td]indirect block
436dae1e52cSAmir Goldstein * before the splice.
437dae1e52cSAmir Goldstein */
438dae1e52cSAmir Goldstein if (where->bh) {
439dae1e52cSAmir Goldstein BUFFER_TRACE(where->bh, "get_write_access");
440188c299eSJan Kara err = ext4_journal_get_write_access(handle, ar->inode->i_sb,
441188c299eSJan Kara where->bh, EXT4_JTR_NONE);
442dae1e52cSAmir Goldstein if (err)
443dae1e52cSAmir Goldstein goto err_out;
444dae1e52cSAmir Goldstein }
445dae1e52cSAmir Goldstein /* That's it */
446dae1e52cSAmir Goldstein
447dae1e52cSAmir Goldstein *where->p = where->key;
448dae1e52cSAmir Goldstein
449dae1e52cSAmir Goldstein /*
450dae1e52cSAmir Goldstein * Update the host buffer_head or inode to point to more just allocated
451dae1e52cSAmir Goldstein * direct blocks blocks
452dae1e52cSAmir Goldstein */
453a5211002STheodore Ts'o if (num == 0 && ar->len > 1) {
454dae1e52cSAmir Goldstein current_block = le32_to_cpu(where->key) + 1;
455a5211002STheodore Ts'o for (i = 1; i < ar->len; i++)
456dae1e52cSAmir Goldstein *(where->p + i) = cpu_to_le32(current_block++);
457dae1e52cSAmir Goldstein }
458dae1e52cSAmir Goldstein
459dae1e52cSAmir Goldstein /* We are done with atomic stuff, now do the rest of housekeeping */
460dae1e52cSAmir Goldstein /* had we spliced it onto indirect block? */
461dae1e52cSAmir Goldstein if (where->bh) {
462dae1e52cSAmir Goldstein /*
463dae1e52cSAmir Goldstein * If we spliced it onto an indirect block, we haven't
464dae1e52cSAmir Goldstein * altered the inode. Note however that if it is being spliced
465dae1e52cSAmir Goldstein * onto an indirect block at the very end of the file (the
466dae1e52cSAmir Goldstein * file is growing) then we *will* alter the inode to reflect
467dae1e52cSAmir Goldstein * the new i_size. But that is not done here - it is done in
468dae1e52cSAmir Goldstein * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
469dae1e52cSAmir Goldstein */
4704978c659SJan Kara ext4_debug("splicing indirect only\n");
471dae1e52cSAmir Goldstein BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
472a5211002STheodore Ts'o err = ext4_handle_dirty_metadata(handle, ar->inode, where->bh);
473dae1e52cSAmir Goldstein if (err)
474dae1e52cSAmir Goldstein goto err_out;
475dae1e52cSAmir Goldstein } else {
476dae1e52cSAmir Goldstein /*
477dae1e52cSAmir Goldstein * OK, we spliced it into the inode itself on a direct block.
478dae1e52cSAmir Goldstein */
4794209ae12SHarshad Shirwadkar err = ext4_mark_inode_dirty(handle, ar->inode);
4804209ae12SHarshad Shirwadkar if (unlikely(err))
4814209ae12SHarshad Shirwadkar goto err_out;
4824978c659SJan Kara ext4_debug("splicing direct\n");
483dae1e52cSAmir Goldstein }
484dae1e52cSAmir Goldstein return err;
485dae1e52cSAmir Goldstein
486dae1e52cSAmir Goldstein err_out:
487dae1e52cSAmir Goldstein for (i = 1; i <= num; i++) {
488dae1e52cSAmir Goldstein /*
489dae1e52cSAmir Goldstein * branch[i].bh is newly allocated, so there is no
490dae1e52cSAmir Goldstein * need to revoke the block, which is why we don't
491dae1e52cSAmir Goldstein * need to set EXT4_FREE_BLOCKS_METADATA.
492dae1e52cSAmir Goldstein */
493a5211002STheodore Ts'o ext4_free_blocks(handle, ar->inode, where[i].bh, 0, 1,
494dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_FORGET);
495dae1e52cSAmir Goldstein }
496a5211002STheodore Ts'o ext4_free_blocks(handle, ar->inode, NULL, le32_to_cpu(where[num].key),
497a5211002STheodore Ts'o ar->len, 0);
498dae1e52cSAmir Goldstein
499dae1e52cSAmir Goldstein return err;
500dae1e52cSAmir Goldstein }
501dae1e52cSAmir Goldstein
502dae1e52cSAmir Goldstein /*
503dae1e52cSAmir Goldstein * The ext4_ind_map_blocks() function handles non-extents inodes
504dae1e52cSAmir Goldstein * (i.e., using the traditional indirect/double-indirect i_blocks
505dae1e52cSAmir Goldstein * scheme) for ext4_map_blocks().
506dae1e52cSAmir Goldstein *
507dae1e52cSAmir Goldstein * Allocation strategy is simple: if we have to allocate something, we will
508dae1e52cSAmir Goldstein * have to go the whole way to leaf. So let's do it before attaching anything
509dae1e52cSAmir Goldstein * to tree, set linkage between the newborn blocks, write them if sync is
510dae1e52cSAmir Goldstein * required, recheck the path, free and repeat if check fails, otherwise
511dae1e52cSAmir Goldstein * set the last missing link (that will protect us from any truncate-generated
512dae1e52cSAmir Goldstein * removals - all blocks on the path are immune now) and possibly force the
513dae1e52cSAmir Goldstein * write on the parent block.
514dae1e52cSAmir Goldstein * That has a nice additional property: no special recovery from the failed
515dae1e52cSAmir Goldstein * allocations is needed - we simply release blocks and do not touch anything
516dae1e52cSAmir Goldstein * reachable from inode.
517dae1e52cSAmir Goldstein *
518dae1e52cSAmir Goldstein * `handle' can be NULL if create == 0.
519dae1e52cSAmir Goldstein *
520dae1e52cSAmir Goldstein * return > 0, # of blocks mapped or allocated.
521dae1e52cSAmir Goldstein * return = 0, if plain lookup failed.
522dae1e52cSAmir Goldstein * return < 0, error case.
523dae1e52cSAmir Goldstein *
524dae1e52cSAmir Goldstein * The ext4_ind_get_blocks() function should be called with
525dae1e52cSAmir Goldstein * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
526dae1e52cSAmir Goldstein * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
527dae1e52cSAmir Goldstein * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
528dae1e52cSAmir Goldstein * blocks.
529dae1e52cSAmir Goldstein */
ext4_ind_map_blocks(handle_t * handle,struct inode * inode,struct ext4_map_blocks * map,int flags)530dae1e52cSAmir Goldstein int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
531dae1e52cSAmir Goldstein struct ext4_map_blocks *map,
532dae1e52cSAmir Goldstein int flags)
533dae1e52cSAmir Goldstein {
534a5211002STheodore Ts'o struct ext4_allocation_request ar;
535dae1e52cSAmir Goldstein int err = -EIO;
536dae1e52cSAmir Goldstein ext4_lblk_t offsets[4];
537dae1e52cSAmir Goldstein Indirect chain[4];
538dae1e52cSAmir Goldstein Indirect *partial;
539dae1e52cSAmir Goldstein int indirect_blks;
540dae1e52cSAmir Goldstein int blocks_to_boundary = 0;
541dae1e52cSAmir Goldstein int depth;
542dae1e52cSAmir Goldstein int count = 0;
543dae1e52cSAmir Goldstein ext4_fsblk_t first_block = 0;
544dae1e52cSAmir Goldstein
545dae1e52cSAmir Goldstein trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
546837c23fbSChunguang Xu ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
547837c23fbSChunguang Xu ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
548dae1e52cSAmir Goldstein depth = ext4_block_to_path(inode, map->m_lblk, offsets,
549dae1e52cSAmir Goldstein &blocks_to_boundary);
550dae1e52cSAmir Goldstein
551dae1e52cSAmir Goldstein if (depth == 0)
552dae1e52cSAmir Goldstein goto out;
553dae1e52cSAmir Goldstein
554dae1e52cSAmir Goldstein partial = ext4_get_branch(inode, depth, offsets, chain, &err);
555dae1e52cSAmir Goldstein
556dae1e52cSAmir Goldstein /* Simplest case - block found, no allocation needed */
557dae1e52cSAmir Goldstein if (!partial) {
558dae1e52cSAmir Goldstein first_block = le32_to_cpu(chain[depth - 1].key);
559dae1e52cSAmir Goldstein count++;
560dae1e52cSAmir Goldstein /*map more blocks*/
561dae1e52cSAmir Goldstein while (count < map->m_len && count <= blocks_to_boundary) {
562dae1e52cSAmir Goldstein ext4_fsblk_t blk;
563dae1e52cSAmir Goldstein
564dae1e52cSAmir Goldstein blk = le32_to_cpu(*(chain[depth-1].p + count));
565dae1e52cSAmir Goldstein
566dae1e52cSAmir Goldstein if (blk == first_block + count)
567dae1e52cSAmir Goldstein count++;
568dae1e52cSAmir Goldstein else
569dae1e52cSAmir Goldstein break;
570dae1e52cSAmir Goldstein }
571dae1e52cSAmir Goldstein goto got_it;
572dae1e52cSAmir Goldstein }
573dae1e52cSAmir Goldstein
574facab4d9SJan Kara /* Next simple case - plain lookup failed */
575facab4d9SJan Kara if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
576facab4d9SJan Kara unsigned epb = inode->i_sb->s_blocksize / sizeof(u32);
577facab4d9SJan Kara int i;
578facab4d9SJan Kara
5792ee3ee06SJan Kara /*
5802ee3ee06SJan Kara * Count number blocks in a subtree under 'partial'. At each
5812ee3ee06SJan Kara * level we count number of complete empty subtrees beyond
5822ee3ee06SJan Kara * current offset and then descend into the subtree only
5832ee3ee06SJan Kara * partially beyond current offset.
5842ee3ee06SJan Kara */
5852ee3ee06SJan Kara count = 0;
5862ee3ee06SJan Kara for (i = partial - chain + 1; i < depth; i++)
5872ee3ee06SJan Kara count = count * epb + (epb - offsets[i] - 1);
5882ee3ee06SJan Kara count++;
589facab4d9SJan Kara /* Fill in size of a hole we found */
590facab4d9SJan Kara map->m_pblk = 0;
591facab4d9SJan Kara map->m_len = min_t(unsigned int, map->m_len, count);
592facab4d9SJan Kara goto cleanup;
593facab4d9SJan Kara }
594facab4d9SJan Kara
595facab4d9SJan Kara /* Failed read of indirect block */
596facab4d9SJan Kara if (err == -EIO)
597dae1e52cSAmir Goldstein goto cleanup;
598dae1e52cSAmir Goldstein
599dae1e52cSAmir Goldstein /*
600dae1e52cSAmir Goldstein * Okay, we need to do block allocation.
601dae1e52cSAmir Goldstein */
602e2b911c5SDarrick J. Wong if (ext4_has_feature_bigalloc(inode->i_sb)) {
603bab08ab9STheodore Ts'o EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
604bab08ab9STheodore Ts'o "non-extent mapped inodes with bigalloc");
6052be7d717SZhang Qilong err = -EFSCORRUPTED;
6062be7d717SZhang Qilong goto out;
607bab08ab9STheodore Ts'o }
608bab08ab9STheodore Ts'o
609a5211002STheodore Ts'o /* Set up for the direct block allocation */
610a5211002STheodore Ts'o memset(&ar, 0, sizeof(ar));
611a5211002STheodore Ts'o ar.inode = inode;
612a5211002STheodore Ts'o ar.logical = map->m_lblk;
613a5211002STheodore Ts'o if (S_ISREG(inode->i_mode))
614a5211002STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA;
615e3cf5d5dSTheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
616e3cf5d5dSTheodore Ts'o ar.flags |= EXT4_MB_DELALLOC_RESERVED;
617c5e298aeSTheodore Ts'o if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
618c5e298aeSTheodore Ts'o ar.flags |= EXT4_MB_USE_RESERVED;
619a5211002STheodore Ts'o
620a5211002STheodore Ts'o ar.goal = ext4_find_goal(inode, map->m_lblk, partial);
621dae1e52cSAmir Goldstein
622dae1e52cSAmir Goldstein /* the number of blocks need to allocate for [d,t]indirect blocks */
623dae1e52cSAmir Goldstein indirect_blks = (chain + depth) - partial - 1;
624dae1e52cSAmir Goldstein
625dae1e52cSAmir Goldstein /*
626dae1e52cSAmir Goldstein * Next look up the indirect map to count the totoal number of
627dae1e52cSAmir Goldstein * direct blocks to allocate for this branch.
628dae1e52cSAmir Goldstein */
629a5211002STheodore Ts'o ar.len = ext4_blks_to_allocate(partial, indirect_blks,
630dae1e52cSAmir Goldstein map->m_len, blocks_to_boundary);
631a5211002STheodore Ts'o
632dae1e52cSAmir Goldstein /*
633dae1e52cSAmir Goldstein * Block out ext4_truncate while we alter the tree
634dae1e52cSAmir Goldstein */
635a5211002STheodore Ts'o err = ext4_alloc_branch(handle, &ar, indirect_blks,
636dae1e52cSAmir Goldstein offsets + (partial - chain), partial);
637dae1e52cSAmir Goldstein
638dae1e52cSAmir Goldstein /*
639dae1e52cSAmir Goldstein * The ext4_splice_branch call will free and forget any buffers
640dae1e52cSAmir Goldstein * on the new chain if there is a failure, but that risks using
641dae1e52cSAmir Goldstein * up transaction credits, especially for bitmaps where the
642dae1e52cSAmir Goldstein * credits cannot be returned. Can we handle this somehow? We
643dae1e52cSAmir Goldstein * may need to return -EAGAIN upwards in the worst case. --sct
644dae1e52cSAmir Goldstein */
645dae1e52cSAmir Goldstein if (!err)
646a5211002STheodore Ts'o err = ext4_splice_branch(handle, &ar, partial, indirect_blks);
647dae1e52cSAmir Goldstein if (err)
648dae1e52cSAmir Goldstein goto cleanup;
649dae1e52cSAmir Goldstein
650dae1e52cSAmir Goldstein map->m_flags |= EXT4_MAP_NEW;
651dae1e52cSAmir Goldstein
652dae1e52cSAmir Goldstein ext4_update_inode_fsync_trans(handle, inode, 1);
653a5211002STheodore Ts'o count = ar.len;
654de25d6e9SBaokun Li
655de25d6e9SBaokun Li /*
656de25d6e9SBaokun Li * Update reserved blocks/metadata blocks after successful block
657de25d6e9SBaokun Li * allocation which had been deferred till now.
658de25d6e9SBaokun Li */
659de25d6e9SBaokun Li if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
660de25d6e9SBaokun Li ext4_da_update_reserve_space(inode, count, 1);
661de25d6e9SBaokun Li
662dae1e52cSAmir Goldstein got_it:
663dae1e52cSAmir Goldstein map->m_flags |= EXT4_MAP_MAPPED;
664dae1e52cSAmir Goldstein map->m_pblk = le32_to_cpu(chain[depth-1].key);
665dae1e52cSAmir Goldstein map->m_len = count;
666dae1e52cSAmir Goldstein if (count > blocks_to_boundary)
667dae1e52cSAmir Goldstein map->m_flags |= EXT4_MAP_BOUNDARY;
668dae1e52cSAmir Goldstein err = count;
669dae1e52cSAmir Goldstein /* Clean up and exit */
670dae1e52cSAmir Goldstein partial = chain + depth - 1; /* the whole chain */
671dae1e52cSAmir Goldstein cleanup:
672dae1e52cSAmir Goldstein while (partial > chain) {
673dae1e52cSAmir Goldstein BUFFER_TRACE(partial->bh, "call brelse");
674dae1e52cSAmir Goldstein brelse(partial->bh);
675dae1e52cSAmir Goldstein partial--;
676dae1e52cSAmir Goldstein }
677dae1e52cSAmir Goldstein out:
67821ddd568STheodore Ts'o trace_ext4_ind_map_blocks_exit(inode, flags, map, err);
679dae1e52cSAmir Goldstein return err;
680dae1e52cSAmir Goldstein }
681dae1e52cSAmir Goldstein
682dae1e52cSAmir Goldstein /*
683fa55a0edSJan Kara * Calculate number of indirect blocks touched by mapping @nrblocks logically
684fa55a0edSJan Kara * contiguous blocks
685fa55a0edSJan Kara */
ext4_ind_trans_blocks(struct inode * inode,int nrblocks)686fa55a0edSJan Kara int ext4_ind_trans_blocks(struct inode *inode, int nrblocks)
687dae1e52cSAmir Goldstein {
688dae1e52cSAmir Goldstein /*
689dae1e52cSAmir Goldstein * With N contiguous data blocks, we need at most
690dae1e52cSAmir Goldstein * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
691dae1e52cSAmir Goldstein * 2 dindirect blocks, and 1 tindirect block
692dae1e52cSAmir Goldstein */
693fa55a0edSJan Kara return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
694dae1e52cSAmir Goldstein }
695dae1e52cSAmir Goldstein
ext4_ind_trunc_restart_fn(handle_t * handle,struct inode * inode,struct buffer_head * bh,int * dropped)696a4130367SJan Kara static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode,
697a4130367SJan Kara struct buffer_head *bh, int *dropped)
698a4130367SJan Kara {
699a4130367SJan Kara int err;
700a4130367SJan Kara
701a4130367SJan Kara if (bh) {
702a4130367SJan Kara BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
703a4130367SJan Kara err = ext4_handle_dirty_metadata(handle, inode, bh);
704a4130367SJan Kara if (unlikely(err))
705a4130367SJan Kara return err;
706a4130367SJan Kara }
707a4130367SJan Kara err = ext4_mark_inode_dirty(handle, inode);
708a4130367SJan Kara if (unlikely(err))
709a4130367SJan Kara return err;
710a4130367SJan Kara /*
711a4130367SJan Kara * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
712a4130367SJan Kara * moment, get_block can be called only for blocks inside i_size since
713a4130367SJan Kara * page cache has been already dropped and writes are blocked by
714f340b3d9Shongnanli * i_rwsem. So we can safely drop the i_data_sem here.
715a4130367SJan Kara */
716a4130367SJan Kara BUG_ON(EXT4_JOURNAL(inode) == NULL);
71727bc446eSbrookxu ext4_discard_preallocations(inode, 0);
718a4130367SJan Kara up_write(&EXT4_I(inode)->i_data_sem);
719a4130367SJan Kara *dropped = 1;
720a4130367SJan Kara return 0;
721a4130367SJan Kara }
722a4130367SJan Kara
723dae1e52cSAmir Goldstein /*
724dae1e52cSAmir Goldstein * Truncate transactions can be complex and absolutely huge. So we need to
7253088e5a5SBhaskar Chowdhury * be able to restart the transaction at a convenient checkpoint to make
726dae1e52cSAmir Goldstein * sure we don't overflow the journal.
727dae1e52cSAmir Goldstein *
728819c4920STheodore Ts'o * Try to extend this transaction for the purposes of truncation. If
729a4130367SJan Kara * extend fails, we restart transaction.
730dae1e52cSAmir Goldstein */
ext4_ind_truncate_ensure_credits(handle_t * handle,struct inode * inode,struct buffer_head * bh,int revoke_creds)731a4130367SJan Kara static int ext4_ind_truncate_ensure_credits(handle_t *handle,
732a4130367SJan Kara struct inode *inode,
73383448bdfSJan Kara struct buffer_head *bh,
73483448bdfSJan Kara int revoke_creds)
735dae1e52cSAmir Goldstein {
736a4130367SJan Kara int ret;
737a4130367SJan Kara int dropped = 0;
738a4130367SJan Kara
739a4130367SJan Kara ret = ext4_journal_ensure_credits_fn(handle, EXT4_RESERVE_TRANS_BLOCKS,
74083448bdfSJan Kara ext4_blocks_for_truncate(inode), revoke_creds,
741a4130367SJan Kara ext4_ind_trunc_restart_fn(handle, inode, bh, &dropped));
742a4130367SJan Kara if (dropped)
743a4130367SJan Kara down_write(&EXT4_I(inode)->i_data_sem);
744a4130367SJan Kara if (ret <= 0)
745a4130367SJan Kara return ret;
746a4130367SJan Kara if (bh) {
747a4130367SJan Kara BUFFER_TRACE(bh, "retaking write access");
748188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, bh,
749188c299eSJan Kara EXT4_JTR_NONE);
750a4130367SJan Kara if (unlikely(ret))
751a4130367SJan Kara return ret;
752a4130367SJan Kara }
753dae1e52cSAmir Goldstein return 0;
754dae1e52cSAmir Goldstein }
755dae1e52cSAmir Goldstein
756dae1e52cSAmir Goldstein /*
757dae1e52cSAmir Goldstein * Probably it should be a library function... search for first non-zero word
758dae1e52cSAmir Goldstein * or memcmp with zero_page, whatever is better for particular architecture.
759dae1e52cSAmir Goldstein * Linus?
760dae1e52cSAmir Goldstein */
all_zeroes(__le32 * p,__le32 * q)761dae1e52cSAmir Goldstein static inline int all_zeroes(__le32 *p, __le32 *q)
762dae1e52cSAmir Goldstein {
763dae1e52cSAmir Goldstein while (p < q)
764dae1e52cSAmir Goldstein if (*p++)
765dae1e52cSAmir Goldstein return 0;
766dae1e52cSAmir Goldstein return 1;
767dae1e52cSAmir Goldstein }
768dae1e52cSAmir Goldstein
769dae1e52cSAmir Goldstein /**
770dae1e52cSAmir Goldstein * ext4_find_shared - find the indirect blocks for partial truncation.
771dae1e52cSAmir Goldstein * @inode: inode in question
772dae1e52cSAmir Goldstein * @depth: depth of the affected branch
773dae1e52cSAmir Goldstein * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
774dae1e52cSAmir Goldstein * @chain: place to store the pointers to partial indirect blocks
775dae1e52cSAmir Goldstein * @top: place to the (detached) top of branch
776dae1e52cSAmir Goldstein *
777dae1e52cSAmir Goldstein * This is a helper function used by ext4_truncate().
778dae1e52cSAmir Goldstein *
779dae1e52cSAmir Goldstein * When we do truncate() we may have to clean the ends of several
780dae1e52cSAmir Goldstein * indirect blocks but leave the blocks themselves alive. Block is
781dae1e52cSAmir Goldstein * partially truncated if some data below the new i_size is referred
782dae1e52cSAmir Goldstein * from it (and it is on the path to the first completely truncated
783dae1e52cSAmir Goldstein * data block, indeed). We have to free the top of that path along
784dae1e52cSAmir Goldstein * with everything to the right of the path. Since no allocation
785dae1e52cSAmir Goldstein * past the truncation point is possible until ext4_truncate()
786dae1e52cSAmir Goldstein * finishes, we may safely do the latter, but top of branch may
787dae1e52cSAmir Goldstein * require special attention - pageout below the truncation point
788dae1e52cSAmir Goldstein * might try to populate it.
789dae1e52cSAmir Goldstein *
790dae1e52cSAmir Goldstein * We atomically detach the top of branch from the tree, store the
791dae1e52cSAmir Goldstein * block number of its root in *@top, pointers to buffer_heads of
792dae1e52cSAmir Goldstein * partially truncated blocks - in @chain[].bh and pointers to
793dae1e52cSAmir Goldstein * their last elements that should not be removed - in
794dae1e52cSAmir Goldstein * @chain[].p. Return value is the pointer to last filled element
795dae1e52cSAmir Goldstein * of @chain.
796dae1e52cSAmir Goldstein *
797dae1e52cSAmir Goldstein * The work left to caller to do the actual freeing of subtrees:
798dae1e52cSAmir Goldstein * a) free the subtree starting from *@top
799dae1e52cSAmir Goldstein * b) free the subtrees whose roots are stored in
800dae1e52cSAmir Goldstein * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
801dae1e52cSAmir Goldstein * c) free the subtrees growing from the inode past the @chain[0].
802dae1e52cSAmir Goldstein * (no partially truncated stuff there). */
803dae1e52cSAmir Goldstein
ext4_find_shared(struct inode * inode,int depth,ext4_lblk_t offsets[4],Indirect chain[4],__le32 * top)804dae1e52cSAmir Goldstein static Indirect *ext4_find_shared(struct inode *inode, int depth,
805dae1e52cSAmir Goldstein ext4_lblk_t offsets[4], Indirect chain[4],
806dae1e52cSAmir Goldstein __le32 *top)
807dae1e52cSAmir Goldstein {
808dae1e52cSAmir Goldstein Indirect *partial, *p;
809dae1e52cSAmir Goldstein int k, err;
810dae1e52cSAmir Goldstein
811dae1e52cSAmir Goldstein *top = 0;
812dae1e52cSAmir Goldstein /* Make k index the deepest non-null offset + 1 */
813dae1e52cSAmir Goldstein for (k = depth; k > 1 && !offsets[k-1]; k--)
814dae1e52cSAmir Goldstein ;
815dae1e52cSAmir Goldstein partial = ext4_get_branch(inode, k, offsets, chain, &err);
816dae1e52cSAmir Goldstein /* Writer: pointers */
817dae1e52cSAmir Goldstein if (!partial)
818dae1e52cSAmir Goldstein partial = chain + k-1;
819dae1e52cSAmir Goldstein /*
820dae1e52cSAmir Goldstein * If the branch acquired continuation since we've looked at it -
821dae1e52cSAmir Goldstein * fine, it should all survive and (new) top doesn't belong to us.
822dae1e52cSAmir Goldstein */
823dae1e52cSAmir Goldstein if (!partial->key && *partial->p)
824dae1e52cSAmir Goldstein /* Writer: end */
825dae1e52cSAmir Goldstein goto no_top;
826dae1e52cSAmir Goldstein for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
827dae1e52cSAmir Goldstein ;
828dae1e52cSAmir Goldstein /*
829dae1e52cSAmir Goldstein * OK, we've found the last block that must survive. The rest of our
830dae1e52cSAmir Goldstein * branch should be detached before unlocking. However, if that rest
831dae1e52cSAmir Goldstein * of branch is all ours and does not grow immediately from the inode
832dae1e52cSAmir Goldstein * it's easier to cheat and just decrement partial->p.
833dae1e52cSAmir Goldstein */
834dae1e52cSAmir Goldstein if (p == chain + k - 1 && p > chain) {
835dae1e52cSAmir Goldstein p->p--;
836dae1e52cSAmir Goldstein } else {
837dae1e52cSAmir Goldstein *top = *p->p;
838dae1e52cSAmir Goldstein /* Nope, don't do this in ext4. Must leave the tree intact */
839dae1e52cSAmir Goldstein #if 0
840dae1e52cSAmir Goldstein *p->p = 0;
841dae1e52cSAmir Goldstein #endif
842dae1e52cSAmir Goldstein }
843dae1e52cSAmir Goldstein /* Writer: end */
844dae1e52cSAmir Goldstein
845dae1e52cSAmir Goldstein while (partial > p) {
846dae1e52cSAmir Goldstein brelse(partial->bh);
847dae1e52cSAmir Goldstein partial--;
848dae1e52cSAmir Goldstein }
849dae1e52cSAmir Goldstein no_top:
850dae1e52cSAmir Goldstein return partial;
851dae1e52cSAmir Goldstein }
852dae1e52cSAmir Goldstein
853dae1e52cSAmir Goldstein /*
854dae1e52cSAmir Goldstein * Zero a number of block pointers in either an inode or an indirect block.
855dae1e52cSAmir Goldstein * If we restart the transaction we must again get write access to the
856dae1e52cSAmir Goldstein * indirect block for further modification.
857dae1e52cSAmir Goldstein *
858dae1e52cSAmir Goldstein * We release `count' blocks on disk, but (last - first) may be greater
859dae1e52cSAmir Goldstein * than `count' because there can be holes in there.
860dae1e52cSAmir Goldstein *
861dae1e52cSAmir Goldstein * Return 0 on success, 1 on invalid block range
862dae1e52cSAmir Goldstein * and < 0 on fatal error.
863dae1e52cSAmir Goldstein */
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)864dae1e52cSAmir Goldstein static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
865dae1e52cSAmir Goldstein struct buffer_head *bh,
866dae1e52cSAmir Goldstein ext4_fsblk_t block_to_free,
867dae1e52cSAmir Goldstein unsigned long count, __le32 *first,
868dae1e52cSAmir Goldstein __le32 *last)
869dae1e52cSAmir Goldstein {
870dae1e52cSAmir Goldstein __le32 *p;
871981250caSTheodore Ts'o int flags = EXT4_FREE_BLOCKS_VALIDATED;
872dae1e52cSAmir Goldstein int err;
873dae1e52cSAmir Goldstein
874ddfa17e4STahsin Erdogan if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
875ddfa17e4STahsin Erdogan ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
876981250caSTheodore Ts'o flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
877981250caSTheodore Ts'o else if (ext4_should_journal_data(inode))
878981250caSTheodore Ts'o flags |= EXT4_FREE_BLOCKS_FORGET;
879dae1e52cSAmir Goldstein
880ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, block_to_free, count)) {
881dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode, "attempt to clear invalid "
882dae1e52cSAmir Goldstein "blocks %llu len %lu",
883dae1e52cSAmir Goldstein (unsigned long long) block_to_free, count);
884dae1e52cSAmir Goldstein return 1;
885dae1e52cSAmir Goldstein }
886dae1e52cSAmir Goldstein
88783448bdfSJan Kara err = ext4_ind_truncate_ensure_credits(handle, inode, bh,
88883448bdfSJan Kara ext4_free_data_revoke_credits(inode, count));
889a4130367SJan Kara if (err < 0)
890dae1e52cSAmir Goldstein goto out_err;
891dae1e52cSAmir Goldstein
892dae1e52cSAmir Goldstein for (p = first; p < last; p++)
893dae1e52cSAmir Goldstein *p = 0;
894dae1e52cSAmir Goldstein
895dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
896dae1e52cSAmir Goldstein return 0;
897dae1e52cSAmir Goldstein out_err:
898dae1e52cSAmir Goldstein ext4_std_error(inode->i_sb, err);
899dae1e52cSAmir Goldstein return err;
900dae1e52cSAmir Goldstein }
901dae1e52cSAmir Goldstein
902dae1e52cSAmir Goldstein /**
903dae1e52cSAmir Goldstein * ext4_free_data - free a list of data blocks
904dae1e52cSAmir Goldstein * @handle: handle for this transaction
905dae1e52cSAmir Goldstein * @inode: inode we are dealing with
906dae1e52cSAmir Goldstein * @this_bh: indirect buffer_head which contains *@first and *@last
907dae1e52cSAmir Goldstein * @first: array of block numbers
908dae1e52cSAmir Goldstein * @last: points immediately past the end of array
909dae1e52cSAmir Goldstein *
910dae1e52cSAmir Goldstein * We are freeing all blocks referred from that array (numbers are stored as
911dae1e52cSAmir Goldstein * little-endian 32-bit) and updating @inode->i_blocks appropriately.
912dae1e52cSAmir Goldstein *
913dae1e52cSAmir Goldstein * We accumulate contiguous runs of blocks to free. Conveniently, if these
914dae1e52cSAmir Goldstein * blocks are contiguous then releasing them at one time will only affect one
915dae1e52cSAmir Goldstein * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
916dae1e52cSAmir Goldstein * actually use a lot of journal space.
917dae1e52cSAmir Goldstein *
918dae1e52cSAmir Goldstein * @this_bh will be %NULL if @first and @last point into the inode's direct
919dae1e52cSAmir Goldstein * block pointers.
920dae1e52cSAmir Goldstein */
ext4_free_data(handle_t * handle,struct inode * inode,struct buffer_head * this_bh,__le32 * first,__le32 * last)921dae1e52cSAmir Goldstein static void ext4_free_data(handle_t *handle, struct inode *inode,
922dae1e52cSAmir Goldstein struct buffer_head *this_bh,
923dae1e52cSAmir Goldstein __le32 *first, __le32 *last)
924dae1e52cSAmir Goldstein {
925dae1e52cSAmir Goldstein ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
926dae1e52cSAmir Goldstein unsigned long count = 0; /* Number of blocks in the run */
927dae1e52cSAmir Goldstein __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
928dae1e52cSAmir Goldstein corresponding to
929dae1e52cSAmir Goldstein block_to_free */
930dae1e52cSAmir Goldstein ext4_fsblk_t nr; /* Current block # */
931dae1e52cSAmir Goldstein __le32 *p; /* Pointer into inode/ind
932dae1e52cSAmir Goldstein for current block */
933dae1e52cSAmir Goldstein int err = 0;
934dae1e52cSAmir Goldstein
935dae1e52cSAmir Goldstein if (this_bh) { /* For indirect block */
936dae1e52cSAmir Goldstein BUFFER_TRACE(this_bh, "get_write_access");
937188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb,
938188c299eSJan Kara this_bh, EXT4_JTR_NONE);
939dae1e52cSAmir Goldstein /* Important: if we can't update the indirect pointers
940dae1e52cSAmir Goldstein * to the blocks, we can't free them. */
941dae1e52cSAmir Goldstein if (err)
942dae1e52cSAmir Goldstein return;
943dae1e52cSAmir Goldstein }
944dae1e52cSAmir Goldstein
945dae1e52cSAmir Goldstein for (p = first; p < last; p++) {
946dae1e52cSAmir Goldstein nr = le32_to_cpu(*p);
947dae1e52cSAmir Goldstein if (nr) {
948dae1e52cSAmir Goldstein /* accumulate blocks to free if they're contiguous */
949dae1e52cSAmir Goldstein if (count == 0) {
950dae1e52cSAmir Goldstein block_to_free = nr;
951dae1e52cSAmir Goldstein block_to_free_p = p;
952dae1e52cSAmir Goldstein count = 1;
953dae1e52cSAmir Goldstein } else if (nr == block_to_free + count) {
954dae1e52cSAmir Goldstein count++;
955dae1e52cSAmir Goldstein } else {
956dae1e52cSAmir Goldstein err = ext4_clear_blocks(handle, inode, this_bh,
957dae1e52cSAmir Goldstein block_to_free, count,
958dae1e52cSAmir Goldstein block_to_free_p, p);
959dae1e52cSAmir Goldstein if (err)
960dae1e52cSAmir Goldstein break;
961dae1e52cSAmir Goldstein block_to_free = nr;
962dae1e52cSAmir Goldstein block_to_free_p = p;
963dae1e52cSAmir Goldstein count = 1;
964dae1e52cSAmir Goldstein }
965dae1e52cSAmir Goldstein }
966dae1e52cSAmir Goldstein }
967dae1e52cSAmir Goldstein
968dae1e52cSAmir Goldstein if (!err && count > 0)
969dae1e52cSAmir Goldstein err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
970dae1e52cSAmir Goldstein count, block_to_free_p, p);
971dae1e52cSAmir Goldstein if (err < 0)
972dae1e52cSAmir Goldstein /* fatal error */
973dae1e52cSAmir Goldstein return;
974dae1e52cSAmir Goldstein
975dae1e52cSAmir Goldstein if (this_bh) {
976dae1e52cSAmir Goldstein BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
977dae1e52cSAmir Goldstein
978dae1e52cSAmir Goldstein /*
979dae1e52cSAmir Goldstein * The buffer head should have an attached journal head at this
980dae1e52cSAmir Goldstein * point. However, if the data is corrupted and an indirect
981dae1e52cSAmir Goldstein * block pointed to itself, it would have been detached when
982dae1e52cSAmir Goldstein * the block was cleared. Check for this instead of OOPSing.
983dae1e52cSAmir Goldstein */
984dae1e52cSAmir Goldstein if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
985dae1e52cSAmir Goldstein ext4_handle_dirty_metadata(handle, inode, this_bh);
986dae1e52cSAmir Goldstein else
987dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode,
988dae1e52cSAmir Goldstein "circular indirect block detected at "
989dae1e52cSAmir Goldstein "block %llu",
990dae1e52cSAmir Goldstein (unsigned long long) this_bh->b_blocknr);
991dae1e52cSAmir Goldstein }
992dae1e52cSAmir Goldstein }
993dae1e52cSAmir Goldstein
994dae1e52cSAmir Goldstein /**
995dae1e52cSAmir Goldstein * ext4_free_branches - free an array of branches
996dae1e52cSAmir Goldstein * @handle: JBD handle for this transaction
997dae1e52cSAmir Goldstein * @inode: inode we are dealing with
998dae1e52cSAmir Goldstein * @parent_bh: the buffer_head which contains *@first and *@last
999dae1e52cSAmir Goldstein * @first: array of block numbers
1000dae1e52cSAmir Goldstein * @last: pointer immediately past the end of array
1001dae1e52cSAmir Goldstein * @depth: depth of the branches to free
1002dae1e52cSAmir Goldstein *
1003dae1e52cSAmir Goldstein * We are freeing all blocks referred from these branches (numbers are
1004dae1e52cSAmir Goldstein * stored as little-endian 32-bit) and updating @inode->i_blocks
1005dae1e52cSAmir Goldstein * appropriately.
1006dae1e52cSAmir Goldstein */
ext4_free_branches(handle_t * handle,struct inode * inode,struct buffer_head * parent_bh,__le32 * first,__le32 * last,int depth)1007dae1e52cSAmir Goldstein static void ext4_free_branches(handle_t *handle, struct inode *inode,
1008dae1e52cSAmir Goldstein struct buffer_head *parent_bh,
1009dae1e52cSAmir Goldstein __le32 *first, __le32 *last, int depth)
1010dae1e52cSAmir Goldstein {
1011dae1e52cSAmir Goldstein ext4_fsblk_t nr;
1012dae1e52cSAmir Goldstein __le32 *p;
1013dae1e52cSAmir Goldstein
1014dae1e52cSAmir Goldstein if (ext4_handle_is_aborted(handle))
1015dae1e52cSAmir Goldstein return;
1016dae1e52cSAmir Goldstein
1017dae1e52cSAmir Goldstein if (depth--) {
1018dae1e52cSAmir Goldstein struct buffer_head *bh;
1019dae1e52cSAmir Goldstein int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1020dae1e52cSAmir Goldstein p = last;
1021dae1e52cSAmir Goldstein while (--p >= first) {
1022dae1e52cSAmir Goldstein nr = le32_to_cpu(*p);
1023dae1e52cSAmir Goldstein if (!nr)
1024dae1e52cSAmir Goldstein continue; /* A hole */
1025dae1e52cSAmir Goldstein
1026ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, nr, 1)) {
1027dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode,
1028dae1e52cSAmir Goldstein "invalid indirect mapped "
1029dae1e52cSAmir Goldstein "block %lu (level %d)",
1030dae1e52cSAmir Goldstein (unsigned long) nr, depth);
1031dae1e52cSAmir Goldstein break;
1032dae1e52cSAmir Goldstein }
1033dae1e52cSAmir Goldstein
1034dae1e52cSAmir Goldstein /* Go read the buffer for the next level down */
10350a846f49Szhangyi (F) bh = ext4_sb_bread(inode->i_sb, nr, 0);
1036dae1e52cSAmir Goldstein
1037dae1e52cSAmir Goldstein /*
1038dae1e52cSAmir Goldstein * A read failure? Report error and clear slot
1039dae1e52cSAmir Goldstein * (should be rare).
1040dae1e52cSAmir Goldstein */
10410a846f49Szhangyi (F) if (IS_ERR(bh)) {
10420a846f49Szhangyi (F) ext4_error_inode_block(inode, nr, -PTR_ERR(bh),
1043dae1e52cSAmir Goldstein "Read failure");
1044dae1e52cSAmir Goldstein continue;
1045dae1e52cSAmir Goldstein }
1046dae1e52cSAmir Goldstein
1047dae1e52cSAmir Goldstein /* This zaps the entire block. Bottom up. */
1048dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "free child branches");
1049dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, bh,
1050dae1e52cSAmir Goldstein (__le32 *) bh->b_data,
1051dae1e52cSAmir Goldstein (__le32 *) bh->b_data + addr_per_block,
1052dae1e52cSAmir Goldstein depth);
1053dae1e52cSAmir Goldstein brelse(bh);
1054dae1e52cSAmir Goldstein
1055dae1e52cSAmir Goldstein /*
1056b483bb77SRandy Dunlap * Everything below this pointer has been
1057dae1e52cSAmir Goldstein * released. Now let this top-of-subtree go.
1058dae1e52cSAmir Goldstein *
1059dae1e52cSAmir Goldstein * We want the freeing of this indirect block to be
1060dae1e52cSAmir Goldstein * atomic in the journal with the updating of the
1061dae1e52cSAmir Goldstein * bitmap block which owns it. So make some room in
1062dae1e52cSAmir Goldstein * the journal.
1063dae1e52cSAmir Goldstein *
1064dae1e52cSAmir Goldstein * We zero the parent pointer *after* freeing its
1065dae1e52cSAmir Goldstein * pointee in the bitmaps, so if extend_transaction()
1066dae1e52cSAmir Goldstein * for some reason fails to put the bitmap changes and
1067dae1e52cSAmir Goldstein * the release into the same transaction, recovery
1068dae1e52cSAmir Goldstein * will merely complain about releasing a free block,
1069dae1e52cSAmir Goldstein * rather than leaking blocks.
1070dae1e52cSAmir Goldstein */
1071dae1e52cSAmir Goldstein if (ext4_handle_is_aborted(handle))
1072dae1e52cSAmir Goldstein return;
1073a4130367SJan Kara if (ext4_ind_truncate_ensure_credits(handle, inode,
107483448bdfSJan Kara NULL,
107583448bdfSJan Kara ext4_free_metadata_revoke_credits(
107683448bdfSJan Kara inode->i_sb, 1)) < 0)
1077a4130367SJan Kara return;
1078dae1e52cSAmir Goldstein
1079dae1e52cSAmir Goldstein /*
1080dae1e52cSAmir Goldstein * The forget flag here is critical because if
1081dae1e52cSAmir Goldstein * we are journaling (and not doing data
1082dae1e52cSAmir Goldstein * journaling), we have to make sure a revoke
1083dae1e52cSAmir Goldstein * record is written to prevent the journal
1084dae1e52cSAmir Goldstein * replay from overwriting the (former)
1085dae1e52cSAmir Goldstein * indirect block if it gets reallocated as a
1086dae1e52cSAmir Goldstein * data block. This must happen in the same
1087dae1e52cSAmir Goldstein * transaction where the data blocks are
1088dae1e52cSAmir Goldstein * actually freed.
1089dae1e52cSAmir Goldstein */
1090dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, nr, 1,
1091dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_METADATA|
1092dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_FORGET);
1093dae1e52cSAmir Goldstein
1094dae1e52cSAmir Goldstein if (parent_bh) {
1095dae1e52cSAmir Goldstein /*
1096dae1e52cSAmir Goldstein * The block which we have just freed is
1097dae1e52cSAmir Goldstein * pointed to by an indirect block: journal it
1098dae1e52cSAmir Goldstein */
1099dae1e52cSAmir Goldstein BUFFER_TRACE(parent_bh, "get_write_access");
1100dae1e52cSAmir Goldstein if (!ext4_journal_get_write_access(handle,
1101188c299eSJan Kara inode->i_sb, parent_bh,
1102188c299eSJan Kara EXT4_JTR_NONE)) {
1103dae1e52cSAmir Goldstein *p = 0;
1104dae1e52cSAmir Goldstein BUFFER_TRACE(parent_bh,
1105dae1e52cSAmir Goldstein "call ext4_handle_dirty_metadata");
1106dae1e52cSAmir Goldstein ext4_handle_dirty_metadata(handle,
1107dae1e52cSAmir Goldstein inode,
1108dae1e52cSAmir Goldstein parent_bh);
1109dae1e52cSAmir Goldstein }
1110dae1e52cSAmir Goldstein }
1111dae1e52cSAmir Goldstein }
1112dae1e52cSAmir Goldstein } else {
1113dae1e52cSAmir Goldstein /* We have reached the bottom of the tree. */
1114dae1e52cSAmir Goldstein BUFFER_TRACE(parent_bh, "free data blocks");
1115dae1e52cSAmir Goldstein ext4_free_data(handle, inode, parent_bh, first, last);
1116dae1e52cSAmir Goldstein }
1117dae1e52cSAmir Goldstein }
1118dae1e52cSAmir Goldstein
ext4_ind_truncate(handle_t * handle,struct inode * inode)1119819c4920STheodore Ts'o void ext4_ind_truncate(handle_t *handle, struct inode *inode)
1120dae1e52cSAmir Goldstein {
1121dae1e52cSAmir Goldstein struct ext4_inode_info *ei = EXT4_I(inode);
1122dae1e52cSAmir Goldstein __le32 *i_data = ei->i_data;
1123dae1e52cSAmir Goldstein int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1124dae1e52cSAmir Goldstein ext4_lblk_t offsets[4];
1125dae1e52cSAmir Goldstein Indirect chain[4];
1126dae1e52cSAmir Goldstein Indirect *partial;
1127dae1e52cSAmir Goldstein __le32 nr = 0;
1128dae1e52cSAmir Goldstein int n = 0;
1129dae1e52cSAmir Goldstein ext4_lblk_t last_block, max_block;
1130dae1e52cSAmir Goldstein unsigned blocksize = inode->i_sb->s_blocksize;
1131dae1e52cSAmir Goldstein
1132dae1e52cSAmir Goldstein last_block = (inode->i_size + blocksize-1)
1133dae1e52cSAmir Goldstein >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1134dae1e52cSAmir Goldstein max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
1135dae1e52cSAmir Goldstein >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
1136dae1e52cSAmir Goldstein
1137dae1e52cSAmir Goldstein if (last_block != max_block) {
1138dae1e52cSAmir Goldstein n = ext4_block_to_path(inode, last_block, offsets, NULL);
1139dae1e52cSAmir Goldstein if (n == 0)
1140819c4920STheodore Ts'o return;
1141dae1e52cSAmir Goldstein }
1142dae1e52cSAmir Goldstein
114351865fdaSZheng Liu ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
1144dae1e52cSAmir Goldstein
1145dae1e52cSAmir Goldstein /*
1146dae1e52cSAmir Goldstein * The orphan list entry will now protect us from any crash which
1147dae1e52cSAmir Goldstein * occurs before the truncate completes, so it is now safe to propagate
1148dae1e52cSAmir Goldstein * the new, shorter inode size (held for now in i_size) into the
1149dae1e52cSAmir Goldstein * on-disk inode. We do this via i_disksize, which is the value which
1150dae1e52cSAmir Goldstein * ext4 *really* writes onto the disk inode.
1151dae1e52cSAmir Goldstein */
1152dae1e52cSAmir Goldstein ei->i_disksize = inode->i_size;
1153dae1e52cSAmir Goldstein
1154dae1e52cSAmir Goldstein if (last_block == max_block) {
1155dae1e52cSAmir Goldstein /*
1156dae1e52cSAmir Goldstein * It is unnecessary to free any data blocks if last_block is
1157dae1e52cSAmir Goldstein * equal to the indirect block limit.
1158dae1e52cSAmir Goldstein */
1159819c4920STheodore Ts'o return;
1160dae1e52cSAmir Goldstein } else if (n == 1) { /* direct blocks */
1161dae1e52cSAmir Goldstein ext4_free_data(handle, inode, NULL, i_data+offsets[0],
1162dae1e52cSAmir Goldstein i_data + EXT4_NDIR_BLOCKS);
1163dae1e52cSAmir Goldstein goto do_indirects;
1164dae1e52cSAmir Goldstein }
1165dae1e52cSAmir Goldstein
1166dae1e52cSAmir Goldstein partial = ext4_find_shared(inode, n, offsets, chain, &nr);
1167dae1e52cSAmir Goldstein /* Kill the top of shared branch (not detached) */
1168dae1e52cSAmir Goldstein if (nr) {
1169dae1e52cSAmir Goldstein if (partial == chain) {
1170dae1e52cSAmir Goldstein /* Shared branch grows from the inode */
1171dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL,
1172dae1e52cSAmir Goldstein &nr, &nr+1, (chain+n-1) - partial);
1173dae1e52cSAmir Goldstein *partial->p = 0;
1174dae1e52cSAmir Goldstein /*
1175dae1e52cSAmir Goldstein * We mark the inode dirty prior to restart,
1176dae1e52cSAmir Goldstein * and prior to stop. No need for it here.
1177dae1e52cSAmir Goldstein */
1178dae1e52cSAmir Goldstein } else {
1179dae1e52cSAmir Goldstein /* Shared branch grows from an indirect block */
1180dae1e52cSAmir Goldstein BUFFER_TRACE(partial->bh, "get_write_access");
1181dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, partial->bh,
1182dae1e52cSAmir Goldstein partial->p,
1183dae1e52cSAmir Goldstein partial->p+1, (chain+n-1) - partial);
1184dae1e52cSAmir Goldstein }
1185dae1e52cSAmir Goldstein }
1186dae1e52cSAmir Goldstein /* Clear the ends of indirect blocks on the shared branch */
1187dae1e52cSAmir Goldstein while (partial > chain) {
1188dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
1189dae1e52cSAmir Goldstein (__le32*)partial->bh->b_data+addr_per_block,
1190dae1e52cSAmir Goldstein (chain+n-1) - partial);
1191dae1e52cSAmir Goldstein BUFFER_TRACE(partial->bh, "call brelse");
1192dae1e52cSAmir Goldstein brelse(partial->bh);
1193dae1e52cSAmir Goldstein partial--;
1194dae1e52cSAmir Goldstein }
1195dae1e52cSAmir Goldstein do_indirects:
1196dae1e52cSAmir Goldstein /* Kill the remaining (whole) subtrees */
1197dae1e52cSAmir Goldstein switch (offsets[0]) {
1198dae1e52cSAmir Goldstein default:
1199dae1e52cSAmir Goldstein nr = i_data[EXT4_IND_BLOCK];
1200dae1e52cSAmir Goldstein if (nr) {
1201dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
1202dae1e52cSAmir Goldstein i_data[EXT4_IND_BLOCK] = 0;
1203dae1e52cSAmir Goldstein }
120470d7ced2SShijie Luo fallthrough;
1205dae1e52cSAmir Goldstein case EXT4_IND_BLOCK:
1206dae1e52cSAmir Goldstein nr = i_data[EXT4_DIND_BLOCK];
1207dae1e52cSAmir Goldstein if (nr) {
1208dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
1209dae1e52cSAmir Goldstein i_data[EXT4_DIND_BLOCK] = 0;
1210dae1e52cSAmir Goldstein }
121170d7ced2SShijie Luo fallthrough;
1212dae1e52cSAmir Goldstein case EXT4_DIND_BLOCK:
1213dae1e52cSAmir Goldstein nr = i_data[EXT4_TIND_BLOCK];
1214dae1e52cSAmir Goldstein if (nr) {
1215dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
1216dae1e52cSAmir Goldstein i_data[EXT4_TIND_BLOCK] = 0;
1217dae1e52cSAmir Goldstein }
121870d7ced2SShijie Luo fallthrough;
1219dae1e52cSAmir Goldstein case EXT4_TIND_BLOCK:
1220dae1e52cSAmir Goldstein ;
1221dae1e52cSAmir Goldstein }
1222dae1e52cSAmir Goldstein }
1223dae1e52cSAmir Goldstein
12244f579ae7SLukas Czerner /**
12254f579ae7SLukas Czerner * ext4_ind_remove_space - remove space from the range
12264f579ae7SLukas Czerner * @handle: JBD handle for this transaction
12274f579ae7SLukas Czerner * @inode: inode we are dealing with
12284f579ae7SLukas Czerner * @start: First block to remove
12294f579ae7SLukas Czerner * @end: One block after the last block to remove (exclusive)
12304f579ae7SLukas Czerner *
12314f579ae7SLukas Czerner * Free the blocks in the defined range (end is exclusive endpoint of
12324f579ae7SLukas Czerner * range). This is used by ext4_punch_hole().
12334f579ae7SLukas Czerner */
ext4_ind_remove_space(handle_t * handle,struct inode * inode,ext4_lblk_t start,ext4_lblk_t end)12344f579ae7SLukas Czerner int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
12354f579ae7SLukas Czerner ext4_lblk_t start, ext4_lblk_t end)
12368bad6fc8SZheng Liu {
12374f579ae7SLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode);
12384f579ae7SLukas Czerner __le32 *i_data = ei->i_data;
12398bad6fc8SZheng Liu int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
12404f579ae7SLukas Czerner ext4_lblk_t offsets[4], offsets2[4];
12414f579ae7SLukas Czerner Indirect chain[4], chain2[4];
12424f579ae7SLukas Czerner Indirect *partial, *partial2;
12435e86bddaSzhangyi (F) Indirect *p = NULL, *p2 = NULL;
12444f579ae7SLukas Czerner ext4_lblk_t max_block;
12454f579ae7SLukas Czerner __le32 nr = 0, nr2 = 0;
12464f579ae7SLukas Czerner int n = 0, n2 = 0;
12474f579ae7SLukas Czerner unsigned blocksize = inode->i_sb->s_blocksize;
12488bad6fc8SZheng Liu
12494f579ae7SLukas Czerner max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
12504f579ae7SLukas Czerner >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
12514f579ae7SLukas Czerner if (end >= max_block)
12524f579ae7SLukas Czerner end = max_block;
12534f579ae7SLukas Czerner if ((start >= end) || (start > max_block))
12544f579ae7SLukas Czerner return 0;
1255a93cd4cfSJan Kara
12564f579ae7SLukas Czerner n = ext4_block_to_path(inode, start, offsets, NULL);
12574f579ae7SLukas Czerner n2 = ext4_block_to_path(inode, end, offsets2, NULL);
12584f579ae7SLukas Czerner
12594f579ae7SLukas Czerner BUG_ON(n > n2);
12604f579ae7SLukas Czerner
12614f579ae7SLukas Czerner if ((n == 1) && (n == n2)) {
12624f579ae7SLukas Czerner /* We're punching only within direct block range */
12634f579ae7SLukas Czerner ext4_free_data(handle, inode, NULL, i_data + offsets[0],
12644f579ae7SLukas Czerner i_data + offsets2[0]);
12654f579ae7SLukas Czerner return 0;
12664f579ae7SLukas Czerner } else if (n2 > n) {
12674f579ae7SLukas Czerner /*
12684f579ae7SLukas Czerner * Start and end are on a different levels so we're going to
12694f579ae7SLukas Czerner * free partial block at start, and partial block at end of
12704f579ae7SLukas Czerner * the range. If there are some levels in between then
12714f579ae7SLukas Czerner * do_indirects label will take care of that.
12724f579ae7SLukas Czerner */
12734f579ae7SLukas Czerner
12744f579ae7SLukas Czerner if (n == 1) {
12754f579ae7SLukas Czerner /*
12764f579ae7SLukas Czerner * Start is at the direct block level, free
12774f579ae7SLukas Czerner * everything to the end of the level.
12784f579ae7SLukas Czerner */
12794f579ae7SLukas Czerner ext4_free_data(handle, inode, NULL, i_data + offsets[0],
12804f579ae7SLukas Czerner i_data + EXT4_NDIR_BLOCKS);
12814f579ae7SLukas Czerner goto end_range;
12828bad6fc8SZheng Liu }
12834f579ae7SLukas Czerner
12844f579ae7SLukas Czerner
12855e86bddaSzhangyi (F) partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
12864f579ae7SLukas Czerner if (nr) {
12874f579ae7SLukas Czerner if (partial == chain) {
12884f579ae7SLukas Czerner /* Shared branch grows from the inode */
12894f579ae7SLukas Czerner ext4_free_branches(handle, inode, NULL,
12904f579ae7SLukas Czerner &nr, &nr+1, (chain+n-1) - partial);
12914f579ae7SLukas Czerner *partial->p = 0;
1292a93cd4cfSJan Kara } else {
12934f579ae7SLukas Czerner /* Shared branch grows from an indirect block */
12944f579ae7SLukas Czerner BUFFER_TRACE(partial->bh, "get_write_access");
12954f579ae7SLukas Czerner ext4_free_branches(handle, inode, partial->bh,
12964f579ae7SLukas Czerner partial->p,
12974f579ae7SLukas Czerner partial->p+1, (chain+n-1) - partial);
1298a93cd4cfSJan Kara }
12998bad6fc8SZheng Liu }
13008bad6fc8SZheng Liu
13014f579ae7SLukas Czerner /*
13024f579ae7SLukas Czerner * Clear the ends of indirect blocks on the shared branch
13034f579ae7SLukas Czerner * at the start of the range
13044f579ae7SLukas Czerner */
13054f579ae7SLukas Czerner while (partial > chain) {
13064f579ae7SLukas Czerner ext4_free_branches(handle, inode, partial->bh,
13074f579ae7SLukas Czerner partial->p + 1,
13084f579ae7SLukas Czerner (__le32 *)partial->bh->b_data+addr_per_block,
13094f579ae7SLukas Czerner (chain+n-1) - partial);
13104f579ae7SLukas Czerner partial--;
13118bad6fc8SZheng Liu }
13128bad6fc8SZheng Liu
13134f579ae7SLukas Czerner end_range:
13145e86bddaSzhangyi (F) partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
13154f579ae7SLukas Czerner if (nr2) {
13164f579ae7SLukas Czerner if (partial2 == chain2) {
13174f579ae7SLukas Czerner /*
13184f579ae7SLukas Czerner * Remember, end is exclusive so here we're at
13194f579ae7SLukas Czerner * the start of the next level we're not going
13204f579ae7SLukas Czerner * to free. Everything was covered by the start
13214f579ae7SLukas Czerner * of the range.
13224f579ae7SLukas Czerner */
13236f30b7e3SOmar Sandoval goto do_indirects;
13248bad6fc8SZheng Liu }
13254f579ae7SLukas Czerner } else {
13264f579ae7SLukas Czerner /*
13274f579ae7SLukas Czerner * ext4_find_shared returns Indirect structure which
13284f579ae7SLukas Czerner * points to the last element which should not be
13294f579ae7SLukas Czerner * removed by truncate. But this is end of the range
13304f579ae7SLukas Czerner * in punch_hole so we need to point to the next element
13314f579ae7SLukas Czerner */
13324f579ae7SLukas Czerner partial2->p++;
13334f579ae7SLukas Czerner }
13344f579ae7SLukas Czerner
13354f579ae7SLukas Czerner /*
13364f579ae7SLukas Czerner * Clear the ends of indirect blocks on the shared branch
13374f579ae7SLukas Czerner * at the end of the range
13384f579ae7SLukas Czerner */
13394f579ae7SLukas Czerner while (partial2 > chain2) {
13404f579ae7SLukas Czerner ext4_free_branches(handle, inode, partial2->bh,
13414f579ae7SLukas Czerner (__le32 *)partial2->bh->b_data,
13424f579ae7SLukas Czerner partial2->p,
13434f579ae7SLukas Czerner (chain2+n2-1) - partial2);
13444f579ae7SLukas Czerner partial2--;
13454f579ae7SLukas Czerner }
13464f579ae7SLukas Czerner goto do_indirects;
13474f579ae7SLukas Czerner }
13484f579ae7SLukas Czerner
13494f579ae7SLukas Czerner /* Punch happened within the same level (n == n2) */
13505e86bddaSzhangyi (F) partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
13515e86bddaSzhangyi (F) partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
13526f30b7e3SOmar Sandoval
13536f30b7e3SOmar Sandoval /* Free top, but only if partial2 isn't its subtree. */
13546f30b7e3SOmar Sandoval if (nr) {
13556f30b7e3SOmar Sandoval int level = min(partial - chain, partial2 - chain2);
13566f30b7e3SOmar Sandoval int i;
13576f30b7e3SOmar Sandoval int subtree = 1;
13586f30b7e3SOmar Sandoval
13596f30b7e3SOmar Sandoval for (i = 0; i <= level; i++) {
13606f30b7e3SOmar Sandoval if (offsets[i] != offsets2[i]) {
13616f30b7e3SOmar Sandoval subtree = 0;
13626f30b7e3SOmar Sandoval break;
13636f30b7e3SOmar Sandoval }
13646f30b7e3SOmar Sandoval }
13656f30b7e3SOmar Sandoval
13666f30b7e3SOmar Sandoval if (!subtree) {
13676f30b7e3SOmar Sandoval if (partial == chain) {
13686f30b7e3SOmar Sandoval /* Shared branch grows from the inode */
13696f30b7e3SOmar Sandoval ext4_free_branches(handle, inode, NULL,
13706f30b7e3SOmar Sandoval &nr, &nr+1,
13716f30b7e3SOmar Sandoval (chain+n-1) - partial);
13726f30b7e3SOmar Sandoval *partial->p = 0;
13736f30b7e3SOmar Sandoval } else {
13746f30b7e3SOmar Sandoval /* Shared branch grows from an indirect block */
13756f30b7e3SOmar Sandoval BUFFER_TRACE(partial->bh, "get_write_access");
13766f30b7e3SOmar Sandoval ext4_free_branches(handle, inode, partial->bh,
13776f30b7e3SOmar Sandoval partial->p,
13786f30b7e3SOmar Sandoval partial->p+1,
13796f30b7e3SOmar Sandoval (chain+n-1) - partial);
13806f30b7e3SOmar Sandoval }
13816f30b7e3SOmar Sandoval }
13826f30b7e3SOmar Sandoval }
13836f30b7e3SOmar Sandoval
13846f30b7e3SOmar Sandoval if (!nr2) {
13854f579ae7SLukas Czerner /*
13864f579ae7SLukas Czerner * ext4_find_shared returns Indirect structure which
13874f579ae7SLukas Czerner * points to the last element which should not be
13884f579ae7SLukas Czerner * removed by truncate. But this is end of the range
13894f579ae7SLukas Czerner * in punch_hole so we need to point to the next element
13904f579ae7SLukas Czerner */
13914f579ae7SLukas Czerner partial2->p++;
13926f30b7e3SOmar Sandoval }
13936f30b7e3SOmar Sandoval
13946f30b7e3SOmar Sandoval while (partial > chain || partial2 > chain2) {
13956f30b7e3SOmar Sandoval int depth = (chain+n-1) - partial;
13966f30b7e3SOmar Sandoval int depth2 = (chain2+n2-1) - partial2;
13976f30b7e3SOmar Sandoval
13986f30b7e3SOmar Sandoval if (partial > chain && partial2 > chain2 &&
13996f30b7e3SOmar Sandoval partial->bh->b_blocknr == partial2->bh->b_blocknr) {
14006f30b7e3SOmar Sandoval /*
14016f30b7e3SOmar Sandoval * We've converged on the same block. Clear the range,
14026f30b7e3SOmar Sandoval * then we're done.
14036f30b7e3SOmar Sandoval */
14044f579ae7SLukas Czerner ext4_free_branches(handle, inode, partial->bh,
14054f579ae7SLukas Czerner partial->p + 1,
14064f579ae7SLukas Czerner partial2->p,
14074f579ae7SLukas Czerner (chain+n-1) - partial);
14085e86bddaSzhangyi (F) goto cleanup;
14094f579ae7SLukas Czerner }
14106f30b7e3SOmar Sandoval
14114f579ae7SLukas Czerner /*
14126f30b7e3SOmar Sandoval * The start and end partial branches may not be at the same
14136f30b7e3SOmar Sandoval * level even though the punch happened within one level. So, we
14146f30b7e3SOmar Sandoval * give them a chance to arrive at the same level, then walk
14156f30b7e3SOmar Sandoval * them in step with each other until we converge on the same
14166f30b7e3SOmar Sandoval * block.
14174f579ae7SLukas Czerner */
14186f30b7e3SOmar Sandoval if (partial > chain && depth <= depth2) {
14194f579ae7SLukas Czerner ext4_free_branches(handle, inode, partial->bh,
14204f579ae7SLukas Czerner partial->p + 1,
14214f579ae7SLukas Czerner (__le32 *)partial->bh->b_data+addr_per_block,
14224f579ae7SLukas Czerner (chain+n-1) - partial);
14234f579ae7SLukas Czerner partial--;
14244f579ae7SLukas Czerner }
14256f30b7e3SOmar Sandoval if (partial2 > chain2 && depth2 <= depth) {
14264f579ae7SLukas Czerner ext4_free_branches(handle, inode, partial2->bh,
14274f579ae7SLukas Czerner (__le32 *)partial2->bh->b_data,
14284f579ae7SLukas Czerner partial2->p,
14296f30b7e3SOmar Sandoval (chain2+n2-1) - partial2);
14304f579ae7SLukas Czerner partial2--;
14318bad6fc8SZheng Liu }
14328bad6fc8SZheng Liu }
14335e86bddaSzhangyi (F)
14345e86bddaSzhangyi (F) cleanup:
14355e86bddaSzhangyi (F) while (p && p > chain) {
14365e86bddaSzhangyi (F) BUFFER_TRACE(p->bh, "call brelse");
14375e86bddaSzhangyi (F) brelse(p->bh);
14385e86bddaSzhangyi (F) p--;
14395e86bddaSzhangyi (F) }
14405e86bddaSzhangyi (F) while (p2 && p2 > chain2) {
14415e86bddaSzhangyi (F) BUFFER_TRACE(p2->bh, "call brelse");
14425e86bddaSzhangyi (F) brelse(p2->bh);
14435e86bddaSzhangyi (F) p2--;
14445e86bddaSzhangyi (F) }
14456f30b7e3SOmar Sandoval return 0;
14468bad6fc8SZheng Liu
14474f579ae7SLukas Czerner do_indirects:
14484f579ae7SLukas Czerner /* Kill the remaining (whole) subtrees */
14494f579ae7SLukas Czerner switch (offsets[0]) {
14504f579ae7SLukas Czerner default:
14514f579ae7SLukas Czerner if (++n >= n2)
14525e86bddaSzhangyi (F) break;
14534f579ae7SLukas Czerner nr = i_data[EXT4_IND_BLOCK];
14544f579ae7SLukas Czerner if (nr) {
14554f579ae7SLukas Czerner ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
14564f579ae7SLukas Czerner i_data[EXT4_IND_BLOCK] = 0;
14578bad6fc8SZheng Liu }
145870d7ced2SShijie Luo fallthrough;
14594f579ae7SLukas Czerner case EXT4_IND_BLOCK:
14604f579ae7SLukas Czerner if (++n >= n2)
14615e86bddaSzhangyi (F) break;
14624f579ae7SLukas Czerner nr = i_data[EXT4_DIND_BLOCK];
14634f579ae7SLukas Czerner if (nr) {
14644f579ae7SLukas Czerner ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
14654f579ae7SLukas Czerner i_data[EXT4_DIND_BLOCK] = 0;
14664f579ae7SLukas Czerner }
146770d7ced2SShijie Luo fallthrough;
14684f579ae7SLukas Czerner case EXT4_DIND_BLOCK:
14694f579ae7SLukas Czerner if (++n >= n2)
14705e86bddaSzhangyi (F) break;
14714f579ae7SLukas Czerner nr = i_data[EXT4_TIND_BLOCK];
14724f579ae7SLukas Czerner if (nr) {
14734f579ae7SLukas Czerner ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
14744f579ae7SLukas Czerner i_data[EXT4_TIND_BLOCK] = 0;
14754f579ae7SLukas Czerner }
147670d7ced2SShijie Luo fallthrough;
14774f579ae7SLukas Czerner case EXT4_TIND_BLOCK:
14784f579ae7SLukas Czerner ;
14794f579ae7SLukas Czerner }
14805e86bddaSzhangyi (F) goto cleanup;
14814f579ae7SLukas Czerner }
1482