xref: /openbmc/linux/fs/ext4/migrate.c (revision 00d873c1)
1f5166768STheodore Ts'o // SPDX-License-Identifier: LGPL-2.1
2c14c6fd5SAneesh Kumar K.V /*
3c14c6fd5SAneesh Kumar K.V  * Copyright IBM Corporation, 2007
4c14c6fd5SAneesh Kumar K.V  * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5c14c6fd5SAneesh Kumar K.V  *
6c14c6fd5SAneesh Kumar K.V  */
7c14c6fd5SAneesh Kumar K.V 
85a0e3ad6STejun Heo #include <linux/slab.h>
93dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
104a092d73STheodore Ts'o #include "ext4_extents.h"
11c14c6fd5SAneesh Kumar K.V 
12c14c6fd5SAneesh Kumar K.V /*
13c14c6fd5SAneesh Kumar K.V  * The contiguous blocks details which can be
14c14c6fd5SAneesh Kumar K.V  * represented by a single extent
15c14c6fd5SAneesh Kumar K.V  */
16fba90ffeSDmitry Monakhov struct migrate_struct {
17fba90ffeSDmitry Monakhov 	ext4_lblk_t first_block, last_block, curr_block;
18c14c6fd5SAneesh Kumar K.V 	ext4_fsblk_t first_pblock, last_pblock;
19c14c6fd5SAneesh Kumar K.V };
20c14c6fd5SAneesh Kumar K.V 
finish_range(handle_t * handle,struct inode * inode,struct migrate_struct * lb)21c14c6fd5SAneesh Kumar K.V static int finish_range(handle_t *handle, struct inode *inode,
22fba90ffeSDmitry Monakhov 				struct migrate_struct *lb)
23c14c6fd5SAneesh Kumar K.V 
24c14c6fd5SAneesh Kumar K.V {
25c14c6fd5SAneesh Kumar K.V 	int retval = 0, needed;
26c14c6fd5SAneesh Kumar K.V 	struct ext4_extent newext;
27c14c6fd5SAneesh Kumar K.V 	struct ext4_ext_path *path;
28c14c6fd5SAneesh Kumar K.V 	if (lb->first_pblock == 0)
29c14c6fd5SAneesh Kumar K.V 		return 0;
30c14c6fd5SAneesh Kumar K.V 
31c14c6fd5SAneesh Kumar K.V 	/* Add the extent to temp inode*/
32c14c6fd5SAneesh Kumar K.V 	newext.ee_block = cpu_to_le32(lb->first_block);
33c14c6fd5SAneesh Kumar K.V 	newext.ee_len   = cpu_to_le16(lb->last_block - lb->first_block + 1);
34c14c6fd5SAneesh Kumar K.V 	ext4_ext_store_pblock(&newext, lb->first_pblock);
353088e5a5SBhaskar Chowdhury 	/* Locking only for convenience since we are operating on temp inode */
364b1f1660SDmitry Monakhov 	down_write(&EXT4_I(inode)->i_data_sem);
37ed8a1a76STheodore Ts'o 	path = ext4_find_extent(inode, lb->first_block, NULL, 0);
38c14c6fd5SAneesh Kumar K.V 	if (IS_ERR(path)) {
39c14c6fd5SAneesh Kumar K.V 		retval = PTR_ERR(path);
40b35905c1SAneesh Kumar K.V 		path = NULL;
41c14c6fd5SAneesh Kumar K.V 		goto err_out;
42c14c6fd5SAneesh Kumar K.V 	}
43c14c6fd5SAneesh Kumar K.V 
44c14c6fd5SAneesh Kumar K.V 	/*
45c14c6fd5SAneesh Kumar K.V 	 * Calculate the credit needed to inserting this extent
463088e5a5SBhaskar Chowdhury 	 * Since we are doing this in loop we may accumulate extra
473088e5a5SBhaskar Chowdhury 	 * credit. But below we try to not accumulate too much
48c14c6fd5SAneesh Kumar K.V 	 * of them by restarting the journal.
49c14c6fd5SAneesh Kumar K.V 	 */
50ee12b630SMingming Cao 	needed = ext4_ext_calc_credits_for_single_extent(inode,
51ee12b630SMingming Cao 		    lb->last_block - lb->first_block + 1, path);
52c14c6fd5SAneesh Kumar K.V 
5383448bdfSJan Kara 	retval = ext4_datasem_ensure_credits(handle, inode, needed, needed, 0);
54a4130367SJan Kara 	if (retval < 0)
55c14c6fd5SAneesh Kumar K.V 		goto err_out;
56dfe50809STheodore Ts'o 	retval = ext4_ext_insert_extent(handle, inode, &path, &newext, 0);
57c14c6fd5SAneesh Kumar K.V err_out:
584b1f1660SDmitry Monakhov 	up_write((&EXT4_I(inode)->i_data_sem));
597ff5fddaSYe Bin 	ext4_free_ext_path(path);
60c14c6fd5SAneesh Kumar K.V 	lb->first_pblock = 0;
61c14c6fd5SAneesh Kumar K.V 	return retval;
62c14c6fd5SAneesh Kumar K.V }
63c14c6fd5SAneesh Kumar K.V 
update_extent_range(handle_t * handle,struct inode * inode,ext4_fsblk_t pblock,struct migrate_struct * lb)64c14c6fd5SAneesh Kumar K.V static int update_extent_range(handle_t *handle, struct inode *inode,
65fba90ffeSDmitry Monakhov 			       ext4_fsblk_t pblock, struct migrate_struct *lb)
66c14c6fd5SAneesh Kumar K.V {
67c14c6fd5SAneesh Kumar K.V 	int retval;
68c14c6fd5SAneesh Kumar K.V 	/*
69c14c6fd5SAneesh Kumar K.V 	 * See if we can add on to the existing range (if it exists)
70c14c6fd5SAneesh Kumar K.V 	 */
71c14c6fd5SAneesh Kumar K.V 	if (lb->first_pblock &&
72c14c6fd5SAneesh Kumar K.V 		(lb->last_pblock+1 == pblock) &&
73fba90ffeSDmitry Monakhov 		(lb->last_block+1 == lb->curr_block)) {
74c14c6fd5SAneesh Kumar K.V 		lb->last_pblock = pblock;
75fba90ffeSDmitry Monakhov 		lb->last_block = lb->curr_block;
76fba90ffeSDmitry Monakhov 		lb->curr_block++;
77c14c6fd5SAneesh Kumar K.V 		return 0;
78c14c6fd5SAneesh Kumar K.V 	}
79c14c6fd5SAneesh Kumar K.V 	/*
80c14c6fd5SAneesh Kumar K.V 	 * Start a new range.
81c14c6fd5SAneesh Kumar K.V 	 */
82c14c6fd5SAneesh Kumar K.V 	retval = finish_range(handle, inode, lb);
83c14c6fd5SAneesh Kumar K.V 	lb->first_pblock = lb->last_pblock = pblock;
84fba90ffeSDmitry Monakhov 	lb->first_block = lb->last_block = lb->curr_block;
85fba90ffeSDmitry Monakhov 	lb->curr_block++;
86c14c6fd5SAneesh Kumar K.V 	return retval;
87c14c6fd5SAneesh Kumar K.V }
88c14c6fd5SAneesh Kumar K.V 
update_ind_extent_range(handle_t * handle,struct inode * inode,ext4_fsblk_t pblock,struct migrate_struct * lb)89c14c6fd5SAneesh Kumar K.V static int update_ind_extent_range(handle_t *handle, struct inode *inode,
90fba90ffeSDmitry Monakhov 				   ext4_fsblk_t pblock,
91fba90ffeSDmitry Monakhov 				   struct migrate_struct *lb)
92c14c6fd5SAneesh Kumar K.V {
93c14c6fd5SAneesh Kumar K.V 	struct buffer_head *bh;
94c14c6fd5SAneesh Kumar K.V 	__le32 *i_data;
95c14c6fd5SAneesh Kumar K.V 	int i, retval = 0;
96c14c6fd5SAneesh Kumar K.V 	unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
97c14c6fd5SAneesh Kumar K.V 
98fb265c9cSTheodore Ts'o 	bh = ext4_sb_bread(inode->i_sb, pblock, 0);
99fb265c9cSTheodore Ts'o 	if (IS_ERR(bh))
100fb265c9cSTheodore Ts'o 		return PTR_ERR(bh);
101c14c6fd5SAneesh Kumar K.V 
102c14c6fd5SAneesh Kumar K.V 	i_data = (__le32 *)bh->b_data;
103fba90ffeSDmitry Monakhov 	for (i = 0; i < max_entries; i++) {
104c14c6fd5SAneesh Kumar K.V 		if (i_data[i]) {
105c14c6fd5SAneesh Kumar K.V 			retval = update_extent_range(handle, inode,
106fba90ffeSDmitry Monakhov 						le32_to_cpu(i_data[i]), lb);
107c14c6fd5SAneesh Kumar K.V 			if (retval)
108c14c6fd5SAneesh Kumar K.V 				break;
109fba90ffeSDmitry Monakhov 		} else {
110fba90ffeSDmitry Monakhov 			lb->curr_block++;
111c14c6fd5SAneesh Kumar K.V 		}
112c14c6fd5SAneesh Kumar K.V 	}
113c14c6fd5SAneesh Kumar K.V 	put_bh(bh);
114c14c6fd5SAneesh Kumar K.V 	return retval;
115c14c6fd5SAneesh Kumar K.V 
116c14c6fd5SAneesh Kumar K.V }
117c14c6fd5SAneesh Kumar K.V 
update_dind_extent_range(handle_t * handle,struct inode * inode,ext4_fsblk_t pblock,struct migrate_struct * lb)118c14c6fd5SAneesh Kumar K.V static int update_dind_extent_range(handle_t *handle, struct inode *inode,
119fba90ffeSDmitry Monakhov 				    ext4_fsblk_t pblock,
120fba90ffeSDmitry Monakhov 				    struct migrate_struct *lb)
121c14c6fd5SAneesh Kumar K.V {
122c14c6fd5SAneesh Kumar K.V 	struct buffer_head *bh;
123c14c6fd5SAneesh Kumar K.V 	__le32 *i_data;
124c14c6fd5SAneesh Kumar K.V 	int i, retval = 0;
125c14c6fd5SAneesh Kumar K.V 	unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
126c14c6fd5SAneesh Kumar K.V 
127fb265c9cSTheodore Ts'o 	bh = ext4_sb_bread(inode->i_sb, pblock, 0);
128fb265c9cSTheodore Ts'o 	if (IS_ERR(bh))
129fb265c9cSTheodore Ts'o 		return PTR_ERR(bh);
130c14c6fd5SAneesh Kumar K.V 
131c14c6fd5SAneesh Kumar K.V 	i_data = (__le32 *)bh->b_data;
132c14c6fd5SAneesh Kumar K.V 	for (i = 0; i < max_entries; i++) {
133c14c6fd5SAneesh Kumar K.V 		if (i_data[i]) {
134c14c6fd5SAneesh Kumar K.V 			retval = update_ind_extent_range(handle, inode,
135fba90ffeSDmitry Monakhov 						le32_to_cpu(i_data[i]), lb);
136c14c6fd5SAneesh Kumar K.V 			if (retval)
137c14c6fd5SAneesh Kumar K.V 				break;
138c14c6fd5SAneesh Kumar K.V 		} else {
139c14c6fd5SAneesh Kumar K.V 			/* Only update the file block number */
140fba90ffeSDmitry Monakhov 			lb->curr_block += max_entries;
141c14c6fd5SAneesh Kumar K.V 		}
142c14c6fd5SAneesh Kumar K.V 	}
143c14c6fd5SAneesh Kumar K.V 	put_bh(bh);
144c14c6fd5SAneesh Kumar K.V 	return retval;
145c14c6fd5SAneesh Kumar K.V 
146c14c6fd5SAneesh Kumar K.V }
147c14c6fd5SAneesh Kumar K.V 
update_tind_extent_range(handle_t * handle,struct inode * inode,ext4_fsblk_t pblock,struct migrate_struct * lb)148c14c6fd5SAneesh Kumar K.V static int update_tind_extent_range(handle_t *handle, struct inode *inode,
149fba90ffeSDmitry Monakhov 				    ext4_fsblk_t pblock,
150fba90ffeSDmitry Monakhov 				    struct migrate_struct *lb)
151c14c6fd5SAneesh Kumar K.V {
152c14c6fd5SAneesh Kumar K.V 	struct buffer_head *bh;
153c14c6fd5SAneesh Kumar K.V 	__le32 *i_data;
154c14c6fd5SAneesh Kumar K.V 	int i, retval = 0;
155c14c6fd5SAneesh Kumar K.V 	unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
156c14c6fd5SAneesh Kumar K.V 
157fb265c9cSTheodore Ts'o 	bh = ext4_sb_bread(inode->i_sb, pblock, 0);
158fb265c9cSTheodore Ts'o 	if (IS_ERR(bh))
159fb265c9cSTheodore Ts'o 		return PTR_ERR(bh);
160c14c6fd5SAneesh Kumar K.V 
161c14c6fd5SAneesh Kumar K.V 	i_data = (__le32 *)bh->b_data;
162c14c6fd5SAneesh Kumar K.V 	for (i = 0; i < max_entries; i++) {
163c14c6fd5SAneesh Kumar K.V 		if (i_data[i]) {
164c14c6fd5SAneesh Kumar K.V 			retval = update_dind_extent_range(handle, inode,
165fba90ffeSDmitry Monakhov 						le32_to_cpu(i_data[i]), lb);
166c14c6fd5SAneesh Kumar K.V 			if (retval)
167c14c6fd5SAneesh Kumar K.V 				break;
168fba90ffeSDmitry Monakhov 		} else {
169c14c6fd5SAneesh Kumar K.V 			/* Only update the file block number */
170fba90ffeSDmitry Monakhov 			lb->curr_block += max_entries * max_entries;
171c14c6fd5SAneesh Kumar K.V 		}
172fba90ffeSDmitry Monakhov 	}
173c14c6fd5SAneesh Kumar K.V 	put_bh(bh);
174c14c6fd5SAneesh Kumar K.V 	return retval;
175c14c6fd5SAneesh Kumar K.V 
176c14c6fd5SAneesh Kumar K.V }
177c14c6fd5SAneesh Kumar K.V 
free_dind_blocks(handle_t * handle,struct inode * inode,__le32 i_data)178c14c6fd5SAneesh Kumar K.V static int free_dind_blocks(handle_t *handle,
179c14c6fd5SAneesh Kumar K.V 				struct inode *inode, __le32 i_data)
180c14c6fd5SAneesh Kumar K.V {
181c14c6fd5SAneesh Kumar K.V 	int i;
182c14c6fd5SAneesh Kumar K.V 	__le32 *tmp_idata;
183c14c6fd5SAneesh Kumar K.V 	struct buffer_head *bh;
18483448bdfSJan Kara 	struct super_block *sb = inode->i_sb;
185c14c6fd5SAneesh Kumar K.V 	unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
186a4130367SJan Kara 	int err;
187c14c6fd5SAneesh Kumar K.V 
18883448bdfSJan Kara 	bh = ext4_sb_bread(sb, le32_to_cpu(i_data), 0);
189fb265c9cSTheodore Ts'o 	if (IS_ERR(bh))
190fb265c9cSTheodore Ts'o 		return PTR_ERR(bh);
191c14c6fd5SAneesh Kumar K.V 
192c14c6fd5SAneesh Kumar K.V 	tmp_idata = (__le32 *)bh->b_data;
193c14c6fd5SAneesh Kumar K.V 	for (i = 0; i < max_entries; i++) {
1948009f9fbSAneesh Kumar K.V 		if (tmp_idata[i]) {
195a4130367SJan Kara 			err = ext4_journal_ensure_credits(handle,
19683448bdfSJan Kara 				EXT4_RESERVE_TRANS_BLOCKS,
19783448bdfSJan Kara 				ext4_free_metadata_revoke_credits(sb, 1));
198a4130367SJan Kara 			if (err < 0) {
199a4130367SJan Kara 				put_bh(bh);
200a4130367SJan Kara 				return err;
201a4130367SJan Kara 			}
2027dc57615SPeter Huewe 			ext4_free_blocks(handle, inode, NULL,
203e6362609STheodore Ts'o 					 le32_to_cpu(tmp_idata[i]), 1,
204e6362609STheodore Ts'o 					 EXT4_FREE_BLOCKS_METADATA |
205e6362609STheodore Ts'o 					 EXT4_FREE_BLOCKS_FORGET);
206c14c6fd5SAneesh Kumar K.V 		}
2078009f9fbSAneesh Kumar K.V 	}
208c14c6fd5SAneesh Kumar K.V 	put_bh(bh);
20983448bdfSJan Kara 	err = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS,
21083448bdfSJan Kara 				ext4_free_metadata_revoke_credits(sb, 1));
211a4130367SJan Kara 	if (err < 0)
212a4130367SJan Kara 		return err;
2137dc57615SPeter Huewe 	ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
214e6362609STheodore Ts'o 			 EXT4_FREE_BLOCKS_METADATA |
215e6362609STheodore Ts'o 			 EXT4_FREE_BLOCKS_FORGET);
216c14c6fd5SAneesh Kumar K.V 	return 0;
217c14c6fd5SAneesh Kumar K.V }
218c14c6fd5SAneesh Kumar K.V 
free_tind_blocks(handle_t * handle,struct inode * inode,__le32 i_data)219c14c6fd5SAneesh Kumar K.V static int free_tind_blocks(handle_t *handle,
220c14c6fd5SAneesh Kumar K.V 				struct inode *inode, __le32 i_data)
221c14c6fd5SAneesh Kumar K.V {
222c14c6fd5SAneesh Kumar K.V 	int i, retval = 0;
223c14c6fd5SAneesh Kumar K.V 	__le32 *tmp_idata;
224c14c6fd5SAneesh Kumar K.V 	struct buffer_head *bh;
225c14c6fd5SAneesh Kumar K.V 	unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
226c14c6fd5SAneesh Kumar K.V 
227fb265c9cSTheodore Ts'o 	bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0);
228fb265c9cSTheodore Ts'o 	if (IS_ERR(bh))
229fb265c9cSTheodore Ts'o 		return PTR_ERR(bh);
230c14c6fd5SAneesh Kumar K.V 
231c14c6fd5SAneesh Kumar K.V 	tmp_idata = (__le32 *)bh->b_data;
232c14c6fd5SAneesh Kumar K.V 	for (i = 0; i < max_entries; i++) {
233c14c6fd5SAneesh Kumar K.V 		if (tmp_idata[i]) {
234c14c6fd5SAneesh Kumar K.V 			retval = free_dind_blocks(handle,
235c14c6fd5SAneesh Kumar K.V 					inode, tmp_idata[i]);
236c14c6fd5SAneesh Kumar K.V 			if (retval) {
237c14c6fd5SAneesh Kumar K.V 				put_bh(bh);
238c14c6fd5SAneesh Kumar K.V 				return retval;
239c14c6fd5SAneesh Kumar K.V 			}
240c14c6fd5SAneesh Kumar K.V 		}
241c14c6fd5SAneesh Kumar K.V 	}
242c14c6fd5SAneesh Kumar K.V 	put_bh(bh);
24383448bdfSJan Kara 	retval = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS,
24483448bdfSJan Kara 			ext4_free_metadata_revoke_credits(inode->i_sb, 1));
245a4130367SJan Kara 	if (retval < 0)
246a4130367SJan Kara 		return retval;
2477dc57615SPeter Huewe 	ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
248e6362609STheodore Ts'o 			 EXT4_FREE_BLOCKS_METADATA |
249e6362609STheodore Ts'o 			 EXT4_FREE_BLOCKS_FORGET);
250c14c6fd5SAneesh Kumar K.V 	return 0;
251c14c6fd5SAneesh Kumar K.V }
252c14c6fd5SAneesh Kumar K.V 
free_ind_block(handle_t * handle,struct inode * inode,__le32 * i_data)2538009f9fbSAneesh Kumar K.V static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
254c14c6fd5SAneesh Kumar K.V {
255c14c6fd5SAneesh Kumar K.V 	int retval;
256c14c6fd5SAneesh Kumar K.V 
2578009f9fbSAneesh Kumar K.V 	/* ei->i_data[EXT4_IND_BLOCK] */
2588009f9fbSAneesh Kumar K.V 	if (i_data[0]) {
259a4130367SJan Kara 		retval = ext4_journal_ensure_credits(handle,
26083448bdfSJan Kara 			EXT4_RESERVE_TRANS_BLOCKS,
26183448bdfSJan Kara 			ext4_free_metadata_revoke_credits(inode->i_sb, 1));
262a4130367SJan Kara 		if (retval < 0)
263a4130367SJan Kara 			return retval;
2647dc57615SPeter Huewe 		ext4_free_blocks(handle, inode, NULL,
265e6362609STheodore Ts'o 				le32_to_cpu(i_data[0]), 1,
266e6362609STheodore Ts'o 				 EXT4_FREE_BLOCKS_METADATA |
267e6362609STheodore Ts'o 				 EXT4_FREE_BLOCKS_FORGET);
2688009f9fbSAneesh Kumar K.V 	}
269c14c6fd5SAneesh Kumar K.V 
2708009f9fbSAneesh Kumar K.V 	/* ei->i_data[EXT4_DIND_BLOCK] */
2718009f9fbSAneesh Kumar K.V 	if (i_data[1]) {
2728009f9fbSAneesh Kumar K.V 		retval = free_dind_blocks(handle, inode, i_data[1]);
273c14c6fd5SAneesh Kumar K.V 		if (retval)
274c14c6fd5SAneesh Kumar K.V 			return retval;
275c14c6fd5SAneesh Kumar K.V 	}
276c14c6fd5SAneesh Kumar K.V 
2778009f9fbSAneesh Kumar K.V 	/* ei->i_data[EXT4_TIND_BLOCK] */
2788009f9fbSAneesh Kumar K.V 	if (i_data[2]) {
2798009f9fbSAneesh Kumar K.V 		retval = free_tind_blocks(handle, inode, i_data[2]);
280c14c6fd5SAneesh Kumar K.V 		if (retval)
281c14c6fd5SAneesh Kumar K.V 			return retval;
282c14c6fd5SAneesh Kumar K.V 	}
283c14c6fd5SAneesh Kumar K.V 	return 0;
284c14c6fd5SAneesh Kumar K.V }
285c14c6fd5SAneesh Kumar K.V 
ext4_ext_swap_inode_data(handle_t * handle,struct inode * inode,struct inode * tmp_inode)286c14c6fd5SAneesh Kumar K.V static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
2878009f9fbSAneesh Kumar K.V 						struct inode *tmp_inode)
288c14c6fd5SAneesh Kumar K.V {
2894209ae12SHarshad Shirwadkar 	int retval, retval2 = 0;
2908009f9fbSAneesh Kumar K.V 	__le32	i_data[3];
291c14c6fd5SAneesh Kumar K.V 	struct ext4_inode_info *ei = EXT4_I(inode);
292c14c6fd5SAneesh Kumar K.V 	struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
293c14c6fd5SAneesh Kumar K.V 
294c14c6fd5SAneesh Kumar K.V 	/*
295c14c6fd5SAneesh Kumar K.V 	 * One credit accounted for writing the
296c14c6fd5SAneesh Kumar K.V 	 * i_data field of the original inode
297c14c6fd5SAneesh Kumar K.V 	 */
29883448bdfSJan Kara 	retval = ext4_journal_ensure_credits(handle, 1, 0);
299a4130367SJan Kara 	if (retval < 0)
300c14c6fd5SAneesh Kumar K.V 		goto err_out;
301c14c6fd5SAneesh Kumar K.V 
3028009f9fbSAneesh Kumar K.V 	i_data[0] = ei->i_data[EXT4_IND_BLOCK];
3038009f9fbSAneesh Kumar K.V 	i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
3048009f9fbSAneesh Kumar K.V 	i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
3058009f9fbSAneesh Kumar K.V 
3068009f9fbSAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
307c14c6fd5SAneesh Kumar K.V 	/*
3081b9c12f4STheodore Ts'o 	 * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation
309267e4db9SAneesh Kumar K.V 	 * happened after we started the migrate. We need to
310267e4db9SAneesh Kumar K.V 	 * fail the migrate
311267e4db9SAneesh Kumar K.V 	 */
31219f5fb7aSTheodore Ts'o 	if (!ext4_test_inode_state(inode, EXT4_STATE_EXT_MIGRATE)) {
313267e4db9SAneesh Kumar K.V 		retval = -EAGAIN;
314267e4db9SAneesh Kumar K.V 		up_write(&EXT4_I(inode)->i_data_sem);
315267e4db9SAneesh Kumar K.V 		goto err_out;
316267e4db9SAneesh Kumar K.V 	} else
31719f5fb7aSTheodore Ts'o 		ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
318267e4db9SAneesh Kumar K.V 	/*
319c14c6fd5SAneesh Kumar K.V 	 * We have the extent map build with the tmp inode.
320c14c6fd5SAneesh Kumar K.V 	 * Now copy the i_data across
321c14c6fd5SAneesh Kumar K.V 	 */
32274e4e6dbSTheodore Ts'o 	ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
323c14c6fd5SAneesh Kumar K.V 	memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
324c14c6fd5SAneesh Kumar K.V 
325c14c6fd5SAneesh Kumar K.V 	/*
326c14c6fd5SAneesh Kumar K.V 	 * Update i_blocks with the new blocks that got
327c14c6fd5SAneesh Kumar K.V 	 * allocated while adding extents for extent index
328c14c6fd5SAneesh Kumar K.V 	 * blocks.
329c14c6fd5SAneesh Kumar K.V 	 *
330c14c6fd5SAneesh Kumar K.V 	 * While converting to extents we need not
331b8a07463SAdam Buchbinder 	 * update the original inode i_blocks for extent blocks
332c14c6fd5SAneesh Kumar K.V 	 * via quota APIs. The quota update happened via tmp_inode already.
333c14c6fd5SAneesh Kumar K.V 	 */
334c14c6fd5SAneesh Kumar K.V 	spin_lock(&inode->i_lock);
335c14c6fd5SAneesh Kumar K.V 	inode->i_blocks += tmp_inode->i_blocks;
336c14c6fd5SAneesh Kumar K.V 	spin_unlock(&inode->i_lock);
3378009f9fbSAneesh Kumar K.V 	up_write(&EXT4_I(inode)->i_data_sem);
338c14c6fd5SAneesh Kumar K.V 
3398009f9fbSAneesh Kumar K.V 	/*
3408009f9fbSAneesh Kumar K.V 	 * We mark the inode dirty after, because we decrement the
3418009f9fbSAneesh Kumar K.V 	 * i_blocks when freeing the indirect meta-data blocks
3428009f9fbSAneesh Kumar K.V 	 */
3438009f9fbSAneesh Kumar K.V 	retval = free_ind_block(handle, inode, i_data);
3444209ae12SHarshad Shirwadkar 	retval2 = ext4_mark_inode_dirty(handle, inode);
3454209ae12SHarshad Shirwadkar 	if (unlikely(retval2 && !retval))
3464209ae12SHarshad Shirwadkar 		retval = retval2;
3478009f9fbSAneesh Kumar K.V 
348c14c6fd5SAneesh Kumar K.V err_out:
349c14c6fd5SAneesh Kumar K.V 	return retval;
350c14c6fd5SAneesh Kumar K.V }
351c14c6fd5SAneesh Kumar K.V 
free_ext_idx(handle_t * handle,struct inode * inode,struct ext4_extent_idx * ix)352c14c6fd5SAneesh Kumar K.V static int free_ext_idx(handle_t *handle, struct inode *inode,
353c14c6fd5SAneesh Kumar K.V 					struct ext4_extent_idx *ix)
354c14c6fd5SAneesh Kumar K.V {
355c14c6fd5SAneesh Kumar K.V 	int i, retval = 0;
356c14c6fd5SAneesh Kumar K.V 	ext4_fsblk_t block;
357c14c6fd5SAneesh Kumar K.V 	struct buffer_head *bh;
358c14c6fd5SAneesh Kumar K.V 	struct ext4_extent_header *eh;
359c14c6fd5SAneesh Kumar K.V 
360bf89d16fSTheodore Ts'o 	block = ext4_idx_pblock(ix);
361fb265c9cSTheodore Ts'o 	bh = ext4_sb_bread(inode->i_sb, block, 0);
362fb265c9cSTheodore Ts'o 	if (IS_ERR(bh))
363fb265c9cSTheodore Ts'o 		return PTR_ERR(bh);
364c14c6fd5SAneesh Kumar K.V 
365c14c6fd5SAneesh Kumar K.V 	eh = (struct ext4_extent_header *)bh->b_data;
366c14c6fd5SAneesh Kumar K.V 	if (eh->eh_depth != 0) {
367c14c6fd5SAneesh Kumar K.V 		ix = EXT_FIRST_INDEX(eh);
368c14c6fd5SAneesh Kumar K.V 		for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
369c14c6fd5SAneesh Kumar K.V 			retval = free_ext_idx(handle, inode, ix);
370a4130367SJan Kara 			if (retval) {
371a4130367SJan Kara 				put_bh(bh);
372a4130367SJan Kara 				return retval;
373a4130367SJan Kara 			}
374c14c6fd5SAneesh Kumar K.V 		}
375c14c6fd5SAneesh Kumar K.V 	}
376c14c6fd5SAneesh Kumar K.V 	put_bh(bh);
37783448bdfSJan Kara 	retval = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS,
37883448bdfSJan Kara 			ext4_free_metadata_revoke_credits(inode->i_sb, 1));
379a4130367SJan Kara 	if (retval < 0)
380a4130367SJan Kara 		return retval;
3817dc57615SPeter Huewe 	ext4_free_blocks(handle, inode, NULL, block, 1,
382e6362609STheodore Ts'o 			 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
383a4130367SJan Kara 	return 0;
384c14c6fd5SAneesh Kumar K.V }
385c14c6fd5SAneesh Kumar K.V 
386c14c6fd5SAneesh Kumar K.V /*
387c14c6fd5SAneesh Kumar K.V  * Free the extent meta data blocks only
388c14c6fd5SAneesh Kumar K.V  */
free_ext_block(handle_t * handle,struct inode * inode)389c14c6fd5SAneesh Kumar K.V static int free_ext_block(handle_t *handle, struct inode *inode)
390c14c6fd5SAneesh Kumar K.V {
391c14c6fd5SAneesh Kumar K.V 	int i, retval = 0;
392c14c6fd5SAneesh Kumar K.V 	struct ext4_inode_info *ei = EXT4_I(inode);
393c14c6fd5SAneesh Kumar K.V 	struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
394c14c6fd5SAneesh Kumar K.V 	struct ext4_extent_idx *ix;
395c14c6fd5SAneesh Kumar K.V 	if (eh->eh_depth == 0)
396c14c6fd5SAneesh Kumar K.V 		/*
397c14c6fd5SAneesh Kumar K.V 		 * No extra blocks allocated for extent meta data
398c14c6fd5SAneesh Kumar K.V 		 */
399c14c6fd5SAneesh Kumar K.V 		return 0;
400c14c6fd5SAneesh Kumar K.V 	ix = EXT_FIRST_INDEX(eh);
401c14c6fd5SAneesh Kumar K.V 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
402c14c6fd5SAneesh Kumar K.V 		retval = free_ext_idx(handle, inode, ix);
403c14c6fd5SAneesh Kumar K.V 		if (retval)
404c14c6fd5SAneesh Kumar K.V 			return retval;
405c14c6fd5SAneesh Kumar K.V 	}
406c14c6fd5SAneesh Kumar K.V 	return retval;
407c14c6fd5SAneesh Kumar K.V }
408c14c6fd5SAneesh Kumar K.V 
ext4_ext_migrate(struct inode * inode)4092a43a878SAneesh Kumar K.V int ext4_ext_migrate(struct inode *inode)
410c14c6fd5SAneesh Kumar K.V {
411c14c6fd5SAneesh Kumar K.V 	handle_t *handle;
412c14c6fd5SAneesh Kumar K.V 	int retval = 0, i;
413c14c6fd5SAneesh Kumar K.V 	__le32 *i_data;
414c14c6fd5SAneesh Kumar K.V 	struct ext4_inode_info *ei;
415c14c6fd5SAneesh Kumar K.V 	struct inode *tmp_inode = NULL;
416fba90ffeSDmitry Monakhov 	struct migrate_struct lb;
417c14c6fd5SAneesh Kumar K.V 	unsigned long max_entries;
41807ea7a61SLi Lingfeng 	__u32 goal, tmp_csum_seed;
4195cb81dabSDmitry Monakhov 	uid_t owner[2];
420*00d873c1SJan Kara 	int alloc_ctx;
421c14c6fd5SAneesh Kumar K.V 
422c14c6fd5SAneesh Kumar K.V 	/*
42383982b6fSTheodore Ts'o 	 * If the filesystem does not support extents, or the inode
42483982b6fSTheodore Ts'o 	 * already is extent-based, error out.
425c14c6fd5SAneesh Kumar K.V 	 */
426e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(inode->i_sb) ||
4271b8f787eSYe Bin 	    ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
4281b8f787eSYe Bin 	    ext4_has_inline_data(inode))
429c14c6fd5SAneesh Kumar K.V 		return -EINVAL;
430c14c6fd5SAneesh Kumar K.V 
431b8356c46SValerie Clement 	if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
432b8356c46SValerie Clement 		/*
433b8356c46SValerie Clement 		 * don't migrate fast symlink
434b8356c46SValerie Clement 		 */
435b8356c46SValerie Clement 		return retval;
436b8356c46SValerie Clement 
437*00d873c1SJan Kara 	alloc_ctx = ext4_writepages_down_write(inode->i_sb);
438cb85f4d2SEric Biggers 
4394b217630STheodore Ts'o 	/*
4406eeaf88fSTheodore Ts'o 	 * Worst case we can touch the allocation bitmaps and a block
4411f3ddff3SXiang wangx 	 * group descriptor block.  We do need to worry about
4426eeaf88fSTheodore Ts'o 	 * credits for modifying the quota inode.
4434b217630STheodore Ts'o 	 */
4449924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_MIGRATE,
4456eeaf88fSTheodore Ts'o 		3 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
4464b217630STheodore Ts'o 
447c14c6fd5SAneesh Kumar K.V 	if (IS_ERR(handle)) {
448c14c6fd5SAneesh Kumar K.V 		retval = PTR_ERR(handle);
449cb85f4d2SEric Biggers 		goto out_unlock;
450c14c6fd5SAneesh Kumar K.V 	}
45111013911SAndreas Dilger 	goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) *
45211013911SAndreas Dilger 		EXT4_INODES_PER_GROUP(inode->i_sb)) + 1;
45308cefc7aSEric W. Biederman 	owner[0] = i_uid_read(inode);
45408cefc7aSEric W. Biederman 	owner[1] = i_gid_read(inode);
4552b0143b5SDavid Howells 	tmp_inode = ext4_new_inode(handle, d_inode(inode->i_sb->s_root),
4561b917ed8STahsin Erdogan 				   S_IFREG, NULL, goal, owner, 0);
457c14c6fd5SAneesh Kumar K.V 	if (IS_ERR(tmp_inode)) {
458a0cc910fSDan Carpenter 		retval = PTR_ERR(tmp_inode);
459c14c6fd5SAneesh Kumar K.V 		ext4_journal_stop(handle);
460cb85f4d2SEric Biggers 		goto out_unlock;
461c14c6fd5SAneesh Kumar K.V 	}
462e81c9302SLuís Henriques 	/*
463e81c9302SLuís Henriques 	 * Use the correct seed for checksum (i.e. the seed from 'inode').  This
464e81c9302SLuís Henriques 	 * is so that the metadata blocks will have the correct checksum after
465e81c9302SLuís Henriques 	 * the migration.
466e81c9302SLuís Henriques 	 */
467e81c9302SLuís Henriques 	ei = EXT4_I(inode);
46807ea7a61SLi Lingfeng 	tmp_csum_seed = EXT4_I(tmp_inode)->i_csum_seed;
469e81c9302SLuís Henriques 	EXT4_I(tmp_inode)->i_csum_seed = ei->i_csum_seed;
470c14c6fd5SAneesh Kumar K.V 	i_size_write(tmp_inode, i_size_read(inode));
471c14c6fd5SAneesh Kumar K.V 	/*
472f39490bcSDmitry Monakhov 	 * Set the i_nlink to zero so it will be deleted later
473f39490bcSDmitry Monakhov 	 * when we drop inode reference.
474c14c6fd5SAneesh Kumar K.V 	 */
4756d6b77f1SMiklos Szeredi 	clear_nlink(tmp_inode);
476c14c6fd5SAneesh Kumar K.V 
477c14c6fd5SAneesh Kumar K.V 	ext4_ext_tree_init(handle, tmp_inode);
478c14c6fd5SAneesh Kumar K.V 	ext4_journal_stop(handle);
479c14c6fd5SAneesh Kumar K.V 
480c14c6fd5SAneesh Kumar K.V 	/*
481c14c6fd5SAneesh Kumar K.V 	 * start with one credit accounted for
482c14c6fd5SAneesh Kumar K.V 	 * superblock modification.
483c14c6fd5SAneesh Kumar K.V 	 *
48425985edcSLucas De Marchi 	 * For the tmp_inode we already have committed the
48570261f56SAnatol Pomozov 	 * transaction that created the inode. Later as and
486c14c6fd5SAneesh Kumar K.V 	 * when we add extents we extent the journal
487c14c6fd5SAneesh Kumar K.V 	 */
4888009f9fbSAneesh Kumar K.V 	/*
489f340b3d9Shongnanli 	 * Even though we take i_rwsem we can still cause block
4901b9c12f4STheodore Ts'o 	 * allocation via mmap write to holes. If we have allocated
4911b9c12f4STheodore Ts'o 	 * new blocks we fail migrate.  New block allocation will
4921b9c12f4STheodore Ts'o 	 * clear EXT4_STATE_EXT_MIGRATE flag.  The flag is updated
4931b9c12f4STheodore Ts'o 	 * with i_data_sem held to prevent racing with block
4941b9c12f4STheodore Ts'o 	 * allocation.
495267e4db9SAneesh Kumar K.V 	 */
496c8b459f4SLukas Czerner 	down_read(&EXT4_I(inode)->i_data_sem);
49719f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
498267e4db9SAneesh Kumar K.V 	up_read((&EXT4_I(inode)->i_data_sem));
499267e4db9SAneesh Kumar K.V 
5009924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
501f39490bcSDmitry Monakhov 	if (IS_ERR(handle)) {
502f39490bcSDmitry Monakhov 		retval = PTR_ERR(handle);
503cb85f4d2SEric Biggers 		goto out_tmp_inode;
504f39490bcSDmitry Monakhov 	}
5058009f9fbSAneesh Kumar K.V 
5068009f9fbSAneesh Kumar K.V 	i_data = ei->i_data;
5078009f9fbSAneesh Kumar K.V 	memset(&lb, 0, sizeof(lb));
5088009f9fbSAneesh Kumar K.V 
5098009f9fbSAneesh Kumar K.V 	/* 32 bit block address 4 bytes */
5108009f9fbSAneesh Kumar K.V 	max_entries = inode->i_sb->s_blocksize >> 2;
511fba90ffeSDmitry Monakhov 	for (i = 0; i < EXT4_NDIR_BLOCKS; i++) {
512c14c6fd5SAneesh Kumar K.V 		if (i_data[i]) {
513c14c6fd5SAneesh Kumar K.V 			retval = update_extent_range(handle, tmp_inode,
514fba90ffeSDmitry Monakhov 						le32_to_cpu(i_data[i]), &lb);
515c14c6fd5SAneesh Kumar K.V 			if (retval)
516c14c6fd5SAneesh Kumar K.V 				goto err_out;
517fba90ffeSDmitry Monakhov 		} else
518fba90ffeSDmitry Monakhov 			lb.curr_block++;
519c14c6fd5SAneesh Kumar K.V 	}
520c14c6fd5SAneesh Kumar K.V 	if (i_data[EXT4_IND_BLOCK]) {
521c14c6fd5SAneesh Kumar K.V 		retval = update_ind_extent_range(handle, tmp_inode,
522fba90ffeSDmitry Monakhov 				le32_to_cpu(i_data[EXT4_IND_BLOCK]), &lb);
523c14c6fd5SAneesh Kumar K.V 		if (retval)
524c14c6fd5SAneesh Kumar K.V 			goto err_out;
525c14c6fd5SAneesh Kumar K.V 	} else
526fba90ffeSDmitry Monakhov 		lb.curr_block += max_entries;
527c14c6fd5SAneesh Kumar K.V 	if (i_data[EXT4_DIND_BLOCK]) {
528c14c6fd5SAneesh Kumar K.V 		retval = update_dind_extent_range(handle, tmp_inode,
529fba90ffeSDmitry Monakhov 				le32_to_cpu(i_data[EXT4_DIND_BLOCK]), &lb);
530c14c6fd5SAneesh Kumar K.V 		if (retval)
531c14c6fd5SAneesh Kumar K.V 			goto err_out;
532c14c6fd5SAneesh Kumar K.V 	} else
533fba90ffeSDmitry Monakhov 		lb.curr_block += max_entries * max_entries;
534c14c6fd5SAneesh Kumar K.V 	if (i_data[EXT4_TIND_BLOCK]) {
535c14c6fd5SAneesh Kumar K.V 		retval = update_tind_extent_range(handle, tmp_inode,
536fba90ffeSDmitry Monakhov 				le32_to_cpu(i_data[EXT4_TIND_BLOCK]), &lb);
537c14c6fd5SAneesh Kumar K.V 		if (retval)
538c14c6fd5SAneesh Kumar K.V 			goto err_out;
539c14c6fd5SAneesh Kumar K.V 	}
540c14c6fd5SAneesh Kumar K.V 	/*
541c14c6fd5SAneesh Kumar K.V 	 * Build the last extent
542c14c6fd5SAneesh Kumar K.V 	 */
543c14c6fd5SAneesh Kumar K.V 	retval = finish_range(handle, tmp_inode, &lb);
544c14c6fd5SAneesh Kumar K.V err_out:
545c14c6fd5SAneesh Kumar K.V 	if (retval)
546c14c6fd5SAneesh Kumar K.V 		/*
547c14c6fd5SAneesh Kumar K.V 		 * Failure case delete the extent information with the
548c14c6fd5SAneesh Kumar K.V 		 * tmp_inode
549c14c6fd5SAneesh Kumar K.V 		 */
550c14c6fd5SAneesh Kumar K.V 		free_ext_block(handle, tmp_inode);
551267e4db9SAneesh Kumar K.V 	else {
552267e4db9SAneesh Kumar K.V 		retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
553267e4db9SAneesh Kumar K.V 		if (retval)
554267e4db9SAneesh Kumar K.V 			/*
555267e4db9SAneesh Kumar K.V 			 * if we fail to swap inode data free the extent
556267e4db9SAneesh Kumar K.V 			 * details of the tmp inode
557267e4db9SAneesh Kumar K.V 			 */
558267e4db9SAneesh Kumar K.V 			free_ext_block(handle, tmp_inode);
559267e4db9SAneesh Kumar K.V 	}
5608009f9fbSAneesh Kumar K.V 
5618009f9fbSAneesh Kumar K.V 	/* We mark the tmp_inode dirty via ext4_ext_tree_init. */
56283448bdfSJan Kara 	retval = ext4_journal_ensure_credits(handle, 1, 0);
563a4130367SJan Kara 	if (retval < 0)
564a4130367SJan Kara 		goto out_stop;
565c14c6fd5SAneesh Kumar K.V 	/*
566c14c6fd5SAneesh Kumar K.V 	 * Mark the tmp_inode as of size zero
567c14c6fd5SAneesh Kumar K.V 	 */
568c14c6fd5SAneesh Kumar K.V 	i_size_write(tmp_inode, 0);
569c14c6fd5SAneesh Kumar K.V 
570c14c6fd5SAneesh Kumar K.V 	/*
571c14c6fd5SAneesh Kumar K.V 	 * set the  i_blocks count to zero
57258d86a50SWang Shilong 	 * so that the ext4_evict_inode() does the
573c14c6fd5SAneesh Kumar K.V 	 * right job
574c14c6fd5SAneesh Kumar K.V 	 *
575c14c6fd5SAneesh Kumar K.V 	 * We don't need to take the i_lock because
576c14c6fd5SAneesh Kumar K.V 	 * the inode is not visible to user space.
577c14c6fd5SAneesh Kumar K.V 	 */
578c14c6fd5SAneesh Kumar K.V 	tmp_inode->i_blocks = 0;
57907ea7a61SLi Lingfeng 	EXT4_I(tmp_inode)->i_csum_seed = tmp_csum_seed;
580c14c6fd5SAneesh Kumar K.V 
581c14c6fd5SAneesh Kumar K.V 	/* Reset the extent details */
582c14c6fd5SAneesh Kumar K.V 	ext4_ext_tree_init(handle, tmp_inode);
583a4130367SJan Kara out_stop:
584c14c6fd5SAneesh Kumar K.V 	ext4_journal_stop(handle);
585cb85f4d2SEric Biggers out_tmp_inode:
586a8526e84SAneesh Kumar K.V 	unlock_new_inode(tmp_inode);
587c14c6fd5SAneesh Kumar K.V 	iput(tmp_inode);
588cb85f4d2SEric Biggers out_unlock:
589*00d873c1SJan Kara 	ext4_writepages_up_write(inode->i_sb, alloc_ctx);
590c14c6fd5SAneesh Kumar K.V 	return retval;
591c14c6fd5SAneesh Kumar K.V }
5920d14b098SLukas Czerner 
5930d14b098SLukas Czerner /*
5940d14b098SLukas Czerner  * Migrate a simple extent-based inode to use the i_blocks[] array
5950d14b098SLukas Czerner  */
ext4_ind_migrate(struct inode * inode)5960d14b098SLukas Czerner int ext4_ind_migrate(struct inode *inode)
5970d14b098SLukas Czerner {
5980d14b098SLukas Czerner 	struct ext4_extent_header	*eh;
599cb85f4d2SEric Biggers 	struct ext4_sb_info		*sbi = EXT4_SB(inode->i_sb);
600cb85f4d2SEric Biggers 	struct ext4_super_block		*es = sbi->s_es;
6010d14b098SLukas Czerner 	struct ext4_inode_info		*ei = EXT4_I(inode);
6020d14b098SLukas Czerner 	struct ext4_extent		*ex;
6030d14b098SLukas Czerner 	unsigned int			i, len;
6048974fec7SEryu Guan 	ext4_lblk_t			start, end;
6050d14b098SLukas Czerner 	ext4_fsblk_t			blk;
6060d14b098SLukas Czerner 	handle_t			*handle;
6074209ae12SHarshad Shirwadkar 	int				ret, ret2 = 0;
608*00d873c1SJan Kara 	int				alloc_ctx;
6090d14b098SLukas Czerner 
610e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(inode->i_sb) ||
6110d14b098SLukas Czerner 	    (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
6120d14b098SLukas Czerner 		return -EINVAL;
6130d14b098SLukas Czerner 
614e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(inode->i_sb))
61543e50f50SLukas Czerner 		return -EOPNOTSUPP;
61643e50f50SLukas Czerner 
617d6f123a9SEryu Guan 	/*
618d6f123a9SEryu Guan 	 * In order to get correct extent info, force all delayed allocation
619d6f123a9SEryu Guan 	 * blocks to be allocated, otherwise delayed allocation blocks may not
620d6f123a9SEryu Guan 	 * be reflected and bypass the checks on extent header.
621d6f123a9SEryu Guan 	 */
622d6f123a9SEryu Guan 	if (test_opt(inode->i_sb, DELALLOC))
623d6f123a9SEryu Guan 		ext4_alloc_da_blocks(inode);
624d6f123a9SEryu Guan 
625*00d873c1SJan Kara 	alloc_ctx = ext4_writepages_down_write(inode->i_sb);
626cb85f4d2SEric Biggers 
6270d14b098SLukas Czerner 	handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
628cb85f4d2SEric Biggers 	if (IS_ERR(handle)) {
629cb85f4d2SEric Biggers 		ret = PTR_ERR(handle);
630cb85f4d2SEric Biggers 		goto out_unlock;
631cb85f4d2SEric Biggers 	}
6320d14b098SLukas Czerner 
6330d14b098SLukas Czerner 	down_write(&EXT4_I(inode)->i_data_sem);
6340d14b098SLukas Czerner 	ret = ext4_ext_check_inode(inode);
6350d14b098SLukas Czerner 	if (ret)
6360d14b098SLukas Czerner 		goto errout;
6370d14b098SLukas Czerner 
6380d14b098SLukas Czerner 	eh = ext_inode_hdr(inode);
6390d14b098SLukas Czerner 	ex  = EXT_FIRST_EXTENT(eh);
6400d14b098SLukas Czerner 	if (ext4_blocks_count(es) > EXT4_MAX_BLOCK_FILE_PHYS ||
6410d14b098SLukas Czerner 	    eh->eh_depth != 0 || le16_to_cpu(eh->eh_entries) > 1) {
6420d14b098SLukas Czerner 		ret = -EOPNOTSUPP;
6430d14b098SLukas Czerner 		goto errout;
6440d14b098SLukas Czerner 	}
6450d14b098SLukas Czerner 	if (eh->eh_entries == 0)
6468974fec7SEryu Guan 		blk = len = start = end = 0;
6470d14b098SLukas Czerner 	else {
6480d14b098SLukas Czerner 		len = le16_to_cpu(ex->ee_len);
6490d14b098SLukas Czerner 		blk = ext4_ext_pblock(ex);
6508974fec7SEryu Guan 		start = le32_to_cpu(ex->ee_block);
6518974fec7SEryu Guan 		end = start + len - 1;
652d6f123a9SEryu Guan 		if (end >= EXT4_NDIR_BLOCKS) {
6530d14b098SLukas Czerner 			ret = -EOPNOTSUPP;
6540d14b098SLukas Czerner 			goto errout;
6550d14b098SLukas Czerner 		}
6560d14b098SLukas Czerner 	}
6570d14b098SLukas Czerner 
6580d14b098SLukas Czerner 	ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
6590d14b098SLukas Czerner 	memset(ei->i_data, 0, sizeof(ei->i_data));
6608974fec7SEryu Guan 	for (i = start; i <= end; i++)
6610d14b098SLukas Czerner 		ei->i_data[i] = cpu_to_le32(blk++);
6624209ae12SHarshad Shirwadkar 	ret2 = ext4_mark_inode_dirty(handle, inode);
6634209ae12SHarshad Shirwadkar 	if (unlikely(ret2 && !ret))
6644209ae12SHarshad Shirwadkar 		ret = ret2;
6650d14b098SLukas Czerner errout:
6660d14b098SLukas Czerner 	ext4_journal_stop(handle);
6670d14b098SLukas Czerner 	up_write(&EXT4_I(inode)->i_data_sem);
668cb85f4d2SEric Biggers out_unlock:
669*00d873c1SJan Kara 	ext4_writepages_up_write(inode->i_sb, alloc_ctx);
6700d14b098SLukas Czerner 	return ret;
6710d14b098SLukas Czerner }
672