blob: bc96529d1509bbc812e6e7b66a77825c3a1cad94 [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0
Alex Tomasa86c6182006-10-11 01:21:03 -07002/*
3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4 * Written by Alex Tomas <alex@clusterfs.com>
5 *
6 * Architecture independence:
7 * Copyright (c) 2005, Bull S.A.
8 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
Alex Tomasa86c6182006-10-11 01:21:03 -07009 */
10
11/*
12 * Extents support for EXT4
13 *
14 * TODO:
15 * - ext4*_error() should be used in some situations
16 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
17 * - smart tree reduction
18 */
19
Alex Tomasa86c6182006-10-11 01:21:03 -070020#include <linux/fs.h>
21#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040022#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070023#include <linux/highuid.h>
24#include <linux/pagemap.h>
25#include <linux/quotaops.h>
26#include <linux/string.h>
27#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040029#include <linux/fiemap.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040030#include <linux/backing-dev.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040031#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050032#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050033#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070034
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040035#include <trace/events/ext4.h>
36
Lukas Czerner5f95d212012-03-19 23:03:19 -040037/*
38 * used by extent splitting.
39 */
40#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
41 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040042#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
43#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040044
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040045#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
46#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
47
Darrick J. Wong7ac59902012-04-29 18:37:10 -040048static __le32 ext4_extent_block_csum(struct inode *inode,
49 struct ext4_extent_header *eh)
50{
51 struct ext4_inode_info *ei = EXT4_I(inode);
52 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
53 __u32 csum;
54
55 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
56 EXT4_EXTENT_TAIL_OFFSET(eh));
57 return cpu_to_le32(csum);
58}
59
60static int ext4_extent_block_csum_verify(struct inode *inode,
61 struct ext4_extent_header *eh)
62{
63 struct ext4_extent_tail *et;
64
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040065 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040066 return 1;
67
68 et = find_ext4_extent_tail(eh);
69 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
70 return 0;
71 return 1;
72}
73
74static void ext4_extent_block_csum_set(struct inode *inode,
75 struct ext4_extent_header *eh)
76{
77 struct ext4_extent_tail *et;
78
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040079 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040080 return;
81
82 et = find_ext4_extent_tail(eh);
83 et->et_checksum = ext4_extent_block_csum(inode, eh);
84}
85
Lukas Czerner5f95d212012-03-19 23:03:19 -040086static int ext4_split_extent_at(handle_t *handle,
87 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040088 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -040089 ext4_lblk_t split,
90 int split_flag,
91 int flags);
92
Lukas Czerner91dd8c12012-11-28 12:32:26 -050093static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -050094 struct extent_status *newes);
Lukas Czerner91dd8c12012-11-28 12:32:26 -050095
Jan Karaa4130362019-11-05 17:44:16 +010096static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
Alex Tomasa86c6182006-10-11 01:21:03 -070097{
Theodore Ts'o7b808192016-04-25 23:13:17 -040098 /*
Jan Karaa4130362019-11-05 17:44:16 +010099 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
100 * moment, get_block can be called only for blocks inside i_size since
101 * page cache has been already dropped and writes are blocked by
102 * i_mutex. So we can safely drop the i_data_sem here.
Theodore Ts'o7b808192016-04-25 23:13:17 -0400103 */
Jan Karaa4130362019-11-05 17:44:16 +0100104 BUG_ON(EXT4_JOURNAL(inode) == NULL);
105 ext4_discard_preallocations(inode);
106 up_write(&EXT4_I(inode)->i_data_sem);
107 *dropped = 1;
108 return 0;
109}
Jan Kara487caee2009-08-17 22:17:20 -0400110
Jan Karaa4130362019-11-05 17:44:16 +0100111/*
112 * Make sure 'handle' has at least 'check_cred' credits. If not, restart
113 * transaction with 'restart_cred' credits. The function drops i_data_sem
114 * when restarting transaction and gets it after transaction is restarted.
115 *
116 * The function returns 0 on success, 1 if transaction had to be restarted,
117 * and < 0 in case of fatal error.
118 */
119int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
Jan Kara83448bd2019-11-05 17:44:29 +0100120 int check_cred, int restart_cred,
121 int revoke_cred)
Jan Karaa4130362019-11-05 17:44:16 +0100122{
123 int ret;
124 int dropped = 0;
125
126 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
Jan Kara83448bd2019-11-05 17:44:29 +0100127 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
Jan Karaa4130362019-11-05 17:44:16 +0100128 if (dropped)
129 down_write(&EXT4_I(inode)->i_data_sem);
130 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700131}
132
133/*
134 * could return:
135 * - EROFS
136 * - ENOMEM
137 */
138static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
139 struct ext4_ext_path *path)
140{
141 if (path->p_bh) {
142 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400143 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700144 return ext4_journal_get_write_access(handle, path->p_bh);
145 }
146 /* path points to leaf/index in inode body */
147 /* we use in-core data, no need to protect them */
148 return 0;
149}
150
151/*
152 * could return:
153 * - EROFS
154 * - ENOMEM
155 * - EIO
156 */
Eric Biggers43f81672019-12-31 12:04:40 -0600157static int __ext4_ext_dirty(const char *where, unsigned int line,
158 handle_t *handle, struct inode *inode,
159 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700160{
161 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400162
163 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700164 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400165 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700166 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400167 err = __ext4_handle_dirty_metadata(where, line, handle,
168 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700169 } else {
170 /* path points to leaf/index in inode body */
171 err = ext4_mark_inode_dirty(handle, inode);
172 }
173 return err;
174}
175
Eric Biggers43f81672019-12-31 12:04:40 -0600176#define ext4_ext_dirty(handle, inode, path) \
177 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
178
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700179static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700180 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500181 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700182{
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400184 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700185 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700186
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500187 /*
188 * Try to predict block placement assuming that we are
189 * filling in a file which will eventually be
190 * non-sparse --- i.e., in the case of libbfd writing
191 * an ELF object sections out-of-order but in a way
192 * the eventually results in a contiguous object or
193 * executable file, or some database extending a table
194 * space file. However, this is actually somewhat
195 * non-ideal if we are writing a sparse file such as
196 * qemu or KVM writing a raw image file that is going
197 * to stay fairly sparse, since it will end up
198 * fragmenting the file system's free space. Maybe we
199 * should have some hueristics or some way to allow
200 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800201 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500202 * common.
203 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800204 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500205 if (ex) {
206 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
207 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
208
209 if (block > ext_block)
210 return ext_pblk + (block - ext_block);
211 else
212 return ext_pblk - (ext_block - block);
213 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700214
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700215 /* it looks like index is empty;
216 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700217 if (path[depth].p_bh)
218 return path[depth].p_bh->b_blocknr;
219 }
220
221 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400222 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700223}
224
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400225/*
226 * Allocation for a meta data block
227 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700228static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400229ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700230 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400231 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700232{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700233 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700234
235 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400236 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
237 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700238 return newblock;
239}
240
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400241static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700242{
243 int size;
244
245 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
246 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100247#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400248 if (!check && size > 6)
249 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700250#endif
251 return size;
252}
253
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400254static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700255{
256 int size;
257
258 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
259 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100260#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400261 if (!check && size > 5)
262 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700263#endif
264 return size;
265}
266
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400267static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700268{
269 int size;
270
271 size = sizeof(EXT4_I(inode)->i_data);
272 size -= sizeof(struct ext4_extent_header);
273 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100274#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400275 if (!check && size > 3)
276 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700277#endif
278 return size;
279}
280
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400281static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700282{
283 int size;
284
285 size = sizeof(EXT4_I(inode)->i_data);
286 size -= sizeof(struct ext4_extent_header);
287 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100288#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400289 if (!check && size > 4)
290 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700291#endif
292 return size;
293}
294
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400295static inline int
296ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400297 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400298 int nofail)
299{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400300 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400301 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
302
Theodore Ts'odfe50802014-09-01 14:37:09 -0400303 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400304 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
305 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
306 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
307}
308
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400309static int
310ext4_ext_max_entries(struct inode *inode, int depth)
311{
312 int max;
313
314 if (depth == ext_depth(inode)) {
315 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400316 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400317 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400318 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400319 } else {
320 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400321 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400322 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400323 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400324 }
325
326 return max;
327}
328
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400329static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
330{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400331 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400332 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500333 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400334
Vegard Nossumf70749c2016-06-30 11:53:46 -0400335 /*
336 * We allow neither:
337 * - zero length
338 * - overflow/wrap-around
339 */
340 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400341 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400342 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400343}
344
345static int ext4_valid_extent_idx(struct inode *inode,
346 struct ext4_extent_idx *ext_idx)
347{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400348 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400349
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400350 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400351}
352
353static int ext4_valid_extent_entries(struct inode *inode,
354 struct ext4_extent_header *eh,
355 int depth)
356{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400357 unsigned short entries;
358 if (eh->eh_entries == 0)
359 return 1;
360
361 entries = le16_to_cpu(eh->eh_entries);
362
363 if (depth == 0) {
364 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400365 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500366 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
367 ext4_fsblk_t pblock = 0;
368 ext4_lblk_t lblock = 0;
369 ext4_lblk_t prev = 0;
370 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400371 while (entries) {
372 if (!ext4_valid_extent(inode, ext))
373 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500374
375 /* Check for overlapping extents */
376 lblock = le32_to_cpu(ext->ee_block);
377 len = ext4_ext_get_actual_len(ext);
378 if ((lblock <= prev) && prev) {
379 pblock = ext4_ext_pblock(ext);
380 es->s_last_error_block = cpu_to_le64(pblock);
381 return 0;
382 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400383 ext++;
384 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500385 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400386 }
387 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400388 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400389 while (entries) {
390 if (!ext4_valid_extent_idx(inode, ext_idx))
391 return 0;
392 ext_idx++;
393 entries--;
394 }
395 }
396 return 1;
397}
398
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400399static int __ext4_ext_check(const char *function, unsigned int line,
400 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400401 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400402{
403 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400404 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400405
406 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
407 error_msg = "invalid magic";
408 goto corrupted;
409 }
410 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
411 error_msg = "unexpected eh_depth";
412 goto corrupted;
413 }
414 if (unlikely(eh->eh_max == 0)) {
415 error_msg = "invalid eh_max";
416 goto corrupted;
417 }
418 max = ext4_ext_max_entries(inode, depth);
419 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
420 error_msg = "too large eh_max";
421 goto corrupted;
422 }
423 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
424 error_msg = "invalid eh_entries";
425 goto corrupted;
426 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400427 if (!ext4_valid_extent_entries(inode, eh, depth)) {
428 error_msg = "invalid extent entries";
429 goto corrupted;
430 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400431 if (unlikely(depth > 32)) {
432 error_msg = "too large eh_depth";
433 goto corrupted;
434 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400435 /* Verify checksum on non-root extent tree nodes */
436 if (ext_depth(inode) != depth &&
437 !ext4_extent_block_csum_verify(inode, eh)) {
438 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400439 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400440 goto corrupted;
441 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400442 return 0;
443
444corrupted:
Theodore Ts'o878520a2019-11-19 21:54:15 -0500445 ext4_set_errno(inode->i_sb, -err);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400446 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400447 "pblk %llu bad header/extent: %s - magic %x, "
448 "entries %u, max %u(%u), depth %u(%u)",
449 (unsigned long long) pblk, error_msg,
450 le16_to_cpu(eh->eh_magic),
451 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
452 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400453 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400454}
455
Theodore Ts'oc3491792013-08-16 21:21:41 -0400456#define ext4_ext_check(inode, eh, depth, pblk) \
457 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400458
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400459int ext4_ext_check_inode(struct inode *inode)
460{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400461 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400462}
463
Dmitry Monakhov40686642019-11-06 12:25:02 +0000464static void ext4_cache_extents(struct inode *inode,
465 struct ext4_extent_header *eh)
466{
467 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
468 ext4_lblk_t prev = 0;
469 int i;
470
471 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
472 unsigned int status = EXTENT_STATUS_WRITTEN;
473 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
474 int len = ext4_ext_get_actual_len(ex);
475
476 if (prev && (prev != lblk))
477 ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
478 EXTENT_STATUS_HOLE);
479
480 if (ext4_ext_is_unwritten(ex))
481 status = EXTENT_STATUS_UNWRITTEN;
482 ext4_es_cache_extent(inode, lblk, len,
483 ext4_ext_pblock(ex), status);
484 prev = lblk + len;
485 }
486}
487
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400488static struct buffer_head *
489__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400490 struct inode *inode, ext4_fsblk_t pblk, int depth,
491 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400492{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400493 struct buffer_head *bh;
494 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400495
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400496 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400497 if (unlikely(!bh))
498 return ERR_PTR(-ENOMEM);
499
500 if (!bh_uptodate_or_lock(bh)) {
501 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
502 err = bh_submit_read(bh);
503 if (err < 0)
504 goto errout;
505 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400506 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400507 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400508 if (!ext4_has_feature_journal(inode->i_sb) ||
509 (inode->i_ino !=
510 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
511 err = __ext4_ext_check(function, line, inode,
512 ext_block_hdr(bh), depth, pblk);
513 if (err)
514 goto errout;
515 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400516 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400517 /*
518 * If this is a leaf block, cache all of its entries
519 */
520 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
521 struct ext4_extent_header *eh = ext_block_hdr(bh);
Dmitry Monakhov40686642019-11-06 12:25:02 +0000522 ext4_cache_extents(inode, eh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400523 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400524 return bh;
525errout:
526 put_bh(bh);
527 return ERR_PTR(err);
528
Darrick J. Wongf8489122012-04-29 18:21:10 -0400529}
530
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400531#define read_extent_tree_block(inode, pblk, depth, flags) \
532 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
533 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400534
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400535/*
536 * This function is called to cache a file's extent information in the
537 * extent status tree
538 */
539int ext4_ext_precache(struct inode *inode)
540{
541 struct ext4_inode_info *ei = EXT4_I(inode);
542 struct ext4_ext_path *path = NULL;
543 struct buffer_head *bh;
544 int i = 0, depth, ret = 0;
545
546 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
547 return 0; /* not an extent-mapped inode */
548
549 down_read(&ei->i_data_sem);
550 depth = ext_depth(inode);
551
Kees Cook6396bb22018-06-12 14:03:40 -0700552 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400553 GFP_NOFS);
554 if (path == NULL) {
555 up_read(&ei->i_data_sem);
556 return -ENOMEM;
557 }
558
559 /* Don't cache anything if there are no external extent blocks */
560 if (depth == 0)
561 goto out;
562 path[0].p_hdr = ext_inode_hdr(inode);
563 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
564 if (ret)
565 goto out;
566 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
567 while (i >= 0) {
568 /*
569 * If this is a leaf block or we've reached the end of
570 * the index block, go up
571 */
572 if ((i == depth) ||
573 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
574 brelse(path[i].p_bh);
575 path[i].p_bh = NULL;
576 i--;
577 continue;
578 }
579 bh = read_extent_tree_block(inode,
580 ext4_idx_pblock(path[i].p_idx++),
581 depth - i - 1,
582 EXT4_EX_FORCE_CACHE);
583 if (IS_ERR(bh)) {
584 ret = PTR_ERR(bh);
585 break;
586 }
587 i++;
588 path[i].p_bh = bh;
589 path[i].p_hdr = ext_block_hdr(bh);
590 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
591 }
592 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
593out:
594 up_read(&ei->i_data_sem);
595 ext4_ext_drop_refs(path);
596 kfree(path);
597 return ret;
598}
599
Alex Tomasa86c6182006-10-11 01:21:03 -0700600#ifdef EXT_DEBUG
601static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
602{
603 int k, l = path->p_depth;
604
605 ext_debug("path:");
606 for (k = 0; k <= l; k++, path++) {
607 if (path->p_idx) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600608 ext_debug(" %d->%llu",
609 le32_to_cpu(path->p_idx->ei_block),
610 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700611 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400612 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700613 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400614 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400615 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400616 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700617 } else
618 ext_debug(" []");
619 }
620 ext_debug("\n");
621}
622
623static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
624{
625 int depth = ext_depth(inode);
626 struct ext4_extent_header *eh;
627 struct ext4_extent *ex;
628 int i;
629
630 if (!path)
631 return;
632
633 eh = path[depth].p_hdr;
634 ex = EXT_FIRST_EXTENT(eh);
635
Mingming553f9002009-09-18 13:34:55 -0400636 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
637
Alex Tomasa86c6182006-10-11 01:21:03 -0700638 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400639 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400640 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400641 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700642 }
643 ext_debug("\n");
644}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400645
646static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
647 ext4_fsblk_t newblock, int level)
648{
649 int depth = ext_depth(inode);
650 struct ext4_extent *ex;
651
652 if (depth != level) {
653 struct ext4_extent_idx *idx;
654 idx = path[level].p_idx;
655 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
656 ext_debug("%d: move %d:%llu in new index %llu\n", level,
657 le32_to_cpu(idx->ei_block),
658 ext4_idx_pblock(idx),
659 newblock);
660 idx++;
661 }
662
663 return;
664 }
665
666 ex = path[depth].p_ext;
667 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
668 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
669 le32_to_cpu(ex->ee_block),
670 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400671 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400672 ext4_ext_get_actual_len(ex),
673 newblock);
674 ex++;
675 }
676}
677
Alex Tomasa86c6182006-10-11 01:21:03 -0700678#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400679#define ext4_ext_show_path(inode, path)
680#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400681#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700682#endif
683
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500684void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700685{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400686 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700687
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400688 if (!path)
689 return;
690 depth = path->p_depth;
Eric Biggersde745482019-12-31 12:04:44 -0600691 for (i = 0; i <= depth; i++, path++) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700692 if (path->p_bh) {
693 brelse(path->p_bh);
694 path->p_bh = NULL;
695 }
Eric Biggersde745482019-12-31 12:04:44 -0600696 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700697}
698
699/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700700 * ext4_ext_binsearch_idx:
701 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400702 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700703 */
704static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500705ext4_ext_binsearch_idx(struct inode *inode,
706 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700707{
708 struct ext4_extent_header *eh = path->p_hdr;
709 struct ext4_extent_idx *r, *l, *m;
710
Alex Tomasa86c6182006-10-11 01:21:03 -0700711
Eric Sandeenbba90742008-01-28 23:58:27 -0500712 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700713
714 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400715 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700716 while (l <= r) {
717 m = l + (r - l) / 2;
718 if (block < le32_to_cpu(m->ei_block))
719 r = m - 1;
720 else
721 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400722 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
723 m, le32_to_cpu(m->ei_block),
724 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700725 }
726
727 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400728 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400729 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700730
731#ifdef CHECK_BINSEARCH
732 {
733 struct ext4_extent_idx *chix, *ix;
734 int k;
735
736 chix = ix = EXT_FIRST_INDEX(eh);
737 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600738 if (k != 0 && le32_to_cpu(ix->ei_block) <=
739 le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400740 printk(KERN_DEBUG "k=%d, ix=0x%p, "
741 "first=0x%p\n", k,
742 ix, EXT_FIRST_INDEX(eh));
743 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700744 le32_to_cpu(ix->ei_block),
745 le32_to_cpu(ix[-1].ei_block));
746 }
747 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400748 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700749 if (block < le32_to_cpu(ix->ei_block))
750 break;
751 chix = ix;
752 }
753 BUG_ON(chix != path->p_idx);
754 }
755#endif
756
757}
758
759/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700760 * ext4_ext_binsearch:
761 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400762 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700763 */
764static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500765ext4_ext_binsearch(struct inode *inode,
766 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700767{
768 struct ext4_extent_header *eh = path->p_hdr;
769 struct ext4_extent *r, *l, *m;
770
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 if (eh->eh_entries == 0) {
772 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700773 * this leaf is empty:
774 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700775 */
776 return;
777 }
778
Eric Sandeenbba90742008-01-28 23:58:27 -0500779 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700780
781 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400782 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700783
784 while (l <= r) {
785 m = l + (r - l) / 2;
786 if (block < le32_to_cpu(m->ee_block))
787 r = m - 1;
788 else
789 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400790 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
791 m, le32_to_cpu(m->ee_block),
792 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700793 }
794
795 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400796 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400797 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400798 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400799 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400800 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700801
802#ifdef CHECK_BINSEARCH
803 {
804 struct ext4_extent *chex, *ex;
805 int k;
806
807 chex = ex = EXT_FIRST_EXTENT(eh);
808 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
809 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400810 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700811 if (block < le32_to_cpu(ex->ee_block))
812 break;
813 chex = ex;
814 }
815 BUG_ON(chex != path->p_ext);
816 }
817#endif
818
819}
820
821int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
822{
823 struct ext4_extent_header *eh;
824
825 eh = ext_inode_hdr(inode);
826 eh->eh_depth = 0;
827 eh->eh_entries = 0;
828 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400829 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700830 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 return 0;
832}
833
834struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400835ext4_find_extent(struct inode *inode, ext4_lblk_t block,
836 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700837{
838 struct ext4_extent_header *eh;
839 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400840 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
841 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500842 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700843
844 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400845 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400846 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
847 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
848 depth);
849 ret = -EFSCORRUPTED;
850 goto err;
851 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700852
Theodore Ts'o10809df82014-09-01 14:40:09 -0400853 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400854 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400855 if (depth > path[0].p_maxdepth) {
856 kfree(path);
857 *orig_path = path = NULL;
858 }
859 }
860 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400861 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700862 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Alex Tomasa86c6182006-10-11 01:21:03 -0700863 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400864 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400866 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700867 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700868 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400869 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700870
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400871 i = depth;
Dmitry Monakhov40686642019-11-06 12:25:02 +0000872 if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
873 ext4_cache_extents(inode, eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700874 /* walk through the tree */
875 while (i) {
876 ext_debug("depth %d: num %d, max %d\n",
877 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400878
Alex Tomasa86c6182006-10-11 01:21:03 -0700879 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400880 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 path[ppos].p_depth = i;
882 path[ppos].p_ext = NULL;
883
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400884 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
885 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530886 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400887 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500889 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400890
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 eh = ext_block_hdr(bh);
892 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700893 path[ppos].p_bh = bh;
894 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 }
896
897 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 path[ppos].p_ext = NULL;
899 path[ppos].p_idx = NULL;
900
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 /* find extent */
902 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400903 /* if not an empty leaf */
904 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400905 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700906
907 ext4_ext_show_path(inode, path);
908
909 return path;
910
911err:
912 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400913 kfree(path);
914 if (orig_path)
915 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500916 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700917}
918
919/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700920 * ext4_ext_insert_index:
921 * insert new index [@logical;@ptr] into the block at @curp;
922 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700923 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400924static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
925 struct ext4_ext_path *curp,
926 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700927{
928 struct ext4_extent_idx *ix;
929 int len, err;
930
Avantika Mathur7e028972006-12-06 20:41:33 -0800931 err = ext4_ext_get_access(handle, inode, curp);
932 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700933 return err;
934
Frank Mayhar273df552010-03-02 11:46:09 -0500935 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
936 EXT4_ERROR_INODE(inode,
937 "logical %d == ei_block %d!",
938 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400939 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500940 }
Robin Dongd4620312011-07-17 23:43:42 -0400941
942 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
943 >= le16_to_cpu(curp->p_hdr->eh_max))) {
944 EXT4_ERROR_INODE(inode,
945 "eh_entries %d >= eh_max %d!",
946 le16_to_cpu(curp->p_hdr->eh_entries),
947 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400948 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400949 }
950
Alex Tomasa86c6182006-10-11 01:21:03 -0700951 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
952 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400953 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 ix = curp->p_idx + 1;
955 } else {
956 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400957 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700958 ix = curp->p_idx;
959 }
960
Eric Gouriou80e675f2011-10-27 11:52:18 -0400961 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
962 BUG_ON(len < 0);
963 if (len > 0) {
964 ext_debug("insert new index %d: "
965 "move %d indices from 0x%p to 0x%p\n",
966 logical, len, ix, ix + 1);
967 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
968 }
969
Tao Maf472e022011-10-17 10:13:46 -0400970 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
971 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400972 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -0400973 }
974
Alex Tomasa86c6182006-10-11 01:21:03 -0700975 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700976 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400977 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700978
Frank Mayhar273df552010-03-02 11:46:09 -0500979 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
980 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400981 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500982 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700983
984 err = ext4_ext_dirty(handle, inode, curp);
985 ext4_std_error(inode->i_sb, err);
986
987 return err;
988}
989
990/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700991 * ext4_ext_split:
992 * inserts new subtree into the path, using free index entry
993 * at depth @at:
994 * - allocates all needed blocks (new leaf and all intermediate index blocks)
995 * - makes decision where to split
996 * - moves remaining extents and index entries (right to the split point)
997 * into the newly allocated blocks
998 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700999 */
1000static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001001 unsigned int flags,
1002 struct ext4_ext_path *path,
1003 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001004{
1005 struct buffer_head *bh = NULL;
1006 int depth = ext_depth(inode);
1007 struct ext4_extent_header *neh;
1008 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001009 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001010 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001011 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001012 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001013 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001014 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001015
1016 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001017 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001018
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001019 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001020 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001021 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1022 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001023 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001024 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001025 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1026 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001027 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001028 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001029 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001030 } else {
1031 border = newext->ee_block;
1032 ext_debug("leaf will be added."
1033 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001034 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 }
1036
1037 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001038 * If error occurs, then we break processing
1039 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001041 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 */
1043
1044 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001045 * Get array to track all allocated blocks.
1046 * We need this to handle errors and free blocks
1047 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 */
Kees Cook6396bb22018-06-12 14:03:40 -07001049 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 if (!ablocks)
1051 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001052
1053 /* allocate all needed blocks */
1054 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1055 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001056 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001057 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001058 if (newblock == 0)
1059 goto cleanup;
1060 ablocks[a] = newblock;
1061 }
1062
1063 /* initialize new leaf */
1064 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001065 if (unlikely(newblock == 0)) {
1066 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001067 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001068 goto cleanup;
1069 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001070 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001071 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001072 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001073 goto cleanup;
1074 }
1075 lock_buffer(bh);
1076
Avantika Mathur7e028972006-12-06 20:41:33 -08001077 err = ext4_journal_get_create_access(handle, bh);
1078 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001079 goto cleanup;
1080
1081 neh = ext_block_hdr(bh);
1082 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001083 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001084 neh->eh_magic = EXT4_EXT_MAGIC;
1085 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001086
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001087 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001088 if (unlikely(path[depth].p_hdr->eh_entries !=
1089 path[depth].p_hdr->eh_max)) {
1090 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1091 path[depth].p_hdr->eh_entries,
1092 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001093 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001094 goto cleanup;
1095 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001096 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001097 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1098 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001099 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001100 struct ext4_extent *ex;
1101 ex = EXT_FIRST_EXTENT(neh);
1102 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001103 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001104 }
1105
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001106 /* zero out unused area in the extent block */
1107 ext_size = sizeof(struct ext4_extent_header) +
1108 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1109 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001110 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001111 set_buffer_uptodate(bh);
1112 unlock_buffer(bh);
1113
Frank Mayhar03901312009-01-07 00:06:22 -05001114 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001115 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001116 goto cleanup;
1117 brelse(bh);
1118 bh = NULL;
1119
1120 /* correct old leaf */
1121 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001122 err = ext4_ext_get_access(handle, inode, path + depth);
1123 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001125 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001126 err = ext4_ext_dirty(handle, inode, path + depth);
1127 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001128 goto cleanup;
1129
1130 }
1131
1132 /* create intermediate indexes */
1133 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001134 if (unlikely(k < 0)) {
1135 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001136 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001137 goto cleanup;
1138 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001139 if (k)
1140 ext_debug("create %d intermediate indices\n", k);
1141 /* insert new index into current index block */
1142 /* current depth stored in i var */
1143 i = depth - 1;
1144 while (k--) {
1145 oldblock = newblock;
1146 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001147 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001148 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001149 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001150 goto cleanup;
1151 }
1152 lock_buffer(bh);
1153
Avantika Mathur7e028972006-12-06 20:41:33 -08001154 err = ext4_journal_get_create_access(handle, bh);
1155 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001156 goto cleanup;
1157
1158 neh = ext_block_hdr(bh);
1159 neh->eh_entries = cpu_to_le16(1);
1160 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001161 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001162 neh->eh_depth = cpu_to_le16(depth - i);
1163 fidx = EXT_FIRST_INDEX(neh);
1164 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001165 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001166
Eric Sandeenbba90742008-01-28 23:58:27 -05001167 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1168 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001169
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001170 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001171 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1172 EXT_LAST_INDEX(path[i].p_hdr))) {
1173 EXT4_ERROR_INODE(inode,
1174 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1175 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001176 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001177 goto cleanup;
1178 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001179 /* start copy indexes */
1180 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1181 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1182 EXT_MAX_INDEX(path[i].p_hdr));
1183 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001184 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001185 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001186 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001187 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001188 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001189 /* zero out unused area in the extent block */
1190 ext_size = sizeof(struct ext4_extent_header) +
1191 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1192 memset(bh->b_data + ext_size, 0,
1193 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001194 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001195 set_buffer_uptodate(bh);
1196 unlock_buffer(bh);
1197
Frank Mayhar03901312009-01-07 00:06:22 -05001198 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001199 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001200 goto cleanup;
1201 brelse(bh);
1202 bh = NULL;
1203
1204 /* correct old index */
1205 if (m) {
1206 err = ext4_ext_get_access(handle, inode, path + i);
1207 if (err)
1208 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001209 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001210 err = ext4_ext_dirty(handle, inode, path + i);
1211 if (err)
1212 goto cleanup;
1213 }
1214
1215 i--;
1216 }
1217
1218 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001219 err = ext4_ext_insert_index(handle, inode, path + at,
1220 le32_to_cpu(border), newblock);
1221
1222cleanup:
1223 if (bh) {
1224 if (buffer_locked(bh))
1225 unlock_buffer(bh);
1226 brelse(bh);
1227 }
1228
1229 if (err) {
1230 /* free all allocated blocks in error case */
1231 for (i = 0; i < depth; i++) {
1232 if (!ablocks[i])
1233 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001234 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001235 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001236 }
1237 }
1238 kfree(ablocks);
1239
1240 return err;
1241}
1242
1243/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001244 * ext4_ext_grow_indepth:
1245 * implements tree growing procedure:
1246 * - allocates new block
1247 * - moves top-level data (index block or leaf) into the new block
1248 * - initializes new top-level, creating index that points to the
1249 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001250 */
1251static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001252 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001253{
Alex Tomasa86c6182006-10-11 01:21:03 -07001254 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001255 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001256 ext4_fsblk_t newblock, goal = 0;
1257 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001258 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001259 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001260
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001261 /* Try to prepend new index to old one */
1262 if (ext_depth(inode))
1263 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1264 if (goal > le32_to_cpu(es->s_first_data_block)) {
1265 flags |= EXT4_MB_HINT_TRY_GOAL;
1266 goal--;
1267 } else
1268 goal = ext4_inode_to_goal_block(inode);
1269 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1270 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001271 if (newblock == 0)
1272 return err;
1273
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001274 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001275 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001276 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001277 lock_buffer(bh);
1278
Avantika Mathur7e028972006-12-06 20:41:33 -08001279 err = ext4_journal_get_create_access(handle, bh);
1280 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001281 unlock_buffer(bh);
1282 goto out;
1283 }
1284
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001285 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001286 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001287 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1288 /* zero out unused area in the extent block */
1289 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001290
1291 /* set size of new block */
1292 neh = ext_block_hdr(bh);
1293 /* old root could have indexes or leaves
1294 * so calculate e_max right way */
1295 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001296 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001297 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001298 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001299 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001300 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001301 set_buffer_uptodate(bh);
1302 unlock_buffer(bh);
1303
Frank Mayhar03901312009-01-07 00:06:22 -05001304 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001305 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001306 goto out;
1307
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001308 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001309 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001310 neh->eh_entries = cpu_to_le16(1);
1311 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1312 if (neh->eh_depth == 0) {
1313 /* Root extent block becomes index block */
1314 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1315 EXT_FIRST_INDEX(neh)->ei_block =
1316 EXT_FIRST_EXTENT(neh)->ee_block;
1317 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001318 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001319 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001320 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001321 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001322
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001323 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001324 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001325out:
1326 brelse(bh);
1327
1328 return err;
1329}
1330
1331/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001332 * ext4_ext_create_new_leaf:
1333 * finds empty index and adds new leaf.
1334 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001335 */
1336static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001337 unsigned int mb_flags,
1338 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001339 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001340 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001341{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001342 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001343 struct ext4_ext_path *curp;
1344 int depth, i, err = 0;
1345
1346repeat:
1347 i = depth = ext_depth(inode);
1348
1349 /* walk up to the tree and look for free index entry */
1350 curp = path + depth;
1351 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1352 i--;
1353 curp--;
1354 }
1355
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001356 /* we use already allocated block for index block,
1357 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001358 if (EXT_HAS_FREE_INDEX(curp)) {
1359 /* if we found index with free entry, then use that
1360 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001361 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001362 if (err)
1363 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001364
1365 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001366 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001367 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001368 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001369 if (IS_ERR(path))
1370 err = PTR_ERR(path);
1371 } else {
1372 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001373 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001374 if (err)
1375 goto out;
1376
1377 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001378 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001379 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001380 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001381 if (IS_ERR(path)) {
1382 err = PTR_ERR(path);
1383 goto out;
1384 }
1385
1386 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001387 * only first (depth 0 -> 1) produces free space;
1388 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001389 */
1390 depth = ext_depth(inode);
1391 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001392 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001393 goto repeat;
1394 }
1395 }
1396
1397out:
1398 return err;
1399}
1400
1401/*
Alex Tomas1988b512008-01-28 23:58:27 -05001402 * search the closest allocated block to the left for *logical
1403 * and returns it at @logical + it's physical address at @phys
1404 * if *logical is the smallest allocated block, the function
1405 * returns 0 at @phys
1406 * return value contains 0 (success) or error code
1407 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001408static int ext4_ext_search_left(struct inode *inode,
1409 struct ext4_ext_path *path,
1410 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001411{
1412 struct ext4_extent_idx *ix;
1413 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001414 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001415
Frank Mayhar273df552010-03-02 11:46:09 -05001416 if (unlikely(path == NULL)) {
1417 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001418 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001419 }
Alex Tomas1988b512008-01-28 23:58:27 -05001420 depth = path->p_depth;
1421 *phys = 0;
1422
1423 if (depth == 0 && path->p_ext == NULL)
1424 return 0;
1425
1426 /* usually extent in the path covers blocks smaller
1427 * then *logical, but it can be that extent is the
1428 * first one in the file */
1429
1430 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001431 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001432 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001433 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1434 EXT4_ERROR_INODE(inode,
1435 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1436 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001437 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001438 }
Alex Tomas1988b512008-01-28 23:58:27 -05001439 while (--depth >= 0) {
1440 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001441 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1442 EXT4_ERROR_INODE(inode,
1443 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001444 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001445 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001446 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001447 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001448 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001449 }
Alex Tomas1988b512008-01-28 23:58:27 -05001450 }
1451 return 0;
1452 }
1453
Frank Mayhar273df552010-03-02 11:46:09 -05001454 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1455 EXT4_ERROR_INODE(inode,
1456 "logical %d < ee_block %d + ee_len %d!",
1457 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001458 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001459 }
Alex Tomas1988b512008-01-28 23:58:27 -05001460
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001461 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001462 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001463 return 0;
1464}
1465
1466/*
1467 * search the closest allocated block to the right for *logical
1468 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001469 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001470 * returns 0 at @phys
1471 * return value contains 0 (success) or error code
1472 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001473static int ext4_ext_search_right(struct inode *inode,
1474 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001475 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1476 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001477{
1478 struct buffer_head *bh = NULL;
1479 struct ext4_extent_header *eh;
1480 struct ext4_extent_idx *ix;
1481 struct ext4_extent *ex;
1482 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001483 int depth; /* Note, NOT eh_depth; depth from top of tree */
1484 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001485
Frank Mayhar273df552010-03-02 11:46:09 -05001486 if (unlikely(path == NULL)) {
1487 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001488 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001489 }
Alex Tomas1988b512008-01-28 23:58:27 -05001490 depth = path->p_depth;
1491 *phys = 0;
1492
1493 if (depth == 0 && path->p_ext == NULL)
1494 return 0;
1495
1496 /* usually extent in the path covers blocks smaller
1497 * then *logical, but it can be that extent is the
1498 * first one in the file */
1499
1500 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001501 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001502 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001503 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1504 EXT4_ERROR_INODE(inode,
1505 "first_extent(path[%d].p_hdr) != ex",
1506 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001507 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001508 }
Alex Tomas1988b512008-01-28 23:58:27 -05001509 while (--depth >= 0) {
1510 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001511 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1512 EXT4_ERROR_INODE(inode,
1513 "ix != EXT_FIRST_INDEX *logical %d!",
1514 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001515 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001516 }
Alex Tomas1988b512008-01-28 23:58:27 -05001517 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001518 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001519 }
1520
Frank Mayhar273df552010-03-02 11:46:09 -05001521 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1522 EXT4_ERROR_INODE(inode,
1523 "logical %d < ee_block %d + ee_len %d!",
1524 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001525 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001526 }
Alex Tomas1988b512008-01-28 23:58:27 -05001527
1528 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1529 /* next allocated block in this leaf */
1530 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001531 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001532 }
1533
1534 /* go up and search for index to the right */
1535 while (--depth >= 0) {
1536 ix = path[depth].p_idx;
1537 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001538 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001539 }
1540
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001541 /* we've gone up to the root and found no index to the right */
1542 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001543
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001544got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001545 /* we've found index to the right, let's
1546 * follow it and find the closest allocated
1547 * block to the right */
1548 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001549 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001550 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001551 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001552 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001553 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001554 if (IS_ERR(bh))
1555 return PTR_ERR(bh);
1556 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001557 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001558 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001559 put_bh(bh);
1560 }
1561
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001562 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001563 if (IS_ERR(bh))
1564 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001565 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001566 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001567found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001568 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001569 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001570 *ret_ex = ex;
1571 if (bh)
1572 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001573 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001574}
1575
1576/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001577 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001578 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001579 * NOTE: it considers block number from index entry as
1580 * allocated block. Thus, index entries have to be consistent
1581 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001582 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001583ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001584ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1585{
1586 int depth;
1587
1588 BUG_ON(path == NULL);
1589 depth = path->p_depth;
1590
1591 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001592 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001593
1594 while (depth >= 0) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001595 struct ext4_ext_path *p = &path[depth];
1596
Alex Tomasa86c6182006-10-11 01:21:03 -07001597 if (depth == path->p_depth) {
1598 /* leaf */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001599 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
1600 return le32_to_cpu(p->p_ext[1].ee_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001601 } else {
1602 /* index */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001603 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
1604 return le32_to_cpu(p->p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001605 }
1606 depth--;
1607 }
1608
Lukas Czernerf17722f2011-06-06 00:05:17 -04001609 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001610}
1611
1612/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001613 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001614 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001615 */
Robin Dong57187892011-07-23 21:49:07 -04001616static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001617{
1618 int depth;
1619
1620 BUG_ON(path == NULL);
1621 depth = path->p_depth;
1622
1623 /* zero-tree has no leaf blocks at all */
1624 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001625 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001626
1627 /* go to index block */
1628 depth--;
1629
1630 while (depth >= 0) {
1631 if (path[depth].p_idx !=
1632 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001633 return (ext4_lblk_t)
1634 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001635 depth--;
1636 }
1637
Lukas Czernerf17722f2011-06-06 00:05:17 -04001638 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001639}
1640
1641/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001642 * ext4_ext_correct_indexes:
1643 * if leaf gets modified and modified extent is first in the leaf,
1644 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001645 * TODO: do we need to correct tree in all cases?
1646 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001647static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001648 struct ext4_ext_path *path)
1649{
1650 struct ext4_extent_header *eh;
1651 int depth = ext_depth(inode);
1652 struct ext4_extent *ex;
1653 __le32 border;
1654 int k, err = 0;
1655
1656 eh = path[depth].p_hdr;
1657 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001658
1659 if (unlikely(ex == NULL || eh == NULL)) {
1660 EXT4_ERROR_INODE(inode,
1661 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001662 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001663 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001664
1665 if (depth == 0) {
1666 /* there is no tree at all */
1667 return 0;
1668 }
1669
1670 if (ex != EXT_FIRST_EXTENT(eh)) {
1671 /* we correct tree if first leaf got modified only */
1672 return 0;
1673 }
1674
1675 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001676 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001677 */
1678 k = depth - 1;
1679 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001680 err = ext4_ext_get_access(handle, inode, path + k);
1681 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 return err;
1683 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001684 err = ext4_ext_dirty(handle, inode, path + k);
1685 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001686 return err;
1687
1688 while (k--) {
1689 /* change all left-side indexes */
1690 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1691 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001692 err = ext4_ext_get_access(handle, inode, path + k);
1693 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 break;
1695 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001696 err = ext4_ext_dirty(handle, inode, path + k);
1697 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001698 break;
1699 }
1700
1701 return err;
1702}
1703
Eric Biggers43f81672019-12-31 12:04:40 -06001704static int ext4_can_extents_be_merged(struct inode *inode,
1705 struct ext4_extent *ex1,
1706 struct ext4_extent *ex2)
Alex Tomasa86c6182006-10-11 01:21:03 -07001707{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001708 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001709
Lukas Czerner556615d2014-04-20 23:45:47 -04001710 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001711 return 0;
1712
1713 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1714 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1715
1716 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001717 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001718 return 0;
1719
Eric Sandeenda0169b2013-11-04 09:58:26 -05001720 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001721 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001722
Lukas Czerner556615d2014-04-20 23:45:47 -04001723 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001724 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001725 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001726#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001727 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001728 return 0;
1729#endif
1730
Theodore Ts'obf89d162010-10-27 21:30:14 -04001731 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001732 return 1;
1733 return 0;
1734}
1735
1736/*
Amit Arora56055d32007-07-17 21:42:38 -04001737 * This function tries to merge the "ex" extent to the next extent in the tree.
1738 * It always tries to merge towards right. If you want to merge towards
1739 * left, pass "ex - 1" as argument instead of "ex".
1740 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1741 * 1 if they got merged.
1742 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001743static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001744 struct ext4_ext_path *path,
1745 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001746{
1747 struct ext4_extent_header *eh;
1748 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001749 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001750
1751 depth = ext_depth(inode);
1752 BUG_ON(path[depth].p_hdr == NULL);
1753 eh = path[depth].p_hdr;
1754
1755 while (ex < EXT_LAST_EXTENT(eh)) {
1756 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1757 break;
1758 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001759 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001760 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1761 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001762 if (unwritten)
1763 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001764
1765 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1766 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1767 * sizeof(struct ext4_extent);
1768 memmove(ex + 1, ex + 2, len);
1769 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001770 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001771 merge_done = 1;
1772 WARN_ON(eh->eh_entries == 0);
1773 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001774 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001775 }
1776
1777 return merge_done;
1778}
1779
1780/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001781 * This function does a very simple check to see if we can collapse
1782 * an extent tree with a single extent tree leaf block into the inode.
1783 */
1784static void ext4_ext_try_to_merge_up(handle_t *handle,
1785 struct inode *inode,
1786 struct ext4_ext_path *path)
1787{
1788 size_t s;
1789 unsigned max_root = ext4_ext_space_root(inode, 0);
1790 ext4_fsblk_t blk;
1791
1792 if ((path[0].p_depth != 1) ||
1793 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1794 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1795 return;
1796
1797 /*
1798 * We need to modify the block allocation bitmap and the block
1799 * group descriptor to release the extent tree block. If we
1800 * can't get the journal credits, give up.
1801 */
Jan Kara83448bd2019-11-05 17:44:29 +01001802 if (ext4_journal_extend(handle, 2,
1803 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001804 return;
1805
1806 /*
1807 * Copy the extent data up to the inode
1808 */
1809 blk = ext4_idx_pblock(path[0].p_idx);
1810 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1811 sizeof(struct ext4_extent_idx);
1812 s += sizeof(struct ext4_extent_header);
1813
Theodore Ts'o10809df82014-09-01 14:40:09 -04001814 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001815 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1816 path[0].p_depth = 0;
1817 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1818 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1819 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1820
1821 brelse(path[1].p_bh);
1822 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001823 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001824}
1825
1826/*
Eric Biggersadde81c2019-12-31 12:04:41 -06001827 * This function tries to merge the @ex extent to neighbours in the tree, then
1828 * tries to collapse the extent tree into the inode.
Yongqiang Yang197217a2011-05-03 11:45:29 -04001829 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001830static void ext4_ext_try_to_merge(handle_t *handle,
1831 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001832 struct ext4_ext_path *path,
Eric Biggersadde81c2019-12-31 12:04:41 -06001833 struct ext4_extent *ex)
1834{
Yongqiang Yang197217a2011-05-03 11:45:29 -04001835 struct ext4_extent_header *eh;
1836 unsigned int depth;
1837 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001838
1839 depth = ext_depth(inode);
1840 BUG_ON(path[depth].p_hdr == NULL);
1841 eh = path[depth].p_hdr;
1842
1843 if (ex > EXT_FIRST_EXTENT(eh))
1844 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1845
1846 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001847 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001848
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001849 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001850}
1851
1852/*
Amit Arora25d14f92007-05-24 13:04:13 -04001853 * check if a portion of the "newext" extent overlaps with an
1854 * existing extent.
1855 *
1856 * If there is an overlap discovered, it updates the length of the newext
1857 * such that there will be no overlap, and then returns 1.
1858 * If there is no overlap found, it returns 0.
1859 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001860static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1861 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001862 struct ext4_extent *newext,
1863 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001864{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001865 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001866 unsigned int depth, len1;
1867 unsigned int ret = 0;
1868
1869 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001870 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001871 depth = ext_depth(inode);
1872 if (!path[depth].p_ext)
1873 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001874 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001875
1876 /*
1877 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001878 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001879 */
1880 if (b2 < b1) {
1881 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001882 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001883 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001884 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001885 }
1886
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001887 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001888 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001889 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001890 newext->ee_len = cpu_to_le16(len1);
1891 ret = 1;
1892 }
1893
1894 /* check for overlap */
1895 if (b1 + len1 > b2) {
1896 newext->ee_len = cpu_to_le16(b2 - b1);
1897 ret = 1;
1898 }
1899out:
1900 return ret;
1901}
1902
1903/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001904 * ext4_ext_insert_extent:
1905 * tries to merge requsted extent into the existing extent or
1906 * inserts requested extent as new one into the tree,
1907 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001908 */
1909int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001910 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001911 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001912{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001913 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001914 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001915 struct ext4_extent *ex, *fex;
1916 struct ext4_extent *nearex; /* nearest extent */
1917 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001918 int depth, len, err;
1919 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001920 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001921
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001922 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1923 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001924 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1925 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001926 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001927 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001928 depth = ext_depth(inode);
1929 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001930 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001931 if (unlikely(path[depth].p_hdr == NULL)) {
1932 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001933 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001934 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001935
1936 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001937 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001938
1939 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001940 * Try to see whether we should rather test the extent on
1941 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001942 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001943 * left, or on the right from the searched position. This
1944 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001945 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001946 if (ex < EXT_LAST_EXTENT(eh) &&
1947 (le32_to_cpu(ex->ee_block) +
1948 ext4_ext_get_actual_len(ex) <
1949 le32_to_cpu(newext->ee_block))) {
1950 ex += 1;
1951 goto prepend;
1952 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1953 (le32_to_cpu(newext->ee_block) +
1954 ext4_ext_get_actual_len(newext) <
1955 le32_to_cpu(ex->ee_block)))
1956 ex -= 1;
1957
1958 /* Try to append newex to the ex */
1959 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1960 ext_debug("append [%d]%d block to %u:[%d]%d"
1961 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001962 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001963 ext4_ext_get_actual_len(newext),
1964 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001965 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001966 ext4_ext_get_actual_len(ex),
1967 ext4_ext_pblock(ex));
1968 err = ext4_ext_get_access(handle, inode,
1969 path + depth);
1970 if (err)
1971 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001972 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001973 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001974 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001975 if (unwritten)
1976 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001977 eh = path[depth].p_hdr;
1978 nearex = ex;
1979 goto merge;
1980 }
1981
1982prepend:
1983 /* Try to prepend newex to the ex */
1984 if (ext4_can_extents_be_merged(inode, newext, ex)) {
1985 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
1986 "(from %llu)\n",
1987 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001988 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001989 ext4_ext_get_actual_len(newext),
1990 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001991 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001992 ext4_ext_get_actual_len(ex),
1993 ext4_ext_pblock(ex));
1994 err = ext4_ext_get_access(handle, inode,
1995 path + depth);
1996 if (err)
1997 return err;
1998
Lukas Czerner556615d2014-04-20 23:45:47 -04001999 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002000 ex->ee_block = newext->ee_block;
2001 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2002 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2003 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002004 if (unwritten)
2005 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002006 eh = path[depth].p_hdr;
2007 nearex = ex;
2008 goto merge;
2009 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002010 }
2011
Alex Tomasa86c6182006-10-11 01:21:03 -07002012 depth = ext_depth(inode);
2013 eh = path[depth].p_hdr;
2014 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2015 goto has_space;
2016
2017 /* probably next leaf has space for us? */
2018 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002019 next = EXT_MAX_BLOCKS;
2020 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002021 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002022 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002023 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002025 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002026 if (IS_ERR(npath))
2027 return PTR_ERR(npath);
2028 BUG_ON(npath->p_depth != path->p_depth);
2029 eh = npath[depth].p_hdr;
2030 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002031 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002032 le16_to_cpu(eh->eh_entries));
2033 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002034 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002035 }
2036 ext_debug("next leaf has no free space(%d,%d)\n",
2037 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2038 }
2039
2040 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002041 * There is no free space in the found leaf.
2042 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002043 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002044 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002045 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002046 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002047 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 if (err)
2049 goto cleanup;
2050 depth = ext_depth(inode);
2051 eh = path[depth].p_hdr;
2052
2053has_space:
2054 nearex = path[depth].p_ext;
2055
Avantika Mathur7e028972006-12-06 20:41:33 -08002056 err = ext4_ext_get_access(handle, inode, path + depth);
2057 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002058 goto cleanup;
2059
2060 if (!nearex) {
2061 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002062 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002063 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002064 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002065 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002066 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002067 nearex = EXT_FIRST_EXTENT(eh);
2068 } else {
2069 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002070 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002071 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002072 ext_debug("insert %u:%llu:[%d]%d before: "
2073 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002074 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002075 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002076 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002077 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002078 nearex);
2079 nearex++;
2080 } else {
2081 /* Insert before */
2082 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002083 ext_debug("insert %u:%llu:[%d]%d after: "
2084 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002085 le32_to_cpu(newext->ee_block),
2086 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002087 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002088 ext4_ext_get_actual_len(newext),
2089 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002090 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002091 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2092 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002093 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002094 "move %d extents from 0x%p to 0x%p\n",
2095 le32_to_cpu(newext->ee_block),
2096 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002097 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002098 ext4_ext_get_actual_len(newext),
2099 len, nearex, nearex + 1);
2100 memmove(nearex + 1, nearex,
2101 len * sizeof(struct ext4_extent));
2102 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002103 }
2104
Marcin Slusarze8546d02008-04-17 10:38:59 -04002105 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002106 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002107 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002108 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002109 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002110
2111merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002112 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002113 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002114 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002115
Alex Tomasa86c6182006-10-11 01:21:03 -07002116
2117 /* time to correct all indexes above */
2118 err = ext4_ext_correct_indexes(handle, inode, path);
2119 if (err)
2120 goto cleanup;
2121
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002122 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002123
2124cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002125 ext4_ext_drop_refs(npath);
2126 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002127 return err;
2128}
2129
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002130static int ext4_fill_fiemap_extents(struct inode *inode,
2131 ext4_lblk_t block, ext4_lblk_t num,
2132 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002133{
2134 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002135 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002136 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002137 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002138 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002139 int exists, depth = 0, err = 0;
2140 unsigned int flags = 0;
2141 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002142
Lukas Czernerf17722f2011-06-06 00:05:17 -04002143 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002144 num = last - block;
2145 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002146 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002147
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002148 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002149 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002150 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002151 err = PTR_ERR(path);
2152 path = NULL;
2153 break;
2154 }
2155
2156 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002157 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002158 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002159 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002160 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002161 break;
2162 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002163 ex = path[depth].p_ext;
2164 next = ext4_ext_next_allocated_block(path);
2165
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002166 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002167 exists = 0;
2168 if (!ex) {
2169 /* there is no extent yet, so try to allocate
2170 * all requested space */
2171 start = block;
2172 end = block + num;
2173 } else if (le32_to_cpu(ex->ee_block) > block) {
2174 /* need to allocate space before found extent */
2175 start = block;
2176 end = le32_to_cpu(ex->ee_block);
2177 if (block + num < end)
2178 end = block + num;
2179 } else if (block >= le32_to_cpu(ex->ee_block)
2180 + ext4_ext_get_actual_len(ex)) {
2181 /* need to allocate space after found extent */
2182 start = block;
2183 end = block + num;
2184 if (end >= next)
2185 end = next;
2186 } else if (block >= le32_to_cpu(ex->ee_block)) {
2187 /*
2188 * some part of requested space is covered
2189 * by found extent
2190 */
2191 start = block;
2192 end = le32_to_cpu(ex->ee_block)
2193 + ext4_ext_get_actual_len(ex);
2194 if (block + num < end)
2195 end = block + num;
2196 exists = 1;
2197 } else {
2198 BUG();
2199 }
2200 BUG_ON(end <= start);
2201
2202 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002203 es.es_lblk = start;
2204 es.es_len = end - start;
2205 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002206 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002207 es.es_lblk = le32_to_cpu(ex->ee_block);
2208 es.es_len = ext4_ext_get_actual_len(ex);
2209 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002210 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002211 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002212 }
2213
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002214 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002215 * Find delayed extent and update es accordingly. We call
2216 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002217 * last existing extent or not.
2218 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002219 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002220 if (!exists && next_del) {
2221 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002222 flags |= (FIEMAP_EXTENT_DELALLOC |
2223 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002224 }
2225 up_read(&EXT4_I(inode)->i_data_sem);
2226
Zheng Liu69eb33d2013-02-18 00:31:07 -05002227 if (unlikely(es.es_len == 0)) {
2228 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002229 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002230 break;
2231 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002232
Zheng Liuf7fec032013-02-18 00:28:47 -05002233 /*
2234 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2235 * we need to check next == EXT_MAX_BLOCKS because it is
2236 * possible that an extent is with unwritten and delayed
2237 * status due to when an extent is delayed allocated and
2238 * is allocated by fallocate status tree will track both of
2239 * them in a extent.
2240 *
2241 * So we could return a unwritten and delayed extent, and
2242 * its block is equal to 'next'.
2243 */
2244 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002245 flags |= FIEMAP_EXTENT_LAST;
2246 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2247 next != EXT_MAX_BLOCKS)) {
2248 EXT4_ERROR_INODE(inode,
2249 "next extent == %u, next "
2250 "delalloc extent = %u",
2251 next, next_del);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002252 err = -EFSCORRUPTED;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002253 break;
2254 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002255 }
2256
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002257 if (exists) {
2258 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002259 (__u64)es.es_lblk << blksize_bits,
2260 (__u64)es.es_pblk << blksize_bits,
2261 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002262 flags);
2263 if (err < 0)
2264 break;
2265 if (err == 1) {
2266 err = 0;
2267 break;
2268 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002269 }
2270
Zheng Liu69eb33d2013-02-18 00:31:07 -05002271 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002272 }
2273
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002274 ext4_ext_drop_refs(path);
2275 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002276 return err;
2277}
2278
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002279static int ext4_fill_es_cache_info(struct inode *inode,
2280 ext4_lblk_t block, ext4_lblk_t num,
2281 struct fiemap_extent_info *fieinfo)
2282{
2283 ext4_lblk_t next, end = block + num - 1;
2284 struct extent_status es;
2285 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2286 unsigned int flags;
2287 int err;
2288
2289 while (block <= end) {
2290 next = 0;
2291 flags = 0;
2292 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2293 break;
2294 if (ext4_es_is_unwritten(&es))
2295 flags |= FIEMAP_EXTENT_UNWRITTEN;
2296 if (ext4_es_is_delayed(&es))
2297 flags |= (FIEMAP_EXTENT_DELALLOC |
2298 FIEMAP_EXTENT_UNKNOWN);
2299 if (ext4_es_is_hole(&es))
2300 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2301 if (next == 0)
2302 flags |= FIEMAP_EXTENT_LAST;
2303 if (flags & (FIEMAP_EXTENT_DELALLOC|
2304 EXT4_FIEMAP_EXTENT_HOLE))
2305 es.es_pblk = 0;
2306 else
2307 es.es_pblk = ext4_es_pblock(&es);
2308 err = fiemap_fill_next_extent(fieinfo,
2309 (__u64)es.es_lblk << blksize_bits,
2310 (__u64)es.es_pblk << blksize_bits,
2311 (__u64)es.es_len << blksize_bits,
2312 flags);
2313 if (next == 0)
2314 break;
2315 block = next;
2316 if (err < 0)
2317 return err;
2318 if (err == 1)
2319 return 0;
2320 }
2321 return 0;
2322}
2323
2324
Alex Tomasa86c6182006-10-11 01:21:03 -07002325/*
Jan Kara140a5252016-03-09 22:46:57 -05002326 * ext4_ext_determine_hole - determine hole around given block
2327 * @inode: inode we lookup in
2328 * @path: path in extent tree to @lblk
2329 * @lblk: pointer to logical block around which we want to determine hole
2330 *
2331 * Determine hole length (and start if easily possible) around given logical
2332 * block. We don't try too hard to find the beginning of the hole but @path
2333 * actually points to extent before @lblk, we provide it.
2334 *
2335 * The function returns the length of a hole starting at @lblk. We update @lblk
2336 * to the beginning of the hole if we managed to find it.
2337 */
2338static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2339 struct ext4_ext_path *path,
2340 ext4_lblk_t *lblk)
2341{
2342 int depth = ext_depth(inode);
2343 struct ext4_extent *ex;
2344 ext4_lblk_t len;
2345
2346 ex = path[depth].p_ext;
2347 if (ex == NULL) {
2348 /* there is no extent yet, so gap is [0;-] */
2349 *lblk = 0;
2350 len = EXT_MAX_BLOCKS;
2351 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2352 len = le32_to_cpu(ex->ee_block) - *lblk;
2353 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2354 + ext4_ext_get_actual_len(ex)) {
2355 ext4_lblk_t next;
2356
2357 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2358 next = ext4_ext_next_allocated_block(path);
2359 BUG_ON(next == *lblk);
2360 len = next - *lblk;
2361 } else {
2362 BUG();
2363 }
2364 return len;
2365}
2366
2367/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002368 * ext4_ext_put_gap_in_cache:
2369 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002370 * and cache this gap
2371 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002372static void
Jan Kara140a5252016-03-09 22:46:57 -05002373ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2374 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002375{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002376 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002377
Eric Whitneyad431022018-10-01 14:10:39 -04002378 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2379 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002380 if (es.es_len) {
2381 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002382 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002383 return;
Jan Kara140a5252016-03-09 22:46:57 -05002384 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002385 }
Jan Kara140a5252016-03-09 22:46:57 -05002386 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2387 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2388 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002389}
2390
2391/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002392 * ext4_ext_rm_idx:
2393 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002394 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002395static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002396 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002397{
Alex Tomasa86c6182006-10-11 01:21:03 -07002398 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002399 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002400
2401 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002402 depth--;
2403 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002404 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002405 if (unlikely(path->p_hdr->eh_entries == 0)) {
2406 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002407 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002408 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002409 err = ext4_ext_get_access(handle, inode, path);
2410 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002411 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002412
2413 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2414 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2415 len *= sizeof(struct ext4_extent_idx);
2416 memmove(path->p_idx, path->p_idx + 1, len);
2417 }
2418
Marcin Slusarze8546d02008-04-17 10:38:59 -04002419 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002420 err = ext4_ext_dirty(handle, inode, path);
2421 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002422 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002423 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002424 trace_ext4_ext_rm_idx(inode, leaf);
2425
Peter Huewe7dc57612011-02-21 21:01:42 -05002426 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002427 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002428
2429 while (--depth >= 0) {
2430 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2431 break;
2432 path--;
2433 err = ext4_ext_get_access(handle, inode, path);
2434 if (err)
2435 break;
2436 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2437 err = ext4_ext_dirty(handle, inode, path);
2438 if (err)
2439 break;
2440 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002441 return err;
2442}
2443
2444/*
Mingming Caoee12b632008-08-19 22:16:05 -04002445 * ext4_ext_calc_credits_for_single_extent:
2446 * This routine returns max. credits that needed to insert an extent
2447 * to the extent tree.
2448 * When pass the actual path, the caller should calculate credits
2449 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002450 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002451int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002452 struct ext4_ext_path *path)
2453{
Alex Tomasa86c6182006-10-11 01:21:03 -07002454 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002455 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002456 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002457
Alex Tomasa86c6182006-10-11 01:21:03 -07002458 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002459 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002460 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2461
2462 /*
2463 * There are some space in the leaf tree, no
2464 * need to account for leaf block credit
2465 *
2466 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002467 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002468 * accounted.
2469 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002470 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002471 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002472 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002473 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002474 }
2475
Mingming Cao525f4ed2008-08-19 22:15:58 -04002476 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002477}
Alex Tomasa86c6182006-10-11 01:21:03 -07002478
Mingming Caoee12b632008-08-19 22:16:05 -04002479/*
Jan Karafffb2732013-06-04 13:01:11 -04002480 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002481 *
Jan Karafffb2732013-06-04 13:01:11 -04002482 * If we add a single extent, then in the worse case, each tree level
2483 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002484 *
Jan Karafffb2732013-06-04 13:01:11 -04002485 * If more extents are inserted, they could cause the whole tree split more
2486 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002487 */
Jan Karafffb2732013-06-04 13:01:11 -04002488int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002489{
2490 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002491 int depth;
2492
2493 /* If we are converting the inline data, only one is needed here. */
2494 if (ext4_has_inline_data(inode))
2495 return 1;
2496
2497 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002498
Jan Karafffb2732013-06-04 13:01:11 -04002499 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002500 index = depth * 2;
2501 else
2502 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002503
Mingming Caoee12b632008-08-19 22:16:05 -04002504 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002505}
2506
Theodore Ts'o981250c2013-06-12 11:48:29 -04002507static inline int get_default_free_blocks_flags(struct inode *inode)
2508{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002509 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2510 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002511 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2512 else if (ext4_should_journal_data(inode))
2513 return EXT4_FREE_BLOCKS_FORGET;
2514 return 0;
2515}
2516
Eric Whitney9fe67142018-10-01 14:25:08 -04002517/*
2518 * ext4_rereserve_cluster - increment the reserved cluster count when
2519 * freeing a cluster with a pending reservation
2520 *
2521 * @inode - file containing the cluster
2522 * @lblk - logical block in cluster to be reserved
2523 *
2524 * Increments the reserved cluster count and adjusts quota in a bigalloc
2525 * file system when freeing a partial cluster containing at least one
2526 * delayed and unwritten block. A partial cluster meeting that
2527 * requirement will have a pending reservation. If so, the
2528 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2529 * defer reserved and allocated space accounting to a subsequent call
2530 * to this function.
2531 */
2532static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2533{
2534 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2535 struct ext4_inode_info *ei = EXT4_I(inode);
2536
2537 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2538
2539 spin_lock(&ei->i_block_reservation_lock);
2540 ei->i_reserved_data_blocks++;
2541 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2542 spin_unlock(&ei->i_block_reservation_lock);
2543
2544 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2545 ext4_remove_pending(inode, lblk);
2546}
2547
Alex Tomasa86c6182006-10-11 01:21:03 -07002548static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002549 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002550 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002551 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002552{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002553 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002554 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002555 ext4_fsblk_t last_pblk, pblk;
2556 ext4_lblk_t num;
2557 int flags;
2558
2559 /* only extent tail removal is allowed */
2560 if (from < le32_to_cpu(ex->ee_block) ||
2561 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2562 ext4_error(sbi->s_sb,
2563 "strange request: removal(2) %u-%u from %u:%u",
2564 from, to, le32_to_cpu(ex->ee_block), ee_len);
2565 return 0;
2566 }
2567
2568#ifdef EXTENTS_STATS
2569 spin_lock(&sbi->s_ext_stats_lock);
2570 sbi->s_ext_blocks += ee_len;
2571 sbi->s_ext_extents++;
2572 if (ee_len < sbi->s_ext_min)
2573 sbi->s_ext_min = ee_len;
2574 if (ee_len > sbi->s_ext_max)
2575 sbi->s_ext_max = ee_len;
2576 if (ext_depth(inode) > sbi->s_depth_max)
2577 sbi->s_depth_max = ext_depth(inode);
2578 spin_unlock(&sbi->s_ext_stats_lock);
2579#endif
2580
2581 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2582
2583 /*
2584 * if we have a partial cluster, and it's different from the
2585 * cluster of the last block in the extent, we free it
2586 */
2587 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2588
2589 if (partial->state != initial &&
2590 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2591 if (partial->state == tofree) {
2592 flags = get_default_free_blocks_flags(inode);
2593 if (ext4_is_pending(inode, partial->lblk))
2594 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2595 ext4_free_blocks(handle, inode, NULL,
2596 EXT4_C2B(sbi, partial->pclu),
2597 sbi->s_cluster_ratio, flags);
2598 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2599 ext4_rereserve_cluster(inode, partial->lblk);
2600 }
2601 partial->state = initial;
2602 }
2603
2604 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2605 pblk = ext4_ext_pblock(ex) + ee_len - num;
2606
2607 /*
2608 * We free the partial cluster at the end of the extent (if any),
2609 * unless the cluster is used by another extent (partial_cluster
2610 * state is nofree). If a partial cluster exists here, it must be
2611 * shared with the last block in the extent.
2612 */
2613 flags = get_default_free_blocks_flags(inode);
2614
2615 /* partial, left end cluster aligned, right end unaligned */
2616 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2617 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2618 (partial->state != nofree)) {
2619 if (ext4_is_pending(inode, to))
2620 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2621 ext4_free_blocks(handle, inode, NULL,
2622 EXT4_PBLK_CMASK(sbi, last_pblk),
2623 sbi->s_cluster_ratio, flags);
2624 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2625 ext4_rereserve_cluster(inode, to);
2626 partial->state = initial;
2627 flags = get_default_free_blocks_flags(inode);
2628 }
2629
2630 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002631
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002632 /*
2633 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002634 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002635 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002636 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002637 */
2638 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002639 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002640
Eric Whitney9fe67142018-10-01 14:25:08 -04002641 /* reset the partial cluster if we've freed past it */
2642 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2643 partial->state = initial;
2644
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002645 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002646 * If we've freed the entire extent but the beginning is not left
2647 * cluster aligned and is not marked as ineligible for freeing we
2648 * record the partial cluster at the beginning of the extent. It
2649 * wasn't freed by the preceding ext4_free_blocks() call, and we
2650 * need to look farther to the left to determine if it's to be freed
2651 * (not shared with another extent). Else, reset the partial
2652 * cluster - we're either done freeing or the beginning of the
2653 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002654 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002655 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2656 if (partial->state == initial) {
2657 partial->pclu = EXT4_B2C(sbi, pblk);
2658 partial->lblk = from;
2659 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002660 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002661 } else {
2662 partial->state = initial;
2663 }
2664
Alex Tomasa86c6182006-10-11 01:21:03 -07002665 return 0;
2666}
2667
Allison Hendersond583fb82011-05-25 07:41:43 -04002668/*
2669 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002670 * blocks appearing between "start" and "end". Both "start"
2671 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002672 *
2673 * @handle: The journal handle
2674 * @inode: The files inode
2675 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002676 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002677 * has been released from it. However, if this value is
2678 * negative, it's a cluster just to the right of the
2679 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002680 * @start: The first block to remove
2681 * @end: The last block to remove
2682 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002683static int
2684ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002685 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002686 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002687 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002688{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002689 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002690 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002691 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002692 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002693 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002694 unsigned num;
2695 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002696 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002697 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002698 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002699 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002700
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002701 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002702 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002703 if (!path[depth].p_hdr)
2704 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2705 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002706 if (unlikely(path[depth].p_hdr == NULL)) {
2707 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002708 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002709 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002710 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002711 ex = path[depth].p_ext;
2712 if (!ex)
2713 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002714
2715 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002716 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002717
Eric Whitney9fe67142018-10-01 14:25:08 -04002718 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002719
Alex Tomasa86c6182006-10-11 01:21:03 -07002720 while (ex >= EXT_FIRST_EXTENT(eh) &&
2721 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002722
Lukas Czerner556615d2014-04-20 23:45:47 -04002723 if (ext4_ext_is_unwritten(ex))
2724 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002725 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002726 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002727
Mingming553f9002009-09-18 13:34:55 -04002728 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002729 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002730 path[depth].p_ext = ex;
2731
2732 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002733 b = ex_ee_block+ex_ee_len - 1 < end ?
2734 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002735
2736 ext_debug(" border %u:%u\n", a, b);
2737
Allison Hendersond583fb82011-05-25 07:41:43 -04002738 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002739 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002740 /*
2741 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002742 * so note that its first cluster is in use to avoid
2743 * freeing it when removing blocks. Eventually, the
2744 * right edge of the truncated/punched region will
2745 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002746 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002747 if (sbi->s_cluster_ratio > 1) {
2748 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002749 partial->pclu = EXT4_B2C(sbi, pblk);
2750 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002751 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002752 ex--;
2753 ex_ee_block = le32_to_cpu(ex->ee_block);
2754 ex_ee_len = ext4_ext_get_actual_len(ex);
2755 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002756 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002757 EXT4_ERROR_INODE(inode,
2758 "can not handle truncate %u:%u "
2759 "on extent %u:%u",
2760 start, end, ex_ee_block,
2761 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002762 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002763 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002764 } else if (a != ex_ee_block) {
2765 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002766 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002767 } else {
2768 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002769 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002770 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002771 /*
2772 * 3 for leaf, sb, and inode plus 2 (bmap and group
2773 * descriptor) for each block group; assume two block
2774 * groups plus ex_ee_len/blocks_per_block_group for
2775 * the worst case
2776 */
2777 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002778 if (ex == EXT_FIRST_EXTENT(eh)) {
2779 correct_index = 1;
2780 credits += (ext_depth(inode)) + 1;
2781 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002782 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002783 /*
2784 * We may end up freeing some index blocks and data from the
2785 * punched range. Note that partial clusters are accounted for
2786 * by ext4_free_data_revoke_credits().
2787 */
2788 revoke_credits =
2789 ext4_free_metadata_revoke_credits(inode->i_sb,
2790 ext_depth(inode)) +
2791 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002792
Jan Karaa4130362019-11-05 17:44:16 +01002793 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002794 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002795 if (err) {
2796 if (err > 0)
2797 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002798 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002799 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002800
2801 err = ext4_ext_get_access(handle, inode, path + depth);
2802 if (err)
2803 goto out;
2804
Eric Whitney9fe67142018-10-01 14:25:08 -04002805 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002806 if (err)
2807 goto out;
2808
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002809 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002810 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002811 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002812
Alex Tomasa86c6182006-10-11 01:21:03 -07002813 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002814 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002815 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002816 * extent have been removed.
2817 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002818 if (unwritten && num)
2819 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002820 /*
2821 * If the extent was completely released,
2822 * we need to remove it from the leaf
2823 */
2824 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002825 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002826 /*
2827 * For hole punching, we need to scoot all the
2828 * extents up when an extent is removed so that
2829 * we dont have blank extents in the middle
2830 */
2831 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2832 sizeof(struct ext4_extent));
2833
2834 /* Now get rid of the one at the end */
2835 memset(EXT_LAST_EXTENT(eh), 0,
2836 sizeof(struct ext4_extent));
2837 }
2838 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002839 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002840
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002841 err = ext4_ext_dirty(handle, inode, path + depth);
2842 if (err)
2843 goto out;
2844
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002845 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002846 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002847 ex--;
2848 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002849 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002850 }
2851
2852 if (correct_index && eh->eh_entries)
2853 err = ext4_ext_correct_indexes(handle, inode, path);
2854
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002855 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002856 * If there's a partial cluster and at least one extent remains in
2857 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002858 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002859 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002860 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002861 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002862 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002863 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002864 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2865 int flags = get_default_free_blocks_flags(inode);
2866
2867 if (ext4_is_pending(inode, partial->lblk))
2868 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002869 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002870 EXT4_C2B(sbi, partial->pclu),
2871 sbi->s_cluster_ratio, flags);
2872 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2873 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002874 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002875 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002876 }
2877
Alex Tomasa86c6182006-10-11 01:21:03 -07002878 /* if this leaf is free, then we should
2879 * remove it from index block above */
2880 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002881 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002882
2883out:
2884 return err;
2885}
2886
2887/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002888 * ext4_ext_more_to_rm:
2889 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002890 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002891static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002892ext4_ext_more_to_rm(struct ext4_ext_path *path)
2893{
2894 BUG_ON(path->p_idx == NULL);
2895
2896 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2897 return 0;
2898
2899 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002900 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002901 * so we have to consider current index for truncation
2902 */
2903 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2904 return 0;
2905 return 1;
2906}
2907
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002908int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2909 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002910{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002911 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002912 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002913 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002914 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002915 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002916 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002917
Eric Whitney9fe67142018-10-01 14:25:08 -04002918 partial.pclu = 0;
2919 partial.lblk = 0;
2920 partial.state = initial;
2921
Lukas Czerner5f95d212012-03-19 23:03:19 -04002922 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002923
2924 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002925 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2926 depth + 1,
2927 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002928 if (IS_ERR(handle))
2929 return PTR_ERR(handle);
2930
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002931again:
Lukas Czerner61801322013-05-27 23:32:35 -04002932 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002933
Alex Tomasa86c6182006-10-11 01:21:03 -07002934 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002935 * Check if we are removing extents inside the extent tree. If that
2936 * is the case, we are going to punch a hole inside the extent tree
2937 * so we have to check whether we need to split the extent covering
2938 * the last block to remove so we can easily remove the part of it
2939 * in ext4_ext_rm_leaf().
2940 */
2941 if (end < EXT_MAX_BLOCKS - 1) {
2942 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002943 ext4_lblk_t ee_block, ex_end, lblk;
2944 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002945
Eric Whitneyf4226d92014-11-23 00:55:42 -05002946 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002947 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002948 if (IS_ERR(path)) {
2949 ext4_journal_stop(handle);
2950 return PTR_ERR(path);
2951 }
2952 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002953 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002954 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002955 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002956 if (depth) {
2957 EXT4_ERROR_INODE(inode,
2958 "path[%d].p_hdr == NULL",
2959 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002960 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002961 }
2962 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002963 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002964
2965 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002966 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002967
2968 /*
2969 * See if the last block is inside the extent, if so split
2970 * the extent at 'end' block so we can easily remove the
2971 * tail of the first part of the split extent in
2972 * ext4_ext_rm_leaf().
2973 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002974 if (end >= ee_block && end < ex_end) {
2975
2976 /*
2977 * If we're going to split the extent, note that
2978 * the cluster containing the block after 'end' is
2979 * in use to avoid freeing it when removing blocks.
2980 */
2981 if (sbi->s_cluster_ratio > 1) {
2982 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04002983 partial.pclu = EXT4_B2C(sbi, pblk);
2984 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002985 }
2986
Lukas Czerner5f95d212012-03-19 23:03:19 -04002987 /*
2988 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002989 * block in the first new extent. Also we should not
2990 * fail removing space due to ENOSPC so try to use
2991 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002992 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002993 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002994 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002995 if (err < 0)
2996 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002997
Eric Whitney7bd75232019-02-28 23:34:11 -05002998 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
2999 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05003000 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05003001 * If we're punching, there's an extent to the right.
3002 * If the partial cluster hasn't been set, set it to
3003 * that extent's first cluster and its state to nofree
3004 * so it won't be freed should it contain blocks to be
3005 * removed. If it's already set (tofree/nofree), we're
3006 * retrying and keep the original partial cluster info
3007 * so a cluster marked tofree as a result of earlier
3008 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05003009 */
3010 lblk = ex_end + 1;
3011 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
3012 &ex);
3013 if (err)
3014 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04003015 if (pblk) {
3016 partial.pclu = EXT4_B2C(sbi, pblk);
3017 partial.state = nofree;
3018 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003019 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003020 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003021 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003022 * We start scanning from right side, freeing all the blocks
3023 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07003024 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04003025 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04003026 if (path) {
3027 int k = i = depth;
3028 while (--k > 0)
3029 path[k].p_block =
3030 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
3031 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003032 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Ashish Sangwan968dee72012-07-22 22:49:08 -04003033 GFP_NOFS);
3034 if (path == NULL) {
3035 ext4_journal_stop(handle);
3036 return -ENOMEM;
3037 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04003038 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003039 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04003040 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003041
Theodore Ts'oc3491792013-08-16 21:21:41 -04003042 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003043 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003044 goto out;
3045 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003046 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04003047 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07003048
3049 while (i >= 0 && err == 0) {
3050 if (i == depth) {
3051 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04003052 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04003053 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003054 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003055 brelse(path[i].p_bh);
3056 path[i].p_bh = NULL;
3057 i--;
3058 continue;
3059 }
3060
3061 /* this is index block */
3062 if (!path[i].p_hdr) {
3063 ext_debug("initialize header\n");
3064 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003065 }
3066
Alex Tomasa86c6182006-10-11 01:21:03 -07003067 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003068 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07003069 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
3070 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
3071 ext_debug("init index ptr: hdr 0x%p, num %d\n",
3072 path[i].p_hdr,
3073 le16_to_cpu(path[i].p_hdr->eh_entries));
3074 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003075 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07003076 path[i].p_idx--;
3077 }
3078
3079 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3080 i, EXT_FIRST_INDEX(path[i].p_hdr),
3081 path[i].p_idx);
3082 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003083 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003084 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07003085 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04003086 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07003087 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003088 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04003089 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
3090 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003091 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003092 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003093 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003094 break;
3095 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04003096 /* Yield here to deal with large extent trees.
3097 * Should be a no-op if we did IO above. */
3098 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003099 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003100 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003101 break;
3102 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003103 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003104
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003105 /* save actual number of indexes since this
3106 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07003107 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3108 i++;
3109 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003110 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07003111 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003112 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07003113 * handle must be already prepared by the
3114 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05003115 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07003116 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003117 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003118 brelse(path[i].p_bh);
3119 path[i].p_bh = NULL;
3120 i--;
3121 ext_debug("return to level %d\n", i);
3122 }
3123 }
3124
Eric Whitney9fe67142018-10-01 14:25:08 -04003125 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
3126 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003127
Eric Whitney0756b902014-11-23 00:59:39 -05003128 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04003129 * if there's a partial cluster and we have removed the first extent
3130 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05003131 */
Eric Whitney9fe67142018-10-01 14:25:08 -04003132 if (partial.state == tofree && err == 0) {
3133 int flags = get_default_free_blocks_flags(inode);
3134
3135 if (ext4_is_pending(inode, partial.lblk))
3136 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003137 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003138 EXT4_C2B(sbi, partial.pclu),
3139 sbi->s_cluster_ratio, flags);
3140 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3141 ext4_rereserve_cluster(inode, partial.lblk);
3142 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003143 }
3144
Alex Tomasa86c6182006-10-11 01:21:03 -07003145 /* TODO: flexible tree reduction should be here */
3146 if (path->p_hdr->eh_entries == 0) {
3147 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003148 * truncate to zero freed all the tree,
3149 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003150 */
3151 err = ext4_ext_get_access(handle, inode, path);
3152 if (err == 0) {
3153 ext_inode_hdr(inode)->eh_depth = 0;
3154 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003155 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003156 err = ext4_ext_dirty(handle, inode, path);
3157 }
3158 }
3159out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003160 ext4_ext_drop_refs(path);
3161 kfree(path);
3162 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003163 if (err == -EAGAIN)
3164 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003165 ext4_journal_stop(handle);
3166
3167 return err;
3168}
3169
3170/*
3171 * called at mount time
3172 */
3173void ext4_ext_init(struct super_block *sb)
3174{
3175 /*
3176 * possible initialization would be here
3177 */
3178
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003179 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003180#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003181 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003182#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003183 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003184#endif
3185#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003186 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003187#endif
3188#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003189 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003190#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003191 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003192#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003193#ifdef EXTENTS_STATS
3194 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3195 EXT4_SB(sb)->s_ext_min = 1 << 30;
3196 EXT4_SB(sb)->s_ext_max = 0;
3197#endif
3198 }
3199}
3200
3201/*
3202 * called at umount time
3203 */
3204void ext4_ext_release(struct super_block *sb)
3205{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003206 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003207 return;
3208
3209#ifdef EXTENTS_STATS
3210 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3211 struct ext4_sb_info *sbi = EXT4_SB(sb);
3212 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3213 sbi->s_ext_blocks, sbi->s_ext_extents,
3214 sbi->s_ext_blocks / sbi->s_ext_extents);
3215 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3216 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3217 }
3218#endif
3219}
3220
Zheng Liud7b2a002013-08-28 14:47:06 -04003221static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3222{
3223 ext4_lblk_t ee_block;
3224 ext4_fsblk_t ee_pblock;
3225 unsigned int ee_len;
3226
3227 ee_block = le32_to_cpu(ex->ee_block);
3228 ee_len = ext4_ext_get_actual_len(ex);
3229 ee_pblock = ext4_ext_pblock(ex);
3230
3231 if (ee_len == 0)
3232 return 0;
3233
3234 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3235 EXTENT_STATUS_WRITTEN);
3236}
3237
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003238/* FIXME!! we need to try to merge to left or right after zero-out */
3239static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3240{
Lukas Czerner24075182010-10-27 21:30:06 -04003241 ext4_fsblk_t ee_pblock;
3242 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003243
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003244 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003245 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003246 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3247 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003248}
3249
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003250/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003251 * ext4_split_extent_at() splits an extent at given block.
3252 *
3253 * @handle: the journal handle
3254 * @inode: the file inode
3255 * @path: the path to the extent
3256 * @split: the logical block where the extent is splitted.
3257 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003258 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003259 * @flags: flags used to insert new extent to extent tree.
3260 *
3261 *
3262 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3263 * of which are deterimined by split_flag.
3264 *
3265 * There are two cases:
3266 * a> the extent are splitted into two extent.
3267 * b> split is not needed, and just mark the extent.
3268 *
3269 * return 0 on success.
3270 */
3271static int ext4_split_extent_at(handle_t *handle,
3272 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003273 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003274 ext4_lblk_t split,
3275 int split_flag,
3276 int flags)
3277{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003278 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003279 ext4_fsblk_t newblock;
3280 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003281 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003282 struct ext4_extent *ex2 = NULL;
3283 unsigned int ee_len, depth;
3284 int err = 0;
3285
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003286 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3287 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3288
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003289 ext_debug("ext4_split_extents_at: inode %lu, logical"
3290 "block %llu\n", inode->i_ino, (unsigned long long)split);
3291
3292 ext4_ext_show_leaf(inode, path);
3293
3294 depth = ext_depth(inode);
3295 ex = path[depth].p_ext;
3296 ee_block = le32_to_cpu(ex->ee_block);
3297 ee_len = ext4_ext_get_actual_len(ex);
3298 newblock = split - ee_block + ext4_ext_pblock(ex);
3299
3300 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003301 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003302 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003303 EXT4_EXT_MARK_UNWRIT1 |
3304 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003305
3306 err = ext4_ext_get_access(handle, inode, path + depth);
3307 if (err)
3308 goto out;
3309
3310 if (split == ee_block) {
3311 /*
3312 * case b: block @split is the block that the extent begins with
3313 * then we just change the state of the extent, and splitting
3314 * is not needed.
3315 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003316 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3317 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003318 else
3319 ext4_ext_mark_initialized(ex);
3320
3321 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003322 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003323
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003324 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003325 goto out;
3326 }
3327
3328 /* case a */
3329 memcpy(&orig_ex, ex, sizeof(orig_ex));
3330 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003331 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3332 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003333
3334 /*
3335 * path may lead to new leaf, not to original leaf any more
3336 * after ext4_ext_insert_extent() returns,
3337 */
3338 err = ext4_ext_dirty(handle, inode, path + depth);
3339 if (err)
3340 goto fix_extent_len;
3341
3342 ex2 = &newex;
3343 ex2->ee_block = cpu_to_le32(split);
3344 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3345 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003346 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3347 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003348
Theodore Ts'odfe50802014-09-01 14:37:09 -04003349 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003350 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003351 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003352 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003353 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003354 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003355 zero_ex.ee_len = cpu_to_le16(
3356 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003357 ext4_ext_store_pblock(&zero_ex,
3358 ext4_ext_pblock(ex2));
3359 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003360 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003361 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003362 zero_ex.ee_len = cpu_to_le16(
3363 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003364 ext4_ext_store_pblock(&zero_ex,
3365 ext4_ext_pblock(ex));
3366 }
3367 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003368 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003369 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003370 zero_ex.ee_len = cpu_to_le16(
3371 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003372 ext4_ext_store_pblock(&zero_ex,
3373 ext4_ext_pblock(&orig_ex));
3374 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003375
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003376 if (err)
3377 goto fix_extent_len;
3378 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003379 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003380 ext4_ext_try_to_merge(handle, inode, path, ex);
3381 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003382 if (err)
3383 goto fix_extent_len;
3384
3385 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003386 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003387
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003388 goto out;
3389 } else if (err)
3390 goto fix_extent_len;
3391
3392out:
3393 ext4_ext_show_leaf(inode, path);
3394 return err;
3395
3396fix_extent_len:
3397 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003398 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003399 return err;
3400}
3401
3402/*
3403 * ext4_split_extents() splits an extent and mark extent which is covered
3404 * by @map as split_flags indicates
3405 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003406 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003407 * There are three possibilities:
3408 * a> There is no split required
3409 * b> Splits in two extents: Split is happening at either end of the extent
3410 * c> Splits in three extents: Somone is splitting in middle of the extent
3411 *
3412 */
3413static int ext4_split_extent(handle_t *handle,
3414 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003415 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003416 struct ext4_map_blocks *map,
3417 int split_flag,
3418 int flags)
3419{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003420 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003421 ext4_lblk_t ee_block;
3422 struct ext4_extent *ex;
3423 unsigned int ee_len, depth;
3424 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003425 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003426 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003427 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003428
3429 depth = ext_depth(inode);
3430 ex = path[depth].p_ext;
3431 ee_block = le32_to_cpu(ex->ee_block);
3432 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003433 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003434
3435 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003436 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003437 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003438 if (unwritten)
3439 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3440 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003441 if (split_flag & EXT4_EXT_DATA_VALID2)
3442 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003443 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003444 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003445 if (err)
3446 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003447 } else {
3448 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003449 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003450 /*
3451 * Update path is required because previous ext4_split_extent_at() may
3452 * result in split of original leaf or extent zeroout.
3453 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003454 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003455 if (IS_ERR(path))
3456 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003457 depth = ext_depth(inode);
3458 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003459 if (!ex) {
3460 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3461 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003462 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003463 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003464 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003465 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003466
3467 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003468 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003469 if (unwritten) {
3470 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003471 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003472 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003473 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003474 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003475 map->m_lblk, split_flag1, flags);
3476 if (err)
3477 goto out;
3478 }
3479
3480 ext4_ext_show_leaf(inode, path);
3481out:
Zheng Liu3a225672013-03-10 21:20:23 -04003482 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003483}
3484
Amit Arora56055d32007-07-17 21:42:38 -04003485/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003486 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003487 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003488 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003489 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003490 * There are three possibilities:
3491 * a> There is no split required: Entire extent should be initialized
3492 * b> Splits in two extents: Write is happening at either end of the extent
3493 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003494 *
3495 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003496 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003497 * - The extent pointed to by 'path' contains a superset
3498 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3499 *
3500 * Post-conditions on success:
3501 * - the returned value is the number of blocks beyond map->l_lblk
3502 * that are allocated and initialized.
3503 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003504 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003505static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003506 struct inode *inode,
3507 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003508 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003509 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003510{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003511 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003512 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003513 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003514 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003515 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003516 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003517 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003518 unsigned int ee_len, depth, map_len = map->m_len;
3519 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003520 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003521 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003522
3523 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3524 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003525 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003526
Zheng Liu67a5da52012-08-17 09:54:17 -04003527 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003528 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3529 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003530 if (eof_block < map->m_lblk + map_len)
3531 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003532
3533 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003534 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003535 ex = path[depth].p_ext;
3536 ee_block = le32_to_cpu(ex->ee_block);
3537 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003538 zero_ex1.ee_len = 0;
3539 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003540
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003541 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3542
3543 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003544 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003545 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003546
3547 /*
3548 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003549 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003550 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003551 * memmove() calls. Transferring to the left is the common case in
3552 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3553 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003554 *
3555 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003556 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003557 * This would require removing the extent if the transfer
3558 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003559 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003560 * same extent tree node.
3561 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003562 if ((map->m_lblk == ee_block) &&
3563 /* See if we can merge left */
3564 (map_len < ee_len) && /*L1*/
3565 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003566 ext4_lblk_t prev_lblk;
3567 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003568 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003569
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003570 abut_ex = ex - 1;
3571 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3572 prev_len = ext4_ext_get_actual_len(abut_ex);
3573 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003574 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003575
3576 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003577 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003578 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003579 * - C1: abut_ex is initialized,
3580 * - C2: abut_ex is logically abutting ex,
3581 * - C3: abut_ex is physically abutting ex,
3582 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003583 * overflowing the (initialized) length limit.
3584 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003585 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003586 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3587 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003588 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003589 err = ext4_ext_get_access(handle, inode, path + depth);
3590 if (err)
3591 goto out;
3592
3593 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003594 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003595
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003596 /* Shift the start of ex by 'map_len' blocks */
3597 ex->ee_block = cpu_to_le32(ee_block + map_len);
3598 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3599 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003600 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003601
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003602 /* Extend abut_ex by 'map_len' blocks */
3603 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003604
3605 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003606 allocated = map_len;
3607 }
3608 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3609 (map_len < ee_len) && /*L1*/
3610 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3611 /* See if we can merge right */
3612 ext4_lblk_t next_lblk;
3613 ext4_fsblk_t next_pblk, ee_pblk;
3614 unsigned int next_len;
3615
3616 abut_ex = ex + 1;
3617 next_lblk = le32_to_cpu(abut_ex->ee_block);
3618 next_len = ext4_ext_get_actual_len(abut_ex);
3619 next_pblk = ext4_ext_pblock(abut_ex);
3620 ee_pblk = ext4_ext_pblock(ex);
3621
3622 /*
3623 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3624 * upon those conditions:
3625 * - C1: abut_ex is initialized,
3626 * - C2: abut_ex is logically abutting ex,
3627 * - C3: abut_ex is physically abutting ex,
3628 * - C4: abut_ex can receive the additional blocks without
3629 * overflowing the (initialized) length limit.
3630 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003631 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003632 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3633 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3634 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3635 err = ext4_ext_get_access(handle, inode, path + depth);
3636 if (err)
3637 goto out;
3638
3639 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3640 map, ex, abut_ex);
3641
3642 /* Shift the start of abut_ex by 'map_len' blocks */
3643 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3644 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3645 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003646 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003647
3648 /* Extend abut_ex by 'map_len' blocks */
3649 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3650
3651 /* Result: number of initialized blocks past m_lblk */
3652 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003653 }
3654 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003655 if (allocated) {
3656 /* Mark the block containing both extents as dirty */
3657 ext4_ext_dirty(handle, inode, path + depth);
3658
3659 /* Update path to point to the right extent */
3660 path[depth].p_ext = abut_ex;
3661 goto out;
3662 } else
3663 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003664
Yongqiang Yang667eff32011-05-03 12:25:07 -04003665 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003666 /*
3667 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003668 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003669 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003670 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003671
Zheng Liu67a5da52012-08-17 09:54:17 -04003672 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3673 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003674 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003675
Amit Arora56055d32007-07-17 21:42:38 -04003676 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003677 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003678 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003679 * 2. split the extent into two extents, zeroout the head of the first
3680 * extent.
3681 * 3. split the extent into two extents, zeroout the tail of the second
3682 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003683 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003684 * 5. no splitting needed, just possibly zeroout the head and / or the
3685 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003686 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003687 split_map.m_lblk = map->m_lblk;
3688 split_map.m_len = map->m_len;
3689
Jan Kara4f8caa62017-05-26 17:40:52 -04003690 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003691 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003692 /* case 3 or 5 */
3693 zero_ex1.ee_block =
3694 cpu_to_le32(split_map.m_lblk +
3695 split_map.m_len);
3696 zero_ex1.ee_len =
3697 cpu_to_le16(allocated - split_map.m_len);
3698 ext4_ext_store_pblock(&zero_ex1,
3699 ext4_ext_pblock(ex) + split_map.m_lblk +
3700 split_map.m_len - ee_block);
3701 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003702 if (err)
3703 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003704 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003705 }
3706 if (split_map.m_lblk - ee_block + split_map.m_len <
3707 max_zeroout) {
3708 /* case 2 or 5 */
3709 if (split_map.m_lblk != ee_block) {
3710 zero_ex2.ee_block = ex->ee_block;
3711 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003712 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003713 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003714 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003715 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003716 if (err)
3717 goto out;
3718 }
3719
Jan Kara4f8caa62017-05-26 17:40:52 -04003720 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003721 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003722 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003723 }
3724 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003725
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003726 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3727 flags);
3728 if (err > 0)
3729 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003730out:
Zheng Liuadb23552013-03-10 21:13:05 -04003731 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003732 if (!err) {
3733 err = ext4_zeroout_es(inode, &zero_ex1);
3734 if (!err)
3735 err = ext4_zeroout_es(inode, &zero_ex2);
3736 }
Amit Arora56055d32007-07-17 21:42:38 -04003737 return err ? err : allocated;
3738}
3739
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003740/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003741 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003742 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003743 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003744 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003745 * Writing to an unwritten extent may result in splitting the unwritten
3746 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003747 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003748 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003749 * b> Splits in two extents: Write is happening at either end of the extent
3750 * c> Splits in three extents: Somone is writing in middle of the extent
3751 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003752 * This works the same way in the case of initialized -> unwritten conversion.
3753 *
Mingming Cao00314622009-09-28 15:49:08 -04003754 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003755 * the unwritten extent split. To prevent ENOSPC occur at the IO
3756 * complete, we need to split the unwritten extent before DIO submit
3757 * the IO. The unwritten extent called at this time will be split
3758 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003759 * being filled will be convert to initialized by the end_io callback function
3760 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003761 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003762 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003763 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003764static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003765 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003766 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003767 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003768 int flags)
3769{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003770 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003771 ext4_lblk_t eof_block;
3772 ext4_lblk_t ee_block;
3773 struct ext4_extent *ex;
3774 unsigned int ee_len;
3775 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003776
Lukas Czernerb8a86842014-03-18 18:05:35 -04003777 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3778 __func__, inode->i_ino,
3779 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003780
3781 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3782 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003783 if (eof_block < map->m_lblk + map->m_len)
3784 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003785 /*
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003786 * It is safe to convert extent to initialized via explicit
3787 * zeroout only if extent is fully insde i_size or new_size.
3788 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003789 depth = ext_depth(inode);
3790 ex = path[depth].p_ext;
3791 ee_block = le32_to_cpu(ex->ee_block);
3792 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003793
Lukas Czernerb8a86842014-03-18 18:05:35 -04003794 /* Convert to unwritten */
3795 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3796 split_flag |= EXT4_EXT_DATA_VALID1;
3797 /* Convert to initialized */
3798 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3799 split_flag |= ee_block + ee_len <= eof_block ?
3800 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003801 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003802 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003803 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003804 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003805}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003806
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003807static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003808 struct inode *inode,
3809 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003810 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003811{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003812 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003813 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003814 ext4_lblk_t ee_block;
3815 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003816 int depth;
3817 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003818
3819 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003820 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003821 ee_block = le32_to_cpu(ex->ee_block);
3822 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003823
Yongqiang Yang197217a2011-05-03 11:45:29 -04003824 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3825 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003826 (unsigned long long)ee_block, ee_len);
3827
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003828 /* If extent is larger than requested it is a clear sign that we still
3829 * have some extent state machine issues left. So extent_split is still
3830 * required.
3831 * TODO: Once all related issues will be fixed this situation should be
3832 * illegal.
3833 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003834 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003835#ifdef CONFIG_EXT4_DEBUG
3836 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003837 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003838 inode->i_ino, (unsigned long long)ee_block, ee_len,
3839 (unsigned long long)map->m_lblk, map->m_len);
3840#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003841 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003842 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003843 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003844 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003845 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003846 if (IS_ERR(path))
3847 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003848 depth = ext_depth(inode);
3849 ex = path[depth].p_ext;
3850 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003851
Mingming Cao00314622009-09-28 15:49:08 -04003852 err = ext4_ext_get_access(handle, inode, path + depth);
3853 if (err)
3854 goto out;
3855 /* first mark the extent as initialized */
3856 ext4_ext_mark_initialized(ex);
3857
Yongqiang Yang197217a2011-05-03 11:45:29 -04003858 /* note: ext4_ext_correct_indexes() isn't needed here because
3859 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003860 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003861 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003862
Mingming Cao00314622009-09-28 15:49:08 -04003863 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003864 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003865out:
3866 ext4_ext_show_leaf(inode, path);
3867 return err;
3868}
3869
3870static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003871convert_initialized_extent(handle_t *handle, struct inode *inode,
3872 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003873 struct ext4_ext_path **ppath,
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003874 unsigned int *allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003875{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003876 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003877 struct ext4_extent *ex;
3878 ext4_lblk_t ee_block;
3879 unsigned int ee_len;
3880 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003881 int err = 0;
3882
3883 /*
3884 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003885 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003886 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003887 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3888 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003889
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003890 depth = ext_depth(inode);
3891 ex = path[depth].p_ext;
3892 ee_block = le32_to_cpu(ex->ee_block);
3893 ee_len = ext4_ext_get_actual_len(ex);
3894
3895 ext_debug("%s: inode %lu, logical"
3896 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3897 (unsigned long long)ee_block, ee_len);
3898
3899 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003900 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003901 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3902 if (err < 0)
3903 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003904 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003905 if (IS_ERR(path))
3906 return PTR_ERR(path);
3907 depth = ext_depth(inode);
3908 ex = path[depth].p_ext;
3909 if (!ex) {
3910 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3911 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003912 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003913 }
3914 }
3915
3916 err = ext4_ext_get_access(handle, inode, path + depth);
3917 if (err)
3918 return err;
3919 /* first mark the extent as unwritten */
3920 ext4_ext_mark_unwritten(ex);
3921
3922 /* note: ext4_ext_correct_indexes() isn't needed here because
3923 * borders are not changed
3924 */
3925 ext4_ext_try_to_merge(handle, inode, path, ex);
3926
3927 /* Mark modified extent as dirty */
3928 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3929 if (err)
3930 return err;
3931 ext4_ext_show_leaf(inode, path);
3932
3933 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003934
Lukas Czernerb8a86842014-03-18 18:05:35 -04003935 map->m_flags |= EXT4_MAP_UNWRITTEN;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003936 if (*allocated > map->m_len)
3937 *allocated = map->m_len;
3938 map->m_len = *allocated;
3939 return 0;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003940}
3941
3942static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003943ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003944 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003945 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003946 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003947{
Eric Whitney4337ecd2020-02-11 16:02:16 -05003948#ifdef EXT_DEBUG
Theodore Ts'odfe50802014-09-01 14:37:09 -04003949 struct ext4_ext_path *path = *ppath;
Eric Whitney4337ecd2020-02-11 16:02:16 -05003950#endif
Mingming Cao00314622009-09-28 15:49:08 -04003951 int ret = 0;
3952 int err = 0;
3953
Lukas Czerner556615d2014-04-20 23:45:47 -04003954 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05003955 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003956 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003957 flags, allocated);
3958 ext4_ext_show_leaf(inode, path);
3959
Lukas Czerner27dd4382013-04-09 22:11:22 -04003960 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04003961 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04003962 * allocate metadata blocks for the new extent block if needed.
3963 */
3964 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
3965
Lukas Czerner556615d2014-04-20 23:45:47 -04003966 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05003967 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003968
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003969 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003970 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003971 ret = ext4_split_convert_extents(handle, inode, map, ppath,
3972 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04003973 if (ret <= 0)
3974 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003975 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003976 goto out;
3977 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003978 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003979 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05003980 if (flags & EXT4_GET_BLOCKS_ZERO) {
3981 if (allocated > map->m_len)
3982 allocated = map->m_len;
3983 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
3984 allocated);
3985 if (err < 0)
3986 goto out2;
3987 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003988 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003989 ppath);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003990 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003991 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003992 else
Theodore Ts'o58590b02010-10-27 21:23:12 -04003993 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04003994 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05003995 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04003996 if (allocated > map->m_len)
3997 allocated = map->m_len;
3998 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003999 goto out2;
4000 }
4001 /* buffered IO case */
4002 /*
4003 * repeat fallocate creation request
4004 * we already have an unwritten extent
4005 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004006 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004007 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004008 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004009 }
Mingming Cao00314622009-09-28 15:49:08 -04004010
4011 /* buffered READ or buffered write_begin() lookup */
4012 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4013 /*
4014 * We have blocks reserved already. We
4015 * return allocated blocks so that delalloc
4016 * won't do block reservation for us. But
4017 * the buffer head will be unmapped so that
4018 * a read from the block returns 0s.
4019 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004020 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004021 goto out1;
4022 }
4023
4024 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004025 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004026 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004027 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004028out:
4029 if (ret <= 0) {
4030 err = ret;
4031 goto out2;
4032 } else
4033 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004034 map->m_flags |= EXT4_MAP_NEW;
zhangyi (F)16e08b12019-02-10 23:32:07 -05004035 if (allocated > map->m_len)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004036 allocated = map->m_len;
Zheng Liu3a225672013-03-10 21:20:23 -04004037 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004038
Mingming Cao00314622009-09-28 15:49:08 -04004039map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004040 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04004041out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004042 if (allocated > map->m_len)
4043 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004044 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004045 map->m_pblk = newblock;
4046 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004047out2:
Mingming Cao00314622009-09-28 15:49:08 -04004048 return err ? err : allocated;
4049}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004050
Mingming Cao00314622009-09-28 15:49:08 -04004051/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004052 * get_implied_cluster_alloc - check to see if the requested
4053 * allocation (in the map structure) overlaps with a cluster already
4054 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004055 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004056 * @map The requested lblk->pblk mapping
4057 * @ex The extent structure which might contain an implied
4058 * cluster allocation
4059 *
4060 * This function is called by ext4_ext_map_blocks() after we failed to
4061 * find blocks that were already in the inode's extent tree. Hence,
4062 * we know that the beginning of the requested region cannot overlap
4063 * the extent from the inode's extent tree. There are three cases we
4064 * want to catch. The first is this case:
4065 *
4066 * |--- cluster # N--|
4067 * |--- extent ---| |---- requested region ---|
4068 * |==========|
4069 *
4070 * The second case that we need to test for is this one:
4071 *
4072 * |--------- cluster # N ----------------|
4073 * |--- requested region --| |------- extent ----|
4074 * |=======================|
4075 *
4076 * The third case is when the requested region lies between two extents
4077 * within the same cluster:
4078 * |------------- cluster # N-------------|
4079 * |----- ex -----| |---- ex_right ----|
4080 * |------ requested region ------|
4081 * |================|
4082 *
4083 * In each of the above cases, we need to set the map->m_pblk and
4084 * map->m_len so it corresponds to the return the extent labelled as
4085 * "|====|" from cluster #N, since it is already in use for data in
4086 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4087 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4088 * as a new "allocated" block region. Otherwise, we will return 0 and
4089 * ext4_ext_map_blocks() will then allocate one or more new clusters
4090 * by calling ext4_mb_new_blocks().
4091 */
Aditya Kalid8990242011-09-09 19:18:51 -04004092static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004093 struct ext4_map_blocks *map,
4094 struct ext4_extent *ex,
4095 struct ext4_ext_path *path)
4096{
Aditya Kalid8990242011-09-09 19:18:51 -04004097 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004098 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004099 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004100 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004101 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4102 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4103 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4104
4105 /* The extent passed in that we are trying to match */
4106 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4107 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4108
4109 /* The requested region passed into ext4_map_blocks() */
4110 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004111
4112 if ((rr_cluster_start == ex_cluster_end) ||
4113 (rr_cluster_start == ex_cluster_start)) {
4114 if (rr_cluster_start == ex_cluster_end)
4115 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004116 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004117 map->m_len = min(map->m_len,
4118 (unsigned) sbi->s_cluster_ratio - c_offset);
4119 /*
4120 * Check for and handle this case:
4121 *
4122 * |--------- cluster # N-------------|
4123 * |------- extent ----|
4124 * |--- requested region ---|
4125 * |===========|
4126 */
4127
4128 if (map->m_lblk < ee_block)
4129 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4130
4131 /*
4132 * Check for the case where there is already another allocated
4133 * block to the right of 'ex' but before the end of the cluster.
4134 *
4135 * |------------- cluster # N-------------|
4136 * |----- ex -----| |---- ex_right ----|
4137 * |------ requested region ------|
4138 * |================|
4139 */
4140 if (map->m_lblk > ee_block) {
4141 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4142 map->m_len = min(map->m_len, next - map->m_lblk);
4143 }
Aditya Kalid8990242011-09-09 19:18:51 -04004144
4145 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004146 return 1;
4147 }
Aditya Kalid8990242011-09-09 19:18:51 -04004148
4149 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004150 return 0;
4151}
4152
4153
4154/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004155 * Block allocation/map/preallocation routine for extents based files
4156 *
4157 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004158 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004159 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4160 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004161 *
4162 * return > 0, number of of blocks already mapped/allocated
4163 * if create == 0 and these are pre-allocated blocks
4164 * buffer head is unmapped
4165 * otherwise blocks are mapped
4166 *
4167 * return = 0, if plain look up failed (blocks have not been allocated)
4168 * buffer head is unmapped
4169 *
4170 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004171 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004172int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4173 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004174{
4175 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004176 struct ext4_extent newex, *ex, *ex2;
4177 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004178 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004179 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004180 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004181 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004182 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004183 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004184 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004185
Mingming84fe3be2009-09-01 08:44:37 -04004186 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004187 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004188 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004189
Alex Tomasa86c6182006-10-11 01:21:03 -07004190 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004191 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004192 if (IS_ERR(path)) {
4193 err = PTR_ERR(path);
4194 path = NULL;
4195 goto out2;
4196 }
4197
4198 depth = ext_depth(inode);
4199
4200 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004201 * consistent leaf must not be empty;
4202 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004203 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004204 */
Frank Mayhar273df552010-03-02 11:46:09 -05004205 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4206 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004207 "lblock: %lu, depth: %d pblock %lld",
4208 (unsigned long) map->m_lblk, depth,
4209 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004210 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004211 goto out2;
4212 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004213
Avantika Mathur7e028972006-12-06 20:41:33 -08004214 ex = path[depth].p_ext;
4215 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004216 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004217 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004218 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004219
Lukas Czernerb8a86842014-03-18 18:05:35 -04004220
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004221 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004222 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004223 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004224 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004225 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004226
4227 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4228
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004229 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004230 if (in_range(map->m_lblk, ee_block, ee_len)) {
4231 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004232 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004233 allocated = ee_len - (map->m_lblk - ee_block);
4234 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4235 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004236
Lukas Czernerb8a86842014-03-18 18:05:35 -04004237 /*
4238 * If the extent is initialized check whether the
4239 * caller wants to convert it to unwritten.
4240 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004241 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004242 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004243 err = convert_initialized_extent(handle,
4244 inode, map, &path, &allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004245 goto out2;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004246 } else if (!ext4_ext_is_unwritten(ex)) {
Lukas Czerner78771912012-03-19 23:05:43 -04004247 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004248 }
Zheng Liu69eb33d2013-02-18 00:31:07 -05004249
Lukas Czerner556615d2014-04-20 23:45:47 -04004250 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004251 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004252 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004253 if (ret < 0)
4254 err = ret;
4255 else
4256 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004257 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004258 }
4259 }
4260
4261 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004262 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004263 * we couldn't try to create block if create flag is zero
4264 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004265 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004266 ext4_lblk_t hole_start, hole_len;
4267
Jan Karafacab4d2016-03-09 22:54:00 -05004268 hole_start = map->m_lblk;
4269 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004270 /*
4271 * put just found gap into cache to speed up
4272 * subsequent requests
4273 */
Jan Kara140a5252016-03-09 22:46:57 -05004274 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004275
4276 /* Update hole_len to reflect hole size after map->m_lblk */
4277 if (hole_start != map->m_lblk)
4278 hole_len -= map->m_lblk - hole_start;
4279 map->m_pblk = 0;
4280 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4281
Alex Tomasa86c6182006-10-11 01:21:03 -07004282 goto out2;
4283 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004284
Alex Tomasa86c6182006-10-11 01:21:03 -07004285 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004286 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004287 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004288 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004289 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004290
4291 /*
4292 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004293 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004294 */
4295 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004296 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004297 ar.len = allocated = map->m_len;
4298 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004299 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004300 goto got_allocated_blocks;
4301 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004302
Alex Tomasc9de5602008-01-29 00:19:52 -05004303 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004304 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004305 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4306 if (err)
4307 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004308 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004309 ex2 = NULL;
4310 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004311 if (err)
4312 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004313
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004314 /* Check if the extent after searching to the right implies a
4315 * cluster we can use. */
4316 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004317 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004318 ar.len = allocated = map->m_len;
4319 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004320 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004321 goto got_allocated_blocks;
4322 }
4323
Amit Arora749269f2007-07-18 09:02:56 -04004324 /*
4325 * See if request is beyond maximum number of blocks we can have in
4326 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004327 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4328 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004329 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004330 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004331 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004332 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004333 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4334 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4335 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004336
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004337 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004338 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004339 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004340 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004341 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004342 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004343 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004344
4345 /* allocate new block */
4346 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004347 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4348 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004349 /*
4350 * We calculate the offset from the beginning of the cluster
4351 * for the logical block number, since when we allocate a
4352 * physical cluster, the physical block should start at the
4353 * same offset from the beginning of the cluster. This is
4354 * needed so that future calls to get_implied_cluster_alloc()
4355 * work correctly.
4356 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004357 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004358 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4359 ar.goal -= offset;
4360 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004361 if (S_ISREG(inode->i_mode))
4362 ar.flags = EXT4_MB_HINT_DATA;
4363 else
4364 /* disable in-core preallocation for non-regular files */
4365 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004366 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4367 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004368 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4369 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004370 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4371 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004372 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004373 if (!newblock)
4374 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004375 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004376 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004377 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004378 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004379 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4380 if (ar.len > allocated)
4381 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004382
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004383got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004384 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004385 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004386 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004387 /* Mark unwritten */
4388 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4389 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004390 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004391 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004392
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004393 err = 0;
Eric Whitney4337ecd2020-02-11 16:02:16 -05004394 err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004395
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004396 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004397 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4398 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004399 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004400 /* not a good idea to call discard here directly,
4401 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004402 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004403 ext4_free_blocks(handle, inode, NULL, newblock,
4404 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004405 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004406 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004407
Alex Tomasa86c6182006-10-11 01:21:03 -07004408 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004409 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004410 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004411 if (allocated > map->m_len)
4412 allocated = map->m_len;
4413 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004414
Jan Karab436b9b2009-12-08 23:51:10 -05004415 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004416 * Reduce the reserved cluster count to reflect successful deferred
4417 * allocation of delayed allocated clusters or direct allocation of
4418 * clusters discovered to be delayed allocated. Once allocated, a
4419 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004420 */
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004421 if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) {
4422 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004423 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004424 * When allocating delayed allocated clusters, simply
4425 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004426 */
4427 ext4_da_update_reserve_space(inode, allocated_clusters,
4428 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004429 } else {
4430 ext4_lblk_t lblk, len;
4431 unsigned int n;
4432
4433 /*
4434 * When allocating non-delayed allocated clusters
4435 * (from fallocate, filemap, DIO, or clusters
4436 * allocated when delalloc has been disabled by
4437 * ext4_nonda_switch), reduce the reserved cluster
4438 * count by the number of allocated clusters that
4439 * have previously been delayed allocated. Quota
4440 * has been claimed by ext4_mb_new_blocks() above,
4441 * so release the quota reservations made for any
4442 * previously delayed allocated clusters.
4443 */
4444 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4445 len = allocated_clusters << sbi->s_cluster_bits;
4446 n = ext4_es_delayed_clu(inode, lblk, len);
4447 if (n > 0)
4448 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004449 }
4450 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004451
4452 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004453 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004454 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004455 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004456 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004457 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004458 else
Jan Karab436b9b2009-12-08 23:51:10 -05004459 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004460out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004461 if (allocated > map->m_len)
4462 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004463 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004464 map->m_flags |= EXT4_MAP_MAPPED;
4465 map->m_pblk = newblock;
4466 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004467out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004468 ext4_ext_drop_refs(path);
4469 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004470
Theodore Ts'o63b99962013-07-16 10:28:47 -04004471 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4472 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004473 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004474}
4475
Theodore Ts'od0abb362016-11-13 22:02:28 -05004476int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004477{
Alex Tomasa86c6182006-10-11 01:21:03 -07004478 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004479 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004480 int err = 0;
4481
4482 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004483 * TODO: optimization is possible here.
4484 * Probably we need not scan at all,
4485 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004486 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004487
4488 /* we have to know where to truncate from in crash case */
4489 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004490 err = ext4_mark_inode_dirty(handle, inode);
4491 if (err)
4492 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004493
4494 last_block = (inode->i_size + sb->s_blocksize - 1)
4495 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004496retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004497 err = ext4_es_remove_extent(inode, last_block,
4498 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004499 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004500 cond_resched();
4501 congestion_wait(BLK_RW_ASYNC, HZ/50);
4502 goto retry;
4503 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004504 if (err)
4505 return err;
4506 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004507}
4508
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004509static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004510 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004511 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004512{
Al Viro496ad9a2013-01-23 17:07:38 -05004513 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004514 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004515 int ret = 0;
4516 int ret2 = 0;
4517 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004518 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004519 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004520 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004521 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004522
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004523 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004524 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004525 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004526 /*
4527 * Don't normalize the request if it can fit in one extent so
4528 * that it doesn't get unnecessarily split into multiple
4529 * extents.
4530 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004531 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004532 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004533
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004534 /*
4535 * credits to insert 1 extent into extent tree
4536 */
4537 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004538 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004539
Amit Aroraa2df2a62007-07-17 21:42:41 -04004540retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004541 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004542 /*
4543 * Recalculate credits when extent tree depth changes.
4544 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004545 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004546 credits = ext4_chunk_trans_blocks(inode, len);
4547 depth = ext_depth(inode);
4548 }
4549
Theodore Ts'o9924a922013-02-08 21:59:22 -05004550 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4551 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004552 if (IS_ERR(handle)) {
4553 ret = PTR_ERR(handle);
4554 break;
4555 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004556 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004557 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004558 ext4_debug("inode #%lu: block %u: len %u: "
4559 "ext4_ext_map_blocks returned %d",
4560 inode->i_ino, map.m_lblk,
4561 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004562 ext4_mark_inode_dirty(handle, inode);
4563 ret2 = ext4_journal_stop(handle);
4564 break;
4565 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004566 map.m_lblk += ret;
4567 map.m_len = len = len - ret;
4568 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004569 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004570 if (new_size) {
4571 if (epos > new_size)
4572 epos = new_size;
4573 if (ext4_update_inode_size(inode, epos) & 0x1)
4574 inode->i_mtime = inode->i_ctime;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004575 }
4576 ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004577 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004578 ret2 = ext4_journal_stop(handle);
4579 if (ret2)
4580 break;
4581 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004582 if (ret == -ENOSPC &&
4583 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4584 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004585 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004586 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004587
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004588 return ret > 0 ? ret2 : ret;
4589}
4590
Eric Biggers43f81672019-12-31 12:04:40 -06004591static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
4592
4593static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len);
4594
Lukas Czernerb8a86842014-03-18 18:05:35 -04004595static long ext4_zero_range(struct file *file, loff_t offset,
4596 loff_t len, int mode)
4597{
4598 struct inode *inode = file_inode(file);
4599 handle_t *handle = NULL;
4600 unsigned int max_blocks;
4601 loff_t new_size = 0;
4602 int ret = 0;
4603 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004604 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004605 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004606 loff_t start, end;
4607 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004608 unsigned int blkbits = inode->i_blkbits;
4609
4610 trace_ext4_zero_range(inode, offset, len, mode);
4611
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004612 /* Call ext4_force_commit to flush all data in case of data=journal. */
4613 if (ext4_should_journal_data(inode)) {
4614 ret = ext4_force_commit(inode->i_sb);
4615 if (ret)
4616 return ret;
4617 }
4618
Lukas Czernerb8a86842014-03-18 18:05:35 -04004619 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004620 * Round up offset. This is not fallocate, we neet to zero out
4621 * blocks, so convert interior block aligned part of the range to
4622 * unwritten and possibly manually zero out unaligned parts of the
4623 * range.
4624 */
4625 start = round_up(offset, 1 << blkbits);
4626 end = round_down((offset + len), 1 << blkbits);
4627
4628 if (start < offset || end > offset + len)
4629 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004630 partial_begin = offset & ((1 << blkbits) - 1);
4631 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004632
4633 lblk = start >> blkbits;
4634 max_blocks = (end >> blkbits);
4635 if (max_blocks < lblk)
4636 max_blocks = 0;
4637 else
4638 max_blocks -= lblk;
4639
Al Viro59551022016-01-22 15:40:57 -05004640 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004641
4642 /*
4643 * Indirect files do not support unwritten extnets
4644 */
4645 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4646 ret = -EOPNOTSUPP;
4647 goto out_mutex;
4648 }
4649
4650 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004651 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004652 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004653 new_size = offset + len;
4654 ret = inode_newsize_ok(inode, new_size);
4655 if (ret)
4656 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004657 }
4658
Lukas Czerner0f2af212015-04-03 00:09:13 -04004659 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4660 if (mode & FALLOC_FL_KEEP_SIZE)
4661 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4662
Jan Kara17048e82015-12-07 14:29:17 -05004663 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004664 inode_dio_wait(inode);
4665
Lukas Czerner0f2af212015-04-03 00:09:13 -04004666 /* Preallocate the range including the unaligned edges */
4667 if (partial_begin || partial_end) {
4668 ret = ext4_alloc_file_blocks(file,
4669 round_down(offset, 1 << blkbits) >> blkbits,
4670 (round_up((offset + len), 1 << blkbits) -
4671 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004672 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004673 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004674 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004675
4676 }
4677
4678 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004679 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004680 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4681 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004682
Jan Karaea3d7202015-12-07 14:28:03 -05004683 /*
4684 * Prevent page faults from reinstantiating pages we have
4685 * released from page cache.
4686 */
4687 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004688
4689 ret = ext4_break_layouts(inode);
4690 if (ret) {
4691 up_write(&EXT4_I(inode)->i_mmap_sem);
4692 goto out_mutex;
4693 }
4694
Jan Kara01127842015-12-07 14:34:49 -05004695 ret = ext4_update_disksize_before_punch(inode, offset, len);
4696 if (ret) {
4697 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004698 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004699 }
Jan Karaea3d7202015-12-07 14:28:03 -05004700 /* Now release the pages and zero block aligned part of pages */
4701 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004702 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004703
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004704 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004705 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004706 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004707 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004708 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004709 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004710 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004711 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004712
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004713 /*
4714 * In worst case we have to writeout two nonadjacent unwritten
4715 * blocks and update the inode
4716 */
4717 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4718 if (ext4_should_journal_data(inode))
4719 credits += 2;
4720 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004721 if (IS_ERR(handle)) {
4722 ret = PTR_ERR(handle);
4723 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004724 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004725 }
4726
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004727 inode->i_mtime = inode->i_ctime = current_time(inode);
Eric Whitney4337ecd2020-02-11 16:02:16 -05004728 if (new_size)
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004729 ext4_update_inode_size(inode, new_size);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004730 ext4_mark_inode_dirty(handle, inode);
4731
4732 /* Zero out partial block at the edges of the range */
4733 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004734 if (ret >= 0)
4735 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004736
4737 if (file->f_flags & O_SYNC)
4738 ext4_handle_sync(handle);
4739
4740 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004741out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004742 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004743 return ret;
4744}
4745
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004746/*
4747 * preallocate space for a file. This implements ext4's fallocate file
4748 * operation, which gets called from sys_fallocate system call.
4749 * For block-mapped files, posix_fallocate should fall back to the method
4750 * of writing zeroes to the required new blocks (the same behavior which is
4751 * expected for file systems which do not support fallocate() system call).
4752 */
4753long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4754{
4755 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004756 loff_t new_size = 0;
4757 unsigned int max_blocks;
4758 int ret = 0;
4759 int flags;
4760 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004761 unsigned int blkbits = inode->i_blkbits;
4762
Michael Halcrow2058f832015-04-12 00:55:10 -04004763 /*
4764 * Encrypted inodes can't handle collapse range or insert
4765 * range since we would need to re-encrypt blocks with a
4766 * different IV or XTS tweak (which are based on the logical
4767 * block number).
Michael Halcrow2058f832015-04-12 00:55:10 -04004768 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304769 if (IS_ENCRYPTED(inode) &&
Eric Biggers457b1e32019-12-26 09:42:16 -06004770 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004771 return -EOPNOTSUPP;
4772
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004773 /* Return error if mode is not supported */
4774 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004775 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4776 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004777 return -EOPNOTSUPP;
4778
4779 if (mode & FALLOC_FL_PUNCH_HOLE)
4780 return ext4_punch_hole(inode, offset, len);
4781
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004782 ret = ext4_convert_inline_data(inode);
4783 if (ret)
4784 return ret;
4785
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004786 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4787 return ext4_collapse_range(inode, offset, len);
4788
Namjae Jeon331573f2015-06-09 01:55:03 -04004789 if (mode & FALLOC_FL_INSERT_RANGE)
4790 return ext4_insert_range(inode, offset, len);
4791
Lukas Czernerb8a86842014-03-18 18:05:35 -04004792 if (mode & FALLOC_FL_ZERO_RANGE)
4793 return ext4_zero_range(file, offset, len, mode);
4794
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004795 trace_ext4_fallocate_enter(inode, offset, len, mode);
4796 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004797
Fabian Frederick518eaa62016-09-15 11:55:01 -04004798 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004799 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004800 if (mode & FALLOC_FL_KEEP_SIZE)
4801 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4802
Al Viro59551022016-01-22 15:40:57 -05004803 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004804
Davide Italiano280227a2015-05-02 23:21:15 -04004805 /*
4806 * We only support preallocation for extent-based files only
4807 */
4808 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4809 ret = -EOPNOTSUPP;
4810 goto out;
4811 }
4812
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004813 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004814 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004815 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004816 new_size = offset + len;
4817 ret = inode_newsize_ok(inode, new_size);
4818 if (ret)
4819 goto out;
4820 }
4821
Jan Kara17048e82015-12-07 14:29:17 -05004822 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004823 inode_dio_wait(inode);
4824
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004825 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004826 if (ret)
4827 goto out;
4828
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004829 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4830 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4831 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004832 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004833out:
Al Viro59551022016-01-22 15:40:57 -05004834 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004835 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4836 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004837}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004838
4839/*
Mingming Cao00314622009-09-28 15:49:08 -04004840 * This function convert a range of blocks to written extents
4841 * The caller of this function will pass the start offset and the size.
4842 * all unwritten extents within this range will be converted to
4843 * written extents.
4844 *
4845 * This function is called from the direct IO end io call back
4846 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004847 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004848 */
Jan Kara6b523df2013-06-04 13:21:11 -04004849int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4850 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004851{
Mingming Cao00314622009-09-28 15:49:08 -04004852 unsigned int max_blocks;
4853 int ret = 0;
4854 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004855 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304856 unsigned int blkbits = inode->i_blkbits;
4857 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004858
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004859 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004860 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4861
Ritesh Harjania00713e2019-10-16 13:07:08 +05304862 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004863 /*
4864 * credits to insert 1 extent into extent tree
4865 */
4866 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4867 }
Mingming Cao00314622009-09-28 15:49:08 -04004868 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004869 map.m_lblk += ret;
4870 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04004871 if (credits) {
4872 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4873 credits);
4874 if (IS_ERR(handle)) {
4875 ret = PTR_ERR(handle);
4876 break;
4877 }
Mingming Cao00314622009-09-28 15:49:08 -04004878 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004879 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004880 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05004881 if (ret <= 0)
4882 ext4_warning(inode->i_sb,
4883 "inode #%lu: block %u: len %u: "
4884 "ext4_ext_map_blocks returned %d",
4885 inode->i_ino, map.m_lblk,
4886 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004887 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04004888 if (credits)
4889 ret2 = ext4_journal_stop(handle);
4890 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04004891 break;
4892 }
4893 return ret > 0 ? ret2 : ret;
4894}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004895
Ritesh Harjania00713e2019-10-16 13:07:08 +05304896int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
4897{
4898 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304899 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304900
4901 /*
4902 * This is somewhat ugly but the idea is clear: When transaction is
4903 * reserved, everything goes into it. Otherwise we rather start several
4904 * smaller transactions for conversion of each extent separately.
4905 */
4906 if (handle) {
4907 handle = ext4_journal_start_reserved(handle,
4908 EXT4_HT_EXT_CONVERT);
4909 if (IS_ERR(handle))
4910 return PTR_ERR(handle);
4911 }
4912
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304913 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
4914 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
4915 io_end_vec->offset,
4916 io_end_vec->size);
4917 if (ret)
4918 break;
4919 }
4920
Ritesh Harjania00713e2019-10-16 13:07:08 +05304921 if (handle)
4922 err = ext4_journal_stop(handle);
4923
4924 return ret < 0 ? ret : err;
4925}
4926
Mingming Cao00314622009-09-28 15:49:08 -04004927/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05004928 * If newes is not existing extent (newes->ec_pblk equals zero) find
4929 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004930 * return start of the next delayed extent.
4931 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05004932 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004933 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05004934 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004935 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004936static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05004937 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004938{
Zheng Liub3aff3e2012-11-08 21:57:37 -05004939 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05004940 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004941
Zheng Liu69eb33d2013-02-18 00:31:07 -05004942 if (newes->es_pblk == 0) {
Eric Whitneyad431022018-10-01 14:10:39 -04004943 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
4944 newes->es_lblk,
4945 newes->es_lblk + newes->es_len - 1,
4946 &es);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04004947
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004948 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05004949 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004950 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004951 */
Zheng Liu06b0c882013-02-18 00:26:51 -05004952 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05004953 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004954 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004955
Zheng Liu69eb33d2013-02-18 00:31:07 -05004956 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05004957 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05004958 newes->es_len = min(es.es_lblk - newes->es_lblk,
4959 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004960 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004961 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004962
Zheng Liu69eb33d2013-02-18 00:31:07 -05004963 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004964 }
4965
Zheng Liu69eb33d2013-02-18 00:31:07 -05004966 block = newes->es_lblk + newes->es_len;
Eric Whitneyad431022018-10-01 14:10:39 -04004967 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block,
4968 EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05004969 if (es.es_len == 0)
4970 next_del = EXT_MAX_BLOCKS;
4971 else
4972 next_del = es.es_lblk;
4973
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004974 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004975}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004976
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004977static int ext4_xattr_fiemap(struct inode *inode,
4978 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004979{
4980 __u64 physical = 0;
4981 __u64 length;
4982 __u32 flags = FIEMAP_EXTENT_LAST;
4983 int blockbits = inode->i_sb->s_blocksize_bits;
4984 int error = 0;
4985
4986 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004987 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004988 struct ext4_iloc iloc;
4989 int offset; /* offset of xattr in inode */
4990
4991 error = ext4_get_inode_loc(inode, &iloc);
4992 if (error)
4993 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04004994 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004995 offset = EXT4_GOOD_OLD_INODE_SIZE +
4996 EXT4_I(inode)->i_extra_isize;
4997 physical += offset;
4998 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4999 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005000 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005001 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005002 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005003 length = inode->i_sb->s_blocksize;
5004 }
5005
5006 if (physical)
5007 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5008 length, flags);
5009 return (error < 0 ? error : 0);
5010}
5011
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005012static int _ext4_fiemap(struct inode *inode,
5013 struct fiemap_extent_info *fieinfo,
5014 __u64 start, __u64 len,
5015 int (*fill)(struct inode *, ext4_lblk_t,
5016 ext4_lblk_t,
5017 struct fiemap_extent_info *))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005018{
5019 ext4_lblk_t start_blk;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005020 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR;
5021
Eric Sandeen6873fa02008-10-07 00:46:36 -04005022 int error = 0;
5023
Tao Ma94191982012-12-10 14:06:02 -05005024 if (ext4_has_inline_data(inode)) {
5025 int has_inline = 1;
5026
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005027 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5028 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005029
5030 if (has_inline)
5031 return error;
5032 }
5033
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005034 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5035 error = ext4_ext_precache(inode);
5036 if (error)
5037 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005038 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005039 }
5040
Eric Sandeen6873fa02008-10-07 00:46:36 -04005041 /* fallback to generic here if not in extents fmt */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005042 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
5043 fill == ext4_fill_fiemap_extents)
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005044 return generic_block_fiemap(inode, fieinfo, start, len,
5045 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005046
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005047 if (fill == ext4_fill_es_cache_info)
5048 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
5049 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005050 return -EBADR;
5051
5052 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5053 error = ext4_xattr_fiemap(inode, fieinfo);
5054 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005055 ext4_lblk_t len_blks;
5056 __u64 last_blk;
5057
Eric Sandeen6873fa02008-10-07 00:46:36 -04005058 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005059 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005060 if (last_blk >= EXT_MAX_BLOCKS)
5061 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005062 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005063
5064 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005065 * Walk the extent tree gathering extent information
5066 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005067 */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005068 error = fill(inode, start_blk, len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005069 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005070 return error;
5071}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005072
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005073int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5074 __u64 start, __u64 len)
5075{
5076 return _ext4_fiemap(inode, fieinfo, start, len,
5077 ext4_fill_fiemap_extents);
5078}
5079
5080int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
5081 __u64 start, __u64 len)
5082{
5083 if (ext4_has_inline_data(inode)) {
5084 int has_inline;
5085
5086 down_read(&EXT4_I(inode)->xattr_sem);
5087 has_inline = ext4_has_inline_data(inode);
5088 up_read(&EXT4_I(inode)->xattr_sem);
5089 if (has_inline)
5090 return 0;
5091 }
5092
5093 return _ext4_fiemap(inode, fieinfo, start, len,
5094 ext4_fill_es_cache_info);
5095}
5096
5097
Namjae Jeon9eb79482014-02-23 15:18:59 -05005098/*
5099 * ext4_access_path:
5100 * Function to access the path buffer for marking it dirty.
5101 * It also checks if there are sufficient credits left in the journal handle
5102 * to update path.
5103 */
5104static int
5105ext4_access_path(handle_t *handle, struct inode *inode,
5106 struct ext4_ext_path *path)
5107{
5108 int credits, err;
5109
5110 if (!ext4_handle_valid(handle))
5111 return 0;
5112
5113 /*
5114 * Check if need to extend journal credits
5115 * 3 for leaf, sb, and inode plus 2 (bmap and group
5116 * descriptor) for each block group; assume two block
5117 * groups
5118 */
Jan Karaa4130362019-11-05 17:44:16 +01005119 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01005120 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01005121 if (err < 0)
5122 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005123
5124 err = ext4_ext_get_access(handle, inode, path);
5125 return err;
5126}
5127
5128/*
5129 * ext4_ext_shift_path_extents:
5130 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005131 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5132 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005133 */
5134static int
5135ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5136 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005137 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005138{
5139 int depth, err = 0;
5140 struct ext4_extent *ex_start, *ex_last;
zhengbin4756ee12019-12-25 10:45:59 +08005141 bool update = false;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005142 depth = path->p_depth;
5143
5144 while (depth >= 0) {
5145 if (depth == path->p_depth) {
5146 ex_start = path[depth].p_ext;
5147 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005148 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005149
5150 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005151
5152 err = ext4_access_path(handle, inode, path + depth);
5153 if (err)
5154 goto out;
5155
5156 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
zhengbin4756ee12019-12-25 10:45:59 +08005157 update = true;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005158
Namjae Jeon9eb79482014-02-23 15:18:59 -05005159 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005160 if (SHIFT == SHIFT_LEFT) {
5161 le32_add_cpu(&ex_start->ee_block,
5162 -shift);
5163 /* Try to merge to the left. */
5164 if ((ex_start >
5165 EXT_FIRST_EXTENT(path[depth].p_hdr))
5166 &&
5167 ext4_ext_try_to_merge_right(inode,
5168 path, ex_start - 1))
5169 ex_last--;
5170 else
5171 ex_start++;
5172 } else {
5173 le32_add_cpu(&ex_last->ee_block, shift);
5174 ext4_ext_try_to_merge_right(inode, path,
5175 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005176 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005177 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005178 }
5179 err = ext4_ext_dirty(handle, inode, path + depth);
5180 if (err)
5181 goto out;
5182
5183 if (--depth < 0 || !update)
5184 break;
5185 }
5186
5187 /* Update index too */
5188 err = ext4_access_path(handle, inode, path + depth);
5189 if (err)
5190 goto out;
5191
Namjae Jeon331573f2015-06-09 01:55:03 -04005192 if (SHIFT == SHIFT_LEFT)
5193 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5194 else
5195 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005196 err = ext4_ext_dirty(handle, inode, path + depth);
5197 if (err)
5198 goto out;
5199
5200 /* we are done if current index is not a starting index */
5201 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5202 break;
5203
5204 depth--;
5205 }
5206
5207out:
5208 return err;
5209}
5210
5211/*
5212 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005213 * All the extents which lies in the range from @start to the last allocated
5214 * block for the @inode are shifted either towards left or right (depending
5215 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005216 * On success, 0 is returned, error otherwise.
5217 */
5218static int
5219ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005220 ext4_lblk_t start, ext4_lblk_t shift,
5221 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005222{
5223 struct ext4_ext_path *path;
5224 int ret = 0, depth;
5225 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005226 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005227
5228 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005229 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5230 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005231 if (IS_ERR(path))
5232 return PTR_ERR(path);
5233
5234 depth = path->p_depth;
5235 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005236 if (!extent)
5237 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005238
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005239 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005240
Namjae Jeon331573f2015-06-09 01:55:03 -04005241 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005242 * For left shifts, make sure the hole on the left is big enough to
5243 * accommodate the shift. For right shifts, make sure the last extent
5244 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005245 */
5246 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005247 path = ext4_find_extent(inode, start - 1, &path,
5248 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005249 if (IS_ERR(path))
5250 return PTR_ERR(path);
5251 depth = path->p_depth;
5252 extent = path[depth].p_ext;
5253 if (extent) {
5254 ex_start = le32_to_cpu(extent->ee_block);
5255 ex_end = le32_to_cpu(extent->ee_block) +
5256 ext4_ext_get_actual_len(extent);
5257 } else {
5258 ex_start = 0;
5259 ex_end = 0;
5260 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005261
Namjae Jeon331573f2015-06-09 01:55:03 -04005262 if ((start == ex_start && shift > ex_start) ||
5263 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005264 ret = -EINVAL;
5265 goto out;
5266 }
5267 } else {
5268 if (shift > EXT_MAX_BLOCKS -
5269 (stop + ext4_ext_get_actual_len(extent))) {
5270 ret = -EINVAL;
5271 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005272 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005273 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005274
Namjae Jeon331573f2015-06-09 01:55:03 -04005275 /*
5276 * In case of left shift, iterator points to start and it is increased
5277 * till we reach stop. In case of right shift, iterator points to stop
5278 * and it is decreased till we reach start.
5279 */
5280 if (SHIFT == SHIFT_LEFT)
5281 iterator = &start;
5282 else
5283 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005284
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005285 /*
5286 * Its safe to start updating extents. Start and stop are unsigned, so
5287 * in case of right shift if extent with 0 block is reached, iterator
5288 * becomes NULL to indicate the end of the loop.
5289 */
5290 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005291 path = ext4_find_extent(inode, *iterator, &path,
5292 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005293 if (IS_ERR(path))
5294 return PTR_ERR(path);
5295 depth = path->p_depth;
5296 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005297 if (!extent) {
5298 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005299 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005300 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005301 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005302 if (SHIFT == SHIFT_LEFT && *iterator >
5303 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005304 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005305 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5306 path[depth].p_ext++;
5307 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005308 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005309 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005310 }
5311 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005312
5313 if (SHIFT == SHIFT_LEFT) {
5314 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5315 *iterator = le32_to_cpu(extent->ee_block) +
5316 ext4_ext_get_actual_len(extent);
5317 } else {
5318 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005319 if (le32_to_cpu(extent->ee_block) > 0)
5320 *iterator = le32_to_cpu(extent->ee_block) - 1;
5321 else
5322 /* Beginning is reached, end of the loop */
5323 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005324 /* Update path extent in case we need to stop */
5325 while (le32_to_cpu(extent->ee_block) < start)
5326 extent++;
5327 path[depth].p_ext = extent;
5328 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005329 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005330 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005331 if (ret)
5332 break;
5333 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005334out:
5335 ext4_ext_drop_refs(path);
5336 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005337 return ret;
5338}
5339
5340/*
5341 * ext4_collapse_range:
5342 * This implements the fallocate's collapse range functionality for ext4
5343 * Returns: 0 and non-zero on error.
5344 */
Eric Biggers43f81672019-12-31 12:04:40 -06005345static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005346{
5347 struct super_block *sb = inode->i_sb;
5348 ext4_lblk_t punch_start, punch_stop;
5349 handle_t *handle;
5350 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005351 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005352 int ret;
5353
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005354 /*
5355 * We need to test this early because xfstests assumes that a
5356 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5357 * system does not support collapse range.
5358 */
5359 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5360 return -EOPNOTSUPP;
5361
Eric Biggers9b02e492019-12-31 12:04:38 -06005362 /* Collapse range works only on fs cluster size aligned regions. */
5363 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005364 return -EINVAL;
5365
Namjae Jeon9eb79482014-02-23 15:18:59 -05005366 trace_ext4_collapse_range(inode, offset, len);
5367
5368 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5369 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5370
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005371 /* Call ext4_force_commit to flush all data in case of data=journal. */
5372 if (ext4_should_journal_data(inode)) {
5373 ret = ext4_force_commit(inode->i_sb);
5374 if (ret)
5375 return ret;
5376 }
5377
Al Viro59551022016-01-22 15:40:57 -05005378 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005379 /*
5380 * There is no need to overlap collapse range with EOF, in which case
5381 * it is effectively a truncate operation
5382 */
Eric Biggers9b02e492019-12-31 12:04:38 -06005383 if (offset + len >= inode->i_size) {
Lukas Czerner23fffa92014-04-12 09:56:41 -04005384 ret = -EINVAL;
5385 goto out_mutex;
5386 }
5387
Namjae Jeon9eb79482014-02-23 15:18:59 -05005388 /* Currently just for extent based files */
5389 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5390 ret = -EOPNOTSUPP;
5391 goto out_mutex;
5392 }
5393
Namjae Jeon9eb79482014-02-23 15:18:59 -05005394 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005395 inode_dio_wait(inode);
5396
Jan Karaea3d7202015-12-07 14:28:03 -05005397 /*
5398 * Prevent page faults from reinstantiating pages we have released from
5399 * page cache.
5400 */
5401 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005402
5403 ret = ext4_break_layouts(inode);
5404 if (ret)
5405 goto out_mmap;
5406
Jan Kara32ebffd2015-12-07 14:31:11 -05005407 /*
5408 * Need to round down offset to be aligned with page size boundary
5409 * for page size > block size.
5410 */
5411 ioffset = round_down(offset, PAGE_SIZE);
5412 /*
5413 * Write tail of the last page before removed range since it will get
5414 * removed from the page cache below.
5415 */
5416 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5417 if (ret)
5418 goto out_mmap;
5419 /*
5420 * Write data that will be shifted to preserve them when discarding
5421 * page cache below. We are also protected from pages becoming dirty
5422 * by i_mmap_sem.
5423 */
5424 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5425 LLONG_MAX);
5426 if (ret)
5427 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005428 truncate_pagecache(inode, ioffset);
5429
Namjae Jeon9eb79482014-02-23 15:18:59 -05005430 credits = ext4_writepage_trans_blocks(inode);
5431 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5432 if (IS_ERR(handle)) {
5433 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005434 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005435 }
5436
5437 down_write(&EXT4_I(inode)->i_data_sem);
5438 ext4_discard_preallocations(inode);
5439
5440 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005441 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005442 if (ret) {
5443 up_write(&EXT4_I(inode)->i_data_sem);
5444 goto out_stop;
5445 }
5446
5447 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5448 if (ret) {
5449 up_write(&EXT4_I(inode)->i_data_sem);
5450 goto out_stop;
5451 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005452 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005453
5454 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005455 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005456 if (ret) {
5457 up_write(&EXT4_I(inode)->i_data_sem);
5458 goto out_stop;
5459 }
5460
Eric Biggers9b02e492019-12-31 12:04:38 -06005461 new_size = inode->i_size - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005462 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005463 EXT4_I(inode)->i_disksize = new_size;
5464
Namjae Jeon9eb79482014-02-23 15:18:59 -05005465 up_write(&EXT4_I(inode)->i_data_sem);
5466 if (IS_SYNC(inode))
5467 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005468 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005469 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005470 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005471
5472out_stop:
5473 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005474out_mmap:
5475 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005476out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005477 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005478 return ret;
5479}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005480
Namjae Jeon331573f2015-06-09 01:55:03 -04005481/*
5482 * ext4_insert_range:
5483 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5484 * The data blocks starting from @offset to the EOF are shifted by @len
5485 * towards right to create a hole in the @inode. Inode size is increased
5486 * by len bytes.
5487 * Returns 0 on success, error otherwise.
5488 */
Eric Biggers43f81672019-12-31 12:04:40 -06005489static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon331573f2015-06-09 01:55:03 -04005490{
5491 struct super_block *sb = inode->i_sb;
5492 handle_t *handle;
5493 struct ext4_ext_path *path;
5494 struct ext4_extent *extent;
5495 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5496 unsigned int credits, ee_len;
5497 int ret = 0, depth, split_flag = 0;
5498 loff_t ioffset;
5499
5500 /*
5501 * We need to test this early because xfstests assumes that an
5502 * insert range of (0, 1) will return EOPNOTSUPP if the file
5503 * system does not support insert range.
5504 */
5505 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5506 return -EOPNOTSUPP;
5507
Eric Biggers9b02e492019-12-31 12:04:38 -06005508 /* Insert range works only on fs cluster size aligned regions. */
5509 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon331573f2015-06-09 01:55:03 -04005510 return -EINVAL;
5511
Namjae Jeon331573f2015-06-09 01:55:03 -04005512 trace_ext4_insert_range(inode, offset, len);
5513
5514 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5515 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5516
5517 /* Call ext4_force_commit to flush all data in case of data=journal */
5518 if (ext4_should_journal_data(inode)) {
5519 ret = ext4_force_commit(inode->i_sb);
5520 if (ret)
5521 return ret;
5522 }
5523
Al Viro59551022016-01-22 15:40:57 -05005524 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005525 /* Currently just for extent based files */
5526 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5527 ret = -EOPNOTSUPP;
5528 goto out_mutex;
5529 }
5530
Eric Biggers9b02e492019-12-31 12:04:38 -06005531 /* Check whether the maximum file size would be exceeded */
5532 if (len > inode->i_sb->s_maxbytes - inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005533 ret = -EFBIG;
5534 goto out_mutex;
5535 }
5536
Eric Biggers9b02e492019-12-31 12:04:38 -06005537 /* Offset must be less than i_size */
5538 if (offset >= inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005539 ret = -EINVAL;
5540 goto out_mutex;
5541 }
5542
Namjae Jeon331573f2015-06-09 01:55:03 -04005543 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005544 inode_dio_wait(inode);
5545
Jan Karaea3d7202015-12-07 14:28:03 -05005546 /*
5547 * Prevent page faults from reinstantiating pages we have released from
5548 * page cache.
5549 */
5550 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005551
5552 ret = ext4_break_layouts(inode);
5553 if (ret)
5554 goto out_mmap;
5555
Jan Kara32ebffd2015-12-07 14:31:11 -05005556 /*
5557 * Need to round down to align start offset to page size boundary
5558 * for page size > block size.
5559 */
5560 ioffset = round_down(offset, PAGE_SIZE);
5561 /* Write out all dirty pages */
5562 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5563 LLONG_MAX);
5564 if (ret)
5565 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005566 truncate_pagecache(inode, ioffset);
5567
Namjae Jeon331573f2015-06-09 01:55:03 -04005568 credits = ext4_writepage_trans_blocks(inode);
5569 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5570 if (IS_ERR(handle)) {
5571 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005572 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005573 }
5574
5575 /* Expand file to avoid data loss if there is error while shifting */
5576 inode->i_size += len;
5577 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005578 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005579 ret = ext4_mark_inode_dirty(handle, inode);
5580 if (ret)
5581 goto out_stop;
5582
5583 down_write(&EXT4_I(inode)->i_data_sem);
5584 ext4_discard_preallocations(inode);
5585
5586 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5587 if (IS_ERR(path)) {
5588 up_write(&EXT4_I(inode)->i_data_sem);
5589 goto out_stop;
5590 }
5591
5592 depth = ext_depth(inode);
5593 extent = path[depth].p_ext;
5594 if (extent) {
5595 ee_start_lblk = le32_to_cpu(extent->ee_block);
5596 ee_len = ext4_ext_get_actual_len(extent);
5597
5598 /*
5599 * If offset_lblk is not the starting block of extent, split
5600 * the extent @offset_lblk
5601 */
5602 if ((offset_lblk > ee_start_lblk) &&
5603 (offset_lblk < (ee_start_lblk + ee_len))) {
5604 if (ext4_ext_is_unwritten(extent))
5605 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5606 EXT4_EXT_MARK_UNWRIT2;
5607 ret = ext4_split_extent_at(handle, inode, &path,
5608 offset_lblk, split_flag,
5609 EXT4_EX_NOCACHE |
5610 EXT4_GET_BLOCKS_PRE_IO |
5611 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5612 }
5613
5614 ext4_ext_drop_refs(path);
5615 kfree(path);
5616 if (ret < 0) {
5617 up_write(&EXT4_I(inode)->i_data_sem);
5618 goto out_stop;
5619 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005620 } else {
5621 ext4_ext_drop_refs(path);
5622 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005623 }
5624
5625 ret = ext4_es_remove_extent(inode, offset_lblk,
5626 EXT_MAX_BLOCKS - offset_lblk);
5627 if (ret) {
5628 up_write(&EXT4_I(inode)->i_data_sem);
5629 goto out_stop;
5630 }
5631
5632 /*
5633 * if offset_lblk lies in a hole which is at start of file, use
5634 * ee_start_lblk to shift extents
5635 */
5636 ret = ext4_ext_shift_extents(inode, handle,
5637 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5638 len_lblk, SHIFT_RIGHT);
5639
5640 up_write(&EXT4_I(inode)->i_data_sem);
5641 if (IS_SYNC(inode))
5642 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005643 if (ret >= 0)
5644 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005645
5646out_stop:
5647 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005648out_mmap:
5649 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005650out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005651 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005652 return ret;
5653}
5654
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005655/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005656 * ext4_swap_extents() - Swap extents between two inodes
5657 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005658 * @inode1: First inode
5659 * @inode2: Second inode
5660 * @lblk1: Start block for first inode
5661 * @lblk2: Start block for second inode
5662 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005663 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005664 * @erp: Pointer to save error value
5665 *
5666 * This helper routine does exactly what is promise "swap extents". All other
5667 * stuff such as page-cache locking consistency, bh mapping consistency or
5668 * extent's data copying must be performed by caller.
5669 * Locking:
5670 * i_mutex is held for both inodes
5671 * i_data_sem is locked for write for both inodes
5672 * Assumptions:
5673 * All pages from requested range are locked for both inodes
5674 */
5675int
5676ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005677 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005678 ext4_lblk_t count, int unwritten, int *erp)
5679{
5680 struct ext4_ext_path *path1 = NULL;
5681 struct ext4_ext_path *path2 = NULL;
5682 int replaced_count = 0;
5683
5684 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5685 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005686 BUG_ON(!inode_is_locked(inode1));
5687 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005688
5689 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005690 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005691 return 0;
5692 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005693 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005694 return 0;
5695
5696 while (count) {
5697 struct ext4_extent *ex1, *ex2, tmp_ex;
5698 ext4_lblk_t e1_blk, e2_blk;
5699 int e1_len, e2_len, len;
5700 int split = 0;
5701
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005702 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305703 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005704 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005705 path1 = NULL;
5706 finish:
5707 count = 0;
5708 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005709 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005710 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305711 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005712 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005713 path2 = NULL;
5714 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005715 }
5716 ex1 = path1[path1->p_depth].p_ext;
5717 ex2 = path2[path2->p_depth].p_ext;
5718 /* Do we have somthing to swap ? */
5719 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005720 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005721
5722 e1_blk = le32_to_cpu(ex1->ee_block);
5723 e2_blk = le32_to_cpu(ex2->ee_block);
5724 e1_len = ext4_ext_get_actual_len(ex1);
5725 e2_len = ext4_ext_get_actual_len(ex2);
5726
5727 /* Hole handling */
5728 if (!in_range(lblk1, e1_blk, e1_len) ||
5729 !in_range(lblk2, e2_blk, e2_len)) {
5730 ext4_lblk_t next1, next2;
5731
5732 /* if hole after extent, then go to next extent */
5733 next1 = ext4_ext_next_allocated_block(path1);
5734 next2 = ext4_ext_next_allocated_block(path2);
5735 /* If hole before extent, then shift to that extent */
5736 if (e1_blk > lblk1)
5737 next1 = e1_blk;
5738 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005739 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005740 /* Do we have something to swap */
5741 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005742 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005743 /* Move to the rightest boundary */
5744 len = next1 - lblk1;
5745 if (len < next2 - lblk2)
5746 len = next2 - lblk2;
5747 if (len > count)
5748 len = count;
5749 lblk1 += len;
5750 lblk2 += len;
5751 count -= len;
5752 goto repeat;
5753 }
5754
5755 /* Prepare left boundary */
5756 if (e1_blk < lblk1) {
5757 split = 1;
5758 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005759 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005760 if (unlikely(*erp))
5761 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005762 }
5763 if (e2_blk < lblk2) {
5764 split = 1;
5765 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005766 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005767 if (unlikely(*erp))
5768 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005769 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005770 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005771 * path must to be revalidated. */
5772 if (split)
5773 goto repeat;
5774
5775 /* Prepare right boundary */
5776 len = count;
5777 if (len > e1_blk + e1_len - lblk1)
5778 len = e1_blk + e1_len - lblk1;
5779 if (len > e2_blk + e2_len - lblk2)
5780 len = e2_blk + e2_len - lblk2;
5781
5782 if (len != e1_len) {
5783 split = 1;
5784 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005785 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005786 if (unlikely(*erp))
5787 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005788 }
5789 if (len != e2_len) {
5790 split = 1;
5791 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005792 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005793 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005794 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005795 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005796 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005797 * path must to be revalidated. */
5798 if (split)
5799 goto repeat;
5800
5801 BUG_ON(e2_len != e1_len);
5802 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005803 if (unlikely(*erp))
5804 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005805 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005806 if (unlikely(*erp))
5807 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005808
5809 /* Both extents are fully inside boundaries. Swap it now */
5810 tmp_ex = *ex1;
5811 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5812 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5813 ex1->ee_len = cpu_to_le16(e2_len);
5814 ex2->ee_len = cpu_to_le16(e1_len);
5815 if (unwritten)
5816 ext4_ext_mark_unwritten(ex2);
5817 if (ext4_ext_is_unwritten(&tmp_ex))
5818 ext4_ext_mark_unwritten(ex1);
5819
5820 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5821 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5822 *erp = ext4_ext_dirty(handle, inode2, path2 +
5823 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005824 if (unlikely(*erp))
5825 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005826 *erp = ext4_ext_dirty(handle, inode1, path1 +
5827 path1->p_depth);
5828 /*
5829 * Looks scarry ah..? second inode already points to new blocks,
5830 * and it was successfully dirtied. But luckily error may happen
5831 * only due to journal error, so full transaction will be
5832 * aborted anyway.
5833 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005834 if (unlikely(*erp))
5835 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005836 lblk1 += len;
5837 lblk2 += len;
5838 replaced_count += len;
5839 count -= len;
5840
5841 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005842 ext4_ext_drop_refs(path1);
5843 kfree(path1);
5844 ext4_ext_drop_refs(path2);
5845 kfree(path2);
5846 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005847 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005848 return replaced_count;
5849}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005850
5851/*
5852 * ext4_clu_mapped - determine whether any block in a logical cluster has
5853 * been mapped to a physical cluster
5854 *
5855 * @inode - file containing the logical cluster
5856 * @lclu - logical cluster of interest
5857 *
5858 * Returns 1 if any block in the logical cluster is mapped, signifying
5859 * that a physical cluster has been allocated for it. Otherwise,
5860 * returns 0. Can also return negative error codes. Derived from
5861 * ext4_ext_map_blocks().
5862 */
5863int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5864{
5865 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5866 struct ext4_ext_path *path;
5867 int depth, mapped = 0, err = 0;
5868 struct ext4_extent *extent;
5869 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5870
5871 /* search for the extent closest to the first block in the cluster */
5872 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5873 if (IS_ERR(path)) {
5874 err = PTR_ERR(path);
5875 path = NULL;
5876 goto out;
5877 }
5878
5879 depth = ext_depth(inode);
5880
5881 /*
5882 * A consistent leaf must not be empty. This situation is possible,
5883 * though, _during_ tree modification, and it's why an assert can't
5884 * be put in ext4_find_extent().
5885 */
5886 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
5887 EXT4_ERROR_INODE(inode,
5888 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
5889 (unsigned long) EXT4_C2B(sbi, lclu),
5890 depth, path[depth].p_block);
5891 err = -EFSCORRUPTED;
5892 goto out;
5893 }
5894
5895 extent = path[depth].p_ext;
5896
5897 /* can't be mapped if the extent tree is empty */
5898 if (extent == NULL)
5899 goto out;
5900
5901 first_lblk = le32_to_cpu(extent->ee_block);
5902 first_lclu = EXT4_B2C(sbi, first_lblk);
5903
5904 /*
5905 * Three possible outcomes at this point - found extent spanning
5906 * the target cluster, to the left of the target cluster, or to the
5907 * right of the target cluster. The first two cases are handled here.
5908 * The last case indicates the target cluster is not mapped.
5909 */
5910 if (lclu >= first_lclu) {
5911 last_lclu = EXT4_B2C(sbi, first_lblk +
5912 ext4_ext_get_actual_len(extent) - 1);
5913 if (lclu <= last_lclu) {
5914 mapped = 1;
5915 } else {
5916 first_lblk = ext4_ext_next_allocated_block(path);
5917 first_lclu = EXT4_B2C(sbi, first_lblk);
5918 if (lclu == first_lclu)
5919 mapped = 1;
5920 }
5921 }
5922
5923out:
5924 ext4_ext_drop_refs(path);
5925 kfree(path);
5926
5927 return err ? err : mapped;
5928}